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--;
155 return (ret == 0) ? -EBUSY : ret;
157 /* halt io unsuccessful. */
158 cdev->private->iretry = 255; /* 255 clear retries. */
160 /* Stage 3: clear io. */
161 if (cdev->private->iretry) {
162 cdev->private->iretry--;
163 ret = cio_clear (sch);
164 return (ret == 0) ? -EBUSY : ret;
166 panic("Can't stop i/o on subchannel.\n");
170 ccw_device_handle_oper(struct ccw_device *cdev)
172 struct subchannel *sch;
174 sch = to_subchannel(cdev->dev.parent);
175 cdev->private->flags.recog_done = 1;
177 * Check if cu type and device type still match. If
178 * not, it is certainly another device and we have to
179 * de- and re-register. Also check here for non-matching devno.
181 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
182 cdev->id.cu_model != cdev->private->senseid.cu_model ||
183 cdev->id.dev_type != cdev->private->senseid.dev_type ||
184 cdev->id.dev_model != cdev->private->senseid.dev_model ||
185 cdev->private->devno != sch->schib.pmcw.dev) {
186 PREPARE_WORK(&cdev->private->kick_work,
187 ccw_device_do_unreg_rereg, (void *)cdev);
188 queue_work(ccw_device_work, &cdev->private->kick_work);
191 cdev->private->flags.donotify = 1;
196 * The machine won't give us any notification by machine check if a chpid has
197 * been varied online on the SE so we have to find out by magic (i. e. driving
198 * the channel subsystem to device selection and updating our path masks).
201 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
205 for (i = 0; i<8; i++) {
207 if (!(sch->lpm & mask))
211 chpid_is_actually_online(sch->schib.pmcw.chpid[i]);
216 * Stop device recognition.
219 ccw_device_recog_done(struct ccw_device *cdev, int state)
221 struct subchannel *sch;
222 int notify, old_lpm, same_dev;
224 sch = to_subchannel(cdev->dev.parent);
226 ccw_device_set_timeout(cdev, 0);
227 cio_disable_subchannel(sch);
229 * Now that we tried recognition, we have performed device selection
230 * through ssch() and the path information is up to date.
233 stsch(sch->schid, &sch->schib);
234 sch->lpm = sch->schib.pmcw.pim &
235 sch->schib.pmcw.pam &
236 sch->schib.pmcw.pom &
238 /* Check since device may again have become not operational. */
239 if (!sch->schib.pmcw.dnv)
240 state = DEV_STATE_NOT_OPER;
241 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
242 /* Force reprobe on all chpids. */
244 if (sch->lpm != old_lpm)
245 __recover_lost_chpids(sch, old_lpm);
246 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
247 if (state == DEV_STATE_NOT_OPER) {
248 cdev->private->flags.recog_done = 1;
249 cdev->private->state = DEV_STATE_DISCONNECTED;
252 /* Boxed devices don't need extra treatment. */
255 same_dev = 0; /* Keep the compiler quiet... */
257 case DEV_STATE_NOT_OPER:
258 CIO_DEBUG(KERN_WARNING, 2,
259 "SenseID : unknown device %04x on subchannel "
260 "0.%x.%04x\n", cdev->private->devno,
261 sch->schid.ssid, sch->schid.sch_no);
263 case DEV_STATE_OFFLINE:
264 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
265 same_dev = ccw_device_handle_oper(cdev);
268 /* fill out sense information */
269 cdev->id = (struct ccw_device_id) {
270 .cu_type = cdev->private->senseid.cu_type,
271 .cu_model = cdev->private->senseid.cu_model,
272 .dev_type = cdev->private->senseid.dev_type,
273 .dev_model = cdev->private->senseid.dev_model,
276 cdev->private->state = DEV_STATE_OFFLINE;
278 /* Get device online again. */
279 ccw_device_online(cdev);
280 wake_up(&cdev->private->wait_q);
284 /* Issue device info message. */
285 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
286 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
288 cdev->private->ssid, cdev->private->devno,
289 cdev->id.cu_type, cdev->id.cu_model,
290 cdev->id.dev_type, cdev->id.dev_model);
292 case DEV_STATE_BOXED:
293 CIO_DEBUG(KERN_WARNING, 2,
294 "SenseID : boxed device %04x on subchannel "
295 "0.%x.%04x\n", cdev->private->devno,
296 sch->schid.ssid, sch->schid.sch_no);
299 cdev->private->state = state;
300 io_subchannel_recog_done(cdev);
301 if (state != DEV_STATE_NOT_OPER)
302 wake_up(&cdev->private->wait_q);
306 * Function called from device_id.c after sense id has completed.
309 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
313 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
315 case -ETIME: /* Sense id stopped by timeout. */
316 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
319 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
325 ccw_device_oper_notify(void *data)
327 struct ccw_device *cdev;
328 struct subchannel *sch;
331 cdev = (struct ccw_device *)data;
332 sch = to_subchannel(cdev->dev.parent);
333 ret = (sch->driver && sch->driver->notify) ?
334 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
336 /* Driver doesn't want device back. */
337 ccw_device_do_unreg_rereg((void *)cdev);
339 /* Reenable channel measurements, if needed. */
341 wake_up(&cdev->private->wait_q);
346 * Finished with online/offline processing.
349 ccw_device_done(struct ccw_device *cdev, int state)
351 struct subchannel *sch;
353 sch = to_subchannel(cdev->dev.parent);
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->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);
382 * Function called from device_pgid.c after sense path ground has completed.
385 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
387 struct subchannel *sch;
389 sch = to_subchannel(cdev->dev.parent);
392 /* Start Path Group verification. */
393 sch->vpm = 0; /* Start with no path groups set. */
394 cdev->private->state = DEV_STATE_VERIFY;
395 ccw_device_verify_start(cdev);
397 case -ETIME: /* Sense path group id stopped by timeout. */
398 case -EUSERS: /* device is reserved for someone else. */
399 ccw_device_done(cdev, DEV_STATE_BOXED);
401 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
402 cdev->private->options.pgroup = 0;
403 ccw_device_done(cdev, DEV_STATE_ONLINE);
406 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
412 * Start device recognition.
415 ccw_device_recognition(struct ccw_device *cdev)
417 struct subchannel *sch;
420 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
421 (cdev->private->state != DEV_STATE_BOXED))
423 sch = to_subchannel(cdev->dev.parent);
424 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
426 /* Couldn't enable the subchannel for i/o. Sick device. */
429 /* After 60s the device recognition is considered to have failed. */
430 ccw_device_set_timeout(cdev, 60*HZ);
433 * We used to start here with a sense pgid to find out whether a device
434 * is locked by someone else. Unfortunately, the sense pgid command
435 * code has other meanings on devices predating the path grouping
436 * algorithm, so we start with sense id and box the device after an
437 * timeout (or if sense pgid during path verification detects the device
438 * is locked, as may happen on newer devices).
440 cdev->private->flags.recog_done = 0;
441 cdev->private->state = DEV_STATE_SENSE_ID;
442 ccw_device_sense_id_start(cdev);
447 * Handle timeout in device recognition.
450 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
454 ret = ccw_device_cancel_halt_clear(cdev);
457 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
460 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
463 ccw_device_set_timeout(cdev, 3*HZ);
469 ccw_device_nopath_notify(void *data)
471 struct ccw_device *cdev;
472 struct subchannel *sch;
475 cdev = (struct ccw_device *)data;
476 sch = to_subchannel(cdev->dev.parent);
480 ret = (sch->driver && sch->driver->notify) ?
481 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
483 if (get_device(&sch->dev)) {
484 /* Driver doesn't want to keep device. */
485 cio_disable_subchannel(sch);
486 if (get_device(&cdev->dev)) {
487 PREPARE_WORK(&cdev->private->kick_work,
488 ccw_device_call_sch_unregister,
490 queue_work(ccw_device_work,
491 &cdev->private->kick_work);
493 put_device(&sch->dev);
496 cio_disable_subchannel(sch);
497 ccw_device_set_timeout(cdev, 0);
498 cdev->private->flags.fake_irb = 0;
499 cdev->private->state = DEV_STATE_DISCONNECTED;
500 wake_up(&cdev->private->wait_q);
505 ccw_device_verify_done(struct ccw_device *cdev, int err)
507 cdev->private->flags.doverify = 0;
509 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
510 cdev->private->options.pgroup = 0;
512 ccw_device_done(cdev, DEV_STATE_ONLINE);
513 /* Deliver fake irb to device driver, if needed. */
514 if (cdev->private->flags.fake_irb) {
515 memset(&cdev->private->irb, 0, sizeof(struct irb));
516 cdev->private->irb.scsw = (struct scsw) {
518 .fctl = SCSW_FCTL_START_FUNC,
519 .actl = SCSW_ACTL_START_PEND,
520 .stctl = SCSW_STCTL_STATUS_PEND,
522 cdev->private->flags.fake_irb = 0;
524 cdev->handler(cdev, cdev->private->intparm,
525 &cdev->private->irb);
526 memset(&cdev->private->irb, 0, sizeof(struct irb));
530 ccw_device_done(cdev, DEV_STATE_BOXED);
533 PREPARE_WORK(&cdev->private->kick_work,
534 ccw_device_nopath_notify, (void *)cdev);
535 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
536 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
545 ccw_device_online(struct ccw_device *cdev)
547 struct subchannel *sch;
550 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
551 (cdev->private->state != DEV_STATE_BOXED))
553 sch = to_subchannel(cdev->dev.parent);
554 if (css_init_done && !get_device(&cdev->dev))
556 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
558 /* Couldn't enable the subchannel for i/o. Sick device. */
560 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
563 /* Do we want to do path grouping? */
564 if (!cdev->private->options.pgroup) {
565 /* No, set state online immediately. */
566 ccw_device_done(cdev, DEV_STATE_ONLINE);
569 /* Do a SensePGID first. */
570 cdev->private->state = DEV_STATE_SENSE_PGID;
571 ccw_device_sense_pgid_start(cdev);
576 ccw_device_disband_done(struct ccw_device *cdev, int err)
580 ccw_device_done(cdev, DEV_STATE_OFFLINE);
583 ccw_device_done(cdev, DEV_STATE_BOXED);
586 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
595 ccw_device_offline(struct ccw_device *cdev)
597 struct subchannel *sch;
599 sch = to_subchannel(cdev->dev.parent);
600 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
602 if (cdev->private->state != DEV_STATE_ONLINE) {
603 if (sch->schib.scsw.actl != 0)
607 if (sch->schib.scsw.actl != 0)
609 /* Are we doing path grouping? */
610 if (!cdev->private->options.pgroup) {
611 /* No, set state offline immediately. */
612 ccw_device_done(cdev, DEV_STATE_OFFLINE);
615 /* Start Set Path Group commands. */
616 cdev->private->state = DEV_STATE_DISBAND_PGID;
617 ccw_device_disband_start(cdev);
622 * Handle timeout in device online/offline process.
625 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
629 ret = ccw_device_cancel_halt_clear(cdev);
632 ccw_device_done(cdev, DEV_STATE_BOXED);
635 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
638 ccw_device_set_timeout(cdev, 3*HZ);
643 * Handle not oper event in device recognition.
646 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
648 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
652 * Handle not operational event while offline.
655 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
657 struct subchannel *sch;
659 cdev->private->state = DEV_STATE_NOT_OPER;
660 sch = to_subchannel(cdev->dev.parent);
661 if (get_device(&cdev->dev)) {
662 PREPARE_WORK(&cdev->private->kick_work,
663 ccw_device_call_sch_unregister, (void *)cdev);
664 queue_work(ccw_device_work, &cdev->private->kick_work);
666 wake_up(&cdev->private->wait_q);
670 * Handle not operational event while online.
673 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
675 struct subchannel *sch;
677 sch = to_subchannel(cdev->dev.parent);
678 if (sch->driver->notify &&
679 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
680 ccw_device_set_timeout(cdev, 0);
681 cdev->private->flags.fake_irb = 0;
682 cdev->private->state = DEV_STATE_DISCONNECTED;
683 wake_up(&cdev->private->wait_q);
686 cdev->private->state = DEV_STATE_NOT_OPER;
687 cio_disable_subchannel(sch);
688 if (sch->schib.scsw.actl != 0) {
689 // FIXME: not-oper indication to device driver ?
690 ccw_device_call_handler(cdev);
692 if (get_device(&cdev->dev)) {
693 PREPARE_WORK(&cdev->private->kick_work,
694 ccw_device_call_sch_unregister, (void *)cdev);
695 queue_work(ccw_device_work, &cdev->private->kick_work);
697 wake_up(&cdev->private->wait_q);
701 * Handle path verification event.
704 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
706 struct subchannel *sch;
708 if (!cdev->private->options.pgroup)
710 if (cdev->private->state == DEV_STATE_W4SENSE) {
711 cdev->private->flags.doverify = 1;
714 sch = to_subchannel(cdev->dev.parent);
716 * Since we might not just be coming from an interrupt from the
717 * subchannel we have to update the schib.
719 stsch(sch->schid, &sch->schib);
721 if (sch->schib.scsw.actl != 0 ||
722 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
724 * No final status yet or final status not yet delivered
725 * to the device driver. Can't do path verfication now,
726 * delay until final status was delivered.
728 cdev->private->flags.doverify = 1;
731 /* Device is idle, we can do the path verification. */
732 cdev->private->state = DEV_STATE_VERIFY;
733 ccw_device_verify_start(cdev);
737 * Got an interrupt for a normal io (state online).
740 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
744 irb = (struct irb *) __LC_IRB;
745 /* Check for unsolicited interrupt. */
746 if ((irb->scsw.stctl ==
747 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
748 && (!irb->scsw.cc)) {
749 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
750 !irb->esw.esw0.erw.cons) {
751 /* Unit check but no sense data. Need basic sense. */
752 if (ccw_device_do_sense(cdev, irb) != 0)
753 goto call_handler_unsol;
754 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
755 cdev->private->state = DEV_STATE_W4SENSE;
756 cdev->private->intparm = 0;
761 cdev->handler (cdev, 0, irb);
764 /* Accumulate status and find out if a basic sense is needed. */
765 ccw_device_accumulate_irb(cdev, irb);
766 if (cdev->private->flags.dosense) {
767 if (ccw_device_do_sense(cdev, irb) == 0) {
768 cdev->private->state = DEV_STATE_W4SENSE;
772 /* Call the handler. */
773 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
774 /* Start delayed path verification. */
775 ccw_device_online_verify(cdev, 0);
779 * Got an timeout in online state.
782 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
786 ccw_device_set_timeout(cdev, 0);
787 ret = ccw_device_cancel_halt_clear(cdev);
789 ccw_device_set_timeout(cdev, 3*HZ);
790 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
793 if (ret == -ENODEV) {
794 struct subchannel *sch;
796 sch = to_subchannel(cdev->dev.parent);
798 PREPARE_WORK(&cdev->private->kick_work,
799 ccw_device_nopath_notify, (void *)cdev);
800 queue_work(ccw_device_notify_work,
801 &cdev->private->kick_work);
803 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
804 } else if (cdev->handler)
805 cdev->handler(cdev, cdev->private->intparm,
806 ERR_PTR(-ETIMEDOUT));
810 * Got an interrupt for a basic sense.
813 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
817 irb = (struct irb *) __LC_IRB;
818 /* Check for unsolicited interrupt. */
819 if (irb->scsw.stctl ==
820 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
821 if (irb->scsw.cc == 1)
822 /* Basic sense hasn't started. Try again. */
823 ccw_device_do_sense(cdev, irb);
825 printk("Huh? %s(%s): unsolicited interrupt...\n",
826 __FUNCTION__, cdev->dev.bus_id);
828 cdev->handler (cdev, 0, irb);
833 * Check if a halt or clear has been issued in the meanwhile. If yes,
834 * only deliver the halt/clear interrupt to the device driver as if it
835 * had killed the original request.
837 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
838 cdev->private->flags.dosense = 0;
839 memset(&cdev->private->irb, 0, sizeof(struct irb));
840 ccw_device_accumulate_irb(cdev, irb);
843 /* Add basic sense info to irb. */
844 ccw_device_accumulate_basic_sense(cdev, irb);
845 if (cdev->private->flags.dosense) {
846 /* Another basic sense is needed. */
847 ccw_device_do_sense(cdev, irb);
851 cdev->private->state = DEV_STATE_ONLINE;
852 /* Call the handler. */
853 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
854 /* Start delayed path verification. */
855 ccw_device_online_verify(cdev, 0);
859 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
863 irb = (struct irb *) __LC_IRB;
864 /* Accumulate status. We don't do basic sense. */
865 ccw_device_accumulate_irb(cdev, irb);
866 /* Remember to clear irb to avoid residuals. */
867 memset(&cdev->private->irb, 0, sizeof(struct irb));
868 /* Try to start delayed device verification. */
869 ccw_device_online_verify(cdev, 0);
870 /* Note: Don't call handler for cio initiated clear! */
874 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
876 struct subchannel *sch;
878 sch = to_subchannel(cdev->dev.parent);
879 ccw_device_set_timeout(cdev, 0);
880 /* OK, i/o is dead now. Call interrupt handler. */
881 cdev->private->state = DEV_STATE_ONLINE;
883 cdev->handler(cdev, cdev->private->intparm,
884 ERR_PTR(-ETIMEDOUT));
886 PREPARE_WORK(&cdev->private->kick_work,
887 ccw_device_nopath_notify, (void *)cdev);
888 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
889 } else if (cdev->private->flags.doverify)
890 /* Start delayed path verification. */
891 ccw_device_online_verify(cdev, 0);
895 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
899 ret = ccw_device_cancel_halt_clear(cdev);
901 ccw_device_set_timeout(cdev, 3*HZ);
904 if (ret == -ENODEV) {
905 struct subchannel *sch;
907 sch = to_subchannel(cdev->dev.parent);
909 PREPARE_WORK(&cdev->private->kick_work,
910 ccw_device_nopath_notify, (void *)cdev);
911 queue_work(ccw_device_notify_work,
912 &cdev->private->kick_work);
914 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
917 //FIXME: Can we get here?
918 cdev->private->state = DEV_STATE_ONLINE;
920 cdev->handler(cdev, cdev->private->intparm,
921 ERR_PTR(-ETIMEDOUT));
925 ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event)
928 struct subchannel *sch;
930 irb = (struct irb *) __LC_IRB;
932 * Accumulate status and find out if a basic sense is needed.
933 * This is fine since we have already adapted the lpm.
935 ccw_device_accumulate_irb(cdev, irb);
936 if (cdev->private->flags.dosense) {
937 if (ccw_device_do_sense(cdev, irb) == 0) {
938 cdev->private->state = DEV_STATE_W4SENSE;
943 /* Iff device is idle, reset timeout. */
944 sch = to_subchannel(cdev->dev.parent);
945 if (!stsch(sch->schid, &sch->schib))
946 if (sch->schib.scsw.actl == 0)
947 ccw_device_set_timeout(cdev, 0);
948 /* Call the handler. */
949 ccw_device_call_handler(cdev);
951 PREPARE_WORK(&cdev->private->kick_work,
952 ccw_device_nopath_notify, (void *)cdev);
953 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
954 } else if (cdev->private->flags.doverify)
955 ccw_device_online_verify(cdev, 0);
959 ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event)
962 struct subchannel *sch;
964 sch = to_subchannel(cdev->dev.parent);
965 ccw_device_set_timeout(cdev, 0);
966 ret = ccw_device_cancel_halt_clear(cdev);
968 ccw_device_set_timeout(cdev, 3*HZ);
969 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
972 if (ret == -ENODEV) {
974 PREPARE_WORK(&cdev->private->kick_work,
975 ccw_device_nopath_notify, (void *)cdev);
976 queue_work(ccw_device_notify_work,
977 &cdev->private->kick_work);
979 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
983 cdev->handler(cdev, cdev->private->intparm,
984 ERR_PTR(-ETIMEDOUT));
986 PREPARE_WORK(&cdev->private->kick_work,
987 ccw_device_nopath_notify, (void *)cdev);
988 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
989 } else if (cdev->private->flags.doverify)
990 /* Start delayed path verification. */
991 ccw_device_online_verify(cdev, 0);
995 ccw_device_wait4io_verify(struct ccw_device *cdev, enum dev_event dev_event)
997 /* When the I/O has terminated, we have to start verification. */
998 if (cdev->private->options.pgroup)
999 cdev->private->flags.doverify = 1;
1003 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1007 switch (dev_event) {
1008 case DEV_EVENT_INTERRUPT:
1009 irb = (struct irb *) __LC_IRB;
1010 /* Check for unsolicited interrupt. */
1011 if ((irb->scsw.stctl ==
1012 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1014 /* FIXME: we should restart stlck here, but this
1015 * is extremely unlikely ... */
1018 ccw_device_accumulate_irb(cdev, irb);
1019 /* We don't care about basic sense etc. */
1021 default: /* timeout */
1025 wake_up(&cdev->private->wait_q);
1029 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1031 struct subchannel *sch;
1033 sch = to_subchannel(cdev->dev.parent);
1034 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1035 /* Couldn't enable the subchannel for i/o. Sick device. */
1038 /* After 60s the device recognition is considered to have failed. */
1039 ccw_device_set_timeout(cdev, 60*HZ);
1041 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1042 ccw_device_sense_id_start(cdev);
1046 device_trigger_reprobe(struct subchannel *sch)
1048 struct ccw_device *cdev;
1050 if (!sch->dev.driver_data)
1052 cdev = sch->dev.driver_data;
1053 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1056 /* Update some values. */
1057 if (stsch(sch->schid, &sch->schib))
1061 * The pim, pam, pom values may not be accurate, but they are the best
1062 * we have before performing device selection :/
1064 sch->lpm = sch->schib.pmcw.pim &
1065 sch->schib.pmcw.pam &
1066 sch->schib.pmcw.pom &
1068 /* Re-set some bits in the pmcw that were lost. */
1069 sch->schib.pmcw.isc = 3;
1070 sch->schib.pmcw.csense = 1;
1071 sch->schib.pmcw.ena = 0;
1072 if ((sch->lpm & (sch->lpm - 1)) != 0)
1073 sch->schib.pmcw.mp = 1;
1074 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1075 /* We should also udate ssd info, but this has to wait. */
1076 ccw_device_start_id(cdev, 0);
1080 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1082 struct subchannel *sch;
1084 sch = to_subchannel(cdev->dev.parent);
1086 * An interrupt in state offline means a previous disable was not
1087 * successful. Try again.
1089 cio_disable_subchannel(sch);
1093 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1095 retry_set_schib(cdev);
1096 cdev->private->state = DEV_STATE_ONLINE;
1097 dev_fsm_event(cdev, dev_event);
1100 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1101 enum dev_event dev_event)
1103 cmf_retry_copy_block(cdev);
1104 cdev->private->state = DEV_STATE_ONLINE;
1105 dev_fsm_event(cdev, dev_event);
1109 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1111 ccw_device_set_timeout(cdev, 0);
1112 if (dev_event == DEV_EVENT_NOTOPER)
1113 cdev->private->state = DEV_STATE_NOT_OPER;
1115 cdev->private->state = DEV_STATE_OFFLINE;
1116 wake_up(&cdev->private->wait_q);
1120 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1124 ret = ccw_device_cancel_halt_clear(cdev);
1127 cdev->private->state = DEV_STATE_OFFLINE;
1128 wake_up(&cdev->private->wait_q);
1131 cdev->private->state = DEV_STATE_NOT_OPER;
1132 wake_up(&cdev->private->wait_q);
1135 ccw_device_set_timeout(cdev, HZ/10);
1140 * No operation action. This is used e.g. to ignore a timeout event in
1144 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1149 * Bug operation action.
1152 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1154 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1155 cdev->private->state, dev_event);
1160 * device statemachine
1162 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1163 [DEV_STATE_NOT_OPER] = {
1164 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1165 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1166 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1167 [DEV_EVENT_VERIFY] = ccw_device_nop,
1169 [DEV_STATE_SENSE_PGID] = {
1170 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1171 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1172 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1173 [DEV_EVENT_VERIFY] = ccw_device_nop,
1175 [DEV_STATE_SENSE_ID] = {
1176 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1177 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1178 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1179 [DEV_EVENT_VERIFY] = ccw_device_nop,
1181 [DEV_STATE_OFFLINE] = {
1182 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1183 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1184 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1185 [DEV_EVENT_VERIFY] = ccw_device_nop,
1187 [DEV_STATE_VERIFY] = {
1188 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1189 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1190 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1191 [DEV_EVENT_VERIFY] = ccw_device_nop,
1193 [DEV_STATE_ONLINE] = {
1194 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1195 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1196 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1197 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1199 [DEV_STATE_W4SENSE] = {
1200 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1201 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1202 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1203 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1205 [DEV_STATE_DISBAND_PGID] = {
1206 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1207 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1208 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1209 [DEV_EVENT_VERIFY] = ccw_device_nop,
1211 [DEV_STATE_BOXED] = {
1212 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1213 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1214 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1215 [DEV_EVENT_VERIFY] = ccw_device_nop,
1217 /* states to wait for i/o completion before doing something */
1218 [DEV_STATE_CLEAR_VERIFY] = {
1219 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1220 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1221 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1222 [DEV_EVENT_VERIFY] = ccw_device_nop,
1224 [DEV_STATE_TIMEOUT_KILL] = {
1225 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1226 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1227 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1228 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1230 [DEV_STATE_WAIT4IO] = {
1231 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1232 [DEV_EVENT_INTERRUPT] = ccw_device_wait4io_irq,
1233 [DEV_EVENT_TIMEOUT] = ccw_device_wait4io_timeout,
1234 [DEV_EVENT_VERIFY] = ccw_device_wait4io_verify,
1236 [DEV_STATE_QUIESCE] = {
1237 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1238 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1239 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1240 [DEV_EVENT_VERIFY] = ccw_device_nop,
1242 /* special states for devices gone not operational */
1243 [DEV_STATE_DISCONNECTED] = {
1244 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1245 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1246 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1247 [DEV_EVENT_VERIFY] = ccw_device_nop,
1249 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1250 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1251 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1252 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1253 [DEV_EVENT_VERIFY] = ccw_device_nop,
1255 [DEV_STATE_CMFCHANGE] = {
1256 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1257 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1258 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1259 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1261 [DEV_STATE_CMFUPDATE] = {
1262 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1263 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1264 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1265 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1270 * io_subchannel_irq is called for "real" interrupts or for status
1271 * pending conditions on msch.
1274 io_subchannel_irq (struct device *pdev)
1276 struct ccw_device *cdev;
1278 cdev = to_subchannel(pdev)->dev.driver_data;
1280 CIO_TRACE_EVENT (3, "IRQ");
1281 CIO_TRACE_EVENT (3, pdev->bus_id);
1283 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1286 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);