2 * drivers/s390/cio/ccwgroup.c
3 * bus driver for ccwgroup
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
10 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/list.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
16 #include <linux/ctype.h>
17 #include <linux/dcache.h>
19 #include <asm/ccwdev.h>
20 #include <asm/ccwgroup.h>
22 /* In Linux 2.4, we had a channel device layer called "chandev"
23 * that did all sorts of obscure stuff for networking devices.
24 * This is another driver that serves as a replacement for just
25 * one of its functions, namely the translation of single subchannels
26 * to devices that use multiple subchannels.
29 /* a device matches a driver if all its slave devices match the same
30 * entry of the driver */
32 ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
34 struct ccwgroup_device *gdev;
35 struct ccwgroup_driver *gdrv;
37 gdev = to_ccwgroupdev(dev);
38 gdrv = to_ccwgroupdrv(drv);
40 if (gdev->creator_id == gdrv->driver_id)
46 ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
52 static struct bus_type ccwgroup_bus_type;
55 __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
60 for (i = 0; i < gdev->count; i++) {
61 sprintf(str, "cdev%d", i);
62 sysfs_remove_link(&gdev->dev.kobj, str);
63 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
69 * Provide an 'ungroup' attribute so the user can remove group devices no
70 * longer needed or accidentially created. Saves memory :)
72 static void ccwgroup_ungroup_callback(struct device *dev)
74 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
76 mutex_lock(&gdev->reg_mutex);
77 if (device_is_registered(&gdev->dev)) {
78 __ccwgroup_remove_symlinks(gdev);
79 device_unregister(dev);
81 mutex_unlock(&gdev->reg_mutex);
85 ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
87 struct ccwgroup_device *gdev;
90 gdev = to_ccwgroupdev(dev);
92 if (gdev->state != CCWGROUP_OFFLINE)
95 /* Note that we cannot unregister the device from one of its
96 * attribute methods, so we have to use this roundabout approach.
98 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
104 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
107 ccwgroup_release (struct device *dev)
109 struct ccwgroup_device *gdev;
112 gdev = to_ccwgroupdev(dev);
114 for (i = 0; i < gdev->count; i++) {
115 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
116 put_device(&gdev->cdev[i]->dev);
122 __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
127 for (i = 0; i < gdev->count; i++) {
128 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
131 for (--i; i >= 0; i--)
132 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
137 for (i = 0; i < gdev->count; i++) {
138 sprintf(str, "cdev%d", i);
139 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
142 for (--i; i >= 0; i--) {
143 sprintf(str, "cdev%d", i);
144 sysfs_remove_link(&gdev->dev.kobj, str);
146 for (i = 0; i < gdev->count; i++)
147 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
155 static int __get_next_bus_id(const char **buf, char *bus_id)
160 start = (char *)*buf;
161 end = strchr(start, ',');
163 /* Last entry. Strip trailing newline, if applicable. */
164 end = strchr(start, '\n');
167 len = strlen(start) + 1;
169 len = end - start + 1;
172 if (len < BUS_ID_SIZE) {
173 strlcpy(bus_id, start, len);
181 static int __is_valid_bus_id(char bus_id[BUS_ID_SIZE])
183 int cssid, ssid, devno;
185 /* Must be of form %x.%x.%04x */
186 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
192 * ccwgroup_create_from_string() - create and register a ccw group device
193 * @root: parent device for the new device
194 * @creator_id: identifier of creating driver
195 * @cdrv: ccw driver of slave devices
196 * @num_devices: number of slave devices
197 * @buf: buffer containing comma separated bus ids of slave devices
199 * Create and register a new ccw group device as a child of @root. Slave
200 * devices are obtained from the list of bus ids given in @buf and must all
203 * %0 on success and an error code on failure.
207 int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
208 struct ccw_driver *cdrv, int num_devices,
211 struct ccwgroup_device *gdev;
213 char tmp_bus_id[BUS_ID_SIZE];
214 const char *curr_buf;
216 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
221 atomic_set(&gdev->onoff, 0);
222 mutex_init(&gdev->reg_mutex);
223 mutex_lock(&gdev->reg_mutex);
225 for (i = 0; i < num_devices && curr_buf; i++) {
226 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
229 if (!__is_valid_bus_id(tmp_bus_id)) {
233 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
235 * All devices have to be of the same type in
236 * order to be grouped.
239 || gdev->cdev[i]->id.driver_info !=
240 gdev->cdev[0]->id.driver_info) {
244 /* Don't allow a device to belong to more than one group. */
245 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
249 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
251 /* Check for sufficient number of bus ids. */
252 if (i < num_devices && !curr_buf) {
256 /* Check for trailing stuff. */
257 if (i == num_devices && strlen(curr_buf) > 0) {
261 gdev->creator_id = creator_id;
262 gdev->count = num_devices;
263 gdev->dev.bus = &ccwgroup_bus_type;
264 gdev->dev.parent = root;
265 gdev->dev.release = ccwgroup_release;
267 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
268 gdev->cdev[0]->dev.bus_id);
270 rc = device_register(&gdev->dev);
273 get_device(&gdev->dev);
274 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
277 device_unregister(&gdev->dev);
281 rc = __ccwgroup_create_symlinks(gdev);
283 mutex_unlock(&gdev->reg_mutex);
284 put_device(&gdev->dev);
287 device_remove_file(&gdev->dev, &dev_attr_ungroup);
288 device_unregister(&gdev->dev);
290 for (i = 0; i < num_devices; i++)
292 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
293 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
294 put_device(&gdev->cdev[i]->dev);
296 mutex_unlock(&gdev->reg_mutex);
297 put_device(&gdev->dev);
300 EXPORT_SYMBOL(ccwgroup_create_from_string);
305 return bus_register (&ccwgroup_bus_type);
309 cleanup_ccwgroup (void)
311 bus_unregister (&ccwgroup_bus_type);
314 module_init(init_ccwgroup);
315 module_exit(cleanup_ccwgroup);
317 /************************** driver stuff ******************************/
320 ccwgroup_set_online(struct ccwgroup_device *gdev)
322 struct ccwgroup_driver *gdrv;
325 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
327 if (gdev->state == CCWGROUP_ONLINE) {
331 if (!gdev->dev.driver) {
335 gdrv = to_ccwgroupdrv (gdev->dev.driver);
336 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
339 gdev->state = CCWGROUP_ONLINE;
341 atomic_set(&gdev->onoff, 0);
346 ccwgroup_set_offline(struct ccwgroup_device *gdev)
348 struct ccwgroup_driver *gdrv;
351 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
353 if (gdev->state == CCWGROUP_OFFLINE) {
357 if (!gdev->dev.driver) {
361 gdrv = to_ccwgroupdrv (gdev->dev.driver);
362 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
365 gdev->state = CCWGROUP_OFFLINE;
367 atomic_set(&gdev->onoff, 0);
372 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
374 struct ccwgroup_device *gdev;
375 struct ccwgroup_driver *gdrv;
379 gdev = to_ccwgroupdev(dev);
383 gdrv = to_ccwgroupdrv (gdev->dev.driver);
384 if (!try_module_get(gdrv->owner))
387 ret = strict_strtoul(buf, 0, &value);
392 ccwgroup_set_online(gdev);
394 ccwgroup_set_offline(gdev);
398 module_put(gdrv->owner);
403 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
407 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
409 return sprintf(buf, online ? "1\n" : "0\n");
412 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
415 ccwgroup_probe (struct device *dev)
417 struct ccwgroup_device *gdev;
418 struct ccwgroup_driver *gdrv;
422 gdev = to_ccwgroupdev(dev);
423 gdrv = to_ccwgroupdrv(dev->driver);
425 if ((ret = device_create_file(dev, &dev_attr_online)))
428 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
430 device_remove_file(dev, &dev_attr_online);
436 ccwgroup_remove (struct device *dev)
438 struct ccwgroup_device *gdev;
439 struct ccwgroup_driver *gdrv;
441 gdev = to_ccwgroupdev(dev);
442 gdrv = to_ccwgroupdrv(dev->driver);
444 device_remove_file(dev, &dev_attr_online);
446 if (gdrv && gdrv->remove)
451 static void ccwgroup_shutdown(struct device *dev)
453 struct ccwgroup_device *gdev;
454 struct ccwgroup_driver *gdrv;
456 gdev = to_ccwgroupdev(dev);
457 gdrv = to_ccwgroupdrv(dev->driver);
458 if (gdrv && gdrv->shutdown)
459 gdrv->shutdown(gdev);
462 static struct bus_type ccwgroup_bus_type = {
464 .match = ccwgroup_bus_match,
465 .uevent = ccwgroup_uevent,
466 .probe = ccwgroup_probe,
467 .remove = ccwgroup_remove,
468 .shutdown = ccwgroup_shutdown,
472 * ccwgroup_driver_register() - register a ccw group driver
473 * @cdriver: driver to be registered
475 * This function is mainly a wrapper around driver_register().
477 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
479 /* register our new driver with the core */
480 cdriver->driver.bus = &ccwgroup_bus_type;
481 cdriver->driver.name = cdriver->name;
482 cdriver->driver.owner = cdriver->owner;
484 return driver_register(&cdriver->driver);
488 __ccwgroup_match_all(struct device *dev, void *data)
494 * ccwgroup_driver_unregister() - deregister a ccw group driver
495 * @cdriver: driver to be deregistered
497 * This function is mainly a wrapper around driver_unregister().
499 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
503 /* We don't want ccwgroup devices to live longer than their driver. */
504 get_driver(&cdriver->driver);
505 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
506 __ccwgroup_match_all))) {
507 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
509 mutex_lock(&gdev->reg_mutex);
510 __ccwgroup_remove_symlinks(gdev);
511 device_unregister(dev);
512 mutex_unlock(&gdev->reg_mutex);
515 put_driver(&cdriver->driver);
516 driver_unregister(&cdriver->driver);
520 * ccwgroup_probe_ccwdev() - probe function for slave devices
521 * @cdev: ccw device to be probed
523 * This is a dummy probe function for ccw devices that are slave devices in
524 * a ccw group device.
528 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
533 static struct ccwgroup_device *
534 __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
536 struct ccwgroup_device *gdev;
538 gdev = dev_get_drvdata(&cdev->dev);
540 if (get_device(&gdev->dev)) {
541 mutex_lock(&gdev->reg_mutex);
542 if (device_is_registered(&gdev->dev))
544 mutex_unlock(&gdev->reg_mutex);
545 put_device(&gdev->dev);
553 * ccwgroup_remove_ccwdev() - remove function for slave devices
554 * @cdev: ccw device to be removed
556 * This is a remove function for ccw devices that are slave devices in a ccw
557 * group device. It sets the ccw device offline and also deregisters the
558 * embedding ccw group device.
560 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
562 struct ccwgroup_device *gdev;
564 /* Ignore offlining errors, device is gone anyway. */
565 ccw_device_set_offline(cdev);
566 /* If one of its devices is gone, the whole group is done for. */
567 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
569 __ccwgroup_remove_symlinks(gdev);
570 device_unregister(&gdev->dev);
571 mutex_unlock(&gdev->reg_mutex);
572 put_device(&gdev->dev);
576 MODULE_LICENSE("GPL");
577 EXPORT_SYMBOL(ccwgroup_driver_register);
578 EXPORT_SYMBOL(ccwgroup_driver_unregister);
579 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
580 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);