1 // SPDX-License-Identifier: GPL-2.0
3 * bus driver for ccwgroup
5 * Copyright IBM Corp. 2002, 2012
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>
20 #include <asm/ccwdev.h>
21 #include <asm/ccwgroup.h>
25 #define CCW_BUS_ID_SIZE 10
27 /* In Linux 2.4, we had a channel device layer called "chandev"
28 * that did all sorts of obscure stuff for networking devices.
29 * This is another driver that serves as a replacement for just
30 * one of its functions, namely the translation of single subchannels
31 * to devices that use multiple subchannels.
34 static struct bus_type ccwgroup_bus_type
;
36 static void __ccwgroup_remove_symlinks(struct ccwgroup_device
*gdev
)
41 for (i
= 0; i
< gdev
->count
; i
++) {
42 sprintf(str
, "cdev%d", i
);
43 sysfs_remove_link(&gdev
->dev
.kobj
, str
);
44 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
, "group_device");
49 * Remove references from ccw devices to ccw group device and from
50 * ccw group device to ccw devices.
52 static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device
*gdev
)
54 struct ccw_device
*cdev
;
57 for (i
= 0; i
< gdev
->count
; i
++) {
61 spin_lock_irq(cdev
->ccwlock
);
62 dev_set_drvdata(&cdev
->dev
, NULL
);
63 spin_unlock_irq(cdev
->ccwlock
);
65 put_device(&cdev
->dev
);
70 * ccwgroup_set_online() - enable a ccwgroup device
71 * @gdev: target ccwgroup device
73 * This function attempts to put the ccwgroup device into the online state.
75 * %0 on success and a negative error value on failure.
77 int ccwgroup_set_online(struct ccwgroup_device
*gdev
)
79 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
82 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
84 if (gdev
->state
== CCWGROUP_ONLINE
)
87 ret
= gdrv
->set_online(gdev
);
91 gdev
->state
= CCWGROUP_ONLINE
;
93 atomic_set(&gdev
->onoff
, 0);
96 EXPORT_SYMBOL(ccwgroup_set_online
);
99 * ccwgroup_set_offline() - disable a ccwgroup device
100 * @gdev: target ccwgroup device
102 * This function attempts to put the ccwgroup device into the offline state.
104 * %0 on success and a negative error value on failure.
106 int ccwgroup_set_offline(struct ccwgroup_device
*gdev
)
108 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
111 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
113 if (gdev
->state
== CCWGROUP_OFFLINE
)
115 if (gdrv
->set_offline
)
116 ret
= gdrv
->set_offline(gdev
);
120 gdev
->state
= CCWGROUP_OFFLINE
;
122 atomic_set(&gdev
->onoff
, 0);
125 EXPORT_SYMBOL(ccwgroup_set_offline
);
127 static ssize_t
ccwgroup_online_store(struct device
*dev
,
128 struct device_attribute
*attr
,
129 const char *buf
, size_t count
)
131 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
141 ret
= kstrtoul(buf
, 0, &value
);
146 ret
= ccwgroup_set_online(gdev
);
148 ret
= ccwgroup_set_offline(gdev
);
153 return (ret
== 0) ? count
: ret
;
156 static ssize_t
ccwgroup_online_show(struct device
*dev
,
157 struct device_attribute
*attr
,
160 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
163 online
= (gdev
->state
== CCWGROUP_ONLINE
) ? 1 : 0;
165 return scnprintf(buf
, PAGE_SIZE
, "%d\n", online
);
169 * Provide an 'ungroup' attribute so the user can remove group devices no
170 * longer needed or accidentially created. Saves memory :)
172 static void ccwgroup_ungroup(struct ccwgroup_device
*gdev
)
174 mutex_lock(&gdev
->reg_mutex
);
175 if (device_is_registered(&gdev
->dev
)) {
176 __ccwgroup_remove_symlinks(gdev
);
177 device_unregister(&gdev
->dev
);
178 __ccwgroup_remove_cdev_refs(gdev
);
180 mutex_unlock(&gdev
->reg_mutex
);
183 static ssize_t
ccwgroup_ungroup_store(struct device
*dev
,
184 struct device_attribute
*attr
,
185 const char *buf
, size_t count
)
187 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
190 /* Prevent concurrent online/offline processing and ungrouping. */
191 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
193 if (gdev
->state
!= CCWGROUP_OFFLINE
) {
198 if (device_remove_file_self(dev
, attr
))
199 ccwgroup_ungroup(gdev
);
204 /* Release onoff "lock" when ungrouping failed. */
205 atomic_set(&gdev
->onoff
, 0);
210 static DEVICE_ATTR(ungroup
, 0200, NULL
, ccwgroup_ungroup_store
);
211 static DEVICE_ATTR(online
, 0644, ccwgroup_online_show
, ccwgroup_online_store
);
213 static struct attribute
*ccwgroup_attrs
[] = {
214 &dev_attr_online
.attr
,
215 &dev_attr_ungroup
.attr
,
218 static struct attribute_group ccwgroup_attr_group
= {
219 .attrs
= ccwgroup_attrs
,
221 static const struct attribute_group
*ccwgroup_attr_groups
[] = {
222 &ccwgroup_attr_group
,
226 static void ccwgroup_ungroup_workfn(struct work_struct
*work
)
228 struct ccwgroup_device
*gdev
=
229 container_of(work
, struct ccwgroup_device
, ungroup_work
);
231 ccwgroup_ungroup(gdev
);
232 put_device(&gdev
->dev
);
235 static void ccwgroup_release(struct device
*dev
)
237 kfree(to_ccwgroupdev(dev
));
240 static int __ccwgroup_create_symlinks(struct ccwgroup_device
*gdev
)
245 for (i
= 0; i
< gdev
->count
; i
++) {
246 rc
= sysfs_create_link(&gdev
->cdev
[i
]->dev
.kobj
,
247 &gdev
->dev
.kobj
, "group_device");
249 for (--i
; i
>= 0; i
--)
250 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
,
255 for (i
= 0; i
< gdev
->count
; i
++) {
256 sprintf(str
, "cdev%d", i
);
257 rc
= sysfs_create_link(&gdev
->dev
.kobj
,
258 &gdev
->cdev
[i
]->dev
.kobj
, str
);
260 for (--i
; i
>= 0; i
--) {
261 sprintf(str
, "cdev%d", i
);
262 sysfs_remove_link(&gdev
->dev
.kobj
, str
);
264 for (i
= 0; i
< gdev
->count
; i
++)
265 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
,
273 static int __get_next_id(const char **buf
, struct ccw_dev_id
*id
)
275 unsigned int cssid
, ssid
, devno
;
279 start
= (char *)*buf
;
280 end
= strchr(start
, ',');
282 /* Last entry. Strip trailing newline, if applicable. */
283 end
= strchr(start
, '\n');
286 len
= strlen(start
) + 1;
288 len
= end
- start
+ 1;
291 if (len
<= CCW_BUS_ID_SIZE
) {
292 if (sscanf(start
, "%2x.%1x.%04x", &cssid
, &ssid
, &devno
) != 3)
306 * ccwgroup_create_dev() - create and register a ccw group device
307 * @parent: parent device for the new device
308 * @gdrv: driver for the new group device
309 * @num_devices: number of slave devices
310 * @buf: buffer containing comma separated bus ids of slave devices
312 * Create and register a new ccw group device as a child of @parent. Slave
313 * devices are obtained from the list of bus ids given in @buf.
315 * %0 on success and an error code on failure.
319 int ccwgroup_create_dev(struct device
*parent
, struct ccwgroup_driver
*gdrv
,
320 int num_devices
, const char *buf
)
322 struct ccwgroup_device
*gdev
;
323 struct ccw_dev_id dev_id
;
329 gdev
= kzalloc(struct_size(gdev
, cdev
, num_devices
), GFP_KERNEL
);
333 atomic_set(&gdev
->onoff
, 0);
334 mutex_init(&gdev
->reg_mutex
);
335 mutex_lock(&gdev
->reg_mutex
);
336 INIT_WORK(&gdev
->ungroup_work
, ccwgroup_ungroup_workfn
);
337 gdev
->count
= num_devices
;
338 gdev
->dev
.bus
= &ccwgroup_bus_type
;
339 gdev
->dev
.parent
= parent
;
340 gdev
->dev
.release
= ccwgroup_release
;
341 device_initialize(&gdev
->dev
);
343 for (i
= 0; i
< num_devices
&& buf
; i
++) {
344 rc
= __get_next_id(&buf
, &dev_id
);
347 gdev
->cdev
[i
] = get_ccwdev_by_dev_id(&dev_id
);
349 * All devices have to be of the same type in
350 * order to be grouped.
352 if (!gdev
->cdev
[i
] || !gdev
->cdev
[i
]->drv
||
353 gdev
->cdev
[i
]->drv
!= gdev
->cdev
[0]->drv
||
354 gdev
->cdev
[i
]->id
.driver_info
!=
355 gdev
->cdev
[0]->id
.driver_info
) {
359 /* Don't allow a device to belong to more than one group. */
360 spin_lock_irq(gdev
->cdev
[i
]->ccwlock
);
361 if (dev_get_drvdata(&gdev
->cdev
[i
]->dev
)) {
362 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
366 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, gdev
);
367 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
369 /* Check for sufficient number of bus ids. */
370 if (i
< num_devices
) {
374 /* Check for trailing stuff. */
375 if (i
== num_devices
&& strlen(buf
) > 0) {
379 /* Check if the devices are bound to the required ccw driver. */
380 if (gdrv
&& gdrv
->ccw_driver
&&
381 gdev
->cdev
[0]->drv
!= gdrv
->ccw_driver
) {
386 dev_set_name(&gdev
->dev
, "%s", dev_name(&gdev
->cdev
[0]->dev
));
387 gdev
->dev
.groups
= ccwgroup_attr_groups
;
390 gdev
->dev
.driver
= &gdrv
->driver
;
391 rc
= gdrv
->setup
? gdrv
->setup(gdev
) : 0;
395 rc
= device_add(&gdev
->dev
);
398 rc
= __ccwgroup_create_symlinks(gdev
);
400 device_del(&gdev
->dev
);
403 mutex_unlock(&gdev
->reg_mutex
);
406 for (i
= 0; i
< num_devices
; i
++)
408 spin_lock_irq(gdev
->cdev
[i
]->ccwlock
);
409 if (dev_get_drvdata(&gdev
->cdev
[i
]->dev
) == gdev
)
410 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, NULL
);
411 spin_unlock_irq(gdev
->cdev
[i
]->ccwlock
);
412 put_device(&gdev
->cdev
[i
]->dev
);
413 gdev
->cdev
[i
] = NULL
;
415 mutex_unlock(&gdev
->reg_mutex
);
416 put_device(&gdev
->dev
);
419 EXPORT_SYMBOL(ccwgroup_create_dev
);
421 static int ccwgroup_notifier(struct notifier_block
*nb
, unsigned long action
,
424 struct ccwgroup_device
*gdev
= to_ccwgroupdev(data
);
426 if (action
== BUS_NOTIFY_UNBIND_DRIVER
) {
427 get_device(&gdev
->dev
);
428 schedule_work(&gdev
->ungroup_work
);
434 static struct notifier_block ccwgroup_nb
= {
435 .notifier_call
= ccwgroup_notifier
438 static int __init
init_ccwgroup(void)
442 ret
= bus_register(&ccwgroup_bus_type
);
446 ret
= bus_register_notifier(&ccwgroup_bus_type
, &ccwgroup_nb
);
448 bus_unregister(&ccwgroup_bus_type
);
453 static void __exit
cleanup_ccwgroup(void)
455 bus_unregister_notifier(&ccwgroup_bus_type
, &ccwgroup_nb
);
456 bus_unregister(&ccwgroup_bus_type
);
459 module_init(init_ccwgroup
);
460 module_exit(cleanup_ccwgroup
);
462 /************************** driver stuff ******************************/
464 static int ccwgroup_remove(struct device
*dev
)
466 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
467 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(dev
->driver
);
477 static void ccwgroup_shutdown(struct device
*dev
)
479 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
480 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(dev
->driver
);
485 gdrv
->shutdown(gdev
);
488 static int ccwgroup_pm_prepare(struct device
*dev
)
490 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
491 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
493 /* Fail while device is being set online/offline. */
494 if (atomic_read(&gdev
->onoff
))
497 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
500 return gdrv
->prepare
? gdrv
->prepare(gdev
) : 0;
503 static void ccwgroup_pm_complete(struct device
*dev
)
505 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
506 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(dev
->driver
);
508 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
512 gdrv
->complete(gdev
);
515 static int ccwgroup_pm_freeze(struct device
*dev
)
517 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
518 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
520 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
523 return gdrv
->freeze
? gdrv
->freeze(gdev
) : 0;
526 static int ccwgroup_pm_thaw(struct device
*dev
)
528 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
529 struct ccwgroup_driver
*gdrv
= to_ccwgroupdrv(gdev
->dev
.driver
);
531 if (!gdev
->dev
.driver
|| gdev
->state
!= CCWGROUP_ONLINE
)
534 return gdrv
->thaw
? gdrv
->thaw(gdev
) : 0;
537 static int ccwgroup_pm_restore(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
->restore
? gdrv
->restore(gdev
) : 0;
548 static const struct dev_pm_ops ccwgroup_pm_ops
= {
549 .prepare
= ccwgroup_pm_prepare
,
550 .complete
= ccwgroup_pm_complete
,
551 .freeze
= ccwgroup_pm_freeze
,
552 .thaw
= ccwgroup_pm_thaw
,
553 .restore
= ccwgroup_pm_restore
,
556 static struct bus_type ccwgroup_bus_type
= {
558 .remove
= ccwgroup_remove
,
559 .shutdown
= ccwgroup_shutdown
,
560 .pm
= &ccwgroup_pm_ops
,
563 bool dev_is_ccwgroup(struct device
*dev
)
565 return dev
->bus
== &ccwgroup_bus_type
;
567 EXPORT_SYMBOL(dev_is_ccwgroup
);
570 * ccwgroup_driver_register() - register a ccw group driver
571 * @cdriver: driver to be registered
573 * This function is mainly a wrapper around driver_register().
575 int ccwgroup_driver_register(struct ccwgroup_driver
*cdriver
)
577 /* register our new driver with the core */
578 cdriver
->driver
.bus
= &ccwgroup_bus_type
;
580 return driver_register(&cdriver
->driver
);
582 EXPORT_SYMBOL(ccwgroup_driver_register
);
584 static int __ccwgroup_match_all(struct device
*dev
, void *data
)
590 * ccwgroup_driver_unregister() - deregister a ccw group driver
591 * @cdriver: driver to be deregistered
593 * This function is mainly a wrapper around driver_unregister().
595 void ccwgroup_driver_unregister(struct ccwgroup_driver
*cdriver
)
599 /* We don't want ccwgroup devices to live longer than their driver. */
600 while ((dev
= driver_find_device(&cdriver
->driver
, NULL
, NULL
,
601 __ccwgroup_match_all
))) {
602 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
604 ccwgroup_ungroup(gdev
);
607 driver_unregister(&cdriver
->driver
);
609 EXPORT_SYMBOL(ccwgroup_driver_unregister
);
612 * ccwgroup_probe_ccwdev() - probe function for slave devices
613 * @cdev: ccw device to be probed
615 * This is a dummy probe function for ccw devices that are slave devices in
616 * a ccw group device.
620 int ccwgroup_probe_ccwdev(struct ccw_device
*cdev
)
624 EXPORT_SYMBOL(ccwgroup_probe_ccwdev
);
627 * ccwgroup_remove_ccwdev() - remove function for slave devices
628 * @cdev: ccw device to be removed
630 * This is a remove function for ccw devices that are slave devices in a ccw
631 * group device. It sets the ccw device offline and also deregisters the
632 * embedding ccw group device.
634 void ccwgroup_remove_ccwdev(struct ccw_device
*cdev
)
636 struct ccwgroup_device
*gdev
;
638 /* Ignore offlining errors, device is gone anyway. */
639 ccw_device_set_offline(cdev
);
640 /* If one of its devices is gone, the whole group is done for. */
641 spin_lock_irq(cdev
->ccwlock
);
642 gdev
= dev_get_drvdata(&cdev
->dev
);
644 spin_unlock_irq(cdev
->ccwlock
);
647 /* Get ccwgroup device reference for local processing. */
648 get_device(&gdev
->dev
);
649 spin_unlock_irq(cdev
->ccwlock
);
650 /* Unregister group device. */
651 ccwgroup_ungroup(gdev
);
652 /* Release ccwgroup device reference for local processing. */
653 put_device(&gdev
->dev
);
655 EXPORT_SYMBOL(ccwgroup_remove_ccwdev
);
656 MODULE_LICENSE("GPL");