]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/s390/cio/device.c
UBUNTU: Ubuntu-5.11.0-22.23
[mirror_ubuntu-hirsute-kernel.git] / drivers / s390 / cio / device.c
CommitLineData
724117b7 1// SPDX-License-Identifier: GPL-1.0+
1da177e4 2/*
1da177e4 3 * bus driver for ccw devices
1da177e4 4 *
a53c8fab 5 * Copyright IBM Corp. 2002, 2008
1da177e4 6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
4ce3b30c 7 * Cornelia Huck (cornelia.huck@de.ibm.com)
1da177e4
LT
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
a7ae2c02
PO
10
11#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
a00f761f 14#include <linux/export.h>
1da177e4
LT
15#include <linux/init.h>
16#include <linux/spinlock.h>
17#include <linux/errno.h>
18#include <linux/err.h>
19#include <linux/slab.h>
20#include <linux/list.h>
21#include <linux/device.h>
22#include <linux/workqueue.h>
188561a4 23#include <linux/delay.h>
90ab1336 24#include <linux/timer.h>
de400d6b 25#include <linux/kernel_stat.h>
174cd4b1 26#include <linux/sched/signal.h>
37db8985 27#include <linux/dma-mapping.h>
1da177e4
LT
28
29#include <asm/ccwdev.h>
30#include <asm/cio.h>
4e57b681 31#include <asm/param.h> /* HZ */
1842f2b1 32#include <asm/cmb.h>
3a3fc29a 33#include <asm/isc.h>
1da177e4 34
0ae7a7b2 35#include "chp.h"
1da177e4 36#include "cio.h"
d7b5a4c9 37#include "cio_debug.h"
1da177e4
LT
38#include "css.h"
39#include "device.h"
40#include "ioasm.h"
cd6b4f27 41#include "io_sch.h"
ecf5d9ef 42#include "blacklist.h"
fd0457a6 43#include "chsc.h"
1da177e4 44
90ab1336 45static struct timer_list recovery_timer;
486d0a00 46static DEFINE_SPINLOCK(recovery_lock);
90ab1336
PO
47static int recovery_phase;
48static const unsigned long recovery_delay[] = { 3, 30, 300 };
49
0ad8f714
SO
50static 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
1da177e4
LT
54/******************* 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
db0c2d59
PO
79/* 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). */
cfbe9bb2 82static int snprint_alias(char *buf, size_t size,
db0c2d59 83 struct ccw_device_id *id, const char *suffix)
1da177e4 84{
db0c2d59 85 int len;
1da177e4 86
cfbe9bb2 87 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
db0c2d59
PO
88 if (len > size)
89 return len;
90 buf += len;
91 size -= len;
1da177e4 92
db0c2d59
PO
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);
1da177e4 98
db0c2d59
PO
99 return len;
100}
101
102/* Set up environment variables for ccw device uevent. Return 0 on success,
103 * non-zero otherwise. */
7eff2e7a 104static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
db0c2d59
PO
105{
106 struct ccw_device *cdev = to_ccwdev(dev);
107 struct ccw_device_id *id = &(cdev->id);
cfbe9bb2
CH
108 int ret;
109 char modalias_buf[30];
1da177e4 110
db0c2d59 111 /* CU_TYPE= */
7eff2e7a 112 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
cfbe9bb2
CH
113 if (ret)
114 return ret;
db0c2d59
PO
115
116 /* CU_MODEL= */
7eff2e7a 117 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
cfbe9bb2
CH
118 if (ret)
119 return ret;
1da177e4
LT
120
121 /* The next two can be zero, that's ok for us */
db0c2d59 122 /* DEV_TYPE= */
7eff2e7a 123 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
cfbe9bb2
CH
124 if (ret)
125 return ret;
1da177e4 126
db0c2d59 127 /* DEV_MODEL= */
7eff2e7a 128 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
cfbe9bb2
CH
129 if (ret)
130 return ret;
db0c2d59
PO
131
132 /* MODALIAS= */
cfbe9bb2 133 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
7eff2e7a
KS
134 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
135 return ret;
1da177e4
LT
136}
137
602b20f2
CH
138static void io_subchannel_irq(struct subchannel *);
139static int io_subchannel_probe(struct subchannel *);
140static int io_subchannel_remove(struct subchannel *);
8bbace7e 141static void io_subchannel_shutdown(struct subchannel *);
c820de39 142static int io_subchannel_sch_event(struct subchannel *, int);
99611f87
CH
143static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
144 int);
846d0c6f 145static void recovery_func(struct timer_list *unused);
1da177e4 146
f08adc00
CH
147static struct css_device_id io_subchannel_ids[] = {
148 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
149 { /* end of list */ },
150};
f08adc00 151
b4c70721 152static int io_subchannel_settle(void)
8ea7f559 153{
b4c70721
SO
154 int ret;
155
156 ret = wait_event_interruptible(ccw_device_init_wq,
157 atomic_read(&ccw_device_init_count) == 0);
158 if (ret)
159 return -EINTR;
be5d3823 160 flush_workqueue(cio_work_q);
b4c70721 161 return 0;
8ea7f559
SO
162}
163
f7e5d67c 164static struct css_driver io_subchannel_driver = {
e6aed122
SO
165 .drv = {
166 .owner = THIS_MODULE,
167 .name = "io_subchannel",
168 },
f08adc00 169 .subchannel_type = io_subchannel_ids,
1da177e4 170 .irq = io_subchannel_irq,
c820de39
CH
171 .sch_event = io_subchannel_sch_event,
172 .chp_event = io_subchannel_chp_event,
8bbace7e
CH
173 .probe = io_subchannel_probe,
174 .remove = io_subchannel_remove,
175 .shutdown = io_subchannel_shutdown,
8ea7f559 176 .settle = io_subchannel_settle,
1da177e4
LT
177};
178
2f17644d 179int __init io_subchannel_init(void)
1da177e4
LT
180{
181 int ret;
182
846d0c6f 183 timer_setup(&recovery_timer, recovery_func, 0);
be5d3823
SO
184 ret = bus_register(&ccw_bus_type);
185 if (ret)
186 return ret;
25b7bb58
CH
187 ret = css_driver_register(&io_subchannel_driver);
188 if (ret)
be5d3823 189 bus_unregister(&ccw_bus_type);
1da177e4 190
1da177e4
LT
191 return ret;
192}
193
1da177e4
LT
194
195/************************ device handling **************************/
196
1da177e4 197static ssize_t
3fd3c0a5 198devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
199{
200 struct ccw_device *cdev = to_ccwdev(dev);
201 struct ccw_device_id *id = &(cdev->id);
202
203 if (id->dev_type != 0)
204 return sprintf(buf, "%04x/%02x\n",
205 id->dev_type, id->dev_model);
206 else
207 return sprintf(buf, "n/a\n");
208}
209
210static ssize_t
3fd3c0a5 211cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
212{
213 struct ccw_device *cdev = to_ccwdev(dev);
214 struct ccw_device_id *id = &(cdev->id);
215
216 return sprintf(buf, "%04x/%02x\n",
217 id->cu_type, id->cu_model);
218}
219
f1fc78a8
BB
220static ssize_t
221modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
222{
223 struct ccw_device *cdev = to_ccwdev(dev);
224 struct ccw_device_id *id = &(cdev->id);
db0c2d59 225 int len;
f1fc78a8 226
086a6c62 227 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
db0c2d59
PO
228
229 return len > PAGE_SIZE ? PAGE_SIZE : len;
f1fc78a8
BB
230}
231
1da177e4 232static ssize_t
3fd3c0a5 233online_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
234{
235 struct ccw_device *cdev = to_ccwdev(dev);
236
237 return sprintf(buf, cdev->online ? "1\n" : "0\n");
238}
239
d7b5a4c9
CH
240int ccw_device_is_orphan(struct ccw_device *cdev)
241{
242 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
243}
244
ef99516c 245static void ccw_device_unregister(struct ccw_device *cdev)
7674da77 246{
7d253b9a 247 if (device_is_registered(&cdev->dev)) {
24a1872d 248 /* Undo device_add(). */
ef99516c 249 device_del(&cdev->dev);
24a1872d
SO
250 }
251 if (cdev->private->flags.initialized) {
252 cdev->private->flags.initialized = 0;
3b554a14
SO
253 /* Release reference from device_initialize(). */
254 put_device(&cdev->dev);
255 }
7674da77
CH
256}
257
d40f7b75
SO
258static void io_subchannel_quiesce(struct subchannel *);
259
b2ffd8e9
CH
260/**
261 * ccw_device_set_offline() - disable a ccw device for I/O
262 * @cdev: target ccw device
263 *
264 * This function calls the driver's set_offline() function for @cdev, if
265 * given, and then disables @cdev.
266 * Returns:
267 * %0 on success and a negative error value on failure.
268 * Context:
269 * enabled, ccw device lock not held
270 */
271int ccw_device_set_offline(struct ccw_device *cdev)
1da177e4 272{
d40f7b75
SO
273 struct subchannel *sch;
274 int ret, state;
1da177e4
LT
275
276 if (!cdev)
277 return -ENODEV;
278 if (!cdev->online || !cdev->drv)
279 return -EINVAL;
280
281 if (cdev->drv->set_offline) {
282 ret = cdev->drv->set_offline(cdev);
283 if (ret != 0)
284 return ret;
285 }
1da177e4 286 spin_lock_irq(cdev->ccwlock);
d40f7b75 287 sch = to_subchannel(cdev->dev.parent);
74bd0d85 288 cdev->online = 0;
217ee6c6
ME
289 /* Wait until a final state or DISCONNECTED is reached */
290 while (!dev_fsm_final_state(cdev) &&
291 cdev->private->state != DEV_STATE_DISCONNECTED) {
1da177e4 292 spin_unlock_irq(cdev->ccwlock);
217ee6c6
ME
293 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
294 cdev->private->state == DEV_STATE_DISCONNECTED));
295 spin_lock_irq(cdev->ccwlock);
1da177e4 296 }
d40f7b75
SO
297 do {
298 ret = ccw_device_offline(cdev);
299 if (!ret)
300 break;
301 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
302 "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
303 cdev->private->dev_id.devno);
304 if (ret != -EBUSY)
305 goto error;
306 state = cdev->private->state;
307 spin_unlock_irq(cdev->ccwlock);
308 io_subchannel_quiesce(sch);
309 spin_lock_irq(cdev->ccwlock);
310 cdev->private->state = state;
311 } while (ret == -EBUSY);
1da177e4 312 spin_unlock_irq(cdev->ccwlock);
217ee6c6
ME
313 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
314 cdev->private->state == DEV_STATE_DISCONNECTED));
a7ae2c02
PO
315 /* Inform the user if set offline failed. */
316 if (cdev->private->state == DEV_STATE_BOXED) {
baebc70a
JP
317 pr_warn("%s: The device entered boxed state while being set offline\n",
318 dev_name(&cdev->dev));
a7ae2c02 319 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
baebc70a
JP
320 pr_warn("%s: The device stopped operating while being set offline\n",
321 dev_name(&cdev->dev));
a7ae2c02 322 }
217ee6c6
ME
323 /* Give up reference from ccw_device_set_online(). */
324 put_device(&cdev->dev);
325 return 0;
326
327error:
217ee6c6
ME
328 cdev->private->state = DEV_STATE_OFFLINE;
329 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
330 spin_unlock_irq(cdev->ccwlock);
331 /* Give up reference from ccw_device_set_online(). */
332 put_device(&cdev->dev);
333 return -ENODEV;
1da177e4
LT
334}
335
b2ffd8e9
CH
336/**
337 * ccw_device_set_online() - enable a ccw device for I/O
338 * @cdev: target ccw device
339 *
340 * This function first enables @cdev and then calls the driver's set_online()
341 * function for @cdev, if given. If set_online() returns an error, @cdev is
342 * disabled again.
343 * Returns:
344 * %0 on success and a negative error value on failure.
345 * Context:
346 * enabled, ccw device lock not held
347 */
348int ccw_device_set_online(struct ccw_device *cdev)
1da177e4
LT
349{
350 int ret;
217ee6c6 351 int ret2;
1da177e4
LT
352
353 if (!cdev)
354 return -ENODEV;
355 if (cdev->online || !cdev->drv)
356 return -EINVAL;
9cd67421
CH
357 /* Hold on to an extra reference while device is online. */
358 if (!get_device(&cdev->dev))
359 return -ENODEV;
1da177e4
LT
360
361 spin_lock_irq(cdev->ccwlock);
362 ret = ccw_device_online(cdev);
363 spin_unlock_irq(cdev->ccwlock);
364 if (ret == 0)
365 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
366 else {
139b83dd 367 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
e556bbbd
CH
368 "device 0.%x.%04x\n",
369 ret, cdev->private->dev_id.ssid,
370 cdev->private->dev_id.devno);
9cd67421
CH
371 /* Give up online reference since onlining failed. */
372 put_device(&cdev->dev);
1da177e4
LT
373 return ret;
374 }
217ee6c6
ME
375 spin_lock_irq(cdev->ccwlock);
376 /* Check if online processing was successful */
377 if ((cdev->private->state != DEV_STATE_ONLINE) &&
378 (cdev->private->state != DEV_STATE_W4SENSE)) {
379 spin_unlock_irq(cdev->ccwlock);
a7ae2c02
PO
380 /* Inform the user that set online failed. */
381 if (cdev->private->state == DEV_STATE_BOXED) {
baebc70a
JP
382 pr_warn("%s: Setting the device online failed because it is boxed\n",
383 dev_name(&cdev->dev));
a7ae2c02 384 } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
baebc70a
JP
385 pr_warn("%s: Setting the device online failed because it is not operational\n",
386 dev_name(&cdev->dev));
a7ae2c02 387 }
9cd67421
CH
388 /* Give up online reference since onlining failed. */
389 put_device(&cdev->dev);
1da177e4 390 return -ENODEV;
9cd67421 391 }
217ee6c6
ME
392 spin_unlock_irq(cdev->ccwlock);
393 if (cdev->drv->set_online)
394 ret = cdev->drv->set_online(cdev);
395 if (ret)
396 goto rollback;
74bd0d85
SO
397
398 spin_lock_irq(cdev->ccwlock);
217ee6c6 399 cdev->online = 1;
74bd0d85 400 spin_unlock_irq(cdev->ccwlock);
217ee6c6
ME
401 return 0;
402
403rollback:
1da177e4 404 spin_lock_irq(cdev->ccwlock);
217ee6c6
ME
405 /* Wait until a final state or DISCONNECTED is reached */
406 while (!dev_fsm_final_state(cdev) &&
407 cdev->private->state != DEV_STATE_DISCONNECTED) {
408 spin_unlock_irq(cdev->ccwlock);
409 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
410 cdev->private->state == DEV_STATE_DISCONNECTED));
411 spin_lock_irq(cdev->ccwlock);
412 }
413 ret2 = ccw_device_offline(cdev);
414 if (ret2)
415 goto error;
416 spin_unlock_irq(cdev->ccwlock);
417 wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
418 cdev->private->state == DEV_STATE_DISCONNECTED));
419 /* Give up online reference since onlining failed. */
420 put_device(&cdev->dev);
421 return ret;
422
423error:
424 CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
425 "device 0.%x.%04x\n",
426 ret2, cdev->private->dev_id.ssid,
427 cdev->private->dev_id.devno);
428 cdev->private->state = DEV_STATE_OFFLINE;
1da177e4 429 spin_unlock_irq(cdev->ccwlock);
9cd67421
CH
430 /* Give up online reference since onlining failed. */
431 put_device(&cdev->dev);
217ee6c6 432 return ret;
1da177e4
LT
433}
434
e74fe0ce 435static int online_store_handle_offline(struct ccw_device *cdev)
f5ba6c86 436{
37de53bb
PO
437 if (cdev->private->state == DEV_STATE_DISCONNECTED) {
438 spin_lock_irq(cdev->ccwlock);
439 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
440 spin_unlock_irq(cdev->ccwlock);
7cd40314
SO
441 return 0;
442 }
443 if (cdev->drv && cdev->drv->set_offline)
e74fe0ce 444 return ccw_device_set_offline(cdev);
7cd40314 445 return -EINVAL;
f5ba6c86
CH
446}
447
448static int online_store_recog_and_online(struct ccw_device *cdev)
449{
f5ba6c86 450 /* Do device recognition, if needed. */
99f6a570 451 if (cdev->private->state == DEV_STATE_BOXED) {
1f5bd384 452 spin_lock_irq(cdev->ccwlock);
736b5db8 453 ccw_device_recognition(cdev);
1f5bd384 454 spin_unlock_irq(cdev->ccwlock);
f5ba6c86
CH
455 wait_event(cdev->private->wait_q,
456 cdev->private->flags.recog_done);
156013ff
SO
457 if (cdev->private->state != DEV_STATE_OFFLINE)
458 /* recognition failed */
459 return -EAGAIN;
f5ba6c86
CH
460 }
461 if (cdev->drv && cdev->drv->set_online)
7cd40314
SO
462 return ccw_device_set_online(cdev);
463 return -EINVAL;
f5ba6c86 464}
156013ff 465
c78aa6cb 466static int online_store_handle_online(struct ccw_device *cdev, int force)
f5ba6c86
CH
467{
468 int ret;
469
470 ret = online_store_recog_and_online(cdev);
156013ff 471 if (ret && !force)
c78aa6cb 472 return ret;
f5ba6c86
CH
473 if (force && cdev->private->state == DEV_STATE_BOXED) {
474 ret = ccw_device_stlck(cdev);
c78aa6cb
ME
475 if (ret)
476 return ret;
f5ba6c86
CH
477 if (cdev->id.cu_type == 0)
478 cdev->private->state = DEV_STATE_NOT_OPER;
156013ff
SO
479 ret = online_store_recog_and_online(cdev);
480 if (ret)
481 return ret;
f5ba6c86 482 }
c78aa6cb 483 return 0;
f5ba6c86
CH
484}
485
486static ssize_t online_store (struct device *dev, struct device_attribute *attr,
487 const char *buf, size_t count)
1da177e4
LT
488{
489 struct ccw_device *cdev = to_ccwdev(dev);
2f972202
CH
490 int force, ret;
491 unsigned long i;
1da177e4 492
a2fc8485 493 /* Prevent conflict between multiple on-/offline processing requests. */
350e9120 494 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
1da177e4 495 return -EAGAIN;
a2fc8485
PO
496 /* Prevent conflict between internal I/Os and on-/offline processing. */
497 if (!dev_fsm_final_state(cdev) &&
498 cdev->private->state != DEV_STATE_DISCONNECTED) {
499 ret = -EAGAIN;
00381eeb 500 goto out;
a2fc8485
PO
501 }
502 /* Prevent conflict between pending work and on-/offline processing.*/
503 if (work_pending(&cdev->private->todo_work)) {
504 ret = -EAGAIN;
00381eeb 505 goto out;
1da177e4
LT
506 }
507 if (!strncmp(buf, "force\n", count)) {
508 force = 1;
509 i = 1;
2f972202 510 ret = 0;
1da177e4
LT
511 } else {
512 force = 0;
0178722b 513 ret = kstrtoul(buf, 16, &i);
1da177e4 514 }
2f972202
CH
515 if (ret)
516 goto out;
00381eeb
SO
517
518 device_lock(dev);
f5ba6c86
CH
519 switch (i) {
520 case 0:
e74fe0ce 521 ret = online_store_handle_offline(cdev);
f5ba6c86
CH
522 break;
523 case 1:
c78aa6cb 524 ret = online_store_handle_online(cdev, force);
f5ba6c86
CH
525 break;
526 default:
2f972202 527 ret = -EINVAL;
1da177e4 528 }
00381eeb
SO
529 device_unlock(dev);
530
2f972202 531out:
1da177e4 532 atomic_set(&cdev->private->onoff, 0);
e74fe0ce 533 return (ret < 0) ? ret : count;
1da177e4
LT
534}
535
536static ssize_t
3fd3c0a5 537available_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
538{
539 struct ccw_device *cdev = to_ccwdev(dev);
540 struct subchannel *sch;
541
d7b5a4c9
CH
542 if (ccw_device_is_orphan(cdev))
543 return sprintf(buf, "no device\n");
1da177e4
LT
544 switch (cdev->private->state) {
545 case DEV_STATE_BOXED:
546 return sprintf(buf, "boxed\n");
547 case DEV_STATE_DISCONNECTED:
548 case DEV_STATE_DISCONNECTED_SENSE_ID:
549 case DEV_STATE_NOT_OPER:
550 sch = to_subchannel(dev->parent);
551 if (!sch->lpm)
552 return sprintf(buf, "no path\n");
553 else
554 return sprintf(buf, "no device\n");
555 default:
556 /* All other states considered fine. */
557 return sprintf(buf, "good\n");
558 }
559}
560
fd0457a6
ME
561static ssize_t
562initiate_logging(struct device *dev, struct device_attribute *attr,
563 const char *buf, size_t count)
564{
565 struct subchannel *sch = to_subchannel(dev);
566 int rc;
567
568 rc = chsc_siosl(sch->schid);
569 if (rc < 0) {
baebc70a
JP
570 pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
571 sch->schid.ssid, sch->schid.sch_no, rc);
fd0457a6
ME
572 return rc;
573 }
574 pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
575 sch->schid.ssid, sch->schid.sch_no);
576 return count;
577}
578
84c57ad5
SO
579static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
580 char *buf)
581{
582 struct subchannel *sch = to_subchannel(dev);
583
584 return sprintf(buf, "%02x\n", sch->vpm);
585}
586
c828a892
JP
587static DEVICE_ATTR_RO(devtype);
588static DEVICE_ATTR_RO(cutype);
589static DEVICE_ATTR_RO(modalias);
b6b996b6 590static DEVICE_ATTR_RW(online);
1da177e4 591static DEVICE_ATTR(availability, 0444, available_show, NULL);
fd0457a6 592static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
c828a892 593static DEVICE_ATTR_RO(vpm);
1da177e4 594
7e9db9ea 595static struct attribute *io_subchannel_attrs[] = {
fd0457a6 596 &dev_attr_logging.attr,
84c57ad5 597 &dev_attr_vpm.attr,
1da177e4
LT
598 NULL,
599};
600
f460d113 601static const struct attribute_group io_subchannel_attr_group = {
7e9db9ea 602 .attrs = io_subchannel_attrs,
529192f3 603};
1da177e4
LT
604
605static struct attribute * ccwdev_attrs[] = {
606 &dev_attr_devtype.attr,
607 &dev_attr_cutype.attr,
f1fc78a8 608 &dev_attr_modalias.attr,
1da177e4
LT
609 &dev_attr_online.attr,
610 &dev_attr_cmb_enable.attr,
611 &dev_attr_availability.attr,
612 NULL,
613};
614
f460d113 615static const struct attribute_group ccwdev_attr_group = {
1da177e4
LT
616 .attrs = ccwdev_attrs,
617};
618
a4dbd674 619static const struct attribute_group *ccwdev_attr_groups[] = {
ef99516c
CH
620 &ccwdev_attr_group,
621 NULL,
622};
1da177e4 623
2c3e7e15 624static int ccw_device_add(struct ccw_device *cdev)
1da177e4
LT
625{
626 struct device *dev = &cdev->dev;
1da177e4
LT
627
628 dev->bus = &ccw_bus_type;
7d253b9a 629 return device_add(dev);
1da177e4
LT
630}
631
418e3ea1 632static int match_dev_id(struct device *dev, const void *data)
b0744bd2 633{
5d6e6b6f 634 struct ccw_device *cdev = to_ccwdev(dev);
418e3ea1 635 struct ccw_dev_id *dev_id = (void *)data;
d7b5a4c9 636
d7b5a4c9
CH
637 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
638}
639
b7a610f7
SO
640/**
641 * get_ccwdev_by_dev_id() - obtain device from a ccw device id
642 * @dev_id: id of the device to be searched
643 *
644 * This function searches all devices attached to the ccw bus for a device
645 * matching @dev_id.
646 * Returns:
647 * If a device is found its reference count is increased and returned;
648 * else %NULL is returned.
649 */
650struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
d7b5a4c9
CH
651{
652 struct device *dev;
653
5d6e6b6f 654 dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
d7b5a4c9
CH
655
656 return dev ? to_ccwdev(dev) : NULL;
657}
b7a610f7 658EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
d7b5a4c9 659
37de53bb 660static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
1da177e4 661{
eb32ae8d 662 int ret;
1da177e4 663
7d253b9a 664 if (device_is_registered(&cdev->dev)) {
eb32ae8d
CH
665 device_release_driver(&cdev->dev);
666 ret = device_attach(&cdev->dev);
667 WARN_ON(ret == -ENODEV);
668 }
1da177e4
LT
669}
670
671static void
672ccw_device_release(struct device *dev)
673{
674 struct ccw_device *cdev;
675
676 cdev = to_ccwdev(dev);
37db8985
HP
677 cio_gp_dma_free(cdev->private->dma_pool, cdev->private->dma_area,
678 sizeof(*cdev->private->dma_area));
679 cio_gp_dma_destroy(cdev->private->dma_pool, &cdev->dev);
6eff208f
CH
680 /* Release reference of parent subchannel. */
681 put_device(cdev->dev.parent);
1da177e4
LT
682 kfree(cdev->private);
683 kfree(cdev);
684}
685
7674da77
CH
686static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
687{
688 struct ccw_device *cdev;
37db8985 689 struct gen_pool *dma_pool;
7674da77
CH
690
691 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
37db8985
HP
692 if (!cdev)
693 goto err_cdev;
694 cdev->private = kzalloc(sizeof(struct ccw_device_private),
695 GFP_KERNEL | GFP_DMA);
696 if (!cdev->private)
697 goto err_priv;
698 cdev->dev.coherent_dma_mask = sch->dev.coherent_dma_mask;
05668e1d 699 cdev->dev.dma_mask = sch->dev.dma_mask;
37db8985
HP
700 dma_pool = cio_gp_dma_create(&cdev->dev, 1);
701 if (!dma_pool)
702 goto err_dma_pool;
703 cdev->private->dma_pool = dma_pool;
704 cdev->private->dma_area = cio_gp_dma_zalloc(dma_pool, &cdev->dev,
705 sizeof(*cdev->private->dma_area));
706 if (!cdev->private->dma_area)
707 goto err_dma_area;
708 return cdev;
709err_dma_area:
710 cio_gp_dma_destroy(dma_pool, &cdev->dev);
711err_dma_pool:
712 kfree(cdev->private);
713err_priv:
7674da77 714 kfree(cdev);
37db8985 715err_cdev:
7674da77
CH
716 return ERR_PTR(-ENOMEM);
717}
718
37de53bb
PO
719static void ccw_device_todo(struct work_struct *work);
720
7674da77
CH
721static int io_subchannel_initialize_dev(struct subchannel *sch,
722 struct ccw_device *cdev)
723{
2c3e7e15
SO
724 struct ccw_device_private *priv = cdev->private;
725 int ret;
726
727 priv->cdev = cdev;
728 priv->int_class = IRQIO_CIO;
729 priv->state = DEV_STATE_NOT_OPER;
730 priv->dev_id.devno = sch->schib.pmcw.dev;
731 priv->dev_id.ssid = sch->schid.ssid;
2c3e7e15
SO
732
733 INIT_WORK(&priv->todo_work, ccw_device_todo);
734 INIT_LIST_HEAD(&priv->cmb_list);
735 init_waitqueue_head(&priv->wait_q);
846d0c6f 736 timer_setup(&priv->timer, ccw_device_timeout, 0);
2c3e7e15
SO
737
738 atomic_set(&priv->onoff, 0);
739 cdev->ccwlock = sch->lock;
7674da77
CH
740 cdev->dev.parent = &sch->dev;
741 cdev->dev.release = ccw_device_release;
ef99516c 742 cdev->dev.groups = ccwdev_attr_groups;
7674da77
CH
743 /* Do first half of device_register. */
744 device_initialize(&cdev->dev);
2c3e7e15
SO
745 ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
746 cdev->private->dev_id.devno);
747 if (ret)
748 goto out_put;
7674da77 749 if (!get_device(&sch->dev)) {
2c3e7e15
SO
750 ret = -ENODEV;
751 goto out_put;
7674da77 752 }
2c3e7e15
SO
753 priv->flags.initialized = 1;
754 spin_lock_irq(sch->lock);
755 sch_set_cdev(sch, cdev);
756 spin_unlock_irq(sch->lock);
7674da77 757 return 0;
2c3e7e15
SO
758
759out_put:
760 /* Release reference from device_initialize(). */
761 put_device(&cdev->dev);
762 return ret;
7674da77
CH
763}
764
765static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
766{
767 struct ccw_device *cdev;
768 int ret;
769
770 cdev = io_subchannel_allocate_dev(sch);
771 if (!IS_ERR(cdev)) {
772 ret = io_subchannel_initialize_dev(sch, cdev);
06739a8a 773 if (ret)
7674da77 774 cdev = ERR_PTR(ret);
7674da77
CH
775 }
776 return cdev;
777}
778
736b5db8 779static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
d7b5a4c9 780
d7b5a4c9
CH
781static void sch_create_and_recog_new_device(struct subchannel *sch)
782{
783 struct ccw_device *cdev;
784
785 /* Need to allocate a new ccw device. */
786 cdev = io_subchannel_create_ccwdev(sch);
787 if (IS_ERR(cdev)) {
788 /* OK, we did everything we could... */
789 css_sch_device_unregister(sch);
790 return;
791 }
d7b5a4c9 792 /* Start recognition for the new ccw device. */
736b5db8 793 io_subchannel_recog(cdev, sch);
d7b5a4c9
CH
794}
795
1da177e4
LT
796/*
797 * Register recognized device.
798 */
37de53bb 799static void io_subchannel_register(struct ccw_device *cdev)
1da177e4 800{
1da177e4 801 struct subchannel *sch;
a290156f 802 int ret, adjust_init_count = 1;
1da177e4
LT
803 unsigned long flags;
804
1da177e4 805 sch = to_subchannel(cdev->dev.parent);
5fb6b854
CH
806 /*
807 * Check if subchannel is still registered. It may have become
808 * unregistered if a machine check hit us after finishing
809 * device recognition but before the register work could be
810 * queued.
811 */
812 if (!device_is_registered(&sch->dev))
813 goto out_err;
82b7ac05 814 css_update_ssd_info(sch);
47af5518
CH
815 /*
816 * io_subchannel_register() will also be called after device
817 * recognition has been done for a boxed device (which will already
818 * be registered). We need to reprobe since we may now have sense id
819 * information.
820 */
d6a30761 821 if (device_is_registered(&cdev->dev)) {
47af5518
CH
822 if (!cdev->drv) {
823 ret = device_reprobe(&cdev->dev);
824 if (ret)
825 /* We can't do much here. */
139b83dd 826 CIO_MSG_EVENT(0, "device_reprobe() returned"
e556bbbd
CH
827 " %d for 0.%x.%04x\n", ret,
828 cdev->private->dev_id.ssid,
829 cdev->private->dev_id.devno);
47af5518 830 }
a290156f 831 adjust_init_count = 0;
1da177e4
LT
832 goto out;
833 }
fa1a8c23
CH
834 /*
835 * Now we know this subchannel will stay, we can throw
836 * our delayed uevent.
837 */
05ce3e53
CH
838 if (dev_get_uevent_suppress(&sch->dev)) {
839 dev_set_uevent_suppress(&sch->dev, 0);
840 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
841 }
1da177e4 842 /* make it known to the system */
2c3e7e15 843 ret = ccw_device_add(cdev);
1da177e4 844 if (ret) {
e556bbbd
CH
845 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
846 cdev->private->dev_id.ssid,
847 cdev->private->dev_id.devno, ret);
2ec22984 848 spin_lock_irqsave(sch->lock, flags);
db6a6423 849 sch_set_cdev(sch, NULL);
2ec22984 850 spin_unlock_irqrestore(sch->lock, flags);
6eff208f
CH
851 /* Release initial device reference. */
852 put_device(&cdev->dev);
5fb6b854 853 goto out_err;
1da177e4 854 }
1da177e4
LT
855out:
856 cdev->private->flags.recog_done = 1;
1da177e4 857 wake_up(&cdev->private->wait_q);
5fb6b854 858out_err:
a290156f 859 if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
1da177e4
LT
860 wake_up(&ccw_device_init_wq);
861}
862
37de53bb 863static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
1da177e4 864{
1da177e4
LT
865 struct subchannel *sch;
866
46fbe4e4
PO
867 /* Get subchannel reference for local processing. */
868 if (!get_device(cdev->dev.parent))
869 return;
1da177e4 870 sch = to_subchannel(cdev->dev.parent);
6ab4879a 871 css_sch_device_unregister(sch);
46fbe4e4 872 /* Release subchannel reference for local processing. */
1da177e4
LT
873 put_device(&sch->dev);
874}
875
876/*
877 * subchannel recognition done. Called from the state machine.
878 */
879void
880io_subchannel_recog_done(struct ccw_device *cdev)
881{
1da177e4
LT
882 if (css_init_done == 0) {
883 cdev->private->flags.recog_done = 1;
884 return;
885 }
886 switch (cdev->private->state) {
47593bfa
SO
887 case DEV_STATE_BOXED:
888 /* Device did not respond in time. */
1da177e4
LT
889 case DEV_STATE_NOT_OPER:
890 cdev->private->flags.recog_done = 1;
37de53bb
PO
891 /* Remove device found not operational. */
892 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1da177e4
LT
893 if (atomic_dec_and_test(&ccw_device_init_count))
894 wake_up(&ccw_device_init_wq);
895 break;
1da177e4 896 case DEV_STATE_OFFLINE:
37db8985 897 /*
1da177e4
LT
898 * We can't register the device in interrupt context so
899 * we schedule a work item.
900 */
37de53bb 901 ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
1da177e4
LT
902 break;
903 }
904}
905
736b5db8 906static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1da177e4 907{
1da177e4
LT
908 /* Increase counter of devices currently in recognition. */
909 atomic_inc(&ccw_device_init_count);
910
911 /* Start async. device sensing. */
2ec22984 912 spin_lock_irq(sch->lock);
736b5db8 913 ccw_device_recognition(cdev);
2ec22984 914 spin_unlock_irq(sch->lock);
1da177e4
LT
915}
916
5d6e6b6f
PO
917static int ccw_device_move_to_sch(struct ccw_device *cdev,
918 struct subchannel *sch)
d7b5a4c9 919{
5d6e6b6f 920 struct subchannel *old_sch;
0c609fca 921 int rc, old_enabled = 0;
d7b5a4c9 922
5d6e6b6f
PO
923 old_sch = to_subchannel(cdev->dev.parent);
924 /* Obtain child reference for new parent. */
6eff208f 925 if (!get_device(&sch->dev))
5d6e6b6f 926 return -ENODEV;
0c609fca
SO
927
928 if (!sch_is_pseudo_sch(old_sch)) {
929 spin_lock_irq(old_sch->lock);
930 old_enabled = old_sch->schib.pmcw.ena;
931 rc = 0;
932 if (old_enabled)
933 rc = cio_disable_subchannel(old_sch);
934 spin_unlock_irq(old_sch->lock);
935 if (rc == -EBUSY) {
936 /* Release child reference for new parent. */
937 put_device(&sch->dev);
938 return rc;
939 }
940 }
941
d7b5a4c9 942 mutex_lock(&sch->reg_mutex);
ffa6a705 943 rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
d7b5a4c9
CH
944 mutex_unlock(&sch->reg_mutex);
945 if (rc) {
5d6e6b6f 946 CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
d7b5a4c9
CH
947 cdev->private->dev_id.ssid,
948 cdev->private->dev_id.devno, sch->schid.ssid,
5d6e6b6f 949 sch->schib.pmcw.dev, rc);
0c609fca
SO
950 if (old_enabled) {
951 /* Try to reenable the old subchannel. */
952 spin_lock_irq(old_sch->lock);
953 cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
954 spin_unlock_irq(old_sch->lock);
955 }
5d6e6b6f 956 /* Release child reference for new parent. */
6eff208f 957 put_device(&sch->dev);
5d6e6b6f 958 return rc;
d7b5a4c9 959 }
5d6e6b6f
PO
960 /* Clean up old subchannel. */
961 if (!sch_is_pseudo_sch(old_sch)) {
962 spin_lock_irq(old_sch->lock);
963 sch_set_cdev(old_sch, NULL);
5d6e6b6f
PO
964 spin_unlock_irq(old_sch->lock);
965 css_schedule_eval(old_sch->schid);
d7b5a4c9 966 }
5d6e6b6f
PO
967 /* Release child reference for old parent. */
968 put_device(&old_sch->dev);
969 /* Initialize new subchannel. */
970 spin_lock_irq(sch->lock);
5d6e6b6f
PO
971 cdev->ccwlock = sch->lock;
972 if (!sch_is_pseudo_sch(sch))
973 sch_set_cdev(sch, cdev);
974 spin_unlock_irq(sch->lock);
975 if (!sch_is_pseudo_sch(sch))
976 css_update_ssd_info(sch);
977 return 0;
978}
979
980static int ccw_device_move_to_orph(struct ccw_device *cdev)
981{
982 struct subchannel *sch = to_subchannel(cdev->dev.parent);
983 struct channel_subsystem *css = to_css(sch->dev.parent);
984
985 return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
d7b5a4c9
CH
986}
987
602b20f2
CH
988static void io_subchannel_irq(struct subchannel *sch)
989{
990 struct ccw_device *cdev;
991
db6a6423 992 cdev = sch_get_cdev(sch);
602b20f2 993
efd986db
SO
994 CIO_TRACE_EVENT(6, "IRQ");
995 CIO_TRACE_EVENT(6, dev_name(&sch->dev));
602b20f2
CH
996 if (cdev)
997 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
de400d6b 998 else
420f42ec 999 inc_irq_stat(IRQIO_CIO);
602b20f2
CH
1000}
1001
13952ec1
SO
1002void io_subchannel_init_config(struct subchannel *sch)
1003{
1004 memset(&sch->config, 0, sizeof(sch->config));
1005 sch->config.csense = 1;
13952ec1
SO
1006}
1007
0ae7a7b2
CH
1008static void io_subchannel_init_fields(struct subchannel *sch)
1009{
1010 if (cio_is_console(sch->schid))
1011 sch->opm = 0xff;
1012 else
1013 sch->opm = chp_get_sch_opm(sch);
1014 sch->lpm = sch->schib.pmcw.pam & sch->opm;
3a3fc29a 1015 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
0ae7a7b2
CH
1016
1017 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1018 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1019 sch->schib.pmcw.dev, sch->schid.ssid,
1020 sch->schid.sch_no, sch->schib.pmcw.pim,
1021 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
13952ec1
SO
1022
1023 io_subchannel_init_config(sch);
0ae7a7b2
CH
1024}
1025
90ed2b69
CH
1026/*
1027 * Note: We always return 0 so that we bind to the device even on error.
1028 * This is needed so that our remove function is called on unregister.
1029 */
0ae7a7b2 1030static int io_subchannel_probe(struct subchannel *sch)
1da177e4 1031{
f92519e8 1032 struct io_subchannel_private *io_priv;
1da177e4
LT
1033 struct ccw_device *cdev;
1034 int rc;
1da177e4 1035
6d7c5afc 1036 if (cio_is_console(sch->schid)) {
7e9db9ea
CH
1037 rc = sysfs_create_group(&sch->dev.kobj,
1038 &io_subchannel_attr_group);
1039 if (rc)
1040 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1041 "attributes for subchannel "
1042 "0.%x.%04x (rc=%d)\n",
1043 sch->schid.ssid, sch->schid.sch_no, rc);
1da177e4 1044 /*
6d7c5afc 1045 * The console subchannel already has an associated ccw_device.
7e9db9ea 1046 * Throw the delayed uevent for the subchannel, register
6d7c5afc 1047 * the ccw_device and exit.
1da177e4 1048 */
05ce3e53
CH
1049 if (dev_get_uevent_suppress(&sch->dev)) {
1050 /* should always be the case for the console */
1051 dev_set_uevent_suppress(&sch->dev, 0);
1052 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1053 }
6d7c5afc 1054 cdev = sch_get_cdev(sch);
2c3e7e15 1055 rc = ccw_device_add(cdev);
afdfed0f
SO
1056 if (rc) {
1057 /* Release online reference. */
1058 put_device(&cdev->dev);
1059 goto out_schedule;
1060 }
0ad8f714
SO
1061 if (atomic_dec_and_test(&ccw_device_init_count))
1062 wake_up(&ccw_device_init_wq);
1da177e4
LT
1063 return 0;
1064 }
0ae7a7b2 1065 io_subchannel_init_fields(sch);
f444cc0e
SO
1066 rc = cio_commit_config(sch);
1067 if (rc)
1068 goto out_schedule;
7e9db9ea
CH
1069 rc = sysfs_create_group(&sch->dev.kobj,
1070 &io_subchannel_attr_group);
1071 if (rc)
90ed2b69 1072 goto out_schedule;
cd6b4f27 1073 /* Allocate I/O subchannel private data. */
f92519e8
SO
1074 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1075 if (!io_priv)
48e4c385 1076 goto out_schedule;
f92519e8 1077
37db8985
HP
1078 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1079 sizeof(*io_priv->dma_area),
1080 &io_priv->dma_area_dma, GFP_KERNEL);
1081 if (!io_priv->dma_area) {
1082 kfree(io_priv);
1083 goto out_schedule;
1084 }
1085
f92519e8 1086 set_io_private(sch, io_priv);
5d6e6b6f 1087 css_schedule_eval(sch->schid);
7e9db9ea 1088 return 0;
48e4c385 1089
90ed2b69 1090out_schedule:
390935ac
PO
1091 spin_lock_irq(sch->lock);
1092 css_sched_sch_todo(sch, SCH_TODO_UNREG);
1093 spin_unlock_irq(sch->lock);
90ed2b69 1094 return 0;
1da177e4
LT
1095}
1096
135a8b4c 1097static int io_subchannel_remove(struct subchannel *sch)
1da177e4 1098{
f92519e8 1099 struct io_subchannel_private *io_priv = to_io_private(sch);
1da177e4 1100 struct ccw_device *cdev;
1da177e4 1101
db6a6423
CH
1102 cdev = sch_get_cdev(sch);
1103 if (!cdev)
48e4c385 1104 goto out_free;
135a8b4c
SO
1105
1106 ccw_device_unregister(cdev);
1107 spin_lock_irq(sch->lock);
db6a6423 1108 sch_set_cdev(sch, NULL);
f92519e8 1109 set_io_private(sch, NULL);
135a8b4c 1110 spin_unlock_irq(sch->lock);
48e4c385 1111out_free:
37db8985
HP
1112 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1113 io_priv->dma_area, io_priv->dma_area_dma);
f92519e8 1114 kfree(io_priv);
7e9db9ea 1115 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1da177e4
LT
1116 return 0;
1117}
1118
602b20f2 1119static void io_subchannel_verify(struct subchannel *sch)
1da177e4
LT
1120{
1121 struct ccw_device *cdev;
1122
db6a6423 1123 cdev = sch_get_cdev(sch);
1da177e4
LT
1124 if (cdev)
1125 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1126}
1127
c820de39
CH
1128static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1129{
1130 struct ccw_device *cdev;
1131
1132 cdev = sch_get_cdev(sch);
1133 if (!cdev)
1134 return;
4257aaec
PO
1135 if (cio_update_schib(sch))
1136 goto err;
1137 /* Check for I/O on path. */
1138 if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1139 goto out;
1140 if (cdev->private->state == DEV_STATE_ONLINE) {
1141 ccw_device_kill_io(cdev);
1142 goto out;
1143 }
1144 if (cio_clear(sch))
1145 goto err;
1146out:
1147 /* Trigger path verification. */
1148 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1149 return;
c820de39 1150
4257aaec
PO
1151err:
1152 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
c820de39
CH
1153}
1154
99611f87
CH
1155static int io_subchannel_chp_event(struct subchannel *sch,
1156 struct chp_link *link, int event)
c820de39 1157{
585b954e 1158 struct ccw_device *cdev = sch_get_cdev(sch);
32ef9388
VV
1159 int mask, chpid, valid_bit;
1160 int path_event[8];
c820de39 1161
99611f87 1162 mask = chp_ssd_get_mask(&sch->ssd_info, link);
c820de39
CH
1163 if (!mask)
1164 return 0;
1165 switch (event) {
1166 case CHP_VARY_OFF:
1167 sch->opm &= ~mask;
1168 sch->lpm &= ~mask;
585b954e
SO
1169 if (cdev)
1170 cdev->private->path_gone_mask |= mask;
c820de39
CH
1171 io_subchannel_terminate_path(sch, mask);
1172 break;
1173 case CHP_VARY_ON:
1174 sch->opm |= mask;
1175 sch->lpm |= mask;
585b954e
SO
1176 if (cdev)
1177 cdev->private->path_new_mask |= mask;
c820de39
CH
1178 io_subchannel_verify(sch);
1179 break;
1180 case CHP_OFFLINE:
cdb912a4 1181 if (cio_update_schib(sch))
c820de39 1182 return -ENODEV;
585b954e
SO
1183 if (cdev)
1184 cdev->private->path_gone_mask |= mask;
c820de39
CH
1185 io_subchannel_terminate_path(sch, mask);
1186 break;
1187 case CHP_ONLINE:
cdb912a4
SO
1188 if (cio_update_schib(sch))
1189 return -ENODEV;
c820de39 1190 sch->lpm |= mask & sch->opm;
585b954e
SO
1191 if (cdev)
1192 cdev->private->path_new_mask |= mask;
c820de39
CH
1193 io_subchannel_verify(sch);
1194 break;
32ef9388
VV
1195 case CHP_FCES_EVENT:
1196 /* Forward Endpoint Security event */
1197 for (chpid = 0, valid_bit = 0x80; chpid < 8; chpid++,
1198 valid_bit >>= 1) {
1199 if (mask & valid_bit)
1200 path_event[chpid] = PE_PATH_FCES_EVENT;
1201 else
1202 path_event[chpid] = PE_NONE;
1203 }
1204 if (cdev)
1205 cdev->drv->path_event(cdev, path_event);
1206 break;
c820de39
CH
1207 }
1208 return 0;
1209}
1210
6e9a0f67 1211static void io_subchannel_quiesce(struct subchannel *sch)
1da177e4 1212{
1da177e4
LT
1213 struct ccw_device *cdev;
1214 int ret;
1215
56e6b796 1216 spin_lock_irq(sch->lock);
db6a6423 1217 cdev = sch_get_cdev(sch);
a8237fc4 1218 if (cio_is_console(sch->schid))
56e6b796 1219 goto out_unlock;
1da177e4 1220 if (!sch->schib.pmcw.ena)
56e6b796 1221 goto out_unlock;
1da177e4
LT
1222 ret = cio_disable_subchannel(sch);
1223 if (ret != -EBUSY)
56e6b796 1224 goto out_unlock;
1da177e4 1225 if (cdev->handler)
56e6b796
SO
1226 cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1227 while (ret == -EBUSY) {
1228 cdev->private->state = DEV_STATE_QUIESCE;
376ae475 1229 cdev->private->iretry = 255;
56e6b796
SO
1230 ret = ccw_device_cancel_halt_clear(cdev);
1231 if (ret == -EBUSY) {
1232 ccw_device_set_timeout(cdev, HZ/10);
1233 spin_unlock_irq(sch->lock);
1234 wait_event(cdev->private->wait_q,
1235 cdev->private->state != DEV_STATE_QUIESCE);
1236 spin_lock_irq(sch->lock);
1237 }
1238 ret = cio_disable_subchannel(sch);
1da177e4 1239 }
56e6b796
SO
1240out_unlock:
1241 spin_unlock_irq(sch->lock);
1da177e4
LT
1242}
1243
6e9a0f67
SO
1244static void io_subchannel_shutdown(struct subchannel *sch)
1245{
1246 io_subchannel_quiesce(sch);
1247}
1248
c820de39
CH
1249static int device_is_disconnected(struct ccw_device *cdev)
1250{
1251 if (!cdev)
1252 return 0;
1253 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1254 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1255}
1256
1257static int recovery_check(struct device *dev, void *data)
1258{
1259 struct ccw_device *cdev = to_ccwdev(dev);
55fb7347 1260 struct subchannel *sch;
c820de39
CH
1261 int *redo = data;
1262
1263 spin_lock_irq(cdev->ccwlock);
1264 switch (cdev->private->state) {
55fb7347
SO
1265 case DEV_STATE_ONLINE:
1266 sch = to_subchannel(cdev->dev.parent);
1267 if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1268 break;
b09fcecb 1269 fallthrough;
c820de39
CH
1270 case DEV_STATE_DISCONNECTED:
1271 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1272 cdev->private->dev_id.ssid,
1273 cdev->private->dev_id.devno);
1274 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1275 *redo = 1;
1276 break;
1277 case DEV_STATE_DISCONNECTED_SENSE_ID:
1278 *redo = 1;
1279 break;
1280 }
1281 spin_unlock_irq(cdev->ccwlock);
1282
1283 return 0;
1284}
1285
1286static void recovery_work_func(struct work_struct *unused)
1287{
1288 int redo = 0;
1289
1290 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1291 if (redo) {
1292 spin_lock_irq(&recovery_lock);
1293 if (!timer_pending(&recovery_timer)) {
1294 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1295 recovery_phase++;
1296 mod_timer(&recovery_timer, jiffies +
1297 recovery_delay[recovery_phase] * HZ);
1298 }
1299 spin_unlock_irq(&recovery_lock);
1300 } else
55fb7347 1301 CIO_MSG_EVENT(3, "recovery: end\n");
c820de39
CH
1302}
1303
1304static DECLARE_WORK(recovery_work, recovery_work_func);
1305
846d0c6f 1306static void recovery_func(struct timer_list *unused)
c820de39
CH
1307{
1308 /*
1309 * We can't do our recovery in softirq context and it's not
1310 * performance critical, so we schedule it.
1311 */
1312 schedule_work(&recovery_work);
1313}
1314
55fb7347 1315void ccw_device_schedule_recovery(void)
c820de39
CH
1316{
1317 unsigned long flags;
1318
55fb7347 1319 CIO_MSG_EVENT(3, "recovery: schedule\n");
c820de39
CH
1320 spin_lock_irqsave(&recovery_lock, flags);
1321 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1322 recovery_phase = 0;
1323 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1324 }
1325 spin_unlock_irqrestore(&recovery_lock, flags);
1326}
1327
ecf5d9ef
PO
1328static int purge_fn(struct device *dev, void *data)
1329{
1330 struct ccw_device *cdev = to_ccwdev(dev);
37de53bb 1331 struct ccw_dev_id *id = &cdev->private->dev_id;
ecf5d9ef
PO
1332
1333 spin_lock_irq(cdev->ccwlock);
37de53bb 1334 if (is_blacklisted(id->ssid, id->devno) &&
a2fc8485
PO
1335 (cdev->private->state == DEV_STATE_OFFLINE) &&
1336 (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
37de53bb
PO
1337 CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1338 id->devno);
1339 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
a2fc8485 1340 atomic_set(&cdev->private->onoff, 0);
37de53bb 1341 }
ecf5d9ef 1342 spin_unlock_irq(cdev->ccwlock);
ecf5d9ef
PO
1343 /* Abort loop in case of pending signal. */
1344 if (signal_pending(current))
1345 return -EINTR;
1346
1347 return 0;
1348}
1349
1350/**
1351 * ccw_purge_blacklisted - purge unused, blacklisted devices
1352 *
1353 * Unregister all ccw devices that are offline and on the blacklist.
1354 */
1355int ccw_purge_blacklisted(void)
1356{
1357 CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1358 bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1359 return 0;
1360}
1361
6afcc775 1362void ccw_device_set_disconnected(struct ccw_device *cdev)
c820de39
CH
1363{
1364 if (!cdev)
1365 return;
1366 ccw_device_set_timeout(cdev, 0);
1367 cdev->private->flags.fake_irb = 0;
1368 cdev->private->state = DEV_STATE_DISCONNECTED;
1369 if (cdev->online)
1370 ccw_device_schedule_recovery();
1371}
1372
91c36919
PO
1373void ccw_device_set_notoper(struct ccw_device *cdev)
1374{
1375 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1376
1377 CIO_TRACE_EVENT(2, "notoper");
b9d3aed7 1378 CIO_TRACE_EVENT(2, dev_name(&sch->dev));
91c36919
PO
1379 ccw_device_set_timeout(cdev, 0);
1380 cio_disable_subchannel(sch);
1381 cdev->private->state = DEV_STATE_NOT_OPER;
1382}
1383
5d6e6b6f
PO
1384enum io_sch_action {
1385 IO_SCH_UNREG,
1386 IO_SCH_ORPH_UNREG,
1387 IO_SCH_ATTACH,
1388 IO_SCH_UNREG_ATTACH,
1389 IO_SCH_ORPH_ATTACH,
1390 IO_SCH_REPROBE,
1391 IO_SCH_VERIFY,
1392 IO_SCH_DISC,
1393 IO_SCH_NOP,
1394};
1395
1396static enum io_sch_action sch_get_action(struct subchannel *sch)
1397{
1398 struct ccw_device *cdev;
1399
1400 cdev = sch_get_cdev(sch);
1401 if (cio_update_schib(sch)) {
1402 /* Not operational. */
1403 if (!cdev)
1404 return IO_SCH_UNREG;
76e6fb4b 1405 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
5d6e6b6f
PO
1406 return IO_SCH_UNREG;
1407 return IO_SCH_ORPH_UNREG;
1408 }
1409 /* Operational. */
1410 if (!cdev)
1411 return IO_SCH_ATTACH;
1412 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
76e6fb4b 1413 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
5d6e6b6f
PO
1414 return IO_SCH_UNREG_ATTACH;
1415 return IO_SCH_ORPH_ATTACH;
1416 }
1417 if ((sch->schib.pmcw.pam & sch->opm) == 0) {
76e6fb4b 1418 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
5d6e6b6f
PO
1419 return IO_SCH_UNREG;
1420 return IO_SCH_DISC;
1421 }
1422 if (device_is_disconnected(cdev))
1423 return IO_SCH_REPROBE;
688b1977 1424 if (cdev->online)
5d6e6b6f 1425 return IO_SCH_VERIFY;
43d0be75
SO
1426 if (cdev->private->state == DEV_STATE_NOT_OPER)
1427 return IO_SCH_UNREG_ATTACH;
5d6e6b6f
PO
1428 return IO_SCH_NOP;
1429}
1430
1431/**
1432 * io_subchannel_sch_event - process subchannel event
1433 * @sch: subchannel
1434 * @process: non-zero if function is called in process context
1435 *
1436 * An unspecified event occurred for this subchannel. Adjust data according
1437 * to the current operational state of the subchannel and device. Return
1438 * zero when the event has been handled sufficiently or -EAGAIN when this
1439 * function should be called again in process context.
1440 */
1441static int io_subchannel_sch_event(struct subchannel *sch, int process)
c820de39 1442{
c820de39 1443 unsigned long flags;
c820de39 1444 struct ccw_device *cdev;
5d6e6b6f
PO
1445 struct ccw_dev_id dev_id;
1446 enum io_sch_action action;
1447 int rc = -EAGAIN;
c820de39
CH
1448
1449 spin_lock_irqsave(sch->lock, flags);
5d6e6b6f
PO
1450 if (!device_is_registered(&sch->dev))
1451 goto out_unlock;
390935ac
PO
1452 if (work_pending(&sch->todo_work))
1453 goto out_unlock;
37de53bb
PO
1454 cdev = sch_get_cdev(sch);
1455 if (cdev && work_pending(&cdev->private->todo_work))
1456 goto out_unlock;
5d6e6b6f
PO
1457 action = sch_get_action(sch);
1458 CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1459 sch->schid.ssid, sch->schid.sch_no, process,
1460 action);
1461 /* Perform immediate actions while holding the lock. */
5d6e6b6f
PO
1462 switch (action) {
1463 case IO_SCH_REPROBE:
1464 /* Trigger device recognition. */
1465 ccw_device_trigger_reprobe(cdev);
1466 rc = 0;
1467 goto out_unlock;
1468 case IO_SCH_VERIFY:
1469 /* Trigger path verification. */
1470 io_subchannel_verify(sch);
1471 rc = 0;
1472 goto out_unlock;
1473 case IO_SCH_DISC:
1474 ccw_device_set_disconnected(cdev);
1475 rc = 0;
1476 goto out_unlock;
1477 case IO_SCH_ORPH_UNREG:
1478 case IO_SCH_ORPH_ATTACH:
1479 ccw_device_set_disconnected(cdev);
1480 break;
1481 case IO_SCH_UNREG_ATTACH:
1482 case IO_SCH_UNREG:
16d2ce27
SO
1483 if (!cdev)
1484 break;
1485 if (cdev->private->state == DEV_STATE_SENSE_ID) {
1486 /*
1487 * Note: delayed work triggered by this event
1488 * and repeated calls to sch_event are synchronized
1489 * by the above check for work_pending(cdev).
1490 */
1491 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1492 } else
5d6e6b6f
PO
1493 ccw_device_set_notoper(cdev);
1494 break;
1495 case IO_SCH_NOP:
1496 rc = 0;
1497 goto out_unlock;
1498 default:
1499 break;
c820de39 1500 }
5d6e6b6f
PO
1501 spin_unlock_irqrestore(sch->lock, flags);
1502 /* All other actions require process context. */
1503 if (!process)
1504 goto out;
1505 /* Handle attached ccw device. */
1506 switch (action) {
1507 case IO_SCH_ORPH_UNREG:
1508 case IO_SCH_ORPH_ATTACH:
1509 /* Move ccw device to orphanage. */
1510 rc = ccw_device_move_to_orph(cdev);
1511 if (rc)
1512 goto out;
c820de39 1513 break;
5d6e6b6f 1514 case IO_SCH_UNREG_ATTACH:
3368ba25 1515 spin_lock_irqsave(sch->lock, flags);
3368ba25
SO
1516 sch_set_cdev(sch, NULL);
1517 spin_unlock_irqrestore(sch->lock, flags);
5d6e6b6f 1518 /* Unregister ccw device. */
74b6127e 1519 ccw_device_unregister(cdev);
c820de39 1520 break;
5d6e6b6f 1521 default:
c820de39
CH
1522 break;
1523 }
5d6e6b6f 1524 /* Handle subchannel. */
c820de39 1525 switch (action) {
5d6e6b6f
PO
1526 case IO_SCH_ORPH_UNREG:
1527 case IO_SCH_UNREG:
8c4662a7 1528 css_sch_device_unregister(sch);
c820de39 1529 break;
5d6e6b6f
PO
1530 case IO_SCH_ORPH_ATTACH:
1531 case IO_SCH_UNREG_ATTACH:
1532 case IO_SCH_ATTACH:
1533 dev_id.ssid = sch->schid.ssid;
1534 dev_id.devno = sch->schib.pmcw.dev;
1535 cdev = get_ccwdev_by_dev_id(&dev_id);
1536 if (!cdev) {
1537 sch_create_and_recog_new_device(sch);
1538 break;
1539 }
1540 rc = ccw_device_move_to_sch(cdev, sch);
1541 if (rc) {
1542 /* Release reference from get_ccwdev_by_dev_id() */
1543 put_device(&cdev->dev);
1544 goto out;
1545 }
1546 spin_lock_irqsave(sch->lock, flags);
c820de39 1547 ccw_device_trigger_reprobe(cdev);
5d6e6b6f
PO
1548 spin_unlock_irqrestore(sch->lock, flags);
1549 /* Release reference from get_ccwdev_by_dev_id() */
1550 put_device(&cdev->dev);
91c36919 1551 break;
c820de39
CH
1552 default:
1553 break;
1554 }
5d6e6b6f 1555 return 0;
c820de39 1556
5d6e6b6f
PO
1557out_unlock:
1558 spin_unlock_irqrestore(sch->lock, flags);
1559out:
1560 return rc;
c820de39
CH
1561}
1562
137a14f4
SO
1563static void ccw_device_set_int_class(struct ccw_device *cdev)
1564{
1565 struct ccw_driver *cdrv = cdev->drv;
1566
1567 /* Note: we interpret class 0 in this context as an uninitialized
1568 * field since it translates to a non-I/O interrupt class. */
1569 if (cdrv->int_class != 0)
1570 cdev->private->int_class = cdrv->int_class;
1571 else
1572 cdev->private->int_class = IRQIO_CIO;
1573}
1574
1da177e4 1575#ifdef CONFIG_CCW_CONSOLE
1e532096 1576int __init ccw_device_enable_console(struct ccw_device *cdev)
1da177e4 1577{
1e532096 1578 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1da177e4
LT
1579 int rc;
1580
1e532096
SO
1581 if (!cdev->drv || !cdev->handler)
1582 return -EINVAL;
1583
0ae7a7b2 1584 io_subchannel_init_fields(sch);
f444cc0e
SO
1585 rc = cio_commit_config(sch);
1586 if (rc)
1587 return rc;
0ae7a7b2 1588 sch->driver = &io_subchannel_driver;
736b5db8 1589 io_subchannel_recog(cdev, sch);
1da177e4
LT
1590 /* Now wait for the async. recognition to come to an end. */
1591 spin_lock_irq(cdev->ccwlock);
1592 while (!dev_fsm_final_state(cdev))
188561a4
SO
1593 ccw_device_wait_idle(cdev);
1594
afdfed0f
SO
1595 /* Hold on to an extra reference while device is online. */
1596 get_device(&cdev->dev);
1597 rc = ccw_device_online(cdev);
1598 if (rc)
1da177e4 1599 goto out_unlock;
afdfed0f 1600
1da177e4 1601 while (!dev_fsm_final_state(cdev))
188561a4
SO
1602 ccw_device_wait_idle(cdev);
1603
afdfed0f
SO
1604 if (cdev->private->state == DEV_STATE_ONLINE)
1605 cdev->online = 1;
1606 else
1607 rc = -EIO;
1da177e4
LT
1608out_unlock:
1609 spin_unlock_irq(cdev->ccwlock);
afdfed0f
SO
1610 if (rc) /* Give up online reference since onlining failed. */
1611 put_device(&cdev->dev);
736b5db8 1612 return rc;
1da177e4
LT
1613}
1614
1e532096 1615struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1da177e4 1616{
863fc849 1617 struct io_subchannel_private *io_priv;
afdfed0f 1618 struct ccw_device *cdev;
1da177e4 1619 struct subchannel *sch;
1da177e4 1620
1da177e4 1621 sch = cio_probe_console();
afdfed0f
SO
1622 if (IS_ERR(sch))
1623 return ERR_CAST(sch);
863fc849
SO
1624
1625 io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
37db8985
HP
1626 if (!io_priv)
1627 goto err_priv;
1628 io_priv->dma_area = dma_alloc_coherent(&sch->dev,
1629 sizeof(*io_priv->dma_area),
1630 &io_priv->dma_area_dma, GFP_KERNEL);
1631 if (!io_priv->dma_area)
1632 goto err_dma_area;
2c3e7e15 1633 set_io_private(sch, io_priv);
afdfed0f
SO
1634 cdev = io_subchannel_create_ccwdev(sch);
1635 if (IS_ERR(cdev)) {
37db8985
HP
1636 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1637 io_priv->dma_area, io_priv->dma_area_dma);
1638 set_io_private(sch, NULL);
afdfed0f
SO
1639 put_device(&sch->dev);
1640 kfree(io_priv);
1641 return cdev;
1642 }
2253e8d7 1643 cdev->drv = drv;
137a14f4 1644 ccw_device_set_int_class(cdev);
afdfed0f 1645 return cdev;
37db8985
HP
1646
1647err_dma_area:
1648 kfree(io_priv);
1649err_priv:
1650 put_device(&sch->dev);
1651 return ERR_PTR(-ENOMEM);
1da177e4 1652}
1f4e7eda 1653
1e532096
SO
1654void __init ccw_device_destroy_console(struct ccw_device *cdev)
1655{
1656 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1657 struct io_subchannel_private *io_priv = to_io_private(sch);
1658
1659 set_io_private(sch, NULL);
37db8985
HP
1660 dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
1661 io_priv->dma_area, io_priv->dma_area_dma);
14d4c4fa
QM
1662 put_device(&sch->dev);
1663 put_device(&cdev->dev);
1e532096
SO
1664 kfree(io_priv);
1665}
1666
188561a4
SO
1667/**
1668 * ccw_device_wait_idle() - busy wait for device to become idle
1669 * @cdev: ccw device
1670 *
1671 * Poll until activity control is zero, that is, no function or data
1672 * transfer is pending/active.
1673 * Called with device lock being held.
1674 */
1675void ccw_device_wait_idle(struct ccw_device *cdev)
1676{
1677 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1678
1679 while (1) {
1680 cio_tsch(sch);
1681 if (sch->schib.scsw.cmd.actl == 0)
1682 break;
e0d62dcb 1683 udelay(100);
188561a4
SO
1684 }
1685}
1da177e4
LT
1686#endif
1687
b2ffd8e9
CH
1688/**
1689 * get_ccwdev_by_busid() - obtain device from a bus id
1690 * @cdrv: driver the device is owned by
1691 * @bus_id: bus id of the device to be searched
1692 *
1693 * This function searches all devices owned by @cdrv for a device with a bus
1694 * id matching @bus_id.
1695 * Returns:
1696 * If a match is found, its reference count of the found device is increased
1697 * and it is returned; else %NULL is returned.
1698 */
1699struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1700 const char *bus_id)
1da177e4 1701{
b0744bd2 1702 struct device *dev;
1da177e4 1703
6cda08a2 1704 dev = driver_find_device_by_name(&cdrv->driver, bus_id);
1da177e4 1705
d2c993d8 1706 return dev ? to_ccwdev(dev) : NULL;
1da177e4
LT
1707}
1708
1709/************************** device driver handling ************************/
1710
1711/* This is the implementation of the ccw_driver class. The probe, remove
1712 * and release methods are initially very similar to the device_driver
1713 * implementations, with the difference that they have ccw_device
1714 * arguments.
1715 *
1716 * A ccw driver also contains the information that is needed for
1717 * device matching.
1718 */
1719static int
1720ccw_device_probe (struct device *dev)
1721{
1722 struct ccw_device *cdev = to_ccwdev(dev);
1723 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1724 int ret;
1725
1726 cdev->drv = cdrv; /* to let the driver call _set_online */
137a14f4 1727 ccw_device_set_int_class(cdev);
1da177e4 1728 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1da177e4 1729 if (ret) {
d2c993d8 1730 cdev->drv = NULL;
420f42ec 1731 cdev->private->int_class = IRQIO_CIO;
1da177e4
LT
1732 return ret;
1733 }
1734
1735 return 0;
1736}
1737
74bd0d85 1738static int ccw_device_remove(struct device *dev)
1da177e4
LT
1739{
1740 struct ccw_device *cdev = to_ccwdev(dev);
1741 struct ccw_driver *cdrv = cdev->drv;
135a8b4c 1742 struct subchannel *sch;
1da177e4
LT
1743 int ret;
1744
1da177e4
LT
1745 if (cdrv->remove)
1746 cdrv->remove(cdev);
74bd0d85
SO
1747
1748 spin_lock_irq(cdev->ccwlock);
1da177e4
LT
1749 if (cdev->online) {
1750 cdev->online = 0;
1da177e4
LT
1751 ret = ccw_device_offline(cdev);
1752 spin_unlock_irq(cdev->ccwlock);
1753 if (ret == 0)
1754 wait_event(cdev->private->wait_q,
1755 dev_fsm_final_state(cdev));
1756 else
139b83dd 1757 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
e556bbbd
CH
1758 "device 0.%x.%04x\n",
1759 ret, cdev->private->dev_id.ssid,
1760 cdev->private->dev_id.devno);
9cd67421
CH
1761 /* Give up reference obtained in ccw_device_set_online(). */
1762 put_device(&cdev->dev);
74bd0d85 1763 spin_lock_irq(cdev->ccwlock);
1da177e4
LT
1764 }
1765 ccw_device_set_timeout(cdev, 0);
d2c993d8 1766 cdev->drv = NULL;
420f42ec 1767 cdev->private->int_class = IRQIO_CIO;
135a8b4c 1768 sch = to_subchannel(cdev->dev.parent);
74bd0d85 1769 spin_unlock_irq(cdev->ccwlock);
135a8b4c 1770 io_subchannel_quiesce(sch);
a6ef1565
SO
1771 __disable_cmf(cdev);
1772
1da177e4
LT
1773 return 0;
1774}
1775
958974fb
CH
1776static void ccw_device_shutdown(struct device *dev)
1777{
1778 struct ccw_device *cdev;
1779
1780 cdev = to_ccwdev(dev);
1781 if (cdev->drv && cdev->drv->shutdown)
1782 cdev->drv->shutdown(cdev);
1bc6664b 1783 __disable_cmf(cdev);
958974fb
CH
1784}
1785
d5ab5276 1786static struct bus_type ccw_bus_type = {
8bbace7e
CH
1787 .name = "ccw",
1788 .match = ccw_bus_match,
1789 .uevent = ccw_uevent,
1790 .probe = ccw_device_probe,
1791 .remove = ccw_device_remove,
958974fb 1792 .shutdown = ccw_device_shutdown,
8bbace7e
CH
1793};
1794
b2ffd8e9
CH
1795/**
1796 * ccw_driver_register() - register a ccw driver
1797 * @cdriver: driver to be registered
1798 *
1799 * This function is mainly a wrapper around driver_register().
1800 * Returns:
1801 * %0 on success and a negative error value on failure.
1802 */
1803int ccw_driver_register(struct ccw_driver *cdriver)
1da177e4
LT
1804{
1805 struct device_driver *drv = &cdriver->driver;
1806
1807 drv->bus = &ccw_bus_type;
1da177e4
LT
1808
1809 return driver_register(drv);
1810}
1811
b2ffd8e9
CH
1812/**
1813 * ccw_driver_unregister() - deregister a ccw driver
1814 * @cdriver: driver to be deregistered
1815 *
1816 * This function is mainly a wrapper around driver_unregister().
1817 */
1818void ccw_driver_unregister(struct ccw_driver *cdriver)
1da177e4
LT
1819{
1820 driver_unregister(&cdriver->driver);
1821}
1822
37de53bb
PO
1823static void ccw_device_todo(struct work_struct *work)
1824{
1825 struct ccw_device_private *priv;
1826 struct ccw_device *cdev;
1827 struct subchannel *sch;
1828 enum cdev_todo todo;
1829
1830 priv = container_of(work, struct ccw_device_private, todo_work);
1831 cdev = priv->cdev;
1832 sch = to_subchannel(cdev->dev.parent);
1833 /* Find out todo. */
1834 spin_lock_irq(cdev->ccwlock);
1835 todo = priv->todo;
1836 priv->todo = CDEV_TODO_NOTHING;
1837 CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
1838 priv->dev_id.ssid, priv->dev_id.devno, todo);
1839 spin_unlock_irq(cdev->ccwlock);
1840 /* Perform todo. */
1841 switch (todo) {
1842 case CDEV_TODO_ENABLE_CMF:
1843 cmf_reenable(cdev);
1844 break;
1845 case CDEV_TODO_REBIND:
1846 ccw_device_do_unbind_bind(cdev);
1847 break;
1848 case CDEV_TODO_REGISTER:
1849 io_subchannel_register(cdev);
1850 break;
1851 case CDEV_TODO_UNREG_EVAL:
1852 if (!sch_is_pseudo_sch(sch))
1853 css_schedule_eval(sch->schid);
b09fcecb 1854 fallthrough;
37de53bb
PO
1855 case CDEV_TODO_UNREG:
1856 if (sch_is_pseudo_sch(sch))
1857 ccw_device_unregister(cdev);
1858 else
1859 ccw_device_call_sch_unregister(cdev);
1860 break;
1861 default:
1862 break;
1863 }
1864 /* Release workqueue ref. */
1865 put_device(&cdev->dev);
1866}
1867
1868/**
1869 * ccw_device_sched_todo - schedule ccw device operation
1870 * @cdev: ccw device
1871 * @todo: todo
1872 *
1873 * Schedule the operation identified by @todo to be performed on the slow path
1874 * workqueue. Do nothing if another operation with higher priority is already
1875 * scheduled. Needs to be called with ccwdev lock held.
1876 */
1877void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
1878{
1879 CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
1880 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
1881 todo);
1882 if (cdev->private->todo >= todo)
1883 return;
1884 cdev->private->todo = todo;
1885 /* Get workqueue ref. */
1886 if (!get_device(&cdev->dev))
1887 return;
be5d3823 1888 if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
37de53bb
PO
1889 /* Already queued, release workqueue ref. */
1890 put_device(&cdev->dev);
1891 }
1892}
1893
fd0457a6
ME
1894/**
1895 * ccw_device_siosl() - initiate logging
1896 * @cdev: ccw device
1897 *
1898 * This function is used to invoke model-dependent logging within the channel
1899 * subsystem.
1900 */
1901int ccw_device_siosl(struct ccw_device *cdev)
1902{
1903 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1904
1905 return chsc_siosl(sch->schid);
1906}
1907EXPORT_SYMBOL_GPL(ccw_device_siosl);
1908
1da177e4
LT
1909EXPORT_SYMBOL(ccw_device_set_online);
1910EXPORT_SYMBOL(ccw_device_set_offline);
1911EXPORT_SYMBOL(ccw_driver_register);
1912EXPORT_SYMBOL(ccw_driver_unregister);
1913EXPORT_SYMBOL(get_ccwdev_by_busid);