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++) {
116 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
117 put_device(&gdev->cdev[i]->dev);
124 __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
129 for (i = 0; i < gdev->count; i++) {
130 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
133 for (--i; i >= 0; i--)
134 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
139 for (i = 0; i < gdev->count; i++) {
140 sprintf(str, "cdev%d", i);
141 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
144 for (--i; i >= 0; i--) {
145 sprintf(str, "cdev%d", i);
146 sysfs_remove_link(&gdev->dev.kobj, str);
148 for (i = 0; i < gdev->count; i++)
149 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
157 static int __get_next_bus_id(const char **buf, char *bus_id)
162 start = (char *)*buf;
163 end = strchr(start, ',');
165 /* Last entry. Strip trailing newline, if applicable. */
166 end = strchr(start, '\n');
169 len = strlen(start) + 1;
171 len = end - start + 1;
174 if (len < BUS_ID_SIZE) {
175 strlcpy(bus_id, start, len);
183 static int __is_valid_bus_id(char bus_id[BUS_ID_SIZE])
185 int cssid, ssid, devno;
187 /* Must be of form %x.%x.%04x */
188 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
194 * ccwgroup_create_from_string() - create and register a ccw group device
195 * @root: parent device for the new device
196 * @creator_id: identifier of creating driver
197 * @cdrv: ccw driver of slave devices
198 * @num_devices: number of slave devices
199 * @buf: buffer containing comma separated bus ids of slave devices
201 * Create and register a new ccw group device as a child of @root. Slave
202 * devices are obtained from the list of bus ids given in @buf and must all
205 * %0 on success and an error code on failure.
209 int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
210 struct ccw_driver *cdrv, int num_devices,
213 struct ccwgroup_device *gdev;
215 char tmp_bus_id[BUS_ID_SIZE];
216 const char *curr_buf;
218 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
223 atomic_set(&gdev->onoff, 0);
224 mutex_init(&gdev->reg_mutex);
225 mutex_lock(&gdev->reg_mutex);
226 gdev->creator_id = creator_id;
227 gdev->count = num_devices;
228 gdev->dev.bus = &ccwgroup_bus_type;
229 gdev->dev.parent = root;
230 gdev->dev.release = ccwgroup_release;
231 device_initialize(&gdev->dev);
234 for (i = 0; i < num_devices && curr_buf; i++) {
235 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
238 if (!__is_valid_bus_id(tmp_bus_id)) {
242 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
244 * All devices have to be of the same type in
245 * order to be grouped.
248 || gdev->cdev[i]->id.driver_info !=
249 gdev->cdev[0]->id.driver_info) {
253 /* Don't allow a device to belong to more than one group. */
254 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
258 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
260 /* Check for sufficient number of bus ids. */
261 if (i < num_devices && !curr_buf) {
265 /* Check for trailing stuff. */
266 if (i == num_devices && strlen(curr_buf) > 0) {
271 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
272 gdev->cdev[0]->dev.bus_id);
274 rc = device_add(&gdev->dev);
277 get_device(&gdev->dev);
278 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
281 device_unregister(&gdev->dev);
285 rc = __ccwgroup_create_symlinks(gdev);
287 mutex_unlock(&gdev->reg_mutex);
288 put_device(&gdev->dev);
291 device_remove_file(&gdev->dev, &dev_attr_ungroup);
292 device_unregister(&gdev->dev);
294 for (i = 0; i < num_devices; i++)
296 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
297 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
298 put_device(&gdev->cdev[i]->dev);
300 mutex_unlock(&gdev->reg_mutex);
301 put_device(&gdev->dev);
304 EXPORT_SYMBOL(ccwgroup_create_from_string);
309 return bus_register (&ccwgroup_bus_type);
313 cleanup_ccwgroup (void)
315 bus_unregister (&ccwgroup_bus_type);
318 module_init(init_ccwgroup);
319 module_exit(cleanup_ccwgroup);
321 /************************** driver stuff ******************************/
324 ccwgroup_set_online(struct ccwgroup_device *gdev)
326 struct ccwgroup_driver *gdrv;
329 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
331 if (gdev->state == CCWGROUP_ONLINE) {
335 if (!gdev->dev.driver) {
339 gdrv = to_ccwgroupdrv (gdev->dev.driver);
340 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
343 gdev->state = CCWGROUP_ONLINE;
345 atomic_set(&gdev->onoff, 0);
350 ccwgroup_set_offline(struct ccwgroup_device *gdev)
352 struct ccwgroup_driver *gdrv;
355 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
357 if (gdev->state == CCWGROUP_OFFLINE) {
361 if (!gdev->dev.driver) {
365 gdrv = to_ccwgroupdrv (gdev->dev.driver);
366 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
369 gdev->state = CCWGROUP_OFFLINE;
371 atomic_set(&gdev->onoff, 0);
376 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
378 struct ccwgroup_device *gdev;
379 struct ccwgroup_driver *gdrv;
383 gdev = to_ccwgroupdev(dev);
387 gdrv = to_ccwgroupdrv (gdev->dev.driver);
388 if (!try_module_get(gdrv->owner))
391 ret = strict_strtoul(buf, 0, &value);
396 ccwgroup_set_online(gdev);
398 ccwgroup_set_offline(gdev);
402 module_put(gdrv->owner);
407 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
411 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
413 return sprintf(buf, online ? "1\n" : "0\n");
416 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
419 ccwgroup_probe (struct device *dev)
421 struct ccwgroup_device *gdev;
422 struct ccwgroup_driver *gdrv;
426 gdev = to_ccwgroupdev(dev);
427 gdrv = to_ccwgroupdrv(dev->driver);
429 if ((ret = device_create_file(dev, &dev_attr_online)))
432 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
434 device_remove_file(dev, &dev_attr_online);
440 ccwgroup_remove (struct device *dev)
442 struct ccwgroup_device *gdev;
443 struct ccwgroup_driver *gdrv;
445 gdev = to_ccwgroupdev(dev);
446 gdrv = to_ccwgroupdrv(dev->driver);
448 device_remove_file(dev, &dev_attr_online);
450 if (gdrv && gdrv->remove)
455 static void ccwgroup_shutdown(struct device *dev)
457 struct ccwgroup_device *gdev;
458 struct ccwgroup_driver *gdrv;
460 gdev = to_ccwgroupdev(dev);
461 gdrv = to_ccwgroupdrv(dev->driver);
462 if (gdrv && gdrv->shutdown)
463 gdrv->shutdown(gdev);
466 static struct bus_type ccwgroup_bus_type = {
468 .match = ccwgroup_bus_match,
469 .uevent = ccwgroup_uevent,
470 .probe = ccwgroup_probe,
471 .remove = ccwgroup_remove,
472 .shutdown = ccwgroup_shutdown,
476 * ccwgroup_driver_register() - register a ccw group driver
477 * @cdriver: driver to be registered
479 * This function is mainly a wrapper around driver_register().
481 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
483 /* register our new driver with the core */
484 cdriver->driver.bus = &ccwgroup_bus_type;
485 cdriver->driver.name = cdriver->name;
486 cdriver->driver.owner = cdriver->owner;
488 return driver_register(&cdriver->driver);
492 __ccwgroup_match_all(struct device *dev, void *data)
498 * ccwgroup_driver_unregister() - deregister a ccw group driver
499 * @cdriver: driver to be deregistered
501 * This function is mainly a wrapper around driver_unregister().
503 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
507 /* We don't want ccwgroup devices to live longer than their driver. */
508 get_driver(&cdriver->driver);
509 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
510 __ccwgroup_match_all))) {
511 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
513 mutex_lock(&gdev->reg_mutex);
514 __ccwgroup_remove_symlinks(gdev);
515 device_unregister(dev);
516 mutex_unlock(&gdev->reg_mutex);
519 put_driver(&cdriver->driver);
520 driver_unregister(&cdriver->driver);
524 * ccwgroup_probe_ccwdev() - probe function for slave devices
525 * @cdev: ccw device to be probed
527 * This is a dummy probe function for ccw devices that are slave devices in
528 * a ccw group device.
532 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
537 static struct ccwgroup_device *
538 __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
540 struct ccwgroup_device *gdev;
542 gdev = dev_get_drvdata(&cdev->dev);
544 if (get_device(&gdev->dev)) {
545 mutex_lock(&gdev->reg_mutex);
546 if (device_is_registered(&gdev->dev))
548 mutex_unlock(&gdev->reg_mutex);
549 put_device(&gdev->dev);
557 * ccwgroup_remove_ccwdev() - remove function for slave devices
558 * @cdev: ccw device to be removed
560 * This is a remove function for ccw devices that are slave devices in a ccw
561 * group device. It sets the ccw device offline and also deregisters the
562 * embedding ccw group device.
564 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
566 struct ccwgroup_device *gdev;
568 /* Ignore offlining errors, device is gone anyway. */
569 ccw_device_set_offline(cdev);
570 /* If one of its devices is gone, the whole group is done for. */
571 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
573 __ccwgroup_remove_symlinks(gdev);
574 device_unregister(&gdev->dev);
575 mutex_unlock(&gdev->reg_mutex);
576 put_device(&gdev->dev);
580 MODULE_LICENSE("GPL");
581 EXPORT_SYMBOL(ccwgroup_driver_register);
582 EXPORT_SYMBOL(ccwgroup_driver_unregister);
583 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
584 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);