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_DEBUG, EDAC_MC, "Scrub rate set to %d\n", new_bw);
  389. return count;
  390. }
  391. edac_printk(KERN_DEBUG, EDAC_MC, "Error setting scrub rate to: %lu\n", bandwidth);
  392. return -EINVAL;
  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. edac_printk(KERN_DEBUG, EDAC_MC, "Read scrub rate: %d\n", bandwidth);
  408. return sprintf(data, "%d\n", bandwidth);
  409. }
  410. /* default attribute files for the MCI object */
  411. static ssize_t mci_ue_count_show(struct mem_ctl_info *mci, char *data)
  412. {
  413. return sprintf(data, "%d\n", mci->ue_count);
  414. }
  415. static ssize_t mci_ce_count_show(struct mem_ctl_info *mci, char *data)
  416. {
  417. return sprintf(data, "%d\n", mci->ce_count);
  418. }
  419. static ssize_t mci_ce_noinfo_show(struct mem_ctl_info *mci, char *data)
  420. {
  421. return sprintf(data, "%d\n", mci->ce_noinfo_count);
  422. }
  423. static ssize_t mci_ue_noinfo_show(struct mem_ctl_info *mci, char *data)
  424. {
  425. return sprintf(data, "%d\n", mci->ue_noinfo_count);
  426. }
  427. static ssize_t mci_seconds_show(struct mem_ctl_info *mci, char *data)
  428. {
  429. return sprintf(data, "%ld\n", (jiffies - mci->start_time) / HZ);
  430. }
  431. static ssize_t mci_ctl_name_show(struct mem_ctl_info *mci, char *data)
  432. {
  433. return sprintf(data, "%s\n", mci->ctl_name);
  434. }
  435. static ssize_t mci_size_mb_show(struct mem_ctl_info *mci, char *data)
  436. {
  437. int total_pages, csrow_idx;
  438. for (total_pages = csrow_idx = 0; csrow_idx < mci->nr_csrows;
  439. csrow_idx++) {
  440. struct csrow_info *csrow = &mci->csrows[csrow_idx];
  441. if (!csrow->nr_pages)
  442. continue;
  443. total_pages += csrow->nr_pages;
  444. }
  445. return sprintf(data, "%u\n", PAGES_TO_MiB(total_pages));
  446. }
  447. #define to_mci(k) container_of(k, struct mem_ctl_info, edac_mci_kobj)
  448. #define to_mcidev_attr(a) container_of(a,struct mcidev_sysfs_attribute,attr)
  449. /* MCI show/store functions for top most object */
  450. static ssize_t mcidev_show(struct kobject *kobj, struct attribute *attr,
  451. char *buffer)
  452. {
  453. struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
  454. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  455. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  456. if (mcidev_attr->show)
  457. return mcidev_attr->show(mem_ctl_info, buffer);
  458. return -EIO;
  459. }
  460. static ssize_t mcidev_store(struct kobject *kobj, struct attribute *attr,
  461. const char *buffer, size_t count)
  462. {
  463. struct mem_ctl_info *mem_ctl_info = to_mci(kobj);
  464. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  465. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  466. if (mcidev_attr->store)
  467. return mcidev_attr->store(mem_ctl_info, buffer, count);
  468. return -EIO;
  469. }
  470. /* Intermediate show/store table */
  471. static const struct sysfs_ops mci_ops = {
  472. .show = mcidev_show,
  473. .store = mcidev_store
  474. };
  475. #define MCIDEV_ATTR(_name,_mode,_show,_store) \
  476. static struct mcidev_sysfs_attribute mci_attr_##_name = { \
  477. .attr = {.name = __stringify(_name), .mode = _mode }, \
  478. .show = _show, \
  479. .store = _store, \
  480. };
  481. /* default Control file */
  482. MCIDEV_ATTR(reset_counters, S_IWUSR, NULL, mci_reset_counters_store);
  483. /* default Attribute files */
  484. MCIDEV_ATTR(mc_name, S_IRUGO, mci_ctl_name_show, NULL);
  485. MCIDEV_ATTR(size_mb, S_IRUGO, mci_size_mb_show, NULL);
  486. MCIDEV_ATTR(seconds_since_reset, S_IRUGO, mci_seconds_show, NULL);
  487. MCIDEV_ATTR(ue_noinfo_count, S_IRUGO, mci_ue_noinfo_show, NULL);
  488. MCIDEV_ATTR(ce_noinfo_count, S_IRUGO, mci_ce_noinfo_show, NULL);
  489. MCIDEV_ATTR(ue_count, S_IRUGO, mci_ue_count_show, NULL);
  490. MCIDEV_ATTR(ce_count, S_IRUGO, mci_ce_count_show, NULL);
  491. /* memory scrubber attribute file */
  492. MCIDEV_ATTR(sdram_scrub_rate, S_IRUGO | S_IWUSR, mci_sdram_scrub_rate_show,
  493. mci_sdram_scrub_rate_store);
  494. static struct mcidev_sysfs_attribute *mci_attr[] = {
  495. &mci_attr_reset_counters,
  496. &mci_attr_mc_name,
  497. &mci_attr_size_mb,
  498. &mci_attr_seconds_since_reset,
  499. &mci_attr_ue_noinfo_count,
  500. &mci_attr_ce_noinfo_count,
  501. &mci_attr_ue_count,
  502. &mci_attr_ce_count,
  503. &mci_attr_sdram_scrub_rate,
  504. NULL
  505. };
  506. /*
  507. * Release of a MC controlling instance
  508. *
  509. * each MC control instance has the following resources upon entry:
  510. * a) a ref count on the top memctl kobj
  511. * b) a ref count on this module
  512. *
  513. * this function must decrement those ref counts and then
  514. * issue a free on the instance's memory
  515. */
  516. static void edac_mci_control_release(struct kobject *kobj)
  517. {
  518. struct mem_ctl_info *mci;
  519. mci = to_mci(kobj);
  520. debugf0("%s() mci instance idx=%d releasing\n", __func__, mci->mc_idx);
  521. /* decrement the module ref count */
  522. module_put(mci->owner);
  523. }
  524. static struct kobj_type ktype_mci = {
  525. .release = edac_mci_control_release,
  526. .sysfs_ops = &mci_ops,
  527. .default_attrs = (struct attribute **)mci_attr,
  528. };
  529. /* EDAC memory controller sysfs kset:
  530. * /sys/devices/system/edac/mc
  531. */
  532. static struct kset *mc_kset;
  533. /*
  534. * edac_mc_register_sysfs_main_kobj
  535. *
  536. * setups and registers the main kobject for each mci
  537. */
  538. int edac_mc_register_sysfs_main_kobj(struct mem_ctl_info *mci)
  539. {
  540. struct kobject *kobj_mci;
  541. int err;
  542. debugf1("%s()\n", __func__);
  543. kobj_mci = &mci->edac_mci_kobj;
  544. /* Init the mci's kobject */
  545. memset(kobj_mci, 0, sizeof(*kobj_mci));
  546. /* Record which module 'owns' this control structure
  547. * and bump the ref count of the module
  548. */
  549. mci->owner = THIS_MODULE;
  550. /* bump ref count on this module */
  551. if (!try_module_get(mci->owner)) {
  552. err = -ENODEV;
  553. goto fail_out;
  554. }
  555. /* this instance become part of the mc_kset */
  556. kobj_mci->kset = mc_kset;
  557. /* register the mc<id> kobject to the mc_kset */
  558. err = kobject_init_and_add(kobj_mci, &ktype_mci, NULL,
  559. "mc%d", mci->mc_idx);
  560. if (err) {
  561. debugf1("%s()Failed to register '.../edac/mc%d'\n",
  562. __func__, mci->mc_idx);
  563. goto kobj_reg_fail;
  564. }
  565. kobject_uevent(kobj_mci, KOBJ_ADD);
  566. /* At this point, to 'free' the control struct,
  567. * edac_mc_unregister_sysfs_main_kobj() must be used
  568. */
  569. debugf1("%s() Registered '.../edac/mc%d' kobject\n",
  570. __func__, mci->mc_idx);
  571. return 0;
  572. /* Error exit stack */
  573. kobj_reg_fail:
  574. module_put(mci->owner);
  575. fail_out:
  576. return err;
  577. }
  578. /*
  579. * edac_mc_register_sysfs_main_kobj
  580. *
  581. * tears down and the main mci kobject from the mc_kset
  582. */
  583. void edac_mc_unregister_sysfs_main_kobj(struct mem_ctl_info *mci)
  584. {
  585. debugf1("%s()\n", __func__);
  586. /* delete the kobj from the mc_kset */
  587. kobject_put(&mci->edac_mci_kobj);
  588. }
  589. #define EDAC_DEVICE_SYMLINK "device"
  590. #define grp_to_mci(k) (container_of(k, struct mcidev_sysfs_group_kobj, kobj)->mci)
  591. /* MCI show/store functions for top most object */
  592. static ssize_t inst_grp_show(struct kobject *kobj, struct attribute *attr,
  593. char *buffer)
  594. {
  595. struct mem_ctl_info *mem_ctl_info = grp_to_mci(kobj);
  596. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  597. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  598. if (mcidev_attr->show)
  599. return mcidev_attr->show(mem_ctl_info, buffer);
  600. return -EIO;
  601. }
  602. static ssize_t inst_grp_store(struct kobject *kobj, struct attribute *attr,
  603. const char *buffer, size_t count)
  604. {
  605. struct mem_ctl_info *mem_ctl_info = grp_to_mci(kobj);
  606. struct mcidev_sysfs_attribute *mcidev_attr = to_mcidev_attr(attr);
  607. debugf1("%s() mem_ctl_info %p\n", __func__, mem_ctl_info);
  608. if (mcidev_attr->store)
  609. return mcidev_attr->store(mem_ctl_info, buffer, count);
  610. return -EIO;
  611. }
  612. /* No memory to release for this kobj */
  613. static void edac_inst_grp_release(struct kobject *kobj)
  614. {
  615. struct mcidev_sysfs_group_kobj *grp;
  616. struct mem_ctl_info *mci;
  617. debugf1("%s()\n", __func__);
  618. grp = container_of(kobj, struct mcidev_sysfs_group_kobj, kobj);
  619. mci = grp->mci;
  620. }
  621. /* Intermediate show/store table */
  622. static struct sysfs_ops inst_grp_ops = {
  623. .show = inst_grp_show,
  624. .store = inst_grp_store
  625. };
  626. /* the kobj_type instance for a instance group */
  627. static struct kobj_type ktype_inst_grp = {
  628. .release = edac_inst_grp_release,
  629. .sysfs_ops = &inst_grp_ops,
  630. };
  631. /*
  632. * edac_create_mci_instance_attributes
  633. * create MC driver specific attributes bellow an specified kobj
  634. * This routine calls itself recursively, in order to create an entire
  635. * object tree.
  636. */
  637. static int edac_create_mci_instance_attributes(struct mem_ctl_info *mci,
  638. const struct mcidev_sysfs_attribute *sysfs_attrib,
  639. struct kobject *kobj)
  640. {
  641. int err;
  642. debugf1("%s()\n", __func__);
  643. while (sysfs_attrib) {
  644. debugf1("%s() sysfs_attrib = %p\n",__func__, sysfs_attrib);
  645. if (sysfs_attrib->grp) {
  646. struct mcidev_sysfs_group_kobj *grp_kobj;
  647. grp_kobj = kzalloc(sizeof(*grp_kobj), GFP_KERNEL);
  648. if (!grp_kobj)
  649. return -ENOMEM;
  650. grp_kobj->grp = sysfs_attrib->grp;
  651. grp_kobj->mci = mci;
  652. list_add_tail(&grp_kobj->list, &mci->grp_kobj_list);
  653. debugf0("%s() grp %s, mci %p\n", __func__,
  654. sysfs_attrib->grp->name, mci);
  655. err = kobject_init_and_add(&grp_kobj->kobj,
  656. &ktype_inst_grp,
  657. &mci->edac_mci_kobj,
  658. sysfs_attrib->grp->name);
  659. if (err < 0) {
  660. printk(KERN_ERR "kobject_init_and_add failed: %d\n", err);
  661. return err;
  662. }
  663. err = edac_create_mci_instance_attributes(mci,
  664. grp_kobj->grp->mcidev_attr,
  665. &grp_kobj->kobj);
  666. if (err < 0)
  667. return err;
  668. } else if (sysfs_attrib->attr.name) {
  669. debugf0("%s() file %s\n", __func__,
  670. sysfs_attrib->attr.name);
  671. err = sysfs_create_file(kobj, &sysfs_attrib->attr);
  672. if (err < 0) {
  673. printk(KERN_ERR "sysfs_create_file failed: %d\n", err);
  674. return err;
  675. }
  676. } else
  677. break;
  678. sysfs_attrib++;
  679. }
  680. return 0;
  681. }
  682. /*
  683. * edac_remove_mci_instance_attributes
  684. * remove MC driver specific attributes at the topmost level
  685. * directory of this mci instance.
  686. */
  687. static void edac_remove_mci_instance_attributes(struct mem_ctl_info *mci,
  688. const struct mcidev_sysfs_attribute *sysfs_attrib,
  689. struct kobject *kobj, int count)
  690. {
  691. struct mcidev_sysfs_group_kobj *grp_kobj, *tmp;
  692. debugf1("%s()\n", __func__);
  693. /*
  694. * loop if there are attributes and until we hit a NULL entry
  695. * Remove first all the atributes
  696. */
  697. while (sysfs_attrib) {
  698. debugf1("%s() sysfs_attrib = %p\n",__func__, sysfs_attrib);
  699. if (sysfs_attrib->grp) {
  700. debugf1("%s() seeking for group %s\n",
  701. __func__, sysfs_attrib->grp->name);
  702. list_for_each_entry(grp_kobj,
  703. &mci->grp_kobj_list, list) {
  704. debugf1("%s() grp_kobj->grp = %p\n",__func__, grp_kobj->grp);
  705. if (grp_kobj->grp == sysfs_attrib->grp) {
  706. edac_remove_mci_instance_attributes(mci,
  707. grp_kobj->grp->mcidev_attr,
  708. &grp_kobj->kobj, count + 1);
  709. debugf0("%s() group %s\n", __func__,
  710. sysfs_attrib->grp->name);
  711. kobject_put(&grp_kobj->kobj);
  712. }
  713. }
  714. debugf1("%s() end of seeking for group %s\n",
  715. __func__, sysfs_attrib->grp->name);
  716. } else if (sysfs_attrib->attr.name) {
  717. debugf0("%s() file %s\n", __func__,
  718. sysfs_attrib->attr.name);
  719. sysfs_remove_file(kobj, &sysfs_attrib->attr);
  720. } else
  721. break;
  722. sysfs_attrib++;
  723. }
  724. /* Remove the group objects */
  725. if (count)
  726. return;
  727. list_for_each_entry_safe(grp_kobj, tmp,
  728. &mci->grp_kobj_list, list) {
  729. list_del(&grp_kobj->list);
  730. kfree(grp_kobj);
  731. }
  732. }
  733. /*
  734. * Create a new Memory Controller kobject instance,
  735. * mc<id> under the 'mc' directory
  736. *
  737. * Return:
  738. * 0 Success
  739. * !0 Failure
  740. */
  741. int edac_create_sysfs_mci_device(struct mem_ctl_info *mci)
  742. {
  743. int i;
  744. int err;
  745. struct csrow_info *csrow;
  746. struct kobject *kobj_mci = &mci->edac_mci_kobj;
  747. debugf0("%s() idx=%d\n", __func__, mci->mc_idx);
  748. INIT_LIST_HEAD(&mci->grp_kobj_list);
  749. /* create a symlink for the device */
  750. err = sysfs_create_link(kobj_mci, &mci->dev->kobj,
  751. EDAC_DEVICE_SYMLINK);
  752. if (err) {
  753. debugf1("%s() failure to create symlink\n", __func__);
  754. goto fail0;
  755. }
  756. /* If the low level driver desires some attributes,
  757. * then create them now for the driver.
  758. */
  759. if (mci->mc_driver_sysfs_attributes) {
  760. err = edac_create_mci_instance_attributes(mci,
  761. mci->mc_driver_sysfs_attributes,
  762. &mci->edac_mci_kobj);
  763. if (err) {
  764. debugf1("%s() failure to create mci attributes\n",
  765. __func__);
  766. goto fail0;
  767. }
  768. }
  769. /* Make directories for each CSROW object under the mc<id> kobject
  770. */
  771. for (i = 0; i < mci->nr_csrows; i++) {
  772. csrow = &mci->csrows[i];
  773. /* Only expose populated CSROWs */
  774. if (csrow->nr_pages > 0) {
  775. err = edac_create_csrow_object(mci, csrow, i);
  776. if (err) {
  777. debugf1("%s() failure: create csrow %d obj\n",
  778. __func__, i);
  779. goto fail1;
  780. }
  781. }
  782. }
  783. return 0;
  784. /* CSROW error: backout what has already been registered, */
  785. fail1:
  786. for (i--; i >= 0; i--) {
  787. if (csrow->nr_pages > 0) {
  788. kobject_put(&mci->csrows[i].kobj);
  789. }
  790. }
  791. /* remove the mci instance's attributes, if any */
  792. edac_remove_mci_instance_attributes(mci,
  793. mci->mc_driver_sysfs_attributes, &mci->edac_mci_kobj, 0);
  794. /* remove the symlink */
  795. sysfs_remove_link(kobj_mci, EDAC_DEVICE_SYMLINK);
  796. fail0:
  797. return err;
  798. }
  799. /*
  800. * remove a Memory Controller instance
  801. */
  802. void edac_remove_sysfs_mci_device(struct mem_ctl_info *mci)
  803. {
  804. int i;
  805. debugf0("%s()\n", __func__);
  806. /* remove all csrow kobjects */
  807. debugf0("%s() unregister this mci kobj\n", __func__);
  808. for (i = 0; i < mci->nr_csrows; i++) {
  809. if (mci->csrows[i].nr_pages > 0) {
  810. debugf0("%s() unreg csrow-%d\n", __func__, i);
  811. kobject_put(&mci->csrows[i].kobj);
  812. }
  813. }
  814. /* remove this mci instance's attribtes */
  815. if (mci->mc_driver_sysfs_attributes) {
  816. debugf0("%s() unregister mci private attributes\n", __func__);
  817. edac_remove_mci_instance_attributes(mci,
  818. mci->mc_driver_sysfs_attributes,
  819. &mci->edac_mci_kobj, 0);
  820. }
  821. /* remove the symlink */
  822. debugf0("%s() remove_link\n", __func__);
  823. sysfs_remove_link(&mci->edac_mci_kobj, EDAC_DEVICE_SYMLINK);
  824. /* unregister this instance's kobject */
  825. debugf0("%s() remove_mci_instance\n", __func__);
  826. kobject_put(&mci->edac_mci_kobj);
  827. }
  828. /*
  829. * edac_setup_sysfs_mc_kset(void)
  830. *
  831. * Initialize the mc_kset for the 'mc' entry
  832. * This requires creating the top 'mc' directory with a kset
  833. * and its controls/attributes.
  834. *
  835. * To this 'mc' kset, instance 'mci' will be grouped as children.
  836. *
  837. * Return: 0 SUCCESS
  838. * !0 FAILURE error code
  839. */
  840. int edac_sysfs_setup_mc_kset(void)
  841. {
  842. int err = -EINVAL;
  843. struct sysdev_class *edac_class;
  844. debugf1("%s()\n", __func__);
  845. /* get the /sys/devices/system/edac class reference */
  846. edac_class = edac_get_sysfs_class();
  847. if (edac_class == NULL) {
  848. debugf1("%s() no edac_class error=%d\n", __func__, err);
  849. goto fail_out;
  850. }
  851. /* Init the MC's kobject */
  852. mc_kset = kset_create_and_add("mc", NULL, &edac_class->kset.kobj);
  853. if (!mc_kset) {
  854. err = -ENOMEM;
  855. debugf1("%s() Failed to register '.../edac/mc'\n", __func__);
  856. goto fail_kset;
  857. }
  858. debugf1("%s() Registered '.../edac/mc' kobject\n", __func__);
  859. return 0;
  860. fail_kset:
  861. edac_put_sysfs_class();
  862. fail_out:
  863. return err;
  864. }
  865. /*
  866. * edac_sysfs_teardown_mc_kset
  867. *
  868. * deconstruct the mc_ket for memory controllers
  869. */
  870. void edac_sysfs_teardown_mc_kset(void)
  871. {
  872. kset_unregister(mc_kset);
  873. edac_put_sysfs_class();
  874. }