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", cdev->dev.bus_id);
56 printk(KERN_WARNING "cio: subchannel bus id: %s\n", sch->dev.bus_id);
57 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
58 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
61 printk(KERN_WARNING "cio: orb indicates transport mode\n");
62 printk(KERN_WARNING "cio: last tcw:\n");
63 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
64 (void *)(addr_t)orb->tm.tcw,
65 sizeof(struct tcw), 0);
67 printk(KERN_WARNING "cio: orb indicates command mode\n");
68 if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
69 (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
70 printk(KERN_WARNING "cio: last channel program "
73 printk(KERN_WARNING "cio: last channel program:\n");
75 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
76 (void *)(addr_t)orb->cmd.cpa,
77 sizeof(struct ccw1), 0);
79 printk(KERN_WARNING "cio: ccw device state: %d\n",
80 cdev->private->state);
81 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
82 printk(KERN_WARNING "cio: schib:\n");
83 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
84 &schib, sizeof(schib), 0);
85 printk(KERN_WARNING "cio: ccw device flags:\n");
86 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
87 &cdev->private->flags, sizeof(cdev->private->flags), 0);
91 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
94 ccw_device_timeout(unsigned long data)
96 struct ccw_device *cdev;
98 cdev = (struct ccw_device *) data;
99 spin_lock_irq(cdev->ccwlock);
100 if (timeout_log_enabled)
101 ccw_timeout_log(cdev);
102 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
103 spin_unlock_irq(cdev->ccwlock);
110 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
113 del_timer(&cdev->private->timer);
116 if (timer_pending(&cdev->private->timer)) {
117 if (mod_timer(&cdev->private->timer, jiffies + expires))
120 cdev->private->timer.function = ccw_device_timeout;
121 cdev->private->timer.data = (unsigned long) cdev;
122 cdev->private->timer.expires = jiffies + expires;
123 add_timer(&cdev->private->timer);
127 * Cancel running i/o. This is called repeatedly since halt/clear are
128 * asynchronous operations. We do one try with cio_cancel, two tries
129 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
130 * Returns 0 if device now idle, -ENODEV for device not operational and
131 * -EBUSY if an interrupt is expected (either from halt/clear or from a
135 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
137 struct subchannel *sch;
140 sch = to_subchannel(cdev->dev.parent);
141 ret = stsch(sch->schid, &sch->schib);
142 if (ret || !sch->schib.pmcw.dnv)
144 if (!sch->schib.pmcw.ena)
145 /* Not operational -> done. */
147 /* Stage 1: cancel io. */
148 if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
149 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
150 if (!scsw_is_tm(&sch->schib.scsw)) {
151 ret = cio_cancel(sch);
155 /* cancel io unsuccessful or not applicable (transport mode).
156 * Continue with asynchronous instructions. */
157 cdev->private->iretry = 3; /* 3 halt retries. */
159 if (!(scsw_actl(&sch->schib.scsw) & 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)
216 for (i = 0; i<8; i++) {
218 if (!(sch->lpm & mask))
222 chpid.id = sch->schib.pmcw.chpid[i];
223 if (!chp_is_registered(chpid))
224 css_schedule_eval_all();
229 * Stop device recognition.
232 ccw_device_recog_done(struct ccw_device *cdev, int state)
234 struct subchannel *sch;
235 int notify, old_lpm, same_dev;
237 sch = to_subchannel(cdev->dev.parent);
239 ccw_device_set_timeout(cdev, 0);
240 cio_disable_subchannel(sch);
242 * Now that we tried recognition, we have performed device selection
243 * through ssch() and the path information is up to date.
246 stsch(sch->schid, &sch->schib);
247 sch->lpm = sch->schib.pmcw.pam & sch->opm;
248 /* Check since device may again have become not operational. */
249 if (!sch->schib.pmcw.dnv)
250 state = DEV_STATE_NOT_OPER;
251 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
252 /* Force reprobe on all chpids. */
254 if (sch->lpm != old_lpm)
255 __recover_lost_chpids(sch, old_lpm);
256 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
257 if (state == DEV_STATE_NOT_OPER) {
258 cdev->private->flags.recog_done = 1;
259 cdev->private->state = DEV_STATE_DISCONNECTED;
262 /* Boxed devices don't need extra treatment. */
265 same_dev = 0; /* Keep the compiler quiet... */
267 case DEV_STATE_NOT_OPER:
268 CIO_MSG_EVENT(2, "SenseID : unknown device %04x on "
269 "subchannel 0.%x.%04x\n",
270 cdev->private->dev_id.devno,
271 sch->schid.ssid, sch->schid.sch_no);
273 case DEV_STATE_OFFLINE:
274 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
275 same_dev = ccw_device_handle_oper(cdev);
278 /* fill out sense information */
279 memset(&cdev->id, 0, sizeof(cdev->id));
280 cdev->id.cu_type = cdev->private->senseid.cu_type;
281 cdev->id.cu_model = cdev->private->senseid.cu_model;
282 cdev->id.dev_type = cdev->private->senseid.dev_type;
283 cdev->id.dev_model = cdev->private->senseid.dev_model;
285 cdev->private->state = DEV_STATE_OFFLINE;
287 /* Get device online again. */
288 ccw_device_online(cdev);
289 wake_up(&cdev->private->wait_q);
293 /* Issue device info message. */
294 CIO_MSG_EVENT(4, "SenseID : device 0.%x.%04x reports: "
295 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
297 cdev->private->dev_id.ssid,
298 cdev->private->dev_id.devno,
299 cdev->id.cu_type, cdev->id.cu_model,
300 cdev->id.dev_type, cdev->id.dev_model);
302 case DEV_STATE_BOXED:
303 CIO_MSG_EVENT(0, "SenseID : boxed device %04x on "
304 " subchannel 0.%x.%04x\n",
305 cdev->private->dev_id.devno,
306 sch->schid.ssid, sch->schid.sch_no);
309 cdev->private->state = state;
310 io_subchannel_recog_done(cdev);
311 if (state != DEV_STATE_NOT_OPER)
312 wake_up(&cdev->private->wait_q);
316 * Function called from device_id.c after sense id has completed.
319 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
323 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
325 case -ETIME: /* Sense id stopped by timeout. */
326 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
329 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
334 int ccw_device_notify(struct ccw_device *cdev, int event)
340 return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
344 ccw_device_oper_notify(struct work_struct *work)
346 struct ccw_device_private *priv;
347 struct ccw_device *cdev;
351 priv = container_of(work, struct ccw_device_private, kick_work);
353 ret = ccw_device_notify(cdev, CIO_OPER);
354 spin_lock_irqsave(cdev->ccwlock, flags);
356 /* Reenable channel measurements, if needed. */
357 spin_unlock_irqrestore(cdev->ccwlock, flags);
359 spin_lock_irqsave(cdev->ccwlock, flags);
360 wake_up(&cdev->private->wait_q);
362 spin_unlock_irqrestore(cdev->ccwlock, flags);
364 /* Driver doesn't want device back. */
365 ccw_device_do_unreg_rereg(work);
369 * Finished with online/offline processing.
372 ccw_device_done(struct ccw_device *cdev, int state)
374 struct subchannel *sch;
376 sch = to_subchannel(cdev->dev.parent);
378 ccw_device_set_timeout(cdev, 0);
380 if (state != DEV_STATE_ONLINE)
381 cio_disable_subchannel(sch);
383 /* Reset device status. */
384 memset(&cdev->private->irb, 0, sizeof(struct irb));
386 cdev->private->state = state;
389 if (state == DEV_STATE_BOXED)
390 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
391 cdev->private->dev_id.devno, sch->schid.sch_no);
393 if (cdev->private->flags.donotify) {
394 cdev->private->flags.donotify = 0;
395 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
396 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
398 wake_up(&cdev->private->wait_q);
400 if (css_init_done && state != DEV_STATE_ONLINE)
401 put_device (&cdev->dev);
404 static int cmp_pgid(struct pgid *p1, struct pgid *p2)
412 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
415 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
421 for (i = 0; i < 8; i++) {
422 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
425 if (cdev->private->pgid[last].inf.ps.state1 ==
427 /* First non-zero PGID */
431 if (cmp_pgid(&cdev->private->pgid[i],
432 &cdev->private->pgid[last]) == 0)
433 /* Non-conflicting PGIDs */
436 /* PGID mismatch, can't pathgroup. */
437 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
438 "0.%x.%04x, can't pathgroup\n",
439 cdev->private->dev_id.ssid,
440 cdev->private->dev_id.devno);
441 cdev->private->options.pgroup = 0;
444 if (cdev->private->pgid[last].inf.ps.state1 ==
446 /* No previous pgid found */
447 memcpy(&cdev->private->pgid[0],
448 &channel_subsystems[0]->global_pgid,
449 sizeof(struct pgid));
451 /* Use existing pgid */
452 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
453 sizeof(struct pgid));
457 * Function called from device_pgid.c after sense path ground has completed.
460 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
462 struct subchannel *sch;
464 sch = to_subchannel(cdev->dev.parent);
466 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
467 cdev->private->options.pgroup = 0;
469 case 0: /* success */
470 case -EACCES: /* partial success, some paths not operational */
471 /* Check if all pgids are equal or 0. */
472 __ccw_device_get_common_pgid(cdev);
474 case -ETIME: /* Sense path group id stopped by timeout. */
475 case -EUSERS: /* device is reserved for someone else. */
476 ccw_device_done(cdev, DEV_STATE_BOXED);
479 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
482 /* Start Path Group verification. */
483 cdev->private->state = DEV_STATE_VERIFY;
484 cdev->private->flags.doverify = 0;
485 ccw_device_verify_start(cdev);
489 * Start device recognition.
492 ccw_device_recognition(struct ccw_device *cdev)
494 struct subchannel *sch;
497 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
498 (cdev->private->state != DEV_STATE_BOXED))
500 sch = to_subchannel(cdev->dev.parent);
501 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
503 /* Couldn't enable the subchannel for i/o. Sick device. */
506 /* After 60s the device recognition is considered to have failed. */
507 ccw_device_set_timeout(cdev, 60*HZ);
510 * We used to start here with a sense pgid to find out whether a device
511 * is locked by someone else. Unfortunately, the sense pgid command
512 * code has other meanings on devices predating the path grouping
513 * algorithm, so we start with sense id and box the device after an
514 * timeout (or if sense pgid during path verification detects the device
515 * is locked, as may happen on newer devices).
517 cdev->private->flags.recog_done = 0;
518 cdev->private->state = DEV_STATE_SENSE_ID;
519 ccw_device_sense_id_start(cdev);
524 * Handle timeout in device recognition.
527 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
531 ret = ccw_device_cancel_halt_clear(cdev);
534 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
537 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
540 ccw_device_set_timeout(cdev, 3*HZ);
546 ccw_device_verify_done(struct ccw_device *cdev, int err)
548 struct subchannel *sch;
550 sch = to_subchannel(cdev->dev.parent);
551 /* Update schib - pom may have changed. */
552 stsch(sch->schid, &sch->schib);
553 /* Update lpm with verified path mask. */
555 /* Repeat path verification? */
556 if (cdev->private->flags.doverify) {
557 cdev->private->flags.doverify = 0;
558 ccw_device_verify_start(cdev);
562 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
563 cdev->private->options.pgroup = 0;
565 ccw_device_done(cdev, DEV_STATE_ONLINE);
566 /* Deliver fake irb to device driver, if needed. */
567 if (cdev->private->flags.fake_irb) {
568 memset(&cdev->private->irb, 0, sizeof(struct irb));
569 cdev->private->irb.scsw.cmd.cc = 1;
570 cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
571 cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
572 cdev->private->irb.scsw.cmd.stctl =
573 SCSW_STCTL_STATUS_PEND;
574 cdev->private->flags.fake_irb = 0;
576 cdev->handler(cdev, cdev->private->intparm,
577 &cdev->private->irb);
578 memset(&cdev->private->irb, 0, sizeof(struct irb));
582 /* Reset oper notify indication after verify error. */
583 cdev->private->flags.donotify = 0;
584 ccw_device_done(cdev, DEV_STATE_BOXED);
587 /* Reset oper notify indication after verify error. */
588 cdev->private->flags.donotify = 0;
590 ccw_device_set_timeout(cdev, 0);
591 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
593 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
602 ccw_device_online(struct ccw_device *cdev)
604 struct subchannel *sch;
607 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
608 (cdev->private->state != DEV_STATE_BOXED))
610 sch = to_subchannel(cdev->dev.parent);
611 if (css_init_done && !get_device(&cdev->dev))
613 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
615 /* Couldn't enable the subchannel for i/o. Sick device. */
617 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
620 /* Do we want to do path grouping? */
621 if (!cdev->private->options.pgroup) {
622 /* Start initial path verification. */
623 cdev->private->state = DEV_STATE_VERIFY;
624 cdev->private->flags.doverify = 0;
625 ccw_device_verify_start(cdev);
628 /* Do a SensePGID first. */
629 cdev->private->state = DEV_STATE_SENSE_PGID;
630 ccw_device_sense_pgid_start(cdev);
635 ccw_device_disband_done(struct ccw_device *cdev, int err)
639 ccw_device_done(cdev, DEV_STATE_OFFLINE);
642 ccw_device_done(cdev, DEV_STATE_BOXED);
645 cdev->private->flags.donotify = 0;
646 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
647 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
656 ccw_device_offline(struct ccw_device *cdev)
658 struct subchannel *sch;
660 if (ccw_device_is_orphan(cdev)) {
661 ccw_device_done(cdev, DEV_STATE_OFFLINE);
664 sch = to_subchannel(cdev->dev.parent);
665 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
667 if (scsw_actl(&sch->schib.scsw) != 0)
669 if (cdev->private->state != DEV_STATE_ONLINE)
671 /* Are we doing path grouping? */
672 if (!cdev->private->options.pgroup) {
673 /* No, set state offline immediately. */
674 ccw_device_done(cdev, DEV_STATE_OFFLINE);
677 /* Start Set Path Group commands. */
678 cdev->private->state = DEV_STATE_DISBAND_PGID;
679 ccw_device_disband_start(cdev);
684 * Handle timeout in device online/offline process.
687 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
691 ret = ccw_device_cancel_halt_clear(cdev);
694 ccw_device_done(cdev, DEV_STATE_BOXED);
697 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
700 ccw_device_set_timeout(cdev, 3*HZ);
705 * Handle not oper event in device recognition.
708 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
710 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
714 * Handle not operational event in non-special state.
716 static void ccw_device_generic_notoper(struct ccw_device *cdev,
717 enum dev_event dev_event)
719 struct subchannel *sch;
721 cdev->private->state = DEV_STATE_NOT_OPER;
722 sch = to_subchannel(cdev->dev.parent);
723 css_schedule_eval(sch->schid);
727 * Handle path verification event.
730 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
732 struct subchannel *sch;
734 if (cdev->private->state == DEV_STATE_W4SENSE) {
735 cdev->private->flags.doverify = 1;
738 sch = to_subchannel(cdev->dev.parent);
740 * Since we might not just be coming from an interrupt from the
741 * subchannel we have to update the schib.
743 stsch(sch->schid, &sch->schib);
745 if (scsw_actl(&sch->schib.scsw) != 0 ||
746 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
747 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
749 * No final status yet or final status not yet delivered
750 * to the device driver. Can't do path verfication now,
751 * delay until final status was delivered.
753 cdev->private->flags.doverify = 1;
756 /* Device is idle, we can do the path verification. */
757 cdev->private->state = DEV_STATE_VERIFY;
758 cdev->private->flags.doverify = 0;
759 ccw_device_verify_start(cdev);
763 * Got an interrupt for a normal io (state online).
766 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
771 irb = (struct irb *) __LC_IRB;
772 is_cmd = !scsw_is_tm(&irb->scsw);
773 /* Check for unsolicited interrupt. */
774 if (!scsw_is_solicited(&irb->scsw)) {
775 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
776 !irb->esw.esw0.erw.cons) {
777 /* Unit check but no sense data. Need basic sense. */
778 if (ccw_device_do_sense(cdev, irb) != 0)
779 goto call_handler_unsol;
780 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
781 cdev->private->state = DEV_STATE_W4SENSE;
782 cdev->private->intparm = 0;
787 cdev->handler (cdev, 0, irb);
788 if (cdev->private->flags.doverify)
789 ccw_device_online_verify(cdev, 0);
792 /* Accumulate status and find out if a basic sense is needed. */
793 ccw_device_accumulate_irb(cdev, irb);
794 if (is_cmd && cdev->private->flags.dosense) {
795 if (ccw_device_do_sense(cdev, irb) == 0) {
796 cdev->private->state = DEV_STATE_W4SENSE;
800 /* Call the handler. */
801 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
802 /* Start delayed path verification. */
803 ccw_device_online_verify(cdev, 0);
807 * Got an timeout in online state.
810 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
814 ccw_device_set_timeout(cdev, 0);
815 ret = ccw_device_cancel_halt_clear(cdev);
817 ccw_device_set_timeout(cdev, 3*HZ);
818 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
822 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
823 else if (cdev->handler)
824 cdev->handler(cdev, cdev->private->intparm,
825 ERR_PTR(-ETIMEDOUT));
829 * Got an interrupt for a basic sense.
832 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
836 irb = (struct irb *) __LC_IRB;
837 /* Check for unsolicited interrupt. */
838 if (scsw_stctl(&irb->scsw) ==
839 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
840 if (scsw_cc(&irb->scsw) == 1)
841 /* Basic sense hasn't started. Try again. */
842 ccw_device_do_sense(cdev, irb);
844 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
845 "interrupt during w4sense...\n",
846 cdev->private->dev_id.ssid,
847 cdev->private->dev_id.devno);
849 cdev->handler (cdev, 0, irb);
854 * Check if a halt or clear has been issued in the meanwhile. If yes,
855 * only deliver the halt/clear interrupt to the device driver as if it
856 * had killed the original request.
858 if (scsw_fctl(&irb->scsw) &
859 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
860 /* Retry Basic Sense if requested. */
861 if (cdev->private->flags.intretry) {
862 cdev->private->flags.intretry = 0;
863 ccw_device_do_sense(cdev, irb);
866 cdev->private->flags.dosense = 0;
867 memset(&cdev->private->irb, 0, sizeof(struct irb));
868 ccw_device_accumulate_irb(cdev, irb);
871 /* Add basic sense info to irb. */
872 ccw_device_accumulate_basic_sense(cdev, irb);
873 if (cdev->private->flags.dosense) {
874 /* Another basic sense is needed. */
875 ccw_device_do_sense(cdev, irb);
879 cdev->private->state = DEV_STATE_ONLINE;
880 /* Call the handler. */
881 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
882 /* Start delayed path verification. */
883 ccw_device_online_verify(cdev, 0);
887 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
891 irb = (struct irb *) __LC_IRB;
892 /* Accumulate status. We don't do basic sense. */
893 ccw_device_accumulate_irb(cdev, irb);
894 /* Remember to clear irb to avoid residuals. */
895 memset(&cdev->private->irb, 0, sizeof(struct irb));
896 /* Try to start delayed device verification. */
897 ccw_device_online_verify(cdev, 0);
898 /* Note: Don't call handler for cio initiated clear! */
902 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
904 struct subchannel *sch;
906 sch = to_subchannel(cdev->dev.parent);
907 ccw_device_set_timeout(cdev, 0);
908 /* Start delayed path verification. */
909 ccw_device_online_verify(cdev, 0);
910 /* OK, i/o is dead now. Call interrupt handler. */
912 cdev->handler(cdev, cdev->private->intparm,
917 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
921 ret = ccw_device_cancel_halt_clear(cdev);
923 ccw_device_set_timeout(cdev, 3*HZ);
926 /* Start delayed path verification. */
927 ccw_device_online_verify(cdev, 0);
929 cdev->handler(cdev, cdev->private->intparm,
933 void ccw_device_kill_io(struct ccw_device *cdev)
937 ret = ccw_device_cancel_halt_clear(cdev);
939 ccw_device_set_timeout(cdev, 3*HZ);
940 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
943 /* Start delayed path verification. */
944 ccw_device_online_verify(cdev, 0);
946 cdev->handler(cdev, cdev->private->intparm,
951 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
953 /* Start verification after current task finished. */
954 cdev->private->flags.doverify = 1;
958 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
963 case DEV_EVENT_INTERRUPT:
964 irb = (struct irb *) __LC_IRB;
965 /* Check for unsolicited interrupt. */
966 if ((scsw_stctl(&irb->scsw) ==
967 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
968 (!scsw_cc(&irb->scsw)))
969 /* FIXME: we should restart stlck here, but this
970 * is extremely unlikely ... */
973 ccw_device_accumulate_irb(cdev, irb);
974 /* We don't care about basic sense etc. */
976 default: /* timeout */
980 wake_up(&cdev->private->wait_q);
984 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
986 struct subchannel *sch;
988 sch = to_subchannel(cdev->dev.parent);
989 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
990 /* Couldn't enable the subchannel for i/o. Sick device. */
993 /* After 60s the device recognition is considered to have failed. */
994 ccw_device_set_timeout(cdev, 60*HZ);
996 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
997 ccw_device_sense_id_start(cdev);
1000 void ccw_device_trigger_reprobe(struct ccw_device *cdev)
1002 struct subchannel *sch;
1004 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1007 sch = to_subchannel(cdev->dev.parent);
1008 /* Update some values. */
1009 if (stsch(sch->schid, &sch->schib))
1011 if (!sch->schib.pmcw.dnv)
1014 * The pim, pam, pom values may not be accurate, but they are the best
1015 * we have before performing device selection :/
1017 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1018 /* Re-set some bits in the pmcw that were lost. */
1019 sch->schib.pmcw.csense = 1;
1020 sch->schib.pmcw.ena = 0;
1021 if ((sch->lpm & (sch->lpm - 1)) != 0)
1022 sch->schib.pmcw.mp = 1;
1023 /* We should also udate ssd info, but this has to wait. */
1024 /* Check if this is another device which appeared on the same sch. */
1025 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1026 PREPARE_WORK(&cdev->private->kick_work,
1027 ccw_device_move_to_orphanage);
1028 queue_work(slow_path_wq, &cdev->private->kick_work);
1030 ccw_device_start_id(cdev, 0);
1034 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1036 struct subchannel *sch;
1038 sch = to_subchannel(cdev->dev.parent);
1040 * An interrupt in state offline means a previous disable was not
1041 * successful. Try again.
1043 cio_disable_subchannel(sch);
1047 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1049 retry_set_schib(cdev);
1050 cdev->private->state = DEV_STATE_ONLINE;
1051 dev_fsm_event(cdev, dev_event);
1054 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1055 enum dev_event dev_event)
1057 cmf_retry_copy_block(cdev);
1058 cdev->private->state = DEV_STATE_ONLINE;
1059 dev_fsm_event(cdev, dev_event);
1063 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1065 ccw_device_set_timeout(cdev, 0);
1066 if (dev_event == DEV_EVENT_NOTOPER)
1067 cdev->private->state = DEV_STATE_NOT_OPER;
1069 cdev->private->state = DEV_STATE_OFFLINE;
1070 wake_up(&cdev->private->wait_q);
1074 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1078 ret = ccw_device_cancel_halt_clear(cdev);
1081 cdev->private->state = DEV_STATE_OFFLINE;
1082 wake_up(&cdev->private->wait_q);
1085 cdev->private->state = DEV_STATE_NOT_OPER;
1086 wake_up(&cdev->private->wait_q);
1089 ccw_device_set_timeout(cdev, HZ/10);
1094 * No operation action. This is used e.g. to ignore a timeout event in
1098 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1103 * Bug operation action.
1106 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1108 CIO_MSG_EVENT(0, "Internal state [%i][%i] not handled for device "
1109 "0.%x.%04x\n", cdev->private->state, dev_event,
1110 cdev->private->dev_id.ssid,
1111 cdev->private->dev_id.devno);
1116 * device statemachine
1118 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1119 [DEV_STATE_NOT_OPER] = {
1120 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1121 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1122 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1123 [DEV_EVENT_VERIFY] = ccw_device_nop,
1125 [DEV_STATE_SENSE_PGID] = {
1126 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1127 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1128 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1129 [DEV_EVENT_VERIFY] = ccw_device_nop,
1131 [DEV_STATE_SENSE_ID] = {
1132 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1133 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1134 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1135 [DEV_EVENT_VERIFY] = ccw_device_nop,
1137 [DEV_STATE_OFFLINE] = {
1138 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1139 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1140 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1141 [DEV_EVENT_VERIFY] = ccw_device_nop,
1143 [DEV_STATE_VERIFY] = {
1144 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1145 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1146 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1147 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1149 [DEV_STATE_ONLINE] = {
1150 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1151 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1152 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1153 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1155 [DEV_STATE_W4SENSE] = {
1156 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1157 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1158 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1159 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1161 [DEV_STATE_DISBAND_PGID] = {
1162 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1163 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1164 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1165 [DEV_EVENT_VERIFY] = ccw_device_nop,
1167 [DEV_STATE_BOXED] = {
1168 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1169 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1170 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1171 [DEV_EVENT_VERIFY] = ccw_device_nop,
1173 /* states to wait for i/o completion before doing something */
1174 [DEV_STATE_CLEAR_VERIFY] = {
1175 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1176 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1177 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1178 [DEV_EVENT_VERIFY] = ccw_device_nop,
1180 [DEV_STATE_TIMEOUT_KILL] = {
1181 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1182 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1183 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1184 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1186 [DEV_STATE_QUIESCE] = {
1187 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1188 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1189 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1190 [DEV_EVENT_VERIFY] = ccw_device_nop,
1192 /* special states for devices gone not operational */
1193 [DEV_STATE_DISCONNECTED] = {
1194 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1195 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1196 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1197 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1199 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1200 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1201 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1202 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1203 [DEV_EVENT_VERIFY] = ccw_device_nop,
1205 [DEV_STATE_CMFCHANGE] = {
1206 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1207 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1208 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1209 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1211 [DEV_STATE_CMFUPDATE] = {
1212 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1213 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1214 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1215 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1219 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);