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 = container_of(dev, struct ccwgroup_device, dev);
39 gdrv = container_of(drv, struct ccwgroup_driver, driver);
41 if (gdev->creator_id == gdrv->driver_id)
47 ccwgroup_uevent (struct device *dev, char **envp, int num_envp, char *buffer,
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 __ccwgroup_remove_symlinks(gdev);
80 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 gdev->cdev[i]->dev.driver_data = 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,
156 * ccwgroup_create() - create and register a ccw group device
157 * @root: parent device for the new device
158 * @creator_id: identifier of creating driver
159 * @cdrv: ccw driver of slave devices
160 * @argc: number of slave devices
161 * @argv: bus ids of slave devices
163 * Create and register a new ccw group device as a child of @root. Slave
164 * devices are obtained from the list of bus ids given in @argv[] and must all
167 * %0 on success and an error code on failure.
171 int ccwgroup_create(struct device *root, unsigned int creator_id,
172 struct ccw_driver *cdrv, int argc, char *argv[])
174 struct ccwgroup_device *gdev;
178 if (argc > 256) /* disallow dumb users */
181 gdev = kzalloc(sizeof(*gdev) + argc*sizeof(gdev->cdev[0]), GFP_KERNEL);
185 atomic_set(&gdev->onoff, 0);
186 mutex_init(&gdev->reg_mutex);
187 mutex_lock(&gdev->reg_mutex);
188 for (i = 0; i < argc; i++) {
189 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, argv[i]);
191 /* all devices have to be of the same type in
192 * order to be grouped */
194 || gdev->cdev[i]->id.driver_info !=
195 gdev->cdev[0]->id.driver_info) {
199 /* Don't allow a device to belong to more than one group. */
200 if (gdev->cdev[i]->dev.driver_data) {
204 gdev->cdev[i]->dev.driver_data = gdev;
207 gdev->creator_id = creator_id;
209 gdev->dev.bus = &ccwgroup_bus_type;
210 gdev->dev.parent = root;
211 gdev->dev.release = ccwgroup_release;
213 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
214 gdev->cdev[0]->dev.bus_id);
216 rc = device_register(&gdev->dev);
219 get_device(&gdev->dev);
220 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
223 device_unregister(&gdev->dev);
227 rc = __ccwgroup_create_symlinks(gdev);
229 mutex_unlock(&gdev->reg_mutex);
230 put_device(&gdev->dev);
233 device_remove_file(&gdev->dev, &dev_attr_ungroup);
234 device_unregister(&gdev->dev);
236 for (i = 0; i < argc; i++)
238 if (gdev->cdev[i]->dev.driver_data == gdev)
239 gdev->cdev[i]->dev.driver_data = NULL;
240 put_device(&gdev->cdev[i]->dev);
242 mutex_unlock(&gdev->reg_mutex);
243 put_device(&gdev->dev);
250 return bus_register (&ccwgroup_bus_type);
254 cleanup_ccwgroup (void)
256 bus_unregister (&ccwgroup_bus_type);
259 module_init(init_ccwgroup);
260 module_exit(cleanup_ccwgroup);
262 /************************** driver stuff ******************************/
265 ccwgroup_set_online(struct ccwgroup_device *gdev)
267 struct ccwgroup_driver *gdrv;
270 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
272 if (gdev->state == CCWGROUP_ONLINE) {
276 if (!gdev->dev.driver) {
280 gdrv = to_ccwgroupdrv (gdev->dev.driver);
281 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
284 gdev->state = CCWGROUP_ONLINE;
286 atomic_set(&gdev->onoff, 0);
291 ccwgroup_set_offline(struct ccwgroup_device *gdev)
293 struct ccwgroup_driver *gdrv;
296 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
298 if (gdev->state == CCWGROUP_OFFLINE) {
302 if (!gdev->dev.driver) {
306 gdrv = to_ccwgroupdrv (gdev->dev.driver);
307 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
310 gdev->state = CCWGROUP_OFFLINE;
312 atomic_set(&gdev->onoff, 0);
317 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
319 struct ccwgroup_device *gdev;
320 struct ccwgroup_driver *gdrv;
324 gdev = to_ccwgroupdev(dev);
328 gdrv = to_ccwgroupdrv (gdev->dev.driver);
329 if (!try_module_get(gdrv->owner))
332 value = simple_strtoul(buf, NULL, 0);
335 ccwgroup_set_online(gdev);
337 ccwgroup_set_offline(gdev);
340 module_put(gdrv->owner);
345 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
349 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
351 return sprintf(buf, online ? "1\n" : "0\n");
354 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
357 ccwgroup_probe (struct device *dev)
359 struct ccwgroup_device *gdev;
360 struct ccwgroup_driver *gdrv;
364 gdev = to_ccwgroupdev(dev);
365 gdrv = to_ccwgroupdrv(dev->driver);
367 if ((ret = device_create_file(dev, &dev_attr_online)))
370 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
372 device_remove_file(dev, &dev_attr_online);
378 ccwgroup_remove (struct device *dev)
380 struct ccwgroup_device *gdev;
381 struct ccwgroup_driver *gdrv;
383 gdev = to_ccwgroupdev(dev);
384 gdrv = to_ccwgroupdrv(dev->driver);
386 device_remove_file(dev, &dev_attr_online);
388 if (gdrv && gdrv->remove)
393 static struct bus_type ccwgroup_bus_type = {
395 .match = ccwgroup_bus_match,
396 .uevent = ccwgroup_uevent,
397 .probe = ccwgroup_probe,
398 .remove = ccwgroup_remove,
402 * ccwgroup_driver_register() - register a ccw group driver
403 * @cdriver: driver to be registered
405 * This function is mainly a wrapper around driver_register().
407 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
409 /* register our new driver with the core */
410 cdriver->driver.bus = &ccwgroup_bus_type;
411 cdriver->driver.name = cdriver->name;
413 return driver_register(&cdriver->driver);
417 __ccwgroup_match_all(struct device *dev, void *data)
423 * ccwgroup_driver_unregister() - deregister a ccw group driver
424 * @cdriver: driver to be deregistered
426 * This function is mainly a wrapper around driver_unregister().
428 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
432 /* We don't want ccwgroup devices to live longer than their driver. */
433 get_driver(&cdriver->driver);
434 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
435 __ccwgroup_match_all))) {
436 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
438 mutex_lock(&gdev->reg_mutex);
439 __ccwgroup_remove_symlinks(gdev);
440 device_unregister(dev);
441 mutex_unlock(&gdev->reg_mutex);
444 put_driver(&cdriver->driver);
445 driver_unregister(&cdriver->driver);
449 * ccwgroup_probe_ccwdev() - probe function for slave devices
450 * @cdev: ccw device to be probed
452 * This is a dummy probe function for ccw devices that are slave devices in
453 * a ccw group device.
457 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
462 static struct ccwgroup_device *
463 __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
465 struct ccwgroup_device *gdev;
467 if (cdev->dev.driver_data) {
468 gdev = (struct ccwgroup_device *)cdev->dev.driver_data;
469 if (get_device(&gdev->dev)) {
470 mutex_lock(&gdev->reg_mutex);
471 if (device_is_registered(&gdev->dev))
473 mutex_unlock(&gdev->reg_mutex);
474 put_device(&gdev->dev);
482 * ccwgroup_remove_ccwdev() - remove function for slave devices
483 * @cdev: ccw device to be removed
485 * This is a remove function for ccw devices that are slave devices in a ccw
486 * group device. It sets the ccw device offline and also deregisters the
487 * embedding ccw group device.
489 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
491 struct ccwgroup_device *gdev;
493 /* Ignore offlining errors, device is gone anyway. */
494 ccw_device_set_offline(cdev);
495 /* If one of its devices is gone, the whole group is done for. */
496 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
498 __ccwgroup_remove_symlinks(gdev);
499 device_unregister(&gdev->dev);
500 mutex_unlock(&gdev->reg_mutex);
501 put_device(&gdev->dev);
505 MODULE_LICENSE("GPL");
506 EXPORT_SYMBOL(ccwgroup_driver_register);
507 EXPORT_SYMBOL(ccwgroup_driver_unregister);
508 EXPORT_SYMBOL(ccwgroup_create);
509 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
510 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);