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 ccw_device_set_timeout(cdev, 0);
354 if (state != DEV_STATE_ONLINE)
355 cio_disable_subchannel(sch);
357 /* Reset device status. */
358 memset(&cdev->private->irb, 0, sizeof(struct irb));
360 cdev->private->state = state;
363 if (state == DEV_STATE_BOXED)
364 CIO_DEBUG(KERN_WARNING, 2,
365 "Boxed device %04x on subchannel %04x\n",
366 cdev->private->devno, sch->schid.sch_no);
368 if (cdev->private->flags.donotify) {
369 cdev->private->flags.donotify = 0;
370 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify,
372 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
374 wake_up(&cdev->private->wait_q);
376 if (css_init_done && state != DEV_STATE_ONLINE)
377 put_device (&cdev->dev);
380 static inline int cmp_pgid(struct pgid *p1, struct pgid *p2)
388 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
391 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
397 for (i = 0; i < 8; i++) {
398 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
401 if (cdev->private->pgid[last].inf.ps.state1 ==
403 /* First non-zero PGID */
407 if (cmp_pgid(&cdev->private->pgid[i],
408 &cdev->private->pgid[last]) == 0)
409 /* Non-conflicting PGIDs */
412 /* PGID mismatch, can't pathgroup. */
413 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
414 "0.%x.%04x, can't pathgroup\n",
415 cdev->private->ssid, cdev->private->devno);
416 cdev->private->options.pgroup = 0;
419 if (cdev->private->pgid[last].inf.ps.state1 ==
421 /* No previous pgid found */
422 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
423 sizeof(struct pgid));
425 /* Use existing pgid */
426 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
427 sizeof(struct pgid));
431 * Function called from device_pgid.c after sense path ground has completed.
434 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
436 struct subchannel *sch;
438 sch = to_subchannel(cdev->dev.parent);
440 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
441 cdev->private->options.pgroup = 0;
443 case 0: /* success */
444 case -EACCES: /* partial success, some paths not operational */
445 /* Check if all pgids are equal or 0. */
446 __ccw_device_get_common_pgid(cdev);
448 case -ETIME: /* Sense path group id stopped by timeout. */
449 case -EUSERS: /* device is reserved for someone else. */
450 ccw_device_done(cdev, DEV_STATE_BOXED);
453 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
456 /* Start Path Group verification. */
457 cdev->private->state = DEV_STATE_VERIFY;
458 cdev->private->flags.doverify = 0;
459 ccw_device_verify_start(cdev);
463 * Start device recognition.
466 ccw_device_recognition(struct ccw_device *cdev)
468 struct subchannel *sch;
471 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
472 (cdev->private->state != DEV_STATE_BOXED))
474 sch = to_subchannel(cdev->dev.parent);
475 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
477 /* Couldn't enable the subchannel for i/o. Sick device. */
480 /* After 60s the device recognition is considered to have failed. */
481 ccw_device_set_timeout(cdev, 60*HZ);
484 * We used to start here with a sense pgid to find out whether a device
485 * is locked by someone else. Unfortunately, the sense pgid command
486 * code has other meanings on devices predating the path grouping
487 * algorithm, so we start with sense id and box the device after an
488 * timeout (or if sense pgid during path verification detects the device
489 * is locked, as may happen on newer devices).
491 cdev->private->flags.recog_done = 0;
492 cdev->private->state = DEV_STATE_SENSE_ID;
493 ccw_device_sense_id_start(cdev);
498 * Handle timeout in device recognition.
501 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
505 ret = ccw_device_cancel_halt_clear(cdev);
508 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
511 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
514 ccw_device_set_timeout(cdev, 3*HZ);
520 ccw_device_nopath_notify(void *data)
522 struct ccw_device *cdev;
523 struct subchannel *sch;
526 cdev = (struct ccw_device *)data;
527 sch = to_subchannel(cdev->dev.parent);
531 ret = (sch->driver && sch->driver->notify) ?
532 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
534 if (get_device(&sch->dev)) {
535 /* Driver doesn't want to keep device. */
536 cio_disable_subchannel(sch);
537 if (get_device(&cdev->dev)) {
538 PREPARE_WORK(&cdev->private->kick_work,
539 ccw_device_call_sch_unregister,
541 queue_work(ccw_device_work,
542 &cdev->private->kick_work);
544 put_device(&sch->dev);
547 cio_disable_subchannel(sch);
548 ccw_device_set_timeout(cdev, 0);
549 cdev->private->flags.fake_irb = 0;
550 cdev->private->state = DEV_STATE_DISCONNECTED;
551 wake_up(&cdev->private->wait_q);
556 ccw_device_verify_done(struct ccw_device *cdev, int err)
558 struct subchannel *sch;
560 sch = to_subchannel(cdev->dev.parent);
561 /* Update schib - pom may have changed. */
562 stsch(sch->schid, &sch->schib);
563 /* Update lpm with verified path mask. */
565 /* Repeat path verification? */
566 if (cdev->private->flags.doverify) {
567 cdev->private->flags.doverify = 0;
568 ccw_device_verify_start(cdev);
572 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
573 cdev->private->options.pgroup = 0;
575 ccw_device_done(cdev, DEV_STATE_ONLINE);
576 /* Deliver fake irb to device driver, if needed. */
577 if (cdev->private->flags.fake_irb) {
578 memset(&cdev->private->irb, 0, sizeof(struct irb));
579 cdev->private->irb.scsw.cc = 1;
580 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
581 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
582 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
583 cdev->private->flags.fake_irb = 0;
585 cdev->handler(cdev, cdev->private->intparm,
586 &cdev->private->irb);
587 memset(&cdev->private->irb, 0, sizeof(struct irb));
591 ccw_device_done(cdev, DEV_STATE_BOXED);
594 PREPARE_WORK(&cdev->private->kick_work,
595 ccw_device_nopath_notify, (void *)cdev);
596 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
597 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
606 ccw_device_online(struct ccw_device *cdev)
608 struct subchannel *sch;
611 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
612 (cdev->private->state != DEV_STATE_BOXED))
614 sch = to_subchannel(cdev->dev.parent);
615 if (css_init_done && !get_device(&cdev->dev))
617 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
619 /* Couldn't enable the subchannel for i/o. Sick device. */
621 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
624 /* Do we want to do path grouping? */
625 if (!cdev->private->options.pgroup) {
626 /* Start initial path verification. */
627 cdev->private->state = DEV_STATE_VERIFY;
628 cdev->private->flags.doverify = 0;
629 ccw_device_verify_start(cdev);
632 /* Do a SensePGID first. */
633 cdev->private->state = DEV_STATE_SENSE_PGID;
634 ccw_device_sense_pgid_start(cdev);
639 ccw_device_disband_done(struct ccw_device *cdev, int err)
643 ccw_device_done(cdev, DEV_STATE_OFFLINE);
646 ccw_device_done(cdev, DEV_STATE_BOXED);
649 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
658 ccw_device_offline(struct ccw_device *cdev)
660 struct subchannel *sch;
662 sch = to_subchannel(cdev->dev.parent);
663 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
665 if (cdev->private->state != DEV_STATE_ONLINE) {
666 if (sch->schib.scsw.actl != 0)
670 if (sch->schib.scsw.actl != 0)
672 /* Are we doing path grouping? */
673 if (!cdev->private->options.pgroup) {
674 /* No, set state offline immediately. */
675 ccw_device_done(cdev, DEV_STATE_OFFLINE);
678 /* Start Set Path Group commands. */
679 cdev->private->state = DEV_STATE_DISBAND_PGID;
680 ccw_device_disband_start(cdev);
685 * Handle timeout in device online/offline process.
688 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
692 ret = ccw_device_cancel_halt_clear(cdev);
695 ccw_device_done(cdev, DEV_STATE_BOXED);
698 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
701 ccw_device_set_timeout(cdev, 3*HZ);
706 * Handle not oper event in device recognition.
709 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
711 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
715 * Handle not operational event while offline.
718 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
720 struct subchannel *sch;
722 cdev->private->state = DEV_STATE_NOT_OPER;
723 sch = to_subchannel(cdev->dev.parent);
724 if (get_device(&cdev->dev)) {
725 PREPARE_WORK(&cdev->private->kick_work,
726 ccw_device_call_sch_unregister, (void *)cdev);
727 queue_work(ccw_device_work, &cdev->private->kick_work);
729 wake_up(&cdev->private->wait_q);
733 * Handle not operational event while online.
736 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
738 struct subchannel *sch;
740 sch = to_subchannel(cdev->dev.parent);
741 if (sch->driver->notify &&
742 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
743 ccw_device_set_timeout(cdev, 0);
744 cdev->private->flags.fake_irb = 0;
745 cdev->private->state = DEV_STATE_DISCONNECTED;
746 wake_up(&cdev->private->wait_q);
749 cdev->private->state = DEV_STATE_NOT_OPER;
750 cio_disable_subchannel(sch);
751 if (sch->schib.scsw.actl != 0) {
752 // FIXME: not-oper indication to device driver ?
753 ccw_device_call_handler(cdev);
755 if (get_device(&cdev->dev)) {
756 PREPARE_WORK(&cdev->private->kick_work,
757 ccw_device_call_sch_unregister, (void *)cdev);
758 queue_work(ccw_device_work, &cdev->private->kick_work);
760 wake_up(&cdev->private->wait_q);
764 * Handle path verification event.
767 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
769 struct subchannel *sch;
771 if (cdev->private->state == DEV_STATE_W4SENSE) {
772 cdev->private->flags.doverify = 1;
775 sch = to_subchannel(cdev->dev.parent);
777 * Since we might not just be coming from an interrupt from the
778 * subchannel we have to update the schib.
780 stsch(sch->schid, &sch->schib);
782 if (sch->schib.scsw.actl != 0 ||
783 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
784 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
786 * No final status yet or final status not yet delivered
787 * to the device driver. Can't do path verfication now,
788 * delay until final status was delivered.
790 cdev->private->flags.doverify = 1;
793 /* Device is idle, we can do the path verification. */
794 cdev->private->state = DEV_STATE_VERIFY;
795 cdev->private->flags.doverify = 0;
796 ccw_device_verify_start(cdev);
800 * Got an interrupt for a normal io (state online).
803 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
807 irb = (struct irb *) __LC_IRB;
808 /* Check for unsolicited interrupt. */
809 if ((irb->scsw.stctl ==
810 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
811 && (!irb->scsw.cc)) {
812 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
813 !irb->esw.esw0.erw.cons) {
814 /* Unit check but no sense data. Need basic sense. */
815 if (ccw_device_do_sense(cdev, irb) != 0)
816 goto call_handler_unsol;
817 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
818 cdev->private->state = DEV_STATE_W4SENSE;
819 cdev->private->intparm = 0;
824 cdev->handler (cdev, 0, irb);
827 /* Accumulate status and find out if a basic sense is needed. */
828 ccw_device_accumulate_irb(cdev, irb);
829 if (cdev->private->flags.dosense) {
830 if (ccw_device_do_sense(cdev, irb) == 0) {
831 cdev->private->state = DEV_STATE_W4SENSE;
835 /* Call the handler. */
836 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
837 /* Start delayed path verification. */
838 ccw_device_online_verify(cdev, 0);
842 * Got an timeout in online state.
845 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
849 ccw_device_set_timeout(cdev, 0);
850 ret = ccw_device_cancel_halt_clear(cdev);
852 ccw_device_set_timeout(cdev, 3*HZ);
853 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
856 if (ret == -ENODEV) {
857 struct subchannel *sch;
859 sch = to_subchannel(cdev->dev.parent);
861 PREPARE_WORK(&cdev->private->kick_work,
862 ccw_device_nopath_notify, (void *)cdev);
863 queue_work(ccw_device_notify_work,
864 &cdev->private->kick_work);
866 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
867 } else if (cdev->handler)
868 cdev->handler(cdev, cdev->private->intparm,
869 ERR_PTR(-ETIMEDOUT));
873 * Got an interrupt for a basic sense.
876 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
880 irb = (struct irb *) __LC_IRB;
881 /* Check for unsolicited interrupt. */
882 if (irb->scsw.stctl ==
883 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
884 if (irb->scsw.cc == 1)
885 /* Basic sense hasn't started. Try again. */
886 ccw_device_do_sense(cdev, irb);
888 printk("Huh? %s(%s): unsolicited interrupt...\n",
889 __FUNCTION__, cdev->dev.bus_id);
891 cdev->handler (cdev, 0, irb);
896 * Check if a halt or clear has been issued in the meanwhile. If yes,
897 * only deliver the halt/clear interrupt to the device driver as if it
898 * had killed the original request.
900 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
901 cdev->private->flags.dosense = 0;
902 memset(&cdev->private->irb, 0, sizeof(struct irb));
903 ccw_device_accumulate_irb(cdev, irb);
906 /* Add basic sense info to irb. */
907 ccw_device_accumulate_basic_sense(cdev, irb);
908 if (cdev->private->flags.dosense) {
909 /* Another basic sense is needed. */
910 ccw_device_do_sense(cdev, irb);
914 cdev->private->state = DEV_STATE_ONLINE;
915 /* Call the handler. */
916 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
917 /* Start delayed path verification. */
918 ccw_device_online_verify(cdev, 0);
922 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
926 irb = (struct irb *) __LC_IRB;
927 /* Accumulate status. We don't do basic sense. */
928 ccw_device_accumulate_irb(cdev, irb);
929 /* Remember to clear irb to avoid residuals. */
930 memset(&cdev->private->irb, 0, sizeof(struct irb));
931 /* Try to start delayed device verification. */
932 ccw_device_online_verify(cdev, 0);
933 /* Note: Don't call handler for cio initiated clear! */
937 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
939 struct subchannel *sch;
941 sch = to_subchannel(cdev->dev.parent);
942 ccw_device_set_timeout(cdev, 0);
943 /* OK, i/o is dead now. Call interrupt handler. */
944 cdev->private->state = DEV_STATE_ONLINE;
946 cdev->handler(cdev, cdev->private->intparm,
947 ERR_PTR(-ETIMEDOUT));
949 PREPARE_WORK(&cdev->private->kick_work,
950 ccw_device_nopath_notify, (void *)cdev);
951 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
952 } else if (cdev->private->flags.doverify)
953 /* Start delayed path verification. */
954 ccw_device_online_verify(cdev, 0);
958 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
962 ret = ccw_device_cancel_halt_clear(cdev);
964 ccw_device_set_timeout(cdev, 3*HZ);
967 if (ret == -ENODEV) {
968 struct subchannel *sch;
970 sch = to_subchannel(cdev->dev.parent);
972 PREPARE_WORK(&cdev->private->kick_work,
973 ccw_device_nopath_notify, (void *)cdev);
974 queue_work(ccw_device_notify_work,
975 &cdev->private->kick_work);
977 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
980 //FIXME: Can we get here?
981 cdev->private->state = DEV_STATE_ONLINE;
983 cdev->handler(cdev, cdev->private->intparm,
984 ERR_PTR(-ETIMEDOUT));
988 ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event)
991 struct subchannel *sch;
993 irb = (struct irb *) __LC_IRB;
995 * Accumulate status and find out if a basic sense is needed.
996 * This is fine since we have already adapted the lpm.
998 ccw_device_accumulate_irb(cdev, irb);
999 if (cdev->private->flags.dosense) {
1000 if (ccw_device_do_sense(cdev, irb) == 0) {
1001 cdev->private->state = DEV_STATE_W4SENSE;
1006 /* Iff device is idle, reset timeout. */
1007 sch = to_subchannel(cdev->dev.parent);
1008 if (!stsch(sch->schid, &sch->schib))
1009 if (sch->schib.scsw.actl == 0)
1010 ccw_device_set_timeout(cdev, 0);
1011 /* Call the handler. */
1012 ccw_device_call_handler(cdev);
1014 PREPARE_WORK(&cdev->private->kick_work,
1015 ccw_device_nopath_notify, (void *)cdev);
1016 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1017 } else if (cdev->private->flags.doverify)
1018 ccw_device_online_verify(cdev, 0);
1022 ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1025 struct subchannel *sch;
1027 sch = to_subchannel(cdev->dev.parent);
1028 ccw_device_set_timeout(cdev, 0);
1029 ret = ccw_device_cancel_halt_clear(cdev);
1030 if (ret == -EBUSY) {
1031 ccw_device_set_timeout(cdev, 3*HZ);
1032 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1035 if (ret == -ENODEV) {
1037 PREPARE_WORK(&cdev->private->kick_work,
1038 ccw_device_nopath_notify, (void *)cdev);
1039 queue_work(ccw_device_notify_work,
1040 &cdev->private->kick_work);
1042 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1046 cdev->handler(cdev, cdev->private->intparm,
1047 ERR_PTR(-ETIMEDOUT));
1049 PREPARE_WORK(&cdev->private->kick_work,
1050 ccw_device_nopath_notify, (void *)cdev);
1051 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1052 } else if (cdev->private->flags.doverify)
1053 /* Start delayed path verification. */
1054 ccw_device_online_verify(cdev, 0);
1058 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
1060 /* Start verification after current task finished. */
1061 cdev->private->flags.doverify = 1;
1065 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1069 switch (dev_event) {
1070 case DEV_EVENT_INTERRUPT:
1071 irb = (struct irb *) __LC_IRB;
1072 /* Check for unsolicited interrupt. */
1073 if ((irb->scsw.stctl ==
1074 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1076 /* FIXME: we should restart stlck here, but this
1077 * is extremely unlikely ... */
1080 ccw_device_accumulate_irb(cdev, irb);
1081 /* We don't care about basic sense etc. */
1083 default: /* timeout */
1087 wake_up(&cdev->private->wait_q);
1091 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1093 struct subchannel *sch;
1095 sch = to_subchannel(cdev->dev.parent);
1096 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1097 /* Couldn't enable the subchannel for i/o. Sick device. */
1100 /* After 60s the device recognition is considered to have failed. */
1101 ccw_device_set_timeout(cdev, 60*HZ);
1103 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1104 ccw_device_sense_id_start(cdev);
1108 device_trigger_reprobe(struct subchannel *sch)
1110 struct ccw_device *cdev;
1112 if (!sch->dev.driver_data)
1114 cdev = sch->dev.driver_data;
1115 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1118 /* Update some values. */
1119 if (stsch(sch->schid, &sch->schib))
1123 * The pim, pam, pom values may not be accurate, but they are the best
1124 * we have before performing device selection :/
1126 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1127 /* Re-set some bits in the pmcw that were lost. */
1128 sch->schib.pmcw.isc = 3;
1129 sch->schib.pmcw.csense = 1;
1130 sch->schib.pmcw.ena = 0;
1131 if ((sch->lpm & (sch->lpm - 1)) != 0)
1132 sch->schib.pmcw.mp = 1;
1133 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1134 /* We should also udate ssd info, but this has to wait. */
1135 ccw_device_start_id(cdev, 0);
1139 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1141 struct subchannel *sch;
1143 sch = to_subchannel(cdev->dev.parent);
1145 * An interrupt in state offline means a previous disable was not
1146 * successful. Try again.
1148 cio_disable_subchannel(sch);
1152 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1154 retry_set_schib(cdev);
1155 cdev->private->state = DEV_STATE_ONLINE;
1156 dev_fsm_event(cdev, dev_event);
1159 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1160 enum dev_event dev_event)
1162 cmf_retry_copy_block(cdev);
1163 cdev->private->state = DEV_STATE_ONLINE;
1164 dev_fsm_event(cdev, dev_event);
1168 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1170 ccw_device_set_timeout(cdev, 0);
1171 if (dev_event == DEV_EVENT_NOTOPER)
1172 cdev->private->state = DEV_STATE_NOT_OPER;
1174 cdev->private->state = DEV_STATE_OFFLINE;
1175 wake_up(&cdev->private->wait_q);
1179 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1183 ret = ccw_device_cancel_halt_clear(cdev);
1186 cdev->private->state = DEV_STATE_OFFLINE;
1187 wake_up(&cdev->private->wait_q);
1190 cdev->private->state = DEV_STATE_NOT_OPER;
1191 wake_up(&cdev->private->wait_q);
1194 ccw_device_set_timeout(cdev, HZ/10);
1199 * No operation action. This is used e.g. to ignore a timeout event in
1203 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1208 * Bug operation action.
1211 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1213 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1214 cdev->private->state, dev_event);
1219 * device statemachine
1221 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1222 [DEV_STATE_NOT_OPER] = {
1223 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1224 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1225 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1226 [DEV_EVENT_VERIFY] = ccw_device_nop,
1228 [DEV_STATE_SENSE_PGID] = {
1229 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1230 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1231 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1232 [DEV_EVENT_VERIFY] = ccw_device_nop,
1234 [DEV_STATE_SENSE_ID] = {
1235 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1236 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1237 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1238 [DEV_EVENT_VERIFY] = ccw_device_nop,
1240 [DEV_STATE_OFFLINE] = {
1241 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1242 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1243 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1244 [DEV_EVENT_VERIFY] = ccw_device_nop,
1246 [DEV_STATE_VERIFY] = {
1247 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1248 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1249 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1250 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1252 [DEV_STATE_ONLINE] = {
1253 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1254 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1255 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1256 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1258 [DEV_STATE_W4SENSE] = {
1259 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1260 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1261 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1262 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1264 [DEV_STATE_DISBAND_PGID] = {
1265 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1266 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1267 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1268 [DEV_EVENT_VERIFY] = ccw_device_nop,
1270 [DEV_STATE_BOXED] = {
1271 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1272 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1273 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1274 [DEV_EVENT_VERIFY] = ccw_device_nop,
1276 /* states to wait for i/o completion before doing something */
1277 [DEV_STATE_CLEAR_VERIFY] = {
1278 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1279 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1280 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1281 [DEV_EVENT_VERIFY] = ccw_device_nop,
1283 [DEV_STATE_TIMEOUT_KILL] = {
1284 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1285 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1286 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1287 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1289 [DEV_STATE_WAIT4IO] = {
1290 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1291 [DEV_EVENT_INTERRUPT] = ccw_device_wait4io_irq,
1292 [DEV_EVENT_TIMEOUT] = ccw_device_wait4io_timeout,
1293 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1295 [DEV_STATE_QUIESCE] = {
1296 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1297 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1298 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1299 [DEV_EVENT_VERIFY] = ccw_device_nop,
1301 /* special states for devices gone not operational */
1302 [DEV_STATE_DISCONNECTED] = {
1303 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1304 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1305 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1306 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1308 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1309 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1310 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1311 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1312 [DEV_EVENT_VERIFY] = ccw_device_nop,
1314 [DEV_STATE_CMFCHANGE] = {
1315 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1316 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1317 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1318 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1320 [DEV_STATE_CMFUPDATE] = {
1321 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1322 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1323 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1324 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1329 * io_subchannel_irq is called for "real" interrupts or for status
1330 * pending conditions on msch.
1333 io_subchannel_irq (struct device *pdev)
1335 struct ccw_device *cdev;
1337 cdev = to_subchannel(pdev)->dev.driver_data;
1339 CIO_TRACE_EVENT (3, "IRQ");
1340 CIO_TRACE_EVENT (3, pdev->bus_id);
1342 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1345 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);