edac_mc_sysfs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  2. * edac_mc kernel module
  3. * (C) 2005-2007 Linux Networx (http://lnxi.com)
  4. *
  5. * This file may be distributed under the terms of the
  6. * GNU General Public License.
  7. *
  8. * Written Doug Thompson <norsk5@xmission.com> www.softwarebitmaker.com
  9. *
  10. */
  11. #include <linux/ctype.h>
  12. #include <linux/slab.h>
  13. #include <linux/bug.h>
  14. #include "edac_core.h"
  15. #include "edac_module.h"
  16. /* MC EDAC Controls, setable by module parameter, and sysfs */
  17. static int edac_mc_log_ue = 1;
  18. static int edac_mc_log_ce = 1;
  19. static int edac_mc_panic_on_ue;
  20. static int edac_mc_poll_msec = 1000;
  21. /* Getter functions for above */
  22. int edac_mc_get_log_ue(void)
  23. {
  24. return edac_mc_log_ue;
  25. }
  26. int edac_mc_get_log_ce(void)
  27. {
  28. return edac_mc_log_ce;
  29. }
  30. int edac_mc_get_panic_on_ue(void)
  31. {
  32. return edac_mc_panic_on_ue;
  33. }
  34. /* this is temporary */
  35. int edac_mc_get_poll_msec(void)
  36. {
  37. return edac_mc_poll_msec;
  38. }
  39. static int edac_set_poll_msec(const char *val, struct kernel_param *kp)
  40. {
  41. long l;
  42. int ret;
  43. if (!val)
  44. return -EINVAL;
  45. ret = strict_strtol(val, 0, &l);
  46. if (ret == -EINVAL || ((int)l != l))
  47. return -EINVAL;
  48. *((int *)kp->arg) = l;
  49. /* notify edac_mc engine to reset the poll period */
  50. edac_mc_reset_delay_period(l);
  51. return 0;
  52. }
  53. /* Parameter declarations for above */
  54. module_param(edac_mc_panic_on_ue, int, 0644);
  55. MODULE_PARM_DESC(edac_mc_panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
  56. module_param(edac_mc_log_ue, int, 0644);
  57. MODULE_PARM_DESC(edac_mc_log_ue,
  58. "Log uncorrectable error to console: 0=off 1=on");
  59. module_param(edac_mc_log_ce, int, 0644);
  60. MODULE_PARM_DESC(edac_mc_log_ce,
  61. "Log correctable error to console: 0=off 1=on");
  62. module_param_call(edac_mc_poll_msec, edac_set_poll_msec, param_get_int,
  63. &edac_mc_poll_msec, 0644);
  64. MODULE_PARM_DESC(edac_mc_poll_msec, "Polling period in milliseconds");
  65. /*
  66. * various constants for Memory Controllers
  67. */
  68. static const char *mem_types[] = {
  69. [MEM_EMPTY] = "Empty",
  70. [MEM_RESERVED] = "Reserved",
  71. [MEM_UNKNOWN] = "Unknown",
  72. [MEM_FPM] = "FPM",
  73. [MEM_EDO] = "EDO",
  74. [MEM_BEDO] = "BEDO",
  75. [MEM_SDR] = "Unbuffered-SDR",
  76. [MEM_RDR] = "Registered-SDR",
  77. [MEM_DDR] = "Unbuffered-DDR",
  78. [MEM_RDDR] = "Registered-DDR",
  79. [MEM_RMBS] = "RMBS",
  80. [MEM_DDR2] = "Unbuffered-DDR2",
  81. [MEM_FB_DDR2] = "FullyBuffered-DDR2",
  82. [MEM_RDDR2] = "Registered-DDR2",
  83. [MEM_XDR] = "XDR",
  84. [MEM_DDR3] = "Unbuffered-DDR3",
  85. [MEM_RDDR3] = "Registered-DDR3"
  86. };
  87. static const char *dev_types[] = {
  88. [DEV_UNKNOWN] = "Unknown",
  89. [DEV_X1] = "x1",
  90. [DEV_X2] = "x2",
  91. [DEV_X4] = "x4",
  92. [DEV_X8] = "x8",
  93. [DEV_X16] = "x16",
  94. [DEV_X32] = "x32",
  95. [DEV_X64] = "x64"
  96. };
  97. static const char *edac_caps[] = {
  98. [EDAC_UNKNOWN] = "Unknown",
  99. [EDAC_NONE] = "None",
  100. [EDAC_RESERVED] = "Reserved",
  101. [EDAC_PARITY] = "PARITY",
  102. [EDAC_EC] = "EC",
  103. [EDAC_SECDED] = "SECDED",
  104. [EDAC_S2ECD2ED] = "S2ECD2ED",
  105. [EDAC_S4ECD4ED] = "S4ECD4ED",
  106. [EDAC_S8ECD8ED] = "S8ECD8ED",
  107. [EDAC_S16ECD16ED] = "S16ECD16ED"
  108. };
  109. /* EDAC sysfs CSROW data structures and methods
  110. */
  111. /* Set of more default csrow<id> attribute show/store functions */
  112. static ssize_t csrow_ue_count_show(struct csrow_info *csrow, char *data,
  113. int private)
  114. {
  115. return sprintf(data, "%u\n", csrow->ue_count);
  116. }
  117. static ssize_t csrow_ce_count_show(struct csrow_info *csrow, char *data,
  118. int private)
  119. {
  120. return sprintf(data, "%u\n", csrow->ce_count);
  121. }
  122. static ssize_t csrow_size_show(struct csrow_info *csrow, char *data,
  123. int private)
  124. {
  125. return sprintf(data, "%u\n", PAGES_TO_MiB(csrow->nr_pages));
  126. }
  127. static ssize_t csrow_mem_type_show(struct csrow_info *csrow, char *data,
  128. int private)
  129. {
  130. return sprintf(data, "%s\n", mem_types[csrow->mtype]);
  131. }
  132. static ssize_t csrow_dev_type_show(struct csrow_info *csrow, char *data,
  133. int private)
  134. {
  135. return sprintf(data, "%s\n", dev_types[csrow->dtype]);
  136. }
  137. static ssize_t csrow_edac_mode_show(struct csrow_info *csrow, char *data,
  138. int private)
  139. {
  140. return sprintf(data, "%s\n", edac_caps[csrow->edac_mode]);
  141. }
  142. /* show/store functions for DIMM Label attributes */
  143. static ssize_t channel_dimm_label_show(struct csrow_info *csrow,
  144. char *data, int channel)
  145. {
  146. /* if field has not been initialized, there is nothing to send */
  147. if (!csrow->channels[channel].label[0])
  148. return 0;
  149. return snprintf(data, EDAC_MC_LABEL_LEN, "%s\n",
  150. csrow->channels[channel].label);
  151. }
  152. static ssize_t channel_dimm_label_store(struct csrow_info *csrow,
  153. const char *data,
  154. size_t count, int channel)
  155. {
  156. ssize_t max_size = 0;
  157. max_size = min((ssize_t) count, (ssize_t) EDAC_MC_LABEL_LEN - 1);
  158. strncpy(csrow->channels[channel].label, data, max_size);
  159. csrow->channels[channel].label[max_size] = '\0';
  160. return max_size;
  161. }
  162. /* show function for dynamic chX_ce_count attribute */
  163. static ssize_t channel_ce_count_show(struct csrow_info *csrow,
  164. char *data, int channel)
  165. {
  166. return sprintf(data, "%u\n", csrow->channels[channel].ce_count);
  167. }
  168. /* csrow specific attribute structure */
  169. struct csrowdev_attribute {
  170. struct attribute attr;
  171. ssize_t(*show) (struct csrow_info *, char *, int);
  172. ssize_t(*store) (struct csrow_info *, const char *, size_t, int);
  173. int private;
  174. };
  175. #define to_csrow(k) container_of(k, struct csrow_info, kobj)
  176. #define to_csrowdev_attr(a) container_of(a, struct csrowdev_attribute, attr)
  177. /* Set of show/store higher level functions for default csrow attributes */
  178. static ssize_t csrowdev_show(struct kobject *kobj,
  179. struct attribute *attr, char *buffer)
  180. {
  181. struct csrow_info *csrow = to_csrow(kobj);
  182. struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
  183. if (csrowdev_attr->show)
  184. return csrowdev_attr->show(csrow,
  185. buffer, csrowdev_attr->private);
  186. return -EIO;
  187. }
  188. static ssize_t csrowdev_store(struct kobject *kobj, struct attribute *attr,
  189. const char *buffer, size_t count)
  190. {
  191. struct csrow_info *csrow = to_csrow(kobj);
  192. struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
  193. if (csrowdev_attr->store)
  194. return csrowdev_attr->store(csrow,
  195. buffer,
  196. count, csrowdev_attr->private);
  197. return -EIO;
  198. }
  199. static const struct sysfs_ops csrowfs_ops = {
  200. .show = csrowdev_show,
  201. .store = csrowdev_store
  202. };
  203. #define CSROWDEV_ATTR(_name,_mode,_show,_store,_private) \
  204. static struct csrowdev_attribute attr_##_name = { \
  205. .attr = {.name = __stringify(_name), .mode = _mode }, \
  206. .show = _show, \
  207. .store = _store, \
  208. .private = _private, \
  209. };
  210. /* default cwrow<id>/attribute files */
  211. CSROWDEV_ATTR(size_mb, S_IRUGO, csrow_size_show, NULL, 0);
  212. CSROWDEV_ATTR(dev_type, S_IRUGO, csrow_dev_type_show, NULL, 0);
  213. CSROWDEV_ATTR(mem_type, S_IRUGO, csrow_mem_type_show, NULL, 0);
  214. CSROWDEV_ATTR(edac_mode, S_IRUGO, csrow_edac_mode_show, NULL, 0);
  215. CSROWDEV_ATTR(ue_count, S_IRUGO, csrow_ue_count_show, NULL, 0);
  216. CSROWDEV_ATTR(ce_count, S_IRUGO, csrow_ce_count_show, NULL, 0);
  217. /* default attributes of the CSROW<id> object */
  218. static struct csrowdev_attribute *default_csrow_attr[] = {
  219. &attr_dev_type,
  220. &attr_mem_type,
  221. &attr_edac_mode,
  222. &attr_size_mb,
  223. &attr_ue_count,
  224. &attr_ce_count,
  225. NULL,
  226. };
  227. /* possible dynamic channel DIMM Label attribute files */
  228. CSROWDEV_ATTR(ch0_dimm_label, S_IRUGO | S_IWUSR,
  229. channel_dimm_label_show, channel_dimm_label_store, 0);
  230. CSROWDEV_ATTR(ch1_dimm_label, S_IRUGO | S_IWUSR,
  231. channel_dimm_label_show, channel_dimm_label_store, 1);
  232. CSROWDEV_ATTR(ch2_dimm_label, S_IRUGO | S_IWUSR,
  233. channel_dimm_label_show, channel_dimm_label_store, 2);
  234. CSROWDEV_ATTR(ch3_dimm_label, S_IRUGO | S_IWUSR,
  235. channel_dimm_label_show, channel_dimm_label_store, 3);
  236. CSROWDEV_ATTR(ch4_dimm_label, S_IRUGO | S_IWUSR,
  237. channel_dimm_label_show, channel_dimm_label_store, 4);
  238. CSROWDEV_ATTR(ch5_dimm_label, S_IRUGO | S_IWUSR,
  239. channel_dimm_label_show, channel_dimm_label_store, 5);
  240. /* Total possible dynamic DIMM Label attribute file table */
  241. static struct csrowdev_attribute *dynamic_csrow_dimm_attr[] = {
  242. &attr_ch0_dimm_label,
  243. &attr_ch1_dimm_label,
  244. &attr_ch2_dimm_label,
  245. &attr_ch3_dimm_label,
  246. &attr_ch4_dimm_label,
  247. &attr_ch5_dimm_label
  248. };
  249. /* possible dynamic channel ce_count attribute files */
  250. CSROWDEV_ATTR(ch0_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 0);
  251. CSROWDEV_ATTR(ch1_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 1);
  252. CSROWDEV_ATTR(ch2_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 2);
  253. CSROWDEV_ATTR(ch3_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 3);
  254. CSROWDEV_ATTR(ch4_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 4);
  255. CSROWDEV_ATTR(ch5_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 5);
  256. /* Total possible dynamic ce_count attribute file table */
  257. static struct csrowdev_attribute *dynamic_csrow_ce_count_attr[] = {
  258. &attr_ch0_ce_count,
  259. &attr_ch1_ce_count,
  260. &attr_ch2_ce_count,
  261. &attr_ch3_ce_count,
  262. &attr_ch4_ce_count,
  263. &attr_ch5_ce_count
  264. };
  265. #define EDAC_NR_CHANNELS 6
  266. /* Create dynamic CHANNEL files, indexed by 'chan', under specifed CSROW */
  267. static int edac_create_channel_files(struct kobject *kobj, int chan)
  268. {
  269. int err = -ENODEV;
  270. if (chan >= EDAC_NR_CHANNELS)
  271. return err;
  272. /* create the DIMM label attribute file */
  273. err = sysfs_create_file(kobj,
  274. (struct attribute *)
  275. dynamic_csrow_dimm_attr[chan]);
  276. if (!err) {
  277. /* create the CE Count attribute file */
  278. err = sysfs_create_file(kobj,
  279. (struct attribute *)
  280. dynamic_csrow_ce_count_attr[chan]);
  281. } else {
  282. debugf1("%s() dimm labels and ce_count files created",
  283. __func__);
  284. }
  285. return err;
  286. }
  287. /* No memory to release for this kobj */
  288. static void edac_csrow_instance_release(struct kobject *kobj)
  289. {
  290. struct mem_ctl_info *mci;
  291. struct csrow_info *cs;
  292. debugf1("%s()\n", __func__);
  293. cs = container_of(kobj, struct csrow_info, kobj);
  294. mci = cs->mci;
  295. kobject_put(&mci->edac_mci_kobj);
  296. }
  297. /* the kobj_type instance for a CSROW */
  298. static struct kobj_type ktype_csrow = {
  299. .release = edac_csrow_instance_release,
  300. .sysfs_ops = &csrowfs_ops,
  301. .default_attrs = (struct attribute **)default_csrow_attr,
  302. };
  303. /* Create a CSROW object under specifed edac_mc_device */
  304. static int edac_create_csrow_object(struct mem_ctl_info *mci,
  305. struct csrow_info *csrow, int index)
  306. {
  307. struct kobject *kobj_mci = &mci->edac_mci_kobj;
  308. struct kobject *kobj;
  309. int chan;
  310. int err;
  311. /* generate ..../edac/mc/mc<id>/csrow<index> */
  312. memset(&csrow->kobj, 0, sizeof(csrow->kobj));
  313. csrow->mci = mci; /* include container up link */
  314. /* bump the mci instance's kobject's ref count */
  315. kobj = kobject_get(&mci->edac_mci_kobj);
  316. if (!kobj) {
  317. err = -ENODEV;
  318. goto err_out;
  319. }
  320. /* Instanstiate the csrow object */
  321. err = kobject_init_and_add(&csrow->kobj, &ktype_csrow, kobj_mci,
  322. "csrow%d", index);
  323. if (err)
  324. goto err_release_top_kobj;
  325. /* At this point, to release a csrow kobj, one must
  326. * call the kobject_put and allow that tear down
  327. * to work the releasing
  328. */
  329. /* Create the dyanmic attribute files on this csrow,
  330. * namely, the DIMM labels and the channel ce_count
  331. */
  332. for (chan = 0; chan < csrow->nr_channels; chan++) {
  333. err = edac_create_channel_files(&csrow->kobj, chan);
  334. if (err) {
  335. /* special case the unregister here */
  336. kobject_put(&csrow->kobj);
  337. goto err_out;
  338. }
  339. }
  340. kobject_uevent(&csrow->kobj, KOBJ_ADD);
  341. return 0;
  342. /* error unwind stack */
  343. err_release_top_kobj:
  344. kobject_put(&mci->edac_mci_kobj);
  345. err_out:
  346. return err;
  347. }
  348. /* default sysfs methods and data structures for the main MCI kobject */
  349. static ssize_t mci_reset_counters_store(struct mem_ctl_info *mci,
  350. const char *data, size_t count)
  351. {
  352. int row, chan;
  353. mci->ue_noinfo_count = 0;
  354. mci->ce_noinfo_count = 0;
  355. mci->ue_count = 0;
  356. mci->ce_count = 0;
  357. for (row = 0; row < mci->nr_csrows; row++) {
  358. struct csrow_info *ri = &mci->csrows[row];
  359. ri->ue_count = 0;
  360. ri->ce_count = 0;
  361. for (chan = 0; chan < ri->nr_channels; chan++)
  362. ri->channels[chan].ce_count = 0;
  363. }
  364. mci->start_time = jiffies;
  365. return count;
  366. }
  367. /* memory scrubbing */
  368. static ssize_t mci_sdram_scrub_rate_store(struct mem_ctl_info *mci,
  369. const char *data, size_t count)
  370. {
  371. unsigned long bandwidth = 0;
  372. int err;
  373. if (!mci->set_sdram_scrub_rate) {
  374. edac_printk(KERN_WARNING, EDAC_MC,
  375. "Memory scrub rate setting not implemented!\n");
  376. return -EINVAL;
  377. }
  378. if (strict_strtoul(data, 10, &bandwidth) < 0)
  379. return -EINVAL;
  380. err = mci->set_sdram_scrub_rate(mci, (u32)bandwidth);
  381. if (err) {
  382. edac_printk(KERN_DEBUG, EDAC_MC,
  383. "Failed setting scrub rate to %lu\n", bandwidth);
  384. return -EINVAL;
  385. }
  386. else {
  387. edac_printk(KERN_DEBUG, EDAC_MC,
  388. "Scrub rate set to: %lu\n", bandwidth);
  389. return count;
  390. }
  391. }
  392. static ssize_t mci_sdram_scrub_rate_show(struct mem_ctl_info *mci, char *data)
  393. {
  394. u32 bandwidth = 0;
  395. int err;
  396. if (!mci->get_sdram_scrub_rate) {
  397. edac_printk(KERN_WARNING, EDAC_MC,
  398. "Memory scrub rate reading not implemented\n");
  399. return -EINVAL;
  400. }
  401. err = mci->get_sdram_scrub_rate(mci, &bandwidth);
  402. if (err) {
  403. edac_printk(KERN_DEBUG, EDAC_MC, "Error reading scrub rate\n");
  404. return err;
  405. }
  406. else {
  407. edac_printk(KERN_DEBUG, EDAC_MC,
  408. "Read scrub rate: %d\n", bandwidth);
  409. return sprintf(data, "%d\n", bandwidth);
  410. }
  411. }
  412. /* default attribute files for the MCI object */
  413. static ssize_t mci_ue_count_show(struct mem_ctl_info *mci, char *data)
  414. {
  415. return sprintf(data, "%d\n", mci->ue_count);
  416. }
  417. static ssize_t mci_ce_count_show(struct mem_ctl_info *mci, char *data)
  418. {
  419. return sprintf(data, "%d\n", mci->ce_count);
  420. }
  421. static ssize_t mci_ce_noinfo_show(struct mem_ctl_info *mci, char *data)
  422. {
  423. return sprintf(data, "%d\n", mci->ce_noinfo_count);
  424. }
  425. static ssize_t mci_ue_noinfo_show(struct mem_ctl_info *mci, char *data)
  426. {
  427. return sprintf(data, "%d\n", mci->ue_noinfo_count);
  428. }
  429. static ssize_t mci_seconds_show(struct mem_ctl_info *mci, char *data)
  430. {
  431. return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
  432. }
  433. static ssize_t mci_ctl_name_show(struct mem_ctl_info *mci, char *data)
  434. {
  435. return sprintf(data, "%s\n", mci->ctl_name);
  436. }
  437. static ssize_t mci_size_mb_show(struct mem_ctl_info *mci, char *data)
  438. {
  439. int total_pages, csrow_idx;
  440. for (total_pages = csrow_idx = 0; csrow_idx < mci->nr_csrows;
  441. csrow_idx++) {
  442. struct csrow_info *csrow = &mci->csrows[csrow_idx];
  443. if (!csrow->nr_pages)
  444. continue;
  445. total_pages += csrow->nr_pages;
  446. }
  447. return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
  448. }
  449. #define to_mci(k) container_of(k, struct mem_ctl_info, edac_mci_kobj)
  450. #define to_mcidev_attr(a) container_of(a,struct mcidev_sysfs_attribute,attr)
  451. /* MCI show/store functions for top most object */
  452. static ssize_t mcidev_show(struct kobject *kobj, struct attribute *attr,
  453. char *buffer)
  454. {
  455. struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
  456. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  457. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  458. if (mcidev_attr->show)
  459. return mcidev_attr->show(mem_ctl_info, buffer);
  460. return -EIO;
  461. }
  462. static ssize_t mcidev_store(struct kobject *kobj, struct attribute *attr,
  463. const char *buffer, size_t count)
  464. {
  465. struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
  466. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  467. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  468. if (mcidev_attr->store)
  469. return mcidev_attr->store(mem_ctl_info, buffer, count);
  470. return -EIO;
  471. }
  472. /* Intermediate show/store table */
  473. static const struct sysfs_ops mci_ops = {
  474. .show = mcidev_show,
  475. .store = mcidev_store
  476. };
  477. #define MCIDEV_ATTR(_name,_mode,_show,_store) \
  478. static struct mcidev_sysfs_attribute mci_attr_##_name = { \
  479. .attr = {.name = __stringify(_name), .mode = _mode }, \
  480. .show = _show, \
  481. .store = _store, \
  482. };
  483. /* default Control file */
  484. MCIDEV_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
  485. /* default Attribute files */
  486. MCIDEV_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
  487. MCIDEV_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
  488. MCIDEV_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
  489. MCIDEV_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
  490. MCIDEV_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
  491. MCIDEV_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
  492. MCIDEV_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
  493. /* memory scrubber attribute file */
  494. MCIDEV_ATTR(sdram_scrub_rate, S_IRUGO | S_IWUSR, mci_sdram_scrub_rate_show,
  495. mci_sdram_scrub_rate_store);
  496. static struct mcidev_sysfs_attribute *mci_attr[] = {
  497. &mci_attr_reset_counters,
  498. &mci_attr_mc_name,
  499. &mci_attr_size_mb,
  500. &mci_attr_seconds_since_reset,
  501. &mci_attr_ue_noinfo_count,
  502. &mci_attr_ce_noinfo_count,
  503. &mci_attr_ue_count,
  504. &mci_attr_ce_count,
  505. &mci_attr_sdram_scrub_rate,
  506. NULL
  507. };
  508. /*
  509. * Release of a MC controlling instance
  510. *
  511. * each MC control instance has the following resources upon entry:
  512. * a) a ref count on the top memctl kobj
  513. * b) a ref count on this module
  514. *
  515. * this function must decrement those ref counts and then
  516. * issue a free on the instance's memory
  517. */
  518. static void edac_mci_control_release(struct kobject *kobj)
  519. {
  520. struct mem_ctl_info *mci;
  521. mci = to_mci(kobj);
  522. debugf0("%s() mci instance idx=%d releasing\n", __func__, mci->mc_idx);
  523. /* decrement the module ref count */
  524. module_put(mci->owner);
  525. /* free the mci instance memory here */
  526. kfree(mci);
  527. }
  528. static struct kobj_type ktype_mci = {
  529. .release = edac_mci_control_release,
  530. .sysfs_ops = &mci_ops,
  531. .default_attrs = (struct attribute **)mci_attr,
  532. };
  533. /* EDAC memory controller sysfs kset:
  534. * /sys/devices/system/edac/mc
  535. */
  536. static struct kset *mc_kset;
  537. /*
  538. * edac_mc_register_sysfs_main_kobj
  539. *
  540. * setups and registers the main kobject for each mci
  541. */
  542. int edac_mc_register_sysfs_main_kobj(struct mem_ctl_info *mci)
  543. {
  544. struct kobject *kobj_mci;
  545. int err;
  546. debugf1("%s()\n", __func__);
  547. kobj_mci = &mci->edac_mci_kobj;
  548. /* Init the mci's kobject */
  549. memset(kobj_mci, 0, sizeof(*kobj_mci));
  550. /* Record which module 'owns' this control structure
  551. * and bump the ref count of the module
  552. */
  553. mci->owner = THIS_MODULE;
  554. /* bump ref count on this module */
  555. if (!try_module_get(mci->owner)) {
  556. err = -ENODEV;
  557. goto fail_out;
  558. }
  559. /* this instance become part of the mc_kset */
  560. kobj_mci->kset = mc_kset;
  561. /* register the mc<id> kobject to the mc_kset */
  562. err = kobject_init_and_add(kobj_mci, &ktype_mci, NULL,
  563. "mc%d", mci->mc_idx);
  564. if (err) {
  565. debugf1("%s()Failed to register '.../edac/mc%d'\n",
  566. __func__, mci->mc_idx);
  567. goto kobj_reg_fail;
  568. }
  569. kobject_uevent(kobj_mci, KOBJ_ADD);
  570. /* At this point, to 'free' the control struct,
  571. * edac_mc_unregister_sysfs_main_kobj() must be used
  572. */
  573. debugf1("%s() Registered '.../edac/mc%d' kobject\n",
  574. __func__, mci->mc_idx);
  575. return 0;
  576. /* Error exit stack */
  577. kobj_reg_fail:
  578. module_put(mci->owner);
  579. fail_out:
  580. return err;
  581. }
  582. /*
  583. * edac_mc_register_sysfs_main_kobj
  584. *
  585. * tears down and the main mci kobject from the mc_kset
  586. */
  587. void edac_mc_unregister_sysfs_main_kobj(struct mem_ctl_info *mci)
  588. {
  589. /* delete the kobj from the mc_kset */
  590. kobject_put(&mci->edac_mci_kobj);
  591. }
  592. #define EDAC_DEVICE_SYMLINK "device"
  593. #define grp_to_mci(k) (container_of(k, struct mcidev_sysfs_group_kobj, kobj)->mci)
  594. /* MCI show/store functions for top most object */
  595. static ssize_t inst_grp_show(struct kobject *kobj, struct attribute *attr,
  596. char *buffer)
  597. {
  598. struct mem_ctl_info *mem_ctl_info = grp_to_mci(kobj);
  599. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  600. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  601. if (mcidev_attr->show)
  602. return mcidev_attr->show(mem_ctl_info, buffer);
  603. return -EIO;
  604. }
  605. static ssize_t inst_grp_store(struct kobject *kobj, struct attribute *attr,
  606. const char *buffer, size_t count)
  607. {
  608. struct mem_ctl_info *mem_ctl_info = grp_to_mci(kobj);
  609. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  610. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  611. if (mcidev_attr->store)
  612. return mcidev_attr->store(mem_ctl_info, buffer, count);
  613. return -EIO;
  614. }
  615. /* No memory to release for this kobj */
  616. static void edac_inst_grp_release(struct kobject *kobj)
  617. {
  618. struct mcidev_sysfs_group_kobj *grp;
  619. struct mem_ctl_info *mci;
  620. debugf1("%s()\n", __func__);
  621. grp = container_of(kobj, struct mcidev_sysfs_group_kobj, kobj);
  622. mci = grp->mci;
  623. kobject_put(&mci->edac_mci_kobj);
  624. }
  625. /* Intermediate show/store table */
  626. static struct sysfs_ops inst_grp_ops = {
  627. .show = inst_grp_show,
  628. .store = inst_grp_store
  629. };
  630. /* the kobj_type instance for a instance group */
  631. static struct kobj_type ktype_inst_grp = {
  632. .release = edac_inst_grp_release,
  633. .sysfs_ops = &inst_grp_ops,
  634. };
  635. /*
  636. * edac_create_mci_instance_attributes
  637. * create MC driver specific attributes bellow an specified kobj
  638. * This routine calls itself recursively, in order to create an entire
  639. * object tree.
  640. */
  641. static int edac_create_mci_instance_attributes(struct mem_ctl_info *mci,
  642. struct mcidev_sysfs_attribute *sysfs_attrib,
  643. struct kobject *kobj)
  644. {
  645. int err;
  646. debugf1("%s()\n", __func__);
  647. while (sysfs_attrib) {
  648. if (sysfs_attrib->grp) {
  649. struct mcidev_sysfs_group_kobj *grp_kobj;
  650. grp_kobj = kzalloc(sizeof(*grp_kobj), GFP_KERNEL);
  651. if (!grp_kobj)
  652. return -ENOMEM;
  653. list_add_tail(&grp_kobj->list, &mci->grp_kobj_list);
  654. grp_kobj->grp = sysfs_attrib->grp;
  655. grp_kobj->mci = mci;
  656. debugf0("%s() grp %s, mci %p\n", __func__,
  657. sysfs_attrib->grp->name, mci);
  658. err = kobject_init_and_add(&grp_kobj->kobj,
  659. &ktype_inst_grp,
  660. &mci->edac_mci_kobj,
  661. sysfs_attrib->grp->name);
  662. if (err)
  663. return err;
  664. err = edac_create_mci_instance_attributes(mci,
  665. grp_kobj->grp->mcidev_attr,
  666. &grp_kobj->kobj);
  667. if (err)
  668. return err;
  669. } else if (sysfs_attrib->attr.name) {
  670. debugf0("%s() file %s\n", __func__,
  671. sysfs_attrib->attr.name);
  672. err = sysfs_create_file(kobj, &sysfs_attrib->attr);
  673. } else
  674. break;
  675. if (err) {
  676. return err;
  677. }
  678. sysfs_attrib++;
  679. }
  680. return 0;
  681. }
  682. /*
  683. * edac_remove_mci_instance_attributes
  684. * remove MC driver specific attributes at the topmost level
  685. * directory of this mci instance.
  686. */
  687. static void edac_remove_mci_instance_attributes(struct mem_ctl_info *mci,
  688. struct mcidev_sysfs_attribute *sysfs_attrib,
  689. struct kobject *kobj, int count)
  690. {
  691. struct mcidev_sysfs_group_kobj *grp_kobj, *tmp;
  692. debugf1("%s()\n", __func__);
  693. /*
  694. * loop if there are attributes and until we hit a NULL entry
  695. * Remove first all the atributes
  696. */
  697. while (sysfs_attrib) {
  698. if (sysfs_attrib->grp) {
  699. list_for_each_entry(grp_kobj, &mci->grp_kobj_list,
  700. list)
  701. if (grp_kobj->grp == sysfs_attrib->grp)
  702. edac_remove_mci_instance_attributes(mci,
  703. grp_kobj->grp->mcidev_attr,
  704. &grp_kobj->kobj, count + 1);
  705. } else if (sysfs_attrib->attr.name) {
  706. debugf0("%s() file %s\n", __func__,
  707. sysfs_attrib->attr.name);
  708. sysfs_remove_file(kobj, &sysfs_attrib->attr);
  709. } else
  710. break;
  711. sysfs_attrib++;
  712. }
  713. /*
  714. * Now that all attributes got removed, it is save to remove all groups
  715. */
  716. if (!count)
  717. list_for_each_entry_safe(grp_kobj, tmp, &mci->grp_kobj_list,
  718. list) {
  719. debugf0("%s() grp %s\n", __func__, grp_kobj->grp->name);
  720. kobject_put(&grp_kobj->kobj);
  721. }
  722. }
  723. /*
  724. * Create a new Memory Controller kobject instance,
  725. * mc<id> under the 'mc' directory
  726. *
  727. * Return:
  728. * 0 Success
  729. * !0 Failure
  730. */
  731. int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
  732. {
  733. int i;
  734. int err;
  735. struct csrow_info *csrow;
  736. struct kobject *kobj_mci = &mci->edac_mci_kobj;
  737. debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
  738. INIT_LIST_HEAD(&mci->grp_kobj_list);
  739. /* create a symlink for the device */
  740. err = sysfs_create_link(kobj_mci, &mci->dev->kobj,
  741. EDAC_DEVICE_SYMLINK);
  742. if (err) {
  743. debugf1("%s() failure to create symlink\n", __func__);
  744. goto fail0;
  745. }
  746. /* If the low level driver desires some attributes,
  747. * then create them now for the driver.
  748. */
  749. if (mci->mc_driver_sysfs_attributes) {
  750. err = edac_create_mci_instance_attributes(mci,
  751. mci->mc_driver_sysfs_attributes,
  752. &mci->edac_mci_kobj);
  753. if (err) {
  754. debugf1("%s() failure to create mci attributes\n",
  755. __func__);
  756. goto fail0;
  757. }
  758. }
  759. /* Make directories for each CSROW object under the mc<id> kobject
  760. */
  761. for (i = 0; i < mci->nr_csrows; i++) {
  762. csrow = &mci->csrows[i];
  763. /* Only expose populated CSROWs */
  764. if (csrow->nr_pages > 0) {
  765. err = edac_create_csrow_object(mci, csrow, i);
  766. if (err) {
  767. debugf1("%s() failure: create csrow %d obj\n",
  768. __func__, i);
  769. goto fail1;
  770. }
  771. }
  772. }
  773. return 0;
  774. /* CSROW error: backout what has already been registered, */
  775. fail1:
  776. for (i--; i >= 0; i--) {
  777. if (csrow->nr_pages > 0) {
  778. kobject_put(&mci->csrows[i].kobj);
  779. }
  780. }
  781. /* remove the mci instance's attributes, if any */
  782. edac_remove_mci_instance_attributes(mci,
  783. mci->mc_driver_sysfs_attributes, &mci->edac_mci_kobj, 0);
  784. /* remove the symlink */
  785. sysfs_remove_link(kobj_mci, EDAC_DEVICE_SYMLINK);
  786. fail0:
  787. return err;
  788. }
  789. /*
  790. * remove a Memory Controller instance
  791. */
  792. void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
  793. {
  794. int i;
  795. debugf0("%s()\n", __func__);
  796. /* remove all csrow kobjects */
  797. for (i = 0; i < mci->nr_csrows; i++) {
  798. if (mci->csrows[i].nr_pages > 0) {
  799. debugf0("%s() unreg csrow-%d\n", __func__, i);
  800. kobject_put(&mci->csrows[i].kobj);
  801. }
  802. }
  803. debugf0("%s() remove_link\n", __func__);
  804. /* remove the symlink */
  805. sysfs_remove_link(&mci->edac_mci_kobj, EDAC_DEVICE_SYMLINK);
  806. debugf0("%s() remove_mci_instance\n", __func__);
  807. /* remove this mci instance's attribtes */
  808. edac_remove_mci_instance_attributes(mci,
  809. mci->mc_driver_sysfs_attributes,
  810. &mci->edac_mci_kobj, 0);
  811. debugf0("%s() unregister this mci kobj\n", __func__);
  812. /* unregister this instance's kobject */
  813. kobject_put(&mci->edac_mci_kobj);
  814. }
  815. /*
  816. * edac_setup_sysfs_mc_kset(void)
  817. *
  818. * Initialize the mc_kset for the 'mc' entry
  819. * This requires creating the top 'mc' directory with a kset
  820. * and its controls/attributes.
  821. *
  822. * To this 'mc' kset, instance 'mci' will be grouped as children.
  823. *
  824. * Return: 0 SUCCESS
  825. * !0 FAILURE error code
  826. */
  827. int edac_sysfs_setup_mc_kset(void)
  828. {
  829. int err = 0;
  830. struct sysdev_class *edac_class;
  831. debugf1("%s()\n", __func__);
  832. /* get the /sys/devices/system/edac class reference */
  833. edac_class = edac_get_edac_class();
  834. if (edac_class == NULL) {
  835. debugf1("%s() no edac_class error=%d\n", __func__, err);
  836. goto fail_out;
  837. }
  838. /* Init the MC's kobject */
  839. mc_kset = kset_create_and_add("mc", NULL, &edac_class->kset.kobj);
  840. if (!mc_kset) {
  841. err = -ENOMEM;
  842. debugf1("%s() Failed to register '.../edac/mc'\n", __func__);
  843. goto fail_out;
  844. }
  845. debugf1("%s() Registered '.../edac/mc' kobject\n", __func__);
  846. return 0;
  847. /* error unwind stack */
  848. fail_out:
  849. return err;
  850. }
  851. /*
  852. * edac_sysfs_teardown_mc_kset
  853. *
  854. * deconstruct the mc_ket for memory controllers
  855. */
  856. void edac_sysfs_teardown_mc_kset(void)
  857. {
  858. kset_unregister(mc_kset);
  859. }