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.
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 PREPARE_WORK(&cdev
->private->kick_work
,
196 ccw_device_do_unreg_rereg
);
197 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
200 cdev
->private->flags
.donotify
= 1;
205 * The machine won't give us any notification by machine check if a chpid has
206 * been varied online on the SE so we have to find out by magic (i. e. driving
207 * the channel subsystem to device selection and updating our path masks).
210 __recover_lost_chpids(struct subchannel
*sch
, int old_lpm
)
214 for (i
= 0; i
<8; i
++) {
216 if (!(sch
->lpm
& mask
))
220 chpid_is_actually_online(sch
->schib
.pmcw
.chpid
[i
]);
225 * Stop device recognition.
228 ccw_device_recog_done(struct ccw_device
*cdev
, int state
)
230 struct subchannel
*sch
;
231 int notify
, old_lpm
, same_dev
;
233 sch
= to_subchannel(cdev
->dev
.parent
);
235 ccw_device_set_timeout(cdev
, 0);
236 cio_disable_subchannel(sch
);
238 * Now that we tried recognition, we have performed device selection
239 * through ssch() and the path information is up to date.
242 stsch(sch
->schid
, &sch
->schib
);
243 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
244 /* Check since device may again have become not operational. */
245 if (!sch
->schib
.pmcw
.dnv
)
246 state
= DEV_STATE_NOT_OPER
;
247 if (cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
)
248 /* Force reprobe on all chpids. */
250 if (sch
->lpm
!= old_lpm
)
251 __recover_lost_chpids(sch
, old_lpm
);
252 if (cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
) {
253 if (state
== DEV_STATE_NOT_OPER
) {
254 cdev
->private->flags
.recog_done
= 1;
255 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
258 /* Boxed devices don't need extra treatment. */
261 same_dev
= 0; /* Keep the compiler quiet... */
263 case DEV_STATE_NOT_OPER
:
264 CIO_DEBUG(KERN_WARNING
, 2,
265 "SenseID : unknown device %04x on subchannel "
266 "0.%x.%04x\n", cdev
->private->dev_id
.devno
,
267 sch
->schid
.ssid
, sch
->schid
.sch_no
);
269 case DEV_STATE_OFFLINE
:
270 if (cdev
->private->state
== DEV_STATE_DISCONNECTED_SENSE_ID
) {
271 same_dev
= ccw_device_handle_oper(cdev
);
274 /* fill out sense information */
275 memset(&cdev
->id
, 0, sizeof(cdev
->id
));
276 cdev
->id
.cu_type
= cdev
->private->senseid
.cu_type
;
277 cdev
->id
.cu_model
= cdev
->private->senseid
.cu_model
;
278 cdev
->id
.dev_type
= cdev
->private->senseid
.dev_type
;
279 cdev
->id
.dev_model
= cdev
->private->senseid
.dev_model
;
281 cdev
->private->state
= DEV_STATE_OFFLINE
;
283 /* Get device online again. */
284 ccw_device_online(cdev
);
285 wake_up(&cdev
->private->wait_q
);
289 /* Issue device info message. */
290 CIO_DEBUG(KERN_INFO
, 2, "SenseID : device 0.%x.%04x reports: "
291 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
293 cdev
->private->dev_id
.ssid
,
294 cdev
->private->dev_id
.devno
,
295 cdev
->id
.cu_type
, cdev
->id
.cu_model
,
296 cdev
->id
.dev_type
, cdev
->id
.dev_model
);
298 case DEV_STATE_BOXED
:
299 CIO_DEBUG(KERN_WARNING
, 2,
300 "SenseID : boxed device %04x on subchannel "
301 "0.%x.%04x\n", cdev
->private->dev_id
.devno
,
302 sch
->schid
.ssid
, sch
->schid
.sch_no
);
305 cdev
->private->state
= state
;
306 io_subchannel_recog_done(cdev
);
307 if (state
!= DEV_STATE_NOT_OPER
)
308 wake_up(&cdev
->private->wait_q
);
312 * Function called from device_id.c after sense id has completed.
315 ccw_device_sense_id_done(struct ccw_device
*cdev
, int err
)
319 ccw_device_recog_done(cdev
, DEV_STATE_OFFLINE
);
321 case -ETIME
: /* Sense id stopped by timeout. */
322 ccw_device_recog_done(cdev
, DEV_STATE_BOXED
);
325 ccw_device_recog_done(cdev
, DEV_STATE_NOT_OPER
);
331 ccw_device_oper_notify(struct work_struct
*work
)
333 struct ccw_device_private
*priv
;
334 struct ccw_device
*cdev
;
335 struct subchannel
*sch
;
338 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
340 sch
= to_subchannel(cdev
->dev
.parent
);
341 ret
= (sch
->driver
&& sch
->driver
->notify
) ?
342 sch
->driver
->notify(&sch
->dev
, CIO_OPER
) : 0;
344 /* Driver doesn't want device back. */
345 ccw_device_do_unreg_rereg(work
);
347 /* Reenable channel measurements, if needed. */
349 wake_up(&cdev
->private->wait_q
);
354 * Finished with online/offline processing.
357 ccw_device_done(struct ccw_device
*cdev
, int state
)
359 struct subchannel
*sch
;
361 sch
= to_subchannel(cdev
->dev
.parent
);
363 ccw_device_set_timeout(cdev
, 0);
365 if (state
!= DEV_STATE_ONLINE
)
366 cio_disable_subchannel(sch
);
368 /* Reset device status. */
369 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
371 cdev
->private->state
= state
;
374 if (state
== DEV_STATE_BOXED
)
375 CIO_DEBUG(KERN_WARNING
, 2,
376 "Boxed device %04x on subchannel %04x\n",
377 cdev
->private->dev_id
.devno
, sch
->schid
.sch_no
);
379 if (cdev
->private->flags
.donotify
) {
380 cdev
->private->flags
.donotify
= 0;
381 PREPARE_WORK(&cdev
->private->kick_work
, ccw_device_oper_notify
);
382 queue_work(ccw_device_notify_work
, &cdev
->private->kick_work
);
384 wake_up(&cdev
->private->wait_q
);
386 if (css_init_done
&& state
!= DEV_STATE_ONLINE
)
387 put_device (&cdev
->dev
);
390 static int cmp_pgid(struct pgid
*p1
, struct pgid
*p2
)
398 return memcmp(c1
+ 1, c2
+ 1, sizeof(struct pgid
) - 1);
401 static void __ccw_device_get_common_pgid(struct ccw_device
*cdev
)
407 for (i
= 0; i
< 8; i
++) {
408 if (cdev
->private->pgid
[i
].inf
.ps
.state1
== SNID_STATE1_RESET
)
411 if (cdev
->private->pgid
[last
].inf
.ps
.state1
==
413 /* First non-zero PGID */
417 if (cmp_pgid(&cdev
->private->pgid
[i
],
418 &cdev
->private->pgid
[last
]) == 0)
419 /* Non-conflicting PGIDs */
422 /* PGID mismatch, can't pathgroup. */
423 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
424 "0.%x.%04x, can't pathgroup\n",
425 cdev
->private->dev_id
.ssid
,
426 cdev
->private->dev_id
.devno
);
427 cdev
->private->options
.pgroup
= 0;
430 if (cdev
->private->pgid
[last
].inf
.ps
.state1
==
432 /* No previous pgid found */
433 memcpy(&cdev
->private->pgid
[0], &css
[0]->global_pgid
,
434 sizeof(struct pgid
));
436 /* Use existing pgid */
437 memcpy(&cdev
->private->pgid
[0], &cdev
->private->pgid
[last
],
438 sizeof(struct pgid
));
442 * Function called from device_pgid.c after sense path ground has completed.
445 ccw_device_sense_pgid_done(struct ccw_device
*cdev
, int err
)
447 struct subchannel
*sch
;
449 sch
= to_subchannel(cdev
->dev
.parent
);
451 case -EOPNOTSUPP
: /* path grouping not supported, use nop instead. */
452 cdev
->private->options
.pgroup
= 0;
454 case 0: /* success */
455 case -EACCES
: /* partial success, some paths not operational */
456 /* Check if all pgids are equal or 0. */
457 __ccw_device_get_common_pgid(cdev
);
459 case -ETIME
: /* Sense path group id stopped by timeout. */
460 case -EUSERS
: /* device is reserved for someone else. */
461 ccw_device_done(cdev
, DEV_STATE_BOXED
);
464 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
467 /* Start Path Group verification. */
468 cdev
->private->state
= DEV_STATE_VERIFY
;
469 cdev
->private->flags
.doverify
= 0;
470 ccw_device_verify_start(cdev
);
474 * Start device recognition.
477 ccw_device_recognition(struct ccw_device
*cdev
)
479 struct subchannel
*sch
;
482 if ((cdev
->private->state
!= DEV_STATE_NOT_OPER
) &&
483 (cdev
->private->state
!= DEV_STATE_BOXED
))
485 sch
= to_subchannel(cdev
->dev
.parent
);
486 ret
= cio_enable_subchannel(sch
, sch
->schib
.pmcw
.isc
);
488 /* Couldn't enable the subchannel for i/o. Sick device. */
491 /* After 60s the device recognition is considered to have failed. */
492 ccw_device_set_timeout(cdev
, 60*HZ
);
495 * We used to start here with a sense pgid to find out whether a device
496 * is locked by someone else. Unfortunately, the sense pgid command
497 * code has other meanings on devices predating the path grouping
498 * algorithm, so we start with sense id and box the device after an
499 * timeout (or if sense pgid during path verification detects the device
500 * is locked, as may happen on newer devices).
502 cdev
->private->flags
.recog_done
= 0;
503 cdev
->private->state
= DEV_STATE_SENSE_ID
;
504 ccw_device_sense_id_start(cdev
);
509 * Handle timeout in device recognition.
512 ccw_device_recog_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
516 ret
= ccw_device_cancel_halt_clear(cdev
);
519 ccw_device_recog_done(cdev
, DEV_STATE_BOXED
);
522 ccw_device_recog_done(cdev
, DEV_STATE_NOT_OPER
);
525 ccw_device_set_timeout(cdev
, 3*HZ
);
531 ccw_device_nopath_notify(struct work_struct
*work
)
533 struct ccw_device_private
*priv
;
534 struct ccw_device
*cdev
;
535 struct subchannel
*sch
;
538 priv
= container_of(work
, struct ccw_device_private
, kick_work
);
540 sch
= to_subchannel(cdev
->dev
.parent
);
544 ret
= (sch
->driver
&& sch
->driver
->notify
) ?
545 sch
->driver
->notify(&sch
->dev
, CIO_NO_PATH
) : 0;
547 if (get_device(&sch
->dev
)) {
548 /* Driver doesn't want to keep device. */
549 cio_disable_subchannel(sch
);
550 if (get_device(&cdev
->dev
)) {
551 PREPARE_WORK(&cdev
->private->kick_work
,
552 ccw_device_call_sch_unregister
);
553 queue_work(ccw_device_work
,
554 &cdev
->private->kick_work
);
556 put_device(&sch
->dev
);
559 cio_disable_subchannel(sch
);
560 ccw_device_set_timeout(cdev
, 0);
561 cdev
->private->flags
.fake_irb
= 0;
562 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
563 wake_up(&cdev
->private->wait_q
);
568 ccw_device_verify_done(struct ccw_device
*cdev
, int err
)
570 struct subchannel
*sch
;
572 sch
= to_subchannel(cdev
->dev
.parent
);
573 /* Update schib - pom may have changed. */
574 stsch(sch
->schid
, &sch
->schib
);
575 /* Update lpm with verified path mask. */
577 /* Repeat path verification? */
578 if (cdev
->private->flags
.doverify
) {
579 cdev
->private->flags
.doverify
= 0;
580 ccw_device_verify_start(cdev
);
584 case -EOPNOTSUPP
: /* path grouping not supported, just set online. */
585 cdev
->private->options
.pgroup
= 0;
587 ccw_device_done(cdev
, DEV_STATE_ONLINE
);
588 /* Deliver fake irb to device driver, if needed. */
589 if (cdev
->private->flags
.fake_irb
) {
590 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
591 cdev
->private->irb
.scsw
.cc
= 1;
592 cdev
->private->irb
.scsw
.fctl
= SCSW_FCTL_START_FUNC
;
593 cdev
->private->irb
.scsw
.actl
= SCSW_ACTL_START_PEND
;
594 cdev
->private->irb
.scsw
.stctl
= SCSW_STCTL_STATUS_PEND
;
595 cdev
->private->flags
.fake_irb
= 0;
597 cdev
->handler(cdev
, cdev
->private->intparm
,
598 &cdev
->private->irb
);
599 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
603 /* Reset oper notify indication after verify error. */
604 cdev
->private->flags
.donotify
= 0;
605 ccw_device_done(cdev
, DEV_STATE_BOXED
);
608 /* Reset oper notify indication after verify error. */
609 cdev
->private->flags
.donotify
= 0;
610 PREPARE_WORK(&cdev
->private->kick_work
,
611 ccw_device_nopath_notify
);
612 queue_work(ccw_device_notify_work
, &cdev
->private->kick_work
);
613 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
622 ccw_device_online(struct ccw_device
*cdev
)
624 struct subchannel
*sch
;
627 if ((cdev
->private->state
!= DEV_STATE_OFFLINE
) &&
628 (cdev
->private->state
!= DEV_STATE_BOXED
))
630 sch
= to_subchannel(cdev
->dev
.parent
);
631 if (css_init_done
&& !get_device(&cdev
->dev
))
633 ret
= cio_enable_subchannel(sch
, sch
->schib
.pmcw
.isc
);
635 /* Couldn't enable the subchannel for i/o. Sick device. */
637 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
640 /* Do we want to do path grouping? */
641 if (!cdev
->private->options
.pgroup
) {
642 /* Start initial path verification. */
643 cdev
->private->state
= DEV_STATE_VERIFY
;
644 cdev
->private->flags
.doverify
= 0;
645 ccw_device_verify_start(cdev
);
648 /* Do a SensePGID first. */
649 cdev
->private->state
= DEV_STATE_SENSE_PGID
;
650 ccw_device_sense_pgid_start(cdev
);
655 ccw_device_disband_done(struct ccw_device
*cdev
, int err
)
659 ccw_device_done(cdev
, DEV_STATE_OFFLINE
);
662 ccw_device_done(cdev
, DEV_STATE_BOXED
);
665 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
674 ccw_device_offline(struct ccw_device
*cdev
)
676 struct subchannel
*sch
;
678 if (ccw_device_is_orphan(cdev
)) {
679 ccw_device_done(cdev
, DEV_STATE_OFFLINE
);
682 sch
= to_subchannel(cdev
->dev
.parent
);
683 if (stsch(sch
->schid
, &sch
->schib
) || !sch
->schib
.pmcw
.dnv
)
685 if (cdev
->private->state
!= DEV_STATE_ONLINE
) {
686 if (sch
->schib
.scsw
.actl
!= 0)
690 if (sch
->schib
.scsw
.actl
!= 0)
692 /* Are we doing path grouping? */
693 if (!cdev
->private->options
.pgroup
) {
694 /* No, set state offline immediately. */
695 ccw_device_done(cdev
, DEV_STATE_OFFLINE
);
698 /* Start Set Path Group commands. */
699 cdev
->private->state
= DEV_STATE_DISBAND_PGID
;
700 ccw_device_disband_start(cdev
);
705 * Handle timeout in device online/offline process.
708 ccw_device_onoff_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
712 ret
= ccw_device_cancel_halt_clear(cdev
);
715 ccw_device_done(cdev
, DEV_STATE_BOXED
);
718 ccw_device_done(cdev
, DEV_STATE_NOT_OPER
);
721 ccw_device_set_timeout(cdev
, 3*HZ
);
726 * Handle not oper event in device recognition.
729 ccw_device_recog_notoper(struct ccw_device
*cdev
, enum dev_event dev_event
)
731 ccw_device_recog_done(cdev
, DEV_STATE_NOT_OPER
);
735 * Handle not operational event while offline.
738 ccw_device_offline_notoper(struct ccw_device
*cdev
, enum dev_event dev_event
)
740 struct subchannel
*sch
;
742 cdev
->private->state
= DEV_STATE_NOT_OPER
;
743 sch
= to_subchannel(cdev
->dev
.parent
);
744 if (get_device(&cdev
->dev
)) {
745 PREPARE_WORK(&cdev
->private->kick_work
,
746 ccw_device_call_sch_unregister
);
747 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
749 wake_up(&cdev
->private->wait_q
);
753 * Handle not operational event while online.
756 ccw_device_online_notoper(struct ccw_device
*cdev
, enum dev_event dev_event
)
758 struct subchannel
*sch
;
760 sch
= to_subchannel(cdev
->dev
.parent
);
761 if (sch
->driver
->notify
&&
762 sch
->driver
->notify(&sch
->dev
, sch
->lpm
? CIO_GONE
: CIO_NO_PATH
)) {
763 ccw_device_set_timeout(cdev
, 0);
764 cdev
->private->flags
.fake_irb
= 0;
765 cdev
->private->state
= DEV_STATE_DISCONNECTED
;
766 wake_up(&cdev
->private->wait_q
);
769 cdev
->private->state
= DEV_STATE_NOT_OPER
;
770 cio_disable_subchannel(sch
);
771 if (sch
->schib
.scsw
.actl
!= 0) {
772 // FIXME: not-oper indication to device driver ?
773 ccw_device_call_handler(cdev
);
775 if (get_device(&cdev
->dev
)) {
776 PREPARE_WORK(&cdev
->private->kick_work
,
777 ccw_device_call_sch_unregister
);
778 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
780 wake_up(&cdev
->private->wait_q
);
784 * Handle path verification event.
787 ccw_device_online_verify(struct ccw_device
*cdev
, enum dev_event dev_event
)
789 struct subchannel
*sch
;
791 if (cdev
->private->state
== DEV_STATE_W4SENSE
) {
792 cdev
->private->flags
.doverify
= 1;
795 sch
= to_subchannel(cdev
->dev
.parent
);
797 * Since we might not just be coming from an interrupt from the
798 * subchannel we have to update the schib.
800 stsch(sch
->schid
, &sch
->schib
);
802 if (sch
->schib
.scsw
.actl
!= 0 ||
803 (sch
->schib
.scsw
.stctl
& SCSW_STCTL_STATUS_PEND
) ||
804 (cdev
->private->irb
.scsw
.stctl
& SCSW_STCTL_STATUS_PEND
)) {
806 * No final status yet or final status not yet delivered
807 * to the device driver. Can't do path verfication now,
808 * delay until final status was delivered.
810 cdev
->private->flags
.doverify
= 1;
813 /* Device is idle, we can do the path verification. */
814 cdev
->private->state
= DEV_STATE_VERIFY
;
815 cdev
->private->flags
.doverify
= 0;
816 ccw_device_verify_start(cdev
);
820 * Got an interrupt for a normal io (state online).
823 ccw_device_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
827 irb
= (struct irb
*) __LC_IRB
;
828 /* Check for unsolicited interrupt. */
829 if ((irb
->scsw
.stctl
==
830 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
))
831 && (!irb
->scsw
.cc
)) {
832 if ((irb
->scsw
.dstat
& DEV_STAT_UNIT_CHECK
) &&
833 !irb
->esw
.esw0
.erw
.cons
) {
834 /* Unit check but no sense data. Need basic sense. */
835 if (ccw_device_do_sense(cdev
, irb
) != 0)
836 goto call_handler_unsol
;
837 memcpy(&cdev
->private->irb
, irb
, sizeof(struct irb
));
838 cdev
->private->state
= DEV_STATE_W4SENSE
;
839 cdev
->private->intparm
= 0;
844 cdev
->handler (cdev
, 0, irb
);
845 if (cdev
->private->flags
.doverify
)
846 ccw_device_online_verify(cdev
, 0);
849 /* Accumulate status and find out if a basic sense is needed. */
850 ccw_device_accumulate_irb(cdev
, irb
);
851 if (cdev
->private->flags
.dosense
) {
852 if (ccw_device_do_sense(cdev
, irb
) == 0) {
853 cdev
->private->state
= DEV_STATE_W4SENSE
;
857 /* Call the handler. */
858 if (ccw_device_call_handler(cdev
) && cdev
->private->flags
.doverify
)
859 /* Start delayed path verification. */
860 ccw_device_online_verify(cdev
, 0);
864 * Got an timeout in online state.
867 ccw_device_online_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
871 ccw_device_set_timeout(cdev
, 0);
872 ret
= ccw_device_cancel_halt_clear(cdev
);
874 ccw_device_set_timeout(cdev
, 3*HZ
);
875 cdev
->private->state
= DEV_STATE_TIMEOUT_KILL
;
878 if (ret
== -ENODEV
) {
879 struct subchannel
*sch
;
881 sch
= to_subchannel(cdev
->dev
.parent
);
883 PREPARE_WORK(&cdev
->private->kick_work
,
884 ccw_device_nopath_notify
);
885 queue_work(ccw_device_notify_work
,
886 &cdev
->private->kick_work
);
888 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
889 } else if (cdev
->handler
)
890 cdev
->handler(cdev
, cdev
->private->intparm
,
891 ERR_PTR(-ETIMEDOUT
));
895 * Got an interrupt for a basic sense.
898 ccw_device_w4sense(struct ccw_device
*cdev
, enum dev_event dev_event
)
902 irb
= (struct irb
*) __LC_IRB
;
903 /* Check for unsolicited interrupt. */
904 if (irb
->scsw
.stctl
==
905 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
)) {
906 if (irb
->scsw
.cc
== 1)
907 /* Basic sense hasn't started. Try again. */
908 ccw_device_do_sense(cdev
, irb
);
910 printk(KERN_INFO
"Huh? %s(%s): unsolicited "
912 __FUNCTION__
, cdev
->dev
.bus_id
);
914 cdev
->handler (cdev
, 0, irb
);
919 * Check if a halt or clear has been issued in the meanwhile. If yes,
920 * only deliver the halt/clear interrupt to the device driver as if it
921 * had killed the original request.
923 if (irb
->scsw
.fctl
& (SCSW_FCTL_CLEAR_FUNC
| SCSW_FCTL_HALT_FUNC
)) {
924 /* Retry Basic Sense if requested. */
925 if (cdev
->private->flags
.intretry
) {
926 cdev
->private->flags
.intretry
= 0;
927 ccw_device_do_sense(cdev
, irb
);
930 cdev
->private->flags
.dosense
= 0;
931 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
932 ccw_device_accumulate_irb(cdev
, irb
);
935 /* Add basic sense info to irb. */
936 ccw_device_accumulate_basic_sense(cdev
, irb
);
937 if (cdev
->private->flags
.dosense
) {
938 /* Another basic sense is needed. */
939 ccw_device_do_sense(cdev
, irb
);
943 cdev
->private->state
= DEV_STATE_ONLINE
;
944 /* Call the handler. */
945 if (ccw_device_call_handler(cdev
) && cdev
->private->flags
.doverify
)
946 /* Start delayed path verification. */
947 ccw_device_online_verify(cdev
, 0);
951 ccw_device_clear_verify(struct ccw_device
*cdev
, enum dev_event dev_event
)
955 irb
= (struct irb
*) __LC_IRB
;
956 /* Accumulate status. We don't do basic sense. */
957 ccw_device_accumulate_irb(cdev
, irb
);
958 /* Remember to clear irb to avoid residuals. */
959 memset(&cdev
->private->irb
, 0, sizeof(struct irb
));
960 /* Try to start delayed device verification. */
961 ccw_device_online_verify(cdev
, 0);
962 /* Note: Don't call handler for cio initiated clear! */
966 ccw_device_killing_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
968 struct subchannel
*sch
;
970 sch
= to_subchannel(cdev
->dev
.parent
);
971 ccw_device_set_timeout(cdev
, 0);
972 /* OK, i/o is dead now. Call interrupt handler. */
973 cdev
->private->state
= DEV_STATE_ONLINE
;
975 cdev
->handler(cdev
, cdev
->private->intparm
,
978 PREPARE_WORK(&cdev
->private->kick_work
,
979 ccw_device_nopath_notify
);
980 queue_work(ccw_device_notify_work
, &cdev
->private->kick_work
);
981 } else if (cdev
->private->flags
.doverify
)
982 /* Start delayed path verification. */
983 ccw_device_online_verify(cdev
, 0);
987 ccw_device_killing_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
991 ret
= ccw_device_cancel_halt_clear(cdev
);
993 ccw_device_set_timeout(cdev
, 3*HZ
);
996 if (ret
== -ENODEV
) {
997 struct subchannel
*sch
;
999 sch
= to_subchannel(cdev
->dev
.parent
);
1001 PREPARE_WORK(&cdev
->private->kick_work
,
1002 ccw_device_nopath_notify
);
1003 queue_work(ccw_device_notify_work
,
1004 &cdev
->private->kick_work
);
1006 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
1009 //FIXME: Can we get here?
1010 cdev
->private->state
= DEV_STATE_ONLINE
;
1012 cdev
->handler(cdev
, cdev
->private->intparm
,
1016 void device_kill_io(struct subchannel
*sch
)
1019 struct ccw_device
*cdev
;
1021 cdev
= sch
->dev
.driver_data
;
1022 ret
= ccw_device_cancel_halt_clear(cdev
);
1023 if (ret
== -EBUSY
) {
1024 ccw_device_set_timeout(cdev
, 3*HZ
);
1025 cdev
->private->state
= DEV_STATE_TIMEOUT_KILL
;
1028 if (ret
== -ENODEV
) {
1030 PREPARE_WORK(&cdev
->private->kick_work
,
1031 ccw_device_nopath_notify
);
1032 queue_work(ccw_device_notify_work
,
1033 &cdev
->private->kick_work
);
1035 dev_fsm_event(cdev
, DEV_EVENT_NOTOPER
);
1039 cdev
->handler(cdev
, cdev
->private->intparm
,
1042 PREPARE_WORK(&cdev
->private->kick_work
,
1043 ccw_device_nopath_notify
);
1044 queue_work(ccw_device_notify_work
, &cdev
->private->kick_work
);
1046 /* Start delayed path verification. */
1047 ccw_device_online_verify(cdev
, 0);
1051 ccw_device_delay_verify(struct ccw_device
*cdev
, enum dev_event dev_event
)
1053 /* Start verification after current task finished. */
1054 cdev
->private->flags
.doverify
= 1;
1058 ccw_device_stlck_done(struct ccw_device
*cdev
, enum dev_event dev_event
)
1062 switch (dev_event
) {
1063 case DEV_EVENT_INTERRUPT
:
1064 irb
= (struct irb
*) __LC_IRB
;
1065 /* Check for unsolicited interrupt. */
1066 if ((irb
->scsw
.stctl
==
1067 (SCSW_STCTL_STATUS_PEND
| SCSW_STCTL_ALERT_STATUS
)) &&
1069 /* FIXME: we should restart stlck here, but this
1070 * is extremely unlikely ... */
1073 ccw_device_accumulate_irb(cdev
, irb
);
1074 /* We don't care about basic sense etc. */
1076 default: /* timeout */
1080 wake_up(&cdev
->private->wait_q
);
1084 ccw_device_start_id(struct ccw_device
*cdev
, enum dev_event dev_event
)
1086 struct subchannel
*sch
;
1088 sch
= to_subchannel(cdev
->dev
.parent
);
1089 if (cio_enable_subchannel(sch
, sch
->schib
.pmcw
.isc
) != 0)
1090 /* Couldn't enable the subchannel for i/o. Sick device. */
1093 /* After 60s the device recognition is considered to have failed. */
1094 ccw_device_set_timeout(cdev
, 60*HZ
);
1096 cdev
->private->state
= DEV_STATE_DISCONNECTED_SENSE_ID
;
1097 ccw_device_sense_id_start(cdev
);
1101 device_trigger_reprobe(struct subchannel
*sch
)
1103 struct ccw_device
*cdev
;
1105 if (!sch
->dev
.driver_data
)
1107 cdev
= sch
->dev
.driver_data
;
1108 if (cdev
->private->state
!= DEV_STATE_DISCONNECTED
)
1111 /* Update some values. */
1112 if (stsch(sch
->schid
, &sch
->schib
))
1114 if (!sch
->schib
.pmcw
.dnv
)
1117 * The pim, pam, pom values may not be accurate, but they are the best
1118 * we have before performing device selection :/
1120 sch
->lpm
= sch
->schib
.pmcw
.pam
& sch
->opm
;
1121 /* Re-set some bits in the pmcw that were lost. */
1122 sch
->schib
.pmcw
.isc
= 3;
1123 sch
->schib
.pmcw
.csense
= 1;
1124 sch
->schib
.pmcw
.ena
= 0;
1125 if ((sch
->lpm
& (sch
->lpm
- 1)) != 0)
1126 sch
->schib
.pmcw
.mp
= 1;
1127 sch
->schib
.pmcw
.intparm
= (__u32
)(unsigned long)sch
;
1128 /* We should also udate ssd info, but this has to wait. */
1129 /* Check if this is another device which appeared on the same sch. */
1130 if (sch
->schib
.pmcw
.dev
!= cdev
->private->dev_id
.devno
) {
1131 PREPARE_WORK(&cdev
->private->kick_work
,
1132 ccw_device_move_to_orphanage
);
1133 queue_work(ccw_device_work
, &cdev
->private->kick_work
);
1135 ccw_device_start_id(cdev
, 0);
1139 ccw_device_offline_irq(struct ccw_device
*cdev
, enum dev_event dev_event
)
1141 struct subchannel
*sch
;
1143 sch
= to_subchannel(cdev
->dev
.parent
);
1145 * An interrupt in state offline means a previous disable was not
1146 * successful. Try again.
1148 cio_disable_subchannel(sch
);
1152 ccw_device_change_cmfstate(struct ccw_device
*cdev
, enum dev_event dev_event
)
1154 retry_set_schib(cdev
);
1155 cdev
->private->state
= DEV_STATE_ONLINE
;
1156 dev_fsm_event(cdev
, dev_event
);
1159 static void ccw_device_update_cmfblock(struct ccw_device
*cdev
,
1160 enum dev_event dev_event
)
1162 cmf_retry_copy_block(cdev
);
1163 cdev
->private->state
= DEV_STATE_ONLINE
;
1164 dev_fsm_event(cdev
, dev_event
);
1168 ccw_device_quiesce_done(struct ccw_device
*cdev
, enum dev_event dev_event
)
1170 ccw_device_set_timeout(cdev
, 0);
1171 if (dev_event
== DEV_EVENT_NOTOPER
)
1172 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1174 cdev
->private->state
= DEV_STATE_OFFLINE
;
1175 wake_up(&cdev
->private->wait_q
);
1179 ccw_device_quiesce_timeout(struct ccw_device
*cdev
, enum dev_event dev_event
)
1183 ret
= ccw_device_cancel_halt_clear(cdev
);
1186 cdev
->private->state
= DEV_STATE_OFFLINE
;
1187 wake_up(&cdev
->private->wait_q
);
1190 cdev
->private->state
= DEV_STATE_NOT_OPER
;
1191 wake_up(&cdev
->private->wait_q
);
1194 ccw_device_set_timeout(cdev
, HZ
/10);
1199 * No operation action. This is used e.g. to ignore a timeout event in
1203 ccw_device_nop(struct ccw_device
*cdev
, enum dev_event dev_event
)
1208 * Bug operation action.
1211 ccw_device_bug(struct ccw_device
*cdev
, enum dev_event dev_event
)
1213 printk(KERN_EMERG
"dev_jumptable[%i][%i] == NULL\n",
1214 cdev
->private->state
, dev_event
);
1219 * device statemachine
1221 fsm_func_t
*dev_jumptable
[NR_DEV_STATES
][NR_DEV_EVENTS
] = {
1222 [DEV_STATE_NOT_OPER
] = {
1223 [DEV_EVENT_NOTOPER
] = ccw_device_nop
,
1224 [DEV_EVENT_INTERRUPT
] = ccw_device_bug
,
1225 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1226 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1228 [DEV_STATE_SENSE_PGID
] = {
1229 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1230 [DEV_EVENT_INTERRUPT
] = ccw_device_sense_pgid_irq
,
1231 [DEV_EVENT_TIMEOUT
] = ccw_device_onoff_timeout
,
1232 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1234 [DEV_STATE_SENSE_ID
] = {
1235 [DEV_EVENT_NOTOPER
] = ccw_device_recog_notoper
,
1236 [DEV_EVENT_INTERRUPT
] = ccw_device_sense_id_irq
,
1237 [DEV_EVENT_TIMEOUT
] = ccw_device_recog_timeout
,
1238 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1240 [DEV_STATE_OFFLINE
] = {
1241 [DEV_EVENT_NOTOPER
] = ccw_device_offline_notoper
,
1242 [DEV_EVENT_INTERRUPT
] = ccw_device_offline_irq
,
1243 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1244 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1246 [DEV_STATE_VERIFY
] = {
1247 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1248 [DEV_EVENT_INTERRUPT
] = ccw_device_verify_irq
,
1249 [DEV_EVENT_TIMEOUT
] = ccw_device_onoff_timeout
,
1250 [DEV_EVENT_VERIFY
] = ccw_device_delay_verify
,
1252 [DEV_STATE_ONLINE
] = {
1253 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1254 [DEV_EVENT_INTERRUPT
] = ccw_device_irq
,
1255 [DEV_EVENT_TIMEOUT
] = ccw_device_online_timeout
,
1256 [DEV_EVENT_VERIFY
] = ccw_device_online_verify
,
1258 [DEV_STATE_W4SENSE
] = {
1259 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1260 [DEV_EVENT_INTERRUPT
] = ccw_device_w4sense
,
1261 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1262 [DEV_EVENT_VERIFY
] = ccw_device_online_verify
,
1264 [DEV_STATE_DISBAND_PGID
] = {
1265 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1266 [DEV_EVENT_INTERRUPT
] = ccw_device_disband_irq
,
1267 [DEV_EVENT_TIMEOUT
] = ccw_device_onoff_timeout
,
1268 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1270 [DEV_STATE_BOXED
] = {
1271 [DEV_EVENT_NOTOPER
] = ccw_device_offline_notoper
,
1272 [DEV_EVENT_INTERRUPT
] = ccw_device_stlck_done
,
1273 [DEV_EVENT_TIMEOUT
] = ccw_device_stlck_done
,
1274 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1276 /* states to wait for i/o completion before doing something */
1277 [DEV_STATE_CLEAR_VERIFY
] = {
1278 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1279 [DEV_EVENT_INTERRUPT
] = ccw_device_clear_verify
,
1280 [DEV_EVENT_TIMEOUT
] = ccw_device_nop
,
1281 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1283 [DEV_STATE_TIMEOUT_KILL
] = {
1284 [DEV_EVENT_NOTOPER
] = ccw_device_online_notoper
,
1285 [DEV_EVENT_INTERRUPT
] = ccw_device_killing_irq
,
1286 [DEV_EVENT_TIMEOUT
] = ccw_device_killing_timeout
,
1287 [DEV_EVENT_VERIFY
] = ccw_device_nop
, //FIXME
1289 [DEV_STATE_QUIESCE
] = {
1290 [DEV_EVENT_NOTOPER
] = ccw_device_quiesce_done
,
1291 [DEV_EVENT_INTERRUPT
] = ccw_device_quiesce_done
,
1292 [DEV_EVENT_TIMEOUT
] = ccw_device_quiesce_timeout
,
1293 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1295 /* special states for devices gone not operational */
1296 [DEV_STATE_DISCONNECTED
] = {
1297 [DEV_EVENT_NOTOPER
] = ccw_device_nop
,
1298 [DEV_EVENT_INTERRUPT
] = ccw_device_start_id
,
1299 [DEV_EVENT_TIMEOUT
] = ccw_device_bug
,
1300 [DEV_EVENT_VERIFY
] = ccw_device_start_id
,
1302 [DEV_STATE_DISCONNECTED_SENSE_ID
] = {
1303 [DEV_EVENT_NOTOPER
] = ccw_device_recog_notoper
,
1304 [DEV_EVENT_INTERRUPT
] = ccw_device_sense_id_irq
,
1305 [DEV_EVENT_TIMEOUT
] = ccw_device_recog_timeout
,
1306 [DEV_EVENT_VERIFY
] = ccw_device_nop
,
1308 [DEV_STATE_CMFCHANGE
] = {
1309 [DEV_EVENT_NOTOPER
] = ccw_device_change_cmfstate
,
1310 [DEV_EVENT_INTERRUPT
] = ccw_device_change_cmfstate
,
1311 [DEV_EVENT_TIMEOUT
] = ccw_device_change_cmfstate
,
1312 [DEV_EVENT_VERIFY
] = ccw_device_change_cmfstate
,
1314 [DEV_STATE_CMFUPDATE
] = {
1315 [DEV_EVENT_NOTOPER
] = ccw_device_update_cmfblock
,
1316 [DEV_EVENT_INTERRUPT
] = ccw_device_update_cmfblock
,
1317 [DEV_EVENT_TIMEOUT
] = ccw_device_update_cmfblock
,
1318 [DEV_EVENT_VERIFY
] = ccw_device_update_cmfblock
,
1323 * io_subchannel_irq is called for "real" interrupts or for status
1324 * pending conditions on msch.
1327 io_subchannel_irq (struct device
*pdev
)
1329 struct ccw_device
*cdev
;
1331 cdev
= to_subchannel(pdev
)->dev
.driver_data
;
1333 CIO_TRACE_EVENT (3, "IRQ");
1334 CIO_TRACE_EVENT (3, pdev
->bus_id
);
1336 dev_fsm_event(cdev
, DEV_EVENT_INTERRUPT
);
1339 EXPORT_SYMBOL_GPL(ccw_device_set_timeout
);