xref: /openbmc/linux/drivers/scsi/raid_class.c (revision 8e4a0cf7)
161a7afa2SJames Bottomley /*
2b1081ea6SJames Bottomley  * raid_class.c - implementation of a simple raid visualisation class
3b1081ea6SJames Bottomley  *
4b1081ea6SJames Bottomley  * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
5b1081ea6SJames Bottomley  *
6b1081ea6SJames Bottomley  * This file is licensed under GPLv2
7b1081ea6SJames Bottomley  *
8b1081ea6SJames Bottomley  * This class is designed to allow raid attributes to be visualised and
9b1081ea6SJames Bottomley  * manipulated in a form independent of the underlying raid.  Ultimately this
10b1081ea6SJames Bottomley  * should work for both hardware and software raids.
1161a7afa2SJames Bottomley  */
1261a7afa2SJames Bottomley #include <linux/init.h>
1361a7afa2SJames Bottomley #include <linux/module.h>
1461a7afa2SJames Bottomley #include <linux/list.h>
158c65b4a6STim Schmielau #include <linux/slab.h>
168c65b4a6STim Schmielau #include <linux/string.h>
1761a7afa2SJames Bottomley #include <linux/raid_class.h>
1861a7afa2SJames Bottomley #include <scsi/scsi_device.h>
1961a7afa2SJames Bottomley #include <scsi/scsi_host.h>
2061a7afa2SJames Bottomley 
2161a7afa2SJames Bottomley #define RAID_NUM_ATTRS	3
2261a7afa2SJames Bottomley 
2361a7afa2SJames Bottomley struct raid_internal {
2461a7afa2SJames Bottomley 	struct raid_template r;
2561a7afa2SJames Bottomley 	struct raid_function_template *f;
2661a7afa2SJames Bottomley 	/* The actual attributes */
27ee959b00STony Jones 	struct device_attribute private_attrs[RAID_NUM_ATTRS];
2861a7afa2SJames Bottomley 	/* The array of null terminated pointers to attributes
2961a7afa2SJames Bottomley 	 * needed by scsi_sysfs.c */
30ee959b00STony Jones 	struct device_attribute *attrs[RAID_NUM_ATTRS + 1];
3161a7afa2SJames Bottomley };
3261a7afa2SJames Bottomley 
3361a7afa2SJames Bottomley struct raid_component {
3461a7afa2SJames Bottomley 	struct list_head node;
35ee959b00STony Jones 	struct device dev;
3661a7afa2SJames Bottomley 	int num;
3761a7afa2SJames Bottomley };
3861a7afa2SJames Bottomley 
3961a7afa2SJames Bottomley #define to_raid_internal(tmpl)	container_of(tmpl, struct raid_internal, r)
4061a7afa2SJames Bottomley 
4161a7afa2SJames Bottomley #define tc_to_raid_internal(tcont) ({					\
4261a7afa2SJames Bottomley 	struct raid_template *r =					\
4361a7afa2SJames Bottomley 		container_of(tcont, struct raid_template, raid_attrs);	\
4461a7afa2SJames Bottomley 	to_raid_internal(r);						\
4561a7afa2SJames Bottomley })
4661a7afa2SJames Bottomley 
4761a7afa2SJames Bottomley #define ac_to_raid_internal(acont) ({					\
4861a7afa2SJames Bottomley 	struct transport_container *tc =				\
4961a7afa2SJames Bottomley 		container_of(acont, struct transport_container, ac);	\
5061a7afa2SJames Bottomley 	tc_to_raid_internal(tc);					\
5161a7afa2SJames Bottomley })
5261a7afa2SJames Bottomley 
53ee959b00STony Jones #define device_to_raid_internal(dev) ({				\
5461a7afa2SJames Bottomley 	struct attribute_container *ac =				\
55ee959b00STony Jones 		attribute_container_classdev_to_container(dev);	\
5661a7afa2SJames Bottomley 	ac_to_raid_internal(ac);					\
5761a7afa2SJames Bottomley })
5861a7afa2SJames Bottomley 
5961a7afa2SJames Bottomley 
6061a7afa2SJames Bottomley static int raid_match(struct attribute_container *cont, struct device *dev)
6161a7afa2SJames Bottomley {
6261a7afa2SJames Bottomley 	/* We have to look for every subsystem that could house
6361a7afa2SJames Bottomley 	 * emulated RAID devices, so start with SCSI */
6461a7afa2SJames Bottomley 	struct raid_internal *i = ac_to_raid_internal(cont);
6561a7afa2SJames Bottomley 
6661a7afa2SJames Bottomley 	if (scsi_is_sdev_device(dev)) {
6761a7afa2SJames Bottomley 		struct scsi_device *sdev = to_scsi_device(dev);
6861a7afa2SJames Bottomley 
6961a7afa2SJames Bottomley 		if (i->f->cookie != sdev->host->hostt)
7061a7afa2SJames Bottomley 			return 0;
7161a7afa2SJames Bottomley 
7261a7afa2SJames Bottomley 		return i->f->is_raid(dev);
7361a7afa2SJames Bottomley 	}
7461a7afa2SJames Bottomley 	/* FIXME: look at other subsystems too */
7561a7afa2SJames Bottomley 	return 0;
7661a7afa2SJames Bottomley }
7761a7afa2SJames Bottomley 
7861a7afa2SJames Bottomley static int raid_setup(struct transport_container *tc, struct device *dev,
79ee959b00STony Jones 		       struct device *cdev)
8061a7afa2SJames Bottomley {
8161a7afa2SJames Bottomley 	struct raid_data *rd;
8261a7afa2SJames Bottomley 
83ee959b00STony Jones 	BUG_ON(dev_get_drvdata(cdev));
8461a7afa2SJames Bottomley 
85b1081ea6SJames Bottomley 	rd = kzalloc(sizeof(*rd), GFP_KERNEL);
8661a7afa2SJames Bottomley 	if (!rd)
8761a7afa2SJames Bottomley 		return -ENOMEM;
8861a7afa2SJames Bottomley 
8961a7afa2SJames Bottomley 	INIT_LIST_HEAD(&rd->component_list);
90ee959b00STony Jones 	dev_set_drvdata(cdev, rd);
9161a7afa2SJames Bottomley 
9261a7afa2SJames Bottomley 	return 0;
9361a7afa2SJames Bottomley }
9461a7afa2SJames Bottomley 
9561a7afa2SJames Bottomley static int raid_remove(struct transport_container *tc, struct device *dev,
96ee959b00STony Jones 		       struct device *cdev)
9761a7afa2SJames Bottomley {
98ee959b00STony Jones 	struct raid_data *rd = dev_get_drvdata(cdev);
9961a7afa2SJames Bottomley 	struct raid_component *rc, *next;
100b1081ea6SJames Bottomley 	dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
101ee959b00STony Jones 	dev_set_drvdata(cdev, NULL);
10261a7afa2SJames Bottomley 	list_for_each_entry_safe(rc, next, &rd->component_list, node) {
10361a7afa2SJames Bottomley 		list_del(&rc->node);
104ee959b00STony Jones 		dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n");
105ee959b00STony Jones 		device_unregister(&rc->dev);
10661a7afa2SJames Bottomley 	}
107b1081ea6SJames Bottomley 	dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
108b1081ea6SJames Bottomley 	kfree(rd);
10961a7afa2SJames Bottomley 	return 0;
11061a7afa2SJames Bottomley }
11161a7afa2SJames Bottomley 
11261a7afa2SJames Bottomley static DECLARE_TRANSPORT_CLASS(raid_class,
11361a7afa2SJames Bottomley 			       "raid_devices",
11461a7afa2SJames Bottomley 			       raid_setup,
11561a7afa2SJames Bottomley 			       raid_remove,
11661a7afa2SJames Bottomley 			       NULL);
11761a7afa2SJames Bottomley 
1180ad78200SArjan van de Ven static const struct {
11961a7afa2SJames Bottomley 	enum raid_state	value;
12061a7afa2SJames Bottomley 	char		*name;
12161a7afa2SJames Bottomley } raid_states[] = {
122b1081ea6SJames Bottomley 	{ RAID_STATE_UNKNOWN, "unknown" },
123b1081ea6SJames Bottomley 	{ RAID_STATE_ACTIVE, "active" },
124b1081ea6SJames Bottomley 	{ RAID_STATE_DEGRADED, "degraded" },
125b1081ea6SJames Bottomley 	{ RAID_STATE_RESYNCING, "resyncing" },
126b1081ea6SJames Bottomley 	{ RAID_STATE_OFFLINE, "offline" },
12761a7afa2SJames Bottomley };
12861a7afa2SJames Bottomley 
12961a7afa2SJames Bottomley static const char *raid_state_name(enum raid_state state)
13061a7afa2SJames Bottomley {
13161a7afa2SJames Bottomley 	int i;
13261a7afa2SJames Bottomley 	char *name = NULL;
13361a7afa2SJames Bottomley 
1346391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(raid_states); i++) {
13561a7afa2SJames Bottomley 		if (raid_states[i].value == state) {
13661a7afa2SJames Bottomley 			name = raid_states[i].name;
13761a7afa2SJames Bottomley 			break;
13861a7afa2SJames Bottomley 		}
13961a7afa2SJames Bottomley 	}
14061a7afa2SJames Bottomley 	return name;
14161a7afa2SJames Bottomley }
14261a7afa2SJames Bottomley 
143b1081ea6SJames Bottomley static struct {
144b1081ea6SJames Bottomley 	enum raid_level value;
145b1081ea6SJames Bottomley 	char *name;
146b1081ea6SJames Bottomley } raid_levels[] = {
147b1081ea6SJames Bottomley 	{ RAID_LEVEL_UNKNOWN, "unknown" },
148b1081ea6SJames Bottomley 	{ RAID_LEVEL_LINEAR, "linear" },
149b1081ea6SJames Bottomley 	{ RAID_LEVEL_0, "raid0" },
150b1081ea6SJames Bottomley 	{ RAID_LEVEL_1, "raid1" },
1518e32ca49SMoore, Eric 	{ RAID_LEVEL_10, "raid10" },
1528e4a0cf7SKashyap, Desai 	{ RAID_LEVEL_1E, "raid1e" },
153b1081ea6SJames Bottomley 	{ RAID_LEVEL_3, "raid3" },
154b1081ea6SJames Bottomley 	{ RAID_LEVEL_4, "raid4" },
155b1081ea6SJames Bottomley 	{ RAID_LEVEL_5, "raid5" },
1568e32ca49SMoore, Eric 	{ RAID_LEVEL_50, "raid50" },
157b1081ea6SJames Bottomley 	{ RAID_LEVEL_6, "raid6" },
158b1081ea6SJames Bottomley };
159b1081ea6SJames Bottomley 
160b1081ea6SJames Bottomley static const char *raid_level_name(enum raid_level level)
161b1081ea6SJames Bottomley {
162b1081ea6SJames Bottomley 	int i;
163b1081ea6SJames Bottomley 	char *name = NULL;
164b1081ea6SJames Bottomley 
1656391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(raid_levels); i++) {
166b1081ea6SJames Bottomley 		if (raid_levels[i].value == level) {
167b1081ea6SJames Bottomley 			name = raid_levels[i].name;
168b1081ea6SJames Bottomley 			break;
169b1081ea6SJames Bottomley 		}
170b1081ea6SJames Bottomley 	}
171b1081ea6SJames Bottomley 	return name;
172b1081ea6SJames Bottomley }
17361a7afa2SJames Bottomley 
17461a7afa2SJames Bottomley #define raid_attr_show_internal(attr, fmt, var, code)			\
175ee959b00STony Jones static ssize_t raid_show_##attr(struct device *dev, 			\
176ee959b00STony Jones 				struct device_attribute *attr, 		\
177ee959b00STony Jones 				char *buf)				\
17861a7afa2SJames Bottomley {									\
179ee959b00STony Jones 	struct raid_data *rd = dev_get_drvdata(dev);			\
18061a7afa2SJames Bottomley 	code								\
18161a7afa2SJames Bottomley 	return snprintf(buf, 20, #fmt "\n", var);			\
18261a7afa2SJames Bottomley }
18361a7afa2SJames Bottomley 
18461a7afa2SJames Bottomley #define raid_attr_ro_states(attr, states, code)				\
18561a7afa2SJames Bottomley raid_attr_show_internal(attr, %s, name,					\
18661a7afa2SJames Bottomley 	const char *name;						\
18761a7afa2SJames Bottomley 	code								\
18861a7afa2SJames Bottomley 	name = raid_##states##_name(rd->attr);				\
18961a7afa2SJames Bottomley )									\
190ee959b00STony Jones static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
19161a7afa2SJames Bottomley 
19261a7afa2SJames Bottomley 
19361a7afa2SJames Bottomley #define raid_attr_ro_internal(attr, code)				\
19461a7afa2SJames Bottomley raid_attr_show_internal(attr, %d, rd->attr, code)			\
195ee959b00STony Jones static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
19661a7afa2SJames Bottomley 
19761a7afa2SJames Bottomley #define ATTR_CODE(attr)							\
198ee959b00STony Jones 	struct raid_internal *i = device_to_raid_internal(dev);		\
19961a7afa2SJames Bottomley 	if (i->f->get_##attr)						\
200ee959b00STony Jones 		i->f->get_##attr(dev->parent);
20161a7afa2SJames Bottomley 
20261a7afa2SJames Bottomley #define raid_attr_ro(attr)	raid_attr_ro_internal(attr, )
20361a7afa2SJames Bottomley #define raid_attr_ro_fn(attr)	raid_attr_ro_internal(attr, ATTR_CODE(attr))
204b1081ea6SJames Bottomley #define raid_attr_ro_state(attr)	raid_attr_ro_states(attr, attr, )
205b1081ea6SJames Bottomley #define raid_attr_ro_state_fn(attr)	raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
20661a7afa2SJames Bottomley 
207b1081ea6SJames Bottomley 
208b1081ea6SJames Bottomley raid_attr_ro_state(level);
20961a7afa2SJames Bottomley raid_attr_ro_fn(resync);
210b1081ea6SJames Bottomley raid_attr_ro_state_fn(state);
211b1081ea6SJames Bottomley 
212ee959b00STony Jones static void raid_component_release(struct device *dev)
213b1081ea6SJames Bottomley {
214ee959b00STony Jones 	struct raid_component *rc =
215ee959b00STony Jones 		container_of(dev, struct raid_component, dev);
216ee959b00STony Jones 	dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE\n");
217ee959b00STony Jones 	put_device(rc->dev.parent);
218b1081ea6SJames Bottomley 	kfree(rc);
219b1081ea6SJames Bottomley }
22061a7afa2SJames Bottomley 
221ed542bedSJeff Garzik int raid_component_add(struct raid_template *r,struct device *raid_dev,
22261a7afa2SJames Bottomley 		       struct device *component_dev)
22361a7afa2SJames Bottomley {
224ee959b00STony Jones 	struct device *cdev =
22561a7afa2SJames Bottomley 		attribute_container_find_class_device(&r->raid_attrs.ac,
22661a7afa2SJames Bottomley 						      raid_dev);
22761a7afa2SJames Bottomley 	struct raid_component *rc;
228ee959b00STony Jones 	struct raid_data *rd = dev_get_drvdata(cdev);
229ed542bedSJeff Garzik 	int err;
23061a7afa2SJames Bottomley 
231b1081ea6SJames Bottomley 	rc = kzalloc(sizeof(*rc), GFP_KERNEL);
23261a7afa2SJames Bottomley 	if (!rc)
233ed542bedSJeff Garzik 		return -ENOMEM;
23461a7afa2SJames Bottomley 
23561a7afa2SJames Bottomley 	INIT_LIST_HEAD(&rc->node);
236ee959b00STony Jones 	device_initialize(&rc->dev);
237ee959b00STony Jones 	rc->dev.release = raid_component_release;
238ee959b00STony Jones 	rc->dev.parent = get_device(component_dev);
23961a7afa2SJames Bottomley 	rc->num = rd->component_count++;
24061a7afa2SJames Bottomley 
24171610f55SKay Sievers 	dev_set_name(&rc->dev, "component-%d", rc->num);
24261a7afa2SJames Bottomley 	list_add_tail(&rc->node, &rd->component_list);
243ee959b00STony Jones 	rc->dev.class = &raid_class.class;
244ee959b00STony Jones 	err = device_add(&rc->dev);
245ed542bedSJeff Garzik 	if (err)
246ed542bedSJeff Garzik 		goto err_out;
247ed542bedSJeff Garzik 
248ed542bedSJeff Garzik 	return 0;
249ed542bedSJeff Garzik 
250ed542bedSJeff Garzik err_out:
251ed542bedSJeff Garzik 	list_del(&rc->node);
252ed542bedSJeff Garzik 	rd->component_count--;
253ed542bedSJeff Garzik 	put_device(component_dev);
254ed542bedSJeff Garzik 	kfree(rc);
255ed542bedSJeff Garzik 	return err;
25661a7afa2SJames Bottomley }
25761a7afa2SJames Bottomley EXPORT_SYMBOL(raid_component_add);
25861a7afa2SJames Bottomley 
25961a7afa2SJames Bottomley struct raid_template *
26061a7afa2SJames Bottomley raid_class_attach(struct raid_function_template *ft)
26161a7afa2SJames Bottomley {
262b1081ea6SJames Bottomley 	struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
26361a7afa2SJames Bottomley 					  GFP_KERNEL);
26461a7afa2SJames Bottomley 	int count = 0;
26561a7afa2SJames Bottomley 
26661a7afa2SJames Bottomley 	if (unlikely(!i))
26761a7afa2SJames Bottomley 		return NULL;
26861a7afa2SJames Bottomley 
26961a7afa2SJames Bottomley 	i->f = ft;
27061a7afa2SJames Bottomley 
27161a7afa2SJames Bottomley 	i->r.raid_attrs.ac.class = &raid_class.class;
27261a7afa2SJames Bottomley 	i->r.raid_attrs.ac.match = raid_match;
27361a7afa2SJames Bottomley 	i->r.raid_attrs.ac.attrs = &i->attrs[0];
27461a7afa2SJames Bottomley 
27561a7afa2SJames Bottomley 	attribute_container_register(&i->r.raid_attrs.ac);
27661a7afa2SJames Bottomley 
277ee959b00STony Jones 	i->attrs[count++] = &dev_attr_level;
278ee959b00STony Jones 	i->attrs[count++] = &dev_attr_resync;
279ee959b00STony Jones 	i->attrs[count++] = &dev_attr_state;
28061a7afa2SJames Bottomley 
28161a7afa2SJames Bottomley 	i->attrs[count] = NULL;
28261a7afa2SJames Bottomley 	BUG_ON(count > RAID_NUM_ATTRS);
28361a7afa2SJames Bottomley 
28461a7afa2SJames Bottomley 	return &i->r;
28561a7afa2SJames Bottomley }
28661a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_attach);
28761a7afa2SJames Bottomley 
28861a7afa2SJames Bottomley void
28961a7afa2SJames Bottomley raid_class_release(struct raid_template *r)
29061a7afa2SJames Bottomley {
29161a7afa2SJames Bottomley 	struct raid_internal *i = to_raid_internal(r);
29261a7afa2SJames Bottomley 
2932f3edc69SJames Bottomley 	BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac));
29461a7afa2SJames Bottomley 
29561a7afa2SJames Bottomley 	kfree(i);
29661a7afa2SJames Bottomley }
29761a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_release);
29861a7afa2SJames Bottomley 
29961a7afa2SJames Bottomley static __init int raid_init(void)
30061a7afa2SJames Bottomley {
30161a7afa2SJames Bottomley 	return transport_class_register(&raid_class);
30261a7afa2SJames Bottomley }
30361a7afa2SJames Bottomley 
30461a7afa2SJames Bottomley static __exit void raid_exit(void)
30561a7afa2SJames Bottomley {
30661a7afa2SJames Bottomley 	transport_class_unregister(&raid_class);
30761a7afa2SJames Bottomley }
30861a7afa2SJames Bottomley 
30961a7afa2SJames Bottomley MODULE_AUTHOR("James Bottomley");
31061a7afa2SJames Bottomley MODULE_DESCRIPTION("RAID device class");
31161a7afa2SJames Bottomley MODULE_LICENSE("GPL");
31261a7afa2SJames Bottomley 
31361a7afa2SJames Bottomley module_init(raid_init);
31461a7afa2SJames Bottomley module_exit(raid_exit);
31561a7afa2SJames Bottomley 
316