2 * drivers/s390/cio/device_fsm.c
3 * finite state machine for device handling
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/jiffies.h>
14 #include <linux/string.h>
16 #include <asm/ccwdev.h>
20 #include "cio_debug.h"
28 device_is_online(struct subchannel
*sch
)
30 struct ccw_device
*cdev
;
32 if (!sch
->dev
.driver_data
)
34 cdev
= sch
->dev
.driver_data
;
35 return (cdev
->private->state
== DEV_STATE_ONLINE
);
39 device_is_disconnected(struct subchannel
*sch
)
41 struct ccw_device
*cdev
;
43 if (!sch
->dev
.driver_data
)
45 cdev
= sch
->dev
.driver_data
;
46 return (cdev
->private->state
== DEV_STATE_DISCONNECTED
||
47 cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
);
51 device_set_disconnected(struct subchannel
*sch
)
53 struct ccw_device
*cdev
;
55 if (!sch
->dev
.driver_data
)
57 cdev
= sch
->dev
.driver_data
;
58 ccw_device_set_timeout(cdev
, 0);
59 cdev
->private->flags
.fake_irb
= 0;
60 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
63 void device_set_intretry(struct subchannel
*sch
)
65 struct ccw_device
*cdev
;
67 cdev
= sch
->dev
.driver_data
;
70 cdev
->private->flags
.intretry
= 1;
73 int device_trigger_verify(struct subchannel
*sch
)
75 struct ccw_device
*cdev
;
77 cdev
= sch
->dev
.driver_data
;
78 if (!cdev
|| !cdev
->online
)
80 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
85 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
88 ccw_device_timeout(unsigned long data
)
90 struct ccw_device
*cdev
;
92 cdev
= (struct ccw_device
*) data
;
93 spin_lock_irq(cdev
->ccwlock
);
94 dev_fsm_event(cdev
, DEV_EVENT_TIMEOUT
);
95 spin_unlock_irq(cdev
->ccwlock
);
102 ccw_device_set_timeout(struct ccw_device
*cdev
, int expires
)
105 del_timer(&cdev
->private->timer
);
108 if (timer_pending(&cdev
->private->timer
)) {
109 if (mod_timer(&cdev
->private->timer
, jiffies
+ expires
))
112 cdev
->private->timer
.function
= ccw_device_timeout
;
113 cdev
->private->timer
.data
= (unsigned long) cdev
;
114 cdev
->private->timer
.expires
= jiffies
+ expires
;
115 add_timer(&cdev
->private->timer
);
118 /* Kill any pending timers after machine check. */
120 device_kill_pending_timer(struct subchannel
*sch
)
122 struct ccw_device
*cdev
;
124 if (!sch
->dev
.driver_data
)
126 cdev
= sch
->dev
.driver_data
;
127 ccw_device_set_timeout(cdev
, 0);
131 * Cancel running i/o. This is called repeatedly since halt/clear are
132 * asynchronous operations. We do one try with cio_cancel, two tries
133 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
134 * Returns 0 if device now idle, -ENODEV for device not operational and
135 * -EBUSY if an interrupt is expected (either from halt/clear or from a
139 ccw_device_cancel_halt_clear(struct ccw_device
*cdev
)
141 struct subchannel
*sch
;
144 sch
= to_subchannel(cdev
->dev
.parent
);
145 ret
= stsch(sch
->schid
, &sch
->schib
);
146 if (ret
|| !sch
->schib
.pmcw
.dnv
)
148 if (!sch
->schib
.pmcw
.ena
)
149 /* Not operational -> done. */
151 /* Stage 1: cancel io. */
152 if (!(sch
->schib
.scsw
.actl
& SCSW_ACTL_HALT_PEND
) &&
153 !(sch
->schib
.scsw
.actl
& SCSW_ACTL_CLEAR_PEND
)) {
154 ret
= cio_cancel(sch
);
157 /* cancel io unsuccessful. From now on it is asynchronous. */
158 cdev
->private->iretry
= 3; /* 3 halt retries. */
160 if (!(sch
->schib
.scsw
.actl
& SCSW_ACTL_CLEAR_PEND
)) {
161 /* Stage 2: halt io. */
162 if (cdev
->private->iretry
) {
163 cdev
->private->iretry
--;
166 return (ret
== 0) ? -EBUSY
: ret
;
168 /* halt io unsuccessful. */
169 cdev
->private->iretry
= 255; /* 255 clear retries. */
171 /* Stage 3: clear io. */
172 if (cdev
->private->iretry
) {
173 cdev
->private->iretry
--;
174 ret
= cio_clear (sch
);
175 return (ret
== 0) ? -EBUSY
: ret
;
177 panic("Can't stop i/o on subchannel.\n");
181 ccw_device_handle_oper(struct ccw_device
*cdev
)
183 struct subchannel
*sch
;
185 sch
= to_subchannel(cdev
->dev
.parent
);
186 cdev
->private->flags
.recog_done
= 1;
188 * Check if cu type and device type still match. If
189 * not, it is certainly another device and we have to
190 * de- and re-register.
192 if (cdev
->id
.cu_type
!= cdev
->private->senseid
.cu_type
||
193 cdev
->id
.cu_model
!= cdev
->private->senseid
.cu_model
||
194 cdev
->id
.dev_type
!= cdev
->private->senseid
.dev_type
||
195 cdev
->id
.dev_model
!= cdev
->private->senseid
.dev_model
) {
196 PREPARE_WORK(&cdev
->private->kick_work
,
197 ccw_device_do_unreg_rereg
);
198 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
201 cdev
->private->flags
.donotify
= 1;
206 * The machine won't give us any notification by machine check if a chpid has
207 * been varied online on the SE so we have to find out by magic (i. e. driving
208 * the channel subsystem to device selection and updating our path masks).
211 __recover_lost_chpids(struct subchannel
*sch
, int old_lpm
)
217 for (i
= 0; i
<8; i
++) {
219 if (!(sch
->lpm
& mask
))
223 chpid
.id
= sch
->schib
.pmcw
.chpid
[i
];
224 chpid_is_actually_online(chpid
);
229 * Stop device recognition.
232 ccw_device_recog_done(struct ccw_device
*cdev
, int state
)
234 struct subchannel
*sch
;
235 int notify
, old_lpm
, same_dev
;
237 sch
= to_subchannel(cdev
->dev
.parent
);
239 ccw_device_set_timeout(cdev
, 0);
240 cio_disable_subchannel(sch
);
242 * Now that we tried recognition, we have performed device selection
243 * through ssch() and the path information is up to date.
246 stsch(sch
->schid
, &sch
->schib
);
247 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
248 /* Check since device may again have become not operational. */
249 if (!sch
->schib
.pmcw
.dnv
)
250 state
= DEV_STATE_NOT_OPER
;
251 if (cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
)
252 /* Force reprobe on all chpids. */
254 if (sch
->lpm
!= old_lpm
)
255 __recover_lost_chpids(sch
, old_lpm
);
256 if (cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
) {
257 if (state
== DEV_STATE_NOT_OPER
) {
258 cdev
->private->flags
.recog_done
= 1;
259 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
262 /* Boxed devices don't need extra treatment. */
265 same_dev
= 0; /* Keep the compiler quiet... */
267 case DEV_STATE_NOT_OPER
:
268 CIO_DEBUG(KERN_WARNING
, 2,
269 "SenseID : unknown device %04x on subchannel "
270 "0.%x.%04x\n", cdev
->private->dev_id
.devno
,
271 sch
->schid
.ssid
, sch
->schid
.sch_no
);
273 case DEV_STATE_OFFLINE
:
274 if (cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
) {
275 same_dev
= ccw_device_handle_oper(cdev
);
278 /* fill out sense information */
279 memset(&cdev
->id
, 0, sizeof(cdev
->id
));
280 cdev
->id
.cu_type
= cdev
->private->senseid
.cu_type
;
281 cdev
->id
.cu_model
= cdev
->private->senseid
.cu_model
;
282 cdev
->id
.dev_type
= cdev
->private->senseid
.dev_type
;
283 cdev
->id
.dev_model
= cdev
->private->senseid
.dev_model
;
285 cdev
->private->state
= DEV_STATE_OFFLINE
;
287 /* Get device online again. */
288 ccw_device_online(cdev
);
289 wake_up(&cdev
->private->wait_q
);
293 /* Issue device info message. */
294 CIO_DEBUG(KERN_INFO
, 2, "SenseID : device 0.%x.%04x reports: "
295 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
297 cdev
->private->dev_id
.ssid
,
298 cdev
->private->dev_id
.devno
,
299 cdev
->id
.cu_type
, cdev
->id
.cu_model
,
300 cdev
->id
.dev_type
, cdev
->id
.dev_model
);
302 case DEV_STATE_BOXED
:
303 CIO_DEBUG(KERN_WARNING
, 2,
304 "SenseID : boxed device %04x on subchannel "
305 "0.%x.%04x\n", cdev
->private->dev_id
.devno
,
306 sch
->schid
.ssid
, sch
->schid
.sch_no
);
309 cdev
->private->state
= state
;
310 io_subchannel_recog_done(cdev
);
311 if (state
!= DEV_STATE_NOT_OPER
)
312 wake_up(&cdev
->private->wait_q
);
316 * Function called from device_id.c after sense id has completed.
319 ccw_device_sense_id_done(struct ccw_device
*cdev
, int err
)
323 ccw_device_recog_done(cdev
, DEV_STATE_OFFLINE
);
325 case -ETIME
: /* Sense id stopped by timeout. */
326 ccw_device_recog_done(cdev
, DEV_STATE_BOXED
);
329 ccw_device_recog_done(cdev
, DEV_STATE_NOT_OPER
);
335 ccw_device_oper_notify(struct work_struct
*work
)
337 struct ccw_device_private
*priv
;
338 struct ccw_device
*cdev
;
339 struct subchannel
*sch
;
343 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
345 spin_lock_irqsave(cdev
->ccwlock
, flags
);
346 sch
= to_subchannel(cdev
->dev
.parent
);
347 if (sch
->driver
&& sch
->driver
->notify
) {
348 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
349 ret
= sch
->driver
->notify(&sch
->dev
, CIO_OPER
);
350 spin_lock_irqsave(cdev
->ccwlock
, flags
);
354 /* Reenable channel measurements, if needed. */
355 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
357 spin_lock_irqsave(cdev
->ccwlock
, flags
);
358 wake_up(&cdev
->private->wait_q
);
360 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
362 /* Driver doesn't want device back. */
363 ccw_device_do_unreg_rereg(work
);
367 * Finished with online/offline processing.
370 ccw_device_done(struct ccw_device
*cdev
, int state
)
372 struct subchannel
*sch
;
374 sch
= to_subchannel(cdev
->dev
.parent
);
376 ccw_device_set_timeout(cdev
, 0);
378 if (state
!= DEV_STATE_ONLINE
)
379 cio_disable_subchannel(sch
);
381 /* Reset device status. */
382 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
384 cdev
->private->state
= state
;
387 if (state
== DEV_STATE_BOXED
)
388 CIO_DEBUG(KERN_WARNING
, 2,
389 "Boxed device %04x on subchannel %04x\n",
390 cdev
->private->dev_id
.devno
, sch
->schid
.sch_no
);
392 if (cdev
->private->flags
.donotify
) {
393 cdev
->private->flags
.donotify
= 0;
394 PREPARE_WORK(&cdev
->private->kick_work
, ccw_device_oper_notify
);
395 queue_work(ccw_device_notify_work
, &cdev
->private->kick_work
);
397 wake_up(&cdev
->private->wait_q
);
399 if (css_init_done
&& state
!= DEV_STATE_ONLINE
)
400 put_device (&cdev
->dev
);
403 static int cmp_pgid(struct pgid
*p1
, struct pgid
*p2
)
411 return memcmp(c1
+ 1, c2
+ 1, sizeof(struct pgid
) - 1);
414 static void __ccw_device_get_common_pgid(struct ccw_device
*cdev
)
420 for (i
= 0; i
< 8; i
++) {
421 if (cdev
->private->pgid
[i
].inf
.ps
.state1
== SNID_STATE1_RESET
)
424 if (cdev
->private->pgid
[last
].inf
.ps
.state1
==
426 /* First non-zero PGID */
430 if (cmp_pgid(&cdev
->private->pgid
[i
],
431 &cdev
->private->pgid
[last
]) == 0)
432 /* Non-conflicting PGIDs */
435 /* PGID mismatch, can't pathgroup. */
436 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
437 "0.%x.%04x, can't pathgroup\n",
438 cdev
->private->dev_id
.ssid
,
439 cdev
->private->dev_id
.devno
);
440 cdev
->private->options
.pgroup
= 0;
443 if (cdev
->private->pgid
[last
].inf
.ps
.state1
==
445 /* No previous pgid found */
446 memcpy(&cdev
->private->pgid
[0], &css
[0]->global_pgid
,
447 sizeof(struct pgid
));
449 /* Use existing pgid */
450 memcpy(&cdev
->private->pgid
[0], &cdev
->private->pgid
[last
],
451 sizeof(struct pgid
));
455 * Function called from device_pgid.c after sense path ground has completed.
458 ccw_device_sense_pgid_done(struct ccw_device
*cdev
, int err
)
460 struct subchannel
*sch
;
462 sch
= to_subchannel(cdev
->dev
.parent
);
464 case -EOPNOTSUPP
: /* path grouping not supported, use nop instead. */
465 cdev
->private->options
.pgroup
= 0;
467 case 0: /* success */
468 case -EACCES
: /* partial success, some paths not operational */
469 /* Check if all pgids are equal or 0. */
470 __ccw_device_get_common_pgid(cdev
);
472 case -ETIME
: /* Sense path group id stopped by timeout. */
473 case -EUSERS
: /* device is reserved for someone else. */
474 ccw_device_done(cdev
, DEV_STATE_BOXED
);
477 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
480 /* Start Path Group verification. */
481 cdev
->private->state
= DEV_STATE_VERIFY
;
482 cdev
->private->flags
.doverify
= 0;
483 ccw_device_verify_start(cdev
);
487 * Start device recognition.
490 ccw_device_recognition(struct ccw_device
*cdev
)
492 struct subchannel
*sch
;
495 if ((cdev
->private->state
!= DEV_STATE_NOT_OPER
) &&
496 (cdev
->private->state
!= DEV_STATE_BOXED
))
498 sch
= to_subchannel(cdev
->dev
.parent
);
499 ret
= cio_enable_subchannel(sch
, sch
->schib
.pmcw
.isc
);
501 /* Couldn't enable the subchannel for i/o. Sick device. */
504 /* After 60s the device recognition is considered to have failed. */
505 ccw_device_set_timeout(cdev
, 60*HZ
);
508 * We used to start here with a sense pgid to find out whether a device
509 * is locked by someone else. Unfortunately, the sense pgid command
510 * code has other meanings on devices predating the path grouping
511 * algorithm, so we start with sense id and box the device after an
512 * timeout (or if sense pgid during path verification detects the device
513 * is locked, as may happen on newer devices).
515 cdev
->private->flags
.recog_done
= 0;
516 cdev
->private->state
= DEV_STATE_SENSE_ID
;
517 ccw_device_sense_id_start(cdev
);
522 * Handle timeout in device recognition.
525 ccw_device_recog_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
529 ret
= ccw_device_cancel_halt_clear(cdev
);
532 ccw_device_recog_done(cdev
, DEV_STATE_BOXED
);
535 ccw_device_recog_done(cdev
, DEV_STATE_NOT_OPER
);
538 ccw_device_set_timeout(cdev
, 3*HZ
);
544 ccw_device_nopath_notify(struct work_struct
*work
)
546 struct ccw_device_private
*priv
;
547 struct ccw_device
*cdev
;
548 struct subchannel
*sch
;
552 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
554 spin_lock_irqsave(cdev
->ccwlock
, flags
);
555 sch
= to_subchannel(cdev
->dev
.parent
);
559 if (sch
->driver
&& sch
->driver
->notify
) {
560 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
561 ret
= sch
->driver
->notify(&sch
->dev
, CIO_NO_PATH
);
562 spin_lock_irqsave(cdev
->ccwlock
, flags
);
566 if (get_device(&sch
->dev
)) {
567 /* Driver doesn't want to keep device. */
568 cio_disable_subchannel(sch
);
569 if (get_device(&cdev
->dev
)) {
570 PREPARE_WORK(&cdev
->private->kick_work
,
571 ccw_device_call_sch_unregister
);
572 queue_work(ccw_device_work
,
573 &cdev
->private->kick_work
);
575 put_device(&sch
->dev
);
578 cio_disable_subchannel(sch
);
579 ccw_device_set_timeout(cdev
, 0);
580 cdev
->private->flags
.fake_irb
= 0;
581 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
582 wake_up(&cdev
->private->wait_q
);
585 spin_unlock_irqrestore(cdev
->ccwlock
, flags
);
589 ccw_device_verify_done(struct ccw_device
*cdev
, int err
)
591 struct subchannel
*sch
;
593 sch
= to_subchannel(cdev
->dev
.parent
);
594 /* Update schib - pom may have changed. */
595 stsch(sch
->schid
, &sch
->schib
);
596 /* Update lpm with verified path mask. */
598 /* Repeat path verification? */
599 if (cdev
->private->flags
.doverify
) {
600 cdev
->private->flags
.doverify
= 0;
601 ccw_device_verify_start(cdev
);
605 case -EOPNOTSUPP
: /* path grouping not supported, just set online. */
606 cdev
->private->options
.pgroup
= 0;
608 ccw_device_done(cdev
, DEV_STATE_ONLINE
);
609 /* Deliver fake irb to device driver, if needed. */
610 if (cdev
->private->flags
.fake_irb
) {
611 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
612 cdev
->private->irb
.scsw
.cc
= 1;
613 cdev
->private->irb
.scsw
.fctl
= SCSW_FCTL_START_FUNC
;
614 cdev
->private->irb
.scsw
.actl
= SCSW_ACTL_START_PEND
;
615 cdev
->private->irb
.scsw
.stctl
= SCSW_STCTL_STATUS_PEND
;
616 cdev
->private->flags
.fake_irb
= 0;
618 cdev
->handler(cdev
, cdev
->private->intparm
,
619 &cdev
->private->irb
);
620 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
624 /* Reset oper notify indication after verify error. */
625 cdev
->private->flags
.donotify
= 0;
626 ccw_device_done(cdev
, DEV_STATE_BOXED
);
629 /* Reset oper notify indication after verify error. */
630 cdev
->private->flags
.donotify
= 0;
632 PREPARE_WORK(&cdev
->private->kick_work
,
633 ccw_device_nopath_notify
);
634 queue_work(ccw_device_notify_work
,
635 &cdev
->private->kick_work
);
637 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
646 ccw_device_online(struct ccw_device
*cdev
)
648 struct subchannel
*sch
;
651 if ((cdev
->private->state
!= DEV_STATE_OFFLINE
) &&
652 (cdev
->private->state
!= DEV_STATE_BOXED
))
654 sch
= to_subchannel(cdev
->dev
.parent
);
655 if (css_init_done
&& !get_device(&cdev
->dev
))
657 ret
= cio_enable_subchannel(sch
, sch
->schib
.pmcw
.isc
);
659 /* Couldn't enable the subchannel for i/o. Sick device. */
661 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
664 /* Do we want to do path grouping? */
665 if (!cdev
->private->options
.pgroup
) {
666 /* Start initial path verification. */
667 cdev
->private->state
= DEV_STATE_VERIFY
;
668 cdev
->private->flags
.doverify
= 0;
669 ccw_device_verify_start(cdev
);
672 /* Do a SensePGID first. */
673 cdev
->private->state
= DEV_STATE_SENSE_PGID
;
674 ccw_device_sense_pgid_start(cdev
);
679 ccw_device_disband_done(struct ccw_device
*cdev
, int err
)
683 ccw_device_done(cdev
, DEV_STATE_OFFLINE
);
686 ccw_device_done(cdev
, DEV_STATE_BOXED
);
689 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
698 ccw_device_offline(struct ccw_device
*cdev
)
700 struct subchannel
*sch
;
702 if (ccw_device_is_orphan(cdev
)) {
703 ccw_device_done(cdev
, DEV_STATE_OFFLINE
);
706 sch
= to_subchannel(cdev
->dev
.parent
);
707 if (stsch(sch
->schid
, &sch
->schib
) || !sch
->schib
.pmcw
.dnv
)
709 if (cdev
->private->state
!= DEV_STATE_ONLINE
) {
710 if (sch
->schib
.scsw
.actl
!= 0)
714 if (sch
->schib
.scsw
.actl
!= 0)
716 /* Are we doing path grouping? */
717 if (!cdev
->private->options
.pgroup
) {
718 /* No, set state offline immediately. */
719 ccw_device_done(cdev
, DEV_STATE_OFFLINE
);
722 /* Start Set Path Group commands. */
723 cdev
->private->state
= DEV_STATE_DISBAND_PGID
;
724 ccw_device_disband_start(cdev
);
729 * Handle timeout in device online/offline process.
732 ccw_device_onoff_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
736 ret
= ccw_device_cancel_halt_clear(cdev
);
739 ccw_device_done(cdev
, DEV_STATE_BOXED
);
742 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
745 ccw_device_set_timeout(cdev
, 3*HZ
);
750 * Handle not oper event in device recognition.
753 ccw_device_recog_notoper(struct ccw_device
*cdev
, enum dev_event dev_event
)
755 ccw_device_recog_done(cdev
, DEV_STATE_NOT_OPER
);
759 * Handle not operational event while offline.
762 ccw_device_offline_notoper(struct ccw_device
*cdev
, enum dev_event dev_event
)
764 struct subchannel
*sch
;
766 cdev
->private->state
= DEV_STATE_NOT_OPER
;
767 sch
= to_subchannel(cdev
->dev
.parent
);
768 if (get_device(&cdev
->dev
)) {
769 PREPARE_WORK(&cdev
->private->kick_work
,
770 ccw_device_call_sch_unregister
);
771 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
773 wake_up(&cdev
->private->wait_q
);
777 * Handle not operational event while online.
780 ccw_device_online_notoper(struct ccw_device
*cdev
, enum dev_event dev_event
)
782 struct subchannel
*sch
;
785 sch
= to_subchannel(cdev
->dev
.parent
);
786 if (sch
->driver
->notify
) {
787 spin_unlock_irq(cdev
->ccwlock
);
788 ret
= sch
->driver
->notify(&sch
->dev
,
789 sch
->lpm
? CIO_GONE
: CIO_NO_PATH
);
790 spin_lock_irq(cdev
->ccwlock
);
794 ccw_device_set_timeout(cdev
, 0);
795 cdev
->private->flags
.fake_irb
= 0;
796 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
797 wake_up(&cdev
->private->wait_q
);
800 cdev
->private->state
= DEV_STATE_NOT_OPER
;
801 cio_disable_subchannel(sch
);
802 if (sch
->schib
.scsw
.actl
!= 0) {
803 // FIXME: not-oper indication to device driver ?
804 ccw_device_call_handler(cdev
);
806 if (get_device(&cdev
->dev
)) {
807 PREPARE_WORK(&cdev
->private->kick_work
,
808 ccw_device_call_sch_unregister
);
809 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
811 wake_up(&cdev
->private->wait_q
);
815 * Handle path verification event.
818 ccw_device_online_verify(struct ccw_device
*cdev
, enum dev_event dev_event
)
820 struct subchannel
*sch
;
822 if (cdev
->private->state
== DEV_STATE_W4SENSE
) {
823 cdev
->private->flags
.doverify
= 1;
826 sch
= to_subchannel(cdev
->dev
.parent
);
828 * Since we might not just be coming from an interrupt from the
829 * subchannel we have to update the schib.
831 stsch(sch
->schid
, &sch
->schib
);
833 if (sch
->schib
.scsw
.actl
!= 0 ||
834 (sch
->schib
.scsw
.stctl
& SCSW_STCTL_STATUS_PEND
) ||
835 (cdev
->private->irb
.scsw
.stctl
& SCSW_STCTL_STATUS_PEND
)) {
837 * No final status yet or final status not yet delivered
838 * to the device driver. Can't do path verfication now,
839 * delay until final status was delivered.
841 cdev
->private->flags
.doverify
= 1;
844 /* Device is idle, we can do the path verification. */
845 cdev
->private->state
= DEV_STATE_VERIFY
;
846 cdev
->private->flags
.doverify
= 0;
847 ccw_device_verify_start(cdev
);
851 * Got an interrupt for a normal io (state online).
854 ccw_device_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
858 irb
= (struct irb
*) __LC_IRB
;
859 /* Check for unsolicited interrupt. */
860 if ((irb
->scsw
.stctl
==
861 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
))
862 && (!irb
->scsw
.cc
)) {
863 if ((irb
->scsw
.dstat
& DEV_STAT_UNIT_CHECK
) &&
864 !irb
->esw
.esw0
.erw
.cons
) {
865 /* Unit check but no sense data. Need basic sense. */
866 if (ccw_device_do_sense(cdev
, irb
) != 0)
867 goto call_handler_unsol
;
868 memcpy(&cdev
->private->irb
, irb
, sizeof(struct irb
));
869 cdev
->private->state
= DEV_STATE_W4SENSE
;
870 cdev
->private->intparm
= 0;
875 cdev
->handler (cdev
, 0, irb
);
876 if (cdev
->private->flags
.doverify
)
877 ccw_device_online_verify(cdev
, 0);
880 /* Accumulate status and find out if a basic sense is needed. */
881 ccw_device_accumulate_irb(cdev
, irb
);
882 if (cdev
->private->flags
.dosense
) {
883 if (ccw_device_do_sense(cdev
, irb
) == 0) {
884 cdev
->private->state
= DEV_STATE_W4SENSE
;
888 /* Call the handler. */
889 if (ccw_device_call_handler(cdev
) && cdev
->private->flags
.doverify
)
890 /* Start delayed path verification. */
891 ccw_device_online_verify(cdev
, 0);
895 * Got an timeout in online state.
898 ccw_device_online_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
902 ccw_device_set_timeout(cdev
, 0);
903 ret
= ccw_device_cancel_halt_clear(cdev
);
905 ccw_device_set_timeout(cdev
, 3*HZ
);
906 cdev
->private->state
= DEV_STATE_TIMEOUT_KILL
;
909 if (ret
== -ENODEV
) {
910 struct subchannel
*sch
;
912 sch
= to_subchannel(cdev
->dev
.parent
);
914 PREPARE_WORK(&cdev
->private->kick_work
,
915 ccw_device_nopath_notify
);
916 queue_work(ccw_device_notify_work
,
917 &cdev
->private->kick_work
);
919 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
920 } else if (cdev
->handler
)
921 cdev
->handler(cdev
, cdev
->private->intparm
,
922 ERR_PTR(-ETIMEDOUT
));
926 * Got an interrupt for a basic sense.
929 ccw_device_w4sense(struct ccw_device
*cdev
, enum dev_event dev_event
)
933 irb
= (struct irb
*) __LC_IRB
;
934 /* Check for unsolicited interrupt. */
935 if (irb
->scsw
.stctl
==
936 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
)) {
937 if (irb
->scsw
.cc
== 1)
938 /* Basic sense hasn't started. Try again. */
939 ccw_device_do_sense(cdev
, irb
);
941 printk(KERN_INFO
"Huh? %s(%s): unsolicited "
943 __FUNCTION__
, cdev
->dev
.bus_id
);
945 cdev
->handler (cdev
, 0, irb
);
950 * Check if a halt or clear has been issued in the meanwhile. If yes,
951 * only deliver the halt/clear interrupt to the device driver as if it
952 * had killed the original request.
954 if (irb
->scsw
.fctl
& (SCSW_FCTL_CLEAR_FUNC
| SCSW_FCTL_HALT_FUNC
)) {
955 /* Retry Basic Sense if requested. */
956 if (cdev
->private->flags
.intretry
) {
957 cdev
->private->flags
.intretry
= 0;
958 ccw_device_do_sense(cdev
, irb
);
961 cdev
->private->flags
.dosense
= 0;
962 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
963 ccw_device_accumulate_irb(cdev
, irb
);
966 /* Add basic sense info to irb. */
967 ccw_device_accumulate_basic_sense(cdev
, irb
);
968 if (cdev
->private->flags
.dosense
) {
969 /* Another basic sense is needed. */
970 ccw_device_do_sense(cdev
, irb
);
974 cdev
->private->state
= DEV_STATE_ONLINE
;
975 /* Call the handler. */
976 if (ccw_device_call_handler(cdev
) && cdev
->private->flags
.doverify
)
977 /* Start delayed path verification. */
978 ccw_device_online_verify(cdev
, 0);
982 ccw_device_clear_verify(struct ccw_device
*cdev
, enum dev_event dev_event
)
986 irb
= (struct irb
*) __LC_IRB
;
987 /* Accumulate status. We don't do basic sense. */
988 ccw_device_accumulate_irb(cdev
, irb
);
989 /* Remember to clear irb to avoid residuals. */
990 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
991 /* Try to start delayed device verification. */
992 ccw_device_online_verify(cdev
, 0);
993 /* Note: Don't call handler for cio initiated clear! */
997 ccw_device_killing_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
999 struct subchannel
*sch
;
1001 sch
= to_subchannel(cdev
->dev
.parent
);
1002 ccw_device_set_timeout(cdev
, 0);
1003 /* Start delayed path verification. */
1004 ccw_device_online_verify(cdev
, 0);
1005 /* OK, i/o is dead now. Call interrupt handler. */
1007 cdev
->handler(cdev
, cdev
->private->intparm
,
1012 ccw_device_killing_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
1016 ret
= ccw_device_cancel_halt_clear(cdev
);
1017 if (ret
== -EBUSY
) {
1018 ccw_device_set_timeout(cdev
, 3*HZ
);
1021 /* Start delayed path verification. */
1022 ccw_device_online_verify(cdev
, 0);
1024 cdev
->handler(cdev
, cdev
->private->intparm
,
1028 void device_kill_io(struct subchannel
*sch
)
1031 struct ccw_device
*cdev
;
1033 cdev
= sch
->dev
.driver_data
;
1034 ret
= ccw_device_cancel_halt_clear(cdev
);
1035 if (ret
== -EBUSY
) {
1036 ccw_device_set_timeout(cdev
, 3*HZ
);
1037 cdev
->private->state
= DEV_STATE_TIMEOUT_KILL
;
1040 /* Start delayed path verification. */
1041 ccw_device_online_verify(cdev
, 0);
1043 cdev
->handler(cdev
, cdev
->private->intparm
,
1048 ccw_device_delay_verify(struct ccw_device
*cdev
, enum dev_event dev_event
)
1050 /* Start verification after current task finished. */
1051 cdev
->private->flags
.doverify
= 1;
1055 ccw_device_stlck_done(struct ccw_device
*cdev
, enum dev_event dev_event
)
1059 switch (dev_event
) {
1060 case DEV_EVENT_INTERRUPT
:
1061 irb
= (struct irb
*) __LC_IRB
;
1062 /* Check for unsolicited interrupt. */
1063 if ((irb
->scsw
.stctl
==
1064 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
)) &&
1066 /* FIXME: we should restart stlck here, but this
1067 * is extremely unlikely ... */
1070 ccw_device_accumulate_irb(cdev
, irb
);
1071 /* We don't care about basic sense etc. */
1073 default: /* timeout */
1077 wake_up(&cdev
->private->wait_q
);
1081 ccw_device_start_id(struct ccw_device
*cdev
, enum dev_event dev_event
)
1083 struct subchannel
*sch
;
1085 sch
= to_subchannel(cdev
->dev
.parent
);
1086 if (cio_enable_subchannel(sch
, sch
->schib
.pmcw
.isc
) != 0)
1087 /* Couldn't enable the subchannel for i/o. Sick device. */
1090 /* After 60s the device recognition is considered to have failed. */
1091 ccw_device_set_timeout(cdev
, 60*HZ
);
1093 cdev
->private->state
= DEV_STATE_DISCONNECTED_SENSE_ID
;
1094 ccw_device_sense_id_start(cdev
);
1098 device_trigger_reprobe(struct subchannel
*sch
)
1100 struct ccw_device
*cdev
;
1102 if (!sch
->dev
.driver_data
)
1104 cdev
= sch
->dev
.driver_data
;
1105 if (cdev
->private->state
!= DEV_STATE_DISCONNECTED
)
1108 /* Update some values. */
1109 if (stsch(sch
->schid
, &sch
->schib
))
1111 if (!sch
->schib
.pmcw
.dnv
)
1114 * The pim, pam, pom values may not be accurate, but they are the best
1115 * we have before performing device selection :/
1117 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1118 /* Re-set some bits in the pmcw that were lost. */
1119 sch
->schib
.pmcw
.isc
= 3;
1120 sch
->schib
.pmcw
.csense
= 1;
1121 sch
->schib
.pmcw
.ena
= 0;
1122 if ((sch
->lpm
& (sch
->lpm
- 1)) != 0)
1123 sch
->schib
.pmcw
.mp
= 1;
1124 sch
->schib
.pmcw
.intparm
= (__u32
)(unsigned long)sch
;
1125 /* We should also udate ssd info, but this has to wait. */
1126 /* Check if this is another device which appeared on the same sch. */
1127 if (sch
->schib
.pmcw
.dev
!= cdev
->private->dev_id
.devno
) {
1128 PREPARE_WORK(&cdev
->private->kick_work
,
1129 ccw_device_move_to_orphanage
);
1130 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
1132 ccw_device_start_id(cdev
, 0);
1136 ccw_device_offline_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
1138 struct subchannel
*sch
;
1140 sch
= to_subchannel(cdev
->dev
.parent
);
1142 * An interrupt in state offline means a previous disable was not
1143 * successful. Try again.
1145 cio_disable_subchannel(sch
);
1149 ccw_device_change_cmfstate(struct ccw_device
*cdev
, enum dev_event dev_event
)
1151 retry_set_schib(cdev
);
1152 cdev
->private->state
= DEV_STATE_ONLINE
;
1153 dev_fsm_event(cdev
, dev_event
);
1156 static void ccw_device_update_cmfblock(struct ccw_device
*cdev
,
1157 enum dev_event dev_event
)
1159 cmf_retry_copy_block(cdev
);
1160 cdev
->private->state
= DEV_STATE_ONLINE
;
1161 dev_fsm_event(cdev
, dev_event
);
1165 ccw_device_quiesce_done(struct ccw_device
*cdev
, enum dev_event dev_event
)
1167 ccw_device_set_timeout(cdev
, 0);
1168 if (dev_event
== DEV_EVENT_NOTOPER
)
1169 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1171 cdev
->private->state
= DEV_STATE_OFFLINE
;
1172 wake_up(&cdev
->private->wait_q
);
1176 ccw_device_quiesce_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
1180 ret
= ccw_device_cancel_halt_clear(cdev
);
1183 cdev
->private->state
= DEV_STATE_OFFLINE
;
1184 wake_up(&cdev
->private->wait_q
);
1187 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1188 wake_up(&cdev
->private->wait_q
);
1191 ccw_device_set_timeout(cdev
, HZ
/10);
1196 * No operation action. This is used e.g. to ignore a timeout event in
1200 ccw_device_nop(struct ccw_device
*cdev
, enum dev_event dev_event
)
1205 * Bug operation action.
1208 ccw_device_bug(struct ccw_device
*cdev
, enum dev_event dev_event
)
1210 printk(KERN_EMERG
"dev_jumptable[%i][%i] == NULL\n",
1211 cdev
->private->state
, dev_event
);
1216 * device statemachine
1218 fsm_func_t
*dev_jumptable
[NR_DEV_STATES
][NR_DEV_EVENTS
] = {
1219 [DEV_STATE_NOT_OPER
] = {
1220 [DEV_EVENT_NOTOPER
] = ccw_device_nop
,
1221 [DEV_EVENT_INTERRUPT
] = ccw_device_bug
,
1222 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1223 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1225 [DEV_STATE_SENSE_PGID
] = {
1226 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1227 [DEV_EVENT_INTERRUPT
] = ccw_device_sense_pgid_irq
,
1228 [DEV_EVENT_TIMEOUT
] = ccw_device_onoff_timeout
,
1229 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1231 [DEV_STATE_SENSE_ID
] = {
1232 [DEV_EVENT_NOTOPER
] = ccw_device_recog_notoper
,
1233 [DEV_EVENT_INTERRUPT
] = ccw_device_sense_id_irq
,
1234 [DEV_EVENT_TIMEOUT
] = ccw_device_recog_timeout
,
1235 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1237 [DEV_STATE_OFFLINE
] = {
1238 [DEV_EVENT_NOTOPER
] = ccw_device_offline_notoper
,
1239 [DEV_EVENT_INTERRUPT
] = ccw_device_offline_irq
,
1240 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1241 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1243 [DEV_STATE_VERIFY
] = {
1244 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1245 [DEV_EVENT_INTERRUPT
] = ccw_device_verify_irq
,
1246 [DEV_EVENT_TIMEOUT
] = ccw_device_onoff_timeout
,
1247 [DEV_EVENT_VERIFY
] = ccw_device_delay_verify
,
1249 [DEV_STATE_ONLINE
] = {
1250 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1251 [DEV_EVENT_INTERRUPT
] = ccw_device_irq
,
1252 [DEV_EVENT_TIMEOUT
] = ccw_device_online_timeout
,
1253 [DEV_EVENT_VERIFY
] = ccw_device_online_verify
,
1255 [DEV_STATE_W4SENSE
] = {
1256 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1257 [DEV_EVENT_INTERRUPT
] = ccw_device_w4sense
,
1258 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1259 [DEV_EVENT_VERIFY
] = ccw_device_online_verify
,
1261 [DEV_STATE_DISBAND_PGID
] = {
1262 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1263 [DEV_EVENT_INTERRUPT
] = ccw_device_disband_irq
,
1264 [DEV_EVENT_TIMEOUT
] = ccw_device_onoff_timeout
,
1265 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1267 [DEV_STATE_BOXED
] = {
1268 [DEV_EVENT_NOTOPER
] = ccw_device_offline_notoper
,
1269 [DEV_EVENT_INTERRUPT
] = ccw_device_stlck_done
,
1270 [DEV_EVENT_TIMEOUT
] = ccw_device_stlck_done
,
1271 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1273 /* states to wait for i/o completion before doing something */
1274 [DEV_STATE_CLEAR_VERIFY
] = {
1275 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1276 [DEV_EVENT_INTERRUPT
] = ccw_device_clear_verify
,
1277 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1278 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1280 [DEV_STATE_TIMEOUT_KILL
] = {
1281 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1282 [DEV_EVENT_INTERRUPT
] = ccw_device_killing_irq
,
1283 [DEV_EVENT_TIMEOUT
] = ccw_device_killing_timeout
,
1284 [DEV_EVENT_VERIFY
] = ccw_device_nop
, //FIXME
1286 [DEV_STATE_QUIESCE
] = {
1287 [DEV_EVENT_NOTOPER
] = ccw_device_quiesce_done
,
1288 [DEV_EVENT_INTERRUPT
] = ccw_device_quiesce_done
,
1289 [DEV_EVENT_TIMEOUT
] = ccw_device_quiesce_timeout
,
1290 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1292 /* special states for devices gone not operational */
1293 [DEV_STATE_DISCONNECTED
] = {
1294 [DEV_EVENT_NOTOPER
] = ccw_device_nop
,
1295 [DEV_EVENT_INTERRUPT
] = ccw_device_start_id
,
1296 [DEV_EVENT_TIMEOUT
] = ccw_device_bug
,
1297 [DEV_EVENT_VERIFY
] = ccw_device_start_id
,
1299 [DEV_STATE_DISCONNECTED_SENSE_ID
] = {
1300 [DEV_EVENT_NOTOPER
] = ccw_device_recog_notoper
,
1301 [DEV_EVENT_INTERRUPT
] = ccw_device_sense_id_irq
,
1302 [DEV_EVENT_TIMEOUT
] = ccw_device_recog_timeout
,
1303 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1305 [DEV_STATE_CMFCHANGE
] = {
1306 [DEV_EVENT_NOTOPER
] = ccw_device_change_cmfstate
,
1307 [DEV_EVENT_INTERRUPT
] = ccw_device_change_cmfstate
,
1308 [DEV_EVENT_TIMEOUT
] = ccw_device_change_cmfstate
,
1309 [DEV_EVENT_VERIFY
] = ccw_device_change_cmfstate
,
1311 [DEV_STATE_CMFUPDATE
] = {
1312 [DEV_EVENT_NOTOPER
] = ccw_device_update_cmfblock
,
1313 [DEV_EVENT_INTERRUPT
] = ccw_device_update_cmfblock
,
1314 [DEV_EVENT_TIMEOUT
] = ccw_device_update_cmfblock
,
1315 [DEV_EVENT_VERIFY
] = ccw_device_update_cmfblock
,
1320 * io_subchannel_irq is called for "real" interrupts or for status
1321 * pending conditions on msch.
1324 io_subchannel_irq (struct device
*pdev
)
1326 struct ccw_device
*cdev
;
1328 cdev
= to_subchannel(pdev
)->dev
.driver_data
;
1330 CIO_TRACE_EVENT (3, "IRQ");
1331 CIO_TRACE_EVENT (3, pdev
->bus_id
);
1333 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1336 EXPORT_SYMBOL_GPL(ccw_device_set_timeout
);