Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/s390/cio/ccwgroup.c |
| 3 | * bus driver for ccwgroup |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, |
| 6 | * IBM Corporation |
| 7 | * Author(s): Arnd Bergmann (arndb@de.ibm.com) |
Cornelia Huck | 4ce3b30 | 2006-01-14 13:21:04 -0800 | [diff] [blame] | 8 | * Cornelia Huck (cornelia.huck@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Paul Jackson | 6d20b03 | 2005-11-25 20:04:26 -0800 | [diff] [blame] | 47 | ccwgroup_uevent (struct device *dev, char **envp, int num_envp, char *buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | int buffer_size) |
| 49 | { |
| 50 | /* TODO */ |
| 51 | return 0; |
| 52 | } |
| 53 | |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 54 | static struct bus_type ccwgroup_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 75 | 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] | 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 | int del_drvdata; |
| 156 | |
| 157 | if (argc > 256) /* disallow dumb users */ |
| 158 | return -EINVAL; |
| 159 | |
| 160 | gdev = kmalloc(sizeof(*gdev) + argc*sizeof(gdev->cdev[0]), GFP_KERNEL); |
| 161 | if (!gdev) |
| 162 | return -ENOMEM; |
| 163 | |
| 164 | memset(gdev, 0, sizeof(*gdev) + argc*sizeof(gdev->cdev[0])); |
| 165 | atomic_set(&gdev->onoff, 0); |
| 166 | |
| 167 | del_drvdata = 0; |
| 168 | for (i = 0; i < argc; i++) { |
| 169 | gdev->cdev[i] = get_ccwdev_by_busid(cdrv, argv[i]); |
| 170 | |
| 171 | /* all devices have to be of the same type in |
| 172 | * order to be grouped */ |
| 173 | if (!gdev->cdev[i] |
| 174 | || gdev->cdev[i]->id.driver_info != |
| 175 | gdev->cdev[0]->id.driver_info) { |
| 176 | rc = -EINVAL; |
| 177 | goto free_dev; |
| 178 | } |
| 179 | /* Don't allow a device to belong to more than one group. */ |
| 180 | if (gdev->cdev[i]->dev.driver_data) { |
| 181 | rc = -EINVAL; |
| 182 | goto free_dev; |
| 183 | } |
| 184 | } |
| 185 | for (i = 0; i < argc; i++) |
| 186 | gdev->cdev[i]->dev.driver_data = gdev; |
| 187 | del_drvdata = 1; |
| 188 | |
| 189 | gdev->creator_id = creator_id; |
| 190 | gdev->count = argc; |
| 191 | gdev->dev = (struct device ) { |
| 192 | .bus = &ccwgroup_bus_type, |
| 193 | .parent = root, |
| 194 | .release = ccwgroup_release, |
| 195 | }; |
| 196 | |
| 197 | snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s", |
| 198 | gdev->cdev[0]->dev.bus_id); |
| 199 | |
| 200 | rc = device_register(&gdev->dev); |
| 201 | |
| 202 | if (rc) |
| 203 | goto free_dev; |
| 204 | get_device(&gdev->dev); |
| 205 | rc = device_create_file(&gdev->dev, &dev_attr_ungroup); |
| 206 | |
| 207 | if (rc) { |
| 208 | device_unregister(&gdev->dev); |
| 209 | goto error; |
| 210 | } |
| 211 | |
| 212 | rc = __ccwgroup_create_symlinks(gdev); |
| 213 | if (!rc) { |
| 214 | put_device(&gdev->dev); |
| 215 | return 0; |
| 216 | } |
| 217 | device_remove_file(&gdev->dev, &dev_attr_ungroup); |
| 218 | device_unregister(&gdev->dev); |
| 219 | error: |
| 220 | for (i = 0; i < argc; i++) |
| 221 | if (gdev->cdev[i]) { |
| 222 | put_device(&gdev->cdev[i]->dev); |
| 223 | gdev->cdev[i]->dev.driver_data = NULL; |
| 224 | } |
| 225 | put_device(&gdev->dev); |
| 226 | return rc; |
| 227 | free_dev: |
| 228 | for (i = 0; i < argc; i++) |
| 229 | if (gdev->cdev[i]) { |
| 230 | put_device(&gdev->cdev[i]->dev); |
| 231 | if (del_drvdata) |
| 232 | gdev->cdev[i]->dev.driver_data = NULL; |
| 233 | } |
| 234 | kfree(gdev); |
| 235 | return rc; |
| 236 | } |
| 237 | |
| 238 | static int __init |
| 239 | init_ccwgroup (void) |
| 240 | { |
| 241 | return bus_register (&ccwgroup_bus_type); |
| 242 | } |
| 243 | |
| 244 | static void __exit |
| 245 | cleanup_ccwgroup (void) |
| 246 | { |
| 247 | bus_unregister (&ccwgroup_bus_type); |
| 248 | } |
| 249 | |
| 250 | module_init(init_ccwgroup); |
| 251 | module_exit(cleanup_ccwgroup); |
| 252 | |
| 253 | /************************** driver stuff ******************************/ |
| 254 | |
| 255 | static int |
| 256 | ccwgroup_set_online(struct ccwgroup_device *gdev) |
| 257 | { |
| 258 | struct ccwgroup_driver *gdrv; |
| 259 | int ret; |
| 260 | |
Martin Schwidefsky | 973bd99 | 2006-01-06 00:19:07 -0800 | [diff] [blame] | 261 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return -EAGAIN; |
| 263 | if (gdev->state == CCWGROUP_ONLINE) { |
| 264 | ret = 0; |
| 265 | goto out; |
| 266 | } |
| 267 | if (!gdev->dev.driver) { |
| 268 | ret = -EINVAL; |
| 269 | goto out; |
| 270 | } |
| 271 | gdrv = to_ccwgroupdrv (gdev->dev.driver); |
Cornelia Huck | a001640 | 2005-11-07 00:59:05 -0800 | [diff] [blame] | 272 | if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | goto out; |
| 274 | |
| 275 | gdev->state = CCWGROUP_ONLINE; |
| 276 | out: |
| 277 | atomic_set(&gdev->onoff, 0); |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static int |
| 282 | ccwgroup_set_offline(struct ccwgroup_device *gdev) |
| 283 | { |
| 284 | struct ccwgroup_driver *gdrv; |
| 285 | int ret; |
| 286 | |
Martin Schwidefsky | 973bd99 | 2006-01-06 00:19:07 -0800 | [diff] [blame] | 287 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return -EAGAIN; |
| 289 | if (gdev->state == CCWGROUP_OFFLINE) { |
| 290 | ret = 0; |
| 291 | goto out; |
| 292 | } |
| 293 | if (!gdev->dev.driver) { |
| 294 | ret = -EINVAL; |
| 295 | goto out; |
| 296 | } |
| 297 | gdrv = to_ccwgroupdrv (gdev->dev.driver); |
Cornelia Huck | a001640 | 2005-11-07 00:59:05 -0800 | [diff] [blame] | 298 | if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | goto out; |
| 300 | |
| 301 | gdev->state = CCWGROUP_OFFLINE; |
| 302 | out: |
| 303 | atomic_set(&gdev->onoff, 0); |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 308 | 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] | 309 | { |
| 310 | struct ccwgroup_device *gdev; |
| 311 | struct ccwgroup_driver *gdrv; |
| 312 | unsigned int value; |
| 313 | int ret; |
| 314 | |
| 315 | gdev = to_ccwgroupdev(dev); |
| 316 | if (!dev->driver) |
| 317 | return count; |
| 318 | |
| 319 | gdrv = to_ccwgroupdrv (gdev->dev.driver); |
| 320 | if (!try_module_get(gdrv->owner)) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | value = simple_strtoul(buf, 0, 0); |
| 324 | ret = count; |
| 325 | if (value == 1) |
| 326 | ccwgroup_set_online(gdev); |
| 327 | else if (value == 0) |
| 328 | ccwgroup_set_offline(gdev); |
| 329 | else |
| 330 | ret = -EINVAL; |
| 331 | module_put(gdrv->owner); |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 336 | ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
| 338 | int online; |
| 339 | |
| 340 | online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE); |
| 341 | |
| 342 | return sprintf(buf, online ? "1\n" : "0\n"); |
| 343 | } |
| 344 | |
| 345 | static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store); |
| 346 | |
| 347 | static int |
| 348 | ccwgroup_probe (struct device *dev) |
| 349 | { |
| 350 | struct ccwgroup_device *gdev; |
| 351 | struct ccwgroup_driver *gdrv; |
| 352 | |
| 353 | int ret; |
| 354 | |
| 355 | gdev = to_ccwgroupdev(dev); |
| 356 | gdrv = to_ccwgroupdrv(dev->driver); |
| 357 | |
| 358 | if ((ret = device_create_file(dev, &dev_attr_online))) |
| 359 | return ret; |
| 360 | |
| 361 | pr_debug("%s: device %s\n", __func__, gdev->dev.bus_id); |
| 362 | ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV; |
| 363 | if (ret) |
| 364 | device_remove_file(dev, &dev_attr_online); |
| 365 | |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static int |
| 370 | ccwgroup_remove (struct device *dev) |
| 371 | { |
| 372 | struct ccwgroup_device *gdev; |
| 373 | struct ccwgroup_driver *gdrv; |
| 374 | |
| 375 | gdev = to_ccwgroupdev(dev); |
| 376 | gdrv = to_ccwgroupdrv(dev->driver); |
| 377 | |
| 378 | pr_debug("%s: device %s\n", __func__, gdev->dev.bus_id); |
| 379 | |
| 380 | device_remove_file(dev, &dev_attr_online); |
| 381 | |
| 382 | if (gdrv && gdrv->remove) |
| 383 | gdrv->remove(gdev); |
| 384 | return 0; |
| 385 | } |
| 386 | |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 387 | static struct bus_type ccwgroup_bus_type = { |
| 388 | .name = "ccwgroup", |
| 389 | .match = ccwgroup_bus_match, |
| 390 | .uevent = ccwgroup_uevent, |
| 391 | .probe = ccwgroup_probe, |
| 392 | .remove = ccwgroup_remove, |
| 393 | }; |
| 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | int |
| 396 | ccwgroup_driver_register (struct ccwgroup_driver *cdriver) |
| 397 | { |
| 398 | /* register our new driver with the core */ |
| 399 | cdriver->driver = (struct device_driver) { |
| 400 | .bus = &ccwgroup_bus_type, |
| 401 | .name = cdriver->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | return driver_register(&cdriver->driver); |
| 405 | } |
| 406 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 407 | static int |
| 408 | __ccwgroup_driver_unregister_device(struct device *dev, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 410 | __ccwgroup_remove_symlinks(to_ccwgroupdev(dev)); |
| 411 | device_unregister(dev); |
| 412 | put_device(dev); |
| 413 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void |
| 417 | ccwgroup_driver_unregister (struct ccwgroup_driver *cdriver) |
| 418 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | /* We don't want ccwgroup devices to live longer than their driver. */ |
| 420 | get_driver(&cdriver->driver); |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 421 | driver_for_each_device(&cdriver->driver, NULL, NULL, |
| 422 | __ccwgroup_driver_unregister_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | put_driver(&cdriver->driver); |
| 424 | driver_unregister(&cdriver->driver); |
| 425 | } |
| 426 | |
| 427 | int |
| 428 | ccwgroup_probe_ccwdev(struct ccw_device *cdev) |
| 429 | { |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static inline struct ccwgroup_device * |
| 434 | __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev) |
| 435 | { |
| 436 | struct ccwgroup_device *gdev; |
| 437 | |
| 438 | if (cdev->dev.driver_data) { |
| 439 | gdev = (struct ccwgroup_device *)cdev->dev.driver_data; |
| 440 | if (get_device(&gdev->dev)) { |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 441 | if (device_is_registered(&gdev->dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | return gdev; |
| 443 | put_device(&gdev->dev); |
| 444 | } |
| 445 | return NULL; |
| 446 | } |
| 447 | return NULL; |
| 448 | } |
| 449 | |
| 450 | void |
| 451 | ccwgroup_remove_ccwdev(struct ccw_device *cdev) |
| 452 | { |
| 453 | struct ccwgroup_device *gdev; |
| 454 | |
| 455 | /* Ignore offlining errors, device is gone anyway. */ |
| 456 | ccw_device_set_offline(cdev); |
| 457 | /* If one of its devices is gone, the whole group is done for. */ |
| 458 | gdev = __ccwgroup_get_gdev_by_cdev(cdev); |
| 459 | if (gdev) { |
| 460 | __ccwgroup_remove_symlinks(gdev); |
| 461 | device_unregister(&gdev->dev); |
| 462 | put_device(&gdev->dev); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | MODULE_LICENSE("GPL"); |
| 467 | EXPORT_SYMBOL(ccwgroup_driver_register); |
| 468 | EXPORT_SYMBOL(ccwgroup_driver_unregister); |
| 469 | EXPORT_SYMBOL(ccwgroup_create); |
| 470 | EXPORT_SYMBOL(ccwgroup_probe_ccwdev); |
| 471 | EXPORT_SYMBOL(ccwgroup_remove_ccwdev); |