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"
27 device_is_online(struct subchannel
*sch
)
29 struct ccw_device
*cdev
;
31 if (!sch
->dev
.driver_data
)
33 cdev
= sch
->dev
.driver_data
;
34 return (cdev
->private->state
== DEV_STATE_ONLINE
);
38 device_is_disconnected(struct subchannel
*sch
)
40 struct ccw_device
*cdev
;
42 if (!sch
->dev
.driver_data
)
44 cdev
= sch
->dev
.driver_data
;
45 return (cdev
->private->state
== DEV_STATE_DISCONNECTED
||
46 cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
);
50 device_set_disconnected(struct subchannel
*sch
)
52 struct ccw_device
*cdev
;
54 if (!sch
->dev
.driver_data
)
56 cdev
= sch
->dev
.driver_data
;
57 ccw_device_set_timeout(cdev
, 0);
58 cdev
->private->flags
.fake_irb
= 0;
59 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
62 void device_set_intretry(struct subchannel
*sch
)
64 struct ccw_device
*cdev
;
66 cdev
= sch
->dev
.driver_data
;
69 cdev
->private->flags
.intretry
= 1;
72 int device_trigger_verify(struct subchannel
*sch
)
74 struct ccw_device
*cdev
;
76 cdev
= sch
->dev
.driver_data
;
77 if (!cdev
|| !cdev
->online
)
79 dev_fsm_event(cdev
, DEV_EVENT_VERIFY
);
84 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
87 ccw_device_timeout(unsigned long data
)
89 struct ccw_device
*cdev
;
91 cdev
= (struct ccw_device
*) data
;
92 spin_lock_irq(cdev
->ccwlock
);
93 dev_fsm_event(cdev
, DEV_EVENT_TIMEOUT
);
94 spin_unlock_irq(cdev
->ccwlock
);
101 ccw_device_set_timeout(struct ccw_device
*cdev
, int expires
)
104 del_timer(&cdev
->private->timer
);
107 if (timer_pending(&cdev
->private->timer
)) {
108 if (mod_timer(&cdev
->private->timer
, jiffies
+ expires
))
111 cdev
->private->timer
.function
= ccw_device_timeout
;
112 cdev
->private->timer
.data
= (unsigned long) cdev
;
113 cdev
->private->timer
.expires
= jiffies
+ expires
;
114 add_timer(&cdev
->private->timer
);
117 /* Kill any pending timers after machine check. */
119 device_kill_pending_timer(struct subchannel
*sch
)
121 struct ccw_device
*cdev
;
123 if (!sch
->dev
.driver_data
)
125 cdev
= sch
->dev
.driver_data
;
126 ccw_device_set_timeout(cdev
, 0);
130 * Cancel running i/o. This is called repeatedly since halt/clear are
131 * asynchronous operations. We do one try with cio_cancel, two tries
132 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
133 * Returns 0 if device now idle, -ENODEV for device not operational and
134 * -EBUSY if an interrupt is expected (either from halt/clear or from a
138 ccw_device_cancel_halt_clear(struct ccw_device
*cdev
)
140 struct subchannel
*sch
;
143 sch
= to_subchannel(cdev
->dev
.parent
);
144 ret
= stsch(sch
->schid
, &sch
->schib
);
145 if (ret
|| !sch
->schib
.pmcw
.dnv
)
147 if (!sch
->schib
.pmcw
.ena
|| sch
->schib
.scsw
.actl
== 0)
148 /* Not operational or no activity -> done. */
150 /* Stage 1: cancel io. */
151 if (!(sch
->schib
.scsw
.actl
& SCSW_ACTL_HALT_PEND
) &&
152 !(sch
->schib
.scsw
.actl
& SCSW_ACTL_CLEAR_PEND
)) {
153 ret
= cio_cancel(sch
);
156 /* cancel io unsuccessful. From now on it is asynchronous. */
157 cdev
->private->iretry
= 3; /* 3 halt retries. */
159 if (!(sch
->schib
.scsw
.actl
& SCSW_ACTL_CLEAR_PEND
)) {
160 /* Stage 2: halt io. */
161 if (cdev
->private->iretry
) {
162 cdev
->private->iretry
--;
165 return (ret
== 0) ? -EBUSY
: ret
;
167 /* halt io unsuccessful. */
168 cdev
->private->iretry
= 255; /* 255 clear retries. */
170 /* Stage 3: clear io. */
171 if (cdev
->private->iretry
) {
172 cdev
->private->iretry
--;
173 ret
= cio_clear (sch
);
174 return (ret
== 0) ? -EBUSY
: ret
;
176 panic("Can't stop i/o on subchannel.\n");
180 ccw_device_handle_oper(struct ccw_device
*cdev
)
182 struct subchannel
*sch
;
184 sch
= to_subchannel(cdev
->dev
.parent
);
185 cdev
->private->flags
.recog_done
= 1;
187 * Check if cu type and device type still match. If
188 * not, it is certainly another device and we have to
189 * de- and re-register. Also check here for non-matching devno.
191 if (cdev
->id
.cu_type
!= cdev
->private->senseid
.cu_type
||
192 cdev
->id
.cu_model
!= cdev
->private->senseid
.cu_model
||
193 cdev
->id
.dev_type
!= cdev
->private->senseid
.dev_type
||
194 cdev
->id
.dev_model
!= cdev
->private->senseid
.dev_model
||
195 cdev
->private->dev_id
.devno
!= sch
->schib
.pmcw
.dev
) {
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
)
215 for (i
= 0; i
<8; i
++) {
217 if (!(sch
->lpm
& mask
))
221 chpid_is_actually_online(sch
->schib
.pmcw
.chpid
[i
]);
226 * Stop device recognition.
229 ccw_device_recog_done(struct ccw_device
*cdev
, int state
)
231 struct subchannel
*sch
;
232 int notify
, old_lpm
, same_dev
;
234 sch
= to_subchannel(cdev
->dev
.parent
);
236 ccw_device_set_timeout(cdev
, 0);
237 cio_disable_subchannel(sch
);
239 * Now that we tried recognition, we have performed device selection
240 * through ssch() and the path information is up to date.
243 stsch(sch
->schid
, &sch
->schib
);
244 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
245 /* Check since device may again have become not operational. */
246 if (!sch
->schib
.pmcw
.dnv
)
247 state
= DEV_STATE_NOT_OPER
;
248 if (cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
)
249 /* Force reprobe on all chpids. */
251 if (sch
->lpm
!= old_lpm
)
252 __recover_lost_chpids(sch
, old_lpm
);
253 if (cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
) {
254 if (state
== DEV_STATE_NOT_OPER
) {
255 cdev
->private->flags
.recog_done
= 1;
256 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
259 /* Boxed devices don't need extra treatment. */
262 same_dev
= 0; /* Keep the compiler quiet... */
264 case DEV_STATE_NOT_OPER
:
265 CIO_DEBUG(KERN_WARNING
, 2,
266 "SenseID : unknown device %04x on subchannel "
267 "0.%x.%04x\n", cdev
->private->dev_id
.devno
,
268 sch
->schid
.ssid
, sch
->schid
.sch_no
);
270 case DEV_STATE_OFFLINE
:
271 if (cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
) {
272 same_dev
= ccw_device_handle_oper(cdev
);
275 /* fill out sense information */
276 memset(&cdev
->id
, 0, sizeof(cdev
->id
));
277 cdev
->id
.cu_type
= cdev
->private->senseid
.cu_type
;
278 cdev
->id
.cu_model
= cdev
->private->senseid
.cu_model
;
279 cdev
->id
.dev_type
= cdev
->private->senseid
.dev_type
;
280 cdev
->id
.dev_model
= cdev
->private->senseid
.dev_model
;
282 cdev
->private->state
= DEV_STATE_OFFLINE
;
284 /* Get device online again. */
285 ccw_device_online(cdev
);
286 wake_up(&cdev
->private->wait_q
);
290 /* Issue device info message. */
291 CIO_DEBUG(KERN_INFO
, 2, "SenseID : device 0.%x.%04x reports: "
292 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
294 cdev
->private->dev_id
.ssid
,
295 cdev
->private->dev_id
.devno
,
296 cdev
->id
.cu_type
, cdev
->id
.cu_model
,
297 cdev
->id
.dev_type
, cdev
->id
.dev_model
);
299 case DEV_STATE_BOXED
:
300 CIO_DEBUG(KERN_WARNING
, 2,
301 "SenseID : boxed device %04x on subchannel "
302 "0.%x.%04x\n", cdev
->private->dev_id
.devno
,
303 sch
->schid
.ssid
, sch
->schid
.sch_no
);
306 cdev
->private->state
= state
;
307 io_subchannel_recog_done(cdev
);
308 if (state
!= DEV_STATE_NOT_OPER
)
309 wake_up(&cdev
->private->wait_q
);
313 * Function called from device_id.c after sense id has completed.
316 ccw_device_sense_id_done(struct ccw_device
*cdev
, int err
)
320 ccw_device_recog_done(cdev
, DEV_STATE_OFFLINE
);
322 case -ETIME
: /* Sense id stopped by timeout. */
323 ccw_device_recog_done(cdev
, DEV_STATE_BOXED
);
326 ccw_device_recog_done(cdev
, DEV_STATE_NOT_OPER
);
332 ccw_device_oper_notify(struct work_struct
*work
)
334 struct ccw_device_private
*priv
;
335 struct ccw_device
*cdev
;
336 struct subchannel
*sch
;
339 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
341 sch
= to_subchannel(cdev
->dev
.parent
);
342 ret
= (sch
->driver
&& sch
->driver
->notify
) ?
343 sch
->driver
->notify(&sch
->dev
, CIO_OPER
) : 0;
345 /* Driver doesn't want device back. */
346 ccw_device_do_unreg_rereg(work
);
348 /* Reenable channel measurements, if needed. */
350 wake_up(&cdev
->private->wait_q
);
355 * Finished with online/offline processing.
358 ccw_device_done(struct ccw_device
*cdev
, int state
)
360 struct subchannel
*sch
;
362 sch
= to_subchannel(cdev
->dev
.parent
);
364 ccw_device_set_timeout(cdev
, 0);
366 if (state
!= DEV_STATE_ONLINE
)
367 cio_disable_subchannel(sch
);
369 /* Reset device status. */
370 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
372 cdev
->private->state
= state
;
375 if (state
== DEV_STATE_BOXED
)
376 CIO_DEBUG(KERN_WARNING
, 2,
377 "Boxed device %04x on subchannel %04x\n",
378 cdev
->private->dev_id
.devno
, sch
->schid
.sch_no
);
380 if (cdev
->private->flags
.donotify
) {
381 cdev
->private->flags
.donotify
= 0;
382 PREPARE_WORK(&cdev
->private->kick_work
, ccw_device_oper_notify
);
383 queue_work(ccw_device_notify_work
, &cdev
->private->kick_work
);
385 wake_up(&cdev
->private->wait_q
);
387 if (css_init_done
&& state
!= DEV_STATE_ONLINE
)
388 put_device (&cdev
->dev
);
391 static inline int cmp_pgid(struct pgid
*p1
, struct pgid
*p2
)
399 return memcmp(c1
+ 1, c2
+ 1, sizeof(struct pgid
) - 1);
402 static void __ccw_device_get_common_pgid(struct ccw_device
*cdev
)
408 for (i
= 0; i
< 8; i
++) {
409 if (cdev
->private->pgid
[i
].inf
.ps
.state1
== SNID_STATE1_RESET
)
412 if (cdev
->private->pgid
[last
].inf
.ps
.state1
==
414 /* First non-zero PGID */
418 if (cmp_pgid(&cdev
->private->pgid
[i
],
419 &cdev
->private->pgid
[last
]) == 0)
420 /* Non-conflicting PGIDs */
423 /* PGID mismatch, can't pathgroup. */
424 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
425 "0.%x.%04x, can't pathgroup\n",
426 cdev
->private->dev_id
.ssid
,
427 cdev
->private->dev_id
.devno
);
428 cdev
->private->options
.pgroup
= 0;
431 if (cdev
->private->pgid
[last
].inf
.ps
.state1
==
433 /* No previous pgid found */
434 memcpy(&cdev
->private->pgid
[0], &css
[0]->global_pgid
,
435 sizeof(struct pgid
));
437 /* Use existing pgid */
438 memcpy(&cdev
->private->pgid
[0], &cdev
->private->pgid
[last
],
439 sizeof(struct pgid
));
443 * Function called from device_pgid.c after sense path ground has completed.
446 ccw_device_sense_pgid_done(struct ccw_device
*cdev
, int err
)
448 struct subchannel
*sch
;
450 sch
= to_subchannel(cdev
->dev
.parent
);
452 case -EOPNOTSUPP
: /* path grouping not supported, use nop instead. */
453 cdev
->private->options
.pgroup
= 0;
455 case 0: /* success */
456 case -EACCES
: /* partial success, some paths not operational */
457 /* Check if all pgids are equal or 0. */
458 __ccw_device_get_common_pgid(cdev
);
460 case -ETIME
: /* Sense path group id stopped by timeout. */
461 case -EUSERS
: /* device is reserved for someone else. */
462 ccw_device_done(cdev
, DEV_STATE_BOXED
);
465 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
468 /* Start Path Group verification. */
469 cdev
->private->state
= DEV_STATE_VERIFY
;
470 cdev
->private->flags
.doverify
= 0;
471 ccw_device_verify_start(cdev
);
475 * Start device recognition.
478 ccw_device_recognition(struct ccw_device
*cdev
)
480 struct subchannel
*sch
;
483 if ((cdev
->private->state
!= DEV_STATE_NOT_OPER
) &&
484 (cdev
->private->state
!= DEV_STATE_BOXED
))
486 sch
= to_subchannel(cdev
->dev
.parent
);
487 ret
= cio_enable_subchannel(sch
, sch
->schib
.pmcw
.isc
);
489 /* Couldn't enable the subchannel for i/o. Sick device. */
492 /* After 60s the device recognition is considered to have failed. */
493 ccw_device_set_timeout(cdev
, 60*HZ
);
496 * We used to start here with a sense pgid to find out whether a device
497 * is locked by someone else. Unfortunately, the sense pgid command
498 * code has other meanings on devices predating the path grouping
499 * algorithm, so we start with sense id and box the device after an
500 * timeout (or if sense pgid during path verification detects the device
501 * is locked, as may happen on newer devices).
503 cdev
->private->flags
.recog_done
= 0;
504 cdev
->private->state
= DEV_STATE_SENSE_ID
;
505 ccw_device_sense_id_start(cdev
);
510 * Handle timeout in device recognition.
513 ccw_device_recog_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
517 ret
= ccw_device_cancel_halt_clear(cdev
);
520 ccw_device_recog_done(cdev
, DEV_STATE_BOXED
);
523 ccw_device_recog_done(cdev
, DEV_STATE_NOT_OPER
);
526 ccw_device_set_timeout(cdev
, 3*HZ
);
532 ccw_device_nopath_notify(struct work_struct
*work
)
534 struct ccw_device_private
*priv
;
535 struct ccw_device
*cdev
;
536 struct subchannel
*sch
;
539 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
541 sch
= to_subchannel(cdev
->dev
.parent
);
545 ret
= (sch
->driver
&& sch
->driver
->notify
) ?
546 sch
->driver
->notify(&sch
->dev
, CIO_NO_PATH
) : 0;
548 if (get_device(&sch
->dev
)) {
549 /* Driver doesn't want to keep device. */
550 cio_disable_subchannel(sch
);
551 if (get_device(&cdev
->dev
)) {
552 PREPARE_WORK(&cdev
->private->kick_work
,
553 ccw_device_call_sch_unregister
);
554 queue_work(ccw_device_work
,
555 &cdev
->private->kick_work
);
557 put_device(&sch
->dev
);
560 cio_disable_subchannel(sch
);
561 ccw_device_set_timeout(cdev
, 0);
562 cdev
->private->flags
.fake_irb
= 0;
563 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
564 wake_up(&cdev
->private->wait_q
);
569 ccw_device_verify_done(struct ccw_device
*cdev
, int err
)
571 struct subchannel
*sch
;
573 sch
= to_subchannel(cdev
->dev
.parent
);
574 /* Update schib - pom may have changed. */
575 stsch(sch
->schid
, &sch
->schib
);
576 /* Update lpm with verified path mask. */
578 /* Repeat path verification? */
579 if (cdev
->private->flags
.doverify
) {
580 cdev
->private->flags
.doverify
= 0;
581 ccw_device_verify_start(cdev
);
585 case -EOPNOTSUPP
: /* path grouping not supported, just set online. */
586 cdev
->private->options
.pgroup
= 0;
588 ccw_device_done(cdev
, DEV_STATE_ONLINE
);
589 /* Deliver fake irb to device driver, if needed. */
590 if (cdev
->private->flags
.fake_irb
) {
591 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
592 cdev
->private->irb
.scsw
.cc
= 1;
593 cdev
->private->irb
.scsw
.fctl
= SCSW_FCTL_START_FUNC
;
594 cdev
->private->irb
.scsw
.actl
= SCSW_ACTL_START_PEND
;
595 cdev
->private->irb
.scsw
.stctl
= SCSW_STCTL_STATUS_PEND
;
596 cdev
->private->flags
.fake_irb
= 0;
598 cdev
->handler(cdev
, cdev
->private->intparm
,
599 &cdev
->private->irb
);
600 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
604 /* Reset oper notify indication after verify error. */
605 cdev
->private->flags
.donotify
= 0;
606 ccw_device_done(cdev
, DEV_STATE_BOXED
);
609 /* Reset oper notify indication after verify error. */
610 cdev
->private->flags
.donotify
= 0;
611 PREPARE_WORK(&cdev
->private->kick_work
,
612 ccw_device_nopath_notify
);
613 queue_work(ccw_device_notify_work
, &cdev
->private->kick_work
);
614 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
623 ccw_device_online(struct ccw_device
*cdev
)
625 struct subchannel
*sch
;
628 if ((cdev
->private->state
!= DEV_STATE_OFFLINE
) &&
629 (cdev
->private->state
!= DEV_STATE_BOXED
))
631 sch
= to_subchannel(cdev
->dev
.parent
);
632 if (css_init_done
&& !get_device(&cdev
->dev
))
634 ret
= cio_enable_subchannel(sch
, sch
->schib
.pmcw
.isc
);
636 /* Couldn't enable the subchannel for i/o. Sick device. */
638 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
641 /* Do we want to do path grouping? */
642 if (!cdev
->private->options
.pgroup
) {
643 /* Start initial path verification. */
644 cdev
->private->state
= DEV_STATE_VERIFY
;
645 cdev
->private->flags
.doverify
= 0;
646 ccw_device_verify_start(cdev
);
649 /* Do a SensePGID first. */
650 cdev
->private->state
= DEV_STATE_SENSE_PGID
;
651 ccw_device_sense_pgid_start(cdev
);
656 ccw_device_disband_done(struct ccw_device
*cdev
, int err
)
660 ccw_device_done(cdev
, DEV_STATE_OFFLINE
);
663 ccw_device_done(cdev
, DEV_STATE_BOXED
);
666 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
675 ccw_device_offline(struct ccw_device
*cdev
)
677 struct subchannel
*sch
;
679 sch
= to_subchannel(cdev
->dev
.parent
);
680 if (stsch(sch
->schid
, &sch
->schib
) || !sch
->schib
.pmcw
.dnv
)
682 if (cdev
->private->state
!= DEV_STATE_ONLINE
) {
683 if (sch
->schib
.scsw
.actl
!= 0)
687 if (sch
->schib
.scsw
.actl
!= 0)
689 /* Are we doing path grouping? */
690 if (!cdev
->private->options
.pgroup
) {
691 /* No, set state offline immediately. */
692 ccw_device_done(cdev
, DEV_STATE_OFFLINE
);
695 /* Start Set Path Group commands. */
696 cdev
->private->state
= DEV_STATE_DISBAND_PGID
;
697 ccw_device_disband_start(cdev
);
702 * Handle timeout in device online/offline process.
705 ccw_device_onoff_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
709 ret
= ccw_device_cancel_halt_clear(cdev
);
712 ccw_device_done(cdev
, DEV_STATE_BOXED
);
715 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
718 ccw_device_set_timeout(cdev
, 3*HZ
);
723 * Handle not oper event in device recognition.
726 ccw_device_recog_notoper(struct ccw_device
*cdev
, enum dev_event dev_event
)
728 ccw_device_recog_done(cdev
, DEV_STATE_NOT_OPER
);
732 * Handle not operational event while offline.
735 ccw_device_offline_notoper(struct ccw_device
*cdev
, enum dev_event dev_event
)
737 struct subchannel
*sch
;
739 cdev
->private->state
= DEV_STATE_NOT_OPER
;
740 sch
= to_subchannel(cdev
->dev
.parent
);
741 if (get_device(&cdev
->dev
)) {
742 PREPARE_WORK(&cdev
->private->kick_work
,
743 ccw_device_call_sch_unregister
);
744 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
746 wake_up(&cdev
->private->wait_q
);
750 * Handle not operational event while online.
753 ccw_device_online_notoper(struct ccw_device
*cdev
, enum dev_event dev_event
)
755 struct subchannel
*sch
;
757 sch
= to_subchannel(cdev
->dev
.parent
);
758 if (sch
->driver
->notify
&&
759 sch
->driver
->notify(&sch
->dev
, sch
->lpm
? CIO_GONE
: CIO_NO_PATH
)) {
760 ccw_device_set_timeout(cdev
, 0);
761 cdev
->private->flags
.fake_irb
= 0;
762 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
763 wake_up(&cdev
->private->wait_q
);
766 cdev
->private->state
= DEV_STATE_NOT_OPER
;
767 cio_disable_subchannel(sch
);
768 if (sch
->schib
.scsw
.actl
!= 0) {
769 // FIXME: not-oper indication to device driver ?
770 ccw_device_call_handler(cdev
);
772 if (get_device(&cdev
->dev
)) {
773 PREPARE_WORK(&cdev
->private->kick_work
,
774 ccw_device_call_sch_unregister
);
775 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
777 wake_up(&cdev
->private->wait_q
);
781 * Handle path verification event.
784 ccw_device_online_verify(struct ccw_device
*cdev
, enum dev_event dev_event
)
786 struct subchannel
*sch
;
788 if (cdev
->private->state
== DEV_STATE_W4SENSE
) {
789 cdev
->private->flags
.doverify
= 1;
792 sch
= to_subchannel(cdev
->dev
.parent
);
794 * Since we might not just be coming from an interrupt from the
795 * subchannel we have to update the schib.
797 stsch(sch
->schid
, &sch
->schib
);
799 if (sch
->schib
.scsw
.actl
!= 0 ||
800 (sch
->schib
.scsw
.stctl
& SCSW_STCTL_STATUS_PEND
) ||
801 (cdev
->private->irb
.scsw
.stctl
& SCSW_STCTL_STATUS_PEND
)) {
803 * No final status yet or final status not yet delivered
804 * to the device driver. Can't do path verfication now,
805 * delay until final status was delivered.
807 cdev
->private->flags
.doverify
= 1;
810 /* Device is idle, we can do the path verification. */
811 cdev
->private->state
= DEV_STATE_VERIFY
;
812 cdev
->private->flags
.doverify
= 0;
813 ccw_device_verify_start(cdev
);
817 * Got an interrupt for a normal io (state online).
820 ccw_device_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
824 irb
= (struct irb
*) __LC_IRB
;
825 /* Check for unsolicited interrupt. */
826 if ((irb
->scsw
.stctl
==
827 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
))
828 && (!irb
->scsw
.cc
)) {
829 if ((irb
->scsw
.dstat
& DEV_STAT_UNIT_CHECK
) &&
830 !irb
->esw
.esw0
.erw
.cons
) {
831 /* Unit check but no sense data. Need basic sense. */
832 if (ccw_device_do_sense(cdev
, irb
) != 0)
833 goto call_handler_unsol
;
834 memcpy(&cdev
->private->irb
, irb
, sizeof(struct irb
));
835 cdev
->private->state
= DEV_STATE_W4SENSE
;
836 cdev
->private->intparm
= 0;
841 cdev
->handler (cdev
, 0, irb
);
844 /* Accumulate status and find out if a basic sense is needed. */
845 ccw_device_accumulate_irb(cdev
, irb
);
846 if (cdev
->private->flags
.dosense
) {
847 if (ccw_device_do_sense(cdev
, irb
) == 0) {
848 cdev
->private->state
= DEV_STATE_W4SENSE
;
852 /* Call the handler. */
853 if (ccw_device_call_handler(cdev
) && cdev
->private->flags
.doverify
)
854 /* Start delayed path verification. */
855 ccw_device_online_verify(cdev
, 0);
859 * Got an timeout in online state.
862 ccw_device_online_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
866 ccw_device_set_timeout(cdev
, 0);
867 ret
= ccw_device_cancel_halt_clear(cdev
);
869 ccw_device_set_timeout(cdev
, 3*HZ
);
870 cdev
->private->state
= DEV_STATE_TIMEOUT_KILL
;
873 if (ret
== -ENODEV
) {
874 struct subchannel
*sch
;
876 sch
= to_subchannel(cdev
->dev
.parent
);
878 PREPARE_WORK(&cdev
->private->kick_work
,
879 ccw_device_nopath_notify
);
880 queue_work(ccw_device_notify_work
,
881 &cdev
->private->kick_work
);
883 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
884 } else if (cdev
->handler
)
885 cdev
->handler(cdev
, cdev
->private->intparm
,
886 ERR_PTR(-ETIMEDOUT
));
890 * Got an interrupt for a basic sense.
893 ccw_device_w4sense(struct ccw_device
*cdev
, enum dev_event dev_event
)
897 irb
= (struct irb
*) __LC_IRB
;
898 /* Check for unsolicited interrupt. */
899 if (irb
->scsw
.stctl
==
900 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
)) {
901 if (irb
->scsw
.cc
== 1)
902 /* Basic sense hasn't started. Try again. */
903 ccw_device_do_sense(cdev
, irb
);
905 printk(KERN_INFO
"Huh? %s(%s): unsolicited "
907 __FUNCTION__
, cdev
->dev
.bus_id
);
909 cdev
->handler (cdev
, 0, irb
);
914 * Check if a halt or clear has been issued in the meanwhile. If yes,
915 * only deliver the halt/clear interrupt to the device driver as if it
916 * had killed the original request.
918 if (irb
->scsw
.fctl
& (SCSW_FCTL_CLEAR_FUNC
| SCSW_FCTL_HALT_FUNC
)) {
919 /* Retry Basic Sense if requested. */
920 if (cdev
->private->flags
.intretry
) {
921 cdev
->private->flags
.intretry
= 0;
922 ccw_device_do_sense(cdev
, irb
);
925 cdev
->private->flags
.dosense
= 0;
926 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
927 ccw_device_accumulate_irb(cdev
, irb
);
930 /* Add basic sense info to irb. */
931 ccw_device_accumulate_basic_sense(cdev
, irb
);
932 if (cdev
->private->flags
.dosense
) {
933 /* Another basic sense is needed. */
934 ccw_device_do_sense(cdev
, irb
);
938 cdev
->private->state
= DEV_STATE_ONLINE
;
939 /* Call the handler. */
940 if (ccw_device_call_handler(cdev
) && cdev
->private->flags
.doverify
)
941 /* Start delayed path verification. */
942 ccw_device_online_verify(cdev
, 0);
946 ccw_device_clear_verify(struct ccw_device
*cdev
, enum dev_event dev_event
)
950 irb
= (struct irb
*) __LC_IRB
;
951 /* Accumulate status. We don't do basic sense. */
952 ccw_device_accumulate_irb(cdev
, irb
);
953 /* Remember to clear irb to avoid residuals. */
954 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
955 /* Try to start delayed device verification. */
956 ccw_device_online_verify(cdev
, 0);
957 /* Note: Don't call handler for cio initiated clear! */
961 ccw_device_killing_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
963 struct subchannel
*sch
;
965 sch
= to_subchannel(cdev
->dev
.parent
);
966 ccw_device_set_timeout(cdev
, 0);
967 /* OK, i/o is dead now. Call interrupt handler. */
968 cdev
->private->state
= DEV_STATE_ONLINE
;
970 cdev
->handler(cdev
, cdev
->private->intparm
,
973 PREPARE_WORK(&cdev
->private->kick_work
,
974 ccw_device_nopath_notify
);
975 queue_work(ccw_device_notify_work
, &cdev
->private->kick_work
);
976 } else if (cdev
->private->flags
.doverify
)
977 /* Start delayed path verification. */
978 ccw_device_online_verify(cdev
, 0);
982 ccw_device_killing_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
986 ret
= ccw_device_cancel_halt_clear(cdev
);
988 ccw_device_set_timeout(cdev
, 3*HZ
);
991 if (ret
== -ENODEV
) {
992 struct subchannel
*sch
;
994 sch
= to_subchannel(cdev
->dev
.parent
);
996 PREPARE_WORK(&cdev
->private->kick_work
,
997 ccw_device_nopath_notify
);
998 queue_work(ccw_device_notify_work
,
999 &cdev
->private->kick_work
);
1001 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
1004 //FIXME: Can we get here?
1005 cdev
->private->state
= DEV_STATE_ONLINE
;
1007 cdev
->handler(cdev
, cdev
->private->intparm
,
1011 void device_kill_io(struct subchannel
*sch
)
1014 struct ccw_device
*cdev
;
1016 cdev
= sch
->dev
.driver_data
;
1017 ret
= ccw_device_cancel_halt_clear(cdev
);
1018 if (ret
== -EBUSY
) {
1019 ccw_device_set_timeout(cdev
, 3*HZ
);
1020 cdev
->private->state
= DEV_STATE_TIMEOUT_KILL
;
1023 if (ret
== -ENODEV
) {
1025 PREPARE_WORK(&cdev
->private->kick_work
,
1026 ccw_device_nopath_notify
);
1027 queue_work(ccw_device_notify_work
,
1028 &cdev
->private->kick_work
);
1030 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
1034 cdev
->handler(cdev
, cdev
->private->intparm
,
1037 PREPARE_WORK(&cdev
->private->kick_work
,
1038 ccw_device_nopath_notify
);
1039 queue_work(ccw_device_notify_work
, &cdev
->private->kick_work
);
1041 /* Start delayed path verification. */
1042 ccw_device_online_verify(cdev
, 0);
1046 ccw_device_delay_verify(struct ccw_device
*cdev
, enum dev_event dev_event
)
1048 /* Start verification after current task finished. */
1049 cdev
->private->flags
.doverify
= 1;
1053 ccw_device_stlck_done(struct ccw_device
*cdev
, enum dev_event dev_event
)
1057 switch (dev_event
) {
1058 case DEV_EVENT_INTERRUPT
:
1059 irb
= (struct irb
*) __LC_IRB
;
1060 /* Check for unsolicited interrupt. */
1061 if ((irb
->scsw
.stctl
==
1062 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
)) &&
1064 /* FIXME: we should restart stlck here, but this
1065 * is extremely unlikely ... */
1068 ccw_device_accumulate_irb(cdev
, irb
);
1069 /* We don't care about basic sense etc. */
1071 default: /* timeout */
1075 wake_up(&cdev
->private->wait_q
);
1079 ccw_device_start_id(struct ccw_device
*cdev
, enum dev_event dev_event
)
1081 struct subchannel
*sch
;
1083 sch
= to_subchannel(cdev
->dev
.parent
);
1084 if (cio_enable_subchannel(sch
, sch
->schib
.pmcw
.isc
) != 0)
1085 /* Couldn't enable the subchannel for i/o. Sick device. */
1088 /* After 60s the device recognition is considered to have failed. */
1089 ccw_device_set_timeout(cdev
, 60*HZ
);
1091 cdev
->private->state
= DEV_STATE_DISCONNECTED_SENSE_ID
;
1092 ccw_device_sense_id_start(cdev
);
1096 device_trigger_reprobe(struct subchannel
*sch
)
1098 struct ccw_device
*cdev
;
1100 if (!sch
->dev
.driver_data
)
1102 cdev
= sch
->dev
.driver_data
;
1103 if (cdev
->private->state
!= DEV_STATE_DISCONNECTED
)
1106 /* Update some values. */
1107 if (stsch(sch
->schid
, &sch
->schib
))
1109 if (!sch
->schib
.pmcw
.dnv
)
1112 * The pim, pam, pom values may not be accurate, but they are the best
1113 * we have before performing device selection :/
1115 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1116 /* Re-set some bits in the pmcw that were lost. */
1117 sch
->schib
.pmcw
.isc
= 3;
1118 sch
->schib
.pmcw
.csense
= 1;
1119 sch
->schib
.pmcw
.ena
= 0;
1120 if ((sch
->lpm
& (sch
->lpm
- 1)) != 0)
1121 sch
->schib
.pmcw
.mp
= 1;
1122 sch
->schib
.pmcw
.intparm
= (__u32
)(unsigned long)sch
;
1123 /* We should also udate ssd info, but this has to wait. */
1124 ccw_device_start_id(cdev
, 0);
1128 ccw_device_offline_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
1130 struct subchannel
*sch
;
1132 sch
= to_subchannel(cdev
->dev
.parent
);
1134 * An interrupt in state offline means a previous disable was not
1135 * successful. Try again.
1137 cio_disable_subchannel(sch
);
1141 ccw_device_change_cmfstate(struct ccw_device
*cdev
, enum dev_event dev_event
)
1143 retry_set_schib(cdev
);
1144 cdev
->private->state
= DEV_STATE_ONLINE
;
1145 dev_fsm_event(cdev
, dev_event
);
1148 static void ccw_device_update_cmfblock(struct ccw_device
*cdev
,
1149 enum dev_event dev_event
)
1151 cmf_retry_copy_block(cdev
);
1152 cdev
->private->state
= DEV_STATE_ONLINE
;
1153 dev_fsm_event(cdev
, dev_event
);
1157 ccw_device_quiesce_done(struct ccw_device
*cdev
, enum dev_event dev_event
)
1159 ccw_device_set_timeout(cdev
, 0);
1160 if (dev_event
== DEV_EVENT_NOTOPER
)
1161 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1163 cdev
->private->state
= DEV_STATE_OFFLINE
;
1164 wake_up(&cdev
->private->wait_q
);
1168 ccw_device_quiesce_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
1172 ret
= ccw_device_cancel_halt_clear(cdev
);
1175 cdev
->private->state
= DEV_STATE_OFFLINE
;
1176 wake_up(&cdev
->private->wait_q
);
1179 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1180 wake_up(&cdev
->private->wait_q
);
1183 ccw_device_set_timeout(cdev
, HZ
/10);
1188 * No operation action. This is used e.g. to ignore a timeout event in
1192 ccw_device_nop(struct ccw_device
*cdev
, enum dev_event dev_event
)
1197 * Bug operation action.
1200 ccw_device_bug(struct ccw_device
*cdev
, enum dev_event dev_event
)
1202 printk(KERN_EMERG
"dev_jumptable[%i][%i] == NULL\n",
1203 cdev
->private->state
, dev_event
);
1208 * device statemachine
1210 fsm_func_t
*dev_jumptable
[NR_DEV_STATES
][NR_DEV_EVENTS
] = {
1211 [DEV_STATE_NOT_OPER
] = {
1212 [DEV_EVENT_NOTOPER
] = ccw_device_nop
,
1213 [DEV_EVENT_INTERRUPT
] = ccw_device_bug
,
1214 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1215 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1217 [DEV_STATE_SENSE_PGID
] = {
1218 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1219 [DEV_EVENT_INTERRUPT
] = ccw_device_sense_pgid_irq
,
1220 [DEV_EVENT_TIMEOUT
] = ccw_device_onoff_timeout
,
1221 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1223 [DEV_STATE_SENSE_ID
] = {
1224 [DEV_EVENT_NOTOPER
] = ccw_device_recog_notoper
,
1225 [DEV_EVENT_INTERRUPT
] = ccw_device_sense_id_irq
,
1226 [DEV_EVENT_TIMEOUT
] = ccw_device_recog_timeout
,
1227 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1229 [DEV_STATE_OFFLINE
] = {
1230 [DEV_EVENT_NOTOPER
] = ccw_device_offline_notoper
,
1231 [DEV_EVENT_INTERRUPT
] = ccw_device_offline_irq
,
1232 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1233 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1235 [DEV_STATE_VERIFY
] = {
1236 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1237 [DEV_EVENT_INTERRUPT
] = ccw_device_verify_irq
,
1238 [DEV_EVENT_TIMEOUT
] = ccw_device_onoff_timeout
,
1239 [DEV_EVENT_VERIFY
] = ccw_device_delay_verify
,
1241 [DEV_STATE_ONLINE
] = {
1242 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1243 [DEV_EVENT_INTERRUPT
] = ccw_device_irq
,
1244 [DEV_EVENT_TIMEOUT
] = ccw_device_online_timeout
,
1245 [DEV_EVENT_VERIFY
] = ccw_device_online_verify
,
1247 [DEV_STATE_W4SENSE
] = {
1248 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1249 [DEV_EVENT_INTERRUPT
] = ccw_device_w4sense
,
1250 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1251 [DEV_EVENT_VERIFY
] = ccw_device_online_verify
,
1253 [DEV_STATE_DISBAND_PGID
] = {
1254 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1255 [DEV_EVENT_INTERRUPT
] = ccw_device_disband_irq
,
1256 [DEV_EVENT_TIMEOUT
] = ccw_device_onoff_timeout
,
1257 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1259 [DEV_STATE_BOXED
] = {
1260 [DEV_EVENT_NOTOPER
] = ccw_device_offline_notoper
,
1261 [DEV_EVENT_INTERRUPT
] = ccw_device_stlck_done
,
1262 [DEV_EVENT_TIMEOUT
] = ccw_device_stlck_done
,
1263 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1265 /* states to wait for i/o completion before doing something */
1266 [DEV_STATE_CLEAR_VERIFY
] = {
1267 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1268 [DEV_EVENT_INTERRUPT
] = ccw_device_clear_verify
,
1269 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1270 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1272 [DEV_STATE_TIMEOUT_KILL
] = {
1273 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1274 [DEV_EVENT_INTERRUPT
] = ccw_device_killing_irq
,
1275 [DEV_EVENT_TIMEOUT
] = ccw_device_killing_timeout
,
1276 [DEV_EVENT_VERIFY
] = ccw_device_nop
, //FIXME
1278 [DEV_STATE_QUIESCE
] = {
1279 [DEV_EVENT_NOTOPER
] = ccw_device_quiesce_done
,
1280 [DEV_EVENT_INTERRUPT
] = ccw_device_quiesce_done
,
1281 [DEV_EVENT_TIMEOUT
] = ccw_device_quiesce_timeout
,
1282 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1284 /* special states for devices gone not operational */
1285 [DEV_STATE_DISCONNECTED
] = {
1286 [DEV_EVENT_NOTOPER
] = ccw_device_nop
,
1287 [DEV_EVENT_INTERRUPT
] = ccw_device_start_id
,
1288 [DEV_EVENT_TIMEOUT
] = ccw_device_bug
,
1289 [DEV_EVENT_VERIFY
] = ccw_device_start_id
,
1291 [DEV_STATE_DISCONNECTED_SENSE_ID
] = {
1292 [DEV_EVENT_NOTOPER
] = ccw_device_recog_notoper
,
1293 [DEV_EVENT_INTERRUPT
] = ccw_device_sense_id_irq
,
1294 [DEV_EVENT_TIMEOUT
] = ccw_device_recog_timeout
,
1295 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1297 [DEV_STATE_CMFCHANGE
] = {
1298 [DEV_EVENT_NOTOPER
] = ccw_device_change_cmfstate
,
1299 [DEV_EVENT_INTERRUPT
] = ccw_device_change_cmfstate
,
1300 [DEV_EVENT_TIMEOUT
] = ccw_device_change_cmfstate
,
1301 [DEV_EVENT_VERIFY
] = ccw_device_change_cmfstate
,
1303 [DEV_STATE_CMFUPDATE
] = {
1304 [DEV_EVENT_NOTOPER
] = ccw_device_update_cmfblock
,
1305 [DEV_EVENT_INTERRUPT
] = ccw_device_update_cmfblock
,
1306 [DEV_EVENT_TIMEOUT
] = ccw_device_update_cmfblock
,
1307 [DEV_EVENT_VERIFY
] = ccw_device_update_cmfblock
,
1312 * io_subchannel_irq is called for "real" interrupts or for status
1313 * pending conditions on msch.
1316 io_subchannel_irq (struct device
*pdev
)
1318 struct ccw_device
*cdev
;
1320 cdev
= to_subchannel(pdev
)->dev
.driver_data
;
1322 CIO_TRACE_EVENT (3, "IRQ");
1323 CIO_TRACE_EVENT (3, pdev
->bus_id
);
1325 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1328 EXPORT_SYMBOL_GPL(ccw_device_set_timeout
);