blob: cda9bd6e48e8d19743290a9fec017194a116655b [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 Ott7e597a22009-06-16 10:30:21 +02004 * Copyright IBM Corp. 2002, 2009
5 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/ccwdev.h>
19#include <asm/ccwgroup.h>
20
Kay Sievers98df67b2008-12-25 13:38:55 +010021#define CCW_BUS_ID_SIZE 20
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/* In Linux 2.4, we had a channel device layer called "chandev"
24 * that did all sorts of obscure stuff for networking devices.
25 * This is another driver that serves as a replacement for just
26 * one of its functions, namely the translation of single subchannels
27 * to devices that use multiple subchannels.
28 */
29
30/* a device matches a driver if all its slave devices match the same
31 * entry of the driver */
32static int
33ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
34{
35 struct ccwgroup_device *gdev;
36 struct ccwgroup_driver *gdrv;
37
Cornelia Huck084325d2008-01-26 14:10:38 +010038 gdev = to_ccwgroupdev(dev);
39 gdrv = to_ccwgroupdrv(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 if (gdev->creator_id == gdrv->driver_id)
42 return 1;
43
44 return 0;
45}
46static int
Kay Sievers7eff2e72007-08-14 15:15:12 +020047ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 /* TODO */
50 return 0;
51}
52
Russell Kingf9ccf452006-01-05 14:42:09 +000053static struct bus_type ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Heiko Carstens4d284ca2007-02-05 21:18:53 +010055static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070056__ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
57{
58 int i;
59 char str[8];
60
61 for (i = 0; i < gdev->count; i++) {
62 sprintf(str, "cdev%d", i);
63 sysfs_remove_link(&gdev->dev.kobj, str);
64 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
65 }
66
67}
68
69/*
Peter Oberparleiterc0301752011-01-05 12:48:13 +010070 * Remove references from ccw devices to ccw group device and from
71 * ccw group device to ccw devices.
72 */
73static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
74{
75 struct ccw_device *cdev;
76 int i;
77
78 for (i = 0; i < gdev->count; i++) {
79 cdev = gdev->cdev[i];
80 if (!cdev)
81 continue;
82 spin_lock_irq(cdev->ccwlock);
83 dev_set_drvdata(&cdev->dev, NULL);
84 spin_unlock_irq(cdev->ccwlock);
85 gdev->cdev[i] = NULL;
86 put_device(&cdev->dev);
87 }
88}
89
Sebastian Ottdbdf1af2011-10-30 15:16:52 +010090static ssize_t ccwgroup_online_store(struct device *dev,
91 struct device_attribute *attr,
92 const char *buf, size_t count);
93static ssize_t ccwgroup_online_show(struct device *dev,
94 struct device_attribute *attr,
95 char *buf);
Peter Oberparleiterc0301752011-01-05 12:48:13 +010096/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * Provide an 'ungroup' attribute so the user can remove group devices no
98 * longer needed or accidentially created. Saves memory :)
99 */
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400100static void ccwgroup_ungroup_callback(struct device *dev)
101{
102 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
103
Cornelia Huckd76123e2007-04-27 16:01:37 +0200104 mutex_lock(&gdev->reg_mutex);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100105 if (device_is_registered(&gdev->dev)) {
106 __ccwgroup_remove_symlinks(gdev);
107 device_unregister(dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100108 __ccwgroup_remove_cdev_refs(gdev);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100109 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200110 mutex_unlock(&gdev->reg_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400114ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct ccwgroup_device *gdev;
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400117 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 gdev = to_ccwgroupdev(dev);
120
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100121 /* Prevent concurrent online/offline processing and ungrouping. */
122 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
123 return -EAGAIN;
124 if (gdev->state != CCWGROUP_OFFLINE) {
125 rc = -EINVAL;
126 goto out;
127 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400128 /* Note that we cannot unregister the device from one of its
129 * attribute methods, so we have to use this roundabout approach.
130 */
131 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100132out:
133 if (rc) {
Alex Chiang66942062009-03-13 12:07:36 -0600134 if (rc != -EAGAIN)
135 /* Release onoff "lock" when ungrouping failed. */
136 atomic_set(&gdev->onoff, 0);
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100137 return rc;
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return count;
140}
141
142static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100143static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
144
145static struct attribute *ccwgroup_attrs[] = {
146 &dev_attr_online.attr,
147 &dev_attr_ungroup.attr,
148 NULL,
149};
150static struct attribute_group ccwgroup_attr_group = {
151 .attrs = ccwgroup_attrs,
152};
153static const struct attribute_group *ccwgroup_attr_groups[] = {
154 &ccwgroup_attr_group,
155 NULL,
156};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158static void
159ccwgroup_release (struct device *dev)
160{
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100161 kfree(to_ccwgroupdev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100164static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165__ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
166{
167 char str[8];
168 int i, rc;
169
170 for (i = 0; i < gdev->count; i++) {
171 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
172 "group_device");
173 if (rc) {
174 for (--i; i >= 0; i--)
175 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
176 "group_device");
177 return rc;
178 }
179 }
180 for (i = 0; i < gdev->count; i++) {
181 sprintf(str, "cdev%d", i);
182 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
183 str);
184 if (rc) {
185 for (--i; i >= 0; i--) {
186 sprintf(str, "cdev%d", i);
187 sysfs_remove_link(&gdev->dev.kobj, str);
188 }
189 for (i = 0; i < gdev->count; i++)
190 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
191 "group_device");
192 return rc;
193 }
194 }
195 return 0;
196}
197
Ursula Braun022b6602008-04-24 10:15:20 +0200198static int __get_next_bus_id(const char **buf, char *bus_id)
199{
200 int rc, len;
201 char *start, *end;
202
203 start = (char *)*buf;
204 end = strchr(start, ',');
205 if (!end) {
206 /* Last entry. Strip trailing newline, if applicable. */
207 end = strchr(start, '\n');
208 if (end)
209 *end = '\0';
210 len = strlen(start) + 1;
211 } else {
212 len = end - start + 1;
213 end++;
214 }
Kay Sievers98df67b2008-12-25 13:38:55 +0100215 if (len < CCW_BUS_ID_SIZE) {
Ursula Braun022b6602008-04-24 10:15:20 +0200216 strlcpy(bus_id, start, len);
217 rc = 0;
218 } else
219 rc = -EINVAL;
220 *buf = end;
221 return rc;
222}
223
Kay Sievers98df67b2008-12-25 13:38:55 +0100224static int __is_valid_bus_id(char bus_id[CCW_BUS_ID_SIZE])
Ursula Braun022b6602008-04-24 10:15:20 +0200225{
226 int cssid, ssid, devno;
227
228 /* Must be of form %x.%x.%04x */
229 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
230 return 0;
231 return 1;
232}
233
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200234/**
Ursula Braun022b6602008-04-24 10:15:20 +0200235 * ccwgroup_create_from_string() - create and register a ccw group device
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200236 * @root: parent device for the new device
237 * @creator_id: identifier of creating driver
238 * @cdrv: ccw driver of slave devices
Ursula Braun022b6602008-04-24 10:15:20 +0200239 * @num_devices: number of slave devices
240 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200241 *
242 * Create and register a new ccw group device as a child of @root. Slave
Ursula Braun022b6602008-04-24 10:15:20 +0200243 * devices are obtained from the list of bus ids given in @buf and must all
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200244 * belong to @cdrv.
245 * Returns:
246 * %0 on success and an error code on failure.
247 * Context:
248 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Ursula Braun022b6602008-04-24 10:15:20 +0200250int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
251 struct ccw_driver *cdrv, int num_devices,
252 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct ccwgroup_device *gdev;
Ursula Braun022b6602008-04-24 10:15:20 +0200255 int rc, i;
Kay Sievers98df67b2008-12-25 13:38:55 +0100256 char tmp_bus_id[CCW_BUS_ID_SIZE];
Ursula Braun022b6602008-04-24 10:15:20 +0200257 const char *curr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Ursula Braun022b6602008-04-24 10:15:20 +0200259 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
260 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!gdev)
262 return -ENOMEM;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200265 mutex_init(&gdev->reg_mutex);
266 mutex_lock(&gdev->reg_mutex);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200267 gdev->creator_id = creator_id;
268 gdev->count = num_devices;
269 gdev->dev.bus = &ccwgroup_bus_type;
270 gdev->dev.parent = root;
271 gdev->dev.release = ccwgroup_release;
272 device_initialize(&gdev->dev);
273
Ursula Braun022b6602008-04-24 10:15:20 +0200274 curr_buf = buf;
275 for (i = 0; i < num_devices && curr_buf; i++) {
276 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
277 if (rc != 0)
278 goto error;
279 if (!__is_valid_bus_id(tmp_bus_id)) {
280 rc = -EINVAL;
281 goto error;
282 }
283 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
284 /*
285 * All devices have to be of the same type in
286 * order to be grouped.
287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!gdev->cdev[i]
289 || gdev->cdev[i]->id.driver_info !=
290 gdev->cdev[0]->id.driver_info) {
291 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200292 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 /* Don't allow a device to belong to more than one group. */
Sebastian Ottc560d102010-05-26 23:27:07 +0200295 spin_lock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100296 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Sebastian Ottc560d102010-05-26 23:27:07 +0200297 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200299 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100301 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Sebastian Ottc560d102010-05-26 23:27:07 +0200302 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huck17088222006-07-27 14:00:33 +0200303 }
Ursula Braun022b6602008-04-24 10:15:20 +0200304 /* Check for sufficient number of bus ids. */
305 if (i < num_devices && !curr_buf) {
306 rc = -EINVAL;
307 goto error;
308 }
309 /* Check for trailing stuff. */
310 if (i == num_devices && strlen(curr_buf) > 0) {
311 rc = -EINVAL;
312 goto error;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200315 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100316 gdev->dev.groups = ccwgroup_attr_groups;
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200317 rc = device_add(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200319 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 get_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 rc = __ccwgroup_create_symlinks(gdev);
322 if (!rc) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200323 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 put_device(&gdev->dev);
325 return 0;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 device_unregister(&gdev->dev);
328error:
Ursula Braun022b6602008-04-24 10:15:20 +0200329 for (i = 0; i < num_devices; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (gdev->cdev[i]) {
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) == gdev)
333 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Sebastian Ottc560d102010-05-26 23:27:07 +0200334 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huck17088222006-07-27 14:00:33 +0200335 put_device(&gdev->cdev[i]->dev);
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700336 gdev->cdev[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200338 mutex_unlock(&gdev->reg_mutex);
339 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return rc;
341}
Ursula Braun022b6602008-04-24 10:15:20 +0200342EXPORT_SYMBOL(ccwgroup_create_from_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Sebastian Otte9090742009-03-26 15:24:15 +0100344static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
345 void *data);
346
347static struct notifier_block ccwgroup_nb = {
348 .notifier_call = ccwgroup_notifier
349};
350
351static int __init init_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Sebastian Otte9090742009-03-26 15:24:15 +0100353 int ret;
354
355 ret = bus_register(&ccwgroup_bus_type);
356 if (ret)
357 return ret;
358
359 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
360 if (ret)
361 bus_unregister(&ccwgroup_bus_type);
362
363 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Sebastian Otte9090742009-03-26 15:24:15 +0100366static void __exit cleanup_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Sebastian Otte9090742009-03-26 15:24:15 +0100368 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
369 bus_unregister(&ccwgroup_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372module_init(init_ccwgroup);
373module_exit(cleanup_ccwgroup);
374
375/************************** driver stuff ******************************/
376
377static int
378ccwgroup_set_online(struct ccwgroup_device *gdev)
379{
380 struct ccwgroup_driver *gdrv;
381 int ret;
382
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800383 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -EAGAIN;
385 if (gdev->state == CCWGROUP_ONLINE) {
386 ret = 0;
387 goto out;
388 }
389 if (!gdev->dev.driver) {
390 ret = -EINVAL;
391 goto out;
392 }
393 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800394 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto out;
396
397 gdev->state = CCWGROUP_ONLINE;
398 out:
399 atomic_set(&gdev->onoff, 0);
400 return ret;
401}
402
403static int
404ccwgroup_set_offline(struct ccwgroup_device *gdev)
405{
406 struct ccwgroup_driver *gdrv;
407 int ret;
408
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800409 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -EAGAIN;
411 if (gdev->state == CCWGROUP_OFFLINE) {
412 ret = 0;
413 goto out;
414 }
415 if (!gdev->dev.driver) {
416 ret = -EINVAL;
417 goto out;
418 }
419 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800420 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 goto out;
422
423 gdev->state = CCWGROUP_OFFLINE;
424 out:
425 atomic_set(&gdev->onoff, 0);
426 return ret;
427}
428
429static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400430ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct ccwgroup_device *gdev;
433 struct ccwgroup_driver *gdrv;
Cornelia Huck2f972202008-04-30 13:38:33 +0200434 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int ret;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!dev->driver)
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100438 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Sebastian Ott40c9f992009-03-26 15:24:13 +0100440 gdev = to_ccwgroupdev(dev);
441 gdrv = to_ccwgroupdrv(dev->driver);
442
Sebastian Ott3c190c52011-03-23 10:16:04 +0100443 if (!try_module_get(gdrv->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -EINVAL;
445
Cornelia Huck2f972202008-04-30 13:38:33 +0200446 ret = strict_strtoul(buf, 0, &value);
447 if (ret)
448 goto out;
Sebastian Ott40c9f992009-03-26 15:24:13 +0100449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (value == 1)
Sebastian Ott40c9f992009-03-26 15:24:13 +0100451 ret = ccwgroup_set_online(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 else if (value == 0)
Sebastian Ott40c9f992009-03-26 15:24:13 +0100453 ret = ccwgroup_set_offline(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 else
455 ret = -EINVAL;
Cornelia Huck2f972202008-04-30 13:38:33 +0200456out:
Sebastian Ott3c190c52011-03-23 10:16:04 +0100457 module_put(gdrv->driver.owner);
Sebastian Ott40c9f992009-03-26 15:24:13 +0100458 return (ret == 0) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400462ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 int online;
465
466 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
467
468 return sprintf(buf, online ? "1\n" : "0\n");
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471static int
472ccwgroup_probe (struct device *dev)
473{
474 struct ccwgroup_device *gdev;
475 struct ccwgroup_driver *gdrv;
476
477 int ret;
478
479 gdev = to_ccwgroupdev(dev);
480 gdrv = to_ccwgroupdrv(dev->driver);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return ret;
485}
486
487static int
488ccwgroup_remove (struct device *dev)
489{
490 struct ccwgroup_device *gdev;
491 struct ccwgroup_driver *gdrv;
492
Sebastian Ott50f15482009-03-26 15:24:14 +0100493 if (!dev->driver)
494 return 0;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 gdev = to_ccwgroupdev(dev);
497 gdrv = to_ccwgroupdrv(dev->driver);
498
Sebastian Ott50f15482009-03-26 15:24:14 +0100499 if (gdrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 gdrv->remove(gdev);
Sebastian Ott50f15482009-03-26 15:24:14 +0100501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503}
504
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100505static void ccwgroup_shutdown(struct device *dev)
506{
507 struct ccwgroup_device *gdev;
508 struct ccwgroup_driver *gdrv;
509
Sebastian Ott50f15482009-03-26 15:24:14 +0100510 if (!dev->driver)
511 return;
512
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100513 gdev = to_ccwgroupdev(dev);
514 gdrv = to_ccwgroupdrv(dev->driver);
Sebastian Ott50f15482009-03-26 15:24:14 +0100515
516 if (gdrv->shutdown)
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100517 gdrv->shutdown(gdev);
518}
519
Sebastian Ott7e597a22009-06-16 10:30:21 +0200520static int ccwgroup_pm_prepare(struct device *dev)
521{
522 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
523 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
524
525 /* Fail while device is being set online/offline. */
526 if (atomic_read(&gdev->onoff))
527 return -EAGAIN;
528
529 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
530 return 0;
531
532 return gdrv->prepare ? gdrv->prepare(gdev) : 0;
533}
534
535static void ccwgroup_pm_complete(struct device *dev)
536{
537 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
538 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
539
540 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
541 return;
542
543 if (gdrv->complete)
544 gdrv->complete(gdev);
545}
546
547static int ccwgroup_pm_freeze(struct device *dev)
548{
549 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
550 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
551
552 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
553 return 0;
554
555 return gdrv->freeze ? gdrv->freeze(gdev) : 0;
556}
557
558static int ccwgroup_pm_thaw(struct device *dev)
559{
560 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
561 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
562
563 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
564 return 0;
565
566 return gdrv->thaw ? gdrv->thaw(gdev) : 0;
567}
568
569static int ccwgroup_pm_restore(struct device *dev)
570{
571 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
572 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
573
574 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
575 return 0;
576
577 return gdrv->restore ? gdrv->restore(gdev) : 0;
578}
579
Alexey Dobriyan47145212009-12-14 18:00:08 -0800580static const struct dev_pm_ops ccwgroup_pm_ops = {
Sebastian Ott7e597a22009-06-16 10:30:21 +0200581 .prepare = ccwgroup_pm_prepare,
582 .complete = ccwgroup_pm_complete,
583 .freeze = ccwgroup_pm_freeze,
584 .thaw = ccwgroup_pm_thaw,
585 .restore = ccwgroup_pm_restore,
586};
587
Russell Kingf9ccf452006-01-05 14:42:09 +0000588static struct bus_type ccwgroup_bus_type = {
589 .name = "ccwgroup",
590 .match = ccwgroup_bus_match,
591 .uevent = ccwgroup_uevent,
592 .probe = ccwgroup_probe,
593 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100594 .shutdown = ccwgroup_shutdown,
Sebastian Ott7e597a22009-06-16 10:30:21 +0200595 .pm = &ccwgroup_pm_ops,
Russell Kingf9ccf452006-01-05 14:42:09 +0000596};
597
Sebastian Otte9090742009-03-26 15:24:15 +0100598
599static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
600 void *data)
601{
602 struct device *dev = data;
603
604 if (action == BUS_NOTIFY_UNBIND_DRIVER)
605 device_schedule_callback(dev, ccwgroup_ungroup_callback);
606
607 return NOTIFY_OK;
608}
609
610
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200611/**
612 * ccwgroup_driver_register() - register a ccw group driver
613 * @cdriver: driver to be registered
614 *
615 * This function is mainly a wrapper around driver_register().
616 */
617int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200620 cdriver->driver.bus = &ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 return driver_register(&cdriver->driver);
623}
624
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700625static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200626__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Cornelia Huck887ab592006-06-29 14:56:52 +0200628 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200631/**
632 * ccwgroup_driver_unregister() - deregister a ccw group driver
633 * @cdriver: driver to be deregistered
634 *
635 * This function is mainly a wrapper around driver_unregister().
636 */
637void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Cornelia Huck887ab592006-06-29 14:56:52 +0200639 struct device *dev;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* We don't want ccwgroup devices to live longer than their driver. */
642 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200643 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
644 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200645 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
646
647 mutex_lock(&gdev->reg_mutex);
648 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200649 device_unregister(dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100650 __ccwgroup_remove_cdev_refs(gdev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200651 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200652 put_device(dev);
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 put_driver(&cdriver->driver);
655 driver_unregister(&cdriver->driver);
656}
657
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200658/**
659 * ccwgroup_probe_ccwdev() - probe function for slave devices
660 * @cdev: ccw device to be probed
661 *
662 * This is a dummy probe function for ccw devices that are slave devices in
663 * a ccw group device.
664 * Returns:
665 * always %0
666 */
667int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 return 0;
670}
671
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200672/**
673 * ccwgroup_remove_ccwdev() - remove function for slave devices
674 * @cdev: ccw device to be removed
675 *
676 * This is a remove function for ccw devices that are slave devices in a ccw
677 * group device. It sets the ccw device offline and also deregisters the
678 * embedding ccw group device.
679 */
680void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct ccwgroup_device *gdev;
683
684 /* Ignore offlining errors, device is gone anyway. */
685 ccw_device_set_offline(cdev);
686 /* If one of its devices is gone, the whole group is done for. */
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100687 spin_lock_irq(cdev->ccwlock);
688 gdev = dev_get_drvdata(&cdev->dev);
689 if (!gdev) {
690 spin_unlock_irq(cdev->ccwlock);
691 return;
692 }
693 /* Get ccwgroup device reference for local processing. */
694 get_device(&gdev->dev);
695 spin_unlock_irq(cdev->ccwlock);
696 /* Unregister group device. */
697 mutex_lock(&gdev->reg_mutex);
698 if (device_is_registered(&gdev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 __ccwgroup_remove_symlinks(gdev);
700 device_unregister(&gdev->dev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100701 __ccwgroup_remove_cdev_refs(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100703 mutex_unlock(&gdev->reg_mutex);
704 /* Release ccwgroup device reference for local processing. */
705 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708MODULE_LICENSE("GPL");
709EXPORT_SYMBOL(ccwgroup_driver_register);
710EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
712EXPORT_SYMBOL(ccwgroup_remove_ccwdev);