Commit | Line | Data |
---|---|---|
724117b7 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
1da177e4 | 3 | * bus driver for ccwgroup |
1da177e4 | 4 | * |
f2962dae | 5 | * Copyright IBM Corp. 2002, 2012 |
7e597a21 SO |
6 | * |
7 | * Author(s): Arnd Bergmann (arndb@de.ibm.com) | |
8 | * Cornelia Huck (cornelia.huck@de.ibm.com) | |
1da177e4 LT |
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 | ||
b7a610f7 | 19 | #include <asm/cio.h> |
1da177e4 LT |
20 | #include <asm/ccwdev.h> |
21 | #include <asm/ccwgroup.h> | |
22 | ||
b7a610f7 SO |
23 | #include "device.h" |
24 | ||
25 | #define CCW_BUS_ID_SIZE 10 | |
98df67b3 | 26 | |
1da177e4 LT |
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 | ||
f9ccf456 | 34 | static struct bus_type ccwgroup_bus_type; |
1da177e4 | 35 | |
dad572e3 | 36 | static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev) |
1da177e4 LT |
37 | { |
38 | int i; | |
2685df67 | 39 | char str[16]; |
1da177e4 LT |
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 | } | |
1da177e4 LT |
46 | } |
47 | ||
c0301754 PO |
48 | /* |
49 | * Remove references from ccw devices to ccw group device and from | |
50 | * ccw group device to ccw devices. | |
51 | */ | |
52 | static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev) | |
53 | { | |
54 | struct ccw_device *cdev; | |
55 | int i; | |
56 | ||
57 | for (i = 0; i < gdev->count; i++) { | |
58 | cdev = gdev->cdev[i]; | |
59 | if (!cdev) | |
60 | continue; | |
61 | spin_lock_irq(cdev->ccwlock); | |
62 | dev_set_drvdata(&cdev->dev, NULL); | |
63 | spin_unlock_irq(cdev->ccwlock); | |
64 | gdev->cdev[i] = NULL; | |
65 | put_device(&cdev->dev); | |
66 | } | |
67 | } | |
68 | ||
683c3dce SO |
69 | /** |
70 | * ccwgroup_set_online() - enable a ccwgroup device | |
71 | * @gdev: target ccwgroup device | |
72 | * | |
73 | * This function attempts to put the ccwgroup device into the online state. | |
74 | * Returns: | |
75 | * %0 on success and a negative error value on failure. | |
76 | */ | |
77 | int ccwgroup_set_online(struct ccwgroup_device *gdev) | |
dad572e3 SO |
78 | { |
79 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); | |
cff97102 | 80 | int ret = -EINVAL; |
dad572e3 SO |
81 | |
82 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) | |
83 | return -EAGAIN; | |
84 | if (gdev->state == CCWGROUP_ONLINE) | |
85 | goto out; | |
86 | if (gdrv->set_online) | |
87 | ret = gdrv->set_online(gdev); | |
88 | if (ret) | |
89 | goto out; | |
90 | ||
91 | gdev->state = CCWGROUP_ONLINE; | |
92 | out: | |
93 | atomic_set(&gdev->onoff, 0); | |
94 | return ret; | |
95 | } | |
683c3dce | 96 | EXPORT_SYMBOL(ccwgroup_set_online); |
dad572e3 | 97 | |
683c3dce SO |
98 | /** |
99 | * ccwgroup_set_offline() - disable a ccwgroup device | |
100 | * @gdev: target ccwgroup device | |
101 | * | |
102 | * This function attempts to put the ccwgroup device into the offline state. | |
103 | * Returns: | |
104 | * %0 on success and a negative error value on failure. | |
105 | */ | |
106 | int ccwgroup_set_offline(struct ccwgroup_device *gdev) | |
dad572e3 SO |
107 | { |
108 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); | |
cff97102 | 109 | int ret = -EINVAL; |
dad572e3 SO |
110 | |
111 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) | |
112 | return -EAGAIN; | |
113 | if (gdev->state == CCWGROUP_OFFLINE) | |
114 | goto out; | |
115 | if (gdrv->set_offline) | |
116 | ret = gdrv->set_offline(gdev); | |
117 | if (ret) | |
118 | goto out; | |
119 | ||
120 | gdev->state = CCWGROUP_OFFLINE; | |
121 | out: | |
122 | atomic_set(&gdev->onoff, 0); | |
123 | return ret; | |
124 | } | |
683c3dce | 125 | EXPORT_SYMBOL(ccwgroup_set_offline); |
dad572e3 | 126 | |
dbdf1afc SO |
127 | static ssize_t ccwgroup_online_store(struct device *dev, |
128 | struct device_attribute *attr, | |
dad572e3 SO |
129 | const char *buf, size_t count) |
130 | { | |
131 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); | |
dad572e3 SO |
132 | unsigned long value; |
133 | int ret; | |
134 | ||
d5b877ff SO |
135 | device_lock(dev); |
136 | if (!dev->driver) { | |
137 | ret = -EINVAL; | |
138 | goto out; | |
139 | } | |
dad572e3 | 140 | |
0178722b | 141 | ret = kstrtoul(buf, 0, &value); |
dad572e3 SO |
142 | if (ret) |
143 | goto out; | |
144 | ||
145 | if (value == 1) | |
146 | ret = ccwgroup_set_online(gdev); | |
147 | else if (value == 0) | |
148 | ret = ccwgroup_set_offline(gdev); | |
149 | else | |
150 | ret = -EINVAL; | |
151 | out: | |
d5b877ff | 152 | device_unlock(dev); |
dad572e3 SO |
153 | return (ret == 0) ? count : ret; |
154 | } | |
155 | ||
dbdf1afc SO |
156 | static ssize_t ccwgroup_online_show(struct device *dev, |
157 | struct device_attribute *attr, | |
dad572e3 SO |
158 | char *buf) |
159 | { | |
160 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); | |
161 | int online; | |
162 | ||
163 | online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0; | |
164 | ||
165 | return scnprintf(buf, PAGE_SIZE, "%d\n", online); | |
166 | } | |
167 | ||
1da177e4 LT |
168 | /* |
169 | * Provide an 'ungroup' attribute so the user can remove group devices no | |
170 | * longer needed or accidentially created. Saves memory :) | |
171 | */ | |
0b60f9ea | 172 | static void ccwgroup_ungroup(struct ccwgroup_device *gdev) |
d9a9cdfb | 173 | { |
d76123eb | 174 | mutex_lock(&gdev->reg_mutex); |
1a908c73 CH |
175 | if (device_is_registered(&gdev->dev)) { |
176 | __ccwgroup_remove_symlinks(gdev); | |
0b60f9ea | 177 | device_unregister(&gdev->dev); |
c0301754 | 178 | __ccwgroup_remove_cdev_refs(gdev); |
1a908c73 | 179 | } |
d76123eb | 180 | mutex_unlock(&gdev->reg_mutex); |
d9a9cdfb AS |
181 | } |
182 | ||
dad572e3 SO |
183 | static ssize_t ccwgroup_ungroup_store(struct device *dev, |
184 | struct device_attribute *attr, | |
185 | const char *buf, size_t count) | |
1da177e4 | 186 | { |
dad572e3 | 187 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
0310c8b5 | 188 | int rc = 0; |
1da177e4 | 189 | |
c619d422 PO |
190 | /* Prevent concurrent online/offline processing and ungrouping. */ |
191 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) | |
192 | return -EAGAIN; | |
193 | if (gdev->state != CCWGROUP_OFFLINE) { | |
194 | rc = -EINVAL; | |
195 | goto out; | |
196 | } | |
0b60f9ea TH |
197 | |
198 | if (device_remove_file_self(dev, attr)) | |
199 | ccwgroup_ungroup(gdev); | |
0310c8b5 SO |
200 | else |
201 | rc = -ENODEV; | |
c619d422 PO |
202 | out: |
203 | if (rc) { | |
0310c8b5 SO |
204 | /* Release onoff "lock" when ungrouping failed. */ |
205 | atomic_set(&gdev->onoff, 0); | |
c619d422 PO |
206 | return rc; |
207 | } | |
1da177e4 LT |
208 | return count; |
209 | } | |
1da177e4 | 210 | static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store); |
dbdf1afc SO |
211 | static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store); |
212 | ||
213 | static struct attribute *ccwgroup_attrs[] = { | |
214 | &dev_attr_online.attr, | |
215 | &dev_attr_ungroup.attr, | |
216 | NULL, | |
217 | }; | |
218 | static struct attribute_group ccwgroup_attr_group = { | |
219 | .attrs = ccwgroup_attrs, | |
220 | }; | |
221 | static const struct attribute_group *ccwgroup_attr_groups[] = { | |
222 | &ccwgroup_attr_group, | |
223 | NULL, | |
224 | }; | |
1da177e4 | 225 | |
0b60f9ea TH |
226 | static void ccwgroup_ungroup_workfn(struct work_struct *work) |
227 | { | |
228 | struct ccwgroup_device *gdev = | |
229 | container_of(work, struct ccwgroup_device, ungroup_work); | |
230 | ||
231 | ccwgroup_ungroup(gdev); | |
9280ddb1 | 232 | put_device(&gdev->dev); |
0b60f9ea TH |
233 | } |
234 | ||
dad572e3 | 235 | static void ccwgroup_release(struct device *dev) |
1da177e4 | 236 | { |
c0301754 | 237 | kfree(to_ccwgroupdev(dev)); |
1da177e4 LT |
238 | } |
239 | ||
dad572e3 | 240 | static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev) |
1da177e4 | 241 | { |
2685df67 | 242 | char str[16]; |
1da177e4 LT |
243 | int i, rc; |
244 | ||
245 | for (i = 0; i < gdev->count; i++) { | |
dad572e3 SO |
246 | rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, |
247 | &gdev->dev.kobj, "group_device"); | |
1da177e4 LT |
248 | if (rc) { |
249 | for (--i; i >= 0; i--) | |
250 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, | |
251 | "group_device"); | |
252 | return rc; | |
253 | } | |
254 | } | |
255 | for (i = 0; i < gdev->count; i++) { | |
256 | sprintf(str, "cdev%d", i); | |
dad572e3 SO |
257 | rc = sysfs_create_link(&gdev->dev.kobj, |
258 | &gdev->cdev[i]->dev.kobj, str); | |
1da177e4 LT |
259 | if (rc) { |
260 | for (--i; i >= 0; i--) { | |
261 | sprintf(str, "cdev%d", i); | |
262 | sysfs_remove_link(&gdev->dev.kobj, str); | |
263 | } | |
264 | for (i = 0; i < gdev->count; i++) | |
265 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, | |
266 | "group_device"); | |
267 | return rc; | |
268 | } | |
269 | } | |
270 | return 0; | |
271 | } | |
272 | ||
b7a610f7 | 273 | static int __get_next_id(const char **buf, struct ccw_dev_id *id) |
022b660a | 274 | { |
b7a610f7 SO |
275 | unsigned int cssid, ssid, devno; |
276 | int ret = 0, len; | |
022b660a UB |
277 | char *start, *end; |
278 | ||
279 | start = (char *)*buf; | |
280 | end = strchr(start, ','); | |
281 | if (!end) { | |
282 | /* Last entry. Strip trailing newline, if applicable. */ | |
283 | end = strchr(start, '\n'); | |
284 | if (end) | |
285 | *end = '\0'; | |
286 | len = strlen(start) + 1; | |
287 | } else { | |
288 | len = end - start + 1; | |
289 | end++; | |
290 | } | |
b7a610f7 SO |
291 | if (len <= CCW_BUS_ID_SIZE) { |
292 | if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3) | |
293 | ret = -EINVAL; | |
022b660a | 294 | } else |
b7a610f7 | 295 | ret = -EINVAL; |
022b660a | 296 | |
b7a610f7 SO |
297 | if (!ret) { |
298 | id->ssid = ssid; | |
299 | id->devno = devno; | |
300 | } | |
301 | *buf = end; | |
302 | return ret; | |
022b660a UB |
303 | } |
304 | ||
b2ffd8e9 | 305 | /** |
f2962dae SO |
306 | * ccwgroup_create_dev() - create and register a ccw group device |
307 | * @parent: parent device for the new device | |
f2962dae | 308 | * @gdrv: driver for the new group device |
022b660a UB |
309 | * @num_devices: number of slave devices |
310 | * @buf: buffer containing comma separated bus ids of slave devices | |
b2ffd8e9 | 311 | * |
f2962dae | 312 | * Create and register a new ccw group device as a child of @parent. Slave |
b7a610f7 | 313 | * devices are obtained from the list of bus ids given in @buf. |
b2ffd8e9 CH |
314 | * Returns: |
315 | * %0 on success and an error code on failure. | |
316 | * Context: | |
317 | * non-atomic | |
1da177e4 | 318 | */ |
9814fdfb SO |
319 | int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv, |
320 | int num_devices, const char *buf) | |
1da177e4 LT |
321 | { |
322 | struct ccwgroup_device *gdev; | |
b7a610f7 | 323 | struct ccw_dev_id dev_id; |
022b660a | 324 | int rc, i; |
1da177e4 | 325 | |
ab7efda4 JW |
326 | if (num_devices < 1) |
327 | return -EINVAL; | |
328 | ||
acafe7e3 | 329 | gdev = kzalloc(struct_size(gdev, cdev, num_devices), GFP_KERNEL); |
1da177e4 LT |
330 | if (!gdev) |
331 | return -ENOMEM; | |
332 | ||
1da177e4 | 333 | atomic_set(&gdev->onoff, 0); |
d76123eb CH |
334 | mutex_init(&gdev->reg_mutex); |
335 | mutex_lock(&gdev->reg_mutex); | |
0b60f9ea | 336 | INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn); |
16f7f956 PO |
337 | gdev->count = num_devices; |
338 | gdev->dev.bus = &ccwgroup_bus_type; | |
f2962dae | 339 | gdev->dev.parent = parent; |
16f7f956 PO |
340 | gdev->dev.release = ccwgroup_release; |
341 | device_initialize(&gdev->dev); | |
342 | ||
b7a610f7 SO |
343 | for (i = 0; i < num_devices && buf; i++) { |
344 | rc = __get_next_id(&buf, &dev_id); | |
022b660a UB |
345 | if (rc != 0) |
346 | goto error; | |
b7a610f7 | 347 | gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id); |
022b660a UB |
348 | /* |
349 | * All devices have to be of the same type in | |
350 | * order to be grouped. | |
351 | */ | |
b7a610f7 SO |
352 | if (!gdev->cdev[i] || !gdev->cdev[i]->drv || |
353 | gdev->cdev[i]->drv != gdev->cdev[0]->drv || | |
354 | gdev->cdev[i]->id.driver_info != | |
1da177e4 LT |
355 | gdev->cdev[0]->id.driver_info) { |
356 | rc = -EINVAL; | |
d76123eb | 357 | goto error; |
1da177e4 LT |
358 | } |
359 | /* Don't allow a device to belong to more than one group. */ | |
c560d105 | 360 | spin_lock_irq(gdev->cdev[i]->ccwlock); |
db6a6423 | 361 | if (dev_get_drvdata(&gdev->cdev[i]->dev)) { |
c560d105 | 362 | spin_unlock_irq(gdev->cdev[i]->ccwlock); |
1da177e4 | 363 | rc = -EINVAL; |
d76123eb | 364 | goto error; |
1da177e4 | 365 | } |
db6a6423 | 366 | dev_set_drvdata(&gdev->cdev[i]->dev, gdev); |
c560d105 | 367 | spin_unlock_irq(gdev->cdev[i]->ccwlock); |
17088229 | 368 | } |
022b660a | 369 | /* Check for sufficient number of bus ids. */ |
b7a610f7 | 370 | if (i < num_devices) { |
022b660a UB |
371 | rc = -EINVAL; |
372 | goto error; | |
373 | } | |
374 | /* Check for trailing stuff. */ | |
b7a610f7 | 375 | if (i == num_devices && strlen(buf) > 0) { |
022b660a UB |
376 | rc = -EINVAL; |
377 | goto error; | |
378 | } | |
f9a5d70c | 379 | /* Check if the devices are bound to the required ccw driver. */ |
ab7efda4 | 380 | if (gdrv && gdrv->ccw_driver && |
f9a5d70c JW |
381 | gdev->cdev[0]->drv != gdrv->ccw_driver) { |
382 | rc = -EINVAL; | |
383 | goto error; | |
384 | } | |
1da177e4 | 385 | |
1bf5b285 | 386 | dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev)); |
dbdf1afc | 387 | gdev->dev.groups = ccwgroup_attr_groups; |
f2962dae SO |
388 | |
389 | if (gdrv) { | |
390 | gdev->dev.driver = &gdrv->driver; | |
391 | rc = gdrv->setup ? gdrv->setup(gdev) : 0; | |
392 | if (rc) | |
393 | goto error; | |
394 | } | |
16f7f956 | 395 | rc = device_add(&gdev->dev); |
1da177e4 | 396 | if (rc) |
d76123eb | 397 | goto error; |
1da177e4 | 398 | rc = __ccwgroup_create_symlinks(gdev); |
dad572e3 SO |
399 | if (rc) { |
400 | device_del(&gdev->dev); | |
401 | goto error; | |
1da177e4 | 402 | } |
dad572e3 SO |
403 | mutex_unlock(&gdev->reg_mutex); |
404 | return 0; | |
1da177e4 | 405 | error: |
022b660a | 406 | for (i = 0; i < num_devices; i++) |
1da177e4 | 407 | if (gdev->cdev[i]) { |
c560d105 | 408 | spin_lock_irq(gdev->cdev[i]->ccwlock); |
db6a6423 CH |
409 | if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev) |
410 | dev_set_drvdata(&gdev->cdev[i]->dev, NULL); | |
c560d105 | 411 | spin_unlock_irq(gdev->cdev[i]->ccwlock); |
17088229 | 412 | put_device(&gdev->cdev[i]->dev); |
f26fd5d6 | 413 | gdev->cdev[i] = NULL; |
1da177e4 | 414 | } |
d76123eb CH |
415 | mutex_unlock(&gdev->reg_mutex); |
416 | put_device(&gdev->dev); | |
1da177e4 LT |
417 | return rc; |
418 | } | |
f2962dae SO |
419 | EXPORT_SYMBOL(ccwgroup_create_dev); |
420 | ||
e909074b | 421 | static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action, |
dad572e3 SO |
422 | void *data) |
423 | { | |
0b60f9ea | 424 | struct ccwgroup_device *gdev = to_ccwgroupdev(data); |
dad572e3 | 425 | |
9280ddb1 SO |
426 | if (action == BUS_NOTIFY_UNBIND_DRIVER) { |
427 | get_device(&gdev->dev); | |
0b60f9ea | 428 | schedule_work(&gdev->ungroup_work); |
9280ddb1 | 429 | } |
dad572e3 SO |
430 | |
431 | return NOTIFY_OK; | |
432 | } | |
e909074b SO |
433 | |
434 | static struct notifier_block ccwgroup_nb = { | |
435 | .notifier_call = ccwgroup_notifier | |
436 | }; | |
437 | ||
438 | static int __init init_ccwgroup(void) | |
1da177e4 | 439 | { |
e909074b SO |
440 | int ret; |
441 | ||
442 | ret = bus_register(&ccwgroup_bus_type); | |
443 | if (ret) | |
444 | return ret; | |
445 | ||
446 | ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb); | |
447 | if (ret) | |
448 | bus_unregister(&ccwgroup_bus_type); | |
449 | ||
450 | return ret; | |
1da177e4 LT |
451 | } |
452 | ||
e909074b | 453 | static void __exit cleanup_ccwgroup(void) |
1da177e4 | 454 | { |
e909074b SO |
455 | bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb); |
456 | bus_unregister(&ccwgroup_bus_type); | |
1da177e4 LT |
457 | } |
458 | ||
459 | module_init(init_ccwgroup); | |
460 | module_exit(cleanup_ccwgroup); | |
461 | ||
462 | /************************** driver stuff ******************************/ | |
463 | ||
dad572e3 | 464 | static int ccwgroup_remove(struct device *dev) |
1da177e4 | 465 | { |
dad572e3 SO |
466 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
467 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver); | |
1da177e4 | 468 | |
50f15483 SO |
469 | if (!dev->driver) |
470 | return 0; | |
50f15483 | 471 | if (gdrv->remove) |
1da177e4 | 472 | gdrv->remove(gdev); |
50f15483 | 473 | |
1da177e4 LT |
474 | return 0; |
475 | } | |
476 | ||
01bc8ad1 CH |
477 | static void ccwgroup_shutdown(struct device *dev) |
478 | { | |
dad572e3 SO |
479 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
480 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver); | |
01bc8ad1 | 481 | |
50f15483 SO |
482 | if (!dev->driver) |
483 | return; | |
50f15483 | 484 | if (gdrv->shutdown) |
01bc8ad1 CH |
485 | gdrv->shutdown(gdev); |
486 | } | |
487 | ||
7e597a21 SO |
488 | static int ccwgroup_pm_prepare(struct device *dev) |
489 | { | |
490 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); | |
491 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); | |
492 | ||
493 | /* Fail while device is being set online/offline. */ | |
494 | if (atomic_read(&gdev->onoff)) | |
495 | return -EAGAIN; | |
496 | ||
497 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) | |
498 | return 0; | |
499 | ||
500 | return gdrv->prepare ? gdrv->prepare(gdev) : 0; | |
501 | } | |
502 | ||
503 | static void ccwgroup_pm_complete(struct device *dev) | |
504 | { | |
505 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); | |
506 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver); | |
507 | ||
508 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) | |
509 | return; | |
510 | ||
511 | if (gdrv->complete) | |
512 | gdrv->complete(gdev); | |
513 | } | |
514 | ||
515 | static int ccwgroup_pm_freeze(struct device *dev) | |
516 | { | |
517 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); | |
518 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); | |
519 | ||
520 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) | |
521 | return 0; | |
522 | ||
523 | return gdrv->freeze ? gdrv->freeze(gdev) : 0; | |
524 | } | |
525 | ||
526 | static int ccwgroup_pm_thaw(struct device *dev) | |
527 | { | |
528 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); | |
529 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); | |
530 | ||
531 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) | |
532 | return 0; | |
533 | ||
534 | return gdrv->thaw ? gdrv->thaw(gdev) : 0; | |
535 | } | |
536 | ||
537 | static int ccwgroup_pm_restore(struct device *dev) | |
538 | { | |
539 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); | |
540 | struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver); | |
541 | ||
542 | if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE) | |
543 | return 0; | |
544 | ||
545 | return gdrv->restore ? gdrv->restore(gdev) : 0; | |
546 | } | |
547 | ||
47145210 | 548 | static const struct dev_pm_ops ccwgroup_pm_ops = { |
7e597a21 SO |
549 | .prepare = ccwgroup_pm_prepare, |
550 | .complete = ccwgroup_pm_complete, | |
551 | .freeze = ccwgroup_pm_freeze, | |
552 | .thaw = ccwgroup_pm_thaw, | |
553 | .restore = ccwgroup_pm_restore, | |
554 | }; | |
555 | ||
f9ccf456 RK |
556 | static struct bus_type ccwgroup_bus_type = { |
557 | .name = "ccwgroup", | |
f9ccf456 | 558 | .remove = ccwgroup_remove, |
01bc8ad1 | 559 | .shutdown = ccwgroup_shutdown, |
7e597a21 | 560 | .pm = &ccwgroup_pm_ops, |
f9ccf456 RK |
561 | }; |
562 | ||
a166c368 SO |
563 | bool dev_is_ccwgroup(struct device *dev) |
564 | { | |
565 | return dev->bus == &ccwgroup_bus_type; | |
566 | } | |
567 | EXPORT_SYMBOL(dev_is_ccwgroup); | |
568 | ||
b2ffd8e9 CH |
569 | /** |
570 | * ccwgroup_driver_register() - register a ccw group driver | |
571 | * @cdriver: driver to be registered | |
572 | * | |
573 | * This function is mainly a wrapper around driver_register(). | |
574 | */ | |
575 | int ccwgroup_driver_register(struct ccwgroup_driver *cdriver) | |
1da177e4 LT |
576 | { |
577 | /* register our new driver with the core */ | |
292888c8 | 578 | cdriver->driver.bus = &ccwgroup_bus_type; |
1da177e4 LT |
579 | |
580 | return driver_register(&cdriver->driver); | |
581 | } | |
dad572e3 | 582 | EXPORT_SYMBOL(ccwgroup_driver_register); |
1da177e4 | 583 | |
92ce7e83 | 584 | static int __ccwgroup_match_all(struct device *dev, const void *data) |
1da177e4 | 585 | { |
887ab599 | 586 | return 1; |
1da177e4 LT |
587 | } |
588 | ||
b2ffd8e9 CH |
589 | /** |
590 | * ccwgroup_driver_unregister() - deregister a ccw group driver | |
591 | * @cdriver: driver to be deregistered | |
592 | * | |
593 | * This function is mainly a wrapper around driver_unregister(). | |
594 | */ | |
595 | void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver) | |
1da177e4 | 596 | { |
887ab599 CH |
597 | struct device *dev; |
598 | ||
1da177e4 | 599 | /* We don't want ccwgroup devices to live longer than their driver. */ |
887ab599 CH |
600 | while ((dev = driver_find_device(&cdriver->driver, NULL, NULL, |
601 | __ccwgroup_match_all))) { | |
d76123eb CH |
602 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
603 | ||
fa73eb47 | 604 | ccwgroup_ungroup(gdev); |
887ab599 CH |
605 | put_device(dev); |
606 | } | |
1da177e4 LT |
607 | driver_unregister(&cdriver->driver); |
608 | } | |
dad572e3 | 609 | EXPORT_SYMBOL(ccwgroup_driver_unregister); |
1da177e4 | 610 | |
346e485d JW |
611 | /** |
612 | * get_ccwgroupdev_by_busid() - obtain device from a bus id | |
613 | * @gdrv: driver the device is owned by | |
614 | * @bus_id: bus id of the device to be searched | |
615 | * | |
616 | * This function searches all devices owned by @gdrv for a device with a bus | |
617 | * id matching @bus_id. | |
618 | * Returns: | |
619 | * If a match is found, its reference count of the found device is increased | |
620 | * and it is returned; else %NULL is returned. | |
621 | */ | |
622 | struct ccwgroup_device *get_ccwgroupdev_by_busid(struct ccwgroup_driver *gdrv, | |
623 | char *bus_id) | |
624 | { | |
625 | struct device *dev; | |
626 | ||
6cda08a2 | 627 | dev = driver_find_device_by_name(&gdrv->driver, bus_id); |
346e485d JW |
628 | |
629 | return dev ? to_ccwgroupdev(dev) : NULL; | |
630 | } | |
631 | EXPORT_SYMBOL_GPL(get_ccwgroupdev_by_busid); | |
632 | ||
b2ffd8e9 CH |
633 | /** |
634 | * ccwgroup_probe_ccwdev() - probe function for slave devices | |
635 | * @cdev: ccw device to be probed | |
636 | * | |
637 | * This is a dummy probe function for ccw devices that are slave devices in | |
638 | * a ccw group device. | |
639 | * Returns: | |
640 | * always %0 | |
641 | */ | |
642 | int ccwgroup_probe_ccwdev(struct ccw_device *cdev) | |
1da177e4 LT |
643 | { |
644 | return 0; | |
645 | } | |
dad572e3 | 646 | EXPORT_SYMBOL(ccwgroup_probe_ccwdev); |
1da177e4 | 647 | |
b2ffd8e9 CH |
648 | /** |
649 | * ccwgroup_remove_ccwdev() - remove function for slave devices | |
650 | * @cdev: ccw device to be removed | |
651 | * | |
652 | * This is a remove function for ccw devices that are slave devices in a ccw | |
653 | * group device. It sets the ccw device offline and also deregisters the | |
654 | * embedding ccw group device. | |
655 | */ | |
656 | void ccwgroup_remove_ccwdev(struct ccw_device *cdev) | |
1da177e4 LT |
657 | { |
658 | struct ccwgroup_device *gdev; | |
659 | ||
660 | /* Ignore offlining errors, device is gone anyway. */ | |
661 | ccw_device_set_offline(cdev); | |
662 | /* If one of its devices is gone, the whole group is done for. */ | |
c0301754 PO |
663 | spin_lock_irq(cdev->ccwlock); |
664 | gdev = dev_get_drvdata(&cdev->dev); | |
665 | if (!gdev) { | |
666 | spin_unlock_irq(cdev->ccwlock); | |
667 | return; | |
668 | } | |
669 | /* Get ccwgroup device reference for local processing. */ | |
670 | get_device(&gdev->dev); | |
671 | spin_unlock_irq(cdev->ccwlock); | |
672 | /* Unregister group device. */ | |
fa73eb47 | 673 | ccwgroup_ungroup(gdev); |
c0301754 PO |
674 | /* Release ccwgroup device reference for local processing. */ |
675 | put_device(&gdev->dev); | |
1da177e4 | 676 | } |
1da177e4 | 677 | EXPORT_SYMBOL(ccwgroup_remove_ccwdev); |
dad572e3 | 678 | MODULE_LICENSE("GPL"); |