2 * drivers/s390/cio/device_fsm.c
3 * finite state machine for device handling
5 * Copyright IBM Corp. 2002,2008
6 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
7 * Martin Schwidefsky (schwidefsky@de.ibm.com)
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/jiffies.h>
13 #include <linux/string.h>
15 #include <asm/ccwdev.h>
17 #include <asm/chpid.h>
20 #include "cio_debug.h"
27 static int timeout_log_enabled;
29 static int __init ccw_timeout_log_setup(char *unused)
31 timeout_log_enabled = 1;
35 __setup("ccw_timeout_log", ccw_timeout_log_setup);
37 static void ccw_timeout_log(struct ccw_device *cdev)
40 struct subchannel *sch;
41 struct io_subchannel_private *private;
45 sch = to_subchannel(cdev->dev.parent);
46 private = to_io_private(sch);
48 cc = stsch(sch->schid, &schib);
50 printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
51 "device information:\n", get_clock());
52 printk(KERN_WARNING "cio: orb:\n");
53 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
54 orb, sizeof(*orb), 0);
55 printk(KERN_WARNING "cio: ccw device bus id: %s\n",
56 dev_name(&cdev->dev));
57 printk(KERN_WARNING "cio: subchannel bus id: %s\n",
59 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
60 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
63 printk(KERN_WARNING "cio: orb indicates transport mode\n");
64 printk(KERN_WARNING "cio: last tcw:\n");
65 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
66 (void *)(addr_t)orb->tm.tcw,
67 sizeof(struct tcw), 0);
69 printk(KERN_WARNING "cio: orb indicates command mode\n");
70 if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
71 (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
72 printk(KERN_WARNING "cio: last channel program "
75 printk(KERN_WARNING "cio: last channel program:\n");
77 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
78 (void *)(addr_t)orb->cmd.cpa,
79 sizeof(struct ccw1), 0);
81 printk(KERN_WARNING "cio: ccw device state: %d\n",
82 cdev->private->state);
83 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
84 printk(KERN_WARNING "cio: schib:\n");
85 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
86 &schib, sizeof(schib), 0);
87 printk(KERN_WARNING "cio: ccw device flags:\n");
88 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
89 &cdev->private->flags, sizeof(cdev->private->flags), 0);
93 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
96 ccw_device_timeout(unsigned long data)
98 struct ccw_device *cdev;
100 cdev = (struct ccw_device *) data;
101 spin_lock_irq(cdev->ccwlock);
102 if (timeout_log_enabled)
103 ccw_timeout_log(cdev);
104 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
105 spin_unlock_irq(cdev->ccwlock);
112 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
115 del_timer(&cdev->private->timer);
118 if (timer_pending(&cdev->private->timer)) {
119 if (mod_timer(&cdev->private->timer, jiffies + expires))
122 cdev->private->timer.function = ccw_device_timeout;
123 cdev->private->timer.data = (unsigned long) cdev;
124 cdev->private->timer.expires = jiffies + expires;
125 add_timer(&cdev->private->timer);
129 * Cancel running i/o. This is called repeatedly since halt/clear are
130 * asynchronous operations. We do one try with cio_cancel, two tries
131 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
132 * Returns 0 if device now idle, -ENODEV for device not operational and
133 * -EBUSY if an interrupt is expected (either from halt/clear or from a
137 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
139 struct subchannel *sch;
142 sch = to_subchannel(cdev->dev.parent);
143 if (cio_update_schib(sch))
145 if (!sch->schib.pmcw.ena)
146 /* Not operational -> done. */
148 /* Stage 1: cancel io. */
149 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
150 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
151 if (!scsw_is_tm(&sch->schib.scsw)) {
152 ret = cio_cancel(sch);
156 /* cancel io unsuccessful or not applicable (transport mode).
157 * Continue with asynchronous instructions. */
158 cdev->private->iretry = 3; /* 3 halt retries. */
160 if (!(scsw_actl(&sch->schib.scsw) & 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");
180 void ccw_device_update_sense_data(struct ccw_device *cdev)
182 memset(&cdev->id, 0, sizeof(cdev->id));
183 cdev->id.cu_type = cdev->private->senseid.cu_type;
184 cdev->id.cu_model = cdev->private->senseid.cu_model;
185 cdev->id.dev_type = cdev->private->senseid.dev_type;
186 cdev->id.dev_model = cdev->private->senseid.dev_model;
189 int ccw_device_test_sense_data(struct ccw_device *cdev)
191 return 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;
198 * The machine won't give us any notification by machine check if a chpid has
199 * been varied online on the SE so we have to find out by magic (i. e. driving
200 * the channel subsystem to device selection and updating our path masks).
203 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
209 for (i = 0; i<8; i++) {
211 if (!(sch->lpm & mask))
215 chpid.id = sch->schib.pmcw.chpid[i];
216 if (!chp_is_registered(chpid))
217 css_schedule_eval_all();
222 * Stop device recognition.
225 ccw_device_recog_done(struct ccw_device *cdev, int state)
227 struct subchannel *sch;
230 sch = to_subchannel(cdev->dev.parent);
232 ccw_device_set_timeout(cdev, 0);
233 cio_disable_subchannel(sch);
235 * Now that we tried recognition, we have performed device selection
236 * through ssch() and the path information is up to date.
240 /* Check since device may again have become not operational. */
241 if (cio_update_schib(sch))
242 state = DEV_STATE_NOT_OPER;
244 sch->lpm = sch->schib.pmcw.pam & sch->opm;
246 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
247 /* Force reprobe on all chpids. */
249 if (sch->lpm != old_lpm)
250 __recover_lost_chpids(sch, old_lpm);
251 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
252 (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
253 cdev->private->flags.recog_done = 1;
254 cdev->private->state = DEV_STATE_DISCONNECTED;
255 wake_up(&cdev->private->wait_q);
258 if (cdev->private->flags.resuming) {
259 cdev->private->state = state;
260 cdev->private->flags.recog_done = 1;
261 wake_up(&cdev->private->wait_q);
265 case DEV_STATE_NOT_OPER:
266 CIO_MSG_EVENT(2, "SenseID : unknown device %04x on "
267 "subchannel 0.%x.%04x\n",
268 cdev->private->dev_id.devno,
269 sch->schid.ssid, sch->schid.sch_no);
271 case DEV_STATE_OFFLINE:
273 ccw_device_update_sense_data(cdev);
274 /* Issue device info message. */
275 CIO_MSG_EVENT(4, "SenseID : device 0.%x.%04x reports: "
276 "CU Type/Mod = %04X/%02X, Dev Type/Mod "
278 cdev->private->dev_id.ssid,
279 cdev->private->dev_id.devno,
280 cdev->id.cu_type, cdev->id.cu_model,
281 cdev->id.dev_type, cdev->id.dev_model);
284 cdev->private->state = DEV_STATE_OFFLINE;
285 cdev->private->flags.recog_done = 1;
286 if (ccw_device_test_sense_data(cdev)) {
287 cdev->private->flags.donotify = 1;
288 ccw_device_online(cdev);
289 wake_up(&cdev->private->wait_q);
291 ccw_device_update_sense_data(cdev);
292 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
295 case DEV_STATE_BOXED:
296 CIO_MSG_EVENT(0, "SenseID : boxed device %04x on "
297 " subchannel 0.%x.%04x\n",
298 cdev->private->dev_id.devno,
299 sch->schid.ssid, sch->schid.sch_no);
300 if (cdev->id.cu_type != 0) { /* device was recognized before */
301 cdev->private->flags.recog_done = 1;
302 cdev->private->state = DEV_STATE_BOXED;
303 wake_up(&cdev->private->wait_q);
308 cdev->private->state = state;
309 io_subchannel_recog_done(cdev);
310 wake_up(&cdev->private->wait_q);
314 * Function called from device_id.c after sense id has completed.
317 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
321 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
323 case -ETIME: /* Sense id stopped by timeout. */
324 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
327 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
332 int ccw_device_notify(struct ccw_device *cdev, int event)
338 CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
339 cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
341 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
344 static void ccw_device_oper_notify(struct ccw_device *cdev)
346 if (ccw_device_notify(cdev, CIO_OPER)) {
347 /* Reenable channel measurements, if needed. */
348 ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
351 /* Driver doesn't want device back. */
352 ccw_device_set_notoper(cdev);
353 ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
357 * Finished with online/offline processing.
360 ccw_device_done(struct ccw_device *cdev, int state)
362 struct subchannel *sch;
364 sch = to_subchannel(cdev->dev.parent);
366 ccw_device_set_timeout(cdev, 0);
368 if (state != DEV_STATE_ONLINE)
369 cio_disable_subchannel(sch);
371 /* Reset device status. */
372 memset(&cdev->private->irb, 0, sizeof(struct irb));
374 cdev->private->state = state;
377 case DEV_STATE_BOXED:
378 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
379 cdev->private->dev_id.devno, sch->schid.sch_no);
380 if (cdev->online && !ccw_device_notify(cdev, CIO_BOXED))
381 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
382 cdev->private->flags.donotify = 0;
384 case DEV_STATE_NOT_OPER:
385 CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
386 cdev->private->dev_id.devno, sch->schid.sch_no);
387 if (!ccw_device_notify(cdev, CIO_GONE))
388 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
390 ccw_device_set_disconnected(cdev);
391 cdev->private->flags.donotify = 0;
393 case DEV_STATE_DISCONNECTED:
394 CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
395 "%04x\n", cdev->private->dev_id.devno,
397 if (!ccw_device_notify(cdev, CIO_NO_PATH))
398 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
400 ccw_device_set_disconnected(cdev);
401 cdev->private->flags.donotify = 0;
407 if (cdev->private->flags.donotify) {
408 cdev->private->flags.donotify = 0;
409 ccw_device_oper_notify(cdev);
411 wake_up(&cdev->private->wait_q);
414 static int cmp_pgid(struct pgid *p1, struct pgid *p2)
422 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
425 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
431 for (i = 0; i < 8; i++) {
432 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
435 if (cdev->private->pgid[last].inf.ps.state1 ==
437 /* First non-zero PGID */
441 if (cmp_pgid(&cdev->private->pgid[i],
442 &cdev->private->pgid[last]) == 0)
443 /* Non-conflicting PGIDs */
446 /* PGID mismatch, can't pathgroup. */
447 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
448 "0.%x.%04x, can't pathgroup\n",
449 cdev->private->dev_id.ssid,
450 cdev->private->dev_id.devno);
451 cdev->private->options.pgroup = 0;
454 if (cdev->private->pgid[last].inf.ps.state1 ==
456 /* No previous pgid found */
457 memcpy(&cdev->private->pgid[0],
458 &channel_subsystems[0]->global_pgid,
459 sizeof(struct pgid));
461 /* Use existing pgid */
462 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
463 sizeof(struct pgid));
467 * Function called from device_pgid.c after sense path ground has completed.
470 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
472 struct subchannel *sch;
474 sch = to_subchannel(cdev->dev.parent);
476 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
477 cdev->private->options.pgroup = 0;
479 case 0: /* success */
480 case -EACCES: /* partial success, some paths not operational */
481 /* Check if all pgids are equal or 0. */
482 __ccw_device_get_common_pgid(cdev);
484 case -ETIME: /* Sense path group id stopped by timeout. */
485 case -EUSERS: /* device is reserved for someone else. */
486 ccw_device_done(cdev, DEV_STATE_BOXED);
489 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
492 /* Start Path Group verification. */
493 cdev->private->state = DEV_STATE_VERIFY;
494 cdev->private->flags.doverify = 0;
495 ccw_device_verify_start(cdev);
499 * Start device recognition.
502 ccw_device_recognition(struct ccw_device *cdev)
504 struct subchannel *sch;
507 sch = to_subchannel(cdev->dev.parent);
508 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
510 /* Couldn't enable the subchannel for i/o. Sick device. */
513 /* After 60s the device recognition is considered to have failed. */
514 ccw_device_set_timeout(cdev, 60*HZ);
517 * We used to start here with a sense pgid to find out whether a device
518 * is locked by someone else. Unfortunately, the sense pgid command
519 * code has other meanings on devices predating the path grouping
520 * algorithm, so we start with sense id and box the device after an
521 * timeout (or if sense pgid during path verification detects the device
522 * is locked, as may happen on newer devices).
524 cdev->private->flags.recog_done = 0;
525 cdev->private->state = DEV_STATE_SENSE_ID;
526 ccw_device_sense_id_start(cdev);
531 * Handle timeout in device recognition.
534 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
538 ret = ccw_device_cancel_halt_clear(cdev);
541 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
544 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
547 ccw_device_set_timeout(cdev, 3*HZ);
553 ccw_device_verify_done(struct ccw_device *cdev, int err)
555 struct subchannel *sch;
557 sch = to_subchannel(cdev->dev.parent);
558 /* Update schib - pom may have changed. */
559 if (cio_update_schib(sch)) {
560 cdev->private->flags.donotify = 0;
561 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
564 /* Update lpm with verified path mask. */
566 /* Repeat path verification? */
567 if (cdev->private->flags.doverify) {
568 cdev->private->flags.doverify = 0;
569 ccw_device_verify_start(cdev);
573 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
574 cdev->private->options.pgroup = 0;
576 ccw_device_done(cdev, DEV_STATE_ONLINE);
577 /* Deliver fake irb to device driver, if needed. */
578 if (cdev->private->flags.fake_irb) {
579 memset(&cdev->private->irb, 0, sizeof(struct irb));
580 cdev->private->irb.scsw.cmd.cc = 1;
581 cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
582 cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
583 cdev->private->irb.scsw.cmd.stctl =
584 SCSW_STCTL_STATUS_PEND;
585 cdev->private->flags.fake_irb = 0;
587 cdev->handler(cdev, cdev->private->intparm,
588 &cdev->private->irb);
589 memset(&cdev->private->irb, 0, sizeof(struct irb));
593 /* Reset oper notify indication after verify error. */
594 cdev->private->flags.donotify = 0;
595 ccw_device_done(cdev, DEV_STATE_BOXED);
598 /* Reset oper notify indication after verify error. */
599 cdev->private->flags.donotify = 0;
601 ccw_device_set_timeout(cdev, 0);
602 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
604 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
613 ccw_device_online(struct ccw_device *cdev)
615 struct subchannel *sch;
618 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
619 (cdev->private->state != DEV_STATE_BOXED))
621 sch = to_subchannel(cdev->dev.parent);
622 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
624 /* Couldn't enable the subchannel for i/o. Sick device. */
626 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
629 /* Do we want to do path grouping? */
630 if (!cdev->private->options.pgroup) {
631 /* Start initial path verification. */
632 cdev->private->state = DEV_STATE_VERIFY;
633 cdev->private->flags.doverify = 0;
634 ccw_device_verify_start(cdev);
637 /* Do a SensePGID first. */
638 cdev->private->state = DEV_STATE_SENSE_PGID;
639 ccw_device_sense_pgid_start(cdev);
644 ccw_device_disband_done(struct ccw_device *cdev, int err)
648 ccw_device_done(cdev, DEV_STATE_OFFLINE);
651 ccw_device_done(cdev, DEV_STATE_BOXED);
654 cdev->private->flags.donotify = 0;
655 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
656 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
665 ccw_device_offline(struct ccw_device *cdev)
667 struct subchannel *sch;
669 /* Allow ccw_device_offline while disconnected. */
670 if (cdev->private->state == DEV_STATE_DISCONNECTED ||
671 cdev->private->state == DEV_STATE_NOT_OPER) {
672 cdev->private->flags.donotify = 0;
673 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
676 if (cdev->private->state == DEV_STATE_BOXED) {
677 ccw_device_done(cdev, DEV_STATE_BOXED);
680 if (ccw_device_is_orphan(cdev)) {
681 ccw_device_done(cdev, DEV_STATE_OFFLINE);
684 sch = to_subchannel(cdev->dev.parent);
685 if (cio_update_schib(sch))
687 if (scsw_actl(&sch->schib.scsw) != 0)
689 if (cdev->private->state != DEV_STATE_ONLINE)
691 /* Are we doing path grouping? */
692 if (!cdev->private->options.pgroup) {
693 /* No, set state offline immediately. */
694 ccw_device_done(cdev, DEV_STATE_OFFLINE);
697 /* Start Set Path Group commands. */
698 cdev->private->state = DEV_STATE_DISBAND_PGID;
699 ccw_device_disband_start(cdev);
704 * Handle timeout in device online/offline process.
707 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
711 ret = ccw_device_cancel_halt_clear(cdev);
714 ccw_device_done(cdev, DEV_STATE_BOXED);
717 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
720 ccw_device_set_timeout(cdev, 3*HZ);
725 * Handle not oper event in device recognition.
728 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
730 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
734 * Handle not operational event in non-special state.
736 static void ccw_device_generic_notoper(struct ccw_device *cdev,
737 enum dev_event dev_event)
739 if (!ccw_device_notify(cdev, CIO_GONE))
740 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
742 ccw_device_set_disconnected(cdev);
746 * Handle path verification event in offline state.
748 static void ccw_device_offline_verify(struct ccw_device *cdev,
749 enum dev_event dev_event)
751 struct subchannel *sch = to_subchannel(cdev->dev.parent);
753 css_schedule_eval(sch->schid);
757 * Handle path verification event.
760 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
762 struct subchannel *sch;
764 if (cdev->private->state == DEV_STATE_W4SENSE) {
765 cdev->private->flags.doverify = 1;
768 sch = to_subchannel(cdev->dev.parent);
770 * Since we might not just be coming from an interrupt from the
771 * subchannel we have to update the schib.
773 if (cio_update_schib(sch)) {
774 ccw_device_verify_done(cdev, -ENODEV);
778 if (scsw_actl(&sch->schib.scsw) != 0 ||
779 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
780 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
782 * No final status yet or final status not yet delivered
783 * to the device driver. Can't do path verfication now,
784 * delay until final status was delivered.
786 cdev->private->flags.doverify = 1;
789 /* Device is idle, we can do the path verification. */
790 cdev->private->state = DEV_STATE_VERIFY;
791 cdev->private->flags.doverify = 0;
792 ccw_device_verify_start(cdev);
796 * Got an interrupt for a normal io (state online).
799 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
804 irb = (struct irb *) __LC_IRB;
805 is_cmd = !scsw_is_tm(&irb->scsw);
806 /* Check for unsolicited interrupt. */
807 if (!scsw_is_solicited(&irb->scsw)) {
808 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
809 !irb->esw.esw0.erw.cons) {
810 /* Unit check but no sense data. Need basic sense. */
811 if (ccw_device_do_sense(cdev, irb) != 0)
812 goto call_handler_unsol;
813 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
814 cdev->private->state = DEV_STATE_W4SENSE;
815 cdev->private->intparm = 0;
820 cdev->handler (cdev, 0, irb);
821 if (cdev->private->flags.doverify)
822 ccw_device_online_verify(cdev, 0);
825 /* Accumulate status and find out if a basic sense is needed. */
826 ccw_device_accumulate_irb(cdev, irb);
827 if (is_cmd && cdev->private->flags.dosense) {
828 if (ccw_device_do_sense(cdev, irb) == 0) {
829 cdev->private->state = DEV_STATE_W4SENSE;
833 /* Call the handler. */
834 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
835 /* Start delayed path verification. */
836 ccw_device_online_verify(cdev, 0);
840 * Got an timeout in online state.
843 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
847 ccw_device_set_timeout(cdev, 0);
848 ret = ccw_device_cancel_halt_clear(cdev);
850 ccw_device_set_timeout(cdev, 3*HZ);
851 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
855 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
856 else if (cdev->handler)
857 cdev->handler(cdev, cdev->private->intparm,
858 ERR_PTR(-ETIMEDOUT));
862 * Got an interrupt for a basic sense.
865 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
869 irb = (struct irb *) __LC_IRB;
870 /* Check for unsolicited interrupt. */
871 if (scsw_stctl(&irb->scsw) ==
872 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
873 if (scsw_cc(&irb->scsw) == 1)
874 /* Basic sense hasn't started. Try again. */
875 ccw_device_do_sense(cdev, irb);
877 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
878 "interrupt during w4sense...\n",
879 cdev->private->dev_id.ssid,
880 cdev->private->dev_id.devno);
882 cdev->handler (cdev, 0, irb);
887 * Check if a halt or clear has been issued in the meanwhile. If yes,
888 * only deliver the halt/clear interrupt to the device driver as if it
889 * had killed the original request.
891 if (scsw_fctl(&irb->scsw) &
892 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
893 /* Retry Basic Sense if requested. */
894 if (cdev->private->flags.intretry) {
895 cdev->private->flags.intretry = 0;
896 ccw_device_do_sense(cdev, irb);
899 cdev->private->flags.dosense = 0;
900 memset(&cdev->private->irb, 0, sizeof(struct irb));
901 ccw_device_accumulate_irb(cdev, irb);
904 /* Add basic sense info to irb. */
905 ccw_device_accumulate_basic_sense(cdev, irb);
906 if (cdev->private->flags.dosense) {
907 /* Another basic sense is needed. */
908 ccw_device_do_sense(cdev, irb);
912 cdev->private->state = DEV_STATE_ONLINE;
913 /* In case sensing interfered with setting the device online */
914 wake_up(&cdev->private->wait_q);
915 /* Call the handler. */
916 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
917 /* Start delayed path verification. */
918 ccw_device_online_verify(cdev, 0);
922 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
926 irb = (struct irb *) __LC_IRB;
927 /* Accumulate status. We don't do basic sense. */
928 ccw_device_accumulate_irb(cdev, irb);
929 /* Remember to clear irb to avoid residuals. */
930 memset(&cdev->private->irb, 0, sizeof(struct irb));
931 /* Try to start delayed device verification. */
932 ccw_device_online_verify(cdev, 0);
933 /* Note: Don't call handler for cio initiated clear! */
937 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
939 struct subchannel *sch;
941 sch = to_subchannel(cdev->dev.parent);
942 ccw_device_set_timeout(cdev, 0);
943 /* Start delayed path verification. */
944 ccw_device_online_verify(cdev, 0);
945 /* OK, i/o is dead now. Call interrupt handler. */
947 cdev->handler(cdev, cdev->private->intparm,
952 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
956 ret = ccw_device_cancel_halt_clear(cdev);
958 ccw_device_set_timeout(cdev, 3*HZ);
961 /* Start delayed path verification. */
962 ccw_device_online_verify(cdev, 0);
964 cdev->handler(cdev, cdev->private->intparm,
968 void ccw_device_kill_io(struct ccw_device *cdev)
972 ret = ccw_device_cancel_halt_clear(cdev);
974 ccw_device_set_timeout(cdev, 3*HZ);
975 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
978 /* Start delayed path verification. */
979 ccw_device_online_verify(cdev, 0);
981 cdev->handler(cdev, cdev->private->intparm,
986 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
988 /* Start verification after current task finished. */
989 cdev->private->flags.doverify = 1;
993 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
998 case DEV_EVENT_INTERRUPT:
999 irb = (struct irb *) __LC_IRB;
1000 /* Check for unsolicited interrupt. */
1001 if ((scsw_stctl(&irb->scsw) ==
1002 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1003 (!scsw_cc(&irb->scsw)))
1004 /* FIXME: we should restart stlck here, but this
1005 * is extremely unlikely ... */
1008 ccw_device_accumulate_irb(cdev, irb);
1009 /* We don't care about basic sense etc. */
1011 default: /* timeout */
1015 wake_up(&cdev->private->wait_q);
1019 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1021 struct subchannel *sch;
1023 sch = to_subchannel(cdev->dev.parent);
1024 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
1025 /* Couldn't enable the subchannel for i/o. Sick device. */
1028 /* After 60s the device recognition is considered to have failed. */
1029 ccw_device_set_timeout(cdev, 60*HZ);
1031 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1032 ccw_device_sense_id_start(cdev);
1035 void ccw_device_trigger_reprobe(struct ccw_device *cdev)
1037 struct subchannel *sch;
1039 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1042 sch = to_subchannel(cdev->dev.parent);
1043 /* Update some values. */
1044 if (cio_update_schib(sch))
1047 * The pim, pam, pom values may not be accurate, but they are the best
1048 * we have before performing device selection :/
1050 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1052 * Use the initial configuration since we can't be shure that the old
1055 io_subchannel_init_config(sch);
1056 if (cio_commit_config(sch))
1059 /* We should also udate ssd info, but this has to wait. */
1060 /* Check if this is another device which appeared on the same sch. */
1061 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
1062 css_schedule_eval(sch->schid);
1064 ccw_device_start_id(cdev, 0);
1068 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1070 struct subchannel *sch;
1072 sch = to_subchannel(cdev->dev.parent);
1074 * An interrupt in state offline means a previous disable was not
1075 * successful - should not happen, but we try to disable again.
1077 cio_disable_subchannel(sch);
1081 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1083 retry_set_schib(cdev);
1084 cdev->private->state = DEV_STATE_ONLINE;
1085 dev_fsm_event(cdev, dev_event);
1088 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1089 enum dev_event dev_event)
1091 cmf_retry_copy_block(cdev);
1092 cdev->private->state = DEV_STATE_ONLINE;
1093 dev_fsm_event(cdev, dev_event);
1097 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1099 ccw_device_set_timeout(cdev, 0);
1100 if (dev_event == DEV_EVENT_NOTOPER)
1101 cdev->private->state = DEV_STATE_NOT_OPER;
1103 cdev->private->state = DEV_STATE_OFFLINE;
1104 wake_up(&cdev->private->wait_q);
1108 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1112 ret = ccw_device_cancel_halt_clear(cdev);
1115 cdev->private->state = DEV_STATE_OFFLINE;
1116 wake_up(&cdev->private->wait_q);
1119 cdev->private->state = DEV_STATE_NOT_OPER;
1120 wake_up(&cdev->private->wait_q);
1123 ccw_device_set_timeout(cdev, HZ/10);
1128 * No operation action. This is used e.g. to ignore a timeout event in
1132 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1137 * Bug operation action.
1140 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1142 CIO_MSG_EVENT(0, "Internal state [%i][%i] not handled for device "
1143 "0.%x.%04x\n", cdev->private->state, dev_event,
1144 cdev->private->dev_id.ssid,
1145 cdev->private->dev_id.devno);
1150 * device statemachine
1152 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1153 [DEV_STATE_NOT_OPER] = {
1154 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1155 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1156 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1157 [DEV_EVENT_VERIFY] = ccw_device_nop,
1159 [DEV_STATE_SENSE_PGID] = {
1160 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1161 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1162 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1163 [DEV_EVENT_VERIFY] = ccw_device_nop,
1165 [DEV_STATE_SENSE_ID] = {
1166 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1167 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1168 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1169 [DEV_EVENT_VERIFY] = ccw_device_nop,
1171 [DEV_STATE_OFFLINE] = {
1172 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1173 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1174 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1175 [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
1177 [DEV_STATE_VERIFY] = {
1178 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1179 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1180 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1181 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1183 [DEV_STATE_ONLINE] = {
1184 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1185 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1186 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1187 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1189 [DEV_STATE_W4SENSE] = {
1190 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1191 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1192 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1193 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1195 [DEV_STATE_DISBAND_PGID] = {
1196 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1197 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1198 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1199 [DEV_EVENT_VERIFY] = ccw_device_nop,
1201 [DEV_STATE_BOXED] = {
1202 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1203 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1204 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1205 [DEV_EVENT_VERIFY] = ccw_device_nop,
1207 /* states to wait for i/o completion before doing something */
1208 [DEV_STATE_CLEAR_VERIFY] = {
1209 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1210 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1211 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1212 [DEV_EVENT_VERIFY] = ccw_device_nop,
1214 [DEV_STATE_TIMEOUT_KILL] = {
1215 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1216 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1217 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1218 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1220 [DEV_STATE_QUIESCE] = {
1221 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1222 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1223 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1224 [DEV_EVENT_VERIFY] = ccw_device_nop,
1226 /* special states for devices gone not operational */
1227 [DEV_STATE_DISCONNECTED] = {
1228 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1229 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1230 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1231 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1233 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1234 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1235 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1236 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1237 [DEV_EVENT_VERIFY] = ccw_device_nop,
1239 [DEV_STATE_CMFCHANGE] = {
1240 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1241 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1242 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1243 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1245 [DEV_STATE_CMFUPDATE] = {
1246 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1247 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1248 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1249 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1253 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);