blob: 86b136cb09e0b0f7fb0b315fcebbf3e204b183cc [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) {
107 /* Release onoff "lock" when ungrouping failed. */
108 atomic_set(&gdev->onoff, 0);
109 return rc;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return count;
112}
113
114static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
115
116static void
117ccwgroup_release (struct device *dev)
118{
119 struct ccwgroup_device *gdev;
120 int i;
121
122 gdev = to_ccwgroupdev(dev);
123
124 for (i = 0; i < gdev->count; i++) {
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200125 if (gdev->cdev[i]) {
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700126 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
127 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200128 put_device(&gdev->cdev[i]->dev);
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 kfree(gdev);
132}
133
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100134static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135__ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
136{
137 char str[8];
138 int i, rc;
139
140 for (i = 0; i < gdev->count; i++) {
141 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
142 "group_device");
143 if (rc) {
144 for (--i; i >= 0; i--)
145 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
146 "group_device");
147 return rc;
148 }
149 }
150 for (i = 0; i < gdev->count; i++) {
151 sprintf(str, "cdev%d", i);
152 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
153 str);
154 if (rc) {
155 for (--i; i >= 0; i--) {
156 sprintf(str, "cdev%d", i);
157 sysfs_remove_link(&gdev->dev.kobj, str);
158 }
159 for (i = 0; i < gdev->count; i++)
160 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
161 "group_device");
162 return rc;
163 }
164 }
165 return 0;
166}
167
Ursula Braun022b6602008-04-24 10:15:20 +0200168static int __get_next_bus_id(const char **buf, char *bus_id)
169{
170 int rc, len;
171 char *start, *end;
172
173 start = (char *)*buf;
174 end = strchr(start, ',');
175 if (!end) {
176 /* Last entry. Strip trailing newline, if applicable. */
177 end = strchr(start, '\n');
178 if (end)
179 *end = '\0';
180 len = strlen(start) + 1;
181 } else {
182 len = end - start + 1;
183 end++;
184 }
Kay Sievers98df67b2008-12-25 13:38:55 +0100185 if (len < CCW_BUS_ID_SIZE) {
Ursula Braun022b6602008-04-24 10:15:20 +0200186 strlcpy(bus_id, start, len);
187 rc = 0;
188 } else
189 rc = -EINVAL;
190 *buf = end;
191 return rc;
192}
193
Kay Sievers98df67b2008-12-25 13:38:55 +0100194static int __is_valid_bus_id(char bus_id[CCW_BUS_ID_SIZE])
Ursula Braun022b6602008-04-24 10:15:20 +0200195{
196 int cssid, ssid, devno;
197
198 /* Must be of form %x.%x.%04x */
199 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
200 return 0;
201 return 1;
202}
203
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200204/**
Ursula Braun022b6602008-04-24 10:15:20 +0200205 * ccwgroup_create_from_string() - create and register a ccw group device
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200206 * @root: parent device for the new device
207 * @creator_id: identifier of creating driver
208 * @cdrv: ccw driver of slave devices
Ursula Braun022b6602008-04-24 10:15:20 +0200209 * @num_devices: number of slave devices
210 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200211 *
212 * Create and register a new ccw group device as a child of @root. Slave
Ursula Braun022b6602008-04-24 10:15:20 +0200213 * devices are obtained from the list of bus ids given in @buf and must all
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200214 * belong to @cdrv.
215 * Returns:
216 * %0 on success and an error code on failure.
217 * Context:
218 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
Ursula Braun022b6602008-04-24 10:15:20 +0200220int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
221 struct ccw_driver *cdrv, int num_devices,
222 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct ccwgroup_device *gdev;
Ursula Braun022b6602008-04-24 10:15:20 +0200225 int rc, i;
Kay Sievers98df67b2008-12-25 13:38:55 +0100226 char tmp_bus_id[CCW_BUS_ID_SIZE];
Ursula Braun022b6602008-04-24 10:15:20 +0200227 const char *curr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Ursula Braun022b6602008-04-24 10:15:20 +0200229 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
230 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!gdev)
232 return -ENOMEM;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200235 mutex_init(&gdev->reg_mutex);
236 mutex_lock(&gdev->reg_mutex);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200237 gdev->creator_id = creator_id;
238 gdev->count = num_devices;
239 gdev->dev.bus = &ccwgroup_bus_type;
240 gdev->dev.parent = root;
241 gdev->dev.release = ccwgroup_release;
242 device_initialize(&gdev->dev);
243
Ursula Braun022b6602008-04-24 10:15:20 +0200244 curr_buf = buf;
245 for (i = 0; i < num_devices && curr_buf; i++) {
246 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
247 if (rc != 0)
248 goto error;
249 if (!__is_valid_bus_id(tmp_bus_id)) {
250 rc = -EINVAL;
251 goto error;
252 }
253 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
254 /*
255 * All devices have to be of the same type in
256 * order to be grouped.
257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!gdev->cdev[i]
259 || gdev->cdev[i]->id.driver_info !=
260 gdev->cdev[0]->id.driver_info) {
261 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200262 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 /* Don't allow a device to belong to more than one group. */
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100265 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200267 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100269 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Cornelia Huck17088222006-07-27 14:00:33 +0200270 }
Ursula Braun022b6602008-04-24 10:15:20 +0200271 /* Check for sufficient number of bus ids. */
272 if (i < num_devices && !curr_buf) {
273 rc = -EINVAL;
274 goto error;
275 }
276 /* Check for trailing stuff. */
277 if (i == num_devices && strlen(curr_buf) > 0) {
278 rc = -EINVAL;
279 goto error;
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200282 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200284 rc = device_add(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200286 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 get_device(&gdev->dev);
288 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
289
290 if (rc) {
291 device_unregister(&gdev->dev);
292 goto error;
293 }
294
295 rc = __ccwgroup_create_symlinks(gdev);
296 if (!rc) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200297 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 put_device(&gdev->dev);
299 return 0;
300 }
301 device_remove_file(&gdev->dev, &dev_attr_ungroup);
302 device_unregister(&gdev->dev);
303error:
Ursula Braun022b6602008-04-24 10:15:20 +0200304 for (i = 0; i < num_devices; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (gdev->cdev[i]) {
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100306 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
307 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Cornelia Huck17088222006-07-27 14:00:33 +0200308 put_device(&gdev->cdev[i]->dev);
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700309 gdev->cdev[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200311 mutex_unlock(&gdev->reg_mutex);
312 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return rc;
314}
Ursula Braun022b6602008-04-24 10:15:20 +0200315EXPORT_SYMBOL(ccwgroup_create_from_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Sebastian Otte9090742009-03-26 15:24:15 +0100317static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
318 void *data);
319
320static struct notifier_block ccwgroup_nb = {
321 .notifier_call = ccwgroup_notifier
322};
323
324static int __init init_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Sebastian Otte9090742009-03-26 15:24:15 +0100326 int ret;
327
328 ret = bus_register(&ccwgroup_bus_type);
329 if (ret)
330 return ret;
331
332 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
333 if (ret)
334 bus_unregister(&ccwgroup_bus_type);
335
336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Sebastian Otte9090742009-03-26 15:24:15 +0100339static void __exit cleanup_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Sebastian Otte9090742009-03-26 15:24:15 +0100341 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
342 bus_unregister(&ccwgroup_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345module_init(init_ccwgroup);
346module_exit(cleanup_ccwgroup);
347
348/************************** driver stuff ******************************/
349
350static int
351ccwgroup_set_online(struct ccwgroup_device *gdev)
352{
353 struct ccwgroup_driver *gdrv;
354 int ret;
355
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800356 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EAGAIN;
358 if (gdev->state == CCWGROUP_ONLINE) {
359 ret = 0;
360 goto out;
361 }
362 if (!gdev->dev.driver) {
363 ret = -EINVAL;
364 goto out;
365 }
366 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800367 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto out;
369
370 gdev->state = CCWGROUP_ONLINE;
371 out:
372 atomic_set(&gdev->onoff, 0);
373 return ret;
374}
375
376static int
377ccwgroup_set_offline(struct ccwgroup_device *gdev)
378{
379 struct ccwgroup_driver *gdrv;
380 int ret;
381
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800382 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -EAGAIN;
384 if (gdev->state == CCWGROUP_OFFLINE) {
385 ret = 0;
386 goto out;
387 }
388 if (!gdev->dev.driver) {
389 ret = -EINVAL;
390 goto out;
391 }
392 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800393 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto out;
395
396 gdev->state = CCWGROUP_OFFLINE;
397 out:
398 atomic_set(&gdev->onoff, 0);
399 return ret;
400}
401
402static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400403ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 struct ccwgroup_device *gdev;
406 struct ccwgroup_driver *gdrv;
Cornelia Huck2f972202008-04-30 13:38:33 +0200407 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int ret;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!dev->driver)
Sebastian Ott40c9f992009-03-26 15:24:13 +0100411 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Sebastian Ott40c9f992009-03-26 15:24:13 +0100413 gdev = to_ccwgroupdev(dev);
414 gdrv = to_ccwgroupdrv(dev->driver);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!try_module_get(gdrv->owner))
417 return -EINVAL;
418
Cornelia Huck2f972202008-04-30 13:38:33 +0200419 ret = strict_strtoul(buf, 0, &value);
420 if (ret)
421 goto out;
Sebastian Ott40c9f992009-03-26 15:24:13 +0100422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (value == 1)
Sebastian Ott40c9f992009-03-26 15:24:13 +0100424 ret = ccwgroup_set_online(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 else if (value == 0)
Sebastian Ott40c9f992009-03-26 15:24:13 +0100426 ret = ccwgroup_set_offline(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 else
428 ret = -EINVAL;
Cornelia Huck2f972202008-04-30 13:38:33 +0200429out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 module_put(gdrv->owner);
Sebastian Ott40c9f992009-03-26 15:24:13 +0100431 return (ret == 0) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400435ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 int online;
438
439 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
440
441 return sprintf(buf, online ? "1\n" : "0\n");
442}
443
444static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
445
446static int
447ccwgroup_probe (struct device *dev)
448{
449 struct ccwgroup_device *gdev;
450 struct ccwgroup_driver *gdrv;
451
452 int ret;
453
454 gdev = to_ccwgroupdev(dev);
455 gdrv = to_ccwgroupdrv(dev->driver);
456
457 if ((ret = device_create_file(dev, &dev_attr_online)))
458 return ret;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
461 if (ret)
462 device_remove_file(dev, &dev_attr_online);
463
464 return ret;
465}
466
467static int
468ccwgroup_remove (struct device *dev)
469{
470 struct ccwgroup_device *gdev;
471 struct ccwgroup_driver *gdrv;
472
Sebastian Ott50f15482009-03-26 15:24:14 +0100473 device_remove_file(dev, &dev_attr_online);
Sebastian Otte9090742009-03-26 15:24:15 +0100474 device_remove_file(dev, &dev_attr_ungroup);
Sebastian Ott50f15482009-03-26 15:24:14 +0100475
476 if (!dev->driver)
477 return 0;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 gdev = to_ccwgroupdev(dev);
480 gdrv = to_ccwgroupdrv(dev->driver);
481
Sebastian Ott50f15482009-03-26 15:24:14 +0100482 if (gdrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 gdrv->remove(gdev);
Sebastian Ott50f15482009-03-26 15:24:14 +0100484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486}
487
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100488static void ccwgroup_shutdown(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;
495
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100496 gdev = to_ccwgroupdev(dev);
497 gdrv = to_ccwgroupdrv(dev->driver);
Sebastian Ott50f15482009-03-26 15:24:14 +0100498
499 if (gdrv->shutdown)
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100500 gdrv->shutdown(gdev);
501}
502
Russell Kingf9ccf452006-01-05 14:42:09 +0000503static struct bus_type ccwgroup_bus_type = {
504 .name = "ccwgroup",
505 .match = ccwgroup_bus_match,
506 .uevent = ccwgroup_uevent,
507 .probe = ccwgroup_probe,
508 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100509 .shutdown = ccwgroup_shutdown,
Russell Kingf9ccf452006-01-05 14:42:09 +0000510};
511
Sebastian Otte9090742009-03-26 15:24:15 +0100512
513static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
514 void *data)
515{
516 struct device *dev = data;
517
518 if (action == BUS_NOTIFY_UNBIND_DRIVER)
519 device_schedule_callback(dev, ccwgroup_ungroup_callback);
520
521 return NOTIFY_OK;
522}
523
524
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200525/**
526 * ccwgroup_driver_register() - register a ccw group driver
527 * @cdriver: driver to be registered
528 *
529 * This function is mainly a wrapper around driver_register().
530 */
531int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200534 cdriver->driver.bus = &ccwgroup_bus_type;
535 cdriver->driver.name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +0100536 cdriver->driver.owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 return driver_register(&cdriver->driver);
539}
540
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700541static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200542__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Cornelia Huck887ab592006-06-29 14:56:52 +0200544 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200547/**
548 * ccwgroup_driver_unregister() - deregister a ccw group driver
549 * @cdriver: driver to be deregistered
550 *
551 * This function is mainly a wrapper around driver_unregister().
552 */
553void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Cornelia Huck887ab592006-06-29 14:56:52 +0200555 struct device *dev;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* We don't want ccwgroup devices to live longer than their driver. */
558 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200559 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
560 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200561 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
562
563 mutex_lock(&gdev->reg_mutex);
564 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200565 device_unregister(dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200566 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200567 put_device(dev);
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 put_driver(&cdriver->driver);
570 driver_unregister(&cdriver->driver);
571}
572
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200573/**
574 * ccwgroup_probe_ccwdev() - probe function for slave devices
575 * @cdev: ccw device to be probed
576 *
577 * This is a dummy probe function for ccw devices that are slave devices in
578 * a ccw group device.
579 * Returns:
580 * always %0
581 */
582int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 return 0;
585}
586
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100587static struct ccwgroup_device *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
589{
590 struct ccwgroup_device *gdev;
591
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100592 gdev = dev_get_drvdata(&cdev->dev);
593 if (gdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (get_device(&gdev->dev)) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200595 mutex_lock(&gdev->reg_mutex);
Daniel Ritzd305ef52005-09-22 00:47:24 -0700596 if (device_is_registered(&gdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return gdev;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200598 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 put_device(&gdev->dev);
600 }
601 return NULL;
602 }
603 return NULL;
604}
605
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200606/**
607 * ccwgroup_remove_ccwdev() - remove function for slave devices
608 * @cdev: ccw device to be removed
609 *
610 * This is a remove function for ccw devices that are slave devices in a ccw
611 * group device. It sets the ccw device offline and also deregisters the
612 * embedding ccw group device.
613 */
614void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct ccwgroup_device *gdev;
617
618 /* Ignore offlining errors, device is gone anyway. */
619 ccw_device_set_offline(cdev);
620 /* If one of its devices is gone, the whole group is done for. */
621 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
622 if (gdev) {
623 __ccwgroup_remove_symlinks(gdev);
624 device_unregister(&gdev->dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200625 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 put_device(&gdev->dev);
627 }
628}
629
630MODULE_LICENSE("GPL");
631EXPORT_SYMBOL(ccwgroup_driver_register);
632EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
634EXPORT_SYMBOL(ccwgroup_remove_ccwdev);