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