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