edac_mc_sysfs.c 27 KB

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