2 * drivers/s390/cio/ccwgroup.c
3 * bus driver for ccwgroup
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
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>
19 #include <asm/semaphore.h>
20 #include <asm/ccwdev.h>
21 #include <asm/ccwgroup.h>
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 if (gdev
->state
!= CCWGROUP_OFFLINE
)
96 /* Note that we cannot unregister the device from one of its
97 * attribute methods, so we have to use this roundabout approach.
99 rc
= device_schedule_callback(dev
, ccwgroup_ungroup_callback
);
105 static DEVICE_ATTR(ungroup
, 0200, NULL
, ccwgroup_ungroup_store
);
108 ccwgroup_release (struct device
*dev
)
110 struct ccwgroup_device
*gdev
;
113 gdev
= to_ccwgroupdev(dev
);
115 for (i
= 0; i
< gdev
->count
; i
++) {
116 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, NULL
);
117 put_device(&gdev
->cdev
[i
]->dev
);
123 __ccwgroup_create_symlinks(struct ccwgroup_device
*gdev
)
128 for (i
= 0; i
< gdev
->count
; i
++) {
129 rc
= sysfs_create_link(&gdev
->cdev
[i
]->dev
.kobj
, &gdev
->dev
.kobj
,
132 for (--i
; i
>= 0; i
--)
133 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
,
138 for (i
= 0; i
< gdev
->count
; i
++) {
139 sprintf(str
, "cdev%d", i
);
140 rc
= sysfs_create_link(&gdev
->dev
.kobj
, &gdev
->cdev
[i
]->dev
.kobj
,
143 for (--i
; i
>= 0; i
--) {
144 sprintf(str
, "cdev%d", i
);
145 sysfs_remove_link(&gdev
->dev
.kobj
, str
);
147 for (i
= 0; i
< gdev
->count
; i
++)
148 sysfs_remove_link(&gdev
->cdev
[i
]->dev
.kobj
,
157 * ccwgroup_create() - create and register a ccw group device
158 * @root: parent device for the new device
159 * @creator_id: identifier of creating driver
160 * @cdrv: ccw driver of slave devices
161 * @argc: number of slave devices
162 * @argv: bus ids of slave devices
164 * Create and register a new ccw group device as a child of @root. Slave
165 * devices are obtained from the list of bus ids given in @argv[] and must all
168 * %0 on success and an error code on failure.
172 int ccwgroup_create(struct device
*root
, unsigned int creator_id
,
173 struct ccw_driver
*cdrv
, int argc
, char *argv
[])
175 struct ccwgroup_device
*gdev
;
179 if (argc
> 256) /* disallow dumb users */
182 gdev
= kzalloc(sizeof(*gdev
) + argc
*sizeof(gdev
->cdev
[0]), GFP_KERNEL
);
186 atomic_set(&gdev
->onoff
, 0);
187 mutex_init(&gdev
->reg_mutex
);
188 mutex_lock(&gdev
->reg_mutex
);
189 for (i
= 0; i
< argc
; i
++) {
190 gdev
->cdev
[i
] = get_ccwdev_by_busid(cdrv
, argv
[i
]);
192 /* all devices have to be of the same type in
193 * order to be grouped */
195 || gdev
->cdev
[i
]->id
.driver_info
!=
196 gdev
->cdev
[0]->id
.driver_info
) {
200 /* Don't allow a device to belong to more than one group. */
201 if (dev_get_drvdata(&gdev
->cdev
[i
]->dev
)) {
205 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, gdev
);
208 gdev
->creator_id
= creator_id
;
210 gdev
->dev
.bus
= &ccwgroup_bus_type
;
211 gdev
->dev
.parent
= root
;
212 gdev
->dev
.release
= ccwgroup_release
;
214 snprintf (gdev
->dev
.bus_id
, BUS_ID_SIZE
, "%s",
215 gdev
->cdev
[0]->dev
.bus_id
);
217 rc
= device_register(&gdev
->dev
);
220 get_device(&gdev
->dev
);
221 rc
= device_create_file(&gdev
->dev
, &dev_attr_ungroup
);
224 device_unregister(&gdev
->dev
);
228 rc
= __ccwgroup_create_symlinks(gdev
);
230 mutex_unlock(&gdev
->reg_mutex
);
231 put_device(&gdev
->dev
);
234 device_remove_file(&gdev
->dev
, &dev_attr_ungroup
);
235 device_unregister(&gdev
->dev
);
237 for (i
= 0; i
< argc
; i
++)
239 if (dev_get_drvdata(&gdev
->cdev
[i
]->dev
) == gdev
)
240 dev_set_drvdata(&gdev
->cdev
[i
]->dev
, NULL
);
241 put_device(&gdev
->cdev
[i
]->dev
);
243 mutex_unlock(&gdev
->reg_mutex
);
244 put_device(&gdev
->dev
);
251 return bus_register (&ccwgroup_bus_type
);
255 cleanup_ccwgroup (void)
257 bus_unregister (&ccwgroup_bus_type
);
260 module_init(init_ccwgroup
);
261 module_exit(cleanup_ccwgroup
);
263 /************************** driver stuff ******************************/
266 ccwgroup_set_online(struct ccwgroup_device
*gdev
)
268 struct ccwgroup_driver
*gdrv
;
271 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
273 if (gdev
->state
== CCWGROUP_ONLINE
) {
277 if (!gdev
->dev
.driver
) {
281 gdrv
= to_ccwgroupdrv (gdev
->dev
.driver
);
282 if ((ret
= gdrv
->set_online
? gdrv
->set_online(gdev
) : 0))
285 gdev
->state
= CCWGROUP_ONLINE
;
287 atomic_set(&gdev
->onoff
, 0);
292 ccwgroup_set_offline(struct ccwgroup_device
*gdev
)
294 struct ccwgroup_driver
*gdrv
;
297 if (atomic_cmpxchg(&gdev
->onoff
, 0, 1) != 0)
299 if (gdev
->state
== CCWGROUP_OFFLINE
) {
303 if (!gdev
->dev
.driver
) {
307 gdrv
= to_ccwgroupdrv (gdev
->dev
.driver
);
308 if ((ret
= gdrv
->set_offline
? gdrv
->set_offline(gdev
) : 0))
311 gdev
->state
= CCWGROUP_OFFLINE
;
313 atomic_set(&gdev
->onoff
, 0);
318 ccwgroup_online_store (struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
320 struct ccwgroup_device
*gdev
;
321 struct ccwgroup_driver
*gdrv
;
325 gdev
= to_ccwgroupdev(dev
);
329 gdrv
= to_ccwgroupdrv (gdev
->dev
.driver
);
330 if (!try_module_get(gdrv
->owner
))
333 value
= simple_strtoul(buf
, NULL
, 0);
336 ccwgroup_set_online(gdev
);
338 ccwgroup_set_offline(gdev
);
341 module_put(gdrv
->owner
);
346 ccwgroup_online_show (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
350 online
= (to_ccwgroupdev(dev
)->state
== CCWGROUP_ONLINE
);
352 return sprintf(buf
, online
? "1\n" : "0\n");
355 static DEVICE_ATTR(online
, 0644, ccwgroup_online_show
, ccwgroup_online_store
);
358 ccwgroup_probe (struct device
*dev
)
360 struct ccwgroup_device
*gdev
;
361 struct ccwgroup_driver
*gdrv
;
365 gdev
= to_ccwgroupdev(dev
);
366 gdrv
= to_ccwgroupdrv(dev
->driver
);
368 if ((ret
= device_create_file(dev
, &dev_attr_online
)))
371 ret
= gdrv
->probe
? gdrv
->probe(gdev
) : -ENODEV
;
373 device_remove_file(dev
, &dev_attr_online
);
379 ccwgroup_remove (struct device
*dev
)
381 struct ccwgroup_device
*gdev
;
382 struct ccwgroup_driver
*gdrv
;
384 gdev
= to_ccwgroupdev(dev
);
385 gdrv
= to_ccwgroupdrv(dev
->driver
);
387 device_remove_file(dev
, &dev_attr_online
);
389 if (gdrv
&& gdrv
->remove
)
394 static struct bus_type ccwgroup_bus_type
= {
396 .match
= ccwgroup_bus_match
,
397 .uevent
= ccwgroup_uevent
,
398 .probe
= ccwgroup_probe
,
399 .remove
= ccwgroup_remove
,
403 * ccwgroup_driver_register() - register a ccw group driver
404 * @cdriver: driver to be registered
406 * This function is mainly a wrapper around driver_register().
408 int ccwgroup_driver_register(struct ccwgroup_driver
*cdriver
)
410 /* register our new driver with the core */
411 cdriver
->driver
.bus
= &ccwgroup_bus_type
;
412 cdriver
->driver
.name
= cdriver
->name
;
413 cdriver
->driver
.owner
= cdriver
->owner
;
415 return driver_register(&cdriver
->driver
);
419 __ccwgroup_match_all(struct device
*dev
, void *data
)
425 * ccwgroup_driver_unregister() - deregister a ccw group driver
426 * @cdriver: driver to be deregistered
428 * This function is mainly a wrapper around driver_unregister().
430 void ccwgroup_driver_unregister(struct ccwgroup_driver
*cdriver
)
434 /* We don't want ccwgroup devices to live longer than their driver. */
435 get_driver(&cdriver
->driver
);
436 while ((dev
= driver_find_device(&cdriver
->driver
, NULL
, NULL
,
437 __ccwgroup_match_all
))) {
438 struct ccwgroup_device
*gdev
= to_ccwgroupdev(dev
);
440 mutex_lock(&gdev
->reg_mutex
);
441 __ccwgroup_remove_symlinks(gdev
);
442 device_unregister(dev
);
443 mutex_unlock(&gdev
->reg_mutex
);
446 put_driver(&cdriver
->driver
);
447 driver_unregister(&cdriver
->driver
);
451 * ccwgroup_probe_ccwdev() - probe function for slave devices
452 * @cdev: ccw device to be probed
454 * This is a dummy probe function for ccw devices that are slave devices in
455 * a ccw group device.
459 int ccwgroup_probe_ccwdev(struct ccw_device
*cdev
)
464 static struct ccwgroup_device
*
465 __ccwgroup_get_gdev_by_cdev(struct ccw_device
*cdev
)
467 struct ccwgroup_device
*gdev
;
469 gdev
= dev_get_drvdata(&cdev
->dev
);
471 if (get_device(&gdev
->dev
)) {
472 mutex_lock(&gdev
->reg_mutex
);
473 if (device_is_registered(&gdev
->dev
))
475 mutex_unlock(&gdev
->reg_mutex
);
476 put_device(&gdev
->dev
);
484 * ccwgroup_remove_ccwdev() - remove function for slave devices
485 * @cdev: ccw device to be removed
487 * This is a remove function for ccw devices that are slave devices in a ccw
488 * group device. It sets the ccw device offline and also deregisters the
489 * embedding ccw group device.
491 void ccwgroup_remove_ccwdev(struct ccw_device
*cdev
)
493 struct ccwgroup_device
*gdev
;
495 /* Ignore offlining errors, device is gone anyway. */
496 ccw_device_set_offline(cdev
);
497 /* If one of its devices is gone, the whole group is done for. */
498 gdev
= __ccwgroup_get_gdev_by_cdev(cdev
);
500 __ccwgroup_remove_symlinks(gdev
);
501 device_unregister(&gdev
->dev
);
502 mutex_unlock(&gdev
->reg_mutex
);
503 put_device(&gdev
->dev
);
507 MODULE_LICENSE("GPL");
508 EXPORT_SYMBOL(ccwgroup_driver_register
);
509 EXPORT_SYMBOL(ccwgroup_driver_unregister
);
510 EXPORT_SYMBOL(ccwgroup_create
);
511 EXPORT_SYMBOL(ccwgroup_probe_ccwdev
);
512 EXPORT_SYMBOL(ccwgroup_remove_ccwdev
);