[PATCH] s3c2410fb: Minor warning fix
[linux-2.6/sactl.git] / drivers / scsi / raid_class.c
blobf1ea5027865f5851310769b08e0f5b53df163f71
1 /*
2 * RAID Attributes
3 */
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/list.h>
7 #include <linux/raid_class.h>
8 #include <scsi/scsi_device.h>
9 #include <scsi/scsi_host.h>
11 #define RAID_NUM_ATTRS 3
13 struct raid_internal {
14 struct raid_template r;
15 struct raid_function_template *f;
16 /* The actual attributes */
17 struct class_device_attribute private_attrs[RAID_NUM_ATTRS];
18 /* The array of null terminated pointers to attributes
19 * needed by scsi_sysfs.c */
20 struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
23 struct raid_component {
24 struct list_head node;
25 struct device *dev;
26 int num;
29 #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r)
31 #define tc_to_raid_internal(tcont) ({ \
32 struct raid_template *r = \
33 container_of(tcont, struct raid_template, raid_attrs); \
34 to_raid_internal(r); \
37 #define ac_to_raid_internal(acont) ({ \
38 struct transport_container *tc = \
39 container_of(acont, struct transport_container, ac); \
40 tc_to_raid_internal(tc); \
43 #define class_device_to_raid_internal(cdev) ({ \
44 struct attribute_container *ac = \
45 attribute_container_classdev_to_container(cdev); \
46 ac_to_raid_internal(ac); \
50 static int raid_match(struct attribute_container *cont, struct device *dev)
52 /* We have to look for every subsystem that could house
53 * emulated RAID devices, so start with SCSI */
54 struct raid_internal *i = ac_to_raid_internal(cont);
56 if (scsi_is_sdev_device(dev)) {
57 struct scsi_device *sdev = to_scsi_device(dev);
59 if (i->f->cookie != sdev->host->hostt)
60 return 0;
62 return i->f->is_raid(dev);
64 /* FIXME: look at other subsystems too */
65 return 0;
68 static int raid_setup(struct transport_container *tc, struct device *dev,
69 struct class_device *cdev)
71 struct raid_data *rd;
73 BUG_ON(class_get_devdata(cdev));
75 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
76 if (!rd)
77 return -ENOMEM;
79 memset(rd, 0, sizeof(*rd));
80 INIT_LIST_HEAD(&rd->component_list);
81 class_set_devdata(cdev, rd);
83 return 0;
86 static int raid_remove(struct transport_container *tc, struct device *dev,
87 struct class_device *cdev)
89 struct raid_data *rd = class_get_devdata(cdev);
90 struct raid_component *rc, *next;
91 class_set_devdata(cdev, NULL);
92 list_for_each_entry_safe(rc, next, &rd->component_list, node) {
93 char buf[40];
94 snprintf(buf, sizeof(buf), "component-%d", rc->num);
95 list_del(&rc->node);
96 sysfs_remove_link(&cdev->kobj, buf);
97 kfree(rc);
99 kfree(class_get_devdata(cdev));
100 return 0;
103 static DECLARE_TRANSPORT_CLASS(raid_class,
104 "raid_devices",
105 raid_setup,
106 raid_remove,
107 NULL);
109 static struct {
110 enum raid_state value;
111 char *name;
112 } raid_states[] = {
113 { RAID_ACTIVE, "active" },
114 { RAID_DEGRADED, "degraded" },
115 { RAID_RESYNCING, "resyncing" },
116 { RAID_OFFLINE, "offline" },
119 static const char *raid_state_name(enum raid_state state)
121 int i;
122 char *name = NULL;
124 for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) {
125 if (raid_states[i].value == state) {
126 name = raid_states[i].name;
127 break;
130 return name;
134 #define raid_attr_show_internal(attr, fmt, var, code) \
135 static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \
137 struct raid_data *rd = class_get_devdata(cdev); \
138 code \
139 return snprintf(buf, 20, #fmt "\n", var); \
142 #define raid_attr_ro_states(attr, states, code) \
143 raid_attr_show_internal(attr, %s, name, \
144 const char *name; \
145 code \
146 name = raid_##states##_name(rd->attr); \
148 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
151 #define raid_attr_ro_internal(attr, code) \
152 raid_attr_show_internal(attr, %d, rd->attr, code) \
153 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
155 #define ATTR_CODE(attr) \
156 struct raid_internal *i = class_device_to_raid_internal(cdev); \
157 if (i->f->get_##attr) \
158 i->f->get_##attr(cdev->dev);
160 #define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
161 #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
162 #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
164 raid_attr_ro(level);
165 raid_attr_ro_fn(resync);
166 raid_attr_ro_state(state);
168 void raid_component_add(struct raid_template *r,struct device *raid_dev,
169 struct device *component_dev)
171 struct class_device *cdev =
172 attribute_container_find_class_device(&r->raid_attrs.ac,
173 raid_dev);
174 struct raid_component *rc;
175 struct raid_data *rd = class_get_devdata(cdev);
176 char buf[40];
178 rc = kmalloc(sizeof(*rc), GFP_KERNEL);
179 if (!rc)
180 return;
182 INIT_LIST_HEAD(&rc->node);
183 rc->dev = component_dev;
184 rc->num = rd->component_count++;
186 snprintf(buf, sizeof(buf), "component-%d", rc->num);
187 list_add_tail(&rc->node, &rd->component_list);
188 sysfs_create_link(&cdev->kobj, &component_dev->kobj, buf);
190 EXPORT_SYMBOL(raid_component_add);
192 struct raid_template *
193 raid_class_attach(struct raid_function_template *ft)
195 struct raid_internal *i = kmalloc(sizeof(struct raid_internal),
196 GFP_KERNEL);
197 int count = 0;
199 if (unlikely(!i))
200 return NULL;
202 memset(i, 0, sizeof(*i));
204 i->f = ft;
206 i->r.raid_attrs.ac.class = &raid_class.class;
207 i->r.raid_attrs.ac.match = raid_match;
208 i->r.raid_attrs.ac.attrs = &i->attrs[0];
210 attribute_container_register(&i->r.raid_attrs.ac);
212 i->attrs[count++] = &class_device_attr_level;
213 i->attrs[count++] = &class_device_attr_resync;
214 i->attrs[count++] = &class_device_attr_state;
216 i->attrs[count] = NULL;
217 BUG_ON(count > RAID_NUM_ATTRS);
219 return &i->r;
221 EXPORT_SYMBOL(raid_class_attach);
223 void
224 raid_class_release(struct raid_template *r)
226 struct raid_internal *i = to_raid_internal(r);
228 attribute_container_unregister(&i->r.raid_attrs.ac);
230 kfree(i);
232 EXPORT_SYMBOL(raid_class_release);
234 static __init int raid_init(void)
236 return transport_class_register(&raid_class);
239 static __exit void raid_exit(void)
241 transport_class_unregister(&raid_class);
244 MODULE_AUTHOR("James Bottomley");
245 MODULE_DESCRIPTION("RAID device class");
246 MODULE_LICENSE("GPL");
248 module_init(raid_init);
249 module_exit(raid_exit);