edac_mc_sysfs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*
  2. * edac_mc kernel module
  3. * (C) 2005-2007 Linux Networx (http://lnxi.com)
  4. *
  5. * This file may be distributed under the terms of the
  6. * GNU General Public License.
  7. *
  8. * Written Doug Thompson <norsk5@xmission.com> www.softwarebitmaker.com
  9. *
  10. * (c) 2012-2013 - Mauro Carvalho Chehab
  11. * The entire API were re-written, and ported to use struct device
  12. *
  13. */
  14. #include <linux/ctype.h>
  15. #include <linux/slab.h>
  16. #include <linux/edac.h>
  17. #include <linux/bug.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/uaccess.h>
  20. #include "edac_mc.h"
  21. #include "edac_module.h"
  22. /* MC EDAC Controls, setable by module parameter, and sysfs */
  23. static int edac_mc_log_ue = 1;
  24. static int edac_mc_log_ce = 1;
  25. static int edac_mc_panic_on_ue;
  26. static int edac_mc_poll_msec = 1000;
  27. /* Getter functions for above */
  28. int edac_mc_get_log_ue(void)
  29. {
  30. return edac_mc_log_ue;
  31. }
  32. int edac_mc_get_log_ce(void)
  33. {
  34. return edac_mc_log_ce;
  35. }
  36. int edac_mc_get_panic_on_ue(void)
  37. {
  38. return edac_mc_panic_on_ue;
  39. }
  40. /* this is temporary */
  41. int edac_mc_get_poll_msec(void)
  42. {
  43. return edac_mc_poll_msec;
  44. }
  45. static int edac_set_poll_msec(const char *val, const struct kernel_param *kp)
  46. {
  47. unsigned long l;
  48. int ret;
  49. if (!val)
  50. return -EINVAL;
  51. ret = kstrtoul(val, 0, &l);
  52. if (ret)
  53. return ret;
  54. if (l < 1000)
  55. return -EINVAL;
  56. *((unsigned long *)kp->arg) = l;
  57. /* notify edac_mc engine to reset the poll period */
  58. edac_mc_reset_delay_period(l);
  59. return 0;
  60. }
  61. /* Parameter declarations for above */
  62. module_param(edac_mc_panic_on_ue, int, 0644);
  63. MODULE_PARM_DESC(edac_mc_panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
  64. module_param(edac_mc_log_ue, int, 0644);
  65. MODULE_PARM_DESC(edac_mc_log_ue,
  66. "Log uncorrectable error to console: 0=off 1=on");
  67. module_param(edac_mc_log_ce, int, 0644);
  68. MODULE_PARM_DESC(edac_mc_log_ce,
  69. "Log correctable error to console: 0=off 1=on");
  70. module_param_call(edac_mc_poll_msec, edac_set_poll_msec, param_get_int,
  71. &edac_mc_poll_msec, 0644);
  72. MODULE_PARM_DESC(edac_mc_poll_msec, "Polling period in milliseconds");
  73. static struct device *mci_pdev;
  74. /*
  75. * various constants for Memory Controllers
  76. */
  77. static const char * const 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 const 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 const 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. csrow->dev.type = &csrow_attr_type;
  358. csrow->dev.bus = mci->bus;
  359. csrow->dev.groups = csrow_dev_groups;
  360. device_initialize(&csrow->dev);
  361. csrow->dev.parent = &mci->dev;
  362. csrow->mci = mci;
  363. dev_set_name(&csrow->dev, "csrow%d", index);
  364. dev_set_drvdata(&csrow->dev, csrow);
  365. edac_dbg(0, "creating (virtual) csrow node %s\n",
  366. dev_name(&csrow->dev));
  367. return device_add(&csrow->dev);
  368. }
  369. /* Create a CSROW object under specifed edac_mc_device */
  370. static int edac_create_csrow_objects(struct mem_ctl_info *mci)
  371. {
  372. int err, i;
  373. struct csrow_info *csrow;
  374. for (i = 0; i < mci->nr_csrows; i++) {
  375. csrow = mci->csrows[i];
  376. if (!nr_pages_per_csrow(csrow))
  377. continue;
  378. err = edac_create_csrow_object(mci, mci->csrows[i], i);
  379. if (err < 0) {
  380. edac_dbg(1,
  381. "failure: create csrow objects for csrow %d\n",
  382. i);
  383. goto error;
  384. }
  385. }
  386. return 0;
  387. error:
  388. for (--i; i >= 0; i--) {
  389. csrow = mci->csrows[i];
  390. if (!nr_pages_per_csrow(csrow))
  391. continue;
  392. put_device(&mci->csrows[i]->dev);
  393. }
  394. return err;
  395. }
  396. static void edac_delete_csrow_objects(struct mem_ctl_info *mci)
  397. {
  398. int i;
  399. struct csrow_info *csrow;
  400. for (i = mci->nr_csrows - 1; i >= 0; i--) {
  401. csrow = mci->csrows[i];
  402. if (!nr_pages_per_csrow(csrow))
  403. continue;
  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. return edac_dimm_info_location(dimm, data, PAGE_SIZE);
  418. }
  419. static ssize_t dimmdev_label_show(struct device *dev,
  420. struct device_attribute *mattr, char *data)
  421. {
  422. struct dimm_info *dimm = to_dimm(dev);
  423. /* if field has not been initialized, there is nothing to send */
  424. if (!dimm->label[0])
  425. return 0;
  426. return snprintf(data, sizeof(dimm->label) + 1, "%s\n", dimm->label);
  427. }
  428. static ssize_t dimmdev_label_store(struct device *dev,
  429. struct device_attribute *mattr,
  430. const char *data,
  431. size_t count)
  432. {
  433. struct dimm_info *dimm = to_dimm(dev);
  434. size_t copy_count = count;
  435. if (count == 0)
  436. return -EINVAL;
  437. if (data[count - 1] == '\0' || data[count - 1] == '\n')
  438. copy_count -= 1;
  439. if (copy_count == 0 || copy_count >= sizeof(dimm->label))
  440. return -EINVAL;
  441. strncpy(dimm->label, data, copy_count);
  442. dimm->label[copy_count] = '\0';
  443. return count;
  444. }
  445. static ssize_t dimmdev_size_show(struct device *dev,
  446. struct device_attribute *mattr, char *data)
  447. {
  448. struct dimm_info *dimm = to_dimm(dev);
  449. return sprintf(data, "%u\n", PAGES_TO_MiB(dimm->nr_pages));
  450. }
  451. static ssize_t dimmdev_mem_type_show(struct device *dev,
  452. struct device_attribute *mattr, char *data)
  453. {
  454. struct dimm_info *dimm = to_dimm(dev);
  455. return sprintf(data, "%s\n", mem_types[dimm->mtype]);
  456. }
  457. static ssize_t dimmdev_dev_type_show(struct device *dev,
  458. struct device_attribute *mattr, char *data)
  459. {
  460. struct dimm_info *dimm = to_dimm(dev);
  461. return sprintf(data, "%s\n", dev_types[dimm->dtype]);
  462. }
  463. static ssize_t dimmdev_edac_mode_show(struct device *dev,
  464. struct device_attribute *mattr,
  465. char *data)
  466. {
  467. struct dimm_info *dimm = to_dimm(dev);
  468. return sprintf(data, "%s\n", edac_caps[dimm->edac_mode]);
  469. }
  470. static ssize_t dimmdev_ce_count_show(struct device *dev,
  471. struct device_attribute *mattr,
  472. char *data)
  473. {
  474. struct dimm_info *dimm = to_dimm(dev);
  475. u32 count;
  476. int off;
  477. off = EDAC_DIMM_OFF(dimm->mci->layers,
  478. dimm->mci->n_layers,
  479. dimm->location[0],
  480. dimm->location[1],
  481. dimm->location[2]);
  482. count = dimm->mci->ce_per_layer[dimm->mci->n_layers-1][off];
  483. return sprintf(data, "%u\n", count);
  484. }
  485. static ssize_t dimmdev_ue_count_show(struct device *dev,
  486. struct device_attribute *mattr,
  487. char *data)
  488. {
  489. struct dimm_info *dimm = to_dimm(dev);
  490. u32 count;
  491. int off;
  492. off = EDAC_DIMM_OFF(dimm->mci->layers,
  493. dimm->mci->n_layers,
  494. dimm->location[0],
  495. dimm->location[1],
  496. dimm->location[2]);
  497. count = dimm->mci->ue_per_layer[dimm->mci->n_layers-1][off];
  498. return sprintf(data, "%u\n", count);
  499. }
  500. /* dimm/rank attribute files */
  501. static DEVICE_ATTR(dimm_label, S_IRUGO | S_IWUSR,
  502. dimmdev_label_show, dimmdev_label_store);
  503. static DEVICE_ATTR(dimm_location, S_IRUGO, dimmdev_location_show, NULL);
  504. static DEVICE_ATTR(size, S_IRUGO, dimmdev_size_show, NULL);
  505. static DEVICE_ATTR(dimm_mem_type, S_IRUGO, dimmdev_mem_type_show, NULL);
  506. static DEVICE_ATTR(dimm_dev_type, S_IRUGO, dimmdev_dev_type_show, NULL);
  507. static DEVICE_ATTR(dimm_edac_mode, S_IRUGO, dimmdev_edac_mode_show, NULL);
  508. static DEVICE_ATTR(dimm_ce_count, S_IRUGO, dimmdev_ce_count_show, NULL);
  509. static DEVICE_ATTR(dimm_ue_count, S_IRUGO, dimmdev_ue_count_show, NULL);
  510. /* attributes of the dimm<id>/rank<id> object */
  511. static struct attribute *dimm_attrs[] = {
  512. &dev_attr_dimm_label.attr,
  513. &dev_attr_dimm_location.attr,
  514. &dev_attr_size.attr,
  515. &dev_attr_dimm_mem_type.attr,
  516. &dev_attr_dimm_dev_type.attr,
  517. &dev_attr_dimm_edac_mode.attr,
  518. &dev_attr_dimm_ce_count.attr,
  519. &dev_attr_dimm_ue_count.attr,
  520. NULL,
  521. };
  522. static const struct attribute_group dimm_attr_grp = {
  523. .attrs = dimm_attrs,
  524. };
  525. static const struct attribute_group *dimm_attr_groups[] = {
  526. &dimm_attr_grp,
  527. NULL
  528. };
  529. static void dimm_attr_release(struct device *dev)
  530. {
  531. struct dimm_info *dimm = container_of(dev, struct dimm_info, dev);
  532. edac_dbg(1, "Releasing dimm device %s\n", dev_name(dev));
  533. kfree(dimm);
  534. }
  535. static const struct device_type dimm_attr_type = {
  536. .groups = dimm_attr_groups,
  537. .release = dimm_attr_release,
  538. };
  539. /* Create a DIMM object under specifed memory controller device */
  540. static int edac_create_dimm_object(struct mem_ctl_info *mci,
  541. struct dimm_info *dimm,
  542. int index)
  543. {
  544. int err;
  545. dimm->mci = mci;
  546. dimm->dev.type = &dimm_attr_type;
  547. dimm->dev.bus = mci->bus;
  548. device_initialize(&dimm->dev);
  549. dimm->dev.parent = &mci->dev;
  550. if (mci->csbased)
  551. dev_set_name(&dimm->dev, "rank%d", index);
  552. else
  553. dev_set_name(&dimm->dev, "dimm%d", index);
  554. dev_set_drvdata(&dimm->dev, dimm);
  555. pm_runtime_forbid(&mci->dev);
  556. err = device_add(&dimm->dev);
  557. edac_dbg(0, "creating rank/dimm device %s\n", dev_name(&dimm->dev));
  558. return err;
  559. }
  560. /*
  561. * Memory controller device
  562. */
  563. #define to_mci(k) container_of(k, struct mem_ctl_info, dev)
  564. static ssize_t mci_reset_counters_store(struct device *dev,
  565. struct device_attribute *mattr,
  566. const char *data, size_t count)
  567. {
  568. struct mem_ctl_info *mci = to_mci(dev);
  569. int cnt, row, chan, i;
  570. mci->ue_mc = 0;
  571. mci->ce_mc = 0;
  572. mci->ue_noinfo_count = 0;
  573. mci->ce_noinfo_count = 0;
  574. for (row = 0; row < mci->nr_csrows; row++) {
  575. struct csrow_info *ri = mci->csrows[row];
  576. ri->ue_count = 0;
  577. ri->ce_count = 0;
  578. for (chan = 0; chan < ri->nr_channels; chan++)
  579. ri->channels[chan]->ce_count = 0;
  580. }
  581. cnt = 1;
  582. for (i = 0; i < mci->n_layers; i++) {
  583. cnt *= mci->layers[i].size;
  584. memset(mci->ce_per_layer[i], 0, cnt * sizeof(u32));
  585. memset(mci->ue_per_layer[i], 0, cnt * sizeof(u32));
  586. }
  587. mci->start_time = jiffies;
  588. return count;
  589. }
  590. /* Memory scrubbing interface:
  591. *
  592. * A MC driver can limit the scrubbing bandwidth based on the CPU type.
  593. * Therefore, ->set_sdram_scrub_rate should be made to return the actual
  594. * bandwidth that is accepted or 0 when scrubbing is to be disabled.
  595. *
  596. * Negative value still means that an error has occurred while setting
  597. * the scrub rate.
  598. */
  599. static ssize_t mci_sdram_scrub_rate_store(struct device *dev,
  600. struct device_attribute *mattr,
  601. const char *data, size_t count)
  602. {
  603. struct mem_ctl_info *mci = to_mci(dev);
  604. unsigned long bandwidth = 0;
  605. int new_bw = 0;
  606. if (kstrtoul(data, 10, &bandwidth) < 0)
  607. return -EINVAL;
  608. new_bw = mci->set_sdram_scrub_rate(mci, bandwidth);
  609. if (new_bw < 0) {
  610. edac_printk(KERN_WARNING, EDAC_MC,
  611. "Error setting scrub rate to: %lu\n", bandwidth);
  612. return -EINVAL;
  613. }
  614. return count;
  615. }
  616. /*
  617. * ->get_sdram_scrub_rate() return value semantics same as above.
  618. */
  619. static ssize_t mci_sdram_scrub_rate_show(struct device *dev,
  620. struct device_attribute *mattr,
  621. char *data)
  622. {
  623. struct mem_ctl_info *mci = to_mci(dev);
  624. int bandwidth = 0;
  625. bandwidth = mci->get_sdram_scrub_rate(mci);
  626. if (bandwidth < 0) {
  627. edac_printk(KERN_DEBUG, EDAC_MC, "Error reading scrub rate\n");
  628. return bandwidth;
  629. }
  630. return sprintf(data, "%d\n", bandwidth);
  631. }
  632. /* default attribute files for the MCI object */
  633. static ssize_t mci_ue_count_show(struct device *dev,
  634. struct device_attribute *mattr,
  635. char *data)
  636. {
  637. struct mem_ctl_info *mci = to_mci(dev);
  638. return sprintf(data, "%d\n", mci->ue_mc);
  639. }
  640. static ssize_t mci_ce_count_show(struct device *dev,
  641. struct device_attribute *mattr,
  642. char *data)
  643. {
  644. struct mem_ctl_info *mci = to_mci(dev);
  645. return sprintf(data, "%d\n", mci->ce_mc);
  646. }
  647. static ssize_t mci_ce_noinfo_show(struct device *dev,
  648. struct device_attribute *mattr,
  649. char *data)
  650. {
  651. struct mem_ctl_info *mci = to_mci(dev);
  652. return sprintf(data, "%d\n", mci->ce_noinfo_count);
  653. }
  654. static ssize_t mci_ue_noinfo_show(struct device *dev,
  655. struct device_attribute *mattr,
  656. char *data)
  657. {
  658. struct mem_ctl_info *mci = to_mci(dev);
  659. return sprintf(data, "%d\n", mci->ue_noinfo_count);
  660. }
  661. static ssize_t mci_seconds_show(struct device *dev,
  662. struct device_attribute *mattr,
  663. char *data)
  664. {
  665. struct mem_ctl_info *mci = to_mci(dev);
  666. return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
  667. }
  668. static ssize_t mci_ctl_name_show(struct device *dev,
  669. struct device_attribute *mattr,
  670. char *data)
  671. {
  672. struct mem_ctl_info *mci = to_mci(dev);
  673. return sprintf(data, "%s\n", mci->ctl_name);
  674. }
  675. static ssize_t mci_size_mb_show(struct device *dev,
  676. struct device_attribute *mattr,
  677. char *data)
  678. {
  679. struct mem_ctl_info *mci = to_mci(dev);
  680. int total_pages = 0, csrow_idx, j;
  681. for (csrow_idx = 0; csrow_idx < mci->nr_csrows; csrow_idx++) {
  682. struct csrow_info *csrow = mci->csrows[csrow_idx];
  683. for (j = 0; j < csrow->nr_channels; j++) {
  684. struct dimm_info *dimm = csrow->channels[j]->dimm;
  685. total_pages += dimm->nr_pages;
  686. }
  687. }
  688. return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
  689. }
  690. static ssize_t mci_max_location_show(struct device *dev,
  691. struct device_attribute *mattr,
  692. char *data)
  693. {
  694. struct mem_ctl_info *mci = to_mci(dev);
  695. int i;
  696. char *p = data;
  697. for (i = 0; i < mci->n_layers; i++) {
  698. p += sprintf(p, "%s %d ",
  699. edac_layer_name[mci->layers[i].type],
  700. mci->layers[i].size - 1);
  701. }
  702. return p - data;
  703. }
  704. /* default Control file */
  705. static DEVICE_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
  706. /* default Attribute files */
  707. static DEVICE_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
  708. static DEVICE_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
  709. static DEVICE_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
  710. static DEVICE_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
  711. static DEVICE_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
  712. static DEVICE_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
  713. static DEVICE_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
  714. static DEVICE_ATTR(max_location, S_IRUGO, mci_max_location_show, NULL);
  715. /* memory scrubber attribute file */
  716. static DEVICE_ATTR(sdram_scrub_rate, 0, mci_sdram_scrub_rate_show,
  717. mci_sdram_scrub_rate_store); /* umode set later in is_visible */
  718. static struct attribute *mci_attrs[] = {
  719. &dev_attr_reset_counters.attr,
  720. &dev_attr_mc_name.attr,
  721. &dev_attr_size_mb.attr,
  722. &dev_attr_seconds_since_reset.attr,
  723. &dev_attr_ue_noinfo_count.attr,
  724. &dev_attr_ce_noinfo_count.attr,
  725. &dev_attr_ue_count.attr,
  726. &dev_attr_ce_count.attr,
  727. &dev_attr_max_location.attr,
  728. &dev_attr_sdram_scrub_rate.attr,
  729. NULL
  730. };
  731. static umode_t mci_attr_is_visible(struct kobject *kobj,
  732. struct attribute *attr, int idx)
  733. {
  734. struct device *dev = kobj_to_dev(kobj);
  735. struct mem_ctl_info *mci = to_mci(dev);
  736. umode_t mode = 0;
  737. if (attr != &dev_attr_sdram_scrub_rate.attr)
  738. return attr->mode;
  739. if (mci->get_sdram_scrub_rate)
  740. mode |= S_IRUGO;
  741. if (mci->set_sdram_scrub_rate)
  742. mode |= S_IWUSR;
  743. return mode;
  744. }
  745. static const struct attribute_group mci_attr_grp = {
  746. .attrs = mci_attrs,
  747. .is_visible = mci_attr_is_visible,
  748. };
  749. static const struct attribute_group *mci_attr_groups[] = {
  750. &mci_attr_grp,
  751. NULL
  752. };
  753. static void mci_attr_release(struct device *dev)
  754. {
  755. struct mem_ctl_info *mci = container_of(dev, struct mem_ctl_info, dev);
  756. edac_dbg(1, "Releasing csrow device %s\n", dev_name(dev));
  757. kfree(mci);
  758. }
  759. static const struct device_type mci_attr_type = {
  760. .groups = mci_attr_groups,
  761. .release = mci_attr_release,
  762. };
  763. /*
  764. * Create a new Memory Controller kobject instance,
  765. * mc<id> under the 'mc' directory
  766. *
  767. * Return:
  768. * 0 Success
  769. * !0 Failure
  770. */
  771. int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
  772. const struct attribute_group **groups)
  773. {
  774. char *name;
  775. int i, err;
  776. /*
  777. * The memory controller needs its own bus, in order to avoid
  778. * namespace conflicts at /sys/bus/edac.
  779. */
  780. name = kasprintf(GFP_KERNEL, "mc%d", mci->mc_idx);
  781. if (!name)
  782. return -ENOMEM;
  783. mci->bus->name = name;
  784. edac_dbg(0, "creating bus %s\n", mci->bus->name);
  785. err = bus_register(mci->bus);
  786. if (err < 0) {
  787. kfree(name);
  788. return err;
  789. }
  790. /* get the /sys/devices/system/edac subsys reference */
  791. mci->dev.type = &mci_attr_type;
  792. device_initialize(&mci->dev);
  793. mci->dev.parent = mci_pdev;
  794. mci->dev.bus = mci->bus;
  795. mci->dev.groups = groups;
  796. dev_set_name(&mci->dev, "mc%d", mci->mc_idx);
  797. dev_set_drvdata(&mci->dev, mci);
  798. pm_runtime_forbid(&mci->dev);
  799. edac_dbg(0, "creating device %s\n", dev_name(&mci->dev));
  800. err = device_add(&mci->dev);
  801. if (err < 0) {
  802. edac_dbg(1, "failure: create device %s\n", dev_name(&mci->dev));
  803. goto fail_unregister_bus;
  804. }
  805. /*
  806. * Create the dimm/rank devices
  807. */
  808. for (i = 0; i < mci->tot_dimms; i++) {
  809. struct dimm_info *dimm = mci->dimms[i];
  810. /* Only expose populated DIMMs */
  811. if (!dimm->nr_pages)
  812. continue;
  813. #ifdef CONFIG_EDAC_DEBUG
  814. edac_dbg(1, "creating dimm%d, located at ", i);
  815. if (edac_debug_level >= 1) {
  816. int lay;
  817. for (lay = 0; lay < mci->n_layers; lay++)
  818. printk(KERN_CONT "%s %d ",
  819. edac_layer_name[mci->layers[lay].type],
  820. dimm->location[lay]);
  821. printk(KERN_CONT "\n");
  822. }
  823. #endif
  824. err = edac_create_dimm_object(mci, dimm, i);
  825. if (err) {
  826. edac_dbg(1, "failure: create dimm %d obj\n", i);
  827. goto fail_unregister_dimm;
  828. }
  829. }
  830. #ifdef CONFIG_EDAC_LEGACY_SYSFS
  831. err = edac_create_csrow_objects(mci);
  832. if (err < 0)
  833. goto fail_unregister_dimm;
  834. #endif
  835. edac_create_debugfs_nodes(mci);
  836. return 0;
  837. fail_unregister_dimm:
  838. for (i--; i >= 0; i--) {
  839. struct dimm_info *dimm = mci->dimms[i];
  840. if (!dimm->nr_pages)
  841. continue;
  842. device_unregister(&dimm->dev);
  843. }
  844. device_unregister(&mci->dev);
  845. fail_unregister_bus:
  846. bus_unregister(mci->bus);
  847. kfree(name);
  848. return err;
  849. }
  850. /*
  851. * remove a Memory Controller instance
  852. */
  853. void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
  854. {
  855. int i;
  856. edac_dbg(0, "\n");
  857. #ifdef CONFIG_EDAC_DEBUG
  858. edac_debugfs_remove_recursive(mci->debugfs);
  859. #endif
  860. #ifdef CONFIG_EDAC_LEGACY_SYSFS
  861. edac_delete_csrow_objects(mci);
  862. #endif
  863. for (i = 0; i < mci->tot_dimms; i++) {
  864. struct dimm_info *dimm = mci->dimms[i];
  865. if (dimm->nr_pages == 0)
  866. continue;
  867. edac_dbg(0, "removing device %s\n", dev_name(&dimm->dev));
  868. device_unregister(&dimm->dev);
  869. }
  870. }
  871. void edac_unregister_sysfs(struct mem_ctl_info *mci)
  872. {
  873. struct bus_type *bus = mci->bus;
  874. const char *name = mci->bus->name;
  875. edac_dbg(1, "Unregistering device %s\n", dev_name(&mci->dev));
  876. device_unregister(&mci->dev);
  877. bus_unregister(bus);
  878. kfree(name);
  879. }
  880. static void mc_attr_release(struct device *dev)
  881. {
  882. /*
  883. * There's no container structure here, as this is just the mci
  884. * parent device, used to create the /sys/devices/mc sysfs node.
  885. * So, there are no attributes on it.
  886. */
  887. edac_dbg(1, "Releasing device %s\n", dev_name(dev));
  888. kfree(dev);
  889. }
  890. static const struct device_type mc_attr_type = {
  891. .release = mc_attr_release,
  892. };
  893. /*
  894. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  895. */
  896. int __init edac_mc_sysfs_init(void)
  897. {
  898. int err;
  899. mci_pdev = kzalloc(sizeof(*mci_pdev), GFP_KERNEL);
  900. if (!mci_pdev) {
  901. err = -ENOMEM;
  902. goto out;
  903. }
  904. mci_pdev->bus = edac_get_sysfs_subsys();
  905. mci_pdev->type = &mc_attr_type;
  906. device_initialize(mci_pdev);
  907. dev_set_name(mci_pdev, "mc");
  908. err = device_add(mci_pdev);
  909. if (err < 0)
  910. goto out_dev_free;
  911. edac_dbg(0, "device %s created\n", dev_name(mci_pdev));
  912. return 0;
  913. out_dev_free:
  914. kfree(mci_pdev);
  915. out:
  916. return err;
  917. }
  918. void edac_mc_sysfs_exit(void)
  919. {
  920. device_unregister(mci_pdev);
  921. }