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 * 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 if (device_is_registered(&gdev
->dev
)) {
79 __ccwgroup_remove_symlinks(gdev
);
80 device_unregister(dev
);
82 mutex_unlock(&gdev
->reg_mutex
);
86 ccwgroup_ungroup_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
88 struct ccwgroup_device
*gdev
;
91 gdev
= to_ccwgroupdev(dev
);
93 /* Prevent concurrent online/offline processing and ungrouping. */
94 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
96 if (gdev
->state
!= CCWGROUP_OFFLINE
) {
100 /* Note that we cannot unregister the device from one of its
101 * attribute methods, so we have to use this roundabout approach.
103 rc
= device_schedule_callback(dev
, ccwgroup_ungroup_callback
);
107 /* Release onoff "lock" when ungrouping failed. */
108 atomic_set(&gdev
->onoff
, 0);
114 static DEVICE_ATTR(ungroup
, 0200, NULL
, ccwgroup_ungroup_store
);
117 ccwgroup_release (struct device
*dev
)
119 struct ccwgroup_device
*gdev
;
122 gdev
= to_ccwgroupdev(dev
);
124 for (i
= 0; i
< gdev
->count
; i
++) {
126 spin_lock_irq(gdev
->cdev
[i
]->ccwlock
);
127 if (dev_get_drvdata(&gdev
->cdev
[i
]->dev
) == gdev
)
128 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, NULL
);
129 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
130 put_device(&gdev
->cdev
[i
]->dev
);
137 __ccwgroup_create_symlinks(struct ccwgroup_device
*gdev
)
142 for (i
= 0; i
< gdev
->count
; i
++) {
143 rc
= sysfs_create_link(&gdev
->cdev
[i
]->dev
.kobj
, &gdev
->dev
.kobj
,
146 for (--i
; i
>= 0; i
--)
147 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
,
152 for (i
= 0; i
< gdev
->count
; i
++) {
153 sprintf(str
, "cdev%d", i
);
154 rc
= sysfs_create_link(&gdev
->dev
.kobj
, &gdev
->cdev
[i
]->dev
.kobj
,
157 for (--i
; i
>= 0; i
--) {
158 sprintf(str
, "cdev%d", i
);
159 sysfs_remove_link(&gdev
->dev
.kobj
, str
);
161 for (i
= 0; i
< gdev
->count
; i
++)
162 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
,
170 static int __get_next_bus_id(const char **buf
, char *bus_id
)
175 start
= (char *)*buf
;
176 end
= strchr(start
, ',');
178 /* Last entry. Strip trailing newline, if applicable. */
179 end
= strchr(start
, '\n');
182 len
= strlen(start
) + 1;
184 len
= end
- start
+ 1;
187 if (len
< CCW_BUS_ID_SIZE
) {
188 strlcpy(bus_id
, start
, len
);
196 static int __is_valid_bus_id(char bus_id
[CCW_BUS_ID_SIZE
])
198 int cssid
, ssid
, devno
;
200 /* Must be of form %x.%x.%04x */
201 if (sscanf(bus_id
, "%x.%1x.%04x", &cssid
, &ssid
, &devno
) != 3)
207 * ccwgroup_create_from_string() - create and register a ccw group device
208 * @root: parent device for the new device
209 * @creator_id: identifier of creating driver
210 * @cdrv: ccw driver of slave devices
211 * @num_devices: number of slave devices
212 * @buf: buffer containing comma separated bus ids of slave devices
214 * Create and register a new ccw group device as a child of @root. Slave
215 * devices are obtained from the list of bus ids given in @buf and must all
218 * %0 on success and an error code on failure.
222 int ccwgroup_create_from_string(struct device
*root
, unsigned int creator_id
,
223 struct ccw_driver
*cdrv
, int num_devices
,
226 struct ccwgroup_device
*gdev
;
228 char tmp_bus_id
[CCW_BUS_ID_SIZE
];
229 const char *curr_buf
;
231 gdev
= kzalloc(sizeof(*gdev
) + num_devices
* sizeof(gdev
->cdev
[0]),
236 atomic_set(&gdev
->onoff
, 0);
237 mutex_init(&gdev
->reg_mutex
);
238 mutex_lock(&gdev
->reg_mutex
);
239 gdev
->creator_id
= creator_id
;
240 gdev
->count
= num_devices
;
241 gdev
->dev
.bus
= &ccwgroup_bus_type
;
242 gdev
->dev
.parent
= root
;
243 gdev
->dev
.release
= ccwgroup_release
;
244 device_initialize(&gdev
->dev
);
247 for (i
= 0; i
< num_devices
&& curr_buf
; i
++) {
248 rc
= __get_next_bus_id(&curr_buf
, tmp_bus_id
);
251 if (!__is_valid_bus_id(tmp_bus_id
)) {
255 gdev
->cdev
[i
] = get_ccwdev_by_busid(cdrv
, tmp_bus_id
);
257 * All devices have to be of the same type in
258 * order to be grouped.
261 || gdev
->cdev
[i
]->id
.driver_info
!=
262 gdev
->cdev
[0]->id
.driver_info
) {
266 /* Don't allow a device to belong to more than one group. */
267 spin_lock_irq(gdev
->cdev
[i
]->ccwlock
);
268 if (dev_get_drvdata(&gdev
->cdev
[i
]->dev
)) {
269 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
273 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, gdev
);
274 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
276 /* Check for sufficient number of bus ids. */
277 if (i
< num_devices
&& !curr_buf
) {
281 /* Check for trailing stuff. */
282 if (i
== num_devices
&& strlen(curr_buf
) > 0) {
287 dev_set_name(&gdev
->dev
, "%s", dev_name(&gdev
->cdev
[0]->dev
));
289 rc
= device_add(&gdev
->dev
);
292 get_device(&gdev
->dev
);
293 rc
= device_create_file(&gdev
->dev
, &dev_attr_ungroup
);
296 device_unregister(&gdev
->dev
);
300 rc
= __ccwgroup_create_symlinks(gdev
);
302 mutex_unlock(&gdev
->reg_mutex
);
303 put_device(&gdev
->dev
);
306 device_remove_file(&gdev
->dev
, &dev_attr_ungroup
);
307 device_unregister(&gdev
->dev
);
309 for (i
= 0; i
< num_devices
; i
++)
311 spin_lock_irq(gdev
->cdev
[i
]->ccwlock
);
312 if (dev_get_drvdata(&gdev
->cdev
[i
]->dev
) == gdev
)
313 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, NULL
);
314 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
315 put_device(&gdev
->cdev
[i
]->dev
);
316 gdev
->cdev
[i
] = NULL
;
318 mutex_unlock(&gdev
->reg_mutex
);
319 put_device(&gdev
->dev
);
322 EXPORT_SYMBOL(ccwgroup_create_from_string
);
324 static int ccwgroup_notifier(struct notifier_block
*nb
, unsigned long action
,
327 static struct notifier_block ccwgroup_nb
= {
328 .notifier_call
= ccwgroup_notifier
331 static int __init
init_ccwgroup(void)
335 ret
= bus_register(&ccwgroup_bus_type
);
339 ret
= bus_register_notifier(&ccwgroup_bus_type
, &ccwgroup_nb
);
341 bus_unregister(&ccwgroup_bus_type
);
346 static void __exit
cleanup_ccwgroup(void)
348 bus_unregister_notifier(&ccwgroup_bus_type
, &ccwgroup_nb
);
349 bus_unregister(&ccwgroup_bus_type
);
352 module_init(init_ccwgroup
);
353 module_exit(cleanup_ccwgroup
);
355 /************************** driver stuff ******************************/
358 ccwgroup_set_online(struct ccwgroup_device
*gdev
)
360 struct ccwgroup_driver
*gdrv
;
363 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
365 if (gdev
->state
== CCWGROUP_ONLINE
) {
369 if (!gdev
->dev
.driver
) {
373 gdrv
= to_ccwgroupdrv (gdev
->dev
.driver
);
374 if ((ret
= gdrv
->set_online
? gdrv
->set_online(gdev
) : 0))
377 gdev
->state
= CCWGROUP_ONLINE
;
379 atomic_set(&gdev
->onoff
, 0);
384 ccwgroup_set_offline(struct ccwgroup_device
*gdev
)
386 struct ccwgroup_driver
*gdrv
;
389 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
391 if (gdev
->state
== CCWGROUP_OFFLINE
) {
395 if (!gdev
->dev
.driver
) {
399 gdrv
= to_ccwgroupdrv (gdev
->dev
.driver
);
400 if ((ret
= gdrv
->set_offline
? gdrv
->set_offline(gdev
) : 0))
403 gdev
->state
= CCWGROUP_OFFLINE
;
405 atomic_set(&gdev
->onoff
, 0);
410 ccwgroup_online_store (struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
412 struct ccwgroup_device
*gdev
;
413 struct ccwgroup_driver
*gdrv
;
420 gdev
= to_ccwgroupdev(dev
);
421 gdrv
= to_ccwgroupdrv(dev
->driver
);
423 if (!try_module_get(gdrv
->owner
))
426 ret
= strict_strtoul(buf
, 0, &value
);
431 ret
= ccwgroup_set_online(gdev
);
433 ret
= ccwgroup_set_offline(gdev
);
437 module_put(gdrv
->owner
);
438 return (ret
== 0) ? count
: ret
;
442 ccwgroup_online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
446 online
= (to_ccwgroupdev(dev
)->state
== CCWGROUP_ONLINE
);
448 return sprintf(buf
, online
? "1\n" : "0\n");
451 static DEVICE_ATTR(online
, 0644, ccwgroup_online_show
, ccwgroup_online_store
);
454 ccwgroup_probe (struct device
*dev
)
456 struct ccwgroup_device
*gdev
;
457 struct ccwgroup_driver
*gdrv
;
461 gdev
= to_ccwgroupdev(dev
);
462 gdrv
= to_ccwgroupdrv(dev
->driver
);
464 if ((ret
= device_create_file(dev
, &dev_attr_online
)))
467 ret
= gdrv
->probe
? gdrv
->probe(gdev
) : -ENODEV
;
469 device_remove_file(dev
, &dev_attr_online
);
475 ccwgroup_remove (struct device
*dev
)
477 struct ccwgroup_device
*gdev
;
478 struct ccwgroup_driver
*gdrv
;
480 device_remove_file(dev
, &dev_attr_online
);
481 device_remove_file(dev
, &dev_attr_ungroup
);
486 gdev
= to_ccwgroupdev(dev
);
487 gdrv
= to_ccwgroupdrv(dev
->driver
);
495 static void ccwgroup_shutdown(struct device
*dev
)
497 struct ccwgroup_device
*gdev
;
498 struct ccwgroup_driver
*gdrv
;
503 gdev
= to_ccwgroupdev(dev
);
504 gdrv
= to_ccwgroupdrv(dev
->driver
);
507 gdrv
->shutdown(gdev
);
510 static int ccwgroup_pm_prepare(struct device
*dev
)
512 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
513 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
515 /* Fail while device is being set online/offline. */
516 if (atomic_read(&gdev
->onoff
))
519 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
522 return gdrv
->prepare
? gdrv
->prepare(gdev
) : 0;
525 static void ccwgroup_pm_complete(struct device
*dev
)
527 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
528 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(dev
->driver
);
530 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
534 gdrv
->complete(gdev
);
537 static int ccwgroup_pm_freeze(struct device
*dev
)
539 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
540 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
542 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
545 return gdrv
->freeze
? gdrv
->freeze(gdev
) : 0;
548 static int ccwgroup_pm_thaw(struct device
*dev
)
550 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
551 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
553 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
556 return gdrv
->thaw
? gdrv
->thaw(gdev
) : 0;
559 static int ccwgroup_pm_restore(struct device
*dev
)
561 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
562 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
564 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
567 return gdrv
->restore
? gdrv
->restore(gdev
) : 0;
570 static const struct dev_pm_ops ccwgroup_pm_ops
= {
571 .prepare
= ccwgroup_pm_prepare
,
572 .complete
= ccwgroup_pm_complete
,
573 .freeze
= ccwgroup_pm_freeze
,
574 .thaw
= ccwgroup_pm_thaw
,
575 .restore
= ccwgroup_pm_restore
,
578 static struct bus_type ccwgroup_bus_type
= {
580 .match
= ccwgroup_bus_match
,
581 .uevent
= ccwgroup_uevent
,
582 .probe
= ccwgroup_probe
,
583 .remove
= ccwgroup_remove
,
584 .shutdown
= ccwgroup_shutdown
,
585 .pm
= &ccwgroup_pm_ops
,
589 static int ccwgroup_notifier(struct notifier_block
*nb
, unsigned long action
,
592 struct device
*dev
= data
;
594 if (action
== BUS_NOTIFY_UNBIND_DRIVER
)
595 device_schedule_callback(dev
, ccwgroup_ungroup_callback
);
602 * ccwgroup_driver_register() - register a ccw group driver
603 * @cdriver: driver to be registered
605 * This function is mainly a wrapper around driver_register().
607 int ccwgroup_driver_register(struct ccwgroup_driver
*cdriver
)
609 /* register our new driver with the core */
610 cdriver
->driver
.bus
= &ccwgroup_bus_type
;
611 cdriver
->driver
.name
= cdriver
->name
;
612 cdriver
->driver
.owner
= cdriver
->owner
;
614 return driver_register(&cdriver
->driver
);
618 __ccwgroup_match_all(struct device
*dev
, void *data
)
624 * ccwgroup_driver_unregister() - deregister a ccw group driver
625 * @cdriver: driver to be deregistered
627 * This function is mainly a wrapper around driver_unregister().
629 void ccwgroup_driver_unregister(struct ccwgroup_driver
*cdriver
)
633 /* We don't want ccwgroup devices to live longer than their driver. */
634 get_driver(&cdriver
->driver
);
635 while ((dev
= driver_find_device(&cdriver
->driver
, NULL
, NULL
,
636 __ccwgroup_match_all
))) {
637 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
639 mutex_lock(&gdev
->reg_mutex
);
640 __ccwgroup_remove_symlinks(gdev
);
641 device_unregister(dev
);
642 mutex_unlock(&gdev
->reg_mutex
);
645 put_driver(&cdriver
->driver
);
646 driver_unregister(&cdriver
->driver
);
650 * ccwgroup_probe_ccwdev() - probe function for slave devices
651 * @cdev: ccw device to be probed
653 * This is a dummy probe function for ccw devices that are slave devices in
654 * a ccw group device.
658 int ccwgroup_probe_ccwdev(struct ccw_device
*cdev
)
663 static struct ccwgroup_device
*
664 __ccwgroup_get_gdev_by_cdev(struct ccw_device
*cdev
)
666 struct ccwgroup_device
*gdev
;
668 gdev
= dev_get_drvdata(&cdev
->dev
);
670 if (get_device(&gdev
->dev
)) {
671 mutex_lock(&gdev
->reg_mutex
);
672 if (device_is_registered(&gdev
->dev
))
674 mutex_unlock(&gdev
->reg_mutex
);
675 put_device(&gdev
->dev
);
683 * ccwgroup_remove_ccwdev() - remove function for slave devices
684 * @cdev: ccw device to be removed
686 * This is a remove function for ccw devices that are slave devices in a ccw
687 * group device. It sets the ccw device offline and also deregisters the
688 * embedding ccw group device.
690 void ccwgroup_remove_ccwdev(struct ccw_device
*cdev
)
692 struct ccwgroup_device
*gdev
;
694 /* Ignore offlining errors, device is gone anyway. */
695 ccw_device_set_offline(cdev
);
696 /* If one of its devices is gone, the whole group is done for. */
697 gdev
= __ccwgroup_get_gdev_by_cdev(cdev
);
699 __ccwgroup_remove_symlinks(gdev
);
700 device_unregister(&gdev
->dev
);
701 mutex_unlock(&gdev
->reg_mutex
);
702 put_device(&gdev
->dev
);
706 MODULE_LICENSE("GPL");
707 EXPORT_SYMBOL(ccwgroup_driver_register
);
708 EXPORT_SYMBOL(ccwgroup_driver_unregister
);
709 EXPORT_SYMBOL(ccwgroup_probe_ccwdev
);
710 EXPORT_SYMBOL(ccwgroup_remove_ccwdev
);