blob: c7319a07ba35161fde79dac21de10332d0387736 [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
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}
46static int
Paul Jackson6d20b032005-11-25 20:04:26 -080047ccwgroup_uevent (struct device *dev, char **envp, int num_envp, char *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 int buffer_size)
49{
50 /* TODO */
51 return 0;
52}
53
Russell Kingf9ccf452006-01-05 14:42:09 +000054static struct bus_type ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static 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 */
74static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -040075ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
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
90static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
91
92static void
93ccwgroup_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
107static 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 */
146int
147ccwgroup_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 int del_drvdata;
156
157 if (argc > 256) /* disallow dumb users */
158 return -EINVAL;
159
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800160 gdev = kzalloc(sizeof(*gdev) + argc*sizeof(gdev->cdev[0]), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!gdev)
162 return -ENOMEM;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 atomic_set(&gdev->onoff, 0);
165
166 del_drvdata = 0;
167 for (i = 0; i < argc; i++) {
168 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, argv[i]);
169
170 /* all devices have to be of the same type in
171 * order to be grouped */
172 if (!gdev->cdev[i]
173 || gdev->cdev[i]->id.driver_info !=
174 gdev->cdev[0]->id.driver_info) {
175 rc = -EINVAL;
176 goto free_dev;
177 }
178 /* Don't allow a device to belong to more than one group. */
179 if (gdev->cdev[i]->dev.driver_data) {
180 rc = -EINVAL;
181 goto free_dev;
182 }
183 }
184 for (i = 0; i < argc; i++)
185 gdev->cdev[i]->dev.driver_data = gdev;
186 del_drvdata = 1;
187
188 gdev->creator_id = creator_id;
189 gdev->count = argc;
190 gdev->dev = (struct device ) {
191 .bus = &ccwgroup_bus_type,
192 .parent = root,
193 .release = ccwgroup_release,
194 };
195
196 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
197 gdev->cdev[0]->dev.bus_id);
198
199 rc = device_register(&gdev->dev);
200
201 if (rc)
202 goto free_dev;
203 get_device(&gdev->dev);
204 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
205
206 if (rc) {
207 device_unregister(&gdev->dev);
208 goto error;
209 }
210
211 rc = __ccwgroup_create_symlinks(gdev);
212 if (!rc) {
213 put_device(&gdev->dev);
214 return 0;
215 }
216 device_remove_file(&gdev->dev, &dev_attr_ungroup);
217 device_unregister(&gdev->dev);
218error:
219 for (i = 0; i < argc; i++)
220 if (gdev->cdev[i]) {
221 put_device(&gdev->cdev[i]->dev);
222 gdev->cdev[i]->dev.driver_data = NULL;
223 }
224 put_device(&gdev->dev);
225 return rc;
226free_dev:
227 for (i = 0; i < argc; i++)
228 if (gdev->cdev[i]) {
229 put_device(&gdev->cdev[i]->dev);
230 if (del_drvdata)
231 gdev->cdev[i]->dev.driver_data = NULL;
232 }
233 kfree(gdev);
234 return rc;
235}
236
237static int __init
238init_ccwgroup (void)
239{
240 return bus_register (&ccwgroup_bus_type);
241}
242
243static void __exit
244cleanup_ccwgroup (void)
245{
246 bus_unregister (&ccwgroup_bus_type);
247}
248
249module_init(init_ccwgroup);
250module_exit(cleanup_ccwgroup);
251
252/************************** driver stuff ******************************/
253
254static int
255ccwgroup_set_online(struct ccwgroup_device *gdev)
256{
257 struct ccwgroup_driver *gdrv;
258 int ret;
259
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800260 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -EAGAIN;
262 if (gdev->state == CCWGROUP_ONLINE) {
263 ret = 0;
264 goto out;
265 }
266 if (!gdev->dev.driver) {
267 ret = -EINVAL;
268 goto out;
269 }
270 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800271 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 goto out;
273
274 gdev->state = CCWGROUP_ONLINE;
275 out:
276 atomic_set(&gdev->onoff, 0);
277 return ret;
278}
279
280static int
281ccwgroup_set_offline(struct ccwgroup_device *gdev)
282{
283 struct ccwgroup_driver *gdrv;
284 int ret;
285
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800286 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EAGAIN;
288 if (gdev->state == CCWGROUP_OFFLINE) {
289 ret = 0;
290 goto out;
291 }
292 if (!gdev->dev.driver) {
293 ret = -EINVAL;
294 goto out;
295 }
296 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800297 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto out;
299
300 gdev->state = CCWGROUP_OFFLINE;
301 out:
302 atomic_set(&gdev->onoff, 0);
303 return ret;
304}
305
306static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400307ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 struct ccwgroup_device *gdev;
310 struct ccwgroup_driver *gdrv;
311 unsigned int value;
312 int ret;
313
314 gdev = to_ccwgroupdev(dev);
315 if (!dev->driver)
316 return count;
317
318 gdrv = to_ccwgroupdrv (gdev->dev.driver);
319 if (!try_module_get(gdrv->owner))
320 return -EINVAL;
321
322 value = simple_strtoul(buf, 0, 0);
323 ret = count;
324 if (value == 1)
325 ccwgroup_set_online(gdev);
326 else if (value == 0)
327 ccwgroup_set_offline(gdev);
328 else
329 ret = -EINVAL;
330 module_put(gdrv->owner);
331 return ret;
332}
333
334static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400335ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 int online;
338
339 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
340
341 return sprintf(buf, online ? "1\n" : "0\n");
342}
343
344static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
345
346static int
347ccwgroup_probe (struct device *dev)
348{
349 struct ccwgroup_device *gdev;
350 struct ccwgroup_driver *gdrv;
351
352 int ret;
353
354 gdev = to_ccwgroupdev(dev);
355 gdrv = to_ccwgroupdrv(dev->driver);
356
357 if ((ret = device_create_file(dev, &dev_attr_online)))
358 return ret;
359
360 pr_debug("%s: device %s\n", __func__, gdev->dev.bus_id);
361 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
362 if (ret)
363 device_remove_file(dev, &dev_attr_online);
364
365 return ret;
366}
367
368static int
369ccwgroup_remove (struct device *dev)
370{
371 struct ccwgroup_device *gdev;
372 struct ccwgroup_driver *gdrv;
373
374 gdev = to_ccwgroupdev(dev);
375 gdrv = to_ccwgroupdrv(dev->driver);
376
377 pr_debug("%s: device %s\n", __func__, gdev->dev.bus_id);
378
379 device_remove_file(dev, &dev_attr_online);
380
381 if (gdrv && gdrv->remove)
382 gdrv->remove(gdev);
383 return 0;
384}
385
Russell Kingf9ccf452006-01-05 14:42:09 +0000386static struct bus_type ccwgroup_bus_type = {
387 .name = "ccwgroup",
388 .match = ccwgroup_bus_match,
389 .uevent = ccwgroup_uevent,
390 .probe = ccwgroup_probe,
391 .remove = ccwgroup_remove,
392};
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394int
395ccwgroup_driver_register (struct ccwgroup_driver *cdriver)
396{
397 /* register our new driver with the core */
398 cdriver->driver = (struct device_driver) {
399 .bus = &ccwgroup_bus_type,
400 .name = cdriver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 };
402
403 return driver_register(&cdriver->driver);
404}
405
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700406static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200407__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Cornelia Huck887ab592006-06-29 14:56:52 +0200409 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412void
413ccwgroup_driver_unregister (struct ccwgroup_driver *cdriver)
414{
Cornelia Huck887ab592006-06-29 14:56:52 +0200415 struct device *dev;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* We don't want ccwgroup devices to live longer than their driver. */
418 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200419 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
420 __ccwgroup_match_all))) {
421 __ccwgroup_remove_symlinks(to_ccwgroupdev(dev));
422 device_unregister(dev);
423 put_device(dev);
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 put_driver(&cdriver->driver);
426 driver_unregister(&cdriver->driver);
427}
428
429int
430ccwgroup_probe_ccwdev(struct ccw_device *cdev)
431{
432 return 0;
433}
434
435static inline struct ccwgroup_device *
436__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
437{
438 struct ccwgroup_device *gdev;
439
440 if (cdev->dev.driver_data) {
441 gdev = (struct ccwgroup_device *)cdev->dev.driver_data;
442 if (get_device(&gdev->dev)) {
Daniel Ritzd305ef52005-09-22 00:47:24 -0700443 if (device_is_registered(&gdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return gdev;
445 put_device(&gdev->dev);
446 }
447 return NULL;
448 }
449 return NULL;
450}
451
452void
453ccwgroup_remove_ccwdev(struct ccw_device *cdev)
454{
455 struct ccwgroup_device *gdev;
456
457 /* Ignore offlining errors, device is gone anyway. */
458 ccw_device_set_offline(cdev);
459 /* If one of its devices is gone, the whole group is done for. */
460 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
461 if (gdev) {
462 __ccwgroup_remove_symlinks(gdev);
463 device_unregister(&gdev->dev);
464 put_device(&gdev->dev);
465 }
466}
467
468MODULE_LICENSE("GPL");
469EXPORT_SYMBOL(ccwgroup_driver_register);
470EXPORT_SYMBOL(ccwgroup_driver_unregister);
471EXPORT_SYMBOL(ccwgroup_create);
472EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
473EXPORT_SYMBOL(ccwgroup_remove_ccwdev);