ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / s390 / cio / ccwgroup.c
blob26a930e832bdab58c7f649c866124200e1f3f8c8
1 /*
2 * drivers/s390/cio/ccwgroup.c
3 * bus driver for ccwgroup
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 */
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>
19 #include <asm/ccwdev.h>
20 #include <asm/ccwgroup.h>
22 /* In Linux 2.4, we had a channel device layer called "chandev"
23 * that did all sorts of obscure stuff for networking devices.
24 * This is another driver that serves as a replacement for just
25 * one of its functions, namely the translation of single subchannels
26 * to devices that use multiple subchannels.
29 /* a device matches a driver if all its slave devices match the same
30 * entry of the driver */
31 static int
32 ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
34 struct ccwgroup_device *gdev;
35 struct ccwgroup_driver *gdrv;
37 gdev = to_ccwgroupdev(dev);
38 gdrv = to_ccwgroupdrv(drv);
40 if (gdev->creator_id == gdrv->driver_id)
41 return 1;
43 return 0;
45 static int
46 ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
48 /* TODO */
49 return 0;
52 static struct bus_type ccwgroup_bus_type;
54 static void
55 __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
57 int i;
58 char str[8];
60 for (i = 0; i < gdev->count; i++) {
61 sprintf(str, "cdev%d", i);
62 sysfs_remove_link(&gdev->dev.kobj, str);
63 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
69 * Provide an 'ungroup' attribute so the user can remove group devices no
70 * longer needed or accidentially created. Saves memory :)
72 static void ccwgroup_ungroup_callback(struct device *dev)
74 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
76 mutex_lock(&gdev->reg_mutex);
77 if (device_is_registered(&gdev->dev)) {
78 __ccwgroup_remove_symlinks(gdev);
79 device_unregister(dev);
81 mutex_unlock(&gdev->reg_mutex);
84 static ssize_t
85 ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
87 struct ccwgroup_device *gdev;
88 int rc;
90 gdev = to_ccwgroupdev(dev);
92 if (gdev->state != CCWGROUP_OFFLINE)
93 return -EINVAL;
95 /* Note that we cannot unregister the device from one of its
96 * attribute methods, so we have to use this roundabout approach.
98 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
99 if (rc)
100 count = rc;
101 return count;
104 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
106 static void
107 ccwgroup_release (struct device *dev)
109 struct ccwgroup_device *gdev;
110 int i;
112 gdev = to_ccwgroupdev(dev);
114 for (i = 0; i < gdev->count; i++) {
115 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
116 put_device(&gdev->cdev[i]->dev);
118 kfree(gdev);
121 static int
122 __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
124 char str[8];
125 int i, rc;
127 for (i = 0; i < gdev->count; i++) {
128 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
129 "group_device");
130 if (rc) {
131 for (--i; i >= 0; i--)
132 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
133 "group_device");
134 return rc;
137 for (i = 0; i < gdev->count; i++) {
138 sprintf(str, "cdev%d", i);
139 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
140 str);
141 if (rc) {
142 for (--i; i >= 0; i--) {
143 sprintf(str, "cdev%d", i);
144 sysfs_remove_link(&gdev->dev.kobj, str);
146 for (i = 0; i < gdev->count; i++)
147 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
148 "group_device");
149 return rc;
152 return 0;
155 static int __get_next_bus_id(const char **buf, char *bus_id)
157 int rc, len;
158 char *start, *end;
160 start = (char *)*buf;
161 end = strchr(start, ',');
162 if (!end) {
163 /* Last entry. Strip trailing newline, if applicable. */
164 end = strchr(start, '\n');
165 if (end)
166 *end = '\0';
167 len = strlen(start) + 1;
168 } else {
169 len = end - start + 1;
170 end++;
172 if (len < BUS_ID_SIZE) {
173 strlcpy(bus_id, start, len);
174 rc = 0;
175 } else
176 rc = -EINVAL;
177 *buf = end;
178 return rc;
181 static int __is_valid_bus_id(char bus_id[BUS_ID_SIZE])
183 int cssid, ssid, devno;
185 /* Must be of form %x.%x.%04x */
186 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
187 return 0;
188 return 1;
192 * ccwgroup_create_from_string() - create and register a ccw group device
193 * @root: parent device for the new device
194 * @creator_id: identifier of creating driver
195 * @cdrv: ccw driver of slave devices
196 * @num_devices: number of slave devices
197 * @buf: buffer containing comma separated bus ids of slave devices
199 * Create and register a new ccw group device as a child of @root. Slave
200 * devices are obtained from the list of bus ids given in @buf and must all
201 * belong to @cdrv.
202 * Returns:
203 * %0 on success and an error code on failure.
204 * Context:
205 * non-atomic
207 int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
208 struct ccw_driver *cdrv, int num_devices,
209 const char *buf)
211 struct ccwgroup_device *gdev;
212 int rc, i;
213 char tmp_bus_id[BUS_ID_SIZE];
214 const char *curr_buf;
216 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
217 GFP_KERNEL);
218 if (!gdev)
219 return -ENOMEM;
221 atomic_set(&gdev->onoff, 0);
222 mutex_init(&gdev->reg_mutex);
223 mutex_lock(&gdev->reg_mutex);
224 curr_buf = buf;
225 for (i = 0; i < num_devices && curr_buf; i++) {
226 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
227 if (rc != 0)
228 goto error;
229 if (!__is_valid_bus_id(tmp_bus_id)) {
230 rc = -EINVAL;
231 goto error;
233 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
235 * All devices have to be of the same type in
236 * order to be grouped.
238 if (!gdev->cdev[i]
239 || gdev->cdev[i]->id.driver_info !=
240 gdev->cdev[0]->id.driver_info) {
241 rc = -EINVAL;
242 goto error;
244 /* Don't allow a device to belong to more than one group. */
245 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
246 rc = -EINVAL;
247 goto error;
249 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
251 /* Check for sufficient number of bus ids. */
252 if (i < num_devices && !curr_buf) {
253 rc = -EINVAL;
254 goto error;
256 /* Check for trailing stuff. */
257 if (i == num_devices && strlen(curr_buf) > 0) {
258 rc = -EINVAL;
259 goto error;
261 gdev->creator_id = creator_id;
262 gdev->count = num_devices;
263 gdev->dev.bus = &ccwgroup_bus_type;
264 gdev->dev.parent = root;
265 gdev->dev.release = ccwgroup_release;
267 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
268 gdev->cdev[0]->dev.bus_id);
270 rc = device_register(&gdev->dev);
271 if (rc)
272 goto error;
273 get_device(&gdev->dev);
274 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
276 if (rc) {
277 device_unregister(&gdev->dev);
278 goto error;
281 rc = __ccwgroup_create_symlinks(gdev);
282 if (!rc) {
283 mutex_unlock(&gdev->reg_mutex);
284 put_device(&gdev->dev);
285 return 0;
287 device_remove_file(&gdev->dev, &dev_attr_ungroup);
288 device_unregister(&gdev->dev);
289 error:
290 for (i = 0; i < num_devices; i++)
291 if (gdev->cdev[i]) {
292 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
293 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
294 put_device(&gdev->cdev[i]->dev);
296 mutex_unlock(&gdev->reg_mutex);
297 put_device(&gdev->dev);
298 return rc;
300 EXPORT_SYMBOL(ccwgroup_create_from_string);
302 static int __init
303 init_ccwgroup (void)
305 return bus_register (&ccwgroup_bus_type);
308 static void __exit
309 cleanup_ccwgroup (void)
311 bus_unregister (&ccwgroup_bus_type);
314 module_init(init_ccwgroup);
315 module_exit(cleanup_ccwgroup);
317 /************************** driver stuff ******************************/
319 static int
320 ccwgroup_set_online(struct ccwgroup_device *gdev)
322 struct ccwgroup_driver *gdrv;
323 int ret;
325 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
326 return -EAGAIN;
327 if (gdev->state == CCWGROUP_ONLINE) {
328 ret = 0;
329 goto out;
331 if (!gdev->dev.driver) {
332 ret = -EINVAL;
333 goto out;
335 gdrv = to_ccwgroupdrv (gdev->dev.driver);
336 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
337 goto out;
339 gdev->state = CCWGROUP_ONLINE;
340 out:
341 atomic_set(&gdev->onoff, 0);
342 return ret;
345 static int
346 ccwgroup_set_offline(struct ccwgroup_device *gdev)
348 struct ccwgroup_driver *gdrv;
349 int ret;
351 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
352 return -EAGAIN;
353 if (gdev->state == CCWGROUP_OFFLINE) {
354 ret = 0;
355 goto out;
357 if (!gdev->dev.driver) {
358 ret = -EINVAL;
359 goto out;
361 gdrv = to_ccwgroupdrv (gdev->dev.driver);
362 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
363 goto out;
365 gdev->state = CCWGROUP_OFFLINE;
366 out:
367 atomic_set(&gdev->onoff, 0);
368 return ret;
371 static ssize_t
372 ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
374 struct ccwgroup_device *gdev;
375 struct ccwgroup_driver *gdrv;
376 unsigned long value;
377 int ret;
379 gdev = to_ccwgroupdev(dev);
380 if (!dev->driver)
381 return count;
383 gdrv = to_ccwgroupdrv (gdev->dev.driver);
384 if (!try_module_get(gdrv->owner))
385 return -EINVAL;
387 ret = strict_strtoul(buf, 0, &value);
388 if (ret)
389 goto out;
390 ret = count;
391 if (value == 1)
392 ccwgroup_set_online(gdev);
393 else if (value == 0)
394 ccwgroup_set_offline(gdev);
395 else
396 ret = -EINVAL;
397 out:
398 module_put(gdrv->owner);
399 return ret;
402 static ssize_t
403 ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
405 int online;
407 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
409 return sprintf(buf, online ? "1\n" : "0\n");
412 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
414 static int
415 ccwgroup_probe (struct device *dev)
417 struct ccwgroup_device *gdev;
418 struct ccwgroup_driver *gdrv;
420 int ret;
422 gdev = to_ccwgroupdev(dev);
423 gdrv = to_ccwgroupdrv(dev->driver);
425 if ((ret = device_create_file(dev, &dev_attr_online)))
426 return ret;
428 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
429 if (ret)
430 device_remove_file(dev, &dev_attr_online);
432 return ret;
435 static int
436 ccwgroup_remove (struct device *dev)
438 struct ccwgroup_device *gdev;
439 struct ccwgroup_driver *gdrv;
441 gdev = to_ccwgroupdev(dev);
442 gdrv = to_ccwgroupdrv(dev->driver);
444 device_remove_file(dev, &dev_attr_online);
446 if (gdrv && gdrv->remove)
447 gdrv->remove(gdev);
448 return 0;
451 static void ccwgroup_shutdown(struct device *dev)
453 struct ccwgroup_device *gdev;
454 struct ccwgroup_driver *gdrv;
456 gdev = to_ccwgroupdev(dev);
457 gdrv = to_ccwgroupdrv(dev->driver);
458 if (gdrv && gdrv->shutdown)
459 gdrv->shutdown(gdev);
462 static struct bus_type ccwgroup_bus_type = {
463 .name = "ccwgroup",
464 .match = ccwgroup_bus_match,
465 .uevent = ccwgroup_uevent,
466 .probe = ccwgroup_probe,
467 .remove = ccwgroup_remove,
468 .shutdown = ccwgroup_shutdown,
472 * ccwgroup_driver_register() - register a ccw group driver
473 * @cdriver: driver to be registered
475 * This function is mainly a wrapper around driver_register().
477 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
479 /* register our new driver with the core */
480 cdriver->driver.bus = &ccwgroup_bus_type;
481 cdriver->driver.name = cdriver->name;
482 cdriver->driver.owner = cdriver->owner;
484 return driver_register(&cdriver->driver);
487 static int
488 __ccwgroup_match_all(struct device *dev, void *data)
490 return 1;
494 * ccwgroup_driver_unregister() - deregister a ccw group driver
495 * @cdriver: driver to be deregistered
497 * This function is mainly a wrapper around driver_unregister().
499 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
501 struct device *dev;
503 /* We don't want ccwgroup devices to live longer than their driver. */
504 get_driver(&cdriver->driver);
505 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
506 __ccwgroup_match_all))) {
507 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
509 mutex_lock(&gdev->reg_mutex);
510 __ccwgroup_remove_symlinks(gdev);
511 device_unregister(dev);
512 mutex_unlock(&gdev->reg_mutex);
513 put_device(dev);
515 put_driver(&cdriver->driver);
516 driver_unregister(&cdriver->driver);
520 * ccwgroup_probe_ccwdev() - probe function for slave devices
521 * @cdev: ccw device to be probed
523 * This is a dummy probe function for ccw devices that are slave devices in
524 * a ccw group device.
525 * Returns:
526 * always %0
528 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
530 return 0;
533 static struct ccwgroup_device *
534 __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
536 struct ccwgroup_device *gdev;
538 gdev = dev_get_drvdata(&cdev->dev);
539 if (gdev) {
540 if (get_device(&gdev->dev)) {
541 mutex_lock(&gdev->reg_mutex);
542 if (device_is_registered(&gdev->dev))
543 return gdev;
544 mutex_unlock(&gdev->reg_mutex);
545 put_device(&gdev->dev);
547 return NULL;
549 return NULL;
553 * ccwgroup_remove_ccwdev() - remove function for slave devices
554 * @cdev: ccw device to be removed
556 * This is a remove function for ccw devices that are slave devices in a ccw
557 * group device. It sets the ccw device offline and also deregisters the
558 * embedding ccw group device.
560 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
562 struct ccwgroup_device *gdev;
564 /* Ignore offlining errors, device is gone anyway. */
565 ccw_device_set_offline(cdev);
566 /* If one of its devices is gone, the whole group is done for. */
567 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
568 if (gdev) {
569 __ccwgroup_remove_symlinks(gdev);
570 device_unregister(&gdev->dev);
571 mutex_unlock(&gdev->reg_mutex);
572 put_device(&gdev->dev);
576 MODULE_LICENSE("GPL");
577 EXPORT_SYMBOL(ccwgroup_driver_register);
578 EXPORT_SYMBOL(ccwgroup_driver_unregister);
579 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
580 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);