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.pim &
236 sch->schib.pmcw.pam &
237 sch->schib.pmcw.pom &
239 /* Check since device may again have become not operational. */
240 if (!sch->schib.pmcw.dnv)
241 state = DEV_STATE_NOT_OPER;
242 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
243 /* Force reprobe on all chpids. */
245 if (sch->lpm != old_lpm)
246 __recover_lost_chpids(sch, old_lpm);
247 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
248 if (state == DEV_STATE_NOT_OPER) {
249 cdev->private->flags.recog_done = 1;
250 cdev->private->state = DEV_STATE_DISCONNECTED;
253 /* Boxed devices don't need extra treatment. */
256 same_dev = 0; /* Keep the compiler quiet... */
258 case DEV_STATE_NOT_OPER:
259 CIO_DEBUG(KERN_WARNING, 2,
260 "SenseID : unknown device %04x on subchannel "
261 "0.%x.%04x\n", cdev->private->devno,
262 sch->schid.ssid, sch->schid.sch_no);
264 case DEV_STATE_OFFLINE:
265 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
266 same_dev = ccw_device_handle_oper(cdev);
269 /* fill out sense information */
270 cdev->id.cu_type = cdev->private->senseid.cu_type;
271 cdev->id.cu_model = cdev->private->senseid.cu_model;
272 cdev->id.dev_type = cdev->private->senseid.dev_type;
273 cdev->id.dev_model = cdev->private->senseid.dev_model;
275 cdev->private->state = DEV_STATE_OFFLINE;
277 /* Get device online again. */
278 ccw_device_online(cdev);
279 wake_up(&cdev->private->wait_q);
283 /* Issue device info message. */
284 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
285 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
287 cdev->private->ssid, cdev->private->devno,
288 cdev->id.cu_type, cdev->id.cu_model,
289 cdev->id.dev_type, cdev->id.dev_model);
291 case DEV_STATE_BOXED:
292 CIO_DEBUG(KERN_WARNING, 2,
293 "SenseID : boxed device %04x on subchannel "
294 "0.%x.%04x\n", cdev->private->devno,
295 sch->schid.ssid, sch->schid.sch_no);
298 cdev->private->state = state;
299 io_subchannel_recog_done(cdev);
300 if (state != DEV_STATE_NOT_OPER)
301 wake_up(&cdev->private->wait_q);
305 * Function called from device_id.c after sense id has completed.
308 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
312 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
314 case -ETIME: /* Sense id stopped by timeout. */
315 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
318 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
324 ccw_device_oper_notify(void *data)
326 struct ccw_device *cdev;
327 struct subchannel *sch;
330 cdev = (struct ccw_device *)data;
331 sch = to_subchannel(cdev->dev.parent);
332 ret = (sch->driver && sch->driver->notify) ?
333 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
335 /* Driver doesn't want device back. */
336 ccw_device_do_unreg_rereg((void *)cdev);
338 /* Reenable channel measurements, if needed. */
340 wake_up(&cdev->private->wait_q);
345 * Finished with online/offline processing.
348 ccw_device_done(struct ccw_device *cdev, int state)
350 struct subchannel *sch;
352 sch = to_subchannel(cdev->dev.parent);
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 sch->vpm = 0; /* Start with no path groups set. */
458 cdev->private->state = DEV_STATE_VERIFY;
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 cdev->private->flags.doverify = 0;
560 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
561 cdev->private->options.pgroup = 0;
563 ccw_device_done(cdev, DEV_STATE_ONLINE);
564 /* Deliver fake irb to device driver, if needed. */
565 if (cdev->private->flags.fake_irb) {
566 memset(&cdev->private->irb, 0, sizeof(struct irb));
567 cdev->private->irb.scsw.cc = 1;
568 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
569 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
570 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
571 cdev->private->flags.fake_irb = 0;
573 cdev->handler(cdev, cdev->private->intparm,
574 &cdev->private->irb);
575 memset(&cdev->private->irb, 0, sizeof(struct irb));
579 ccw_device_done(cdev, DEV_STATE_BOXED);
582 PREPARE_WORK(&cdev->private->kick_work,
583 ccw_device_nopath_notify, (void *)cdev);
584 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
585 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
594 ccw_device_online(struct ccw_device *cdev)
596 struct subchannel *sch;
599 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
600 (cdev->private->state != DEV_STATE_BOXED))
602 sch = to_subchannel(cdev->dev.parent);
603 if (css_init_done && !get_device(&cdev->dev))
605 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
607 /* Couldn't enable the subchannel for i/o. Sick device. */
609 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
612 /* Do we want to do path grouping? */
613 if (!cdev->private->options.pgroup) {
614 /* Start initial path verification. */
615 cdev->private->state = DEV_STATE_VERIFY;
616 ccw_device_verify_start(cdev);
619 /* Do a SensePGID first. */
620 cdev->private->state = DEV_STATE_SENSE_PGID;
621 ccw_device_sense_pgid_start(cdev);
626 ccw_device_disband_done(struct ccw_device *cdev, int err)
630 ccw_device_done(cdev, DEV_STATE_OFFLINE);
633 ccw_device_done(cdev, DEV_STATE_BOXED);
636 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
645 ccw_device_offline(struct ccw_device *cdev)
647 struct subchannel *sch;
649 sch = to_subchannel(cdev->dev.parent);
650 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
652 if (cdev->private->state != DEV_STATE_ONLINE) {
653 if (sch->schib.scsw.actl != 0)
657 if (sch->schib.scsw.actl != 0)
659 /* Are we doing path grouping? */
660 if (!cdev->private->options.pgroup) {
661 /* No, set state offline immediately. */
663 ccw_device_done(cdev, DEV_STATE_OFFLINE);
666 /* Start Set Path Group commands. */
667 cdev->private->state = DEV_STATE_DISBAND_PGID;
668 ccw_device_disband_start(cdev);
673 * Handle timeout in device online/offline process.
676 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
680 ret = ccw_device_cancel_halt_clear(cdev);
683 ccw_device_done(cdev, DEV_STATE_BOXED);
686 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
689 ccw_device_set_timeout(cdev, 3*HZ);
694 * Handle not oper event in device recognition.
697 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
699 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
703 * Handle not operational event while offline.
706 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
708 struct subchannel *sch;
710 cdev->private->state = DEV_STATE_NOT_OPER;
711 sch = to_subchannel(cdev->dev.parent);
712 if (get_device(&cdev->dev)) {
713 PREPARE_WORK(&cdev->private->kick_work,
714 ccw_device_call_sch_unregister, (void *)cdev);
715 queue_work(ccw_device_work, &cdev->private->kick_work);
717 wake_up(&cdev->private->wait_q);
721 * Handle not operational event while online.
724 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
726 struct subchannel *sch;
728 sch = to_subchannel(cdev->dev.parent);
729 if (sch->driver->notify &&
730 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
731 ccw_device_set_timeout(cdev, 0);
732 cdev->private->flags.fake_irb = 0;
733 cdev->private->state = DEV_STATE_DISCONNECTED;
734 wake_up(&cdev->private->wait_q);
737 cdev->private->state = DEV_STATE_NOT_OPER;
738 cio_disable_subchannel(sch);
739 if (sch->schib.scsw.actl != 0) {
740 // FIXME: not-oper indication to device driver ?
741 ccw_device_call_handler(cdev);
743 if (get_device(&cdev->dev)) {
744 PREPARE_WORK(&cdev->private->kick_work,
745 ccw_device_call_sch_unregister, (void *)cdev);
746 queue_work(ccw_device_work, &cdev->private->kick_work);
748 wake_up(&cdev->private->wait_q);
752 * Handle path verification event.
755 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
757 struct subchannel *sch;
759 if (cdev->private->state == DEV_STATE_W4SENSE) {
760 cdev->private->flags.doverify = 1;
763 sch = to_subchannel(cdev->dev.parent);
765 * Since we might not just be coming from an interrupt from the
766 * subchannel we have to update the schib.
768 stsch(sch->schid, &sch->schib);
770 if (sch->schib.scsw.actl != 0 ||
771 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
772 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
774 * No final status yet or final status not yet delivered
775 * to the device driver. Can't do path verfication now,
776 * delay until final status was delivered.
778 cdev->private->flags.doverify = 1;
781 /* Device is idle, we can do the path verification. */
782 cdev->private->state = DEV_STATE_VERIFY;
783 ccw_device_verify_start(cdev);
787 * Got an interrupt for a normal io (state online).
790 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
794 irb = (struct irb *) __LC_IRB;
795 /* Check for unsolicited interrupt. */
796 if ((irb->scsw.stctl ==
797 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
798 && (!irb->scsw.cc)) {
799 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
800 !irb->esw.esw0.erw.cons) {
801 /* Unit check but no sense data. Need basic sense. */
802 if (ccw_device_do_sense(cdev, irb) != 0)
803 goto call_handler_unsol;
804 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
805 cdev->private->state = DEV_STATE_W4SENSE;
806 cdev->private->intparm = 0;
811 cdev->handler (cdev, 0, irb);
814 /* Accumulate status and find out if a basic sense is needed. */
815 ccw_device_accumulate_irb(cdev, irb);
816 if (cdev->private->flags.dosense) {
817 if (ccw_device_do_sense(cdev, irb) == 0) {
818 cdev->private->state = DEV_STATE_W4SENSE;
822 /* Call the handler. */
823 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
824 /* Start delayed path verification. */
825 ccw_device_online_verify(cdev, 0);
829 * Got an timeout in online state.
832 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
836 ccw_device_set_timeout(cdev, 0);
837 ret = ccw_device_cancel_halt_clear(cdev);
839 ccw_device_set_timeout(cdev, 3*HZ);
840 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
843 if (ret == -ENODEV) {
844 struct subchannel *sch;
846 sch = to_subchannel(cdev->dev.parent);
848 PREPARE_WORK(&cdev->private->kick_work,
849 ccw_device_nopath_notify, (void *)cdev);
850 queue_work(ccw_device_notify_work,
851 &cdev->private->kick_work);
853 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
854 } else if (cdev->handler)
855 cdev->handler(cdev, cdev->private->intparm,
856 ERR_PTR(-ETIMEDOUT));
860 * Got an interrupt for a basic sense.
863 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
867 irb = (struct irb *) __LC_IRB;
868 /* Check for unsolicited interrupt. */
869 if (irb->scsw.stctl ==
870 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
871 if (irb->scsw.cc == 1)
872 /* Basic sense hasn't started. Try again. */
873 ccw_device_do_sense(cdev, irb);
875 printk("Huh? %s(%s): unsolicited interrupt...\n",
876 __FUNCTION__, cdev->dev.bus_id);
878 cdev->handler (cdev, 0, irb);
883 * Check if a halt or clear has been issued in the meanwhile. If yes,
884 * only deliver the halt/clear interrupt to the device driver as if it
885 * had killed the original request.
887 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
888 cdev->private->flags.dosense = 0;
889 memset(&cdev->private->irb, 0, sizeof(struct irb));
890 ccw_device_accumulate_irb(cdev, irb);
893 /* Add basic sense info to irb. */
894 ccw_device_accumulate_basic_sense(cdev, irb);
895 if (cdev->private->flags.dosense) {
896 /* Another basic sense is needed. */
897 ccw_device_do_sense(cdev, irb);
901 cdev->private->state = DEV_STATE_ONLINE;
902 /* Call the handler. */
903 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
904 /* Start delayed path verification. */
905 ccw_device_online_verify(cdev, 0);
909 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
913 irb = (struct irb *) __LC_IRB;
914 /* Accumulate status. We don't do basic sense. */
915 ccw_device_accumulate_irb(cdev, irb);
916 /* Remember to clear irb to avoid residuals. */
917 memset(&cdev->private->irb, 0, sizeof(struct irb));
918 /* Try to start delayed device verification. */
919 ccw_device_online_verify(cdev, 0);
920 /* Note: Don't call handler for cio initiated clear! */
924 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
926 struct subchannel *sch;
928 sch = to_subchannel(cdev->dev.parent);
929 ccw_device_set_timeout(cdev, 0);
930 /* OK, i/o is dead now. Call interrupt handler. */
931 cdev->private->state = DEV_STATE_ONLINE;
933 cdev->handler(cdev, cdev->private->intparm,
934 ERR_PTR(-ETIMEDOUT));
936 PREPARE_WORK(&cdev->private->kick_work,
937 ccw_device_nopath_notify, (void *)cdev);
938 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
939 } else if (cdev->private->flags.doverify)
940 /* Start delayed path verification. */
941 ccw_device_online_verify(cdev, 0);
945 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
949 ret = ccw_device_cancel_halt_clear(cdev);
951 ccw_device_set_timeout(cdev, 3*HZ);
954 if (ret == -ENODEV) {
955 struct subchannel *sch;
957 sch = to_subchannel(cdev->dev.parent);
959 PREPARE_WORK(&cdev->private->kick_work,
960 ccw_device_nopath_notify, (void *)cdev);
961 queue_work(ccw_device_notify_work,
962 &cdev->private->kick_work);
964 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
967 //FIXME: Can we get here?
968 cdev->private->state = DEV_STATE_ONLINE;
970 cdev->handler(cdev, cdev->private->intparm,
971 ERR_PTR(-ETIMEDOUT));
975 ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event)
978 struct subchannel *sch;
980 irb = (struct irb *) __LC_IRB;
982 * Accumulate status and find out if a basic sense is needed.
983 * This is fine since we have already adapted the lpm.
985 ccw_device_accumulate_irb(cdev, irb);
986 if (cdev->private->flags.dosense) {
987 if (ccw_device_do_sense(cdev, irb) == 0) {
988 cdev->private->state = DEV_STATE_W4SENSE;
993 /* Iff device is idle, reset timeout. */
994 sch = to_subchannel(cdev->dev.parent);
995 if (!stsch(sch->schid, &sch->schib))
996 if (sch->schib.scsw.actl == 0)
997 ccw_device_set_timeout(cdev, 0);
998 /* Call the handler. */
999 ccw_device_call_handler(cdev);
1001 PREPARE_WORK(&cdev->private->kick_work,
1002 ccw_device_nopath_notify, (void *)cdev);
1003 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1004 } else if (cdev->private->flags.doverify)
1005 ccw_device_online_verify(cdev, 0);
1009 ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1012 struct subchannel *sch;
1014 sch = to_subchannel(cdev->dev.parent);
1015 ccw_device_set_timeout(cdev, 0);
1016 ret = ccw_device_cancel_halt_clear(cdev);
1017 if (ret == -EBUSY) {
1018 ccw_device_set_timeout(cdev, 3*HZ);
1019 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1022 if (ret == -ENODEV) {
1024 PREPARE_WORK(&cdev->private->kick_work,
1025 ccw_device_nopath_notify, (void *)cdev);
1026 queue_work(ccw_device_notify_work,
1027 &cdev->private->kick_work);
1029 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1033 cdev->handler(cdev, cdev->private->intparm,
1034 ERR_PTR(-ETIMEDOUT));
1036 PREPARE_WORK(&cdev->private->kick_work,
1037 ccw_device_nopath_notify, (void *)cdev);
1038 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1039 } else if (cdev->private->flags.doverify)
1040 /* Start delayed path verification. */
1041 ccw_device_online_verify(cdev, 0);
1045 ccw_device_wait4io_verify(struct ccw_device *cdev, enum dev_event dev_event)
1047 /* When the I/O has terminated, we have to start verification. */
1048 cdev->private->flags.doverify = 1;
1052 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1056 switch (dev_event) {
1057 case DEV_EVENT_INTERRUPT:
1058 irb = (struct irb *) __LC_IRB;
1059 /* Check for unsolicited interrupt. */
1060 if ((irb->scsw.stctl ==
1061 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1063 /* FIXME: we should restart stlck here, but this
1064 * is extremely unlikely ... */
1067 ccw_device_accumulate_irb(cdev, irb);
1068 /* We don't care about basic sense etc. */
1070 default: /* timeout */
1074 wake_up(&cdev->private->wait_q);
1078 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1080 struct subchannel *sch;
1082 sch = to_subchannel(cdev->dev.parent);
1083 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1084 /* Couldn't enable the subchannel for i/o. Sick device. */
1087 /* After 60s the device recognition is considered to have failed. */
1088 ccw_device_set_timeout(cdev, 60*HZ);
1090 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1091 ccw_device_sense_id_start(cdev);
1095 device_trigger_reprobe(struct subchannel *sch)
1097 struct ccw_device *cdev;
1099 if (!sch->dev.driver_data)
1101 cdev = sch->dev.driver_data;
1102 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1105 /* Update some values. */
1106 if (stsch(sch->schid, &sch->schib))
1110 * The pim, pam, pom values may not be accurate, but they are the best
1111 * we have before performing device selection :/
1113 sch->lpm = sch->schib.pmcw.pim &
1114 sch->schib.pmcw.pam &
1115 sch->schib.pmcw.pom &
1117 /* Re-set some bits in the pmcw that were lost. */
1118 sch->schib.pmcw.isc = 3;
1119 sch->schib.pmcw.csense = 1;
1120 sch->schib.pmcw.ena = 0;
1121 if ((sch->lpm & (sch->lpm - 1)) != 0)
1122 sch->schib.pmcw.mp = 1;
1123 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1124 /* We should also udate ssd info, but this has to wait. */
1125 ccw_device_start_id(cdev, 0);
1129 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1131 struct subchannel *sch;
1133 sch = to_subchannel(cdev->dev.parent);
1135 * An interrupt in state offline means a previous disable was not
1136 * successful. Try again.
1138 cio_disable_subchannel(sch);
1142 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1144 retry_set_schib(cdev);
1145 cdev->private->state = DEV_STATE_ONLINE;
1146 dev_fsm_event(cdev, dev_event);
1149 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1150 enum dev_event dev_event)
1152 cmf_retry_copy_block(cdev);
1153 cdev->private->state = DEV_STATE_ONLINE;
1154 dev_fsm_event(cdev, dev_event);
1158 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1160 ccw_device_set_timeout(cdev, 0);
1161 if (dev_event == DEV_EVENT_NOTOPER)
1162 cdev->private->state = DEV_STATE_NOT_OPER;
1164 cdev->private->state = DEV_STATE_OFFLINE;
1165 wake_up(&cdev->private->wait_q);
1169 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1173 ret = ccw_device_cancel_halt_clear(cdev);
1176 cdev->private->state = DEV_STATE_OFFLINE;
1177 wake_up(&cdev->private->wait_q);
1180 cdev->private->state = DEV_STATE_NOT_OPER;
1181 wake_up(&cdev->private->wait_q);
1184 ccw_device_set_timeout(cdev, HZ/10);
1189 * No operation action. This is used e.g. to ignore a timeout event in
1193 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1198 * Bug operation action.
1201 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1203 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1204 cdev->private->state, dev_event);
1209 * device statemachine
1211 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1212 [DEV_STATE_NOT_OPER] = {
1213 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1214 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1215 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1216 [DEV_EVENT_VERIFY] = ccw_device_nop,
1218 [DEV_STATE_SENSE_PGID] = {
1219 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1220 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1221 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1222 [DEV_EVENT_VERIFY] = ccw_device_nop,
1224 [DEV_STATE_SENSE_ID] = {
1225 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1226 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1227 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1228 [DEV_EVENT_VERIFY] = ccw_device_nop,
1230 [DEV_STATE_OFFLINE] = {
1231 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1232 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1233 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1234 [DEV_EVENT_VERIFY] = ccw_device_nop,
1236 [DEV_STATE_VERIFY] = {
1237 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1238 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1239 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1240 [DEV_EVENT_VERIFY] = ccw_device_nop,
1242 [DEV_STATE_ONLINE] = {
1243 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1244 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1245 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1246 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1248 [DEV_STATE_W4SENSE] = {
1249 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1250 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1251 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1252 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1254 [DEV_STATE_DISBAND_PGID] = {
1255 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1256 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1257 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1258 [DEV_EVENT_VERIFY] = ccw_device_nop,
1260 [DEV_STATE_BOXED] = {
1261 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1262 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1263 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1264 [DEV_EVENT_VERIFY] = ccw_device_nop,
1266 /* states to wait for i/o completion before doing something */
1267 [DEV_STATE_CLEAR_VERIFY] = {
1268 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1269 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1270 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1271 [DEV_EVENT_VERIFY] = ccw_device_nop,
1273 [DEV_STATE_TIMEOUT_KILL] = {
1274 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1275 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1276 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1277 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1279 [DEV_STATE_WAIT4IO] = {
1280 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1281 [DEV_EVENT_INTERRUPT] = ccw_device_wait4io_irq,
1282 [DEV_EVENT_TIMEOUT] = ccw_device_wait4io_timeout,
1283 [DEV_EVENT_VERIFY] = ccw_device_wait4io_verify,
1285 [DEV_STATE_QUIESCE] = {
1286 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1287 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1288 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1289 [DEV_EVENT_VERIFY] = ccw_device_nop,
1291 /* special states for devices gone not operational */
1292 [DEV_STATE_DISCONNECTED] = {
1293 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1294 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1295 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1296 [DEV_EVENT_VERIFY] = ccw_device_nop,
1298 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1299 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1300 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1301 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1302 [DEV_EVENT_VERIFY] = ccw_device_nop,
1304 [DEV_STATE_CMFCHANGE] = {
1305 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1306 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1307 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1308 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1310 [DEV_STATE_CMFUPDATE] = {
1311 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1312 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1313 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1314 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1319 * io_subchannel_irq is called for "real" interrupts or for status
1320 * pending conditions on msch.
1323 io_subchannel_irq (struct device *pdev)
1325 struct ccw_device *cdev;
1327 cdev = to_subchannel(pdev)->dev.driver_data;
1329 CIO_TRACE_EVENT (3, "IRQ");
1330 CIO_TRACE_EVENT (3, pdev->bus_id);
1332 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1335 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);