xref: /openbmc/linux/drivers/s390/cio/ccwgroup.c (revision e0f6d1a5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  bus driver for ccwgroup
4  *
5  *  Copyright IBM Corp. 2002, 2012
6  *
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>
18 
19 #include <asm/cio.h>
20 #include <asm/ccwdev.h>
21 #include <asm/ccwgroup.h>
22 
23 #include "device.h"
24 
25 #define CCW_BUS_ID_SIZE		10
26 
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.
32  */
33 
34 static struct bus_type ccwgroup_bus_type;
35 
36 static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
37 {
38 	int i;
39 	char str[16];
40 
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");
45 	}
46 }
47 
48 /*
49  * Remove references from ccw devices to ccw group device and from
50  * ccw group device to ccw devices.
51  */
52 static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
53 {
54 	struct ccw_device *cdev;
55 	int i;
56 
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);
66 	}
67 }
68 
69 /**
70  * ccwgroup_set_online() - enable a ccwgroup device
71  * @gdev: target ccwgroup device
72  *
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.
76  */
77 int ccwgroup_set_online(struct ccwgroup_device *gdev)
78 {
79 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
80 	int ret = -EINVAL;
81 
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;
90 
91 	gdev->state = CCWGROUP_ONLINE;
92 out:
93 	atomic_set(&gdev->onoff, 0);
94 	return ret;
95 }
96 EXPORT_SYMBOL(ccwgroup_set_online);
97 
98 /**
99  * ccwgroup_set_offline() - disable a ccwgroup device
100  * @gdev: target ccwgroup device
101  *
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.
105  */
106 int ccwgroup_set_offline(struct ccwgroup_device *gdev)
107 {
108 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
109 	int ret = -EINVAL;
110 
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;
119 
120 	gdev->state = CCWGROUP_OFFLINE;
121 out:
122 	atomic_set(&gdev->onoff, 0);
123 	return ret;
124 }
125 EXPORT_SYMBOL(ccwgroup_set_offline);
126 
127 static ssize_t ccwgroup_online_store(struct device *dev,
128 				     struct device_attribute *attr,
129 				     const char *buf, size_t count)
130 {
131 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
132 	unsigned long value;
133 	int ret;
134 
135 	device_lock(dev);
136 	if (!dev->driver) {
137 		ret = -EINVAL;
138 		goto out;
139 	}
140 
141 	ret = kstrtoul(buf, 0, &value);
142 	if (ret)
143 		goto out;
144 
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;
154 }
155 
156 static ssize_t ccwgroup_online_show(struct device *dev,
157 				    struct device_attribute *attr,
158 				    char *buf)
159 {
160 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
161 	int online;
162 
163 	online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
164 
165 	return scnprintf(buf, PAGE_SIZE, "%d\n", online);
166 }
167 
168 /*
169  * Provide an 'ungroup' attribute so the user can remove group devices no
170  * longer needed or accidentially created. Saves memory :)
171  */
172 static void ccwgroup_ungroup(struct ccwgroup_device *gdev)
173 {
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);
179 	}
180 	mutex_unlock(&gdev->reg_mutex);
181 }
182 
183 static ssize_t ccwgroup_ungroup_store(struct device *dev,
184 				      struct device_attribute *attr,
185 				      const char *buf, size_t count)
186 {
187 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
188 	int rc = 0;
189 
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;
196 	}
197 
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;
207 	}
208 	return count;
209 }
210 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
211 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
212 
213 static struct attribute *ccwgroup_attrs[] = {
214 	&dev_attr_online.attr,
215 	&dev_attr_ungroup.attr,
216 	NULL,
217 };
218 static struct attribute_group ccwgroup_attr_group = {
219 	.attrs = ccwgroup_attrs,
220 };
221 static const struct attribute_group *ccwgroup_attr_groups[] = {
222 	&ccwgroup_attr_group,
223 	NULL,
224 };
225 
226 static void ccwgroup_ungroup_workfn(struct work_struct *work)
227 {
228 	struct ccwgroup_device *gdev =
229 		container_of(work, struct ccwgroup_device, ungroup_work);
230 
231 	ccwgroup_ungroup(gdev);
232 	put_device(&gdev->dev);
233 }
234 
235 static void ccwgroup_release(struct device *dev)
236 {
237 	kfree(to_ccwgroupdev(dev));
238 }
239 
240 static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
241 {
242 	char str[16];
243 	int i, rc;
244 
245 	for (i = 0; i < gdev->count; i++) {
246 		rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
247 				       &gdev->dev.kobj, "group_device");
248 		if (rc) {
249 			for (--i; i >= 0; i--)
250 				sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
251 						  "group_device");
252 			return rc;
253 		}
254 	}
255 	for (i = 0; i < gdev->count; i++) {
256 		sprintf(str, "cdev%d", i);
257 		rc = sysfs_create_link(&gdev->dev.kobj,
258 				       &gdev->cdev[i]->dev.kobj, str);
259 		if (rc) {
260 			for (--i; i >= 0; i--) {
261 				sprintf(str, "cdev%d", i);
262 				sysfs_remove_link(&gdev->dev.kobj, str);
263 			}
264 			for (i = 0; i < gdev->count; i++)
265 				sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
266 						  "group_device");
267 			return rc;
268 		}
269 	}
270 	return 0;
271 }
272 
273 static int __get_next_id(const char **buf, struct ccw_dev_id *id)
274 {
275 	unsigned int cssid, ssid, devno;
276 	int ret = 0, len;
277 	char *start, *end;
278 
279 	start = (char *)*buf;
280 	end = strchr(start, ',');
281 	if (!end) {
282 		/* Last entry. Strip trailing newline, if applicable. */
283 		end = strchr(start, '\n');
284 		if (end)
285 			*end = '\0';
286 		len = strlen(start) + 1;
287 	} else {
288 		len = end - start + 1;
289 		end++;
290 	}
291 	if (len <= CCW_BUS_ID_SIZE) {
292 		if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
293 			ret = -EINVAL;
294 	} else
295 		ret = -EINVAL;
296 
297 	if (!ret) {
298 		id->ssid = ssid;
299 		id->devno = devno;
300 	}
301 	*buf = end;
302 	return ret;
303 }
304 
305 /**
306  * ccwgroup_create_dev() - create and register a ccw group device
307  * @parent: parent device for the new device
308  * @gdrv: driver for the new group device
309  * @num_devices: number of slave devices
310  * @buf: buffer containing comma separated bus ids of slave devices
311  *
312  * Create and register a new ccw group device as a child of @parent. Slave
313  * devices are obtained from the list of bus ids given in @buf.
314  * Returns:
315  *  %0 on success and an error code on failure.
316  * Context:
317  *  non-atomic
318  */
319 int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
320 			int num_devices, const char *buf)
321 {
322 	struct ccwgroup_device *gdev;
323 	struct ccw_dev_id dev_id;
324 	int rc, i;
325 
326 	if (num_devices < 1)
327 		return -EINVAL;
328 
329 	gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
330 		       GFP_KERNEL);
331 	if (!gdev)
332 		return -ENOMEM;
333 
334 	atomic_set(&gdev->onoff, 0);
335 	mutex_init(&gdev->reg_mutex);
336 	mutex_lock(&gdev->reg_mutex);
337 	INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn);
338 	gdev->count = num_devices;
339 	gdev->dev.bus = &ccwgroup_bus_type;
340 	gdev->dev.parent = parent;
341 	gdev->dev.release = ccwgroup_release;
342 	device_initialize(&gdev->dev);
343 
344 	for (i = 0; i < num_devices && buf; i++) {
345 		rc = __get_next_id(&buf, &dev_id);
346 		if (rc != 0)
347 			goto error;
348 		gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
349 		/*
350 		 * All devices have to be of the same type in
351 		 * order to be grouped.
352 		 */
353 		if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
354 		    gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
355 		    gdev->cdev[i]->id.driver_info !=
356 		    gdev->cdev[0]->id.driver_info) {
357 			rc = -EINVAL;
358 			goto error;
359 		}
360 		/* Don't allow a device to belong to more than one group. */
361 		spin_lock_irq(gdev->cdev[i]->ccwlock);
362 		if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
363 			spin_unlock_irq(gdev->cdev[i]->ccwlock);
364 			rc = -EINVAL;
365 			goto error;
366 		}
367 		dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
368 		spin_unlock_irq(gdev->cdev[i]->ccwlock);
369 	}
370 	/* Check for sufficient number of bus ids. */
371 	if (i < num_devices) {
372 		rc = -EINVAL;
373 		goto error;
374 	}
375 	/* Check for trailing stuff. */
376 	if (i == num_devices && strlen(buf) > 0) {
377 		rc = -EINVAL;
378 		goto error;
379 	}
380 	/* Check if the devices are bound to the required ccw driver. */
381 	if (gdrv && gdrv->ccw_driver &&
382 	    gdev->cdev[0]->drv != gdrv->ccw_driver) {
383 		rc = -EINVAL;
384 		goto error;
385 	}
386 
387 	dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
388 	gdev->dev.groups = ccwgroup_attr_groups;
389 
390 	if (gdrv) {
391 		gdev->dev.driver = &gdrv->driver;
392 		rc = gdrv->setup ? gdrv->setup(gdev) : 0;
393 		if (rc)
394 			goto error;
395 	}
396 	rc = device_add(&gdev->dev);
397 	if (rc)
398 		goto error;
399 	rc = __ccwgroup_create_symlinks(gdev);
400 	if (rc) {
401 		device_del(&gdev->dev);
402 		goto error;
403 	}
404 	mutex_unlock(&gdev->reg_mutex);
405 	return 0;
406 error:
407 	for (i = 0; i < num_devices; i++)
408 		if (gdev->cdev[i]) {
409 			spin_lock_irq(gdev->cdev[i]->ccwlock);
410 			if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
411 				dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
412 			spin_unlock_irq(gdev->cdev[i]->ccwlock);
413 			put_device(&gdev->cdev[i]->dev);
414 			gdev->cdev[i] = NULL;
415 		}
416 	mutex_unlock(&gdev->reg_mutex);
417 	put_device(&gdev->dev);
418 	return rc;
419 }
420 EXPORT_SYMBOL(ccwgroup_create_dev);
421 
422 static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
423 			     void *data)
424 {
425 	struct ccwgroup_device *gdev = to_ccwgroupdev(data);
426 
427 	if (action == BUS_NOTIFY_UNBIND_DRIVER) {
428 		get_device(&gdev->dev);
429 		schedule_work(&gdev->ungroup_work);
430 	}
431 
432 	return NOTIFY_OK;
433 }
434 
435 static struct notifier_block ccwgroup_nb = {
436 	.notifier_call = ccwgroup_notifier
437 };
438 
439 static int __init init_ccwgroup(void)
440 {
441 	int ret;
442 
443 	ret = bus_register(&ccwgroup_bus_type);
444 	if (ret)
445 		return ret;
446 
447 	ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
448 	if (ret)
449 		bus_unregister(&ccwgroup_bus_type);
450 
451 	return ret;
452 }
453 
454 static void __exit cleanup_ccwgroup(void)
455 {
456 	bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
457 	bus_unregister(&ccwgroup_bus_type);
458 }
459 
460 module_init(init_ccwgroup);
461 module_exit(cleanup_ccwgroup);
462 
463 /************************** driver stuff ******************************/
464 
465 static int ccwgroup_remove(struct device *dev)
466 {
467 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
468 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
469 
470 	if (!dev->driver)
471 		return 0;
472 	if (gdrv->remove)
473 		gdrv->remove(gdev);
474 
475 	return 0;
476 }
477 
478 static void ccwgroup_shutdown(struct device *dev)
479 {
480 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
481 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
482 
483 	if (!dev->driver)
484 		return;
485 	if (gdrv->shutdown)
486 		gdrv->shutdown(gdev);
487 }
488 
489 static int ccwgroup_pm_prepare(struct device *dev)
490 {
491 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
492 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
493 
494 	/* Fail while device is being set online/offline. */
495 	if (atomic_read(&gdev->onoff))
496 		return -EAGAIN;
497 
498 	if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
499 		return 0;
500 
501 	return gdrv->prepare ? gdrv->prepare(gdev) : 0;
502 }
503 
504 static void ccwgroup_pm_complete(struct device *dev)
505 {
506 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
507 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
508 
509 	if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
510 		return;
511 
512 	if (gdrv->complete)
513 		gdrv->complete(gdev);
514 }
515 
516 static int ccwgroup_pm_freeze(struct device *dev)
517 {
518 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
519 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
520 
521 	if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
522 		return 0;
523 
524 	return gdrv->freeze ? gdrv->freeze(gdev) : 0;
525 }
526 
527 static int ccwgroup_pm_thaw(struct device *dev)
528 {
529 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
530 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
531 
532 	if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
533 		return 0;
534 
535 	return gdrv->thaw ? gdrv->thaw(gdev) : 0;
536 }
537 
538 static int ccwgroup_pm_restore(struct device *dev)
539 {
540 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
541 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
542 
543 	if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
544 		return 0;
545 
546 	return gdrv->restore ? gdrv->restore(gdev) : 0;
547 }
548 
549 static const struct dev_pm_ops ccwgroup_pm_ops = {
550 	.prepare = ccwgroup_pm_prepare,
551 	.complete = ccwgroup_pm_complete,
552 	.freeze = ccwgroup_pm_freeze,
553 	.thaw = ccwgroup_pm_thaw,
554 	.restore = ccwgroup_pm_restore,
555 };
556 
557 static struct bus_type ccwgroup_bus_type = {
558 	.name   = "ccwgroup",
559 	.remove = ccwgroup_remove,
560 	.shutdown = ccwgroup_shutdown,
561 	.pm = &ccwgroup_pm_ops,
562 };
563 
564 /**
565  * ccwgroup_driver_register() - register a ccw group driver
566  * @cdriver: driver to be registered
567  *
568  * This function is mainly a wrapper around driver_register().
569  */
570 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
571 {
572 	/* register our new driver with the core */
573 	cdriver->driver.bus = &ccwgroup_bus_type;
574 
575 	return driver_register(&cdriver->driver);
576 }
577 EXPORT_SYMBOL(ccwgroup_driver_register);
578 
579 static int __ccwgroup_match_all(struct device *dev, void *data)
580 {
581 	return 1;
582 }
583 
584 /**
585  * ccwgroup_driver_unregister() - deregister a ccw group driver
586  * @cdriver: driver to be deregistered
587  *
588  * This function is mainly a wrapper around driver_unregister().
589  */
590 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
591 {
592 	struct device *dev;
593 
594 	/* We don't want ccwgroup devices to live longer than their driver. */
595 	while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
596 					 __ccwgroup_match_all))) {
597 		struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
598 
599 		ccwgroup_ungroup(gdev);
600 		put_device(dev);
601 	}
602 	driver_unregister(&cdriver->driver);
603 }
604 EXPORT_SYMBOL(ccwgroup_driver_unregister);
605 
606 /**
607  * ccwgroup_probe_ccwdev() - probe function for slave devices
608  * @cdev: ccw device to be probed
609  *
610  * This is a dummy probe function for ccw devices that are slave devices in
611  * a ccw group device.
612  * Returns:
613  *  always %0
614  */
615 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
616 {
617 	return 0;
618 }
619 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
620 
621 /**
622  * ccwgroup_remove_ccwdev() - remove function for slave devices
623  * @cdev: ccw device to be removed
624  *
625  * This is a remove function for ccw devices that are slave devices in a ccw
626  * group device. It sets the ccw device offline and also deregisters the
627  * embedding ccw group device.
628  */
629 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
630 {
631 	struct ccwgroup_device *gdev;
632 
633 	/* Ignore offlining errors, device is gone anyway. */
634 	ccw_device_set_offline(cdev);
635 	/* If one of its devices is gone, the whole group is done for. */
636 	spin_lock_irq(cdev->ccwlock);
637 	gdev = dev_get_drvdata(&cdev->dev);
638 	if (!gdev) {
639 		spin_unlock_irq(cdev->ccwlock);
640 		return;
641 	}
642 	/* Get ccwgroup device reference for local processing. */
643 	get_device(&gdev->dev);
644 	spin_unlock_irq(cdev->ccwlock);
645 	/* Unregister group device. */
646 	ccwgroup_ungroup(gdev);
647 	/* Release ccwgroup device reference for local processing. */
648 	put_device(&gdev->dev);
649 }
650 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
651 MODULE_LICENSE("GPL");
652