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 = (struct ccw_device_id) {
271 .cu_type = cdev->private->senseid.cu_type,
272 .cu_model = cdev->private->senseid.cu_model,
273 .dev_type = cdev->private->senseid.dev_type,
274 .dev_model = cdev->private->senseid.dev_model,
277 cdev->private->state = DEV_STATE_OFFLINE;
279 /* Get device online again. */
280 ccw_device_online(cdev);
281 wake_up(&cdev->private->wait_q);
285 /* Issue device info message. */
286 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
287 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
289 cdev->private->ssid, cdev->private->devno,
290 cdev->id.cu_type, cdev->id.cu_model,
291 cdev->id.dev_type, cdev->id.dev_model);
293 case DEV_STATE_BOXED:
294 CIO_DEBUG(KERN_WARNING, 2,
295 "SenseID : boxed device %04x on subchannel "
296 "0.%x.%04x\n", cdev->private->devno,
297 sch->schid.ssid, sch->schid.sch_no);
300 cdev->private->state = state;
301 io_subchannel_recog_done(cdev);
302 if (state != DEV_STATE_NOT_OPER)
303 wake_up(&cdev->private->wait_q);
307 * Function called from device_id.c after sense id has completed.
310 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
314 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
316 case -ETIME: /* Sense id stopped by timeout. */
317 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
320 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
326 ccw_device_oper_notify(void *data)
328 struct ccw_device *cdev;
329 struct subchannel *sch;
332 cdev = (struct ccw_device *)data;
333 sch = to_subchannel(cdev->dev.parent);
334 ret = (sch->driver && sch->driver->notify) ?
335 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
337 /* Driver doesn't want device back. */
338 ccw_device_do_unreg_rereg((void *)cdev);
340 /* Reenable channel measurements, if needed. */
342 wake_up(&cdev->private->wait_q);
347 * Finished with online/offline processing.
350 ccw_device_done(struct ccw_device *cdev, int state)
352 struct subchannel *sch;
354 sch = to_subchannel(cdev->dev.parent);
356 if (state != DEV_STATE_ONLINE)
357 cio_disable_subchannel(sch);
359 /* Reset device status. */
360 memset(&cdev->private->irb, 0, sizeof(struct irb));
362 cdev->private->state = state;
365 if (state == DEV_STATE_BOXED)
366 CIO_DEBUG(KERN_WARNING, 2,
367 "Boxed device %04x on subchannel %04x\n",
368 cdev->private->devno, sch->schid.sch_no);
370 if (cdev->private->flags.donotify) {
371 cdev->private->flags.donotify = 0;
372 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify,
374 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
376 wake_up(&cdev->private->wait_q);
378 if (css_init_done && state != DEV_STATE_ONLINE)
379 put_device (&cdev->dev);
382 static inline int cmp_pgid(struct pgid *p1, struct pgid *p2)
390 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
393 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
399 for (i = 0; i < 8; i++) {
400 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
403 if (cdev->private->pgid[last].inf.ps.state1 ==
405 /* First non-zero PGID */
409 if (cmp_pgid(&cdev->private->pgid[i],
410 &cdev->private->pgid[last]) == 0)
411 /* Non-conflicting PGIDs */
414 /* PGID mismatch, can't pathgroup. */
415 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
416 "0.%x.%04x, can't pathgroup\n",
417 cdev->private->ssid, cdev->private->devno);
418 cdev->private->options.pgroup = 0;
421 if (cdev->private->pgid[last].inf.ps.state1 ==
423 /* No previous pgid found */
424 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
425 sizeof(struct pgid));
427 /* Use existing pgid */
428 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
429 sizeof(struct pgid));
433 * Function called from device_pgid.c after sense path ground has completed.
436 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
438 struct subchannel *sch;
440 sch = to_subchannel(cdev->dev.parent);
442 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
443 cdev->private->options.pgroup = 0;
445 case 0: /* success */
446 case -EACCES: /* partial success, some paths not operational */
447 /* Check if all pgids are equal or 0. */
448 __ccw_device_get_common_pgid(cdev);
450 case -ETIME: /* Sense path group id stopped by timeout. */
451 case -EUSERS: /* device is reserved for someone else. */
452 ccw_device_done(cdev, DEV_STATE_BOXED);
455 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
458 /* Start Path Group verification. */
459 sch->vpm = 0; /* Start with no path groups set. */
460 cdev->private->state = DEV_STATE_VERIFY;
461 ccw_device_verify_start(cdev);
465 * Start device recognition.
468 ccw_device_recognition(struct ccw_device *cdev)
470 struct subchannel *sch;
473 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
474 (cdev->private->state != DEV_STATE_BOXED))
476 sch = to_subchannel(cdev->dev.parent);
477 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
479 /* Couldn't enable the subchannel for i/o. Sick device. */
482 /* After 60s the device recognition is considered to have failed. */
483 ccw_device_set_timeout(cdev, 60*HZ);
486 * We used to start here with a sense pgid to find out whether a device
487 * is locked by someone else. Unfortunately, the sense pgid command
488 * code has other meanings on devices predating the path grouping
489 * algorithm, so we start with sense id and box the device after an
490 * timeout (or if sense pgid during path verification detects the device
491 * is locked, as may happen on newer devices).
493 cdev->private->flags.recog_done = 0;
494 cdev->private->state = DEV_STATE_SENSE_ID;
495 ccw_device_sense_id_start(cdev);
500 * Handle timeout in device recognition.
503 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
507 ret = ccw_device_cancel_halt_clear(cdev);
510 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
513 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
516 ccw_device_set_timeout(cdev, 3*HZ);
522 ccw_device_nopath_notify(void *data)
524 struct ccw_device *cdev;
525 struct subchannel *sch;
528 cdev = (struct ccw_device *)data;
529 sch = to_subchannel(cdev->dev.parent);
533 ret = (sch->driver && sch->driver->notify) ?
534 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
536 if (get_device(&sch->dev)) {
537 /* Driver doesn't want to keep device. */
538 cio_disable_subchannel(sch);
539 if (get_device(&cdev->dev)) {
540 PREPARE_WORK(&cdev->private->kick_work,
541 ccw_device_call_sch_unregister,
543 queue_work(ccw_device_work,
544 &cdev->private->kick_work);
546 put_device(&sch->dev);
549 cio_disable_subchannel(sch);
550 ccw_device_set_timeout(cdev, 0);
551 cdev->private->flags.fake_irb = 0;
552 cdev->private->state = DEV_STATE_DISCONNECTED;
553 wake_up(&cdev->private->wait_q);
558 ccw_device_verify_done(struct ccw_device *cdev, int err)
560 cdev->private->flags.doverify = 0;
562 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
563 cdev->private->options.pgroup = 0;
565 ccw_device_done(cdev, DEV_STATE_ONLINE);
566 /* Deliver fake irb to device driver, if needed. */
567 if (cdev->private->flags.fake_irb) {
568 memset(&cdev->private->irb, 0, sizeof(struct irb));
569 cdev->private->irb.scsw = (struct scsw) {
571 .fctl = SCSW_FCTL_START_FUNC,
572 .actl = SCSW_ACTL_START_PEND,
573 .stctl = SCSW_STCTL_STATUS_PEND,
575 cdev->private->flags.fake_irb = 0;
577 cdev->handler(cdev, cdev->private->intparm,
578 &cdev->private->irb);
579 memset(&cdev->private->irb, 0, sizeof(struct irb));
583 ccw_device_done(cdev, DEV_STATE_BOXED);
586 PREPARE_WORK(&cdev->private->kick_work,
587 ccw_device_nopath_notify, (void *)cdev);
588 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
589 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
598 ccw_device_online(struct ccw_device *cdev)
600 struct subchannel *sch;
603 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
604 (cdev->private->state != DEV_STATE_BOXED))
606 sch = to_subchannel(cdev->dev.parent);
607 if (css_init_done && !get_device(&cdev->dev))
609 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
611 /* Couldn't enable the subchannel for i/o. Sick device. */
613 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
616 /* Do we want to do path grouping? */
617 if (!cdev->private->options.pgroup) {
618 /* Start initial path verification. */
619 cdev->private->state = DEV_STATE_VERIFY;
620 ccw_device_verify_start(cdev);
623 /* Do a SensePGID first. */
624 cdev->private->state = DEV_STATE_SENSE_PGID;
625 ccw_device_sense_pgid_start(cdev);
630 ccw_device_disband_done(struct ccw_device *cdev, int err)
634 ccw_device_done(cdev, DEV_STATE_OFFLINE);
637 ccw_device_done(cdev, DEV_STATE_BOXED);
640 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
649 ccw_device_offline(struct ccw_device *cdev)
651 struct subchannel *sch;
653 sch = to_subchannel(cdev->dev.parent);
654 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
656 if (cdev->private->state != DEV_STATE_ONLINE) {
657 if (sch->schib.scsw.actl != 0)
661 if (sch->schib.scsw.actl != 0)
663 /* Are we doing path grouping? */
664 if (!cdev->private->options.pgroup) {
665 /* No, set state offline immediately. */
667 ccw_device_done(cdev, DEV_STATE_OFFLINE);
670 /* Start Set Path Group commands. */
671 cdev->private->state = DEV_STATE_DISBAND_PGID;
672 ccw_device_disband_start(cdev);
677 * Handle timeout in device online/offline process.
680 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
684 ret = ccw_device_cancel_halt_clear(cdev);
687 ccw_device_done(cdev, DEV_STATE_BOXED);
690 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
693 ccw_device_set_timeout(cdev, 3*HZ);
698 * Handle not oper event in device recognition.
701 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
703 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
707 * Handle not operational event while offline.
710 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
712 struct subchannel *sch;
714 cdev->private->state = DEV_STATE_NOT_OPER;
715 sch = to_subchannel(cdev->dev.parent);
716 if (get_device(&cdev->dev)) {
717 PREPARE_WORK(&cdev->private->kick_work,
718 ccw_device_call_sch_unregister, (void *)cdev);
719 queue_work(ccw_device_work, &cdev->private->kick_work);
721 wake_up(&cdev->private->wait_q);
725 * Handle not operational event while online.
728 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
730 struct subchannel *sch;
732 sch = to_subchannel(cdev->dev.parent);
733 if (sch->driver->notify &&
734 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
735 ccw_device_set_timeout(cdev, 0);
736 cdev->private->flags.fake_irb = 0;
737 cdev->private->state = DEV_STATE_DISCONNECTED;
738 wake_up(&cdev->private->wait_q);
741 cdev->private->state = DEV_STATE_NOT_OPER;
742 cio_disable_subchannel(sch);
743 if (sch->schib.scsw.actl != 0) {
744 // FIXME: not-oper indication to device driver ?
745 ccw_device_call_handler(cdev);
747 if (get_device(&cdev->dev)) {
748 PREPARE_WORK(&cdev->private->kick_work,
749 ccw_device_call_sch_unregister, (void *)cdev);
750 queue_work(ccw_device_work, &cdev->private->kick_work);
752 wake_up(&cdev->private->wait_q);
756 * Handle path verification event.
759 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
761 struct subchannel *sch;
763 if (cdev->private->state == DEV_STATE_W4SENSE) {
764 cdev->private->flags.doverify = 1;
767 sch = to_subchannel(cdev->dev.parent);
769 * Since we might not just be coming from an interrupt from the
770 * subchannel we have to update the schib.
772 stsch(sch->schid, &sch->schib);
774 if (sch->schib.scsw.actl != 0 ||
775 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
776 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
778 * No final status yet or final status not yet delivered
779 * to the device driver. Can't do path verfication now,
780 * delay until final status was delivered.
782 cdev->private->flags.doverify = 1;
785 /* Device is idle, we can do the path verification. */
786 cdev->private->state = DEV_STATE_VERIFY;
787 ccw_device_verify_start(cdev);
791 * Got an interrupt for a normal io (state online).
794 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
798 irb = (struct irb *) __LC_IRB;
799 /* Check for unsolicited interrupt. */
800 if ((irb->scsw.stctl ==
801 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
802 && (!irb->scsw.cc)) {
803 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
804 !irb->esw.esw0.erw.cons) {
805 /* Unit check but no sense data. Need basic sense. */
806 if (ccw_device_do_sense(cdev, irb) != 0)
807 goto call_handler_unsol;
808 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
809 cdev->private->state = DEV_STATE_W4SENSE;
810 cdev->private->intparm = 0;
815 cdev->handler (cdev, 0, irb);
818 /* Accumulate status and find out if a basic sense is needed. */
819 ccw_device_accumulate_irb(cdev, irb);
820 if (cdev->private->flags.dosense) {
821 if (ccw_device_do_sense(cdev, irb) == 0) {
822 cdev->private->state = DEV_STATE_W4SENSE;
826 /* Call the handler. */
827 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
828 /* Start delayed path verification. */
829 ccw_device_online_verify(cdev, 0);
833 * Got an timeout in online state.
836 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
840 ccw_device_set_timeout(cdev, 0);
841 ret = ccw_device_cancel_halt_clear(cdev);
843 ccw_device_set_timeout(cdev, 3*HZ);
844 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
847 if (ret == -ENODEV) {
848 struct subchannel *sch;
850 sch = to_subchannel(cdev->dev.parent);
852 PREPARE_WORK(&cdev->private->kick_work,
853 ccw_device_nopath_notify, (void *)cdev);
854 queue_work(ccw_device_notify_work,
855 &cdev->private->kick_work);
857 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
858 } else if (cdev->handler)
859 cdev->handler(cdev, cdev->private->intparm,
860 ERR_PTR(-ETIMEDOUT));
864 * Got an interrupt for a basic sense.
867 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
871 irb = (struct irb *) __LC_IRB;
872 /* Check for unsolicited interrupt. */
873 if (irb->scsw.stctl ==
874 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
875 if (irb->scsw.cc == 1)
876 /* Basic sense hasn't started. Try again. */
877 ccw_device_do_sense(cdev, irb);
879 printk("Huh? %s(%s): unsolicited interrupt...\n",
880 __FUNCTION__, cdev->dev.bus_id);
882 cdev->handler (cdev, 0, irb);
887 * Check if a halt or clear has been issued in the meanwhile. If yes,
888 * only deliver the halt/clear interrupt to the device driver as if it
889 * had killed the original request.
891 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
892 cdev->private->flags.dosense = 0;
893 memset(&cdev->private->irb, 0, sizeof(struct irb));
894 ccw_device_accumulate_irb(cdev, irb);
897 /* Add basic sense info to irb. */
898 ccw_device_accumulate_basic_sense(cdev, irb);
899 if (cdev->private->flags.dosense) {
900 /* Another basic sense is needed. */
901 ccw_device_do_sense(cdev, irb);
905 cdev->private->state = DEV_STATE_ONLINE;
906 /* Call the handler. */
907 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
908 /* Start delayed path verification. */
909 ccw_device_online_verify(cdev, 0);
913 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
917 irb = (struct irb *) __LC_IRB;
918 /* Accumulate status. We don't do basic sense. */
919 ccw_device_accumulate_irb(cdev, irb);
920 /* Remember to clear irb to avoid residuals. */
921 memset(&cdev->private->irb, 0, sizeof(struct irb));
922 /* Try to start delayed device verification. */
923 ccw_device_online_verify(cdev, 0);
924 /* Note: Don't call handler for cio initiated clear! */
928 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
930 struct subchannel *sch;
932 sch = to_subchannel(cdev->dev.parent);
933 ccw_device_set_timeout(cdev, 0);
934 /* OK, i/o is dead now. Call interrupt handler. */
935 cdev->private->state = DEV_STATE_ONLINE;
937 cdev->handler(cdev, cdev->private->intparm,
938 ERR_PTR(-ETIMEDOUT));
940 PREPARE_WORK(&cdev->private->kick_work,
941 ccw_device_nopath_notify, (void *)cdev);
942 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
943 } else if (cdev->private->flags.doverify)
944 /* Start delayed path verification. */
945 ccw_device_online_verify(cdev, 0);
949 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
953 ret = ccw_device_cancel_halt_clear(cdev);
955 ccw_device_set_timeout(cdev, 3*HZ);
958 if (ret == -ENODEV) {
959 struct subchannel *sch;
961 sch = to_subchannel(cdev->dev.parent);
963 PREPARE_WORK(&cdev->private->kick_work,
964 ccw_device_nopath_notify, (void *)cdev);
965 queue_work(ccw_device_notify_work,
966 &cdev->private->kick_work);
968 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
971 //FIXME: Can we get here?
972 cdev->private->state = DEV_STATE_ONLINE;
974 cdev->handler(cdev, cdev->private->intparm,
975 ERR_PTR(-ETIMEDOUT));
979 ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event)
982 struct subchannel *sch;
984 irb = (struct irb *) __LC_IRB;
986 * Accumulate status and find out if a basic sense is needed.
987 * This is fine since we have already adapted the lpm.
989 ccw_device_accumulate_irb(cdev, irb);
990 if (cdev->private->flags.dosense) {
991 if (ccw_device_do_sense(cdev, irb) == 0) {
992 cdev->private->state = DEV_STATE_W4SENSE;
997 /* Iff device is idle, reset timeout. */
998 sch = to_subchannel(cdev->dev.parent);
999 if (!stsch(sch->schid, &sch->schib))
1000 if (sch->schib.scsw.actl == 0)
1001 ccw_device_set_timeout(cdev, 0);
1002 /* Call the handler. */
1003 ccw_device_call_handler(cdev);
1005 PREPARE_WORK(&cdev->private->kick_work,
1006 ccw_device_nopath_notify, (void *)cdev);
1007 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1008 } else if (cdev->private->flags.doverify)
1009 ccw_device_online_verify(cdev, 0);
1013 ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1016 struct subchannel *sch;
1018 sch = to_subchannel(cdev->dev.parent);
1019 ccw_device_set_timeout(cdev, 0);
1020 ret = ccw_device_cancel_halt_clear(cdev);
1021 if (ret == -EBUSY) {
1022 ccw_device_set_timeout(cdev, 3*HZ);
1023 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1026 if (ret == -ENODEV) {
1028 PREPARE_WORK(&cdev->private->kick_work,
1029 ccw_device_nopath_notify, (void *)cdev);
1030 queue_work(ccw_device_notify_work,
1031 &cdev->private->kick_work);
1033 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1037 cdev->handler(cdev, cdev->private->intparm,
1038 ERR_PTR(-ETIMEDOUT));
1040 PREPARE_WORK(&cdev->private->kick_work,
1041 ccw_device_nopath_notify, (void *)cdev);
1042 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1043 } else if (cdev->private->flags.doverify)
1044 /* Start delayed path verification. */
1045 ccw_device_online_verify(cdev, 0);
1049 ccw_device_wait4io_verify(struct ccw_device *cdev, enum dev_event dev_event)
1051 /* When the I/O has terminated, we have to start verification. */
1052 cdev->private->flags.doverify = 1;
1056 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1060 switch (dev_event) {
1061 case DEV_EVENT_INTERRUPT:
1062 irb = (struct irb *) __LC_IRB;
1063 /* Check for unsolicited interrupt. */
1064 if ((irb->scsw.stctl ==
1065 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1067 /* FIXME: we should restart stlck here, but this
1068 * is extremely unlikely ... */
1071 ccw_device_accumulate_irb(cdev, irb);
1072 /* We don't care about basic sense etc. */
1074 default: /* timeout */
1078 wake_up(&cdev->private->wait_q);
1082 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1084 struct subchannel *sch;
1086 sch = to_subchannel(cdev->dev.parent);
1087 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1088 /* Couldn't enable the subchannel for i/o. Sick device. */
1091 /* After 60s the device recognition is considered to have failed. */
1092 ccw_device_set_timeout(cdev, 60*HZ);
1094 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1095 ccw_device_sense_id_start(cdev);
1099 device_trigger_reprobe(struct subchannel *sch)
1101 struct ccw_device *cdev;
1103 if (!sch->dev.driver_data)
1105 cdev = sch->dev.driver_data;
1106 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1109 /* Update some values. */
1110 if (stsch(sch->schid, &sch->schib))
1114 * The pim, pam, pom values may not be accurate, but they are the best
1115 * we have before performing device selection :/
1117 sch->lpm = sch->schib.pmcw.pim &
1118 sch->schib.pmcw.pam &
1119 sch->schib.pmcw.pom &
1121 /* Re-set some bits in the pmcw that were lost. */
1122 sch->schib.pmcw.isc = 3;
1123 sch->schib.pmcw.csense = 1;
1124 sch->schib.pmcw.ena = 0;
1125 if ((sch->lpm & (sch->lpm - 1)) != 0)
1126 sch->schib.pmcw.mp = 1;
1127 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1128 /* We should also udate ssd info, but this has to wait. */
1129 ccw_device_start_id(cdev, 0);
1133 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1135 struct subchannel *sch;
1137 sch = to_subchannel(cdev->dev.parent);
1139 * An interrupt in state offline means a previous disable was not
1140 * successful. Try again.
1142 cio_disable_subchannel(sch);
1146 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1148 retry_set_schib(cdev);
1149 cdev->private->state = DEV_STATE_ONLINE;
1150 dev_fsm_event(cdev, dev_event);
1153 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1154 enum dev_event dev_event)
1156 cmf_retry_copy_block(cdev);
1157 cdev->private->state = DEV_STATE_ONLINE;
1158 dev_fsm_event(cdev, dev_event);
1162 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1164 ccw_device_set_timeout(cdev, 0);
1165 if (dev_event == DEV_EVENT_NOTOPER)
1166 cdev->private->state = DEV_STATE_NOT_OPER;
1168 cdev->private->state = DEV_STATE_OFFLINE;
1169 wake_up(&cdev->private->wait_q);
1173 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1177 ret = ccw_device_cancel_halt_clear(cdev);
1180 cdev->private->state = DEV_STATE_OFFLINE;
1181 wake_up(&cdev->private->wait_q);
1184 cdev->private->state = DEV_STATE_NOT_OPER;
1185 wake_up(&cdev->private->wait_q);
1188 ccw_device_set_timeout(cdev, HZ/10);
1193 * No operation action. This is used e.g. to ignore a timeout event in
1197 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1202 * Bug operation action.
1205 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1207 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1208 cdev->private->state, dev_event);
1213 * device statemachine
1215 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1216 [DEV_STATE_NOT_OPER] = {
1217 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1218 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1219 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1220 [DEV_EVENT_VERIFY] = ccw_device_nop,
1222 [DEV_STATE_SENSE_PGID] = {
1223 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1224 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1225 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1226 [DEV_EVENT_VERIFY] = ccw_device_nop,
1228 [DEV_STATE_SENSE_ID] = {
1229 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1230 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1231 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1232 [DEV_EVENT_VERIFY] = ccw_device_nop,
1234 [DEV_STATE_OFFLINE] = {
1235 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1236 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1237 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1238 [DEV_EVENT_VERIFY] = ccw_device_nop,
1240 [DEV_STATE_VERIFY] = {
1241 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1242 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1243 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1244 [DEV_EVENT_VERIFY] = ccw_device_nop,
1246 [DEV_STATE_ONLINE] = {
1247 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1248 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1249 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1250 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1252 [DEV_STATE_W4SENSE] = {
1253 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1254 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1255 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1256 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1258 [DEV_STATE_DISBAND_PGID] = {
1259 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1260 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1261 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1262 [DEV_EVENT_VERIFY] = ccw_device_nop,
1264 [DEV_STATE_BOXED] = {
1265 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1266 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1267 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1268 [DEV_EVENT_VERIFY] = ccw_device_nop,
1270 /* states to wait for i/o completion before doing something */
1271 [DEV_STATE_CLEAR_VERIFY] = {
1272 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1273 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1274 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1275 [DEV_EVENT_VERIFY] = ccw_device_nop,
1277 [DEV_STATE_TIMEOUT_KILL] = {
1278 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1279 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1280 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1281 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1283 [DEV_STATE_WAIT4IO] = {
1284 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1285 [DEV_EVENT_INTERRUPT] = ccw_device_wait4io_irq,
1286 [DEV_EVENT_TIMEOUT] = ccw_device_wait4io_timeout,
1287 [DEV_EVENT_VERIFY] = ccw_device_wait4io_verify,
1289 [DEV_STATE_QUIESCE] = {
1290 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1291 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1292 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1293 [DEV_EVENT_VERIFY] = ccw_device_nop,
1295 /* special states for devices gone not operational */
1296 [DEV_STATE_DISCONNECTED] = {
1297 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1298 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1299 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1300 [DEV_EVENT_VERIFY] = ccw_device_nop,
1302 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1303 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1304 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1305 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1306 [DEV_EVENT_VERIFY] = ccw_device_nop,
1308 [DEV_STATE_CMFCHANGE] = {
1309 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1310 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1311 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1312 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1314 [DEV_STATE_CMFUPDATE] = {
1315 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1316 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1317 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1318 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1323 * io_subchannel_irq is called for "real" interrupts or for status
1324 * pending conditions on msch.
1327 io_subchannel_irq (struct device *pdev)
1329 struct ccw_device *cdev;
1331 cdev = to_subchannel(pdev)->dev.driver_data;
1333 CIO_TRACE_EVENT (3, "IRQ");
1334 CIO_TRACE_EVENT (3, pdev->bus_id);
1336 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1339 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);