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/semaphore.h>
20 #include <asm/ccwdev.h>
21 #include <asm/ccwgroup.h>
23 /* In Linux 2.4, we had a channel device layer called "chandev"
24 * that did all sorts of obscure stuff for networking devices.
25 * This is another driver that serves as a replacement for just
26 * one of its functions, namely the translation of single subchannels
27 * to devices that use multiple subchannels.
30 /* a device matches a driver if all its slave devices match the same
31 * entry of the driver */
33 ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
35 struct ccwgroup_device *gdev;
36 struct ccwgroup_driver *gdrv;
38 gdev = to_ccwgroupdev(dev);
39 gdrv = to_ccwgroupdrv(drv);
41 if (gdev->creator_id == gdrv->driver_id)
47 ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
53 static struct bus_type ccwgroup_bus_type;
56 __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
61 for (i = 0; i < gdev->count; i++) {
62 sprintf(str, "cdev%d", i);
63 sysfs_remove_link(&gdev->dev.kobj, str);
64 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
70 * Provide an 'ungroup' attribute so the user can remove group devices no
71 * longer needed or accidentially created. Saves memory :)
73 static void ccwgroup_ungroup_callback(struct device *dev)
75 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
77 mutex_lock(&gdev->reg_mutex);
78 __ccwgroup_remove_symlinks(gdev);
79 device_unregister(dev);
80 mutex_unlock(&gdev->reg_mutex);
84 ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
86 struct ccwgroup_device *gdev;
89 gdev = to_ccwgroupdev(dev);
91 if (gdev->state != CCWGROUP_OFFLINE)
94 /* Note that we cannot unregister the device from one of its
95 * attribute methods, so we have to use this roundabout approach.
97 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
103 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
106 ccwgroup_release (struct device *dev)
108 struct ccwgroup_device *gdev;
111 gdev = to_ccwgroupdev(dev);
113 for (i = 0; i < gdev->count; i++) {
114 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
115 put_device(&gdev->cdev[i]->dev);
121 __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
126 for (i = 0; i < gdev->count; i++) {
127 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
130 for (--i; i >= 0; i--)
131 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
136 for (i = 0; i < gdev->count; i++) {
137 sprintf(str, "cdev%d", i);
138 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
141 for (--i; i >= 0; i--) {
142 sprintf(str, "cdev%d", i);
143 sysfs_remove_link(&gdev->dev.kobj, str);
145 for (i = 0; i < gdev->count; i++)
146 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
155 * ccwgroup_create() - create and register a ccw group device
156 * @root: parent device for the new device
157 * @creator_id: identifier of creating driver
158 * @cdrv: ccw driver of slave devices
159 * @argc: number of slave devices
160 * @argv: bus ids of slave devices
162 * Create and register a new ccw group device as a child of @root. Slave
163 * devices are obtained from the list of bus ids given in @argv[] and must all
166 * %0 on success and an error code on failure.
170 int ccwgroup_create(struct device *root, unsigned int creator_id,
171 struct ccw_driver *cdrv, int argc, char *argv[])
173 struct ccwgroup_device *gdev;
177 if (argc > 256) /* disallow dumb users */
180 gdev = kzalloc(sizeof(*gdev) + argc*sizeof(gdev->cdev[0]), GFP_KERNEL);
184 atomic_set(&gdev->onoff, 0);
185 mutex_init(&gdev->reg_mutex);
186 mutex_lock(&gdev->reg_mutex);
187 for (i = 0; i < argc; i++) {
188 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, argv[i]);
190 /* all devices have to be of the same type in
191 * order to be grouped */
193 || gdev->cdev[i]->id.driver_info !=
194 gdev->cdev[0]->id.driver_info) {
198 /* Don't allow a device to belong to more than one group. */
199 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
203 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
206 gdev->creator_id = creator_id;
208 gdev->dev.bus = &ccwgroup_bus_type;
209 gdev->dev.parent = root;
210 gdev->dev.release = ccwgroup_release;
212 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
213 gdev->cdev[0]->dev.bus_id);
215 rc = device_register(&gdev->dev);
218 get_device(&gdev->dev);
219 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
222 device_unregister(&gdev->dev);
226 rc = __ccwgroup_create_symlinks(gdev);
228 mutex_unlock(&gdev->reg_mutex);
229 put_device(&gdev->dev);
232 device_remove_file(&gdev->dev, &dev_attr_ungroup);
233 device_unregister(&gdev->dev);
235 for (i = 0; i < argc; i++)
237 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
238 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
239 put_device(&gdev->cdev[i]->dev);
241 mutex_unlock(&gdev->reg_mutex);
242 put_device(&gdev->dev);
249 return bus_register (&ccwgroup_bus_type);
253 cleanup_ccwgroup (void)
255 bus_unregister (&ccwgroup_bus_type);
258 module_init(init_ccwgroup);
259 module_exit(cleanup_ccwgroup);
261 /************************** driver stuff ******************************/
264 ccwgroup_set_online(struct ccwgroup_device *gdev)
266 struct ccwgroup_driver *gdrv;
269 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
271 if (gdev->state == CCWGROUP_ONLINE) {
275 if (!gdev->dev.driver) {
279 gdrv = to_ccwgroupdrv (gdev->dev.driver);
280 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
283 gdev->state = CCWGROUP_ONLINE;
285 atomic_set(&gdev->onoff, 0);
290 ccwgroup_set_offline(struct ccwgroup_device *gdev)
292 struct ccwgroup_driver *gdrv;
295 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
297 if (gdev->state == CCWGROUP_OFFLINE) {
301 if (!gdev->dev.driver) {
305 gdrv = to_ccwgroupdrv (gdev->dev.driver);
306 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
309 gdev->state = CCWGROUP_OFFLINE;
311 atomic_set(&gdev->onoff, 0);
316 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
318 struct ccwgroup_device *gdev;
319 struct ccwgroup_driver *gdrv;
323 gdev = to_ccwgroupdev(dev);
327 gdrv = to_ccwgroupdrv (gdev->dev.driver);
328 if (!try_module_get(gdrv->owner))
331 value = simple_strtoul(buf, NULL, 0);
334 ccwgroup_set_online(gdev);
336 ccwgroup_set_offline(gdev);
339 module_put(gdrv->owner);
344 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
348 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
350 return sprintf(buf, online ? "1\n" : "0\n");
353 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
356 ccwgroup_probe (struct device *dev)
358 struct ccwgroup_device *gdev;
359 struct ccwgroup_driver *gdrv;
363 gdev = to_ccwgroupdev(dev);
364 gdrv = to_ccwgroupdrv(dev->driver);
366 if ((ret = device_create_file(dev, &dev_attr_online)))
369 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
371 device_remove_file(dev, &dev_attr_online);
377 ccwgroup_remove (struct device *dev)
379 struct ccwgroup_device *gdev;
380 struct ccwgroup_driver *gdrv;
382 gdev = to_ccwgroupdev(dev);
383 gdrv = to_ccwgroupdrv(dev->driver);
385 device_remove_file(dev, &dev_attr_online);
387 if (gdrv && gdrv->remove)
392 static struct bus_type ccwgroup_bus_type = {
394 .match = ccwgroup_bus_match,
395 .uevent = ccwgroup_uevent,
396 .probe = ccwgroup_probe,
397 .remove = ccwgroup_remove,
401 * ccwgroup_driver_register() - register a ccw group driver
402 * @cdriver: driver to be registered
404 * This function is mainly a wrapper around driver_register().
406 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
408 /* register our new driver with the core */
409 cdriver->driver.bus = &ccwgroup_bus_type;
410 cdriver->driver.name = cdriver->name;
412 return driver_register(&cdriver->driver);
416 __ccwgroup_match_all(struct device *dev, void *data)
422 * ccwgroup_driver_unregister() - deregister a ccw group driver
423 * @cdriver: driver to be deregistered
425 * This function is mainly a wrapper around driver_unregister().
427 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
431 /* We don't want ccwgroup devices to live longer than their driver. */
432 get_driver(&cdriver->driver);
433 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
434 __ccwgroup_match_all))) {
435 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
437 mutex_lock(&gdev->reg_mutex);
438 __ccwgroup_remove_symlinks(gdev);
439 device_unregister(dev);
440 mutex_unlock(&gdev->reg_mutex);
443 put_driver(&cdriver->driver);
444 driver_unregister(&cdriver->driver);
448 * ccwgroup_probe_ccwdev() - probe function for slave devices
449 * @cdev: ccw device to be probed
451 * This is a dummy probe function for ccw devices that are slave devices in
452 * a ccw group device.
456 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
461 static struct ccwgroup_device *
462 __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
464 struct ccwgroup_device *gdev;
466 gdev = dev_get_drvdata(&cdev->dev);
468 if (get_device(&gdev->dev)) {
469 mutex_lock(&gdev->reg_mutex);
470 if (device_is_registered(&gdev->dev))
472 mutex_unlock(&gdev->reg_mutex);
473 put_device(&gdev->dev);
481 * ccwgroup_remove_ccwdev() - remove function for slave devices
482 * @cdev: ccw device to be removed
484 * This is a remove function for ccw devices that are slave devices in a ccw
485 * group device. It sets the ccw device offline and also deregisters the
486 * embedding ccw group device.
488 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
490 struct ccwgroup_device *gdev;
492 /* Ignore offlining errors, device is gone anyway. */
493 ccw_device_set_offline(cdev);
494 /* If one of its devices is gone, the whole group is done for. */
495 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
497 __ccwgroup_remove_symlinks(gdev);
498 device_unregister(&gdev->dev);
499 mutex_unlock(&gdev->reg_mutex);
500 put_device(&gdev->dev);
504 MODULE_LICENSE("GPL");
505 EXPORT_SYMBOL(ccwgroup_driver_register);
506 EXPORT_SYMBOL(ccwgroup_driver_unregister);
507 EXPORT_SYMBOL(ccwgroup_create);
508 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
509 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);