edac_mc_sysfs.c 26 KB

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