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