edac_mc_sysfs.c 23 KB

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