Greg Kroah-Hartman | 724117b | 2017-11-14 18:38:02 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * bus driver for ccwgroup |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 2002, 2012 |
Sebastian Ott | 7e597a2 | 2009-06-16 10:30:21 +0200 | [diff] [blame] | 6 | * |
| 7 | * Author(s): Arnd Bergmann (arndb@de.ibm.com) |
| 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 | |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 19 | #include <asm/cio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/ccwdev.h> |
| 21 | #include <asm/ccwgroup.h> |
| 22 | |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 23 | #include "device.h" |
| 24 | |
| 25 | #define CCW_BUS_ID_SIZE 10 |
Kay Sievers | 98df67b | 2008-12-25 13:38:55 +0100 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* In Linux 2.4, we had a channel device layer called "chandev" |
| 28 | * that did all sorts of obscure stuff for networking devices. |
| 29 | * This is another driver that serves as a replacement for just |
| 30 | * one of its functions, namely the translation of single subchannels |
| 31 | * to devices that use multiple subchannels. |
| 32 | */ |
| 33 | |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 34 | static struct bus_type ccwgroup_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 36 | static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
| 38 | int i; |
Heiko Carstens | 2685df6 | 2017-05-04 13:35:33 +0200 | [diff] [blame] | 39 | char str[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | for (i = 0; i < gdev->count; i++) { |
| 42 | sprintf(str, "cdev%d", i); |
| 43 | sysfs_remove_link(&gdev->dev.kobj, str); |
| 44 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device"); |
| 45 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Sebastian Ott | 683c3dc | 2012-11-22 15:56:39 +0100 | [diff] [blame] | 48 | /** |
| 49 | * ccwgroup_set_online() - enable a ccwgroup device |
| 50 | * @gdev: target ccwgroup device |
| 51 | * |
| 52 | * This function attempts to put the ccwgroup device into the online state. |
| 53 | * Returns: |
| 54 | * %0 on success and a negative error value on failure. |
| 55 | */ |
| 56 | int ccwgroup_set_online(struct ccwgroup_device *gdev) |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 57 | { |
| 58 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); |
Sebastian Ott | cff9710 | 2012-11-09 14:33:06 +0100 | [diff] [blame] | 59 | int ret = -EINVAL; |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 60 | |
| 61 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
| 62 | return -EAGAIN; |
| 63 | if (gdev->state == CCWGROUP_ONLINE) |
| 64 | goto out; |
| 65 | if (gdrv->set_online) |
| 66 | ret = gdrv->set_online(gdev); |
| 67 | if (ret) |
| 68 | goto out; |
| 69 | |
| 70 | gdev->state = CCWGROUP_ONLINE; |
| 71 | out: |
| 72 | atomic_set(&gdev->onoff, 0); |
| 73 | return ret; |
| 74 | } |
Sebastian Ott | 683c3dc | 2012-11-22 15:56:39 +0100 | [diff] [blame] | 75 | EXPORT_SYMBOL(ccwgroup_set_online); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 76 | |
Sebastian Ott | 683c3dc | 2012-11-22 15:56:39 +0100 | [diff] [blame] | 77 | /** |
| 78 | * ccwgroup_set_offline() - disable a ccwgroup device |
| 79 | * @gdev: target ccwgroup device |
Alexandra Winter | d2b59bd | 2021-09-21 16:52:17 +0200 | [diff] [blame] | 80 | * @call_gdrv: Call the registered gdrv set_offline function |
Sebastian Ott | 683c3dc | 2012-11-22 15:56:39 +0100 | [diff] [blame] | 81 | * |
| 82 | * This function attempts to put the ccwgroup device into the offline state. |
| 83 | * Returns: |
| 84 | * %0 on success and a negative error value on failure. |
| 85 | */ |
Alexandra Winter | d2b59bd | 2021-09-21 16:52:17 +0200 | [diff] [blame] | 86 | int ccwgroup_set_offline(struct ccwgroup_device *gdev, bool call_gdrv) |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 87 | { |
| 88 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); |
Sebastian Ott | cff9710 | 2012-11-09 14:33:06 +0100 | [diff] [blame] | 89 | int ret = -EINVAL; |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 90 | |
| 91 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
| 92 | return -EAGAIN; |
| 93 | if (gdev->state == CCWGROUP_OFFLINE) |
| 94 | goto out; |
Alexandra Winter | d2b59bd | 2021-09-21 16:52:17 +0200 | [diff] [blame] | 95 | if (!call_gdrv) { |
| 96 | ret = 0; |
| 97 | goto offline; |
| 98 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 99 | if (gdrv->set_offline) |
| 100 | ret = gdrv->set_offline(gdev); |
| 101 | if (ret) |
| 102 | goto out; |
| 103 | |
Alexandra Winter | d2b59bd | 2021-09-21 16:52:17 +0200 | [diff] [blame] | 104 | offline: |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 105 | gdev->state = CCWGROUP_OFFLINE; |
| 106 | out: |
| 107 | atomic_set(&gdev->onoff, 0); |
| 108 | return ret; |
| 109 | } |
Sebastian Ott | 683c3dc | 2012-11-22 15:56:39 +0100 | [diff] [blame] | 110 | EXPORT_SYMBOL(ccwgroup_set_offline); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 111 | |
Sebastian Ott | dbdf1af | 2011-10-30 15:16:52 +0100 | [diff] [blame] | 112 | static ssize_t ccwgroup_online_store(struct device *dev, |
| 113 | struct device_attribute *attr, |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 114 | const char *buf, size_t count) |
| 115 | { |
| 116 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 117 | unsigned long value; |
| 118 | int ret; |
| 119 | |
Sebastian Ott | d5b877f | 2013-12-16 10:56:46 +0100 | [diff] [blame] | 120 | device_lock(dev); |
| 121 | if (!dev->driver) { |
| 122 | ret = -EINVAL; |
| 123 | goto out; |
| 124 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 125 | |
Jingoo Han | 0178722 | 2013-07-22 10:18:15 +0900 | [diff] [blame] | 126 | ret = kstrtoul(buf, 0, &value); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 127 | if (ret) |
| 128 | goto out; |
| 129 | |
| 130 | if (value == 1) |
| 131 | ret = ccwgroup_set_online(gdev); |
| 132 | else if (value == 0) |
Alexandra Winter | d2b59bd | 2021-09-21 16:52:17 +0200 | [diff] [blame] | 133 | ret = ccwgroup_set_offline(gdev, true); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 134 | else |
| 135 | ret = -EINVAL; |
| 136 | out: |
Sebastian Ott | d5b877f | 2013-12-16 10:56:46 +0100 | [diff] [blame] | 137 | device_unlock(dev); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 138 | return (ret == 0) ? count : ret; |
| 139 | } |
| 140 | |
Sebastian Ott | dbdf1af | 2011-10-30 15:16:52 +0100 | [diff] [blame] | 141 | static ssize_t ccwgroup_online_show(struct device *dev, |
| 142 | struct device_attribute *attr, |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 143 | char *buf) |
| 144 | { |
| 145 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 146 | int online; |
| 147 | |
| 148 | online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0; |
| 149 | |
| 150 | return scnprintf(buf, PAGE_SIZE, "%d\n", online); |
| 151 | } |
| 152 | |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 153 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | * Provide an 'ungroup' attribute so the user can remove group devices no |
Heiko Carstens | cada938 | 2023-06-28 16:23:20 +0200 | [diff] [blame] | 155 | * longer needed or accidentally created. Saves memory :) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | */ |
Tejun Heo | 0b60f9e | 2014-02-03 14:03:04 -0500 | [diff] [blame] | 157 | static void ccwgroup_ungroup(struct ccwgroup_device *gdev) |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 158 | { |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 159 | mutex_lock(&gdev->reg_mutex); |
Cornelia Huck | 1a908c7 | 2008-01-26 14:10:50 +0100 | [diff] [blame] | 160 | if (device_is_registered(&gdev->dev)) { |
| 161 | __ccwgroup_remove_symlinks(gdev); |
Tejun Heo | 0b60f9e | 2014-02-03 14:03:04 -0500 | [diff] [blame] | 162 | device_unregister(&gdev->dev); |
Cornelia Huck | 1a908c7 | 2008-01-26 14:10:50 +0100 | [diff] [blame] | 163 | } |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 164 | mutex_unlock(&gdev->reg_mutex); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 167 | static ssize_t ccwgroup_ungroup_store(struct device *dev, |
| 168 | struct device_attribute *attr, |
| 169 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 171 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
Sebastian Ott | 0310c8b | 2014-06-13 17:29:11 +0200 | [diff] [blame] | 172 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Peter Oberparleiter | c619d42 | 2008-12-25 13:39:04 +0100 | [diff] [blame] | 174 | /* Prevent concurrent online/offline processing and ungrouping. */ |
| 175 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
| 176 | return -EAGAIN; |
| 177 | if (gdev->state != CCWGROUP_OFFLINE) { |
| 178 | rc = -EINVAL; |
| 179 | goto out; |
| 180 | } |
Tejun Heo | 0b60f9e | 2014-02-03 14:03:04 -0500 | [diff] [blame] | 181 | |
| 182 | if (device_remove_file_self(dev, attr)) |
| 183 | ccwgroup_ungroup(gdev); |
Sebastian Ott | 0310c8b | 2014-06-13 17:29:11 +0200 | [diff] [blame] | 184 | else |
| 185 | rc = -ENODEV; |
Peter Oberparleiter | c619d42 | 2008-12-25 13:39:04 +0100 | [diff] [blame] | 186 | out: |
| 187 | if (rc) { |
Sebastian Ott | 0310c8b | 2014-06-13 17:29:11 +0200 | [diff] [blame] | 188 | /* Release onoff "lock" when ungrouping failed. */ |
| 189 | atomic_set(&gdev->onoff, 0); |
Peter Oberparleiter | c619d42 | 2008-12-25 13:39:04 +0100 | [diff] [blame] | 190 | return rc; |
| 191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return count; |
| 193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store); |
Sebastian Ott | dbdf1af | 2011-10-30 15:16:52 +0100 | [diff] [blame] | 195 | static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store); |
| 196 | |
Julian Wiedmann | 0506631 | 2020-12-07 14:12:29 +0100 | [diff] [blame] | 197 | static struct attribute *ccwgroup_dev_attrs[] = { |
Sebastian Ott | dbdf1af | 2011-10-30 15:16:52 +0100 | [diff] [blame] | 198 | &dev_attr_online.attr, |
| 199 | &dev_attr_ungroup.attr, |
| 200 | NULL, |
| 201 | }; |
Julian Wiedmann | 0506631 | 2020-12-07 14:12:29 +0100 | [diff] [blame] | 202 | ATTRIBUTE_GROUPS(ccwgroup_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Tejun Heo | 0b60f9e | 2014-02-03 14:03:04 -0500 | [diff] [blame] | 204 | static void ccwgroup_ungroup_workfn(struct work_struct *work) |
| 205 | { |
| 206 | struct ccwgroup_device *gdev = |
| 207 | container_of(work, struct ccwgroup_device, ungroup_work); |
| 208 | |
| 209 | ccwgroup_ungroup(gdev); |
Sebastian Ott | 9280ddb | 2014-06-13 17:02:24 +0200 | [diff] [blame] | 210 | put_device(&gdev->dev); |
Tejun Heo | 0b60f9e | 2014-02-03 14:03:04 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 213 | static void ccwgroup_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Julian Wiedmann | 197cec2 | 2021-04-24 12:12:38 +0200 | [diff] [blame] | 215 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 216 | unsigned int i; |
| 217 | |
| 218 | for (i = 0; i < gdev->count; i++) { |
| 219 | struct ccw_device *cdev = gdev->cdev[i]; |
| 220 | unsigned long flags; |
| 221 | |
| 222 | if (cdev) { |
| 223 | spin_lock_irqsave(cdev->ccwlock, flags); |
| 224 | if (dev_get_drvdata(&cdev->dev) == gdev) |
| 225 | dev_set_drvdata(&cdev->dev, NULL); |
| 226 | spin_unlock_irqrestore(cdev->ccwlock, flags); |
| 227 | put_device(&cdev->dev); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | kfree(gdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 234 | static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Heiko Carstens | 2685df6 | 2017-05-04 13:35:33 +0200 | [diff] [blame] | 236 | char str[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | int i, rc; |
| 238 | |
| 239 | for (i = 0; i < gdev->count; i++) { |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 240 | rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, |
| 241 | &gdev->dev.kobj, "group_device"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (rc) { |
| 243 | for (--i; i >= 0; i--) |
| 244 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, |
| 245 | "group_device"); |
| 246 | return rc; |
| 247 | } |
| 248 | } |
| 249 | for (i = 0; i < gdev->count; i++) { |
| 250 | sprintf(str, "cdev%d", i); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 251 | rc = sysfs_create_link(&gdev->dev.kobj, |
| 252 | &gdev->cdev[i]->dev.kobj, str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | if (rc) { |
| 254 | for (--i; i >= 0; i--) { |
| 255 | sprintf(str, "cdev%d", i); |
| 256 | sysfs_remove_link(&gdev->dev.kobj, str); |
| 257 | } |
| 258 | for (i = 0; i < gdev->count; i++) |
| 259 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, |
| 260 | "group_device"); |
| 261 | return rc; |
| 262 | } |
| 263 | } |
| 264 | return 0; |
| 265 | } |
| 266 | |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 267 | 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] | 268 | { |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 269 | unsigned int cssid, ssid, devno; |
| 270 | int ret = 0, len; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 271 | char *start, *end; |
| 272 | |
| 273 | start = (char *)*buf; |
| 274 | end = strchr(start, ','); |
| 275 | if (!end) { |
| 276 | /* Last entry. Strip trailing newline, if applicable. */ |
| 277 | end = strchr(start, '\n'); |
| 278 | if (end) |
| 279 | *end = '\0'; |
| 280 | len = strlen(start) + 1; |
| 281 | } else { |
| 282 | len = end - start + 1; |
| 283 | end++; |
| 284 | } |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 285 | if (len <= CCW_BUS_ID_SIZE) { |
| 286 | if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3) |
| 287 | ret = -EINVAL; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 288 | } else |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 289 | ret = -EINVAL; |
| 290 | |
| 291 | if (!ret) { |
| 292 | id->ssid = ssid; |
| 293 | id->devno = devno; |
| 294 | } |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 295 | *buf = end; |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 296 | return ret; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 299 | /** |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 300 | * ccwgroup_create_dev() - create and register a ccw group device |
| 301 | * @parent: parent device for the new device |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 302 | * @gdrv: driver for the new group device |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 303 | * @num_devices: number of slave devices |
| 304 | * @buf: buffer containing comma separated bus ids of slave devices |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 305 | * |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 306 | * 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] | 307 | * devices are obtained from the list of bus ids given in @buf. |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 308 | * Returns: |
| 309 | * %0 on success and an error code on failure. |
| 310 | * Context: |
| 311 | * non-atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | */ |
Sebastian Ott | 9814fdf | 2012-05-15 18:03:46 +0200 | [diff] [blame] | 313 | int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv, |
| 314 | int num_devices, const char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
| 316 | struct ccwgroup_device *gdev; |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 317 | struct ccw_dev_id dev_id; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 318 | int rc, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Julian Wiedmann | ab7efda | 2018-01-30 14:39:34 +0100 | [diff] [blame] | 320 | if (num_devices < 1) |
| 321 | return -EINVAL; |
| 322 | |
Kees Cook | acafe7e | 2018-05-08 13:45:50 -0700 | [diff] [blame] | 323 | gdev = kzalloc(struct_size(gdev, cdev, num_devices), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | if (!gdev) |
| 325 | return -ENOMEM; |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | atomic_set(&gdev->onoff, 0); |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 328 | mutex_init(&gdev->reg_mutex); |
| 329 | mutex_lock(&gdev->reg_mutex); |
Tejun Heo | 0b60f9e | 2014-02-03 14:03:04 -0500 | [diff] [blame] | 330 | INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn); |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 331 | gdev->count = num_devices; |
| 332 | gdev->dev.bus = &ccwgroup_bus_type; |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 333 | gdev->dev.parent = parent; |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 334 | gdev->dev.release = ccwgroup_release; |
| 335 | device_initialize(&gdev->dev); |
| 336 | |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 337 | for (i = 0; i < num_devices && buf; i++) { |
| 338 | rc = __get_next_id(&buf, &dev_id); |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 339 | if (rc != 0) |
| 340 | goto error; |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 341 | gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id); |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 342 | /* |
| 343 | * All devices have to be of the same type in |
| 344 | * order to be grouped. |
| 345 | */ |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 346 | if (!gdev->cdev[i] || !gdev->cdev[i]->drv || |
| 347 | gdev->cdev[i]->drv != gdev->cdev[0]->drv || |
| 348 | gdev->cdev[i]->id.driver_info != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | gdev->cdev[0]->id.driver_info) { |
| 350 | rc = -EINVAL; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 351 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | /* Don't allow a device to belong to more than one group. */ |
Sebastian Ott | c560d10 | 2010-05-26 23:27:07 +0200 | [diff] [blame] | 354 | spin_lock_irq(gdev->cdev[i]->ccwlock); |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 355 | if (dev_get_drvdata(&gdev->cdev[i]->dev)) { |
Sebastian Ott | c560d10 | 2010-05-26 23:27:07 +0200 | [diff] [blame] | 356 | spin_unlock_irq(gdev->cdev[i]->ccwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | rc = -EINVAL; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 358 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 360 | dev_set_drvdata(&gdev->cdev[i]->dev, gdev); |
Sebastian Ott | c560d10 | 2010-05-26 23:27:07 +0200 | [diff] [blame] | 361 | spin_unlock_irq(gdev->cdev[i]->ccwlock); |
Cornelia Huck | 1708822 | 2006-07-27 14:00:33 +0200 | [diff] [blame] | 362 | } |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 363 | /* Check for sufficient number of bus ids. */ |
Sebastian Ott | b7a610f | 2012-05-15 17:52:07 +0200 | [diff] [blame] | 364 | if (i < num_devices) { |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 365 | rc = -EINVAL; |
| 366 | goto error; |
| 367 | } |
| 368 | /* Check for trailing stuff. */ |
Vasily Gorbik | ea298e6 | 2019-09-17 20:04:04 +0200 | [diff] [blame] | 369 | if (i == num_devices && buf && strlen(buf) > 0) { |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 370 | rc = -EINVAL; |
| 371 | goto error; |
| 372 | } |
Julian Wiedmann | f9a5d70 | 2017-09-14 09:52:32 +0200 | [diff] [blame] | 373 | /* Check if the devices are bound to the required ccw driver. */ |
Julian Wiedmann | ab7efda | 2018-01-30 14:39:34 +0100 | [diff] [blame] | 374 | if (gdrv && gdrv->ccw_driver && |
Julian Wiedmann | f9a5d70 | 2017-09-14 09:52:32 +0200 | [diff] [blame] | 375 | gdev->cdev[0]->drv != gdrv->ccw_driver) { |
| 376 | rc = -EINVAL; |
| 377 | goto error; |
| 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Cornelia Huck | 1bf5b28 | 2008-10-10 21:33:10 +0200 | [diff] [blame] | 380 | dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev)); |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 381 | |
| 382 | if (gdrv) { |
| 383 | gdev->dev.driver = &gdrv->driver; |
| 384 | rc = gdrv->setup ? gdrv->setup(gdev) : 0; |
| 385 | if (rc) |
| 386 | goto error; |
| 387 | } |
Peter Oberparleiter | 16f7f95 | 2008-08-21 19:46:36 +0200 | [diff] [blame] | 388 | rc = device_add(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if (rc) |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 390 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | rc = __ccwgroup_create_symlinks(gdev); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 392 | if (rc) { |
| 393 | device_del(&gdev->dev); |
| 394 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 396 | mutex_unlock(&gdev->reg_mutex); |
| 397 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | error: |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 399 | mutex_unlock(&gdev->reg_mutex); |
| 400 | put_device(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return rc; |
| 402 | } |
Sebastian Ott | f2962da | 2012-05-15 17:49:12 +0200 | [diff] [blame] | 403 | EXPORT_SYMBOL(ccwgroup_create_dev); |
| 404 | |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 405 | static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action, |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 406 | void *data) |
| 407 | { |
Tejun Heo | 0b60f9e | 2014-02-03 14:03:04 -0500 | [diff] [blame] | 408 | struct ccwgroup_device *gdev = to_ccwgroupdev(data); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 409 | |
Julian Wiedmann | 17c0b86 | 2021-05-05 10:28:21 +0200 | [diff] [blame] | 410 | if (action == BUS_NOTIFY_UNBOUND_DRIVER) { |
Sebastian Ott | 9280ddb | 2014-06-13 17:02:24 +0200 | [diff] [blame] | 411 | get_device(&gdev->dev); |
Tejun Heo | 0b60f9e | 2014-02-03 14:03:04 -0500 | [diff] [blame] | 412 | schedule_work(&gdev->ungroup_work); |
Sebastian Ott | 9280ddb | 2014-06-13 17:02:24 +0200 | [diff] [blame] | 413 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 414 | |
| 415 | return NOTIFY_OK; |
| 416 | } |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 417 | |
| 418 | static struct notifier_block ccwgroup_nb = { |
| 419 | .notifier_call = ccwgroup_notifier |
| 420 | }; |
| 421 | |
| 422 | static int __init init_ccwgroup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 424 | int ret; |
| 425 | |
| 426 | ret = bus_register(&ccwgroup_bus_type); |
| 427 | if (ret) |
| 428 | return ret; |
| 429 | |
| 430 | ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb); |
| 431 | if (ret) |
| 432 | bus_unregister(&ccwgroup_bus_type); |
| 433 | |
| 434 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 437 | static void __exit cleanup_ccwgroup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Sebastian Ott | e909074 | 2009-03-26 15:24:15 +0100 | [diff] [blame] | 439 | bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb); |
| 440 | bus_unregister(&ccwgroup_bus_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | module_init(init_ccwgroup); |
| 444 | module_exit(cleanup_ccwgroup); |
| 445 | |
| 446 | /************************** driver stuff ******************************/ |
| 447 | |
Uwe Kleine-König | fc7a620 | 2021-07-13 21:35:22 +0200 | [diff] [blame] | 448 | static void ccwgroup_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 450 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 451 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Sebastian Ott | 50f1548 | 2009-03-26 15:24:14 +0100 | [diff] [blame] | 453 | if (gdrv->remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | gdrv->remove(gdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 457 | static void ccwgroup_shutdown(struct device *dev) |
| 458 | { |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 459 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 460 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver); |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 461 | |
Sebastian Ott | 50f1548 | 2009-03-26 15:24:14 +0100 | [diff] [blame] | 462 | if (!dev->driver) |
| 463 | return; |
Sebastian Ott | 50f1548 | 2009-03-26 15:24:14 +0100 | [diff] [blame] | 464 | if (gdrv->shutdown) |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 465 | gdrv->shutdown(gdev); |
| 466 | } |
| 467 | |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 468 | static struct bus_type ccwgroup_bus_type = { |
| 469 | .name = "ccwgroup", |
Julian Wiedmann | 0506631 | 2020-12-07 14:12:29 +0100 | [diff] [blame] | 470 | .dev_groups = ccwgroup_dev_groups, |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 471 | .remove = ccwgroup_remove, |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 472 | .shutdown = ccwgroup_shutdown, |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
Sebastian Ott | a166c36 | 2017-02-15 11:45:07 +0100 | [diff] [blame] | 475 | bool dev_is_ccwgroup(struct device *dev) |
| 476 | { |
| 477 | return dev->bus == &ccwgroup_bus_type; |
| 478 | } |
| 479 | EXPORT_SYMBOL(dev_is_ccwgroup); |
| 480 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 481 | /** |
| 482 | * ccwgroup_driver_register() - register a ccw group driver |
| 483 | * @cdriver: driver to be registered |
| 484 | * |
| 485 | * This function is mainly a wrapper around driver_register(). |
| 486 | */ |
| 487 | int ccwgroup_driver_register(struct ccwgroup_driver *cdriver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
| 489 | /* register our new driver with the core */ |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 490 | cdriver->driver.bus = &ccwgroup_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | return driver_register(&cdriver->driver); |
| 493 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 494 | EXPORT_SYMBOL(ccwgroup_driver_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 496 | /** |
| 497 | * ccwgroup_driver_unregister() - deregister a ccw group driver |
| 498 | * @cdriver: driver to be deregistered |
| 499 | * |
| 500 | * This function is mainly a wrapper around driver_unregister(). |
| 501 | */ |
| 502 | void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | driver_unregister(&cdriver->driver); |
| 505 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 506 | EXPORT_SYMBOL(ccwgroup_driver_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Julian Wiedmann | 346e485 | 2018-09-14 13:47:41 +0200 | [diff] [blame] | 508 | /** |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 509 | * ccwgroup_probe_ccwdev() - probe function for slave devices |
| 510 | * @cdev: ccw device to be probed |
| 511 | * |
| 512 | * This is a dummy probe function for ccw devices that are slave devices in |
| 513 | * a ccw group device. |
| 514 | * Returns: |
| 515 | * always %0 |
| 516 | */ |
| 517 | int ccwgroup_probe_ccwdev(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
| 519 | return 0; |
| 520 | } |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 521 | EXPORT_SYMBOL(ccwgroup_probe_ccwdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 523 | /** |
| 524 | * ccwgroup_remove_ccwdev() - remove function for slave devices |
| 525 | * @cdev: ccw device to be removed |
| 526 | * |
| 527 | * This is a remove function for ccw devices that are slave devices in a ccw |
| 528 | * group device. It sets the ccw device offline and also deregisters the |
| 529 | * embedding ccw group device. |
| 530 | */ |
| 531 | void ccwgroup_remove_ccwdev(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
| 533 | struct ccwgroup_device *gdev; |
| 534 | |
| 535 | /* Ignore offlining errors, device is gone anyway. */ |
| 536 | ccw_device_set_offline(cdev); |
| 537 | /* 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] | 538 | spin_lock_irq(cdev->ccwlock); |
| 539 | gdev = dev_get_drvdata(&cdev->dev); |
| 540 | if (!gdev) { |
| 541 | spin_unlock_irq(cdev->ccwlock); |
| 542 | return; |
| 543 | } |
| 544 | /* Get ccwgroup device reference for local processing. */ |
| 545 | get_device(&gdev->dev); |
| 546 | spin_unlock_irq(cdev->ccwlock); |
| 547 | /* Unregister group device. */ |
Sebastian Ott | fa73eb4 | 2014-06-13 18:11:37 +0200 | [diff] [blame] | 548 | ccwgroup_ungroup(gdev); |
Peter Oberparleiter | c030175 | 2011-01-05 12:48:13 +0100 | [diff] [blame] | 549 | /* Release ccwgroup device reference for local processing. */ |
| 550 | put_device(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | EXPORT_SYMBOL(ccwgroup_remove_ccwdev); |
Sebastian Ott | dad572e3 | 2011-10-30 15:16:53 +0100 | [diff] [blame] | 553 | MODULE_LICENSE("GPL"); |