blob: b29fe8d50baf25677d90b1aed9310abc11a4159f [file] [log] [blame]
Greg Kroah-Hartman724117b2017-11-14 18:38:02 +01001// SPDX-License-Identifier: GPL-1.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 2002, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +010010
11#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Paul Gortmakera00f7612016-10-30 16:37:24 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/spinlock.h>
17#include <linux/errno.h>
18#include <linux/err.h>
19#include <linux/slab.h>
20#include <linux/list.h>
21#include <linux/device.h>
22#include <linux/workqueue.h>
Sebastian Ott188561a2013-04-13 12:53:21 +020023#include <linux/delay.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010024#include <linux/timer.h>
Peter Oberparleiterde400d62011-10-30 15:16:04 +010025#include <linux/kernel_stat.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Halil Pasic37db8982019-03-26 12:41:09 +010027#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/ccwdev.h>
30#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080031#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020032#include <asm/cmb.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020033#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +020035#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010037#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "css.h"
39#include "device.h"
40#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010041#include "io_sch.h"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020042#include "blacklist.h"
Michael Ernstfd0457a2010-08-09 18:12:50 +020043#include "chsc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010045static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010046static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010047static int recovery_phase;
48static const unsigned long recovery_delay[] = { 3, 30, 300 };
49
Sebastian Ott0ad8f714a2013-04-13 13:06:27 +020050static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
51static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
52static struct bus_type ccw_bus_type;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/******************* bus type handling ***********************/
55
56/* The Linux driver model distinguishes between a bus type and
57 * the bus itself. Of course we only have one channel
58 * subsystem driver and one channel system per machine, but
59 * we still use the abstraction. T.R. says it's a good idea. */
60static int
61ccw_bus_match (struct device * dev, struct device_driver * drv)
62{
63 struct ccw_device *cdev = to_ccwdev(dev);
64 struct ccw_driver *cdrv = to_ccwdrv(drv);
65 const struct ccw_device_id *ids = cdrv->ids, *found;
66
67 if (!ids)
68 return 0;
69
70 found = ccw_device_id_match(ids, &cdev->id);
71 if (!found)
72 return 0;
73
74 cdev->id.driver_info = found->driver_info;
75
76 return 1;
77}
78
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020079/* Store modalias string delimited by prefix/suffix string into buffer with
80 * specified size. Return length of resulting string (excluding trailing '\0')
81 * even if string doesn't fit buffer (snprintf semantics). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020082static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020083 struct ccw_device_id *id, const char *suffix)
84{
85 int len;
86
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020087 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020088 if (len > size)
89 return len;
90 buf += len;
91 size -= len;
92
93 if (id->dev_type != 0)
94 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
95 id->dev_model, suffix);
96 else
97 len += snprintf(buf, size, "dtdm%s", suffix);
98
99 return len;
100}
101
102/* Set up environment variables for ccw device uevent. Return 0 on success,
103 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200107 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200108 int ret;
109 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200111 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200112 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200113 if (ret)
114 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200115
116 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200117 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200118 if (ret)
119 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200122 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200123 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200124 if (ret)
125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200127 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200128 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200129 if (ret)
130 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200131
132 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200133 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200134 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Cornelia Huck602b20f2008-01-26 14:10:39 +0100138static void io_subchannel_irq(struct subchannel *);
139static int io_subchannel_probe(struct subchannel *);
140static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100141static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200142static int io_subchannel_sch_event(struct subchannel *, int);
Cornelia Huck99611f82008-07-14 09:59:02 +0200143static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
144 int);
Kees Cook846d0c62017-10-16 16:43:25 -0700145static void recovery_func(struct timer_list *unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Cornelia Huckf08adc02008-07-14 09:59:03 +0200147static struct css_device_id io_subchannel_ids[] = {
148 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
149 { /* end of list */ },
150};
Cornelia Huckf08adc02008-07-14 09:59:03 +0200151
Cornelia Huck93a27592009-06-16 10:30:23 +0200152static int io_subchannel_prepare(struct subchannel *sch)
153{
154 struct ccw_device *cdev;
155 /*
156 * Don't allow suspend while a ccw device registration
157 * is still outstanding.
158 */
159 cdev = sch_get_cdev(sch);
160 if (cdev && !device_is_registered(&cdev->dev))
161 return -EAGAIN;
162 return 0;
163}
164
Sebastian Ottb4c70722010-02-26 22:37:27 +0100165static int io_subchannel_settle(void)
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200166{
Sebastian Ottb4c70722010-02-26 22:37:27 +0100167 int ret;
168
169 ret = wait_event_interruptible(ccw_device_init_wq,
170 atomic_read(&ccw_device_init_count) == 0);
171 if (ret)
172 return -EINTR;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100173 flush_workqueue(cio_work_q);
Sebastian Ottb4c70722010-02-26 22:37:27 +0100174 return 0;
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200175}
176
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200177static struct css_driver io_subchannel_driver = {
Sebastian Otte6aed122011-03-15 17:08:30 +0100178 .drv = {
179 .owner = THIS_MODULE,
180 .name = "io_subchannel",
181 },
Cornelia Huckf08adc02008-07-14 09:59:03 +0200182 .subchannel_type = io_subchannel_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200184 .sch_event = io_subchannel_sch_event,
185 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100186 .probe = io_subchannel_probe,
187 .remove = io_subchannel_remove,
188 .shutdown = io_subchannel_shutdown,
Cornelia Huck93a27592009-06-16 10:30:23 +0200189 .prepare = io_subchannel_prepare,
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200190 .settle = io_subchannel_settle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Sebastian Ott2f176442009-09-22 22:58:33 +0200193int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 int ret;
196
Kees Cook846d0c62017-10-16 16:43:25 -0700197 timer_setup(&recovery_timer, recovery_func, 0);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100198 ret = bus_register(&ccw_bus_type);
199 if (ret)
200 return ret;
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100201 ret = css_driver_register(&io_subchannel_driver);
202 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100203 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return ret;
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/************************ device handling **************************/
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400212devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct ccw_device *cdev = to_ccwdev(dev);
215 struct ccw_device_id *id = &(cdev->id);
216
217 if (id->dev_type != 0)
218 return sprintf(buf, "%04x/%02x\n",
219 id->dev_type, id->dev_model);
220 else
221 return sprintf(buf, "n/a\n");
222}
223
224static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400225cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct ccw_device *cdev = to_ccwdev(dev);
228 struct ccw_device_id *id = &(cdev->id);
229
230 return sprintf(buf, "%04x/%02x\n",
231 id->cu_type, id->cu_model);
232}
233
234static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800235modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
236{
237 struct ccw_device *cdev = to_ccwdev(dev);
238 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200239 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800240
Cornelia Huck086a6c62007-07-17 13:36:08 +0200241 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200242
243 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800244}
245
246static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400247online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct ccw_device *cdev = to_ccwdev(dev);
250
251 return sprintf(buf, cdev->online ? "1\n" : "0\n");
252}
253
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100254int ccw_device_is_orphan(struct ccw_device *cdev)
255{
256 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
257}
258
Cornelia Huckef995162007-04-27 16:01:39 +0200259static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100260{
Sebastian Ott7d253b92009-12-07 12:51:33 +0100261 if (device_is_registered(&cdev->dev)) {
Sebastian Ott24a18722009-12-07 12:51:34 +0100262 /* Undo device_add(). */
Cornelia Huckef995162007-04-27 16:01:39 +0200263 device_del(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +0100264 }
265 if (cdev->private->flags.initialized) {
266 cdev->private->flags.initialized = 0;
Sebastian Ott3b554a12009-09-11 10:28:26 +0200267 /* Release reference from device_initialize(). */
268 put_device(&cdev->dev);
269 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100270}
271
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100272static void io_subchannel_quiesce(struct subchannel *);
273
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200274/**
275 * ccw_device_set_offline() - disable a ccw device for I/O
276 * @cdev: target ccw device
277 *
278 * This function calls the driver's set_offline() function for @cdev, if
279 * given, and then disables @cdev.
280 * Returns:
281 * %0 on success and a negative error value on failure.
282 * Context:
283 * enabled, ccw device lock not held
284 */
285int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100287 struct subchannel *sch;
288 int ret, state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (!cdev)
291 return -ENODEV;
292 if (!cdev->online || !cdev->drv)
293 return -EINVAL;
294
295 if (cdev->drv->set_offline) {
296 ret = cdev->drv->set_offline(cdev);
297 if (ret != 0)
298 return ret;
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 spin_lock_irq(cdev->ccwlock);
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100301 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott74bd0d82013-12-16 10:51:54 +0100302 cdev->online = 0;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200303 /* Wait until a final state or DISCONNECTED is reached */
304 while (!dev_fsm_final_state(cdev) &&
305 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200307 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
308 cdev->private->state == DEV_STATE_DISCONNECTED));
309 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100311 do {
312 ret = ccw_device_offline(cdev);
313 if (!ret)
314 break;
315 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
316 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
317 cdev->private->dev_id.devno);
318 if (ret != -EBUSY)
319 goto error;
320 state = cdev->private->state;
321 spin_unlock_irq(cdev->ccwlock);
322 io_subchannel_quiesce(sch);
323 spin_lock_irq(cdev->ccwlock);
324 cdev->private->state = state;
325 } while (ret == -EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200327 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
328 cdev->private->state == DEV_STATE_DISCONNECTED));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100329 /* Inform the user if set offline failed. */
330 if (cdev->private->state == DEV_STATE_BOXED) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800331 pr_warn("%s: The device entered boxed state while being set offline\n",
332 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100333 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800334 pr_warn("%s: The device stopped operating while being set offline\n",
335 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100336 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200337 /* Give up reference from ccw_device_set_online(). */
338 put_device(&cdev->dev);
339 return 0;
340
341error:
Michael Ernst217ee6c2009-09-11 10:28:21 +0200342 cdev->private->state = DEV_STATE_OFFLINE;
343 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
344 spin_unlock_irq(cdev->ccwlock);
345 /* Give up reference from ccw_device_set_online(). */
346 put_device(&cdev->dev);
347 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200350/**
351 * ccw_device_set_online() - enable a ccw device for I/O
352 * @cdev: target ccw device
353 *
354 * This function first enables @cdev and then calls the driver's set_online()
355 * function for @cdev, if given. If set_online() returns an error, @cdev is
356 * disabled again.
357 * Returns:
358 * %0 on success and a negative error value on failure.
359 * Context:
360 * enabled, ccw device lock not held
361 */
362int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200365 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (!cdev)
368 return -ENODEV;
369 if (cdev->online || !cdev->drv)
370 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100371 /* Hold on to an extra reference while device is online. */
372 if (!get_device(&cdev->dev))
373 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 spin_lock_irq(cdev->ccwlock);
376 ret = ccw_device_online(cdev);
377 spin_unlock_irq(cdev->ccwlock);
378 if (ret == 0)
379 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
380 else {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200381 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200382 "device 0.%x.%04x\n",
383 ret, cdev->private->dev_id.ssid,
384 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100385 /* Give up online reference since onlining failed. */
386 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return ret;
388 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200389 spin_lock_irq(cdev->ccwlock);
390 /* Check if online processing was successful */
391 if ((cdev->private->state != DEV_STATE_ONLINE) &&
392 (cdev->private->state != DEV_STATE_W4SENSE)) {
393 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100394 /* Inform the user that set online failed. */
395 if (cdev->private->state == DEV_STATE_BOXED) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800396 pr_warn("%s: Setting the device online failed because it is boxed\n",
397 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100398 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800399 pr_warn("%s: Setting the device online failed because it is not operational\n",
400 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100401 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100402 /* Give up online reference since onlining failed. */
403 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200407 if (cdev->drv->set_online)
408 ret = cdev->drv->set_online(cdev);
409 if (ret)
410 goto rollback;
Sebastian Ott74bd0d82013-12-16 10:51:54 +0100411
412 spin_lock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200413 cdev->online = 1;
Sebastian Ott74bd0d82013-12-16 10:51:54 +0100414 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200415 return 0;
416
417rollback:
418 spin_lock_irq(cdev->ccwlock);
419 /* Wait until a final state or DISCONNECTED is reached */
420 while (!dev_fsm_final_state(cdev) &&
421 cdev->private->state != DEV_STATE_DISCONNECTED) {
422 spin_unlock_irq(cdev->ccwlock);
423 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
424 cdev->private->state == DEV_STATE_DISCONNECTED));
425 spin_lock_irq(cdev->ccwlock);
426 }
427 ret2 = ccw_device_offline(cdev);
428 if (ret2)
429 goto error;
430 spin_unlock_irq(cdev->ccwlock);
431 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
432 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100433 /* Give up online reference since onlining failed. */
434 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200435 return ret;
436
437error:
438 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
439 "device 0.%x.%04x\n",
440 ret2, cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
442 cdev->private->state = DEV_STATE_OFFLINE;
443 spin_unlock_irq(cdev->ccwlock);
444 /* Give up online reference since onlining failed. */
445 put_device(&cdev->dev);
446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100449static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200450{
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100451 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
452 spin_lock_irq(cdev->ccwlock);
453 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
454 spin_unlock_irq(cdev->ccwlock);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200455 return 0;
456 }
457 if (cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100458 return ccw_device_set_offline(cdev);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200459 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200460}
461
462static int online_store_recog_and_online(struct ccw_device *cdev)
463{
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200464 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200465 if (cdev->private->state == DEV_STATE_BOXED) {
Peter Oberparleiter1f5bd38482009-12-07 12:51:23 +0100466 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100467 ccw_device_recognition(cdev);
Peter Oberparleiter1f5bd38482009-12-07 12:51:23 +0100468 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200469 wait_event(cdev->private->wait_q,
470 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200471 if (cdev->private->state != DEV_STATE_OFFLINE)
472 /* recognition failed */
473 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200474 }
475 if (cdev->drv && cdev->drv->set_online)
Sebastian Ott7cd40312010-08-09 18:12:52 +0200476 return ccw_device_set_online(cdev);
477 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200478}
Sebastian Ott156013f2009-03-31 19:16:03 +0200479
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200480static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200481{
482 int ret;
483
484 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200485 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200486 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200487 if (force && cdev->private->state == DEV_STATE_BOXED) {
488 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200489 if (ret)
490 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200491 if (cdev->id.cu_type == 0)
492 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200493 ret = online_store_recog_and_online(cdev);
494 if (ret)
495 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200496 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200497 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200498}
499
500static ssize_t online_store (struct device *dev, struct device_attribute *attr,
501 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200504 int force, ret;
505 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200507 /* Prevent conflict between multiple on-/offline processing requests. */
Peter Oberparleiter350e9122009-12-07 12:51:28 +0100508 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return -EAGAIN;
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200510 /* Prevent conflict between internal I/Os and on-/offline processing. */
511 if (!dev_fsm_final_state(cdev) &&
512 cdev->private->state != DEV_STATE_DISCONNECTED) {
513 ret = -EAGAIN;
Sebastian Ott00381ee2013-12-16 10:54:13 +0100514 goto out;
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200515 }
516 /* Prevent conflict between pending work and on-/offline processing.*/
517 if (work_pending(&cdev->private->todo_work)) {
518 ret = -EAGAIN;
Sebastian Ott00381ee2013-12-16 10:54:13 +0100519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 if (!strncmp(buf, "force\n", count)) {
522 force = 1;
523 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200524 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else {
526 force = 0;
Jingoo Han01787222013-07-22 10:18:15 +0900527 ret = kstrtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200529 if (ret)
530 goto out;
Sebastian Ott00381ee2013-12-16 10:54:13 +0100531
532 device_lock(dev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200533 switch (i) {
534 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100535 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200536 break;
537 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200538 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200539 break;
540 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200541 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Sebastian Ott00381ee2013-12-16 10:54:13 +0100543 device_unlock(dev);
544
Cornelia Huck2f972202008-04-30 13:38:33 +0200545out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100547 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400551available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct ccw_device *cdev = to_ccwdev(dev);
554 struct subchannel *sch;
555
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100556 if (ccw_device_is_orphan(cdev))
557 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 switch (cdev->private->state) {
559 case DEV_STATE_BOXED:
560 return sprintf(buf, "boxed\n");
561 case DEV_STATE_DISCONNECTED:
562 case DEV_STATE_DISCONNECTED_SENSE_ID:
563 case DEV_STATE_NOT_OPER:
564 sch = to_subchannel(dev->parent);
565 if (!sch->lpm)
566 return sprintf(buf, "no path\n");
567 else
568 return sprintf(buf, "no device\n");
569 default:
570 /* All other states considered fine. */
571 return sprintf(buf, "good\n");
572 }
573}
574
Michael Ernstfd0457a2010-08-09 18:12:50 +0200575static ssize_t
576initiate_logging(struct device *dev, struct device_attribute *attr,
577 const char *buf, size_t count)
578{
579 struct subchannel *sch = to_subchannel(dev);
580 int rc;
581
582 rc = chsc_siosl(sch->schid);
583 if (rc < 0) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800584 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
585 sch->schid.ssid, sch->schid.sch_no, rc);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200586 return rc;
587 }
588 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
589 sch->schid.ssid, sch->schid.sch_no);
590 return count;
591}
592
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100593static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
594 char *buf)
595{
596 struct subchannel *sch = to_subchannel(dev);
597
598 return sprintf(buf, "%02x\n", sch->vpm);
599}
600
Joe Perchesc828a892017-12-19 10:15:08 -0800601static DEVICE_ATTR_RO(devtype);
602static DEVICE_ATTR_RO(cutype);
603static DEVICE_ATTR_RO(modalias);
Joe Perchesb6b996b2017-12-19 10:15:07 -0800604static DEVICE_ATTR_RW(online);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605static DEVICE_ATTR(availability, 0444, available_show, NULL);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200606static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
Joe Perchesc828a892017-12-19 10:15:08 -0800607static DEVICE_ATTR_RO(vpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200609static struct attribute *io_subchannel_attrs[] = {
Michael Ernstfd0457a2010-08-09 18:12:50 +0200610 &dev_attr_logging.attr,
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100611 &dev_attr_vpm.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 NULL,
613};
614
Arvind Yadavf460d112017-07-19 12:39:13 +0530615static const struct attribute_group io_subchannel_attr_group = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200616 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100617};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619static struct attribute * ccwdev_attrs[] = {
620 &dev_attr_devtype.attr,
621 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800622 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 &dev_attr_online.attr,
624 &dev_attr_cmb_enable.attr,
625 &dev_attr_availability.attr,
626 NULL,
627};
628
Arvind Yadavf460d112017-07-19 12:39:13 +0530629static const struct attribute_group ccwdev_attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .attrs = ccwdev_attrs,
631};
632
David Brownella4dbd672009-06-24 10:06:31 -0700633static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200634 &ccwdev_attr_group,
635 NULL,
636};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200638static int ccw_device_add(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct device *dev = &cdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 dev->bus = &ccw_bus_type;
Sebastian Ott7d253b92009-12-07 12:51:33 +0100643 return device_add(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +0100646static int match_dev_id(struct device *dev, const void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700647{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100648 struct ccw_device *cdev = to_ccwdev(dev);
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +0100649 struct ccw_dev_id *dev_id = (void *)data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700650
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100651 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
652}
653
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200654/**
655 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
656 * @dev_id: id of the device to be searched
657 *
658 * This function searches all devices attached to the ccw bus for a device
659 * matching @dev_id.
660 * Returns:
661 * If a device is found its reference count is increased and returned;
662 * else %NULL is returned.
663 */
664struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100665{
666 struct device *dev;
667
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100668 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100669
670 return dev ? to_ccwdev(dev) : NULL;
671}
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200672EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100673
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100674static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100676 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Sebastian Ott7d253b92009-12-07 12:51:33 +0100678 if (device_is_registered(&cdev->dev)) {
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100679 device_release_driver(&cdev->dev);
680 ret = device_attach(&cdev->dev);
681 WARN_ON(ret == -ENODEV);
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685static void
686ccw_device_release(struct device *dev)
687{
688 struct ccw_device *cdev;
689
690 cdev = to_ccwdev(dev);
Halil Pasic37db8982019-03-26 12:41:09 +0100691 cio_gp_dma_free(cdev->private->dma_pool, cdev->private->dma_area,
692 sizeof(*cdev->private->dma_area));
693 cio_gp_dma_destroy(cdev->private->dma_pool, &cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100694 /* Release reference of parent subchannel. */
695 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 kfree(cdev->private);
697 kfree(cdev);
698}
699
Cornelia Huck7674da72006-12-08 15:54:21 +0100700static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
701{
702 struct ccw_device *cdev;
Halil Pasic37db8982019-03-26 12:41:09 +0100703 struct gen_pool *dma_pool;
Cornelia Huck7674da72006-12-08 15:54:21 +0100704
705 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
Halil Pasic37db8982019-03-26 12:41:09 +0100706 if (!cdev)
707 goto err_cdev;
708 cdev->private = kzalloc(sizeof(struct ccw_device_private),
709 GFP_KERNEL | GFP_DMA);
710 if (!cdev->private)
711 goto err_priv;
712 cdev->dev.coherent_dma_mask = sch->dev.coherent_dma_mask;
Halil Pasic05668e12019-09-30 17:38:02 +0200713 cdev->dev.dma_mask = sch->dev.dma_mask;
Halil Pasic37db8982019-03-26 12:41:09 +0100714 dma_pool = cio_gp_dma_create(&cdev->dev, 1);
715 if (!dma_pool)
716 goto err_dma_pool;
717 cdev->private->dma_pool = dma_pool;
718 cdev->private->dma_area = cio_gp_dma_zalloc(dma_pool, &cdev->dev,
719 sizeof(*cdev->private->dma_area));
720 if (!cdev->private->dma_area)
721 goto err_dma_area;
722 return cdev;
723err_dma_area:
724 cio_gp_dma_destroy(dma_pool, &cdev->dev);
725err_dma_pool:
726 kfree(cdev->private);
727err_priv:
Cornelia Huck7674da72006-12-08 15:54:21 +0100728 kfree(cdev);
Halil Pasic37db8982019-03-26 12:41:09 +0100729err_cdev:
Cornelia Huck7674da72006-12-08 15:54:21 +0100730 return ERR_PTR(-ENOMEM);
731}
732
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100733static void ccw_device_todo(struct work_struct *work);
734
Cornelia Huck7674da72006-12-08 15:54:21 +0100735static int io_subchannel_initialize_dev(struct subchannel *sch,
736 struct ccw_device *cdev)
737{
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200738 struct ccw_device_private *priv = cdev->private;
739 int ret;
740
741 priv->cdev = cdev;
742 priv->int_class = IRQIO_CIO;
743 priv->state = DEV_STATE_NOT_OPER;
744 priv->dev_id.devno = sch->schib.pmcw.dev;
745 priv->dev_id.ssid = sch->schid.ssid;
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200746
747 INIT_WORK(&priv->todo_work, ccw_device_todo);
748 INIT_LIST_HEAD(&priv->cmb_list);
749 init_waitqueue_head(&priv->wait_q);
Kees Cook846d0c62017-10-16 16:43:25 -0700750 timer_setup(&priv->timer, ccw_device_timeout, 0);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200751
752 atomic_set(&priv->onoff, 0);
753 cdev->ccwlock = sch->lock;
Cornelia Huck7674da72006-12-08 15:54:21 +0100754 cdev->dev.parent = &sch->dev;
755 cdev->dev.release = ccw_device_release;
Cornelia Huckef995162007-04-27 16:01:39 +0200756 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100757 /* Do first half of device_register. */
758 device_initialize(&cdev->dev);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200759 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
760 cdev->private->dev_id.devno);
761 if (ret)
762 goto out_put;
Cornelia Huck7674da72006-12-08 15:54:21 +0100763 if (!get_device(&sch->dev)) {
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200764 ret = -ENODEV;
765 goto out_put;
Cornelia Huck7674da72006-12-08 15:54:21 +0100766 }
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200767 priv->flags.initialized = 1;
768 spin_lock_irq(sch->lock);
769 sch_set_cdev(sch, cdev);
770 spin_unlock_irq(sch->lock);
Cornelia Huck7674da72006-12-08 15:54:21 +0100771 return 0;
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200772
773out_put:
774 /* Release reference from device_initialize(). */
775 put_device(&cdev->dev);
776 return ret;
Cornelia Huck7674da72006-12-08 15:54:21 +0100777}
778
779static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
780{
781 struct ccw_device *cdev;
782 int ret;
783
784 cdev = io_subchannel_allocate_dev(sch);
785 if (!IS_ERR(cdev)) {
786 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200787 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100788 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100789 }
790 return cdev;
791}
792
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100793static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100794
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100795static void sch_create_and_recog_new_device(struct subchannel *sch)
796{
797 struct ccw_device *cdev;
798
799 /* Need to allocate a new ccw device. */
800 cdev = io_subchannel_create_ccwdev(sch);
801 if (IS_ERR(cdev)) {
802 /* OK, we did everything we could... */
803 css_sch_device_unregister(sch);
804 return;
805 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100806 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100807 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/*
811 * Register recognized device.
812 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100813static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 struct subchannel *sch;
Sebastian Otta2901562010-03-08 12:25:17 +0100816 int ret, adjust_init_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 unsigned long flags;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100820 /*
821 * Check if subchannel is still registered. It may have become
822 * unregistered if a machine check hit us after finishing
823 * device recognition but before the register work could be
824 * queued.
825 */
826 if (!device_is_registered(&sch->dev))
827 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200828 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100829 /*
830 * io_subchannel_register() will also be called after device
831 * recognition has been done for a boxed device (which will already
832 * be registered). We need to reprobe since we may now have sense id
833 * information.
834 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100835 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100836 if (!cdev->drv) {
837 ret = device_reprobe(&cdev->dev);
838 if (ret)
839 /* We can't do much here. */
Michael Ernst139b83dd2008-05-07 09:22:54 +0200840 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200841 " %d for 0.%x.%04x\n", ret,
842 cdev->private->dev_id.ssid,
843 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100844 }
Sebastian Otta2901562010-03-08 12:25:17 +0100845 adjust_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto out;
847 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700848 /*
849 * Now we know this subchannel will stay, we can throw
850 * our delayed uevent.
851 */
Cornelia Huck05ce3e52020-03-27 13:45:02 +0100852 if (dev_get_uevent_suppress(&sch->dev)) {
853 dev_set_uevent_suppress(&sch->dev, 0);
854 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* make it known to the system */
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200857 ret = ccw_device_add(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200859 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
860 cdev->private->dev_id.ssid,
861 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100862 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100863 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100864 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100865 /* Release initial device reference. */
866 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100867 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869out:
870 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100872out_err:
Sebastian Otta2901562010-03-08 12:25:17 +0100873 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 wake_up(&ccw_device_init_wq);
875}
876
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100877static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct subchannel *sch;
880
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200881 /* Get subchannel reference for local processing. */
882 if (!get_device(cdev->dev.parent))
883 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200885 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200886 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 put_device(&sch->dev);
888}
889
890/*
891 * subchannel recognition done. Called from the state machine.
892 */
893void
894io_subchannel_recog_done(struct ccw_device *cdev)
895{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (css_init_done == 0) {
897 cdev->private->flags.recog_done = 1;
898 return;
899 }
900 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200901 case DEV_STATE_BOXED:
902 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 case DEV_STATE_NOT_OPER:
904 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100905 /* Remove device found not operational. */
906 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (atomic_dec_and_test(&ccw_device_init_count))
908 wake_up(&ccw_device_init_wq);
909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 case DEV_STATE_OFFLINE:
Halil Pasic37db8982019-03-26 12:41:09 +0100911 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * We can't register the device in interrupt context so
913 * we schedule a work item.
914 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100915 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break;
917 }
918}
919
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100920static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /* Increase counter of devices currently in recognition. */
923 atomic_inc(&ccw_device_init_count);
924
925 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100926 spin_lock_irq(sch->lock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100927 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100928 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100931static int ccw_device_move_to_sch(struct ccw_device *cdev,
932 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100933{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100934 struct subchannel *old_sch;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100935 int rc, old_enabled = 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100936
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100937 old_sch = to_subchannel(cdev->dev.parent);
938 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100939 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100940 return -ENODEV;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100941
942 if (!sch_is_pseudo_sch(old_sch)) {
943 spin_lock_irq(old_sch->lock);
944 old_enabled = old_sch->schib.pmcw.ena;
945 rc = 0;
946 if (old_enabled)
947 rc = cio_disable_subchannel(old_sch);
948 spin_unlock_irq(old_sch->lock);
949 if (rc == -EBUSY) {
950 /* Release child reference for new parent. */
951 put_device(&sch->dev);
952 return rc;
953 }
954 }
955
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100956 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100957 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100958 mutex_unlock(&sch->reg_mutex);
959 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100960 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100961 cdev->private->dev_id.ssid,
962 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100963 sch->schib.pmcw.dev, rc);
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100964 if (old_enabled) {
965 /* Try to reenable the old subchannel. */
966 spin_lock_irq(old_sch->lock);
967 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
968 spin_unlock_irq(old_sch->lock);
969 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100970 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100971 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100972 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100973 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100974 /* Clean up old subchannel. */
975 if (!sch_is_pseudo_sch(old_sch)) {
976 spin_lock_irq(old_sch->lock);
977 sch_set_cdev(old_sch, NULL);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100978 spin_unlock_irq(old_sch->lock);
979 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100980 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100981 /* Release child reference for old parent. */
982 put_device(&old_sch->dev);
983 /* Initialize new subchannel. */
984 spin_lock_irq(sch->lock);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100985 cdev->ccwlock = sch->lock;
986 if (!sch_is_pseudo_sch(sch))
987 sch_set_cdev(sch, cdev);
988 spin_unlock_irq(sch->lock);
989 if (!sch_is_pseudo_sch(sch))
990 css_update_ssd_info(sch);
991 return 0;
992}
993
994static int ccw_device_move_to_orph(struct ccw_device *cdev)
995{
996 struct subchannel *sch = to_subchannel(cdev->dev.parent);
997 struct channel_subsystem *css = to_css(sch->dev.parent);
998
999 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001000}
1001
Cornelia Huck602b20f2008-01-26 14:10:39 +01001002static void io_subchannel_irq(struct subchannel *sch)
1003{
1004 struct ccw_device *cdev;
1005
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001006 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001007
Sebastian Ottefd986d2009-09-11 10:28:18 +02001008 CIO_TRACE_EVENT(6, "IRQ");
1009 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +01001010 if (cdev)
1011 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
Peter Oberparleiterde400d62011-10-30 15:16:04 +01001012 else
Heiko Carstens420f42e2013-01-02 15:18:18 +01001013 inc_irq_stat(IRQIO_CIO);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001014}
1015
Sebastian Ott13952ec2008-12-25 13:39:13 +01001016void io_subchannel_init_config(struct subchannel *sch)
1017{
1018 memset(&sch->config, 0, sizeof(sch->config));
1019 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001020}
1021
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001022static void io_subchannel_init_fields(struct subchannel *sch)
1023{
1024 if (cio_is_console(sch->schid))
1025 sch->opm = 0xff;
1026 else
1027 sch->opm = chp_get_sch_opm(sch);
1028 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001029 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001030
1031 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1032 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1033 sch->schib.pmcw.dev, sch->schid.ssid,
1034 sch->schid.sch_no, sch->schib.pmcw.pim,
1035 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001036
1037 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001038}
1039
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001040/*
1041 * Note: We always return 0 so that we bind to the device even on error.
1042 * This is needed so that our remove function is called on unregister.
1043 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001044static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001046 struct io_subchannel_private *io_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct ccw_device *cdev;
1048 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001050 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001051 rc = sysfs_create_group(&sch->dev.kobj,
1052 &io_subchannel_attr_group);
1053 if (rc)
1054 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1055 "attributes for subchannel "
1056 "0.%x.%04x (rc=%d)\n",
1057 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001059 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001060 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001061 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Cornelia Huck05ce3e52020-03-27 13:45:02 +01001063 if (dev_get_uevent_suppress(&sch->dev)) {
1064 /* should always be the case for the console */
1065 dev_set_uevent_suppress(&sch->dev, 0);
1066 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1067 }
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001068 cdev = sch_get_cdev(sch);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +02001069 rc = ccw_device_add(cdev);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001070 if (rc) {
1071 /* Release online reference. */
1072 put_device(&cdev->dev);
1073 goto out_schedule;
1074 }
Sebastian Ott0ad8f714a2013-04-13 13:06:27 +02001075 if (atomic_dec_and_test(&ccw_device_init_count))
1076 wake_up(&ccw_device_init_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 0;
1078 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001079 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001080 rc = cio_commit_config(sch);
1081 if (rc)
1082 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001083 rc = sysfs_create_group(&sch->dev.kobj,
1084 &io_subchannel_attr_group);
1085 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001086 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001087 /* Allocate I/O subchannel private data. */
Sebastian Ottf92519e2011-03-15 17:08:27 +01001088 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1089 if (!io_priv)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001090 goto out_schedule;
Sebastian Ottf92519e2011-03-15 17:08:27 +01001091
Halil Pasic37db8982019-03-26 12:41:09 +01001092 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1093 sizeof(*io_priv->dma_area),
1094 &io_priv->dma_area_dma, GFP_KERNEL);
1095 if (!io_priv->dma_area) {
1096 kfree(io_priv);
1097 goto out_schedule;
1098 }
1099
Sebastian Ottf92519e2011-03-15 17:08:27 +01001100 set_io_private(sch, io_priv);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001101 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001102 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001103
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001104out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001105 spin_lock_irq(sch->lock);
1106 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1107 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Sebastian Ott135a8b42018-03-15 15:03:43 +01001111static int io_subchannel_remove(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001113 struct io_subchannel_private *io_priv = to_io_private(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001116 cdev = sch_get_cdev(sch);
1117 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001118 goto out_free;
Sebastian Ott135a8b42018-03-15 15:03:43 +01001119
1120 ccw_device_unregister(cdev);
1121 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001122 sch_set_cdev(sch, NULL);
Sebastian Ottf92519e2011-03-15 17:08:27 +01001123 set_io_private(sch, NULL);
Sebastian Ott135a8b42018-03-15 15:03:43 +01001124 spin_unlock_irq(sch->lock);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001125out_free:
Halil Pasic37db8982019-03-26 12:41:09 +01001126 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1127 io_priv->dma_area, io_priv->dma_area_dma);
Sebastian Ottf92519e2011-03-15 17:08:27 +01001128 kfree(io_priv);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001129 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return 0;
1131}
1132
Cornelia Huck602b20f2008-01-26 14:10:39 +01001133static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 struct ccw_device *cdev;
1136
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001137 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (cdev)
1139 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1140}
1141
Cornelia Huckc820de32008-07-14 09:58:45 +02001142static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 struct ccw_device *cdev;
1145
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001146 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!cdev)
1148 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001149 if (cio_update_schib(sch))
1150 goto err;
1151 /* Check for I/O on path. */
1152 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1153 goto out;
1154 if (cdev->private->state == DEV_STATE_ONLINE) {
1155 ccw_device_kill_io(cdev);
1156 goto out;
1157 }
1158 if (cio_clear(sch))
1159 goto err;
1160out:
1161 /* Trigger path verification. */
1162 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1163 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001164
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001165err:
1166 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001167}
1168
Cornelia Huck99611f82008-07-14 09:59:02 +02001169static int io_subchannel_chp_event(struct subchannel *sch,
1170 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001171{
Sebastian Ott585b9542010-10-25 16:10:34 +02001172 struct ccw_device *cdev = sch_get_cdev(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001173 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001174
Cornelia Huck99611f82008-07-14 09:59:02 +02001175 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001176 if (!mask)
1177 return 0;
1178 switch (event) {
1179 case CHP_VARY_OFF:
1180 sch->opm &= ~mask;
1181 sch->lpm &= ~mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001182 if (cdev)
1183 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001184 io_subchannel_terminate_path(sch, mask);
1185 break;
1186 case CHP_VARY_ON:
1187 sch->opm |= mask;
1188 sch->lpm |= mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001189 if (cdev)
1190 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001191 io_subchannel_verify(sch);
1192 break;
1193 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001194 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001195 return -ENODEV;
Sebastian Ott585b9542010-10-25 16:10:34 +02001196 if (cdev)
1197 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001198 io_subchannel_terminate_path(sch, mask);
1199 break;
1200 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001201 if (cio_update_schib(sch))
1202 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001203 sch->lpm |= mask & sch->opm;
Sebastian Ott585b9542010-10-25 16:10:34 +02001204 if (cdev)
1205 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001206 io_subchannel_verify(sch);
1207 break;
1208 }
1209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001212static void io_subchannel_quiesce(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 struct ccw_device *cdev;
1215 int ret;
1216
Sebastian Ott56e6b792009-12-07 12:51:35 +01001217 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001218 cdev = sch_get_cdev(sch);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001219 if (cio_is_console(sch->schid))
Sebastian Ott56e6b792009-12-07 12:51:35 +01001220 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (!sch->schib.pmcw.ena)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001222 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 ret = cio_disable_subchannel(sch);
1224 if (ret != -EBUSY)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001225 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (cdev->handler)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001227 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1228 while (ret == -EBUSY) {
1229 cdev->private->state = DEV_STATE_QUIESCE;
Peter Oberparleiter376ae472010-10-25 16:10:44 +02001230 cdev->private->iretry = 255;
Sebastian Ott56e6b792009-12-07 12:51:35 +01001231 ret = ccw_device_cancel_halt_clear(cdev);
1232 if (ret == -EBUSY) {
1233 ccw_device_set_timeout(cdev, HZ/10);
1234 spin_unlock_irq(sch->lock);
1235 wait_event(cdev->private->wait_q,
1236 cdev->private->state != DEV_STATE_QUIESCE);
1237 spin_lock_irq(sch->lock);
1238 }
1239 ret = cio_disable_subchannel(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
Sebastian Ott56e6b792009-12-07 12:51:35 +01001241out_unlock:
1242 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001245static void io_subchannel_shutdown(struct subchannel *sch)
1246{
1247 io_subchannel_quiesce(sch);
1248}
1249
Cornelia Huckc820de32008-07-14 09:58:45 +02001250static int device_is_disconnected(struct ccw_device *cdev)
1251{
1252 if (!cdev)
1253 return 0;
1254 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1255 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1256}
1257
1258static int recovery_check(struct device *dev, void *data)
1259{
1260 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott55fb7342017-09-14 13:55:22 +02001261 struct subchannel *sch;
Cornelia Huckc820de32008-07-14 09:58:45 +02001262 int *redo = data;
1263
1264 spin_lock_irq(cdev->ccwlock);
1265 switch (cdev->private->state) {
Sebastian Ott55fb7342017-09-14 13:55:22 +02001266 case DEV_STATE_ONLINE:
1267 sch = to_subchannel(cdev->dev.parent);
1268 if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1269 break;
Joe Perchesb09fcec2020-03-10 13:39:50 -07001270 fallthrough;
Cornelia Huckc820de32008-07-14 09:58:45 +02001271 case DEV_STATE_DISCONNECTED:
1272 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1273 cdev->private->dev_id.ssid,
1274 cdev->private->dev_id.devno);
1275 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1276 *redo = 1;
1277 break;
1278 case DEV_STATE_DISCONNECTED_SENSE_ID:
1279 *redo = 1;
1280 break;
1281 }
1282 spin_unlock_irq(cdev->ccwlock);
1283
1284 return 0;
1285}
1286
1287static void recovery_work_func(struct work_struct *unused)
1288{
1289 int redo = 0;
1290
1291 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1292 if (redo) {
1293 spin_lock_irq(&recovery_lock);
1294 if (!timer_pending(&recovery_timer)) {
1295 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1296 recovery_phase++;
1297 mod_timer(&recovery_timer, jiffies +
1298 recovery_delay[recovery_phase] * HZ);
1299 }
1300 spin_unlock_irq(&recovery_lock);
1301 } else
Sebastian Ott55fb7342017-09-14 13:55:22 +02001302 CIO_MSG_EVENT(3, "recovery: end\n");
Cornelia Huckc820de32008-07-14 09:58:45 +02001303}
1304
1305static DECLARE_WORK(recovery_work, recovery_work_func);
1306
Kees Cook846d0c62017-10-16 16:43:25 -07001307static void recovery_func(struct timer_list *unused)
Cornelia Huckc820de32008-07-14 09:58:45 +02001308{
1309 /*
1310 * We can't do our recovery in softirq context and it's not
1311 * performance critical, so we schedule it.
1312 */
1313 schedule_work(&recovery_work);
1314}
1315
Sebastian Ott55fb7342017-09-14 13:55:22 +02001316void ccw_device_schedule_recovery(void)
Cornelia Huckc820de32008-07-14 09:58:45 +02001317{
1318 unsigned long flags;
1319
Sebastian Ott55fb7342017-09-14 13:55:22 +02001320 CIO_MSG_EVENT(3, "recovery: schedule\n");
Cornelia Huckc820de32008-07-14 09:58:45 +02001321 spin_lock_irqsave(&recovery_lock, flags);
1322 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1323 recovery_phase = 0;
1324 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1325 }
1326 spin_unlock_irqrestore(&recovery_lock, flags);
1327}
1328
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001329static int purge_fn(struct device *dev, void *data)
1330{
1331 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001332 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001333
1334 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001335 if (is_blacklisted(id->ssid, id->devno) &&
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001336 (cdev->private->state == DEV_STATE_OFFLINE) &&
1337 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001338 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1339 id->devno);
1340 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001341 atomic_set(&cdev->private->onoff, 0);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001342 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001343 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001344 /* Abort loop in case of pending signal. */
1345 if (signal_pending(current))
1346 return -EINTR;
1347
1348 return 0;
1349}
1350
1351/**
1352 * ccw_purge_blacklisted - purge unused, blacklisted devices
1353 *
1354 * Unregister all ccw devices that are offline and on the blacklist.
1355 */
1356int ccw_purge_blacklisted(void)
1357{
1358 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1359 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1360 return 0;
1361}
1362
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001363void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001364{
1365 if (!cdev)
1366 return;
1367 ccw_device_set_timeout(cdev, 0);
1368 cdev->private->flags.fake_irb = 0;
1369 cdev->private->state = DEV_STATE_DISCONNECTED;
1370 if (cdev->online)
1371 ccw_device_schedule_recovery();
1372}
1373
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001374void ccw_device_set_notoper(struct ccw_device *cdev)
1375{
1376 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1377
1378 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001379 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001380 ccw_device_set_timeout(cdev, 0);
1381 cio_disable_subchannel(sch);
1382 cdev->private->state = DEV_STATE_NOT_OPER;
1383}
1384
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001385enum io_sch_action {
1386 IO_SCH_UNREG,
1387 IO_SCH_ORPH_UNREG,
1388 IO_SCH_ATTACH,
1389 IO_SCH_UNREG_ATTACH,
1390 IO_SCH_ORPH_ATTACH,
1391 IO_SCH_REPROBE,
1392 IO_SCH_VERIFY,
1393 IO_SCH_DISC,
1394 IO_SCH_NOP,
1395};
1396
1397static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001398{
Cornelia Huckc820de32008-07-14 09:58:45 +02001399 struct ccw_device *cdev;
1400
Cornelia Huckc820de32008-07-14 09:58:45 +02001401 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001402 if (cio_update_schib(sch)) {
1403 /* Not operational. */
1404 if (!cdev)
1405 return IO_SCH_UNREG;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001406 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001407 return IO_SCH_UNREG;
1408 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001409 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001410 /* Operational. */
1411 if (!cdev)
1412 return IO_SCH_ATTACH;
1413 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001414 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001415 return IO_SCH_UNREG_ATTACH;
1416 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001417 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001418 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001419 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001420 return IO_SCH_UNREG;
1421 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001422 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001423 if (device_is_disconnected(cdev))
1424 return IO_SCH_REPROBE;
Sebastian Ott31370f752012-10-24 11:22:52 +02001425 if (cdev->online && !cdev->private->flags.resuming)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001426 return IO_SCH_VERIFY;
Sebastian Ott43d0be72012-09-05 14:19:42 +02001427 if (cdev->private->state == DEV_STATE_NOT_OPER)
1428 return IO_SCH_UNREG_ATTACH;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001429 return IO_SCH_NOP;
1430}
1431
1432/**
1433 * io_subchannel_sch_event - process subchannel event
1434 * @sch: subchannel
1435 * @process: non-zero if function is called in process context
1436 *
1437 * An unspecified event occurred for this subchannel. Adjust data according
1438 * to the current operational state of the subchannel and device. Return
1439 * zero when the event has been handled sufficiently or -EAGAIN when this
1440 * function should be called again in process context.
1441 */
1442static int io_subchannel_sch_event(struct subchannel *sch, int process)
1443{
1444 unsigned long flags;
1445 struct ccw_device *cdev;
1446 struct ccw_dev_id dev_id;
1447 enum io_sch_action action;
1448 int rc = -EAGAIN;
1449
1450 spin_lock_irqsave(sch->lock, flags);
1451 if (!device_is_registered(&sch->dev))
1452 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001453 if (work_pending(&sch->todo_work))
1454 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001455 cdev = sch_get_cdev(sch);
1456 if (cdev && work_pending(&cdev->private->todo_work))
1457 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001458 action = sch_get_action(sch);
1459 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1460 sch->schid.ssid, sch->schid.sch_no, process,
1461 action);
1462 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001463 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001464 case IO_SCH_REPROBE:
1465 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001466 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001467 rc = 0;
1468 goto out_unlock;
1469 case IO_SCH_VERIFY:
1470 /* Trigger path verification. */
1471 io_subchannel_verify(sch);
1472 rc = 0;
1473 goto out_unlock;
1474 case IO_SCH_DISC:
1475 ccw_device_set_disconnected(cdev);
1476 rc = 0;
1477 goto out_unlock;
1478 case IO_SCH_ORPH_UNREG:
1479 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001480 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001481 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001482 case IO_SCH_UNREG_ATTACH:
1483 case IO_SCH_UNREG:
Sebastian Ott16d2ce22010-11-10 10:05:53 +01001484 if (!cdev)
1485 break;
1486 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1487 /*
1488 * Note: delayed work triggered by this event
1489 * and repeated calls to sch_event are synchronized
1490 * by the above check for work_pending(cdev).
1491 */
1492 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1493 } else
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001494 ccw_device_set_notoper(cdev);
1495 break;
1496 case IO_SCH_NOP:
1497 rc = 0;
1498 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001499 default:
1500 break;
1501 }
1502 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001503 /* All other actions require process context. */
1504 if (!process)
1505 goto out;
1506 /* Handle attached ccw device. */
1507 switch (action) {
1508 case IO_SCH_ORPH_UNREG:
1509 case IO_SCH_ORPH_ATTACH:
1510 /* Move ccw device to orphanage. */
1511 rc = ccw_device_move_to_orph(cdev);
1512 if (rc)
1513 goto out;
1514 break;
1515 case IO_SCH_UNREG_ATTACH:
Sebastian Ott3368ba22012-09-05 14:20:41 +02001516 spin_lock_irqsave(sch->lock, flags);
Sebastian Ott74b61272010-10-25 16:10:26 +02001517 if (cdev->private->flags.resuming) {
1518 /* Device will be handled later. */
1519 rc = 0;
Sebastian Ott3368ba22012-09-05 14:20:41 +02001520 goto out_unlock;
Sebastian Ott74b61272010-10-25 16:10:26 +02001521 }
Sebastian Ott3368ba22012-09-05 14:20:41 +02001522 sch_set_cdev(sch, NULL);
1523 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001524 /* Unregister ccw device. */
Sebastian Ott74b61272010-10-25 16:10:26 +02001525 ccw_device_unregister(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001526 break;
1527 default:
1528 break;
1529 }
1530 /* Handle subchannel. */
1531 switch (action) {
1532 case IO_SCH_ORPH_UNREG:
1533 case IO_SCH_UNREG:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001534 if (!cdev || !cdev->private->flags.resuming)
1535 css_sch_device_unregister(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001536 break;
1537 case IO_SCH_ORPH_ATTACH:
1538 case IO_SCH_UNREG_ATTACH:
1539 case IO_SCH_ATTACH:
1540 dev_id.ssid = sch->schid.ssid;
1541 dev_id.devno = sch->schib.pmcw.dev;
1542 cdev = get_ccwdev_by_dev_id(&dev_id);
1543 if (!cdev) {
1544 sch_create_and_recog_new_device(sch);
1545 break;
1546 }
1547 rc = ccw_device_move_to_sch(cdev, sch);
1548 if (rc) {
1549 /* Release reference from get_ccwdev_by_dev_id() */
1550 put_device(&cdev->dev);
1551 goto out;
1552 }
1553 spin_lock_irqsave(sch->lock, flags);
1554 ccw_device_trigger_reprobe(cdev);
1555 spin_unlock_irqrestore(sch->lock, flags);
1556 /* Release reference from get_ccwdev_by_dev_id() */
1557 put_device(&cdev->dev);
1558 break;
1559 default:
1560 break;
1561 }
1562 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001563
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001564out_unlock:
1565 spin_unlock_irqrestore(sch->lock, flags);
1566out:
1567 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001568}
1569
Sebastian Ott137a14f2014-01-27 13:29:15 +01001570static void ccw_device_set_int_class(struct ccw_device *cdev)
1571{
1572 struct ccw_driver *cdrv = cdev->drv;
1573
1574 /* Note: we interpret class 0 in this context as an uninitialized
1575 * field since it translates to a non-I/O interrupt class. */
1576 if (cdrv->int_class != 0)
1577 cdev->private->int_class = cdrv->int_class;
1578 else
1579 cdev->private->int_class = IRQIO_CIO;
1580}
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582#ifdef CONFIG_CCW_CONSOLE
Sebastian Ott1e532092014-01-27 13:28:10 +01001583int __init ccw_device_enable_console(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Sebastian Ott1e532092014-01-27 13:28:10 +01001585 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 int rc;
1587
Sebastian Ott1e532092014-01-27 13:28:10 +01001588 if (!cdev->drv || !cdev->handler)
1589 return -EINVAL;
1590
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001591 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001592 rc = cio_commit_config(sch);
1593 if (rc)
1594 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001595 sch->driver = &io_subchannel_driver;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001596 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* Now wait for the async. recognition to come to an end. */
1598 spin_lock_irq(cdev->ccwlock);
1599 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001600 ccw_device_wait_idle(cdev);
1601
Sebastian Ottafdfed02013-04-13 13:03:03 +02001602 /* Hold on to an extra reference while device is online. */
1603 get_device(&cdev->dev);
1604 rc = ccw_device_online(cdev);
1605 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto out_unlock;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001609 ccw_device_wait_idle(cdev);
1610
Sebastian Ottafdfed02013-04-13 13:03:03 +02001611 if (cdev->private->state == DEV_STATE_ONLINE)
1612 cdev->online = 1;
1613 else
1614 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615out_unlock:
1616 spin_unlock_irq(cdev->ccwlock);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001617 if (rc) /* Give up online reference since onlining failed. */
1618 put_device(&cdev->dev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001619 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
Sebastian Ott1e532092014-01-27 13:28:10 +01001622struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Sebastian Ott863fc842013-04-13 13:01:50 +02001624 struct io_subchannel_private *io_priv;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001625 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 sch = cio_probe_console();
Sebastian Ottafdfed02013-04-13 13:03:03 +02001629 if (IS_ERR(sch))
1630 return ERR_CAST(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +02001631
1632 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
Halil Pasic37db8982019-03-26 12:41:09 +01001633 if (!io_priv)
1634 goto err_priv;
1635 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1636 sizeof(*io_priv->dma_area),
1637 &io_priv->dma_area_dma, GFP_KERNEL);
1638 if (!io_priv->dma_area)
1639 goto err_dma_area;
Sebastian Ott2c3e7e12014-06-11 13:06:57 +02001640 set_io_private(sch, io_priv);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001641 cdev = io_subchannel_create_ccwdev(sch);
1642 if (IS_ERR(cdev)) {
Halil Pasic37db8982019-03-26 12:41:09 +01001643 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1644 io_priv->dma_area, io_priv->dma_area_dma);
1645 set_io_private(sch, NULL);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001646 put_device(&sch->dev);
1647 kfree(io_priv);
1648 return cdev;
1649 }
Sebastian Ott2253e8d2014-01-27 13:26:10 +01001650 cdev->drv = drv;
Sebastian Ott137a14f2014-01-27 13:29:15 +01001651 ccw_device_set_int_class(cdev);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001652 return cdev;
Halil Pasic37db8982019-03-26 12:41:09 +01001653
1654err_dma_area:
1655 kfree(io_priv);
1656err_priv:
1657 put_device(&sch->dev);
1658 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001660
Sebastian Ott1e532092014-01-27 13:28:10 +01001661void __init ccw_device_destroy_console(struct ccw_device *cdev)
1662{
1663 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1664 struct io_subchannel_private *io_priv = to_io_private(sch);
1665
1666 set_io_private(sch, NULL);
1667 put_device(&sch->dev);
1668 put_device(&cdev->dev);
Halil Pasic37db8982019-03-26 12:41:09 +01001669 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1670 io_priv->dma_area, io_priv->dma_area_dma);
Sebastian Ott1e532092014-01-27 13:28:10 +01001671 kfree(io_priv);
1672}
1673
Sebastian Ott188561a2013-04-13 12:53:21 +02001674/**
1675 * ccw_device_wait_idle() - busy wait for device to become idle
1676 * @cdev: ccw device
1677 *
1678 * Poll until activity control is zero, that is, no function or data
1679 * transfer is pending/active.
1680 * Called with device lock being held.
1681 */
1682void ccw_device_wait_idle(struct ccw_device *cdev)
1683{
1684 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1685
1686 while (1) {
1687 cio_tsch(sch);
1688 if (sch->schib.scsw.cmd.actl == 0)
1689 break;
1690 udelay_simple(100);
1691 }
1692}
1693
Martin Schwidefsky66648452009-06-16 10:30:28 +02001694static int ccw_device_pm_restore(struct device *dev);
1695
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001696int ccw_device_force_console(struct ccw_device *cdev)
Martin Schwidefsky66648452009-06-16 10:30:28 +02001697{
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001698 return ccw_device_pm_restore(&cdev->dev);
Martin Schwidefsky66648452009-06-16 10:30:28 +02001699}
1700EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701#endif
1702
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001703/**
1704 * get_ccwdev_by_busid() - obtain device from a bus id
1705 * @cdrv: driver the device is owned by
1706 * @bus_id: bus id of the device to be searched
1707 *
1708 * This function searches all devices owned by @cdrv for a device with a bus
1709 * id matching @bus_id.
1710 * Returns:
1711 * If a match is found, its reference count of the found device is increased
1712 * and it is returned; else %NULL is returned.
1713 */
1714struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1715 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001717 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01001719 dev = driver_find_device_by_name(&cdrv->driver, bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001721 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724/************************** device driver handling ************************/
1725
1726/* This is the implementation of the ccw_driver class. The probe, remove
1727 * and release methods are initially very similar to the device_driver
1728 * implementations, with the difference that they have ccw_device
1729 * arguments.
1730 *
1731 * A ccw driver also contains the information that is needed for
1732 * device matching.
1733 */
1734static int
1735ccw_device_probe (struct device *dev)
1736{
1737 struct ccw_device *cdev = to_ccwdev(dev);
1738 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1739 int ret;
1740
1741 cdev->drv = cdrv; /* to let the driver call _set_online */
Sebastian Ott137a14f2014-01-27 13:29:15 +01001742 ccw_device_set_int_class(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001745 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001746 cdev->private->int_class = IRQIO_CIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return ret;
1748 }
1749
1750 return 0;
1751}
1752
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001753static int ccw_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
1755 struct ccw_device *cdev = to_ccwdev(dev);
1756 struct ccw_driver *cdrv = cdev->drv;
Sebastian Ott135a8b42018-03-15 15:03:43 +01001757 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 int ret;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (cdrv->remove)
1761 cdrv->remove(cdev);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001762
1763 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (cdev->online) {
1765 cdev->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 ret = ccw_device_offline(cdev);
1767 spin_unlock_irq(cdev->ccwlock);
1768 if (ret == 0)
1769 wait_event(cdev->private->wait_q,
1770 dev_fsm_final_state(cdev));
1771 else
Michael Ernst139b83dd2008-05-07 09:22:54 +02001772 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001773 "device 0.%x.%04x\n",
1774 ret, cdev->private->dev_id.ssid,
1775 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001776 /* Give up reference obtained in ccw_device_set_online(). */
1777 put_device(&cdev->dev);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001778 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001781 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001782 cdev->private->int_class = IRQIO_CIO;
Sebastian Ott135a8b42018-03-15 15:03:43 +01001783 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001784 spin_unlock_irq(cdev->ccwlock);
Sebastian Ott135a8b42018-03-15 15:03:43 +01001785 io_subchannel_quiesce(sch);
Sebastian Otta6ef1562015-09-07 19:51:39 +02001786 __disable_cmf(cdev);
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return 0;
1789}
1790
Cornelia Huck958974fb2007-10-12 16:11:21 +02001791static void ccw_device_shutdown(struct device *dev)
1792{
1793 struct ccw_device *cdev;
1794
1795 cdev = to_ccwdev(dev);
1796 if (cdev->drv && cdev->drv->shutdown)
1797 cdev->drv->shutdown(cdev);
Sebastian Ott1bc66642015-09-15 13:11:42 +02001798 __disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001799}
1800
Sebastian Ott823d4942009-06-16 10:30:20 +02001801static int ccw_device_pm_prepare(struct device *dev)
1802{
1803 struct ccw_device *cdev = to_ccwdev(dev);
1804
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001805 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001806 return -EAGAIN;
1807 /* Fail while device is being set online/offline. */
1808 if (atomic_read(&cdev->private->onoff))
1809 return -EAGAIN;
1810
1811 if (cdev->online && cdev->drv && cdev->drv->prepare)
1812 return cdev->drv->prepare(cdev);
1813
1814 return 0;
1815}
1816
1817static void ccw_device_pm_complete(struct device *dev)
1818{
1819 struct ccw_device *cdev = to_ccwdev(dev);
1820
1821 if (cdev->online && cdev->drv && cdev->drv->complete)
1822 cdev->drv->complete(cdev);
1823}
1824
1825static int ccw_device_pm_freeze(struct device *dev)
1826{
1827 struct ccw_device *cdev = to_ccwdev(dev);
1828 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1829 int ret, cm_enabled;
1830
1831 /* Fail suspend while device is in transistional state. */
1832 if (!dev_fsm_final_state(cdev))
1833 return -EAGAIN;
1834 if (!cdev->online)
1835 return 0;
1836 if (cdev->drv && cdev->drv->freeze) {
1837 ret = cdev->drv->freeze(cdev);
1838 if (ret)
1839 return ret;
1840 }
1841
1842 spin_lock_irq(sch->lock);
1843 cm_enabled = cdev->private->cmb != NULL;
1844 spin_unlock_irq(sch->lock);
1845 if (cm_enabled) {
1846 /* Don't have the css write on memory. */
1847 ret = ccw_set_cmf(cdev, 0);
1848 if (ret)
1849 return ret;
1850 }
1851 /* From here on, disallow device driver I/O. */
1852 spin_lock_irq(sch->lock);
1853 ret = cio_disable_subchannel(sch);
1854 spin_unlock_irq(sch->lock);
1855
1856 return ret;
1857}
1858
1859static int ccw_device_pm_thaw(struct device *dev)
1860{
1861 struct ccw_device *cdev = to_ccwdev(dev);
1862 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1863 int ret, cm_enabled;
1864
1865 if (!cdev->online)
1866 return 0;
1867
1868 spin_lock_irq(sch->lock);
1869 /* Allow device driver I/O again. */
1870 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1871 cm_enabled = cdev->private->cmb != NULL;
1872 spin_unlock_irq(sch->lock);
1873 if (ret)
1874 return ret;
1875
1876 if (cm_enabled) {
1877 ret = ccw_set_cmf(cdev, 1);
1878 if (ret)
1879 return ret;
1880 }
1881
1882 if (cdev->drv && cdev->drv->thaw)
1883 ret = cdev->drv->thaw(cdev);
1884
1885 return ret;
1886}
1887
1888static void __ccw_device_pm_restore(struct ccw_device *cdev)
1889{
1890 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001891
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001892 spin_lock_irq(sch->lock);
1893 if (cio_is_console(sch->schid)) {
1894 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1895 goto out_unlock;
1896 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001897 /*
1898 * While we were sleeping, devices may have gone or become
1899 * available again. Kick re-detection.
1900 */
Sebastian Ott823d4942009-06-16 10:30:20 +02001901 cdev->private->flags.resuming = 1;
Sebastian Ottb17295e2011-01-12 09:55:10 +01001902 cdev->private->path_new_mask = LPM_ANYPATH;
Sebastian Ott817e5002011-12-01 13:32:19 +01001903 css_sched_sch_todo(sch, SCH_TODO_EVAL);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001904 spin_unlock_irq(sch->lock);
Sebastian Ott817e5002011-12-01 13:32:19 +01001905 css_wait_for_slow_path();
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001906
1907 /* cdev may have been moved to a different subchannel. */
1908 sch = to_subchannel(cdev->dev.parent);
1909 spin_lock_irq(sch->lock);
1910 if (cdev->private->state != DEV_STATE_ONLINE &&
1911 cdev->private->state != DEV_STATE_OFFLINE)
1912 goto out_unlock;
1913
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001914 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001915 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001916 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1917 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001918 spin_lock_irq(sch->lock);
1919
1920out_unlock:
Sebastian Ott823d4942009-06-16 10:30:20 +02001921 cdev->private->flags.resuming = 0;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001922 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001923}
1924
1925static int resume_handle_boxed(struct ccw_device *cdev)
1926{
1927 cdev->private->state = DEV_STATE_BOXED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001928 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001929 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001930 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001931 return -ENODEV;
1932}
1933
1934static int resume_handle_disc(struct ccw_device *cdev)
1935{
1936 cdev->private->state = DEV_STATE_DISCONNECTED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001937 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001938 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001939 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001940 return -ENODEV;
1941}
1942
1943static int ccw_device_pm_restore(struct device *dev)
1944{
1945 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001946 struct subchannel *sch;
1947 int ret = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001948
1949 __ccw_device_pm_restore(cdev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001950 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001951 spin_lock_irq(sch->lock);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001952 if (cio_is_console(sch->schid))
Sebastian Ott823d4942009-06-16 10:30:20 +02001953 goto out_restore;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001954
Sebastian Ott823d4942009-06-16 10:30:20 +02001955 /* check recognition results */
1956 switch (cdev->private->state) {
1957 case DEV_STATE_OFFLINE:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001958 case DEV_STATE_ONLINE:
1959 cdev->private->flags.donotify = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001960 break;
1961 case DEV_STATE_BOXED:
1962 ret = resume_handle_boxed(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001963 if (ret)
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001964 goto out_unlock;
Sebastian Ott823d4942009-06-16 10:30:20 +02001965 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001966 default:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001967 ret = resume_handle_disc(cdev);
1968 if (ret)
1969 goto out_unlock;
1970 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001971 }
1972 /* check if the device type has changed */
1973 if (!ccw_device_test_sense_data(cdev)) {
1974 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001975 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001976 ret = -ENODEV;
1977 goto out_unlock;
1978 }
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001979 if (!cdev->online)
1980 goto out_unlock;
1981
1982 if (ccw_device_online(cdev)) {
1983 ret = resume_handle_disc(cdev);
1984 if (ret)
1985 goto out_unlock;
1986 goto out_restore;
1987 }
1988 spin_unlock_irq(sch->lock);
1989 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1990 spin_lock_irq(sch->lock);
1991
1992 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1993 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1994 ret = -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +02001995 goto out_unlock;
1996 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001997
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001998 /* reenable cmf, if needed */
1999 if (cdev->private->cmb) {
2000 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02002001 ret = ccw_set_cmf(cdev, 1);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01002002 spin_lock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02002003 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02002004 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
2005 "(rc=%d)\n", cdev->private->dev_id.ssid,
2006 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02002007 ret = 0;
2008 }
2009 }
2010
2011out_restore:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01002012 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02002013 if (cdev->online && cdev->drv && cdev->drv->restore)
2014 ret = cdev->drv->restore(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02002015 return ret;
2016
Sebastian Ott823d4942009-06-16 10:30:20 +02002017out_unlock:
2018 spin_unlock_irq(sch->lock);
2019 return ret;
2020}
2021
Alexey Dobriyan47145212009-12-14 18:00:08 -08002022static const struct dev_pm_ops ccw_pm_ops = {
Sebastian Ott823d4942009-06-16 10:30:20 +02002023 .prepare = ccw_device_pm_prepare,
2024 .complete = ccw_device_pm_complete,
2025 .freeze = ccw_device_pm_freeze,
2026 .thaw = ccw_device_pm_thaw,
2027 .restore = ccw_device_pm_restore,
2028};
2029
Sebastian Ottd5ab5272011-03-23 10:16:03 +01002030static struct bus_type ccw_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01002031 .name = "ccw",
2032 .match = ccw_bus_match,
2033 .uevent = ccw_uevent,
2034 .probe = ccw_device_probe,
2035 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02002036 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02002037 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01002038};
2039
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002040/**
2041 * ccw_driver_register() - register a ccw driver
2042 * @cdriver: driver to be registered
2043 *
2044 * This function is mainly a wrapper around driver_register().
2045 * Returns:
2046 * %0 on success and a negative error value on failure.
2047 */
2048int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 struct device_driver *drv = &cdriver->driver;
2051
2052 drv->bus = &ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 return driver_register(drv);
2055}
2056
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002057/**
2058 * ccw_driver_unregister() - deregister a ccw driver
2059 * @cdriver: driver to be deregistered
2060 *
2061 * This function is mainly a wrapper around driver_unregister().
2062 */
2063void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 driver_unregister(&cdriver->driver);
2066}
2067
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002068static void ccw_device_todo(struct work_struct *work)
2069{
2070 struct ccw_device_private *priv;
2071 struct ccw_device *cdev;
2072 struct subchannel *sch;
2073 enum cdev_todo todo;
2074
2075 priv = container_of(work, struct ccw_device_private, todo_work);
2076 cdev = priv->cdev;
2077 sch = to_subchannel(cdev->dev.parent);
2078 /* Find out todo. */
2079 spin_lock_irq(cdev->ccwlock);
2080 todo = priv->todo;
2081 priv->todo = CDEV_TODO_NOTHING;
2082 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2083 priv->dev_id.ssid, priv->dev_id.devno, todo);
2084 spin_unlock_irq(cdev->ccwlock);
2085 /* Perform todo. */
2086 switch (todo) {
2087 case CDEV_TODO_ENABLE_CMF:
2088 cmf_reenable(cdev);
2089 break;
2090 case CDEV_TODO_REBIND:
2091 ccw_device_do_unbind_bind(cdev);
2092 break;
2093 case CDEV_TODO_REGISTER:
2094 io_subchannel_register(cdev);
2095 break;
2096 case CDEV_TODO_UNREG_EVAL:
2097 if (!sch_is_pseudo_sch(sch))
2098 css_schedule_eval(sch->schid);
Joe Perchesb09fcec2020-03-10 13:39:50 -07002099 fallthrough;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002100 case CDEV_TODO_UNREG:
2101 if (sch_is_pseudo_sch(sch))
2102 ccw_device_unregister(cdev);
2103 else
2104 ccw_device_call_sch_unregister(cdev);
2105 break;
2106 default:
2107 break;
2108 }
2109 /* Release workqueue ref. */
2110 put_device(&cdev->dev);
2111}
2112
2113/**
2114 * ccw_device_sched_todo - schedule ccw device operation
2115 * @cdev: ccw device
2116 * @todo: todo
2117 *
2118 * Schedule the operation identified by @todo to be performed on the slow path
2119 * workqueue. Do nothing if another operation with higher priority is already
2120 * scheduled. Needs to be called with ccwdev lock held.
2121 */
2122void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2123{
2124 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2125 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2126 todo);
2127 if (cdev->private->todo >= todo)
2128 return;
2129 cdev->private->todo = todo;
2130 /* Get workqueue ref. */
2131 if (!get_device(&cdev->dev))
2132 return;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01002133 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002134 /* Already queued, release workqueue ref. */
2135 put_device(&cdev->dev);
2136 }
2137}
2138
Michael Ernstfd0457a2010-08-09 18:12:50 +02002139/**
2140 * ccw_device_siosl() - initiate logging
2141 * @cdev: ccw device
2142 *
2143 * This function is used to invoke model-dependent logging within the channel
2144 * subsystem.
2145 */
2146int ccw_device_siosl(struct ccw_device *cdev)
2147{
2148 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2149
2150 return chsc_siosl(sch->schid);
2151}
2152EXPORT_SYMBOL_GPL(ccw_device_siosl);
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154EXPORT_SYMBOL(ccw_device_set_online);
2155EXPORT_SYMBOL(ccw_device_set_offline);
2156EXPORT_SYMBOL(ccw_driver_register);
2157EXPORT_SYMBOL(ccw_driver_unregister);
2158EXPORT_SYMBOL(get_ccwdev_by_busid);