182664963SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
261a7afa2SJames Bottomley /*
3b1081ea6SJames Bottomley * raid_class.c - implementation of a simple raid visualisation class
4b1081ea6SJames Bottomley *
5b1081ea6SJames Bottomley * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
6b1081ea6SJames Bottomley *
7b1081ea6SJames Bottomley * This class is designed to allow raid attributes to be visualised and
8b1081ea6SJames Bottomley * manipulated in a form independent of the underlying raid. Ultimately this
9b1081ea6SJames Bottomley * should work for both hardware and software raids.
1061a7afa2SJames Bottomley */
1161a7afa2SJames Bottomley #include <linux/init.h>
1261a7afa2SJames Bottomley #include <linux/module.h>
1361a7afa2SJames Bottomley #include <linux/list.h>
148c65b4a6STim Schmielau #include <linux/slab.h>
158c65b4a6STim Schmielau #include <linux/string.h>
1661a7afa2SJames Bottomley #include <linux/raid_class.h>
1761a7afa2SJames Bottomley #include <scsi/scsi_device.h>
1861a7afa2SJames Bottomley #include <scsi/scsi_host.h>
1961a7afa2SJames Bottomley
2061a7afa2SJames Bottomley #define RAID_NUM_ATTRS 3
2161a7afa2SJames Bottomley
2261a7afa2SJames Bottomley struct raid_internal {
2361a7afa2SJames Bottomley struct raid_template r;
2461a7afa2SJames Bottomley struct raid_function_template *f;
2561a7afa2SJames Bottomley /* The actual attributes */
26ee959b00STony Jones struct device_attribute private_attrs[RAID_NUM_ATTRS];
2761a7afa2SJames Bottomley /* The array of null terminated pointers to attributes
2861a7afa2SJames Bottomley * needed by scsi_sysfs.c */
29ee959b00STony Jones struct device_attribute *attrs[RAID_NUM_ATTRS + 1];
3061a7afa2SJames Bottomley };
3161a7afa2SJames Bottomley
3261a7afa2SJames Bottomley struct raid_component {
3361a7afa2SJames Bottomley struct list_head node;
34ee959b00STony Jones struct device dev;
3561a7afa2SJames Bottomley int num;
3661a7afa2SJames Bottomley };
3761a7afa2SJames Bottomley
3861a7afa2SJames Bottomley #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r)
3961a7afa2SJames Bottomley
4061a7afa2SJames Bottomley #define tc_to_raid_internal(tcont) ({ \
4161a7afa2SJames Bottomley struct raid_template *r = \
4261a7afa2SJames Bottomley container_of(tcont, struct raid_template, raid_attrs); \
4361a7afa2SJames Bottomley to_raid_internal(r); \
4461a7afa2SJames Bottomley })
4561a7afa2SJames Bottomley
4661a7afa2SJames Bottomley #define ac_to_raid_internal(acont) ({ \
4761a7afa2SJames Bottomley struct transport_container *tc = \
4861a7afa2SJames Bottomley container_of(acont, struct transport_container, ac); \
4961a7afa2SJames Bottomley tc_to_raid_internal(tc); \
5061a7afa2SJames Bottomley })
5161a7afa2SJames Bottomley
52ee959b00STony Jones #define device_to_raid_internal(dev) ({ \
5361a7afa2SJames Bottomley struct attribute_container *ac = \
54ee959b00STony Jones attribute_container_classdev_to_container(dev); \
5561a7afa2SJames Bottomley ac_to_raid_internal(ac); \
5661a7afa2SJames Bottomley })
5761a7afa2SJames Bottomley
5861a7afa2SJames Bottomley
raid_match(struct attribute_container * cont,struct device * dev)5961a7afa2SJames Bottomley static int raid_match(struct attribute_container *cont, struct device *dev)
6061a7afa2SJames Bottomley {
6161a7afa2SJames Bottomley /* We have to look for every subsystem that could house
6261a7afa2SJames Bottomley * emulated RAID devices, so start with SCSI */
6361a7afa2SJames Bottomley struct raid_internal *i = ac_to_raid_internal(cont);
6461a7afa2SJames Bottomley
650eeec014SArnd Bergmann if (IS_ENABLED(CONFIG_SCSI) && scsi_is_sdev_device(dev)) {
6661a7afa2SJames Bottomley struct scsi_device *sdev = to_scsi_device(dev);
6761a7afa2SJames Bottomley
6861a7afa2SJames Bottomley if (i->f->cookie != sdev->host->hostt)
6961a7afa2SJames Bottomley return 0;
7061a7afa2SJames Bottomley
7161a7afa2SJames Bottomley return i->f->is_raid(dev);
7261a7afa2SJames Bottomley }
7361a7afa2SJames Bottomley /* FIXME: look at other subsystems too */
7461a7afa2SJames Bottomley return 0;
7561a7afa2SJames Bottomley }
7661a7afa2SJames Bottomley
raid_setup(struct transport_container * tc,struct device * dev,struct device * cdev)7761a7afa2SJames Bottomley static int raid_setup(struct transport_container *tc, struct device *dev,
78ee959b00STony Jones struct device *cdev)
7961a7afa2SJames Bottomley {
8061a7afa2SJames Bottomley struct raid_data *rd;
8161a7afa2SJames Bottomley
82ee959b00STony Jones BUG_ON(dev_get_drvdata(cdev));
8361a7afa2SJames Bottomley
84b1081ea6SJames Bottomley rd = kzalloc(sizeof(*rd), GFP_KERNEL);
8561a7afa2SJames Bottomley if (!rd)
8661a7afa2SJames Bottomley return -ENOMEM;
8761a7afa2SJames Bottomley
8861a7afa2SJames Bottomley INIT_LIST_HEAD(&rd->component_list);
89ee959b00STony Jones dev_set_drvdata(cdev, rd);
9061a7afa2SJames Bottomley
9161a7afa2SJames Bottomley return 0;
9261a7afa2SJames Bottomley }
9361a7afa2SJames Bottomley
raid_remove(struct transport_container * tc,struct device * dev,struct device * cdev)9461a7afa2SJames Bottomley static int raid_remove(struct transport_container *tc, struct device *dev,
95ee959b00STony Jones struct device *cdev)
9661a7afa2SJames Bottomley {
97ee959b00STony Jones struct raid_data *rd = dev_get_drvdata(cdev);
9861a7afa2SJames Bottomley struct raid_component *rc, *next;
99b1081ea6SJames Bottomley dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
100ee959b00STony Jones dev_set_drvdata(cdev, NULL);
10161a7afa2SJames Bottomley list_for_each_entry_safe(rc, next, &rd->component_list, node) {
10261a7afa2SJames Bottomley list_del(&rc->node);
103ee959b00STony Jones dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n");
104ee959b00STony Jones device_unregister(&rc->dev);
10561a7afa2SJames Bottomley }
106b1081ea6SJames Bottomley dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
107b1081ea6SJames Bottomley kfree(rd);
10861a7afa2SJames Bottomley return 0;
10961a7afa2SJames Bottomley }
11061a7afa2SJames Bottomley
11161a7afa2SJames Bottomley static DECLARE_TRANSPORT_CLASS(raid_class,
11261a7afa2SJames Bottomley "raid_devices",
11361a7afa2SJames Bottomley raid_setup,
11461a7afa2SJames Bottomley raid_remove,
11561a7afa2SJames Bottomley NULL);
11661a7afa2SJames Bottomley
1170ad78200SArjan van de Ven static const struct {
11861a7afa2SJames Bottomley enum raid_state value;
11961a7afa2SJames Bottomley char *name;
12061a7afa2SJames Bottomley } raid_states[] = {
121b1081ea6SJames Bottomley { RAID_STATE_UNKNOWN, "unknown" },
122b1081ea6SJames Bottomley { RAID_STATE_ACTIVE, "active" },
123b1081ea6SJames Bottomley { RAID_STATE_DEGRADED, "degraded" },
124b1081ea6SJames Bottomley { RAID_STATE_RESYNCING, "resyncing" },
125b1081ea6SJames Bottomley { RAID_STATE_OFFLINE, "offline" },
12661a7afa2SJames Bottomley };
12761a7afa2SJames Bottomley
raid_state_name(enum raid_state state)12861a7afa2SJames Bottomley static const char *raid_state_name(enum raid_state state)
12961a7afa2SJames Bottomley {
13061a7afa2SJames Bottomley int i;
13161a7afa2SJames Bottomley char *name = NULL;
13261a7afa2SJames Bottomley
1336391a113STobias Klauser for (i = 0; i < ARRAY_SIZE(raid_states); i++) {
13461a7afa2SJames Bottomley if (raid_states[i].value == state) {
13561a7afa2SJames Bottomley name = raid_states[i].name;
13661a7afa2SJames Bottomley break;
13761a7afa2SJames Bottomley }
13861a7afa2SJames Bottomley }
13961a7afa2SJames Bottomley return name;
14061a7afa2SJames Bottomley }
14161a7afa2SJames Bottomley
142b1081ea6SJames Bottomley static struct {
143b1081ea6SJames Bottomley enum raid_level value;
144b1081ea6SJames Bottomley char *name;
145b1081ea6SJames Bottomley } raid_levels[] = {
146b1081ea6SJames Bottomley { RAID_LEVEL_UNKNOWN, "unknown" },
147b1081ea6SJames Bottomley { RAID_LEVEL_LINEAR, "linear" },
148b1081ea6SJames Bottomley { RAID_LEVEL_0, "raid0" },
149b1081ea6SJames Bottomley { RAID_LEVEL_1, "raid1" },
1508e32ca49SMoore, Eric { RAID_LEVEL_10, "raid10" },
1518e4a0cf7SKashyap, Desai { RAID_LEVEL_1E, "raid1e" },
152b1081ea6SJames Bottomley { RAID_LEVEL_3, "raid3" },
153b1081ea6SJames Bottomley { RAID_LEVEL_4, "raid4" },
154b1081ea6SJames Bottomley { RAID_LEVEL_5, "raid5" },
1558e32ca49SMoore, Eric { RAID_LEVEL_50, "raid50" },
156b1081ea6SJames Bottomley { RAID_LEVEL_6, "raid6" },
15734e81f7aSHannes Reinecke { RAID_LEVEL_JBOD, "jbod" },
158b1081ea6SJames Bottomley };
159b1081ea6SJames Bottomley
raid_level_name(enum raid_level level)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
21261a7afa2SJames Bottomley struct raid_template *
raid_class_attach(struct raid_function_template * ft)21361a7afa2SJames Bottomley raid_class_attach(struct raid_function_template *ft)
21461a7afa2SJames Bottomley {
215b1081ea6SJames Bottomley struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
21661a7afa2SJames Bottomley GFP_KERNEL);
21761a7afa2SJames Bottomley int count = 0;
21861a7afa2SJames Bottomley
21961a7afa2SJames Bottomley if (unlikely(!i))
22061a7afa2SJames Bottomley return NULL;
22161a7afa2SJames Bottomley
22261a7afa2SJames Bottomley i->f = ft;
22361a7afa2SJames Bottomley
22461a7afa2SJames Bottomley i->r.raid_attrs.ac.class = &raid_class.class;
22561a7afa2SJames Bottomley i->r.raid_attrs.ac.match = raid_match;
22661a7afa2SJames Bottomley i->r.raid_attrs.ac.attrs = &i->attrs[0];
22761a7afa2SJames Bottomley
22861a7afa2SJames Bottomley attribute_container_register(&i->r.raid_attrs.ac);
22961a7afa2SJames Bottomley
230ee959b00STony Jones i->attrs[count++] = &dev_attr_level;
231ee959b00STony Jones i->attrs[count++] = &dev_attr_resync;
232ee959b00STony Jones i->attrs[count++] = &dev_attr_state;
23361a7afa2SJames Bottomley
23461a7afa2SJames Bottomley i->attrs[count] = NULL;
23561a7afa2SJames Bottomley BUG_ON(count > RAID_NUM_ATTRS);
23661a7afa2SJames Bottomley
23761a7afa2SJames Bottomley return &i->r;
23861a7afa2SJames Bottomley }
23961a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_attach);
24061a7afa2SJames Bottomley
24161a7afa2SJames Bottomley void
raid_class_release(struct raid_template * r)24261a7afa2SJames Bottomley raid_class_release(struct raid_template *r)
24361a7afa2SJames Bottomley {
24461a7afa2SJames Bottomley struct raid_internal *i = to_raid_internal(r);
24561a7afa2SJames Bottomley
2462f3edc69SJames Bottomley BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac));
24761a7afa2SJames Bottomley
24861a7afa2SJames Bottomley kfree(i);
24961a7afa2SJames Bottomley }
25061a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_release);
25161a7afa2SJames Bottomley
raid_init(void)25261a7afa2SJames Bottomley static __init int raid_init(void)
25361a7afa2SJames Bottomley {
25461a7afa2SJames Bottomley return transport_class_register(&raid_class);
25561a7afa2SJames Bottomley }
25661a7afa2SJames Bottomley
raid_exit(void)25761a7afa2SJames Bottomley static __exit void raid_exit(void)
25861a7afa2SJames Bottomley {
25961a7afa2SJames Bottomley transport_class_unregister(&raid_class);
26061a7afa2SJames Bottomley }
26161a7afa2SJames Bottomley
26261a7afa2SJames Bottomley MODULE_AUTHOR("James Bottomley");
26361a7afa2SJames Bottomley MODULE_DESCRIPTION("RAID device class");
26461a7afa2SJames Bottomley MODULE_LICENSE("GPL");
26561a7afa2SJames Bottomley
26661a7afa2SJames Bottomley module_init(raid_init);
26761a7afa2SJames Bottomley module_exit(raid_exit);
26861a7afa2SJames Bottomley
269