edac_mc_sysfs.c 26 KB

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