blob: aa3292e57e3838c0e30d2c1302fa4b7dcb36b7e7 [file] [log] [blame]
Greg Kroah-Hartman724117b2017-11-14 18:38:02 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * bus driver for ccwgroup
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Sebastian Ottf2962da2012-05-15 17:49:12 +02005 * Copyright IBM Corp. 2002, 2012
Sebastian Ott7e597a22009-06-16 10:30:21 +02006 *
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
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
Sebastian Ottb7a610f2012-05-15 17:52:07 +020019#include <asm/cio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/ccwdev.h>
21#include <asm/ccwgroup.h>
22
Sebastian Ottb7a610f2012-05-15 17:52:07 +020023#include "device.h"
24
25#define CCW_BUS_ID_SIZE 10
Kay Sievers98df67b2008-12-25 13:38:55 +010026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* 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
Russell Kingf9ccf452006-01-05 14:42:09 +000034static struct bus_type ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Sebastian Ottdad572e32011-10-30 15:16:53 +010036static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 int i;
Heiko Carstens2685df62017-05-04 13:35:33 +020039 char str[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Sebastian Ott683c3dc2012-11-22 15:56:39 +010048/**
49 * ccwgroup_set_online() - enable a ccwgroup device
50 * @gdev: target ccwgroup device
51 *
52 * This function attempts to put the ccwgroup device into the online state.
53 * Returns:
54 * %0 on success and a negative error value on failure.
55 */
56int ccwgroup_set_online(struct ccwgroup_device *gdev)
Sebastian Ottdad572e32011-10-30 15:16:53 +010057{
58 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
Sebastian Ottcff97102012-11-09 14:33:06 +010059 int ret = -EINVAL;
Sebastian Ottdad572e32011-10-30 15:16:53 +010060
61 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
62 return -EAGAIN;
63 if (gdev->state == CCWGROUP_ONLINE)
64 goto out;
65 if (gdrv->set_online)
66 ret = gdrv->set_online(gdev);
67 if (ret)
68 goto out;
69
70 gdev->state = CCWGROUP_ONLINE;
71out:
72 atomic_set(&gdev->onoff, 0);
73 return ret;
74}
Sebastian Ott683c3dc2012-11-22 15:56:39 +010075EXPORT_SYMBOL(ccwgroup_set_online);
Sebastian Ottdad572e32011-10-30 15:16:53 +010076
Sebastian Ott683c3dc2012-11-22 15:56:39 +010077/**
78 * ccwgroup_set_offline() - disable a ccwgroup device
79 * @gdev: target ccwgroup device
Alexandra Winterd2b59bd2021-09-21 16:52:17 +020080 * @call_gdrv: Call the registered gdrv set_offline function
Sebastian Ott683c3dc2012-11-22 15:56:39 +010081 *
82 * This function attempts to put the ccwgroup device into the offline state.
83 * Returns:
84 * %0 on success and a negative error value on failure.
85 */
Alexandra Winterd2b59bd2021-09-21 16:52:17 +020086int ccwgroup_set_offline(struct ccwgroup_device *gdev, bool call_gdrv)
Sebastian Ottdad572e32011-10-30 15:16:53 +010087{
88 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
Sebastian Ottcff97102012-11-09 14:33:06 +010089 int ret = -EINVAL;
Sebastian Ottdad572e32011-10-30 15:16:53 +010090
91 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
92 return -EAGAIN;
93 if (gdev->state == CCWGROUP_OFFLINE)
94 goto out;
Alexandra Winterd2b59bd2021-09-21 16:52:17 +020095 if (!call_gdrv) {
96 ret = 0;
97 goto offline;
98 }
Sebastian Ottdad572e32011-10-30 15:16:53 +010099 if (gdrv->set_offline)
100 ret = gdrv->set_offline(gdev);
101 if (ret)
102 goto out;
103
Alexandra Winterd2b59bd2021-09-21 16:52:17 +0200104offline:
Sebastian Ottdad572e32011-10-30 15:16:53 +0100105 gdev->state = CCWGROUP_OFFLINE;
106out:
107 atomic_set(&gdev->onoff, 0);
108 return ret;
109}
Sebastian Ott683c3dc2012-11-22 15:56:39 +0100110EXPORT_SYMBOL(ccwgroup_set_offline);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100111
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100112static ssize_t ccwgroup_online_store(struct device *dev,
113 struct device_attribute *attr,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100114 const char *buf, size_t count)
115{
116 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100117 unsigned long value;
118 int ret;
119
Sebastian Ottd5b877f2013-12-16 10:56:46 +0100120 device_lock(dev);
121 if (!dev->driver) {
122 ret = -EINVAL;
123 goto out;
124 }
Sebastian Ottdad572e32011-10-30 15:16:53 +0100125
Jingoo Han01787222013-07-22 10:18:15 +0900126 ret = kstrtoul(buf, 0, &value);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100127 if (ret)
128 goto out;
129
130 if (value == 1)
131 ret = ccwgroup_set_online(gdev);
132 else if (value == 0)
Alexandra Winterd2b59bd2021-09-21 16:52:17 +0200133 ret = ccwgroup_set_offline(gdev, true);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100134 else
135 ret = -EINVAL;
136out:
Sebastian Ottd5b877f2013-12-16 10:56:46 +0100137 device_unlock(dev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100138 return (ret == 0) ? count : ret;
139}
140
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100141static ssize_t ccwgroup_online_show(struct device *dev,
142 struct device_attribute *attr,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100143 char *buf)
144{
145 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
146 int online;
147
148 online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
149
150 return scnprintf(buf, PAGE_SIZE, "%d\n", online);
151}
152
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Provide an 'ungroup' attribute so the user can remove group devices no
Heiko Carstenscada9382023-06-28 16:23:20 +0200155 * longer needed or accidentally created. Saves memory :)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500157static void ccwgroup_ungroup(struct ccwgroup_device *gdev)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400158{
Cornelia Huckd76123e2007-04-27 16:01:37 +0200159 mutex_lock(&gdev->reg_mutex);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100160 if (device_is_registered(&gdev->dev)) {
161 __ccwgroup_remove_symlinks(gdev);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500162 device_unregister(&gdev->dev);
Cornelia Huck1a908c72008-01-26 14:10:50 +0100163 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200164 mutex_unlock(&gdev->reg_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400165}
166
Sebastian Ottdad572e32011-10-30 15:16:53 +0100167static ssize_t ccwgroup_ungroup_store(struct device *dev,
168 struct device_attribute *attr,
169 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100171 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
Sebastian Ott0310c8b2014-06-13 17:29:11 +0200172 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100174 /* Prevent concurrent online/offline processing and ungrouping. */
175 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
176 return -EAGAIN;
177 if (gdev->state != CCWGROUP_OFFLINE) {
178 rc = -EINVAL;
179 goto out;
180 }
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500181
182 if (device_remove_file_self(dev, attr))
183 ccwgroup_ungroup(gdev);
Sebastian Ott0310c8b2014-06-13 17:29:11 +0200184 else
185 rc = -ENODEV;
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100186out:
187 if (rc) {
Sebastian Ott0310c8b2014-06-13 17:29:11 +0200188 /* Release onoff "lock" when ungrouping failed. */
189 atomic_set(&gdev->onoff, 0);
Peter Oberparleiterc619d422008-12-25 13:39:04 +0100190 return rc;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return count;
193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100195static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
196
Julian Wiedmann05066312020-12-07 14:12:29 +0100197static struct attribute *ccwgroup_dev_attrs[] = {
Sebastian Ottdbdf1af2011-10-30 15:16:52 +0100198 &dev_attr_online.attr,
199 &dev_attr_ungroup.attr,
200 NULL,
201};
Julian Wiedmann05066312020-12-07 14:12:29 +0100202ATTRIBUTE_GROUPS(ccwgroup_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500204static void ccwgroup_ungroup_workfn(struct work_struct *work)
205{
206 struct ccwgroup_device *gdev =
207 container_of(work, struct ccwgroup_device, ungroup_work);
208
209 ccwgroup_ungroup(gdev);
Sebastian Ott9280ddb2014-06-13 17:02:24 +0200210 put_device(&gdev->dev);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500211}
212
Sebastian Ottdad572e32011-10-30 15:16:53 +0100213static void ccwgroup_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Julian Wiedmann197cec22021-04-24 12:12:38 +0200215 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
216 unsigned int i;
217
218 for (i = 0; i < gdev->count; i++) {
219 struct ccw_device *cdev = gdev->cdev[i];
220 unsigned long flags;
221
222 if (cdev) {
223 spin_lock_irqsave(cdev->ccwlock, flags);
224 if (dev_get_drvdata(&cdev->dev) == gdev)
225 dev_set_drvdata(&cdev->dev, NULL);
226 spin_unlock_irqrestore(cdev->ccwlock, flags);
227 put_device(&cdev->dev);
228 }
229 }
230
231 kfree(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Sebastian Ottdad572e32011-10-30 15:16:53 +0100234static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Heiko Carstens2685df62017-05-04 13:35:33 +0200236 char str[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int i, rc;
238
239 for (i = 0; i < gdev->count; i++) {
Sebastian Ottdad572e32011-10-30 15:16:53 +0100240 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
241 &gdev->dev.kobj, "group_device");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (rc) {
243 for (--i; i >= 0; i--)
244 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
245 "group_device");
246 return rc;
247 }
248 }
249 for (i = 0; i < gdev->count; i++) {
250 sprintf(str, "cdev%d", i);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100251 rc = sysfs_create_link(&gdev->dev.kobj,
252 &gdev->cdev[i]->dev.kobj, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (rc) {
254 for (--i; i >= 0; i--) {
255 sprintf(str, "cdev%d", i);
256 sysfs_remove_link(&gdev->dev.kobj, str);
257 }
258 for (i = 0; i < gdev->count; i++)
259 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
260 "group_device");
261 return rc;
262 }
263 }
264 return 0;
265}
266
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200267static int __get_next_id(const char **buf, struct ccw_dev_id *id)
Ursula Braun022b6602008-04-24 10:15:20 +0200268{
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200269 unsigned int cssid, ssid, devno;
270 int ret = 0, len;
Ursula Braun022b6602008-04-24 10:15:20 +0200271 char *start, *end;
272
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++;
284 }
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200285 if (len <= CCW_BUS_ID_SIZE) {
286 if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
287 ret = -EINVAL;
Ursula Braun022b6602008-04-24 10:15:20 +0200288 } else
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200289 ret = -EINVAL;
290
291 if (!ret) {
292 id->ssid = ssid;
293 id->devno = devno;
294 }
Ursula Braun022b6602008-04-24 10:15:20 +0200295 *buf = end;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200296 return ret;
Ursula Braun022b6602008-04-24 10:15:20 +0200297}
298
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200299/**
Sebastian Ottf2962da2012-05-15 17:49:12 +0200300 * ccwgroup_create_dev() - create and register a ccw group device
301 * @parent: parent device for the new device
Sebastian Ottf2962da2012-05-15 17:49:12 +0200302 * @gdrv: driver for the new group device
Ursula Braun022b6602008-04-24 10:15:20 +0200303 * @num_devices: number of slave devices
304 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200305 *
Sebastian Ottf2962da2012-05-15 17:49:12 +0200306 * Create and register a new ccw group device as a child of @parent. Slave
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200307 * devices are obtained from the list of bus ids given in @buf.
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200308 * Returns:
309 * %0 on success and an error code on failure.
310 * Context:
311 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Sebastian Ott9814fdf2012-05-15 18:03:46 +0200313int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
314 int num_devices, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct ccwgroup_device *gdev;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200317 struct ccw_dev_id dev_id;
Ursula Braun022b6602008-04-24 10:15:20 +0200318 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Julian Wiedmannab7efda2018-01-30 14:39:34 +0100320 if (num_devices < 1)
321 return -EINVAL;
322
Kees Cookacafe7e2018-05-08 13:45:50 -0700323 gdev = kzalloc(struct_size(gdev, cdev, num_devices), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!gdev)
325 return -ENOMEM;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200328 mutex_init(&gdev->reg_mutex);
329 mutex_lock(&gdev->reg_mutex);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500330 INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200331 gdev->count = num_devices;
332 gdev->dev.bus = &ccwgroup_bus_type;
Sebastian Ottf2962da2012-05-15 17:49:12 +0200333 gdev->dev.parent = parent;
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200334 gdev->dev.release = ccwgroup_release;
335 device_initialize(&gdev->dev);
336
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200337 for (i = 0; i < num_devices && buf; i++) {
338 rc = __get_next_id(&buf, &dev_id);
Ursula Braun022b6602008-04-24 10:15:20 +0200339 if (rc != 0)
340 goto error;
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200341 gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
Ursula Braun022b6602008-04-24 10:15:20 +0200342 /*
343 * All devices have to be of the same type in
344 * order to be grouped.
345 */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200346 if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
347 gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
348 gdev->cdev[i]->id.driver_info !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 gdev->cdev[0]->id.driver_info) {
350 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200351 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 /* Don't allow a device to belong to more than one group. */
Sebastian Ottc560d102010-05-26 23:27:07 +0200354 spin_lock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100355 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Sebastian Ottc560d102010-05-26 23:27:07 +0200356 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200358 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100360 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Sebastian Ottc560d102010-05-26 23:27:07 +0200361 spin_unlock_irq(gdev->cdev[i]->ccwlock);
Cornelia Huck17088222006-07-27 14:00:33 +0200362 }
Ursula Braun022b6602008-04-24 10:15:20 +0200363 /* Check for sufficient number of bus ids. */
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200364 if (i < num_devices) {
Ursula Braun022b6602008-04-24 10:15:20 +0200365 rc = -EINVAL;
366 goto error;
367 }
368 /* Check for trailing stuff. */
Vasily Gorbikea298e62019-09-17 20:04:04 +0200369 if (i == num_devices && buf && strlen(buf) > 0) {
Ursula Braun022b6602008-04-24 10:15:20 +0200370 rc = -EINVAL;
371 goto error;
372 }
Julian Wiedmannf9a5d702017-09-14 09:52:32 +0200373 /* Check if the devices are bound to the required ccw driver. */
Julian Wiedmannab7efda2018-01-30 14:39:34 +0100374 if (gdrv && gdrv->ccw_driver &&
Julian Wiedmannf9a5d702017-09-14 09:52:32 +0200375 gdev->cdev[0]->drv != gdrv->ccw_driver) {
376 rc = -EINVAL;
377 goto error;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200380 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
Sebastian Ottf2962da2012-05-15 17:49:12 +0200381
382 if (gdrv) {
383 gdev->dev.driver = &gdrv->driver;
384 rc = gdrv->setup ? gdrv->setup(gdev) : 0;
385 if (rc)
386 goto error;
387 }
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200388 rc = device_add(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200390 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 rc = __ccwgroup_create_symlinks(gdev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100392 if (rc) {
393 device_del(&gdev->dev);
394 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Sebastian Ottdad572e32011-10-30 15:16:53 +0100396 mutex_unlock(&gdev->reg_mutex);
397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398error:
Cornelia Huckd76123e2007-04-27 16:01:37 +0200399 mutex_unlock(&gdev->reg_mutex);
400 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return rc;
402}
Sebastian Ottf2962da2012-05-15 17:49:12 +0200403EXPORT_SYMBOL(ccwgroup_create_dev);
404
Sebastian Otte9090742009-03-26 15:24:15 +0100405static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
Sebastian Ottdad572e32011-10-30 15:16:53 +0100406 void *data)
407{
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500408 struct ccwgroup_device *gdev = to_ccwgroupdev(data);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100409
Julian Wiedmann17c0b862021-05-05 10:28:21 +0200410 if (action == BUS_NOTIFY_UNBOUND_DRIVER) {
Sebastian Ott9280ddb2014-06-13 17:02:24 +0200411 get_device(&gdev->dev);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500412 schedule_work(&gdev->ungroup_work);
Sebastian Ott9280ddb2014-06-13 17:02:24 +0200413 }
Sebastian Ottdad572e32011-10-30 15:16:53 +0100414
415 return NOTIFY_OK;
416}
Sebastian Otte9090742009-03-26 15:24:15 +0100417
418static struct notifier_block ccwgroup_nb = {
419 .notifier_call = ccwgroup_notifier
420};
421
422static int __init init_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Sebastian Otte9090742009-03-26 15:24:15 +0100424 int ret;
425
426 ret = bus_register(&ccwgroup_bus_type);
427 if (ret)
428 return ret;
429
430 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
431 if (ret)
432 bus_unregister(&ccwgroup_bus_type);
433
434 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Sebastian Otte9090742009-03-26 15:24:15 +0100437static void __exit cleanup_ccwgroup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Sebastian Otte9090742009-03-26 15:24:15 +0100439 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
440 bus_unregister(&ccwgroup_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443module_init(init_ccwgroup);
444module_exit(cleanup_ccwgroup);
445
446/************************** driver stuff ******************************/
447
Uwe Kleine-Königfc7a6202021-07-13 21:35:22 +0200448static void ccwgroup_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100450 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
451 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Sebastian Ott50f15482009-03-26 15:24:14 +0100453 if (gdrv->remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 gdrv->remove(gdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100457static void ccwgroup_shutdown(struct device *dev)
458{
Sebastian Ottdad572e32011-10-30 15:16:53 +0100459 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
460 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100461
Sebastian Ott50f15482009-03-26 15:24:14 +0100462 if (!dev->driver)
463 return;
Sebastian Ott50f15482009-03-26 15:24:14 +0100464 if (gdrv->shutdown)
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100465 gdrv->shutdown(gdev);
466}
467
Russell Kingf9ccf452006-01-05 14:42:09 +0000468static struct bus_type ccwgroup_bus_type = {
469 .name = "ccwgroup",
Julian Wiedmann05066312020-12-07 14:12:29 +0100470 .dev_groups = ccwgroup_dev_groups,
Russell Kingf9ccf452006-01-05 14:42:09 +0000471 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100472 .shutdown = ccwgroup_shutdown,
Russell Kingf9ccf452006-01-05 14:42:09 +0000473};
474
Sebastian Otta166c362017-02-15 11:45:07 +0100475bool dev_is_ccwgroup(struct device *dev)
476{
477 return dev->bus == &ccwgroup_bus_type;
478}
479EXPORT_SYMBOL(dev_is_ccwgroup);
480
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200481/**
482 * ccwgroup_driver_register() - register a ccw group driver
483 * @cdriver: driver to be registered
484 *
485 * This function is mainly a wrapper around driver_register().
486 */
487int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200490 cdriver->driver.bus = &ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 return driver_register(&cdriver->driver);
493}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100494EXPORT_SYMBOL(ccwgroup_driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200496/**
497 * ccwgroup_driver_unregister() - deregister a ccw group driver
498 * @cdriver: driver to be deregistered
499 *
500 * This function is mainly a wrapper around driver_unregister().
501 */
502void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 driver_unregister(&cdriver->driver);
505}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100506EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Julian Wiedmann346e4852018-09-14 13:47:41 +0200508/**
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200509 * ccwgroup_probe_ccwdev() - probe function for slave devices
510 * @cdev: ccw device to be probed
511 *
512 * This is a dummy probe function for ccw devices that are slave devices in
513 * a ccw group device.
514 * Returns:
515 * always %0
516 */
517int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 return 0;
520}
Sebastian Ottdad572e32011-10-30 15:16:53 +0100521EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200523/**
524 * ccwgroup_remove_ccwdev() - remove function for slave devices
525 * @cdev: ccw device to be removed
526 *
527 * This is a remove function for ccw devices that are slave devices in a ccw
528 * group device. It sets the ccw device offline and also deregisters the
529 * embedding ccw group device.
530 */
531void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct ccwgroup_device *gdev;
534
535 /* Ignore offlining errors, device is gone anyway. */
536 ccw_device_set_offline(cdev);
537 /* If one of its devices is gone, the whole group is done for. */
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100538 spin_lock_irq(cdev->ccwlock);
539 gdev = dev_get_drvdata(&cdev->dev);
540 if (!gdev) {
541 spin_unlock_irq(cdev->ccwlock);
542 return;
543 }
544 /* Get ccwgroup device reference for local processing. */
545 get_device(&gdev->dev);
546 spin_unlock_irq(cdev->ccwlock);
547 /* Unregister group device. */
Sebastian Ottfa73eb42014-06-13 18:11:37 +0200548 ccwgroup_ungroup(gdev);
Peter Oberparleiterc0301752011-01-05 12:48:13 +0100549 /* Release ccwgroup device reference for local processing. */
550 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
Sebastian Ottdad572e32011-10-30 15:16:53 +0100553MODULE_LICENSE("GPL");