edac_mc_sysfs.c 26 KB

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