edac_device_sysfs.c 24 KB

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