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;
62 void device_set_intretry(struct subchannel *sch)
64 struct ccw_device *cdev;
66 cdev = sch->dev.driver_data;
69 cdev->private->flags.intretry = 1;
72 int device_trigger_verify(struct subchannel *sch)
74 struct ccw_device *cdev;
76 cdev = sch->dev.driver_data;
77 if (!cdev || !cdev->online)
79 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
84 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
87 ccw_device_timeout(unsigned long data)
89 struct ccw_device *cdev;
91 cdev = (struct ccw_device *) data;
92 spin_lock_irq(cdev->ccwlock);
93 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
94 spin_unlock_irq(cdev->ccwlock);
101 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
104 del_timer(&cdev->private->timer);
107 if (timer_pending(&cdev->private->timer)) {
108 if (mod_timer(&cdev->private->timer, jiffies + expires))
111 cdev->private->timer.function = ccw_device_timeout;
112 cdev->private->timer.data = (unsigned long) cdev;
113 cdev->private->timer.expires = jiffies + expires;
114 add_timer(&cdev->private->timer);
117 /* Kill any pending timers after machine check. */
119 device_kill_pending_timer(struct subchannel *sch)
121 struct ccw_device *cdev;
123 if (!sch->dev.driver_data)
125 cdev = sch->dev.driver_data;
126 ccw_device_set_timeout(cdev, 0);
130 * Cancel running i/o. This is called repeatedly since halt/clear are
131 * asynchronous operations. We do one try with cio_cancel, two tries
132 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
133 * Returns 0 if device now idle, -ENODEV for device not operational and
134 * -EBUSY if an interrupt is expected (either from halt/clear or from a
138 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
140 struct subchannel *sch;
143 sch = to_subchannel(cdev->dev.parent);
144 ret = stsch(sch->schid, &sch->schib);
145 if (ret || !sch->schib.pmcw.dnv)
147 if (!sch->schib.pmcw.ena || sch->schib.scsw.actl == 0)
148 /* Not operational or no activity -> done. */
150 /* Stage 1: cancel io. */
151 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
152 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
153 ret = cio_cancel(sch);
156 /* cancel io unsuccessful. From now on it is asynchronous. */
157 cdev->private->iretry = 3; /* 3 halt retries. */
159 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
160 /* Stage 2: halt io. */
161 if (cdev->private->iretry) {
162 cdev->private->iretry--;
165 return (ret == 0) ? -EBUSY : ret;
167 /* halt io unsuccessful. */
168 cdev->private->iretry = 255; /* 255 clear retries. */
170 /* Stage 3: clear io. */
171 if (cdev->private->iretry) {
172 cdev->private->iretry--;
173 ret = cio_clear (sch);
174 return (ret == 0) ? -EBUSY : ret;
176 panic("Can't stop i/o on subchannel.\n");
180 ccw_device_handle_oper(struct ccw_device *cdev)
182 struct subchannel *sch;
184 sch = to_subchannel(cdev->dev.parent);
185 cdev->private->flags.recog_done = 1;
187 * Check if cu type and device type still match. If
188 * not, it is certainly another device and we have to
189 * de- and re-register. Also check here for non-matching devno.
191 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
192 cdev->id.cu_model != cdev->private->senseid.cu_model ||
193 cdev->id.dev_type != cdev->private->senseid.dev_type ||
194 cdev->id.dev_model != cdev->private->senseid.dev_model ||
195 cdev->private->dev_id.devno != sch->schib.pmcw.dev) {
196 PREPARE_WORK(&cdev->private->kick_work,
197 ccw_device_do_unreg_rereg, cdev);
198 queue_work(ccw_device_work, &cdev->private->kick_work);
201 cdev->private->flags.donotify = 1;
206 * The machine won't give us any notification by machine check if a chpid has
207 * been varied online on the SE so we have to find out by magic (i. e. driving
208 * the channel subsystem to device selection and updating our path masks).
211 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
215 for (i = 0; i<8; i++) {
217 if (!(sch->lpm & mask))
221 chpid_is_actually_online(sch->schib.pmcw.chpid[i]);
226 * Stop device recognition.
229 ccw_device_recog_done(struct ccw_device *cdev, int state)
231 struct subchannel *sch;
232 int notify, old_lpm, same_dev;
234 sch = to_subchannel(cdev->dev.parent);
236 ccw_device_set_timeout(cdev, 0);
237 cio_disable_subchannel(sch);
239 * Now that we tried recognition, we have performed device selection
240 * through ssch() and the path information is up to date.
243 stsch(sch->schid, &sch->schib);
244 sch->lpm = sch->schib.pmcw.pam & sch->opm;
245 /* Check since device may again have become not operational. */
246 if (!sch->schib.pmcw.dnv)
247 state = DEV_STATE_NOT_OPER;
248 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
249 /* Force reprobe on all chpids. */
251 if (sch->lpm != old_lpm)
252 __recover_lost_chpids(sch, old_lpm);
253 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
254 if (state == DEV_STATE_NOT_OPER) {
255 cdev->private->flags.recog_done = 1;
256 cdev->private->state = DEV_STATE_DISCONNECTED;
259 /* Boxed devices don't need extra treatment. */
262 same_dev = 0; /* Keep the compiler quiet... */
264 case DEV_STATE_NOT_OPER:
265 CIO_DEBUG(KERN_WARNING, 2,
266 "SenseID : unknown device %04x on subchannel "
267 "0.%x.%04x\n", cdev->private->dev_id.devno,
268 sch->schid.ssid, sch->schid.sch_no);
270 case DEV_STATE_OFFLINE:
271 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
272 same_dev = ccw_device_handle_oper(cdev);
275 /* fill out sense information */
276 memset(&cdev->id, 0, sizeof(cdev->id));
277 cdev->id.cu_type = cdev->private->senseid.cu_type;
278 cdev->id.cu_model = cdev->private->senseid.cu_model;
279 cdev->id.dev_type = cdev->private->senseid.dev_type;
280 cdev->id.dev_model = cdev->private->senseid.dev_model;
282 cdev->private->state = DEV_STATE_OFFLINE;
284 /* Get device online again. */
285 ccw_device_online(cdev);
286 wake_up(&cdev->private->wait_q);
290 /* Issue device info message. */
291 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
292 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
294 cdev->private->dev_id.ssid,
295 cdev->private->dev_id.devno,
296 cdev->id.cu_type, cdev->id.cu_model,
297 cdev->id.dev_type, cdev->id.dev_model);
299 case DEV_STATE_BOXED:
300 CIO_DEBUG(KERN_WARNING, 2,
301 "SenseID : boxed device %04x on subchannel "
302 "0.%x.%04x\n", cdev->private->dev_id.devno,
303 sch->schid.ssid, sch->schid.sch_no);
306 cdev->private->state = state;
307 io_subchannel_recog_done(cdev);
308 if (state != DEV_STATE_NOT_OPER)
309 wake_up(&cdev->private->wait_q);
313 * Function called from device_id.c after sense id has completed.
316 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
320 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
322 case -ETIME: /* Sense id stopped by timeout. */
323 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
326 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
332 ccw_device_oper_notify(void *data)
334 struct ccw_device *cdev;
335 struct subchannel *sch;
339 sch = to_subchannel(cdev->dev.parent);
340 ret = (sch->driver && sch->driver->notify) ?
341 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
343 /* Driver doesn't want device back. */
344 ccw_device_do_unreg_rereg(cdev);
346 /* Reenable channel measurements, if needed. */
348 wake_up(&cdev->private->wait_q);
353 * Finished with online/offline processing.
356 ccw_device_done(struct ccw_device *cdev, int state)
358 struct subchannel *sch;
360 sch = to_subchannel(cdev->dev.parent);
362 ccw_device_set_timeout(cdev, 0);
364 if (state != DEV_STATE_ONLINE)
365 cio_disable_subchannel(sch);
367 /* Reset device status. */
368 memset(&cdev->private->irb, 0, sizeof(struct irb));
370 cdev->private->state = state;
373 if (state == DEV_STATE_BOXED)
374 CIO_DEBUG(KERN_WARNING, 2,
375 "Boxed device %04x on subchannel %04x\n",
376 cdev->private->dev_id.devno, sch->schid.sch_no);
378 if (cdev->private->flags.donotify) {
379 cdev->private->flags.donotify = 0;
380 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify,
382 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
384 wake_up(&cdev->private->wait_q);
386 if (css_init_done && state != DEV_STATE_ONLINE)
387 put_device (&cdev->dev);
390 static inline int cmp_pgid(struct pgid *p1, struct pgid *p2)
398 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
401 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
407 for (i = 0; i < 8; i++) {
408 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
411 if (cdev->private->pgid[last].inf.ps.state1 ==
413 /* First non-zero PGID */
417 if (cmp_pgid(&cdev->private->pgid[i],
418 &cdev->private->pgid[last]) == 0)
419 /* Non-conflicting PGIDs */
422 /* PGID mismatch, can't pathgroup. */
423 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
424 "0.%x.%04x, can't pathgroup\n",
425 cdev->private->dev_id.ssid,
426 cdev->private->dev_id.devno);
427 cdev->private->options.pgroup = 0;
430 if (cdev->private->pgid[last].inf.ps.state1 ==
432 /* No previous pgid found */
433 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
434 sizeof(struct pgid));
436 /* Use existing pgid */
437 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
438 sizeof(struct pgid));
442 * Function called from device_pgid.c after sense path ground has completed.
445 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
447 struct subchannel *sch;
449 sch = to_subchannel(cdev->dev.parent);
451 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
452 cdev->private->options.pgroup = 0;
454 case 0: /* success */
455 case -EACCES: /* partial success, some paths not operational */
456 /* Check if all pgids are equal or 0. */
457 __ccw_device_get_common_pgid(cdev);
459 case -ETIME: /* Sense path group id stopped by timeout. */
460 case -EUSERS: /* device is reserved for someone else. */
461 ccw_device_done(cdev, DEV_STATE_BOXED);
464 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
467 /* Start Path Group verification. */
468 cdev->private->state = DEV_STATE_VERIFY;
469 cdev->private->flags.doverify = 0;
470 ccw_device_verify_start(cdev);
474 * Start device recognition.
477 ccw_device_recognition(struct ccw_device *cdev)
479 struct subchannel *sch;
482 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
483 (cdev->private->state != DEV_STATE_BOXED))
485 sch = to_subchannel(cdev->dev.parent);
486 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
488 /* Couldn't enable the subchannel for i/o. Sick device. */
491 /* After 60s the device recognition is considered to have failed. */
492 ccw_device_set_timeout(cdev, 60*HZ);
495 * We used to start here with a sense pgid to find out whether a device
496 * is locked by someone else. Unfortunately, the sense pgid command
497 * code has other meanings on devices predating the path grouping
498 * algorithm, so we start with sense id and box the device after an
499 * timeout (or if sense pgid during path verification detects the device
500 * is locked, as may happen on newer devices).
502 cdev->private->flags.recog_done = 0;
503 cdev->private->state = DEV_STATE_SENSE_ID;
504 ccw_device_sense_id_start(cdev);
509 * Handle timeout in device recognition.
512 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
516 ret = ccw_device_cancel_halt_clear(cdev);
519 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
522 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
525 ccw_device_set_timeout(cdev, 3*HZ);
531 ccw_device_nopath_notify(void *data)
533 struct ccw_device *cdev;
534 struct subchannel *sch;
538 sch = to_subchannel(cdev->dev.parent);
542 ret = (sch->driver && sch->driver->notify) ?
543 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
545 if (get_device(&sch->dev)) {
546 /* Driver doesn't want to keep device. */
547 cio_disable_subchannel(sch);
548 if (get_device(&cdev->dev)) {
549 PREPARE_WORK(&cdev->private->kick_work,
550 ccw_device_call_sch_unregister,
552 queue_work(ccw_device_work,
553 &cdev->private->kick_work);
555 put_device(&sch->dev);
558 cio_disable_subchannel(sch);
559 ccw_device_set_timeout(cdev, 0);
560 cdev->private->flags.fake_irb = 0;
561 cdev->private->state = DEV_STATE_DISCONNECTED;
562 wake_up(&cdev->private->wait_q);
567 ccw_device_verify_done(struct ccw_device *cdev, int err)
569 struct subchannel *sch;
571 sch = to_subchannel(cdev->dev.parent);
572 /* Update schib - pom may have changed. */
573 stsch(sch->schid, &sch->schib);
574 /* Update lpm with verified path mask. */
576 /* Repeat path verification? */
577 if (cdev->private->flags.doverify) {
578 cdev->private->flags.doverify = 0;
579 ccw_device_verify_start(cdev);
583 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
584 cdev->private->options.pgroup = 0;
586 ccw_device_done(cdev, DEV_STATE_ONLINE);
587 /* Deliver fake irb to device driver, if needed. */
588 if (cdev->private->flags.fake_irb) {
589 memset(&cdev->private->irb, 0, sizeof(struct irb));
590 cdev->private->irb.scsw.cc = 1;
591 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
592 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
593 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
594 cdev->private->flags.fake_irb = 0;
596 cdev->handler(cdev, cdev->private->intparm,
597 &cdev->private->irb);
598 memset(&cdev->private->irb, 0, sizeof(struct irb));
602 /* Reset oper notify indication after verify error. */
603 cdev->private->flags.donotify = 0;
604 ccw_device_done(cdev, DEV_STATE_BOXED);
607 /* Reset oper notify indication after verify error. */
608 cdev->private->flags.donotify = 0;
609 PREPARE_WORK(&cdev->private->kick_work,
610 ccw_device_nopath_notify, cdev);
611 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
612 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
621 ccw_device_online(struct ccw_device *cdev)
623 struct subchannel *sch;
626 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
627 (cdev->private->state != DEV_STATE_BOXED))
629 sch = to_subchannel(cdev->dev.parent);
630 if (css_init_done && !get_device(&cdev->dev))
632 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
634 /* Couldn't enable the subchannel for i/o. Sick device. */
636 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
639 /* Do we want to do path grouping? */
640 if (!cdev->private->options.pgroup) {
641 /* Start initial path verification. */
642 cdev->private->state = DEV_STATE_VERIFY;
643 cdev->private->flags.doverify = 0;
644 ccw_device_verify_start(cdev);
647 /* Do a SensePGID first. */
648 cdev->private->state = DEV_STATE_SENSE_PGID;
649 ccw_device_sense_pgid_start(cdev);
654 ccw_device_disband_done(struct ccw_device *cdev, int err)
658 ccw_device_done(cdev, DEV_STATE_OFFLINE);
661 ccw_device_done(cdev, DEV_STATE_BOXED);
664 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
673 ccw_device_offline(struct ccw_device *cdev)
675 struct subchannel *sch;
677 sch = to_subchannel(cdev->dev.parent);
678 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
680 if (cdev->private->state != DEV_STATE_ONLINE) {
681 if (sch->schib.scsw.actl != 0)
685 if (sch->schib.scsw.actl != 0)
687 /* Are we doing path grouping? */
688 if (!cdev->private->options.pgroup) {
689 /* No, set state offline immediately. */
690 ccw_device_done(cdev, DEV_STATE_OFFLINE);
693 /* Start Set Path Group commands. */
694 cdev->private->state = DEV_STATE_DISBAND_PGID;
695 ccw_device_disband_start(cdev);
700 * Handle timeout in device online/offline process.
703 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
707 ret = ccw_device_cancel_halt_clear(cdev);
710 ccw_device_done(cdev, DEV_STATE_BOXED);
713 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
716 ccw_device_set_timeout(cdev, 3*HZ);
721 * Handle not oper event in device recognition.
724 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
726 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
730 * Handle not operational event while offline.
733 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
735 struct subchannel *sch;
737 cdev->private->state = DEV_STATE_NOT_OPER;
738 sch = to_subchannel(cdev->dev.parent);
739 if (get_device(&cdev->dev)) {
740 PREPARE_WORK(&cdev->private->kick_work,
741 ccw_device_call_sch_unregister, cdev);
742 queue_work(ccw_device_work, &cdev->private->kick_work);
744 wake_up(&cdev->private->wait_q);
748 * Handle not operational event while online.
751 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
753 struct subchannel *sch;
755 sch = to_subchannel(cdev->dev.parent);
756 if (sch->driver->notify &&
757 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
758 ccw_device_set_timeout(cdev, 0);
759 cdev->private->flags.fake_irb = 0;
760 cdev->private->state = DEV_STATE_DISCONNECTED;
761 wake_up(&cdev->private->wait_q);
764 cdev->private->state = DEV_STATE_NOT_OPER;
765 cio_disable_subchannel(sch);
766 if (sch->schib.scsw.actl != 0) {
767 // FIXME: not-oper indication to device driver ?
768 ccw_device_call_handler(cdev);
770 if (get_device(&cdev->dev)) {
771 PREPARE_WORK(&cdev->private->kick_work,
772 ccw_device_call_sch_unregister, cdev);
773 queue_work(ccw_device_work, &cdev->private->kick_work);
775 wake_up(&cdev->private->wait_q);
779 * Handle path verification event.
782 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
784 struct subchannel *sch;
786 if (cdev->private->state == DEV_STATE_W4SENSE) {
787 cdev->private->flags.doverify = 1;
790 sch = to_subchannel(cdev->dev.parent);
792 * Since we might not just be coming from an interrupt from the
793 * subchannel we have to update the schib.
795 stsch(sch->schid, &sch->schib);
797 if (sch->schib.scsw.actl != 0 ||
798 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
799 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
801 * No final status yet or final status not yet delivered
802 * to the device driver. Can't do path verfication now,
803 * delay until final status was delivered.
805 cdev->private->flags.doverify = 1;
808 /* Device is idle, we can do the path verification. */
809 cdev->private->state = DEV_STATE_VERIFY;
810 cdev->private->flags.doverify = 0;
811 ccw_device_verify_start(cdev);
815 * Got an interrupt for a normal io (state online).
818 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
822 irb = (struct irb *) __LC_IRB;
823 /* Check for unsolicited interrupt. */
824 if ((irb->scsw.stctl ==
825 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
826 && (!irb->scsw.cc)) {
827 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
828 !irb->esw.esw0.erw.cons) {
829 /* Unit check but no sense data. Need basic sense. */
830 if (ccw_device_do_sense(cdev, irb) != 0)
831 goto call_handler_unsol;
832 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
833 cdev->private->state = DEV_STATE_W4SENSE;
834 cdev->private->intparm = 0;
839 cdev->handler (cdev, 0, irb);
842 /* Accumulate status and find out if a basic sense is needed. */
843 ccw_device_accumulate_irb(cdev, irb);
844 if (cdev->private->flags.dosense) {
845 if (ccw_device_do_sense(cdev, irb) == 0) {
846 cdev->private->state = DEV_STATE_W4SENSE;
850 /* Call the handler. */
851 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
852 /* Start delayed path verification. */
853 ccw_device_online_verify(cdev, 0);
857 * Got an timeout in online state.
860 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
864 ccw_device_set_timeout(cdev, 0);
865 ret = ccw_device_cancel_halt_clear(cdev);
867 ccw_device_set_timeout(cdev, 3*HZ);
868 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
871 if (ret == -ENODEV) {
872 struct subchannel *sch;
874 sch = to_subchannel(cdev->dev.parent);
876 PREPARE_WORK(&cdev->private->kick_work,
877 ccw_device_nopath_notify, cdev);
878 queue_work(ccw_device_notify_work,
879 &cdev->private->kick_work);
881 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
882 } else if (cdev->handler)
883 cdev->handler(cdev, cdev->private->intparm,
884 ERR_PTR(-ETIMEDOUT));
888 * Got an interrupt for a basic sense.
891 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
895 irb = (struct irb *) __LC_IRB;
896 /* Check for unsolicited interrupt. */
897 if (irb->scsw.stctl ==
898 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
899 if (irb->scsw.cc == 1)
900 /* Basic sense hasn't started. Try again. */
901 ccw_device_do_sense(cdev, irb);
903 printk(KERN_INFO "Huh? %s(%s): unsolicited "
905 __FUNCTION__, cdev->dev.bus_id);
907 cdev->handler (cdev, 0, irb);
912 * Check if a halt or clear has been issued in the meanwhile. If yes,
913 * only deliver the halt/clear interrupt to the device driver as if it
914 * had killed the original request.
916 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
917 /* Retry Basic Sense if requested. */
918 if (cdev->private->flags.intretry) {
919 cdev->private->flags.intretry = 0;
920 ccw_device_do_sense(cdev, irb);
923 cdev->private->flags.dosense = 0;
924 memset(&cdev->private->irb, 0, sizeof(struct irb));
925 ccw_device_accumulate_irb(cdev, irb);
928 /* Add basic sense info to irb. */
929 ccw_device_accumulate_basic_sense(cdev, irb);
930 if (cdev->private->flags.dosense) {
931 /* Another basic sense is needed. */
932 ccw_device_do_sense(cdev, irb);
936 cdev->private->state = DEV_STATE_ONLINE;
937 /* Call the handler. */
938 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
939 /* Start delayed path verification. */
940 ccw_device_online_verify(cdev, 0);
944 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
948 irb = (struct irb *) __LC_IRB;
949 /* Accumulate status. We don't do basic sense. */
950 ccw_device_accumulate_irb(cdev, irb);
951 /* Remember to clear irb to avoid residuals. */
952 memset(&cdev->private->irb, 0, sizeof(struct irb));
953 /* Try to start delayed device verification. */
954 ccw_device_online_verify(cdev, 0);
955 /* Note: Don't call handler for cio initiated clear! */
959 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
961 struct subchannel *sch;
963 sch = to_subchannel(cdev->dev.parent);
964 ccw_device_set_timeout(cdev, 0);
965 /* OK, i/o is dead now. Call interrupt handler. */
966 cdev->private->state = DEV_STATE_ONLINE;
968 cdev->handler(cdev, cdev->private->intparm,
971 PREPARE_WORK(&cdev->private->kick_work,
972 ccw_device_nopath_notify, cdev);
973 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
974 } else if (cdev->private->flags.doverify)
975 /* Start delayed path verification. */
976 ccw_device_online_verify(cdev, 0);
980 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
984 ret = ccw_device_cancel_halt_clear(cdev);
986 ccw_device_set_timeout(cdev, 3*HZ);
989 if (ret == -ENODEV) {
990 struct subchannel *sch;
992 sch = to_subchannel(cdev->dev.parent);
994 PREPARE_WORK(&cdev->private->kick_work,
995 ccw_device_nopath_notify, cdev);
996 queue_work(ccw_device_notify_work,
997 &cdev->private->kick_work);
999 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1002 //FIXME: Can we get here?
1003 cdev->private->state = DEV_STATE_ONLINE;
1005 cdev->handler(cdev, cdev->private->intparm,
1009 void device_kill_io(struct subchannel *sch)
1012 struct ccw_device *cdev;
1014 cdev = sch->dev.driver_data;
1015 ret = ccw_device_cancel_halt_clear(cdev);
1016 if (ret == -EBUSY) {
1017 ccw_device_set_timeout(cdev, 3*HZ);
1018 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1021 if (ret == -ENODEV) {
1023 PREPARE_WORK(&cdev->private->kick_work,
1024 ccw_device_nopath_notify, cdev);
1025 queue_work(ccw_device_notify_work,
1026 &cdev->private->kick_work);
1028 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1032 cdev->handler(cdev, cdev->private->intparm,
1035 PREPARE_WORK(&cdev->private->kick_work,
1036 ccw_device_nopath_notify, cdev);
1037 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1039 /* Start delayed path verification. */
1040 ccw_device_online_verify(cdev, 0);
1044 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
1046 /* Start verification after current task finished. */
1047 cdev->private->flags.doverify = 1;
1051 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1055 switch (dev_event) {
1056 case DEV_EVENT_INTERRUPT:
1057 irb = (struct irb *) __LC_IRB;
1058 /* Check for unsolicited interrupt. */
1059 if ((irb->scsw.stctl ==
1060 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1062 /* FIXME: we should restart stlck here, but this
1063 * is extremely unlikely ... */
1066 ccw_device_accumulate_irb(cdev, irb);
1067 /* We don't care about basic sense etc. */
1069 default: /* timeout */
1073 wake_up(&cdev->private->wait_q);
1077 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1079 struct subchannel *sch;
1081 sch = to_subchannel(cdev->dev.parent);
1082 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1083 /* Couldn't enable the subchannel for i/o. Sick device. */
1086 /* After 60s the device recognition is considered to have failed. */
1087 ccw_device_set_timeout(cdev, 60*HZ);
1089 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1090 ccw_device_sense_id_start(cdev);
1094 device_trigger_reprobe(struct subchannel *sch)
1096 struct ccw_device *cdev;
1098 if (!sch->dev.driver_data)
1100 cdev = sch->dev.driver_data;
1101 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1104 /* Update some values. */
1105 if (stsch(sch->schid, &sch->schib))
1109 * The pim, pam, pom values may not be accurate, but they are the best
1110 * we have before performing device selection :/
1112 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1113 /* Re-set some bits in the pmcw that were lost. */
1114 sch->schib.pmcw.isc = 3;
1115 sch->schib.pmcw.csense = 1;
1116 sch->schib.pmcw.ena = 0;
1117 if ((sch->lpm & (sch->lpm - 1)) != 0)
1118 sch->schib.pmcw.mp = 1;
1119 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1120 /* We should also udate ssd info, but this has to wait. */
1121 ccw_device_start_id(cdev, 0);
1125 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1127 struct subchannel *sch;
1129 sch = to_subchannel(cdev->dev.parent);
1131 * An interrupt in state offline means a previous disable was not
1132 * successful. Try again.
1134 cio_disable_subchannel(sch);
1138 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1140 retry_set_schib(cdev);
1141 cdev->private->state = DEV_STATE_ONLINE;
1142 dev_fsm_event(cdev, dev_event);
1145 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1146 enum dev_event dev_event)
1148 cmf_retry_copy_block(cdev);
1149 cdev->private->state = DEV_STATE_ONLINE;
1150 dev_fsm_event(cdev, dev_event);
1154 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1156 ccw_device_set_timeout(cdev, 0);
1157 if (dev_event == DEV_EVENT_NOTOPER)
1158 cdev->private->state = DEV_STATE_NOT_OPER;
1160 cdev->private->state = DEV_STATE_OFFLINE;
1161 wake_up(&cdev->private->wait_q);
1165 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1169 ret = ccw_device_cancel_halt_clear(cdev);
1172 cdev->private->state = DEV_STATE_OFFLINE;
1173 wake_up(&cdev->private->wait_q);
1176 cdev->private->state = DEV_STATE_NOT_OPER;
1177 wake_up(&cdev->private->wait_q);
1180 ccw_device_set_timeout(cdev, HZ/10);
1185 * No operation action. This is used e.g. to ignore a timeout event in
1189 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1194 * Bug operation action.
1197 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1199 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1200 cdev->private->state, dev_event);
1205 * device statemachine
1207 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1208 [DEV_STATE_NOT_OPER] = {
1209 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1210 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1211 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1212 [DEV_EVENT_VERIFY] = ccw_device_nop,
1214 [DEV_STATE_SENSE_PGID] = {
1215 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1216 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1217 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1218 [DEV_EVENT_VERIFY] = ccw_device_nop,
1220 [DEV_STATE_SENSE_ID] = {
1221 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1222 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1223 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1224 [DEV_EVENT_VERIFY] = ccw_device_nop,
1226 [DEV_STATE_OFFLINE] = {
1227 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1228 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1229 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1230 [DEV_EVENT_VERIFY] = ccw_device_nop,
1232 [DEV_STATE_VERIFY] = {
1233 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1234 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1235 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1236 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1238 [DEV_STATE_ONLINE] = {
1239 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1240 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1241 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1242 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1244 [DEV_STATE_W4SENSE] = {
1245 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1246 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1247 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1248 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1250 [DEV_STATE_DISBAND_PGID] = {
1251 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1252 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1253 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1254 [DEV_EVENT_VERIFY] = ccw_device_nop,
1256 [DEV_STATE_BOXED] = {
1257 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1258 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1259 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1260 [DEV_EVENT_VERIFY] = ccw_device_nop,
1262 /* states to wait for i/o completion before doing something */
1263 [DEV_STATE_CLEAR_VERIFY] = {
1264 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1265 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1266 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1267 [DEV_EVENT_VERIFY] = ccw_device_nop,
1269 [DEV_STATE_TIMEOUT_KILL] = {
1270 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1271 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1272 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1273 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1275 [DEV_STATE_QUIESCE] = {
1276 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1277 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1278 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1279 [DEV_EVENT_VERIFY] = ccw_device_nop,
1281 /* special states for devices gone not operational */
1282 [DEV_STATE_DISCONNECTED] = {
1283 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1284 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1285 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1286 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1288 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1289 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1290 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1291 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1292 [DEV_EVENT_VERIFY] = ccw_device_nop,
1294 [DEV_STATE_CMFCHANGE] = {
1295 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1296 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1297 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1298 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1300 [DEV_STATE_CMFUPDATE] = {
1301 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1302 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1303 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1304 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1309 * io_subchannel_irq is called for "real" interrupts or for status
1310 * pending conditions on msch.
1313 io_subchannel_irq (struct device *pdev)
1315 struct ccw_device *cdev;
1317 cdev = to_subchannel(pdev)->dev.driver_data;
1319 CIO_TRACE_EVENT (3, "IRQ");
1320 CIO_TRACE_EVENT (3, pdev->bus_id);
1322 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1325 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);