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 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
777 * No final status yet or final status not yet delivered
778 * to the device driver. Can't do path verfication now,
779 * delay until final status was delivered.
781 cdev->private->flags.doverify = 1;
784 /* Device is idle, we can do the path verification. */
785 cdev->private->state = DEV_STATE_VERIFY;
786 ccw_device_verify_start(cdev);
790 * Got an interrupt for a normal io (state online).
793 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
797 irb = (struct irb *) __LC_IRB;
798 /* Check for unsolicited interrupt. */
799 if ((irb->scsw.stctl ==
800 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
801 && (!irb->scsw.cc)) {
802 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
803 !irb->esw.esw0.erw.cons) {
804 /* Unit check but no sense data. Need basic sense. */
805 if (ccw_device_do_sense(cdev, irb) != 0)
806 goto call_handler_unsol;
807 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
808 cdev->private->state = DEV_STATE_W4SENSE;
809 cdev->private->intparm = 0;
814 cdev->handler (cdev, 0, irb);
817 /* Accumulate status and find out if a basic sense is needed. */
818 ccw_device_accumulate_irb(cdev, irb);
819 if (cdev->private->flags.dosense) {
820 if (ccw_device_do_sense(cdev, irb) == 0) {
821 cdev->private->state = DEV_STATE_W4SENSE;
825 /* Call the handler. */
826 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
827 /* Start delayed path verification. */
828 ccw_device_online_verify(cdev, 0);
832 * Got an timeout in online state.
835 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
839 ccw_device_set_timeout(cdev, 0);
840 ret = ccw_device_cancel_halt_clear(cdev);
842 ccw_device_set_timeout(cdev, 3*HZ);
843 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
846 if (ret == -ENODEV) {
847 struct subchannel *sch;
849 sch = to_subchannel(cdev->dev.parent);
851 PREPARE_WORK(&cdev->private->kick_work,
852 ccw_device_nopath_notify, (void *)cdev);
853 queue_work(ccw_device_notify_work,
854 &cdev->private->kick_work);
856 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
857 } else if (cdev->handler)
858 cdev->handler(cdev, cdev->private->intparm,
859 ERR_PTR(-ETIMEDOUT));
863 * Got an interrupt for a basic sense.
866 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
870 irb = (struct irb *) __LC_IRB;
871 /* Check for unsolicited interrupt. */
872 if (irb->scsw.stctl ==
873 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
874 if (irb->scsw.cc == 1)
875 /* Basic sense hasn't started. Try again. */
876 ccw_device_do_sense(cdev, irb);
878 printk("Huh? %s(%s): unsolicited interrupt...\n",
879 __FUNCTION__, cdev->dev.bus_id);
881 cdev->handler (cdev, 0, irb);
886 * Check if a halt or clear has been issued in the meanwhile. If yes,
887 * only deliver the halt/clear interrupt to the device driver as if it
888 * had killed the original request.
890 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
891 cdev->private->flags.dosense = 0;
892 memset(&cdev->private->irb, 0, sizeof(struct irb));
893 ccw_device_accumulate_irb(cdev, irb);
896 /* Add basic sense info to irb. */
897 ccw_device_accumulate_basic_sense(cdev, irb);
898 if (cdev->private->flags.dosense) {
899 /* Another basic sense is needed. */
900 ccw_device_do_sense(cdev, irb);
904 cdev->private->state = DEV_STATE_ONLINE;
905 /* Call the handler. */
906 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
907 /* Start delayed path verification. */
908 ccw_device_online_verify(cdev, 0);
912 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
916 irb = (struct irb *) __LC_IRB;
917 /* Accumulate status. We don't do basic sense. */
918 ccw_device_accumulate_irb(cdev, irb);
919 /* Remember to clear irb to avoid residuals. */
920 memset(&cdev->private->irb, 0, sizeof(struct irb));
921 /* Try to start delayed device verification. */
922 ccw_device_online_verify(cdev, 0);
923 /* Note: Don't call handler for cio initiated clear! */
927 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
929 struct subchannel *sch;
931 sch = to_subchannel(cdev->dev.parent);
932 ccw_device_set_timeout(cdev, 0);
933 /* OK, i/o is dead now. Call interrupt handler. */
934 cdev->private->state = DEV_STATE_ONLINE;
936 cdev->handler(cdev, cdev->private->intparm,
937 ERR_PTR(-ETIMEDOUT));
939 PREPARE_WORK(&cdev->private->kick_work,
940 ccw_device_nopath_notify, (void *)cdev);
941 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
942 } else if (cdev->private->flags.doverify)
943 /* Start delayed path verification. */
944 ccw_device_online_verify(cdev, 0);
948 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
952 ret = ccw_device_cancel_halt_clear(cdev);
954 ccw_device_set_timeout(cdev, 3*HZ);
957 if (ret == -ENODEV) {
958 struct subchannel *sch;
960 sch = to_subchannel(cdev->dev.parent);
962 PREPARE_WORK(&cdev->private->kick_work,
963 ccw_device_nopath_notify, (void *)cdev);
964 queue_work(ccw_device_notify_work,
965 &cdev->private->kick_work);
967 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
970 //FIXME: Can we get here?
971 cdev->private->state = DEV_STATE_ONLINE;
973 cdev->handler(cdev, cdev->private->intparm,
974 ERR_PTR(-ETIMEDOUT));
978 ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event)
981 struct subchannel *sch;
983 irb = (struct irb *) __LC_IRB;
985 * Accumulate status and find out if a basic sense is needed.
986 * This is fine since we have already adapted the lpm.
988 ccw_device_accumulate_irb(cdev, irb);
989 if (cdev->private->flags.dosense) {
990 if (ccw_device_do_sense(cdev, irb) == 0) {
991 cdev->private->state = DEV_STATE_W4SENSE;
996 /* Iff device is idle, reset timeout. */
997 sch = to_subchannel(cdev->dev.parent);
998 if (!stsch(sch->schid, &sch->schib))
999 if (sch->schib.scsw.actl == 0)
1000 ccw_device_set_timeout(cdev, 0);
1001 /* Call the handler. */
1002 ccw_device_call_handler(cdev);
1004 PREPARE_WORK(&cdev->private->kick_work,
1005 ccw_device_nopath_notify, (void *)cdev);
1006 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1007 } else if (cdev->private->flags.doverify)
1008 ccw_device_online_verify(cdev, 0);
1012 ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1015 struct subchannel *sch;
1017 sch = to_subchannel(cdev->dev.parent);
1018 ccw_device_set_timeout(cdev, 0);
1019 ret = ccw_device_cancel_halt_clear(cdev);
1020 if (ret == -EBUSY) {
1021 ccw_device_set_timeout(cdev, 3*HZ);
1022 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1025 if (ret == -ENODEV) {
1027 PREPARE_WORK(&cdev->private->kick_work,
1028 ccw_device_nopath_notify, (void *)cdev);
1029 queue_work(ccw_device_notify_work,
1030 &cdev->private->kick_work);
1032 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1036 cdev->handler(cdev, cdev->private->intparm,
1037 ERR_PTR(-ETIMEDOUT));
1039 PREPARE_WORK(&cdev->private->kick_work,
1040 ccw_device_nopath_notify, (void *)cdev);
1041 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1042 } else if (cdev->private->flags.doverify)
1043 /* Start delayed path verification. */
1044 ccw_device_online_verify(cdev, 0);
1048 ccw_device_wait4io_verify(struct ccw_device *cdev, enum dev_event dev_event)
1050 /* When the I/O has terminated, we have to start verification. */
1051 cdev->private->flags.doverify = 1;
1055 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1059 switch (dev_event) {
1060 case DEV_EVENT_INTERRUPT:
1061 irb = (struct irb *) __LC_IRB;
1062 /* Check for unsolicited interrupt. */
1063 if ((irb->scsw.stctl ==
1064 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1066 /* FIXME: we should restart stlck here, but this
1067 * is extremely unlikely ... */
1070 ccw_device_accumulate_irb(cdev, irb);
1071 /* We don't care about basic sense etc. */
1073 default: /* timeout */
1077 wake_up(&cdev->private->wait_q);
1081 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1083 struct subchannel *sch;
1085 sch = to_subchannel(cdev->dev.parent);
1086 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1087 /* Couldn't enable the subchannel for i/o. Sick device. */
1090 /* After 60s the device recognition is considered to have failed. */
1091 ccw_device_set_timeout(cdev, 60*HZ);
1093 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1094 ccw_device_sense_id_start(cdev);
1098 device_trigger_reprobe(struct subchannel *sch)
1100 struct ccw_device *cdev;
1102 if (!sch->dev.driver_data)
1104 cdev = sch->dev.driver_data;
1105 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1108 /* Update some values. */
1109 if (stsch(sch->schid, &sch->schib))
1113 * The pim, pam, pom values may not be accurate, but they are the best
1114 * we have before performing device selection :/
1116 sch->lpm = sch->schib.pmcw.pim &
1117 sch->schib.pmcw.pam &
1118 sch->schib.pmcw.pom &
1120 /* Re-set some bits in the pmcw that were lost. */
1121 sch->schib.pmcw.isc = 3;
1122 sch->schib.pmcw.csense = 1;
1123 sch->schib.pmcw.ena = 0;
1124 if ((sch->lpm & (sch->lpm - 1)) != 0)
1125 sch->schib.pmcw.mp = 1;
1126 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1127 /* We should also udate ssd info, but this has to wait. */
1128 ccw_device_start_id(cdev, 0);
1132 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1134 struct subchannel *sch;
1136 sch = to_subchannel(cdev->dev.parent);
1138 * An interrupt in state offline means a previous disable was not
1139 * successful. Try again.
1141 cio_disable_subchannel(sch);
1145 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1147 retry_set_schib(cdev);
1148 cdev->private->state = DEV_STATE_ONLINE;
1149 dev_fsm_event(cdev, dev_event);
1152 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1153 enum dev_event dev_event)
1155 cmf_retry_copy_block(cdev);
1156 cdev->private->state = DEV_STATE_ONLINE;
1157 dev_fsm_event(cdev, dev_event);
1161 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1163 ccw_device_set_timeout(cdev, 0);
1164 if (dev_event == DEV_EVENT_NOTOPER)
1165 cdev->private->state = DEV_STATE_NOT_OPER;
1167 cdev->private->state = DEV_STATE_OFFLINE;
1168 wake_up(&cdev->private->wait_q);
1172 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1176 ret = ccw_device_cancel_halt_clear(cdev);
1179 cdev->private->state = DEV_STATE_OFFLINE;
1180 wake_up(&cdev->private->wait_q);
1183 cdev->private->state = DEV_STATE_NOT_OPER;
1184 wake_up(&cdev->private->wait_q);
1187 ccw_device_set_timeout(cdev, HZ/10);
1192 * No operation action. This is used e.g. to ignore a timeout event in
1196 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1201 * Bug operation action.
1204 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1206 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1207 cdev->private->state, dev_event);
1212 * device statemachine
1214 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1215 [DEV_STATE_NOT_OPER] = {
1216 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1217 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1218 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1219 [DEV_EVENT_VERIFY] = ccw_device_nop,
1221 [DEV_STATE_SENSE_PGID] = {
1222 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1223 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1224 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1225 [DEV_EVENT_VERIFY] = ccw_device_nop,
1227 [DEV_STATE_SENSE_ID] = {
1228 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1229 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1230 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1231 [DEV_EVENT_VERIFY] = ccw_device_nop,
1233 [DEV_STATE_OFFLINE] = {
1234 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1235 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1236 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1237 [DEV_EVENT_VERIFY] = ccw_device_nop,
1239 [DEV_STATE_VERIFY] = {
1240 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1241 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1242 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1243 [DEV_EVENT_VERIFY] = ccw_device_nop,
1245 [DEV_STATE_ONLINE] = {
1246 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1247 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1248 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1249 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1251 [DEV_STATE_W4SENSE] = {
1252 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1253 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1254 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1255 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1257 [DEV_STATE_DISBAND_PGID] = {
1258 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1259 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1260 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1261 [DEV_EVENT_VERIFY] = ccw_device_nop,
1263 [DEV_STATE_BOXED] = {
1264 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1265 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1266 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1267 [DEV_EVENT_VERIFY] = ccw_device_nop,
1269 /* states to wait for i/o completion before doing something */
1270 [DEV_STATE_CLEAR_VERIFY] = {
1271 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1272 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1273 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1274 [DEV_EVENT_VERIFY] = ccw_device_nop,
1276 [DEV_STATE_TIMEOUT_KILL] = {
1277 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1278 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1279 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1280 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1282 [DEV_STATE_WAIT4IO] = {
1283 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1284 [DEV_EVENT_INTERRUPT] = ccw_device_wait4io_irq,
1285 [DEV_EVENT_TIMEOUT] = ccw_device_wait4io_timeout,
1286 [DEV_EVENT_VERIFY] = ccw_device_wait4io_verify,
1288 [DEV_STATE_QUIESCE] = {
1289 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1290 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1291 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1292 [DEV_EVENT_VERIFY] = ccw_device_nop,
1294 /* special states for devices gone not operational */
1295 [DEV_STATE_DISCONNECTED] = {
1296 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1297 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1298 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1299 [DEV_EVENT_VERIFY] = ccw_device_nop,
1301 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1302 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1303 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1304 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1305 [DEV_EVENT_VERIFY] = ccw_device_nop,
1307 [DEV_STATE_CMFCHANGE] = {
1308 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1309 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1310 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1311 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1313 [DEV_STATE_CMFUPDATE] = {
1314 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1315 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1316 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1317 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1322 * io_subchannel_irq is called for "real" interrupts or for status
1323 * pending conditions on msch.
1326 io_subchannel_irq (struct device *pdev)
1328 struct ccw_device *cdev;
1330 cdev = to_subchannel(pdev)->dev.driver_data;
1332 CIO_TRACE_EVENT (3, "IRQ");
1333 CIO_TRACE_EVENT (3, pdev->bus_id);
1335 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1338 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);