blob: 22ce765d537e9f0bccbca4c769131c07b3c03574 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/ccwgroup.c
3 * bus driver for ccwgroup
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10#include <linux/module.h>
11#include <linux/errno.h>
12#include <linux/slab.h>
13#include <linux/list.h>
14#include <linux/device.h>
15#include <linux/init.h>
16#include <linux/ctype.h>
17#include <linux/dcache.h>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/ccwdev.h>
20#include <asm/ccwgroup.h>
21
Kay Sievers98df67b2008-12-25 13:38:55 +010022#define CCW_BUS_ID_SIZE 20
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* In Linux 2.4, we had a channel device layer called "chandev"
25 * that did all sorts of obscure stuff for networking devices.
26 * This is another driver that serves as a replacement for just
27 * one of its functions, namely the translation of single subchannels
28 * to devices that use multiple subchannels.
29 */
30
31/* a device matches a driver if all its slave devices match the same
32 * entry of the driver */
33static int
34ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
35{
36 struct ccwgroup_device *gdev;
37 struct ccwgroup_driver *gdrv;
38
Cornelia Huck084325d2008-01-26 14:10:38 +010039 gdev = to_ccwgroupdev(dev);
40 gdrv = to_ccwgroupdrv(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 if (gdev->creator_id == gdrv->driver_id)
43 return 1;
44
45 return 0;
46}
47static int
Kay Sievers7eff2e72007-08-14 15:15:12 +020048ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 /* TODO */
51 return 0;
52}
53
Russell Kingf9ccf452006-01-05 14:42:09 +000054static struct bus_type ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Heiko Carstens4d284ca2007-02-05 21:18:53 +010056static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070057__ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
58{
59 int i;
60 char str[8];
61
62 for (i = 0; i < gdev->count; i++) {
63 sprintf(str, "cdev%d", i);
64 sysfs_remove_link(&gdev->dev.kobj, str);
65 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
66 }
67
68}
69
70/*
71 * Provide an 'ungroup' attribute so the user can remove group devices no
72 * longer needed or accidentially created. Saves memory :)
73 */
Alan Sternd9a9cdf2007-03-15 15:50:34 -040074static void ccwgroup_ungroup_callback(struct device *dev)
75{
76 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
77
Cornelia Huckd76123e2007-04-27 16:01:37 +020078 mutex_lock(&gdev->reg_mutex);
Cornelia Huck1a908c72008-01-26 14:10:50 +010079 if (device_is_registered(&gdev->dev)) {
80 __ccwgroup_remove_symlinks(gdev);
81 device_unregister(dev);
82 }
Cornelia Huckd76123e2007-04-27 16:01:37 +020083 mutex_unlock(&gdev->reg_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -040084}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -040087ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct ccwgroup_device *gdev;
Alan Sternd9a9cdf2007-03-15 15:50:34 -040090 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 gdev = to_ccwgroupdev(dev);
93
Peter Oberparleiterc619d422008-12-25 13:39:04 +010094 /* Prevent concurrent online/offline processing and ungrouping. */
95 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
96 return -EAGAIN;
97 if (gdev->state != CCWGROUP_OFFLINE) {
98 rc = -EINVAL;
99 goto out;
100 }
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400101 /* Note that we cannot unregister the device from one of its
102 * attribute methods, so we have to use this roundabout approach.
103 */
104 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100105out:
106 if (rc) {
Alex Chiang66942062009-03-13 12:07:36 -0600107 if (rc != -EAGAIN)
108 /* Release onoff "lock" when ungrouping failed. */
109 atomic_set(&gdev->onoff, 0);
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100110 return rc;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return count;
113}
114
115static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
116
117static void
118ccwgroup_release (struct device *dev)
119{
120 struct ccwgroup_device *gdev;
121 int i;
122
123 gdev = to_ccwgroupdev(dev);
124
125 for (i = 0; i < gdev->count; i++) {
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200126 if (gdev->cdev[i]) {
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700127 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
128 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200129 put_device(&gdev->cdev[i]->dev);
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132 kfree(gdev);
133}
134
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100135static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136__ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
137{
138 char str[8];
139 int i, rc;
140
141 for (i = 0; i < gdev->count; i++) {
142 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
143 "group_device");
144 if (rc) {
145 for (--i; i >= 0; i--)
146 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
147 "group_device");
148 return rc;
149 }
150 }
151 for (i = 0; i < gdev->count; i++) {
152 sprintf(str, "cdev%d", i);
153 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
154 str);
155 if (rc) {
156 for (--i; i >= 0; i--) {
157 sprintf(str, "cdev%d", i);
158 sysfs_remove_link(&gdev->dev.kobj, str);
159 }
160 for (i = 0; i < gdev->count; i++)
161 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
162 "group_device");
163 return rc;
164 }
165 }
166 return 0;
167}
168
Ursula Braun022b6602008-04-24 10:15:20 +0200169static int __get_next_bus_id(const char **buf, char *bus_id)
170{
171 int rc, len;
172 char *start, *end;
173
174 start = (char *)*buf;
175 end = strchr(start, ',');
176 if (!end) {
177 /* Last entry. Strip trailing newline, if applicable. */
178 end = strchr(start, '\n');
179 if (end)
180 *end = '\0';
181 len = strlen(start) + 1;
182 } else {
183 len = end - start + 1;
184 end++;
185 }
Kay Sievers98df67b2008-12-25 13:38:55 +0100186 if (len < CCW_BUS_ID_SIZE) {
Ursula Braun022b6602008-04-24 10:15:20 +0200187 strlcpy(bus_id, start, len);
188 rc = 0;
189 } else
190 rc = -EINVAL;
191 *buf = end;
192 return rc;
193}
194
Kay Sievers98df67b2008-12-25 13:38:55 +0100195static int __is_valid_bus_id(char bus_id[CCW_BUS_ID_SIZE])
Ursula Braun022b6602008-04-24 10:15:20 +0200196{
197 int cssid, ssid, devno;
198
199 /* Must be of form %x.%x.%04x */
200 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
201 return 0;
202 return 1;
203}
204
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200205/**
Ursula Braun022b6602008-04-24 10:15:20 +0200206 * ccwgroup_create_from_string() - create and register a ccw group device
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200207 * @root: parent device for the new device
208 * @creator_id: identifier of creating driver
209 * @cdrv: ccw driver of slave devices
Ursula Braun022b6602008-04-24 10:15:20 +0200210 * @num_devices: number of slave devices
211 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200212 *
213 * Create and register a new ccw group device as a child of @root. Slave
Ursula Braun022b6602008-04-24 10:15:20 +0200214 * devices are obtained from the list of bus ids given in @buf and must all
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200215 * belong to @cdrv.
216 * Returns:
217 * %0 on success and an error code on failure.
218 * Context:
219 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
Ursula Braun022b6602008-04-24 10:15:20 +0200221int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
222 struct ccw_driver *cdrv, int num_devices,
223 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct ccwgroup_device *gdev;
Ursula Braun022b6602008-04-24 10:15:20 +0200226 int rc, i;
Kay Sievers98df67b2008-12-25 13:38:55 +0100227 char tmp_bus_id[CCW_BUS_ID_SIZE];
Ursula Braun022b6602008-04-24 10:15:20 +0200228 const char *curr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Ursula Braun022b6602008-04-24 10:15:20 +0200230 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
231 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!gdev)
233 return -ENOMEM;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200236 mutex_init(&gdev->reg_mutex);
237 mutex_lock(&gdev->reg_mutex);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200238 gdev->creator_id = creator_id;
239 gdev->count = num_devices;
240 gdev->dev.bus = &ccwgroup_bus_type;
241 gdev->dev.parent = root;
242 gdev->dev.release = ccwgroup_release;
243 device_initialize(&gdev->dev);
244
Ursula Braun022b6602008-04-24 10:15:20 +0200245 curr_buf = buf;
246 for (i = 0; i < num_devices && curr_buf; i++) {
247 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
248 if (rc != 0)
249 goto error;
250 if (!__is_valid_bus_id(tmp_bus_id)) {
251 rc = -EINVAL;
252 goto error;
253 }
254 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
255 /*
256 * All devices have to be of the same type in
257 * order to be grouped.
258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (!gdev->cdev[i]
260 || gdev->cdev[i]->id.driver_info !=
261 gdev->cdev[0]->id.driver_info) {
262 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200263 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 /* Don't allow a device to belong to more than one group. */
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100266 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200268 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100270 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Cornelia Huck17088222006-07-27 14:00:33 +0200271 }
Ursula Braun022b6602008-04-24 10:15:20 +0200272 /* Check for sufficient number of bus ids. */
273 if (i < num_devices && !curr_buf) {
274 rc = -EINVAL;
275 goto error;
276 }
277 /* Check for trailing stuff. */
278 if (i == num_devices && strlen(curr_buf) > 0) {
279 rc = -EINVAL;
280 goto error;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200283 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200285 rc = device_add(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200287 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 get_device(&gdev->dev);
289 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
290
291 if (rc) {
292 device_unregister(&gdev->dev);
293 goto error;
294 }
295
296 rc = __ccwgroup_create_symlinks(gdev);
297 if (!rc) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200298 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 put_device(&gdev->dev);
300 return 0;
301 }
302 device_remove_file(&gdev->dev, &dev_attr_ungroup);
303 device_unregister(&gdev->dev);
304error:
Ursula Braun022b6602008-04-24 10:15:20 +0200305 for (i = 0; i < num_devices; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (gdev->cdev[i]) {
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100307 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
308 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Cornelia Huck17088222006-07-27 14:00:33 +0200309 put_device(&gdev->cdev[i]->dev);
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700310 gdev->cdev[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200312 mutex_unlock(&gdev->reg_mutex);
313 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return rc;
315}
Ursula Braun022b6602008-04-24 10:15:20 +0200316EXPORT_SYMBOL(ccwgroup_create_from_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Sebastian Otte9090742009-03-26 15:24:15 +0100318static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
319 void *data);
320
321static struct notifier_block ccwgroup_nb = {
322 .notifier_call = ccwgroup_notifier
323};
324
325static int __init init_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Sebastian Otte9090742009-03-26 15:24:15 +0100327 int ret;
328
329 ret = bus_register(&ccwgroup_bus_type);
330 if (ret)
331 return ret;
332
333 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
334 if (ret)
335 bus_unregister(&ccwgroup_bus_type);
336
337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Sebastian Otte9090742009-03-26 15:24:15 +0100340static void __exit cleanup_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Sebastian Otte9090742009-03-26 15:24:15 +0100342 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
343 bus_unregister(&ccwgroup_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346module_init(init_ccwgroup);
347module_exit(cleanup_ccwgroup);
348
349/************************** driver stuff ******************************/
350
351static int
352ccwgroup_set_online(struct ccwgroup_device *gdev)
353{
354 struct ccwgroup_driver *gdrv;
355 int ret;
356
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800357 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -EAGAIN;
359 if (gdev->state == CCWGROUP_ONLINE) {
360 ret = 0;
361 goto out;
362 }
363 if (!gdev->dev.driver) {
364 ret = -EINVAL;
365 goto out;
366 }
367 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800368 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto out;
370
371 gdev->state = CCWGROUP_ONLINE;
372 out:
373 atomic_set(&gdev->onoff, 0);
374 return ret;
375}
376
377static int
378ccwgroup_set_offline(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_OFFLINE) {
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_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto out;
396
397 gdev->state = CCWGROUP_OFFLINE;
398 out:
399 atomic_set(&gdev->onoff, 0);
400 return ret;
401}
402
403static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400404ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct ccwgroup_device *gdev;
407 struct ccwgroup_driver *gdrv;
Cornelia Huck2f972202008-04-30 13:38:33 +0200408 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int ret;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!dev->driver)
Sebastian Ott40c9f992009-03-26 15:24:13 +0100412 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Sebastian Ott40c9f992009-03-26 15:24:13 +0100414 gdev = to_ccwgroupdev(dev);
415 gdrv = to_ccwgroupdrv(dev->driver);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!try_module_get(gdrv->owner))
418 return -EINVAL;
419
Cornelia Huck2f972202008-04-30 13:38:33 +0200420 ret = strict_strtoul(buf, 0, &value);
421 if (ret)
422 goto out;
Sebastian Ott40c9f992009-03-26 15:24:13 +0100423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (value == 1)
Sebastian Ott40c9f992009-03-26 15:24:13 +0100425 ret = ccwgroup_set_online(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 else if (value == 0)
Sebastian Ott40c9f992009-03-26 15:24:13 +0100427 ret = ccwgroup_set_offline(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 else
429 ret = -EINVAL;
Cornelia Huck2f972202008-04-30 13:38:33 +0200430out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 module_put(gdrv->owner);
Sebastian Ott40c9f992009-03-26 15:24:13 +0100432 return (ret == 0) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400436ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 int online;
439
440 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
441
442 return sprintf(buf, online ? "1\n" : "0\n");
443}
444
445static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
446
447static int
448ccwgroup_probe (struct device *dev)
449{
450 struct ccwgroup_device *gdev;
451 struct ccwgroup_driver *gdrv;
452
453 int ret;
454
455 gdev = to_ccwgroupdev(dev);
456 gdrv = to_ccwgroupdrv(dev->driver);
457
458 if ((ret = device_create_file(dev, &dev_attr_online)))
459 return ret;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
462 if (ret)
463 device_remove_file(dev, &dev_attr_online);
464
465 return ret;
466}
467
468static int
469ccwgroup_remove (struct device *dev)
470{
471 struct ccwgroup_device *gdev;
472 struct ccwgroup_driver *gdrv;
473
Sebastian Ott50f15482009-03-26 15:24:14 +0100474 device_remove_file(dev, &dev_attr_online);
Sebastian Otte9090742009-03-26 15:24:15 +0100475 device_remove_file(dev, &dev_attr_ungroup);
Sebastian Ott50f15482009-03-26 15:24:14 +0100476
477 if (!dev->driver)
478 return 0;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 gdev = to_ccwgroupdev(dev);
481 gdrv = to_ccwgroupdrv(dev->driver);
482
Sebastian Ott50f15482009-03-26 15:24:14 +0100483 if (gdrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 gdrv->remove(gdev);
Sebastian Ott50f15482009-03-26 15:24:14 +0100485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
487}
488
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100489static void ccwgroup_shutdown(struct device *dev)
490{
491 struct ccwgroup_device *gdev;
492 struct ccwgroup_driver *gdrv;
493
Sebastian Ott50f15482009-03-26 15:24:14 +0100494 if (!dev->driver)
495 return;
496
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100497 gdev = to_ccwgroupdev(dev);
498 gdrv = to_ccwgroupdrv(dev->driver);
Sebastian Ott50f15482009-03-26 15:24:14 +0100499
500 if (gdrv->shutdown)
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100501 gdrv->shutdown(gdev);
502}
503
Russell Kingf9ccf452006-01-05 14:42:09 +0000504static struct bus_type ccwgroup_bus_type = {
505 .name = "ccwgroup",
506 .match = ccwgroup_bus_match,
507 .uevent = ccwgroup_uevent,
508 .probe = ccwgroup_probe,
509 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100510 .shutdown = ccwgroup_shutdown,
Russell Kingf9ccf452006-01-05 14:42:09 +0000511};
512
Sebastian Otte9090742009-03-26 15:24:15 +0100513
514static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
515 void *data)
516{
517 struct device *dev = data;
518
519 if (action == BUS_NOTIFY_UNBIND_DRIVER)
520 device_schedule_callback(dev, ccwgroup_ungroup_callback);
521
522 return NOTIFY_OK;
523}
524
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;
536 cdriver->driver.name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +0100537 cdriver->driver.owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 return driver_register(&cdriver->driver);
540}
541
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700542static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200543__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Cornelia Huck887ab592006-06-29 14:56:52 +0200545 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200548/**
549 * ccwgroup_driver_unregister() - deregister a ccw group driver
550 * @cdriver: driver to be deregistered
551 *
552 * This function is mainly a wrapper around driver_unregister().
553 */
554void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Cornelia Huck887ab592006-06-29 14:56:52 +0200556 struct device *dev;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* We don't want ccwgroup devices to live longer than their driver. */
559 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200560 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
561 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200562 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
563
564 mutex_lock(&gdev->reg_mutex);
565 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200566 device_unregister(dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200567 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200568 put_device(dev);
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 put_driver(&cdriver->driver);
571 driver_unregister(&cdriver->driver);
572}
573
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200574/**
575 * ccwgroup_probe_ccwdev() - probe function for slave devices
576 * @cdev: ccw device to be probed
577 *
578 * This is a dummy probe function for ccw devices that are slave devices in
579 * a ccw group device.
580 * Returns:
581 * always %0
582 */
583int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 return 0;
586}
587
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100588static struct ccwgroup_device *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
590{
591 struct ccwgroup_device *gdev;
592
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100593 gdev = dev_get_drvdata(&cdev->dev);
594 if (gdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (get_device(&gdev->dev)) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200596 mutex_lock(&gdev->reg_mutex);
Daniel Ritzd305ef52005-09-22 00:47:24 -0700597 if (device_is_registered(&gdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return gdev;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200599 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 put_device(&gdev->dev);
601 }
602 return NULL;
603 }
604 return NULL;
605}
606
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200607/**
608 * ccwgroup_remove_ccwdev() - remove function for slave devices
609 * @cdev: ccw device to be removed
610 *
611 * This is a remove function for ccw devices that are slave devices in a ccw
612 * group device. It sets the ccw device offline and also deregisters the
613 * embedding ccw group device.
614 */
615void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct ccwgroup_device *gdev;
618
619 /* Ignore offlining errors, device is gone anyway. */
620 ccw_device_set_offline(cdev);
621 /* If one of its devices is gone, the whole group is done for. */
622 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
623 if (gdev) {
624 __ccwgroup_remove_symlinks(gdev);
625 device_unregister(&gdev->dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200626 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 put_device(&gdev->dev);
628 }
629}
630
631MODULE_LICENSE("GPL");
632EXPORT_SYMBOL(ccwgroup_driver_register);
633EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
635EXPORT_SYMBOL(ccwgroup_remove_ccwdev);