blob: 959135a01847940a5ecee33ae902b83cf8ecfe44 [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 Ott683c3dc2012-11-22 15:56:39 +010068/**
69 * ccwgroup_set_online() - enable a ccwgroup device
70 * @gdev: target ccwgroup device
71 *
72 * This function attempts to put the ccwgroup device into the online state.
73 * Returns:
74 * %0 on success and a negative error value on failure.
75 */
76int ccwgroup_set_online(struct ccwgroup_device *gdev)
Sebastian Ottdad572e32011-10-30 15:16:53 +010077{
78 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
Sebastian Ottcff97102012-11-09 14:33:06 +010079 int ret = -EINVAL;
Sebastian Ottdad572e32011-10-30 15:16:53 +010080
81 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
82 return -EAGAIN;
83 if (gdev->state == CCWGROUP_ONLINE)
84 goto out;
85 if (gdrv->set_online)
86 ret = gdrv->set_online(gdev);
87 if (ret)
88 goto out;
89
90 gdev->state = CCWGROUP_ONLINE;
91out:
92 atomic_set(&gdev->onoff, 0);
93 return ret;
94}
Sebastian Ott683c3dc2012-11-22 15:56:39 +010095EXPORT_SYMBOL(ccwgroup_set_online);
Sebastian Ottdad572e32011-10-30 15:16:53 +010096
Sebastian Ott683c3dc2012-11-22 15:56:39 +010097/**
98 * ccwgroup_set_offline() - disable a ccwgroup device
99 * @gdev: target ccwgroup device
100 *
101 * This function attempts to put the ccwgroup device into the offline state.
102 * Returns:
103 * %0 on success and a negative error value on failure.
104 */
105int ccwgroup_set_offline(struct ccwgroup_device *gdev)
Sebastian Ottdad572e32011-10-30 15:16:53 +0100106{
107 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
Sebastian Ottcff97102012-11-09 14:33:06 +0100108 int ret = -EINVAL;
Sebastian Ottdad572e32011-10-30 15:16:53 +0100109
110 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
111 return -EAGAIN;
112 if (gdev->state == CCWGROUP_OFFLINE)
113 goto out;
114 if (gdrv->set_offline)
115 ret = gdrv->set_offline(gdev);
116 if (ret)
117 goto out;
118
119 gdev->state = CCWGROUP_OFFLINE;
120out:
121 atomic_set(&gdev->onoff, 0);
122 return ret;
123}
Sebastian Ott683c3dc2012-11-22 15:56:39 +0100124EXPORT_SYMBOL(ccwgroup_set_offline);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100125
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100126static ssize_t ccwgroup_online_store(struct device *dev,
127 struct device_attribute *attr,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100128 const char *buf, size_t count)
129{
130 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
131 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
132 unsigned long value;
133 int ret;
134
135 if (!dev->driver)
136 return -EINVAL;
137 if (!try_module_get(gdrv->driver.owner))
138 return -EINVAL;
139
Jingoo Han01787222013-07-22 10:18:15 +0900140 ret = kstrtoul(buf, 0, &value);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100141 if (ret)
142 goto out;
143
144 if (value == 1)
145 ret = ccwgroup_set_online(gdev);
146 else if (value == 0)
147 ret = ccwgroup_set_offline(gdev);
148 else
149 ret = -EINVAL;
150out:
151 module_put(gdrv->driver.owner);
152 return (ret == 0) ? count : ret;
153}
154
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100155static ssize_t ccwgroup_online_show(struct device *dev,
156 struct device_attribute *attr,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100157 char *buf)
158{
159 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
160 int online;
161
162 online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
163
164 return scnprintf(buf, PAGE_SIZE, "%d\n", online);
165}
166
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100167/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * Provide an 'ungroup' attribute so the user can remove group devices no
169 * longer needed or accidentially created. Saves memory :)
170 */
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400171static void ccwgroup_ungroup_callback(struct device *dev)
172{
173 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
174
Cornelia Huckd76123e2007-04-27 16:01:37 +0200175 mutex_lock(&gdev->reg_mutex);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100176 if (device_is_registered(&gdev->dev)) {
177 __ccwgroup_remove_symlinks(gdev);
178 device_unregister(dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100179 __ccwgroup_remove_cdev_refs(gdev);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100180 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200181 mutex_unlock(&gdev->reg_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400182}
183
Sebastian Ottdad572e32011-10-30 15:16:53 +0100184static ssize_t ccwgroup_ungroup_store(struct device *dev,
185 struct device_attribute *attr,
186 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100188 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400189 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100191 /* Prevent concurrent online/offline processing and ungrouping. */
192 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
193 return -EAGAIN;
194 if (gdev->state != CCWGROUP_OFFLINE) {
195 rc = -EINVAL;
196 goto out;
197 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400198 /* Note that we cannot unregister the device from one of its
199 * attribute methods, so we have to use this roundabout approach.
200 */
201 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100202out:
203 if (rc) {
Alex Chiang66942062009-03-13 12:07:36 -0600204 if (rc != -EAGAIN)
205 /* Release onoff "lock" when ungrouping failed. */
206 atomic_set(&gdev->onoff, 0);
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100207 return rc;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return count;
210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100212static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
213
214static struct attribute *ccwgroup_attrs[] = {
215 &dev_attr_online.attr,
216 &dev_attr_ungroup.attr,
217 NULL,
218};
219static struct attribute_group ccwgroup_attr_group = {
220 .attrs = ccwgroup_attrs,
221};
222static const struct attribute_group *ccwgroup_attr_groups[] = {
223 &ccwgroup_attr_group,
224 NULL,
225};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Sebastian Ottdad572e32011-10-30 15:16:53 +0100227static void ccwgroup_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100229 kfree(to_ccwgroupdev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Sebastian Ottdad572e32011-10-30 15:16:53 +0100232static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 char str[8];
235 int i, rc;
236
237 for (i = 0; i < gdev->count; i++) {
Sebastian Ottdad572e32011-10-30 15:16:53 +0100238 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
239 &gdev->dev.kobj, "group_device");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (rc) {
241 for (--i; i >= 0; i--)
242 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
243 "group_device");
244 return rc;
245 }
246 }
247 for (i = 0; i < gdev->count; i++) {
248 sprintf(str, "cdev%d", i);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100249 rc = sysfs_create_link(&gdev->dev.kobj,
250 &gdev->cdev[i]->dev.kobj, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (rc) {
252 for (--i; i >= 0; i--) {
253 sprintf(str, "cdev%d", i);
254 sysfs_remove_link(&gdev->dev.kobj, str);
255 }
256 for (i = 0; i < gdev->count; i++)
257 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
258 "group_device");
259 return rc;
260 }
261 }
262 return 0;
263}
264
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200265static int __get_next_id(const char **buf, struct ccw_dev_id *id)
Ursula Braun022b6602008-04-24 10:15:20 +0200266{
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200267 unsigned int cssid, ssid, devno;
268 int ret = 0, len;
Ursula Braun022b6602008-04-24 10:15:20 +0200269 char *start, *end;
270
271 start = (char *)*buf;
272 end = strchr(start, ',');
273 if (!end) {
274 /* Last entry. Strip trailing newline, if applicable. */
275 end = strchr(start, '\n');
276 if (end)
277 *end = '\0';
278 len = strlen(start) + 1;
279 } else {
280 len = end - start + 1;
281 end++;
282 }
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200283 if (len <= CCW_BUS_ID_SIZE) {
284 if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
285 ret = -EINVAL;
Ursula Braun022b6602008-04-24 10:15:20 +0200286 } else
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200287 ret = -EINVAL;
288
289 if (!ret) {
290 id->ssid = ssid;
291 id->devno = devno;
292 }
Ursula Braun022b6602008-04-24 10:15:20 +0200293 *buf = end;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200294 return ret;
Ursula Braun022b6602008-04-24 10:15:20 +0200295}
296
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200297/**
Sebastian Ottf2962da2012-05-15 17:49:12 +0200298 * ccwgroup_create_dev() - create and register a ccw group device
299 * @parent: parent device for the new device
Sebastian Ottf2962da2012-05-15 17:49:12 +0200300 * @gdrv: driver for the new group device
Ursula Braun022b6602008-04-24 10:15:20 +0200301 * @num_devices: number of slave devices
302 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200303 *
Sebastian Ottf2962da2012-05-15 17:49:12 +0200304 * Create and register a new ccw group device as a child of @parent. Slave
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200305 * devices are obtained from the list of bus ids given in @buf.
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200306 * Returns:
307 * %0 on success and an error code on failure.
308 * Context:
309 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Sebastian Ott9814fdf2012-05-15 18:03:46 +0200311int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
312 int num_devices, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct ccwgroup_device *gdev;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200315 struct ccw_dev_id dev_id;
Ursula Braun022b6602008-04-24 10:15:20 +0200316 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Ursula Braun022b6602008-04-24 10:15:20 +0200318 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
319 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (!gdev)
321 return -ENOMEM;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200324 mutex_init(&gdev->reg_mutex);
325 mutex_lock(&gdev->reg_mutex);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200326 gdev->count = num_devices;
327 gdev->dev.bus = &ccwgroup_bus_type;
Sebastian Ottf2962da2012-05-15 17:49:12 +0200328 gdev->dev.parent = parent;
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200329 gdev->dev.release = ccwgroup_release;
330 device_initialize(&gdev->dev);
331
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200332 for (i = 0; i < num_devices && buf; i++) {
333 rc = __get_next_id(&buf, &dev_id);
Ursula Braun022b6602008-04-24 10:15:20 +0200334 if (rc != 0)
335 goto error;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200336 gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
Ursula Braun022b6602008-04-24 10:15:20 +0200337 /*
338 * All devices have to be of the same type in
339 * order to be grouped.
340 */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200341 if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
342 gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
343 gdev->cdev[i]->id.driver_info !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 gdev->cdev[0]->id.driver_info) {
345 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200346 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 /* Don't allow a device to belong to more than one group. */
Sebastian Ottc560d102010-05-26 23:27:07 +0200349 spin_lock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100350 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Sebastian Ottc560d102010-05-26 23:27:07 +0200351 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200353 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100355 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Sebastian Ottc560d102010-05-26 23:27:07 +0200356 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huck17088222006-07-27 14:00:33 +0200357 }
Ursula Braun022b6602008-04-24 10:15:20 +0200358 /* Check for sufficient number of bus ids. */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200359 if (i < num_devices) {
Ursula Braun022b6602008-04-24 10:15:20 +0200360 rc = -EINVAL;
361 goto error;
362 }
363 /* Check for trailing stuff. */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200364 if (i == num_devices && strlen(buf) > 0) {
Ursula Braun022b6602008-04-24 10:15:20 +0200365 rc = -EINVAL;
366 goto error;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200369 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100370 gdev->dev.groups = ccwgroup_attr_groups;
Sebastian Ottf2962da2012-05-15 17:49:12 +0200371
372 if (gdrv) {
373 gdev->dev.driver = &gdrv->driver;
374 rc = gdrv->setup ? gdrv->setup(gdev) : 0;
375 if (rc)
376 goto error;
377 }
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200378 rc = device_add(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200380 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 rc = __ccwgroup_create_symlinks(gdev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100382 if (rc) {
383 device_del(&gdev->dev);
384 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Sebastian Ottdad572e32011-10-30 15:16:53 +0100386 mutex_unlock(&gdev->reg_mutex);
387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388error:
Ursula Braun022b6602008-04-24 10:15:20 +0200389 for (i = 0; i < num_devices; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (gdev->cdev[i]) {
Sebastian Ottc560d102010-05-26 23:27:07 +0200391 spin_lock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100392 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
393 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Sebastian Ottc560d102010-05-26 23:27:07 +0200394 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huck17088222006-07-27 14:00:33 +0200395 put_device(&gdev->cdev[i]->dev);
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700396 gdev->cdev[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200398 mutex_unlock(&gdev->reg_mutex);
399 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return rc;
401}
Sebastian Ottf2962da2012-05-15 17:49:12 +0200402EXPORT_SYMBOL(ccwgroup_create_dev);
403
Sebastian Otte9090742009-03-26 15:24:15 +0100404static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100405 void *data)
406{
407 struct device *dev = data;
408
409 if (action == BUS_NOTIFY_UNBIND_DRIVER)
410 device_schedule_callback(dev, ccwgroup_ungroup_callback);
411
412 return NOTIFY_OK;
413}
Sebastian Otte9090742009-03-26 15:24:15 +0100414
415static struct notifier_block ccwgroup_nb = {
416 .notifier_call = ccwgroup_notifier
417};
418
419static int __init init_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Sebastian Otte9090742009-03-26 15:24:15 +0100421 int ret;
422
423 ret = bus_register(&ccwgroup_bus_type);
424 if (ret)
425 return ret;
426
427 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
428 if (ret)
429 bus_unregister(&ccwgroup_bus_type);
430
431 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Sebastian Otte9090742009-03-26 15:24:15 +0100434static void __exit cleanup_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Sebastian Otte9090742009-03-26 15:24:15 +0100436 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
437 bus_unregister(&ccwgroup_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440module_init(init_ccwgroup);
441module_exit(cleanup_ccwgroup);
442
443/************************** driver stuff ******************************/
444
Sebastian Ottdad572e32011-10-30 15:16:53 +0100445static int ccwgroup_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100447 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
448 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Sebastian Ott50f15482009-03-26 15:24:14 +0100450 if (!dev->driver)
451 return 0;
Sebastian Ott50f15482009-03-26 15:24:14 +0100452 if (gdrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 gdrv->remove(gdev);
Sebastian Ott50f15482009-03-26 15:24:14 +0100454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
456}
457
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100458static void ccwgroup_shutdown(struct device *dev)
459{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100460 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
461 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100462
Sebastian Ott50f15482009-03-26 15:24:14 +0100463 if (!dev->driver)
464 return;
Sebastian Ott50f15482009-03-26 15:24:14 +0100465 if (gdrv->shutdown)
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100466 gdrv->shutdown(gdev);
467}
468
Sebastian Ott7e597a22009-06-16 10:30:21 +0200469static int ccwgroup_pm_prepare(struct device *dev)
470{
471 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
472 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
473
474 /* Fail while device is being set online/offline. */
475 if (atomic_read(&gdev->onoff))
476 return -EAGAIN;
477
478 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
479 return 0;
480
481 return gdrv->prepare ? gdrv->prepare(gdev) : 0;
482}
483
484static void ccwgroup_pm_complete(struct device *dev)
485{
486 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
487 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
488
489 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
490 return;
491
492 if (gdrv->complete)
493 gdrv->complete(gdev);
494}
495
496static int ccwgroup_pm_freeze(struct device *dev)
497{
498 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
499 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
500
501 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
502 return 0;
503
504 return gdrv->freeze ? gdrv->freeze(gdev) : 0;
505}
506
507static int ccwgroup_pm_thaw(struct device *dev)
508{
509 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
510 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
511
512 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
513 return 0;
514
515 return gdrv->thaw ? gdrv->thaw(gdev) : 0;
516}
517
518static int ccwgroup_pm_restore(struct device *dev)
519{
520 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
521 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
522
523 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
524 return 0;
525
526 return gdrv->restore ? gdrv->restore(gdev) : 0;
527}
528
Alexey Dobriyan47145212009-12-14 18:00:08 -0800529static const struct dev_pm_ops ccwgroup_pm_ops = {
Sebastian Ott7e597a22009-06-16 10:30:21 +0200530 .prepare = ccwgroup_pm_prepare,
531 .complete = ccwgroup_pm_complete,
532 .freeze = ccwgroup_pm_freeze,
533 .thaw = ccwgroup_pm_thaw,
534 .restore = ccwgroup_pm_restore,
535};
536
Russell Kingf9ccf452006-01-05 14:42:09 +0000537static struct bus_type ccwgroup_bus_type = {
538 .name = "ccwgroup",
Russell Kingf9ccf452006-01-05 14:42:09 +0000539 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100540 .shutdown = ccwgroup_shutdown,
Sebastian Ott7e597a22009-06-16 10:30:21 +0200541 .pm = &ccwgroup_pm_ops,
Russell Kingf9ccf452006-01-05 14:42:09 +0000542};
543
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200544/**
545 * ccwgroup_driver_register() - register a ccw group driver
546 * @cdriver: driver to be registered
547 *
548 * This function is mainly a wrapper around driver_register().
549 */
550int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200553 cdriver->driver.bus = &ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 return driver_register(&cdriver->driver);
556}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100557EXPORT_SYMBOL(ccwgroup_driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Sebastian Ottdad572e32011-10-30 15:16:53 +0100559static int __ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Cornelia Huck887ab592006-06-29 14:56:52 +0200561 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200564/**
565 * ccwgroup_driver_unregister() - deregister a ccw group driver
566 * @cdriver: driver to be deregistered
567 *
568 * This function is mainly a wrapper around driver_unregister().
569 */
570void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Cornelia Huck887ab592006-06-29 14:56:52 +0200572 struct device *dev;
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* We don't want ccwgroup devices to live longer than their driver. */
Cornelia Huck887ab592006-06-29 14:56:52 +0200575 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
576 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200577 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
578
579 mutex_lock(&gdev->reg_mutex);
580 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200581 device_unregister(dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100582 __ccwgroup_remove_cdev_refs(gdev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200583 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200584 put_device(dev);
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 driver_unregister(&cdriver->driver);
587}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100588EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200590/**
591 * ccwgroup_probe_ccwdev() - probe function for slave devices
592 * @cdev: ccw device to be probed
593 *
594 * This is a dummy probe function for ccw devices that are slave devices in
595 * a ccw group device.
596 * Returns:
597 * always %0
598 */
599int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 return 0;
602}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100603EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200605/**
606 * ccwgroup_remove_ccwdev() - remove function for slave devices
607 * @cdev: ccw device to be removed
608 *
609 * This is a remove function for ccw devices that are slave devices in a ccw
610 * group device. It sets the ccw device offline and also deregisters the
611 * embedding ccw group device.
612 */
613void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct ccwgroup_device *gdev;
616
617 /* Ignore offlining errors, device is gone anyway. */
618 ccw_device_set_offline(cdev);
619 /* If one of its devices is gone, the whole group is done for. */
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100620 spin_lock_irq(cdev->ccwlock);
621 gdev = dev_get_drvdata(&cdev->dev);
622 if (!gdev) {
623 spin_unlock_irq(cdev->ccwlock);
624 return;
625 }
626 /* Get ccwgroup device reference for local processing. */
627 get_device(&gdev->dev);
628 spin_unlock_irq(cdev->ccwlock);
629 /* Unregister group device. */
630 mutex_lock(&gdev->reg_mutex);
631 if (device_is_registered(&gdev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 __ccwgroup_remove_symlinks(gdev);
633 device_unregister(&gdev->dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100634 __ccwgroup_remove_cdev_refs(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100636 mutex_unlock(&gdev->reg_mutex);
637 /* Release ccwgroup device reference for local processing. */
638 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100641MODULE_LICENSE("GPL");