Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / s390 / cio / ccwgroup.c
blob444385da57925fd026ca1bc1546c1e0a9cbbe1d3
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * bus driver for ccwgroup
5 * Copyright IBM Corp. 2002, 2012
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/cio.h>
20 #include <asm/ccwdev.h>
21 #include <asm/ccwgroup.h>
23 #include "device.h"
25 #define CCW_BUS_ID_SIZE 10
27 /* In Linux 2.4, we had a channel device layer called "chandev"
28 * that did all sorts of obscure stuff for networking devices.
29 * This is another driver that serves as a replacement for just
30 * one of its functions, namely the translation of single subchannels
31 * to devices that use multiple subchannels.
34 static struct bus_type ccwgroup_bus_type;
36 static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
38 int i;
39 char str[16];
41 for (i = 0; i < gdev->count; i++) {
42 sprintf(str, "cdev%d", i);
43 sysfs_remove_link(&gdev->dev.kobj, str);
44 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
49 * Remove references from ccw devices to ccw group device and from
50 * ccw group device to ccw devices.
52 static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
54 struct ccw_device *cdev;
55 int i;
57 for (i = 0; i < gdev->count; i++) {
58 cdev = gdev->cdev[i];
59 if (!cdev)
60 continue;
61 spin_lock_irq(cdev->ccwlock);
62 dev_set_drvdata(&cdev->dev, NULL);
63 spin_unlock_irq(cdev->ccwlock);
64 gdev->cdev[i] = NULL;
65 put_device(&cdev->dev);
69 /**
70 * ccwgroup_set_online() - enable a ccwgroup device
71 * @gdev: target ccwgroup device
73 * This function attempts to put the ccwgroup device into the online state.
74 * Returns:
75 * %0 on success and a negative error value on failure.
77 int ccwgroup_set_online(struct ccwgroup_device *gdev)
79 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
80 int ret = -EINVAL;
82 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
83 return -EAGAIN;
84 if (gdev->state == CCWGROUP_ONLINE)
85 goto out;
86 if (gdrv->set_online)
87 ret = gdrv->set_online(gdev);
88 if (ret)
89 goto out;
91 gdev->state = CCWGROUP_ONLINE;
92 out:
93 atomic_set(&gdev->onoff, 0);
94 return ret;
96 EXPORT_SYMBOL(ccwgroup_set_online);
98 /**
99 * ccwgroup_set_offline() - disable a ccwgroup device
100 * @gdev: target ccwgroup device
102 * This function attempts to put the ccwgroup device into the offline state.
103 * Returns:
104 * %0 on success and a negative error value on failure.
106 int ccwgroup_set_offline(struct ccwgroup_device *gdev)
108 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
109 int ret = -EINVAL;
111 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
112 return -EAGAIN;
113 if (gdev->state == CCWGROUP_OFFLINE)
114 goto out;
115 if (gdrv->set_offline)
116 ret = gdrv->set_offline(gdev);
117 if (ret)
118 goto out;
120 gdev->state = CCWGROUP_OFFLINE;
121 out:
122 atomic_set(&gdev->onoff, 0);
123 return ret;
125 EXPORT_SYMBOL(ccwgroup_set_offline);
127 static ssize_t ccwgroup_online_store(struct device *dev,
128 struct device_attribute *attr,
129 const char *buf, size_t count)
131 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
132 unsigned long value;
133 int ret;
135 device_lock(dev);
136 if (!dev->driver) {
137 ret = -EINVAL;
138 goto out;
141 ret = kstrtoul(buf, 0, &value);
142 if (ret)
143 goto out;
145 if (value == 1)
146 ret = ccwgroup_set_online(gdev);
147 else if (value == 0)
148 ret = ccwgroup_set_offline(gdev);
149 else
150 ret = -EINVAL;
151 out:
152 device_unlock(dev);
153 return (ret == 0) ? count : ret;
156 static ssize_t ccwgroup_online_show(struct device *dev,
157 struct device_attribute *attr,
158 char *buf)
160 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
161 int online;
163 online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
165 return scnprintf(buf, PAGE_SIZE, "%d\n", online);
169 * Provide an 'ungroup' attribute so the user can remove group devices no
170 * longer needed or accidentially created. Saves memory :)
172 static void ccwgroup_ungroup(struct ccwgroup_device *gdev)
174 mutex_lock(&gdev->reg_mutex);
175 if (device_is_registered(&gdev->dev)) {
176 __ccwgroup_remove_symlinks(gdev);
177 device_unregister(&gdev->dev);
178 __ccwgroup_remove_cdev_refs(gdev);
180 mutex_unlock(&gdev->reg_mutex);
183 static ssize_t ccwgroup_ungroup_store(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf, size_t count)
187 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
188 int rc = 0;
190 /* Prevent concurrent online/offline processing and ungrouping. */
191 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
192 return -EAGAIN;
193 if (gdev->state != CCWGROUP_OFFLINE) {
194 rc = -EINVAL;
195 goto out;
198 if (device_remove_file_self(dev, attr))
199 ccwgroup_ungroup(gdev);
200 else
201 rc = -ENODEV;
202 out:
203 if (rc) {
204 /* Release onoff "lock" when ungrouping failed. */
205 atomic_set(&gdev->onoff, 0);
206 return rc;
208 return count;
210 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
211 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
213 static struct attribute *ccwgroup_dev_attrs[] = {
214 &dev_attr_online.attr,
215 &dev_attr_ungroup.attr,
216 NULL,
218 ATTRIBUTE_GROUPS(ccwgroup_dev);
220 static void ccwgroup_ungroup_workfn(struct work_struct *work)
222 struct ccwgroup_device *gdev =
223 container_of(work, struct ccwgroup_device, ungroup_work);
225 ccwgroup_ungroup(gdev);
226 put_device(&gdev->dev);
229 static void ccwgroup_release(struct device *dev)
231 kfree(to_ccwgroupdev(dev));
234 static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
236 char str[16];
237 int i, rc;
239 for (i = 0; i < gdev->count; i++) {
240 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
241 &gdev->dev.kobj, "group_device");
242 if (rc) {
243 for (--i; i >= 0; i--)
244 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
245 "group_device");
246 return rc;
249 for (i = 0; i < gdev->count; i++) {
250 sprintf(str, "cdev%d", i);
251 rc = sysfs_create_link(&gdev->dev.kobj,
252 &gdev->cdev[i]->dev.kobj, str);
253 if (rc) {
254 for (--i; i >= 0; i--) {
255 sprintf(str, "cdev%d", i);
256 sysfs_remove_link(&gdev->dev.kobj, str);
258 for (i = 0; i < gdev->count; i++)
259 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
260 "group_device");
261 return rc;
264 return 0;
267 static int __get_next_id(const char **buf, struct ccw_dev_id *id)
269 unsigned int cssid, ssid, devno;
270 int ret = 0, len;
271 char *start, *end;
273 start = (char *)*buf;
274 end = strchr(start, ',');
275 if (!end) {
276 /* Last entry. Strip trailing newline, if applicable. */
277 end = strchr(start, '\n');
278 if (end)
279 *end = '\0';
280 len = strlen(start) + 1;
281 } else {
282 len = end - start + 1;
283 end++;
285 if (len <= CCW_BUS_ID_SIZE) {
286 if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
287 ret = -EINVAL;
288 } else
289 ret = -EINVAL;
291 if (!ret) {
292 id->ssid = ssid;
293 id->devno = devno;
295 *buf = end;
296 return ret;
300 * ccwgroup_create_dev() - create and register a ccw group device
301 * @parent: parent device for the new device
302 * @gdrv: driver for the new group device
303 * @num_devices: number of slave devices
304 * @buf: buffer containing comma separated bus ids of slave devices
306 * Create and register a new ccw group device as a child of @parent. Slave
307 * devices are obtained from the list of bus ids given in @buf.
308 * Returns:
309 * %0 on success and an error code on failure.
310 * Context:
311 * non-atomic
313 int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
314 int num_devices, const char *buf)
316 struct ccwgroup_device *gdev;
317 struct ccw_dev_id dev_id;
318 int rc, i;
320 if (num_devices < 1)
321 return -EINVAL;
323 gdev = kzalloc(struct_size(gdev, cdev, num_devices), GFP_KERNEL);
324 if (!gdev)
325 return -ENOMEM;
327 atomic_set(&gdev->onoff, 0);
328 mutex_init(&gdev->reg_mutex);
329 mutex_lock(&gdev->reg_mutex);
330 INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn);
331 gdev->count = num_devices;
332 gdev->dev.bus = &ccwgroup_bus_type;
333 gdev->dev.parent = parent;
334 gdev->dev.release = ccwgroup_release;
335 device_initialize(&gdev->dev);
337 for (i = 0; i < num_devices && buf; i++) {
338 rc = __get_next_id(&buf, &dev_id);
339 if (rc != 0)
340 goto error;
341 gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
343 * All devices have to be of the same type in
344 * order to be grouped.
346 if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
347 gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
348 gdev->cdev[i]->id.driver_info !=
349 gdev->cdev[0]->id.driver_info) {
350 rc = -EINVAL;
351 goto error;
353 /* Don't allow a device to belong to more than one group. */
354 spin_lock_irq(gdev->cdev[i]->ccwlock);
355 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
356 spin_unlock_irq(gdev->cdev[i]->ccwlock);
357 rc = -EINVAL;
358 goto error;
360 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
361 spin_unlock_irq(gdev->cdev[i]->ccwlock);
363 /* Check for sufficient number of bus ids. */
364 if (i < num_devices) {
365 rc = -EINVAL;
366 goto error;
368 /* Check for trailing stuff. */
369 if (i == num_devices && buf && strlen(buf) > 0) {
370 rc = -EINVAL;
371 goto error;
373 /* Check if the devices are bound to the required ccw driver. */
374 if (gdrv && gdrv->ccw_driver &&
375 gdev->cdev[0]->drv != gdrv->ccw_driver) {
376 rc = -EINVAL;
377 goto error;
380 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
382 if (gdrv) {
383 gdev->dev.driver = &gdrv->driver;
384 rc = gdrv->setup ? gdrv->setup(gdev) : 0;
385 if (rc)
386 goto error;
388 rc = device_add(&gdev->dev);
389 if (rc)
390 goto error;
391 rc = __ccwgroup_create_symlinks(gdev);
392 if (rc) {
393 device_del(&gdev->dev);
394 goto error;
396 mutex_unlock(&gdev->reg_mutex);
397 return 0;
398 error:
399 for (i = 0; i < num_devices; i++)
400 if (gdev->cdev[i]) {
401 spin_lock_irq(gdev->cdev[i]->ccwlock);
402 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
403 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
404 spin_unlock_irq(gdev->cdev[i]->ccwlock);
405 put_device(&gdev->cdev[i]->dev);
406 gdev->cdev[i] = NULL;
408 mutex_unlock(&gdev->reg_mutex);
409 put_device(&gdev->dev);
410 return rc;
412 EXPORT_SYMBOL(ccwgroup_create_dev);
414 static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
415 void *data)
417 struct ccwgroup_device *gdev = to_ccwgroupdev(data);
419 if (action == BUS_NOTIFY_UNBIND_DRIVER) {
420 get_device(&gdev->dev);
421 schedule_work(&gdev->ungroup_work);
424 return NOTIFY_OK;
427 static struct notifier_block ccwgroup_nb = {
428 .notifier_call = ccwgroup_notifier
431 static int __init init_ccwgroup(void)
433 int ret;
435 ret = bus_register(&ccwgroup_bus_type);
436 if (ret)
437 return ret;
439 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
440 if (ret)
441 bus_unregister(&ccwgroup_bus_type);
443 return ret;
446 static void __exit cleanup_ccwgroup(void)
448 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
449 bus_unregister(&ccwgroup_bus_type);
452 module_init(init_ccwgroup);
453 module_exit(cleanup_ccwgroup);
455 /************************** driver stuff ******************************/
457 static int ccwgroup_remove(struct device *dev)
459 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
460 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
462 if (!dev->driver)
463 return 0;
464 if (gdrv->remove)
465 gdrv->remove(gdev);
467 return 0;
470 static void ccwgroup_shutdown(struct device *dev)
472 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
473 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
475 if (!dev->driver)
476 return;
477 if (gdrv->shutdown)
478 gdrv->shutdown(gdev);
481 static struct bus_type ccwgroup_bus_type = {
482 .name = "ccwgroup",
483 .dev_groups = ccwgroup_dev_groups,
484 .remove = ccwgroup_remove,
485 .shutdown = ccwgroup_shutdown,
488 bool dev_is_ccwgroup(struct device *dev)
490 return dev->bus == &ccwgroup_bus_type;
492 EXPORT_SYMBOL(dev_is_ccwgroup);
495 * ccwgroup_driver_register() - register a ccw group driver
496 * @cdriver: driver to be registered
498 * This function is mainly a wrapper around driver_register().
500 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
502 /* register our new driver with the core */
503 cdriver->driver.bus = &ccwgroup_bus_type;
505 return driver_register(&cdriver->driver);
507 EXPORT_SYMBOL(ccwgroup_driver_register);
510 * ccwgroup_driver_unregister() - deregister a ccw group driver
511 * @cdriver: driver to be deregistered
513 * This function is mainly a wrapper around driver_unregister().
515 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
517 struct device *dev;
519 /* We don't want ccwgroup devices to live longer than their driver. */
520 while ((dev = driver_find_next_device(&cdriver->driver, NULL))) {
521 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
523 ccwgroup_ungroup(gdev);
524 put_device(dev);
526 driver_unregister(&cdriver->driver);
528 EXPORT_SYMBOL(ccwgroup_driver_unregister);
531 * get_ccwgroupdev_by_busid() - obtain device from a bus id
532 * @gdrv: driver the device is owned by
533 * @bus_id: bus id of the device to be searched
535 * This function searches all devices owned by @gdrv for a device with a bus
536 * id matching @bus_id.
537 * Returns:
538 * If a match is found, its reference count of the found device is increased
539 * and it is returned; else %NULL is returned.
541 struct ccwgroup_device *get_ccwgroupdev_by_busid(struct ccwgroup_driver *gdrv,
542 char *bus_id)
544 struct device *dev;
546 dev = driver_find_device_by_name(&gdrv->driver, bus_id);
548 return dev ? to_ccwgroupdev(dev) : NULL;
550 EXPORT_SYMBOL_GPL(get_ccwgroupdev_by_busid);
553 * ccwgroup_probe_ccwdev() - probe function for slave devices
554 * @cdev: ccw device to be probed
556 * This is a dummy probe function for ccw devices that are slave devices in
557 * a ccw group device.
558 * Returns:
559 * always %0
561 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
563 return 0;
565 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
568 * ccwgroup_remove_ccwdev() - remove function for slave devices
569 * @cdev: ccw device to be removed
571 * This is a remove function for ccw devices that are slave devices in a ccw
572 * group device. It sets the ccw device offline and also deregisters the
573 * embedding ccw group device.
575 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
577 struct ccwgroup_device *gdev;
579 /* Ignore offlining errors, device is gone anyway. */
580 ccw_device_set_offline(cdev);
581 /* If one of its devices is gone, the whole group is done for. */
582 spin_lock_irq(cdev->ccwlock);
583 gdev = dev_get_drvdata(&cdev->dev);
584 if (!gdev) {
585 spin_unlock_irq(cdev->ccwlock);
586 return;
588 /* Get ccwgroup device reference for local processing. */
589 get_device(&gdev->dev);
590 spin_unlock_irq(cdev->ccwlock);
591 /* Unregister group device. */
592 ccwgroup_ungroup(gdev);
593 /* Release ccwgroup device reference for local processing. */
594 put_device(&gdev->dev);
596 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
597 MODULE_LICENSE("GPL");