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