edac_mc_sysfs.c 27 KB

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