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"
29 device_is_online(struct subchannel *sch)
31 struct ccw_device *cdev;
33 if (!sch->dev.driver_data)
35 cdev = sch->dev.driver_data;
36 return (cdev->private->state == DEV_STATE_ONLINE);
40 device_is_disconnected(struct subchannel *sch)
42 struct ccw_device *cdev;
44 if (!sch->dev.driver_data)
46 cdev = sch->dev.driver_data;
47 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
48 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
52 device_set_disconnected(struct subchannel *sch)
54 struct ccw_device *cdev;
56 if (!sch->dev.driver_data)
58 cdev = sch->dev.driver_data;
59 ccw_device_set_timeout(cdev, 0);
60 cdev->private->flags.fake_irb = 0;
61 cdev->private->state = DEV_STATE_DISCONNECTED;
64 void device_set_intretry(struct subchannel *sch)
66 struct ccw_device *cdev;
68 cdev = sch->dev.driver_data;
71 cdev->private->flags.intretry = 1;
74 int device_trigger_verify(struct subchannel *sch)
76 struct ccw_device *cdev;
78 cdev = sch->dev.driver_data;
79 if (!cdev || !cdev->online)
81 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
86 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
89 ccw_device_timeout(unsigned long data)
91 struct ccw_device *cdev;
93 cdev = (struct ccw_device *) data;
94 spin_lock_irq(cdev->ccwlock);
95 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
96 spin_unlock_irq(cdev->ccwlock);
103 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
106 del_timer(&cdev->private->timer);
109 if (timer_pending(&cdev->private->timer)) {
110 if (mod_timer(&cdev->private->timer, jiffies + expires))
113 cdev->private->timer.function = ccw_device_timeout;
114 cdev->private->timer.data = (unsigned long) cdev;
115 cdev->private->timer.expires = jiffies + expires;
116 add_timer(&cdev->private->timer);
119 /* Kill any pending timers after machine check. */
121 device_kill_pending_timer(struct subchannel *sch)
123 struct ccw_device *cdev;
125 if (!sch->dev.driver_data)
127 cdev = sch->dev.driver_data;
128 ccw_device_set_timeout(cdev, 0);
132 * Cancel running i/o. This is called repeatedly since halt/clear are
133 * asynchronous operations. We do one try with cio_cancel, two tries
134 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
135 * Returns 0 if device now idle, -ENODEV for device not operational and
136 * -EBUSY if an interrupt is expected (either from halt/clear or from a
140 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
142 struct subchannel *sch;
145 sch = to_subchannel(cdev->dev.parent);
146 ret = stsch(sch->schid, &sch->schib);
147 if (ret || !sch->schib.pmcw.dnv)
149 if (!sch->schib.pmcw.ena)
150 /* Not operational -> done. */
152 /* Stage 1: cancel io. */
153 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
154 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
155 ret = cio_cancel(sch);
158 /* cancel io unsuccessful. From now on it is asynchronous. */
159 cdev->private->iretry = 3; /* 3 halt retries. */
161 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
162 /* Stage 2: halt io. */
163 if (cdev->private->iretry) {
164 cdev->private->iretry--;
167 return (ret == 0) ? -EBUSY : ret;
169 /* halt io unsuccessful. */
170 cdev->private->iretry = 255; /* 255 clear retries. */
172 /* Stage 3: clear io. */
173 if (cdev->private->iretry) {
174 cdev->private->iretry--;
175 ret = cio_clear (sch);
176 return (ret == 0) ? -EBUSY : ret;
178 panic("Can't stop i/o on subchannel.\n");
182 ccw_device_handle_oper(struct ccw_device *cdev)
184 struct subchannel *sch;
186 sch = to_subchannel(cdev->dev.parent);
187 cdev->private->flags.recog_done = 1;
189 * Check if cu type and device type still match. If
190 * not, it is certainly another device and we have to
191 * de- and re-register.
193 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
194 cdev->id.cu_model != cdev->private->senseid.cu_model ||
195 cdev->id.dev_type != cdev->private->senseid.dev_type ||
196 cdev->id.dev_model != cdev->private->senseid.dev_model) {
197 PREPARE_WORK(&cdev->private->kick_work,
198 ccw_device_do_unreg_rereg);
199 queue_work(ccw_device_work, &cdev->private->kick_work);
202 cdev->private->flags.donotify = 1;
207 * The machine won't give us any notification by machine check if a chpid has
208 * been varied online on the SE so we have to find out by magic (i. e. driving
209 * the channel subsystem to device selection and updating our path masks).
212 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
218 for (i = 0; i<8; i++) {
220 if (!(sch->lpm & mask))
224 chpid.id = sch->schib.pmcw.chpid[i];
225 if (!chp_is_registered(chpid))
226 css_schedule_eval_all();
231 * Stop device recognition.
234 ccw_device_recog_done(struct ccw_device *cdev, int state)
236 struct subchannel *sch;
237 int notify, old_lpm, same_dev;
239 sch = to_subchannel(cdev->dev.parent);
241 ccw_device_set_timeout(cdev, 0);
242 cio_disable_subchannel(sch);
244 * Now that we tried recognition, we have performed device selection
245 * through ssch() and the path information is up to date.
248 stsch(sch->schid, &sch->schib);
249 sch->lpm = sch->schib.pmcw.pam & sch->opm;
250 /* Check since device may again have become not operational. */
251 if (!sch->schib.pmcw.dnv)
252 state = DEV_STATE_NOT_OPER;
253 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
254 /* Force reprobe on all chpids. */
256 if (sch->lpm != old_lpm)
257 __recover_lost_chpids(sch, old_lpm);
258 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
259 if (state == DEV_STATE_NOT_OPER) {
260 cdev->private->flags.recog_done = 1;
261 cdev->private->state = DEV_STATE_DISCONNECTED;
264 /* Boxed devices don't need extra treatment. */
267 same_dev = 0; /* Keep the compiler quiet... */
269 case DEV_STATE_NOT_OPER:
270 CIO_DEBUG(KERN_WARNING, 2,
271 "cio: SenseID : unknown device %04x on subchannel "
272 "0.%x.%04x\n", cdev->private->dev_id.devno,
273 sch->schid.ssid, sch->schid.sch_no);
275 case DEV_STATE_OFFLINE:
276 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
277 same_dev = ccw_device_handle_oper(cdev);
280 /* fill out sense information */
281 memset(&cdev->id, 0, sizeof(cdev->id));
282 cdev->id.cu_type = cdev->private->senseid.cu_type;
283 cdev->id.cu_model = cdev->private->senseid.cu_model;
284 cdev->id.dev_type = cdev->private->senseid.dev_type;
285 cdev->id.dev_model = cdev->private->senseid.dev_model;
287 cdev->private->state = DEV_STATE_OFFLINE;
289 /* Get device online again. */
290 ccw_device_online(cdev);
291 wake_up(&cdev->private->wait_q);
295 /* Issue device info message. */
296 CIO_DEBUG(KERN_INFO, 2,
297 "cio: SenseID : device 0.%x.%04x reports: "
298 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
300 cdev->private->dev_id.ssid,
301 cdev->private->dev_id.devno,
302 cdev->id.cu_type, cdev->id.cu_model,
303 cdev->id.dev_type, cdev->id.dev_model);
305 case DEV_STATE_BOXED:
306 CIO_DEBUG(KERN_WARNING, 2,
307 "cio: SenseID : boxed device %04x on subchannel "
308 "0.%x.%04x\n", cdev->private->dev_id.devno,
309 sch->schid.ssid, sch->schid.sch_no);
312 cdev->private->state = state;
313 io_subchannel_recog_done(cdev);
314 if (state != DEV_STATE_NOT_OPER)
315 wake_up(&cdev->private->wait_q);
319 * Function called from device_id.c after sense id has completed.
322 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
326 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
328 case -ETIME: /* Sense id stopped by timeout. */
329 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
332 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
338 ccw_device_oper_notify(struct work_struct *work)
340 struct ccw_device_private *priv;
341 struct ccw_device *cdev;
342 struct subchannel *sch;
346 priv = container_of(work, struct ccw_device_private, kick_work);
348 spin_lock_irqsave(cdev->ccwlock, flags);
349 sch = to_subchannel(cdev->dev.parent);
350 if (sch->driver && sch->driver->notify) {
351 spin_unlock_irqrestore(cdev->ccwlock, flags);
352 ret = sch->driver->notify(&sch->dev, CIO_OPER);
353 spin_lock_irqsave(cdev->ccwlock, flags);
357 /* Reenable channel measurements, if needed. */
358 spin_unlock_irqrestore(cdev->ccwlock, flags);
360 spin_lock_irqsave(cdev->ccwlock, flags);
361 wake_up(&cdev->private->wait_q);
363 spin_unlock_irqrestore(cdev->ccwlock, flags);
365 /* Driver doesn't want device back. */
366 ccw_device_do_unreg_rereg(work);
370 * Finished with online/offline processing.
373 ccw_device_done(struct ccw_device *cdev, int state)
375 struct subchannel *sch;
377 sch = to_subchannel(cdev->dev.parent);
379 ccw_device_set_timeout(cdev, 0);
381 if (state != DEV_STATE_ONLINE)
382 cio_disable_subchannel(sch);
384 /* Reset device status. */
385 memset(&cdev->private->irb, 0, sizeof(struct irb));
387 cdev->private->state = state;
390 if (state == DEV_STATE_BOXED)
391 CIO_DEBUG(KERN_WARNING, 2,
392 "cio: Boxed device %04x on subchannel %04x\n",
393 cdev->private->dev_id.devno, sch->schid.sch_no);
395 if (cdev->private->flags.donotify) {
396 cdev->private->flags.donotify = 0;
397 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
398 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
400 wake_up(&cdev->private->wait_q);
402 if (css_init_done && state != DEV_STATE_ONLINE)
403 put_device (&cdev->dev);
406 static int cmp_pgid(struct pgid *p1, struct pgid *p2)
414 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
417 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
423 for (i = 0; i < 8; i++) {
424 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
427 if (cdev->private->pgid[last].inf.ps.state1 ==
429 /* First non-zero PGID */
433 if (cmp_pgid(&cdev->private->pgid[i],
434 &cdev->private->pgid[last]) == 0)
435 /* Non-conflicting PGIDs */
438 /* PGID mismatch, can't pathgroup. */
439 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
440 "0.%x.%04x, can't pathgroup\n",
441 cdev->private->dev_id.ssid,
442 cdev->private->dev_id.devno);
443 cdev->private->options.pgroup = 0;
446 if (cdev->private->pgid[last].inf.ps.state1 ==
448 /* No previous pgid found */
449 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
450 sizeof(struct pgid));
452 /* Use existing pgid */
453 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
454 sizeof(struct pgid));
458 * Function called from device_pgid.c after sense path ground has completed.
461 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
463 struct subchannel *sch;
465 sch = to_subchannel(cdev->dev.parent);
467 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
468 cdev->private->options.pgroup = 0;
470 case 0: /* success */
471 case -EACCES: /* partial success, some paths not operational */
472 /* Check if all pgids are equal or 0. */
473 __ccw_device_get_common_pgid(cdev);
475 case -ETIME: /* Sense path group id stopped by timeout. */
476 case -EUSERS: /* device is reserved for someone else. */
477 ccw_device_done(cdev, DEV_STATE_BOXED);
480 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
483 /* Start Path Group verification. */
484 cdev->private->state = DEV_STATE_VERIFY;
485 cdev->private->flags.doverify = 0;
486 ccw_device_verify_start(cdev);
490 * Start device recognition.
493 ccw_device_recognition(struct ccw_device *cdev)
495 struct subchannel *sch;
498 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
499 (cdev->private->state != DEV_STATE_BOXED))
501 sch = to_subchannel(cdev->dev.parent);
502 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
504 /* Couldn't enable the subchannel for i/o. Sick device. */
507 /* After 60s the device recognition is considered to have failed. */
508 ccw_device_set_timeout(cdev, 60*HZ);
511 * We used to start here with a sense pgid to find out whether a device
512 * is locked by someone else. Unfortunately, the sense pgid command
513 * code has other meanings on devices predating the path grouping
514 * algorithm, so we start with sense id and box the device after an
515 * timeout (or if sense pgid during path verification detects the device
516 * is locked, as may happen on newer devices).
518 cdev->private->flags.recog_done = 0;
519 cdev->private->state = DEV_STATE_SENSE_ID;
520 ccw_device_sense_id_start(cdev);
525 * Handle timeout in device recognition.
528 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
532 ret = ccw_device_cancel_halt_clear(cdev);
535 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
538 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
541 ccw_device_set_timeout(cdev, 3*HZ);
547 ccw_device_nopath_notify(struct work_struct *work)
549 struct ccw_device_private *priv;
550 struct ccw_device *cdev;
551 struct subchannel *sch;
555 priv = container_of(work, struct ccw_device_private, kick_work);
557 spin_lock_irqsave(cdev->ccwlock, flags);
558 sch = to_subchannel(cdev->dev.parent);
562 if (sch->driver && sch->driver->notify) {
563 spin_unlock_irqrestore(cdev->ccwlock, flags);
564 ret = sch->driver->notify(&sch->dev, CIO_NO_PATH);
565 spin_lock_irqsave(cdev->ccwlock, flags);
569 if (get_device(&sch->dev)) {
570 /* Driver doesn't want to keep device. */
571 cio_disable_subchannel(sch);
572 if (get_device(&cdev->dev)) {
573 PREPARE_WORK(&cdev->private->kick_work,
574 ccw_device_call_sch_unregister);
575 queue_work(ccw_device_work,
576 &cdev->private->kick_work);
578 put_device(&sch->dev);
581 cio_disable_subchannel(sch);
582 ccw_device_set_timeout(cdev, 0);
583 cdev->private->flags.fake_irb = 0;
584 cdev->private->state = DEV_STATE_DISCONNECTED;
585 wake_up(&cdev->private->wait_q);
588 spin_unlock_irqrestore(cdev->ccwlock, flags);
592 ccw_device_verify_done(struct ccw_device *cdev, int err)
594 struct subchannel *sch;
596 sch = to_subchannel(cdev->dev.parent);
597 /* Update schib - pom may have changed. */
598 stsch(sch->schid, &sch->schib);
599 /* Update lpm with verified path mask. */
601 /* Repeat path verification? */
602 if (cdev->private->flags.doverify) {
603 cdev->private->flags.doverify = 0;
604 ccw_device_verify_start(cdev);
608 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
609 cdev->private->options.pgroup = 0;
611 ccw_device_done(cdev, DEV_STATE_ONLINE);
612 /* Deliver fake irb to device driver, if needed. */
613 if (cdev->private->flags.fake_irb) {
614 memset(&cdev->private->irb, 0, sizeof(struct irb));
615 cdev->private->irb.scsw.cc = 1;
616 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
617 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
618 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
619 cdev->private->flags.fake_irb = 0;
621 cdev->handler(cdev, cdev->private->intparm,
622 &cdev->private->irb);
623 memset(&cdev->private->irb, 0, sizeof(struct irb));
627 /* Reset oper notify indication after verify error. */
628 cdev->private->flags.donotify = 0;
629 ccw_device_done(cdev, DEV_STATE_BOXED);
632 /* Reset oper notify indication after verify error. */
633 cdev->private->flags.donotify = 0;
635 PREPARE_WORK(&cdev->private->kick_work,
636 ccw_device_nopath_notify);
637 queue_work(ccw_device_notify_work,
638 &cdev->private->kick_work);
640 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
649 ccw_device_online(struct ccw_device *cdev)
651 struct subchannel *sch;
654 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
655 (cdev->private->state != DEV_STATE_BOXED))
657 sch = to_subchannel(cdev->dev.parent);
658 if (css_init_done && !get_device(&cdev->dev))
660 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
662 /* Couldn't enable the subchannel for i/o. Sick device. */
664 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
667 /* Do we want to do path grouping? */
668 if (!cdev->private->options.pgroup) {
669 /* Start initial path verification. */
670 cdev->private->state = DEV_STATE_VERIFY;
671 cdev->private->flags.doverify = 0;
672 ccw_device_verify_start(cdev);
675 /* Do a SensePGID first. */
676 cdev->private->state = DEV_STATE_SENSE_PGID;
677 ccw_device_sense_pgid_start(cdev);
682 ccw_device_disband_done(struct ccw_device *cdev, int err)
686 ccw_device_done(cdev, DEV_STATE_OFFLINE);
689 ccw_device_done(cdev, DEV_STATE_BOXED);
692 cdev->private->flags.donotify = 0;
693 if (get_device(&cdev->dev)) {
694 PREPARE_WORK(&cdev->private->kick_work,
695 ccw_device_call_sch_unregister);
696 queue_work(ccw_device_work, &cdev->private->kick_work);
698 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
707 ccw_device_offline(struct ccw_device *cdev)
709 struct subchannel *sch;
711 if (ccw_device_is_orphan(cdev)) {
712 ccw_device_done(cdev, DEV_STATE_OFFLINE);
715 sch = to_subchannel(cdev->dev.parent);
716 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
718 if (cdev->private->state != DEV_STATE_ONLINE) {
719 if (sch->schib.scsw.actl != 0)
723 if (sch->schib.scsw.actl != 0)
725 /* Are we doing path grouping? */
726 if (!cdev->private->options.pgroup) {
727 /* No, set state offline immediately. */
728 ccw_device_done(cdev, DEV_STATE_OFFLINE);
731 /* Start Set Path Group commands. */
732 cdev->private->state = DEV_STATE_DISBAND_PGID;
733 ccw_device_disband_start(cdev);
738 * Handle timeout in device online/offline process.
741 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
745 ret = ccw_device_cancel_halt_clear(cdev);
748 ccw_device_done(cdev, DEV_STATE_BOXED);
751 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
754 ccw_device_set_timeout(cdev, 3*HZ);
759 * Handle not oper event in device recognition.
762 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
764 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
768 * Handle not operational event while offline.
771 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
773 struct subchannel *sch;
775 cdev->private->state = DEV_STATE_NOT_OPER;
776 sch = to_subchannel(cdev->dev.parent);
777 if (get_device(&cdev->dev)) {
778 PREPARE_WORK(&cdev->private->kick_work,
779 ccw_device_call_sch_unregister);
780 queue_work(ccw_device_work, &cdev->private->kick_work);
782 wake_up(&cdev->private->wait_q);
786 * Handle not operational event while online.
789 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
791 struct subchannel *sch;
794 sch = to_subchannel(cdev->dev.parent);
795 if (sch->driver->notify) {
796 spin_unlock_irq(cdev->ccwlock);
797 ret = sch->driver->notify(&sch->dev,
798 sch->lpm ? CIO_GONE : CIO_NO_PATH);
799 spin_lock_irq(cdev->ccwlock);
803 ccw_device_set_timeout(cdev, 0);
804 cdev->private->flags.fake_irb = 0;
805 cdev->private->state = DEV_STATE_DISCONNECTED;
806 wake_up(&cdev->private->wait_q);
809 cdev->private->state = DEV_STATE_NOT_OPER;
810 cio_disable_subchannel(sch);
811 if (sch->schib.scsw.actl != 0) {
812 // FIXME: not-oper indication to device driver ?
813 ccw_device_call_handler(cdev);
815 if (get_device(&cdev->dev)) {
816 PREPARE_WORK(&cdev->private->kick_work,
817 ccw_device_call_sch_unregister);
818 queue_work(ccw_device_work, &cdev->private->kick_work);
820 wake_up(&cdev->private->wait_q);
824 * Handle path verification event.
827 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
829 struct subchannel *sch;
831 if (cdev->private->state == DEV_STATE_W4SENSE) {
832 cdev->private->flags.doverify = 1;
835 sch = to_subchannel(cdev->dev.parent);
837 * Since we might not just be coming from an interrupt from the
838 * subchannel we have to update the schib.
840 stsch(sch->schid, &sch->schib);
842 if (sch->schib.scsw.actl != 0 ||
843 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
844 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
846 * No final status yet or final status not yet delivered
847 * to the device driver. Can't do path verfication now,
848 * delay until final status was delivered.
850 cdev->private->flags.doverify = 1;
853 /* Device is idle, we can do the path verification. */
854 cdev->private->state = DEV_STATE_VERIFY;
855 cdev->private->flags.doverify = 0;
856 ccw_device_verify_start(cdev);
860 * Got an interrupt for a normal io (state online).
863 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
867 irb = (struct irb *) __LC_IRB;
868 /* Check for unsolicited interrupt. */
869 if ((irb->scsw.stctl ==
870 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
871 && (!irb->scsw.cc)) {
872 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
873 !irb->esw.esw0.erw.cons) {
874 /* Unit check but no sense data. Need basic sense. */
875 if (ccw_device_do_sense(cdev, irb) != 0)
876 goto call_handler_unsol;
877 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
878 cdev->private->state = DEV_STATE_W4SENSE;
879 cdev->private->intparm = 0;
884 cdev->handler (cdev, 0, irb);
885 if (cdev->private->flags.doverify)
886 ccw_device_online_verify(cdev, 0);
889 /* Accumulate status and find out if a basic sense is needed. */
890 ccw_device_accumulate_irb(cdev, irb);
891 if (cdev->private->flags.dosense) {
892 if (ccw_device_do_sense(cdev, irb) == 0) {
893 cdev->private->state = DEV_STATE_W4SENSE;
897 /* Call the handler. */
898 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
899 /* Start delayed path verification. */
900 ccw_device_online_verify(cdev, 0);
904 * Got an timeout in online state.
907 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
911 ccw_device_set_timeout(cdev, 0);
912 ret = ccw_device_cancel_halt_clear(cdev);
914 ccw_device_set_timeout(cdev, 3*HZ);
915 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
918 if (ret == -ENODEV) {
919 struct subchannel *sch;
921 sch = to_subchannel(cdev->dev.parent);
923 PREPARE_WORK(&cdev->private->kick_work,
924 ccw_device_nopath_notify);
925 queue_work(ccw_device_notify_work,
926 &cdev->private->kick_work);
928 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
929 } else if (cdev->handler)
930 cdev->handler(cdev, cdev->private->intparm,
931 ERR_PTR(-ETIMEDOUT));
935 * Got an interrupt for a basic sense.
938 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
942 irb = (struct irb *) __LC_IRB;
943 /* Check for unsolicited interrupt. */
944 if (irb->scsw.stctl ==
945 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
946 if (irb->scsw.cc == 1)
947 /* Basic sense hasn't started. Try again. */
948 ccw_device_do_sense(cdev, irb);
950 CIO_MSG_EVENT(2, "Huh? 0.%x.%04x: unsolicited "
951 "interrupt during w4sense...\n",
952 cdev->private->dev_id.ssid,
953 cdev->private->dev_id.devno);
955 cdev->handler (cdev, 0, irb);
960 * Check if a halt or clear has been issued in the meanwhile. If yes,
961 * only deliver the halt/clear interrupt to the device driver as if it
962 * had killed the original request.
964 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
965 /* Retry Basic Sense if requested. */
966 if (cdev->private->flags.intretry) {
967 cdev->private->flags.intretry = 0;
968 ccw_device_do_sense(cdev, irb);
971 cdev->private->flags.dosense = 0;
972 memset(&cdev->private->irb, 0, sizeof(struct irb));
973 ccw_device_accumulate_irb(cdev, irb);
976 /* Add basic sense info to irb. */
977 ccw_device_accumulate_basic_sense(cdev, irb);
978 if (cdev->private->flags.dosense) {
979 /* Another basic sense is needed. */
980 ccw_device_do_sense(cdev, irb);
984 cdev->private->state = DEV_STATE_ONLINE;
985 /* Call the handler. */
986 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
987 /* Start delayed path verification. */
988 ccw_device_online_verify(cdev, 0);
992 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
996 irb = (struct irb *) __LC_IRB;
997 /* Accumulate status. We don't do basic sense. */
998 ccw_device_accumulate_irb(cdev, irb);
999 /* Remember to clear irb to avoid residuals. */
1000 memset(&cdev->private->irb, 0, sizeof(struct irb));
1001 /* Try to start delayed device verification. */
1002 ccw_device_online_verify(cdev, 0);
1003 /* Note: Don't call handler for cio initiated clear! */
1007 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
1009 struct subchannel *sch;
1011 sch = to_subchannel(cdev->dev.parent);
1012 ccw_device_set_timeout(cdev, 0);
1013 /* Start delayed path verification. */
1014 ccw_device_online_verify(cdev, 0);
1015 /* OK, i/o is dead now. Call interrupt handler. */
1017 cdev->handler(cdev, cdev->private->intparm,
1022 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1026 ret = ccw_device_cancel_halt_clear(cdev);
1027 if (ret == -EBUSY) {
1028 ccw_device_set_timeout(cdev, 3*HZ);
1031 /* Start delayed path verification. */
1032 ccw_device_online_verify(cdev, 0);
1034 cdev->handler(cdev, cdev->private->intparm,
1038 void device_kill_io(struct subchannel *sch)
1041 struct ccw_device *cdev;
1043 cdev = sch->dev.driver_data;
1044 ret = ccw_device_cancel_halt_clear(cdev);
1045 if (ret == -EBUSY) {
1046 ccw_device_set_timeout(cdev, 3*HZ);
1047 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1050 /* Start delayed path verification. */
1051 ccw_device_online_verify(cdev, 0);
1053 cdev->handler(cdev, cdev->private->intparm,
1058 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
1060 /* Start verification after current task finished. */
1061 cdev->private->flags.doverify = 1;
1065 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1069 switch (dev_event) {
1070 case DEV_EVENT_INTERRUPT:
1071 irb = (struct irb *) __LC_IRB;
1072 /* Check for unsolicited interrupt. */
1073 if ((irb->scsw.stctl ==
1074 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1076 /* FIXME: we should restart stlck here, but this
1077 * is extremely unlikely ... */
1080 ccw_device_accumulate_irb(cdev, irb);
1081 /* We don't care about basic sense etc. */
1083 default: /* timeout */
1087 wake_up(&cdev->private->wait_q);
1091 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1093 struct subchannel *sch;
1095 sch = to_subchannel(cdev->dev.parent);
1096 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1097 /* Couldn't enable the subchannel for i/o. Sick device. */
1100 /* After 60s the device recognition is considered to have failed. */
1101 ccw_device_set_timeout(cdev, 60*HZ);
1103 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1104 ccw_device_sense_id_start(cdev);
1108 device_trigger_reprobe(struct subchannel *sch)
1110 struct ccw_device *cdev;
1112 if (!sch->dev.driver_data)
1114 cdev = sch->dev.driver_data;
1115 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1118 /* Update some values. */
1119 if (stsch(sch->schid, &sch->schib))
1121 if (!sch->schib.pmcw.dnv)
1124 * The pim, pam, pom values may not be accurate, but they are the best
1125 * we have before performing device selection :/
1127 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1128 /* Re-set some bits in the pmcw that were lost. */
1129 sch->schib.pmcw.isc = 3;
1130 sch->schib.pmcw.csense = 1;
1131 sch->schib.pmcw.ena = 0;
1132 if ((sch->lpm & (sch->lpm - 1)) != 0)
1133 sch->schib.pmcw.mp = 1;
1134 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1135 /* We should also udate ssd info, but this has to wait. */
1136 /* Check if this is another device which appeared on the same sch. */
1137 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1138 PREPARE_WORK(&cdev->private->kick_work,
1139 ccw_device_move_to_orphanage);
1140 queue_work(ccw_device_work, &cdev->private->kick_work);
1142 ccw_device_start_id(cdev, 0);
1146 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1148 struct subchannel *sch;
1150 sch = to_subchannel(cdev->dev.parent);
1152 * An interrupt in state offline means a previous disable was not
1153 * successful. Try again.
1155 cio_disable_subchannel(sch);
1159 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1161 retry_set_schib(cdev);
1162 cdev->private->state = DEV_STATE_ONLINE;
1163 dev_fsm_event(cdev, dev_event);
1166 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1167 enum dev_event dev_event)
1169 cmf_retry_copy_block(cdev);
1170 cdev->private->state = DEV_STATE_ONLINE;
1171 dev_fsm_event(cdev, dev_event);
1175 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1177 ccw_device_set_timeout(cdev, 0);
1178 if (dev_event == DEV_EVENT_NOTOPER)
1179 cdev->private->state = DEV_STATE_NOT_OPER;
1181 cdev->private->state = DEV_STATE_OFFLINE;
1182 wake_up(&cdev->private->wait_q);
1186 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1190 ret = ccw_device_cancel_halt_clear(cdev);
1193 cdev->private->state = DEV_STATE_OFFLINE;
1194 wake_up(&cdev->private->wait_q);
1197 cdev->private->state = DEV_STATE_NOT_OPER;
1198 wake_up(&cdev->private->wait_q);
1201 ccw_device_set_timeout(cdev, HZ/10);
1206 * No operation action. This is used e.g. to ignore a timeout event in
1210 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1215 * Bug operation action.
1218 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1220 CIO_MSG_EVENT(0, "dev_jumptable[%i][%i] == NULL\n",
1221 cdev->private->state, dev_event);
1226 * device statemachine
1228 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1229 [DEV_STATE_NOT_OPER] = {
1230 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1231 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1232 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1233 [DEV_EVENT_VERIFY] = ccw_device_nop,
1235 [DEV_STATE_SENSE_PGID] = {
1236 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1237 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1238 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1239 [DEV_EVENT_VERIFY] = ccw_device_nop,
1241 [DEV_STATE_SENSE_ID] = {
1242 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1243 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1244 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1245 [DEV_EVENT_VERIFY] = ccw_device_nop,
1247 [DEV_STATE_OFFLINE] = {
1248 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1249 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1250 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1251 [DEV_EVENT_VERIFY] = ccw_device_nop,
1253 [DEV_STATE_VERIFY] = {
1254 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1255 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1256 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1257 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1259 [DEV_STATE_ONLINE] = {
1260 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1261 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1262 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1263 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1265 [DEV_STATE_W4SENSE] = {
1266 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1267 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1268 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1269 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1271 [DEV_STATE_DISBAND_PGID] = {
1272 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1273 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1274 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1275 [DEV_EVENT_VERIFY] = ccw_device_nop,
1277 [DEV_STATE_BOXED] = {
1278 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1279 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1280 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1281 [DEV_EVENT_VERIFY] = ccw_device_nop,
1283 /* states to wait for i/o completion before doing something */
1284 [DEV_STATE_CLEAR_VERIFY] = {
1285 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1286 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1287 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1288 [DEV_EVENT_VERIFY] = ccw_device_nop,
1290 [DEV_STATE_TIMEOUT_KILL] = {
1291 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1292 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1293 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1294 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1296 [DEV_STATE_QUIESCE] = {
1297 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1298 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1299 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1300 [DEV_EVENT_VERIFY] = ccw_device_nop,
1302 /* special states for devices gone not operational */
1303 [DEV_STATE_DISCONNECTED] = {
1304 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1305 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1306 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1307 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1309 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1310 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1311 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1312 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1313 [DEV_EVENT_VERIFY] = ccw_device_nop,
1315 [DEV_STATE_CMFCHANGE] = {
1316 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1317 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1318 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1319 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1321 [DEV_STATE_CMFUPDATE] = {
1322 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1323 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1324 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1325 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1330 * io_subchannel_irq is called for "real" interrupts or for status
1331 * pending conditions on msch.
1334 io_subchannel_irq (struct device *pdev)
1336 struct ccw_device *cdev;
1338 cdev = to_subchannel(pdev)->dev.driver_data;
1340 CIO_TRACE_EVENT (3, "IRQ");
1341 CIO_TRACE_EVENT (3, pdev->bus_id);
1343 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1346 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);