ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / s390 / cio / ccwgroup.c
blobcda9bd6e48e8d19743290a9fec017194a116655b
1 /*
2 * bus driver for ccwgroup
4 * Copyright IBM Corp. 2002, 2009
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
8 */
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>
18 #include <asm/ccwdev.h>
19 #include <asm/ccwgroup.h>
21 #define CCW_BUS_ID_SIZE 20
23 /* 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.
30 /* a device matches a driver if all its slave devices match the same
31 * entry of the driver */
32 static int
33 ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
35 struct ccwgroup_device *gdev;
36 struct ccwgroup_driver *gdrv;
38 gdev = to_ccwgroupdev(dev);
39 gdrv = to_ccwgroupdrv(drv);
41 if (gdev->creator_id == gdrv->driver_id)
42 return 1;
44 return 0;
46 static int
47 ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
49 /* TODO */
50 return 0;
53 static struct bus_type ccwgroup_bus_type;
55 static void
56 __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
58 int i;
59 char str[8];
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");
70 * Remove references from ccw devices to ccw group device and from
71 * ccw group device to ccw devices.
73 static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
75 struct ccw_device *cdev;
76 int i;
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);
90 static ssize_t ccwgroup_online_store(struct device *dev,
91 struct device_attribute *attr,
92 const char *buf, size_t count);
93 static ssize_t ccwgroup_online_show(struct device *dev,
94 struct device_attribute *attr,
95 char *buf);
97 * Provide an 'ungroup' attribute so the user can remove group devices no
98 * longer needed or accidentially created. Saves memory :)
100 static void ccwgroup_ungroup_callback(struct device *dev)
102 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
104 mutex_lock(&gdev->reg_mutex);
105 if (device_is_registered(&gdev->dev)) {
106 __ccwgroup_remove_symlinks(gdev);
107 device_unregister(dev);
108 __ccwgroup_remove_cdev_refs(gdev);
110 mutex_unlock(&gdev->reg_mutex);
113 static ssize_t
114 ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
116 struct ccwgroup_device *gdev;
117 int rc;
119 gdev = to_ccwgroupdev(dev);
121 /* 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;
128 /* Note that we cannot unregister the device from one of its
129 * attribute methods, so we have to use this roundabout approach.
131 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
132 out:
133 if (rc) {
134 if (rc != -EAGAIN)
135 /* Release onoff "lock" when ungrouping failed. */
136 atomic_set(&gdev->onoff, 0);
137 return rc;
139 return count;
142 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
143 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
145 static struct attribute *ccwgroup_attrs[] = {
146 &dev_attr_online.attr,
147 &dev_attr_ungroup.attr,
148 NULL,
150 static struct attribute_group ccwgroup_attr_group = {
151 .attrs = ccwgroup_attrs,
153 static const struct attribute_group *ccwgroup_attr_groups[] = {
154 &ccwgroup_attr_group,
155 NULL,
158 static void
159 ccwgroup_release (struct device *dev)
161 kfree(to_ccwgroupdev(dev));
164 static int
165 __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
167 char str[8];
168 int i, rc;
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;
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);
189 for (i = 0; i < gdev->count; i++)
190 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
191 "group_device");
192 return rc;
195 return 0;
198 static int __get_next_bus_id(const char **buf, char *bus_id)
200 int rc, len;
201 char *start, *end;
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++;
215 if (len < CCW_BUS_ID_SIZE) {
216 strlcpy(bus_id, start, len);
217 rc = 0;
218 } else
219 rc = -EINVAL;
220 *buf = end;
221 return rc;
224 static int __is_valid_bus_id(char bus_id[CCW_BUS_ID_SIZE])
226 int cssid, ssid, devno;
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;
235 * ccwgroup_create_from_string() - create and register a ccw group device
236 * @root: parent device for the new device
237 * @creator_id: identifier of creating driver
238 * @cdrv: ccw driver of slave devices
239 * @num_devices: number of slave devices
240 * @buf: buffer containing comma separated bus ids of slave devices
242 * Create and register a new ccw group device as a child of @root. Slave
243 * devices are obtained from the list of bus ids given in @buf and must all
244 * belong to @cdrv.
245 * Returns:
246 * %0 on success and an error code on failure.
247 * Context:
248 * non-atomic
250 int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
251 struct ccw_driver *cdrv, int num_devices,
252 const char *buf)
254 struct ccwgroup_device *gdev;
255 int rc, i;
256 char tmp_bus_id[CCW_BUS_ID_SIZE];
257 const char *curr_buf;
259 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
260 GFP_KERNEL);
261 if (!gdev)
262 return -ENOMEM;
264 atomic_set(&gdev->onoff, 0);
265 mutex_init(&gdev->reg_mutex);
266 mutex_lock(&gdev->reg_mutex);
267 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);
274 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;
283 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
285 * All devices have to be of the same type in
286 * order to be grouped.
288 if (!gdev->cdev[i]
289 || gdev->cdev[i]->id.driver_info !=
290 gdev->cdev[0]->id.driver_info) {
291 rc = -EINVAL;
292 goto error;
294 /* Don't allow a device to belong to more than one group. */
295 spin_lock_irq(gdev->cdev[i]->ccwlock);
296 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
297 spin_unlock_irq(gdev->cdev[i]->ccwlock);
298 rc = -EINVAL;
299 goto error;
301 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
302 spin_unlock_irq(gdev->cdev[i]->ccwlock);
304 /* Check for sufficient number of bus ids. */
305 if (i < num_devices && !curr_buf) {
306 rc = -EINVAL;
307 goto error;
309 /* Check for trailing stuff. */
310 if (i == num_devices && strlen(curr_buf) > 0) {
311 rc = -EINVAL;
312 goto error;
315 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
316 gdev->dev.groups = ccwgroup_attr_groups;
317 rc = device_add(&gdev->dev);
318 if (rc)
319 goto error;
320 get_device(&gdev->dev);
321 rc = __ccwgroup_create_symlinks(gdev);
322 if (!rc) {
323 mutex_unlock(&gdev->reg_mutex);
324 put_device(&gdev->dev);
325 return 0;
327 device_unregister(&gdev->dev);
328 error:
329 for (i = 0; i < num_devices; i++)
330 if (gdev->cdev[i]) {
331 spin_lock_irq(gdev->cdev[i]->ccwlock);
332 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
333 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
334 spin_unlock_irq(gdev->cdev[i]->ccwlock);
335 put_device(&gdev->cdev[i]->dev);
336 gdev->cdev[i] = NULL;
338 mutex_unlock(&gdev->reg_mutex);
339 put_device(&gdev->dev);
340 return rc;
342 EXPORT_SYMBOL(ccwgroup_create_from_string);
344 static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
345 void *data);
347 static struct notifier_block ccwgroup_nb = {
348 .notifier_call = ccwgroup_notifier
351 static int __init init_ccwgroup(void)
353 int ret;
355 ret = bus_register(&ccwgroup_bus_type);
356 if (ret)
357 return ret;
359 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
360 if (ret)
361 bus_unregister(&ccwgroup_bus_type);
363 return ret;
366 static void __exit cleanup_ccwgroup(void)
368 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
369 bus_unregister(&ccwgroup_bus_type);
372 module_init(init_ccwgroup);
373 module_exit(cleanup_ccwgroup);
375 /************************** driver stuff ******************************/
377 static int
378 ccwgroup_set_online(struct ccwgroup_device *gdev)
380 struct ccwgroup_driver *gdrv;
381 int ret;
383 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
384 return -EAGAIN;
385 if (gdev->state == CCWGROUP_ONLINE) {
386 ret = 0;
387 goto out;
389 if (!gdev->dev.driver) {
390 ret = -EINVAL;
391 goto out;
393 gdrv = to_ccwgroupdrv (gdev->dev.driver);
394 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
395 goto out;
397 gdev->state = CCWGROUP_ONLINE;
398 out:
399 atomic_set(&gdev->onoff, 0);
400 return ret;
403 static int
404 ccwgroup_set_offline(struct ccwgroup_device *gdev)
406 struct ccwgroup_driver *gdrv;
407 int ret;
409 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
410 return -EAGAIN;
411 if (gdev->state == CCWGROUP_OFFLINE) {
412 ret = 0;
413 goto out;
415 if (!gdev->dev.driver) {
416 ret = -EINVAL;
417 goto out;
419 gdrv = to_ccwgroupdrv (gdev->dev.driver);
420 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
421 goto out;
423 gdev->state = CCWGROUP_OFFLINE;
424 out:
425 atomic_set(&gdev->onoff, 0);
426 return ret;
429 static ssize_t
430 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
432 struct ccwgroup_device *gdev;
433 struct ccwgroup_driver *gdrv;
434 unsigned long value;
435 int ret;
437 if (!dev->driver)
438 return -EINVAL;
440 gdev = to_ccwgroupdev(dev);
441 gdrv = to_ccwgroupdrv(dev->driver);
443 if (!try_module_get(gdrv->driver.owner))
444 return -EINVAL;
446 ret = strict_strtoul(buf, 0, &value);
447 if (ret)
448 goto out;
450 if (value == 1)
451 ret = ccwgroup_set_online(gdev);
452 else if (value == 0)
453 ret = ccwgroup_set_offline(gdev);
454 else
455 ret = -EINVAL;
456 out:
457 module_put(gdrv->driver.owner);
458 return (ret == 0) ? count : ret;
461 static ssize_t
462 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
464 int online;
466 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
468 return sprintf(buf, online ? "1\n" : "0\n");
471 static int
472 ccwgroup_probe (struct device *dev)
474 struct ccwgroup_device *gdev;
475 struct ccwgroup_driver *gdrv;
477 int ret;
479 gdev = to_ccwgroupdev(dev);
480 gdrv = to_ccwgroupdrv(dev->driver);
482 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
484 return ret;
487 static int
488 ccwgroup_remove (struct device *dev)
490 struct ccwgroup_device *gdev;
491 struct ccwgroup_driver *gdrv;
493 if (!dev->driver)
494 return 0;
496 gdev = to_ccwgroupdev(dev);
497 gdrv = to_ccwgroupdrv(dev->driver);
499 if (gdrv->remove)
500 gdrv->remove(gdev);
502 return 0;
505 static void ccwgroup_shutdown(struct device *dev)
507 struct ccwgroup_device *gdev;
508 struct ccwgroup_driver *gdrv;
510 if (!dev->driver)
511 return;
513 gdev = to_ccwgroupdev(dev);
514 gdrv = to_ccwgroupdrv(dev->driver);
516 if (gdrv->shutdown)
517 gdrv->shutdown(gdev);
520 static int ccwgroup_pm_prepare(struct device *dev)
522 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
523 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
525 /* Fail while device is being set online/offline. */
526 if (atomic_read(&gdev->onoff))
527 return -EAGAIN;
529 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
530 return 0;
532 return gdrv->prepare ? gdrv->prepare(gdev) : 0;
535 static void ccwgroup_pm_complete(struct device *dev)
537 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
538 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
540 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
541 return;
543 if (gdrv->complete)
544 gdrv->complete(gdev);
547 static int ccwgroup_pm_freeze(struct device *dev)
549 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
550 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
552 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
553 return 0;
555 return gdrv->freeze ? gdrv->freeze(gdev) : 0;
558 static int ccwgroup_pm_thaw(struct device *dev)
560 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
561 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
563 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
564 return 0;
566 return gdrv->thaw ? gdrv->thaw(gdev) : 0;
569 static int ccwgroup_pm_restore(struct device *dev)
571 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
572 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
574 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
575 return 0;
577 return gdrv->restore ? gdrv->restore(gdev) : 0;
580 static const struct dev_pm_ops ccwgroup_pm_ops = {
581 .prepare = ccwgroup_pm_prepare,
582 .complete = ccwgroup_pm_complete,
583 .freeze = ccwgroup_pm_freeze,
584 .thaw = ccwgroup_pm_thaw,
585 .restore = ccwgroup_pm_restore,
588 static 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,
594 .shutdown = ccwgroup_shutdown,
595 .pm = &ccwgroup_pm_ops,
599 static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
600 void *data)
602 struct device *dev = data;
604 if (action == BUS_NOTIFY_UNBIND_DRIVER)
605 device_schedule_callback(dev, ccwgroup_ungroup_callback);
607 return NOTIFY_OK;
612 * ccwgroup_driver_register() - register a ccw group driver
613 * @cdriver: driver to be registered
615 * This function is mainly a wrapper around driver_register().
617 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
619 /* register our new driver with the core */
620 cdriver->driver.bus = &ccwgroup_bus_type;
622 return driver_register(&cdriver->driver);
625 static int
626 __ccwgroup_match_all(struct device *dev, void *data)
628 return 1;
632 * ccwgroup_driver_unregister() - deregister a ccw group driver
633 * @cdriver: driver to be deregistered
635 * This function is mainly a wrapper around driver_unregister().
637 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
639 struct device *dev;
641 /* We don't want ccwgroup devices to live longer than their driver. */
642 get_driver(&cdriver->driver);
643 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
644 __ccwgroup_match_all))) {
645 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
647 mutex_lock(&gdev->reg_mutex);
648 __ccwgroup_remove_symlinks(gdev);
649 device_unregister(dev);
650 __ccwgroup_remove_cdev_refs(gdev);
651 mutex_unlock(&gdev->reg_mutex);
652 put_device(dev);
654 put_driver(&cdriver->driver);
655 driver_unregister(&cdriver->driver);
659 * ccwgroup_probe_ccwdev() - probe function for slave devices
660 * @cdev: ccw device to be probed
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
667 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
669 return 0;
673 * ccwgroup_remove_ccwdev() - remove function for slave devices
674 * @cdev: ccw device to be removed
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.
680 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
682 struct ccwgroup_device *gdev;
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. */
687 spin_lock_irq(cdev->ccwlock);
688 gdev = dev_get_drvdata(&cdev->dev);
689 if (!gdev) {
690 spin_unlock_irq(cdev->ccwlock);
691 return;
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)) {
699 __ccwgroup_remove_symlinks(gdev);
700 device_unregister(&gdev->dev);
701 __ccwgroup_remove_cdev_refs(gdev);
703 mutex_unlock(&gdev->reg_mutex);
704 /* Release ccwgroup device reference for local processing. */
705 put_device(&gdev->dev);
708 MODULE_LICENSE("GPL");
709 EXPORT_SYMBOL(ccwgroup_driver_register);
710 EXPORT_SYMBOL(ccwgroup_driver_unregister);
711 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
712 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);