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 #define CCW_BUS_ID_SIZE 20
24 /* In Linux 2.4, we had a channel device layer called "chandev"
25 * that did all sorts of obscure stuff for networking devices.
26 * This is another driver that serves as a replacement for just
27 * one of its functions, namely the translation of single subchannels
28 * to devices that use multiple subchannels.
31 /* a device matches a driver if all its slave devices match the same
32 * entry of the driver */
34 ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
36 struct ccwgroup_device *gdev;
37 struct ccwgroup_driver *gdrv;
39 gdev = to_ccwgroupdev(dev);
40 gdrv = to_ccwgroupdrv(drv);
42 if (gdev->creator_id == gdrv->driver_id)
48 ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
54 static struct bus_type ccwgroup_bus_type;
57 __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
62 for (i = 0; i < gdev->count; i++) {
63 sprintf(str, "cdev%d", i);
64 sysfs_remove_link(&gdev->dev.kobj, str);
65 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
71 * Provide an 'ungroup' attribute so the user can remove group devices no
72 * longer needed or accidentially created. Saves memory :)
74 static void ccwgroup_ungroup_callback(struct device *dev)
76 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
78 mutex_lock(&gdev->reg_mutex);
79 if (device_is_registered(&gdev->dev)) {
80 __ccwgroup_remove_symlinks(gdev);
81 device_unregister(dev);
83 mutex_unlock(&gdev->reg_mutex);
87 ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
89 struct ccwgroup_device *gdev;
92 gdev = to_ccwgroupdev(dev);
94 /* Prevent concurrent online/offline processing and ungrouping. */
95 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
97 if (gdev->state != CCWGROUP_OFFLINE) {
101 /* Note that we cannot unregister the device from one of its
102 * attribute methods, so we have to use this roundabout approach.
104 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
107 /* Release onoff "lock" when ungrouping failed. */
108 atomic_set(&gdev->onoff, 0);
114 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
117 ccwgroup_release (struct device *dev)
119 struct ccwgroup_device *gdev;
122 gdev = to_ccwgroupdev(dev);
124 for (i = 0; i < gdev->count; i++) {
126 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
127 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
128 put_device(&gdev->cdev[i]->dev);
135 __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
140 for (i = 0; i < gdev->count; i++) {
141 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
144 for (--i; i >= 0; i--)
145 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
150 for (i = 0; i < gdev->count; i++) {
151 sprintf(str, "cdev%d", i);
152 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
155 for (--i; i >= 0; i--) {
156 sprintf(str, "cdev%d", i);
157 sysfs_remove_link(&gdev->dev.kobj, str);
159 for (i = 0; i < gdev->count; i++)
160 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
168 static int __get_next_bus_id(const char **buf, char *bus_id)
173 start = (char *)*buf;
174 end = strchr(start, ',');
176 /* Last entry. Strip trailing newline, if applicable. */
177 end = strchr(start, '\n');
180 len = strlen(start) + 1;
182 len = end - start + 1;
185 if (len < CCW_BUS_ID_SIZE) {
186 strlcpy(bus_id, start, len);
194 static int __is_valid_bus_id(char bus_id[CCW_BUS_ID_SIZE])
196 int cssid, ssid, devno;
198 /* Must be of form %x.%x.%04x */
199 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
205 * ccwgroup_create_from_string() - create and register a ccw group device
206 * @root: parent device for the new device
207 * @creator_id: identifier of creating driver
208 * @cdrv: ccw driver of slave devices
209 * @num_devices: number of slave devices
210 * @buf: buffer containing comma separated bus ids of slave devices
212 * Create and register a new ccw group device as a child of @root. Slave
213 * devices are obtained from the list of bus ids given in @buf and must all
216 * %0 on success and an error code on failure.
220 int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
221 struct ccw_driver *cdrv, int num_devices,
224 struct ccwgroup_device *gdev;
226 char tmp_bus_id[CCW_BUS_ID_SIZE];
227 const char *curr_buf;
229 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
234 atomic_set(&gdev->onoff, 0);
235 mutex_init(&gdev->reg_mutex);
236 mutex_lock(&gdev->reg_mutex);
237 gdev->creator_id = creator_id;
238 gdev->count = num_devices;
239 gdev->dev.bus = &ccwgroup_bus_type;
240 gdev->dev.parent = root;
241 gdev->dev.release = ccwgroup_release;
242 device_initialize(&gdev->dev);
245 for (i = 0; i < num_devices && curr_buf; i++) {
246 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
249 if (!__is_valid_bus_id(tmp_bus_id)) {
253 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
255 * All devices have to be of the same type in
256 * order to be grouped.
259 || gdev->cdev[i]->id.driver_info !=
260 gdev->cdev[0]->id.driver_info) {
264 /* Don't allow a device to belong to more than one group. */
265 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
269 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
271 /* Check for sufficient number of bus ids. */
272 if (i < num_devices && !curr_buf) {
276 /* Check for trailing stuff. */
277 if (i == num_devices && strlen(curr_buf) > 0) {
282 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
284 rc = device_add(&gdev->dev);
287 get_device(&gdev->dev);
288 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
291 device_unregister(&gdev->dev);
295 rc = __ccwgroup_create_symlinks(gdev);
297 mutex_unlock(&gdev->reg_mutex);
298 put_device(&gdev->dev);
301 device_remove_file(&gdev->dev, &dev_attr_ungroup);
302 device_unregister(&gdev->dev);
304 for (i = 0; i < num_devices; i++)
306 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
307 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
308 put_device(&gdev->cdev[i]->dev);
309 gdev->cdev[i] = NULL;
311 mutex_unlock(&gdev->reg_mutex);
312 put_device(&gdev->dev);
315 EXPORT_SYMBOL(ccwgroup_create_from_string);
320 return bus_register (&ccwgroup_bus_type);
324 cleanup_ccwgroup (void)
326 bus_unregister (&ccwgroup_bus_type);
329 module_init(init_ccwgroup);
330 module_exit(cleanup_ccwgroup);
332 /************************** driver stuff ******************************/
335 ccwgroup_set_online(struct ccwgroup_device *gdev)
337 struct ccwgroup_driver *gdrv;
340 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
342 if (gdev->state == CCWGROUP_ONLINE) {
346 if (!gdev->dev.driver) {
350 gdrv = to_ccwgroupdrv (gdev->dev.driver);
351 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
354 gdev->state = CCWGROUP_ONLINE;
356 atomic_set(&gdev->onoff, 0);
361 ccwgroup_set_offline(struct ccwgroup_device *gdev)
363 struct ccwgroup_driver *gdrv;
366 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
368 if (gdev->state == CCWGROUP_OFFLINE) {
372 if (!gdev->dev.driver) {
376 gdrv = to_ccwgroupdrv (gdev->dev.driver);
377 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
380 gdev->state = CCWGROUP_OFFLINE;
382 atomic_set(&gdev->onoff, 0);
387 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
389 struct ccwgroup_device *gdev;
390 struct ccwgroup_driver *gdrv;
394 gdev = to_ccwgroupdev(dev);
398 gdrv = to_ccwgroupdrv (gdev->dev.driver);
399 if (!try_module_get(gdrv->owner))
402 ret = strict_strtoul(buf, 0, &value);
407 ccwgroup_set_online(gdev);
409 ccwgroup_set_offline(gdev);
413 module_put(gdrv->owner);
418 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
422 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
424 return sprintf(buf, online ? "1\n" : "0\n");
427 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
430 ccwgroup_probe (struct device *dev)
432 struct ccwgroup_device *gdev;
433 struct ccwgroup_driver *gdrv;
437 gdev = to_ccwgroupdev(dev);
438 gdrv = to_ccwgroupdrv(dev->driver);
440 if ((ret = device_create_file(dev, &dev_attr_online)))
443 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
445 device_remove_file(dev, &dev_attr_online);
451 ccwgroup_remove (struct device *dev)
453 struct ccwgroup_device *gdev;
454 struct ccwgroup_driver *gdrv;
456 gdev = to_ccwgroupdev(dev);
457 gdrv = to_ccwgroupdrv(dev->driver);
459 device_remove_file(dev, &dev_attr_online);
461 if (gdrv && gdrv->remove)
466 static void ccwgroup_shutdown(struct device *dev)
468 struct ccwgroup_device *gdev;
469 struct ccwgroup_driver *gdrv;
471 gdev = to_ccwgroupdev(dev);
472 gdrv = to_ccwgroupdrv(dev->driver);
473 if (gdrv && gdrv->shutdown)
474 gdrv->shutdown(gdev);
477 static struct bus_type ccwgroup_bus_type = {
479 .match = ccwgroup_bus_match,
480 .uevent = ccwgroup_uevent,
481 .probe = ccwgroup_probe,
482 .remove = ccwgroup_remove,
483 .shutdown = ccwgroup_shutdown,
487 * ccwgroup_driver_register() - register a ccw group driver
488 * @cdriver: driver to be registered
490 * This function is mainly a wrapper around driver_register().
492 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
494 /* register our new driver with the core */
495 cdriver->driver.bus = &ccwgroup_bus_type;
496 cdriver->driver.name = cdriver->name;
497 cdriver->driver.owner = cdriver->owner;
499 return driver_register(&cdriver->driver);
503 __ccwgroup_match_all(struct device *dev, void *data)
509 * ccwgroup_driver_unregister() - deregister a ccw group driver
510 * @cdriver: driver to be deregistered
512 * This function is mainly a wrapper around driver_unregister().
514 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
518 /* We don't want ccwgroup devices to live longer than their driver. */
519 get_driver(&cdriver->driver);
520 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
521 __ccwgroup_match_all))) {
522 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
524 mutex_lock(&gdev->reg_mutex);
525 __ccwgroup_remove_symlinks(gdev);
526 device_unregister(dev);
527 mutex_unlock(&gdev->reg_mutex);
530 put_driver(&cdriver->driver);
531 driver_unregister(&cdriver->driver);
535 * ccwgroup_probe_ccwdev() - probe function for slave devices
536 * @cdev: ccw device to be probed
538 * This is a dummy probe function for ccw devices that are slave devices in
539 * a ccw group device.
543 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
548 static struct ccwgroup_device *
549 __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
551 struct ccwgroup_device *gdev;
553 gdev = dev_get_drvdata(&cdev->dev);
555 if (get_device(&gdev->dev)) {
556 mutex_lock(&gdev->reg_mutex);
557 if (device_is_registered(&gdev->dev))
559 mutex_unlock(&gdev->reg_mutex);
560 put_device(&gdev->dev);
568 * ccwgroup_remove_ccwdev() - remove function for slave devices
569 * @cdev: ccw device to be removed
571 * This is a remove function for ccw devices that are slave devices in a ccw
572 * group device. It sets the ccw device offline and also deregisters the
573 * embedding ccw group device.
575 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
577 struct ccwgroup_device *gdev;
579 /* Ignore offlining errors, device is gone anyway. */
580 ccw_device_set_offline(cdev);
581 /* If one of its devices is gone, the whole group is done for. */
582 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
584 __ccwgroup_remove_symlinks(gdev);
585 device_unregister(&gdev->dev);
586 mutex_unlock(&gdev->reg_mutex);
587 put_device(&gdev->dev);
591 MODULE_LICENSE("GPL");
592 EXPORT_SYMBOL(ccwgroup_driver_register);
593 EXPORT_SYMBOL(ccwgroup_driver_unregister);
594 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
595 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);