edac_mc_sysfs.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. */
  11. #include <linux/ctype.h>
  12. #include <linux/slab.h>
  13. #include <linux/edac.h>
  14. #include <linux/bug.h>
  15. #include "edac_core.h"
  16. #include "edac_module.h"
  17. /* MC EDAC Controls, setable by module parameter, and sysfs */
  18. static int edac_mc_log_ue = 1;
  19. static int edac_mc_log_ce = 1;
  20. static int edac_mc_panic_on_ue;
  21. static int edac_mc_poll_msec = 1000;
  22. /* Getter functions for above */
  23. int edac_mc_get_log_ue(void)
  24. {
  25. return edac_mc_log_ue;
  26. }
  27. int edac_mc_get_log_ce(void)
  28. {
  29. return edac_mc_log_ce;
  30. }
  31. int edac_mc_get_panic_on_ue(void)
  32. {
  33. return edac_mc_panic_on_ue;
  34. }
  35. /* this is temporary */
  36. int edac_mc_get_poll_msec(void)
  37. {
  38. return edac_mc_poll_msec;
  39. }
  40. static int edac_set_poll_msec(const char *val, struct kernel_param *kp)
  41. {
  42. long l;
  43. int ret;
  44. if (!val)
  45. return -EINVAL;
  46. ret = strict_strtol(val, 0, &l);
  47. if (ret == -EINVAL || ((int)l != l))
  48. return -EINVAL;
  49. *((int *)kp->arg) = l;
  50. /* notify edac_mc engine to reset the poll period */
  51. edac_mc_reset_delay_period(l);
  52. return 0;
  53. }
  54. /* Parameter declarations for above */
  55. module_param(edac_mc_panic_on_ue, int, 0644);
  56. MODULE_PARM_DESC(edac_mc_panic_on_ue, "Panic on uncorrected error: 0=off 1=on");
  57. module_param(edac_mc_log_ue, int, 0644);
  58. MODULE_PARM_DESC(edac_mc_log_ue,
  59. "Log uncorrectable error to console: 0=off 1=on");
  60. module_param(edac_mc_log_ce, int, 0644);
  61. MODULE_PARM_DESC(edac_mc_log_ce,
  62. "Log correctable error to console: 0=off 1=on");
  63. module_param_call(edac_mc_poll_msec, edac_set_poll_msec, param_get_int,
  64. &edac_mc_poll_msec, 0644);
  65. MODULE_PARM_DESC(edac_mc_poll_msec, "Polling period in milliseconds");
  66. /*
  67. * various constants for Memory Controllers
  68. */
  69. static const char *mem_types[] = {
  70. [MEM_EMPTY] = "Empty",
  71. [MEM_RESERVED] = "Reserved",
  72. [MEM_UNKNOWN] = "Unknown",
  73. [MEM_FPM] = "FPM",
  74. [MEM_EDO] = "EDO",
  75. [MEM_BEDO] = "BEDO",
  76. [MEM_SDR] = "Unbuffered-SDR",
  77. [MEM_RDR] = "Registered-SDR",
  78. [MEM_DDR] = "Unbuffered-DDR",
  79. [MEM_RDDR] = "Registered-DDR",
  80. [MEM_RMBS] = "RMBS",
  81. [MEM_DDR2] = "Unbuffered-DDR2",
  82. [MEM_FB_DDR2] = "FullyBuffered-DDR2",
  83. [MEM_RDDR2] = "Registered-DDR2",
  84. [MEM_XDR] = "XDR",
  85. [MEM_DDR3] = "Unbuffered-DDR3",
  86. [MEM_RDDR3] = "Registered-DDR3"
  87. };
  88. static const char *dev_types[] = {
  89. [DEV_UNKNOWN] = "Unknown",
  90. [DEV_X1] = "x1",
  91. [DEV_X2] = "x2",
  92. [DEV_X4] = "x4",
  93. [DEV_X8] = "x8",
  94. [DEV_X16] = "x16",
  95. [DEV_X32] = "x32",
  96. [DEV_X64] = "x64"
  97. };
  98. static const char *edac_caps[] = {
  99. [EDAC_UNKNOWN] = "Unknown",
  100. [EDAC_NONE] = "None",
  101. [EDAC_RESERVED] = "Reserved",
  102. [EDAC_PARITY] = "PARITY",
  103. [EDAC_EC] = "EC",
  104. [EDAC_SECDED] = "SECDED",
  105. [EDAC_S2ECD2ED] = "S2ECD2ED",
  106. [EDAC_S4ECD4ED] = "S4ECD4ED",
  107. [EDAC_S8ECD8ED] = "S8ECD8ED",
  108. [EDAC_S16ECD16ED] = "S16ECD16ED"
  109. };
  110. /* EDAC sysfs CSROW data structures and methods
  111. */
  112. /* Set of more default csrow<id> attribute show/store functions */
  113. static ssize_t csrow_ue_count_show(struct csrow_info *csrow, char *data,
  114. int private)
  115. {
  116. return sprintf(data, "%u\n", csrow->ue_count);
  117. }
  118. static ssize_t csrow_ce_count_show(struct csrow_info *csrow, char *data,
  119. int private)
  120. {
  121. return sprintf(data, "%u\n", csrow->ce_count);
  122. }
  123. static ssize_t csrow_size_show(struct csrow_info *csrow, char *data,
  124. int private)
  125. {
  126. int i;
  127. u32 nr_pages = 0;
  128. for (i = 0; i < csrow->nr_channels; i++)
  129. nr_pages += csrow->channels[i].dimm->nr_pages;
  130. return sprintf(data, "%u\n", PAGES_TO_MiB(nr_pages));
  131. }
  132. static ssize_t csrow_mem_type_show(struct csrow_info *csrow, char *data,
  133. int private)
  134. {
  135. return sprintf(data, "%s\n", mem_types[csrow->channels[0].dimm->mtype]);
  136. }
  137. static ssize_t csrow_dev_type_show(struct csrow_info *csrow, char *data,
  138. int private)
  139. {
  140. return sprintf(data, "%s\n", dev_types[csrow->channels[0].dimm->dtype]);
  141. }
  142. static ssize_t csrow_edac_mode_show(struct csrow_info *csrow, char *data,
  143. int private)
  144. {
  145. return sprintf(data, "%s\n", edac_caps[csrow->channels[0].dimm->edac_mode]);
  146. }
  147. /* show/store functions for DIMM Label attributes */
  148. static ssize_t channel_dimm_label_show(struct csrow_info *csrow,
  149. char *data, int channel)
  150. {
  151. /* if field has not been initialized, there is nothing to send */
  152. if (!csrow->channels[channel].dimm->label[0])
  153. return 0;
  154. return snprintf(data, EDAC_MC_LABEL_LEN, "%s\n",
  155. csrow->channels[channel].dimm->label);
  156. }
  157. static ssize_t channel_dimm_label_store(struct csrow_info *csrow,
  158. const char *data,
  159. size_t count, int channel)
  160. {
  161. ssize_t max_size = 0;
  162. max_size = min((ssize_t) count, (ssize_t) EDAC_MC_LABEL_LEN - 1);
  163. strncpy(csrow->channels[channel].dimm->label, data, max_size);
  164. csrow->channels[channel].dimm->label[max_size] = '\0';
  165. return max_size;
  166. }
  167. /* show function for dynamic chX_ce_count attribute */
  168. static ssize_t channel_ce_count_show(struct csrow_info *csrow,
  169. char *data, int channel)
  170. {
  171. return sprintf(data, "%u\n", csrow->channels[channel].ce_count);
  172. }
  173. /* csrow specific attribute structure */
  174. struct csrowdev_attribute {
  175. struct attribute attr;
  176. ssize_t(*show) (struct csrow_info *, char *, int);
  177. ssize_t(*store) (struct csrow_info *, const char *, size_t, int);
  178. int private;
  179. };
  180. #define to_csrow(k) container_of(k, struct csrow_info, kobj)
  181. #define to_csrowdev_attr(a) container_of(a, struct csrowdev_attribute, attr)
  182. /* Set of show/store higher level functions for default csrow attributes */
  183. static ssize_t csrowdev_show(struct kobject *kobj,
  184. struct attribute *attr, char *buffer)
  185. {
  186. struct csrow_info *csrow = to_csrow(kobj);
  187. struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
  188. if (csrowdev_attr->show)
  189. return csrowdev_attr->show(csrow,
  190. buffer, csrowdev_attr->private);
  191. return -EIO;
  192. }
  193. static ssize_t csrowdev_store(struct kobject *kobj, struct attribute *attr,
  194. const char *buffer, size_t count)
  195. {
  196. struct csrow_info *csrow = to_csrow(kobj);
  197. struct csrowdev_attribute *csrowdev_attr = to_csrowdev_attr(attr);
  198. if (csrowdev_attr->store)
  199. return csrowdev_attr->store(csrow,
  200. buffer,
  201. count, csrowdev_attr->private);
  202. return -EIO;
  203. }
  204. static const struct sysfs_ops csrowfs_ops = {
  205. .show = csrowdev_show,
  206. .store = csrowdev_store
  207. };
  208. #define CSROWDEV_ATTR(_name,_mode,_show,_store,_private) \
  209. static struct csrowdev_attribute attr_##_name = { \
  210. .attr = {.name = __stringify(_name), .mode = _mode }, \
  211. .show = _show, \
  212. .store = _store, \
  213. .private = _private, \
  214. };
  215. /* default cwrow<id>/attribute files */
  216. CSROWDEV_ATTR(size_mb, S_IRUGO, csrow_size_show, NULL, 0);
  217. CSROWDEV_ATTR(dev_type, S_IRUGO, csrow_dev_type_show, NULL, 0);
  218. CSROWDEV_ATTR(mem_type, S_IRUGO, csrow_mem_type_show, NULL, 0);
  219. CSROWDEV_ATTR(edac_mode, S_IRUGO, csrow_edac_mode_show, NULL, 0);
  220. CSROWDEV_ATTR(ue_count, S_IRUGO, csrow_ue_count_show, NULL, 0);
  221. CSROWDEV_ATTR(ce_count, S_IRUGO, csrow_ce_count_show, NULL, 0);
  222. /* default attributes of the CSROW<id> object */
  223. static struct csrowdev_attribute *default_csrow_attr[] = {
  224. &attr_dev_type,
  225. &attr_mem_type,
  226. &attr_edac_mode,
  227. &attr_size_mb,
  228. &attr_ue_count,
  229. &attr_ce_count,
  230. NULL,
  231. };
  232. /* possible dynamic channel DIMM Label attribute files */
  233. CSROWDEV_ATTR(ch0_dimm_label, S_IRUGO | S_IWUSR,
  234. channel_dimm_label_show, channel_dimm_label_store, 0);
  235. CSROWDEV_ATTR(ch1_dimm_label, S_IRUGO | S_IWUSR,
  236. channel_dimm_label_show, channel_dimm_label_store, 1);
  237. CSROWDEV_ATTR(ch2_dimm_label, S_IRUGO | S_IWUSR,
  238. channel_dimm_label_show, channel_dimm_label_store, 2);
  239. CSROWDEV_ATTR(ch3_dimm_label, S_IRUGO | S_IWUSR,
  240. channel_dimm_label_show, channel_dimm_label_store, 3);
  241. CSROWDEV_ATTR(ch4_dimm_label, S_IRUGO | S_IWUSR,
  242. channel_dimm_label_show, channel_dimm_label_store, 4);
  243. CSROWDEV_ATTR(ch5_dimm_label, S_IRUGO | S_IWUSR,
  244. channel_dimm_label_show, channel_dimm_label_store, 5);
  245. /* Total possible dynamic DIMM Label attribute file table */
  246. static struct csrowdev_attribute *dynamic_csrow_dimm_attr[] = {
  247. &attr_ch0_dimm_label,
  248. &attr_ch1_dimm_label,
  249. &attr_ch2_dimm_label,
  250. &attr_ch3_dimm_label,
  251. &attr_ch4_dimm_label,
  252. &attr_ch5_dimm_label
  253. };
  254. /* possible dynamic channel ce_count attribute files */
  255. CSROWDEV_ATTR(ch0_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 0);
  256. CSROWDEV_ATTR(ch1_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 1);
  257. CSROWDEV_ATTR(ch2_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 2);
  258. CSROWDEV_ATTR(ch3_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 3);
  259. CSROWDEV_ATTR(ch4_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 4);
  260. CSROWDEV_ATTR(ch5_ce_count, S_IRUGO | S_IWUSR, channel_ce_count_show, NULL, 5);
  261. /* Total possible dynamic ce_count attribute file table */
  262. static struct csrowdev_attribute *dynamic_csrow_ce_count_attr[] = {
  263. &attr_ch0_ce_count,
  264. &attr_ch1_ce_count,
  265. &attr_ch2_ce_count,
  266. &attr_ch3_ce_count,
  267. &attr_ch4_ce_count,
  268. &attr_ch5_ce_count
  269. };
  270. #define EDAC_NR_CHANNELS 6
  271. /* Create dynamic CHANNEL files, indexed by 'chan', under specifed CSROW */
  272. static int edac_create_channel_files(struct kobject *kobj, int chan)
  273. {
  274. int err = -ENODEV;
  275. if (chan >= EDAC_NR_CHANNELS)
  276. return err;
  277. /* create the DIMM label attribute file */
  278. err = sysfs_create_file(kobj,
  279. (struct attribute *)
  280. dynamic_csrow_dimm_attr[chan]);
  281. if (!err) {
  282. /* create the CE Count attribute file */
  283. err = sysfs_create_file(kobj,
  284. (struct attribute *)
  285. dynamic_csrow_ce_count_attr[chan]);
  286. } else {
  287. debugf1("%s() dimm labels and ce_count files created",
  288. __func__);
  289. }
  290. return err;
  291. }
  292. /* No memory to release for this kobj */
  293. static void edac_csrow_instance_release(struct kobject *kobj)
  294. {
  295. struct mem_ctl_info *mci;
  296. struct csrow_info *cs;
  297. debugf1("%s()\n", __func__);
  298. cs = container_of(kobj, struct csrow_info, kobj);
  299. mci = cs->mci;
  300. kobject_put(&mci->edac_mci_kobj);
  301. }
  302. /* the kobj_type instance for a CSROW */
  303. static struct kobj_type ktype_csrow = {
  304. .release = edac_csrow_instance_release,
  305. .sysfs_ops = &csrowfs_ops,
  306. .default_attrs = (struct attribute **)default_csrow_attr,
  307. };
  308. /* Create a CSROW object under specifed edac_mc_device */
  309. static int edac_create_csrow_object(struct mem_ctl_info *mci,
  310. struct csrow_info *csrow, int index)
  311. {
  312. struct kobject *kobj_mci = &mci->edac_mci_kobj;
  313. struct kobject *kobj;
  314. int chan;
  315. int err;
  316. /* generate ..../edac/mc/mc<id>/csrow<index> */
  317. memset(&csrow->kobj, 0, sizeof(csrow->kobj));
  318. csrow->mci = mci; /* include container up link */
  319. /* bump the mci instance's kobject's ref count */
  320. kobj = kobject_get(&mci->edac_mci_kobj);
  321. if (!kobj) {
  322. err = -ENODEV;
  323. goto err_out;
  324. }
  325. /* Instanstiate the csrow object */
  326. err = kobject_init_and_add(&csrow->kobj, &ktype_csrow, kobj_mci,
  327. "csrow%d", index);
  328. if (err)
  329. goto err_release_top_kobj;
  330. /* At this point, to release a csrow kobj, one must
  331. * call the kobject_put and allow that tear down
  332. * to work the releasing
  333. */
  334. /* Create the dyanmic attribute files on this csrow,
  335. * namely, the DIMM labels and the channel ce_count
  336. */
  337. for (chan = 0; chan < csrow->nr_channels; chan++) {
  338. err = edac_create_channel_files(&csrow->kobj, chan);
  339. if (err) {
  340. /* special case the unregister here */
  341. kobject_put(&csrow->kobj);
  342. goto err_out;
  343. }
  344. }
  345. kobject_uevent(&csrow->kobj, KOBJ_ADD);
  346. return 0;
  347. /* error unwind stack */
  348. err_release_top_kobj:
  349. kobject_put(&mci->edac_mci_kobj);
  350. err_out:
  351. return err;
  352. }
  353. /* default sysfs methods and data structures for the main MCI kobject */
  354. static ssize_t mci_reset_counters_store(struct mem_ctl_info *mci,
  355. const char *data, size_t count)
  356. {
  357. int row, chan;
  358. mci->ue_noinfo_count = 0;
  359. mci->ce_noinfo_count = 0;
  360. mci->ue_mc = 0;
  361. mci->ce_mc = 0;
  362. for (row = 0; row < mci->nr_csrows; row++) {
  363. struct csrow_info *ri = &mci->csrows[row];
  364. ri->ue_count = 0;
  365. ri->ce_count = 0;
  366. for (chan = 0; chan < ri->nr_channels; chan++)
  367. ri->channels[chan].ce_count = 0;
  368. }
  369. mci->start_time = jiffies;
  370. return count;
  371. }
  372. /* Memory scrubbing interface:
  373. *
  374. * A MC driver can limit the scrubbing bandwidth based on the CPU type.
  375. * Therefore, ->set_sdram_scrub_rate should be made to return the actual
  376. * bandwidth that is accepted or 0 when scrubbing is to be disabled.
  377. *
  378. * Negative value still means that an error has occurred while setting
  379. * the scrub rate.
  380. */
  381. static ssize_t mci_sdram_scrub_rate_store(struct mem_ctl_info *mci,
  382. const char *data, size_t count)
  383. {
  384. unsigned long bandwidth = 0;
  385. int new_bw = 0;
  386. if (!mci->set_sdram_scrub_rate)
  387. return -ENODEV;
  388. if (strict_strtoul(data, 10, &bandwidth) < 0)
  389. return -EINVAL;
  390. new_bw = mci->set_sdram_scrub_rate(mci, bandwidth);
  391. if (new_bw < 0) {
  392. edac_printk(KERN_WARNING, EDAC_MC,
  393. "Error setting scrub rate to: %lu\n", bandwidth);
  394. return -EINVAL;
  395. }
  396. return count;
  397. }
  398. /*
  399. * ->get_sdram_scrub_rate() return value semantics same as above.
  400. */
  401. static ssize_t mci_sdram_scrub_rate_show(struct mem_ctl_info *mci, char *data)
  402. {
  403. int bandwidth = 0;
  404. if (!mci->get_sdram_scrub_rate)
  405. return -ENODEV;
  406. bandwidth = mci->get_sdram_scrub_rate(mci);
  407. if (bandwidth < 0) {
  408. edac_printk(KERN_DEBUG, EDAC_MC, "Error reading scrub rate\n");
  409. return bandwidth;
  410. }
  411. return sprintf(data, "%d\n", bandwidth);
  412. }
  413. /* default attribute files for the MCI object */
  414. static ssize_t mci_ue_count_show(struct mem_ctl_info *mci, char *data)
  415. {
  416. return sprintf(data, "%d\n", mci->ue_mc);
  417. }
  418. static ssize_t mci_ce_count_show(struct mem_ctl_info *mci, char *data)
  419. {
  420. return sprintf(data, "%d\n", mci->ce_mc);
  421. }
  422. static ssize_t mci_ce_noinfo_show(struct mem_ctl_info *mci, char *data)
  423. {
  424. return sprintf(data, "%d\n", mci->ce_noinfo_count);
  425. }
  426. static ssize_t mci_ue_noinfo_show(struct mem_ctl_info *mci, char *data)
  427. {
  428. return sprintf(data, "%d\n", mci->ue_noinfo_count);
  429. }
  430. static ssize_t mci_seconds_show(struct mem_ctl_info *mci, char *data)
  431. {
  432. return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
  433. }
  434. static ssize_t mci_ctl_name_show(struct mem_ctl_info *mci, char *data)
  435. {
  436. return sprintf(data, "%s\n", mci->ctl_name);
  437. }
  438. static ssize_t mci_size_mb_show(struct mem_ctl_info *mci, char *data)
  439. {
  440. int total_pages = 0, csrow_idx, j;
  441. for (csrow_idx = 0; csrow_idx < mci->nr_csrows; csrow_idx++) {
  442. struct csrow_info *csrow = &mci->csrows[csrow_idx];
  443. for (j = 0; j < csrow->nr_channels; j++) {
  444. struct dimm_info *dimm = csrow->channels[j].dimm;
  445. total_pages += dimm->nr_pages;
  446. }
  447. }
  448. return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
  449. }
  450. #define to_mci(k) container_of(k, struct mem_ctl_info, edac_mci_kobj)
  451. #define to_mcidev_attr(a) container_of(a,struct mcidev_sysfs_attribute,attr)
  452. /* MCI show/store functions for top most object */
  453. static ssize_t mcidev_show(struct kobject *kobj, struct attribute *attr,
  454. char *buffer)
  455. {
  456. struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
  457. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  458. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  459. if (mcidev_attr->show)
  460. return mcidev_attr->show(mem_ctl_info, buffer);
  461. return -EIO;
  462. }
  463. static ssize_t mcidev_store(struct kobject *kobj, struct attribute *attr,
  464. const char *buffer, size_t count)
  465. {
  466. struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
  467. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  468. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  469. if (mcidev_attr->store)
  470. return mcidev_attr->store(mem_ctl_info, buffer, count);
  471. return -EIO;
  472. }
  473. /* Intermediate show/store table */
  474. static const struct sysfs_ops mci_ops = {
  475. .show = mcidev_show,
  476. .store = mcidev_store
  477. };
  478. #define MCIDEV_ATTR(_name,_mode,_show,_store) \
  479. static struct mcidev_sysfs_attribute mci_attr_##_name = { \
  480. .attr = {.name = __stringify(_name), .mode = _mode }, \
  481. .show = _show, \
  482. .store = _store, \
  483. };
  484. /* default Control file */
  485. MCIDEV_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
  486. /* default Attribute files */
  487. MCIDEV_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
  488. MCIDEV_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
  489. MCIDEV_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
  490. MCIDEV_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
  491. MCIDEV_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
  492. MCIDEV_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
  493. MCIDEV_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
  494. /* memory scrubber attribute file */
  495. MCIDEV_ATTR(sdram_scrub_rate, S_IRUGO | S_IWUSR, mci_sdram_scrub_rate_show,
  496. mci_sdram_scrub_rate_store);
  497. static struct mcidev_sysfs_attribute *mci_attr[] = {
  498. &mci_attr_reset_counters,
  499. &mci_attr_mc_name,
  500. &mci_attr_size_mb,
  501. &mci_attr_seconds_since_reset,
  502. &mci_attr_ue_noinfo_count,
  503. &mci_attr_ce_noinfo_count,
  504. &mci_attr_ue_count,
  505. &mci_attr_ce_count,
  506. &mci_attr_sdram_scrub_rate,
  507. NULL
  508. };
  509. /*
  510. * Release of a MC controlling instance
  511. *
  512. * each MC control instance has the following resources upon entry:
  513. * a) a ref count on the top memctl kobj
  514. * b) a ref count on this module
  515. *
  516. * this function must decrement those ref counts and then
  517. * issue a free on the instance's memory
  518. */
  519. static void edac_mci_control_release(struct kobject *kobj)
  520. {
  521. struct mem_ctl_info *mci;
  522. mci = to_mci(kobj);
  523. debugf0("%s() mci instance idx=%d releasing\n", __func__, mci->mc_idx);
  524. /* decrement the module ref count */
  525. module_put(mci->owner);
  526. }
  527. static struct kobj_type ktype_mci = {
  528. .release = edac_mci_control_release,
  529. .sysfs_ops = &mci_ops,
  530. .default_attrs = (struct attribute **)mci_attr,
  531. };
  532. /* EDAC memory controller sysfs kset:
  533. * /sys/devices/system/edac/mc
  534. */
  535. static struct kset *mc_kset;
  536. /*
  537. * edac_mc_register_sysfs_main_kobj
  538. *
  539. * setups and registers the main kobject for each mci
  540. */
  541. int edac_mc_register_sysfs_main_kobj(struct mem_ctl_info *mci)
  542. {
  543. struct kobject *kobj_mci;
  544. int err;
  545. debugf1("%s()\n", __func__);
  546. kobj_mci = &mci->edac_mci_kobj;
  547. /* Init the mci's kobject */
  548. memset(kobj_mci, 0, sizeof(*kobj_mci));
  549. /* Record which module 'owns' this control structure
  550. * and bump the ref count of the module
  551. */
  552. mci->owner = THIS_MODULE;
  553. /* bump ref count on this module */
  554. if (!try_module_get(mci->owner)) {
  555. err = -ENODEV;
  556. goto fail_out;
  557. }
  558. /* this instance become part of the mc_kset */
  559. kobj_mci->kset = mc_kset;
  560. /* register the mc<id> kobject to the mc_kset */
  561. err = kobject_init_and_add(kobj_mci, &ktype_mci, NULL,
  562. "mc%d", mci->mc_idx);
  563. if (err) {
  564. debugf1("%s()Failed to register '.../edac/mc%d'\n",
  565. __func__, mci->mc_idx);
  566. goto kobj_reg_fail;
  567. }
  568. kobject_uevent(kobj_mci, KOBJ_ADD);
  569. /* At this point, to 'free' the control struct,
  570. * edac_mc_unregister_sysfs_main_kobj() must be used
  571. */
  572. debugf1("%s() Registered '.../edac/mc%d' kobject\n",
  573. __func__, mci->mc_idx);
  574. return 0;
  575. /* Error exit stack */
  576. kobj_reg_fail:
  577. module_put(mci->owner);
  578. fail_out:
  579. return err;
  580. }
  581. /*
  582. * edac_mc_register_sysfs_main_kobj
  583. *
  584. * tears down and the main mci kobject from the mc_kset
  585. */
  586. void edac_mc_unregister_sysfs_main_kobj(struct mem_ctl_info *mci)
  587. {
  588. debugf1("%s()\n", __func__);
  589. /* delete the kobj from the mc_kset */
  590. kobject_put(&mci->edac_mci_kobj);
  591. }
  592. #define EDAC_DEVICE_SYMLINK "device"
  593. #define grp_to_mci(k) (container_of(k, struct mcidev_sysfs_group_kobj, kobj)->mci)
  594. /* MCI show/store functions for top most object */
  595. static ssize_t inst_grp_show(struct kobject *kobj, struct attribute *attr,
  596. char *buffer)
  597. {
  598. struct mem_ctl_info *mem_ctl_info = grp_to_mci(kobj);
  599. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  600. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  601. if (mcidev_attr->show)
  602. return mcidev_attr->show(mem_ctl_info, buffer);
  603. return -EIO;
  604. }
  605. static ssize_t inst_grp_store(struct kobject *kobj, struct attribute *attr,
  606. const char *buffer, size_t count)
  607. {
  608. struct mem_ctl_info *mem_ctl_info = grp_to_mci(kobj);
  609. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  610. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  611. if (mcidev_attr->store)
  612. return mcidev_attr->store(mem_ctl_info, buffer, count);
  613. return -EIO;
  614. }
  615. /* No memory to release for this kobj */
  616. static void edac_inst_grp_release(struct kobject *kobj)
  617. {
  618. struct mcidev_sysfs_group_kobj *grp;
  619. struct mem_ctl_info *mci;
  620. debugf1("%s()\n", __func__);
  621. grp = container_of(kobj, struct mcidev_sysfs_group_kobj, kobj);
  622. mci = grp->mci;
  623. }
  624. /* Intermediate show/store table */
  625. static struct sysfs_ops inst_grp_ops = {
  626. .show = inst_grp_show,
  627. .store = inst_grp_store
  628. };
  629. /* the kobj_type instance for a instance group */
  630. static struct kobj_type ktype_inst_grp = {
  631. .release = edac_inst_grp_release,
  632. .sysfs_ops = &inst_grp_ops,
  633. };
  634. /*
  635. * edac_create_mci_instance_attributes
  636. * create MC driver specific attributes bellow an specified kobj
  637. * This routine calls itself recursively, in order to create an entire
  638. * object tree.
  639. */
  640. static int edac_create_mci_instance_attributes(struct mem_ctl_info *mci,
  641. const struct mcidev_sysfs_attribute *sysfs_attrib,
  642. struct kobject *kobj)
  643. {
  644. int err;
  645. debugf4("%s()\n", __func__);
  646. while (sysfs_attrib) {
  647. debugf4("%s() sysfs_attrib = %p\n",__func__, sysfs_attrib);
  648. if (sysfs_attrib->grp) {
  649. struct mcidev_sysfs_group_kobj *grp_kobj;
  650. grp_kobj = kzalloc(sizeof(*grp_kobj), GFP_KERNEL);
  651. if (!grp_kobj)
  652. return -ENOMEM;
  653. grp_kobj->grp = sysfs_attrib->grp;
  654. grp_kobj->mci = mci;
  655. list_add_tail(&grp_kobj->list, &mci->grp_kobj_list);
  656. debugf0("%s() grp %s, mci %p\n", __func__,
  657. sysfs_attrib->grp->name, mci);
  658. err = kobject_init_and_add(&grp_kobj->kobj,
  659. &ktype_inst_grp,
  660. &mci->edac_mci_kobj,
  661. sysfs_attrib->grp->name);
  662. if (err < 0) {
  663. printk(KERN_ERR "kobject_init_and_add failed: %d\n", err);
  664. return err;
  665. }
  666. err = edac_create_mci_instance_attributes(mci,
  667. grp_kobj->grp->mcidev_attr,
  668. &grp_kobj->kobj);
  669. if (err < 0)
  670. return err;
  671. } else if (sysfs_attrib->attr.name) {
  672. debugf4("%s() file %s\n", __func__,
  673. sysfs_attrib->attr.name);
  674. err = sysfs_create_file(kobj, &sysfs_attrib->attr);
  675. if (err < 0) {
  676. printk(KERN_ERR "sysfs_create_file failed: %d\n", err);
  677. return err;
  678. }
  679. } else
  680. break;
  681. sysfs_attrib++;
  682. }
  683. return 0;
  684. }
  685. /*
  686. * edac_remove_mci_instance_attributes
  687. * remove MC driver specific attributes at the topmost level
  688. * directory of this mci instance.
  689. */
  690. static void edac_remove_mci_instance_attributes(struct mem_ctl_info *mci,
  691. const struct mcidev_sysfs_attribute *sysfs_attrib,
  692. struct kobject *kobj, int count)
  693. {
  694. struct mcidev_sysfs_group_kobj *grp_kobj, *tmp;
  695. debugf1("%s()\n", __func__);
  696. /*
  697. * loop if there are attributes and until we hit a NULL entry
  698. * Remove first all the attributes
  699. */
  700. while (sysfs_attrib) {
  701. debugf4("%s() sysfs_attrib = %p\n",__func__, sysfs_attrib);
  702. if (sysfs_attrib->grp) {
  703. debugf4("%s() seeking for group %s\n",
  704. __func__, sysfs_attrib->grp->name);
  705. list_for_each_entry(grp_kobj,
  706. &mci->grp_kobj_list, list) {
  707. debugf4("%s() grp_kobj->grp = %p\n",__func__, grp_kobj->grp);
  708. if (grp_kobj->grp == sysfs_attrib->grp) {
  709. edac_remove_mci_instance_attributes(mci,
  710. grp_kobj->grp->mcidev_attr,
  711. &grp_kobj->kobj, count + 1);
  712. debugf4("%s() group %s\n", __func__,
  713. sysfs_attrib->grp->name);
  714. kobject_put(&grp_kobj->kobj);
  715. }
  716. }
  717. debugf4("%s() end of seeking for group %s\n",
  718. __func__, sysfs_attrib->grp->name);
  719. } else if (sysfs_attrib->attr.name) {
  720. debugf4("%s() file %s\n", __func__,
  721. sysfs_attrib->attr.name);
  722. sysfs_remove_file(kobj, &sysfs_attrib->attr);
  723. } else
  724. break;
  725. sysfs_attrib++;
  726. }
  727. /* Remove the group objects */
  728. if (count)
  729. return;
  730. list_for_each_entry_safe(grp_kobj, tmp,
  731. &mci->grp_kobj_list, list) {
  732. list_del(&grp_kobj->list);
  733. kfree(grp_kobj);
  734. }
  735. }
  736. /*
  737. * Create a new Memory Controller kobject instance,
  738. * mc<id> under the 'mc' directory
  739. *
  740. * Return:
  741. * 0 Success
  742. * !0 Failure
  743. */
  744. int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
  745. {
  746. int i, j;
  747. int err;
  748. struct csrow_info *csrow;
  749. struct kobject *kobj_mci = &mci->edac_mci_kobj;
  750. debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
  751. INIT_LIST_HEAD(&mci->grp_kobj_list);
  752. /* create a symlink for the device */
  753. err = sysfs_create_link(kobj_mci, &mci->dev->kobj,
  754. EDAC_DEVICE_SYMLINK);
  755. if (err) {
  756. debugf1("%s() failure to create symlink\n", __func__);
  757. goto fail0;
  758. }
  759. /* If the low level driver desires some attributes,
  760. * then create them now for the driver.
  761. */
  762. if (mci->mc_driver_sysfs_attributes) {
  763. err = edac_create_mci_instance_attributes(mci,
  764. mci->mc_driver_sysfs_attributes,
  765. &mci->edac_mci_kobj);
  766. if (err) {
  767. debugf1("%s() failure to create mci attributes\n",
  768. __func__);
  769. goto fail0;
  770. }
  771. }
  772. /* Make directories for each CSROW object under the mc<id> kobject
  773. */
  774. for (i = 0; i < mci->nr_csrows; i++) {
  775. int nr_pages = 0;
  776. csrow = &mci->csrows[i];
  777. for (j = 0; j < csrow->nr_channels; j++)
  778. nr_pages += csrow->channels[j].dimm->nr_pages;
  779. if (nr_pages > 0) {
  780. err = edac_create_csrow_object(mci, csrow, i);
  781. if (err) {
  782. debugf1("%s() failure: create csrow %d obj\n",
  783. __func__, i);
  784. goto fail1;
  785. }
  786. }
  787. }
  788. return 0;
  789. fail1:
  790. for (i--; i >= 0; i--) {
  791. int nr_pages = 0;
  792. csrow = &mci->csrows[i];
  793. for (j = 0; j < csrow->nr_channels; j++)
  794. nr_pages += csrow->channels[j].dimm->nr_pages;
  795. if (nr_pages > 0)
  796. kobject_put(&mci->csrows[i].kobj);
  797. }
  798. /* remove the mci instance's attributes, if any */
  799. edac_remove_mci_instance_attributes(mci,
  800. mci->mc_driver_sysfs_attributes, &mci->edac_mci_kobj, 0);
  801. /* remove the symlink */
  802. sysfs_remove_link(kobj_mci, EDAC_DEVICE_SYMLINK);
  803. fail0:
  804. return err;
  805. }
  806. /*
  807. * remove a Memory Controller instance
  808. */
  809. void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
  810. {
  811. struct csrow_info *csrow;
  812. int i, j;
  813. debugf0("%s()\n", __func__);
  814. /* remove all csrow kobjects */
  815. debugf4("%s() unregister this mci kobj\n", __func__);
  816. for (i = 0; i < mci->nr_csrows; i++) {
  817. int nr_pages = 0;
  818. csrow = &mci->csrows[i];
  819. for (j = 0; j < csrow->nr_channels; j++)
  820. nr_pages += csrow->channels[j].dimm->nr_pages;
  821. if (nr_pages > 0) {
  822. debugf0("%s() unreg csrow-%d\n", __func__, i);
  823. kobject_put(&mci->csrows[i].kobj);
  824. }
  825. }
  826. /* remove this mci instance's attribtes */
  827. if (mci->mc_driver_sysfs_attributes) {
  828. debugf4("%s() unregister mci private attributes\n", __func__);
  829. edac_remove_mci_instance_attributes(mci,
  830. mci->mc_driver_sysfs_attributes,
  831. &mci->edac_mci_kobj, 0);
  832. }
  833. /* remove the symlink */
  834. debugf4("%s() remove_link\n", __func__);
  835. sysfs_remove_link(&mci->edac_mci_kobj, EDAC_DEVICE_SYMLINK);
  836. /* unregister this instance's kobject */
  837. debugf4("%s() remove_mci_instance\n", __func__);
  838. kobject_put(&mci->edac_mci_kobj);
  839. }
  840. /*
  841. * edac_setup_sysfs_mc_kset(void)
  842. *
  843. * Initialize the mc_kset for the 'mc' entry
  844. * This requires creating the top 'mc' directory with a kset
  845. * and its controls/attributes.
  846. *
  847. * To this 'mc' kset, instance 'mci' will be grouped as children.
  848. *
  849. * Return: 0 SUCCESS
  850. * !0 FAILURE error code
  851. */
  852. int edac_sysfs_setup_mc_kset(void)
  853. {
  854. int err = -EINVAL;
  855. struct bus_type *edac_subsys;
  856. debugf1("%s()\n", __func__);
  857. /* get the /sys/devices/system/edac subsys reference */
  858. edac_subsys = edac_get_sysfs_subsys();
  859. if (edac_subsys == NULL) {
  860. debugf1("%s() no edac_subsys error=%d\n", __func__, err);
  861. goto fail_out;
  862. }
  863. /* Init the MC's kobject */
  864. mc_kset = kset_create_and_add("mc", NULL, &edac_subsys->dev_root->kobj);
  865. if (!mc_kset) {
  866. err = -ENOMEM;
  867. debugf1("%s() Failed to register '.../edac/mc'\n", __func__);
  868. goto fail_kset;
  869. }
  870. debugf1("%s() Registered '.../edac/mc' kobject\n", __func__);
  871. return 0;
  872. fail_kset:
  873. edac_put_sysfs_subsys();
  874. fail_out:
  875. return err;
  876. }
  877. /*
  878. * edac_sysfs_teardown_mc_kset
  879. *
  880. * deconstruct the mc_ket for memory controllers
  881. */
  882. void edac_sysfs_teardown_mc_kset(void)
  883. {
  884. kset_unregister(mc_kset);
  885. edac_put_sysfs_subsys();
  886. }