edac_mc_sysfs.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. const struct mcidev_sysfs_attribute *sysfs_attrib,
  643. struct kobject *kobj)
  644. {
  645. int err;
  646. debugf1("%s()\n", __func__);
  647. while (sysfs_attrib) {
  648. debugf1("%s() sysfs_attrib = %p\n",__func__, sysfs_attrib);
  649. if (sysfs_attrib->grp) {
  650. struct mcidev_sysfs_group_kobj *grp_kobj;
  651. grp_kobj = kzalloc(sizeof(*grp_kobj), GFP_KERNEL);
  652. if (!grp_kobj)
  653. return -ENOMEM;
  654. grp_kobj->grp = sysfs_attrib->grp;
  655. grp_kobj->mci = mci;
  656. list_add_tail(&grp_kobj->list, &mci->grp_kobj_list);
  657. debugf0("%s() grp %s, mci %p\n", __func__,
  658. sysfs_attrib->grp->name, mci);
  659. err = kobject_init_and_add(&grp_kobj->kobj,
  660. &ktype_inst_grp,
  661. &mci->edac_mci_kobj,
  662. sysfs_attrib->grp->name);
  663. if (err < 0) {
  664. printk(KERN_ERR "kobject_init_and_add failed: %d\n", err);
  665. return err;
  666. }
  667. err = edac_create_mci_instance_attributes(mci,
  668. grp_kobj->grp->mcidev_attr,
  669. &grp_kobj->kobj);
  670. if (err < 0)
  671. return err;
  672. } else if (sysfs_attrib->attr.name) {
  673. debugf0("%s() file %s\n", __func__,
  674. sysfs_attrib->attr.name);
  675. err = sysfs_create_file(kobj, &sysfs_attrib->attr);
  676. if (err < 0) {
  677. printk(KERN_ERR "sysfs_create_file failed: %d\n", err);
  678. return err;
  679. }
  680. } else
  681. break;
  682. sysfs_attrib++;
  683. }
  684. return 0;
  685. }
  686. /*
  687. * edac_remove_mci_instance_attributes
  688. * remove MC driver specific attributes at the topmost level
  689. * directory of this mci instance.
  690. */
  691. static void edac_remove_mci_instance_attributes(struct mem_ctl_info *mci,
  692. const struct mcidev_sysfs_attribute *sysfs_attrib,
  693. struct kobject *kobj, int count)
  694. {
  695. struct mcidev_sysfs_group_kobj *grp_kobj, *tmp;
  696. debugf1("%s()\n", __func__);
  697. /*
  698. * loop if there are attributes and until we hit a NULL entry
  699. * Remove first all the atributes
  700. */
  701. while (sysfs_attrib) {
  702. debugf1("%s() sysfs_attrib = %p\n",__func__, sysfs_attrib);
  703. if (sysfs_attrib->grp) {
  704. debugf1("%s() seeking for group %s\n",
  705. __func__, sysfs_attrib->grp->name);
  706. list_for_each_entry(grp_kobj,
  707. &mci->grp_kobj_list, list) {
  708. debugf1("%s() grp_kobj->grp = %p\n",__func__, grp_kobj->grp);
  709. if (grp_kobj->grp == sysfs_attrib->grp) {
  710. edac_remove_mci_instance_attributes(mci,
  711. grp_kobj->grp->mcidev_attr,
  712. &grp_kobj->kobj, count + 1);
  713. debugf0("%s() group %s\n", __func__,
  714. sysfs_attrib->grp->name);
  715. kobject_put(&grp_kobj->kobj);
  716. }
  717. }
  718. debugf1("%s() end of seeking for group %s\n",
  719. __func__, sysfs_attrib->grp->name);
  720. } else if (sysfs_attrib->attr.name) {
  721. debugf0("%s() file %s\n", __func__,
  722. sysfs_attrib->attr.name);
  723. sysfs_remove_file(kobj, &sysfs_attrib->attr);
  724. } else
  725. break;
  726. sysfs_attrib++;
  727. }
  728. /* Remove the group objects */
  729. if (count)
  730. return;
  731. list_for_each_entry_safe(grp_kobj, tmp,
  732. &mci->grp_kobj_list, list) {
  733. list_del(&grp_kobj->list);
  734. kfree(grp_kobj);
  735. }
  736. }
  737. /*
  738. * Create a new Memory Controller kobject instance,
  739. * mc<id> under the 'mc' directory
  740. *
  741. * Return:
  742. * 0 Success
  743. * !0 Failure
  744. */
  745. int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
  746. {
  747. int i;
  748. int err;
  749. struct csrow_info *csrow;
  750. struct kobject *kobj_mci = &mci->edac_mci_kobj;
  751. debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
  752. INIT_LIST_HEAD(&mci->grp_kobj_list);
  753. /* create a symlink for the device */
  754. err = sysfs_create_link(kobj_mci, &mci->dev->kobj,
  755. EDAC_DEVICE_SYMLINK);
  756. if (err) {
  757. debugf1("%s() failure to create symlink\n", __func__);
  758. goto fail0;
  759. }
  760. /* If the low level driver desires some attributes,
  761. * then create them now for the driver.
  762. */
  763. if (mci->mc_driver_sysfs_attributes) {
  764. err = edac_create_mci_instance_attributes(mci,
  765. mci->mc_driver_sysfs_attributes,
  766. &mci->edac_mci_kobj);
  767. if (err) {
  768. debugf1("%s() failure to create mci attributes\n",
  769. __func__);
  770. goto fail0;
  771. }
  772. }
  773. /* Make directories for each CSROW object under the mc<id> kobject
  774. */
  775. for (i = 0; i < mci->nr_csrows; i++) {
  776. csrow = &mci->csrows[i];
  777. /* Only expose populated CSROWs */
  778. if (csrow->nr_pages > 0) {
  779. err = edac_create_csrow_object(mci, csrow, i);
  780. if (err) {
  781. debugf1("%s() failure: create csrow %d obj\n",
  782. __func__, i);
  783. goto fail1;
  784. }
  785. }
  786. }
  787. return 0;
  788. /* CSROW error: backout what has already been registered, */
  789. fail1:
  790. for (i--; i >= 0; i--) {
  791. if (csrow->nr_pages > 0) {
  792. kobject_put(&mci->csrows[i].kobj);
  793. }
  794. }
  795. /* remove the mci instance's attributes, if any */
  796. edac_remove_mci_instance_attributes(mci,
  797. mci->mc_driver_sysfs_attributes, &mci->edac_mci_kobj, 0);
  798. /* remove the symlink */
  799. sysfs_remove_link(kobj_mci, EDAC_DEVICE_SYMLINK);
  800. fail0:
  801. return err;
  802. }
  803. /*
  804. * remove a Memory Controller instance
  805. */
  806. void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
  807. {
  808. int i;
  809. debugf0("%s()\n", __func__);
  810. /* remove all csrow kobjects */
  811. debugf0("%s() unregister this mci kobj\n", __func__);
  812. for (i = 0; i < mci->nr_csrows; i++) {
  813. if (mci->csrows[i].nr_pages > 0) {
  814. debugf0("%s() unreg csrow-%d\n", __func__, i);
  815. kobject_put(&mci->csrows[i].kobj);
  816. }
  817. }
  818. /* remove this mci instance's attribtes */
  819. if (mci->mc_driver_sysfs_attributes) {
  820. debugf0("%s() unregister mci private attributes\n", __func__);
  821. edac_remove_mci_instance_attributes(mci,
  822. mci->mc_driver_sysfs_attributes,
  823. &mci->edac_mci_kobj, 0);
  824. }
  825. /* remove the symlink */
  826. debugf0("%s() remove_link\n", __func__);
  827. sysfs_remove_link(&mci->edac_mci_kobj, EDAC_DEVICE_SYMLINK);
  828. /* unregister this instance's kobject */
  829. debugf0("%s() remove_mci_instance\n", __func__);
  830. kobject_put(&mci->edac_mci_kobj);
  831. }
  832. /*
  833. * edac_setup_sysfs_mc_kset(void)
  834. *
  835. * Initialize the mc_kset for the 'mc' entry
  836. * This requires creating the top 'mc' directory with a kset
  837. * and its controls/attributes.
  838. *
  839. * To this 'mc' kset, instance 'mci' will be grouped as children.
  840. *
  841. * Return: 0 SUCCESS
  842. * !0 FAILURE error code
  843. */
  844. int edac_sysfs_setup_mc_kset(void)
  845. {
  846. int err = 0;
  847. struct sysdev_class *edac_class;
  848. debugf1("%s()\n", __func__);
  849. /* get the /sys/devices/system/edac class reference */
  850. edac_class = edac_get_edac_class();
  851. if (edac_class == NULL) {
  852. debugf1("%s() no edac_class error=%d\n", __func__, err);
  853. goto fail_out;
  854. }
  855. /* Init the MC's kobject */
  856. mc_kset = kset_create_and_add("mc", NULL, &edac_class->kset.kobj);
  857. if (!mc_kset) {
  858. err = -ENOMEM;
  859. debugf1("%s() Failed to register '.../edac/mc'\n", __func__);
  860. goto fail_out;
  861. }
  862. debugf1("%s() Registered '.../edac/mc' kobject\n", __func__);
  863. return 0;
  864. /* error unwind stack */
  865. fail_out:
  866. return err;
  867. }
  868. /*
  869. * edac_sysfs_teardown_mc_kset
  870. *
  871. * deconstruct the mc_ket for memory controllers
  872. */
  873. void edac_sysfs_teardown_mc_kset(void)
  874. {
  875. kset_unregister(mc_kset);
  876. }