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->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->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->ssid, cdev->private->devno,
286 cdev->id.cu_type, cdev->id.cu_model,
287 cdev->id.dev_type, cdev->id.dev_model);
289 case DEV_STATE_BOXED:
290 CIO_DEBUG(KERN_WARNING, 2,
291 "SenseID : boxed device %04x on subchannel "
292 "0.%x.%04x\n", cdev->private->devno,
293 sch->schid.ssid, sch->schid.sch_no);
296 cdev->private->state = state;
297 io_subchannel_recog_done(cdev);
298 if (state != DEV_STATE_NOT_OPER)
299 wake_up(&cdev->private->wait_q);
303 * Function called from device_id.c after sense id has completed.
306 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
310 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
312 case -ETIME: /* Sense id stopped by timeout. */
313 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
316 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
322 ccw_device_oper_notify(void *data)
324 struct ccw_device *cdev;
325 struct subchannel *sch;
328 cdev = (struct ccw_device *)data;
329 sch = to_subchannel(cdev->dev.parent);
330 ret = (sch->driver && sch->driver->notify) ?
331 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
333 /* Driver doesn't want device back. */
334 ccw_device_do_unreg_rereg((void *)cdev);
336 /* Reenable channel measurements, if needed. */
338 wake_up(&cdev->private->wait_q);
343 * Finished with online/offline processing.
346 ccw_device_done(struct ccw_device *cdev, int state)
348 struct subchannel *sch;
350 sch = to_subchannel(cdev->dev.parent);
352 if (state != DEV_STATE_ONLINE)
353 cio_disable_subchannel(sch);
355 /* Reset device status. */
356 memset(&cdev->private->irb, 0, sizeof(struct irb));
358 cdev->private->state = state;
361 if (state == DEV_STATE_BOXED)
362 CIO_DEBUG(KERN_WARNING, 2,
363 "Boxed device %04x on subchannel %04x\n",
364 cdev->private->devno, sch->schid.sch_no);
366 if (cdev->private->flags.donotify) {
367 cdev->private->flags.donotify = 0;
368 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify,
370 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
372 wake_up(&cdev->private->wait_q);
374 if (css_init_done && state != DEV_STATE_ONLINE)
375 put_device (&cdev->dev);
378 static inline int cmp_pgid(struct pgid *p1, struct pgid *p2)
386 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
389 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
395 for (i = 0; i < 8; i++) {
396 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
399 if (cdev->private->pgid[last].inf.ps.state1 ==
401 /* First non-zero PGID */
405 if (cmp_pgid(&cdev->private->pgid[i],
406 &cdev->private->pgid[last]) == 0)
407 /* Non-conflicting PGIDs */
410 /* PGID mismatch, can't pathgroup. */
411 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
412 "0.%x.%04x, can't pathgroup\n",
413 cdev->private->ssid, cdev->private->devno);
414 cdev->private->options.pgroup = 0;
417 if (cdev->private->pgid[last].inf.ps.state1 ==
419 /* No previous pgid found */
420 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
421 sizeof(struct pgid));
423 /* Use existing pgid */
424 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
425 sizeof(struct pgid));
429 * Function called from device_pgid.c after sense path ground has completed.
432 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
434 struct subchannel *sch;
436 sch = to_subchannel(cdev->dev.parent);
438 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
439 cdev->private->options.pgroup = 0;
441 case 0: /* success */
442 case -EACCES: /* partial success, some paths not operational */
443 /* Check if all pgids are equal or 0. */
444 __ccw_device_get_common_pgid(cdev);
446 case -ETIME: /* Sense path group id stopped by timeout. */
447 case -EUSERS: /* device is reserved for someone else. */
448 ccw_device_done(cdev, DEV_STATE_BOXED);
451 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
454 /* Start Path Group verification. */
455 cdev->private->state = DEV_STATE_VERIFY;
456 cdev->private->flags.doverify = 0;
457 ccw_device_verify_start(cdev);
461 * Start device recognition.
464 ccw_device_recognition(struct ccw_device *cdev)
466 struct subchannel *sch;
469 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
470 (cdev->private->state != DEV_STATE_BOXED))
472 sch = to_subchannel(cdev->dev.parent);
473 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
475 /* Couldn't enable the subchannel for i/o. Sick device. */
478 /* After 60s the device recognition is considered to have failed. */
479 ccw_device_set_timeout(cdev, 60*HZ);
482 * We used to start here with a sense pgid to find out whether a device
483 * is locked by someone else. Unfortunately, the sense pgid command
484 * code has other meanings on devices predating the path grouping
485 * algorithm, so we start with sense id and box the device after an
486 * timeout (or if sense pgid during path verification detects the device
487 * is locked, as may happen on newer devices).
489 cdev->private->flags.recog_done = 0;
490 cdev->private->state = DEV_STATE_SENSE_ID;
491 ccw_device_sense_id_start(cdev);
496 * Handle timeout in device recognition.
499 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
503 ret = ccw_device_cancel_halt_clear(cdev);
506 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
509 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
512 ccw_device_set_timeout(cdev, 3*HZ);
518 ccw_device_nopath_notify(void *data)
520 struct ccw_device *cdev;
521 struct subchannel *sch;
524 cdev = (struct ccw_device *)data;
525 sch = to_subchannel(cdev->dev.parent);
529 ret = (sch->driver && sch->driver->notify) ?
530 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
532 if (get_device(&sch->dev)) {
533 /* Driver doesn't want to keep device. */
534 cio_disable_subchannel(sch);
535 if (get_device(&cdev->dev)) {
536 PREPARE_WORK(&cdev->private->kick_work,
537 ccw_device_call_sch_unregister,
539 queue_work(ccw_device_work,
540 &cdev->private->kick_work);
542 put_device(&sch->dev);
545 cio_disable_subchannel(sch);
546 ccw_device_set_timeout(cdev, 0);
547 cdev->private->flags.fake_irb = 0;
548 cdev->private->state = DEV_STATE_DISCONNECTED;
549 wake_up(&cdev->private->wait_q);
554 ccw_device_verify_done(struct ccw_device *cdev, int err)
556 struct subchannel *sch;
558 sch = to_subchannel(cdev->dev.parent);
559 /* Update schib - pom may have changed. */
560 stsch(sch->schid, &sch->schib);
561 /* Update lpm with verified path mask. */
563 /* Repeat path verification? */
564 if (cdev->private->flags.doverify) {
565 cdev->private->flags.doverify = 0;
566 ccw_device_verify_start(cdev);
570 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
571 cdev->private->options.pgroup = 0;
573 ccw_device_done(cdev, DEV_STATE_ONLINE);
574 /* Deliver fake irb to device driver, if needed. */
575 if (cdev->private->flags.fake_irb) {
576 memset(&cdev->private->irb, 0, sizeof(struct irb));
577 cdev->private->irb.scsw.cc = 1;
578 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
579 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
580 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
581 cdev->private->flags.fake_irb = 0;
583 cdev->handler(cdev, cdev->private->intparm,
584 &cdev->private->irb);
585 memset(&cdev->private->irb, 0, sizeof(struct irb));
589 ccw_device_done(cdev, DEV_STATE_BOXED);
592 PREPARE_WORK(&cdev->private->kick_work,
593 ccw_device_nopath_notify, (void *)cdev);
594 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
595 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
604 ccw_device_online(struct ccw_device *cdev)
606 struct subchannel *sch;
609 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
610 (cdev->private->state != DEV_STATE_BOXED))
612 sch = to_subchannel(cdev->dev.parent);
613 if (css_init_done && !get_device(&cdev->dev))
615 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
617 /* Couldn't enable the subchannel for i/o. Sick device. */
619 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
622 /* Do we want to do path grouping? */
623 if (!cdev->private->options.pgroup) {
624 /* Start initial path verification. */
625 cdev->private->state = DEV_STATE_VERIFY;
626 cdev->private->flags.doverify = 0;
627 ccw_device_verify_start(cdev);
630 /* Do a SensePGID first. */
631 cdev->private->state = DEV_STATE_SENSE_PGID;
632 ccw_device_sense_pgid_start(cdev);
637 ccw_device_disband_done(struct ccw_device *cdev, int err)
641 ccw_device_done(cdev, DEV_STATE_OFFLINE);
644 ccw_device_done(cdev, DEV_STATE_BOXED);
647 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
656 ccw_device_offline(struct ccw_device *cdev)
658 struct subchannel *sch;
660 sch = to_subchannel(cdev->dev.parent);
661 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
663 if (cdev->private->state != DEV_STATE_ONLINE) {
664 if (sch->schib.scsw.actl != 0)
668 if (sch->schib.scsw.actl != 0)
670 /* Are we doing path grouping? */
671 if (!cdev->private->options.pgroup) {
672 /* No, set state offline immediately. */
673 ccw_device_done(cdev, DEV_STATE_OFFLINE);
676 /* Start Set Path Group commands. */
677 cdev->private->state = DEV_STATE_DISBAND_PGID;
678 ccw_device_disband_start(cdev);
683 * Handle timeout in device online/offline process.
686 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
690 ret = ccw_device_cancel_halt_clear(cdev);
693 ccw_device_done(cdev, DEV_STATE_BOXED);
696 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
699 ccw_device_set_timeout(cdev, 3*HZ);
704 * Handle not oper event in device recognition.
707 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
709 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
713 * Handle not operational event while offline.
716 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
718 struct subchannel *sch;
720 cdev->private->state = DEV_STATE_NOT_OPER;
721 sch = to_subchannel(cdev->dev.parent);
722 if (get_device(&cdev->dev)) {
723 PREPARE_WORK(&cdev->private->kick_work,
724 ccw_device_call_sch_unregister, (void *)cdev);
725 queue_work(ccw_device_work, &cdev->private->kick_work);
727 wake_up(&cdev->private->wait_q);
731 * Handle not operational event while online.
734 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
736 struct subchannel *sch;
738 sch = to_subchannel(cdev->dev.parent);
739 if (sch->driver->notify &&
740 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
741 ccw_device_set_timeout(cdev, 0);
742 cdev->private->flags.fake_irb = 0;
743 cdev->private->state = DEV_STATE_DISCONNECTED;
744 wake_up(&cdev->private->wait_q);
747 cdev->private->state = DEV_STATE_NOT_OPER;
748 cio_disable_subchannel(sch);
749 if (sch->schib.scsw.actl != 0) {
750 // FIXME: not-oper indication to device driver ?
751 ccw_device_call_handler(cdev);
753 if (get_device(&cdev->dev)) {
754 PREPARE_WORK(&cdev->private->kick_work,
755 ccw_device_call_sch_unregister, (void *)cdev);
756 queue_work(ccw_device_work, &cdev->private->kick_work);
758 wake_up(&cdev->private->wait_q);
762 * Handle path verification event.
765 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
767 struct subchannel *sch;
769 if (cdev->private->state == DEV_STATE_W4SENSE) {
770 cdev->private->flags.doverify = 1;
773 sch = to_subchannel(cdev->dev.parent);
775 * Since we might not just be coming from an interrupt from the
776 * subchannel we have to update the schib.
778 stsch(sch->schid, &sch->schib);
780 if (sch->schib.scsw.actl != 0 ||
781 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
782 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
784 * No final status yet or final status not yet delivered
785 * to the device driver. Can't do path verfication now,
786 * delay until final status was delivered.
788 cdev->private->flags.doverify = 1;
791 /* Device is idle, we can do the path verification. */
792 cdev->private->state = DEV_STATE_VERIFY;
793 cdev->private->flags.doverify = 0;
794 ccw_device_verify_start(cdev);
798 * Got an interrupt for a normal io (state online).
801 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
805 irb = (struct irb *) __LC_IRB;
806 /* Check for unsolicited interrupt. */
807 if ((irb->scsw.stctl ==
808 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
809 && (!irb->scsw.cc)) {
810 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
811 !irb->esw.esw0.erw.cons) {
812 /* Unit check but no sense data. Need basic sense. */
813 if (ccw_device_do_sense(cdev, irb) != 0)
814 goto call_handler_unsol;
815 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
816 cdev->private->state = DEV_STATE_W4SENSE;
817 cdev->private->intparm = 0;
822 cdev->handler (cdev, 0, irb);
825 /* Accumulate status and find out if a basic sense is needed. */
826 ccw_device_accumulate_irb(cdev, irb);
827 if (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;
854 if (ret == -ENODEV) {
855 struct subchannel *sch;
857 sch = to_subchannel(cdev->dev.parent);
859 PREPARE_WORK(&cdev->private->kick_work,
860 ccw_device_nopath_notify, (void *)cdev);
861 queue_work(ccw_device_notify_work,
862 &cdev->private->kick_work);
864 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
865 } else if (cdev->handler)
866 cdev->handler(cdev, cdev->private->intparm,
867 ERR_PTR(-ETIMEDOUT));
871 * Got an interrupt for a basic sense.
874 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
878 irb = (struct irb *) __LC_IRB;
879 /* Check for unsolicited interrupt. */
880 if (irb->scsw.stctl ==
881 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
882 if (irb->scsw.cc == 1)
883 /* Basic sense hasn't started. Try again. */
884 ccw_device_do_sense(cdev, irb);
886 printk("Huh? %s(%s): unsolicited interrupt...\n",
887 __FUNCTION__, cdev->dev.bus_id);
889 cdev->handler (cdev, 0, irb);
894 * Check if a halt or clear has been issued in the meanwhile. If yes,
895 * only deliver the halt/clear interrupt to the device driver as if it
896 * had killed the original request.
898 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
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 /* Call the handler. */
914 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
915 /* Start delayed path verification. */
916 ccw_device_online_verify(cdev, 0);
920 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
924 irb = (struct irb *) __LC_IRB;
925 /* Accumulate status. We don't do basic sense. */
926 ccw_device_accumulate_irb(cdev, irb);
927 /* Remember to clear irb to avoid residuals. */
928 memset(&cdev->private->irb, 0, sizeof(struct irb));
929 /* Try to start delayed device verification. */
930 ccw_device_online_verify(cdev, 0);
931 /* Note: Don't call handler for cio initiated clear! */
935 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
937 struct subchannel *sch;
939 sch = to_subchannel(cdev->dev.parent);
940 ccw_device_set_timeout(cdev, 0);
941 /* OK, i/o is dead now. Call interrupt handler. */
942 cdev->private->state = DEV_STATE_ONLINE;
944 cdev->handler(cdev, cdev->private->intparm,
945 ERR_PTR(-ETIMEDOUT));
947 PREPARE_WORK(&cdev->private->kick_work,
948 ccw_device_nopath_notify, (void *)cdev);
949 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
950 } else if (cdev->private->flags.doverify)
951 /* Start delayed path verification. */
952 ccw_device_online_verify(cdev, 0);
956 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
960 ret = ccw_device_cancel_halt_clear(cdev);
962 ccw_device_set_timeout(cdev, 3*HZ);
965 if (ret == -ENODEV) {
966 struct subchannel *sch;
968 sch = to_subchannel(cdev->dev.parent);
970 PREPARE_WORK(&cdev->private->kick_work,
971 ccw_device_nopath_notify, (void *)cdev);
972 queue_work(ccw_device_notify_work,
973 &cdev->private->kick_work);
975 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
978 //FIXME: Can we get here?
979 cdev->private->state = DEV_STATE_ONLINE;
981 cdev->handler(cdev, cdev->private->intparm,
982 ERR_PTR(-ETIMEDOUT));
986 ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event)
989 struct subchannel *sch;
991 irb = (struct irb *) __LC_IRB;
993 * Accumulate status and find out if a basic sense is needed.
994 * This is fine since we have already adapted the lpm.
996 ccw_device_accumulate_irb(cdev, irb);
997 if (cdev->private->flags.dosense) {
998 if (ccw_device_do_sense(cdev, irb) == 0) {
999 cdev->private->state = DEV_STATE_W4SENSE;
1004 /* Iff device is idle, reset timeout. */
1005 sch = to_subchannel(cdev->dev.parent);
1006 if (!stsch(sch->schid, &sch->schib))
1007 if (sch->schib.scsw.actl == 0)
1008 ccw_device_set_timeout(cdev, 0);
1009 /* Call the handler. */
1010 ccw_device_call_handler(cdev);
1012 PREPARE_WORK(&cdev->private->kick_work,
1013 ccw_device_nopath_notify, (void *)cdev);
1014 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1015 } else if (cdev->private->flags.doverify)
1016 ccw_device_online_verify(cdev, 0);
1020 ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1023 struct subchannel *sch;
1025 sch = to_subchannel(cdev->dev.parent);
1026 ccw_device_set_timeout(cdev, 0);
1027 ret = ccw_device_cancel_halt_clear(cdev);
1028 if (ret == -EBUSY) {
1029 ccw_device_set_timeout(cdev, 3*HZ);
1030 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1033 if (ret == -ENODEV) {
1035 PREPARE_WORK(&cdev->private->kick_work,
1036 ccw_device_nopath_notify, (void *)cdev);
1037 queue_work(ccw_device_notify_work,
1038 &cdev->private->kick_work);
1040 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1044 cdev->handler(cdev, cdev->private->intparm,
1045 ERR_PTR(-ETIMEDOUT));
1047 PREPARE_WORK(&cdev->private->kick_work,
1048 ccw_device_nopath_notify, (void *)cdev);
1049 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1050 } else if (cdev->private->flags.doverify)
1051 /* Start delayed path verification. */
1052 ccw_device_online_verify(cdev, 0);
1056 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
1058 /* Start verification after current task finished. */
1059 cdev->private->flags.doverify = 1;
1063 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1067 switch (dev_event) {
1068 case DEV_EVENT_INTERRUPT:
1069 irb = (struct irb *) __LC_IRB;
1070 /* Check for unsolicited interrupt. */
1071 if ((irb->scsw.stctl ==
1072 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1074 /* FIXME: we should restart stlck here, but this
1075 * is extremely unlikely ... */
1078 ccw_device_accumulate_irb(cdev, irb);
1079 /* We don't care about basic sense etc. */
1081 default: /* timeout */
1085 wake_up(&cdev->private->wait_q);
1089 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1091 struct subchannel *sch;
1093 sch = to_subchannel(cdev->dev.parent);
1094 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1095 /* Couldn't enable the subchannel for i/o. Sick device. */
1098 /* After 60s the device recognition is considered to have failed. */
1099 ccw_device_set_timeout(cdev, 60*HZ);
1101 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1102 ccw_device_sense_id_start(cdev);
1106 device_trigger_reprobe(struct subchannel *sch)
1108 struct ccw_device *cdev;
1110 if (!sch->dev.driver_data)
1112 cdev = sch->dev.driver_data;
1113 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1116 /* Update some values. */
1117 if (stsch(sch->schid, &sch->schib))
1121 * The pim, pam, pom values may not be accurate, but they are the best
1122 * we have before performing device selection :/
1124 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1125 /* Re-set some bits in the pmcw that were lost. */
1126 sch->schib.pmcw.isc = 3;
1127 sch->schib.pmcw.csense = 1;
1128 sch->schib.pmcw.ena = 0;
1129 if ((sch->lpm & (sch->lpm - 1)) != 0)
1130 sch->schib.pmcw.mp = 1;
1131 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1132 /* We should also udate ssd info, but this has to wait. */
1133 ccw_device_start_id(cdev, 0);
1137 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1139 struct subchannel *sch;
1141 sch = to_subchannel(cdev->dev.parent);
1143 * An interrupt in state offline means a previous disable was not
1144 * successful. Try again.
1146 cio_disable_subchannel(sch);
1150 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1152 retry_set_schib(cdev);
1153 cdev->private->state = DEV_STATE_ONLINE;
1154 dev_fsm_event(cdev, dev_event);
1157 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1158 enum dev_event dev_event)
1160 cmf_retry_copy_block(cdev);
1161 cdev->private->state = DEV_STATE_ONLINE;
1162 dev_fsm_event(cdev, dev_event);
1166 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1168 ccw_device_set_timeout(cdev, 0);
1169 if (dev_event == DEV_EVENT_NOTOPER)
1170 cdev->private->state = DEV_STATE_NOT_OPER;
1172 cdev->private->state = DEV_STATE_OFFLINE;
1173 wake_up(&cdev->private->wait_q);
1177 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1181 ret = ccw_device_cancel_halt_clear(cdev);
1184 cdev->private->state = DEV_STATE_OFFLINE;
1185 wake_up(&cdev->private->wait_q);
1188 cdev->private->state = DEV_STATE_NOT_OPER;
1189 wake_up(&cdev->private->wait_q);
1192 ccw_device_set_timeout(cdev, HZ/10);
1197 * No operation action. This is used e.g. to ignore a timeout event in
1201 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1206 * Bug operation action.
1209 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1211 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1212 cdev->private->state, dev_event);
1217 * device statemachine
1219 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1220 [DEV_STATE_NOT_OPER] = {
1221 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1222 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1223 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1224 [DEV_EVENT_VERIFY] = ccw_device_nop,
1226 [DEV_STATE_SENSE_PGID] = {
1227 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1228 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1229 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1230 [DEV_EVENT_VERIFY] = ccw_device_nop,
1232 [DEV_STATE_SENSE_ID] = {
1233 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1234 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1235 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1236 [DEV_EVENT_VERIFY] = ccw_device_nop,
1238 [DEV_STATE_OFFLINE] = {
1239 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1240 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1241 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1242 [DEV_EVENT_VERIFY] = ccw_device_nop,
1244 [DEV_STATE_VERIFY] = {
1245 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1246 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1247 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1248 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1250 [DEV_STATE_ONLINE] = {
1251 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1252 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1253 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1254 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1256 [DEV_STATE_W4SENSE] = {
1257 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1258 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1259 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1260 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1262 [DEV_STATE_DISBAND_PGID] = {
1263 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1264 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1265 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1266 [DEV_EVENT_VERIFY] = ccw_device_nop,
1268 [DEV_STATE_BOXED] = {
1269 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1270 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1271 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1272 [DEV_EVENT_VERIFY] = ccw_device_nop,
1274 /* states to wait for i/o completion before doing something */
1275 [DEV_STATE_CLEAR_VERIFY] = {
1276 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1277 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1278 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1279 [DEV_EVENT_VERIFY] = ccw_device_nop,
1281 [DEV_STATE_TIMEOUT_KILL] = {
1282 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1283 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1284 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1285 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1287 [DEV_STATE_WAIT4IO] = {
1288 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1289 [DEV_EVENT_INTERRUPT] = ccw_device_wait4io_irq,
1290 [DEV_EVENT_TIMEOUT] = ccw_device_wait4io_timeout,
1291 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1293 [DEV_STATE_QUIESCE] = {
1294 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1295 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1296 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1297 [DEV_EVENT_VERIFY] = ccw_device_nop,
1299 /* special states for devices gone not operational */
1300 [DEV_STATE_DISCONNECTED] = {
1301 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1302 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1303 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1304 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1306 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1307 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1308 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1309 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1310 [DEV_EVENT_VERIFY] = ccw_device_nop,
1312 [DEV_STATE_CMFCHANGE] = {
1313 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1314 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1315 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1316 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1318 [DEV_STATE_CMFUPDATE] = {
1319 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1320 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1321 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1322 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1327 * io_subchannel_irq is called for "real" interrupts or for status
1328 * pending conditions on msch.
1331 io_subchannel_irq (struct device *pdev)
1333 struct ccw_device *cdev;
1335 cdev = to_subchannel(pdev)->dev.driver_data;
1337 CIO_TRACE_EVENT (3, "IRQ");
1338 CIO_TRACE_EVENT (3, pdev->bus_id);
1340 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1343 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);