edac_mc_sysfs.c 26 KB

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