edac_mc_sysfs.c 27 KB

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