edac_device.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * edac_device.c
  3. * (C) 2007 www.douglaskthompson.com
  4. *
  5. * This file may be distributed under the terms of the
  6. * GNU General Public License.
  7. *
  8. * Written by Doug Thompson <norsk5@xmission.com>
  9. *
  10. * edac_device API implementation
  11. * 19 Jan 2007
  12. */
  13. #include <asm/page.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/ctype.h>
  16. #include <linux/highmem.h>
  17. #include <linux/init.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/smp.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/sysctl.h>
  24. #include <linux/timer.h>
  25. #include "edac_device.h"
  26. #include "edac_module.h"
  27. /* lock for the list: 'edac_device_list', manipulation of this list
  28. * is protected by the 'device_ctls_mutex' lock
  29. */
  30. static DEFINE_MUTEX(device_ctls_mutex);
  31. static LIST_HEAD(edac_device_list);
  32. /* Default workqueue processing interval on this instance, in msecs */
  33. #define DEFAULT_POLL_INTERVAL 1000
  34. #ifdef CONFIG_EDAC_DEBUG
  35. static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
  36. {
  37. edac_dbg(3, "\tedac_dev = %p dev_idx=%d\n",
  38. edac_dev, edac_dev->dev_idx);
  39. edac_dbg(4, "\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
  40. edac_dbg(3, "\tdev = %p\n", edac_dev->dev);
  41. edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n",
  42. edac_dev->mod_name, edac_dev->ctl_name);
  43. edac_dbg(3, "\tpvt_info = %p\n\n", edac_dev->pvt_info);
  44. }
  45. #endif /* CONFIG_EDAC_DEBUG */
  46. /*
  47. * @off_val: zero, 1, or other based offset
  48. */
  49. struct edac_device_ctl_info *
  50. edac_device_alloc_ctl_info(unsigned pvt_sz, char *dev_name, unsigned nr_instances,
  51. char *blk_name, unsigned nr_blocks, unsigned off_val,
  52. int device_index)
  53. {
  54. struct edac_device_block *dev_blk, *blk_p, *blk;
  55. struct edac_device_instance *dev_inst, *inst;
  56. struct edac_device_ctl_info *dev_ctl;
  57. unsigned instance, block;
  58. void *pvt;
  59. int err;
  60. edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks);
  61. dev_ctl = kzalloc_obj(struct edac_device_ctl_info);
  62. if (!dev_ctl)
  63. return NULL;
  64. dev_inst = kzalloc_objs(struct edac_device_instance, nr_instances);
  65. if (!dev_inst)
  66. goto free;
  67. dev_ctl->instances = dev_inst;
  68. dev_blk = kzalloc_objs(struct edac_device_block,
  69. nr_instances * nr_blocks);
  70. if (!dev_blk)
  71. goto free;
  72. dev_ctl->blocks = dev_blk;
  73. if (pvt_sz) {
  74. pvt = kzalloc(pvt_sz, GFP_KERNEL);
  75. if (!pvt)
  76. goto free;
  77. dev_ctl->pvt_info = pvt;
  78. }
  79. dev_ctl->dev_idx = device_index;
  80. dev_ctl->nr_instances = nr_instances;
  81. /* Default logging of CEs and UEs */
  82. dev_ctl->log_ce = 1;
  83. dev_ctl->log_ue = 1;
  84. /* Name of this edac device */
  85. snprintf(dev_ctl->name, sizeof(dev_ctl->name),"%s", dev_name);
  86. /* Initialize every Instance */
  87. for (instance = 0; instance < nr_instances; instance++) {
  88. inst = &dev_inst[instance];
  89. inst->ctl = dev_ctl;
  90. inst->nr_blocks = nr_blocks;
  91. blk_p = &dev_blk[instance * nr_blocks];
  92. inst->blocks = blk_p;
  93. /* name of this instance */
  94. snprintf(inst->name, sizeof(inst->name), "%s%u", dev_name, instance);
  95. /* Initialize every block in each instance */
  96. for (block = 0; block < nr_blocks; block++) {
  97. blk = &blk_p[block];
  98. blk->instance = inst;
  99. snprintf(blk->name, sizeof(blk->name),
  100. "%s%d", blk_name, block + off_val);
  101. edac_dbg(4, "instance=%d inst_p=%p block=#%d block_p=%p name='%s'\n",
  102. instance, inst, block, blk, blk->name);
  103. }
  104. }
  105. /* Mark this instance as merely ALLOCATED */
  106. dev_ctl->op_state = OP_ALLOC;
  107. /*
  108. * Initialize the 'root' kobj for the edac_device controller
  109. */
  110. err = edac_device_register_sysfs_main_kobj(dev_ctl);
  111. if (err)
  112. goto free;
  113. /* at this point, the root kobj is valid, and in order to
  114. * 'free' the object, then the function:
  115. * edac_device_unregister_sysfs_main_kobj() must be called
  116. * which will perform kobj unregistration and the actual free
  117. * will occur during the kobject callback operation
  118. */
  119. return dev_ctl;
  120. free:
  121. __edac_device_free_ctl_info(dev_ctl);
  122. return NULL;
  123. }
  124. EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
  125. void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
  126. {
  127. edac_device_unregister_sysfs_main_kobj(ctl_info);
  128. }
  129. EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
  130. /*
  131. * find_edac_device_by_dev
  132. * scans the edac_device list for a specific 'struct device *'
  133. *
  134. * lock to be held prior to call: device_ctls_mutex
  135. *
  136. * Return:
  137. * pointer to control structure managing 'dev'
  138. * NULL if not found on list
  139. */
  140. static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
  141. {
  142. struct edac_device_ctl_info *edac_dev;
  143. struct list_head *item;
  144. edac_dbg(0, "\n");
  145. list_for_each(item, &edac_device_list) {
  146. edac_dev = list_entry(item, struct edac_device_ctl_info, link);
  147. if (edac_dev->dev == dev)
  148. return edac_dev;
  149. }
  150. return NULL;
  151. }
  152. /*
  153. * add_edac_dev_to_global_list
  154. * Before calling this function, caller must
  155. * assign a unique value to edac_dev->dev_idx.
  156. *
  157. * lock to be held prior to call: device_ctls_mutex
  158. *
  159. * Return:
  160. * 0 on success
  161. * 1 on failure.
  162. */
  163. static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
  164. {
  165. struct list_head *item, *insert_before;
  166. struct edac_device_ctl_info *rover;
  167. insert_before = &edac_device_list;
  168. /* Determine if already on the list */
  169. rover = find_edac_device_by_dev(edac_dev->dev);
  170. if (unlikely(rover != NULL))
  171. goto fail0;
  172. /* Insert in ascending order by 'dev_idx', so find position */
  173. list_for_each(item, &edac_device_list) {
  174. rover = list_entry(item, struct edac_device_ctl_info, link);
  175. if (rover->dev_idx >= edac_dev->dev_idx) {
  176. if (unlikely(rover->dev_idx == edac_dev->dev_idx))
  177. goto fail1;
  178. insert_before = item;
  179. break;
  180. }
  181. }
  182. list_add_tail_rcu(&edac_dev->link, insert_before);
  183. return 0;
  184. fail0:
  185. edac_printk(KERN_WARNING, EDAC_MC,
  186. "%s (%s) %s %s already assigned %d\n",
  187. dev_name(rover->dev), edac_dev_name(rover),
  188. rover->mod_name, rover->ctl_name, rover->dev_idx);
  189. return 1;
  190. fail1:
  191. edac_printk(KERN_WARNING, EDAC_MC,
  192. "bug in low-level driver: attempt to assign\n"
  193. " duplicate dev_idx %d in %s()\n", rover->dev_idx,
  194. __func__);
  195. return 1;
  196. }
  197. /*
  198. * del_edac_device_from_global_list
  199. */
  200. static void del_edac_device_from_global_list(struct edac_device_ctl_info
  201. *edac_device)
  202. {
  203. list_del_rcu(&edac_device->link);
  204. /* these are for safe removal of devices from global list while
  205. * NMI handlers may be traversing list
  206. */
  207. synchronize_rcu();
  208. INIT_LIST_HEAD(&edac_device->link);
  209. }
  210. /*
  211. * edac_device_workq_function
  212. * performs the operation scheduled by a workq request
  213. *
  214. * this workq is embedded within an edac_device_ctl_info
  215. * structure, that needs to be polled for possible error events.
  216. *
  217. * This operation is to acquire the list mutex lock
  218. * (thus preventing insertation or deletion)
  219. * and then call the device's poll function IFF this device is
  220. * running polled and there is a poll function defined.
  221. */
  222. static void edac_device_workq_function(struct work_struct *work_req)
  223. {
  224. struct delayed_work *d_work = to_delayed_work(work_req);
  225. struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
  226. mutex_lock(&device_ctls_mutex);
  227. /* If we are being removed, bail out immediately */
  228. if (edac_dev->op_state == OP_OFFLINE) {
  229. mutex_unlock(&device_ctls_mutex);
  230. return;
  231. }
  232. /* Only poll controllers that are running polled and have a check */
  233. if ((edac_dev->op_state == OP_RUNNING_POLL) &&
  234. (edac_dev->edac_check != NULL)) {
  235. edac_dev->edac_check(edac_dev);
  236. }
  237. mutex_unlock(&device_ctls_mutex);
  238. /* Reschedule the workq for the next time period to start again
  239. * if the number of msec is for 1 sec, then adjust to the next
  240. * whole one second to save timers firing all over the period
  241. * between integral seconds
  242. */
  243. if (edac_dev->poll_msec == DEFAULT_POLL_INTERVAL)
  244. edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
  245. else
  246. edac_queue_work(&edac_dev->work, edac_dev->delay);
  247. }
  248. /*
  249. * edac_device_workq_setup
  250. * initialize a workq item for this edac_device instance
  251. * passing in the new delay period in msec
  252. */
  253. static void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
  254. unsigned msec)
  255. {
  256. edac_dbg(0, "\n");
  257. /* take the arg 'msec' and set it into the control structure
  258. * to used in the time period calculation
  259. * then calc the number of jiffies that represents
  260. */
  261. edac_dev->poll_msec = msec;
  262. edac_dev->delay = msecs_to_jiffies(msec);
  263. INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
  264. /* optimize here for the 1 second case, which will be normal value, to
  265. * fire ON the 1 second time event. This helps reduce all sorts of
  266. * timers firing on sub-second basis, while they are happy
  267. * to fire together on the 1 second exactly
  268. */
  269. if (edac_dev->poll_msec == DEFAULT_POLL_INTERVAL)
  270. edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
  271. else
  272. edac_queue_work(&edac_dev->work, edac_dev->delay);
  273. }
  274. /*
  275. * edac_device_workq_teardown
  276. * stop the workq processing on this edac_dev
  277. */
  278. static void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
  279. {
  280. if (!edac_dev->edac_check)
  281. return;
  282. edac_dev->op_state = OP_OFFLINE;
  283. edac_stop_work(&edac_dev->work);
  284. }
  285. /*
  286. * edac_device_reset_delay_period
  287. *
  288. * need to stop any outstanding workq queued up at this time
  289. * because we will be resetting the sleep time.
  290. * Then restart the workq on the new delay
  291. */
  292. void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
  293. unsigned long msec)
  294. {
  295. edac_dev->poll_msec = msec;
  296. edac_dev->delay = msecs_to_jiffies(msec);
  297. /* See comment in edac_device_workq_setup() above */
  298. if (edac_dev->poll_msec == DEFAULT_POLL_INTERVAL)
  299. edac_mod_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
  300. else
  301. edac_mod_work(&edac_dev->work, edac_dev->delay);
  302. }
  303. int edac_device_alloc_index(void)
  304. {
  305. static atomic_t device_indexes = ATOMIC_INIT(0);
  306. return atomic_inc_return(&device_indexes) - 1;
  307. }
  308. EXPORT_SYMBOL_GPL(edac_device_alloc_index);
  309. int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
  310. {
  311. edac_dbg(0, "\n");
  312. #ifdef CONFIG_EDAC_DEBUG
  313. if (edac_debug_level >= 3)
  314. edac_device_dump_device(edac_dev);
  315. #endif
  316. mutex_lock(&device_ctls_mutex);
  317. if (add_edac_dev_to_global_list(edac_dev))
  318. goto fail0;
  319. /* set load time so that error rate can be tracked */
  320. edac_dev->start_time = jiffies;
  321. /* create this instance's sysfs entries */
  322. if (edac_device_create_sysfs(edac_dev)) {
  323. edac_device_printk(edac_dev, KERN_WARNING,
  324. "failed to create sysfs device\n");
  325. goto fail1;
  326. }
  327. /* If there IS a check routine, then we are running POLLED */
  328. if (edac_dev->edac_check != NULL) {
  329. /* This instance is NOW RUNNING */
  330. edac_dev->op_state = OP_RUNNING_POLL;
  331. edac_device_workq_setup(edac_dev, edac_dev->poll_msec ?: DEFAULT_POLL_INTERVAL);
  332. } else {
  333. edac_dev->op_state = OP_RUNNING_INTERRUPT;
  334. }
  335. /* Report action taken */
  336. edac_device_printk(edac_dev, KERN_INFO,
  337. "Giving out device to module %s controller %s: DEV %s (%s)\n",
  338. edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
  339. edac_op_state_to_string(edac_dev->op_state));
  340. mutex_unlock(&device_ctls_mutex);
  341. return 0;
  342. fail1:
  343. /* Some error, so remove the entry from the lsit */
  344. del_edac_device_from_global_list(edac_dev);
  345. fail0:
  346. mutex_unlock(&device_ctls_mutex);
  347. return 1;
  348. }
  349. EXPORT_SYMBOL_GPL(edac_device_add_device);
  350. struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
  351. {
  352. struct edac_device_ctl_info *edac_dev;
  353. edac_dbg(0, "\n");
  354. mutex_lock(&device_ctls_mutex);
  355. /* Find the structure on the list, if not there, then leave */
  356. edac_dev = find_edac_device_by_dev(dev);
  357. if (edac_dev == NULL) {
  358. mutex_unlock(&device_ctls_mutex);
  359. return NULL;
  360. }
  361. /* mark this instance as OFFLINE */
  362. edac_dev->op_state = OP_OFFLINE;
  363. /* deregister from global list */
  364. del_edac_device_from_global_list(edac_dev);
  365. mutex_unlock(&device_ctls_mutex);
  366. /* clear workq processing on this instance */
  367. edac_device_workq_teardown(edac_dev);
  368. /* Tear down the sysfs entries for this instance */
  369. edac_device_remove_sysfs(edac_dev);
  370. edac_printk(KERN_INFO, EDAC_MC,
  371. "Removed device %d for %s %s: DEV %s\n",
  372. edac_dev->dev_idx,
  373. edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
  374. return edac_dev;
  375. }
  376. EXPORT_SYMBOL_GPL(edac_device_del_device);
  377. static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
  378. {
  379. return edac_dev->log_ce;
  380. }
  381. static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
  382. {
  383. return edac_dev->log_ue;
  384. }
  385. static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
  386. *edac_dev)
  387. {
  388. return edac_dev->panic_on_ue;
  389. }
  390. void edac_device_handle_ce_count(struct edac_device_ctl_info *edac_dev,
  391. unsigned int count, int inst_nr, int block_nr,
  392. const char *msg)
  393. {
  394. struct edac_device_instance *instance;
  395. struct edac_device_block *block = NULL;
  396. if (!count)
  397. return;
  398. if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
  399. edac_device_printk(edac_dev, KERN_ERR,
  400. "INTERNAL ERROR: 'instance' out of range "
  401. "(%d >= %d)\n", inst_nr,
  402. edac_dev->nr_instances);
  403. return;
  404. }
  405. instance = edac_dev->instances + inst_nr;
  406. if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
  407. edac_device_printk(edac_dev, KERN_ERR,
  408. "INTERNAL ERROR: instance %d 'block' "
  409. "out of range (%d >= %d)\n",
  410. inst_nr, block_nr,
  411. instance->nr_blocks);
  412. return;
  413. }
  414. if (instance->nr_blocks > 0) {
  415. block = instance->blocks + block_nr;
  416. block->counters.ce_count += count;
  417. }
  418. /* Propagate the count up the 'totals' tree */
  419. instance->counters.ce_count += count;
  420. edac_dev->counters.ce_count += count;
  421. if (edac_device_get_log_ce(edac_dev))
  422. edac_device_printk(edac_dev, KERN_WARNING,
  423. "CE: %s instance: %s block: %s count: %d '%s'\n",
  424. edac_dev->ctl_name, instance->name,
  425. block ? block->name : "N/A", count, msg);
  426. }
  427. EXPORT_SYMBOL_GPL(edac_device_handle_ce_count);
  428. void edac_device_handle_ue_count(struct edac_device_ctl_info *edac_dev,
  429. unsigned int count, int inst_nr, int block_nr,
  430. const char *msg)
  431. {
  432. struct edac_device_instance *instance;
  433. struct edac_device_block *block = NULL;
  434. if (!count)
  435. return;
  436. if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
  437. edac_device_printk(edac_dev, KERN_ERR,
  438. "INTERNAL ERROR: 'instance' out of range "
  439. "(%d >= %d)\n", inst_nr,
  440. edac_dev->nr_instances);
  441. return;
  442. }
  443. instance = edac_dev->instances + inst_nr;
  444. if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
  445. edac_device_printk(edac_dev, KERN_ERR,
  446. "INTERNAL ERROR: instance %d 'block' "
  447. "out of range (%d >= %d)\n",
  448. inst_nr, block_nr,
  449. instance->nr_blocks);
  450. return;
  451. }
  452. if (instance->nr_blocks > 0) {
  453. block = instance->blocks + block_nr;
  454. block->counters.ue_count += count;
  455. }
  456. /* Propagate the count up the 'totals' tree */
  457. instance->counters.ue_count += count;
  458. edac_dev->counters.ue_count += count;
  459. if (edac_device_get_log_ue(edac_dev))
  460. edac_device_printk(edac_dev, KERN_EMERG,
  461. "UE: %s instance: %s block: %s count: %d '%s'\n",
  462. edac_dev->ctl_name, instance->name,
  463. block ? block->name : "N/A", count, msg);
  464. if (edac_device_get_panic_on_ue(edac_dev))
  465. panic("EDAC %s: UE instance: %s block %s count: %d '%s'\n",
  466. edac_dev->ctl_name, instance->name,
  467. block ? block->name : "N/A", count, msg);
  468. }
  469. EXPORT_SYMBOL_GPL(edac_device_handle_ue_count);
  470. static void edac_dev_release(struct device *dev)
  471. {
  472. struct edac_dev_feat_ctx *ctx = container_of(dev, struct edac_dev_feat_ctx, dev);
  473. kfree(ctx->mem_repair);
  474. kfree(ctx->scrub);
  475. kfree(ctx->dev.groups);
  476. kfree(ctx);
  477. }
  478. static const struct device_type edac_dev_type = {
  479. .name = "edac_dev",
  480. .release = edac_dev_release,
  481. };
  482. static void edac_dev_unreg(void *data)
  483. {
  484. device_unregister(data);
  485. }
  486. /**
  487. * edac_dev_register - register device for RAS features with EDAC
  488. * @parent: parent device.
  489. * @name: name for the folder in the /sys/bus/edac/devices/,
  490. * which is derived from the parent device.
  491. * For e.g. /sys/bus/edac/devices/cxl_mem0/
  492. * @private: parent driver's data to store in the context if any.
  493. * @num_features: number of RAS features to register.
  494. * @ras_features: list of RAS features to register.
  495. *
  496. * Return:
  497. * * %0 - Success.
  498. * * %-EINVAL - Invalid parameters passed.
  499. * * %-ENOMEM - Dynamic memory allocation failed.
  500. *
  501. */
  502. int edac_dev_register(struct device *parent, char *name,
  503. void *private, int num_features,
  504. const struct edac_dev_feature *ras_features)
  505. {
  506. const struct attribute_group **ras_attr_groups;
  507. struct edac_dev_data *dev_data;
  508. struct edac_dev_feat_ctx *ctx;
  509. int mem_repair_cnt = 0;
  510. int attr_gcnt = 0;
  511. int ret = -ENOMEM;
  512. int scrub_cnt = 0;
  513. int feat;
  514. if (!parent || !name || !num_features || !ras_features)
  515. return -EINVAL;
  516. /* Double parse to make space for attributes */
  517. for (feat = 0; feat < num_features; feat++) {
  518. switch (ras_features[feat].ft_type) {
  519. case RAS_FEAT_SCRUB:
  520. attr_gcnt++;
  521. scrub_cnt++;
  522. break;
  523. case RAS_FEAT_ECS:
  524. attr_gcnt += ras_features[feat].ecs_info.num_media_frus;
  525. break;
  526. case RAS_FEAT_MEM_REPAIR:
  527. attr_gcnt++;
  528. mem_repair_cnt++;
  529. break;
  530. default:
  531. return -EINVAL;
  532. }
  533. }
  534. ctx = kzalloc_obj(*ctx);
  535. if (!ctx)
  536. return -ENOMEM;
  537. ras_attr_groups = kzalloc_objs(*ras_attr_groups, attr_gcnt + 1);
  538. if (!ras_attr_groups)
  539. goto ctx_free;
  540. if (scrub_cnt) {
  541. ctx->scrub = kzalloc_objs(*ctx->scrub, scrub_cnt);
  542. if (!ctx->scrub)
  543. goto groups_free;
  544. }
  545. if (mem_repair_cnt) {
  546. ctx->mem_repair = kzalloc_objs(*ctx->mem_repair, mem_repair_cnt);
  547. if (!ctx->mem_repair)
  548. goto data_mem_free;
  549. }
  550. attr_gcnt = 0;
  551. scrub_cnt = 0;
  552. mem_repair_cnt = 0;
  553. for (feat = 0; feat < num_features; feat++, ras_features++) {
  554. switch (ras_features->ft_type) {
  555. case RAS_FEAT_SCRUB:
  556. if (!ras_features->scrub_ops || scrub_cnt != ras_features->instance) {
  557. ret = -EINVAL;
  558. goto data_mem_free;
  559. }
  560. dev_data = &ctx->scrub[scrub_cnt];
  561. dev_data->instance = scrub_cnt;
  562. dev_data->scrub_ops = ras_features->scrub_ops;
  563. dev_data->private = ras_features->ctx;
  564. ret = edac_scrub_get_desc(parent, &ras_attr_groups[attr_gcnt],
  565. ras_features->instance);
  566. if (ret)
  567. goto data_mem_free;
  568. scrub_cnt++;
  569. attr_gcnt++;
  570. break;
  571. case RAS_FEAT_ECS:
  572. if (!ras_features->ecs_ops) {
  573. ret = -EINVAL;
  574. goto data_mem_free;
  575. }
  576. dev_data = &ctx->ecs;
  577. dev_data->ecs_ops = ras_features->ecs_ops;
  578. dev_data->private = ras_features->ctx;
  579. ret = edac_ecs_get_desc(parent, &ras_attr_groups[attr_gcnt],
  580. ras_features->ecs_info.num_media_frus);
  581. if (ret)
  582. goto data_mem_free;
  583. attr_gcnt += ras_features->ecs_info.num_media_frus;
  584. break;
  585. case RAS_FEAT_MEM_REPAIR:
  586. if (!ras_features->mem_repair_ops ||
  587. mem_repair_cnt != ras_features->instance) {
  588. ret = -EINVAL;
  589. goto data_mem_free;
  590. }
  591. dev_data = &ctx->mem_repair[mem_repair_cnt];
  592. dev_data->instance = mem_repair_cnt;
  593. dev_data->mem_repair_ops = ras_features->mem_repair_ops;
  594. dev_data->private = ras_features->ctx;
  595. ret = edac_mem_repair_get_desc(parent, &ras_attr_groups[attr_gcnt],
  596. ras_features->instance);
  597. if (ret)
  598. goto data_mem_free;
  599. mem_repair_cnt++;
  600. attr_gcnt++;
  601. break;
  602. default:
  603. ret = -EINVAL;
  604. goto data_mem_free;
  605. }
  606. }
  607. ctx->dev.parent = parent;
  608. ctx->dev.bus = edac_get_sysfs_subsys();
  609. ctx->dev.type = &edac_dev_type;
  610. ctx->dev.groups = ras_attr_groups;
  611. ctx->private = private;
  612. dev_set_drvdata(&ctx->dev, ctx);
  613. ret = dev_set_name(&ctx->dev, "%s", name);
  614. if (ret)
  615. goto data_mem_free;
  616. ret = device_register(&ctx->dev);
  617. if (ret) {
  618. put_device(&ctx->dev);
  619. return ret;
  620. }
  621. return devm_add_action_or_reset(parent, edac_dev_unreg, &ctx->dev);
  622. data_mem_free:
  623. kfree(ctx->mem_repair);
  624. kfree(ctx->scrub);
  625. groups_free:
  626. kfree(ras_attr_groups);
  627. ctx_free:
  628. kfree(ctx);
  629. return ret;
  630. }
  631. EXPORT_SYMBOL_GPL(edac_dev_register);