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 */
32 static int ccwgroup_bus_match(struct device
*dev
, struct device_driver
* drv
)
34 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
35 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(drv
);
37 if (gdev
->creator_id
== gdrv
->driver_id
)
43 static struct bus_type ccwgroup_bus_type
;
45 static void __ccwgroup_remove_symlinks(struct ccwgroup_device
*gdev
)
50 for (i
= 0; i
< gdev
->count
; i
++) {
51 sprintf(str
, "cdev%d", i
);
52 sysfs_remove_link(&gdev
->dev
.kobj
, str
);
53 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
, "group_device");
58 * Remove references from ccw devices to ccw group device and from
59 * ccw group device to ccw devices.
61 static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device
*gdev
)
63 struct ccw_device
*cdev
;
66 for (i
= 0; i
< gdev
->count
; i
++) {
70 spin_lock_irq(cdev
->ccwlock
);
71 dev_set_drvdata(&cdev
->dev
, NULL
);
72 spin_unlock_irq(cdev
->ccwlock
);
74 put_device(&cdev
->dev
);
78 static int ccwgroup_set_online(struct ccwgroup_device
*gdev
)
80 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
83 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
85 if (gdev
->state
== CCWGROUP_ONLINE
)
88 ret
= gdrv
->set_online(gdev
);
92 gdev
->state
= CCWGROUP_ONLINE
;
94 atomic_set(&gdev
->onoff
, 0);
98 static int ccwgroup_set_offline(struct ccwgroup_device
*gdev
)
100 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
103 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
105 if (gdev
->state
== CCWGROUP_OFFLINE
)
107 if (gdrv
->set_offline
)
108 ret
= gdrv
->set_offline(gdev
);
112 gdev
->state
= CCWGROUP_OFFLINE
;
114 atomic_set(&gdev
->onoff
, 0);
118 static ssize_t
ccwgroup_online_store(struct device
*dev
,
119 struct device_attribute
*attr
,
120 const char *buf
, size_t count
)
122 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
123 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(dev
->driver
);
129 if (!try_module_get(gdrv
->driver
.owner
))
132 ret
= strict_strtoul(buf
, 0, &value
);
137 ret
= ccwgroup_set_online(gdev
);
139 ret
= ccwgroup_set_offline(gdev
);
143 module_put(gdrv
->driver
.owner
);
144 return (ret
== 0) ? count
: ret
;
147 static ssize_t
ccwgroup_online_show(struct device
*dev
,
148 struct device_attribute
*attr
,
151 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
154 online
= (gdev
->state
== CCWGROUP_ONLINE
) ? 1 : 0;
156 return scnprintf(buf
, PAGE_SIZE
, "%d\n", online
);
160 * Provide an 'ungroup' attribute so the user can remove group devices no
161 * longer needed or accidentially created. Saves memory :)
163 static void ccwgroup_ungroup_callback(struct device
*dev
)
165 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
167 mutex_lock(&gdev
->reg_mutex
);
168 if (device_is_registered(&gdev
->dev
)) {
169 __ccwgroup_remove_symlinks(gdev
);
170 device_unregister(dev
);
171 __ccwgroup_remove_cdev_refs(gdev
);
173 mutex_unlock(&gdev
->reg_mutex
);
176 static ssize_t
ccwgroup_ungroup_store(struct device
*dev
,
177 struct device_attribute
*attr
,
178 const char *buf
, size_t count
)
180 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
183 /* Prevent concurrent online/offline processing and ungrouping. */
184 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
186 if (gdev
->state
!= CCWGROUP_OFFLINE
) {
190 /* Note that we cannot unregister the device from one of its
191 * attribute methods, so we have to use this roundabout approach.
193 rc
= device_schedule_callback(dev
, ccwgroup_ungroup_callback
);
197 /* Release onoff "lock" when ungrouping failed. */
198 atomic_set(&gdev
->onoff
, 0);
203 static DEVICE_ATTR(ungroup
, 0200, NULL
, ccwgroup_ungroup_store
);
204 static DEVICE_ATTR(online
, 0644, ccwgroup_online_show
, ccwgroup_online_store
);
206 static struct attribute
*ccwgroup_attrs
[] = {
207 &dev_attr_online
.attr
,
208 &dev_attr_ungroup
.attr
,
211 static struct attribute_group ccwgroup_attr_group
= {
212 .attrs
= ccwgroup_attrs
,
214 static const struct attribute_group
*ccwgroup_attr_groups
[] = {
215 &ccwgroup_attr_group
,
219 static void ccwgroup_release(struct device
*dev
)
221 kfree(to_ccwgroupdev(dev
));
224 static int __ccwgroup_create_symlinks(struct ccwgroup_device
*gdev
)
229 for (i
= 0; i
< gdev
->count
; i
++) {
230 rc
= sysfs_create_link(&gdev
->cdev
[i
]->dev
.kobj
,
231 &gdev
->dev
.kobj
, "group_device");
233 for (--i
; i
>= 0; i
--)
234 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
,
239 for (i
= 0; i
< gdev
->count
; i
++) {
240 sprintf(str
, "cdev%d", i
);
241 rc
= sysfs_create_link(&gdev
->dev
.kobj
,
242 &gdev
->cdev
[i
]->dev
.kobj
, str
);
244 for (--i
; i
>= 0; i
--) {
245 sprintf(str
, "cdev%d", i
);
246 sysfs_remove_link(&gdev
->dev
.kobj
, str
);
248 for (i
= 0; i
< gdev
->count
; i
++)
249 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
,
257 static int __get_next_bus_id(const char **buf
, char *bus_id
)
262 start
= (char *)*buf
;
263 end
= strchr(start
, ',');
265 /* Last entry. Strip trailing newline, if applicable. */
266 end
= strchr(start
, '\n');
269 len
= strlen(start
) + 1;
271 len
= end
- start
+ 1;
274 if (len
< CCW_BUS_ID_SIZE
) {
275 strlcpy(bus_id
, start
, len
);
283 static int __is_valid_bus_id(char bus_id
[CCW_BUS_ID_SIZE
])
285 int cssid
, ssid
, devno
;
287 /* Must be of form %x.%x.%04x */
288 if (sscanf(bus_id
, "%x.%1x.%04x", &cssid
, &ssid
, &devno
) != 3)
294 * ccwgroup_create_from_string() - create and register a ccw group device
295 * @root: parent device for the new device
296 * @creator_id: identifier of creating driver
297 * @cdrv: ccw driver of slave devices
298 * @num_devices: number of slave devices
299 * @buf: buffer containing comma separated bus ids of slave devices
301 * Create and register a new ccw group device as a child of @root. Slave
302 * devices are obtained from the list of bus ids given in @buf and must all
305 * %0 on success and an error code on failure.
309 int ccwgroup_create_from_string(struct device
*root
, unsigned int creator_id
,
310 struct ccw_driver
*cdrv
, int num_devices
,
313 struct ccwgroup_device
*gdev
;
315 char tmp_bus_id
[CCW_BUS_ID_SIZE
];
316 const char *curr_buf
;
318 gdev
= kzalloc(sizeof(*gdev
) + num_devices
* sizeof(gdev
->cdev
[0]),
323 atomic_set(&gdev
->onoff
, 0);
324 mutex_init(&gdev
->reg_mutex
);
325 mutex_lock(&gdev
->reg_mutex
);
326 gdev
->creator_id
= creator_id
;
327 gdev
->count
= num_devices
;
328 gdev
->dev
.bus
= &ccwgroup_bus_type
;
329 gdev
->dev
.parent
= root
;
330 gdev
->dev
.release
= ccwgroup_release
;
331 device_initialize(&gdev
->dev
);
334 for (i
= 0; i
< num_devices
&& curr_buf
; i
++) {
335 rc
= __get_next_bus_id(&curr_buf
, tmp_bus_id
);
338 if (!__is_valid_bus_id(tmp_bus_id
)) {
342 gdev
->cdev
[i
] = get_ccwdev_by_busid(cdrv
, tmp_bus_id
);
344 * All devices have to be of the same type in
345 * order to be grouped.
348 || gdev
->cdev
[i
]->id
.driver_info
!=
349 gdev
->cdev
[0]->id
.driver_info
) {
353 /* Don't allow a device to belong to more than one group. */
354 spin_lock_irq(gdev
->cdev
[i
]->ccwlock
);
355 if (dev_get_drvdata(&gdev
->cdev
[i
]->dev
)) {
356 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
360 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, gdev
);
361 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
363 /* Check for sufficient number of bus ids. */
364 if (i
< num_devices
&& !curr_buf
) {
368 /* Check for trailing stuff. */
369 if (i
== num_devices
&& strlen(curr_buf
) > 0) {
374 dev_set_name(&gdev
->dev
, "%s", dev_name(&gdev
->cdev
[0]->dev
));
375 gdev
->dev
.groups
= ccwgroup_attr_groups
;
376 rc
= device_add(&gdev
->dev
);
379 rc
= __ccwgroup_create_symlinks(gdev
);
381 device_del(&gdev
->dev
);
384 mutex_unlock(&gdev
->reg_mutex
);
387 for (i
= 0; i
< num_devices
; i
++)
389 spin_lock_irq(gdev
->cdev
[i
]->ccwlock
);
390 if (dev_get_drvdata(&gdev
->cdev
[i
]->dev
) == gdev
)
391 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, NULL
);
392 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
393 put_device(&gdev
->cdev
[i
]->dev
);
394 gdev
->cdev
[i
] = NULL
;
396 mutex_unlock(&gdev
->reg_mutex
);
397 put_device(&gdev
->dev
);
400 EXPORT_SYMBOL(ccwgroup_create_from_string
);
402 static int ccwgroup_notifier(struct notifier_block
*nb
, unsigned long action
,
405 struct device
*dev
= data
;
407 if (action
== BUS_NOTIFY_UNBIND_DRIVER
)
408 device_schedule_callback(dev
, ccwgroup_ungroup_callback
);
413 static struct notifier_block ccwgroup_nb
= {
414 .notifier_call
= ccwgroup_notifier
417 static int __init
init_ccwgroup(void)
421 ret
= bus_register(&ccwgroup_bus_type
);
425 ret
= bus_register_notifier(&ccwgroup_bus_type
, &ccwgroup_nb
);
427 bus_unregister(&ccwgroup_bus_type
);
432 static void __exit
cleanup_ccwgroup(void)
434 bus_unregister_notifier(&ccwgroup_bus_type
, &ccwgroup_nb
);
435 bus_unregister(&ccwgroup_bus_type
);
438 module_init(init_ccwgroup
);
439 module_exit(cleanup_ccwgroup
);
441 /************************** driver stuff ******************************/
443 static int ccwgroup_probe(struct device
*dev
)
445 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
446 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(dev
->driver
);
448 return gdrv
->probe
? gdrv
->probe(gdev
) : -ENODEV
;
451 static int ccwgroup_remove(struct device
*dev
)
453 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
454 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(dev
->driver
);
464 static void ccwgroup_shutdown(struct device
*dev
)
466 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
467 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(dev
->driver
);
472 gdrv
->shutdown(gdev
);
475 static int ccwgroup_pm_prepare(struct device
*dev
)
477 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
478 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
480 /* Fail while device is being set online/offline. */
481 if (atomic_read(&gdev
->onoff
))
484 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
487 return gdrv
->prepare
? gdrv
->prepare(gdev
) : 0;
490 static void ccwgroup_pm_complete(struct device
*dev
)
492 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
493 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(dev
->driver
);
495 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
499 gdrv
->complete(gdev
);
502 static int ccwgroup_pm_freeze(struct device
*dev
)
504 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
505 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
507 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
510 return gdrv
->freeze
? gdrv
->freeze(gdev
) : 0;
513 static int ccwgroup_pm_thaw(struct device
*dev
)
515 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
516 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
518 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
521 return gdrv
->thaw
? gdrv
->thaw(gdev
) : 0;
524 static int ccwgroup_pm_restore(struct device
*dev
)
526 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
527 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
529 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
532 return gdrv
->restore
? gdrv
->restore(gdev
) : 0;
535 static const struct dev_pm_ops ccwgroup_pm_ops
= {
536 .prepare
= ccwgroup_pm_prepare
,
537 .complete
= ccwgroup_pm_complete
,
538 .freeze
= ccwgroup_pm_freeze
,
539 .thaw
= ccwgroup_pm_thaw
,
540 .restore
= ccwgroup_pm_restore
,
543 static struct bus_type ccwgroup_bus_type
= {
545 .match
= ccwgroup_bus_match
,
546 .probe
= ccwgroup_probe
,
547 .remove
= ccwgroup_remove
,
548 .shutdown
= ccwgroup_shutdown
,
549 .pm
= &ccwgroup_pm_ops
,
553 * ccwgroup_driver_register() - register a ccw group driver
554 * @cdriver: driver to be registered
556 * This function is mainly a wrapper around driver_register().
558 int ccwgroup_driver_register(struct ccwgroup_driver
*cdriver
)
560 /* register our new driver with the core */
561 cdriver
->driver
.bus
= &ccwgroup_bus_type
;
563 return driver_register(&cdriver
->driver
);
565 EXPORT_SYMBOL(ccwgroup_driver_register
);
567 static int __ccwgroup_match_all(struct device
*dev
, void *data
)
573 * ccwgroup_driver_unregister() - deregister a ccw group driver
574 * @cdriver: driver to be deregistered
576 * This function is mainly a wrapper around driver_unregister().
578 void ccwgroup_driver_unregister(struct ccwgroup_driver
*cdriver
)
582 /* We don't want ccwgroup devices to live longer than their driver. */
583 get_driver(&cdriver
->driver
);
584 while ((dev
= driver_find_device(&cdriver
->driver
, NULL
, NULL
,
585 __ccwgroup_match_all
))) {
586 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
588 mutex_lock(&gdev
->reg_mutex
);
589 __ccwgroup_remove_symlinks(gdev
);
590 device_unregister(dev
);
591 __ccwgroup_remove_cdev_refs(gdev
);
592 mutex_unlock(&gdev
->reg_mutex
);
595 put_driver(&cdriver
->driver
);
596 driver_unregister(&cdriver
->driver
);
598 EXPORT_SYMBOL(ccwgroup_driver_unregister
);
601 * ccwgroup_probe_ccwdev() - probe function for slave devices
602 * @cdev: ccw device to be probed
604 * This is a dummy probe function for ccw devices that are slave devices in
605 * a ccw group device.
609 int ccwgroup_probe_ccwdev(struct ccw_device
*cdev
)
613 EXPORT_SYMBOL(ccwgroup_probe_ccwdev
);
616 * ccwgroup_remove_ccwdev() - remove function for slave devices
617 * @cdev: ccw device to be removed
619 * This is a remove function for ccw devices that are slave devices in a ccw
620 * group device. It sets the ccw device offline and also deregisters the
621 * embedding ccw group device.
623 void ccwgroup_remove_ccwdev(struct ccw_device
*cdev
)
625 struct ccwgroup_device
*gdev
;
627 /* Ignore offlining errors, device is gone anyway. */
628 ccw_device_set_offline(cdev
);
629 /* If one of its devices is gone, the whole group is done for. */
630 spin_lock_irq(cdev
->ccwlock
);
631 gdev
= dev_get_drvdata(&cdev
->dev
);
633 spin_unlock_irq(cdev
->ccwlock
);
636 /* Get ccwgroup device reference for local processing. */
637 get_device(&gdev
->dev
);
638 spin_unlock_irq(cdev
->ccwlock
);
639 /* Unregister group device. */
640 mutex_lock(&gdev
->reg_mutex
);
641 if (device_is_registered(&gdev
->dev
)) {
642 __ccwgroup_remove_symlinks(gdev
);
643 device_unregister(&gdev
->dev
);
644 __ccwgroup_remove_cdev_refs(gdev
);
646 mutex_unlock(&gdev
->reg_mutex
);
647 /* Release ccwgroup device reference for local processing. */
648 put_device(&gdev
->dev
);
650 EXPORT_SYMBOL(ccwgroup_remove_ccwdev
);
651 MODULE_LICENSE("GPL");