blob: d48e3ca4752c444ba5d58d3c38abaa6a2daeeed9 [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
Heiko Carstens4d284ca2007-02-05 21:18:53 +010056static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070057__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
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100107static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108__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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (argc > 256) /* disallow dumb users */
157 return -EINVAL;
158
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800159 gdev = kzalloc(sizeof(*gdev) + argc*sizeof(gdev->cdev[0]), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!gdev)
161 return -ENOMEM;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 atomic_set(&gdev->onoff, 0);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 gdev->cdev[i]->dev.driver_data = gdev;
Cornelia Huck17088222006-07-27 14:00:33 +0200182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 gdev->creator_id = creator_id;
185 gdev->count = argc;
Heiko Carstens292888c2006-08-30 14:33:35 +0200186 gdev->dev.bus = &ccwgroup_bus_type;
187 gdev->dev.parent = root;
188 gdev->dev.release = ccwgroup_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
191 gdev->cdev[0]->dev.bus_id);
192
193 rc = device_register(&gdev->dev);
194
195 if (rc)
196 goto free_dev;
197 get_device(&gdev->dev);
198 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
199
200 if (rc) {
201 device_unregister(&gdev->dev);
202 goto error;
203 }
204
205 rc = __ccwgroup_create_symlinks(gdev);
206 if (!rc) {
207 put_device(&gdev->dev);
208 return 0;
209 }
210 device_remove_file(&gdev->dev, &dev_attr_ungroup);
211 device_unregister(&gdev->dev);
212error:
213 for (i = 0; i < argc; i++)
214 if (gdev->cdev[i]) {
215 put_device(&gdev->cdev[i]->dev);
216 gdev->cdev[i]->dev.driver_data = NULL;
217 }
218 put_device(&gdev->dev);
219 return rc;
220free_dev:
221 for (i = 0; i < argc; i++)
222 if (gdev->cdev[i]) {
Cornelia Huck17088222006-07-27 14:00:33 +0200223 if (gdev->cdev[i]->dev.driver_data == gdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 gdev->cdev[i]->dev.driver_data = NULL;
Cornelia Huck17088222006-07-27 14:00:33 +0200225 put_device(&gdev->cdev[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 kfree(gdev);
228 return rc;
229}
230
231static int __init
232init_ccwgroup (void)
233{
234 return bus_register (&ccwgroup_bus_type);
235}
236
237static void __exit
238cleanup_ccwgroup (void)
239{
240 bus_unregister (&ccwgroup_bus_type);
241}
242
243module_init(init_ccwgroup);
244module_exit(cleanup_ccwgroup);
245
246/************************** driver stuff ******************************/
247
248static int
249ccwgroup_set_online(struct ccwgroup_device *gdev)
250{
251 struct ccwgroup_driver *gdrv;
252 int ret;
253
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800254 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EAGAIN;
256 if (gdev->state == CCWGROUP_ONLINE) {
257 ret = 0;
258 goto out;
259 }
260 if (!gdev->dev.driver) {
261 ret = -EINVAL;
262 goto out;
263 }
264 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800265 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 goto out;
267
268 gdev->state = CCWGROUP_ONLINE;
269 out:
270 atomic_set(&gdev->onoff, 0);
271 return ret;
272}
273
274static int
275ccwgroup_set_offline(struct ccwgroup_device *gdev)
276{
277 struct ccwgroup_driver *gdrv;
278 int ret;
279
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800280 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -EAGAIN;
282 if (gdev->state == CCWGROUP_OFFLINE) {
283 ret = 0;
284 goto out;
285 }
286 if (!gdev->dev.driver) {
287 ret = -EINVAL;
288 goto out;
289 }
290 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800291 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto out;
293
294 gdev->state = CCWGROUP_OFFLINE;
295 out:
296 atomic_set(&gdev->onoff, 0);
297 return ret;
298}
299
300static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400301ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct ccwgroup_device *gdev;
304 struct ccwgroup_driver *gdrv;
305 unsigned int value;
306 int ret;
307
308 gdev = to_ccwgroupdev(dev);
309 if (!dev->driver)
310 return count;
311
312 gdrv = to_ccwgroupdrv (gdev->dev.driver);
313 if (!try_module_get(gdrv->owner))
314 return -EINVAL;
315
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200316 value = simple_strtoul(buf, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 ret = count;
318 if (value == 1)
319 ccwgroup_set_online(gdev);
320 else if (value == 0)
321 ccwgroup_set_offline(gdev);
322 else
323 ret = -EINVAL;
324 module_put(gdrv->owner);
325 return ret;
326}
327
328static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400329ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 int online;
332
333 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
334
335 return sprintf(buf, online ? "1\n" : "0\n");
336}
337
338static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
339
340static int
341ccwgroup_probe (struct device *dev)
342{
343 struct ccwgroup_device *gdev;
344 struct ccwgroup_driver *gdrv;
345
346 int ret;
347
348 gdev = to_ccwgroupdev(dev);
349 gdrv = to_ccwgroupdrv(dev->driver);
350
351 if ((ret = device_create_file(dev, &dev_attr_online)))
352 return ret;
353
354 pr_debug("%s: device %s\n", __func__, gdev->dev.bus_id);
355 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
356 if (ret)
357 device_remove_file(dev, &dev_attr_online);
358
359 return ret;
360}
361
362static int
363ccwgroup_remove (struct device *dev)
364{
365 struct ccwgroup_device *gdev;
366 struct ccwgroup_driver *gdrv;
367
368 gdev = to_ccwgroupdev(dev);
369 gdrv = to_ccwgroupdrv(dev->driver);
370
371 pr_debug("%s: device %s\n", __func__, gdev->dev.bus_id);
372
373 device_remove_file(dev, &dev_attr_online);
374
375 if (gdrv && gdrv->remove)
376 gdrv->remove(gdev);
377 return 0;
378}
379
Russell Kingf9ccf452006-01-05 14:42:09 +0000380static struct bus_type ccwgroup_bus_type = {
381 .name = "ccwgroup",
382 .match = ccwgroup_bus_match,
383 .uevent = ccwgroup_uevent,
384 .probe = ccwgroup_probe,
385 .remove = ccwgroup_remove,
386};
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388int
389ccwgroup_driver_register (struct ccwgroup_driver *cdriver)
390{
391 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200392 cdriver->driver.bus = &ccwgroup_bus_type;
393 cdriver->driver.name = cdriver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 return driver_register(&cdriver->driver);
396}
397
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700398static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200399__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Cornelia Huck887ab592006-06-29 14:56:52 +0200401 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404void
405ccwgroup_driver_unregister (struct ccwgroup_driver *cdriver)
406{
Cornelia Huck887ab592006-06-29 14:56:52 +0200407 struct device *dev;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* We don't want ccwgroup devices to live longer than their driver. */
410 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200411 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
412 __ccwgroup_match_all))) {
413 __ccwgroup_remove_symlinks(to_ccwgroupdev(dev));
414 device_unregister(dev);
415 put_device(dev);
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 put_driver(&cdriver->driver);
418 driver_unregister(&cdriver->driver);
419}
420
421int
422ccwgroup_probe_ccwdev(struct ccw_device *cdev)
423{
424 return 0;
425}
426
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100427static struct ccwgroup_device *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
429{
430 struct ccwgroup_device *gdev;
431
432 if (cdev->dev.driver_data) {
433 gdev = (struct ccwgroup_device *)cdev->dev.driver_data;
434 if (get_device(&gdev->dev)) {
Daniel Ritzd305ef52005-09-22 00:47:24 -0700435 if (device_is_registered(&gdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return gdev;
437 put_device(&gdev->dev);
438 }
439 return NULL;
440 }
441 return NULL;
442}
443
444void
445ccwgroup_remove_ccwdev(struct ccw_device *cdev)
446{
447 struct ccwgroup_device *gdev;
448
449 /* Ignore offlining errors, device is gone anyway. */
450 ccw_device_set_offline(cdev);
451 /* If one of its devices is gone, the whole group is done for. */
452 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
453 if (gdev) {
454 __ccwgroup_remove_symlinks(gdev);
455 device_unregister(&gdev->dev);
456 put_device(&gdev->dev);
457 }
458}
459
460MODULE_LICENSE("GPL");
461EXPORT_SYMBOL(ccwgroup_driver_register);
462EXPORT_SYMBOL(ccwgroup_driver_unregister);
463EXPORT_SYMBOL(ccwgroup_create);
464EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
465EXPORT_SYMBOL(ccwgroup_remove_ccwdev);