2 * bus driver for ccwgroup
4 * Copyright IBM Corp. 2002, 2009
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
13 #include <linux/device.h>
14 #include <linux/init.h>
15 #include <linux/ctype.h>
16 #include <linux/dcache.h>
18 #include <asm/ccwdev.h>
19 #include <asm/ccwgroup.h>
21 #define CCW_BUS_ID_SIZE 20
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 * Remove references from ccw devices to ccw group device and from
71 * ccw group device to ccw devices.
73 static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
75 struct ccw_device *cdev;
78 for (i = 0; i < gdev->count; i++) {
82 spin_lock_irq(cdev->ccwlock);
83 dev_set_drvdata(&cdev->dev, NULL);
84 spin_unlock_irq(cdev->ccwlock);
86 put_device(&cdev->dev);
91 * Provide an 'ungroup' attribute so the user can remove group devices no
92 * longer needed or accidentially created. Saves memory :)
94 static void ccwgroup_ungroup_callback(struct device *dev)
96 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
98 mutex_lock(&gdev->reg_mutex);
99 if (device_is_registered(&gdev->dev)) {
100 __ccwgroup_remove_symlinks(gdev);
101 device_unregister(dev);
102 __ccwgroup_remove_cdev_refs(gdev);
104 mutex_unlock(&gdev->reg_mutex);
108 ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
110 struct ccwgroup_device *gdev;
113 gdev = to_ccwgroupdev(dev);
115 /* Prevent concurrent online/offline processing and ungrouping. */
116 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
118 if (gdev->state != CCWGROUP_OFFLINE) {
122 /* Note that we cannot unregister the device from one of its
123 * attribute methods, so we have to use this roundabout approach.
125 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
129 /* Release onoff "lock" when ungrouping failed. */
130 atomic_set(&gdev->onoff, 0);
136 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
139 ccwgroup_release (struct device *dev)
141 kfree(to_ccwgroupdev(dev));
145 __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
150 for (i = 0; i < gdev->count; i++) {
151 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
154 for (--i; i >= 0; i--)
155 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
160 for (i = 0; i < gdev->count; i++) {
161 sprintf(str, "cdev%d", i);
162 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
165 for (--i; i >= 0; i--) {
166 sprintf(str, "cdev%d", i);
167 sysfs_remove_link(&gdev->dev.kobj, str);
169 for (i = 0; i < gdev->count; i++)
170 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
178 static int __get_next_bus_id(const char **buf, char *bus_id)
183 start = (char *)*buf;
184 end = strchr(start, ',');
186 /* Last entry. Strip trailing newline, if applicable. */
187 end = strchr(start, '\n');
190 len = strlen(start) + 1;
192 len = end - start + 1;
195 if (len < CCW_BUS_ID_SIZE) {
196 strlcpy(bus_id, start, len);
204 static int __is_valid_bus_id(char bus_id[CCW_BUS_ID_SIZE])
206 int cssid, ssid, devno;
208 /* Must be of form %x.%x.%04x */
209 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
215 * ccwgroup_create_from_string() - create and register a ccw group device
216 * @root: parent device for the new device
217 * @creator_id: identifier of creating driver
218 * @cdrv: ccw driver of slave devices
219 * @num_devices: number of slave devices
220 * @buf: buffer containing comma separated bus ids of slave devices
222 * Create and register a new ccw group device as a child of @root. Slave
223 * devices are obtained from the list of bus ids given in @buf and must all
226 * %0 on success and an error code on failure.
230 int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
231 struct ccw_driver *cdrv, int num_devices,
234 struct ccwgroup_device *gdev;
236 char tmp_bus_id[CCW_BUS_ID_SIZE];
237 const char *curr_buf;
239 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
244 atomic_set(&gdev->onoff, 0);
245 mutex_init(&gdev->reg_mutex);
246 mutex_lock(&gdev->reg_mutex);
247 gdev->creator_id = creator_id;
248 gdev->count = num_devices;
249 gdev->dev.bus = &ccwgroup_bus_type;
250 gdev->dev.parent = root;
251 gdev->dev.release = ccwgroup_release;
252 device_initialize(&gdev->dev);
255 for (i = 0; i < num_devices && curr_buf; i++) {
256 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
259 if (!__is_valid_bus_id(tmp_bus_id)) {
263 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
265 * All devices have to be of the same type in
266 * order to be grouped.
269 || gdev->cdev[i]->id.driver_info !=
270 gdev->cdev[0]->id.driver_info) {
274 /* Don't allow a device to belong to more than one group. */
275 spin_lock_irq(gdev->cdev[i]->ccwlock);
276 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
277 spin_unlock_irq(gdev->cdev[i]->ccwlock);
281 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
282 spin_unlock_irq(gdev->cdev[i]->ccwlock);
284 /* Check for sufficient number of bus ids. */
285 if (i < num_devices && !curr_buf) {
289 /* Check for trailing stuff. */
290 if (i == num_devices && strlen(curr_buf) > 0) {
295 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
297 rc = device_add(&gdev->dev);
300 get_device(&gdev->dev);
301 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
304 device_unregister(&gdev->dev);
308 rc = __ccwgroup_create_symlinks(gdev);
310 mutex_unlock(&gdev->reg_mutex);
311 put_device(&gdev->dev);
314 device_remove_file(&gdev->dev, &dev_attr_ungroup);
315 device_unregister(&gdev->dev);
317 for (i = 0; i < num_devices; i++)
319 spin_lock_irq(gdev->cdev[i]->ccwlock);
320 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
321 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
322 spin_unlock_irq(gdev->cdev[i]->ccwlock);
323 put_device(&gdev->cdev[i]->dev);
324 gdev->cdev[i] = NULL;
326 mutex_unlock(&gdev->reg_mutex);
327 put_device(&gdev->dev);
330 EXPORT_SYMBOL(ccwgroup_create_from_string);
332 static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
335 static struct notifier_block ccwgroup_nb = {
336 .notifier_call = ccwgroup_notifier
339 static int __init init_ccwgroup(void)
343 ret = bus_register(&ccwgroup_bus_type);
347 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
349 bus_unregister(&ccwgroup_bus_type);
354 static void __exit cleanup_ccwgroup(void)
356 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
357 bus_unregister(&ccwgroup_bus_type);
360 module_init(init_ccwgroup);
361 module_exit(cleanup_ccwgroup);
363 /************************** driver stuff ******************************/
366 ccwgroup_set_online(struct ccwgroup_device *gdev)
368 struct ccwgroup_driver *gdrv;
371 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
373 if (gdev->state == CCWGROUP_ONLINE) {
377 if (!gdev->dev.driver) {
381 gdrv = to_ccwgroupdrv (gdev->dev.driver);
382 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
385 gdev->state = CCWGROUP_ONLINE;
387 atomic_set(&gdev->onoff, 0);
392 ccwgroup_set_offline(struct ccwgroup_device *gdev)
394 struct ccwgroup_driver *gdrv;
397 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
399 if (gdev->state == CCWGROUP_OFFLINE) {
403 if (!gdev->dev.driver) {
407 gdrv = to_ccwgroupdrv (gdev->dev.driver);
408 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
411 gdev->state = CCWGROUP_OFFLINE;
413 atomic_set(&gdev->onoff, 0);
418 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
420 struct ccwgroup_device *gdev;
421 struct ccwgroup_driver *gdrv;
428 gdev = to_ccwgroupdev(dev);
429 gdrv = to_ccwgroupdrv(dev->driver);
431 if (!try_module_get(gdrv->owner))
434 ret = strict_strtoul(buf, 0, &value);
439 ret = ccwgroup_set_online(gdev);
441 ret = ccwgroup_set_offline(gdev);
445 module_put(gdrv->owner);
446 return (ret == 0) ? count : ret;
450 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
454 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
456 return sprintf(buf, online ? "1\n" : "0\n");
459 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
462 ccwgroup_probe (struct device *dev)
464 struct ccwgroup_device *gdev;
465 struct ccwgroup_driver *gdrv;
469 gdev = to_ccwgroupdev(dev);
470 gdrv = to_ccwgroupdrv(dev->driver);
472 if ((ret = device_create_file(dev, &dev_attr_online)))
475 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
477 device_remove_file(dev, &dev_attr_online);
483 ccwgroup_remove (struct device *dev)
485 struct ccwgroup_device *gdev;
486 struct ccwgroup_driver *gdrv;
488 device_remove_file(dev, &dev_attr_online);
489 device_remove_file(dev, &dev_attr_ungroup);
494 gdev = to_ccwgroupdev(dev);
495 gdrv = to_ccwgroupdrv(dev->driver);
503 static void ccwgroup_shutdown(struct device *dev)
505 struct ccwgroup_device *gdev;
506 struct ccwgroup_driver *gdrv;
511 gdev = to_ccwgroupdev(dev);
512 gdrv = to_ccwgroupdrv(dev->driver);
515 gdrv->shutdown(gdev);
518 static int ccwgroup_pm_prepare(struct device *dev)
520 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
521 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
523 /* Fail while device is being set online/offline. */
524 if (atomic_read(&gdev->onoff))
527 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
530 return gdrv->prepare ? gdrv->prepare(gdev) : 0;
533 static void ccwgroup_pm_complete(struct device *dev)
535 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
536 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
538 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
542 gdrv->complete(gdev);
545 static int ccwgroup_pm_freeze(struct device *dev)
547 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
548 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
550 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
553 return gdrv->freeze ? gdrv->freeze(gdev) : 0;
556 static int ccwgroup_pm_thaw(struct device *dev)
558 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
559 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
561 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
564 return gdrv->thaw ? gdrv->thaw(gdev) : 0;
567 static int ccwgroup_pm_restore(struct device *dev)
569 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
570 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
572 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
575 return gdrv->restore ? gdrv->restore(gdev) : 0;
578 static const struct dev_pm_ops ccwgroup_pm_ops = {
579 .prepare = ccwgroup_pm_prepare,
580 .complete = ccwgroup_pm_complete,
581 .freeze = ccwgroup_pm_freeze,
582 .thaw = ccwgroup_pm_thaw,
583 .restore = ccwgroup_pm_restore,
586 static struct bus_type ccwgroup_bus_type = {
588 .match = ccwgroup_bus_match,
589 .uevent = ccwgroup_uevent,
590 .probe = ccwgroup_probe,
591 .remove = ccwgroup_remove,
592 .shutdown = ccwgroup_shutdown,
593 .pm = &ccwgroup_pm_ops,
597 static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
600 struct device *dev = data;
602 if (action == BUS_NOTIFY_UNBIND_DRIVER)
603 device_schedule_callback(dev, ccwgroup_ungroup_callback);
610 * ccwgroup_driver_register() - register a ccw group driver
611 * @cdriver: driver to be registered
613 * This function is mainly a wrapper around driver_register().
615 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
617 /* register our new driver with the core */
618 cdriver->driver.bus = &ccwgroup_bus_type;
619 cdriver->driver.name = cdriver->name;
620 cdriver->driver.owner = cdriver->owner;
622 return driver_register(&cdriver->driver);
626 __ccwgroup_match_all(struct device *dev, void *data)
632 * ccwgroup_driver_unregister() - deregister a ccw group driver
633 * @cdriver: driver to be deregistered
635 * This function is mainly a wrapper around driver_unregister().
637 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
641 /* We don't want ccwgroup devices to live longer than their driver. */
642 get_driver(&cdriver->driver);
643 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
644 __ccwgroup_match_all))) {
645 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
647 mutex_lock(&gdev->reg_mutex);
648 __ccwgroup_remove_symlinks(gdev);
649 device_unregister(dev);
650 __ccwgroup_remove_cdev_refs(gdev);
651 mutex_unlock(&gdev->reg_mutex);
654 put_driver(&cdriver->driver);
655 driver_unregister(&cdriver->driver);
659 * ccwgroup_probe_ccwdev() - probe function for slave devices
660 * @cdev: ccw device to be probed
662 * This is a dummy probe function for ccw devices that are slave devices in
663 * a ccw group device.
667 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
673 * ccwgroup_remove_ccwdev() - remove function for slave devices
674 * @cdev: ccw device to be removed
676 * This is a remove function for ccw devices that are slave devices in a ccw
677 * group device. It sets the ccw device offline and also deregisters the
678 * embedding ccw group device.
680 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
682 struct ccwgroup_device *gdev;
684 /* Ignore offlining errors, device is gone anyway. */
685 ccw_device_set_offline(cdev);
686 /* If one of its devices is gone, the whole group is done for. */
687 spin_lock_irq(cdev->ccwlock);
688 gdev = dev_get_drvdata(&cdev->dev);
690 spin_unlock_irq(cdev->ccwlock);
693 /* Get ccwgroup device reference for local processing. */
694 get_device(&gdev->dev);
695 spin_unlock_irq(cdev->ccwlock);
696 /* Unregister group device. */
697 mutex_lock(&gdev->reg_mutex);
698 if (device_is_registered(&gdev->dev)) {
699 __ccwgroup_remove_symlinks(gdev);
700 device_unregister(&gdev->dev);
701 __ccwgroup_remove_cdev_refs(gdev);
703 mutex_unlock(&gdev->reg_mutex);
704 /* Release ccwgroup device reference for local processing. */
705 put_device(&gdev->dev);
708 MODULE_LICENSE("GPL");
709 EXPORT_SYMBOL(ccwgroup_driver_register);
710 EXPORT_SYMBOL(ccwgroup_driver_unregister);
711 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
712 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);