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)
148 /* Not operational -> 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.
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 PREPARE_WORK(&cdev->private->kick_work,
196 ccw_device_do_unreg_rereg);
197 queue_work(ccw_device_work, &cdev->private->kick_work);
200 cdev->private->flags.donotify = 1;
205 * The machine won't give us any notification by machine check if a chpid has
206 * been varied online on the SE so we have to find out by magic (i. e. driving
207 * the channel subsystem to device selection and updating our path masks).
210 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
214 for (i = 0; i<8; i++) {
216 if (!(sch->lpm & mask))
220 chpid_is_actually_online(sch->schib.pmcw.chpid[i]);
225 * Stop device recognition.
228 ccw_device_recog_done(struct ccw_device *cdev, int state)
230 struct subchannel *sch;
231 int notify, old_lpm, same_dev;
233 sch = to_subchannel(cdev->dev.parent);
235 ccw_device_set_timeout(cdev, 0);
236 cio_disable_subchannel(sch);
238 * Now that we tried recognition, we have performed device selection
239 * through ssch() and the path information is up to date.
242 stsch(sch->schid, &sch->schib);
243 sch->lpm = sch->schib.pmcw.pam & sch->opm;
244 /* Check since device may again have become not operational. */
245 if (!sch->schib.pmcw.dnv)
246 state = DEV_STATE_NOT_OPER;
247 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
248 /* Force reprobe on all chpids. */
250 if (sch->lpm != old_lpm)
251 __recover_lost_chpids(sch, old_lpm);
252 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
253 if (state == DEV_STATE_NOT_OPER) {
254 cdev->private->flags.recog_done = 1;
255 cdev->private->state = DEV_STATE_DISCONNECTED;
258 /* Boxed devices don't need extra treatment. */
261 same_dev = 0; /* Keep the compiler quiet... */
263 case DEV_STATE_NOT_OPER:
264 CIO_DEBUG(KERN_WARNING, 2,
265 "SenseID : unknown device %04x on subchannel "
266 "0.%x.%04x\n", cdev->private->dev_id.devno,
267 sch->schid.ssid, sch->schid.sch_no);
269 case DEV_STATE_OFFLINE:
270 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
271 same_dev = ccw_device_handle_oper(cdev);
274 /* fill out sense information */
275 memset(&cdev->id, 0, sizeof(cdev->id));
276 cdev->id.cu_type = cdev->private->senseid.cu_type;
277 cdev->id.cu_model = cdev->private->senseid.cu_model;
278 cdev->id.dev_type = cdev->private->senseid.dev_type;
279 cdev->id.dev_model = cdev->private->senseid.dev_model;
281 cdev->private->state = DEV_STATE_OFFLINE;
283 /* Get device online again. */
284 ccw_device_online(cdev);
285 wake_up(&cdev->private->wait_q);
289 /* Issue device info message. */
290 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
291 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
293 cdev->private->dev_id.ssid,
294 cdev->private->dev_id.devno,
295 cdev->id.cu_type, cdev->id.cu_model,
296 cdev->id.dev_type, cdev->id.dev_model);
298 case DEV_STATE_BOXED:
299 CIO_DEBUG(KERN_WARNING, 2,
300 "SenseID : boxed device %04x on subchannel "
301 "0.%x.%04x\n", cdev->private->dev_id.devno,
302 sch->schid.ssid, sch->schid.sch_no);
305 cdev->private->state = state;
306 io_subchannel_recog_done(cdev);
307 if (state != DEV_STATE_NOT_OPER)
308 wake_up(&cdev->private->wait_q);
312 * Function called from device_id.c after sense id has completed.
315 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
319 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
321 case -ETIME: /* Sense id stopped by timeout. */
322 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
325 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
331 ccw_device_oper_notify(struct work_struct *work)
333 struct ccw_device_private *priv;
334 struct ccw_device *cdev;
335 struct subchannel *sch;
339 priv = container_of(work, struct ccw_device_private, kick_work);
341 spin_lock_irqsave(cdev->ccwlock, flags);
342 sch = to_subchannel(cdev->dev.parent);
343 if (sch->driver && sch->driver->notify) {
344 spin_unlock_irqrestore(cdev->ccwlock, flags);
345 ret = sch->driver->notify(&sch->dev, CIO_OPER);
346 spin_lock_irqsave(cdev->ccwlock, flags);
350 /* Reenable channel measurements, if needed. */
351 spin_unlock_irqrestore(cdev->ccwlock, flags);
353 spin_lock_irqsave(cdev->ccwlock, flags);
354 wake_up(&cdev->private->wait_q);
356 spin_unlock_irqrestore(cdev->ccwlock, flags);
358 /* Driver doesn't want device back. */
359 ccw_device_do_unreg_rereg(work);
363 * Finished with online/offline processing.
366 ccw_device_done(struct ccw_device *cdev, int state)
368 struct subchannel *sch;
370 sch = to_subchannel(cdev->dev.parent);
372 ccw_device_set_timeout(cdev, 0);
374 if (state != DEV_STATE_ONLINE)
375 cio_disable_subchannel(sch);
377 /* Reset device status. */
378 memset(&cdev->private->irb, 0, sizeof(struct irb));
380 cdev->private->state = state;
383 if (state == DEV_STATE_BOXED)
384 CIO_DEBUG(KERN_WARNING, 2,
385 "Boxed device %04x on subchannel %04x\n",
386 cdev->private->dev_id.devno, sch->schid.sch_no);
388 if (cdev->private->flags.donotify) {
389 cdev->private->flags.donotify = 0;
390 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
391 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
393 wake_up(&cdev->private->wait_q);
395 if (css_init_done && state != DEV_STATE_ONLINE)
396 put_device (&cdev->dev);
399 static int cmp_pgid(struct pgid *p1, struct pgid *p2)
407 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
410 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
416 for (i = 0; i < 8; i++) {
417 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
420 if (cdev->private->pgid[last].inf.ps.state1 ==
422 /* First non-zero PGID */
426 if (cmp_pgid(&cdev->private->pgid[i],
427 &cdev->private->pgid[last]) == 0)
428 /* Non-conflicting PGIDs */
431 /* PGID mismatch, can't pathgroup. */
432 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
433 "0.%x.%04x, can't pathgroup\n",
434 cdev->private->dev_id.ssid,
435 cdev->private->dev_id.devno);
436 cdev->private->options.pgroup = 0;
439 if (cdev->private->pgid[last].inf.ps.state1 ==
441 /* No previous pgid found */
442 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
443 sizeof(struct pgid));
445 /* Use existing pgid */
446 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
447 sizeof(struct pgid));
451 * Function called from device_pgid.c after sense path ground has completed.
454 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
456 struct subchannel *sch;
458 sch = to_subchannel(cdev->dev.parent);
460 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
461 cdev->private->options.pgroup = 0;
463 case 0: /* success */
464 case -EACCES: /* partial success, some paths not operational */
465 /* Check if all pgids are equal or 0. */
466 __ccw_device_get_common_pgid(cdev);
468 case -ETIME: /* Sense path group id stopped by timeout. */
469 case -EUSERS: /* device is reserved for someone else. */
470 ccw_device_done(cdev, DEV_STATE_BOXED);
473 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
476 /* Start Path Group verification. */
477 cdev->private->state = DEV_STATE_VERIFY;
478 cdev->private->flags.doverify = 0;
479 ccw_device_verify_start(cdev);
483 * Start device recognition.
486 ccw_device_recognition(struct ccw_device *cdev)
488 struct subchannel *sch;
491 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
492 (cdev->private->state != DEV_STATE_BOXED))
494 sch = to_subchannel(cdev->dev.parent);
495 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
497 /* Couldn't enable the subchannel for i/o. Sick device. */
500 /* After 60s the device recognition is considered to have failed. */
501 ccw_device_set_timeout(cdev, 60*HZ);
504 * We used to start here with a sense pgid to find out whether a device
505 * is locked by someone else. Unfortunately, the sense pgid command
506 * code has other meanings on devices predating the path grouping
507 * algorithm, so we start with sense id and box the device after an
508 * timeout (or if sense pgid during path verification detects the device
509 * is locked, as may happen on newer devices).
511 cdev->private->flags.recog_done = 0;
512 cdev->private->state = DEV_STATE_SENSE_ID;
513 ccw_device_sense_id_start(cdev);
518 * Handle timeout in device recognition.
521 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
525 ret = ccw_device_cancel_halt_clear(cdev);
528 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
531 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
534 ccw_device_set_timeout(cdev, 3*HZ);
540 ccw_device_nopath_notify(struct work_struct *work)
542 struct ccw_device_private *priv;
543 struct ccw_device *cdev;
544 struct subchannel *sch;
548 priv = container_of(work, struct ccw_device_private, kick_work);
550 spin_lock_irqsave(cdev->ccwlock, flags);
551 sch = to_subchannel(cdev->dev.parent);
555 if (sch->driver && sch->driver->notify) {
556 spin_unlock_irqrestore(cdev->ccwlock, flags);
557 ret = sch->driver->notify(&sch->dev, CIO_NO_PATH);
558 spin_lock_irqsave(cdev->ccwlock, flags);
562 if (get_device(&sch->dev)) {
563 /* Driver doesn't want to keep device. */
564 cio_disable_subchannel(sch);
565 if (get_device(&cdev->dev)) {
566 PREPARE_WORK(&cdev->private->kick_work,
567 ccw_device_call_sch_unregister);
568 queue_work(ccw_device_work,
569 &cdev->private->kick_work);
571 put_device(&sch->dev);
574 cio_disable_subchannel(sch);
575 ccw_device_set_timeout(cdev, 0);
576 cdev->private->flags.fake_irb = 0;
577 cdev->private->state = DEV_STATE_DISCONNECTED;
578 wake_up(&cdev->private->wait_q);
581 spin_unlock_irqrestore(cdev->ccwlock, flags);
585 ccw_device_verify_done(struct ccw_device *cdev, int err)
587 struct subchannel *sch;
589 sch = to_subchannel(cdev->dev.parent);
590 /* Update schib - pom may have changed. */
591 stsch(sch->schid, &sch->schib);
592 /* Update lpm with verified path mask. */
594 /* Repeat path verification? */
595 if (cdev->private->flags.doverify) {
596 cdev->private->flags.doverify = 0;
597 ccw_device_verify_start(cdev);
601 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
602 cdev->private->options.pgroup = 0;
604 ccw_device_done(cdev, DEV_STATE_ONLINE);
605 /* Deliver fake irb to device driver, if needed. */
606 if (cdev->private->flags.fake_irb) {
607 memset(&cdev->private->irb, 0, sizeof(struct irb));
608 cdev->private->irb.scsw.cc = 1;
609 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
610 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
611 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
612 cdev->private->flags.fake_irb = 0;
614 cdev->handler(cdev, cdev->private->intparm,
615 &cdev->private->irb);
616 memset(&cdev->private->irb, 0, sizeof(struct irb));
620 /* Reset oper notify indication after verify error. */
621 cdev->private->flags.donotify = 0;
622 ccw_device_done(cdev, DEV_STATE_BOXED);
625 /* Reset oper notify indication after verify error. */
626 cdev->private->flags.donotify = 0;
628 PREPARE_WORK(&cdev->private->kick_work,
629 ccw_device_nopath_notify);
630 queue_work(ccw_device_notify_work,
631 &cdev->private->kick_work);
633 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
642 ccw_device_online(struct ccw_device *cdev)
644 struct subchannel *sch;
647 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
648 (cdev->private->state != DEV_STATE_BOXED))
650 sch = to_subchannel(cdev->dev.parent);
651 if (css_init_done && !get_device(&cdev->dev))
653 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
655 /* Couldn't enable the subchannel for i/o. Sick device. */
657 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
660 /* Do we want to do path grouping? */
661 if (!cdev->private->options.pgroup) {
662 /* Start initial path verification. */
663 cdev->private->state = DEV_STATE_VERIFY;
664 cdev->private->flags.doverify = 0;
665 ccw_device_verify_start(cdev);
668 /* Do a SensePGID first. */
669 cdev->private->state = DEV_STATE_SENSE_PGID;
670 ccw_device_sense_pgid_start(cdev);
675 ccw_device_disband_done(struct ccw_device *cdev, int err)
679 ccw_device_done(cdev, DEV_STATE_OFFLINE);
682 ccw_device_done(cdev, DEV_STATE_BOXED);
685 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
694 ccw_device_offline(struct ccw_device *cdev)
696 struct subchannel *sch;
698 if (ccw_device_is_orphan(cdev)) {
699 ccw_device_done(cdev, DEV_STATE_OFFLINE);
702 sch = to_subchannel(cdev->dev.parent);
703 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
705 if (cdev->private->state != DEV_STATE_ONLINE) {
706 if (sch->schib.scsw.actl != 0)
710 if (sch->schib.scsw.actl != 0)
712 /* Are we doing path grouping? */
713 if (!cdev->private->options.pgroup) {
714 /* No, set state offline immediately. */
715 ccw_device_done(cdev, DEV_STATE_OFFLINE);
718 /* Start Set Path Group commands. */
719 cdev->private->state = DEV_STATE_DISBAND_PGID;
720 ccw_device_disband_start(cdev);
725 * Handle timeout in device online/offline process.
728 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
732 ret = ccw_device_cancel_halt_clear(cdev);
735 ccw_device_done(cdev, DEV_STATE_BOXED);
738 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
741 ccw_device_set_timeout(cdev, 3*HZ);
746 * Handle not oper event in device recognition.
749 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
751 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
755 * Handle not operational event while offline.
758 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
760 struct subchannel *sch;
762 cdev->private->state = DEV_STATE_NOT_OPER;
763 sch = to_subchannel(cdev->dev.parent);
764 if (get_device(&cdev->dev)) {
765 PREPARE_WORK(&cdev->private->kick_work,
766 ccw_device_call_sch_unregister);
767 queue_work(ccw_device_work, &cdev->private->kick_work);
769 wake_up(&cdev->private->wait_q);
773 * Handle not operational event while online.
776 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
778 struct subchannel *sch;
781 sch = to_subchannel(cdev->dev.parent);
782 if (sch->driver->notify) {
783 spin_unlock_irq(cdev->ccwlock);
784 ret = sch->driver->notify(&sch->dev,
785 sch->lpm ? CIO_GONE : CIO_NO_PATH);
786 spin_lock_irq(cdev->ccwlock);
790 ccw_device_set_timeout(cdev, 0);
791 cdev->private->flags.fake_irb = 0;
792 cdev->private->state = DEV_STATE_DISCONNECTED;
793 wake_up(&cdev->private->wait_q);
796 cdev->private->state = DEV_STATE_NOT_OPER;
797 cio_disable_subchannel(sch);
798 if (sch->schib.scsw.actl != 0) {
799 // FIXME: not-oper indication to device driver ?
800 ccw_device_call_handler(cdev);
802 if (get_device(&cdev->dev)) {
803 PREPARE_WORK(&cdev->private->kick_work,
804 ccw_device_call_sch_unregister);
805 queue_work(ccw_device_work, &cdev->private->kick_work);
807 wake_up(&cdev->private->wait_q);
811 * Handle path verification event.
814 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
816 struct subchannel *sch;
818 if (cdev->private->state == DEV_STATE_W4SENSE) {
819 cdev->private->flags.doverify = 1;
822 sch = to_subchannel(cdev->dev.parent);
824 * Since we might not just be coming from an interrupt from the
825 * subchannel we have to update the schib.
827 stsch(sch->schid, &sch->schib);
829 if (sch->schib.scsw.actl != 0 ||
830 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
831 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
833 * No final status yet or final status not yet delivered
834 * to the device driver. Can't do path verfication now,
835 * delay until final status was delivered.
837 cdev->private->flags.doverify = 1;
840 /* Device is idle, we can do the path verification. */
841 cdev->private->state = DEV_STATE_VERIFY;
842 cdev->private->flags.doverify = 0;
843 ccw_device_verify_start(cdev);
847 * Got an interrupt for a normal io (state online).
850 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
854 irb = (struct irb *) __LC_IRB;
855 /* Check for unsolicited interrupt. */
856 if ((irb->scsw.stctl ==
857 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
858 && (!irb->scsw.cc)) {
859 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
860 !irb->esw.esw0.erw.cons) {
861 /* Unit check but no sense data. Need basic sense. */
862 if (ccw_device_do_sense(cdev, irb) != 0)
863 goto call_handler_unsol;
864 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
865 cdev->private->state = DEV_STATE_W4SENSE;
866 cdev->private->intparm = 0;
871 cdev->handler (cdev, 0, irb);
872 if (cdev->private->flags.doverify)
873 ccw_device_online_verify(cdev, 0);
876 /* Accumulate status and find out if a basic sense is needed. */
877 ccw_device_accumulate_irb(cdev, irb);
878 if (cdev->private->flags.dosense) {
879 if (ccw_device_do_sense(cdev, irb) == 0) {
880 cdev->private->state = DEV_STATE_W4SENSE;
884 /* Call the handler. */
885 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
886 /* Start delayed path verification. */
887 ccw_device_online_verify(cdev, 0);
891 * Got an timeout in online state.
894 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
898 ccw_device_set_timeout(cdev, 0);
899 ret = ccw_device_cancel_halt_clear(cdev);
901 ccw_device_set_timeout(cdev, 3*HZ);
902 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
905 if (ret == -ENODEV) {
906 struct subchannel *sch;
908 sch = to_subchannel(cdev->dev.parent);
910 PREPARE_WORK(&cdev->private->kick_work,
911 ccw_device_nopath_notify);
912 queue_work(ccw_device_notify_work,
913 &cdev->private->kick_work);
915 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
916 } else if (cdev->handler)
917 cdev->handler(cdev, cdev->private->intparm,
918 ERR_PTR(-ETIMEDOUT));
922 * Got an interrupt for a basic sense.
925 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
929 irb = (struct irb *) __LC_IRB;
930 /* Check for unsolicited interrupt. */
931 if (irb->scsw.stctl ==
932 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
933 if (irb->scsw.cc == 1)
934 /* Basic sense hasn't started. Try again. */
935 ccw_device_do_sense(cdev, irb);
937 printk(KERN_INFO "Huh? %s(%s): unsolicited "
939 __FUNCTION__, cdev->dev.bus_id);
941 cdev->handler (cdev, 0, irb);
946 * Check if a halt or clear has been issued in the meanwhile. If yes,
947 * only deliver the halt/clear interrupt to the device driver as if it
948 * had killed the original request.
950 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
951 /* Retry Basic Sense if requested. */
952 if (cdev->private->flags.intretry) {
953 cdev->private->flags.intretry = 0;
954 ccw_device_do_sense(cdev, irb);
957 cdev->private->flags.dosense = 0;
958 memset(&cdev->private->irb, 0, sizeof(struct irb));
959 ccw_device_accumulate_irb(cdev, irb);
962 /* Add basic sense info to irb. */
963 ccw_device_accumulate_basic_sense(cdev, irb);
964 if (cdev->private->flags.dosense) {
965 /* Another basic sense is needed. */
966 ccw_device_do_sense(cdev, irb);
970 cdev->private->state = DEV_STATE_ONLINE;
971 /* Call the handler. */
972 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
973 /* Start delayed path verification. */
974 ccw_device_online_verify(cdev, 0);
978 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
982 irb = (struct irb *) __LC_IRB;
983 /* Accumulate status. We don't do basic sense. */
984 ccw_device_accumulate_irb(cdev, irb);
985 /* Remember to clear irb to avoid residuals. */
986 memset(&cdev->private->irb, 0, sizeof(struct irb));
987 /* Try to start delayed device verification. */
988 ccw_device_online_verify(cdev, 0);
989 /* Note: Don't call handler for cio initiated clear! */
993 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
995 struct subchannel *sch;
997 sch = to_subchannel(cdev->dev.parent);
998 ccw_device_set_timeout(cdev, 0);
999 /* Start delayed path verification. */
1000 ccw_device_online_verify(cdev, 0);
1001 /* OK, i/o is dead now. Call interrupt handler. */
1003 cdev->handler(cdev, cdev->private->intparm,
1008 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1012 ret = ccw_device_cancel_halt_clear(cdev);
1013 if (ret == -EBUSY) {
1014 ccw_device_set_timeout(cdev, 3*HZ);
1017 /* Start delayed path verification. */
1018 ccw_device_online_verify(cdev, 0);
1020 cdev->handler(cdev, cdev->private->intparm,
1024 void device_kill_io(struct subchannel *sch)
1027 struct ccw_device *cdev;
1029 cdev = sch->dev.driver_data;
1030 ret = ccw_device_cancel_halt_clear(cdev);
1031 if (ret == -EBUSY) {
1032 ccw_device_set_timeout(cdev, 3*HZ);
1033 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1036 /* Start delayed path verification. */
1037 ccw_device_online_verify(cdev, 0);
1039 cdev->handler(cdev, cdev->private->intparm,
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))
1107 if (!sch->schib.pmcw.dnv)
1110 * The pim, pam, pom values may not be accurate, but they are the best
1111 * we have before performing device selection :/
1113 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1114 /* Re-set some bits in the pmcw that were lost. */
1115 sch->schib.pmcw.isc = 3;
1116 sch->schib.pmcw.csense = 1;
1117 sch->schib.pmcw.ena = 0;
1118 if ((sch->lpm & (sch->lpm - 1)) != 0)
1119 sch->schib.pmcw.mp = 1;
1120 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1121 /* We should also udate ssd info, but this has to wait. */
1122 /* Check if this is another device which appeared on the same sch. */
1123 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1124 PREPARE_WORK(&cdev->private->kick_work,
1125 ccw_device_move_to_orphanage);
1126 queue_work(ccw_device_work, &cdev->private->kick_work);
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_delay_verify,
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_QUIESCE] = {
1283 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1284 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1285 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1286 [DEV_EVENT_VERIFY] = ccw_device_nop,
1288 /* special states for devices gone not operational */
1289 [DEV_STATE_DISCONNECTED] = {
1290 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1291 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1292 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1293 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1295 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1296 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1297 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1298 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1299 [DEV_EVENT_VERIFY] = ccw_device_nop,
1301 [DEV_STATE_CMFCHANGE] = {
1302 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1303 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1304 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1305 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1307 [DEV_STATE_CMFUPDATE] = {
1308 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1309 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1310 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1311 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1316 * io_subchannel_irq is called for "real" interrupts or for status
1317 * pending conditions on msch.
1320 io_subchannel_irq (struct device *pdev)
1322 struct ccw_device *cdev;
1324 cdev = to_subchannel(pdev)->dev.driver_data;
1326 CIO_TRACE_EVENT (3, "IRQ");
1327 CIO_TRACE_EVENT (3, pdev->bus_id);
1329 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1332 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);