blob: 318d8269f5dee10c56114224b4a08b8b617a96da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2002, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08006 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Martin Schwidefsky (schwidefsky@de.ibm.com)
Paul Gortmakera00f7612016-10-30 16:37:24 -04008 *
9 * License: GPL
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Peter Oberparleitera7ae2c022009-12-07 12:51:20 +010011
12#define KMSG_COMPONENT "cio"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Paul Gortmakera00f7612016-10-30 16:37:24 -040015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/spinlock.h>
18#include <linux/errno.h>
19#include <linux/err.h>
20#include <linux/slab.h>
21#include <linux/list.h>
22#include <linux/device.h>
23#include <linux/workqueue.h>
Sebastian Ott188561a2013-04-13 12:53:21 +020024#include <linux/delay.h>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010025#include <linux/timer.h>
Peter Oberparleiterde400d62011-10-30 15:16:04 +010026#include <linux/kernel_stat.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010027#include <linux/sched/signal.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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
602static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800603static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static DEVICE_ATTR(online, 0644, online_show, online_store);
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);
Sebastian Ott84c57ad2013-01-28 19:32:27 +0100607static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
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
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100646static int match_dev_id(struct device *dev, 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);
649 struct ccw_dev_id *dev_id = 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);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100691 /* Release reference of parent subchannel. */
692 put_device(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 kfree(cdev->private);
694 kfree(cdev);
695}
696
Cornelia Huck7674da72006-12-08 15:54:21 +0100697static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
698{
699 struct ccw_device *cdev;
700
701 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
702 if (cdev) {
703 cdev->private = kzalloc(sizeof(struct ccw_device_private),
704 GFP_KERNEL | GFP_DMA);
705 if (cdev->private)
706 return cdev;
707 }
708 kfree(cdev);
709 return ERR_PTR(-ENOMEM);
710}
711
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100712static void ccw_device_todo(struct work_struct *work);
713
Cornelia Huck7674da72006-12-08 15:54:21 +0100714static int io_subchannel_initialize_dev(struct subchannel *sch,
715 struct ccw_device *cdev)
716{
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200717 struct ccw_device_private *priv = cdev->private;
718 int ret;
719
720 priv->cdev = cdev;
721 priv->int_class = IRQIO_CIO;
722 priv->state = DEV_STATE_NOT_OPER;
723 priv->dev_id.devno = sch->schib.pmcw.dev;
724 priv->dev_id.ssid = sch->schid.ssid;
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200725
726 INIT_WORK(&priv->todo_work, ccw_device_todo);
727 INIT_LIST_HEAD(&priv->cmb_list);
728 init_waitqueue_head(&priv->wait_q);
Kees Cook846d0c62017-10-16 16:43:25 -0700729 timer_setup(&priv->timer, ccw_device_timeout, 0);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200730
731 atomic_set(&priv->onoff, 0);
732 cdev->ccwlock = sch->lock;
Cornelia Huck7674da72006-12-08 15:54:21 +0100733 cdev->dev.parent = &sch->dev;
734 cdev->dev.release = ccw_device_release;
Cornelia Huckef995162007-04-27 16:01:39 +0200735 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100736 /* Do first half of device_register. */
737 device_initialize(&cdev->dev);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200738 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
739 cdev->private->dev_id.devno);
740 if (ret)
741 goto out_put;
Cornelia Huck7674da72006-12-08 15:54:21 +0100742 if (!get_device(&sch->dev)) {
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200743 ret = -ENODEV;
744 goto out_put;
Cornelia Huck7674da72006-12-08 15:54:21 +0100745 }
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200746 priv->flags.initialized = 1;
747 spin_lock_irq(sch->lock);
748 sch_set_cdev(sch, cdev);
749 spin_unlock_irq(sch->lock);
Cornelia Huck7674da72006-12-08 15:54:21 +0100750 return 0;
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200751
752out_put:
753 /* Release reference from device_initialize(). */
754 put_device(&cdev->dev);
755 return ret;
Cornelia Huck7674da72006-12-08 15:54:21 +0100756}
757
758static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
759{
760 struct ccw_device *cdev;
761 int ret;
762
763 cdev = io_subchannel_allocate_dev(sch);
764 if (!IS_ERR(cdev)) {
765 ret = io_subchannel_initialize_dev(sch, cdev);
Sebastian Ott06739a82009-08-23 18:09:04 +0200766 if (ret)
Cornelia Huck7674da72006-12-08 15:54:21 +0100767 cdev = ERR_PTR(ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100768 }
769 return cdev;
770}
771
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100772static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100773
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100774static void sch_create_and_recog_new_device(struct subchannel *sch)
775{
776 struct ccw_device *cdev;
777
778 /* Need to allocate a new ccw device. */
779 cdev = io_subchannel_create_ccwdev(sch);
780 if (IS_ERR(cdev)) {
781 /* OK, we did everything we could... */
782 css_sch_device_unregister(sch);
783 return;
784 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100785 /* Start recognition for the new ccw device. */
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100786 io_subchannel_recog(cdev, sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/*
790 * Register recognized device.
791 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100792static void io_subchannel_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct subchannel *sch;
Sebastian Otta2901562010-03-08 12:25:17 +0100795 int ret, adjust_init_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 unsigned long flags;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100799 /*
800 * Check if subchannel is still registered. It may have become
801 * unregistered if a machine check hit us after finishing
802 * device recognition but before the register work could be
803 * queued.
804 */
805 if (!device_is_registered(&sch->dev))
806 goto out_err;
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200807 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100808 /*
809 * io_subchannel_register() will also be called after device
810 * recognition has been done for a boxed device (which will already
811 * be registered). We need to reprobe since we may now have sense id
812 * information.
813 */
Cornelia Huckd6a30762008-12-25 13:39:11 +0100814 if (device_is_registered(&cdev->dev)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100815 if (!cdev->drv) {
816 ret = device_reprobe(&cdev->dev);
817 if (ret)
818 /* We can't do much here. */
Michael Ernst139b83dd2008-05-07 09:22:54 +0200819 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200820 " %d for 0.%x.%04x\n", ret,
821 cdev->private->dev_id.ssid,
822 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100823 }
Sebastian Otta2901562010-03-08 12:25:17 +0100824 adjust_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 goto out;
826 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700827 /*
828 * Now we know this subchannel will stay, we can throw
829 * our delayed uevent.
830 */
Ming Leif67f1292009-03-01 21:10:49 +0800831 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700832 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* make it known to the system */
Sebastian Ott2c3e7e12014-06-11 13:06:57 +0200834 ret = ccw_device_add(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200836 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
837 cdev->private->dev_id.ssid,
838 cdev->private->dev_id.devno, ret);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100839 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100840 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100841 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6eff2082008-12-25 13:39:07 +0100842 /* Release initial device reference. */
843 put_device(&cdev->dev);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100844 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846out:
847 cdev->private->flags.recog_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 wake_up(&cdev->private->wait_q);
Cornelia Huck5fb6b852008-12-25 13:39:08 +0100849out_err:
Sebastian Otta2901562010-03-08 12:25:17 +0100850 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 wake_up(&ccw_device_init_wq);
852}
853
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100854static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 struct subchannel *sch;
857
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200858 /* Get subchannel reference for local processing. */
859 if (!get_device(cdev->dev.parent))
860 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200862 css_sch_device_unregister(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200863 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 put_device(&sch->dev);
865}
866
867/*
868 * subchannel recognition done. Called from the state machine.
869 */
870void
871io_subchannel_recog_done(struct ccw_device *cdev)
872{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (css_init_done == 0) {
874 cdev->private->flags.recog_done = 1;
875 return;
876 }
877 switch (cdev->private->state) {
Sebastian Ott47593bf2009-03-31 19:16:05 +0200878 case DEV_STATE_BOXED:
879 /* Device did not respond in time. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 case DEV_STATE_NOT_OPER:
881 cdev->private->flags.recog_done = 1;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100882 /* Remove device found not operational. */
883 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (atomic_dec_and_test(&ccw_device_init_count))
885 wake_up(&ccw_device_init_wq);
886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 case DEV_STATE_OFFLINE:
888 /*
889 * We can't register the device in interrupt context so
890 * we schedule a work item.
891 */
Peter Oberparleiter37de53b2009-12-07 12:51:19 +0100892 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
894 }
895}
896
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100897static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* Increase counter of devices currently in recognition. */
900 atomic_inc(&ccw_device_init_count);
901
902 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100903 spin_lock_irq(sch->lock);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +0100904 ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100905 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100908static int ccw_device_move_to_sch(struct ccw_device *cdev,
909 struct subchannel *sch)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100910{
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100911 struct subchannel *old_sch;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100912 int rc, old_enabled = 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100913
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100914 old_sch = to_subchannel(cdev->dev.parent);
915 /* Obtain child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100916 if (!get_device(&sch->dev))
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100917 return -ENODEV;
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100918
919 if (!sch_is_pseudo_sch(old_sch)) {
920 spin_lock_irq(old_sch->lock);
921 old_enabled = old_sch->schib.pmcw.ena;
922 rc = 0;
923 if (old_enabled)
924 rc = cio_disable_subchannel(old_sch);
925 spin_unlock_irq(old_sch->lock);
926 if (rc == -EBUSY) {
927 /* Release child reference for new parent. */
928 put_device(&sch->dev);
929 return rc;
930 }
931 }
932
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100933 mutex_lock(&sch->reg_mutex);
Cornelia Huckffa6a702009-03-04 12:44:00 +0100934 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100935 mutex_unlock(&sch->reg_mutex);
936 if (rc) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100937 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100938 cdev->private->dev_id.ssid,
939 cdev->private->dev_id.devno, sch->schid.ssid,
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100940 sch->schib.pmcw.dev, rc);
Sebastian Ott0c609fc2009-12-07 12:51:37 +0100941 if (old_enabled) {
942 /* Try to reenable the old subchannel. */
943 spin_lock_irq(old_sch->lock);
944 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
945 spin_unlock_irq(old_sch->lock);
946 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100947 /* Release child reference for new parent. */
Cornelia Huck6eff2082008-12-25 13:39:07 +0100948 put_device(&sch->dev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100949 return rc;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100950 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100951 /* Clean up old subchannel. */
952 if (!sch_is_pseudo_sch(old_sch)) {
953 spin_lock_irq(old_sch->lock);
954 sch_set_cdev(old_sch, NULL);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100955 spin_unlock_irq(old_sch->lock);
956 css_schedule_eval(old_sch->schid);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100957 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100958 /* Release child reference for old parent. */
959 put_device(&old_sch->dev);
960 /* Initialize new subchannel. */
961 spin_lock_irq(sch->lock);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100962 cdev->ccwlock = sch->lock;
963 if (!sch_is_pseudo_sch(sch))
964 sch_set_cdev(sch, cdev);
965 spin_unlock_irq(sch->lock);
966 if (!sch_is_pseudo_sch(sch))
967 css_update_ssd_info(sch);
968 return 0;
969}
970
971static int ccw_device_move_to_orph(struct ccw_device *cdev)
972{
973 struct subchannel *sch = to_subchannel(cdev->dev.parent);
974 struct channel_subsystem *css = to_css(sch->dev.parent);
975
976 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100977}
978
Cornelia Huck602b20f2008-01-26 14:10:39 +0100979static void io_subchannel_irq(struct subchannel *sch)
980{
981 struct ccw_device *cdev;
982
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100983 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100984
Sebastian Ottefd986d2009-09-11 10:28:18 +0200985 CIO_TRACE_EVENT(6, "IRQ");
986 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
Cornelia Huck602b20f2008-01-26 14:10:39 +0100987 if (cdev)
988 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
Peter Oberparleiterde400d62011-10-30 15:16:04 +0100989 else
Heiko Carstens420f42e2013-01-02 15:18:18 +0100990 inc_irq_stat(IRQIO_CIO);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100991}
992
Sebastian Ott13952ec2008-12-25 13:39:13 +0100993void io_subchannel_init_config(struct subchannel *sch)
994{
995 memset(&sch->config, 0, sizeof(sch->config));
996 sch->config.csense = 1;
Sebastian Ott13952ec2008-12-25 13:39:13 +0100997}
998
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +0200999static void io_subchannel_init_fields(struct subchannel *sch)
1000{
1001 if (cio_is_console(sch->schid))
1002 sch->opm = 0xff;
1003 else
1004 sch->opm = chp_get_sch_opm(sch);
1005 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001006 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001007
1008 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1009 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1010 sch->schib.pmcw.dev, sch->schid.ssid,
1011 sch->schid.sch_no, sch->schib.pmcw.pim,
1012 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
Sebastian Ott13952ec2008-12-25 13:39:13 +01001013
1014 io_subchannel_init_config(sch);
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001015}
1016
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001017/*
1018 * Note: We always return 0 so that we bind to the device even on error.
1019 * This is needed so that our remove function is called on unregister.
1020 */
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001021static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001023 struct io_subchannel_private *io_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 struct ccw_device *cdev;
1025 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001027 if (cio_is_console(sch->schid)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001028 rc = sysfs_create_group(&sch->dev.kobj,
1029 &io_subchannel_attr_group);
1030 if (rc)
1031 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1032 "attributes for subchannel "
1033 "0.%x.%04x (rc=%d)\n",
1034 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /*
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001036 * The console subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001037 * Throw the delayed uevent for the subchannel, register
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001038 * the ccw_device and exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Ming Leif67f1292009-03-01 21:10:49 +08001040 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001041 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Peter Oberparleiter6d7c5af2009-10-14 12:43:50 +02001042 cdev = sch_get_cdev(sch);
Sebastian Ott2c3e7e12014-06-11 13:06:57 +02001043 rc = ccw_device_add(cdev);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001044 if (rc) {
1045 /* Release online reference. */
1046 put_device(&cdev->dev);
1047 goto out_schedule;
1048 }
Sebastian Ott0ad8f714a2013-04-13 13:06:27 +02001049 if (atomic_dec_and_test(&ccw_device_init_count))
1050 wake_up(&ccw_device_init_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 0;
1052 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001053 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001054 rc = cio_commit_config(sch);
1055 if (rc)
1056 goto out_schedule;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001057 rc = sysfs_create_group(&sch->dev.kobj,
1058 &io_subchannel_attr_group);
1059 if (rc)
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001060 goto out_schedule;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001061 /* Allocate I/O subchannel private data. */
Sebastian Ottf92519e2011-03-15 17:08:27 +01001062 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1063 if (!io_priv)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001064 goto out_schedule;
Sebastian Ottf92519e2011-03-15 17:08:27 +01001065
1066 set_io_private(sch, io_priv);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001067 css_schedule_eval(sch->schid);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001068 return 0;
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001069
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001070out_schedule:
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001071 spin_lock_irq(sch->lock);
1072 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1073 spin_unlock_irq(sch->lock);
Cornelia Huck90ed2b62008-12-25 13:39:09 +01001074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001078io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Sebastian Ottf92519e2011-03-15 17:08:27 +01001080 struct io_subchannel_private *io_priv = to_io_private(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001083 cdev = sch_get_cdev(sch);
1084 if (!cdev)
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001085 goto out_free;
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001086 io_subchannel_quiesce(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /* Set ccw device to not operational and drop reference. */
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001088 spin_lock_irq(cdev->ccwlock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001089 sch_set_cdev(sch, NULL);
Sebastian Ottf92519e2011-03-15 17:08:27 +01001090 set_io_private(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 cdev->private->state = DEV_STATE_NOT_OPER;
Sebastian Ott7a8ad102009-12-07 12:51:39 +01001092 spin_unlock_irq(cdev->ccwlock);
Cornelia Huckef995162007-04-27 16:01:39 +02001093 ccw_device_unregister(cdev);
Peter Oberparleiter48e4c382009-12-07 12:51:15 +01001094out_free:
Sebastian Ottf92519e2011-03-15 17:08:27 +01001095 kfree(io_priv);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001096 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098}
1099
Cornelia Huck602b20f2008-01-26 14:10:39 +01001100static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct ccw_device *cdev;
1103
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001104 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (cdev)
1106 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1107}
1108
Cornelia Huckc820de32008-07-14 09:58:45 +02001109static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 struct ccw_device *cdev;
1112
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001113 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!cdev)
1115 return;
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001116 if (cio_update_schib(sch))
1117 goto err;
1118 /* Check for I/O on path. */
1119 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1120 goto out;
1121 if (cdev->private->state == DEV_STATE_ONLINE) {
1122 ccw_device_kill_io(cdev);
1123 goto out;
1124 }
1125 if (cio_clear(sch))
1126 goto err;
1127out:
1128 /* Trigger path verification. */
1129 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1130 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001131
Peter Oberparleiter4257aae2009-12-07 12:51:29 +01001132err:
1133 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
Cornelia Huckc820de32008-07-14 09:58:45 +02001134}
1135
Cornelia Huck99611f82008-07-14 09:59:02 +02001136static int io_subchannel_chp_event(struct subchannel *sch,
1137 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001138{
Sebastian Ott585b9542010-10-25 16:10:34 +02001139 struct ccw_device *cdev = sch_get_cdev(sch);
Cornelia Huckc820de32008-07-14 09:58:45 +02001140 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001141
Cornelia Huck99611f82008-07-14 09:59:02 +02001142 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001143 if (!mask)
1144 return 0;
1145 switch (event) {
1146 case CHP_VARY_OFF:
1147 sch->opm &= ~mask;
1148 sch->lpm &= ~mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001149 if (cdev)
1150 cdev->private->path_gone_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001151 io_subchannel_terminate_path(sch, mask);
1152 break;
1153 case CHP_VARY_ON:
1154 sch->opm |= mask;
1155 sch->lpm |= mask;
Sebastian Ott585b9542010-10-25 16:10:34 +02001156 if (cdev)
1157 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001158 io_subchannel_verify(sch);
1159 break;
1160 case CHP_OFFLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001161 if (cio_update_schib(sch))
Cornelia Huckc820de32008-07-14 09:58:45 +02001162 return -ENODEV;
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_ONLINE:
Sebastian Ottcdb912a2008-12-25 13:39:12 +01001168 if (cio_update_schib(sch))
1169 return -ENODEV;
Cornelia Huckc820de32008-07-14 09:58:45 +02001170 sch->lpm |= mask & sch->opm;
Sebastian Ott585b9542010-10-25 16:10:34 +02001171 if (cdev)
1172 cdev->private->path_new_mask |= mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001173 io_subchannel_verify(sch);
1174 break;
1175 }
1176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001179static void io_subchannel_quiesce(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 struct ccw_device *cdev;
1182 int ret;
1183
Sebastian Ott56e6b792009-12-07 12:51:35 +01001184 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001185 cdev = sch_get_cdev(sch);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001186 if (cio_is_console(sch->schid))
Sebastian Ott56e6b792009-12-07 12:51:35 +01001187 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (!sch->schib.pmcw.ena)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001189 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ret = cio_disable_subchannel(sch);
1191 if (ret != -EBUSY)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001192 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (cdev->handler)
Sebastian Ott56e6b792009-12-07 12:51:35 +01001194 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1195 while (ret == -EBUSY) {
1196 cdev->private->state = DEV_STATE_QUIESCE;
Peter Oberparleiter376ae472010-10-25 16:10:44 +02001197 cdev->private->iretry = 255;
Sebastian Ott56e6b792009-12-07 12:51:35 +01001198 ret = ccw_device_cancel_halt_clear(cdev);
1199 if (ret == -EBUSY) {
1200 ccw_device_set_timeout(cdev, HZ/10);
1201 spin_unlock_irq(sch->lock);
1202 wait_event(cdev->private->wait_q,
1203 cdev->private->state != DEV_STATE_QUIESCE);
1204 spin_lock_irq(sch->lock);
1205 }
1206 ret = cio_disable_subchannel(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Sebastian Ott56e6b792009-12-07 12:51:35 +01001208out_unlock:
1209 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Sebastian Ott6e9a0f62009-12-07 12:51:38 +01001212static void io_subchannel_shutdown(struct subchannel *sch)
1213{
1214 io_subchannel_quiesce(sch);
1215}
1216
Cornelia Huckc820de32008-07-14 09:58:45 +02001217static int device_is_disconnected(struct ccw_device *cdev)
1218{
1219 if (!cdev)
1220 return 0;
1221 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1222 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1223}
1224
1225static int recovery_check(struct device *dev, void *data)
1226{
1227 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott55fb7342017-09-14 13:55:22 +02001228 struct subchannel *sch;
Cornelia Huckc820de32008-07-14 09:58:45 +02001229 int *redo = data;
1230
1231 spin_lock_irq(cdev->ccwlock);
1232 switch (cdev->private->state) {
Sebastian Ott55fb7342017-09-14 13:55:22 +02001233 case DEV_STATE_ONLINE:
1234 sch = to_subchannel(cdev->dev.parent);
1235 if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1236 break;
1237 /* fall through */
Cornelia Huckc820de32008-07-14 09:58:45 +02001238 case DEV_STATE_DISCONNECTED:
1239 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1240 cdev->private->dev_id.ssid,
1241 cdev->private->dev_id.devno);
1242 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1243 *redo = 1;
1244 break;
1245 case DEV_STATE_DISCONNECTED_SENSE_ID:
1246 *redo = 1;
1247 break;
1248 }
1249 spin_unlock_irq(cdev->ccwlock);
1250
1251 return 0;
1252}
1253
1254static void recovery_work_func(struct work_struct *unused)
1255{
1256 int redo = 0;
1257
1258 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1259 if (redo) {
1260 spin_lock_irq(&recovery_lock);
1261 if (!timer_pending(&recovery_timer)) {
1262 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1263 recovery_phase++;
1264 mod_timer(&recovery_timer, jiffies +
1265 recovery_delay[recovery_phase] * HZ);
1266 }
1267 spin_unlock_irq(&recovery_lock);
1268 } else
Sebastian Ott55fb7342017-09-14 13:55:22 +02001269 CIO_MSG_EVENT(3, "recovery: end\n");
Cornelia Huckc820de32008-07-14 09:58:45 +02001270}
1271
1272static DECLARE_WORK(recovery_work, recovery_work_func);
1273
Kees Cook846d0c62017-10-16 16:43:25 -07001274static void recovery_func(struct timer_list *unused)
Cornelia Huckc820de32008-07-14 09:58:45 +02001275{
1276 /*
1277 * We can't do our recovery in softirq context and it's not
1278 * performance critical, so we schedule it.
1279 */
1280 schedule_work(&recovery_work);
1281}
1282
Sebastian Ott55fb7342017-09-14 13:55:22 +02001283void ccw_device_schedule_recovery(void)
Cornelia Huckc820de32008-07-14 09:58:45 +02001284{
1285 unsigned long flags;
1286
Sebastian Ott55fb7342017-09-14 13:55:22 +02001287 CIO_MSG_EVENT(3, "recovery: schedule\n");
Cornelia Huckc820de32008-07-14 09:58:45 +02001288 spin_lock_irqsave(&recovery_lock, flags);
1289 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1290 recovery_phase = 0;
1291 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1292 }
1293 spin_unlock_irqrestore(&recovery_lock, flags);
1294}
1295
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001296static int purge_fn(struct device *dev, void *data)
1297{
1298 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001299 struct ccw_dev_id *id = &cdev->private->dev_id;
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001300
1301 spin_lock_irq(cdev->ccwlock);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001302 if (is_blacklisted(id->ssid, id->devno) &&
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001303 (cdev->private->state == DEV_STATE_OFFLINE) &&
1304 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001305 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1306 id->devno);
1307 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Peter Oberparleitera2fc8482011-04-04 09:43:32 +02001308 atomic_set(&cdev->private->onoff, 0);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001309 }
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001310 spin_unlock_irq(cdev->ccwlock);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +02001311 /* Abort loop in case of pending signal. */
1312 if (signal_pending(current))
1313 return -EINTR;
1314
1315 return 0;
1316}
1317
1318/**
1319 * ccw_purge_blacklisted - purge unused, blacklisted devices
1320 *
1321 * Unregister all ccw devices that are offline and on the blacklist.
1322 */
1323int ccw_purge_blacklisted(void)
1324{
1325 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1326 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1327 return 0;
1328}
1329
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001330void ccw_device_set_disconnected(struct ccw_device *cdev)
Cornelia Huckc820de32008-07-14 09:58:45 +02001331{
1332 if (!cdev)
1333 return;
1334 ccw_device_set_timeout(cdev, 0);
1335 cdev->private->flags.fake_irb = 0;
1336 cdev->private->state = DEV_STATE_DISCONNECTED;
1337 if (cdev->online)
1338 ccw_device_schedule_recovery();
1339}
1340
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001341void ccw_device_set_notoper(struct ccw_device *cdev)
1342{
1343 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1344
1345 CIO_TRACE_EVENT(2, "notoper");
Cornelia Huckb9d3aed2008-10-10 21:33:11 +02001346 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001347 ccw_device_set_timeout(cdev, 0);
1348 cio_disable_subchannel(sch);
1349 cdev->private->state = DEV_STATE_NOT_OPER;
1350}
1351
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001352enum io_sch_action {
1353 IO_SCH_UNREG,
1354 IO_SCH_ORPH_UNREG,
1355 IO_SCH_ATTACH,
1356 IO_SCH_UNREG_ATTACH,
1357 IO_SCH_ORPH_ATTACH,
1358 IO_SCH_REPROBE,
1359 IO_SCH_VERIFY,
1360 IO_SCH_DISC,
1361 IO_SCH_NOP,
1362};
1363
1364static enum io_sch_action sch_get_action(struct subchannel *sch)
Cornelia Huckc820de32008-07-14 09:58:45 +02001365{
Cornelia Huckc820de32008-07-14 09:58:45 +02001366 struct ccw_device *cdev;
1367
Cornelia Huckc820de32008-07-14 09:58:45 +02001368 cdev = sch_get_cdev(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001369 if (cio_update_schib(sch)) {
1370 /* Not operational. */
1371 if (!cdev)
1372 return IO_SCH_UNREG;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001373 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001374 return IO_SCH_UNREG;
1375 return IO_SCH_ORPH_UNREG;
Cornelia Huckc820de32008-07-14 09:58:45 +02001376 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001377 /* Operational. */
1378 if (!cdev)
1379 return IO_SCH_ATTACH;
1380 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001381 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001382 return IO_SCH_UNREG_ATTACH;
1383 return IO_SCH_ORPH_ATTACH;
Cornelia Huckc820de32008-07-14 09:58:45 +02001384 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001385 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001386 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001387 return IO_SCH_UNREG;
1388 return IO_SCH_DISC;
Cornelia Huckc820de32008-07-14 09:58:45 +02001389 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001390 if (device_is_disconnected(cdev))
1391 return IO_SCH_REPROBE;
Sebastian Ott31370f752012-10-24 11:22:52 +02001392 if (cdev->online && !cdev->private->flags.resuming)
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001393 return IO_SCH_VERIFY;
Sebastian Ott43d0be72012-09-05 14:19:42 +02001394 if (cdev->private->state == DEV_STATE_NOT_OPER)
1395 return IO_SCH_UNREG_ATTACH;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001396 return IO_SCH_NOP;
1397}
1398
1399/**
1400 * io_subchannel_sch_event - process subchannel event
1401 * @sch: subchannel
1402 * @process: non-zero if function is called in process context
1403 *
1404 * An unspecified event occurred for this subchannel. Adjust data according
1405 * to the current operational state of the subchannel and device. Return
1406 * zero when the event has been handled sufficiently or -EAGAIN when this
1407 * function should be called again in process context.
1408 */
1409static int io_subchannel_sch_event(struct subchannel *sch, int process)
1410{
1411 unsigned long flags;
1412 struct ccw_device *cdev;
1413 struct ccw_dev_id dev_id;
1414 enum io_sch_action action;
1415 int rc = -EAGAIN;
1416
1417 spin_lock_irqsave(sch->lock, flags);
1418 if (!device_is_registered(&sch->dev))
1419 goto out_unlock;
Peter Oberparleiter390935a2009-12-07 12:51:18 +01001420 if (work_pending(&sch->todo_work))
1421 goto out_unlock;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001422 cdev = sch_get_cdev(sch);
1423 if (cdev && work_pending(&cdev->private->todo_work))
1424 goto out_unlock;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001425 action = sch_get_action(sch);
1426 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1427 sch->schid.ssid, sch->schid.sch_no, process,
1428 action);
1429 /* Perform immediate actions while holding the lock. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001430 switch (action) {
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001431 case IO_SCH_REPROBE:
1432 /* Trigger device recognition. */
Cornelia Huckc820de32008-07-14 09:58:45 +02001433 ccw_device_trigger_reprobe(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001434 rc = 0;
1435 goto out_unlock;
1436 case IO_SCH_VERIFY:
1437 /* Trigger path verification. */
1438 io_subchannel_verify(sch);
1439 rc = 0;
1440 goto out_unlock;
1441 case IO_SCH_DISC:
1442 ccw_device_set_disconnected(cdev);
1443 rc = 0;
1444 goto out_unlock;
1445 case IO_SCH_ORPH_UNREG:
1446 case IO_SCH_ORPH_ATTACH:
Peter Oberparleiter6afcc772009-10-06 10:34:02 +02001447 ccw_device_set_disconnected(cdev);
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001448 break;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001449 case IO_SCH_UNREG_ATTACH:
1450 case IO_SCH_UNREG:
Sebastian Ott16d2ce22010-11-10 10:05:53 +01001451 if (!cdev)
1452 break;
1453 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1454 /*
1455 * Note: delayed work triggered by this event
1456 * and repeated calls to sch_event are synchronized
1457 * by the above check for work_pending(cdev).
1458 */
1459 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1460 } else
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001461 ccw_device_set_notoper(cdev);
1462 break;
1463 case IO_SCH_NOP:
1464 rc = 0;
1465 goto out_unlock;
Cornelia Huckc820de32008-07-14 09:58:45 +02001466 default:
1467 break;
1468 }
1469 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001470 /* All other actions require process context. */
1471 if (!process)
1472 goto out;
1473 /* Handle attached ccw device. */
1474 switch (action) {
1475 case IO_SCH_ORPH_UNREG:
1476 case IO_SCH_ORPH_ATTACH:
1477 /* Move ccw device to orphanage. */
1478 rc = ccw_device_move_to_orph(cdev);
1479 if (rc)
1480 goto out;
1481 break;
1482 case IO_SCH_UNREG_ATTACH:
Sebastian Ott3368ba22012-09-05 14:20:41 +02001483 spin_lock_irqsave(sch->lock, flags);
Sebastian Ott74b61272010-10-25 16:10:26 +02001484 if (cdev->private->flags.resuming) {
1485 /* Device will be handled later. */
1486 rc = 0;
Sebastian Ott3368ba22012-09-05 14:20:41 +02001487 goto out_unlock;
Sebastian Ott74b61272010-10-25 16:10:26 +02001488 }
Sebastian Ott3368ba22012-09-05 14:20:41 +02001489 sch_set_cdev(sch, NULL);
1490 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001491 /* Unregister ccw device. */
Sebastian Ott74b61272010-10-25 16:10:26 +02001492 ccw_device_unregister(cdev);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001493 break;
1494 default:
1495 break;
1496 }
1497 /* Handle subchannel. */
1498 switch (action) {
1499 case IO_SCH_ORPH_UNREG:
1500 case IO_SCH_UNREG:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001501 if (!cdev || !cdev->private->flags.resuming)
1502 css_sch_device_unregister(sch);
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001503 break;
1504 case IO_SCH_ORPH_ATTACH:
1505 case IO_SCH_UNREG_ATTACH:
1506 case IO_SCH_ATTACH:
1507 dev_id.ssid = sch->schid.ssid;
1508 dev_id.devno = sch->schib.pmcw.dev;
1509 cdev = get_ccwdev_by_dev_id(&dev_id);
1510 if (!cdev) {
1511 sch_create_and_recog_new_device(sch);
1512 break;
1513 }
1514 rc = ccw_device_move_to_sch(cdev, sch);
1515 if (rc) {
1516 /* Release reference from get_ccwdev_by_dev_id() */
1517 put_device(&cdev->dev);
1518 goto out;
1519 }
1520 spin_lock_irqsave(sch->lock, flags);
1521 ccw_device_trigger_reprobe(cdev);
1522 spin_unlock_irqrestore(sch->lock, flags);
1523 /* Release reference from get_ccwdev_by_dev_id() */
1524 put_device(&cdev->dev);
1525 break;
1526 default:
1527 break;
1528 }
1529 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001530
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +01001531out_unlock:
1532 spin_unlock_irqrestore(sch->lock, flags);
1533out:
1534 return rc;
Cornelia Huckc820de32008-07-14 09:58:45 +02001535}
1536
Sebastian Ott137a14f2014-01-27 13:29:15 +01001537static void ccw_device_set_int_class(struct ccw_device *cdev)
1538{
1539 struct ccw_driver *cdrv = cdev->drv;
1540
1541 /* Note: we interpret class 0 in this context as an uninitialized
1542 * field since it translates to a non-I/O interrupt class. */
1543 if (cdrv->int_class != 0)
1544 cdev->private->int_class = cdrv->int_class;
1545 else
1546 cdev->private->int_class = IRQIO_CIO;
1547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549#ifdef CONFIG_CCW_CONSOLE
Sebastian Ott1e532092014-01-27 13:28:10 +01001550int __init ccw_device_enable_console(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Sebastian Ott1e532092014-01-27 13:28:10 +01001552 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int rc;
1554
Sebastian Ott1e532092014-01-27 13:28:10 +01001555 if (!cdev->drv || !cdev->handler)
1556 return -EINVAL;
1557
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001558 io_subchannel_init_fields(sch);
Sebastian Ottf444cc02008-12-25 13:39:14 +01001559 rc = cio_commit_config(sch);
1560 if (rc)
1561 return rc;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001562 sch->driver = &io_subchannel_driver;
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001563 io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* Now wait for the async. recognition to come to an end. */
1565 spin_lock_irq(cdev->ccwlock);
1566 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001567 ccw_device_wait_idle(cdev);
1568
Sebastian Ottafdfed02013-04-13 13:03:03 +02001569 /* Hold on to an extra reference while device is online. */
1570 get_device(&cdev->dev);
1571 rc = ccw_device_online(cdev);
1572 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 goto out_unlock;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 while (!dev_fsm_final_state(cdev))
Sebastian Ott188561a2013-04-13 12:53:21 +02001576 ccw_device_wait_idle(cdev);
1577
Sebastian Ottafdfed02013-04-13 13:03:03 +02001578 if (cdev->private->state == DEV_STATE_ONLINE)
1579 cdev->online = 1;
1580 else
1581 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582out_unlock:
1583 spin_unlock_irq(cdev->ccwlock);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001584 if (rc) /* Give up online reference since onlining failed. */
1585 put_device(&cdev->dev);
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001586 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Sebastian Ott1e532092014-01-27 13:28:10 +01001589struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Sebastian Ott863fc842013-04-13 13:01:50 +02001591 struct io_subchannel_private *io_priv;
Sebastian Ottafdfed02013-04-13 13:03:03 +02001592 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 sch = cio_probe_console();
Sebastian Ottafdfed02013-04-13 13:03:03 +02001596 if (IS_ERR(sch))
1597 return ERR_CAST(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +02001598
1599 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1600 if (!io_priv) {
1601 put_device(&sch->dev);
1602 return ERR_PTR(-ENOMEM);
1603 }
Sebastian Ott2c3e7e12014-06-11 13:06:57 +02001604 set_io_private(sch, io_priv);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001605 cdev = io_subchannel_create_ccwdev(sch);
1606 if (IS_ERR(cdev)) {
1607 put_device(&sch->dev);
1608 kfree(io_priv);
1609 return cdev;
1610 }
Sebastian Ott2253e8d2014-01-27 13:26:10 +01001611 cdev->drv = drv;
Sebastian Ott137a14f2014-01-27 13:29:15 +01001612 ccw_device_set_int_class(cdev);
Sebastian Ottafdfed02013-04-13 13:03:03 +02001613 return cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
Cornelia Huck1f4e7ed2008-10-10 21:33:14 +02001615
Sebastian Ott1e532092014-01-27 13:28:10 +01001616void __init ccw_device_destroy_console(struct ccw_device *cdev)
1617{
1618 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1619 struct io_subchannel_private *io_priv = to_io_private(sch);
1620
1621 set_io_private(sch, NULL);
1622 put_device(&sch->dev);
1623 put_device(&cdev->dev);
1624 kfree(io_priv);
1625}
1626
Sebastian Ott188561a2013-04-13 12:53:21 +02001627/**
1628 * ccw_device_wait_idle() - busy wait for device to become idle
1629 * @cdev: ccw device
1630 *
1631 * Poll until activity control is zero, that is, no function or data
1632 * transfer is pending/active.
1633 * Called with device lock being held.
1634 */
1635void ccw_device_wait_idle(struct ccw_device *cdev)
1636{
1637 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1638
1639 while (1) {
1640 cio_tsch(sch);
1641 if (sch->schib.scsw.cmd.actl == 0)
1642 break;
1643 udelay_simple(100);
1644 }
1645}
1646
Martin Schwidefsky66648452009-06-16 10:30:28 +02001647static int ccw_device_pm_restore(struct device *dev);
1648
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001649int ccw_device_force_console(struct ccw_device *cdev)
Martin Schwidefsky66648452009-06-16 10:30:28 +02001650{
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001651 return ccw_device_pm_restore(&cdev->dev);
Martin Schwidefsky66648452009-06-16 10:30:28 +02001652}
1653EXPORT_SYMBOL_GPL(ccw_device_force_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654#endif
1655
1656/*
1657 * get ccw_device matching the busid, but only if owned by cdrv
1658 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001659static int
1660__ccwdev_check_busid(struct device *dev, void *id)
1661{
1662 char *bus_id;
1663
Cornelia Huck12975ae2006-10-11 15:31:47 +02001664 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001665
Kay Sievers98df67b2008-12-25 13:38:55 +01001666 return (strcmp(bus_id, dev_name(dev)) == 0);
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001667}
1668
1669
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001670/**
1671 * get_ccwdev_by_busid() - obtain device from a bus id
1672 * @cdrv: driver the device is owned by
1673 * @bus_id: bus id of the device to be searched
1674 *
1675 * This function searches all devices owned by @cdrv for a device with a bus
1676 * id matching @bus_id.
1677 * Returns:
1678 * If a match is found, its reference count of the found device is increased
1679 * and it is returned; else %NULL is returned.
1680 */
1681struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1682 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001684 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Sebastian Ott9f30ea92012-01-24 13:35:02 -05001686 dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001687 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001689 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692/************************** device driver handling ************************/
1693
1694/* This is the implementation of the ccw_driver class. The probe, remove
1695 * and release methods are initially very similar to the device_driver
1696 * implementations, with the difference that they have ccw_device
1697 * arguments.
1698 *
1699 * A ccw driver also contains the information that is needed for
1700 * device matching.
1701 */
1702static int
1703ccw_device_probe (struct device *dev)
1704{
1705 struct ccw_device *cdev = to_ccwdev(dev);
1706 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1707 int ret;
1708
1709 cdev->drv = cdrv; /* to let the driver call _set_online */
Sebastian Ott137a14f2014-01-27 13:29:15 +01001710 ccw_device_set_int_class(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001713 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001714 cdev->private->int_class = IRQIO_CIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return ret;
1716 }
1717
1718 return 0;
1719}
1720
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001721static int ccw_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 struct ccw_device *cdev = to_ccwdev(dev);
1724 struct ccw_driver *cdrv = cdev->drv;
1725 int ret;
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (cdrv->remove)
1728 cdrv->remove(cdev);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001729
1730 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (cdev->online) {
1732 cdev->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 ret = ccw_device_offline(cdev);
1734 spin_unlock_irq(cdev->ccwlock);
1735 if (ret == 0)
1736 wait_event(cdev->private->wait_q,
1737 dev_fsm_final_state(cdev));
1738 else
Michael Ernst139b83dd2008-05-07 09:22:54 +02001739 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001740 "device 0.%x.%04x\n",
1741 ret, cdev->private->dev_id.ssid,
1742 cdev->private->dev_id.devno);
Cornelia Huck9cd67422008-12-25 13:39:06 +01001743 /* Give up reference obtained in ccw_device_set_online(). */
1744 put_device(&cdev->dev);
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001745 spin_lock_irq(cdev->ccwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001748 cdev->drv = NULL;
Heiko Carstens420f42e2013-01-02 15:18:18 +01001749 cdev->private->int_class = IRQIO_CIO;
Sebastian Ott74bd0d82013-12-16 10:51:54 +01001750 spin_unlock_irq(cdev->ccwlock);
Sebastian Otta6ef1562015-09-07 19:51:39 +02001751 __disable_cmf(cdev);
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return 0;
1754}
1755
Cornelia Huck958974fb2007-10-12 16:11:21 +02001756static void ccw_device_shutdown(struct device *dev)
1757{
1758 struct ccw_device *cdev;
1759
1760 cdev = to_ccwdev(dev);
1761 if (cdev->drv && cdev->drv->shutdown)
1762 cdev->drv->shutdown(cdev);
Sebastian Ott1bc66642015-09-15 13:11:42 +02001763 __disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001764}
1765
Sebastian Ott823d4942009-06-16 10:30:20 +02001766static int ccw_device_pm_prepare(struct device *dev)
1767{
1768 struct ccw_device *cdev = to_ccwdev(dev);
1769
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001770 if (work_pending(&cdev->private->todo_work))
Sebastian Ott823d4942009-06-16 10:30:20 +02001771 return -EAGAIN;
1772 /* Fail while device is being set online/offline. */
1773 if (atomic_read(&cdev->private->onoff))
1774 return -EAGAIN;
1775
1776 if (cdev->online && cdev->drv && cdev->drv->prepare)
1777 return cdev->drv->prepare(cdev);
1778
1779 return 0;
1780}
1781
1782static void ccw_device_pm_complete(struct device *dev)
1783{
1784 struct ccw_device *cdev = to_ccwdev(dev);
1785
1786 if (cdev->online && cdev->drv && cdev->drv->complete)
1787 cdev->drv->complete(cdev);
1788}
1789
1790static int ccw_device_pm_freeze(struct device *dev)
1791{
1792 struct ccw_device *cdev = to_ccwdev(dev);
1793 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1794 int ret, cm_enabled;
1795
1796 /* Fail suspend while device is in transistional state. */
1797 if (!dev_fsm_final_state(cdev))
1798 return -EAGAIN;
1799 if (!cdev->online)
1800 return 0;
1801 if (cdev->drv && cdev->drv->freeze) {
1802 ret = cdev->drv->freeze(cdev);
1803 if (ret)
1804 return ret;
1805 }
1806
1807 spin_lock_irq(sch->lock);
1808 cm_enabled = cdev->private->cmb != NULL;
1809 spin_unlock_irq(sch->lock);
1810 if (cm_enabled) {
1811 /* Don't have the css write on memory. */
1812 ret = ccw_set_cmf(cdev, 0);
1813 if (ret)
1814 return ret;
1815 }
1816 /* From here on, disallow device driver I/O. */
1817 spin_lock_irq(sch->lock);
1818 ret = cio_disable_subchannel(sch);
1819 spin_unlock_irq(sch->lock);
1820
1821 return ret;
1822}
1823
1824static int ccw_device_pm_thaw(struct device *dev)
1825{
1826 struct ccw_device *cdev = to_ccwdev(dev);
1827 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1828 int ret, cm_enabled;
1829
1830 if (!cdev->online)
1831 return 0;
1832
1833 spin_lock_irq(sch->lock);
1834 /* Allow device driver I/O again. */
1835 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1836 cm_enabled = cdev->private->cmb != NULL;
1837 spin_unlock_irq(sch->lock);
1838 if (ret)
1839 return ret;
1840
1841 if (cm_enabled) {
1842 ret = ccw_set_cmf(cdev, 1);
1843 if (ret)
1844 return ret;
1845 }
1846
1847 if (cdev->drv && cdev->drv->thaw)
1848 ret = cdev->drv->thaw(cdev);
1849
1850 return ret;
1851}
1852
1853static void __ccw_device_pm_restore(struct ccw_device *cdev)
1854{
1855 struct subchannel *sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001856
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001857 spin_lock_irq(sch->lock);
1858 if (cio_is_console(sch->schid)) {
1859 cio_enable_subchannel(sch, (u32)(addr_t)sch);
1860 goto out_unlock;
1861 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001862 /*
1863 * While we were sleeping, devices may have gone or become
1864 * available again. Kick re-detection.
1865 */
Sebastian Ott823d4942009-06-16 10:30:20 +02001866 cdev->private->flags.resuming = 1;
Sebastian Ottb17295e2011-01-12 09:55:10 +01001867 cdev->private->path_new_mask = LPM_ANYPATH;
Sebastian Ott817e5002011-12-01 13:32:19 +01001868 css_sched_sch_todo(sch, SCH_TODO_EVAL);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001869 spin_unlock_irq(sch->lock);
Sebastian Ott817e5002011-12-01 13:32:19 +01001870 css_wait_for_slow_path();
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001871
1872 /* cdev may have been moved to a different subchannel. */
1873 sch = to_subchannel(cdev->dev.parent);
1874 spin_lock_irq(sch->lock);
1875 if (cdev->private->state != DEV_STATE_ONLINE &&
1876 cdev->private->state != DEV_STATE_OFFLINE)
1877 goto out_unlock;
1878
Peter Oberparleiter736b5db2009-12-07 12:51:21 +01001879 ccw_device_recognition(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001880 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001881 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1882 cdev->private->state == DEV_STATE_DISCONNECTED);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001883 spin_lock_irq(sch->lock);
1884
1885out_unlock:
Sebastian Ott823d4942009-06-16 10:30:20 +02001886 cdev->private->flags.resuming = 0;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001887 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001888}
1889
1890static int resume_handle_boxed(struct ccw_device *cdev)
1891{
1892 cdev->private->state = DEV_STATE_BOXED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001893 if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001894 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001895 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001896 return -ENODEV;
1897}
1898
1899static int resume_handle_disc(struct ccw_device *cdev)
1900{
1901 cdev->private->state = DEV_STATE_DISCONNECTED;
Sebastian Ott76e6fb42010-02-26 22:37:28 +01001902 if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
Sebastian Ott823d4942009-06-16 10:30:20 +02001903 return 0;
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001904 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
Sebastian Ott823d4942009-06-16 10:30:20 +02001905 return -ENODEV;
1906}
1907
1908static int ccw_device_pm_restore(struct device *dev)
1909{
1910 struct ccw_device *cdev = to_ccwdev(dev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001911 struct subchannel *sch;
1912 int ret = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001913
1914 __ccw_device_pm_restore(cdev);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001915 sch = to_subchannel(cdev->dev.parent);
Sebastian Ott823d4942009-06-16 10:30:20 +02001916 spin_lock_irq(sch->lock);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001917 if (cio_is_console(sch->schid))
Sebastian Ott823d4942009-06-16 10:30:20 +02001918 goto out_restore;
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001919
Sebastian Ott823d4942009-06-16 10:30:20 +02001920 /* check recognition results */
1921 switch (cdev->private->state) {
1922 case DEV_STATE_OFFLINE:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001923 case DEV_STATE_ONLINE:
1924 cdev->private->flags.donotify = 0;
Sebastian Ott823d4942009-06-16 10:30:20 +02001925 break;
1926 case DEV_STATE_BOXED:
1927 ret = resume_handle_boxed(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001928 if (ret)
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001929 goto out_unlock;
Sebastian Ott823d4942009-06-16 10:30:20 +02001930 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001931 default:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001932 ret = resume_handle_disc(cdev);
1933 if (ret)
1934 goto out_unlock;
1935 goto out_restore;
Sebastian Ott823d4942009-06-16 10:30:20 +02001936 }
1937 /* check if the device type has changed */
1938 if (!ccw_device_test_sense_data(cdev)) {
1939 ccw_device_update_sense_data(cdev);
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01001940 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
Sebastian Ott823d4942009-06-16 10:30:20 +02001941 ret = -ENODEV;
1942 goto out_unlock;
1943 }
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001944 if (!cdev->online)
1945 goto out_unlock;
1946
1947 if (ccw_device_online(cdev)) {
1948 ret = resume_handle_disc(cdev);
1949 if (ret)
1950 goto out_unlock;
1951 goto out_restore;
1952 }
1953 spin_unlock_irq(sch->lock);
1954 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1955 spin_lock_irq(sch->lock);
1956
1957 if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1958 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1959 ret = -ENODEV;
Sebastian Ott823d4942009-06-16 10:30:20 +02001960 goto out_unlock;
1961 }
Sebastian Ott823d4942009-06-16 10:30:20 +02001962
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001963 /* reenable cmf, if needed */
1964 if (cdev->private->cmb) {
1965 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001966 ret = ccw_set_cmf(cdev, 1);
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001967 spin_lock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001968 if (ret) {
Sebastian Ottf0148242009-09-11 10:28:23 +02001969 CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1970 "(rc=%d)\n", cdev->private->dev_id.ssid,
1971 cdev->private->dev_id.devno, ret);
Sebastian Ott823d4942009-06-16 10:30:20 +02001972 ret = 0;
1973 }
1974 }
1975
1976out_restore:
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001977 spin_unlock_irq(sch->lock);
Sebastian Ott823d4942009-06-16 10:30:20 +02001978 if (cdev->online && cdev->drv && cdev->drv->restore)
1979 ret = cdev->drv->restore(cdev);
Sebastian Ott823d4942009-06-16 10:30:20 +02001980 return ret;
1981
Sebastian Ott823d4942009-06-16 10:30:20 +02001982out_unlock:
1983 spin_unlock_irq(sch->lock);
1984 return ret;
1985}
1986
Alexey Dobriyan47145212009-12-14 18:00:08 -08001987static const struct dev_pm_ops ccw_pm_ops = {
Sebastian Ott823d4942009-06-16 10:30:20 +02001988 .prepare = ccw_device_pm_prepare,
1989 .complete = ccw_device_pm_complete,
1990 .freeze = ccw_device_pm_freeze,
1991 .thaw = ccw_device_pm_thaw,
1992 .restore = ccw_device_pm_restore,
1993};
1994
Sebastian Ottd5ab5272011-03-23 10:16:03 +01001995static struct bus_type ccw_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001996 .name = "ccw",
1997 .match = ccw_bus_match,
1998 .uevent = ccw_uevent,
1999 .probe = ccw_device_probe,
2000 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02002001 .shutdown = ccw_device_shutdown,
Sebastian Ott823d4942009-06-16 10:30:20 +02002002 .pm = &ccw_pm_ops,
Cornelia Huck8bbace72006-01-11 10:56:22 +01002003};
2004
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002005/**
2006 * ccw_driver_register() - register a ccw driver
2007 * @cdriver: driver to be registered
2008 *
2009 * This function is mainly a wrapper around driver_register().
2010 * Returns:
2011 * %0 on success and a negative error value on failure.
2012 */
2013int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 struct device_driver *drv = &cdriver->driver;
2016
2017 drv->bus = &ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 return driver_register(drv);
2020}
2021
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02002022/**
2023 * ccw_driver_unregister() - deregister a ccw driver
2024 * @cdriver: driver to be deregistered
2025 *
2026 * This function is mainly a wrapper around driver_unregister().
2027 */
2028void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 driver_unregister(&cdriver->driver);
2031}
2032
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002033static void ccw_device_todo(struct work_struct *work)
2034{
2035 struct ccw_device_private *priv;
2036 struct ccw_device *cdev;
2037 struct subchannel *sch;
2038 enum cdev_todo todo;
2039
2040 priv = container_of(work, struct ccw_device_private, todo_work);
2041 cdev = priv->cdev;
2042 sch = to_subchannel(cdev->dev.parent);
2043 /* Find out todo. */
2044 spin_lock_irq(cdev->ccwlock);
2045 todo = priv->todo;
2046 priv->todo = CDEV_TODO_NOTHING;
2047 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2048 priv->dev_id.ssid, priv->dev_id.devno, todo);
2049 spin_unlock_irq(cdev->ccwlock);
2050 /* Perform todo. */
2051 switch (todo) {
2052 case CDEV_TODO_ENABLE_CMF:
2053 cmf_reenable(cdev);
2054 break;
2055 case CDEV_TODO_REBIND:
2056 ccw_device_do_unbind_bind(cdev);
2057 break;
2058 case CDEV_TODO_REGISTER:
2059 io_subchannel_register(cdev);
2060 break;
2061 case CDEV_TODO_UNREG_EVAL:
2062 if (!sch_is_pseudo_sch(sch))
2063 css_schedule_eval(sch->schid);
2064 /* fall-through */
2065 case CDEV_TODO_UNREG:
2066 if (sch_is_pseudo_sch(sch))
2067 ccw_device_unregister(cdev);
2068 else
2069 ccw_device_call_sch_unregister(cdev);
2070 break;
2071 default:
2072 break;
2073 }
2074 /* Release workqueue ref. */
2075 put_device(&cdev->dev);
2076}
2077
2078/**
2079 * ccw_device_sched_todo - schedule ccw device operation
2080 * @cdev: ccw device
2081 * @todo: todo
2082 *
2083 * Schedule the operation identified by @todo to be performed on the slow path
2084 * workqueue. Do nothing if another operation with higher priority is already
2085 * scheduled. Needs to be called with ccwdev lock held.
2086 */
2087void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2088{
2089 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2090 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2091 todo);
2092 if (cdev->private->todo >= todo)
2093 return;
2094 cdev->private->todo = todo;
2095 /* Get workqueue ref. */
2096 if (!get_device(&cdev->dev))
2097 return;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01002098 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
Peter Oberparleiter37de53b2009-12-07 12:51:19 +01002099 /* Already queued, release workqueue ref. */
2100 put_device(&cdev->dev);
2101 }
2102}
2103
Michael Ernstfd0457a2010-08-09 18:12:50 +02002104/**
2105 * ccw_device_siosl() - initiate logging
2106 * @cdev: ccw device
2107 *
2108 * This function is used to invoke model-dependent logging within the channel
2109 * subsystem.
2110 */
2111int ccw_device_siosl(struct ccw_device *cdev)
2112{
2113 struct subchannel *sch = to_subchannel(cdev->dev.parent);
2114
2115 return chsc_siosl(sch->schid);
2116}
2117EXPORT_SYMBOL_GPL(ccw_device_siosl);
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119EXPORT_SYMBOL(ccw_device_set_online);
2120EXPORT_SYMBOL(ccw_device_set_offline);
2121EXPORT_SYMBOL(ccw_driver_register);
2122EXPORT_SYMBOL(ccw_driver_unregister);
2123EXPORT_SYMBOL(get_ccwdev_by_busid);