edac_mc_sysfs.c 23 KB

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