Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / s390 / cio / ccwgroup.c
blob731470e68493b14e8d09830b4400e1da01dde5a2
1 /*
2 * bus driver for ccwgroup
4 * Copyright IBM Corp. 2002, 2012
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/cio.h>
19 #include <asm/ccwdev.h>
20 #include <asm/ccwgroup.h>
22 #include "device.h"
24 #define CCW_BUS_ID_SIZE 10
26 /* In Linux 2.4, we had a channel device layer called "chandev"
27 * that did all sorts of obscure stuff for networking devices.
28 * This is another driver that serves as a replacement for just
29 * one of its functions, namely the translation of single subchannels
30 * to devices that use multiple subchannels.
33 static struct bus_type ccwgroup_bus_type;
35 static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
37 int i;
38 char str[8];
40 for (i = 0; i < gdev->count; i++) {
41 sprintf(str, "cdev%d", i);
42 sysfs_remove_link(&gdev->dev.kobj, str);
43 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
48 * Remove references from ccw devices to ccw group device and from
49 * ccw group device to ccw devices.
51 static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
53 struct ccw_device *cdev;
54 int i;
56 for (i = 0; i < gdev->count; i++) {
57 cdev = gdev->cdev[i];
58 if (!cdev)
59 continue;
60 spin_lock_irq(cdev->ccwlock);
61 dev_set_drvdata(&cdev->dev, NULL);
62 spin_unlock_irq(cdev->ccwlock);
63 gdev->cdev[i] = NULL;
64 put_device(&cdev->dev);
68 static int ccwgroup_set_online(struct ccwgroup_device *gdev)
70 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
71 int ret = 0;
73 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
74 return -EAGAIN;
75 if (gdev->state == CCWGROUP_ONLINE)
76 goto out;
77 if (gdrv->set_online)
78 ret = gdrv->set_online(gdev);
79 if (ret)
80 goto out;
82 gdev->state = CCWGROUP_ONLINE;
83 out:
84 atomic_set(&gdev->onoff, 0);
85 return ret;
88 static int ccwgroup_set_offline(struct ccwgroup_device *gdev)
90 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
91 int ret = 0;
93 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
94 return -EAGAIN;
95 if (gdev->state == CCWGROUP_OFFLINE)
96 goto out;
97 if (gdrv->set_offline)
98 ret = gdrv->set_offline(gdev);
99 if (ret)
100 goto out;
102 gdev->state = CCWGROUP_OFFLINE;
103 out:
104 atomic_set(&gdev->onoff, 0);
105 return ret;
108 static ssize_t ccwgroup_online_store(struct device *dev,
109 struct device_attribute *attr,
110 const char *buf, size_t count)
112 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
113 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
114 unsigned long value;
115 int ret;
117 if (!dev->driver)
118 return -EINVAL;
119 if (!try_module_get(gdrv->driver.owner))
120 return -EINVAL;
122 ret = strict_strtoul(buf, 0, &value);
123 if (ret)
124 goto out;
126 if (value == 1)
127 ret = ccwgroup_set_online(gdev);
128 else if (value == 0)
129 ret = ccwgroup_set_offline(gdev);
130 else
131 ret = -EINVAL;
132 out:
133 module_put(gdrv->driver.owner);
134 return (ret == 0) ? count : ret;
137 static ssize_t ccwgroup_online_show(struct device *dev,
138 struct device_attribute *attr,
139 char *buf)
141 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
142 int online;
144 online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
146 return scnprintf(buf, PAGE_SIZE, "%d\n", online);
150 * Provide an 'ungroup' attribute so the user can remove group devices no
151 * longer needed or accidentially created. Saves memory :)
153 static void ccwgroup_ungroup_callback(struct device *dev)
155 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
157 mutex_lock(&gdev->reg_mutex);
158 if (device_is_registered(&gdev->dev)) {
159 __ccwgroup_remove_symlinks(gdev);
160 device_unregister(dev);
161 __ccwgroup_remove_cdev_refs(gdev);
163 mutex_unlock(&gdev->reg_mutex);
166 static ssize_t ccwgroup_ungroup_store(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
170 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
171 int rc;
173 /* Prevent concurrent online/offline processing and ungrouping. */
174 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
175 return -EAGAIN;
176 if (gdev->state != CCWGROUP_OFFLINE) {
177 rc = -EINVAL;
178 goto out;
180 /* Note that we cannot unregister the device from one of its
181 * attribute methods, so we have to use this roundabout approach.
183 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
184 out:
185 if (rc) {
186 if (rc != -EAGAIN)
187 /* Release onoff "lock" when ungrouping failed. */
188 atomic_set(&gdev->onoff, 0);
189 return rc;
191 return count;
193 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
194 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
196 static struct attribute *ccwgroup_attrs[] = {
197 &dev_attr_online.attr,
198 &dev_attr_ungroup.attr,
199 NULL,
201 static struct attribute_group ccwgroup_attr_group = {
202 .attrs = ccwgroup_attrs,
204 static const struct attribute_group *ccwgroup_attr_groups[] = {
205 &ccwgroup_attr_group,
206 NULL,
209 static void ccwgroup_release(struct device *dev)
211 kfree(to_ccwgroupdev(dev));
214 static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
216 char str[8];
217 int i, rc;
219 for (i = 0; i < gdev->count; i++) {
220 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
221 &gdev->dev.kobj, "group_device");
222 if (rc) {
223 for (--i; i >= 0; i--)
224 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
225 "group_device");
226 return rc;
229 for (i = 0; i < gdev->count; i++) {
230 sprintf(str, "cdev%d", i);
231 rc = sysfs_create_link(&gdev->dev.kobj,
232 &gdev->cdev[i]->dev.kobj, str);
233 if (rc) {
234 for (--i; i >= 0; i--) {
235 sprintf(str, "cdev%d", i);
236 sysfs_remove_link(&gdev->dev.kobj, str);
238 for (i = 0; i < gdev->count; i++)
239 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
240 "group_device");
241 return rc;
244 return 0;
247 static int __get_next_id(const char **buf, struct ccw_dev_id *id)
249 unsigned int cssid, ssid, devno;
250 int ret = 0, len;
251 char *start, *end;
253 start = (char *)*buf;
254 end = strchr(start, ',');
255 if (!end) {
256 /* Last entry. Strip trailing newline, if applicable. */
257 end = strchr(start, '\n');
258 if (end)
259 *end = '\0';
260 len = strlen(start) + 1;
261 } else {
262 len = end - start + 1;
263 end++;
265 if (len <= CCW_BUS_ID_SIZE) {
266 if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
267 ret = -EINVAL;
268 } else
269 ret = -EINVAL;
271 if (!ret) {
272 id->ssid = ssid;
273 id->devno = devno;
275 *buf = end;
276 return ret;
280 * ccwgroup_create_dev() - create and register a ccw group device
281 * @parent: parent device for the new device
282 * @gdrv: driver for the new group device
283 * @num_devices: number of slave devices
284 * @buf: buffer containing comma separated bus ids of slave devices
286 * Create and register a new ccw group device as a child of @parent. Slave
287 * devices are obtained from the list of bus ids given in @buf.
288 * Returns:
289 * %0 on success and an error code on failure.
290 * Context:
291 * non-atomic
293 int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
294 int num_devices, const char *buf)
296 struct ccwgroup_device *gdev;
297 struct ccw_dev_id dev_id;
298 int rc, i;
300 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
301 GFP_KERNEL);
302 if (!gdev)
303 return -ENOMEM;
305 atomic_set(&gdev->onoff, 0);
306 mutex_init(&gdev->reg_mutex);
307 mutex_lock(&gdev->reg_mutex);
308 gdev->count = num_devices;
309 gdev->dev.bus = &ccwgroup_bus_type;
310 gdev->dev.parent = parent;
311 gdev->dev.release = ccwgroup_release;
312 device_initialize(&gdev->dev);
314 for (i = 0; i < num_devices && buf; i++) {
315 rc = __get_next_id(&buf, &dev_id);
316 if (rc != 0)
317 goto error;
318 gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
320 * All devices have to be of the same type in
321 * order to be grouped.
323 if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
324 gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
325 gdev->cdev[i]->id.driver_info !=
326 gdev->cdev[0]->id.driver_info) {
327 rc = -EINVAL;
328 goto error;
330 /* Don't allow a device to belong to more than one group. */
331 spin_lock_irq(gdev->cdev[i]->ccwlock);
332 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
333 spin_unlock_irq(gdev->cdev[i]->ccwlock);
334 rc = -EINVAL;
335 goto error;
337 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
338 spin_unlock_irq(gdev->cdev[i]->ccwlock);
340 /* Check for sufficient number of bus ids. */
341 if (i < num_devices) {
342 rc = -EINVAL;
343 goto error;
345 /* Check for trailing stuff. */
346 if (i == num_devices && strlen(buf) > 0) {
347 rc = -EINVAL;
348 goto error;
351 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
352 gdev->dev.groups = ccwgroup_attr_groups;
354 if (gdrv) {
355 gdev->dev.driver = &gdrv->driver;
356 rc = gdrv->setup ? gdrv->setup(gdev) : 0;
357 if (rc)
358 goto error;
360 rc = device_add(&gdev->dev);
361 if (rc)
362 goto error;
363 rc = __ccwgroup_create_symlinks(gdev);
364 if (rc) {
365 device_del(&gdev->dev);
366 goto error;
368 mutex_unlock(&gdev->reg_mutex);
369 return 0;
370 error:
371 for (i = 0; i < num_devices; i++)
372 if (gdev->cdev[i]) {
373 spin_lock_irq(gdev->cdev[i]->ccwlock);
374 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
375 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
376 spin_unlock_irq(gdev->cdev[i]->ccwlock);
377 put_device(&gdev->cdev[i]->dev);
378 gdev->cdev[i] = NULL;
380 mutex_unlock(&gdev->reg_mutex);
381 put_device(&gdev->dev);
382 return rc;
384 EXPORT_SYMBOL(ccwgroup_create_dev);
386 static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
387 void *data)
389 struct device *dev = data;
391 if (action == BUS_NOTIFY_UNBIND_DRIVER)
392 device_schedule_callback(dev, ccwgroup_ungroup_callback);
394 return NOTIFY_OK;
397 static struct notifier_block ccwgroup_nb = {
398 .notifier_call = ccwgroup_notifier
401 static int __init init_ccwgroup(void)
403 int ret;
405 ret = bus_register(&ccwgroup_bus_type);
406 if (ret)
407 return ret;
409 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
410 if (ret)
411 bus_unregister(&ccwgroup_bus_type);
413 return ret;
416 static void __exit cleanup_ccwgroup(void)
418 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
419 bus_unregister(&ccwgroup_bus_type);
422 module_init(init_ccwgroup);
423 module_exit(cleanup_ccwgroup);
425 /************************** driver stuff ******************************/
427 static int ccwgroup_remove(struct device *dev)
429 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
430 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
432 if (!dev->driver)
433 return 0;
434 if (gdrv->remove)
435 gdrv->remove(gdev);
437 return 0;
440 static void ccwgroup_shutdown(struct device *dev)
442 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
443 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
445 if (!dev->driver)
446 return;
447 if (gdrv->shutdown)
448 gdrv->shutdown(gdev);
451 static int ccwgroup_pm_prepare(struct device *dev)
453 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
454 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
456 /* Fail while device is being set online/offline. */
457 if (atomic_read(&gdev->onoff))
458 return -EAGAIN;
460 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
461 return 0;
463 return gdrv->prepare ? gdrv->prepare(gdev) : 0;
466 static void ccwgroup_pm_complete(struct device *dev)
468 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
469 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
471 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
472 return;
474 if (gdrv->complete)
475 gdrv->complete(gdev);
478 static int ccwgroup_pm_freeze(struct device *dev)
480 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
481 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
483 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
484 return 0;
486 return gdrv->freeze ? gdrv->freeze(gdev) : 0;
489 static int ccwgroup_pm_thaw(struct device *dev)
491 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
492 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
494 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
495 return 0;
497 return gdrv->thaw ? gdrv->thaw(gdev) : 0;
500 static int ccwgroup_pm_restore(struct device *dev)
502 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
503 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
505 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
506 return 0;
508 return gdrv->restore ? gdrv->restore(gdev) : 0;
511 static const struct dev_pm_ops ccwgroup_pm_ops = {
512 .prepare = ccwgroup_pm_prepare,
513 .complete = ccwgroup_pm_complete,
514 .freeze = ccwgroup_pm_freeze,
515 .thaw = ccwgroup_pm_thaw,
516 .restore = ccwgroup_pm_restore,
519 static struct bus_type ccwgroup_bus_type = {
520 .name = "ccwgroup",
521 .remove = ccwgroup_remove,
522 .shutdown = ccwgroup_shutdown,
523 .pm = &ccwgroup_pm_ops,
527 * ccwgroup_driver_register() - register a ccw group driver
528 * @cdriver: driver to be registered
530 * This function is mainly a wrapper around driver_register().
532 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
534 /* register our new driver with the core */
535 cdriver->driver.bus = &ccwgroup_bus_type;
537 return driver_register(&cdriver->driver);
539 EXPORT_SYMBOL(ccwgroup_driver_register);
541 static int __ccwgroup_match_all(struct device *dev, void *data)
543 return 1;
547 * ccwgroup_driver_unregister() - deregister a ccw group driver
548 * @cdriver: driver to be deregistered
550 * This function is mainly a wrapper around driver_unregister().
552 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
554 struct device *dev;
556 /* We don't want ccwgroup devices to live longer than their driver. */
557 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
558 __ccwgroup_match_all))) {
559 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
561 mutex_lock(&gdev->reg_mutex);
562 __ccwgroup_remove_symlinks(gdev);
563 device_unregister(dev);
564 __ccwgroup_remove_cdev_refs(gdev);
565 mutex_unlock(&gdev->reg_mutex);
566 put_device(dev);
568 driver_unregister(&cdriver->driver);
570 EXPORT_SYMBOL(ccwgroup_driver_unregister);
573 * ccwgroup_probe_ccwdev() - probe function for slave devices
574 * @cdev: ccw device to be probed
576 * This is a dummy probe function for ccw devices that are slave devices in
577 * a ccw group device.
578 * Returns:
579 * always %0
581 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
583 return 0;
585 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
588 * ccwgroup_remove_ccwdev() - remove function for slave devices
589 * @cdev: ccw device to be removed
591 * This is a remove function for ccw devices that are slave devices in a ccw
592 * group device. It sets the ccw device offline and also deregisters the
593 * embedding ccw group device.
595 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
597 struct ccwgroup_device *gdev;
599 /* Ignore offlining errors, device is gone anyway. */
600 ccw_device_set_offline(cdev);
601 /* If one of its devices is gone, the whole group is done for. */
602 spin_lock_irq(cdev->ccwlock);
603 gdev = dev_get_drvdata(&cdev->dev);
604 if (!gdev) {
605 spin_unlock_irq(cdev->ccwlock);
606 return;
608 /* Get ccwgroup device reference for local processing. */
609 get_device(&gdev->dev);
610 spin_unlock_irq(cdev->ccwlock);
611 /* Unregister group device. */
612 mutex_lock(&gdev->reg_mutex);
613 if (device_is_registered(&gdev->dev)) {
614 __ccwgroup_remove_symlinks(gdev);
615 device_unregister(&gdev->dev);
616 __ccwgroup_remove_cdev_refs(gdev);
618 mutex_unlock(&gdev->reg_mutex);
619 /* Release ccwgroup device reference for local processing. */
620 put_device(&gdev->dev);
622 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
623 MODULE_LICENSE("GPL");