edac_device_sysfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. * file for managing the edac_device class of devices for EDAC
  3. *
  4. * (C) 2007 SoftwareBitMaker (http://www.softwarebitmaker.com)
  5. *
  6. * This file may be distributed under the terms of the
  7. * GNU General Public License.
  8. *
  9. * Written Doug Thompson <norsk5@xmission.com>
  10. *
  11. */
  12. #include <linux/ctype.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include "edac_core.h"
  16. #include "edac_module.h"
  17. #define EDAC_DEVICE_SYMLINK "device"
  18. #define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj)
  19. #define to_edacdev_attr(a) container_of(a, struct edacdev_attribute, attr)
  20. /*
  21. * Set of edac_device_ctl_info attribute store/show functions
  22. */
  23. /* 'log_ue' */
  24. static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info
  25. *ctl_info, char *data)
  26. {
  27. return sprintf(data, "%u\n", ctl_info->log_ue);
  28. }
  29. static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info
  30. *ctl_info, const char *data,
  31. size_t count)
  32. {
  33. /* if parameter is zero, turn off flag, if non-zero turn on flag */
  34. ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0);
  35. return count;
  36. }
  37. /* 'log_ce' */
  38. static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info
  39. *ctl_info, char *data)
  40. {
  41. return sprintf(data, "%u\n", ctl_info->log_ce);
  42. }
  43. static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info
  44. *ctl_info, const char *data,
  45. size_t count)
  46. {
  47. /* if parameter is zero, turn off flag, if non-zero turn on flag */
  48. ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0);
  49. return count;
  50. }
  51. /* 'panic_on_ue' */
  52. static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info
  53. *ctl_info, char *data)
  54. {
  55. return sprintf(data, "%u\n", ctl_info->panic_on_ue);
  56. }
  57. static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info
  58. *ctl_info, const char *data,
  59. size_t count)
  60. {
  61. /* if parameter is zero, turn off flag, if non-zero turn on flag */
  62. ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0);
  63. return count;
  64. }
  65. /* 'poll_msec' show and store functions*/
  66. static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info
  67. *ctl_info, char *data)
  68. {
  69. return sprintf(data, "%u\n", ctl_info->poll_msec);
  70. }
  71. static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info
  72. *ctl_info, const char *data,
  73. size_t count)
  74. {
  75. unsigned long value;
  76. /* get the value and enforce that it is non-zero, must be at least
  77. * one millisecond for the delay period, between scans
  78. * Then cancel last outstanding delay for the work request
  79. * and set a new one.
  80. */
  81. value = simple_strtoul(data, NULL, 0);
  82. edac_device_reset_delay_period(ctl_info, value);
  83. return count;
  84. }
  85. /* edac_device_ctl_info specific attribute structure */
  86. struct ctl_info_attribute {
  87. struct attribute attr;
  88. ssize_t(*show) (struct edac_device_ctl_info *, char *);
  89. ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t);
  90. };
  91. #define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj)
  92. #define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr)
  93. /* Function to 'show' fields from the edac_dev 'ctl_info' structure */
  94. static ssize_t edac_dev_ctl_info_show(struct kobject *kobj,
  95. struct attribute *attr, char *buffer)
  96. {
  97. struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
  98. struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
  99. if (ctl_info_attr->show)
  100. return ctl_info_attr->show(edac_dev, buffer);
  101. return -EIO;
  102. }
  103. /* Function to 'store' fields into the edac_dev 'ctl_info' structure */
  104. static ssize_t edac_dev_ctl_info_store(struct kobject *kobj,
  105. struct attribute *attr,
  106. const char *buffer, size_t count)
  107. {
  108. struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj);
  109. struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr);
  110. if (ctl_info_attr->store)
  111. return ctl_info_attr->store(edac_dev, buffer, count);
  112. return -EIO;
  113. }
  114. /* edac_dev file operations for an 'ctl_info' */
  115. static const struct sysfs_ops device_ctl_info_ops = {
  116. .show = edac_dev_ctl_info_show,
  117. .store = edac_dev_ctl_info_store
  118. };
  119. #define CTL_INFO_ATTR(_name,_mode,_show,_store) \
  120. static struct ctl_info_attribute attr_ctl_info_##_name = { \
  121. .attr = {.name = __stringify(_name), .mode = _mode }, \
  122. .show = _show, \
  123. .store = _store, \
  124. };
  125. /* Declare the various ctl_info attributes here and their respective ops */
  126. CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR,
  127. edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store);
  128. CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR,
  129. edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store);
  130. CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR,
  131. edac_device_ctl_panic_on_ue_show,
  132. edac_device_ctl_panic_on_ue_store);
  133. CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR,
  134. edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store);
  135. /* Base Attributes of the EDAC_DEVICE ECC object */
  136. static struct ctl_info_attribute *device_ctrl_attr[] = {
  137. &attr_ctl_info_panic_on_ue,
  138. &attr_ctl_info_log_ue,
  139. &attr_ctl_info_log_ce,
  140. &attr_ctl_info_poll_msec,
  141. NULL,
  142. };
  143. /*
  144. * edac_device_ctrl_master_release
  145. *
  146. * called when the reference count for the 'main' kobj
  147. * for a edac_device control struct reaches zero
  148. *
  149. * Reference count model:
  150. * One 'main' kobject for each control structure allocated.
  151. * That main kobj is initially set to one AND
  152. * the reference count for the EDAC 'core' module is
  153. * bumped by one, thus added 'keep in memory' dependency.
  154. *
  155. * Each new internal kobj (in instances and blocks) then
  156. * bumps the 'main' kobject.
  157. *
  158. * When they are released their release functions decrement
  159. * the 'main' kobj.
  160. *
  161. * When the main kobj reaches zero (0) then THIS function
  162. * is called which then decrements the EDAC 'core' module.
  163. * When the module reference count reaches zero then the
  164. * module no longer has dependency on keeping the release
  165. * function code in memory and module can be unloaded.
  166. *
  167. * This will support several control objects as well, each
  168. * with its own 'main' kobj.
  169. */
  170. static void edac_device_ctrl_master_release(struct kobject *kobj)
  171. {
  172. struct edac_device_ctl_info *edac_dev = to_edacdev(kobj);
  173. debugf4("%s() control index=%d\n", __func__, edac_dev->dev_idx);
  174. /* decrement the EDAC CORE module ref count */
  175. module_put(edac_dev->owner);
  176. /* free the control struct containing the 'main' kobj
  177. * passed in to this routine
  178. */
  179. kfree(edac_dev);
  180. }
  181. /* ktype for the main (master) kobject */
  182. static struct kobj_type ktype_device_ctrl = {
  183. .release = edac_device_ctrl_master_release,
  184. .sysfs_ops = &device_ctl_info_ops,
  185. .default_attrs = (struct attribute **)device_ctrl_attr,
  186. };
  187. /*
  188. * edac_device_register_sysfs_main_kobj
  189. *
  190. * perform the high level setup for the new edac_device instance
  191. *
  192. * Return: 0 SUCCESS
  193. * !0 FAILURE
  194. */
  195. int edac_device_register_sysfs_main_kobj(struct edac_device_ctl_info *edac_dev)
  196. {
  197. struct sysdev_class *edac_class;
  198. int err;
  199. debugf1("%s()\n", __func__);
  200. /* get the /sys/devices/system/edac reference */
  201. edac_class = edac_get_edac_class();
  202. if (edac_class == NULL) {
  203. debugf1("%s() no edac_class error\n", __func__);
  204. err = -ENODEV;
  205. goto err_out;
  206. }
  207. /* Point to the 'edac_class' this instance 'reports' to */
  208. edac_dev->edac_class = edac_class;
  209. /* Init the devices's kobject */
  210. memset(&edac_dev->kobj, 0, sizeof(struct kobject));
  211. /* Record which module 'owns' this control structure
  212. * and bump the ref count of the module
  213. */
  214. edac_dev->owner = THIS_MODULE;
  215. if (!try_module_get(edac_dev->owner)) {
  216. err = -ENODEV;
  217. goto err_out;
  218. }
  219. /* register */
  220. err = kobject_init_and_add(&edac_dev->kobj, &ktype_device_ctrl,
  221. &edac_class->kset.kobj,
  222. "%s", edac_dev->name);
  223. if (err) {
  224. debugf1("%s()Failed to register '.../edac/%s'\n",
  225. __func__, edac_dev->name);
  226. goto err_kobj_reg;
  227. }
  228. kobject_uevent(&edac_dev->kobj, KOBJ_ADD);
  229. /* At this point, to 'free' the control struct,
  230. * edac_device_unregister_sysfs_main_kobj() must be used
  231. */
  232. debugf4("%s() Registered '.../edac/%s' kobject\n",
  233. __func__, edac_dev->name);
  234. return 0;
  235. /* Error exit stack */
  236. err_kobj_reg:
  237. module_put(edac_dev->owner);
  238. err_out:
  239. return err;
  240. }
  241. /*
  242. * edac_device_unregister_sysfs_main_kobj:
  243. * the '..../edac/<name>' kobject
  244. */
  245. void edac_device_unregister_sysfs_main_kobj(
  246. struct edac_device_ctl_info *edac_dev)
  247. {
  248. debugf0("%s()\n", __func__);
  249. debugf4("%s() name of kobject is: %s\n",
  250. __func__, kobject_name(&edac_dev->kobj));
  251. /*
  252. * Unregister the edac device's kobject and
  253. * allow for reference count to reach 0 at which point
  254. * the callback will be called to:
  255. * a) module_put() this module
  256. * b) 'kfree' the memory
  257. */
  258. kobject_put(&edac_dev->kobj);
  259. }
  260. /* edac_dev -> instance information */
  261. /*
  262. * Set of low-level instance attribute show functions
  263. */
  264. static ssize_t instance_ue_count_show(struct edac_device_instance *instance,
  265. char *data)
  266. {
  267. return sprintf(data, "%u\n", instance->counters.ue_count);
  268. }
  269. static ssize_t instance_ce_count_show(struct edac_device_instance *instance,
  270. char *data)
  271. {
  272. return sprintf(data, "%u\n", instance->counters.ce_count);
  273. }
  274. #define to_instance(k) container_of(k, struct edac_device_instance, kobj)
  275. #define to_instance_attr(a) container_of(a,struct instance_attribute,attr)
  276. /* DEVICE instance kobject release() function */
  277. static void edac_device_ctrl_instance_release(struct kobject *kobj)
  278. {
  279. struct edac_device_instance *instance;
  280. debugf1("%s()\n", __func__);
  281. /* map from this kobj to the main control struct
  282. * and then dec the main kobj count
  283. */
  284. instance = to_instance(kobj);
  285. kobject_put(&instance->ctl->kobj);
  286. }
  287. /* instance specific attribute structure */
  288. struct instance_attribute {
  289. struct attribute attr;
  290. ssize_t(*show) (struct edac_device_instance *, char *);
  291. ssize_t(*store) (struct edac_device_instance *, const char *, size_t);
  292. };
  293. /* Function to 'show' fields from the edac_dev 'instance' structure */
  294. static ssize_t edac_dev_instance_show(struct kobject *kobj,
  295. struct attribute *attr, char *buffer)
  296. {
  297. struct edac_device_instance *instance = to_instance(kobj);
  298. struct instance_attribute *instance_attr = to_instance_attr(attr);
  299. if (instance_attr->show)
  300. return instance_attr->show(instance, buffer);
  301. return -EIO;
  302. }
  303. /* Function to 'store' fields into the edac_dev 'instance' structure */
  304. static ssize_t edac_dev_instance_store(struct kobject *kobj,
  305. struct attribute *attr,
  306. const char *buffer, size_t count)
  307. {
  308. struct edac_device_instance *instance = to_instance(kobj);
  309. struct instance_attribute *instance_attr = to_instance_attr(attr);
  310. if (instance_attr->store)
  311. return instance_attr->store(instance, buffer, count);
  312. return -EIO;
  313. }
  314. /* edac_dev file operations for an 'instance' */
  315. static const struct sysfs_ops device_instance_ops = {
  316. .show = edac_dev_instance_show,
  317. .store = edac_dev_instance_store
  318. };
  319. #define INSTANCE_ATTR(_name,_mode,_show,_store) \
  320. static struct instance_attribute attr_instance_##_name = { \
  321. .attr = {.name = __stringify(_name), .mode = _mode }, \
  322. .show = _show, \
  323. .store = _store, \
  324. };
  325. /*
  326. * Define attributes visible for the edac_device instance object
  327. * Each contains a pointer to a show and an optional set
  328. * function pointer that does the low level output/input
  329. */
  330. INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL);
  331. INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL);
  332. /* list of edac_dev 'instance' attributes */
  333. static struct instance_attribute *device_instance_attr[] = {
  334. &attr_instance_ce_count,
  335. &attr_instance_ue_count,
  336. NULL,
  337. };
  338. /* The 'ktype' for each edac_dev 'instance' */
  339. static struct kobj_type ktype_instance_ctrl = {
  340. .release = edac_device_ctrl_instance_release,
  341. .sysfs_ops = &device_instance_ops,
  342. .default_attrs = (struct attribute **)device_instance_attr,
  343. };
  344. /* edac_dev -> instance -> block information */
  345. #define to_block(k) container_of(k, struct edac_device_block, kobj)
  346. #define to_block_attr(a) \
  347. container_of(a, struct edac_dev_sysfs_block_attribute, attr)
  348. /*
  349. * Set of low-level block attribute show functions
  350. */
  351. static ssize_t block_ue_count_show(struct kobject *kobj,
  352. struct attribute *attr, char *data)
  353. {
  354. struct edac_device_block *block = to_block(kobj);
  355. return sprintf(data, "%u\n", block->counters.ue_count);
  356. }
  357. static ssize_t block_ce_count_show(struct kobject *kobj,
  358. struct attribute *attr, char *data)
  359. {
  360. struct edac_device_block *block = to_block(kobj);
  361. return sprintf(data, "%u\n", block->counters.ce_count);
  362. }
  363. /* DEVICE block kobject release() function */
  364. static void edac_device_ctrl_block_release(struct kobject *kobj)
  365. {
  366. struct edac_device_block *block;
  367. debugf1("%s()\n", __func__);
  368. /* get the container of the kobj */
  369. block = to_block(kobj);
  370. /* map from 'block kobj' to 'block->instance->controller->main_kobj'
  371. * now 'release' the block kobject
  372. */
  373. kobject_put(&block->instance->ctl->kobj);
  374. }
  375. /* Function to 'show' fields from the edac_dev 'block' structure */
  376. static ssize_t edac_dev_block_show(struct kobject *kobj,
  377. struct attribute *attr, char *buffer)
  378. {
  379. struct edac_dev_sysfs_block_attribute *block_attr =
  380. to_block_attr(attr);
  381. if (block_attr->show)
  382. return block_attr->show(kobj, attr, buffer);
  383. return -EIO;
  384. }
  385. /* Function to 'store' fields into the edac_dev 'block' structure */
  386. static ssize_t edac_dev_block_store(struct kobject *kobj,
  387. struct attribute *attr,
  388. const char *buffer, size_t count)
  389. {
  390. struct edac_dev_sysfs_block_attribute *block_attr;
  391. block_attr = to_block_attr(attr);
  392. if (block_attr->store)
  393. return block_attr->store(kobj, attr, buffer, count);
  394. return -EIO;
  395. }
  396. /* edac_dev file operations for a 'block' */
  397. static const struct sysfs_ops device_block_ops = {
  398. .show = edac_dev_block_show,
  399. .store = edac_dev_block_store
  400. };
  401. #define BLOCK_ATTR(_name,_mode,_show,_store) \
  402. static struct edac_dev_sysfs_block_attribute attr_block_##_name = { \
  403. .attr = {.name = __stringify(_name), .mode = _mode }, \
  404. .show = _show, \
  405. .store = _store, \
  406. };
  407. BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show, NULL);
  408. BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show, NULL);
  409. /* list of edac_dev 'block' attributes */
  410. static struct edac_dev_sysfs_block_attribute *device_block_attr[] = {
  411. &attr_block_ce_count,
  412. &attr_block_ue_count,
  413. NULL,
  414. };
  415. /* The 'ktype' for each edac_dev 'block' */
  416. static struct kobj_type ktype_block_ctrl = {
  417. .release = edac_device_ctrl_block_release,
  418. .sysfs_ops = &device_block_ops,
  419. .default_attrs = (struct attribute **)device_block_attr,
  420. };
  421. /* block ctor/dtor code */
  422. /*
  423. * edac_device_create_block
  424. */
  425. static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
  426. struct edac_device_instance *instance,
  427. struct edac_device_block *block)
  428. {
  429. int i;
  430. int err;
  431. struct edac_dev_sysfs_block_attribute *sysfs_attrib;
  432. struct kobject *main_kobj;
  433. debugf4("%s() Instance '%s' inst_p=%p block '%s' block_p=%p\n",
  434. __func__, instance->name, instance, block->name, block);
  435. debugf4("%s() block kobj=%p block kobj->parent=%p\n",
  436. __func__, &block->kobj, &block->kobj.parent);
  437. /* init this block's kobject */
  438. memset(&block->kobj, 0, sizeof(struct kobject));
  439. /* bump the main kobject's reference count for this controller
  440. * and this instance is dependant on the main
  441. */
  442. main_kobj = kobject_get(&edac_dev->kobj);
  443. if (!main_kobj) {
  444. err = -ENODEV;
  445. goto err_out;
  446. }
  447. /* Add this block's kobject */
  448. err = kobject_init_and_add(&block->kobj, &ktype_block_ctrl,
  449. &instance->kobj,
  450. "%s", block->name);
  451. if (err) {
  452. debugf1("%s() Failed to register instance '%s'\n",
  453. __func__, block->name);
  454. kobject_put(main_kobj);
  455. err = -ENODEV;
  456. goto err_out;
  457. }
  458. /* If there are driver level block attributes, then added them
  459. * to the block kobject
  460. */
  461. sysfs_attrib = block->block_attributes;
  462. if (sysfs_attrib && block->nr_attribs) {
  463. for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
  464. debugf4("%s() creating block attrib='%s' "
  465. "attrib->%p to kobj=%p\n",
  466. __func__,
  467. sysfs_attrib->attr.name,
  468. sysfs_attrib, &block->kobj);
  469. /* Create each block_attribute file */
  470. err = sysfs_create_file(&block->kobj,
  471. &sysfs_attrib->attr);
  472. if (err)
  473. goto err_on_attrib;
  474. }
  475. }
  476. kobject_uevent(&block->kobj, KOBJ_ADD);
  477. return 0;
  478. /* Error unwind stack */
  479. err_on_attrib:
  480. kobject_put(&block->kobj);
  481. err_out:
  482. return err;
  483. }
  484. /*
  485. * edac_device_delete_block(edac_dev,block);
  486. */
  487. static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev,
  488. struct edac_device_block *block)
  489. {
  490. struct edac_dev_sysfs_block_attribute *sysfs_attrib;
  491. int i;
  492. /* if this block has 'attributes' then we need to iterate over the list
  493. * and 'remove' the attributes on this block
  494. */
  495. sysfs_attrib = block->block_attributes;
  496. if (sysfs_attrib && block->nr_attribs) {
  497. for (i = 0; i < block->nr_attribs; i++, sysfs_attrib++) {
  498. /* remove each block_attrib file */
  499. sysfs_remove_file(&block->kobj,
  500. (struct attribute *) sysfs_attrib);
  501. }
  502. }
  503. /* unregister this block's kobject, SEE:
  504. * edac_device_ctrl_block_release() callback operation
  505. */
  506. kobject_put(&block->kobj);
  507. }
  508. /* instance ctor/dtor code */
  509. /*
  510. * edac_device_create_instance
  511. * create just one instance of an edac_device 'instance'
  512. */
  513. static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
  514. int idx)
  515. {
  516. int i, j;
  517. int err;
  518. struct edac_device_instance *instance;
  519. struct kobject *main_kobj;
  520. instance = &edac_dev->instances[idx];
  521. /* Init the instance's kobject */
  522. memset(&instance->kobj, 0, sizeof(struct kobject));
  523. instance->ctl = edac_dev;
  524. /* bump the main kobject's reference count for this controller
  525. * and this instance is dependant on the main
  526. */
  527. main_kobj = kobject_get(&edac_dev->kobj);
  528. if (!main_kobj) {
  529. err = -ENODEV;
  530. goto err_out;
  531. }
  532. /* Formally register this instance's kobject under the edac_device */
  533. err = kobject_init_and_add(&instance->kobj, &ktype_instance_ctrl,
  534. &edac_dev->kobj, "%s", instance->name);
  535. if (err != 0) {
  536. debugf2("%s() Failed to register instance '%s'\n",
  537. __func__, instance->name);
  538. kobject_put(main_kobj);
  539. goto err_out;
  540. }
  541. debugf4("%s() now register '%d' blocks for instance %d\n",
  542. __func__, instance->nr_blocks, idx);
  543. /* register all blocks of this instance */
  544. for (i = 0; i < instance->nr_blocks; i++) {
  545. err = edac_device_create_block(edac_dev, instance,
  546. &instance->blocks[i]);
  547. if (err) {
  548. /* If any fail, remove all previous ones */
  549. for (j = 0; j < i; j++)
  550. edac_device_delete_block(edac_dev,
  551. &instance->blocks[j]);
  552. goto err_release_instance_kobj;
  553. }
  554. }
  555. kobject_uevent(&instance->kobj, KOBJ_ADD);
  556. debugf4("%s() Registered instance %d '%s' kobject\n",
  557. __func__, idx, instance->name);
  558. return 0;
  559. /* error unwind stack */
  560. err_release_instance_kobj:
  561. kobject_put(&instance->kobj);
  562. err_out:
  563. return err;
  564. }
  565. /*
  566. * edac_device_remove_instance
  567. * remove an edac_device instance
  568. */
  569. static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev,
  570. int idx)
  571. {
  572. struct edac_device_instance *instance;
  573. int i;
  574. instance = &edac_dev->instances[idx];
  575. /* unregister all blocks in this instance */
  576. for (i = 0; i < instance->nr_blocks; i++)
  577. edac_device_delete_block(edac_dev, &instance->blocks[i]);
  578. /* unregister this instance's kobject, SEE:
  579. * edac_device_ctrl_instance_release() for callback operation
  580. */
  581. kobject_put(&instance->kobj);
  582. }
  583. /*
  584. * edac_device_create_instances
  585. * create the first level of 'instances' for this device
  586. * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc
  587. */
  588. static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev)
  589. {
  590. int i, j;
  591. int err;
  592. debugf0("%s()\n", __func__);
  593. /* iterate over creation of the instances */
  594. for (i = 0; i < edac_dev->nr_instances; i++) {
  595. err = edac_device_create_instance(edac_dev, i);
  596. if (err) {
  597. /* unwind previous instances on error */
  598. for (j = 0; j < i; j++)
  599. edac_device_delete_instance(edac_dev, j);
  600. return err;
  601. }
  602. }
  603. return 0;
  604. }
  605. /*
  606. * edac_device_delete_instances(edac_dev);
  607. * unregister all the kobjects of the instances
  608. */
  609. static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev)
  610. {
  611. int i;
  612. /* iterate over creation of the instances */
  613. for (i = 0; i < edac_dev->nr_instances; i++)
  614. edac_device_delete_instance(edac_dev, i);
  615. }
  616. /* edac_dev sysfs ctor/dtor code */
  617. /*
  618. * edac_device_add_main_sysfs_attributes
  619. * add some attributes to this instance's main kobject
  620. */
  621. static int edac_device_add_main_sysfs_attributes(
  622. struct edac_device_ctl_info *edac_dev)
  623. {
  624. struct edac_dev_sysfs_attribute *sysfs_attrib;
  625. int err = 0;
  626. sysfs_attrib = edac_dev->sysfs_attributes;
  627. if (sysfs_attrib) {
  628. /* iterate over the array and create an attribute for each
  629. * entry in the list
  630. */
  631. while (sysfs_attrib->attr.name != NULL) {
  632. err = sysfs_create_file(&edac_dev->kobj,
  633. (struct attribute*) sysfs_attrib);
  634. if (err)
  635. goto err_out;
  636. sysfs_attrib++;
  637. }
  638. }
  639. err_out:
  640. return err;
  641. }
  642. /*
  643. * edac_device_remove_main_sysfs_attributes
  644. * remove any attributes to this instance's main kobject
  645. */
  646. static void edac_device_remove_main_sysfs_attributes(
  647. struct edac_device_ctl_info *edac_dev)
  648. {
  649. struct edac_dev_sysfs_attribute *sysfs_attrib;
  650. /* if there are main attributes, defined, remove them. First,
  651. * point to the start of the array and iterate over it
  652. * removing each attribute listed from this device's instance's kobject
  653. */
  654. sysfs_attrib = edac_dev->sysfs_attributes;
  655. if (sysfs_attrib) {
  656. while (sysfs_attrib->attr.name != NULL) {
  657. sysfs_remove_file(&edac_dev->kobj,
  658. (struct attribute *) sysfs_attrib);
  659. sysfs_attrib++;
  660. }
  661. }
  662. }
  663. /*
  664. * edac_device_create_sysfs() Constructor
  665. *
  666. * accept a created edac_device control structure
  667. * and 'export' it to sysfs. The 'main' kobj should already have been
  668. * created. 'instance' and 'block' kobjects should be registered
  669. * along with any 'block' attributes from the low driver. In addition,
  670. * the main attributes (if any) are connected to the main kobject of
  671. * the control structure.
  672. *
  673. * Return:
  674. * 0 Success
  675. * !0 Failure
  676. */
  677. int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev)
  678. {
  679. int err;
  680. struct kobject *edac_kobj = &edac_dev->kobj;
  681. debugf0("%s() idx=%d\n", __func__, edac_dev->dev_idx);
  682. /* go create any main attributes callers wants */
  683. err = edac_device_add_main_sysfs_attributes(edac_dev);
  684. if (err) {
  685. debugf0("%s() failed to add sysfs attribs\n", __func__);
  686. goto err_out;
  687. }
  688. /* create a symlink from the edac device
  689. * to the platform 'device' being used for this
  690. */
  691. err = sysfs_create_link(edac_kobj,
  692. &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK);
  693. if (err) {
  694. debugf0("%s() sysfs_create_link() returned err= %d\n",
  695. __func__, err);
  696. goto err_remove_main_attribs;
  697. }
  698. /* Create the first level instance directories
  699. * In turn, the nested blocks beneath the instances will
  700. * be registered as well
  701. */
  702. err = edac_device_create_instances(edac_dev);
  703. if (err) {
  704. debugf0("%s() edac_device_create_instances() "
  705. "returned err= %d\n", __func__, err);
  706. goto err_remove_link;
  707. }
  708. debugf4("%s() create-instances done, idx=%d\n",
  709. __func__, edac_dev->dev_idx);
  710. return 0;
  711. /* Error unwind stack */
  712. err_remove_link:
  713. /* remove the sym link */
  714. sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
  715. err_remove_main_attribs:
  716. edac_device_remove_main_sysfs_attributes(edac_dev);
  717. err_out:
  718. return err;
  719. }
  720. /*
  721. * edac_device_remove_sysfs() destructor
  722. *
  723. * given an edac_device struct, tear down the kobject resources
  724. */
  725. void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev)
  726. {
  727. debugf0("%s()\n", __func__);
  728. /* remove any main attributes for this device */
  729. edac_device_remove_main_sysfs_attributes(edac_dev);
  730. /* remove the device sym link */
  731. sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK);
  732. /* walk the instance/block kobject tree, deconstructing it */
  733. edac_device_delete_instances(edac_dev);
  734. }