blob: 731470e68493b14e8d09830b4400e1da01dde5a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * bus driver for ccwgroup
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Sebastian Ottf2962da2012-05-15 17:49:12 +02004 * Copyright IBM Corp. 2002, 2012
Sebastian Ott7e597a22009-06-16 10:30:21 +02005 *
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
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 Ottb7a610f2012-05-15 17:52:07 +020018#include <asm/cio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/ccwdev.h>
20#include <asm/ccwgroup.h>
21
Sebastian Ottb7a610f2012-05-15 17:52:07 +020022#include "device.h"
23
24#define CCW_BUS_ID_SIZE 10
Kay Sievers98df67b2008-12-25 13:38:55 +010025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* 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 Kingf9ccf452006-01-05 14:42:09 +000033static struct bus_type ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Sebastian Ottdad572e32011-10-30 15:16:53 +010035static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
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 Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47/*
Peter Oberparleiterc0301752011-01-05 12:48:13 +010048 * Remove references from ccw devices to ccw group device and from
49 * ccw group device to ccw devices.
50 */
51static 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 Ottdad572e32011-10-30 15:16:53 +010068static 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;
83out:
84 atomic_set(&gdev->onoff, 0);
85 return ret;
86}
87
88static 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;
103out:
104 atomic_set(&gdev->onoff, 0);
105 return ret;
106}
107
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100108static ssize_t ccwgroup_online_store(struct device *dev,
109 struct device_attribute *attr,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100110 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;
132out:
133 module_put(gdrv->driver.owner);
134 return (ret == 0) ? count : ret;
135}
136
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100137static ssize_t ccwgroup_online_show(struct device *dev,
138 struct device_attribute *attr,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100139 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 Oberparleiterc0301752011-01-05 12:48:13 +0100149/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * Provide an 'ungroup' attribute so the user can remove group devices no
151 * longer needed or accidentially created. Saves memory :)
152 */
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400153static void ccwgroup_ungroup_callback(struct device *dev)
154{
155 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
156
Cornelia Huckd76123e2007-04-27 16:01:37 +0200157 mutex_lock(&gdev->reg_mutex);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100158 if (device_is_registered(&gdev->dev)) {
159 __ccwgroup_remove_symlinks(gdev);
160 device_unregister(dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100161 __ccwgroup_remove_cdev_refs(gdev);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100162 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200163 mutex_unlock(&gdev->reg_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400164}
165
Sebastian Ottdad572e32011-10-30 15:16:53 +0100166static ssize_t ccwgroup_ungroup_store(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100170 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400171 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100173 /* 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 Sternd9a9cdf2007-03-15 15:50:34 -0400180 /* 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 Oberparleiterc619d422008-12-25 13:39:04 +0100184out:
185 if (rc) {
Alex Chiang66942062009-03-13 12:07:36 -0600186 if (rc != -EAGAIN)
187 /* Release onoff "lock" when ungrouping failed. */
188 atomic_set(&gdev->onoff, 0);
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100189 return rc;
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return count;
192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100194static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
195
196static struct attribute *ccwgroup_attrs[] = {
197 &dev_attr_online.attr,
198 &dev_attr_ungroup.attr,
199 NULL,
200};
201static struct attribute_group ccwgroup_attr_group = {
202 .attrs = ccwgroup_attrs,
203};
204static const struct attribute_group *ccwgroup_attr_groups[] = {
205 &ccwgroup_attr_group,
206 NULL,
207};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Sebastian Ottdad572e32011-10-30 15:16:53 +0100209static void ccwgroup_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100211 kfree(to_ccwgroupdev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Sebastian Ottdad572e32011-10-30 15:16:53 +0100214static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 char str[8];
217 int i, rc;
218
219 for (i = 0; i < gdev->count; i++) {
Sebastian Ottdad572e32011-10-30 15:16:53 +0100220 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
221 &gdev->dev.kobj, "group_device");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 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 Ottdad572e32011-10-30 15:16:53 +0100231 rc = sysfs_create_link(&gdev->dev.kobj,
232 &gdev->cdev[i]->dev.kobj, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 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 Ottb7a610f2012-05-15 17:52:07 +0200247static int __get_next_id(const char **buf, struct ccw_dev_id *id)
Ursula Braun022b6602008-04-24 10:15:20 +0200248{
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200249 unsigned int cssid, ssid, devno;
250 int ret = 0, len;
Ursula Braun022b6602008-04-24 10:15:20 +0200251 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 Ottb7a610f2012-05-15 17:52:07 +0200265 if (len <= CCW_BUS_ID_SIZE) {
266 if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
267 ret = -EINVAL;
Ursula Braun022b6602008-04-24 10:15:20 +0200268 } else
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200269 ret = -EINVAL;
270
271 if (!ret) {
272 id->ssid = ssid;
273 id->devno = devno;
274 }
Ursula Braun022b6602008-04-24 10:15:20 +0200275 *buf = end;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200276 return ret;
Ursula Braun022b6602008-04-24 10:15:20 +0200277}
278
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200279/**
Sebastian Ottf2962da2012-05-15 17:49:12 +0200280 * ccwgroup_create_dev() - create and register a ccw group device
281 * @parent: parent device for the new device
Sebastian Ottf2962da2012-05-15 17:49:12 +0200282 * @gdrv: driver for the new group device
Ursula Braun022b6602008-04-24 10:15:20 +0200283 * @num_devices: number of slave devices
284 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200285 *
Sebastian Ottf2962da2012-05-15 17:49:12 +0200286 * Create and register a new ccw group device as a child of @parent. Slave
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200287 * devices are obtained from the list of bus ids given in @buf.
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200288 * Returns:
289 * %0 on success and an error code on failure.
290 * Context:
291 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Sebastian Ott9814fdf2012-05-15 18:03:46 +0200293int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
294 int num_devices, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct ccwgroup_device *gdev;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200297 struct ccw_dev_id dev_id;
Ursula Braun022b6602008-04-24 10:15:20 +0200298 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Ursula Braun022b6602008-04-24 10:15:20 +0200300 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
301 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!gdev)
303 return -ENOMEM;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200306 mutex_init(&gdev->reg_mutex);
307 mutex_lock(&gdev->reg_mutex);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200308 gdev->count = num_devices;
309 gdev->dev.bus = &ccwgroup_bus_type;
Sebastian Ottf2962da2012-05-15 17:49:12 +0200310 gdev->dev.parent = parent;
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200311 gdev->dev.release = ccwgroup_release;
312 device_initialize(&gdev->dev);
313
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200314 for (i = 0; i < num_devices && buf; i++) {
315 rc = __get_next_id(&buf, &dev_id);
Ursula Braun022b6602008-04-24 10:15:20 +0200316 if (rc != 0)
317 goto error;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200318 gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
Ursula Braun022b6602008-04-24 10:15:20 +0200319 /*
320 * All devices have to be of the same type in
321 * order to be grouped.
322 */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200323 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 Torvalds1da177e2005-04-16 15:20:36 -0700326 gdev->cdev[0]->id.driver_info) {
327 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200328 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 /* Don't allow a device to belong to more than one group. */
Sebastian Ottc560d102010-05-26 23:27:07 +0200331 spin_lock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100332 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Sebastian Ottc560d102010-05-26 23:27:07 +0200333 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200335 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100337 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Sebastian Ottc560d102010-05-26 23:27:07 +0200338 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huck17088222006-07-27 14:00:33 +0200339 }
Ursula Braun022b6602008-04-24 10:15:20 +0200340 /* Check for sufficient number of bus ids. */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200341 if (i < num_devices) {
Ursula Braun022b6602008-04-24 10:15:20 +0200342 rc = -EINVAL;
343 goto error;
344 }
345 /* Check for trailing stuff. */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200346 if (i == num_devices && strlen(buf) > 0) {
Ursula Braun022b6602008-04-24 10:15:20 +0200347 rc = -EINVAL;
348 goto error;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200351 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100352 gdev->dev.groups = ccwgroup_attr_groups;
Sebastian Ottf2962da2012-05-15 17:49:12 +0200353
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 Oberparleiter16f7f952008-08-21 19:46:36 +0200360 rc = device_add(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200362 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 rc = __ccwgroup_create_symlinks(gdev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100364 if (rc) {
365 device_del(&gdev->dev);
366 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Sebastian Ottdad572e32011-10-30 15:16:53 +0100368 mutex_unlock(&gdev->reg_mutex);
369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370error:
Ursula Braun022b6602008-04-24 10:15:20 +0200371 for (i = 0; i < num_devices; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (gdev->cdev[i]) {
Sebastian Ottc560d102010-05-26 23:27:07 +0200373 spin_lock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100374 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
375 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Sebastian Ottc560d102010-05-26 23:27:07 +0200376 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huck17088222006-07-27 14:00:33 +0200377 put_device(&gdev->cdev[i]->dev);
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700378 gdev->cdev[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200380 mutex_unlock(&gdev->reg_mutex);
381 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return rc;
383}
Sebastian Ottf2962da2012-05-15 17:49:12 +0200384EXPORT_SYMBOL(ccwgroup_create_dev);
385
Sebastian Otte9090742009-03-26 15:24:15 +0100386static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100387 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 Otte9090742009-03-26 15:24:15 +0100396
397static struct notifier_block ccwgroup_nb = {
398 .notifier_call = ccwgroup_notifier
399};
400
401static int __init init_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Sebastian Otte9090742009-03-26 15:24:15 +0100403 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 Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Sebastian Otte9090742009-03-26 15:24:15 +0100416static void __exit cleanup_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Sebastian Otte9090742009-03-26 15:24:15 +0100418 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
419 bus_unregister(&ccwgroup_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422module_init(init_ccwgroup);
423module_exit(cleanup_ccwgroup);
424
425/************************** driver stuff ******************************/
426
Sebastian Ottdad572e32011-10-30 15:16:53 +0100427static int ccwgroup_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100429 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
430 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Sebastian Ott50f15482009-03-26 15:24:14 +0100432 if (!dev->driver)
433 return 0;
Sebastian Ott50f15482009-03-26 15:24:14 +0100434 if (gdrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 gdrv->remove(gdev);
Sebastian Ott50f15482009-03-26 15:24:14 +0100436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438}
439
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100440static void ccwgroup_shutdown(struct device *dev)
441{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100442 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
443 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100444
Sebastian Ott50f15482009-03-26 15:24:14 +0100445 if (!dev->driver)
446 return;
Sebastian Ott50f15482009-03-26 15:24:14 +0100447 if (gdrv->shutdown)
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100448 gdrv->shutdown(gdev);
449}
450
Sebastian Ott7e597a22009-06-16 10:30:21 +0200451static 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
466static 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
478static 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
489static 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
500static 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 Dobriyan47145212009-12-14 18:00:08 -0800511static const struct dev_pm_ops ccwgroup_pm_ops = {
Sebastian Ott7e597a22009-06-16 10:30:21 +0200512 .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 Kingf9ccf452006-01-05 14:42:09 +0000519static struct bus_type ccwgroup_bus_type = {
520 .name = "ccwgroup",
Russell Kingf9ccf452006-01-05 14:42:09 +0000521 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100522 .shutdown = ccwgroup_shutdown,
Sebastian Ott7e597a22009-06-16 10:30:21 +0200523 .pm = &ccwgroup_pm_ops,
Russell Kingf9ccf452006-01-05 14:42:09 +0000524};
525
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200526/**
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 */
532int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200535 cdriver->driver.bus = &ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 return driver_register(&cdriver->driver);
538}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100539EXPORT_SYMBOL(ccwgroup_driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Sebastian Ottdad572e32011-10-30 15:16:53 +0100541static int __ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Cornelia Huck887ab592006-06-29 14:56:52 +0200543 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200546/**
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 */
552void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Cornelia Huck887ab592006-06-29 14:56:52 +0200554 struct device *dev;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* We don't want ccwgroup devices to live longer than their driver. */
Cornelia Huck887ab592006-06-29 14:56:52 +0200557 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
558 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200559 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
560
561 mutex_lock(&gdev->reg_mutex);
562 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200563 device_unregister(dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100564 __ccwgroup_remove_cdev_refs(gdev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200565 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200566 put_device(dev);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 driver_unregister(&cdriver->driver);
569}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100570EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200572/**
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 */
581int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 return 0;
584}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100585EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200587/**
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 */
595void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
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 Oberparleiterc0301752011-01-05 12:48:13 +0100602 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 Torvalds1da177e2005-04-16 15:20:36 -0700614 __ccwgroup_remove_symlinks(gdev);
615 device_unregister(&gdev->dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100616 __ccwgroup_remove_cdev_refs(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100618 mutex_unlock(&gdev->reg_mutex);
619 /* Release ccwgroup device reference for local processing. */
620 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100623MODULE_LICENSE("GPL");