edac_mc_sysfs.c 23 KB

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