edac_mc_sysfs.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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 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 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 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 int 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 int 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 const struct device_type csrow_attr_type = {
  226. .groups = csrow_attr_groups,
  227. };
  228. /*
  229. * possible dynamic channel DIMM Label attribute files
  230. *
  231. */
  232. DEVICE_CHANNEL(ch0_dimm_label, S_IRUGO | S_IWUSR,
  233. channel_dimm_label_show, channel_dimm_label_store, 0);
  234. DEVICE_CHANNEL(ch1_dimm_label, S_IRUGO | S_IWUSR,
  235. channel_dimm_label_show, channel_dimm_label_store, 1);
  236. DEVICE_CHANNEL(ch2_dimm_label, S_IRUGO | S_IWUSR,
  237. channel_dimm_label_show, channel_dimm_label_store, 2);
  238. DEVICE_CHANNEL(ch3_dimm_label, S_IRUGO | S_IWUSR,
  239. channel_dimm_label_show, channel_dimm_label_store, 3);
  240. DEVICE_CHANNEL(ch4_dimm_label, S_IRUGO | S_IWUSR,
  241. channel_dimm_label_show, channel_dimm_label_store, 4);
  242. DEVICE_CHANNEL(ch5_dimm_label, S_IRUGO | S_IWUSR,
  243. channel_dimm_label_show, channel_dimm_label_store, 5);
  244. DEVICE_CHANNEL(ch6_dimm_label, S_IRUGO | S_IWUSR,
  245. channel_dimm_label_show, channel_dimm_label_store, 6);
  246. DEVICE_CHANNEL(ch7_dimm_label, S_IRUGO | S_IWUSR,
  247. channel_dimm_label_show, channel_dimm_label_store, 7);
  248. /* Total possible dynamic DIMM Label attribute file table */
  249. static struct attribute *dynamic_csrow_dimm_attr[] = {
  250. &dev_attr_legacy_ch0_dimm_label.attr.attr,
  251. &dev_attr_legacy_ch1_dimm_label.attr.attr,
  252. &dev_attr_legacy_ch2_dimm_label.attr.attr,
  253. &dev_attr_legacy_ch3_dimm_label.attr.attr,
  254. &dev_attr_legacy_ch4_dimm_label.attr.attr,
  255. &dev_attr_legacy_ch5_dimm_label.attr.attr,
  256. &dev_attr_legacy_ch6_dimm_label.attr.attr,
  257. &dev_attr_legacy_ch7_dimm_label.attr.attr,
  258. NULL
  259. };
  260. /* possible dynamic channel ce_count attribute files */
  261. DEVICE_CHANNEL(ch0_ce_count, S_IRUGO,
  262. channel_ce_count_show, NULL, 0);
  263. DEVICE_CHANNEL(ch1_ce_count, S_IRUGO,
  264. channel_ce_count_show, NULL, 1);
  265. DEVICE_CHANNEL(ch2_ce_count, S_IRUGO,
  266. channel_ce_count_show, NULL, 2);
  267. DEVICE_CHANNEL(ch3_ce_count, S_IRUGO,
  268. channel_ce_count_show, NULL, 3);
  269. DEVICE_CHANNEL(ch4_ce_count, S_IRUGO,
  270. channel_ce_count_show, NULL, 4);
  271. DEVICE_CHANNEL(ch5_ce_count, S_IRUGO,
  272. channel_ce_count_show, NULL, 5);
  273. DEVICE_CHANNEL(ch6_ce_count, S_IRUGO,
  274. channel_ce_count_show, NULL, 6);
  275. DEVICE_CHANNEL(ch7_ce_count, S_IRUGO,
  276. channel_ce_count_show, NULL, 7);
  277. /* Total possible dynamic ce_count attribute file table */
  278. static struct attribute *dynamic_csrow_ce_count_attr[] = {
  279. &dev_attr_legacy_ch0_ce_count.attr.attr,
  280. &dev_attr_legacy_ch1_ce_count.attr.attr,
  281. &dev_attr_legacy_ch2_ce_count.attr.attr,
  282. &dev_attr_legacy_ch3_ce_count.attr.attr,
  283. &dev_attr_legacy_ch4_ce_count.attr.attr,
  284. &dev_attr_legacy_ch5_ce_count.attr.attr,
  285. &dev_attr_legacy_ch6_ce_count.attr.attr,
  286. &dev_attr_legacy_ch7_ce_count.attr.attr,
  287. NULL
  288. };
  289. static umode_t csrow_dev_is_visible(struct kobject *kobj,
  290. struct attribute *attr, int idx)
  291. {
  292. struct device *dev = kobj_to_dev(kobj);
  293. struct csrow_info *csrow = container_of(dev, struct csrow_info, dev);
  294. if (idx >= csrow->nr_channels)
  295. return 0;
  296. if (idx >= ARRAY_SIZE(dynamic_csrow_ce_count_attr) - 1) {
  297. WARN_ONCE(1, "idx: %d\n", idx);
  298. return 0;
  299. }
  300. /* Only expose populated DIMMs */
  301. if (!csrow->channels[idx]->dimm->nr_pages)
  302. return 0;
  303. return attr->mode;
  304. }
  305. static const struct attribute_group csrow_dev_dimm_group = {
  306. .attrs = dynamic_csrow_dimm_attr,
  307. .is_visible = csrow_dev_is_visible,
  308. };
  309. static const struct attribute_group csrow_dev_ce_count_group = {
  310. .attrs = dynamic_csrow_ce_count_attr,
  311. .is_visible = csrow_dev_is_visible,
  312. };
  313. static const struct attribute_group *csrow_dev_groups[] = {
  314. &csrow_dev_dimm_group,
  315. &csrow_dev_ce_count_group,
  316. NULL
  317. };
  318. static void csrow_release(struct device *dev)
  319. {
  320. /*
  321. * Nothing to do, just unregister sysfs here. The mci
  322. * device owns the data and will also release it.
  323. */
  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. int err;
  337. csrow->dev.type = &csrow_attr_type;
  338. csrow->dev.groups = csrow_dev_groups;
  339. csrow->dev.release = csrow_release;
  340. device_initialize(&csrow->dev);
  341. csrow->dev.parent = &mci->dev;
  342. csrow->mci = mci;
  343. dev_set_name(&csrow->dev, "csrow%d", index);
  344. dev_set_drvdata(&csrow->dev, csrow);
  345. err = device_add(&csrow->dev);
  346. if (err) {
  347. edac_dbg(1, "failure: create device %s\n", dev_name(&csrow->dev));
  348. put_device(&csrow->dev);
  349. return err;
  350. }
  351. edac_dbg(0, "device %s created\n", dev_name(&csrow->dev));
  352. return 0;
  353. }
  354. /* Create a CSROW object under specifed edac_mc_device */
  355. static int edac_create_csrow_objects(struct mem_ctl_info *mci)
  356. {
  357. int err, i;
  358. struct csrow_info *csrow;
  359. for (i = 0; i < mci->nr_csrows; i++) {
  360. csrow = mci->csrows[i];
  361. if (!nr_pages_per_csrow(csrow))
  362. continue;
  363. err = edac_create_csrow_object(mci, mci->csrows[i], i);
  364. if (err < 0)
  365. goto error;
  366. }
  367. return 0;
  368. error:
  369. for (--i; i >= 0; i--) {
  370. if (device_is_registered(&mci->csrows[i]->dev))
  371. device_unregister(&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. for (i = 0; i < mci->nr_csrows; i++) {
  379. if (device_is_registered(&mci->csrows[i]->dev))
  380. device_unregister(&mci->csrows[i]->dev);
  381. }
  382. }
  383. #endif
  384. /*
  385. * Per-dimm (or per-rank) devices
  386. */
  387. #define to_dimm(k) container_of(k, struct dimm_info, dev)
  388. /* show/store functions for DIMM Label attributes */
  389. static ssize_t dimmdev_location_show(struct device *dev,
  390. struct device_attribute *mattr, char *data)
  391. {
  392. struct dimm_info *dimm = to_dimm(dev);
  393. ssize_t count;
  394. count = edac_dimm_info_location(dimm, data, PAGE_SIZE);
  395. count += scnprintf(data + count, PAGE_SIZE - count, "\n");
  396. return count;
  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. return sprintf(data, "%u\n", dimm->ce_count);
  455. }
  456. static ssize_t dimmdev_ue_count_show(struct device *dev,
  457. struct device_attribute *mattr,
  458. char *data)
  459. {
  460. struct dimm_info *dimm = to_dimm(dev);
  461. return sprintf(data, "%u\n", dimm->ue_count);
  462. }
  463. /* dimm/rank attribute files */
  464. static DEVICE_ATTR(dimm_label, S_IRUGO | S_IWUSR,
  465. dimmdev_label_show, dimmdev_label_store);
  466. static DEVICE_ATTR(dimm_location, S_IRUGO, dimmdev_location_show, NULL);
  467. static DEVICE_ATTR(size, S_IRUGO, dimmdev_size_show, NULL);
  468. static DEVICE_ATTR(dimm_mem_type, S_IRUGO, dimmdev_mem_type_show, NULL);
  469. static DEVICE_ATTR(dimm_dev_type, S_IRUGO, dimmdev_dev_type_show, NULL);
  470. static DEVICE_ATTR(dimm_edac_mode, S_IRUGO, dimmdev_edac_mode_show, NULL);
  471. static DEVICE_ATTR(dimm_ce_count, S_IRUGO, dimmdev_ce_count_show, NULL);
  472. static DEVICE_ATTR(dimm_ue_count, S_IRUGO, dimmdev_ue_count_show, NULL);
  473. /* attributes of the dimm<id>/rank<id> object */
  474. static struct attribute *dimm_attrs[] = {
  475. &dev_attr_dimm_label.attr,
  476. &dev_attr_dimm_location.attr,
  477. &dev_attr_size.attr,
  478. &dev_attr_dimm_mem_type.attr,
  479. &dev_attr_dimm_dev_type.attr,
  480. &dev_attr_dimm_edac_mode.attr,
  481. &dev_attr_dimm_ce_count.attr,
  482. &dev_attr_dimm_ue_count.attr,
  483. NULL,
  484. };
  485. static const struct attribute_group dimm_attr_grp = {
  486. .attrs = dimm_attrs,
  487. };
  488. static const struct attribute_group *dimm_attr_groups[] = {
  489. &dimm_attr_grp,
  490. NULL
  491. };
  492. static const struct device_type dimm_attr_type = {
  493. .groups = dimm_attr_groups,
  494. };
  495. static void dimm_release(struct device *dev)
  496. {
  497. /*
  498. * Nothing to do, just unregister sysfs here. The mci
  499. * device owns the data and will also release it.
  500. */
  501. }
  502. /* Create a DIMM object under specifed memory controller device */
  503. static int edac_create_dimm_object(struct mem_ctl_info *mci,
  504. struct dimm_info *dimm)
  505. {
  506. int err;
  507. dimm->mci = mci;
  508. dimm->dev.type = &dimm_attr_type;
  509. dimm->dev.release = dimm_release;
  510. device_initialize(&dimm->dev);
  511. dimm->dev.parent = &mci->dev;
  512. if (mci->csbased)
  513. dev_set_name(&dimm->dev, "rank%d", dimm->idx);
  514. else
  515. dev_set_name(&dimm->dev, "dimm%d", dimm->idx);
  516. dev_set_drvdata(&dimm->dev, dimm);
  517. pm_runtime_forbid(&mci->dev);
  518. err = device_add(&dimm->dev);
  519. if (err) {
  520. edac_dbg(1, "failure: create device %s\n", dev_name(&dimm->dev));
  521. put_device(&dimm->dev);
  522. return err;
  523. }
  524. if (IS_ENABLED(CONFIG_EDAC_DEBUG)) {
  525. char location[80];
  526. edac_dimm_info_location(dimm, location, sizeof(location));
  527. edac_dbg(0, "device %s created at location %s\n",
  528. dev_name(&dimm->dev), location);
  529. }
  530. return 0;
  531. }
  532. /*
  533. * Memory controller device
  534. */
  535. #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
  536. static ssize_t mci_reset_counters_store(struct device *dev,
  537. struct device_attribute *mattr,
  538. const char *data, size_t count)
  539. {
  540. struct mem_ctl_info *mci = to_mci(dev);
  541. struct dimm_info *dimm;
  542. int row, chan;
  543. mci->ue_mc = 0;
  544. mci->ce_mc = 0;
  545. mci->ue_noinfo_count = 0;
  546. mci->ce_noinfo_count = 0;
  547. for (row = 0; row < mci->nr_csrows; row++) {
  548. struct csrow_info *ri = mci->csrows[row];
  549. ri->ue_count = 0;
  550. ri->ce_count = 0;
  551. for (chan = 0; chan < ri->nr_channels; chan++)
  552. ri->channels[chan]->ce_count = 0;
  553. }
  554. mci_for_each_dimm(mci, dimm) {
  555. dimm->ue_count = 0;
  556. dimm->ce_count = 0;
  557. }
  558. mci->start_time = jiffies;
  559. return count;
  560. }
  561. /* Memory scrubbing interface:
  562. *
  563. * A MC driver can limit the scrubbing bandwidth based on the CPU type.
  564. * Therefore, ->set_sdram_scrub_rate should be made to return the actual
  565. * bandwidth that is accepted or 0 when scrubbing is to be disabled.
  566. *
  567. * Negative value still means that an error has occurred while setting
  568. * the scrub rate.
  569. */
  570. static ssize_t mci_sdram_scrub_rate_store(struct device *dev,
  571. struct device_attribute *mattr,
  572. const char *data, size_t count)
  573. {
  574. struct mem_ctl_info *mci = to_mci(dev);
  575. unsigned long bandwidth = 0;
  576. int new_bw = 0;
  577. if (kstrtoul(data, 10, &bandwidth) < 0)
  578. return -EINVAL;
  579. new_bw = mci->set_sdram_scrub_rate(mci, bandwidth);
  580. if (new_bw < 0) {
  581. edac_printk(KERN_WARNING, EDAC_MC,
  582. "Error setting scrub rate to: %lu\n", bandwidth);
  583. return -EINVAL;
  584. }
  585. return count;
  586. }
  587. /*
  588. * ->get_sdram_scrub_rate() return value semantics same as above.
  589. */
  590. static ssize_t mci_sdram_scrub_rate_show(struct device *dev,
  591. struct device_attribute *mattr,
  592. char *data)
  593. {
  594. struct mem_ctl_info *mci = to_mci(dev);
  595. int bandwidth = 0;
  596. bandwidth = mci->get_sdram_scrub_rate(mci);
  597. if (bandwidth < 0) {
  598. edac_printk(KERN_DEBUG, EDAC_MC, "Error reading scrub rate\n");
  599. return bandwidth;
  600. }
  601. return sprintf(data, "%d\n", bandwidth);
  602. }
  603. /* default attribute files for the MCI object */
  604. static ssize_t mci_ue_count_show(struct device *dev,
  605. struct device_attribute *mattr,
  606. char *data)
  607. {
  608. struct mem_ctl_info *mci = to_mci(dev);
  609. return sprintf(data, "%d\n", mci->ue_mc);
  610. }
  611. static ssize_t mci_ce_count_show(struct device *dev,
  612. struct device_attribute *mattr,
  613. char *data)
  614. {
  615. struct mem_ctl_info *mci = to_mci(dev);
  616. return sprintf(data, "%d\n", mci->ce_mc);
  617. }
  618. static ssize_t mci_ce_noinfo_show(struct device *dev,
  619. struct device_attribute *mattr,
  620. char *data)
  621. {
  622. struct mem_ctl_info *mci = to_mci(dev);
  623. return sprintf(data, "%d\n", mci->ce_noinfo_count);
  624. }
  625. static ssize_t mci_ue_noinfo_show(struct device *dev,
  626. struct device_attribute *mattr,
  627. char *data)
  628. {
  629. struct mem_ctl_info *mci = to_mci(dev);
  630. return sprintf(data, "%d\n", mci->ue_noinfo_count);
  631. }
  632. static ssize_t mci_seconds_show(struct device *dev,
  633. struct device_attribute *mattr,
  634. char *data)
  635. {
  636. struct mem_ctl_info *mci = to_mci(dev);
  637. return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
  638. }
  639. static ssize_t mci_ctl_name_show(struct device *dev,
  640. struct device_attribute *mattr,
  641. char *data)
  642. {
  643. struct mem_ctl_info *mci = to_mci(dev);
  644. return sprintf(data, "%s\n", mci->ctl_name);
  645. }
  646. static ssize_t mci_size_mb_show(struct device *dev,
  647. struct device_attribute *mattr,
  648. char *data)
  649. {
  650. struct mem_ctl_info *mci = to_mci(dev);
  651. int total_pages = 0, csrow_idx, j;
  652. for (csrow_idx = 0; csrow_idx < mci->nr_csrows; csrow_idx++) {
  653. struct csrow_info *csrow = mci->csrows[csrow_idx];
  654. for (j = 0; j < csrow->nr_channels; j++) {
  655. struct dimm_info *dimm = csrow->channels[j]->dimm;
  656. total_pages += dimm->nr_pages;
  657. }
  658. }
  659. return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
  660. }
  661. static ssize_t mci_max_location_show(struct device *dev,
  662. struct device_attribute *mattr,
  663. char *data)
  664. {
  665. struct mem_ctl_info *mci = to_mci(dev);
  666. int len = PAGE_SIZE;
  667. char *p = data;
  668. int i, n;
  669. for (i = 0; i < mci->n_layers; i++) {
  670. n = scnprintf(p, len, "%s %d ",
  671. edac_layer_name[mci->layers[i].type],
  672. mci->layers[i].size - 1);
  673. len -= n;
  674. if (len <= 0)
  675. goto out;
  676. p += n;
  677. }
  678. p += scnprintf(p, len, "\n");
  679. out:
  680. return p - data;
  681. }
  682. /* default Control file */
  683. static DEVICE_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
  684. /* default Attribute files */
  685. static DEVICE_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
  686. static DEVICE_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
  687. static DEVICE_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
  688. static DEVICE_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
  689. static DEVICE_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
  690. static DEVICE_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
  691. static DEVICE_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
  692. static DEVICE_ATTR(max_location, S_IRUGO, mci_max_location_show, NULL);
  693. /* memory scrubber attribute file */
  694. static DEVICE_ATTR(sdram_scrub_rate, 0, mci_sdram_scrub_rate_show,
  695. mci_sdram_scrub_rate_store); /* umode set later in is_visible */
  696. static struct attribute *mci_attrs[] = {
  697. &dev_attr_reset_counters.attr,
  698. &dev_attr_mc_name.attr,
  699. &dev_attr_size_mb.attr,
  700. &dev_attr_seconds_since_reset.attr,
  701. &dev_attr_ue_noinfo_count.attr,
  702. &dev_attr_ce_noinfo_count.attr,
  703. &dev_attr_ue_count.attr,
  704. &dev_attr_ce_count.attr,
  705. &dev_attr_max_location.attr,
  706. &dev_attr_sdram_scrub_rate.attr,
  707. NULL
  708. };
  709. static umode_t mci_attr_is_visible(struct kobject *kobj,
  710. struct attribute *attr, int idx)
  711. {
  712. struct device *dev = kobj_to_dev(kobj);
  713. struct mem_ctl_info *mci = to_mci(dev);
  714. umode_t mode = 0;
  715. if (attr != &dev_attr_sdram_scrub_rate.attr)
  716. return attr->mode;
  717. if (mci->get_sdram_scrub_rate)
  718. mode |= S_IRUGO;
  719. if (mci->set_sdram_scrub_rate)
  720. mode |= S_IWUSR;
  721. return mode;
  722. }
  723. static const struct attribute_group mci_attr_grp = {
  724. .attrs = mci_attrs,
  725. .is_visible = mci_attr_is_visible,
  726. };
  727. static const struct attribute_group *mci_attr_groups[] = {
  728. &mci_attr_grp,
  729. NULL
  730. };
  731. static const struct device_type mci_attr_type = {
  732. .groups = mci_attr_groups,
  733. };
  734. /*
  735. * Create a new Memory Controller kobject instance,
  736. * mc<id> under the 'mc' directory
  737. *
  738. * Return:
  739. * 0 Success
  740. * !0 Failure
  741. */
  742. int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
  743. const struct attribute_group **groups)
  744. {
  745. struct dimm_info *dimm;
  746. int err;
  747. /* get the /sys/devices/system/edac subsys reference */
  748. mci->dev.type = &mci_attr_type;
  749. mci->dev.parent = mci_pdev;
  750. mci->dev.groups = groups;
  751. dev_set_name(&mci->dev, "mc%d", mci->mc_idx);
  752. dev_set_drvdata(&mci->dev, mci);
  753. pm_runtime_forbid(&mci->dev);
  754. err = device_add(&mci->dev);
  755. if (err < 0) {
  756. edac_dbg(1, "failure: create device %s\n", dev_name(&mci->dev));
  757. /* no put_device() here, free mci with _edac_mc_free() */
  758. return err;
  759. }
  760. edac_dbg(0, "device %s created\n", dev_name(&mci->dev));
  761. /*
  762. * Create the dimm/rank devices
  763. */
  764. mci_for_each_dimm(mci, dimm) {
  765. /* Only expose populated DIMMs */
  766. if (!dimm->nr_pages)
  767. continue;
  768. err = edac_create_dimm_object(mci, dimm);
  769. if (err)
  770. goto fail;
  771. }
  772. #ifdef CONFIG_EDAC_LEGACY_SYSFS
  773. err = edac_create_csrow_objects(mci);
  774. if (err < 0)
  775. goto fail;
  776. #endif
  777. edac_create_debugfs_nodes(mci);
  778. return 0;
  779. fail:
  780. edac_remove_sysfs_mci_device(mci);
  781. return err;
  782. }
  783. /*
  784. * remove a Memory Controller instance
  785. */
  786. void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
  787. {
  788. struct dimm_info *dimm;
  789. if (!device_is_registered(&mci->dev))
  790. return;
  791. edac_dbg(0, "\n");
  792. #ifdef CONFIG_EDAC_DEBUG
  793. edac_debugfs_remove_recursive(mci->debugfs);
  794. #endif
  795. #ifdef CONFIG_EDAC_LEGACY_SYSFS
  796. edac_delete_csrow_objects(mci);
  797. #endif
  798. mci_for_each_dimm(mci, dimm) {
  799. if (!device_is_registered(&dimm->dev))
  800. continue;
  801. edac_dbg(1, "unregistering device %s\n", dev_name(&dimm->dev));
  802. device_unregister(&dimm->dev);
  803. }
  804. /* only remove the device, but keep mci */
  805. device_del(&mci->dev);
  806. }
  807. static void mc_attr_release(struct device *dev)
  808. {
  809. /*
  810. * There's no container structure here, as this is just the mci
  811. * parent device, used to create the /sys/devices/mc sysfs node.
  812. * So, there are no attributes on it.
  813. */
  814. edac_dbg(1, "device %s released\n", dev_name(dev));
  815. kfree(dev);
  816. }
  817. /*
  818. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  819. */
  820. int __init edac_mc_sysfs_init(void)
  821. {
  822. int err;
  823. mci_pdev = kzalloc(sizeof(*mci_pdev), GFP_KERNEL);
  824. if (!mci_pdev)
  825. return -ENOMEM;
  826. mci_pdev->bus = edac_get_sysfs_subsys();
  827. mci_pdev->release = mc_attr_release;
  828. mci_pdev->init_name = "mc";
  829. err = device_register(mci_pdev);
  830. if (err < 0) {
  831. edac_dbg(1, "failure: create device %s\n", dev_name(mci_pdev));
  832. put_device(mci_pdev);
  833. return err;
  834. }
  835. edac_dbg(0, "device %s created\n", dev_name(mci_pdev));
  836. return 0;
  837. }
  838. void edac_mc_sysfs_exit(void)
  839. {
  840. device_unregister(mci_pdev);
  841. }