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;
63 device_set_waiting(struct subchannel *sch)
65 struct ccw_device *cdev;
67 if (!sch->dev.driver_data)
69 cdev = sch->dev.driver_data;
70 ccw_device_set_timeout(cdev, 10*HZ);
71 cdev->private->state = DEV_STATE_WAIT4IO;
75 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
78 ccw_device_timeout(unsigned long data)
80 struct ccw_device *cdev;
82 cdev = (struct ccw_device *) data;
83 spin_lock_irq(cdev->ccwlock);
84 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
85 spin_unlock_irq(cdev->ccwlock);
92 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
95 del_timer(&cdev->private->timer);
98 if (timer_pending(&cdev->private->timer)) {
99 if (mod_timer(&cdev->private->timer, jiffies + expires))
102 cdev->private->timer.function = ccw_device_timeout;
103 cdev->private->timer.data = (unsigned long) cdev;
104 cdev->private->timer.expires = jiffies + expires;
105 add_timer(&cdev->private->timer);
108 /* Kill any pending timers after machine check. */
110 device_kill_pending_timer(struct subchannel *sch)
112 struct ccw_device *cdev;
114 if (!sch->dev.driver_data)
116 cdev = sch->dev.driver_data;
117 ccw_device_set_timeout(cdev, 0);
121 * Cancel running i/o. This is called repeatedly since halt/clear are
122 * asynchronous operations. We do one try with cio_cancel, two tries
123 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
124 * Returns 0 if device now idle, -ENODEV for device not operational and
125 * -EBUSY if an interrupt is expected (either from halt/clear or from a
129 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
131 struct subchannel *sch;
134 sch = to_subchannel(cdev->dev.parent);
135 ret = stsch(sch->schid, &sch->schib);
136 if (ret || !sch->schib.pmcw.dnv)
138 if (!sch->schib.pmcw.ena || sch->schib.scsw.actl == 0)
139 /* Not operational or no activity -> done. */
141 /* Stage 1: cancel io. */
142 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
143 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
144 ret = cio_cancel(sch);
147 /* cancel io unsuccessful. From now on it is asynchronous. */
148 cdev->private->iretry = 3; /* 3 halt retries. */
150 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
151 /* Stage 2: halt io. */
152 if (cdev->private->iretry) {
153 cdev->private->iretry--;
156 return (ret == 0) ? -EBUSY : ret;
158 /* halt io unsuccessful. */
159 cdev->private->iretry = 255; /* 255 clear retries. */
161 /* Stage 3: clear io. */
162 if (cdev->private->iretry) {
163 cdev->private->iretry--;
164 ret = cio_clear (sch);
165 return (ret == 0) ? -EBUSY : ret;
167 panic("Can't stop i/o on subchannel.\n");
171 ccw_device_handle_oper(struct ccw_device *cdev)
173 struct subchannel *sch;
175 sch = to_subchannel(cdev->dev.parent);
176 cdev->private->flags.recog_done = 1;
178 * Check if cu type and device type still match. If
179 * not, it is certainly another device and we have to
180 * de- and re-register. Also check here for non-matching devno.
182 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
183 cdev->id.cu_model != cdev->private->senseid.cu_model ||
184 cdev->id.dev_type != cdev->private->senseid.dev_type ||
185 cdev->id.dev_model != cdev->private->senseid.dev_model ||
186 cdev->private->dev_id.devno != sch->schib.pmcw.dev) {
187 PREPARE_WORK(&cdev->private->kick_work,
188 ccw_device_do_unreg_rereg, (void *)cdev);
189 queue_work(ccw_device_work, &cdev->private->kick_work);
192 cdev->private->flags.donotify = 1;
197 * The machine won't give us any notification by machine check if a chpid has
198 * been varied online on the SE so we have to find out by magic (i. e. driving
199 * the channel subsystem to device selection and updating our path masks).
202 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
206 for (i = 0; i<8; i++) {
208 if (!(sch->lpm & mask))
212 chpid_is_actually_online(sch->schib.pmcw.chpid[i]);
217 * Stop device recognition.
220 ccw_device_recog_done(struct ccw_device *cdev, int state)
222 struct subchannel *sch;
223 int notify, old_lpm, same_dev;
225 sch = to_subchannel(cdev->dev.parent);
227 ccw_device_set_timeout(cdev, 0);
228 cio_disable_subchannel(sch);
230 * Now that we tried recognition, we have performed device selection
231 * through ssch() and the path information is up to date.
234 stsch(sch->schid, &sch->schib);
235 sch->lpm = sch->schib.pmcw.pam & sch->opm;
236 /* Check since device may again have become not operational. */
237 if (!sch->schib.pmcw.dnv)
238 state = DEV_STATE_NOT_OPER;
239 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
240 /* Force reprobe on all chpids. */
242 if (sch->lpm != old_lpm)
243 __recover_lost_chpids(sch, old_lpm);
244 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
245 if (state == DEV_STATE_NOT_OPER) {
246 cdev->private->flags.recog_done = 1;
247 cdev->private->state = DEV_STATE_DISCONNECTED;
250 /* Boxed devices don't need extra treatment. */
253 same_dev = 0; /* Keep the compiler quiet... */
255 case DEV_STATE_NOT_OPER:
256 CIO_DEBUG(KERN_WARNING, 2,
257 "SenseID : unknown device %04x on subchannel "
258 "0.%x.%04x\n", cdev->private->dev_id.devno,
259 sch->schid.ssid, sch->schid.sch_no);
261 case DEV_STATE_OFFLINE:
262 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
263 same_dev = ccw_device_handle_oper(cdev);
266 /* fill out sense information */
267 memset(&cdev->id, 0, sizeof(cdev->id));
268 cdev->id.cu_type = cdev->private->senseid.cu_type;
269 cdev->id.cu_model = cdev->private->senseid.cu_model;
270 cdev->id.dev_type = cdev->private->senseid.dev_type;
271 cdev->id.dev_model = cdev->private->senseid.dev_model;
273 cdev->private->state = DEV_STATE_OFFLINE;
275 /* Get device online again. */
276 ccw_device_online(cdev);
277 wake_up(&cdev->private->wait_q);
281 /* Issue device info message. */
282 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
283 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
285 cdev->private->dev_id.ssid,
286 cdev->private->dev_id.devno,
287 cdev->id.cu_type, cdev->id.cu_model,
288 cdev->id.dev_type, cdev->id.dev_model);
290 case DEV_STATE_BOXED:
291 CIO_DEBUG(KERN_WARNING, 2,
292 "SenseID : boxed device %04x on subchannel "
293 "0.%x.%04x\n", cdev->private->dev_id.devno,
294 sch->schid.ssid, sch->schid.sch_no);
297 cdev->private->state = state;
298 io_subchannel_recog_done(cdev);
299 if (state != DEV_STATE_NOT_OPER)
300 wake_up(&cdev->private->wait_q);
304 * Function called from device_id.c after sense id has completed.
307 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
311 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
313 case -ETIME: /* Sense id stopped by timeout. */
314 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
317 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
323 ccw_device_oper_notify(void *data)
325 struct ccw_device *cdev;
326 struct subchannel *sch;
329 cdev = (struct ccw_device *)data;
330 sch = to_subchannel(cdev->dev.parent);
331 ret = (sch->driver && sch->driver->notify) ?
332 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
334 /* Driver doesn't want device back. */
335 ccw_device_do_unreg_rereg((void *)cdev);
337 /* Reenable channel measurements, if needed. */
339 wake_up(&cdev->private->wait_q);
344 * Finished with online/offline processing.
347 ccw_device_done(struct ccw_device *cdev, int state)
349 struct subchannel *sch;
351 sch = to_subchannel(cdev->dev.parent);
353 ccw_device_set_timeout(cdev, 0);
355 if (state != DEV_STATE_ONLINE)
356 cio_disable_subchannel(sch);
358 /* Reset device status. */
359 memset(&cdev->private->irb, 0, sizeof(struct irb));
361 cdev->private->state = state;
364 if (state == DEV_STATE_BOXED)
365 CIO_DEBUG(KERN_WARNING, 2,
366 "Boxed device %04x on subchannel %04x\n",
367 cdev->private->dev_id.devno, sch->schid.sch_no);
369 if (cdev->private->flags.donotify) {
370 cdev->private->flags.donotify = 0;
371 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify,
373 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
375 wake_up(&cdev->private->wait_q);
377 if (css_init_done && state != DEV_STATE_ONLINE)
378 put_device (&cdev->dev);
381 static inline int cmp_pgid(struct pgid *p1, struct pgid *p2)
389 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
392 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
398 for (i = 0; i < 8; i++) {
399 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
402 if (cdev->private->pgid[last].inf.ps.state1 ==
404 /* First non-zero PGID */
408 if (cmp_pgid(&cdev->private->pgid[i],
409 &cdev->private->pgid[last]) == 0)
410 /* Non-conflicting PGIDs */
413 /* PGID mismatch, can't pathgroup. */
414 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
415 "0.%x.%04x, can't pathgroup\n",
416 cdev->private->dev_id.ssid,
417 cdev->private->dev_id.devno);
418 cdev->private->options.pgroup = 0;
421 if (cdev->private->pgid[last].inf.ps.state1 ==
423 /* No previous pgid found */
424 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
425 sizeof(struct pgid));
427 /* Use existing pgid */
428 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
429 sizeof(struct pgid));
433 * Function called from device_pgid.c after sense path ground has completed.
436 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
438 struct subchannel *sch;
440 sch = to_subchannel(cdev->dev.parent);
442 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
443 cdev->private->options.pgroup = 0;
445 case 0: /* success */
446 case -EACCES: /* partial success, some paths not operational */
447 /* Check if all pgids are equal or 0. */
448 __ccw_device_get_common_pgid(cdev);
450 case -ETIME: /* Sense path group id stopped by timeout. */
451 case -EUSERS: /* device is reserved for someone else. */
452 ccw_device_done(cdev, DEV_STATE_BOXED);
455 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
458 /* Start Path Group verification. */
459 cdev->private->state = DEV_STATE_VERIFY;
460 cdev->private->flags.doverify = 0;
461 ccw_device_verify_start(cdev);
465 * Start device recognition.
468 ccw_device_recognition(struct ccw_device *cdev)
470 struct subchannel *sch;
473 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
474 (cdev->private->state != DEV_STATE_BOXED))
476 sch = to_subchannel(cdev->dev.parent);
477 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
479 /* Couldn't enable the subchannel for i/o. Sick device. */
482 /* After 60s the device recognition is considered to have failed. */
483 ccw_device_set_timeout(cdev, 60*HZ);
486 * We used to start here with a sense pgid to find out whether a device
487 * is locked by someone else. Unfortunately, the sense pgid command
488 * code has other meanings on devices predating the path grouping
489 * algorithm, so we start with sense id and box the device after an
490 * timeout (or if sense pgid during path verification detects the device
491 * is locked, as may happen on newer devices).
493 cdev->private->flags.recog_done = 0;
494 cdev->private->state = DEV_STATE_SENSE_ID;
495 ccw_device_sense_id_start(cdev);
500 * Handle timeout in device recognition.
503 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
507 ret = ccw_device_cancel_halt_clear(cdev);
510 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
513 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
516 ccw_device_set_timeout(cdev, 3*HZ);
522 ccw_device_nopath_notify(void *data)
524 struct ccw_device *cdev;
525 struct subchannel *sch;
528 cdev = (struct ccw_device *)data;
529 sch = to_subchannel(cdev->dev.parent);
533 ret = (sch->driver && sch->driver->notify) ?
534 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
536 if (get_device(&sch->dev)) {
537 /* Driver doesn't want to keep device. */
538 cio_disable_subchannel(sch);
539 if (get_device(&cdev->dev)) {
540 PREPARE_WORK(&cdev->private->kick_work,
541 ccw_device_call_sch_unregister,
543 queue_work(ccw_device_work,
544 &cdev->private->kick_work);
546 put_device(&sch->dev);
549 cio_disable_subchannel(sch);
550 ccw_device_set_timeout(cdev, 0);
551 cdev->private->flags.fake_irb = 0;
552 cdev->private->state = DEV_STATE_DISCONNECTED;
553 wake_up(&cdev->private->wait_q);
558 ccw_device_verify_done(struct ccw_device *cdev, int err)
560 struct subchannel *sch;
562 sch = to_subchannel(cdev->dev.parent);
563 /* Update schib - pom may have changed. */
564 stsch(sch->schid, &sch->schib);
565 /* Update lpm with verified path mask. */
567 /* Repeat path verification? */
568 if (cdev->private->flags.doverify) {
569 cdev->private->flags.doverify = 0;
570 ccw_device_verify_start(cdev);
574 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
575 cdev->private->options.pgroup = 0;
577 ccw_device_done(cdev, DEV_STATE_ONLINE);
578 /* Deliver fake irb to device driver, if needed. */
579 if (cdev->private->flags.fake_irb) {
580 memset(&cdev->private->irb, 0, sizeof(struct irb));
581 cdev->private->irb.scsw.cc = 1;
582 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
583 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
584 cdev->private->irb.scsw.stctl = 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 ccw_device_done(cdev, DEV_STATE_BOXED);
596 PREPARE_WORK(&cdev->private->kick_work,
597 ccw_device_nopath_notify, (void *)cdev);
598 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
599 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
608 ccw_device_online(struct ccw_device *cdev)
610 struct subchannel *sch;
613 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
614 (cdev->private->state != DEV_STATE_BOXED))
616 sch = to_subchannel(cdev->dev.parent);
617 if (css_init_done && !get_device(&cdev->dev))
619 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
621 /* Couldn't enable the subchannel for i/o. Sick device. */
623 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
626 /* Do we want to do path grouping? */
627 if (!cdev->private->options.pgroup) {
628 /* Start initial path verification. */
629 cdev->private->state = DEV_STATE_VERIFY;
630 cdev->private->flags.doverify = 0;
631 ccw_device_verify_start(cdev);
634 /* Do a SensePGID first. */
635 cdev->private->state = DEV_STATE_SENSE_PGID;
636 ccw_device_sense_pgid_start(cdev);
641 ccw_device_disband_done(struct ccw_device *cdev, int err)
645 ccw_device_done(cdev, DEV_STATE_OFFLINE);
648 ccw_device_done(cdev, DEV_STATE_BOXED);
651 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
660 ccw_device_offline(struct ccw_device *cdev)
662 struct subchannel *sch;
664 sch = to_subchannel(cdev->dev.parent);
665 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
667 if (cdev->private->state != DEV_STATE_ONLINE) {
668 if (sch->schib.scsw.actl != 0)
672 if (sch->schib.scsw.actl != 0)
674 /* Are we doing path grouping? */
675 if (!cdev->private->options.pgroup) {
676 /* No, set state offline immediately. */
677 ccw_device_done(cdev, DEV_STATE_OFFLINE);
680 /* Start Set Path Group commands. */
681 cdev->private->state = DEV_STATE_DISBAND_PGID;
682 ccw_device_disband_start(cdev);
687 * Handle timeout in device online/offline process.
690 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
694 ret = ccw_device_cancel_halt_clear(cdev);
697 ccw_device_done(cdev, DEV_STATE_BOXED);
700 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
703 ccw_device_set_timeout(cdev, 3*HZ);
708 * Handle not oper event in device recognition.
711 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
713 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
717 * Handle not operational event while offline.
720 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
722 struct subchannel *sch;
724 cdev->private->state = DEV_STATE_NOT_OPER;
725 sch = to_subchannel(cdev->dev.parent);
726 if (get_device(&cdev->dev)) {
727 PREPARE_WORK(&cdev->private->kick_work,
728 ccw_device_call_sch_unregister, (void *)cdev);
729 queue_work(ccw_device_work, &cdev->private->kick_work);
731 wake_up(&cdev->private->wait_q);
735 * Handle not operational event while online.
738 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
740 struct subchannel *sch;
742 sch = to_subchannel(cdev->dev.parent);
743 if (sch->driver->notify &&
744 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
745 ccw_device_set_timeout(cdev, 0);
746 cdev->private->flags.fake_irb = 0;
747 cdev->private->state = DEV_STATE_DISCONNECTED;
748 wake_up(&cdev->private->wait_q);
751 cdev->private->state = DEV_STATE_NOT_OPER;
752 cio_disable_subchannel(sch);
753 if (sch->schib.scsw.actl != 0) {
754 // FIXME: not-oper indication to device driver ?
755 ccw_device_call_handler(cdev);
757 if (get_device(&cdev->dev)) {
758 PREPARE_WORK(&cdev->private->kick_work,
759 ccw_device_call_sch_unregister, (void *)cdev);
760 queue_work(ccw_device_work, &cdev->private->kick_work);
762 wake_up(&cdev->private->wait_q);
766 * Handle path verification event.
769 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
771 struct subchannel *sch;
773 if (cdev->private->state == DEV_STATE_W4SENSE) {
774 cdev->private->flags.doverify = 1;
777 sch = to_subchannel(cdev->dev.parent);
779 * Since we might not just be coming from an interrupt from the
780 * subchannel we have to update the schib.
782 stsch(sch->schid, &sch->schib);
784 if (sch->schib.scsw.actl != 0 ||
785 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
786 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
788 * No final status yet or final status not yet delivered
789 * to the device driver. Can't do path verfication now,
790 * delay until final status was delivered.
792 cdev->private->flags.doverify = 1;
795 /* Device is idle, we can do the path verification. */
796 cdev->private->state = DEV_STATE_VERIFY;
797 cdev->private->flags.doverify = 0;
798 ccw_device_verify_start(cdev);
802 * Got an interrupt for a normal io (state online).
805 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
809 irb = (struct irb *) __LC_IRB;
810 /* Check for unsolicited interrupt. */
811 if ((irb->scsw.stctl ==
812 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
813 && (!irb->scsw.cc)) {
814 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
815 !irb->esw.esw0.erw.cons) {
816 /* Unit check but no sense data. Need basic sense. */
817 if (ccw_device_do_sense(cdev, irb) != 0)
818 goto call_handler_unsol;
819 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
820 cdev->private->state = DEV_STATE_W4SENSE;
821 cdev->private->intparm = 0;
826 cdev->handler (cdev, 0, irb);
829 /* Accumulate status and find out if a basic sense is needed. */
830 ccw_device_accumulate_irb(cdev, irb);
831 if (cdev->private->flags.dosense) {
832 if (ccw_device_do_sense(cdev, irb) == 0) {
833 cdev->private->state = DEV_STATE_W4SENSE;
837 /* Call the handler. */
838 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
839 /* Start delayed path verification. */
840 ccw_device_online_verify(cdev, 0);
844 * Got an timeout in online state.
847 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
851 ccw_device_set_timeout(cdev, 0);
852 ret = ccw_device_cancel_halt_clear(cdev);
854 ccw_device_set_timeout(cdev, 3*HZ);
855 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
858 if (ret == -ENODEV) {
859 struct subchannel *sch;
861 sch = to_subchannel(cdev->dev.parent);
863 PREPARE_WORK(&cdev->private->kick_work,
864 ccw_device_nopath_notify, (void *)cdev);
865 queue_work(ccw_device_notify_work,
866 &cdev->private->kick_work);
868 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
869 } else if (cdev->handler)
870 cdev->handler(cdev, cdev->private->intparm,
871 ERR_PTR(-ETIMEDOUT));
875 * Got an interrupt for a basic sense.
878 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
882 irb = (struct irb *) __LC_IRB;
883 /* Check for unsolicited interrupt. */
884 if (irb->scsw.stctl ==
885 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
886 if (irb->scsw.cc == 1)
887 /* Basic sense hasn't started. Try again. */
888 ccw_device_do_sense(cdev, irb);
890 printk(KERN_INFO "Huh? %s(%s): unsolicited "
892 __FUNCTION__, cdev->dev.bus_id);
894 cdev->handler (cdev, 0, irb);
899 * Check if a halt or clear has been issued in the meanwhile. If yes,
900 * only deliver the halt/clear interrupt to the device driver as if it
901 * had killed the original request.
903 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
904 cdev->private->flags.dosense = 0;
905 memset(&cdev->private->irb, 0, sizeof(struct irb));
906 ccw_device_accumulate_irb(cdev, irb);
909 /* Add basic sense info to irb. */
910 ccw_device_accumulate_basic_sense(cdev, irb);
911 if (cdev->private->flags.dosense) {
912 /* Another basic sense is needed. */
913 ccw_device_do_sense(cdev, irb);
917 cdev->private->state = DEV_STATE_ONLINE;
918 /* Call the handler. */
919 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
920 /* Start delayed path verification. */
921 ccw_device_online_verify(cdev, 0);
925 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
929 irb = (struct irb *) __LC_IRB;
930 /* Accumulate status. We don't do basic sense. */
931 ccw_device_accumulate_irb(cdev, irb);
932 /* Remember to clear irb to avoid residuals. */
933 memset(&cdev->private->irb, 0, sizeof(struct irb));
934 /* Try to start delayed device verification. */
935 ccw_device_online_verify(cdev, 0);
936 /* Note: Don't call handler for cio initiated clear! */
940 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
942 struct subchannel *sch;
944 sch = to_subchannel(cdev->dev.parent);
945 ccw_device_set_timeout(cdev, 0);
946 /* OK, i/o is dead now. Call interrupt handler. */
947 cdev->private->state = DEV_STATE_ONLINE;
949 cdev->handler(cdev, cdev->private->intparm,
950 ERR_PTR(-ETIMEDOUT));
952 PREPARE_WORK(&cdev->private->kick_work,
953 ccw_device_nopath_notify, (void *)cdev);
954 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
955 } else if (cdev->private->flags.doverify)
956 /* Start delayed path verification. */
957 ccw_device_online_verify(cdev, 0);
961 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
965 ret = ccw_device_cancel_halt_clear(cdev);
967 ccw_device_set_timeout(cdev, 3*HZ);
970 if (ret == -ENODEV) {
971 struct subchannel *sch;
973 sch = to_subchannel(cdev->dev.parent);
975 PREPARE_WORK(&cdev->private->kick_work,
976 ccw_device_nopath_notify, (void *)cdev);
977 queue_work(ccw_device_notify_work,
978 &cdev->private->kick_work);
980 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
983 //FIXME: Can we get here?
984 cdev->private->state = DEV_STATE_ONLINE;
986 cdev->handler(cdev, cdev->private->intparm,
987 ERR_PTR(-ETIMEDOUT));
991 ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event)
994 struct subchannel *sch;
996 irb = (struct irb *) __LC_IRB;
998 * Accumulate status and find out if a basic sense is needed.
999 * This is fine since we have already adapted the lpm.
1001 ccw_device_accumulate_irb(cdev, irb);
1002 if (cdev->private->flags.dosense) {
1003 if (ccw_device_do_sense(cdev, irb) == 0) {
1004 cdev->private->state = DEV_STATE_W4SENSE;
1009 /* Iff device is idle, reset timeout. */
1010 sch = to_subchannel(cdev->dev.parent);
1011 if (!stsch(sch->schid, &sch->schib))
1012 if (sch->schib.scsw.actl == 0)
1013 ccw_device_set_timeout(cdev, 0);
1014 /* Call the handler. */
1015 ccw_device_call_handler(cdev);
1017 PREPARE_WORK(&cdev->private->kick_work,
1018 ccw_device_nopath_notify, (void *)cdev);
1019 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1020 } else if (cdev->private->flags.doverify)
1021 ccw_device_online_verify(cdev, 0);
1025 ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1028 struct subchannel *sch;
1030 sch = to_subchannel(cdev->dev.parent);
1031 ccw_device_set_timeout(cdev, 0);
1032 ret = ccw_device_cancel_halt_clear(cdev);
1033 if (ret == -EBUSY) {
1034 ccw_device_set_timeout(cdev, 3*HZ);
1035 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1038 if (ret == -ENODEV) {
1040 PREPARE_WORK(&cdev->private->kick_work,
1041 ccw_device_nopath_notify, (void *)cdev);
1042 queue_work(ccw_device_notify_work,
1043 &cdev->private->kick_work);
1045 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1049 cdev->handler(cdev, cdev->private->intparm,
1050 ERR_PTR(-ETIMEDOUT));
1052 PREPARE_WORK(&cdev->private->kick_work,
1053 ccw_device_nopath_notify, (void *)cdev);
1054 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1055 } else if (cdev->private->flags.doverify)
1056 /* Start delayed path verification. */
1057 ccw_device_online_verify(cdev, 0);
1061 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
1063 /* Start verification after current task finished. */
1064 cdev->private->flags.doverify = 1;
1068 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1072 switch (dev_event) {
1073 case DEV_EVENT_INTERRUPT:
1074 irb = (struct irb *) __LC_IRB;
1075 /* Check for unsolicited interrupt. */
1076 if ((irb->scsw.stctl ==
1077 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1079 /* FIXME: we should restart stlck here, but this
1080 * is extremely unlikely ... */
1083 ccw_device_accumulate_irb(cdev, irb);
1084 /* We don't care about basic sense etc. */
1086 default: /* timeout */
1090 wake_up(&cdev->private->wait_q);
1094 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1096 struct subchannel *sch;
1098 sch = to_subchannel(cdev->dev.parent);
1099 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1100 /* Couldn't enable the subchannel for i/o. Sick device. */
1103 /* After 60s the device recognition is considered to have failed. */
1104 ccw_device_set_timeout(cdev, 60*HZ);
1106 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1107 ccw_device_sense_id_start(cdev);
1111 device_trigger_reprobe(struct subchannel *sch)
1113 struct ccw_device *cdev;
1115 if (!sch->dev.driver_data)
1117 cdev = sch->dev.driver_data;
1118 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1121 /* Update some values. */
1122 if (stsch(sch->schid, &sch->schib))
1126 * The pim, pam, pom values may not be accurate, but they are the best
1127 * we have before performing device selection :/
1129 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1130 /* Re-set some bits in the pmcw that were lost. */
1131 sch->schib.pmcw.isc = 3;
1132 sch->schib.pmcw.csense = 1;
1133 sch->schib.pmcw.ena = 0;
1134 if ((sch->lpm & (sch->lpm - 1)) != 0)
1135 sch->schib.pmcw.mp = 1;
1136 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1137 /* We should also udate ssd info, but this has to wait. */
1138 ccw_device_start_id(cdev, 0);
1142 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1144 struct subchannel *sch;
1146 sch = to_subchannel(cdev->dev.parent);
1148 * An interrupt in state offline means a previous disable was not
1149 * successful. Try again.
1151 cio_disable_subchannel(sch);
1155 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1157 retry_set_schib(cdev);
1158 cdev->private->state = DEV_STATE_ONLINE;
1159 dev_fsm_event(cdev, dev_event);
1162 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1163 enum dev_event dev_event)
1165 cmf_retry_copy_block(cdev);
1166 cdev->private->state = DEV_STATE_ONLINE;
1167 dev_fsm_event(cdev, dev_event);
1171 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1173 ccw_device_set_timeout(cdev, 0);
1174 if (dev_event == DEV_EVENT_NOTOPER)
1175 cdev->private->state = DEV_STATE_NOT_OPER;
1177 cdev->private->state = DEV_STATE_OFFLINE;
1178 wake_up(&cdev->private->wait_q);
1182 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1186 ret = ccw_device_cancel_halt_clear(cdev);
1189 cdev->private->state = DEV_STATE_OFFLINE;
1190 wake_up(&cdev->private->wait_q);
1193 cdev->private->state = DEV_STATE_NOT_OPER;
1194 wake_up(&cdev->private->wait_q);
1197 ccw_device_set_timeout(cdev, HZ/10);
1202 * No operation action. This is used e.g. to ignore a timeout event in
1206 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1211 * Bug operation action.
1214 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1216 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1217 cdev->private->state, dev_event);
1222 * device statemachine
1224 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1225 [DEV_STATE_NOT_OPER] = {
1226 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1227 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1228 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1229 [DEV_EVENT_VERIFY] = ccw_device_nop,
1231 [DEV_STATE_SENSE_PGID] = {
1232 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1233 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1234 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1235 [DEV_EVENT_VERIFY] = ccw_device_nop,
1237 [DEV_STATE_SENSE_ID] = {
1238 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1239 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1240 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1241 [DEV_EVENT_VERIFY] = ccw_device_nop,
1243 [DEV_STATE_OFFLINE] = {
1244 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1245 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1246 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1247 [DEV_EVENT_VERIFY] = ccw_device_nop,
1249 [DEV_STATE_VERIFY] = {
1250 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1251 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1252 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1253 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1255 [DEV_STATE_ONLINE] = {
1256 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1257 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1258 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1259 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1261 [DEV_STATE_W4SENSE] = {
1262 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1263 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1264 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1265 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1267 [DEV_STATE_DISBAND_PGID] = {
1268 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1269 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1270 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1271 [DEV_EVENT_VERIFY] = ccw_device_nop,
1273 [DEV_STATE_BOXED] = {
1274 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1275 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1276 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1277 [DEV_EVENT_VERIFY] = ccw_device_nop,
1279 /* states to wait for i/o completion before doing something */
1280 [DEV_STATE_CLEAR_VERIFY] = {
1281 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1282 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1283 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1284 [DEV_EVENT_VERIFY] = ccw_device_nop,
1286 [DEV_STATE_TIMEOUT_KILL] = {
1287 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1288 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1289 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1290 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1292 [DEV_STATE_WAIT4IO] = {
1293 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1294 [DEV_EVENT_INTERRUPT] = ccw_device_wait4io_irq,
1295 [DEV_EVENT_TIMEOUT] = ccw_device_wait4io_timeout,
1296 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1298 [DEV_STATE_QUIESCE] = {
1299 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1300 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1301 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1302 [DEV_EVENT_VERIFY] = ccw_device_nop,
1304 /* special states for devices gone not operational */
1305 [DEV_STATE_DISCONNECTED] = {
1306 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1307 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1308 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1309 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1311 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1312 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1313 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1314 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1315 [DEV_EVENT_VERIFY] = ccw_device_nop,
1317 [DEV_STATE_CMFCHANGE] = {
1318 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1319 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1320 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1321 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1323 [DEV_STATE_CMFUPDATE] = {
1324 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1325 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1326 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1327 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1332 * io_subchannel_irq is called for "real" interrupts or for status
1333 * pending conditions on msch.
1336 io_subchannel_irq (struct device *pdev)
1338 struct ccw_device *cdev;
1340 cdev = to_subchannel(pdev)->dev.driver_data;
1342 CIO_TRACE_EVENT (3, "IRQ");
1343 CIO_TRACE_EVENT (3, pdev->bus_id);
1345 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1348 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);