]> err.no Git - linux-2.6/blob - drivers/s390/cio/ccwgroup.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux-2.6] / drivers / s390 / cio / ccwgroup.c
1 /*
2  *  drivers/s390/cio/ccwgroup.c
3  *  bus driver for ccwgroup
4  *
5  *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6  *                       IBM Corporation
7  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
8  *               Cornelia Huck (cornelia.huck@de.ibm.com)
9  */
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/ccwdev.h>
20 #include <asm/ccwgroup.h>
21
22 /* In Linux 2.4, we had a channel device layer called "chandev"
23  * that did all sorts of obscure stuff for networking devices.
24  * This is another driver that serves as a replacement for just
25  * one of its functions, namely the translation of single subchannels
26  * to devices that use multiple subchannels.
27  */
28
29 /* a device matches a driver if all its slave devices match the same
30  * entry of the driver */
31 static int
32 ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
33 {
34         struct ccwgroup_device *gdev;
35         struct ccwgroup_driver *gdrv;
36
37         gdev = to_ccwgroupdev(dev);
38         gdrv = to_ccwgroupdrv(drv);
39
40         if (gdev->creator_id == gdrv->driver_id)
41                 return 1;
42
43         return 0;
44 }
45 static int
46 ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
47 {
48         /* TODO */
49         return 0;
50 }
51
52 static struct bus_type ccwgroup_bus_type;
53
54 static void
55 __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
56 {
57         int i;
58         char str[8];
59
60         for (i = 0; i < gdev->count; i++) {
61                 sprintf(str, "cdev%d", i);
62                 sysfs_remove_link(&gdev->dev.kobj, str);
63                 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
64         }
65         
66 }
67
68 /*
69  * Provide an 'ungroup' attribute so the user can remove group devices no
70  * longer needed or accidentially created. Saves memory :)
71  */
72 static void ccwgroup_ungroup_callback(struct device *dev)
73 {
74         struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
75
76         mutex_lock(&gdev->reg_mutex);
77         if (device_is_registered(&gdev->dev)) {
78                 __ccwgroup_remove_symlinks(gdev);
79                 device_unregister(dev);
80         }
81         mutex_unlock(&gdev->reg_mutex);
82 }
83
84 static ssize_t
85 ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
86 {
87         struct ccwgroup_device *gdev;
88         int rc;
89
90         gdev = to_ccwgroupdev(dev);
91
92         if (gdev->state != CCWGROUP_OFFLINE)
93                 return -EINVAL;
94
95         /* Note that we cannot unregister the device from one of its
96          * attribute methods, so we have to use this roundabout approach.
97          */
98         rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
99         if (rc)
100                 count = rc;
101         return count;
102 }
103
104 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
105
106 static void
107 ccwgroup_release (struct device *dev)
108 {
109         struct ccwgroup_device *gdev;
110         int i;
111
112         gdev = to_ccwgroupdev(dev);
113
114         for (i = 0; i < gdev->count; i++) {
115                 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
116                 put_device(&gdev->cdev[i]->dev);
117         }
118         kfree(gdev);
119 }
120
121 static int
122 __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
123 {
124         char str[8];
125         int i, rc;
126
127         for (i = 0; i < gdev->count; i++) {
128                 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
129                                        "group_device");
130                 if (rc) {
131                         for (--i; i >= 0; i--)
132                                 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
133                                                   "group_device");
134                         return rc;
135                 }
136         }
137         for (i = 0; i < gdev->count; i++) {
138                 sprintf(str, "cdev%d", i);
139                 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
140                                        str);
141                 if (rc) {
142                         for (--i; i >= 0; i--) {
143                                 sprintf(str, "cdev%d", i);
144                                 sysfs_remove_link(&gdev->dev.kobj, str);
145                         }
146                         for (i = 0; i < gdev->count; i++)
147                                 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
148                                                   "group_device");
149                         return rc;
150                 }
151         }
152         return 0;
153 }
154
155 /**
156  * ccwgroup_create() - create and register a ccw group device
157  * @root: parent device for the new device
158  * @creator_id: identifier of creating driver
159  * @cdrv: ccw driver of slave devices
160  * @argc: number of slave devices
161  * @argv: bus ids of slave devices
162  *
163  * Create and register a new ccw group device as a child of @root. Slave
164  * devices are obtained from the list of bus ids given in @argv[] and must all
165  * belong to @cdrv.
166  * Returns:
167  *  %0 on success and an error code on failure.
168  * Context:
169  *  non-atomic
170  */
171 int ccwgroup_create(struct device *root, unsigned int creator_id,
172                     struct ccw_driver *cdrv, int argc, char *argv[])
173 {
174         struct ccwgroup_device *gdev;
175         int i;
176         int rc;
177
178         if (argc > 256) /* disallow dumb users */
179                 return -EINVAL;
180
181         gdev = kzalloc(sizeof(*gdev) + argc*sizeof(gdev->cdev[0]), GFP_KERNEL);
182         if (!gdev)
183                 return -ENOMEM;
184
185         atomic_set(&gdev->onoff, 0);
186         mutex_init(&gdev->reg_mutex);
187         mutex_lock(&gdev->reg_mutex);
188         for (i = 0; i < argc; i++) {
189                 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, argv[i]);
190
191                 /* all devices have to be of the same type in
192                  * order to be grouped */
193                 if (!gdev->cdev[i]
194                     || gdev->cdev[i]->id.driver_info !=
195                     gdev->cdev[0]->id.driver_info) {
196                         rc = -EINVAL;
197                         goto error;
198                 }
199                 /* Don't allow a device to belong to more than one group. */
200                 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
201                         rc = -EINVAL;
202                         goto error;
203                 }
204                 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
205         }
206
207         gdev->creator_id = creator_id;
208         gdev->count = argc;
209         gdev->dev.bus = &ccwgroup_bus_type;
210         gdev->dev.parent = root;
211         gdev->dev.release = ccwgroup_release;
212
213         snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
214                         gdev->cdev[0]->dev.bus_id);
215
216         rc = device_register(&gdev->dev);
217         if (rc)
218                 goto error;
219         get_device(&gdev->dev);
220         rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
221
222         if (rc) {
223                 device_unregister(&gdev->dev);
224                 goto error;
225         }
226
227         rc = __ccwgroup_create_symlinks(gdev);
228         if (!rc) {
229                 mutex_unlock(&gdev->reg_mutex);
230                 put_device(&gdev->dev);
231                 return 0;
232         }
233         device_remove_file(&gdev->dev, &dev_attr_ungroup);
234         device_unregister(&gdev->dev);
235 error:
236         for (i = 0; i < argc; i++)
237                 if (gdev->cdev[i]) {
238                         if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
239                                 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
240                         put_device(&gdev->cdev[i]->dev);
241                 }
242         mutex_unlock(&gdev->reg_mutex);
243         put_device(&gdev->dev);
244         return rc;
245 }
246
247 static int __init
248 init_ccwgroup (void)
249 {
250         return bus_register (&ccwgroup_bus_type);
251 }
252
253 static void __exit
254 cleanup_ccwgroup (void)
255 {
256         bus_unregister (&ccwgroup_bus_type);
257 }
258
259 module_init(init_ccwgroup);
260 module_exit(cleanup_ccwgroup);
261
262 /************************** driver stuff ******************************/
263
264 static int
265 ccwgroup_set_online(struct ccwgroup_device *gdev)
266 {
267         struct ccwgroup_driver *gdrv;
268         int ret;
269
270         if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
271                 return -EAGAIN;
272         if (gdev->state == CCWGROUP_ONLINE) {
273                 ret = 0;
274                 goto out;
275         }
276         if (!gdev->dev.driver) {
277                 ret = -EINVAL;
278                 goto out;
279         }
280         gdrv = to_ccwgroupdrv (gdev->dev.driver);
281         if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
282                 goto out;
283
284         gdev->state = CCWGROUP_ONLINE;
285  out:
286         atomic_set(&gdev->onoff, 0);
287         return ret;
288 }
289
290 static int
291 ccwgroup_set_offline(struct ccwgroup_device *gdev)
292 {
293         struct ccwgroup_driver *gdrv;
294         int ret;
295
296         if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
297                 return -EAGAIN;
298         if (gdev->state == CCWGROUP_OFFLINE) {
299                 ret = 0;
300                 goto out;
301         }
302         if (!gdev->dev.driver) {
303                 ret = -EINVAL;
304                 goto out;
305         }
306         gdrv = to_ccwgroupdrv (gdev->dev.driver);
307         if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
308                 goto out;
309
310         gdev->state = CCWGROUP_OFFLINE;
311  out:
312         atomic_set(&gdev->onoff, 0);
313         return ret;
314 }
315
316 static ssize_t
317 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
318 {
319         struct ccwgroup_device *gdev;
320         struct ccwgroup_driver *gdrv;
321         unsigned int value;
322         int ret;
323
324         gdev = to_ccwgroupdev(dev);
325         if (!dev->driver)
326                 return count;
327
328         gdrv = to_ccwgroupdrv (gdev->dev.driver);
329         if (!try_module_get(gdrv->owner))
330                 return -EINVAL;
331
332         value = simple_strtoul(buf, NULL, 0);
333         ret = count;
334         if (value == 1)
335                 ccwgroup_set_online(gdev);
336         else if (value == 0)
337                 ccwgroup_set_offline(gdev);
338         else
339                 ret = -EINVAL;
340         module_put(gdrv->owner);
341         return ret;
342 }
343
344 static ssize_t
345 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
346 {
347         int online;
348
349         online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
350
351         return sprintf(buf, online ? "1\n" : "0\n");
352 }
353
354 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
355
356 static int
357 ccwgroup_probe (struct device *dev)
358 {
359         struct ccwgroup_device *gdev;
360         struct ccwgroup_driver *gdrv;
361
362         int ret;
363
364         gdev = to_ccwgroupdev(dev);
365         gdrv = to_ccwgroupdrv(dev->driver);
366
367         if ((ret = device_create_file(dev, &dev_attr_online)))
368                 return ret;
369
370         ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
371         if (ret)
372                 device_remove_file(dev, &dev_attr_online);
373
374         return ret;
375 }
376
377 static int
378 ccwgroup_remove (struct device *dev)
379 {
380         struct ccwgroup_device *gdev;
381         struct ccwgroup_driver *gdrv;
382
383         gdev = to_ccwgroupdev(dev);
384         gdrv = to_ccwgroupdrv(dev->driver);
385
386         device_remove_file(dev, &dev_attr_online);
387
388         if (gdrv && gdrv->remove)
389                 gdrv->remove(gdev);
390         return 0;
391 }
392
393 static void ccwgroup_shutdown(struct device *dev)
394 {
395         struct ccwgroup_device *gdev;
396         struct ccwgroup_driver *gdrv;
397
398         gdev = to_ccwgroupdev(dev);
399         gdrv = to_ccwgroupdrv(dev->driver);
400         if (gdrv && gdrv->shutdown)
401                 gdrv->shutdown(gdev);
402 }
403
404 static struct bus_type ccwgroup_bus_type = {
405         .name   = "ccwgroup",
406         .match  = ccwgroup_bus_match,
407         .uevent = ccwgroup_uevent,
408         .probe  = ccwgroup_probe,
409         .remove = ccwgroup_remove,
410         .shutdown = ccwgroup_shutdown,
411 };
412
413 /**
414  * ccwgroup_driver_register() - register a ccw group driver
415  * @cdriver: driver to be registered
416  *
417  * This function is mainly a wrapper around driver_register().
418  */
419 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
420 {
421         /* register our new driver with the core */
422         cdriver->driver.bus = &ccwgroup_bus_type;
423         cdriver->driver.name = cdriver->name;
424         cdriver->driver.owner = cdriver->owner;
425
426         return driver_register(&cdriver->driver);
427 }
428
429 static int
430 __ccwgroup_match_all(struct device *dev, void *data)
431 {
432         return 1;
433 }
434
435 /**
436  * ccwgroup_driver_unregister() - deregister a ccw group driver
437  * @cdriver: driver to be deregistered
438  *
439  * This function is mainly a wrapper around driver_unregister().
440  */
441 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
442 {
443         struct device *dev;
444
445         /* We don't want ccwgroup devices to live longer than their driver. */
446         get_driver(&cdriver->driver);
447         while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
448                                          __ccwgroup_match_all))) {
449                 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
450
451                 mutex_lock(&gdev->reg_mutex);
452                 __ccwgroup_remove_symlinks(gdev);
453                 device_unregister(dev);
454                 mutex_unlock(&gdev->reg_mutex);
455                 put_device(dev);
456         }
457         put_driver(&cdriver->driver);
458         driver_unregister(&cdriver->driver);
459 }
460
461 /**
462  * ccwgroup_probe_ccwdev() - probe function for slave devices
463  * @cdev: ccw device to be probed
464  *
465  * This is a dummy probe function for ccw devices that are slave devices in
466  * a ccw group device.
467  * Returns:
468  *  always %0
469  */
470 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
471 {
472         return 0;
473 }
474
475 static struct ccwgroup_device *
476 __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
477 {
478         struct ccwgroup_device *gdev;
479
480         gdev = dev_get_drvdata(&cdev->dev);
481         if (gdev) {
482                 if (get_device(&gdev->dev)) {
483                         mutex_lock(&gdev->reg_mutex);
484                         if (device_is_registered(&gdev->dev))
485                                 return gdev;
486                         mutex_unlock(&gdev->reg_mutex);
487                         put_device(&gdev->dev);
488                 }
489                 return NULL;
490         }
491         return NULL;
492 }
493
494 /**
495  * ccwgroup_remove_ccwdev() - remove function for slave devices
496  * @cdev: ccw device to be removed
497  *
498  * This is a remove function for ccw devices that are slave devices in a ccw
499  * group device. It sets the ccw device offline and also deregisters the
500  * embedding ccw group device.
501  */
502 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
503 {
504         struct ccwgroup_device *gdev;
505
506         /* Ignore offlining errors, device is gone anyway. */
507         ccw_device_set_offline(cdev);
508         /* If one of its devices is gone, the whole group is done for. */
509         gdev = __ccwgroup_get_gdev_by_cdev(cdev);
510         if (gdev) {
511                 __ccwgroup_remove_symlinks(gdev);
512                 device_unregister(&gdev->dev);
513                 mutex_unlock(&gdev->reg_mutex);
514                 put_device(&gdev->dev);
515         }
516 }
517
518 MODULE_LICENSE("GPL");
519 EXPORT_SYMBOL(ccwgroup_driver_register);
520 EXPORT_SYMBOL(ccwgroup_driver_unregister);
521 EXPORT_SYMBOL(ccwgroup_create);
522 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
523 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);