edac_mc_sysfs.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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 unsigned 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. unsigned 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 int i;
  48. int ret;
  49. if (!val)
  50. return -EINVAL;
  51. ret = kstrtouint(val, 0, &i);
  52. if (ret)
  53. return ret;
  54. if (i < 1000)
  55. return -EINVAL;
  56. *((unsigned int *)kp->arg) = i;
  57. /* notify edac_mc engine to reset the poll period */
  58. edac_mc_reset_delay_period(i);
  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_uint,
  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. unsigned 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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 int 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 sysfs_emit(data, "%s\n", rank->dimm->label);
  172. }
  173. static ssize_t channel_dimm_label_store(struct device *dev,
  174. struct device_attribute *mattr,
  175. const char *data, size_t count)
  176. {
  177. struct csrow_info *csrow = to_csrow(dev);
  178. unsigned int chan = to_channel(mattr);
  179. struct rank_info *rank = csrow->channels[chan];
  180. size_t copy_count = count;
  181. if (count == 0)
  182. return -EINVAL;
  183. if (data[count - 1] == '\0' || data[count - 1] == '\n')
  184. copy_count -= 1;
  185. if (copy_count == 0 || copy_count >= sizeof(rank->dimm->label))
  186. return -EINVAL;
  187. memcpy(rank->dimm->label, data, copy_count);
  188. rank->dimm->label[copy_count] = '\0';
  189. return count;
  190. }
  191. /* show function for dynamic chX_ce_count attribute */
  192. static ssize_t channel_ce_count_show(struct device *dev,
  193. struct device_attribute *mattr, char *data)
  194. {
  195. struct csrow_info *csrow = to_csrow(dev);
  196. unsigned int chan = to_channel(mattr);
  197. struct rank_info *rank = csrow->channels[chan];
  198. return sysfs_emit(data, "%u\n", rank->ce_count);
  199. }
  200. /* cwrow<id>/attribute files */
  201. DEVICE_ATTR_LEGACY(size_mb, S_IRUGO, csrow_size_show, NULL);
  202. DEVICE_ATTR_LEGACY(dev_type, S_IRUGO, csrow_dev_type_show, NULL);
  203. DEVICE_ATTR_LEGACY(mem_type, S_IRUGO, csrow_mem_type_show, NULL);
  204. DEVICE_ATTR_LEGACY(edac_mode, S_IRUGO, csrow_edac_mode_show, NULL);
  205. DEVICE_ATTR_LEGACY(ue_count, S_IRUGO, csrow_ue_count_show, NULL);
  206. DEVICE_ATTR_LEGACY(ce_count, S_IRUGO, csrow_ce_count_show, NULL);
  207. /* default attributes of the CSROW<id> object */
  208. static struct attribute *csrow_attrs[] = {
  209. &dev_attr_legacy_dev_type.attr,
  210. &dev_attr_legacy_mem_type.attr,
  211. &dev_attr_legacy_edac_mode.attr,
  212. &dev_attr_legacy_size_mb.attr,
  213. &dev_attr_legacy_ue_count.attr,
  214. &dev_attr_legacy_ce_count.attr,
  215. NULL,
  216. };
  217. static const struct attribute_group csrow_attr_grp = {
  218. .attrs = csrow_attrs,
  219. };
  220. static const struct attribute_group *csrow_attr_groups[] = {
  221. &csrow_attr_grp,
  222. NULL
  223. };
  224. static const struct device_type csrow_attr_type = {
  225. .groups = csrow_attr_groups,
  226. };
  227. /*
  228. * possible dynamic channel DIMM Label attribute files
  229. *
  230. */
  231. DEVICE_CHANNEL(ch0_dimm_label, S_IRUGO | S_IWUSR,
  232. channel_dimm_label_show, channel_dimm_label_store, 0);
  233. DEVICE_CHANNEL(ch1_dimm_label, S_IRUGO | S_IWUSR,
  234. channel_dimm_label_show, channel_dimm_label_store, 1);
  235. DEVICE_CHANNEL(ch2_dimm_label, S_IRUGO | S_IWUSR,
  236. channel_dimm_label_show, channel_dimm_label_store, 2);
  237. DEVICE_CHANNEL(ch3_dimm_label, S_IRUGO | S_IWUSR,
  238. channel_dimm_label_show, channel_dimm_label_store, 3);
  239. DEVICE_CHANNEL(ch4_dimm_label, S_IRUGO | S_IWUSR,
  240. channel_dimm_label_show, channel_dimm_label_store, 4);
  241. DEVICE_CHANNEL(ch5_dimm_label, S_IRUGO | S_IWUSR,
  242. channel_dimm_label_show, channel_dimm_label_store, 5);
  243. DEVICE_CHANNEL(ch6_dimm_label, S_IRUGO | S_IWUSR,
  244. channel_dimm_label_show, channel_dimm_label_store, 6);
  245. DEVICE_CHANNEL(ch7_dimm_label, S_IRUGO | S_IWUSR,
  246. channel_dimm_label_show, channel_dimm_label_store, 7);
  247. DEVICE_CHANNEL(ch8_dimm_label, S_IRUGO | S_IWUSR,
  248. channel_dimm_label_show, channel_dimm_label_store, 8);
  249. DEVICE_CHANNEL(ch9_dimm_label, S_IRUGO | S_IWUSR,
  250. channel_dimm_label_show, channel_dimm_label_store, 9);
  251. DEVICE_CHANNEL(ch10_dimm_label, S_IRUGO | S_IWUSR,
  252. channel_dimm_label_show, channel_dimm_label_store, 10);
  253. DEVICE_CHANNEL(ch11_dimm_label, S_IRUGO | S_IWUSR,
  254. channel_dimm_label_show, channel_dimm_label_store, 11);
  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. &dev_attr_legacy_ch8_dimm_label.attr.attr,
  266. &dev_attr_legacy_ch9_dimm_label.attr.attr,
  267. &dev_attr_legacy_ch10_dimm_label.attr.attr,
  268. &dev_attr_legacy_ch11_dimm_label.attr.attr,
  269. NULL
  270. };
  271. /* possible dynamic channel ce_count attribute files */
  272. DEVICE_CHANNEL(ch0_ce_count, S_IRUGO,
  273. channel_ce_count_show, NULL, 0);
  274. DEVICE_CHANNEL(ch1_ce_count, S_IRUGO,
  275. channel_ce_count_show, NULL, 1);
  276. DEVICE_CHANNEL(ch2_ce_count, S_IRUGO,
  277. channel_ce_count_show, NULL, 2);
  278. DEVICE_CHANNEL(ch3_ce_count, S_IRUGO,
  279. channel_ce_count_show, NULL, 3);
  280. DEVICE_CHANNEL(ch4_ce_count, S_IRUGO,
  281. channel_ce_count_show, NULL, 4);
  282. DEVICE_CHANNEL(ch5_ce_count, S_IRUGO,
  283. channel_ce_count_show, NULL, 5);
  284. DEVICE_CHANNEL(ch6_ce_count, S_IRUGO,
  285. channel_ce_count_show, NULL, 6);
  286. DEVICE_CHANNEL(ch7_ce_count, S_IRUGO,
  287. channel_ce_count_show, NULL, 7);
  288. DEVICE_CHANNEL(ch8_ce_count, S_IRUGO,
  289. channel_ce_count_show, NULL, 8);
  290. DEVICE_CHANNEL(ch9_ce_count, S_IRUGO,
  291. channel_ce_count_show, NULL, 9);
  292. DEVICE_CHANNEL(ch10_ce_count, S_IRUGO,
  293. channel_ce_count_show, NULL, 10);
  294. DEVICE_CHANNEL(ch11_ce_count, S_IRUGO,
  295. channel_ce_count_show, NULL, 11);
  296. /* Total possible dynamic ce_count attribute file table */
  297. static struct attribute *dynamic_csrow_ce_count_attr[] = {
  298. &dev_attr_legacy_ch0_ce_count.attr.attr,
  299. &dev_attr_legacy_ch1_ce_count.attr.attr,
  300. &dev_attr_legacy_ch2_ce_count.attr.attr,
  301. &dev_attr_legacy_ch3_ce_count.attr.attr,
  302. &dev_attr_legacy_ch4_ce_count.attr.attr,
  303. &dev_attr_legacy_ch5_ce_count.attr.attr,
  304. &dev_attr_legacy_ch6_ce_count.attr.attr,
  305. &dev_attr_legacy_ch7_ce_count.attr.attr,
  306. &dev_attr_legacy_ch8_ce_count.attr.attr,
  307. &dev_attr_legacy_ch9_ce_count.attr.attr,
  308. &dev_attr_legacy_ch10_ce_count.attr.attr,
  309. &dev_attr_legacy_ch11_ce_count.attr.attr,
  310. NULL
  311. };
  312. static umode_t csrow_dev_is_visible(struct kobject *kobj,
  313. struct attribute *attr, int idx)
  314. {
  315. struct device *dev = kobj_to_dev(kobj);
  316. struct csrow_info *csrow = container_of(dev, struct csrow_info, dev);
  317. if (idx >= csrow->nr_channels)
  318. return 0;
  319. if (idx >= ARRAY_SIZE(dynamic_csrow_ce_count_attr) - 1) {
  320. WARN_ONCE(1, "idx: %d\n", idx);
  321. return 0;
  322. }
  323. /* Only expose populated DIMMs */
  324. if (!csrow->channels[idx]->dimm->nr_pages)
  325. return 0;
  326. return attr->mode;
  327. }
  328. static const struct attribute_group csrow_dev_dimm_group = {
  329. .attrs = dynamic_csrow_dimm_attr,
  330. .is_visible = csrow_dev_is_visible,
  331. };
  332. static const struct attribute_group csrow_dev_ce_count_group = {
  333. .attrs = dynamic_csrow_ce_count_attr,
  334. .is_visible = csrow_dev_is_visible,
  335. };
  336. static const struct attribute_group *csrow_dev_groups[] = {
  337. &csrow_dev_dimm_group,
  338. &csrow_dev_ce_count_group,
  339. NULL
  340. };
  341. static void csrow_release(struct device *dev)
  342. {
  343. /*
  344. * Nothing to do, just unregister sysfs here. The mci
  345. * device owns the data and will also release it.
  346. */
  347. }
  348. static inline int nr_pages_per_csrow(struct csrow_info *csrow)
  349. {
  350. int chan, nr_pages = 0;
  351. for (chan = 0; chan < csrow->nr_channels; chan++)
  352. nr_pages += csrow->channels[chan]->dimm->nr_pages;
  353. return nr_pages;
  354. }
  355. /* Create a CSROW object under specifed edac_mc_device */
  356. static int edac_create_csrow_object(struct mem_ctl_info *mci,
  357. struct csrow_info *csrow, int index)
  358. {
  359. int err;
  360. csrow->dev.type = &csrow_attr_type;
  361. csrow->dev.groups = csrow_dev_groups;
  362. csrow->dev.release = csrow_release;
  363. device_initialize(&csrow->dev);
  364. csrow->dev.parent = &mci->dev;
  365. csrow->mci = mci;
  366. dev_set_name(&csrow->dev, "csrow%d", index);
  367. dev_set_drvdata(&csrow->dev, csrow);
  368. err = device_add(&csrow->dev);
  369. if (err) {
  370. edac_dbg(1, "failure: create device %s\n", dev_name(&csrow->dev));
  371. put_device(&csrow->dev);
  372. return err;
  373. }
  374. edac_dbg(0, "device %s created\n", dev_name(&csrow->dev));
  375. return 0;
  376. }
  377. /* Create a CSROW object under specifed edac_mc_device */
  378. static int edac_create_csrow_objects(struct mem_ctl_info *mci)
  379. {
  380. int err, i;
  381. struct csrow_info *csrow;
  382. for (i = 0; i < mci->nr_csrows; i++) {
  383. csrow = mci->csrows[i];
  384. if (!nr_pages_per_csrow(csrow))
  385. continue;
  386. err = edac_create_csrow_object(mci, mci->csrows[i], i);
  387. if (err < 0)
  388. goto error;
  389. }
  390. return 0;
  391. error:
  392. for (--i; i >= 0; i--) {
  393. if (device_is_registered(&mci->csrows[i]->dev))
  394. device_unregister(&mci->csrows[i]->dev);
  395. }
  396. return err;
  397. }
  398. static void edac_delete_csrow_objects(struct mem_ctl_info *mci)
  399. {
  400. int i;
  401. for (i = 0; i < mci->nr_csrows; i++) {
  402. if (device_is_registered(&mci->csrows[i]->dev))
  403. device_unregister(&mci->csrows[i]->dev);
  404. }
  405. }
  406. #endif
  407. /*
  408. * Per-dimm (or per-rank) devices
  409. */
  410. #define to_dimm(k) container_of(k, struct dimm_info, dev)
  411. /* show/store functions for DIMM Label attributes */
  412. static ssize_t dimmdev_location_show(struct device *dev,
  413. struct device_attribute *mattr, char *data)
  414. {
  415. struct dimm_info *dimm = to_dimm(dev);
  416. ssize_t count;
  417. count = edac_dimm_info_location(dimm, data, PAGE_SIZE);
  418. count += scnprintf(data + count, PAGE_SIZE - count, "\n");
  419. return count;
  420. }
  421. static ssize_t dimmdev_label_show(struct device *dev,
  422. struct device_attribute *mattr, char *data)
  423. {
  424. struct dimm_info *dimm = to_dimm(dev);
  425. /* if field has not been initialized, there is nothing to send */
  426. if (!dimm->label[0])
  427. return 0;
  428. return sysfs_emit(data, "%s\n", dimm->label);
  429. }
  430. static ssize_t dimmdev_label_store(struct device *dev,
  431. struct device_attribute *mattr,
  432. const char *data,
  433. size_t count)
  434. {
  435. struct dimm_info *dimm = to_dimm(dev);
  436. size_t copy_count = count;
  437. if (count == 0)
  438. return -EINVAL;
  439. if (data[count - 1] == '\0' || data[count - 1] == '\n')
  440. copy_count -= 1;
  441. if (copy_count == 0 || copy_count >= sizeof(dimm->label))
  442. return -EINVAL;
  443. memcpy(dimm->label, data, copy_count);
  444. dimm->label[copy_count] = '\0';
  445. return count;
  446. }
  447. static ssize_t dimmdev_size_show(struct device *dev,
  448. struct device_attribute *mattr, char *data)
  449. {
  450. struct dimm_info *dimm = to_dimm(dev);
  451. return sysfs_emit(data, "%u\n", PAGES_TO_MiB(dimm->nr_pages));
  452. }
  453. static ssize_t dimmdev_mem_type_show(struct device *dev,
  454. struct device_attribute *mattr, char *data)
  455. {
  456. struct dimm_info *dimm = to_dimm(dev);
  457. return sysfs_emit(data, "%s\n", edac_mem_types[dimm->mtype]);
  458. }
  459. static ssize_t dimmdev_dev_type_show(struct device *dev,
  460. struct device_attribute *mattr, char *data)
  461. {
  462. struct dimm_info *dimm = to_dimm(dev);
  463. return sysfs_emit(data, "%s\n", dev_types[dimm->dtype]);
  464. }
  465. static ssize_t dimmdev_edac_mode_show(struct device *dev,
  466. struct device_attribute *mattr,
  467. char *data)
  468. {
  469. struct dimm_info *dimm = to_dimm(dev);
  470. return sysfs_emit(data, "%s\n", edac_caps[dimm->edac_mode]);
  471. }
  472. static ssize_t dimmdev_ce_count_show(struct device *dev,
  473. struct device_attribute *mattr,
  474. char *data)
  475. {
  476. struct dimm_info *dimm = to_dimm(dev);
  477. return sysfs_emit(data, "%u\n", dimm->ce_count);
  478. }
  479. static ssize_t dimmdev_ue_count_show(struct device *dev,
  480. struct device_attribute *mattr,
  481. char *data)
  482. {
  483. struct dimm_info *dimm = to_dimm(dev);
  484. return sysfs_emit(data, "%u\n", dimm->ue_count);
  485. }
  486. /* dimm/rank attribute files */
  487. static DEVICE_ATTR(dimm_label, S_IRUGO | S_IWUSR,
  488. dimmdev_label_show, dimmdev_label_store);
  489. static DEVICE_ATTR(dimm_location, S_IRUGO, dimmdev_location_show, NULL);
  490. static DEVICE_ATTR(size, S_IRUGO, dimmdev_size_show, NULL);
  491. static DEVICE_ATTR(dimm_mem_type, S_IRUGO, dimmdev_mem_type_show, NULL);
  492. static DEVICE_ATTR(dimm_dev_type, S_IRUGO, dimmdev_dev_type_show, NULL);
  493. static DEVICE_ATTR(dimm_edac_mode, S_IRUGO, dimmdev_edac_mode_show, NULL);
  494. static DEVICE_ATTR(dimm_ce_count, S_IRUGO, dimmdev_ce_count_show, NULL);
  495. static DEVICE_ATTR(dimm_ue_count, S_IRUGO, dimmdev_ue_count_show, NULL);
  496. /* attributes of the dimm<id>/rank<id> object */
  497. static struct attribute *dimm_attrs[] = {
  498. &dev_attr_dimm_label.attr,
  499. &dev_attr_dimm_location.attr,
  500. &dev_attr_size.attr,
  501. &dev_attr_dimm_mem_type.attr,
  502. &dev_attr_dimm_dev_type.attr,
  503. &dev_attr_dimm_edac_mode.attr,
  504. &dev_attr_dimm_ce_count.attr,
  505. &dev_attr_dimm_ue_count.attr,
  506. NULL,
  507. };
  508. static const struct attribute_group dimm_attr_grp = {
  509. .attrs = dimm_attrs,
  510. };
  511. static const struct attribute_group *dimm_attr_groups[] = {
  512. &dimm_attr_grp,
  513. NULL
  514. };
  515. static const struct device_type dimm_attr_type = {
  516. .groups = dimm_attr_groups,
  517. };
  518. static void dimm_release(struct device *dev)
  519. {
  520. /*
  521. * Nothing to do, just unregister sysfs here. The mci
  522. * device owns the data and will also release it.
  523. */
  524. }
  525. /* Create a DIMM object under specifed memory controller device */
  526. static int edac_create_dimm_object(struct mem_ctl_info *mci,
  527. struct dimm_info *dimm)
  528. {
  529. int err;
  530. dimm->mci = mci;
  531. dimm->dev.type = &dimm_attr_type;
  532. dimm->dev.release = dimm_release;
  533. device_initialize(&dimm->dev);
  534. dimm->dev.parent = &mci->dev;
  535. if (mci->csbased)
  536. dev_set_name(&dimm->dev, "rank%d", dimm->idx);
  537. else
  538. dev_set_name(&dimm->dev, "dimm%d", dimm->idx);
  539. dev_set_drvdata(&dimm->dev, dimm);
  540. pm_runtime_forbid(&mci->dev);
  541. err = device_add(&dimm->dev);
  542. if (err) {
  543. edac_dbg(1, "failure: create device %s\n", dev_name(&dimm->dev));
  544. put_device(&dimm->dev);
  545. return err;
  546. }
  547. if (IS_ENABLED(CONFIG_EDAC_DEBUG)) {
  548. char location[80];
  549. edac_dimm_info_location(dimm, location, sizeof(location));
  550. edac_dbg(0, "device %s created at location %s\n",
  551. dev_name(&dimm->dev), location);
  552. }
  553. return 0;
  554. }
  555. /*
  556. * Memory controller device
  557. */
  558. #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
  559. static ssize_t mci_reset_counters_store(struct device *dev,
  560. struct device_attribute *mattr,
  561. const char *data, size_t count)
  562. {
  563. struct mem_ctl_info *mci = to_mci(dev);
  564. struct dimm_info *dimm;
  565. int row, chan;
  566. mci->ue_mc = 0;
  567. mci->ce_mc = 0;
  568. mci->ue_noinfo_count = 0;
  569. mci->ce_noinfo_count = 0;
  570. for (row = 0; row < mci->nr_csrows; row++) {
  571. struct csrow_info *ri = mci->csrows[row];
  572. ri->ue_count = 0;
  573. ri->ce_count = 0;
  574. for (chan = 0; chan < ri->nr_channels; chan++)
  575. ri->channels[chan]->ce_count = 0;
  576. }
  577. mci_for_each_dimm(mci, dimm) {
  578. dimm->ue_count = 0;
  579. dimm->ce_count = 0;
  580. }
  581. mci->start_time = jiffies;
  582. return count;
  583. }
  584. /* Memory scrubbing interface:
  585. *
  586. * A MC driver can limit the scrubbing bandwidth based on the CPU type.
  587. * Therefore, ->set_sdram_scrub_rate should be made to return the actual
  588. * bandwidth that is accepted or 0 when scrubbing is to be disabled.
  589. *
  590. * Negative value still means that an error has occurred while setting
  591. * the scrub rate.
  592. */
  593. static ssize_t mci_sdram_scrub_rate_store(struct device *dev,
  594. struct device_attribute *mattr,
  595. const char *data, size_t count)
  596. {
  597. struct mem_ctl_info *mci = to_mci(dev);
  598. unsigned long bandwidth = 0;
  599. int new_bw = 0;
  600. if (kstrtoul(data, 10, &bandwidth) < 0)
  601. return -EINVAL;
  602. new_bw = mci->set_sdram_scrub_rate(mci, bandwidth);
  603. if (new_bw < 0) {
  604. edac_printk(KERN_WARNING, EDAC_MC,
  605. "Error setting scrub rate to: %lu\n", bandwidth);
  606. return -EINVAL;
  607. }
  608. return count;
  609. }
  610. /*
  611. * ->get_sdram_scrub_rate() return value semantics same as above.
  612. */
  613. static ssize_t mci_sdram_scrub_rate_show(struct device *dev,
  614. struct device_attribute *mattr,
  615. char *data)
  616. {
  617. struct mem_ctl_info *mci = to_mci(dev);
  618. int bandwidth = 0;
  619. bandwidth = mci->get_sdram_scrub_rate(mci);
  620. if (bandwidth < 0) {
  621. edac_printk(KERN_DEBUG, EDAC_MC, "Error reading scrub rate\n");
  622. return bandwidth;
  623. }
  624. return sysfs_emit(data, "%d\n", bandwidth);
  625. }
  626. /* default attribute files for the MCI object */
  627. static ssize_t mci_ue_count_show(struct device *dev,
  628. struct device_attribute *mattr,
  629. char *data)
  630. {
  631. struct mem_ctl_info *mci = to_mci(dev);
  632. return sysfs_emit(data, "%u\n", mci->ue_mc);
  633. }
  634. static ssize_t mci_ce_count_show(struct device *dev,
  635. struct device_attribute *mattr,
  636. char *data)
  637. {
  638. struct mem_ctl_info *mci = to_mci(dev);
  639. return sysfs_emit(data, "%u\n", mci->ce_mc);
  640. }
  641. static ssize_t mci_ce_noinfo_show(struct device *dev,
  642. struct device_attribute *mattr,
  643. char *data)
  644. {
  645. struct mem_ctl_info *mci = to_mci(dev);
  646. return sysfs_emit(data, "%u\n", mci->ce_noinfo_count);
  647. }
  648. static ssize_t mci_ue_noinfo_show(struct device *dev,
  649. struct device_attribute *mattr,
  650. char *data)
  651. {
  652. struct mem_ctl_info *mci = to_mci(dev);
  653. return sysfs_emit(data, "%u\n", mci->ue_noinfo_count);
  654. }
  655. static ssize_t mci_seconds_show(struct device *dev,
  656. struct device_attribute *mattr,
  657. char *data)
  658. {
  659. struct mem_ctl_info *mci = to_mci(dev);
  660. return sysfs_emit(data, "%ld\n", (jiffies - mci->start_time) / HZ);
  661. }
  662. static ssize_t mci_ctl_name_show(struct device *dev,
  663. struct device_attribute *mattr,
  664. char *data)
  665. {
  666. struct mem_ctl_info *mci = to_mci(dev);
  667. return sysfs_emit(data, "%s\n", mci->ctl_name);
  668. }
  669. static ssize_t mci_size_mb_show(struct device *dev,
  670. struct device_attribute *mattr,
  671. char *data)
  672. {
  673. struct mem_ctl_info *mci = to_mci(dev);
  674. int total_pages = 0, csrow_idx, j;
  675. for (csrow_idx = 0; csrow_idx < mci->nr_csrows; csrow_idx++) {
  676. struct csrow_info *csrow = mci->csrows[csrow_idx];
  677. for (j = 0; j < csrow->nr_channels; j++) {
  678. struct dimm_info *dimm = csrow->channels[j]->dimm;
  679. total_pages += dimm->nr_pages;
  680. }
  681. }
  682. return sysfs_emit(data, "%u\n", PAGES_TO_MiB(total_pages));
  683. }
  684. static ssize_t mci_max_location_show(struct device *dev,
  685. struct device_attribute *mattr,
  686. char *data)
  687. {
  688. struct mem_ctl_info *mci = to_mci(dev);
  689. int len = PAGE_SIZE;
  690. char *p = data;
  691. int i, n;
  692. for (i = 0; i < mci->n_layers; i++) {
  693. n = scnprintf(p, len, "%s %d ",
  694. edac_layer_name[mci->layers[i].type],
  695. mci->layers[i].size - 1);
  696. len -= n;
  697. if (len <= 0)
  698. goto out;
  699. p += n;
  700. }
  701. p += scnprintf(p, len, "\n");
  702. out:
  703. return p - data;
  704. }
  705. /* default Control file */
  706. static DEVICE_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
  707. /* default Attribute files */
  708. static DEVICE_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
  709. static DEVICE_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
  710. static DEVICE_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
  711. static DEVICE_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
  712. static DEVICE_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
  713. static DEVICE_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
  714. static DEVICE_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
  715. static DEVICE_ATTR(max_location, S_IRUGO, mci_max_location_show, NULL);
  716. /* memory scrubber attribute file */
  717. static DEVICE_ATTR(sdram_scrub_rate, 0, mci_sdram_scrub_rate_show,
  718. mci_sdram_scrub_rate_store); /* umode set later in is_visible */
  719. static struct attribute *mci_attrs[] = {
  720. &dev_attr_reset_counters.attr,
  721. &dev_attr_mc_name.attr,
  722. &dev_attr_size_mb.attr,
  723. &dev_attr_seconds_since_reset.attr,
  724. &dev_attr_ue_noinfo_count.attr,
  725. &dev_attr_ce_noinfo_count.attr,
  726. &dev_attr_ue_count.attr,
  727. &dev_attr_ce_count.attr,
  728. &dev_attr_max_location.attr,
  729. &dev_attr_sdram_scrub_rate.attr,
  730. NULL
  731. };
  732. static umode_t mci_attr_is_visible(struct kobject *kobj,
  733. struct attribute *attr, int idx)
  734. {
  735. struct device *dev = kobj_to_dev(kobj);
  736. struct mem_ctl_info *mci = to_mci(dev);
  737. umode_t mode = 0;
  738. if (attr != &dev_attr_sdram_scrub_rate.attr)
  739. return attr->mode;
  740. if (mci->get_sdram_scrub_rate)
  741. mode |= S_IRUGO;
  742. if (mci->set_sdram_scrub_rate)
  743. mode |= S_IWUSR;
  744. return mode;
  745. }
  746. static const struct attribute_group mci_attr_grp = {
  747. .attrs = mci_attrs,
  748. .is_visible = mci_attr_is_visible,
  749. };
  750. static const struct attribute_group *mci_attr_groups[] = {
  751. &mci_attr_grp,
  752. NULL
  753. };
  754. static const struct device_type mci_attr_type = {
  755. .groups = mci_attr_groups,
  756. };
  757. /*
  758. * Create a new Memory Controller kobject instance,
  759. * mc<id> under the 'mc' directory
  760. *
  761. * Return:
  762. * 0 Success
  763. * !0 Failure
  764. */
  765. int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
  766. const struct attribute_group **groups)
  767. {
  768. struct dimm_info *dimm;
  769. int err;
  770. /* get the /sys/devices/system/edac subsys reference */
  771. mci->dev.type = &mci_attr_type;
  772. mci->dev.parent = mci_pdev;
  773. mci->dev.groups = groups;
  774. dev_set_name(&mci->dev, "mc%d", mci->mc_idx);
  775. dev_set_drvdata(&mci->dev, mci);
  776. pm_runtime_forbid(&mci->dev);
  777. err = device_add(&mci->dev);
  778. if (err < 0) {
  779. edac_dbg(1, "failure: create device %s\n", dev_name(&mci->dev));
  780. /* no put_device() here, free mci with _edac_mc_free() */
  781. return err;
  782. }
  783. edac_dbg(0, "device %s created\n", dev_name(&mci->dev));
  784. /*
  785. * Create the dimm/rank devices
  786. */
  787. mci_for_each_dimm(mci, dimm) {
  788. /* Only expose populated DIMMs */
  789. if (!dimm->nr_pages)
  790. continue;
  791. err = edac_create_dimm_object(mci, dimm);
  792. if (err)
  793. goto fail;
  794. }
  795. #ifdef CONFIG_EDAC_LEGACY_SYSFS
  796. err = edac_create_csrow_objects(mci);
  797. if (err < 0)
  798. goto fail;
  799. #endif
  800. edac_create_debugfs_nodes(mci);
  801. return 0;
  802. fail:
  803. edac_remove_sysfs_mci_device(mci);
  804. return err;
  805. }
  806. /*
  807. * remove a Memory Controller instance
  808. */
  809. void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
  810. {
  811. struct dimm_info *dimm;
  812. if (!device_is_registered(&mci->dev))
  813. return;
  814. edac_dbg(0, "\n");
  815. #ifdef CONFIG_EDAC_DEBUG
  816. edac_debugfs_remove_recursive(mci->debugfs);
  817. #endif
  818. #ifdef CONFIG_EDAC_LEGACY_SYSFS
  819. edac_delete_csrow_objects(mci);
  820. #endif
  821. mci_for_each_dimm(mci, dimm) {
  822. if (!device_is_registered(&dimm->dev))
  823. continue;
  824. edac_dbg(1, "unregistering device %s\n", dev_name(&dimm->dev));
  825. device_unregister(&dimm->dev);
  826. }
  827. /* only remove the device, but keep mci */
  828. device_del(&mci->dev);
  829. }
  830. static void mc_attr_release(struct device *dev)
  831. {
  832. /*
  833. * There's no container structure here, as this is just the mci
  834. * parent device, used to create the /sys/devices/mc sysfs node.
  835. * So, there are no attributes on it.
  836. */
  837. edac_dbg(1, "device %s released\n", dev_name(dev));
  838. kfree(dev);
  839. }
  840. /*
  841. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  842. */
  843. int __init edac_mc_sysfs_init(void)
  844. {
  845. int err;
  846. mci_pdev = kzalloc(sizeof(*mci_pdev), GFP_KERNEL);
  847. if (!mci_pdev)
  848. return -ENOMEM;
  849. mci_pdev->bus = edac_get_sysfs_subsys();
  850. mci_pdev->release = mc_attr_release;
  851. mci_pdev->init_name = "mc";
  852. err = device_register(mci_pdev);
  853. if (err < 0) {
  854. edac_dbg(1, "failure: create device %s\n", dev_name(mci_pdev));
  855. put_device(mci_pdev);
  856. return err;
  857. }
  858. edac_dbg(0, "device %s created\n", dev_name(mci_pdev));
  859. return 0;
  860. }
  861. void edac_mc_sysfs_exit(void)
  862. {
  863. device_unregister(mci_pdev);
  864. }