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