edac_mc_sysfs.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. * (c) 2012-2013 - Mauro Carvalho Chehab
  11. * The entire API were re-written, and ported to use struct device
  12. *
  13. */
  14. #include <linux/ctype.h>
  15. #include <linux/slab.h>
  16. #include <linux/edac.h>
  17. #include <linux/bug.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/uaccess.h>
  20. #include "edac_mc.h"
  21. #include "edac_module.h"
  22. /* MC EDAC Controls, setable by module parameter, and sysfs */
  23. static int edac_mc_log_ue = 1;
  24. static int edac_mc_log_ce = 1;
  25. static int edac_mc_panic_on_ue;
  26. static int edac_mc_poll_msec = 1000;
  27. /* Getter functions for above */
  28. int edac_mc_get_log_ue(void)
  29. {
  30. return edac_mc_log_ue;
  31. }
  32. int edac_mc_get_log_ce(void)
  33. {
  34. return edac_mc_log_ce;
  35. }
  36. int edac_mc_get_panic_on_ue(void)
  37. {
  38. return edac_mc_panic_on_ue;
  39. }
  40. /* this is temporary */
  41. int edac_mc_get_poll_msec(void)
  42. {
  43. return edac_mc_poll_msec;
  44. }
  45. static int edac_set_poll_msec(const char *val, const struct kernel_param *kp)
  46. {
  47. unsigned long l;
  48. int ret;
  49. if (!val)
  50. return -EINVAL;
  51. ret = kstrtoul(val, 0, &l);
  52. if (ret)
  53. return ret;
  54. if (l < 1000)
  55. return -EINVAL;
  56. *((unsigned long *)kp->arg) = l;
  57. /* notify edac_mc engine to reset the poll period */
  58. edac_mc_reset_delay_period(l);
  59. return 0;
  60. }
  61. /* Parameter declarations for above */
  62. module_param(edac_mc_panic_on_ue, int, 0644);
  63. MODULE_PARM_DESC(edac_mc_panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
  64. module_param(edac_mc_log_ue, int, 0644);
  65. MODULE_PARM_DESC(edac_mc_log_ue,
  66. "Log uncorrectable error to console: 0=off 1=on");
  67. module_param(edac_mc_log_ce, int, 0644);
  68. MODULE_PARM_DESC(edac_mc_log_ce,
  69. "Log correctable error to console: 0=off 1=on");
  70. module_param_call(edac_mc_poll_msec, edac_set_poll_msec, param_get_int,
  71. &edac_mc_poll_msec, 0644);
  72. MODULE_PARM_DESC(edac_mc_poll_msec, "Polling period in milliseconds");
  73. static struct device *mci_pdev;
  74. /*
  75. * various constants for Memory Controllers
  76. */
  77. static const char * const dev_types[] = {
  78. [DEV_UNKNOWN] = "Unknown",
  79. [DEV_X1] = "x1",
  80. [DEV_X2] = "x2",
  81. [DEV_X4] = "x4",
  82. [DEV_X8] = "x8",
  83. [DEV_X16] = "x16",
  84. [DEV_X32] = "x32",
  85. [DEV_X64] = "x64"
  86. };
  87. static const char * const edac_caps[] = {
  88. [EDAC_UNKNOWN] = "Unknown",
  89. [EDAC_NONE] = "None",
  90. [EDAC_RESERVED] = "Reserved",
  91. [EDAC_PARITY] = "PARITY",
  92. [EDAC_EC] = "EC",
  93. [EDAC_SECDED] = "SECDED",
  94. [EDAC_S2ECD2ED] = "S2ECD2ED",
  95. [EDAC_S4ECD4ED] = "S4ECD4ED",
  96. [EDAC_S8ECD8ED] = "S8ECD8ED",
  97. [EDAC_S16ECD16ED] = "S16ECD16ED"
  98. };
  99. #ifdef CONFIG_EDAC_LEGACY_SYSFS
  100. /*
  101. * EDAC sysfs CSROW data structures and methods
  102. */
  103. #define to_csrow(k) container_of(k, struct csrow_info, dev)
  104. /*
  105. * We need it to avoid namespace conflicts between the legacy API
  106. * and the per-dimm/per-rank one
  107. */
  108. #define DEVICE_ATTR_LEGACY(_name, _mode, _show, _store) \
  109. static struct device_attribute dev_attr_legacy_##_name = __ATTR(_name, _mode, _show, _store)
  110. struct dev_ch_attribute {
  111. struct device_attribute attr;
  112. int channel;
  113. };
  114. #define DEVICE_CHANNEL(_name, _mode, _show, _store, _var) \
  115. static struct dev_ch_attribute dev_attr_legacy_##_name = \
  116. { __ATTR(_name, _mode, _show, _store), (_var) }
  117. #define to_channel(k) (container_of(k, struct dev_ch_attribute, attr)->channel)
  118. /* Set of more default csrow<id> attribute show/store functions */
  119. static ssize_t csrow_ue_count_show(struct device *dev,
  120. struct device_attribute *mattr, char *data)
  121. {
  122. struct csrow_info *csrow = to_csrow(dev);
  123. return sprintf(data, "%u\n", csrow->ue_count);
  124. }
  125. static ssize_t csrow_ce_count_show(struct device *dev,
  126. struct device_attribute *mattr, char *data)
  127. {
  128. struct csrow_info *csrow = to_csrow(dev);
  129. return sprintf(data, "%u\n", csrow->ce_count);
  130. }
  131. static ssize_t csrow_size_show(struct device *dev,
  132. struct device_attribute *mattr, char *data)
  133. {
  134. struct csrow_info *csrow = to_csrow(dev);
  135. int i;
  136. u32 nr_pages = 0;
  137. for (i = 0; i < csrow->nr_channels; i++)
  138. nr_pages += csrow->channels[i]->dimm->nr_pages;
  139. return sprintf(data, "%u\n", PAGES_TO_MiB(nr_pages));
  140. }
  141. static ssize_t csrow_mem_type_show(struct device *dev,
  142. struct device_attribute *mattr, char *data)
  143. {
  144. struct csrow_info *csrow = to_csrow(dev);
  145. return sprintf(data, "%s\n", edac_mem_types[csrow->channels[0]->dimm->mtype]);
  146. }
  147. static ssize_t csrow_dev_type_show(struct device *dev,
  148. struct device_attribute *mattr, char *data)
  149. {
  150. struct csrow_info *csrow = to_csrow(dev);
  151. return sprintf(data, "%s\n", dev_types[csrow->channels[0]->dimm->dtype]);
  152. }
  153. static ssize_t csrow_edac_mode_show(struct device *dev,
  154. struct device_attribute *mattr,
  155. char *data)
  156. {
  157. struct csrow_info *csrow = to_csrow(dev);
  158. return sprintf(data, "%s\n", edac_caps[csrow->channels[0]->dimm->edac_mode]);
  159. }
  160. /* show/store functions for DIMM Label attributes */
  161. static ssize_t channel_dimm_label_show(struct device *dev,
  162. struct device_attribute *mattr,
  163. char *data)
  164. {
  165. struct csrow_info *csrow = to_csrow(dev);
  166. unsigned chan = to_channel(mattr);
  167. struct rank_info *rank = csrow->channels[chan];
  168. /* if field has not been initialized, there is nothing to send */
  169. if (!rank->dimm->label[0])
  170. return 0;
  171. return snprintf(data, sizeof(rank->dimm->label) + 1, "%s\n",
  172. rank->dimm->label);
  173. }
  174. static ssize_t channel_dimm_label_store(struct device *dev,
  175. struct device_attribute *mattr,
  176. const char *data, size_t count)
  177. {
  178. struct csrow_info *csrow = to_csrow(dev);
  179. unsigned chan = to_channel(mattr);
  180. struct rank_info *rank = csrow->channels[chan];
  181. size_t copy_count = count;
  182. if (count == 0)
  183. return -EINVAL;
  184. if (data[count - 1] == '\0' || data[count - 1] == '\n')
  185. copy_count -= 1;
  186. if (copy_count == 0 || copy_count >= sizeof(rank->dimm->label))
  187. return -EINVAL;
  188. strncpy(rank->dimm->label, data, copy_count);
  189. rank->dimm->label[copy_count] = '\0';
  190. return count;
  191. }
  192. /* show function for dynamic chX_ce_count attribute */
  193. static ssize_t channel_ce_count_show(struct device *dev,
  194. struct device_attribute *mattr, char *data)
  195. {
  196. struct csrow_info *csrow = to_csrow(dev);
  197. unsigned chan = to_channel(mattr);
  198. struct rank_info *rank = csrow->channels[chan];
  199. return sprintf(data, "%u\n", rank->ce_count);
  200. }
  201. /* cwrow<id>/attribute files */
  202. DEVICE_ATTR_LEGACY(size_mb, S_IRUGO, csrow_size_show, NULL);
  203. DEVICE_ATTR_LEGACY(dev_type, S_IRUGO, csrow_dev_type_show, NULL);
  204. DEVICE_ATTR_LEGACY(mem_type, S_IRUGO, csrow_mem_type_show, NULL);
  205. DEVICE_ATTR_LEGACY(edac_mode, S_IRUGO, csrow_edac_mode_show, NULL);
  206. DEVICE_ATTR_LEGACY(ue_count, S_IRUGO, csrow_ue_count_show, NULL);
  207. DEVICE_ATTR_LEGACY(ce_count, S_IRUGO, csrow_ce_count_show, NULL);
  208. /* default attributes of the CSROW<id> object */
  209. static struct attribute *csrow_attrs[] = {
  210. &dev_attr_legacy_dev_type.attr,
  211. &dev_attr_legacy_mem_type.attr,
  212. &dev_attr_legacy_edac_mode.attr,
  213. &dev_attr_legacy_size_mb.attr,
  214. &dev_attr_legacy_ue_count.attr,
  215. &dev_attr_legacy_ce_count.attr,
  216. NULL,
  217. };
  218. static const struct attribute_group csrow_attr_grp = {
  219. .attrs = csrow_attrs,
  220. };
  221. static const struct attribute_group *csrow_attr_groups[] = {
  222. &csrow_attr_grp,
  223. NULL
  224. };
  225. static void csrow_attr_release(struct device *dev)
  226. {
  227. struct csrow_info *csrow = container_of(dev, struct csrow_info, dev);
  228. edac_dbg(1, "Releasing csrow device %s\n", dev_name(dev));
  229. kfree(csrow);
  230. }
  231. static const struct device_type csrow_attr_type = {
  232. .groups = csrow_attr_groups,
  233. .release = csrow_attr_release,
  234. };
  235. /*
  236. * possible dynamic channel DIMM Label attribute files
  237. *
  238. */
  239. DEVICE_CHANNEL(ch0_dimm_label, S_IRUGO | S_IWUSR,
  240. channel_dimm_label_show, channel_dimm_label_store, 0);
  241. DEVICE_CHANNEL(ch1_dimm_label, S_IRUGO | S_IWUSR,
  242. channel_dimm_label_show, channel_dimm_label_store, 1);
  243. DEVICE_CHANNEL(ch2_dimm_label, S_IRUGO | S_IWUSR,
  244. channel_dimm_label_show, channel_dimm_label_store, 2);
  245. DEVICE_CHANNEL(ch3_dimm_label, S_IRUGO | S_IWUSR,
  246. channel_dimm_label_show, channel_dimm_label_store, 3);
  247. DEVICE_CHANNEL(ch4_dimm_label, S_IRUGO | S_IWUSR,
  248. channel_dimm_label_show, channel_dimm_label_store, 4);
  249. DEVICE_CHANNEL(ch5_dimm_label, S_IRUGO | S_IWUSR,
  250. channel_dimm_label_show, channel_dimm_label_store, 5);
  251. DEVICE_CHANNEL(ch6_dimm_label, S_IRUGO | S_IWUSR,
  252. channel_dimm_label_show, channel_dimm_label_store, 6);
  253. DEVICE_CHANNEL(ch7_dimm_label, S_IRUGO | S_IWUSR,
  254. channel_dimm_label_show, channel_dimm_label_store, 7);
  255. /* Total possible dynamic DIMM Label attribute file table */
  256. static struct attribute *dynamic_csrow_dimm_attr[] = {
  257. &dev_attr_legacy_ch0_dimm_label.attr.attr,
  258. &dev_attr_legacy_ch1_dimm_label.attr.attr,
  259. &dev_attr_legacy_ch2_dimm_label.attr.attr,
  260. &dev_attr_legacy_ch3_dimm_label.attr.attr,
  261. &dev_attr_legacy_ch4_dimm_label.attr.attr,
  262. &dev_attr_legacy_ch5_dimm_label.attr.attr,
  263. &dev_attr_legacy_ch6_dimm_label.attr.attr,
  264. &dev_attr_legacy_ch7_dimm_label.attr.attr,
  265. NULL
  266. };
  267. /* possible dynamic channel ce_count attribute files */
  268. DEVICE_CHANNEL(ch0_ce_count, S_IRUGO,
  269. channel_ce_count_show, NULL, 0);
  270. DEVICE_CHANNEL(ch1_ce_count, S_IRUGO,
  271. channel_ce_count_show, NULL, 1);
  272. DEVICE_CHANNEL(ch2_ce_count, S_IRUGO,
  273. channel_ce_count_show, NULL, 2);
  274. DEVICE_CHANNEL(ch3_ce_count, S_IRUGO,
  275. channel_ce_count_show, NULL, 3);
  276. DEVICE_CHANNEL(ch4_ce_count, S_IRUGO,
  277. channel_ce_count_show, NULL, 4);
  278. DEVICE_CHANNEL(ch5_ce_count, S_IRUGO,
  279. channel_ce_count_show, NULL, 5);
  280. DEVICE_CHANNEL(ch6_ce_count, S_IRUGO,
  281. channel_ce_count_show, NULL, 6);
  282. DEVICE_CHANNEL(ch7_ce_count, S_IRUGO,
  283. channel_ce_count_show, NULL, 7);
  284. /* Total possible dynamic ce_count attribute file table */
  285. static struct attribute *dynamic_csrow_ce_count_attr[] = {
  286. &dev_attr_legacy_ch0_ce_count.attr.attr,
  287. &dev_attr_legacy_ch1_ce_count.attr.attr,
  288. &dev_attr_legacy_ch2_ce_count.attr.attr,
  289. &dev_attr_legacy_ch3_ce_count.attr.attr,
  290. &dev_attr_legacy_ch4_ce_count.attr.attr,
  291. &dev_attr_legacy_ch5_ce_count.attr.attr,
  292. &dev_attr_legacy_ch6_ce_count.attr.attr,
  293. &dev_attr_legacy_ch7_ce_count.attr.attr,
  294. NULL
  295. };
  296. static umode_t csrow_dev_is_visible(struct kobject *kobj,
  297. struct attribute *attr, int idx)
  298. {
  299. struct device *dev = kobj_to_dev(kobj);
  300. struct csrow_info *csrow = container_of(dev, struct csrow_info, dev);
  301. if (idx >= csrow->nr_channels)
  302. return 0;
  303. if (idx >= ARRAY_SIZE(dynamic_csrow_ce_count_attr) - 1) {
  304. WARN_ONCE(1, "idx: %d\n", idx);
  305. return 0;
  306. }
  307. /* Only expose populated DIMMs */
  308. if (!csrow->channels[idx]->dimm->nr_pages)
  309. return 0;
  310. return attr->mode;
  311. }
  312. static const struct attribute_group csrow_dev_dimm_group = {
  313. .attrs = dynamic_csrow_dimm_attr,
  314. .is_visible = csrow_dev_is_visible,
  315. };
  316. static const struct attribute_group csrow_dev_ce_count_group = {
  317. .attrs = dynamic_csrow_ce_count_attr,
  318. .is_visible = csrow_dev_is_visible,
  319. };
  320. static const struct attribute_group *csrow_dev_groups[] = {
  321. &csrow_dev_dimm_group,
  322. &csrow_dev_ce_count_group,
  323. NULL
  324. };
  325. static inline int nr_pages_per_csrow(struct csrow_info *csrow)
  326. {
  327. int chan, nr_pages = 0;
  328. for (chan = 0; chan < csrow->nr_channels; chan++)
  329. nr_pages += csrow->channels[chan]->dimm->nr_pages;
  330. return nr_pages;
  331. }
  332. /* Create a CSROW object under specifed edac_mc_device */
  333. static int edac_create_csrow_object(struct mem_ctl_info *mci,
  334. struct csrow_info *csrow, int index)
  335. {
  336. csrow->dev.type = &csrow_attr_type;
  337. csrow->dev.bus = mci->bus;
  338. csrow->dev.groups = csrow_dev_groups;
  339. device_initialize(&csrow->dev);
  340. csrow->dev.parent = &mci->dev;
  341. csrow->mci = mci;
  342. dev_set_name(&csrow->dev, "csrow%d", index);
  343. dev_set_drvdata(&csrow->dev, csrow);
  344. edac_dbg(0, "creating (virtual) csrow node %s\n",
  345. dev_name(&csrow->dev));
  346. return device_add(&csrow->dev);
  347. }
  348. /* Create a CSROW object under specifed edac_mc_device */
  349. static int edac_create_csrow_objects(struct mem_ctl_info *mci)
  350. {
  351. int err, i;
  352. struct csrow_info *csrow;
  353. for (i = 0; i < mci->nr_csrows; i++) {
  354. csrow = mci->csrows[i];
  355. if (!nr_pages_per_csrow(csrow))
  356. continue;
  357. err = edac_create_csrow_object(mci, mci->csrows[i], i);
  358. if (err < 0) {
  359. edac_dbg(1,
  360. "failure: create csrow objects for csrow %d\n",
  361. i);
  362. goto error;
  363. }
  364. }
  365. return 0;
  366. error:
  367. for (--i; i >= 0; i--) {
  368. csrow = mci->csrows[i];
  369. if (!nr_pages_per_csrow(csrow))
  370. continue;
  371. put_device(&mci->csrows[i]->dev);
  372. }
  373. return err;
  374. }
  375. static void edac_delete_csrow_objects(struct mem_ctl_info *mci)
  376. {
  377. int i;
  378. struct csrow_info *csrow;
  379. for (i = mci->nr_csrows - 1; i >= 0; i--) {
  380. csrow = mci->csrows[i];
  381. if (!nr_pages_per_csrow(csrow))
  382. continue;
  383. device_unregister(&mci->csrows[i]->dev);
  384. }
  385. }
  386. #endif
  387. /*
  388. * Per-dimm (or per-rank) devices
  389. */
  390. #define to_dimm(k) container_of(k, struct dimm_info, dev)
  391. /* show/store functions for DIMM Label attributes */
  392. static ssize_t dimmdev_location_show(struct device *dev,
  393. struct device_attribute *mattr, char *data)
  394. {
  395. struct dimm_info *dimm = to_dimm(dev);
  396. return edac_dimm_info_location(dimm, data, PAGE_SIZE);
  397. }
  398. static ssize_t dimmdev_label_show(struct device *dev,
  399. struct device_attribute *mattr, char *data)
  400. {
  401. struct dimm_info *dimm = to_dimm(dev);
  402. /* if field has not been initialized, there is nothing to send */
  403. if (!dimm->label[0])
  404. return 0;
  405. return snprintf(data, sizeof(dimm->label) + 1, "%s\n", dimm->label);
  406. }
  407. static ssize_t dimmdev_label_store(struct device *dev,
  408. struct device_attribute *mattr,
  409. const char *data,
  410. size_t count)
  411. {
  412. struct dimm_info *dimm = to_dimm(dev);
  413. size_t copy_count = count;
  414. if (count == 0)
  415. return -EINVAL;
  416. if (data[count - 1] == '\0' || data[count - 1] == '\n')
  417. copy_count -= 1;
  418. if (copy_count == 0 || copy_count >= sizeof(dimm->label))
  419. return -EINVAL;
  420. strncpy(dimm->label, data, copy_count);
  421. dimm->label[copy_count] = '\0';
  422. return count;
  423. }
  424. static ssize_t dimmdev_size_show(struct device *dev,
  425. struct device_attribute *mattr, char *data)
  426. {
  427. struct dimm_info *dimm = to_dimm(dev);
  428. return sprintf(data, "%u\n", PAGES_TO_MiB(dimm->nr_pages));
  429. }
  430. static ssize_t dimmdev_mem_type_show(struct device *dev,
  431. struct device_attribute *mattr, char *data)
  432. {
  433. struct dimm_info *dimm = to_dimm(dev);
  434. return sprintf(data, "%s\n", edac_mem_types[dimm->mtype]);
  435. }
  436. static ssize_t dimmdev_dev_type_show(struct device *dev,
  437. struct device_attribute *mattr, char *data)
  438. {
  439. struct dimm_info *dimm = to_dimm(dev);
  440. return sprintf(data, "%s\n", dev_types[dimm->dtype]);
  441. }
  442. static ssize_t dimmdev_edac_mode_show(struct device *dev,
  443. struct device_attribute *mattr,
  444. char *data)
  445. {
  446. struct dimm_info *dimm = to_dimm(dev);
  447. return sprintf(data, "%s\n", edac_caps[dimm->edac_mode]);
  448. }
  449. static ssize_t dimmdev_ce_count_show(struct device *dev,
  450. struct device_attribute *mattr,
  451. char *data)
  452. {
  453. struct dimm_info *dimm = to_dimm(dev);
  454. u32 count;
  455. int off;
  456. off = EDAC_DIMM_OFF(dimm->mci->layers,
  457. dimm->mci->n_layers,
  458. dimm->location[0],
  459. dimm->location[1],
  460. dimm->location[2]);
  461. count = dimm->mci->ce_per_layer[dimm->mci->n_layers-1][off];
  462. return sprintf(data, "%u\n", count);
  463. }
  464. static ssize_t dimmdev_ue_count_show(struct device *dev,
  465. struct device_attribute *mattr,
  466. char *data)
  467. {
  468. struct dimm_info *dimm = to_dimm(dev);
  469. u32 count;
  470. int off;
  471. off = EDAC_DIMM_OFF(dimm->mci->layers,
  472. dimm->mci->n_layers,
  473. dimm->location[0],
  474. dimm->location[1],
  475. dimm->location[2]);
  476. count = dimm->mci->ue_per_layer[dimm->mci->n_layers-1][off];
  477. return sprintf(data, "%u\n", count);
  478. }
  479. /* dimm/rank attribute files */
  480. static DEVICE_ATTR(dimm_label, S_IRUGO | S_IWUSR,
  481. dimmdev_label_show, dimmdev_label_store);
  482. static DEVICE_ATTR(dimm_location, S_IRUGO, dimmdev_location_show, NULL);
  483. static DEVICE_ATTR(size, S_IRUGO, dimmdev_size_show, NULL);
  484. static DEVICE_ATTR(dimm_mem_type, S_IRUGO, dimmdev_mem_type_show, NULL);
  485. static DEVICE_ATTR(dimm_dev_type, S_IRUGO, dimmdev_dev_type_show, NULL);
  486. static DEVICE_ATTR(dimm_edac_mode, S_IRUGO, dimmdev_edac_mode_show, NULL);
  487. static DEVICE_ATTR(dimm_ce_count, S_IRUGO, dimmdev_ce_count_show, NULL);
  488. static DEVICE_ATTR(dimm_ue_count, S_IRUGO, dimmdev_ue_count_show, NULL);
  489. /* attributes of the dimm<id>/rank<id> object */
  490. static struct attribute *dimm_attrs[] = {
  491. &dev_attr_dimm_label.attr,
  492. &dev_attr_dimm_location.attr,
  493. &dev_attr_size.attr,
  494. &dev_attr_dimm_mem_type.attr,
  495. &dev_attr_dimm_dev_type.attr,
  496. &dev_attr_dimm_edac_mode.attr,
  497. &dev_attr_dimm_ce_count.attr,
  498. &dev_attr_dimm_ue_count.attr,
  499. NULL,
  500. };
  501. static const struct attribute_group dimm_attr_grp = {
  502. .attrs = dimm_attrs,
  503. };
  504. static const struct attribute_group *dimm_attr_groups[] = {
  505. &dimm_attr_grp,
  506. NULL
  507. };
  508. static void dimm_attr_release(struct device *dev)
  509. {
  510. struct dimm_info *dimm = container_of(dev, struct dimm_info, dev);
  511. edac_dbg(1, "Releasing dimm device %s\n", dev_name(dev));
  512. kfree(dimm);
  513. }
  514. static const struct device_type dimm_attr_type = {
  515. .groups = dimm_attr_groups,
  516. .release = dimm_attr_release,
  517. };
  518. /* Create a DIMM object under specifed memory controller device */
  519. static int edac_create_dimm_object(struct mem_ctl_info *mci,
  520. struct dimm_info *dimm,
  521. int index)
  522. {
  523. int err;
  524. dimm->mci = mci;
  525. dimm->dev.type = &dimm_attr_type;
  526. dimm->dev.bus = mci->bus;
  527. device_initialize(&dimm->dev);
  528. dimm->dev.parent = &mci->dev;
  529. if (mci->csbased)
  530. dev_set_name(&dimm->dev, "rank%d", index);
  531. else
  532. dev_set_name(&dimm->dev, "dimm%d", index);
  533. dev_set_drvdata(&dimm->dev, dimm);
  534. pm_runtime_forbid(&mci->dev);
  535. err = device_add(&dimm->dev);
  536. edac_dbg(0, "creating rank/dimm device %s\n", dev_name(&dimm->dev));
  537. return err;
  538. }
  539. /*
  540. * Memory controller device
  541. */
  542. #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
  543. static ssize_t mci_reset_counters_store(struct device *dev,
  544. struct device_attribute *mattr,
  545. const char *data, size_t count)
  546. {
  547. struct mem_ctl_info *mci = to_mci(dev);
  548. int cnt, row, chan, i;
  549. mci->ue_mc = 0;
  550. mci->ce_mc = 0;
  551. mci->ue_noinfo_count = 0;
  552. mci->ce_noinfo_count = 0;
  553. for (row = 0; row < mci->nr_csrows; row++) {
  554. struct csrow_info *ri = mci->csrows[row];
  555. ri->ue_count = 0;
  556. ri->ce_count = 0;
  557. for (chan = 0; chan < ri->nr_channels; chan++)
  558. ri->channels[chan]->ce_count = 0;
  559. }
  560. cnt = 1;
  561. for (i = 0; i < mci->n_layers; i++) {
  562. cnt *= mci->layers[i].size;
  563. memset(mci->ce_per_layer[i], 0, cnt * sizeof(u32));
  564. memset(mci->ue_per_layer[i], 0, cnt * sizeof(u32));
  565. }
  566. mci->start_time = jiffies;
  567. return count;
  568. }
  569. /* Memory scrubbing interface:
  570. *
  571. * A MC driver can limit the scrubbing bandwidth based on the CPU type.
  572. * Therefore, ->set_sdram_scrub_rate should be made to return the actual
  573. * bandwidth that is accepted or 0 when scrubbing is to be disabled.
  574. *
  575. * Negative value still means that an error has occurred while setting
  576. * the scrub rate.
  577. */
  578. static ssize_t mci_sdram_scrub_rate_store(struct device *dev,
  579. struct device_attribute *mattr,
  580. const char *data, size_t count)
  581. {
  582. struct mem_ctl_info *mci = to_mci(dev);
  583. unsigned long bandwidth = 0;
  584. int new_bw = 0;
  585. if (kstrtoul(data, 10, &bandwidth) < 0)
  586. return -EINVAL;
  587. new_bw = mci->set_sdram_scrub_rate(mci, bandwidth);
  588. if (new_bw < 0) {
  589. edac_printk(KERN_WARNING, EDAC_MC,
  590. "Error setting scrub rate to: %lu\n", bandwidth);
  591. return -EINVAL;
  592. }
  593. return count;
  594. }
  595. /*
  596. * ->get_sdram_scrub_rate() return value semantics same as above.
  597. */
  598. static ssize_t mci_sdram_scrub_rate_show(struct device *dev,
  599. struct device_attribute *mattr,
  600. char *data)
  601. {
  602. struct mem_ctl_info *mci = to_mci(dev);
  603. int bandwidth = 0;
  604. bandwidth = mci->get_sdram_scrub_rate(mci);
  605. if (bandwidth < 0) {
  606. edac_printk(KERN_DEBUG, EDAC_MC, "Error reading scrub rate\n");
  607. return bandwidth;
  608. }
  609. return sprintf(data, "%d\n", bandwidth);
  610. }
  611. /* default attribute files for the MCI object */
  612. static ssize_t mci_ue_count_show(struct device *dev,
  613. struct device_attribute *mattr,
  614. char *data)
  615. {
  616. struct mem_ctl_info *mci = to_mci(dev);
  617. return sprintf(data, "%d\n", mci->ue_mc);
  618. }
  619. static ssize_t mci_ce_count_show(struct device *dev,
  620. struct device_attribute *mattr,
  621. char *data)
  622. {
  623. struct mem_ctl_info *mci = to_mci(dev);
  624. return sprintf(data, "%d\n", mci->ce_mc);
  625. }
  626. static ssize_t mci_ce_noinfo_show(struct device *dev,
  627. struct device_attribute *mattr,
  628. char *data)
  629. {
  630. struct mem_ctl_info *mci = to_mci(dev);
  631. return sprintf(data, "%d\n", mci->ce_noinfo_count);
  632. }
  633. static ssize_t mci_ue_noinfo_show(struct device *dev,
  634. struct device_attribute *mattr,
  635. char *data)
  636. {
  637. struct mem_ctl_info *mci = to_mci(dev);
  638. return sprintf(data, "%d\n", mci->ue_noinfo_count);
  639. }
  640. static ssize_t mci_seconds_show(struct device *dev,
  641. struct device_attribute *mattr,
  642. char *data)
  643. {
  644. struct mem_ctl_info *mci = to_mci(dev);
  645. return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
  646. }
  647. static ssize_t mci_ctl_name_show(struct device *dev,
  648. struct device_attribute *mattr,
  649. char *data)
  650. {
  651. struct mem_ctl_info *mci = to_mci(dev);
  652. return sprintf(data, "%s\n", mci->ctl_name);
  653. }
  654. static ssize_t mci_size_mb_show(struct device *dev,
  655. struct device_attribute *mattr,
  656. char *data)
  657. {
  658. struct mem_ctl_info *mci = to_mci(dev);
  659. int total_pages = 0, csrow_idx, j;
  660. for (csrow_idx = 0; csrow_idx < mci->nr_csrows; csrow_idx++) {
  661. struct csrow_info *csrow = mci->csrows[csrow_idx];
  662. for (j = 0; j < csrow->nr_channels; j++) {
  663. struct dimm_info *dimm = csrow->channels[j]->dimm;
  664. total_pages += dimm->nr_pages;
  665. }
  666. }
  667. return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
  668. }
  669. static ssize_t mci_max_location_show(struct device *dev,
  670. struct device_attribute *mattr,
  671. char *data)
  672. {
  673. struct mem_ctl_info *mci = to_mci(dev);
  674. int i;
  675. char *p = data;
  676. for (i = 0; i < mci->n_layers; i++) {
  677. p += sprintf(p, "%s %d ",
  678. edac_layer_name[mci->layers[i].type],
  679. mci->layers[i].size - 1);
  680. }
  681. return p - data;
  682. }
  683. /* default Control file */
  684. static DEVICE_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
  685. /* default Attribute files */
  686. static DEVICE_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
  687. static DEVICE_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
  688. static DEVICE_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
  689. static DEVICE_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
  690. static DEVICE_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
  691. static DEVICE_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
  692. static DEVICE_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
  693. static DEVICE_ATTR(max_location, S_IRUGO, mci_max_location_show, NULL);
  694. /* memory scrubber attribute file */
  695. static DEVICE_ATTR(sdram_scrub_rate, 0, mci_sdram_scrub_rate_show,
  696. mci_sdram_scrub_rate_store); /* umode set later in is_visible */
  697. static struct attribute *mci_attrs[] = {
  698. &dev_attr_reset_counters.attr,
  699. &dev_attr_mc_name.attr,
  700. &dev_attr_size_mb.attr,
  701. &dev_attr_seconds_since_reset.attr,
  702. &dev_attr_ue_noinfo_count.attr,
  703. &dev_attr_ce_noinfo_count.attr,
  704. &dev_attr_ue_count.attr,
  705. &dev_attr_ce_count.attr,
  706. &dev_attr_max_location.attr,
  707. &dev_attr_sdram_scrub_rate.attr,
  708. NULL
  709. };
  710. static umode_t mci_attr_is_visible(struct kobject *kobj,
  711. struct attribute *attr, int idx)
  712. {
  713. struct device *dev = kobj_to_dev(kobj);
  714. struct mem_ctl_info *mci = to_mci(dev);
  715. umode_t mode = 0;
  716. if (attr != &dev_attr_sdram_scrub_rate.attr)
  717. return attr->mode;
  718. if (mci->get_sdram_scrub_rate)
  719. mode |= S_IRUGO;
  720. if (mci->set_sdram_scrub_rate)
  721. mode |= S_IWUSR;
  722. return mode;
  723. }
  724. static const struct attribute_group mci_attr_grp = {
  725. .attrs = mci_attrs,
  726. .is_visible = mci_attr_is_visible,
  727. };
  728. static const struct attribute_group *mci_attr_groups[] = {
  729. &mci_attr_grp,
  730. NULL
  731. };
  732. static void mci_attr_release(struct device *dev)
  733. {
  734. struct mem_ctl_info *mci = container_of(dev, struct mem_ctl_info, dev);
  735. edac_dbg(1, "Releasing csrow device %s\n", dev_name(dev));
  736. kfree(mci);
  737. }
  738. static const struct device_type mci_attr_type = {
  739. .groups = mci_attr_groups,
  740. .release = mci_attr_release,
  741. };
  742. /*
  743. * Create a new Memory Controller kobject instance,
  744. * mc<id> under the 'mc' directory
  745. *
  746. * Return:
  747. * 0 Success
  748. * !0 Failure
  749. */
  750. int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
  751. const struct attribute_group **groups)
  752. {
  753. char *name;
  754. int i, err;
  755. /*
  756. * The memory controller needs its own bus, in order to avoid
  757. * namespace conflicts at /sys/bus/edac.
  758. */
  759. name = kasprintf(GFP_KERNEL, "mc%d", mci->mc_idx);
  760. if (!name)
  761. return -ENOMEM;
  762. mci->bus->name = name;
  763. edac_dbg(0, "creating bus %s\n", mci->bus->name);
  764. err = bus_register(mci->bus);
  765. if (err < 0) {
  766. kfree(name);
  767. return err;
  768. }
  769. /* get the /sys/devices/system/edac subsys reference */
  770. mci->dev.type = &mci_attr_type;
  771. device_initialize(&mci->dev);
  772. mci->dev.parent = mci_pdev;
  773. mci->dev.bus = mci->bus;
  774. mci->dev.groups = groups;
  775. dev_set_name(&mci->dev, "mc%d", mci->mc_idx);
  776. dev_set_drvdata(&mci->dev, mci);
  777. pm_runtime_forbid(&mci->dev);
  778. edac_dbg(0, "creating device %s\n", dev_name(&mci->dev));
  779. err = device_add(&mci->dev);
  780. if (err < 0) {
  781. edac_dbg(1, "failure: create device %s\n", dev_name(&mci->dev));
  782. goto fail_unregister_bus;
  783. }
  784. /*
  785. * Create the dimm/rank devices
  786. */
  787. for (i = 0; i < mci->tot_dimms; i++) {
  788. struct dimm_info *dimm = mci->dimms[i];
  789. /* Only expose populated DIMMs */
  790. if (!dimm->nr_pages)
  791. continue;
  792. #ifdef CONFIG_EDAC_DEBUG
  793. edac_dbg(1, "creating dimm%d, located at ", i);
  794. if (edac_debug_level >= 1) {
  795. int lay;
  796. for (lay = 0; lay < mci->n_layers; lay++)
  797. printk(KERN_CONT "%s %d ",
  798. edac_layer_name[mci->layers[lay].type],
  799. dimm->location[lay]);
  800. printk(KERN_CONT "\n");
  801. }
  802. #endif
  803. err = edac_create_dimm_object(mci, dimm, i);
  804. if (err) {
  805. edac_dbg(1, "failure: create dimm %d obj\n", i);
  806. goto fail_unregister_dimm;
  807. }
  808. }
  809. #ifdef CONFIG_EDAC_LEGACY_SYSFS
  810. err = edac_create_csrow_objects(mci);
  811. if (err < 0)
  812. goto fail_unregister_dimm;
  813. #endif
  814. edac_create_debugfs_nodes(mci);
  815. return 0;
  816. fail_unregister_dimm:
  817. for (i--; i >= 0; i--) {
  818. struct dimm_info *dimm = mci->dimms[i];
  819. if (!dimm->nr_pages)
  820. continue;
  821. device_unregister(&dimm->dev);
  822. }
  823. device_unregister(&mci->dev);
  824. fail_unregister_bus:
  825. bus_unregister(mci->bus);
  826. kfree(name);
  827. return err;
  828. }
  829. /*
  830. * remove a Memory Controller instance
  831. */
  832. void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
  833. {
  834. int i;
  835. edac_dbg(0, "\n");
  836. #ifdef CONFIG_EDAC_DEBUG
  837. edac_debugfs_remove_recursive(mci->debugfs);
  838. #endif
  839. #ifdef CONFIG_EDAC_LEGACY_SYSFS
  840. edac_delete_csrow_objects(mci);
  841. #endif
  842. for (i = 0; i < mci->tot_dimms; i++) {
  843. struct dimm_info *dimm = mci->dimms[i];
  844. if (dimm->nr_pages == 0)
  845. continue;
  846. edac_dbg(0, "removing device %s\n", dev_name(&dimm->dev));
  847. device_unregister(&dimm->dev);
  848. }
  849. }
  850. void edac_unregister_sysfs(struct mem_ctl_info *mci)
  851. {
  852. struct bus_type *bus = mci->bus;
  853. const char *name = mci->bus->name;
  854. edac_dbg(1, "Unregistering device %s\n", dev_name(&mci->dev));
  855. device_unregister(&mci->dev);
  856. bus_unregister(bus);
  857. kfree(name);
  858. }
  859. static void mc_attr_release(struct device *dev)
  860. {
  861. /*
  862. * There's no container structure here, as this is just the mci
  863. * parent device, used to create the /sys/devices/mc sysfs node.
  864. * So, there are no attributes on it.
  865. */
  866. edac_dbg(1, "Releasing device %s\n", dev_name(dev));
  867. kfree(dev);
  868. }
  869. static const struct device_type mc_attr_type = {
  870. .release = mc_attr_release,
  871. };
  872. /*
  873. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  874. */
  875. int __init edac_mc_sysfs_init(void)
  876. {
  877. int err;
  878. mci_pdev = kzalloc(sizeof(*mci_pdev), GFP_KERNEL);
  879. if (!mci_pdev) {
  880. err = -ENOMEM;
  881. goto out;
  882. }
  883. mci_pdev->bus = edac_get_sysfs_subsys();
  884. mci_pdev->type = &mc_attr_type;
  885. device_initialize(mci_pdev);
  886. dev_set_name(mci_pdev, "mc");
  887. err = device_add(mci_pdev);
  888. if (err < 0)
  889. goto out_dev_free;
  890. edac_dbg(0, "device %s created\n", dev_name(mci_pdev));
  891. return 0;
  892. out_dev_free:
  893. kfree(mci_pdev);
  894. out:
  895. return err;
  896. }
  897. void edac_mc_sysfs_exit(void)
  898. {
  899. device_unregister(mci_pdev);
  900. }