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