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