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>
18 #include <asm/chpid.h>
21 #include "cio_debug.h"
28 static int timeout_log_enabled;
31 device_is_online(struct subchannel *sch)
33 struct ccw_device *cdev;
35 cdev = sch_get_cdev(sch);
38 return (cdev->private->state == DEV_STATE_ONLINE);
42 device_is_disconnected(struct subchannel *sch)
44 struct ccw_device *cdev;
46 cdev = sch_get_cdev(sch);
49 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
50 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
54 device_set_disconnected(struct subchannel *sch)
56 struct ccw_device *cdev;
58 cdev = sch_get_cdev(sch);
61 ccw_device_set_timeout(cdev, 0);
62 cdev->private->flags.fake_irb = 0;
63 cdev->private->state = DEV_STATE_DISCONNECTED;
66 void device_set_intretry(struct subchannel *sch)
68 struct ccw_device *cdev;
70 cdev = sch_get_cdev(sch);
73 cdev->private->flags.intretry = 1;
76 int device_trigger_verify(struct subchannel *sch)
78 struct ccw_device *cdev;
80 cdev = sch_get_cdev(sch);
81 if (!cdev || !cdev->online)
83 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
87 static int __init ccw_timeout_log_setup(char *unused)
89 timeout_log_enabled = 1;
93 __setup("ccw_timeout_log", ccw_timeout_log_setup);
95 static void ccw_timeout_log(struct ccw_device *cdev)
98 struct subchannel *sch;
99 struct io_subchannel_private *private;
102 sch = to_subchannel(cdev->dev.parent);
103 private = to_io_private(sch);
104 cc = stsch(sch->schid, &schib);
106 printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
107 "device information:\n", get_clock());
108 printk(KERN_WARNING "cio: orb:\n");
109 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
110 &private->orb, sizeof(private->orb), 0);
111 printk(KERN_WARNING "cio: ccw device bus id: %s\n", cdev->dev.bus_id);
112 printk(KERN_WARNING "cio: subchannel bus id: %s\n", sch->dev.bus_id);
113 printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
114 "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
116 if ((void *)(addr_t)private->orb.cpa == &private->sense_ccw ||
117 (void *)(addr_t)private->orb.cpa == cdev->private->iccws)
118 printk(KERN_WARNING "cio: last channel program (intern):\n");
120 printk(KERN_WARNING "cio: last channel program:\n");
122 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
123 (void *)(addr_t)private->orb.cpa,
124 sizeof(struct ccw1), 0);
125 printk(KERN_WARNING "cio: ccw device state: %d\n",
126 cdev->private->state);
127 printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
128 printk(KERN_WARNING "cio: schib:\n");
129 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
130 &schib, sizeof(schib), 0);
131 printk(KERN_WARNING "cio: ccw device flags:\n");
132 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
133 &cdev->private->flags, sizeof(cdev->private->flags), 0);
137 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
140 ccw_device_timeout(unsigned long data)
142 struct ccw_device *cdev;
144 cdev = (struct ccw_device *) data;
145 spin_lock_irq(cdev->ccwlock);
146 if (timeout_log_enabled)
147 ccw_timeout_log(cdev);
148 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
149 spin_unlock_irq(cdev->ccwlock);
156 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
159 del_timer(&cdev->private->timer);
162 if (timer_pending(&cdev->private->timer)) {
163 if (mod_timer(&cdev->private->timer, jiffies + expires))
166 cdev->private->timer.function = ccw_device_timeout;
167 cdev->private->timer.data = (unsigned long) cdev;
168 cdev->private->timer.expires = jiffies + expires;
169 add_timer(&cdev->private->timer);
172 /* Kill any pending timers after machine check. */
174 device_kill_pending_timer(struct subchannel *sch)
176 struct ccw_device *cdev;
178 cdev = sch_get_cdev(sch);
181 ccw_device_set_timeout(cdev, 0);
185 * Cancel running i/o. This is called repeatedly since halt/clear are
186 * asynchronous operations. We do one try with cio_cancel, two tries
187 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
188 * Returns 0 if device now idle, -ENODEV for device not operational and
189 * -EBUSY if an interrupt is expected (either from halt/clear or from a
193 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
195 struct subchannel *sch;
198 sch = to_subchannel(cdev->dev.parent);
199 ret = stsch(sch->schid, &sch->schib);
200 if (ret || !sch->schib.pmcw.dnv)
202 if (!sch->schib.pmcw.ena)
203 /* Not operational -> done. */
205 /* Stage 1: cancel io. */
206 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
207 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
208 ret = cio_cancel(sch);
211 /* cancel io unsuccessful. From now on it is asynchronous. */
212 cdev->private->iretry = 3; /* 3 halt retries. */
214 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
215 /* Stage 2: halt io. */
216 if (cdev->private->iretry) {
217 cdev->private->iretry--;
220 return (ret == 0) ? -EBUSY : ret;
222 /* halt io unsuccessful. */
223 cdev->private->iretry = 255; /* 255 clear retries. */
225 /* Stage 3: clear io. */
226 if (cdev->private->iretry) {
227 cdev->private->iretry--;
228 ret = cio_clear (sch);
229 return (ret == 0) ? -EBUSY : ret;
231 panic("Can't stop i/o on subchannel.\n");
235 ccw_device_handle_oper(struct ccw_device *cdev)
237 struct subchannel *sch;
239 sch = to_subchannel(cdev->dev.parent);
240 cdev->private->flags.recog_done = 1;
242 * Check if cu type and device type still match. If
243 * not, it is certainly another device and we have to
244 * de- and re-register.
246 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
247 cdev->id.cu_model != cdev->private->senseid.cu_model ||
248 cdev->id.dev_type != cdev->private->senseid.dev_type ||
249 cdev->id.dev_model != cdev->private->senseid.dev_model) {
250 PREPARE_WORK(&cdev->private->kick_work,
251 ccw_device_do_unreg_rereg);
252 queue_work(ccw_device_work, &cdev->private->kick_work);
255 cdev->private->flags.donotify = 1;
260 * The machine won't give us any notification by machine check if a chpid has
261 * been varied online on the SE so we have to find out by magic (i. e. driving
262 * the channel subsystem to device selection and updating our path masks).
265 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
271 for (i = 0; i<8; i++) {
273 if (!(sch->lpm & mask))
277 chpid.id = sch->schib.pmcw.chpid[i];
278 if (!chp_is_registered(chpid))
279 css_schedule_eval_all();
284 * Stop device recognition.
287 ccw_device_recog_done(struct ccw_device *cdev, int state)
289 struct subchannel *sch;
290 int notify, old_lpm, same_dev;
292 sch = to_subchannel(cdev->dev.parent);
294 ccw_device_set_timeout(cdev, 0);
295 cio_disable_subchannel(sch);
297 * Now that we tried recognition, we have performed device selection
298 * through ssch() and the path information is up to date.
301 stsch(sch->schid, &sch->schib);
302 sch->lpm = sch->schib.pmcw.pam & sch->opm;
303 /* Check since device may again have become not operational. */
304 if (!sch->schib.pmcw.dnv)
305 state = DEV_STATE_NOT_OPER;
306 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
307 /* Force reprobe on all chpids. */
309 if (sch->lpm != old_lpm)
310 __recover_lost_chpids(sch, old_lpm);
311 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
312 if (state == DEV_STATE_NOT_OPER) {
313 cdev->private->flags.recog_done = 1;
314 cdev->private->state = DEV_STATE_DISCONNECTED;
317 /* Boxed devices don't need extra treatment. */
320 same_dev = 0; /* Keep the compiler quiet... */
322 case DEV_STATE_NOT_OPER:
323 CIO_DEBUG(KERN_WARNING, 2,
324 "SenseID : unknown device %04x on subchannel "
325 "0.%x.%04x\n", cdev->private->dev_id.devno,
326 sch->schid.ssid, sch->schid.sch_no);
328 case DEV_STATE_OFFLINE:
329 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
330 same_dev = ccw_device_handle_oper(cdev);
333 /* fill out sense information */
334 memset(&cdev->id, 0, sizeof(cdev->id));
335 cdev->id.cu_type = cdev->private->senseid.cu_type;
336 cdev->id.cu_model = cdev->private->senseid.cu_model;
337 cdev->id.dev_type = cdev->private->senseid.dev_type;
338 cdev->id.dev_model = cdev->private->senseid.dev_model;
340 cdev->private->state = DEV_STATE_OFFLINE;
342 /* Get device online again. */
343 ccw_device_online(cdev);
344 wake_up(&cdev->private->wait_q);
348 /* Issue device info message. */
349 CIO_DEBUG(KERN_INFO, 2,
350 "SenseID : device 0.%x.%04x reports: "
351 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
353 cdev->private->dev_id.ssid,
354 cdev->private->dev_id.devno,
355 cdev->id.cu_type, cdev->id.cu_model,
356 cdev->id.dev_type, cdev->id.dev_model);
358 case DEV_STATE_BOXED:
359 CIO_DEBUG(KERN_WARNING, 2,
360 "SenseID : boxed device %04x on subchannel "
361 "0.%x.%04x\n", cdev->private->dev_id.devno,
362 sch->schid.ssid, sch->schid.sch_no);
365 cdev->private->state = state;
366 io_subchannel_recog_done(cdev);
367 if (state != DEV_STATE_NOT_OPER)
368 wake_up(&cdev->private->wait_q);
372 * Function called from device_id.c after sense id has completed.
375 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
379 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
381 case -ETIME: /* Sense id stopped by timeout. */
382 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
385 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
391 ccw_device_oper_notify(struct work_struct *work)
393 struct ccw_device_private *priv;
394 struct ccw_device *cdev;
395 struct subchannel *sch;
399 priv = container_of(work, struct ccw_device_private, kick_work);
401 spin_lock_irqsave(cdev->ccwlock, flags);
402 sch = to_subchannel(cdev->dev.parent);
403 if (sch->driver && sch->driver->notify) {
404 spin_unlock_irqrestore(cdev->ccwlock, flags);
405 ret = sch->driver->notify(sch, CIO_OPER);
406 spin_lock_irqsave(cdev->ccwlock, flags);
410 /* Reenable channel measurements, if needed. */
411 spin_unlock_irqrestore(cdev->ccwlock, flags);
413 spin_lock_irqsave(cdev->ccwlock, flags);
414 wake_up(&cdev->private->wait_q);
416 spin_unlock_irqrestore(cdev->ccwlock, flags);
418 /* Driver doesn't want device back. */
419 ccw_device_do_unreg_rereg(work);
423 * Finished with online/offline processing.
426 ccw_device_done(struct ccw_device *cdev, int state)
428 struct subchannel *sch;
430 sch = to_subchannel(cdev->dev.parent);
432 ccw_device_set_timeout(cdev, 0);
434 if (state != DEV_STATE_ONLINE)
435 cio_disable_subchannel(sch);
437 /* Reset device status. */
438 memset(&cdev->private->irb, 0, sizeof(struct irb));
440 cdev->private->state = state;
443 if (state == DEV_STATE_BOXED)
444 CIO_DEBUG(KERN_WARNING, 2,
445 "Boxed device %04x on subchannel %04x\n",
446 cdev->private->dev_id.devno, sch->schid.sch_no);
448 if (cdev->private->flags.donotify) {
449 cdev->private->flags.donotify = 0;
450 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
451 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
453 wake_up(&cdev->private->wait_q);
455 if (css_init_done && state != DEV_STATE_ONLINE)
456 put_device (&cdev->dev);
459 static int cmp_pgid(struct pgid *p1, struct pgid *p2)
467 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
470 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
476 for (i = 0; i < 8; i++) {
477 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
480 if (cdev->private->pgid[last].inf.ps.state1 ==
482 /* First non-zero PGID */
486 if (cmp_pgid(&cdev->private->pgid[i],
487 &cdev->private->pgid[last]) == 0)
488 /* Non-conflicting PGIDs */
491 /* PGID mismatch, can't pathgroup. */
492 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
493 "0.%x.%04x, can't pathgroup\n",
494 cdev->private->dev_id.ssid,
495 cdev->private->dev_id.devno);
496 cdev->private->options.pgroup = 0;
499 if (cdev->private->pgid[last].inf.ps.state1 ==
501 /* No previous pgid found */
502 memcpy(&cdev->private->pgid[0],
503 &channel_subsystems[0]->global_pgid,
504 sizeof(struct pgid));
506 /* Use existing pgid */
507 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
508 sizeof(struct pgid));
512 * Function called from device_pgid.c after sense path ground has completed.
515 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
517 struct subchannel *sch;
519 sch = to_subchannel(cdev->dev.parent);
521 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
522 cdev->private->options.pgroup = 0;
524 case 0: /* success */
525 case -EACCES: /* partial success, some paths not operational */
526 /* Check if all pgids are equal or 0. */
527 __ccw_device_get_common_pgid(cdev);
529 case -ETIME: /* Sense path group id stopped by timeout. */
530 case -EUSERS: /* device is reserved for someone else. */
531 ccw_device_done(cdev, DEV_STATE_BOXED);
534 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
537 /* Start Path Group verification. */
538 cdev->private->state = DEV_STATE_VERIFY;
539 cdev->private->flags.doverify = 0;
540 ccw_device_verify_start(cdev);
544 * Start device recognition.
547 ccw_device_recognition(struct ccw_device *cdev)
549 struct subchannel *sch;
552 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
553 (cdev->private->state != DEV_STATE_BOXED))
555 sch = to_subchannel(cdev->dev.parent);
556 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc,
559 /* Couldn't enable the subchannel for i/o. Sick device. */
562 /* After 60s the device recognition is considered to have failed. */
563 ccw_device_set_timeout(cdev, 60*HZ);
566 * We used to start here with a sense pgid to find out whether a device
567 * is locked by someone else. Unfortunately, the sense pgid command
568 * code has other meanings on devices predating the path grouping
569 * algorithm, so we start with sense id and box the device after an
570 * timeout (or if sense pgid during path verification detects the device
571 * is locked, as may happen on newer devices).
573 cdev->private->flags.recog_done = 0;
574 cdev->private->state = DEV_STATE_SENSE_ID;
575 ccw_device_sense_id_start(cdev);
580 * Handle timeout in device recognition.
583 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
587 ret = ccw_device_cancel_halt_clear(cdev);
590 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
593 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
596 ccw_device_set_timeout(cdev, 3*HZ);
602 ccw_device_verify_done(struct ccw_device *cdev, int err)
604 struct subchannel *sch;
606 sch = to_subchannel(cdev->dev.parent);
607 /* Update schib - pom may have changed. */
608 stsch(sch->schid, &sch->schib);
609 /* Update lpm with verified path mask. */
611 /* Repeat path verification? */
612 if (cdev->private->flags.doverify) {
613 cdev->private->flags.doverify = 0;
614 ccw_device_verify_start(cdev);
618 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
619 cdev->private->options.pgroup = 0;
621 ccw_device_done(cdev, DEV_STATE_ONLINE);
622 /* Deliver fake irb to device driver, if needed. */
623 if (cdev->private->flags.fake_irb) {
624 memset(&cdev->private->irb, 0, sizeof(struct irb));
625 cdev->private->irb.scsw.cc = 1;
626 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
627 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
628 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
629 cdev->private->flags.fake_irb = 0;
631 cdev->handler(cdev, cdev->private->intparm,
632 &cdev->private->irb);
633 memset(&cdev->private->irb, 0, sizeof(struct irb));
637 /* Reset oper notify indication after verify error. */
638 cdev->private->flags.donotify = 0;
639 ccw_device_done(cdev, DEV_STATE_BOXED);
642 /* Reset oper notify indication after verify error. */
643 cdev->private->flags.donotify = 0;
645 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
647 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
656 ccw_device_online(struct ccw_device *cdev)
658 struct subchannel *sch;
661 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
662 (cdev->private->state != DEV_STATE_BOXED))
664 sch = to_subchannel(cdev->dev.parent);
665 if (css_init_done && !get_device(&cdev->dev))
667 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc,
670 /* Couldn't enable the subchannel for i/o. Sick device. */
672 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
675 /* Do we want to do path grouping? */
676 if (!cdev->private->options.pgroup) {
677 /* Start initial path verification. */
678 cdev->private->state = DEV_STATE_VERIFY;
679 cdev->private->flags.doverify = 0;
680 ccw_device_verify_start(cdev);
683 /* Do a SensePGID first. */
684 cdev->private->state = DEV_STATE_SENSE_PGID;
685 ccw_device_sense_pgid_start(cdev);
690 ccw_device_disband_done(struct ccw_device *cdev, int err)
694 ccw_device_done(cdev, DEV_STATE_OFFLINE);
697 ccw_device_done(cdev, DEV_STATE_BOXED);
700 cdev->private->flags.donotify = 0;
701 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
702 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
711 ccw_device_offline(struct ccw_device *cdev)
713 struct subchannel *sch;
715 if (ccw_device_is_orphan(cdev)) {
716 ccw_device_done(cdev, DEV_STATE_OFFLINE);
719 sch = to_subchannel(cdev->dev.parent);
720 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
722 if (cdev->private->state != DEV_STATE_ONLINE) {
723 if (sch->schib.scsw.actl != 0)
727 if (sch->schib.scsw.actl != 0)
729 /* Are we doing path grouping? */
730 if (!cdev->private->options.pgroup) {
731 /* No, set state offline immediately. */
732 ccw_device_done(cdev, DEV_STATE_OFFLINE);
735 /* Start Set Path Group commands. */
736 cdev->private->state = DEV_STATE_DISBAND_PGID;
737 ccw_device_disband_start(cdev);
742 * Handle timeout in device online/offline process.
745 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
749 ret = ccw_device_cancel_halt_clear(cdev);
752 ccw_device_done(cdev, DEV_STATE_BOXED);
755 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
758 ccw_device_set_timeout(cdev, 3*HZ);
763 * Handle not oper event in device recognition.
766 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
768 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
772 * Handle not operational event in non-special state.
774 static void ccw_device_generic_notoper(struct ccw_device *cdev,
775 enum dev_event dev_event)
777 struct subchannel *sch;
779 cdev->private->state = DEV_STATE_NOT_OPER;
780 sch = to_subchannel(cdev->dev.parent);
781 css_schedule_eval(sch->schid);
785 * Handle path verification event.
788 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
790 struct subchannel *sch;
792 if (cdev->private->state == DEV_STATE_W4SENSE) {
793 cdev->private->flags.doverify = 1;
796 sch = to_subchannel(cdev->dev.parent);
798 * Since we might not just be coming from an interrupt from the
799 * subchannel we have to update the schib.
801 stsch(sch->schid, &sch->schib);
803 if (sch->schib.scsw.actl != 0 ||
804 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
805 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
807 * No final status yet or final status not yet delivered
808 * to the device driver. Can't do path verfication now,
809 * delay until final status was delivered.
811 cdev->private->flags.doverify = 1;
814 /* Device is idle, we can do the path verification. */
815 cdev->private->state = DEV_STATE_VERIFY;
816 cdev->private->flags.doverify = 0;
817 ccw_device_verify_start(cdev);
821 * Got an interrupt for a normal io (state online).
824 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
828 irb = (struct irb *) __LC_IRB;
829 /* Check for unsolicited interrupt. */
830 if ((irb->scsw.stctl ==
831 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
832 && (!irb->scsw.cc)) {
833 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
834 !irb->esw.esw0.erw.cons) {
835 /* Unit check but no sense data. Need basic sense. */
836 if (ccw_device_do_sense(cdev, irb) != 0)
837 goto call_handler_unsol;
838 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
839 cdev->private->state = DEV_STATE_W4SENSE;
840 cdev->private->intparm = 0;
845 cdev->handler (cdev, 0, irb);
846 if (cdev->private->flags.doverify)
847 ccw_device_online_verify(cdev, 0);
850 /* Accumulate status and find out if a basic sense is needed. */
851 ccw_device_accumulate_irb(cdev, irb);
852 if (cdev->private->flags.dosense) {
853 if (ccw_device_do_sense(cdev, irb) == 0) {
854 cdev->private->state = DEV_STATE_W4SENSE;
858 /* Call the handler. */
859 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
860 /* Start delayed path verification. */
861 ccw_device_online_verify(cdev, 0);
865 * Got an timeout in online state.
868 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
872 ccw_device_set_timeout(cdev, 0);
873 ret = ccw_device_cancel_halt_clear(cdev);
875 ccw_device_set_timeout(cdev, 3*HZ);
876 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
880 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
881 else if (cdev->handler)
882 cdev->handler(cdev, cdev->private->intparm,
883 ERR_PTR(-ETIMEDOUT));
887 * Got an interrupt for a basic sense.
890 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
894 irb = (struct irb *) __LC_IRB;
895 /* Check for unsolicited interrupt. */
896 if (irb->scsw.stctl ==
897 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
898 if (irb->scsw.cc == 1)
899 /* Basic sense hasn't started. Try again. */
900 ccw_device_do_sense(cdev, irb);
902 CIO_MSG_EVENT(2, "Huh? 0.%x.%04x: unsolicited "
903 "interrupt during w4sense...\n",
904 cdev->private->dev_id.ssid,
905 cdev->private->dev_id.devno);
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 /* Start delayed path verification. */
966 ccw_device_online_verify(cdev, 0);
967 /* OK, i/o is dead now. Call interrupt handler. */
969 cdev->handler(cdev, cdev->private->intparm,
974 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
978 ret = ccw_device_cancel_halt_clear(cdev);
980 ccw_device_set_timeout(cdev, 3*HZ);
983 /* Start delayed path verification. */
984 ccw_device_online_verify(cdev, 0);
986 cdev->handler(cdev, cdev->private->intparm,
990 void device_kill_io(struct subchannel *sch)
993 struct ccw_device *cdev;
995 cdev = sch_get_cdev(sch);
996 ret = ccw_device_cancel_halt_clear(cdev);
998 ccw_device_set_timeout(cdev, 3*HZ);
999 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1002 /* Start delayed path verification. */
1003 ccw_device_online_verify(cdev, 0);
1005 cdev->handler(cdev, cdev->private->intparm,
1010 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
1012 /* Start verification after current task finished. */
1013 cdev->private->flags.doverify = 1;
1017 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1021 switch (dev_event) {
1022 case DEV_EVENT_INTERRUPT:
1023 irb = (struct irb *) __LC_IRB;
1024 /* Check for unsolicited interrupt. */
1025 if ((irb->scsw.stctl ==
1026 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1028 /* FIXME: we should restart stlck here, but this
1029 * is extremely unlikely ... */
1032 ccw_device_accumulate_irb(cdev, irb);
1033 /* We don't care about basic sense etc. */
1035 default: /* timeout */
1039 wake_up(&cdev->private->wait_q);
1043 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1045 struct subchannel *sch;
1047 sch = to_subchannel(cdev->dev.parent);
1048 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc,
1049 (u32)(addr_t)sch) != 0)
1050 /* Couldn't enable the subchannel for i/o. Sick device. */
1053 /* After 60s the device recognition is considered to have failed. */
1054 ccw_device_set_timeout(cdev, 60*HZ);
1056 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1057 ccw_device_sense_id_start(cdev);
1061 device_trigger_reprobe(struct subchannel *sch)
1063 struct ccw_device *cdev;
1065 cdev = sch_get_cdev(sch);
1068 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1071 /* Update some values. */
1072 if (stsch(sch->schid, &sch->schib))
1074 if (!sch->schib.pmcw.dnv)
1077 * The pim, pam, pom values may not be accurate, but they are the best
1078 * we have before performing device selection :/
1080 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1081 /* Re-set some bits in the pmcw that were lost. */
1082 sch->schib.pmcw.isc = 3;
1083 sch->schib.pmcw.csense = 1;
1084 sch->schib.pmcw.ena = 0;
1085 if ((sch->lpm & (sch->lpm - 1)) != 0)
1086 sch->schib.pmcw.mp = 1;
1087 sch->schib.pmcw.intparm = (u32)(addr_t)sch;
1088 /* We should also udate ssd info, but this has to wait. */
1089 /* Check if this is another device which appeared on the same sch. */
1090 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1091 PREPARE_WORK(&cdev->private->kick_work,
1092 ccw_device_move_to_orphanage);
1093 queue_work(slow_path_wq, &cdev->private->kick_work);
1095 ccw_device_start_id(cdev, 0);
1099 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1101 struct subchannel *sch;
1103 sch = to_subchannel(cdev->dev.parent);
1105 * An interrupt in state offline means a previous disable was not
1106 * successful. Try again.
1108 cio_disable_subchannel(sch);
1112 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1114 retry_set_schib(cdev);
1115 cdev->private->state = DEV_STATE_ONLINE;
1116 dev_fsm_event(cdev, dev_event);
1119 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1120 enum dev_event dev_event)
1122 cmf_retry_copy_block(cdev);
1123 cdev->private->state = DEV_STATE_ONLINE;
1124 dev_fsm_event(cdev, dev_event);
1128 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1130 ccw_device_set_timeout(cdev, 0);
1131 if (dev_event == DEV_EVENT_NOTOPER)
1132 cdev->private->state = DEV_STATE_NOT_OPER;
1134 cdev->private->state = DEV_STATE_OFFLINE;
1135 wake_up(&cdev->private->wait_q);
1139 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1143 ret = ccw_device_cancel_halt_clear(cdev);
1146 cdev->private->state = DEV_STATE_OFFLINE;
1147 wake_up(&cdev->private->wait_q);
1150 cdev->private->state = DEV_STATE_NOT_OPER;
1151 wake_up(&cdev->private->wait_q);
1154 ccw_device_set_timeout(cdev, HZ/10);
1159 * No operation action. This is used e.g. to ignore a timeout event in
1163 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1168 * Bug operation action.
1171 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1173 CIO_MSG_EVENT(0, "dev_jumptable[%i][%i] == NULL\n",
1174 cdev->private->state, dev_event);
1179 * device statemachine
1181 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1182 [DEV_STATE_NOT_OPER] = {
1183 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1184 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1185 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1186 [DEV_EVENT_VERIFY] = ccw_device_nop,
1188 [DEV_STATE_SENSE_PGID] = {
1189 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1190 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1191 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1192 [DEV_EVENT_VERIFY] = ccw_device_nop,
1194 [DEV_STATE_SENSE_ID] = {
1195 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1196 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1197 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1198 [DEV_EVENT_VERIFY] = ccw_device_nop,
1200 [DEV_STATE_OFFLINE] = {
1201 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1202 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1203 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1204 [DEV_EVENT_VERIFY] = ccw_device_nop,
1206 [DEV_STATE_VERIFY] = {
1207 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1208 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1209 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1210 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1212 [DEV_STATE_ONLINE] = {
1213 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1214 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1215 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1216 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1218 [DEV_STATE_W4SENSE] = {
1219 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1220 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1221 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1222 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1224 [DEV_STATE_DISBAND_PGID] = {
1225 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1226 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1227 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1228 [DEV_EVENT_VERIFY] = ccw_device_nop,
1230 [DEV_STATE_BOXED] = {
1231 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1232 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1233 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1234 [DEV_EVENT_VERIFY] = ccw_device_nop,
1236 /* states to wait for i/o completion before doing something */
1237 [DEV_STATE_CLEAR_VERIFY] = {
1238 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1239 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1240 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1241 [DEV_EVENT_VERIFY] = ccw_device_nop,
1243 [DEV_STATE_TIMEOUT_KILL] = {
1244 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1245 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1246 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1247 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1249 [DEV_STATE_QUIESCE] = {
1250 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1251 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1252 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1253 [DEV_EVENT_VERIFY] = ccw_device_nop,
1255 /* special states for devices gone not operational */
1256 [DEV_STATE_DISCONNECTED] = {
1257 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1258 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1259 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1260 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1262 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1263 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1264 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1265 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1266 [DEV_EVENT_VERIFY] = ccw_device_nop,
1268 [DEV_STATE_CMFCHANGE] = {
1269 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1270 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1271 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1272 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1274 [DEV_STATE_CMFUPDATE] = {
1275 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1276 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1277 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1278 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1282 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);