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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/ccwdev.h> |
| 20 | #include <asm/ccwgroup.h> |
| 21 | |
Kay Sievers | 98df67b | 2008-12-25 13:38:55 +0100 | [diff] [blame] | 22 | #define CCW_BUS_ID_SIZE 20 |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Cornelia Huck | 084325d | 2008-01-26 14:10:38 +0100 | [diff] [blame] | 39 | gdev = to_ccwgroupdev(dev); |
| 40 | gdrv = to_ccwgroupdrv(drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | if (gdev->creator_id == gdrv->driver_id) |
| 43 | return 1; |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | static int |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 48 | ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 56 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 74 | static void ccwgroup_ungroup_callback(struct device *dev) |
| 75 | { |
| 76 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 77 | |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 78 | mutex_lock(&gdev->reg_mutex); |
Cornelia Huck | 1a908c7 | 2008-01-26 14:10:50 +0100 | [diff] [blame] | 79 | if (device_is_registered(&gdev->dev)) { |
| 80 | __ccwgroup_remove_symlinks(gdev); |
| 81 | device_unregister(dev); |
| 82 | } |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 83 | mutex_unlock(&gdev->reg_mutex); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 87 | 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] | 88 | { |
| 89 | struct ccwgroup_device *gdev; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 90 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | gdev = to_ccwgroupdev(dev); |
| 93 | |
Peter Oberparleiter | c619d42 | 2008-12-25 13:39:04 +0100 | [diff] [blame^] | 94 | /* Prevent concurrent online/offline processing and ungrouping. */ |
| 95 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
| 96 | return -EAGAIN; |
| 97 | if (gdev->state != CCWGROUP_OFFLINE) { |
| 98 | rc = -EINVAL; |
| 99 | goto out; |
| 100 | } |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 101 | /* Note that we cannot unregister the device from one of its |
| 102 | * attribute methods, so we have to use this roundabout approach. |
| 103 | */ |
| 104 | rc = device_schedule_callback(dev, ccwgroup_ungroup_callback); |
Peter Oberparleiter | c619d42 | 2008-12-25 13:39:04 +0100 | [diff] [blame^] | 105 | out: |
| 106 | if (rc) { |
| 107 | /* Release onoff "lock" when ungrouping failed. */ |
| 108 | atomic_set(&gdev->onoff, 0); |
| 109 | return rc; |
| 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return count; |
| 112 | } |
| 113 | |
| 114 | static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store); |
| 115 | |
| 116 | static void |
| 117 | ccwgroup_release (struct device *dev) |
| 118 | { |
| 119 | struct ccwgroup_device *gdev; |
| 120 | int i; |
| 121 | |
| 122 | gdev = to_ccwgroupdev(dev); |
| 123 | |
| 124 | for (i = 0; i < gdev->count; i++) { |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 125 | if (gdev->cdev[i]) { |
Cornelia Huck | f26fd5d | 2008-09-16 09:32:18 -0700 | [diff] [blame] | 126 | if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev) |
| 127 | dev_set_drvdata(&gdev->cdev[i]->dev, NULL); |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 128 | put_device(&gdev->cdev[i]->dev); |
| 129 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | kfree(gdev); |
| 132 | } |
| 133 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 134 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | __ccwgroup_create_symlinks(struct ccwgroup_device *gdev) |
| 136 | { |
| 137 | char str[8]; |
| 138 | int i, rc; |
| 139 | |
| 140 | for (i = 0; i < gdev->count; i++) { |
| 141 | rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj, |
| 142 | "group_device"); |
| 143 | if (rc) { |
| 144 | for (--i; i >= 0; i--) |
| 145 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, |
| 146 | "group_device"); |
| 147 | return rc; |
| 148 | } |
| 149 | } |
| 150 | for (i = 0; i < gdev->count; i++) { |
| 151 | sprintf(str, "cdev%d", i); |
| 152 | rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj, |
| 153 | str); |
| 154 | if (rc) { |
| 155 | for (--i; i >= 0; i--) { |
| 156 | sprintf(str, "cdev%d", i); |
| 157 | sysfs_remove_link(&gdev->dev.kobj, str); |
| 158 | } |
| 159 | for (i = 0; i < gdev->count; i++) |
| 160 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, |
| 161 | "group_device"); |
| 162 | return rc; |
| 163 | } |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 168 | static int __get_next_bus_id(const char **buf, char *bus_id) |
| 169 | { |
| 170 | int rc, len; |
| 171 | char *start, *end; |
| 172 | |
| 173 | start = (char *)*buf; |
| 174 | end = strchr(start, ','); |
| 175 | if (!end) { |
| 176 | /* Last entry. Strip trailing newline, if applicable. */ |
| 177 | end = strchr(start, '\n'); |
| 178 | if (end) |
| 179 | *end = '\0'; |
| 180 | len = strlen(start) + 1; |
| 181 | } else { |
| 182 | len = end - start + 1; |
| 183 | end++; |
| 184 | } |
Kay Sievers | 98df67b | 2008-12-25 13:38:55 +0100 | [diff] [blame] | 185 | if (len < CCW_BUS_ID_SIZE) { |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 186 | strlcpy(bus_id, start, len); |
| 187 | rc = 0; |
| 188 | } else |
| 189 | rc = -EINVAL; |
| 190 | *buf = end; |
| 191 | return rc; |
| 192 | } |
| 193 | |
Kay Sievers | 98df67b | 2008-12-25 13:38:55 +0100 | [diff] [blame] | 194 | static int __is_valid_bus_id(char bus_id[CCW_BUS_ID_SIZE]) |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 195 | { |
| 196 | int cssid, ssid, devno; |
| 197 | |
| 198 | /* Must be of form %x.%x.%04x */ |
| 199 | if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3) |
| 200 | return 0; |
| 201 | return 1; |
| 202 | } |
| 203 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 204 | /** |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 205 | * ccwgroup_create_from_string() - create and register a ccw group device |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 206 | * @root: parent device for the new device |
| 207 | * @creator_id: identifier of creating driver |
| 208 | * @cdrv: ccw driver of slave devices |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 209 | * @num_devices: number of slave devices |
| 210 | * @buf: buffer containing comma separated bus ids of slave devices |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 211 | * |
| 212 | * Create and register a new ccw group device as a child of @root. Slave |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 213 | * devices are obtained from the list of bus ids given in @buf and must all |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 214 | * belong to @cdrv. |
| 215 | * Returns: |
| 216 | * %0 on success and an error code on failure. |
| 217 | * Context: |
| 218 | * non-atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | */ |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 220 | int ccwgroup_create_from_string(struct device *root, unsigned int creator_id, |
| 221 | struct ccw_driver *cdrv, int num_devices, |
| 222 | const char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
| 224 | struct ccwgroup_device *gdev; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 225 | int rc, i; |
Kay Sievers | 98df67b | 2008-12-25 13:38:55 +0100 | [diff] [blame] | 226 | char tmp_bus_id[CCW_BUS_ID_SIZE]; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 227 | const char *curr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 229 | gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]), |
| 230 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (!gdev) |
| 232 | return -ENOMEM; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | atomic_set(&gdev->onoff, 0); |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 235 | mutex_init(&gdev->reg_mutex); |
| 236 | mutex_lock(&gdev->reg_mutex); |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 237 | gdev->creator_id = creator_id; |
| 238 | gdev->count = num_devices; |
| 239 | gdev->dev.bus = &ccwgroup_bus_type; |
| 240 | gdev->dev.parent = root; |
| 241 | gdev->dev.release = ccwgroup_release; |
| 242 | device_initialize(&gdev->dev); |
| 243 | |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 244 | curr_buf = buf; |
| 245 | for (i = 0; i < num_devices && curr_buf; i++) { |
| 246 | rc = __get_next_bus_id(&curr_buf, tmp_bus_id); |
| 247 | if (rc != 0) |
| 248 | goto error; |
| 249 | if (!__is_valid_bus_id(tmp_bus_id)) { |
| 250 | rc = -EINVAL; |
| 251 | goto error; |
| 252 | } |
| 253 | gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id); |
| 254 | /* |
| 255 | * All devices have to be of the same type in |
| 256 | * order to be grouped. |
| 257 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (!gdev->cdev[i] |
| 259 | || gdev->cdev[i]->id.driver_info != |
| 260 | gdev->cdev[0]->id.driver_info) { |
| 261 | rc = -EINVAL; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 262 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | /* Don't allow a device to belong to more than one group. */ |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 265 | if (dev_get_drvdata(&gdev->cdev[i]->dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | rc = -EINVAL; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 267 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 269 | dev_set_drvdata(&gdev->cdev[i]->dev, gdev); |
Cornelia Huck | 1708822 | 2006-07-27 14:00:33 +0200 | [diff] [blame] | 270 | } |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 271 | /* Check for sufficient number of bus ids. */ |
| 272 | if (i < num_devices && !curr_buf) { |
| 273 | rc = -EINVAL; |
| 274 | goto error; |
| 275 | } |
| 276 | /* Check for trailing stuff. */ |
| 277 | if (i == num_devices && strlen(curr_buf) > 0) { |
| 278 | rc = -EINVAL; |
| 279 | goto error; |
| 280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Cornelia Huck | 1bf5b28 | 2008-10-10 21:33:10 +0200 | [diff] [blame] | 282 | dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 284 | rc = device_add(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (rc) |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 286 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | get_device(&gdev->dev); |
| 288 | rc = device_create_file(&gdev->dev, &dev_attr_ungroup); |
| 289 | |
| 290 | if (rc) { |
| 291 | device_unregister(&gdev->dev); |
| 292 | goto error; |
| 293 | } |
| 294 | |
| 295 | rc = __ccwgroup_create_symlinks(gdev); |
| 296 | if (!rc) { |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 297 | mutex_unlock(&gdev->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | put_device(&gdev->dev); |
| 299 | return 0; |
| 300 | } |
| 301 | device_remove_file(&gdev->dev, &dev_attr_ungroup); |
| 302 | device_unregister(&gdev->dev); |
| 303 | error: |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 304 | for (i = 0; i < num_devices; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | if (gdev->cdev[i]) { |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 306 | if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev) |
| 307 | dev_set_drvdata(&gdev->cdev[i]->dev, NULL); |
Cornelia Huck | 1708822 | 2006-07-27 14:00:33 +0200 | [diff] [blame] | 308 | put_device(&gdev->cdev[i]->dev); |
Cornelia Huck | f26fd5d | 2008-09-16 09:32:18 -0700 | [diff] [blame] | 309 | gdev->cdev[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 311 | mutex_unlock(&gdev->reg_mutex); |
| 312 | put_device(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return rc; |
| 314 | } |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 315 | EXPORT_SYMBOL(ccwgroup_create_from_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | static int __init |
| 318 | init_ccwgroup (void) |
| 319 | { |
| 320 | return bus_register (&ccwgroup_bus_type); |
| 321 | } |
| 322 | |
| 323 | static void __exit |
| 324 | cleanup_ccwgroup (void) |
| 325 | { |
| 326 | bus_unregister (&ccwgroup_bus_type); |
| 327 | } |
| 328 | |
| 329 | module_init(init_ccwgroup); |
| 330 | module_exit(cleanup_ccwgroup); |
| 331 | |
| 332 | /************************** driver stuff ******************************/ |
| 333 | |
| 334 | static int |
| 335 | ccwgroup_set_online(struct ccwgroup_device *gdev) |
| 336 | { |
| 337 | struct ccwgroup_driver *gdrv; |
| 338 | int ret; |
| 339 | |
Martin Schwidefsky | 973bd99 | 2006-01-06 00:19:07 -0800 | [diff] [blame] | 340 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return -EAGAIN; |
| 342 | if (gdev->state == CCWGROUP_ONLINE) { |
| 343 | ret = 0; |
| 344 | goto out; |
| 345 | } |
| 346 | if (!gdev->dev.driver) { |
| 347 | ret = -EINVAL; |
| 348 | goto out; |
| 349 | } |
| 350 | gdrv = to_ccwgroupdrv (gdev->dev.driver); |
Cornelia Huck | a001640 | 2005-11-07 00:59:05 -0800 | [diff] [blame] | 351 | if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | goto out; |
| 353 | |
| 354 | gdev->state = CCWGROUP_ONLINE; |
| 355 | out: |
| 356 | atomic_set(&gdev->onoff, 0); |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | static int |
| 361 | ccwgroup_set_offline(struct ccwgroup_device *gdev) |
| 362 | { |
| 363 | struct ccwgroup_driver *gdrv; |
| 364 | int ret; |
| 365 | |
Martin Schwidefsky | 973bd99 | 2006-01-06 00:19:07 -0800 | [diff] [blame] | 366 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return -EAGAIN; |
| 368 | if (gdev->state == CCWGROUP_OFFLINE) { |
| 369 | ret = 0; |
| 370 | goto out; |
| 371 | } |
| 372 | if (!gdev->dev.driver) { |
| 373 | ret = -EINVAL; |
| 374 | goto out; |
| 375 | } |
| 376 | gdrv = to_ccwgroupdrv (gdev->dev.driver); |
Cornelia Huck | a001640 | 2005-11-07 00:59:05 -0800 | [diff] [blame] | 377 | if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | goto out; |
| 379 | |
| 380 | gdev->state = CCWGROUP_OFFLINE; |
| 381 | out: |
| 382 | atomic_set(&gdev->onoff, 0); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 387 | 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] | 388 | { |
| 389 | struct ccwgroup_device *gdev; |
| 390 | struct ccwgroup_driver *gdrv; |
Cornelia Huck | 2f97220 | 2008-04-30 13:38:33 +0200 | [diff] [blame] | 391 | unsigned long value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | int ret; |
| 393 | |
| 394 | gdev = to_ccwgroupdev(dev); |
| 395 | if (!dev->driver) |
| 396 | return count; |
| 397 | |
| 398 | gdrv = to_ccwgroupdrv (gdev->dev.driver); |
| 399 | if (!try_module_get(gdrv->owner)) |
| 400 | return -EINVAL; |
| 401 | |
Cornelia Huck | 2f97220 | 2008-04-30 13:38:33 +0200 | [diff] [blame] | 402 | ret = strict_strtoul(buf, 0, &value); |
| 403 | if (ret) |
| 404 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | ret = count; |
| 406 | if (value == 1) |
| 407 | ccwgroup_set_online(gdev); |
| 408 | else if (value == 0) |
| 409 | ccwgroup_set_offline(gdev); |
| 410 | else |
| 411 | ret = -EINVAL; |
Cornelia Huck | 2f97220 | 2008-04-30 13:38:33 +0200 | [diff] [blame] | 412 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | module_put(gdrv->owner); |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 418 | ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { |
| 420 | int online; |
| 421 | |
| 422 | online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE); |
| 423 | |
| 424 | return sprintf(buf, online ? "1\n" : "0\n"); |
| 425 | } |
| 426 | |
| 427 | static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store); |
| 428 | |
| 429 | static int |
| 430 | ccwgroup_probe (struct device *dev) |
| 431 | { |
| 432 | struct ccwgroup_device *gdev; |
| 433 | struct ccwgroup_driver *gdrv; |
| 434 | |
| 435 | int ret; |
| 436 | |
| 437 | gdev = to_ccwgroupdev(dev); |
| 438 | gdrv = to_ccwgroupdrv(dev->driver); |
| 439 | |
| 440 | if ((ret = device_create_file(dev, &dev_attr_online))) |
| 441 | return ret; |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV; |
| 444 | if (ret) |
| 445 | device_remove_file(dev, &dev_attr_online); |
| 446 | |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | static int |
| 451 | ccwgroup_remove (struct device *dev) |
| 452 | { |
| 453 | struct ccwgroup_device *gdev; |
| 454 | struct ccwgroup_driver *gdrv; |
| 455 | |
| 456 | gdev = to_ccwgroupdev(dev); |
| 457 | gdrv = to_ccwgroupdrv(dev->driver); |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | device_remove_file(dev, &dev_attr_online); |
| 460 | |
| 461 | if (gdrv && gdrv->remove) |
| 462 | gdrv->remove(gdev); |
| 463 | return 0; |
| 464 | } |
| 465 | |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 466 | static void ccwgroup_shutdown(struct device *dev) |
| 467 | { |
| 468 | struct ccwgroup_device *gdev; |
| 469 | struct ccwgroup_driver *gdrv; |
| 470 | |
| 471 | gdev = to_ccwgroupdev(dev); |
| 472 | gdrv = to_ccwgroupdrv(dev->driver); |
| 473 | if (gdrv && gdrv->shutdown) |
| 474 | gdrv->shutdown(gdev); |
| 475 | } |
| 476 | |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 477 | static struct bus_type ccwgroup_bus_type = { |
| 478 | .name = "ccwgroup", |
| 479 | .match = ccwgroup_bus_match, |
| 480 | .uevent = ccwgroup_uevent, |
| 481 | .probe = ccwgroup_probe, |
| 482 | .remove = ccwgroup_remove, |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 483 | .shutdown = ccwgroup_shutdown, |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 486 | /** |
| 487 | * ccwgroup_driver_register() - register a ccw group driver |
| 488 | * @cdriver: driver to be registered |
| 489 | * |
| 490 | * This function is mainly a wrapper around driver_register(). |
| 491 | */ |
| 492 | int ccwgroup_driver_register(struct ccwgroup_driver *cdriver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
| 494 | /* register our new driver with the core */ |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 495 | cdriver->driver.bus = &ccwgroup_bus_type; |
| 496 | cdriver->driver.name = cdriver->name; |
Cornelia Huck | 4beee64 | 2008-01-26 14:10:47 +0100 | [diff] [blame] | 497 | cdriver->driver.owner = cdriver->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
| 499 | return driver_register(&cdriver->driver); |
| 500 | } |
| 501 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 502 | static int |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 503 | __ccwgroup_match_all(struct device *dev, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 505 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 508 | /** |
| 509 | * ccwgroup_driver_unregister() - deregister a ccw group driver |
| 510 | * @cdriver: driver to be deregistered |
| 511 | * |
| 512 | * This function is mainly a wrapper around driver_unregister(). |
| 513 | */ |
| 514 | void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 516 | struct device *dev; |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* We don't want ccwgroup devices to live longer than their driver. */ |
| 519 | get_driver(&cdriver->driver); |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 520 | while ((dev = driver_find_device(&cdriver->driver, NULL, NULL, |
| 521 | __ccwgroup_match_all))) { |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 522 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 523 | |
| 524 | mutex_lock(&gdev->reg_mutex); |
| 525 | __ccwgroup_remove_symlinks(gdev); |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 526 | device_unregister(dev); |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 527 | mutex_unlock(&gdev->reg_mutex); |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 528 | put_device(dev); |
| 529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | put_driver(&cdriver->driver); |
| 531 | driver_unregister(&cdriver->driver); |
| 532 | } |
| 533 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 534 | /** |
| 535 | * ccwgroup_probe_ccwdev() - probe function for slave devices |
| 536 | * @cdev: ccw device to be probed |
| 537 | * |
| 538 | * This is a dummy probe function for ccw devices that are slave devices in |
| 539 | * a ccw group device. |
| 540 | * Returns: |
| 541 | * always %0 |
| 542 | */ |
| 543 | int ccwgroup_probe_ccwdev(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
| 545 | return 0; |
| 546 | } |
| 547 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 548 | static struct ccwgroup_device * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev) |
| 550 | { |
| 551 | struct ccwgroup_device *gdev; |
| 552 | |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 553 | gdev = dev_get_drvdata(&cdev->dev); |
| 554 | if (gdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | if (get_device(&gdev->dev)) { |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 556 | mutex_lock(&gdev->reg_mutex); |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 557 | if (device_is_registered(&gdev->dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | return gdev; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 559 | mutex_unlock(&gdev->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | put_device(&gdev->dev); |
| 561 | } |
| 562 | return NULL; |
| 563 | } |
| 564 | return NULL; |
| 565 | } |
| 566 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 567 | /** |
| 568 | * ccwgroup_remove_ccwdev() - remove function for slave devices |
| 569 | * @cdev: ccw device to be removed |
| 570 | * |
| 571 | * This is a remove function for ccw devices that are slave devices in a ccw |
| 572 | * group device. It sets the ccw device offline and also deregisters the |
| 573 | * embedding ccw group device. |
| 574 | */ |
| 575 | void ccwgroup_remove_ccwdev(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
| 577 | struct ccwgroup_device *gdev; |
| 578 | |
| 579 | /* Ignore offlining errors, device is gone anyway. */ |
| 580 | ccw_device_set_offline(cdev); |
| 581 | /* If one of its devices is gone, the whole group is done for. */ |
| 582 | gdev = __ccwgroup_get_gdev_by_cdev(cdev); |
| 583 | if (gdev) { |
| 584 | __ccwgroup_remove_symlinks(gdev); |
| 585 | device_unregister(&gdev->dev); |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 586 | mutex_unlock(&gdev->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | put_device(&gdev->dev); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | MODULE_LICENSE("GPL"); |
| 592 | EXPORT_SYMBOL(ccwgroup_driver_register); |
| 593 | EXPORT_SYMBOL(ccwgroup_driver_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | EXPORT_SYMBOL(ccwgroup_probe_ccwdev); |
| 595 | EXPORT_SYMBOL(ccwgroup_remove_ccwdev); |