Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * bus driver for ccwgroup |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2002, 2012 |
Sebastian Ott | 7e597a2 | 2009-06-16 10:30:21 +0200 | [diff] [blame] | 5 | * |
| 6 | * Author(s): Arnd Bergmann (arndb@de.ibm.com) |
| 7 | * Cornelia Huck (cornelia.huck@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/ctype.h> |
| 16 | #include <linux/dcache.h> |
| 17 | |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 18 | #include <asm/cio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/ccwdev.h> |
| 20 | #include <asm/ccwgroup.h> |
| 21 | |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 22 | #include "device.h" |
| 23 | |
| 24 | #define CCW_BUS_ID_SIZE 10 |
Kay Sievers | 98df67b | 2008-12-25 13:38:55 +0100 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* In Linux 2.4, we had a channel device layer called "chandev" |
| 27 | * that did all sorts of obscure stuff for networking devices. |
| 28 | * This is another driver that serves as a replacement for just |
| 29 | * one of its functions, namely the translation of single subchannels |
| 30 | * to devices that use multiple subchannels. |
| 31 | */ |
| 32 | |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 33 | static struct bus_type ccwgroup_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 35 | static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 37 | int i; |
| 38 | char str[8]; |
| 39 | |
| 40 | for (i = 0; i < gdev->count; i++) { |
| 41 | sprintf(str, "cdev%d", i); |
| 42 | sysfs_remove_link(&gdev->dev.kobj, str); |
| 43 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device"); |
| 44 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 48 | * Remove references from ccw devices to ccw group device and from |
| 49 | * ccw group device to ccw devices. |
| 50 | */ |
| 51 | static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev) |
| 52 | { |
| 53 | struct ccw_device *cdev; |
| 54 | int i; |
| 55 | |
| 56 | for (i = 0; i < gdev->count; i++) { |
| 57 | cdev = gdev->cdev[i]; |
| 58 | if (!cdev) |
| 59 | continue; |
| 60 | spin_lock_irq(cdev->ccwlock); |
| 61 | dev_set_drvdata(&cdev->dev, NULL); |
| 62 | spin_unlock_irq(cdev->ccwlock); |
| 63 | gdev->cdev[i] = NULL; |
| 64 | put_device(&cdev->dev); |
| 65 | } |
| 66 | } |
| 67 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 68 | static int ccwgroup_set_online(struct ccwgroup_device *gdev) |
| 69 | { |
| 70 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); |
| 71 | int ret = 0; |
| 72 | |
| 73 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
| 74 | return -EAGAIN; |
| 75 | if (gdev->state == CCWGROUP_ONLINE) |
| 76 | goto out; |
| 77 | if (gdrv->set_online) |
| 78 | ret = gdrv->set_online(gdev); |
| 79 | if (ret) |
| 80 | goto out; |
| 81 | |
| 82 | gdev->state = CCWGROUP_ONLINE; |
| 83 | out: |
| 84 | atomic_set(&gdev->onoff, 0); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static int ccwgroup_set_offline(struct ccwgroup_device *gdev) |
| 89 | { |
| 90 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); |
| 91 | int ret = 0; |
| 92 | |
| 93 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
| 94 | return -EAGAIN; |
| 95 | if (gdev->state == CCWGROUP_OFFLINE) |
| 96 | goto out; |
| 97 | if (gdrv->set_offline) |
| 98 | ret = gdrv->set_offline(gdev); |
| 99 | if (ret) |
| 100 | goto out; |
| 101 | |
| 102 | gdev->state = CCWGROUP_OFFLINE; |
| 103 | out: |
| 104 | atomic_set(&gdev->onoff, 0); |
| 105 | return ret; |
| 106 | } |
| 107 | |
Sebastian Ott | dbdf1af | 2011-10-30 15:16:52 +0100 | [diff] [blame] | 108 | static ssize_t ccwgroup_online_store(struct device *dev, |
| 109 | struct device_attribute *attr, |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 110 | const char *buf, size_t count) |
| 111 | { |
| 112 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 113 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver); |
| 114 | unsigned long value; |
| 115 | int ret; |
| 116 | |
| 117 | if (!dev->driver) |
| 118 | return -EINVAL; |
| 119 | if (!try_module_get(gdrv->driver.owner)) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | ret = strict_strtoul(buf, 0, &value); |
| 123 | if (ret) |
| 124 | goto out; |
| 125 | |
| 126 | if (value == 1) |
| 127 | ret = ccwgroup_set_online(gdev); |
| 128 | else if (value == 0) |
| 129 | ret = ccwgroup_set_offline(gdev); |
| 130 | else |
| 131 | ret = -EINVAL; |
| 132 | out: |
| 133 | module_put(gdrv->driver.owner); |
| 134 | return (ret == 0) ? count : ret; |
| 135 | } |
| 136 | |
Sebastian Ott | dbdf1af | 2011-10-30 15:16:52 +0100 | [diff] [blame] | 137 | static ssize_t ccwgroup_online_show(struct device *dev, |
| 138 | struct device_attribute *attr, |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 139 | char *buf) |
| 140 | { |
| 141 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 142 | int online; |
| 143 | |
| 144 | online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0; |
| 145 | |
| 146 | return scnprintf(buf, PAGE_SIZE, "%d\n", online); |
| 147 | } |
| 148 | |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 149 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | * Provide an 'ungroup' attribute so the user can remove group devices no |
| 151 | * longer needed or accidentially created. Saves memory :) |
| 152 | */ |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 153 | static void ccwgroup_ungroup_callback(struct device *dev) |
| 154 | { |
| 155 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 156 | |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 157 | mutex_lock(&gdev->reg_mutex); |
Cornelia Huck | 1a908c7 | 2008-01-26 14:10:50 +0100 | [diff] [blame] | 158 | if (device_is_registered(&gdev->dev)) { |
| 159 | __ccwgroup_remove_symlinks(gdev); |
| 160 | device_unregister(dev); |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 161 | __ccwgroup_remove_cdev_refs(gdev); |
Cornelia Huck | 1a908c7 | 2008-01-26 14:10:50 +0100 | [diff] [blame] | 162 | } |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 163 | mutex_unlock(&gdev->reg_mutex); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 166 | static ssize_t ccwgroup_ungroup_store(struct device *dev, |
| 167 | struct device_attribute *attr, |
| 168 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 170 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 171 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Peter Oberparleiter | c619d42 | 2008-12-25 13:39:04 +0100 | [diff] [blame] | 173 | /* Prevent concurrent online/offline processing and ungrouping. */ |
| 174 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
| 175 | return -EAGAIN; |
| 176 | if (gdev->state != CCWGROUP_OFFLINE) { |
| 177 | rc = -EINVAL; |
| 178 | goto out; |
| 179 | } |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 180 | /* Note that we cannot unregister the device from one of its |
| 181 | * attribute methods, so we have to use this roundabout approach. |
| 182 | */ |
| 183 | rc = device_schedule_callback(dev, ccwgroup_ungroup_callback); |
Peter Oberparleiter | c619d42 | 2008-12-25 13:39:04 +0100 | [diff] [blame] | 184 | out: |
| 185 | if (rc) { |
Alex Chiang | 6694206 | 2009-03-13 12:07:36 -0600 | [diff] [blame] | 186 | if (rc != -EAGAIN) |
| 187 | /* Release onoff "lock" when ungrouping failed. */ |
| 188 | atomic_set(&gdev->onoff, 0); |
Peter Oberparleiter | c619d42 | 2008-12-25 13:39:04 +0100 | [diff] [blame] | 189 | return rc; |
| 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return count; |
| 192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store); |
Sebastian Ott | dbdf1af | 2011-10-30 15:16:52 +0100 | [diff] [blame] | 194 | static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store); |
| 195 | |
| 196 | static struct attribute *ccwgroup_attrs[] = { |
| 197 | &dev_attr_online.attr, |
| 198 | &dev_attr_ungroup.attr, |
| 199 | NULL, |
| 200 | }; |
| 201 | static struct attribute_group ccwgroup_attr_group = { |
| 202 | .attrs = ccwgroup_attrs, |
| 203 | }; |
| 204 | static const struct attribute_group *ccwgroup_attr_groups[] = { |
| 205 | &ccwgroup_attr_group, |
| 206 | NULL, |
| 207 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 209 | static void ccwgroup_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 211 | kfree(to_ccwgroupdev(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 214 | static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | char str[8]; |
| 217 | int i, rc; |
| 218 | |
| 219 | for (i = 0; i < gdev->count; i++) { |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 220 | rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, |
| 221 | &gdev->dev.kobj, "group_device"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | if (rc) { |
| 223 | for (--i; i >= 0; i--) |
| 224 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, |
| 225 | "group_device"); |
| 226 | return rc; |
| 227 | } |
| 228 | } |
| 229 | for (i = 0; i < gdev->count; i++) { |
| 230 | sprintf(str, "cdev%d", i); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 231 | rc = sysfs_create_link(&gdev->dev.kobj, |
| 232 | &gdev->cdev[i]->dev.kobj, str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (rc) { |
| 234 | for (--i; i >= 0; i--) { |
| 235 | sprintf(str, "cdev%d", i); |
| 236 | sysfs_remove_link(&gdev->dev.kobj, str); |
| 237 | } |
| 238 | for (i = 0; i < gdev->count; i++) |
| 239 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, |
| 240 | "group_device"); |
| 241 | return rc; |
| 242 | } |
| 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 247 | static int __get_next_id(const char **buf, struct ccw_dev_id *id) |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 248 | { |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 249 | unsigned int cssid, ssid, devno; |
| 250 | int ret = 0, len; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 251 | char *start, *end; |
| 252 | |
| 253 | start = (char *)*buf; |
| 254 | end = strchr(start, ','); |
| 255 | if (!end) { |
| 256 | /* Last entry. Strip trailing newline, if applicable. */ |
| 257 | end = strchr(start, '\n'); |
| 258 | if (end) |
| 259 | *end = '\0'; |
| 260 | len = strlen(start) + 1; |
| 261 | } else { |
| 262 | len = end - start + 1; |
| 263 | end++; |
| 264 | } |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 265 | if (len <= CCW_BUS_ID_SIZE) { |
| 266 | if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3) |
| 267 | ret = -EINVAL; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 268 | } else |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 269 | ret = -EINVAL; |
| 270 | |
| 271 | if (!ret) { |
| 272 | id->ssid = ssid; |
| 273 | id->devno = devno; |
| 274 | } |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 275 | *buf = end; |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 276 | return ret; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 279 | /** |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 280 | * ccwgroup_create_dev() - create and register a ccw group device |
| 281 | * @parent: parent device for the new device |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 282 | * @gdrv: driver for the new group device |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 283 | * @num_devices: number of slave devices |
| 284 | * @buf: buffer containing comma separated bus ids of slave devices |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 285 | * |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 286 | * Create and register a new ccw group device as a child of @parent. Slave |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 287 | * devices are obtained from the list of bus ids given in @buf. |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 288 | * Returns: |
| 289 | * %0 on success and an error code on failure. |
| 290 | * Context: |
| 291 | * non-atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | */ |
Sebastian Ott | 9814fdf | 2012-05-15 18:03:46 +0200 | [diff] [blame] | 293 | int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv, |
| 294 | int num_devices, const char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | { |
| 296 | struct ccwgroup_device *gdev; |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 297 | struct ccw_dev_id dev_id; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 298 | int rc, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 300 | gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]), |
| 301 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (!gdev) |
| 303 | return -ENOMEM; |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | atomic_set(&gdev->onoff, 0); |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 306 | mutex_init(&gdev->reg_mutex); |
| 307 | mutex_lock(&gdev->reg_mutex); |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 308 | gdev->count = num_devices; |
| 309 | gdev->dev.bus = &ccwgroup_bus_type; |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 310 | gdev->dev.parent = parent; |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 311 | gdev->dev.release = ccwgroup_release; |
| 312 | device_initialize(&gdev->dev); |
| 313 | |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 314 | for (i = 0; i < num_devices && buf; i++) { |
| 315 | rc = __get_next_id(&buf, &dev_id); |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 316 | if (rc != 0) |
| 317 | goto error; |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 318 | gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id); |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 319 | /* |
| 320 | * All devices have to be of the same type in |
| 321 | * order to be grouped. |
| 322 | */ |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 323 | if (!gdev->cdev[i] || !gdev->cdev[i]->drv || |
| 324 | gdev->cdev[i]->drv != gdev->cdev[0]->drv || |
| 325 | gdev->cdev[i]->id.driver_info != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | gdev->cdev[0]->id.driver_info) { |
| 327 | rc = -EINVAL; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 328 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | /* Don't allow a device to belong to more than one group. */ |
Sebastian Ott | c560d10 | 2010-05-26 23:27:07 +0200 | [diff] [blame] | 331 | spin_lock_irq(gdev->cdev[i]->ccwlock); |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 332 | if (dev_get_drvdata(&gdev->cdev[i]->dev)) { |
Sebastian Ott | c560d10 | 2010-05-26 23:27:07 +0200 | [diff] [blame] | 333 | spin_unlock_irq(gdev->cdev[i]->ccwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | rc = -EINVAL; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 335 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 337 | dev_set_drvdata(&gdev->cdev[i]->dev, gdev); |
Sebastian Ott | c560d10 | 2010-05-26 23:27:07 +0200 | [diff] [blame] | 338 | spin_unlock_irq(gdev->cdev[i]->ccwlock); |
Cornelia Huck | 1708822 | 2006-07-27 14:00:33 +0200 | [diff] [blame] | 339 | } |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 340 | /* Check for sufficient number of bus ids. */ |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 341 | if (i < num_devices) { |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 342 | rc = -EINVAL; |
| 343 | goto error; |
| 344 | } |
| 345 | /* Check for trailing stuff. */ |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 346 | if (i == num_devices && strlen(buf) > 0) { |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 347 | rc = -EINVAL; |
| 348 | goto error; |
| 349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Cornelia Huck | 1bf5b28 | 2008-10-10 21:33:10 +0200 | [diff] [blame] | 351 | dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev)); |
Sebastian Ott | dbdf1af | 2011-10-30 15:16:52 +0100 | [diff] [blame] | 352 | gdev->dev.groups = ccwgroup_attr_groups; |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 353 | |
| 354 | if (gdrv) { |
| 355 | gdev->dev.driver = &gdrv->driver; |
| 356 | rc = gdrv->setup ? gdrv->setup(gdev) : 0; |
| 357 | if (rc) |
| 358 | goto error; |
| 359 | } |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 360 | rc = device_add(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | if (rc) |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 362 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | rc = __ccwgroup_create_symlinks(gdev); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 364 | if (rc) { |
| 365 | device_del(&gdev->dev); |
| 366 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 368 | mutex_unlock(&gdev->reg_mutex); |
| 369 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | error: |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 371 | for (i = 0; i < num_devices; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if (gdev->cdev[i]) { |
Sebastian Ott | c560d10 | 2010-05-26 23:27:07 +0200 | [diff] [blame] | 373 | spin_lock_irq(gdev->cdev[i]->ccwlock); |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 374 | if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev) |
| 375 | dev_set_drvdata(&gdev->cdev[i]->dev, NULL); |
Sebastian Ott | c560d10 | 2010-05-26 23:27:07 +0200 | [diff] [blame] | 376 | spin_unlock_irq(gdev->cdev[i]->ccwlock); |
Cornelia Huck | 1708822 | 2006-07-27 14:00:33 +0200 | [diff] [blame] | 377 | put_device(&gdev->cdev[i]->dev); |
Cornelia Huck | f26fd5d | 2008-09-16 09:32:18 -0700 | [diff] [blame] | 378 | gdev->cdev[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 380 | mutex_unlock(&gdev->reg_mutex); |
| 381 | put_device(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | return rc; |
| 383 | } |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 384 | EXPORT_SYMBOL(ccwgroup_create_dev); |
| 385 | |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 386 | static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action, |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 387 | void *data) |
| 388 | { |
| 389 | struct device *dev = data; |
| 390 | |
| 391 | if (action == BUS_NOTIFY_UNBIND_DRIVER) |
| 392 | device_schedule_callback(dev, ccwgroup_ungroup_callback); |
| 393 | |
| 394 | return NOTIFY_OK; |
| 395 | } |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 396 | |
| 397 | static struct notifier_block ccwgroup_nb = { |
| 398 | .notifier_call = ccwgroup_notifier |
| 399 | }; |
| 400 | |
| 401 | static int __init init_ccwgroup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 403 | int ret; |
| 404 | |
| 405 | ret = bus_register(&ccwgroup_bus_type); |
| 406 | if (ret) |
| 407 | return ret; |
| 408 | |
| 409 | ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb); |
| 410 | if (ret) |
| 411 | bus_unregister(&ccwgroup_bus_type); |
| 412 | |
| 413 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 416 | static void __exit cleanup_ccwgroup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | { |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 418 | bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb); |
| 419 | bus_unregister(&ccwgroup_bus_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | module_init(init_ccwgroup); |
| 423 | module_exit(cleanup_ccwgroup); |
| 424 | |
| 425 | /************************** driver stuff ******************************/ |
| 426 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 427 | static int ccwgroup_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 429 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 430 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Sebastian Ott | 50f1548 | 2009-03-26 15:24:14 +0100 | [diff] [blame] | 432 | if (!dev->driver) |
| 433 | return 0; |
Sebastian Ott | 50f1548 | 2009-03-26 15:24:14 +0100 | [diff] [blame] | 434 | if (gdrv->remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | gdrv->remove(gdev); |
Sebastian Ott | 50f1548 | 2009-03-26 15:24:14 +0100 | [diff] [blame] | 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 440 | static void ccwgroup_shutdown(struct device *dev) |
| 441 | { |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 442 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 443 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver); |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 444 | |
Sebastian Ott | 50f1548 | 2009-03-26 15:24:14 +0100 | [diff] [blame] | 445 | if (!dev->driver) |
| 446 | return; |
Sebastian Ott | 50f1548 | 2009-03-26 15:24:14 +0100 | [diff] [blame] | 447 | if (gdrv->shutdown) |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 448 | gdrv->shutdown(gdev); |
| 449 | } |
| 450 | |
Sebastian Ott | 7e597a2 | 2009-06-16 10:30:21 +0200 | [diff] [blame] | 451 | static int ccwgroup_pm_prepare(struct device *dev) |
| 452 | { |
| 453 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 454 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); |
| 455 | |
| 456 | /* Fail while device is being set online/offline. */ |
| 457 | if (atomic_read(&gdev->onoff)) |
| 458 | return -EAGAIN; |
| 459 | |
| 460 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) |
| 461 | return 0; |
| 462 | |
| 463 | return gdrv->prepare ? gdrv->prepare(gdev) : 0; |
| 464 | } |
| 465 | |
| 466 | static void ccwgroup_pm_complete(struct device *dev) |
| 467 | { |
| 468 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 469 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver); |
| 470 | |
| 471 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) |
| 472 | return; |
| 473 | |
| 474 | if (gdrv->complete) |
| 475 | gdrv->complete(gdev); |
| 476 | } |
| 477 | |
| 478 | static int ccwgroup_pm_freeze(struct device *dev) |
| 479 | { |
| 480 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 481 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); |
| 482 | |
| 483 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) |
| 484 | return 0; |
| 485 | |
| 486 | return gdrv->freeze ? gdrv->freeze(gdev) : 0; |
| 487 | } |
| 488 | |
| 489 | static int ccwgroup_pm_thaw(struct device *dev) |
| 490 | { |
| 491 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 492 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); |
| 493 | |
| 494 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) |
| 495 | return 0; |
| 496 | |
| 497 | return gdrv->thaw ? gdrv->thaw(gdev) : 0; |
| 498 | } |
| 499 | |
| 500 | static int ccwgroup_pm_restore(struct device *dev) |
| 501 | { |
| 502 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 503 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); |
| 504 | |
| 505 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) |
| 506 | return 0; |
| 507 | |
| 508 | return gdrv->restore ? gdrv->restore(gdev) : 0; |
| 509 | } |
| 510 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 511 | static const struct dev_pm_ops ccwgroup_pm_ops = { |
Sebastian Ott | 7e597a2 | 2009-06-16 10:30:21 +0200 | [diff] [blame] | 512 | .prepare = ccwgroup_pm_prepare, |
| 513 | .complete = ccwgroup_pm_complete, |
| 514 | .freeze = ccwgroup_pm_freeze, |
| 515 | .thaw = ccwgroup_pm_thaw, |
| 516 | .restore = ccwgroup_pm_restore, |
| 517 | }; |
| 518 | |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 519 | static struct bus_type ccwgroup_bus_type = { |
| 520 | .name = "ccwgroup", |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 521 | .remove = ccwgroup_remove, |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 522 | .shutdown = ccwgroup_shutdown, |
Sebastian Ott | 7e597a2 | 2009-06-16 10:30:21 +0200 | [diff] [blame] | 523 | .pm = &ccwgroup_pm_ops, |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 524 | }; |
| 525 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 526 | /** |
| 527 | * ccwgroup_driver_register() - register a ccw group driver |
| 528 | * @cdriver: driver to be registered |
| 529 | * |
| 530 | * This function is mainly a wrapper around driver_register(). |
| 531 | */ |
| 532 | int ccwgroup_driver_register(struct ccwgroup_driver *cdriver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | { |
| 534 | /* register our new driver with the core */ |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 535 | cdriver->driver.bus = &ccwgroup_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | return driver_register(&cdriver->driver); |
| 538 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 539 | EXPORT_SYMBOL(ccwgroup_driver_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 541 | static int __ccwgroup_match_all(struct device *dev, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 543 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 546 | /** |
| 547 | * ccwgroup_driver_unregister() - deregister a ccw group driver |
| 548 | * @cdriver: driver to be deregistered |
| 549 | * |
| 550 | * This function is mainly a wrapper around driver_unregister(). |
| 551 | */ |
| 552 | void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 554 | struct device *dev; |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | /* We don't want ccwgroup devices to live longer than their driver. */ |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 557 | while ((dev = driver_find_device(&cdriver->driver, NULL, NULL, |
| 558 | __ccwgroup_match_all))) { |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 559 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 560 | |
| 561 | mutex_lock(&gdev->reg_mutex); |
| 562 | __ccwgroup_remove_symlinks(gdev); |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 563 | device_unregister(dev); |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 564 | __ccwgroup_remove_cdev_refs(gdev); |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 565 | mutex_unlock(&gdev->reg_mutex); |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 566 | put_device(dev); |
| 567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | driver_unregister(&cdriver->driver); |
| 569 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 570 | EXPORT_SYMBOL(ccwgroup_driver_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 572 | /** |
| 573 | * ccwgroup_probe_ccwdev() - probe function for slave devices |
| 574 | * @cdev: ccw device to be probed |
| 575 | * |
| 576 | * This is a dummy probe function for ccw devices that are slave devices in |
| 577 | * a ccw group device. |
| 578 | * Returns: |
| 579 | * always %0 |
| 580 | */ |
| 581 | int ccwgroup_probe_ccwdev(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | { |
| 583 | return 0; |
| 584 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 585 | EXPORT_SYMBOL(ccwgroup_probe_ccwdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 587 | /** |
| 588 | * ccwgroup_remove_ccwdev() - remove function for slave devices |
| 589 | * @cdev: ccw device to be removed |
| 590 | * |
| 591 | * This is a remove function for ccw devices that are slave devices in a ccw |
| 592 | * group device. It sets the ccw device offline and also deregisters the |
| 593 | * embedding ccw group device. |
| 594 | */ |
| 595 | void ccwgroup_remove_ccwdev(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
| 597 | struct ccwgroup_device *gdev; |
| 598 | |
| 599 | /* Ignore offlining errors, device is gone anyway. */ |
| 600 | ccw_device_set_offline(cdev); |
| 601 | /* If one of its devices is gone, the whole group is done for. */ |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 602 | spin_lock_irq(cdev->ccwlock); |
| 603 | gdev = dev_get_drvdata(&cdev->dev); |
| 604 | if (!gdev) { |
| 605 | spin_unlock_irq(cdev->ccwlock); |
| 606 | return; |
| 607 | } |
| 608 | /* Get ccwgroup device reference for local processing. */ |
| 609 | get_device(&gdev->dev); |
| 610 | spin_unlock_irq(cdev->ccwlock); |
| 611 | /* Unregister group device. */ |
| 612 | mutex_lock(&gdev->reg_mutex); |
| 613 | if (device_is_registered(&gdev->dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | __ccwgroup_remove_symlinks(gdev); |
| 615 | device_unregister(&gdev->dev); |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 616 | __ccwgroup_remove_cdev_refs(gdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | } |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 618 | mutex_unlock(&gdev->reg_mutex); |
| 619 | /* Release ccwgroup device reference for local processing. */ |
| 620 | put_device(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | EXPORT_SYMBOL(ccwgroup_remove_ccwdev); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 623 | MODULE_LICENSE("GPL"); |