edac_device_sysfs.c 23 KB

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