edac_mc_sysfs.c 26 KB

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