blob: 3964056a9a4745f05b607c86a1a837eaa961c0eb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/ccwgroup.c
3 * bus driver for ccwgroup
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
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>
18
19#include <asm/semaphore.h>
20#include <asm/ccwdev.h>
21#include <asm/ccwgroup.h>
22
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.
28 */
29
30/* a device matches a driver if all its slave devices match the same
31 * entry of the driver */
32static int
33ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
34{
35 struct ccwgroup_device *gdev;
36 struct ccwgroup_driver *gdrv;
37
Cornelia Huck084325d2008-01-26 14:10:38 +010038 gdev = to_ccwgroupdev(dev);
39 gdrv = to_ccwgroupdrv(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 if (gdev->creator_id == gdrv->driver_id)
42 return 1;
43
44 return 0;
45}
46static int
Kay Sievers7eff2e72007-08-14 15:15:12 +020047ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 /* TODO */
50 return 0;
51}
52
Russell Kingf9ccf452006-01-05 14:42:09 +000053static struct bus_type ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Heiko Carstens4d284ca2007-02-05 21:18:53 +010055static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070056__ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
57{
58 int i;
59 char str[8];
60
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");
65 }
66
67}
68
69/*
70 * Provide an 'ungroup' attribute so the user can remove group devices no
71 * longer needed or accidentially created. Saves memory :)
72 */
Alan Sternd9a9cdf2007-03-15 15:50:34 -040073static void ccwgroup_ungroup_callback(struct device *dev)
74{
75 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
76
Cornelia Huckd76123e2007-04-27 16:01:37 +020077 mutex_lock(&gdev->reg_mutex);
Cornelia Huck1a908c72008-01-26 14:10:50 +010078 if (device_is_registered(&gdev->dev)) {
79 __ccwgroup_remove_symlinks(gdev);
80 device_unregister(dev);
81 }
Cornelia Huckd76123e2007-04-27 16:01:37 +020082 mutex_unlock(&gdev->reg_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -040083}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -040086ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct ccwgroup_device *gdev;
Alan Sternd9a9cdf2007-03-15 15:50:34 -040089 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 gdev = to_ccwgroupdev(dev);
92
93 if (gdev->state != CCWGROUP_OFFLINE)
94 return -EINVAL;
95
Alan Sternd9a9cdf2007-03-15 15:50:34 -040096 /* Note that we cannot unregister the device from one of its
97 * attribute methods, so we have to use this roundabout approach.
98 */
99 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
100 if (rc)
101 count = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return count;
103}
104
105static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
106
107static void
108ccwgroup_release (struct device *dev)
109{
110 struct ccwgroup_device *gdev;
111 int i;
112
113 gdev = to_ccwgroupdev(dev);
114
115 for (i = 0; i < gdev->count; i++) {
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100116 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 put_device(&gdev->cdev[i]->dev);
118 }
119 kfree(gdev);
120}
121
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100122static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123__ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
124{
125 char str[8];
126 int i, rc;
127
128 for (i = 0; i < gdev->count; i++) {
129 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
130 "group_device");
131 if (rc) {
132 for (--i; i >= 0; i--)
133 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
134 "group_device");
135 return rc;
136 }
137 }
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,
141 str);
142 if (rc) {
143 for (--i; i >= 0; i--) {
144 sprintf(str, "cdev%d", i);
145 sysfs_remove_link(&gdev->dev.kobj, str);
146 }
147 for (i = 0; i < gdev->count; i++)
148 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
149 "group_device");
150 return rc;
151 }
152 }
153 return 0;
154}
155
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200156/**
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
163 *
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
166 * belong to @cdrv.
167 * Returns:
168 * %0 on success and an error code on failure.
169 * Context:
170 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200172int ccwgroup_create(struct device *root, unsigned int creator_id,
173 struct ccw_driver *cdrv, int argc, char *argv[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct ccwgroup_device *gdev;
176 int i;
177 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (argc > 256) /* disallow dumb users */
180 return -EINVAL;
181
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800182 gdev = kzalloc(sizeof(*gdev) + argc*sizeof(gdev->cdev[0]), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (!gdev)
184 return -ENOMEM;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200187 mutex_init(&gdev->reg_mutex);
188 mutex_lock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 for (i = 0; i < argc; i++) {
190 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, argv[i]);
191
192 /* all devices have to be of the same type in
193 * order to be grouped */
194 if (!gdev->cdev[i]
195 || gdev->cdev[i]->id.driver_info !=
196 gdev->cdev[0]->id.driver_info) {
197 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200198 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 /* Don't allow a device to belong to more than one group. */
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100201 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200203 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100205 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Cornelia Huck17088222006-07-27 14:00:33 +0200206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 gdev->creator_id = creator_id;
209 gdev->count = argc;
Heiko Carstens292888c2006-08-30 14:33:35 +0200210 gdev->dev.bus = &ccwgroup_bus_type;
211 gdev->dev.parent = root;
212 gdev->dev.release = ccwgroup_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
215 gdev->cdev[0]->dev.bus_id);
216
217 rc = device_register(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200219 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 get_device(&gdev->dev);
221 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
222
223 if (rc) {
224 device_unregister(&gdev->dev);
225 goto error;
226 }
227
228 rc = __ccwgroup_create_symlinks(gdev);
229 if (!rc) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200230 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 put_device(&gdev->dev);
232 return 0;
233 }
234 device_remove_file(&gdev->dev, &dev_attr_ungroup);
235 device_unregister(&gdev->dev);
236error:
237 for (i = 0; i < argc; i++)
238 if (gdev->cdev[i]) {
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100239 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
240 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Cornelia Huck17088222006-07-27 14:00:33 +0200241 put_device(&gdev->cdev[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200243 mutex_unlock(&gdev->reg_mutex);
244 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return rc;
246}
247
248static int __init
249init_ccwgroup (void)
250{
251 return bus_register (&ccwgroup_bus_type);
252}
253
254static void __exit
255cleanup_ccwgroup (void)
256{
257 bus_unregister (&ccwgroup_bus_type);
258}
259
260module_init(init_ccwgroup);
261module_exit(cleanup_ccwgroup);
262
263/************************** driver stuff ******************************/
264
265static int
266ccwgroup_set_online(struct ccwgroup_device *gdev)
267{
268 struct ccwgroup_driver *gdrv;
269 int ret;
270
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800271 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -EAGAIN;
273 if (gdev->state == CCWGROUP_ONLINE) {
274 ret = 0;
275 goto out;
276 }
277 if (!gdev->dev.driver) {
278 ret = -EINVAL;
279 goto out;
280 }
281 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800282 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 goto out;
284
285 gdev->state = CCWGROUP_ONLINE;
286 out:
287 atomic_set(&gdev->onoff, 0);
288 return ret;
289}
290
291static int
292ccwgroup_set_offline(struct ccwgroup_device *gdev)
293{
294 struct ccwgroup_driver *gdrv;
295 int ret;
296
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800297 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return -EAGAIN;
299 if (gdev->state == CCWGROUP_OFFLINE) {
300 ret = 0;
301 goto out;
302 }
303 if (!gdev->dev.driver) {
304 ret = -EINVAL;
305 goto out;
306 }
307 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800308 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto out;
310
311 gdev->state = CCWGROUP_OFFLINE;
312 out:
313 atomic_set(&gdev->onoff, 0);
314 return ret;
315}
316
317static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400318ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct ccwgroup_device *gdev;
321 struct ccwgroup_driver *gdrv;
322 unsigned int value;
323 int ret;
324
325 gdev = to_ccwgroupdev(dev);
326 if (!dev->driver)
327 return count;
328
329 gdrv = to_ccwgroupdrv (gdev->dev.driver);
330 if (!try_module_get(gdrv->owner))
331 return -EINVAL;
332
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200333 value = simple_strtoul(buf, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 ret = count;
335 if (value == 1)
336 ccwgroup_set_online(gdev);
337 else if (value == 0)
338 ccwgroup_set_offline(gdev);
339 else
340 ret = -EINVAL;
341 module_put(gdrv->owner);
342 return ret;
343}
344
345static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400346ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 int online;
349
350 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
351
352 return sprintf(buf, online ? "1\n" : "0\n");
353}
354
355static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
356
357static int
358ccwgroup_probe (struct device *dev)
359{
360 struct ccwgroup_device *gdev;
361 struct ccwgroup_driver *gdrv;
362
363 int ret;
364
365 gdev = to_ccwgroupdev(dev);
366 gdrv = to_ccwgroupdrv(dev->driver);
367
368 if ((ret = device_create_file(dev, &dev_attr_online)))
369 return ret;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
372 if (ret)
373 device_remove_file(dev, &dev_attr_online);
374
375 return ret;
376}
377
378static int
379ccwgroup_remove (struct device *dev)
380{
381 struct ccwgroup_device *gdev;
382 struct ccwgroup_driver *gdrv;
383
384 gdev = to_ccwgroupdev(dev);
385 gdrv = to_ccwgroupdrv(dev->driver);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 device_remove_file(dev, &dev_attr_online);
388
389 if (gdrv && gdrv->remove)
390 gdrv->remove(gdev);
391 return 0;
392}
393
Russell Kingf9ccf452006-01-05 14:42:09 +0000394static struct bus_type ccwgroup_bus_type = {
395 .name = "ccwgroup",
396 .match = ccwgroup_bus_match,
397 .uevent = ccwgroup_uevent,
398 .probe = ccwgroup_probe,
399 .remove = ccwgroup_remove,
400};
401
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200402/**
403 * ccwgroup_driver_register() - register a ccw group driver
404 * @cdriver: driver to be registered
405 *
406 * This function is mainly a wrapper around driver_register().
407 */
408int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200411 cdriver->driver.bus = &ccwgroup_bus_type;
412 cdriver->driver.name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +0100413 cdriver->driver.owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 return driver_register(&cdriver->driver);
416}
417
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700418static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200419__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Cornelia Huck887ab592006-06-29 14:56:52 +0200421 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200424/**
425 * ccwgroup_driver_unregister() - deregister a ccw group driver
426 * @cdriver: driver to be deregistered
427 *
428 * This function is mainly a wrapper around driver_unregister().
429 */
430void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Cornelia Huck887ab592006-06-29 14:56:52 +0200432 struct device *dev;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* We don't want ccwgroup devices to live longer than their driver. */
435 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200436 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
437 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200438 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
439
440 mutex_lock(&gdev->reg_mutex);
441 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200442 device_unregister(dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200443 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200444 put_device(dev);
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 put_driver(&cdriver->driver);
447 driver_unregister(&cdriver->driver);
448}
449
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200450/**
451 * ccwgroup_probe_ccwdev() - probe function for slave devices
452 * @cdev: ccw device to be probed
453 *
454 * This is a dummy probe function for ccw devices that are slave devices in
455 * a ccw group device.
456 * Returns:
457 * always %0
458 */
459int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 return 0;
462}
463
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100464static struct ccwgroup_device *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
466{
467 struct ccwgroup_device *gdev;
468
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100469 gdev = dev_get_drvdata(&cdev->dev);
470 if (gdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (get_device(&gdev->dev)) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200472 mutex_lock(&gdev->reg_mutex);
Daniel Ritzd305ef52005-09-22 00:47:24 -0700473 if (device_is_registered(&gdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return gdev;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200475 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 put_device(&gdev->dev);
477 }
478 return NULL;
479 }
480 return NULL;
481}
482
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200483/**
484 * ccwgroup_remove_ccwdev() - remove function for slave devices
485 * @cdev: ccw device to be removed
486 *
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.
490 */
491void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct ccwgroup_device *gdev;
494
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);
499 if (gdev) {
500 __ccwgroup_remove_symlinks(gdev);
501 device_unregister(&gdev->dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200502 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 put_device(&gdev->dev);
504 }
505}
506
507MODULE_LICENSE("GPL");
508EXPORT_SYMBOL(ccwgroup_driver_register);
509EXPORT_SYMBOL(ccwgroup_driver_unregister);
510EXPORT_SYMBOL(ccwgroup_create);
511EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
512EXPORT_SYMBOL(ccwgroup_remove_ccwdev);