blob: dcd06806ba3d3299db5f1731940edae27ec80fa4 [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
Sebastian Ottb4c70722010-02-26 22:37:27 +0100152static int io_subchannel_settle(void)
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200153{
Sebastian Ottb4c70722010-02-26 22:37:27 +0100154 int ret;
155
156 ret = wait_event_interruptible(ccw_device_init_wq,
157 atomic_read(&ccw_device_init_count) == 0);
158 if (ret)
159 return -EINTR;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100160 flush_workqueue(cio_work_q);
Sebastian Ottb4c70722010-02-26 22:37:27 +0100161 return 0;
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200162}
163
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200164static struct css_driver io_subchannel_driver = {
Sebastian Otte6aed122011-03-15 17:08:30 +0100165 .drv = {
166 .owner = THIS_MODULE,
167 .name = "io_subchannel",
168 },
Cornelia Huckf08adc02008-07-14 09:59:03 +0200169 .subchannel_type = io_subchannel_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200171 .sch_event = io_subchannel_sch_event,
172 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100173 .probe = io_subchannel_probe,
174 .remove = io_subchannel_remove,
175 .shutdown = io_subchannel_shutdown,
Sebastian Ott8ea7f552009-09-22 22:58:35 +0200176 .settle = io_subchannel_settle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Sebastian Ott2f176442009-09-22 22:58:33 +0200179int __init io_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int ret;
182
Kees Cook846d0c62017-10-16 16:43:25 -0700183 timer_setup(&recovery_timer, recovery_func, 0);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100184 ret = bus_register(&ccw_bus_type);
185 if (ret)
186 return ret;
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100187 ret = css_driver_register(&io_subchannel_driver);
188 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100189 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return ret;
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/************************ device handling **************************/
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400198devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct ccw_device *cdev = to_ccwdev(dev);
201 struct ccw_device_id *id = &(cdev->id);
202
203 if (id->dev_type != 0)
204 return sprintf(buf, "%04x/%02x\n",
205 id->dev_type, id->dev_model);
206 else
207 return sprintf(buf, "n/a\n");
208}
209
210static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400211cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct ccw_device *cdev = to_ccwdev(dev);
214 struct ccw_device_id *id = &(cdev->id);
215
216 return sprintf(buf, "%04x/%02x\n",
217 id->cu_type, id->cu_model);
218}
219
220static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800221modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
222{
223 struct ccw_device *cdev = to_ccwdev(dev);
224 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200225 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800226
Cornelia Huck086a6c62007-07-17 13:36:08 +0200227 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200228
229 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800230}
231
232static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400233online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 struct ccw_device *cdev = to_ccwdev(dev);
236
237 return sprintf(buf, cdev->online ? "1\n" : "0\n");
238}
239
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100240int ccw_device_is_orphan(struct ccw_device *cdev)
241{
242 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
243}
244
Cornelia Huckef995162007-04-27 16:01:39 +0200245static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100246{
Sebastian Ott7d253b92009-12-07 12:51:33 +0100247 if (device_is_registered(&cdev->dev)) {
Sebastian Ott24a18722009-12-07 12:51:34 +0100248 /* Undo device_add(). */
Cornelia Huckef995162007-04-27 16:01:39 +0200249 device_del(&cdev->dev);
Sebastian Ott24a18722009-12-07 12:51:34 +0100250 }
251 if (cdev->private->flags.initialized) {
252 cdev->private->flags.initialized = 0;
Sebastian Ott3b554a12009-09-11 10:28:26 +0200253 /* Release reference from device_initialize(). */
254 put_device(&cdev->dev);
255 }
Cornelia Huck7674da72006-12-08 15:54:21 +0100256}
257
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100258static void io_subchannel_quiesce(struct subchannel *);
259
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200260/**
261 * ccw_device_set_offline() - disable a ccw device for I/O
262 * @cdev: target ccw device
263 *
264 * This function calls the driver's set_offline() function for @cdev, if
265 * given, and then disables @cdev.
266 * Returns:
267 * %0 on success and a negative error value on failure.
268 * Context:
269 * enabled, ccw device lock not held
270 */
271int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100273 struct subchannel *sch;
274 int ret, state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (!cdev)
277 return -ENODEV;
278 if (!cdev->online || !cdev->drv)
279 return -EINVAL;
280
281 if (cdev->drv->set_offline) {
282 ret = cdev->drv->set_offline(cdev);
283 if (ret != 0)
284 return ret;
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 spin_lock_irq(cdev->ccwlock);
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100287 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott74bd0d82013-12-16 10:51:54 +0100288 cdev->online = 0;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200289 /* Wait until a final state or DISCONNECTED is reached */
290 while (!dev_fsm_final_state(cdev) &&
291 cdev->private->state != DEV_STATE_DISCONNECTED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200293 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
294 cdev->private->state == DEV_STATE_DISCONNECTED));
295 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Sebastian Ottd40f7b72009-12-07 12:51:41 +0100297 do {
298 ret = ccw_device_offline(cdev);
299 if (!ret)
300 break;
301 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
302 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
303 cdev->private->dev_id.devno);
304 if (ret != -EBUSY)
305 goto error;
306 state = cdev->private->state;
307 spin_unlock_irq(cdev->ccwlock);
308 io_subchannel_quiesce(sch);
309 spin_lock_irq(cdev->ccwlock);
310 cdev->private->state = state;
311 } while (ret == -EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200313 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
314 cdev->private->state == DEV_STATE_DISCONNECTED));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100315 /* Inform the user if set offline failed. */
316 if (cdev->private->state == DEV_STATE_BOXED) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800317 pr_warn("%s: The device entered boxed state while being set offline\n",
318 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100319 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800320 pr_warn("%s: The device stopped operating while being set offline\n",
321 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100322 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200323 /* Give up reference from ccw_device_set_online(). */
324 put_device(&cdev->dev);
325 return 0;
326
327error:
Michael Ernst217ee6c2009-09-11 10:28:21 +0200328 cdev->private->state = DEV_STATE_OFFLINE;
329 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
330 spin_unlock_irq(cdev->ccwlock);
331 /* Give up reference from ccw_device_set_online(). */
332 put_device(&cdev->dev);
333 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200336/**
337 * ccw_device_set_online() - enable a ccw device for I/O
338 * @cdev: target ccw device
339 *
340 * This function first enables @cdev and then calls the driver's set_online()
341 * function for @cdev, if given. If set_online() returns an error, @cdev is
342 * disabled again.
343 * Returns:
344 * %0 on success and a negative error value on failure.
345 * Context:
346 * enabled, ccw device lock not held
347 */
348int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 int ret;
Michael Ernst217ee6c2009-09-11 10:28:21 +0200351 int ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 if (!cdev)
354 return -ENODEV;
355 if (cdev->online || !cdev->drv)
356 return -EINVAL;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100357 /* Hold on to an extra reference while device is online. */
358 if (!get_device(&cdev->dev))
359 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 spin_lock_irq(cdev->ccwlock);
362 ret = ccw_device_online(cdev);
363 spin_unlock_irq(cdev->ccwlock);
364 if (ret == 0)
365 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
366 else {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200367 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200368 "device 0.%x.%04x\n",
369 ret, cdev->private->dev_id.ssid,
370 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +0100371 /* Give up online reference since onlining failed. */
372 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return ret;
374 }
Michael Ernst217ee6c2009-09-11 10:28:21 +0200375 spin_lock_irq(cdev->ccwlock);
376 /* Check if online processing was successful */
377 if ((cdev->private->state != DEV_STATE_ONLINE) &&
378 (cdev->private->state != DEV_STATE_W4SENSE)) {
379 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100380 /* Inform the user that set online failed. */
381 if (cdev->private->state == DEV_STATE_BOXED) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800382 pr_warn("%s: Setting the device online failed because it is boxed\n",
383 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100384 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800385 pr_warn("%s: Setting the device online failed because it is not operational\n",
386 dev_name(&cdev->dev));
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +0100387 }
Cornelia Huck9cd67422008-12-25 13:39:06 +0100388 /* Give up online reference since onlining failed. */
389 put_device(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -ENODEV;
Cornelia Huck9cd67422008-12-25 13:39:06 +0100391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200393 if (cdev->drv->set_online)
394 ret = cdev->drv->set_online(cdev);
395 if (ret)
396 goto rollback;
Sebastian Ott74bd0d82013-12-16 10:51:54 +0100397
398 spin_lock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200399 cdev->online = 1;
Sebastian Ott74bd0d82013-12-16 10:51:54 +0100400 spin_unlock_irq(cdev->ccwlock);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200401 return 0;
402
403rollback:
404 spin_lock_irq(cdev->ccwlock);
405 /* Wait until a final state or DISCONNECTED is reached */
406 while (!dev_fsm_final_state(cdev) &&
407 cdev->private->state != DEV_STATE_DISCONNECTED) {
408 spin_unlock_irq(cdev->ccwlock);
409 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
410 cdev->private->state == DEV_STATE_DISCONNECTED));
411 spin_lock_irq(cdev->ccwlock);
412 }
413 ret2 = ccw_device_offline(cdev);
414 if (ret2)
415 goto error;
416 spin_unlock_irq(cdev->ccwlock);
417 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
418 cdev->private->state == DEV_STATE_DISCONNECTED));
Cornelia Huck9cd67422008-12-25 13:39:06 +0100419 /* Give up online reference since onlining failed. */
420 put_device(&cdev->dev);
Michael Ernst217ee6c2009-09-11 10:28:21 +0200421 return ret;
422
423error:
424 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
425 "device 0.%x.%04x\n",
426 ret2, cdev->private->dev_id.ssid,
427 cdev->private->dev_id.devno);
428 cdev->private->state = DEV_STATE_OFFLINE;
429 spin_unlock_irq(cdev->ccwlock);
430 /* Give up online reference since onlining failed. */
431 put_device(&cdev->dev);
432 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100435static int online_store_handle_offline(struct ccw_device *cdev)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200436{
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100437 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
438 spin_lock_irq(cdev->ccwlock);
439 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
440 spin_unlock_irq(cdev->ccwlock);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200441 return 0;
442 }
443 if (cdev->drv && cdev->drv->set_offline)
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100444 return ccw_device_set_offline(cdev);
Sebastian Ott7cd40312010-08-09 18:12:52 +0200445 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200446}
447
448static int online_store_recog_and_online(struct ccw_device *cdev)
449{
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200450 /* Do device recognition, if needed. */
Sebastian Ott99f6a5702009-03-31 19:16:07 +0200451 if (cdev->private->state == DEV_STATE_BOXED) {
Peter Oberparleiter1f5bd38482009-12-07 12:51:23 +0100452 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100453 ccw_device_recognition(cdev);
Peter Oberparleiter1f5bd38482009-12-07 12:51:23 +0100454 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200455 wait_event(cdev->private->wait_q,
456 cdev->private->flags.recog_done);
Sebastian Ott156013f2009-03-31 19:16:03 +0200457 if (cdev->private->state != DEV_STATE_OFFLINE)
458 /* recognition failed */
459 return -EAGAIN;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200460 }
461 if (cdev->drv && cdev->drv->set_online)
Sebastian Ott7cd40312010-08-09 18:12:52 +0200462 return ccw_device_set_online(cdev);
463 return -EINVAL;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200464}
Sebastian Ott156013f2009-03-31 19:16:03 +0200465
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200466static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200467{
468 int ret;
469
470 ret = online_store_recog_and_online(cdev);
Sebastian Ott156013f2009-03-31 19:16:03 +0200471 if (ret && !force)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200472 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200473 if (force && cdev->private->state == DEV_STATE_BOXED) {
474 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200475 if (ret)
476 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200477 if (cdev->id.cu_type == 0)
478 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott156013f2009-03-31 19:16:03 +0200479 ret = online_store_recog_and_online(cdev);
480 if (ret)
481 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200482 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200483 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200484}
485
486static ssize_t online_store (struct device *dev, struct device_attribute *attr,
487 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200490 int force, ret;
491 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200493 /* Prevent conflict between multiple on-/offline processing requests. */
Peter Oberparleiter350e9122009-12-07 12:51:28 +0100494 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -EAGAIN;
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200496 /* Prevent conflict between internal I/Os and on-/offline processing. */
497 if (!dev_fsm_final_state(cdev) &&
498 cdev->private->state != DEV_STATE_DISCONNECTED) {
499 ret = -EAGAIN;
Sebastian Ott00381ee2013-12-16 10:54:13 +0100500 goto out;
Peter Oberparleitera2fc8482011-04-04 09:43:32 +0200501 }
502 /* Prevent conflict between pending work and on-/offline processing.*/
503 if (work_pending(&cdev->private->todo_work)) {
504 ret = -EAGAIN;
Sebastian Ott00381ee2013-12-16 10:54:13 +0100505 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 if (!strncmp(buf, "force\n", count)) {
508 force = 1;
509 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200510 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else {
512 force = 0;
Jingoo Han01787222013-07-22 10:18:15 +0900513 ret = kstrtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200515 if (ret)
516 goto out;
Sebastian Ott00381ee2013-12-16 10:54:13 +0100517
518 device_lock(dev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200519 switch (i) {
520 case 0:
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100521 ret = online_store_handle_offline(cdev);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200522 break;
523 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200524 ret = online_store_handle_online(cdev, force);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200525 break;
526 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200527 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Sebastian Ott00381ee2013-12-16 10:54:13 +0100529 device_unlock(dev);
530
Cornelia Huck2f972202008-04-30 13:38:33 +0200531out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 atomic_set(&cdev->private->onoff, 0);
Sebastian Otte74fe0c2009-03-26 15:24:08 +0100533 return (ret < 0) ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400537available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct ccw_device *cdev = to_ccwdev(dev);
540 struct subchannel *sch;
541
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100542 if (ccw_device_is_orphan(cdev))
543 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 switch (cdev->private->state) {
545 case DEV_STATE_BOXED:
546 return sprintf(buf, "boxed\n");
547 case DEV_STATE_DISCONNECTED:
548 case DEV_STATE_DISCONNECTED_SENSE_ID:
549 case DEV_STATE_NOT_OPER:
550 sch = to_subchannel(dev->parent);
551 if (!sch->lpm)
552 return sprintf(buf, "no path\n");
553 else
554 return sprintf(buf, "no device\n");
555 default:
556 /* All other states considered fine. */
557 return sprintf(buf, "good\n");
558 }
559}
560
Michael Ernstfd0457a2010-08-09 18:12:50 +0200561static ssize_t
562initiate_logging(struct device *dev, struct device_attribute *attr,
563 const char *buf, size_t count)
564{
565 struct subchannel *sch = to_subchannel(dev);
566 int rc;
567
568 rc = chsc_siosl(sch->schid);
569 if (rc < 0) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800570 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
571 sch->schid.ssid, sch->schid.sch_no, rc);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200572 return rc;
573 }
574 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
575 sch->schid.ssid, sch->schid.sch_no);
576 return count;
577}
578
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100579static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
580 char *buf)
581{
582 struct subchannel *sch = to_subchannel(dev);
583
584 return sprintf(buf, "%02x\n", sch->vpm);
585}
586
Joe Perchesc828a892017-12-19 10:15:08 -0800587static DEVICE_ATTR_RO(devtype);
588static DEVICE_ATTR_RO(cutype);
589static DEVICE_ATTR_RO(modalias);
Joe Perchesb6b996b2017-12-19 10:15:07 -0800590static DEVICE_ATTR_RW(online);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591static DEVICE_ATTR(availability, 0444, available_show, NULL);
Michael Ernstfd0457a2010-08-09 18:12:50 +0200592static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
Joe Perchesc828a892017-12-19 10:15:08 -0800593static DEVICE_ATTR_RO(vpm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200595static struct attribute *io_subchannel_attrs[] = {
Michael Ernstfd0457a2010-08-09 18:12:50 +0200596 &dev_attr_logging.attr,
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100597 &dev_attr_vpm.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 NULL,
599};
600
Arvind Yadavf460d112017-07-19 12:39:13 +0530601static const struct attribute_group io_subchannel_attr_group = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200602 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100603};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605static struct attribute * ccwdev_attrs[] = {
606 &dev_attr_devtype.attr,
607 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800608 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 &dev_attr_online.attr,
610 &dev_attr_cmb_enable.attr,
611 &dev_attr_availability.attr,
612 NULL,
613};
614
Arvind Yadavf460d112017-07-19 12:39:13 +0530615static const struct attribute_group ccwdev_attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 .attrs = ccwdev_attrs,
617};
618
David Brownella4dbd672009-06-24 10:06:31 -0700619static const struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200620 &ccwdev_attr_group,
621 NULL,
622};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +0100624static int match_dev_id(struct device *dev, const void *data)
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700625{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100626 struct ccw_device *cdev = to_ccwdev(dev);
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +0100627 struct ccw_dev_id *dev_id = (void *)data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700628
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100629 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
630}
631
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200632/**
633 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
634 * @dev_id: id of the device to be searched
635 *
636 * This function searches all devices attached to the ccw bus for a device
637 * matching @dev_id.
638 * Returns:
639 * If a device is found its reference count is increased and returned;
640 * else %NULL is returned.
641 */
642struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100643{
644 struct device *dev;
645
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100646 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100647
648 return dev ? to_ccwdev(dev) : NULL;
649}
Sebastian Ottb7a610f2012-05-15 17:52:07 +0200650EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100651
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100652static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100654 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Sebastian Ott7d253b92009-12-07 12:51:33 +0100656 if (device_is_registered(&cdev->dev)) {
Cornelia Huckeb32ae82009-03-26 15:24:05 +0100657 device_release_driver(&cdev->dev);
658 ret = device_attach(&cdev->dev);
659 WARN_ON(ret == -ENODEV);
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663static void
664ccw_device_release(struct device *dev)
665{
666 struct ccw_device *cdev;
667
668 cdev = to_ccwdev(dev);
Halil Pasic37db8982019-03-26 12:41:09 +0100669 cio_gp_dma_free(cdev->private->dma_pool, cdev->private->dma_area,
670 sizeof(*cdev->private->dma_area));
671 cio_gp_dma_destroy(cdev->private->dma_pool, &cdev->dev);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100672 /* Release reference of parent subchannel. */
673 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 kfree(cdev->private);
675 kfree(cdev);
676}
677
Cornelia Huck7674da72006-12-08 15:54:21 +0100678static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
679{
680 struct ccw_device *cdev;
Halil Pasic37db8982019-03-26 12:41:09 +0100681 struct gen_pool *dma_pool;
Julian Wiedmann4520a912020-12-09 11:24:13 +0100682 int ret;
Cornelia Huck7674da72006-12-08 15:54:21 +0100683
684 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
Julian Wiedmann4520a912020-12-09 11:24:13 +0100685 if (!cdev) {
686 ret = -ENOMEM;
Halil Pasic37db8982019-03-26 12:41:09 +0100687 goto err_cdev;
Julian Wiedmann4520a912020-12-09 11:24:13 +0100688 }
Halil Pasic37db8982019-03-26 12:41:09 +0100689 cdev->private = kzalloc(sizeof(struct ccw_device_private),
690 GFP_KERNEL | GFP_DMA);
Julian Wiedmann4520a912020-12-09 11:24:13 +0100691 if (!cdev->private) {
692 ret = -ENOMEM;
Halil Pasic37db8982019-03-26 12:41:09 +0100693 goto err_priv;
Julian Wiedmann4520a912020-12-09 11:24:13 +0100694 }
695
Halil Pasic05668e12019-09-30 17:38:02 +0200696 cdev->dev.dma_mask = sch->dev.dma_mask;
Julian Wiedmann4520a912020-12-09 11:24:13 +0100697 ret = dma_set_coherent_mask(&cdev->dev, sch->dev.coherent_dma_mask);
698 if (ret)
699 goto err_coherent_mask;
700
Halil Pasic37db8982019-03-26 12:41:09 +0100701 dma_pool = cio_gp_dma_create(&cdev->dev, 1);
Julian Wiedmann4520a912020-12-09 11:24:13 +0100702 if (!dma_pool) {
703 ret = -ENOMEM;
Halil Pasic37db8982019-03-26 12:41:09 +0100704 goto err_dma_pool;
Julian Wiedmann4520a912020-12-09 11:24:13 +0100705 }
Halil Pasic37db8982019-03-26 12:41:09 +0100706 cdev->private->dma_pool = dma_pool;
707 cdev->private->dma_area = cio_gp_dma_zalloc(dma_pool, &cdev->dev,
708 sizeof(*cdev->private->dma_area));
Julian Wiedmann4520a912020-12-09 11:24:13 +0100709 if (!cdev->private->dma_area) {
710 ret = -ENOMEM;
Halil Pasic37db8982019-03-26 12:41:09 +0100711 goto err_dma_area;
Julian Wiedmann4520a912020-12-09 11:24:13 +0100712 }
Halil Pasic37db8982019-03-26 12:41:09 +0100713 return cdev;
714err_dma_area:
715 cio_gp_dma_destroy(dma_pool, &cdev->dev);
716err_dma_pool:
Julian Wiedmann4520a912020-12-09 11:24:13 +0100717err_coherent_mask:
Halil Pasic37db8982019-03-26 12:41:09 +0100718 kfree(cdev->private);
719err_priv:
Cornelia Huck7674da72006-12-08 15:54:21 +0100720 kfree(cdev);
Halil Pasic37db8982019-03-26 12:41:09 +0100721err_cdev:
Julian Wiedmann4520a912020-12-09 11:24:13 +0100722 return ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100723}
724
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100725static void ccw_device_todo(struct work_struct *work);
726
Cornelia Huck7674da72006-12-08 15:54:21 +0100727static int io_subchannel_initialize_dev(struct subchannel *sch,
728 struct ccw_device *cdev)
729{
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200730 struct ccw_device_private *priv = cdev->private;
731 int ret;
732
733 priv->cdev = cdev;
734 priv->int_class = IRQIO_CIO;
735 priv->state = DEV_STATE_NOT_OPER;
736 priv->dev_id.devno = sch->schib.pmcw.dev;
737 priv->dev_id.ssid = sch->schid.ssid;
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200738
739 INIT_WORK(&priv->todo_work, ccw_device_todo);
740 INIT_LIST_HEAD(&priv->cmb_list);
741 init_waitqueue_head(&priv->wait_q);
Kees Cook846d0c62017-10-16 16:43:25 -0700742 timer_setup(&priv->timer, ccw_device_timeout, 0);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200743
744 atomic_set(&priv->onoff, 0);
745 cdev->ccwlock = sch->lock;
Cornelia Huck7674da72006-12-08 15:54:21 +0100746 cdev->dev.parent = &sch->dev;
747 cdev->dev.release = ccw_device_release;
Julian Wiedmann29c53de2020-11-30 10:19:57 +0200748 cdev->dev.bus = &ccw_bus_type;
Cornelia Huckef995162007-04-27 16:01:39 +0200749 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100750 /* Do first half of device_register. */
751 device_initialize(&cdev->dev);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200752 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
753 cdev->private->dev_id.devno);
754 if (ret)
755 goto out_put;
Cornelia Huck7674da72006-12-08 15:54:21 +0100756 if (!get_device(&sch->dev)) {
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200757 ret = -ENODEV;
758 goto out_put;
Cornelia Huck7674da72006-12-08 15:54:21 +0100759 }
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200760 priv->flags.initialized = 1;
761 spin_lock_irq(sch->lock);
762 sch_set_cdev(sch, cdev);
763 spin_unlock_irq(sch->lock);
Cornelia Huck7674da72006-12-08 15:54:21 +0100764 return 0;
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200765
766out_put:
767 /* Release reference from device_initialize(). */
768 put_device(&cdev->dev);
769 return ret;
Cornelia Huck7674da72006-12-08 15:54:21 +0100770}
771
772static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
773{
774 struct ccw_device *cdev;
775 int ret;
776
777 cdev = io_subchannel_allocate_dev(sch);
778 if (!IS_ERR(cdev)) {
779 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200780 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100781 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100782 }
783 return cdev;
784}
785
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100786static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100787
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100788static void sch_create_and_recog_new_device(struct subchannel *sch)
789{
790 struct ccw_device *cdev;
791
792 /* Need to allocate a new ccw device. */
793 cdev = io_subchannel_create_ccwdev(sch);
794 if (IS_ERR(cdev)) {
795 /* OK, we did everything we could... */
796 css_sch_device_unregister(sch);
797 return;
798 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100799 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100800 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/*
804 * Register recognized device.
805 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100806static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct subchannel *sch;
Sebastian Otta2901562010-03-08 12:25:17 +0100809 int ret, adjust_init_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 unsigned long flags;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100813 /*
814 * Check if subchannel is still registered. It may have become
815 * unregistered if a machine check hit us after finishing
816 * device recognition but before the register work could be
817 * queued.
818 */
819 if (!device_is_registered(&sch->dev))
820 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200821 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100822 /*
823 * io_subchannel_register() will also be called after device
824 * recognition has been done for a boxed device (which will already
825 * be registered). We need to reprobe since we may now have sense id
826 * information.
827 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100828 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100829 if (!cdev->drv) {
830 ret = device_reprobe(&cdev->dev);
831 if (ret)
832 /* We can't do much here. */
Michael Ernst139b83dd2008-05-07 09:22:54 +0200833 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200834 " %d for 0.%x.%04x\n", ret,
835 cdev->private->dev_id.ssid,
836 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100837 }
Sebastian Otta2901562010-03-08 12:25:17 +0100838 adjust_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 goto out;
840 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700841 /*
842 * Now we know this subchannel will stay, we can throw
843 * our delayed uevent.
844 */
Cornelia Huck05ce3e52020-03-27 13:45:02 +0100845 if (dev_get_uevent_suppress(&sch->dev)) {
846 dev_set_uevent_suppress(&sch->dev, 0);
847 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* make it known to the system */
Julian Wiedmann29c53de2020-11-30 10:19:57 +0200850 ret = device_add(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200852 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
853 cdev->private->dev_id.ssid,
854 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100855 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100856 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100857 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100858 /* Release initial device reference. */
859 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100860 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862out:
863 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100865out_err:
Sebastian Otta2901562010-03-08 12:25:17 +0100866 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 wake_up(&ccw_device_init_wq);
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870/*
871 * subchannel recognition done. Called from the state machine.
872 */
873void
874io_subchannel_recog_done(struct ccw_device *cdev)
875{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (css_init_done == 0) {
877 cdev->private->flags.recog_done = 1;
878 return;
879 }
880 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200881 case DEV_STATE_BOXED:
882 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 case DEV_STATE_NOT_OPER:
884 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100885 /* Remove device found not operational. */
886 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (atomic_dec_and_test(&ccw_device_init_count))
888 wake_up(&ccw_device_init_wq);
889 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 case DEV_STATE_OFFLINE:
Halil Pasic37db8982019-03-26 12:41:09 +0100891 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * We can't register the device in interrupt context so
893 * we schedule a work item.
894 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100895 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
897 }
898}
899
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100900static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* Increase counter of devices currently in recognition. */
903 atomic_inc(&ccw_device_init_count);
904
905 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100906 spin_lock_irq(sch->lock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100907 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100908 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100911static int ccw_device_move_to_sch(struct ccw_device *cdev,
912 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100913{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100914 struct subchannel *old_sch;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100915 int rc, old_enabled = 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100916
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100917 old_sch = to_subchannel(cdev->dev.parent);
918 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100919 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100920 return -ENODEV;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100921
922 if (!sch_is_pseudo_sch(old_sch)) {
923 spin_lock_irq(old_sch->lock);
924 old_enabled = old_sch->schib.pmcw.ena;
925 rc = 0;
926 if (old_enabled)
927 rc = cio_disable_subchannel(old_sch);
928 spin_unlock_irq(old_sch->lock);
929 if (rc == -EBUSY) {
930 /* Release child reference for new parent. */
931 put_device(&sch->dev);
932 return rc;
933 }
934 }
935
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100936 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100937 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100938 mutex_unlock(&sch->reg_mutex);
939 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100940 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100941 cdev->private->dev_id.ssid,
942 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100943 sch->schib.pmcw.dev, rc);
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100944 if (old_enabled) {
945 /* Try to reenable the old subchannel. */
946 spin_lock_irq(old_sch->lock);
947 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
948 spin_unlock_irq(old_sch->lock);
949 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100950 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100951 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100952 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100953 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100954 /* Clean up old subchannel. */
955 if (!sch_is_pseudo_sch(old_sch)) {
956 spin_lock_irq(old_sch->lock);
957 sch_set_cdev(old_sch, NULL);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100958 spin_unlock_irq(old_sch->lock);
959 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100960 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100961 /* Release child reference for old parent. */
962 put_device(&old_sch->dev);
963 /* Initialize new subchannel. */
964 spin_lock_irq(sch->lock);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100965 cdev->ccwlock = sch->lock;
966 if (!sch_is_pseudo_sch(sch))
967 sch_set_cdev(sch, cdev);
968 spin_unlock_irq(sch->lock);
969 if (!sch_is_pseudo_sch(sch))
970 css_update_ssd_info(sch);
971 return 0;
972}
973
974static int ccw_device_move_to_orph(struct ccw_device *cdev)
975{
976 struct subchannel *sch = to_subchannel(cdev->dev.parent);
977 struct channel_subsystem *css = to_css(sch->dev.parent);
978
979 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100980}
981
Cornelia Huck602b20f2008-01-26 14:10:39 +0100982static void io_subchannel_irq(struct subchannel *sch)
983{
984 struct ccw_device *cdev;
985
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100986 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100987
Sebastian Ottefd986d2009-09-11 10:28:18 +0200988 CIO_TRACE_EVENT(6, "IRQ");
989 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +0100990 if (cdev)
991 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
Peter Oberparleiterde400d62011-10-30 15:16:04 +0100992 else
Heiko Carstens420f42e2013-01-02 15:18:18 +0100993 inc_irq_stat(IRQIO_CIO);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100994}
995
Sebastian Ott13952ec2008-12-25 13:39:13 +0100996void io_subchannel_init_config(struct subchannel *sch)
997{
998 memset(&sch->config, 0, sizeof(sch->config));
999 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +01001000}
1001
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001002static void io_subchannel_init_fields(struct subchannel *sch)
1003{
1004 if (cio_is_console(sch->schid))
1005 sch->opm = 0xff;
1006 else
1007 sch->opm = chp_get_sch_opm(sch);
1008 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001009 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001010
1011 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1012 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1013 sch->schib.pmcw.dev, sch->schid.ssid,
1014 sch->schid.sch_no, sch->schib.pmcw.pim,
1015 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001016
1017 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001018}
1019
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001020/*
1021 * Note: We always return 0 so that we bind to the device even on error.
1022 * This is needed so that our remove function is called on unregister.
1023 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001024static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001026 struct io_subchannel_private *io_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 struct ccw_device *cdev;
1028 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001030 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001031 rc = sysfs_create_group(&sch->dev.kobj,
1032 &io_subchannel_attr_group);
1033 if (rc)
1034 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1035 "attributes for subchannel "
1036 "0.%x.%04x (rc=%d)\n",
1037 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001039 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001040 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001041 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Cornelia Huck05ce3e52020-03-27 13:45:02 +01001043 if (dev_get_uevent_suppress(&sch->dev)) {
1044 /* should always be the case for the console */
1045 dev_set_uevent_suppress(&sch->dev, 0);
1046 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1047 }
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001048 cdev = sch_get_cdev(sch);
Julian Wiedmann29c53de2020-11-30 10:19:57 +02001049 rc = device_add(&cdev->dev);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001050 if (rc) {
1051 /* Release online reference. */
1052 put_device(&cdev->dev);
1053 goto out_schedule;
1054 }
Sebastian Ott0ad8f714a2013-04-13 13:06:27 +02001055 if (atomic_dec_and_test(&ccw_device_init_count))
1056 wake_up(&ccw_device_init_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return 0;
1058 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001059 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001060 rc = cio_commit_config(sch);
1061 if (rc)
1062 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001063 rc = sysfs_create_group(&sch->dev.kobj,
1064 &io_subchannel_attr_group);
1065 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001066 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001067 /* Allocate I/O subchannel private data. */
Sebastian Ottf92519e2011-03-15 17:08:27 +01001068 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1069 if (!io_priv)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001070 goto out_schedule;
Sebastian Ottf92519e2011-03-15 17:08:27 +01001071
Halil Pasic37db8982019-03-26 12:41:09 +01001072 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1073 sizeof(*io_priv->dma_area),
1074 &io_priv->dma_area_dma, GFP_KERNEL);
1075 if (!io_priv->dma_area) {
1076 kfree(io_priv);
1077 goto out_schedule;
1078 }
1079
Sebastian Ottf92519e2011-03-15 17:08:27 +01001080 set_io_private(sch, io_priv);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001081 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001082 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001083
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001084out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001085 spin_lock_irq(sch->lock);
1086 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1087 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Sebastian Ott135a8b42018-03-15 15:03:43 +01001091static int io_subchannel_remove(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001093 struct io_subchannel_private *io_priv = to_io_private(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001096 cdev = sch_get_cdev(sch);
1097 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001098 goto out_free;
Sebastian Ott135a8b42018-03-15 15:03:43 +01001099
1100 ccw_device_unregister(cdev);
1101 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001102 sch_set_cdev(sch, NULL);
Sebastian Ottf92519e2011-03-15 17:08:27 +01001103 set_io_private(sch, NULL);
Sebastian Ott135a8b42018-03-15 15:03:43 +01001104 spin_unlock_irq(sch->lock);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001105out_free:
Halil Pasic37db8982019-03-26 12:41:09 +01001106 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1107 io_priv->dma_area, io_priv->dma_area_dma);
Sebastian Ottf92519e2011-03-15 17:08:27 +01001108 kfree(io_priv);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001109 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return 0;
1111}
1112
Cornelia Huck602b20f2008-01-26 14:10:39 +01001113static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct ccw_device *cdev;
1116
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001117 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (cdev)
1119 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1120}
1121
Cornelia Huckc820de32008-07-14 09:58:45 +02001122static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct ccw_device *cdev;
1125
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001126 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (!cdev)
1128 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001129 if (cio_update_schib(sch))
1130 goto err;
1131 /* Check for I/O on path. */
1132 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1133 goto out;
1134 if (cdev->private->state == DEV_STATE_ONLINE) {
1135 ccw_device_kill_io(cdev);
1136 goto out;
1137 }
1138 if (cio_clear(sch))
1139 goto err;
1140out:
1141 /* Trigger path verification. */
1142 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1143 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001144
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001145err:
1146 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001147}
1148
Cornelia Huck99611f82008-07-14 09:59:02 +02001149static int io_subchannel_chp_event(struct subchannel *sch,
1150 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001151{
Sebastian Ott585b9542010-10-25 16:10:34 +02001152 struct ccw_device *cdev = sch_get_cdev(sch);
Vineeth Vijayan32ef9382020-10-08 15:13:29 +02001153 int mask, chpid, valid_bit;
1154 int path_event[8];
Cornelia Huckc820de32008-07-14 09:58:45 +02001155
Cornelia Huck99611f82008-07-14 09:59:02 +02001156 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001157 if (!mask)
1158 return 0;
1159 switch (event) {
1160 case CHP_VARY_OFF:
1161 sch->opm &= ~mask;
1162 sch->lpm &= ~mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001163 if (cdev)
1164 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001165 io_subchannel_terminate_path(sch, mask);
1166 break;
1167 case CHP_VARY_ON:
1168 sch->opm |= mask;
1169 sch->lpm |= mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001170 if (cdev)
1171 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001172 io_subchannel_verify(sch);
1173 break;
1174 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001175 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001176 return -ENODEV;
Sebastian Ott585b9542010-10-25 16:10:34 +02001177 if (cdev)
1178 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001179 io_subchannel_terminate_path(sch, mask);
1180 break;
1181 case CHP_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001182 if (cio_update_schib(sch))
1183 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001184 sch->lpm |= mask & sch->opm;
Sebastian Ott585b9542010-10-25 16:10:34 +02001185 if (cdev)
1186 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001187 io_subchannel_verify(sch);
1188 break;
Vineeth Vijayan32ef9382020-10-08 15:13:29 +02001189 case CHP_FCES_EVENT:
1190 /* Forward Endpoint Security event */
1191 for (chpid = 0, valid_bit = 0x80; chpid < 8; chpid++,
1192 valid_bit >>= 1) {
1193 if (mask & valid_bit)
1194 path_event[chpid] = PE_PATH_FCES_EVENT;
1195 else
1196 path_event[chpid] = PE_NONE;
1197 }
1198 if (cdev)
1199 cdev->drv->path_event(cdev, path_event);
1200 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001201 }
1202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001205static void io_subchannel_quiesce(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 struct ccw_device *cdev;
1208 int ret;
1209
Sebastian Ott56e6b792009-12-07 12:51:35 +01001210 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001211 cdev = sch_get_cdev(sch);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001212 if (cio_is_console(sch->schid))
Sebastian Ott56e6b792009-12-07 12:51:35 +01001213 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!sch->schib.pmcw.ena)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001215 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ret = cio_disable_subchannel(sch);
1217 if (ret != -EBUSY)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001218 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (cdev->handler)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001220 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1221 while (ret == -EBUSY) {
1222 cdev->private->state = DEV_STATE_QUIESCE;
Peter Oberparleiter376ae472010-10-25 16:10:44 +02001223 cdev->private->iretry = 255;
Sebastian Ott56e6b792009-12-07 12:51:35 +01001224 ret = ccw_device_cancel_halt_clear(cdev);
1225 if (ret == -EBUSY) {
1226 ccw_device_set_timeout(cdev, HZ/10);
1227 spin_unlock_irq(sch->lock);
1228 wait_event(cdev->private->wait_q,
1229 cdev->private->state != DEV_STATE_QUIESCE);
1230 spin_lock_irq(sch->lock);
1231 }
1232 ret = cio_disable_subchannel(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Sebastian Ott56e6b792009-12-07 12:51:35 +01001234out_unlock:
1235 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001238static void io_subchannel_shutdown(struct subchannel *sch)
1239{
1240 io_subchannel_quiesce(sch);
1241}
1242
Cornelia Huckc820de32008-07-14 09:58:45 +02001243static int device_is_disconnected(struct ccw_device *cdev)
1244{
1245 if (!cdev)
1246 return 0;
1247 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1248 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1249}
1250
1251static int recovery_check(struct device *dev, void *data)
1252{
1253 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott55fb7342017-09-14 13:55:22 +02001254 struct subchannel *sch;
Cornelia Huckc820de32008-07-14 09:58:45 +02001255 int *redo = data;
1256
1257 spin_lock_irq(cdev->ccwlock);
1258 switch (cdev->private->state) {
Sebastian Ott55fb7342017-09-14 13:55:22 +02001259 case DEV_STATE_ONLINE:
1260 sch = to_subchannel(cdev->dev.parent);
1261 if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1262 break;
Joe Perchesb09fcec2020-03-10 13:39:50 -07001263 fallthrough;
Cornelia Huckc820de32008-07-14 09:58:45 +02001264 case DEV_STATE_DISCONNECTED:
1265 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1266 cdev->private->dev_id.ssid,
1267 cdev->private->dev_id.devno);
1268 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1269 *redo = 1;
1270 break;
1271 case DEV_STATE_DISCONNECTED_SENSE_ID:
1272 *redo = 1;
1273 break;
1274 }
1275 spin_unlock_irq(cdev->ccwlock);
1276
1277 return 0;
1278}
1279
1280static void recovery_work_func(struct work_struct *unused)
1281{
1282 int redo = 0;
1283
1284 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1285 if (redo) {
1286 spin_lock_irq(&recovery_lock);
1287 if (!timer_pending(&recovery_timer)) {
1288 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1289 recovery_phase++;
1290 mod_timer(&recovery_timer, jiffies +
1291 recovery_delay[recovery_phase] * HZ);
1292 }
1293 spin_unlock_irq(&recovery_lock);
1294 } else
Sebastian Ott55fb7342017-09-14 13:55:22 +02001295 CIO_MSG_EVENT(3, "recovery: end\n");
Cornelia Huckc820de32008-07-14 09:58:45 +02001296}
1297
1298static DECLARE_WORK(recovery_work, recovery_work_func);
1299
Kees Cook846d0c62017-10-16 16:43:25 -07001300static void recovery_func(struct timer_list *unused)
Cornelia Huckc820de32008-07-14 09:58:45 +02001301{
1302 /*
1303 * We can't do our recovery in softirq context and it's not
1304 * performance critical, so we schedule it.
1305 */
1306 schedule_work(&recovery_work);
1307}
1308
Sebastian Ott55fb7342017-09-14 13:55:22 +02001309void ccw_device_schedule_recovery(void)
Cornelia Huckc820de32008-07-14 09:58:45 +02001310{
1311 unsigned long flags;
1312
Sebastian Ott55fb7342017-09-14 13:55:22 +02001313 CIO_MSG_EVENT(3, "recovery: schedule\n");
Cornelia Huckc820de32008-07-14 09:58:45 +02001314 spin_lock_irqsave(&recovery_lock, flags);
1315 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1316 recovery_phase = 0;
1317 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1318 }
1319 spin_unlock_irqrestore(&recovery_lock, flags);
1320}
1321
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001322static int purge_fn(struct device *dev, void *data)
1323{
1324 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001325 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001326
1327 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001328 if (is_blacklisted(id->ssid, id->devno) &&
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001329 (cdev->private->state == DEV_STATE_OFFLINE) &&
1330 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001331 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1332 id->devno);
1333 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001334 atomic_set(&cdev->private->onoff, 0);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001335 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001336 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001337 /* Abort loop in case of pending signal. */
1338 if (signal_pending(current))
1339 return -EINTR;
1340
1341 return 0;
1342}
1343
1344/**
1345 * ccw_purge_blacklisted - purge unused, blacklisted devices
1346 *
1347 * Unregister all ccw devices that are offline and on the blacklist.
1348 */
1349int ccw_purge_blacklisted(void)
1350{
1351 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1352 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1353 return 0;
1354}
1355
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001356void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001357{
1358 if (!cdev)
1359 return;
1360 ccw_device_set_timeout(cdev, 0);
1361 cdev->private->flags.fake_irb = 0;
1362 cdev->private->state = DEV_STATE_DISCONNECTED;
1363 if (cdev->online)
1364 ccw_device_schedule_recovery();
1365}
1366
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001367void ccw_device_set_notoper(struct ccw_device *cdev)
1368{
1369 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1370
1371 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001372 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001373 ccw_device_set_timeout(cdev, 0);
1374 cio_disable_subchannel(sch);
1375 cdev->private->state = DEV_STATE_NOT_OPER;
1376}
1377
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001378enum io_sch_action {
1379 IO_SCH_UNREG,
1380 IO_SCH_ORPH_UNREG,
1381 IO_SCH_ATTACH,
1382 IO_SCH_UNREG_ATTACH,
1383 IO_SCH_ORPH_ATTACH,
1384 IO_SCH_REPROBE,
1385 IO_SCH_VERIFY,
1386 IO_SCH_DISC,
1387 IO_SCH_NOP,
1388};
1389
1390static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001391{
Cornelia Huckc820de32008-07-14 09:58:45 +02001392 struct ccw_device *cdev;
1393
Cornelia Huckc820de32008-07-14 09:58:45 +02001394 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001395 if (cio_update_schib(sch)) {
1396 /* Not operational. */
1397 if (!cdev)
1398 return IO_SCH_UNREG;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001399 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001400 return IO_SCH_UNREG;
1401 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001402 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001403 /* Operational. */
1404 if (!cdev)
1405 return IO_SCH_ATTACH;
1406 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001407 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001408 return IO_SCH_UNREG_ATTACH;
1409 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001410 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001411 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001412 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001413 return IO_SCH_UNREG;
1414 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001415 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001416 if (device_is_disconnected(cdev))
1417 return IO_SCH_REPROBE;
Vineeth Vijayan8cc0dcf2020-11-20 09:36:38 +01001418 if (cdev->online)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001419 return IO_SCH_VERIFY;
Sebastian Ott43d0be72012-09-05 14:19:42 +02001420 if (cdev->private->state == DEV_STATE_NOT_OPER)
1421 return IO_SCH_UNREG_ATTACH;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001422 return IO_SCH_NOP;
1423}
1424
1425/**
1426 * io_subchannel_sch_event - process subchannel event
1427 * @sch: subchannel
1428 * @process: non-zero if function is called in process context
1429 *
1430 * An unspecified event occurred for this subchannel. Adjust data according
1431 * to the current operational state of the subchannel and device. Return
1432 * zero when the event has been handled sufficiently or -EAGAIN when this
1433 * function should be called again in process context.
1434 */
1435static int io_subchannel_sch_event(struct subchannel *sch, int process)
1436{
1437 unsigned long flags;
1438 struct ccw_device *cdev;
1439 struct ccw_dev_id dev_id;
1440 enum io_sch_action action;
1441 int rc = -EAGAIN;
1442
1443 spin_lock_irqsave(sch->lock, flags);
1444 if (!device_is_registered(&sch->dev))
1445 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001446 if (work_pending(&sch->todo_work))
1447 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001448 cdev = sch_get_cdev(sch);
1449 if (cdev && work_pending(&cdev->private->todo_work))
1450 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001451 action = sch_get_action(sch);
1452 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1453 sch->schid.ssid, sch->schid.sch_no, process,
1454 action);
1455 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001456 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001457 case IO_SCH_REPROBE:
1458 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001459 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001460 rc = 0;
1461 goto out_unlock;
1462 case IO_SCH_VERIFY:
1463 /* Trigger path verification. */
1464 io_subchannel_verify(sch);
1465 rc = 0;
1466 goto out_unlock;
1467 case IO_SCH_DISC:
1468 ccw_device_set_disconnected(cdev);
1469 rc = 0;
1470 goto out_unlock;
1471 case IO_SCH_ORPH_UNREG:
1472 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001473 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001474 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001475 case IO_SCH_UNREG_ATTACH:
1476 case IO_SCH_UNREG:
Sebastian Ott16d2ce22010-11-10 10:05:53 +01001477 if (!cdev)
1478 break;
1479 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1480 /*
1481 * Note: delayed work triggered by this event
1482 * and repeated calls to sch_event are synchronized
1483 * by the above check for work_pending(cdev).
1484 */
1485 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1486 } else
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001487 ccw_device_set_notoper(cdev);
1488 break;
1489 case IO_SCH_NOP:
1490 rc = 0;
1491 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001492 default:
1493 break;
1494 }
1495 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001496 /* All other actions require process context. */
1497 if (!process)
1498 goto out;
1499 /* Handle attached ccw device. */
1500 switch (action) {
1501 case IO_SCH_ORPH_UNREG:
1502 case IO_SCH_ORPH_ATTACH:
1503 /* Move ccw device to orphanage. */
1504 rc = ccw_device_move_to_orph(cdev);
1505 if (rc)
1506 goto out;
1507 break;
1508 case IO_SCH_UNREG_ATTACH:
Sebastian Ott3368ba22012-09-05 14:20:41 +02001509 spin_lock_irqsave(sch->lock, flags);
Sebastian Ott3368ba22012-09-05 14:20:41 +02001510 sch_set_cdev(sch, NULL);
1511 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001512 /* Unregister ccw device. */
Sebastian Ott74b61272010-10-25 16:10:26 +02001513 ccw_device_unregister(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001514 break;
1515 default:
1516 break;
1517 }
1518 /* Handle subchannel. */
1519 switch (action) {
1520 case IO_SCH_ORPH_UNREG:
1521 case IO_SCH_UNREG:
Vineeth Vijayan2f7484f2021-04-23 12:08:43 +02001522 css_sch_device_unregister(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001523 break;
1524 case IO_SCH_ORPH_ATTACH:
1525 case IO_SCH_UNREG_ATTACH:
1526 case IO_SCH_ATTACH:
1527 dev_id.ssid = sch->schid.ssid;
1528 dev_id.devno = sch->schib.pmcw.dev;
1529 cdev = get_ccwdev_by_dev_id(&dev_id);
1530 if (!cdev) {
1531 sch_create_and_recog_new_device(sch);
1532 break;
1533 }
1534 rc = ccw_device_move_to_sch(cdev, sch);
1535 if (rc) {
1536 /* Release reference from get_ccwdev_by_dev_id() */
1537 put_device(&cdev->dev);
1538 goto out;
1539 }
1540 spin_lock_irqsave(sch->lock, flags);
1541 ccw_device_trigger_reprobe(cdev);
1542 spin_unlock_irqrestore(sch->lock, flags);
1543 /* Release reference from get_ccwdev_by_dev_id() */
1544 put_device(&cdev->dev);
1545 break;
1546 default:
1547 break;
1548 }
1549 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001550
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001551out_unlock:
1552 spin_unlock_irqrestore(sch->lock, flags);
1553out:
1554 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001555}
1556
Sebastian Ott137a14f2014-01-27 13:29:15 +01001557static void ccw_device_set_int_class(struct ccw_device *cdev)
1558{
1559 struct ccw_driver *cdrv = cdev->drv;
1560
1561 /* Note: we interpret class 0 in this context as an uninitialized
1562 * field since it translates to a non-I/O interrupt class. */
1563 if (cdrv->int_class != 0)
1564 cdev->private->int_class = cdrv->int_class;
1565 else
1566 cdev->private->int_class = IRQIO_CIO;
1567}
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569#ifdef CONFIG_CCW_CONSOLE
Sebastian Ott1e532092014-01-27 13:28:10 +01001570int __init ccw_device_enable_console(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Sebastian Ott1e532092014-01-27 13:28:10 +01001572 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int rc;
1574
Sebastian Ott1e532092014-01-27 13:28:10 +01001575 if (!cdev->drv || !cdev->handler)
1576 return -EINVAL;
1577
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001578 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001579 rc = cio_commit_config(sch);
1580 if (rc)
1581 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001582 sch->driver = &io_subchannel_driver;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001583 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /* Now wait for the async. recognition to come to an end. */
1585 spin_lock_irq(cdev->ccwlock);
1586 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001587 ccw_device_wait_idle(cdev);
1588
Sebastian Ottafdfed02013-04-13 13:03:03 +02001589 /* Hold on to an extra reference while device is online. */
1590 get_device(&cdev->dev);
1591 rc = ccw_device_online(cdev);
1592 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 goto out_unlock;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001596 ccw_device_wait_idle(cdev);
1597
Sebastian Ottafdfed02013-04-13 13:03:03 +02001598 if (cdev->private->state == DEV_STATE_ONLINE)
1599 cdev->online = 1;
1600 else
1601 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602out_unlock:
1603 spin_unlock_irq(cdev->ccwlock);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001604 if (rc) /* Give up online reference since onlining failed. */
1605 put_device(&cdev->dev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001606 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Sebastian Ott1e532092014-01-27 13:28:10 +01001609struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Sebastian Ott863fc842013-04-13 13:01:50 +02001611 struct io_subchannel_private *io_priv;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001612 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 sch = cio_probe_console();
Sebastian Ottafdfed02013-04-13 13:03:03 +02001616 if (IS_ERR(sch))
1617 return ERR_CAST(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +02001618
1619 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
Halil Pasic37db8982019-03-26 12:41:09 +01001620 if (!io_priv)
1621 goto err_priv;
1622 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1623 sizeof(*io_priv->dma_area),
1624 &io_priv->dma_area_dma, GFP_KERNEL);
1625 if (!io_priv->dma_area)
1626 goto err_dma_area;
Sebastian Ott2c3e7e12014-06-11 13:06:57 +02001627 set_io_private(sch, io_priv);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001628 cdev = io_subchannel_create_ccwdev(sch);
1629 if (IS_ERR(cdev)) {
Halil Pasic37db8982019-03-26 12:41:09 +01001630 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1631 io_priv->dma_area, io_priv->dma_area_dma);
1632 set_io_private(sch, NULL);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001633 put_device(&sch->dev);
1634 kfree(io_priv);
1635 return cdev;
1636 }
Sebastian Ott2253e8d2014-01-27 13:26:10 +01001637 cdev->drv = drv;
Sebastian Ott137a14f2014-01-27 13:29:15 +01001638 ccw_device_set_int_class(cdev);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001639 return cdev;
Halil Pasic37db8982019-03-26 12:41:09 +01001640
1641err_dma_area:
1642 kfree(io_priv);
1643err_priv:
1644 put_device(&sch->dev);
1645 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001647
Sebastian Ott1e532092014-01-27 13:28:10 +01001648void __init ccw_device_destroy_console(struct ccw_device *cdev)
1649{
1650 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1651 struct io_subchannel_private *io_priv = to_io_private(sch);
1652
1653 set_io_private(sch, NULL);
Halil Pasic37db8982019-03-26 12:41:09 +01001654 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1655 io_priv->dma_area, io_priv->dma_area_dma);
Qinglang Miao14d4c4f2020-12-01 14:31:50 +08001656 put_device(&sch->dev);
1657 put_device(&cdev->dev);
Sebastian Ott1e532092014-01-27 13:28:10 +01001658 kfree(io_priv);
1659}
1660
Sebastian Ott188561a2013-04-13 12:53:21 +02001661/**
1662 * ccw_device_wait_idle() - busy wait for device to become idle
1663 * @cdev: ccw device
1664 *
1665 * Poll until activity control is zero, that is, no function or data
1666 * transfer is pending/active.
1667 * Called with device lock being held.
1668 */
1669void ccw_device_wait_idle(struct ccw_device *cdev)
1670{
1671 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1672
1673 while (1) {
1674 cio_tsch(sch);
1675 if (sch->schib.scsw.cmd.actl == 0)
1676 break;
Heiko Carstense0d62dc2020-12-14 21:44:39 +01001677 udelay(100);
Sebastian Ott188561a2013-04-13 12:53:21 +02001678 }
1679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680#endif
1681
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001682/**
1683 * get_ccwdev_by_busid() - obtain device from a bus id
1684 * @cdrv: driver the device is owned by
1685 * @bus_id: bus id of the device to be searched
1686 *
1687 * This function searches all devices owned by @cdrv for a device with a bus
1688 * id matching @bus_id.
1689 * Returns:
1690 * If a match is found, its reference count of the found device is increased
1691 * and it is returned; else %NULL is returned.
1692 */
1693struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1694 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001696 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +01001698 dev = driver_find_device_by_name(&cdrv->driver, bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001700 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
1703/************************** device driver handling ************************/
1704
1705/* This is the implementation of the ccw_driver class. The probe, remove
1706 * and release methods are initially very similar to the device_driver
1707 * implementations, with the difference that they have ccw_device
1708 * arguments.
1709 *
1710 * A ccw driver also contains the information that is needed for
1711 * device matching.
1712 */
1713static int
1714ccw_device_probe (struct device *dev)
1715{
1716 struct ccw_device *cdev = to_ccwdev(dev);
1717 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1718 int ret;
1719
1720 cdev->drv = cdrv; /* to let the driver call _set_online */
Sebastian Ott137a14f2014-01-27 13:29:15 +01001721 ccw_device_set_int_class(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001724 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001725 cdev->private->int_class = IRQIO_CIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return ret;
1727 }
1728
1729 return 0;
1730}
1731
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001732static int ccw_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 struct ccw_device *cdev = to_ccwdev(dev);
1735 struct ccw_driver *cdrv = cdev->drv;
Sebastian Ott135a8b42018-03-15 15:03:43 +01001736 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int ret;
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (cdrv->remove)
1740 cdrv->remove(cdev);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001741
1742 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (cdev->online) {
1744 cdev->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 ret = ccw_device_offline(cdev);
1746 spin_unlock_irq(cdev->ccwlock);
1747 if (ret == 0)
1748 wait_event(cdev->private->wait_q,
1749 dev_fsm_final_state(cdev));
1750 else
Michael Ernst139b83dd2008-05-07 09:22:54 +02001751 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001752 "device 0.%x.%04x\n",
1753 ret, cdev->private->dev_id.ssid,
1754 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001755 /* Give up reference obtained in ccw_device_set_online(). */
1756 put_device(&cdev->dev);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001757 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001760 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001761 cdev->private->int_class = IRQIO_CIO;
Sebastian Ott135a8b42018-03-15 15:03:43 +01001762 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001763 spin_unlock_irq(cdev->ccwlock);
Sebastian Ott135a8b42018-03-15 15:03:43 +01001764 io_subchannel_quiesce(sch);
Sebastian Otta6ef1562015-09-07 19:51:39 +02001765 __disable_cmf(cdev);
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
1768}
1769
Cornelia Huck958974fb2007-10-12 16:11:21 +02001770static void ccw_device_shutdown(struct device *dev)
1771{
1772 struct ccw_device *cdev;
1773
1774 cdev = to_ccwdev(dev);
1775 if (cdev->drv && cdev->drv->shutdown)
1776 cdev->drv->shutdown(cdev);
Sebastian Ott1bc66642015-09-15 13:11:42 +02001777 __disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001778}
1779
Sebastian Ottd5ab5272011-03-23 10:16:03 +01001780static struct bus_type ccw_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001781 .name = "ccw",
1782 .match = ccw_bus_match,
1783 .uevent = ccw_uevent,
1784 .probe = ccw_device_probe,
1785 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001786 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001787};
1788
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001789/**
1790 * ccw_driver_register() - register a ccw driver
1791 * @cdriver: driver to be registered
1792 *
1793 * This function is mainly a wrapper around driver_register().
1794 * Returns:
1795 * %0 on success and a negative error value on failure.
1796 */
1797int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 struct device_driver *drv = &cdriver->driver;
1800
1801 drv->bus = &ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 return driver_register(drv);
1804}
1805
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001806/**
1807 * ccw_driver_unregister() - deregister a ccw driver
1808 * @cdriver: driver to be deregistered
1809 *
1810 * This function is mainly a wrapper around driver_unregister().
1811 */
1812void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 driver_unregister(&cdriver->driver);
1815}
1816
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001817static void ccw_device_todo(struct work_struct *work)
1818{
1819 struct ccw_device_private *priv;
1820 struct ccw_device *cdev;
1821 struct subchannel *sch;
1822 enum cdev_todo todo;
1823
1824 priv = container_of(work, struct ccw_device_private, todo_work);
1825 cdev = priv->cdev;
1826 sch = to_subchannel(cdev->dev.parent);
1827 /* Find out todo. */
1828 spin_lock_irq(cdev->ccwlock);
1829 todo = priv->todo;
1830 priv->todo = CDEV_TODO_NOTHING;
1831 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1832 priv->dev_id.ssid, priv->dev_id.devno, todo);
1833 spin_unlock_irq(cdev->ccwlock);
1834 /* Perform todo. */
1835 switch (todo) {
1836 case CDEV_TODO_ENABLE_CMF:
1837 cmf_reenable(cdev);
1838 break;
1839 case CDEV_TODO_REBIND:
1840 ccw_device_do_unbind_bind(cdev);
1841 break;
1842 case CDEV_TODO_REGISTER:
1843 io_subchannel_register(cdev);
1844 break;
1845 case CDEV_TODO_UNREG_EVAL:
1846 if (!sch_is_pseudo_sch(sch))
1847 css_schedule_eval(sch->schid);
Joe Perchesb09fcec2020-03-10 13:39:50 -07001848 fallthrough;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001849 case CDEV_TODO_UNREG:
Vineeth Vijayan22977912021-04-25 10:41:59 +02001850 spin_lock_irq(sch->lock);
1851 sch_set_cdev(sch, NULL);
1852 spin_unlock_irq(sch->lock);
1853 ccw_device_unregister(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001854 break;
1855 default:
1856 break;
1857 }
1858 /* Release workqueue ref. */
1859 put_device(&cdev->dev);
1860}
1861
1862/**
1863 * ccw_device_sched_todo - schedule ccw device operation
1864 * @cdev: ccw device
1865 * @todo: todo
1866 *
1867 * Schedule the operation identified by @todo to be performed on the slow path
1868 * workqueue. Do nothing if another operation with higher priority is already
1869 * scheduled. Needs to be called with ccwdev lock held.
1870 */
1871void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
1872{
1873 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
1874 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
1875 todo);
1876 if (cdev->private->todo >= todo)
1877 return;
1878 cdev->private->todo = todo;
1879 /* Get workqueue ref. */
1880 if (!get_device(&cdev->dev))
1881 return;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001882 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001883 /* Already queued, release workqueue ref. */
1884 put_device(&cdev->dev);
1885 }
1886}
1887
Michael Ernstfd0457a2010-08-09 18:12:50 +02001888/**
1889 * ccw_device_siosl() - initiate logging
1890 * @cdev: ccw device
1891 *
1892 * This function is used to invoke model-dependent logging within the channel
1893 * subsystem.
1894 */
1895int ccw_device_siosl(struct ccw_device *cdev)
1896{
1897 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1898
1899 return chsc_siosl(sch->schid);
1900}
1901EXPORT_SYMBOL_GPL(ccw_device_siosl);
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903EXPORT_SYMBOL(ccw_device_set_online);
1904EXPORT_SYMBOL(ccw_device_set_offline);
1905EXPORT_SYMBOL(ccw_driver_register);
1906EXPORT_SYMBOL(ccw_driver_unregister);
1907EXPORT_SYMBOL(get_ccwdev_by_busid);