xref: /openbmc/linux/drivers/scsi/raid_class.c (revision b1081ea6)
1 /*
2  * raid_class.c - implementation of a simple raid visualisation class
3  *
4  * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
5  *
6  * This file is licensed under GPLv2
7  *
8  * This class is designed to allow raid attributes to be visualised and
9  * manipulated in a form independent of the underlying raid.  Ultimately this
10  * should work for both hardware and software raids.
11  */
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/raid_class.h>
16 #include <scsi/scsi_device.h>
17 #include <scsi/scsi_host.h>
18 
19 #define RAID_NUM_ATTRS	3
20 
21 struct raid_internal {
22 	struct raid_template r;
23 	struct raid_function_template *f;
24 	/* The actual attributes */
25 	struct class_device_attribute private_attrs[RAID_NUM_ATTRS];
26 	/* The array of null terminated pointers to attributes
27 	 * needed by scsi_sysfs.c */
28 	struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
29 };
30 
31 struct raid_component {
32 	struct list_head node;
33 	struct class_device cdev;
34 	int num;
35 };
36 
37 #define to_raid_internal(tmpl)	container_of(tmpl, struct raid_internal, r)
38 
39 #define tc_to_raid_internal(tcont) ({					\
40 	struct raid_template *r =					\
41 		container_of(tcont, struct raid_template, raid_attrs);	\
42 	to_raid_internal(r);						\
43 })
44 
45 #define ac_to_raid_internal(acont) ({					\
46 	struct transport_container *tc =				\
47 		container_of(acont, struct transport_container, ac);	\
48 	tc_to_raid_internal(tc);					\
49 })
50 
51 #define class_device_to_raid_internal(cdev) ({				\
52 	struct attribute_container *ac =				\
53 		attribute_container_classdev_to_container(cdev);	\
54 	ac_to_raid_internal(ac);					\
55 })
56 
57 
58 static int raid_match(struct attribute_container *cont, struct device *dev)
59 {
60 	/* We have to look for every subsystem that could house
61 	 * emulated RAID devices, so start with SCSI */
62 	struct raid_internal *i = ac_to_raid_internal(cont);
63 
64 	if (scsi_is_sdev_device(dev)) {
65 		struct scsi_device *sdev = to_scsi_device(dev);
66 
67 		if (i->f->cookie != sdev->host->hostt)
68 			return 0;
69 
70 		return i->f->is_raid(dev);
71 	}
72 	/* FIXME: look at other subsystems too */
73 	return 0;
74 }
75 
76 static int raid_setup(struct transport_container *tc, struct device *dev,
77 		       struct class_device *cdev)
78 {
79 	struct raid_data *rd;
80 
81 	BUG_ON(class_get_devdata(cdev));
82 
83 	rd = kzalloc(sizeof(*rd), GFP_KERNEL);
84 	if (!rd)
85 		return -ENOMEM;
86 
87 	INIT_LIST_HEAD(&rd->component_list);
88 	class_set_devdata(cdev, rd);
89 
90 	return 0;
91 }
92 
93 static int raid_remove(struct transport_container *tc, struct device *dev,
94 		       struct class_device *cdev)
95 {
96 	struct raid_data *rd = class_get_devdata(cdev);
97 	struct raid_component *rc, *next;
98 	dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
99 	class_set_devdata(cdev, NULL);
100 	list_for_each_entry_safe(rc, next, &rd->component_list, node) {
101 		list_del(&rc->node);
102 		dev_printk(KERN_ERR, rc->cdev.dev, "RAID COMPONENT REMOVE\n");
103 		class_device_unregister(&rc->cdev);
104 	}
105 	dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
106 	kfree(rd);
107 	return 0;
108 }
109 
110 static DECLARE_TRANSPORT_CLASS(raid_class,
111 			       "raid_devices",
112 			       raid_setup,
113 			       raid_remove,
114 			       NULL);
115 
116 static struct {
117 	enum raid_state	value;
118 	char		*name;
119 } raid_states[] = {
120 	{ RAID_STATE_UNKNOWN, "unknown" },
121 	{ RAID_STATE_ACTIVE, "active" },
122 	{ RAID_STATE_DEGRADED, "degraded" },
123 	{ RAID_STATE_RESYNCING, "resyncing" },
124 	{ RAID_STATE_OFFLINE, "offline" },
125 };
126 
127 static const char *raid_state_name(enum raid_state state)
128 {
129 	int i;
130 	char *name = NULL;
131 
132 	for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) {
133 		if (raid_states[i].value == state) {
134 			name = raid_states[i].name;
135 			break;
136 		}
137 	}
138 	return name;
139 }
140 
141 static struct {
142 	enum raid_level value;
143 	char *name;
144 } raid_levels[] = {
145 	{ RAID_LEVEL_UNKNOWN, "unknown" },
146 	{ RAID_LEVEL_LINEAR, "linear" },
147 	{ RAID_LEVEL_0, "raid0" },
148 	{ RAID_LEVEL_1, "raid1" },
149 	{ RAID_LEVEL_3, "raid3" },
150 	{ RAID_LEVEL_4, "raid4" },
151 	{ RAID_LEVEL_5, "raid5" },
152 	{ RAID_LEVEL_6, "raid6" },
153 };
154 
155 static const char *raid_level_name(enum raid_level level)
156 {
157 	int i;
158 	char *name = NULL;
159 
160 	for (i = 0; i < sizeof(raid_levels)/sizeof(raid_levels[0]); i++) {
161 		if (raid_levels[i].value == level) {
162 			name = raid_levels[i].name;
163 			break;
164 		}
165 	}
166 	return name;
167 }
168 
169 #define raid_attr_show_internal(attr, fmt, var, code)			\
170 static ssize_t raid_show_##attr(struct class_device *cdev, char *buf)	\
171 {									\
172 	struct raid_data *rd = class_get_devdata(cdev);			\
173 	code								\
174 	return snprintf(buf, 20, #fmt "\n", var);			\
175 }
176 
177 #define raid_attr_ro_states(attr, states, code)				\
178 raid_attr_show_internal(attr, %s, name,					\
179 	const char *name;						\
180 	code								\
181 	name = raid_##states##_name(rd->attr);				\
182 )									\
183 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
184 
185 
186 #define raid_attr_ro_internal(attr, code)				\
187 raid_attr_show_internal(attr, %d, rd->attr, code)			\
188 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
189 
190 #define ATTR_CODE(attr)							\
191 	struct raid_internal *i = class_device_to_raid_internal(cdev);	\
192 	if (i->f->get_##attr)						\
193 		i->f->get_##attr(cdev->dev);
194 
195 #define raid_attr_ro(attr)	raid_attr_ro_internal(attr, )
196 #define raid_attr_ro_fn(attr)	raid_attr_ro_internal(attr, ATTR_CODE(attr))
197 #define raid_attr_ro_state(attr)	raid_attr_ro_states(attr, attr, )
198 #define raid_attr_ro_state_fn(attr)	raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
199 
200 
201 raid_attr_ro_state(level);
202 raid_attr_ro_fn(resync);
203 raid_attr_ro_state_fn(state);
204 
205 static void raid_component_release(struct class_device *cdev)
206 {
207 	struct raid_component *rc = container_of(cdev, struct raid_component,
208 						 cdev);
209 	dev_printk(KERN_ERR, rc->cdev.dev, "COMPONENT RELEASE\n");
210 	put_device(rc->cdev.dev);
211 	kfree(rc);
212 }
213 
214 void raid_component_add(struct raid_template *r,struct device *raid_dev,
215 			struct device *component_dev)
216 {
217 	struct class_device *cdev =
218 		attribute_container_find_class_device(&r->raid_attrs.ac,
219 						      raid_dev);
220 	struct raid_component *rc;
221 	struct raid_data *rd = class_get_devdata(cdev);
222 
223 	rc = kzalloc(sizeof(*rc), GFP_KERNEL);
224 	if (!rc)
225 		return;
226 
227 	INIT_LIST_HEAD(&rc->node);
228 	class_device_initialize(&rc->cdev);
229 	rc->cdev.release = raid_component_release;
230 	rc->cdev.dev = get_device(component_dev);
231 	rc->num = rd->component_count++;
232 
233 	snprintf(rc->cdev.class_id, sizeof(rc->cdev.class_id),
234 		 "component-%d", rc->num);
235 	list_add_tail(&rc->node, &rd->component_list);
236 	rc->cdev.parent = cdev;
237 	rc->cdev.class = &raid_class.class;
238 	class_device_add(&rc->cdev);
239 }
240 EXPORT_SYMBOL(raid_component_add);
241 
242 struct raid_template *
243 raid_class_attach(struct raid_function_template *ft)
244 {
245 	struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
246 					  GFP_KERNEL);
247 	int count = 0;
248 
249 	if (unlikely(!i))
250 		return NULL;
251 
252 	i->f = ft;
253 
254 	i->r.raid_attrs.ac.class = &raid_class.class;
255 	i->r.raid_attrs.ac.match = raid_match;
256 	i->r.raid_attrs.ac.attrs = &i->attrs[0];
257 
258 	attribute_container_register(&i->r.raid_attrs.ac);
259 
260 	i->attrs[count++] = &class_device_attr_level;
261 	i->attrs[count++] = &class_device_attr_resync;
262 	i->attrs[count++] = &class_device_attr_state;
263 
264 	i->attrs[count] = NULL;
265 	BUG_ON(count > RAID_NUM_ATTRS);
266 
267 	return &i->r;
268 }
269 EXPORT_SYMBOL(raid_class_attach);
270 
271 void
272 raid_class_release(struct raid_template *r)
273 {
274 	struct raid_internal *i = to_raid_internal(r);
275 
276 	attribute_container_unregister(&i->r.raid_attrs.ac);
277 
278 	kfree(i);
279 }
280 EXPORT_SYMBOL(raid_class_release);
281 
282 static __init int raid_init(void)
283 {
284 	return transport_class_register(&raid_class);
285 }
286 
287 static __exit void raid_exit(void)
288 {
289 	transport_class_unregister(&raid_class);
290 }
291 
292 MODULE_AUTHOR("James Bottomley");
293 MODULE_DESCRIPTION("RAID device class");
294 MODULE_LICENSE("GPL");
295 
296 module_init(raid_init);
297 module_exit(raid_exit);
298 
299