Merge master.kernel.org:/home/rmk/linux-2.6-arm
[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/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 */
32 static int
33 ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
34 {
35         struct ccwgroup_device *gdev;
36         struct ccwgroup_driver *gdrv;
37
38         gdev = container_of(dev, struct ccwgroup_device, dev);
39         gdrv = container_of(drv, struct ccwgroup_driver, driver);
40
41         if (gdev->creator_id == gdrv->driver_id)
42                 return 1;
43
44         return 0;
45 }
46 static int
47 ccwgroup_uevent (struct device *dev, char **envp, int num_envp, char *buffer,
48                   int buffer_size)
49 {
50         /* TODO */
51         return 0;
52 }
53
54 static struct bus_type ccwgroup_bus_type;
55
56 static inline void
57 __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
58 {
59         int i;
60         char str[8];
61
62         for (i = 0; i < gdev->count; i++) {
63                 sprintf(str, "cdev%d", i);
64                 sysfs_remove_link(&gdev->dev.kobj, str);
65                 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
66         }
67         
68 }
69
70 /*
71  * Provide an 'ungroup' attribute so the user can remove group devices no
72  * longer needed or accidentially created. Saves memory :)
73  */
74 static ssize_t
75 ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
76 {
77         struct ccwgroup_device *gdev;
78
79         gdev = to_ccwgroupdev(dev);
80
81         if (gdev->state != CCWGROUP_OFFLINE)
82                 return -EINVAL;
83
84         __ccwgroup_remove_symlinks(gdev);
85         device_unregister(dev);
86
87         return count;
88 }
89
90 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
91
92 static void
93 ccwgroup_release (struct device *dev)
94 {
95         struct ccwgroup_device *gdev;
96         int i;
97
98         gdev = to_ccwgroupdev(dev);
99
100         for (i = 0; i < gdev->count; i++) {
101                 gdev->cdev[i]->dev.driver_data = NULL;
102                 put_device(&gdev->cdev[i]->dev);
103         }
104         kfree(gdev);
105 }
106
107 static inline int
108 __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
109 {
110         char str[8];
111         int i, rc;
112
113         for (i = 0; i < gdev->count; i++) {
114                 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
115                                        "group_device");
116                 if (rc) {
117                         for (--i; i >= 0; i--)
118                                 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
119                                                   "group_device");
120                         return rc;
121                 }
122         }
123         for (i = 0; i < gdev->count; i++) {
124                 sprintf(str, "cdev%d", i);
125                 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
126                                        str);
127                 if (rc) {
128                         for (--i; i >= 0; i--) {
129                                 sprintf(str, "cdev%d", i);
130                                 sysfs_remove_link(&gdev->dev.kobj, str);
131                         }
132                         for (i = 0; i < gdev->count; i++)
133                                 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
134                                                   "group_device");
135                         return rc;
136                 }
137         }
138         return 0;
139 }
140
141 /*
142  * try to add a new ccwgroup device for one driver
143  * argc and argv[] are a list of bus_id's of devices
144  * belonging to the driver.
145  */
146 int
147 ccwgroup_create(struct device *root,
148                 unsigned int creator_id,
149                 struct ccw_driver *cdrv,
150                 int argc, char *argv[])
151 {
152         struct ccwgroup_device *gdev;
153         int i;
154         int rc;
155
156         if (argc > 256) /* disallow dumb users */
157                 return -EINVAL;
158
159         gdev = kzalloc(sizeof(*gdev) + argc*sizeof(gdev->cdev[0]), GFP_KERNEL);
160         if (!gdev)
161                 return -ENOMEM;
162
163         atomic_set(&gdev->onoff, 0);
164
165         for (i = 0; i < argc; i++) {
166                 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, argv[i]);
167
168                 /* all devices have to be of the same type in
169                  * order to be grouped */
170                 if (!gdev->cdev[i]
171                     || gdev->cdev[i]->id.driver_info !=
172                     gdev->cdev[0]->id.driver_info) {
173                         rc = -EINVAL;
174                         goto free_dev;
175                 }
176                 /* Don't allow a device to belong to more than one group. */
177                 if (gdev->cdev[i]->dev.driver_data) {
178                         rc = -EINVAL;
179                         goto free_dev;
180                 }
181                 gdev->cdev[i]->dev.driver_data = gdev;
182         }
183
184         gdev->creator_id = creator_id;
185         gdev->count = argc;
186         gdev->dev = (struct device ) {
187                 .bus = &ccwgroup_bus_type,
188                 .parent = root,
189                 .release = ccwgroup_release,
190         };
191
192         snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
193                         gdev->cdev[0]->dev.bus_id);
194
195         rc = device_register(&gdev->dev);
196         
197         if (rc)
198                 goto free_dev;
199         get_device(&gdev->dev);
200         rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
201
202         if (rc) {
203                 device_unregister(&gdev->dev);
204                 goto error;
205         }
206
207         rc = __ccwgroup_create_symlinks(gdev);
208         if (!rc) {
209                 put_device(&gdev->dev);
210                 return 0;
211         }
212         device_remove_file(&gdev->dev, &dev_attr_ungroup);
213         device_unregister(&gdev->dev);
214 error:
215         for (i = 0; i < argc; i++)
216                 if (gdev->cdev[i]) {
217                         put_device(&gdev->cdev[i]->dev);
218                         gdev->cdev[i]->dev.driver_data = NULL;
219                 }
220         put_device(&gdev->dev);
221         return rc;
222 free_dev:
223         for (i = 0; i < argc; i++)
224                 if (gdev->cdev[i]) {
225                         if (gdev->cdev[i]->dev.driver_data == gdev)
226                                 gdev->cdev[i]->dev.driver_data = NULL;
227                         put_device(&gdev->cdev[i]->dev);
228                 }
229         kfree(gdev);
230         return rc;
231 }
232
233 static int __init
234 init_ccwgroup (void)
235 {
236         return bus_register (&ccwgroup_bus_type);
237 }
238
239 static void __exit
240 cleanup_ccwgroup (void)
241 {
242         bus_unregister (&ccwgroup_bus_type);
243 }
244
245 module_init(init_ccwgroup);
246 module_exit(cleanup_ccwgroup);
247
248 /************************** driver stuff ******************************/
249
250 static int
251 ccwgroup_set_online(struct ccwgroup_device *gdev)
252 {
253         struct ccwgroup_driver *gdrv;
254         int ret;
255
256         if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
257                 return -EAGAIN;
258         if (gdev->state == CCWGROUP_ONLINE) {
259                 ret = 0;
260                 goto out;
261         }
262         if (!gdev->dev.driver) {
263                 ret = -EINVAL;
264                 goto out;
265         }
266         gdrv = to_ccwgroupdrv (gdev->dev.driver);
267         if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
268                 goto out;
269
270         gdev->state = CCWGROUP_ONLINE;
271  out:
272         atomic_set(&gdev->onoff, 0);
273         return ret;
274 }
275
276 static int
277 ccwgroup_set_offline(struct ccwgroup_device *gdev)
278 {
279         struct ccwgroup_driver *gdrv;
280         int ret;
281
282         if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
283                 return -EAGAIN;
284         if (gdev->state == CCWGROUP_OFFLINE) {
285                 ret = 0;
286                 goto out;
287         }
288         if (!gdev->dev.driver) {
289                 ret = -EINVAL;
290                 goto out;
291         }
292         gdrv = to_ccwgroupdrv (gdev->dev.driver);
293         if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
294                 goto out;
295
296         gdev->state = CCWGROUP_OFFLINE;
297  out:
298         atomic_set(&gdev->onoff, 0);
299         return ret;
300 }
301
302 static ssize_t
303 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
304 {
305         struct ccwgroup_device *gdev;
306         struct ccwgroup_driver *gdrv;
307         unsigned int value;
308         int ret;
309
310         gdev = to_ccwgroupdev(dev);
311         if (!dev->driver)
312                 return count;
313
314         gdrv = to_ccwgroupdrv (gdev->dev.driver);
315         if (!try_module_get(gdrv->owner))
316                 return -EINVAL;
317
318         value = simple_strtoul(buf, NULL, 0);
319         ret = count;
320         if (value == 1)
321                 ccwgroup_set_online(gdev);
322         else if (value == 0)
323                 ccwgroup_set_offline(gdev);
324         else
325                 ret = -EINVAL;
326         module_put(gdrv->owner);
327         return ret;
328 }
329
330 static ssize_t
331 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
332 {
333         int online;
334
335         online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
336
337         return sprintf(buf, online ? "1\n" : "0\n");
338 }
339
340 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
341
342 static int
343 ccwgroup_probe (struct device *dev)
344 {
345         struct ccwgroup_device *gdev;
346         struct ccwgroup_driver *gdrv;
347
348         int ret;
349
350         gdev = to_ccwgroupdev(dev);
351         gdrv = to_ccwgroupdrv(dev->driver);
352
353         if ((ret = device_create_file(dev, &dev_attr_online)))
354                 return ret;
355
356         pr_debug("%s: device %s\n", __func__, gdev->dev.bus_id);
357         ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
358         if (ret)
359                 device_remove_file(dev, &dev_attr_online);
360
361         return ret;
362 }
363
364 static int
365 ccwgroup_remove (struct device *dev)
366 {
367         struct ccwgroup_device *gdev;
368         struct ccwgroup_driver *gdrv;
369
370         gdev = to_ccwgroupdev(dev);
371         gdrv = to_ccwgroupdrv(dev->driver);
372
373         pr_debug("%s: device %s\n", __func__, gdev->dev.bus_id);
374
375         device_remove_file(dev, &dev_attr_online);
376
377         if (gdrv && gdrv->remove)
378                 gdrv->remove(gdev);
379         return 0;
380 }
381
382 static struct bus_type ccwgroup_bus_type = {
383         .name   = "ccwgroup",
384         .match  = ccwgroup_bus_match,
385         .uevent = ccwgroup_uevent,
386         .probe  = ccwgroup_probe,
387         .remove = ccwgroup_remove,
388 };
389
390 int
391 ccwgroup_driver_register (struct ccwgroup_driver *cdriver)
392 {
393         /* register our new driver with the core */
394         cdriver->driver = (struct device_driver) {
395                 .bus = &ccwgroup_bus_type,
396                 .name = cdriver->name,
397         };
398
399         return driver_register(&cdriver->driver);
400 }
401
402 static int
403 __ccwgroup_match_all(struct device *dev, void *data)
404 {
405         return 1;
406 }
407
408 void
409 ccwgroup_driver_unregister (struct ccwgroup_driver *cdriver)
410 {
411         struct device *dev;
412
413         /* We don't want ccwgroup devices to live longer than their driver. */
414         get_driver(&cdriver->driver);
415         while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
416                                          __ccwgroup_match_all))) {
417                 __ccwgroup_remove_symlinks(to_ccwgroupdev(dev));
418                 device_unregister(dev);
419                 put_device(dev);
420         }
421         put_driver(&cdriver->driver);
422         driver_unregister(&cdriver->driver);
423 }
424
425 int
426 ccwgroup_probe_ccwdev(struct ccw_device *cdev)
427 {
428         return 0;
429 }
430
431 static inline struct ccwgroup_device *
432 __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
433 {
434         struct ccwgroup_device *gdev;
435
436         if (cdev->dev.driver_data) {
437                 gdev = (struct ccwgroup_device *)cdev->dev.driver_data;
438                 if (get_device(&gdev->dev)) {
439                         if (device_is_registered(&gdev->dev))
440                                 return gdev;
441                         put_device(&gdev->dev);
442                 }
443                 return NULL;
444         }
445         return NULL;
446 }
447
448 void
449 ccwgroup_remove_ccwdev(struct ccw_device *cdev)
450 {
451         struct ccwgroup_device *gdev;
452
453         /* Ignore offlining errors, device is gone anyway. */
454         ccw_device_set_offline(cdev);
455         /* If one of its devices is gone, the whole group is done for. */
456         gdev = __ccwgroup_get_gdev_by_cdev(cdev);
457         if (gdev) {
458                 __ccwgroup_remove_symlinks(gdev);
459                 device_unregister(&gdev->dev);
460                 put_device(&gdev->dev);
461         }
462 }
463
464 MODULE_LICENSE("GPL");
465 EXPORT_SYMBOL(ccwgroup_driver_register);
466 EXPORT_SYMBOL(ccwgroup_driver_unregister);
467 EXPORT_SYMBOL(ccwgroup_create);
468 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
469 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);