blob: e443b0d0b23612efec3800a5468e36e58ef7f8d3 [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);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100131 unsigned long value;
132 int ret;
133
Sebastian Ottd5b877f2013-12-16 10:56:46 +0100134 device_lock(dev);
135 if (!dev->driver) {
136 ret = -EINVAL;
137 goto out;
138 }
Sebastian Ottdad572e32011-10-30 15:16:53 +0100139
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:
Sebastian Ottd5b877f2013-12-16 10:56:46 +0100151 device_unlock(dev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100152 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 */
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500171static void ccwgroup_ungroup(struct ccwgroup_device *gdev)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400172{
Cornelia Huckd76123e2007-04-27 16:01:37 +0200173 mutex_lock(&gdev->reg_mutex);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100174 if (device_is_registered(&gdev->dev)) {
175 __ccwgroup_remove_symlinks(gdev);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500176 device_unregister(&gdev->dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100177 __ccwgroup_remove_cdev_refs(gdev);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100178 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200179 mutex_unlock(&gdev->reg_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400180}
181
Sebastian Ottdad572e32011-10-30 15:16:53 +0100182static ssize_t ccwgroup_ungroup_store(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100186 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
Sebastian Ott0310c8b2014-06-13 17:29:11 +0200187 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100189 /* Prevent concurrent online/offline processing and ungrouping. */
190 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
191 return -EAGAIN;
192 if (gdev->state != CCWGROUP_OFFLINE) {
193 rc = -EINVAL;
194 goto out;
195 }
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500196
197 if (device_remove_file_self(dev, attr))
198 ccwgroup_ungroup(gdev);
Sebastian Ott0310c8b2014-06-13 17:29:11 +0200199 else
200 rc = -ENODEV;
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100201out:
202 if (rc) {
Sebastian Ott0310c8b2014-06-13 17:29:11 +0200203 /* Release onoff "lock" when ungrouping failed. */
204 atomic_set(&gdev->onoff, 0);
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100205 return rc;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return count;
208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100210static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
211
212static struct attribute *ccwgroup_attrs[] = {
213 &dev_attr_online.attr,
214 &dev_attr_ungroup.attr,
215 NULL,
216};
217static struct attribute_group ccwgroup_attr_group = {
218 .attrs = ccwgroup_attrs,
219};
220static const struct attribute_group *ccwgroup_attr_groups[] = {
221 &ccwgroup_attr_group,
222 NULL,
223};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500225static void ccwgroup_ungroup_workfn(struct work_struct *work)
226{
227 struct ccwgroup_device *gdev =
228 container_of(work, struct ccwgroup_device, ungroup_work);
229
230 ccwgroup_ungroup(gdev);
Sebastian Ott9280ddb2014-06-13 17:02:24 +0200231 put_device(&gdev->dev);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500232}
233
Sebastian Ottdad572e32011-10-30 15:16:53 +0100234static void ccwgroup_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100236 kfree(to_ccwgroupdev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Sebastian Ottdad572e32011-10-30 15:16:53 +0100239static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 char str[8];
242 int i, rc;
243
244 for (i = 0; i < gdev->count; i++) {
Sebastian Ottdad572e32011-10-30 15:16:53 +0100245 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
246 &gdev->dev.kobj, "group_device");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (rc) {
248 for (--i; i >= 0; i--)
249 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
250 "group_device");
251 return rc;
252 }
253 }
254 for (i = 0; i < gdev->count; i++) {
255 sprintf(str, "cdev%d", i);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100256 rc = sysfs_create_link(&gdev->dev.kobj,
257 &gdev->cdev[i]->dev.kobj, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (rc) {
259 for (--i; i >= 0; i--) {
260 sprintf(str, "cdev%d", i);
261 sysfs_remove_link(&gdev->dev.kobj, str);
262 }
263 for (i = 0; i < gdev->count; i++)
264 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
265 "group_device");
266 return rc;
267 }
268 }
269 return 0;
270}
271
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200272static int __get_next_id(const char **buf, struct ccw_dev_id *id)
Ursula Braun022b6602008-04-24 10:15:20 +0200273{
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200274 unsigned int cssid, ssid, devno;
275 int ret = 0, len;
Ursula Braun022b6602008-04-24 10:15:20 +0200276 char *start, *end;
277
278 start = (char *)*buf;
279 end = strchr(start, ',');
280 if (!end) {
281 /* Last entry. Strip trailing newline, if applicable. */
282 end = strchr(start, '\n');
283 if (end)
284 *end = '\0';
285 len = strlen(start) + 1;
286 } else {
287 len = end - start + 1;
288 end++;
289 }
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200290 if (len <= CCW_BUS_ID_SIZE) {
291 if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
292 ret = -EINVAL;
Ursula Braun022b6602008-04-24 10:15:20 +0200293 } else
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200294 ret = -EINVAL;
295
296 if (!ret) {
297 id->ssid = ssid;
298 id->devno = devno;
299 }
Ursula Braun022b6602008-04-24 10:15:20 +0200300 *buf = end;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200301 return ret;
Ursula Braun022b6602008-04-24 10:15:20 +0200302}
303
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200304/**
Sebastian Ottf2962da2012-05-15 17:49:12 +0200305 * ccwgroup_create_dev() - create and register a ccw group device
306 * @parent: parent device for the new device
Sebastian Ottf2962da2012-05-15 17:49:12 +0200307 * @gdrv: driver for the new group device
Ursula Braun022b6602008-04-24 10:15:20 +0200308 * @num_devices: number of slave devices
309 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200310 *
Sebastian Ottf2962da2012-05-15 17:49:12 +0200311 * Create and register a new ccw group device as a child of @parent. Slave
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200312 * devices are obtained from the list of bus ids given in @buf.
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200313 * Returns:
314 * %0 on success and an error code on failure.
315 * Context:
316 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Sebastian Ott9814fdf2012-05-15 18:03:46 +0200318int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
319 int num_devices, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 struct ccwgroup_device *gdev;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200322 struct ccw_dev_id dev_id;
Ursula Braun022b6602008-04-24 10:15:20 +0200323 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Ursula Braun022b6602008-04-24 10:15:20 +0200325 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
326 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!gdev)
328 return -ENOMEM;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200331 mutex_init(&gdev->reg_mutex);
332 mutex_lock(&gdev->reg_mutex);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500333 INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200334 gdev->count = num_devices;
335 gdev->dev.bus = &ccwgroup_bus_type;
Sebastian Ottf2962da2012-05-15 17:49:12 +0200336 gdev->dev.parent = parent;
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200337 gdev->dev.release = ccwgroup_release;
338 device_initialize(&gdev->dev);
339
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200340 for (i = 0; i < num_devices && buf; i++) {
341 rc = __get_next_id(&buf, &dev_id);
Ursula Braun022b6602008-04-24 10:15:20 +0200342 if (rc != 0)
343 goto error;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200344 gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
Ursula Braun022b6602008-04-24 10:15:20 +0200345 /*
346 * All devices have to be of the same type in
347 * order to be grouped.
348 */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200349 if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
350 gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
351 gdev->cdev[i]->id.driver_info !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 gdev->cdev[0]->id.driver_info) {
353 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200354 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 /* Don't allow a device to belong to more than one group. */
Sebastian Ottc560d102010-05-26 23:27:07 +0200357 spin_lock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100358 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Sebastian Ottc560d102010-05-26 23:27:07 +0200359 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200361 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100363 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Sebastian Ottc560d102010-05-26 23:27:07 +0200364 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huck17088222006-07-27 14:00:33 +0200365 }
Ursula Braun022b6602008-04-24 10:15:20 +0200366 /* Check for sufficient number of bus ids. */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200367 if (i < num_devices) {
Ursula Braun022b6602008-04-24 10:15:20 +0200368 rc = -EINVAL;
369 goto error;
370 }
371 /* Check for trailing stuff. */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200372 if (i == num_devices && strlen(buf) > 0) {
Ursula Braun022b6602008-04-24 10:15:20 +0200373 rc = -EINVAL;
374 goto error;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200377 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100378 gdev->dev.groups = ccwgroup_attr_groups;
Sebastian Ottf2962da2012-05-15 17:49:12 +0200379
380 if (gdrv) {
381 gdev->dev.driver = &gdrv->driver;
382 rc = gdrv->setup ? gdrv->setup(gdev) : 0;
383 if (rc)
384 goto error;
385 }
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200386 rc = device_add(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200388 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 rc = __ccwgroup_create_symlinks(gdev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100390 if (rc) {
391 device_del(&gdev->dev);
392 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Sebastian Ottdad572e32011-10-30 15:16:53 +0100394 mutex_unlock(&gdev->reg_mutex);
395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396error:
Ursula Braun022b6602008-04-24 10:15:20 +0200397 for (i = 0; i < num_devices; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (gdev->cdev[i]) {
Sebastian Ottc560d102010-05-26 23:27:07 +0200399 spin_lock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100400 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
401 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Sebastian Ottc560d102010-05-26 23:27:07 +0200402 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huck17088222006-07-27 14:00:33 +0200403 put_device(&gdev->cdev[i]->dev);
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700404 gdev->cdev[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200406 mutex_unlock(&gdev->reg_mutex);
407 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return rc;
409}
Sebastian Ottf2962da2012-05-15 17:49:12 +0200410EXPORT_SYMBOL(ccwgroup_create_dev);
411
Sebastian Otte9090742009-03-26 15:24:15 +0100412static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100413 void *data)
414{
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500415 struct ccwgroup_device *gdev = to_ccwgroupdev(data);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100416
Sebastian Ott9280ddb2014-06-13 17:02:24 +0200417 if (action == BUS_NOTIFY_UNBIND_DRIVER) {
418 get_device(&gdev->dev);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500419 schedule_work(&gdev->ungroup_work);
Sebastian Ott9280ddb2014-06-13 17:02:24 +0200420 }
Sebastian Ottdad572e32011-10-30 15:16:53 +0100421
422 return NOTIFY_OK;
423}
Sebastian Otte9090742009-03-26 15:24:15 +0100424
425static struct notifier_block ccwgroup_nb = {
426 .notifier_call = ccwgroup_notifier
427};
428
429static int __init init_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Sebastian Otte9090742009-03-26 15:24:15 +0100431 int ret;
432
433 ret = bus_register(&ccwgroup_bus_type);
434 if (ret)
435 return ret;
436
437 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
438 if (ret)
439 bus_unregister(&ccwgroup_bus_type);
440
441 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Sebastian Otte9090742009-03-26 15:24:15 +0100444static void __exit cleanup_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Sebastian Otte9090742009-03-26 15:24:15 +0100446 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
447 bus_unregister(&ccwgroup_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450module_init(init_ccwgroup);
451module_exit(cleanup_ccwgroup);
452
453/************************** driver stuff ******************************/
454
Sebastian Ottdad572e32011-10-30 15:16:53 +0100455static int ccwgroup_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100457 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
458 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Sebastian Ott50f15482009-03-26 15:24:14 +0100460 if (!dev->driver)
461 return 0;
Sebastian Ott50f15482009-03-26 15:24:14 +0100462 if (gdrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 gdrv->remove(gdev);
Sebastian Ott50f15482009-03-26 15:24:14 +0100464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466}
467
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100468static void ccwgroup_shutdown(struct device *dev)
469{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100470 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
471 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100472
Sebastian Ott50f15482009-03-26 15:24:14 +0100473 if (!dev->driver)
474 return;
Sebastian Ott50f15482009-03-26 15:24:14 +0100475 if (gdrv->shutdown)
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100476 gdrv->shutdown(gdev);
477}
478
Sebastian Ott7e597a22009-06-16 10:30:21 +0200479static int ccwgroup_pm_prepare(struct device *dev)
480{
481 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
482 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
483
484 /* Fail while device is being set online/offline. */
485 if (atomic_read(&gdev->onoff))
486 return -EAGAIN;
487
488 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
489 return 0;
490
491 return gdrv->prepare ? gdrv->prepare(gdev) : 0;
492}
493
494static void ccwgroup_pm_complete(struct device *dev)
495{
496 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
497 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
498
499 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
500 return;
501
502 if (gdrv->complete)
503 gdrv->complete(gdev);
504}
505
506static int ccwgroup_pm_freeze(struct device *dev)
507{
508 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
509 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
510
511 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
512 return 0;
513
514 return gdrv->freeze ? gdrv->freeze(gdev) : 0;
515}
516
517static int ccwgroup_pm_thaw(struct device *dev)
518{
519 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
520 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
521
522 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
523 return 0;
524
525 return gdrv->thaw ? gdrv->thaw(gdev) : 0;
526}
527
528static int ccwgroup_pm_restore(struct device *dev)
529{
530 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
531 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
532
533 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
534 return 0;
535
536 return gdrv->restore ? gdrv->restore(gdev) : 0;
537}
538
Alexey Dobriyan47145212009-12-14 18:00:08 -0800539static const struct dev_pm_ops ccwgroup_pm_ops = {
Sebastian Ott7e597a22009-06-16 10:30:21 +0200540 .prepare = ccwgroup_pm_prepare,
541 .complete = ccwgroup_pm_complete,
542 .freeze = ccwgroup_pm_freeze,
543 .thaw = ccwgroup_pm_thaw,
544 .restore = ccwgroup_pm_restore,
545};
546
Russell Kingf9ccf452006-01-05 14:42:09 +0000547static struct bus_type ccwgroup_bus_type = {
548 .name = "ccwgroup",
Russell Kingf9ccf452006-01-05 14:42:09 +0000549 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100550 .shutdown = ccwgroup_shutdown,
Sebastian Ott7e597a22009-06-16 10:30:21 +0200551 .pm = &ccwgroup_pm_ops,
Russell Kingf9ccf452006-01-05 14:42:09 +0000552};
553
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200554/**
555 * ccwgroup_driver_register() - register a ccw group driver
556 * @cdriver: driver to be registered
557 *
558 * This function is mainly a wrapper around driver_register().
559 */
560int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200563 cdriver->driver.bus = &ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 return driver_register(&cdriver->driver);
566}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100567EXPORT_SYMBOL(ccwgroup_driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Sebastian Ottdad572e32011-10-30 15:16:53 +0100569static int __ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Cornelia Huck887ab592006-06-29 14:56:52 +0200571 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200574/**
575 * ccwgroup_driver_unregister() - deregister a ccw group driver
576 * @cdriver: driver to be deregistered
577 *
578 * This function is mainly a wrapper around driver_unregister().
579 */
580void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Cornelia Huck887ab592006-06-29 14:56:52 +0200582 struct device *dev;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* We don't want ccwgroup devices to live longer than their driver. */
Cornelia Huck887ab592006-06-29 14:56:52 +0200585 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
586 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200587 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
588
Sebastian Ottfa73eb42014-06-13 18:11:37 +0200589 ccwgroup_ungroup(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200590 put_device(dev);
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 driver_unregister(&cdriver->driver);
593}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100594EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200596/**
597 * ccwgroup_probe_ccwdev() - probe function for slave devices
598 * @cdev: ccw device to be probed
599 *
600 * This is a dummy probe function for ccw devices that are slave devices in
601 * a ccw group device.
602 * Returns:
603 * always %0
604 */
605int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 return 0;
608}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100609EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200611/**
612 * ccwgroup_remove_ccwdev() - remove function for slave devices
613 * @cdev: ccw device to be removed
614 *
615 * This is a remove function for ccw devices that are slave devices in a ccw
616 * group device. It sets the ccw device offline and also deregisters the
617 * embedding ccw group device.
618 */
619void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct ccwgroup_device *gdev;
622
623 /* Ignore offlining errors, device is gone anyway. */
624 ccw_device_set_offline(cdev);
625 /* If one of its devices is gone, the whole group is done for. */
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100626 spin_lock_irq(cdev->ccwlock);
627 gdev = dev_get_drvdata(&cdev->dev);
628 if (!gdev) {
629 spin_unlock_irq(cdev->ccwlock);
630 return;
631 }
632 /* Get ccwgroup device reference for local processing. */
633 get_device(&gdev->dev);
634 spin_unlock_irq(cdev->ccwlock);
635 /* Unregister group device. */
Sebastian Ottfa73eb42014-06-13 18:11:37 +0200636 ccwgroup_ungroup(gdev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100637 /* 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");