2 * bus driver for ccwgroup
4 * Copyright IBM Corp. 2002, 2012
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>
19 #include <asm/ccwdev.h>
20 #include <asm/ccwgroup.h>
24 #define CCW_BUS_ID_SIZE 10
26 /* In Linux 2.4, we had a channel device layer called "chandev"
27 * that did all sorts of obscure stuff for networking devices.
28 * This is another driver that serves as a replacement for just
29 * one of its functions, namely the translation of single subchannels
30 * to devices that use multiple subchannels.
33 static struct bus_type ccwgroup_bus_type;
35 static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
40 for (i = 0; i < gdev->count; i++) {
41 sprintf(str, "cdev%d", i);
42 sysfs_remove_link(&gdev->dev.kobj, str);
43 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
48 * Remove references from ccw devices to ccw group device and from
49 * ccw group device to ccw devices.
51 static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
53 struct ccw_device *cdev;
56 for (i = 0; i < gdev->count; i++) {
60 spin_lock_irq(cdev->ccwlock);
61 dev_set_drvdata(&cdev->dev, NULL);
62 spin_unlock_irq(cdev->ccwlock);
64 put_device(&cdev->dev);
69 * ccwgroup_set_online() - enable a ccwgroup device
70 * @gdev: target ccwgroup device
72 * This function attempts to put the ccwgroup device into the online state.
74 * %0 on success and a negative error value on failure.
76 int ccwgroup_set_online(struct ccwgroup_device *gdev)
78 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
81 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
83 if (gdev->state == CCWGROUP_ONLINE)
86 ret = gdrv->set_online(gdev);
90 gdev->state = CCWGROUP_ONLINE;
92 atomic_set(&gdev->onoff, 0);
95 EXPORT_SYMBOL(ccwgroup_set_online);
98 * ccwgroup_set_offline() - disable a ccwgroup device
99 * @gdev: target ccwgroup device
101 * This function attempts to put the ccwgroup device into the offline state.
103 * %0 on success and a negative error value on failure.
105 int ccwgroup_set_offline(struct ccwgroup_device *gdev)
107 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
110 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
112 if (gdev->state == CCWGROUP_OFFLINE)
114 if (gdrv->set_offline)
115 ret = gdrv->set_offline(gdev);
119 gdev->state = CCWGROUP_OFFLINE;
121 atomic_set(&gdev->onoff, 0);
124 EXPORT_SYMBOL(ccwgroup_set_offline);
126 static ssize_t ccwgroup_online_store(struct device *dev,
127 struct device_attribute *attr,
128 const char *buf, size_t count)
130 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
140 ret = kstrtoul(buf, 0, &value);
145 ret = ccwgroup_set_online(gdev);
147 ret = ccwgroup_set_offline(gdev);
152 return (ret == 0) ? count : ret;
155 static ssize_t ccwgroup_online_show(struct device *dev,
156 struct device_attribute *attr,
159 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
162 online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
164 return scnprintf(buf, PAGE_SIZE, "%d\n", online);
168 * Provide an 'ungroup' attribute so the user can remove group devices no
169 * longer needed or accidentially created. Saves memory :)
171 static void ccwgroup_ungroup(struct ccwgroup_device *gdev)
173 mutex_lock(&gdev->reg_mutex);
174 if (device_is_registered(&gdev->dev)) {
175 __ccwgroup_remove_symlinks(gdev);
176 device_unregister(&gdev->dev);
177 __ccwgroup_remove_cdev_refs(gdev);
179 mutex_unlock(&gdev->reg_mutex);
182 static ssize_t ccwgroup_ungroup_store(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf, size_t count)
186 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
189 /* Prevent concurrent online/offline processing and ungrouping. */
190 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
192 if (gdev->state != CCWGROUP_OFFLINE) {
197 if (device_remove_file_self(dev, attr))
198 ccwgroup_ungroup(gdev);
203 /* Release onoff "lock" when ungrouping failed. */
204 atomic_set(&gdev->onoff, 0);
209 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
210 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
212 static struct attribute *ccwgroup_attrs[] = {
213 &dev_attr_online.attr,
214 &dev_attr_ungroup.attr,
217 static struct attribute_group ccwgroup_attr_group = {
218 .attrs = ccwgroup_attrs,
220 static const struct attribute_group *ccwgroup_attr_groups[] = {
221 &ccwgroup_attr_group,
225 static void ccwgroup_ungroup_workfn(struct work_struct *work)
227 struct ccwgroup_device *gdev =
228 container_of(work, struct ccwgroup_device, ungroup_work);
230 ccwgroup_ungroup(gdev);
231 put_device(&gdev->dev);
234 static void ccwgroup_release(struct device *dev)
236 kfree(to_ccwgroupdev(dev));
239 static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
244 for (i = 0; i < gdev->count; i++) {
245 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
246 &gdev->dev.kobj, "group_device");
248 for (--i; i >= 0; i--)
249 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
254 for (i = 0; i < gdev->count; i++) {
255 sprintf(str, "cdev%d", i);
256 rc = sysfs_create_link(&gdev->dev.kobj,
257 &gdev->cdev[i]->dev.kobj, str);
259 for (--i; i >= 0; i--) {
260 sprintf(str, "cdev%d", i);
261 sysfs_remove_link(&gdev->dev.kobj, str);
263 for (i = 0; i < gdev->count; i++)
264 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
272 static int __get_next_id(const char **buf, struct ccw_dev_id *id)
274 unsigned int cssid, ssid, devno;
278 start = (char *)*buf;
279 end = strchr(start, ',');
281 /* Last entry. Strip trailing newline, if applicable. */
282 end = strchr(start, '\n');
285 len = strlen(start) + 1;
287 len = end - start + 1;
290 if (len <= CCW_BUS_ID_SIZE) {
291 if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
305 * ccwgroup_create_dev() - create and register a ccw group device
306 * @parent: parent device for the new device
307 * @gdrv: driver for the new group device
308 * @num_devices: number of slave devices
309 * @buf: buffer containing comma separated bus ids of slave devices
311 * Create and register a new ccw group device as a child of @parent. Slave
312 * devices are obtained from the list of bus ids given in @buf.
314 * %0 on success and an error code on failure.
318 int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
319 int num_devices, const char *buf)
321 struct ccwgroup_device *gdev;
322 struct ccw_dev_id dev_id;
325 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
330 atomic_set(&gdev->onoff, 0);
331 mutex_init(&gdev->reg_mutex);
332 mutex_lock(&gdev->reg_mutex);
333 INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn);
334 gdev->count = num_devices;
335 gdev->dev.bus = &ccwgroup_bus_type;
336 gdev->dev.parent = parent;
337 gdev->dev.release = ccwgroup_release;
338 device_initialize(&gdev->dev);
340 for (i = 0; i < num_devices && buf; i++) {
341 rc = __get_next_id(&buf, &dev_id);
344 gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
346 * All devices have to be of the same type in
347 * order to be grouped.
349 if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
350 gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
351 gdev->cdev[i]->id.driver_info !=
352 gdev->cdev[0]->id.driver_info) {
356 /* Don't allow a device to belong to more than one group. */
357 spin_lock_irq(gdev->cdev[i]->ccwlock);
358 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
359 spin_unlock_irq(gdev->cdev[i]->ccwlock);
363 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
364 spin_unlock_irq(gdev->cdev[i]->ccwlock);
366 /* Check for sufficient number of bus ids. */
367 if (i < num_devices) {
371 /* Check for trailing stuff. */
372 if (i == num_devices && strlen(buf) > 0) {
376 /* Check if the devices are bound to the required ccw driver. */
377 if (gdev->count && gdrv && gdrv->ccw_driver &&
378 gdev->cdev[0]->drv != gdrv->ccw_driver) {
383 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
384 gdev->dev.groups = ccwgroup_attr_groups;
387 gdev->dev.driver = &gdrv->driver;
388 rc = gdrv->setup ? gdrv->setup(gdev) : 0;
392 rc = device_add(&gdev->dev);
395 rc = __ccwgroup_create_symlinks(gdev);
397 device_del(&gdev->dev);
400 mutex_unlock(&gdev->reg_mutex);
403 for (i = 0; i < num_devices; i++)
405 spin_lock_irq(gdev->cdev[i]->ccwlock);
406 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
407 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
408 spin_unlock_irq(gdev->cdev[i]->ccwlock);
409 put_device(&gdev->cdev[i]->dev);
410 gdev->cdev[i] = NULL;
412 mutex_unlock(&gdev->reg_mutex);
413 put_device(&gdev->dev);
416 EXPORT_SYMBOL(ccwgroup_create_dev);
418 static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
421 struct ccwgroup_device *gdev = to_ccwgroupdev(data);
423 if (action == BUS_NOTIFY_UNBIND_DRIVER) {
424 get_device(&gdev->dev);
425 schedule_work(&gdev->ungroup_work);
431 static struct notifier_block ccwgroup_nb = {
432 .notifier_call = ccwgroup_notifier
435 static int __init init_ccwgroup(void)
439 ret = bus_register(&ccwgroup_bus_type);
443 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
445 bus_unregister(&ccwgroup_bus_type);
450 static void __exit cleanup_ccwgroup(void)
452 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
453 bus_unregister(&ccwgroup_bus_type);
456 module_init(init_ccwgroup);
457 module_exit(cleanup_ccwgroup);
459 /************************** driver stuff ******************************/
461 static int ccwgroup_remove(struct device *dev)
463 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
464 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
474 static void ccwgroup_shutdown(struct device *dev)
476 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
477 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
482 gdrv->shutdown(gdev);
485 static int ccwgroup_pm_prepare(struct device *dev)
487 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
488 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
490 /* Fail while device is being set online/offline. */
491 if (atomic_read(&gdev->onoff))
494 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
497 return gdrv->prepare ? gdrv->prepare(gdev) : 0;
500 static void ccwgroup_pm_complete(struct device *dev)
502 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
503 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
505 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
509 gdrv->complete(gdev);
512 static int ccwgroup_pm_freeze(struct device *dev)
514 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
515 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
517 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
520 return gdrv->freeze ? gdrv->freeze(gdev) : 0;
523 static int ccwgroup_pm_thaw(struct device *dev)
525 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
526 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
528 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
531 return gdrv->thaw ? gdrv->thaw(gdev) : 0;
534 static int ccwgroup_pm_restore(struct device *dev)
536 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
537 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
539 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
542 return gdrv->restore ? gdrv->restore(gdev) : 0;
545 static const struct dev_pm_ops ccwgroup_pm_ops = {
546 .prepare = ccwgroup_pm_prepare,
547 .complete = ccwgroup_pm_complete,
548 .freeze = ccwgroup_pm_freeze,
549 .thaw = ccwgroup_pm_thaw,
550 .restore = ccwgroup_pm_restore,
553 static struct bus_type ccwgroup_bus_type = {
555 .remove = ccwgroup_remove,
556 .shutdown = ccwgroup_shutdown,
557 .pm = &ccwgroup_pm_ops,
561 * ccwgroup_driver_register() - register a ccw group driver
562 * @cdriver: driver to be registered
564 * This function is mainly a wrapper around driver_register().
566 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
568 /* register our new driver with the core */
569 cdriver->driver.bus = &ccwgroup_bus_type;
571 return driver_register(&cdriver->driver);
573 EXPORT_SYMBOL(ccwgroup_driver_register);
575 static int __ccwgroup_match_all(struct device *dev, void *data)
581 * ccwgroup_driver_unregister() - deregister a ccw group driver
582 * @cdriver: driver to be deregistered
584 * This function is mainly a wrapper around driver_unregister().
586 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
590 /* We don't want ccwgroup devices to live longer than their driver. */
591 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
592 __ccwgroup_match_all))) {
593 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
595 ccwgroup_ungroup(gdev);
598 driver_unregister(&cdriver->driver);
600 EXPORT_SYMBOL(ccwgroup_driver_unregister);
603 * ccwgroup_probe_ccwdev() - probe function for slave devices
604 * @cdev: ccw device to be probed
606 * This is a dummy probe function for ccw devices that are slave devices in
607 * a ccw group device.
611 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
615 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
618 * ccwgroup_remove_ccwdev() - remove function for slave devices
619 * @cdev: ccw device to be removed
621 * This is a remove function for ccw devices that are slave devices in a ccw
622 * group device. It sets the ccw device offline and also deregisters the
623 * embedding ccw group device.
625 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
627 struct ccwgroup_device *gdev;
629 /* Ignore offlining errors, device is gone anyway. */
630 ccw_device_set_offline(cdev);
631 /* If one of its devices is gone, the whole group is done for. */
632 spin_lock_irq(cdev->ccwlock);
633 gdev = dev_get_drvdata(&cdev->dev);
635 spin_unlock_irq(cdev->ccwlock);
638 /* Get ccwgroup device reference for local processing. */
639 get_device(&gdev->dev);
640 spin_unlock_irq(cdev->ccwlock);
641 /* Unregister group device. */
642 ccwgroup_ungroup(gdev);
643 /* Release ccwgroup device reference for local processing. */
644 put_device(&gdev->dev);
646 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
647 MODULE_LICENSE("GPL");