• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/scsi/
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/slab.h>
16#include <linux/string.h>
17#include <linux/raid_class.h>
18#include <scsi/scsi_device.h>
19#include <scsi/scsi_host.h>
20
21#define RAID_NUM_ATTRS	3
22
23struct raid_internal {
24	struct raid_template r;
25	struct raid_function_template *f;
26	/* The actual attributes */
27	struct device_attribute private_attrs[RAID_NUM_ATTRS];
28	/* The array of null terminated pointers to attributes
29	 * needed by scsi_sysfs.c */
30	struct device_attribute *attrs[RAID_NUM_ATTRS + 1];
31};
32
33struct raid_component {
34	struct list_head node;
35	struct device dev;
36	int num;
37};
38
39#define to_raid_internal(tmpl)	container_of(tmpl, struct raid_internal, r)
40
41#define tc_to_raid_internal(tcont) ({					\
42	struct raid_template *r =					\
43		container_of(tcont, struct raid_template, raid_attrs);	\
44	to_raid_internal(r);						\
45})
46
47#define ac_to_raid_internal(acont) ({					\
48	struct transport_container *tc =				\
49		container_of(acont, struct transport_container, ac);	\
50	tc_to_raid_internal(tc);					\
51})
52
53#define device_to_raid_internal(dev) ({				\
54	struct attribute_container *ac =				\
55		attribute_container_classdev_to_container(dev);	\
56	ac_to_raid_internal(ac);					\
57})
58
59
60static int raid_match(struct attribute_container *cont, struct device *dev)
61{
62	/* We have to look for every subsystem that could house
63	 * emulated RAID devices, so start with SCSI */
64	struct raid_internal *i = ac_to_raid_internal(cont);
65
66#if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE)
67	if (scsi_is_sdev_device(dev)) {
68		struct scsi_device *sdev = to_scsi_device(dev);
69
70		if (i->f->cookie != sdev->host->hostt)
71			return 0;
72
73		return i->f->is_raid(dev);
74	}
75#endif
76	return 0;
77}
78
79static int raid_setup(struct transport_container *tc, struct device *dev,
80		       struct device *cdev)
81{
82	struct raid_data *rd;
83
84	BUG_ON(dev_get_drvdata(cdev));
85
86	rd = kzalloc(sizeof(*rd), GFP_KERNEL);
87	if (!rd)
88		return -ENOMEM;
89
90	INIT_LIST_HEAD(&rd->component_list);
91	dev_set_drvdata(cdev, rd);
92
93	return 0;
94}
95
96static int raid_remove(struct transport_container *tc, struct device *dev,
97		       struct device *cdev)
98{
99	struct raid_data *rd = dev_get_drvdata(cdev);
100	struct raid_component *rc, *next;
101	dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
102	dev_set_drvdata(cdev, NULL);
103	list_for_each_entry_safe(rc, next, &rd->component_list, node) {
104		list_del(&rc->node);
105		dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n");
106		device_unregister(&rc->dev);
107	}
108	dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
109	kfree(rd);
110	return 0;
111}
112
113static DECLARE_TRANSPORT_CLASS(raid_class,
114			       "raid_devices",
115			       raid_setup,
116			       raid_remove,
117			       NULL);
118
119static const struct {
120	enum raid_state	value;
121	char		*name;
122} raid_states[] = {
123	{ RAID_STATE_UNKNOWN, "unknown" },
124	{ RAID_STATE_ACTIVE, "active" },
125	{ RAID_STATE_DEGRADED, "degraded" },
126	{ RAID_STATE_RESYNCING, "resyncing" },
127	{ RAID_STATE_OFFLINE, "offline" },
128};
129
130static const char *raid_state_name(enum raid_state state)
131{
132	int i;
133	char *name = NULL;
134
135	for (i = 0; i < ARRAY_SIZE(raid_states); i++) {
136		if (raid_states[i].value == state) {
137			name = raid_states[i].name;
138			break;
139		}
140	}
141	return name;
142}
143
144static struct {
145	enum raid_level value;
146	char *name;
147} raid_levels[] = {
148	{ RAID_LEVEL_UNKNOWN, "unknown" },
149	{ RAID_LEVEL_LINEAR, "linear" },
150	{ RAID_LEVEL_0, "raid0" },
151	{ RAID_LEVEL_1, "raid1" },
152	{ RAID_LEVEL_10, "raid10" },
153	{ RAID_LEVEL_1E, "raid1e" },
154	{ RAID_LEVEL_3, "raid3" },
155	{ RAID_LEVEL_4, "raid4" },
156	{ RAID_LEVEL_5, "raid5" },
157	{ RAID_LEVEL_50, "raid50" },
158	{ RAID_LEVEL_6, "raid6" },
159};
160
161static const char *raid_level_name(enum raid_level level)
162{
163	int i;
164	char *name = NULL;
165
166	for (i = 0; i < ARRAY_SIZE(raid_levels); i++) {
167		if (raid_levels[i].value == level) {
168			name = raid_levels[i].name;
169			break;
170		}
171	}
172	return name;
173}
174
175#define raid_attr_show_internal(attr, fmt, var, code)			\
176static ssize_t raid_show_##attr(struct device *dev, 			\
177				struct device_attribute *attr, 		\
178				char *buf)				\
179{									\
180	struct raid_data *rd = dev_get_drvdata(dev);			\
181	code								\
182	return snprintf(buf, 20, #fmt "\n", var);			\
183}
184
185#define raid_attr_ro_states(attr, states, code)				\
186raid_attr_show_internal(attr, %s, name,					\
187	const char *name;						\
188	code								\
189	name = raid_##states##_name(rd->attr);				\
190)									\
191static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
192
193
194#define raid_attr_ro_internal(attr, code)				\
195raid_attr_show_internal(attr, %d, rd->attr, code)			\
196static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
197
198#define ATTR_CODE(attr)							\
199	struct raid_internal *i = device_to_raid_internal(dev);		\
200	if (i->f->get_##attr)						\
201		i->f->get_##attr(dev->parent);
202
203#define raid_attr_ro(attr)	raid_attr_ro_internal(attr, )
204#define raid_attr_ro_fn(attr)	raid_attr_ro_internal(attr, ATTR_CODE(attr))
205#define raid_attr_ro_state(attr)	raid_attr_ro_states(attr, attr, )
206#define raid_attr_ro_state_fn(attr)	raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
207
208
209raid_attr_ro_state(level);
210raid_attr_ro_fn(resync);
211raid_attr_ro_state_fn(state);
212
213static void raid_component_release(struct device *dev)
214{
215	struct raid_component *rc =
216		container_of(dev, struct raid_component, dev);
217	dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE\n");
218	put_device(rc->dev.parent);
219	kfree(rc);
220}
221
222int raid_component_add(struct raid_template *r,struct device *raid_dev,
223		       struct device *component_dev)
224{
225	struct device *cdev =
226		attribute_container_find_class_device(&r->raid_attrs.ac,
227						      raid_dev);
228	struct raid_component *rc;
229	struct raid_data *rd = dev_get_drvdata(cdev);
230	int err;
231
232	rc = kzalloc(sizeof(*rc), GFP_KERNEL);
233	if (!rc)
234		return -ENOMEM;
235
236	INIT_LIST_HEAD(&rc->node);
237	device_initialize(&rc->dev);
238	rc->dev.release = raid_component_release;
239	rc->dev.parent = get_device(component_dev);
240	rc->num = rd->component_count++;
241
242	dev_set_name(&rc->dev, "component-%d", rc->num);
243	list_add_tail(&rc->node, &rd->component_list);
244	rc->dev.class = &raid_class.class;
245	err = device_add(&rc->dev);
246	if (err)
247		goto err_out;
248
249	return 0;
250
251err_out:
252	list_del(&rc->node);
253	rd->component_count--;
254	put_device(component_dev);
255	kfree(rc);
256	return err;
257}
258EXPORT_SYMBOL(raid_component_add);
259
260struct raid_template *
261raid_class_attach(struct raid_function_template *ft)
262{
263	struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
264					  GFP_KERNEL);
265	int count = 0;
266
267	if (unlikely(!i))
268		return NULL;
269
270	i->f = ft;
271
272	i->r.raid_attrs.ac.class = &raid_class.class;
273	i->r.raid_attrs.ac.match = raid_match;
274	i->r.raid_attrs.ac.attrs = &i->attrs[0];
275
276	attribute_container_register(&i->r.raid_attrs.ac);
277
278	i->attrs[count++] = &dev_attr_level;
279	i->attrs[count++] = &dev_attr_resync;
280	i->attrs[count++] = &dev_attr_state;
281
282	i->attrs[count] = NULL;
283	BUG_ON(count > RAID_NUM_ATTRS);
284
285	return &i->r;
286}
287EXPORT_SYMBOL(raid_class_attach);
288
289void
290raid_class_release(struct raid_template *r)
291{
292	struct raid_internal *i = to_raid_internal(r);
293
294	BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac));
295
296	kfree(i);
297}
298EXPORT_SYMBOL(raid_class_release);
299
300static __init int raid_init(void)
301{
302	return transport_class_register(&raid_class);
303}
304
305static __exit void raid_exit(void)
306{
307	transport_class_unregister(&raid_class);
308}
309
310MODULE_AUTHOR("James Bottomley");
311MODULE_DESCRIPTION("RAID device class");
312MODULE_LICENSE("GPL");
313
314module_init(raid_init);
315module_exit(raid_exit);
316