[SCSI] convert sg to scsi_execute_async
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / raid_class.c
CommitLineData
61a7afa2 1/*
b1081ea6
JB
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.
61a7afa2
JB
11 */
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/list.h>
8c65b4a6
TS
15#include <linux/slab.h>
16#include <linux/string.h>
61a7afa2
JB
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 class_device_attribute private_attrs[RAID_NUM_ATTRS];
28 /* The array of null terminated pointers to attributes
29 * needed by scsi_sysfs.c */
30 struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
31};
32
33struct raid_component {
34 struct list_head node;
b1081ea6 35 struct class_device cdev;
61a7afa2
JB
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 class_device_to_raid_internal(cdev) ({ \
54 struct attribute_container *ac = \
55 attribute_container_classdev_to_container(cdev); \
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 (scsi_is_sdev_device(dev)) {
67 struct scsi_device *sdev = to_scsi_device(dev);
68
69 if (i->f->cookie != sdev->host->hostt)
70 return 0;
71
72 return i->f->is_raid(dev);
73 }
74 /* FIXME: look at other subsystems too */
75 return 0;
76}
77
78static int raid_setup(struct transport_container *tc, struct device *dev,
79 struct class_device *cdev)
80{
81 struct raid_data *rd;
82
83 BUG_ON(class_get_devdata(cdev));
84
b1081ea6 85 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
61a7afa2
JB
86 if (!rd)
87 return -ENOMEM;
88
61a7afa2
JB
89 INIT_LIST_HEAD(&rd->component_list);
90 class_set_devdata(cdev, rd);
91
92 return 0;
93}
94
95static int raid_remove(struct transport_container *tc, struct device *dev,
96 struct class_device *cdev)
97{
98 struct raid_data *rd = class_get_devdata(cdev);
99 struct raid_component *rc, *next;
b1081ea6 100 dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
61a7afa2
JB
101 class_set_devdata(cdev, NULL);
102 list_for_each_entry_safe(rc, next, &rd->component_list, node) {
61a7afa2 103 list_del(&rc->node);
b1081ea6
JB
104 dev_printk(KERN_ERR, rc->cdev.dev, "RAID COMPONENT REMOVE\n");
105 class_device_unregister(&rc->cdev);
61a7afa2 106 }
b1081ea6
JB
107 dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
108 kfree(rd);
61a7afa2
JB
109 return 0;
110}
111
112static DECLARE_TRANSPORT_CLASS(raid_class,
113 "raid_devices",
114 raid_setup,
115 raid_remove,
116 NULL);
117
0ad78200 118static const struct {
61a7afa2
JB
119 enum raid_state value;
120 char *name;
121} raid_states[] = {
b1081ea6
JB
122 { RAID_STATE_UNKNOWN, "unknown" },
123 { RAID_STATE_ACTIVE, "active" },
124 { RAID_STATE_DEGRADED, "degraded" },
125 { RAID_STATE_RESYNCING, "resyncing" },
126 { RAID_STATE_OFFLINE, "offline" },
61a7afa2
JB
127};
128
129static const char *raid_state_name(enum raid_state state)
130{
131 int i;
132 char *name = NULL;
133
134 for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) {
135 if (raid_states[i].value == state) {
136 name = raid_states[i].name;
137 break;
138 }
139 }
140 return name;
141}
142
b1081ea6
JB
143static struct {
144 enum raid_level value;
145 char *name;
146} raid_levels[] = {
147 { RAID_LEVEL_UNKNOWN, "unknown" },
148 { RAID_LEVEL_LINEAR, "linear" },
149 { RAID_LEVEL_0, "raid0" },
150 { RAID_LEVEL_1, "raid1" },
151 { RAID_LEVEL_3, "raid3" },
152 { RAID_LEVEL_4, "raid4" },
153 { RAID_LEVEL_5, "raid5" },
154 { RAID_LEVEL_6, "raid6" },
155};
156
157static const char *raid_level_name(enum raid_level level)
158{
159 int i;
160 char *name = NULL;
161
162 for (i = 0; i < sizeof(raid_levels)/sizeof(raid_levels[0]); i++) {
163 if (raid_levels[i].value == level) {
164 name = raid_levels[i].name;
165 break;
166 }
167 }
168 return name;
169}
61a7afa2
JB
170
171#define raid_attr_show_internal(attr, fmt, var, code) \
172static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \
173{ \
174 struct raid_data *rd = class_get_devdata(cdev); \
175 code \
176 return snprintf(buf, 20, #fmt "\n", var); \
177}
178
179#define raid_attr_ro_states(attr, states, code) \
180raid_attr_show_internal(attr, %s, name, \
181 const char *name; \
182 code \
183 name = raid_##states##_name(rd->attr); \
184) \
185static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
186
187
188#define raid_attr_ro_internal(attr, code) \
189raid_attr_show_internal(attr, %d, rd->attr, code) \
190static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
191
192#define ATTR_CODE(attr) \
193 struct raid_internal *i = class_device_to_raid_internal(cdev); \
194 if (i->f->get_##attr) \
195 i->f->get_##attr(cdev->dev);
196
197#define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
198#define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
b1081ea6
JB
199#define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, )
200#define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
201
61a7afa2 202
b1081ea6 203raid_attr_ro_state(level);
61a7afa2 204raid_attr_ro_fn(resync);
b1081ea6
JB
205raid_attr_ro_state_fn(state);
206
207static void raid_component_release(struct class_device *cdev)
208{
209 struct raid_component *rc = container_of(cdev, struct raid_component,
210 cdev);
211 dev_printk(KERN_ERR, rc->cdev.dev, "COMPONENT RELEASE\n");
212 put_device(rc->cdev.dev);
213 kfree(rc);
214}
61a7afa2
JB
215
216void raid_component_add(struct raid_template *r,struct device *raid_dev,
217 struct device *component_dev)
218{
219 struct class_device *cdev =
220 attribute_container_find_class_device(&r->raid_attrs.ac,
221 raid_dev);
222 struct raid_component *rc;
223 struct raid_data *rd = class_get_devdata(cdev);
61a7afa2 224
b1081ea6 225 rc = kzalloc(sizeof(*rc), GFP_KERNEL);
61a7afa2
JB
226 if (!rc)
227 return;
228
229 INIT_LIST_HEAD(&rc->node);
b1081ea6
JB
230 class_device_initialize(&rc->cdev);
231 rc->cdev.release = raid_component_release;
232 rc->cdev.dev = get_device(component_dev);
61a7afa2
JB
233 rc->num = rd->component_count++;
234
b1081ea6
JB
235 snprintf(rc->cdev.class_id, sizeof(rc->cdev.class_id),
236 "component-%d", rc->num);
61a7afa2 237 list_add_tail(&rc->node, &rd->component_list);
b1081ea6
JB
238 rc->cdev.parent = cdev;
239 rc->cdev.class = &raid_class.class;
240 class_device_add(&rc->cdev);
61a7afa2
JB
241}
242EXPORT_SYMBOL(raid_component_add);
243
244struct raid_template *
245raid_class_attach(struct raid_function_template *ft)
246{
b1081ea6 247 struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
61a7afa2
JB
248 GFP_KERNEL);
249 int count = 0;
250
251 if (unlikely(!i))
252 return NULL;
253
61a7afa2
JB
254 i->f = ft;
255
256 i->r.raid_attrs.ac.class = &raid_class.class;
257 i->r.raid_attrs.ac.match = raid_match;
258 i->r.raid_attrs.ac.attrs = &i->attrs[0];
259
260 attribute_container_register(&i->r.raid_attrs.ac);
261
262 i->attrs[count++] = &class_device_attr_level;
263 i->attrs[count++] = &class_device_attr_resync;
264 i->attrs[count++] = &class_device_attr_state;
265
266 i->attrs[count] = NULL;
267 BUG_ON(count > RAID_NUM_ATTRS);
268
269 return &i->r;
270}
271EXPORT_SYMBOL(raid_class_attach);
272
273void
274raid_class_release(struct raid_template *r)
275{
276 struct raid_internal *i = to_raid_internal(r);
277
278 attribute_container_unregister(&i->r.raid_attrs.ac);
279
280 kfree(i);
281}
282EXPORT_SYMBOL(raid_class_release);
283
284static __init int raid_init(void)
285{
286 return transport_class_register(&raid_class);
287}
288
289static __exit void raid_exit(void)
290{
291 transport_class_unregister(&raid_class);
292}
293
294MODULE_AUTHOR("James Bottomley");
295MODULE_DESCRIPTION("RAID device class");
296MODULE_LICENSE("GPL");
297
298module_init(raid_init);
299module_exit(raid_exit);
300