[S390] cio: use sense-pgid operation for path verification
[pandora-kernel.git] / drivers / s390 / cio / device_fsm.c
1 /*
2  * drivers/s390/cio/device_fsm.c
3  * finite state machine for device handling
4  *
5  *    Copyright IBM Corp. 2002,2008
6  *    Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
7  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
8  */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/jiffies.h>
13 #include <linux/string.h>
14
15 #include <asm/ccwdev.h>
16 #include <asm/cio.h>
17 #include <asm/chpid.h>
18
19 #include "cio.h"
20 #include "cio_debug.h"
21 #include "css.h"
22 #include "device.h"
23 #include "chsc.h"
24 #include "ioasm.h"
25 #include "chp.h"
26
27 static int timeout_log_enabled;
28
29 static int __init ccw_timeout_log_setup(char *unused)
30 {
31         timeout_log_enabled = 1;
32         return 1;
33 }
34
35 __setup("ccw_timeout_log", ccw_timeout_log_setup);
36
37 static void ccw_timeout_log(struct ccw_device *cdev)
38 {
39         struct schib schib;
40         struct subchannel *sch;
41         struct io_subchannel_private *private;
42         union orb *orb;
43         int cc;
44
45         sch = to_subchannel(cdev->dev.parent);
46         private = to_io_private(sch);
47         orb = &private->orb;
48         cc = stsch(sch->schid, &schib);
49
50         printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
51                "device information:\n", get_clock());
52         printk(KERN_WARNING "cio: orb:\n");
53         print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
54                        orb, sizeof(*orb), 0);
55         printk(KERN_WARNING "cio: ccw device bus id: %s\n",
56                dev_name(&cdev->dev));
57         printk(KERN_WARNING "cio: subchannel bus id: %s\n",
58                dev_name(&sch->dev));
59         printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
60                "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
61
62         if (orb->tm.b) {
63                 printk(KERN_WARNING "cio: orb indicates transport mode\n");
64                 printk(KERN_WARNING "cio: last tcw:\n");
65                 print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
66                                (void *)(addr_t)orb->tm.tcw,
67                                sizeof(struct tcw), 0);
68         } else {
69                 printk(KERN_WARNING "cio: orb indicates command mode\n");
70                 if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
71                     (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
72                         printk(KERN_WARNING "cio: last channel program "
73                                "(intern):\n");
74                 else
75                         printk(KERN_WARNING "cio: last channel program:\n");
76
77                 print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
78                                (void *)(addr_t)orb->cmd.cpa,
79                                sizeof(struct ccw1), 0);
80         }
81         printk(KERN_WARNING "cio: ccw device state: %d\n",
82                cdev->private->state);
83         printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
84         printk(KERN_WARNING "cio: schib:\n");
85         print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
86                        &schib, sizeof(schib), 0);
87         printk(KERN_WARNING "cio: ccw device flags:\n");
88         print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
89                        &cdev->private->flags, sizeof(cdev->private->flags), 0);
90 }
91
92 /*
93  * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
94  */
95 static void
96 ccw_device_timeout(unsigned long data)
97 {
98         struct ccw_device *cdev;
99
100         cdev = (struct ccw_device *) data;
101         spin_lock_irq(cdev->ccwlock);
102         if (timeout_log_enabled)
103                 ccw_timeout_log(cdev);
104         dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
105         spin_unlock_irq(cdev->ccwlock);
106 }
107
108 /*
109  * Set timeout
110  */
111 void
112 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
113 {
114         if (expires == 0) {
115                 del_timer(&cdev->private->timer);
116                 return;
117         }
118         if (timer_pending(&cdev->private->timer)) {
119                 if (mod_timer(&cdev->private->timer, jiffies + expires))
120                         return;
121         }
122         cdev->private->timer.function = ccw_device_timeout;
123         cdev->private->timer.data = (unsigned long) cdev;
124         cdev->private->timer.expires = jiffies + expires;
125         add_timer(&cdev->private->timer);
126 }
127
128 /*
129  * Cancel running i/o. This is called repeatedly since halt/clear are
130  * asynchronous operations. We do one try with cio_cancel, two tries
131  * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
132  * Returns 0 if device now idle, -ENODEV for device not operational and
133  * -EBUSY if an interrupt is expected (either from halt/clear or from a
134  * status pending).
135  */
136 int
137 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
138 {
139         struct subchannel *sch;
140         int ret;
141
142         sch = to_subchannel(cdev->dev.parent);
143         if (cio_update_schib(sch))
144                 return -ENODEV; 
145         if (!sch->schib.pmcw.ena)
146                 /* Not operational -> done. */
147                 return 0;
148         /* Stage 1: cancel io. */
149         if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
150             !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
151                 if (!scsw_is_tm(&sch->schib.scsw)) {
152                         ret = cio_cancel(sch);
153                         if (ret != -EINVAL)
154                                 return ret;
155                 }
156                 /* cancel io unsuccessful or not applicable (transport mode).
157                  * Continue with asynchronous instructions. */
158                 cdev->private->iretry = 3;      /* 3 halt retries. */
159         }
160         if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
161                 /* Stage 2: halt io. */
162                 if (cdev->private->iretry) {
163                         cdev->private->iretry--;
164                         ret = cio_halt(sch);
165                         if (ret != -EBUSY)
166                                 return (ret == 0) ? -EBUSY : ret;
167                 }
168                 /* halt io unsuccessful. */
169                 cdev->private->iretry = 255;    /* 255 clear retries. */
170         }
171         /* Stage 3: clear io. */
172         if (cdev->private->iretry) {
173                 cdev->private->iretry--;
174                 ret = cio_clear (sch);
175                 return (ret == 0) ? -EBUSY : ret;
176         }
177         panic("Can't stop i/o on subchannel.\n");
178 }
179
180 void ccw_device_update_sense_data(struct ccw_device *cdev)
181 {
182         memset(&cdev->id, 0, sizeof(cdev->id));
183         cdev->id.cu_type   = cdev->private->senseid.cu_type;
184         cdev->id.cu_model  = cdev->private->senseid.cu_model;
185         cdev->id.dev_type  = cdev->private->senseid.dev_type;
186         cdev->id.dev_model = cdev->private->senseid.dev_model;
187 }
188
189 int ccw_device_test_sense_data(struct ccw_device *cdev)
190 {
191         return 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 }
196
197 /*
198  * The machine won't give us any notification by machine check if a chpid has
199  * been varied online on the SE so we have to find out by magic (i. e. driving
200  * the channel subsystem to device selection and updating our path masks).
201  */
202 static void
203 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
204 {
205         int mask, i;
206         struct chp_id chpid;
207
208         chp_id_init(&chpid);
209         for (i = 0; i<8; i++) {
210                 mask = 0x80 >> i;
211                 if (!(sch->lpm & mask))
212                         continue;
213                 if (old_lpm & mask)
214                         continue;
215                 chpid.id = sch->schib.pmcw.chpid[i];
216                 if (!chp_is_registered(chpid))
217                         css_schedule_eval_all();
218         }
219 }
220
221 /*
222  * Stop device recognition.
223  */
224 static void
225 ccw_device_recog_done(struct ccw_device *cdev, int state)
226 {
227         struct subchannel *sch;
228         int old_lpm;
229
230         sch = to_subchannel(cdev->dev.parent);
231
232         cio_disable_subchannel(sch);
233         /*
234          * Now that we tried recognition, we have performed device selection
235          * through ssch() and the path information is up to date.
236          */
237         old_lpm = sch->lpm;
238
239         /* Check since device may again have become not operational. */
240         if (cio_update_schib(sch))
241                 state = DEV_STATE_NOT_OPER;
242         else
243                 sch->lpm = sch->schib.pmcw.pam & sch->opm;
244
245         if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
246                 /* Force reprobe on all chpids. */
247                 old_lpm = 0;
248         if (sch->lpm != old_lpm)
249                 __recover_lost_chpids(sch, old_lpm);
250         if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
251             (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
252                 cdev->private->flags.recog_done = 1;
253                 cdev->private->state = DEV_STATE_DISCONNECTED;
254                 wake_up(&cdev->private->wait_q);
255                 return;
256         }
257         if (cdev->private->flags.resuming) {
258                 cdev->private->state = state;
259                 cdev->private->flags.recog_done = 1;
260                 wake_up(&cdev->private->wait_q);
261                 return;
262         }
263         switch (state) {
264         case DEV_STATE_NOT_OPER:
265                 break;
266         case DEV_STATE_OFFLINE:
267                 if (!cdev->online) {
268                         ccw_device_update_sense_data(cdev);
269                         break;
270                 }
271                 cdev->private->state = DEV_STATE_OFFLINE;
272                 cdev->private->flags.recog_done = 1;
273                 if (ccw_device_test_sense_data(cdev)) {
274                         cdev->private->flags.donotify = 1;
275                         ccw_device_online(cdev);
276                         wake_up(&cdev->private->wait_q);
277                 } else {
278                         ccw_device_update_sense_data(cdev);
279                         ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
280                 }
281                 return;
282         case DEV_STATE_BOXED:
283                 if (cdev->id.cu_type != 0) { /* device was recognized before */
284                         cdev->private->flags.recog_done = 1;
285                         cdev->private->state = DEV_STATE_BOXED;
286                         wake_up(&cdev->private->wait_q);
287                         return;
288                 }
289                 break;
290         }
291         cdev->private->state = state;
292         io_subchannel_recog_done(cdev);
293         wake_up(&cdev->private->wait_q);
294 }
295
296 /*
297  * Function called from device_id.c after sense id has completed.
298  */
299 void
300 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
301 {
302         switch (err) {
303         case 0:
304                 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
305                 break;
306         case -ETIME:            /* Sense id stopped by timeout. */
307                 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
308                 break;
309         default:
310                 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
311                 break;
312         }
313 }
314
315 int ccw_device_notify(struct ccw_device *cdev, int event)
316 {
317         if (!cdev->drv)
318                 return 0;
319         if (!cdev->online)
320                 return 0;
321         CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
322                       cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
323                       event);
324         return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
325 }
326
327 static void ccw_device_oper_notify(struct ccw_device *cdev)
328 {
329         if (ccw_device_notify(cdev, CIO_OPER)) {
330                 /* Reenable channel measurements, if needed. */
331                 ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
332                 return;
333         }
334         /* Driver doesn't want device back. */
335         ccw_device_set_notoper(cdev);
336         ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
337 }
338
339 /*
340  * Finished with online/offline processing.
341  */
342 static void
343 ccw_device_done(struct ccw_device *cdev, int state)
344 {
345         struct subchannel *sch;
346
347         sch = to_subchannel(cdev->dev.parent);
348
349         ccw_device_set_timeout(cdev, 0);
350
351         if (state != DEV_STATE_ONLINE)
352                 cio_disable_subchannel(sch);
353
354         /* Reset device status. */
355         memset(&cdev->private->irb, 0, sizeof(struct irb));
356
357         cdev->private->state = state;
358
359         switch (state) {
360         case DEV_STATE_BOXED:
361                 CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
362                               cdev->private->dev_id.devno, sch->schid.sch_no);
363                 if (cdev->online && !ccw_device_notify(cdev, CIO_BOXED))
364                         ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
365                 cdev->private->flags.donotify = 0;
366                 break;
367         case DEV_STATE_NOT_OPER:
368                 CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
369                               cdev->private->dev_id.devno, sch->schid.sch_no);
370                 if (!ccw_device_notify(cdev, CIO_GONE))
371                         ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
372                 else
373                         ccw_device_set_disconnected(cdev);
374                 cdev->private->flags.donotify = 0;
375                 break;
376         case DEV_STATE_DISCONNECTED:
377                 CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
378                               "%04x\n", cdev->private->dev_id.devno,
379                               sch->schid.sch_no);
380                 if (!ccw_device_notify(cdev, CIO_NO_PATH))
381                         ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
382                 else
383                         ccw_device_set_disconnected(cdev);
384                 cdev->private->flags.donotify = 0;
385                 break;
386         default:
387                 break;
388         }
389
390         if (cdev->private->flags.donotify) {
391                 cdev->private->flags.donotify = 0;
392                 ccw_device_oper_notify(cdev);
393         }
394         wake_up(&cdev->private->wait_q);
395 }
396
397 /*
398  * Start device recognition.
399  */
400 void ccw_device_recognition(struct ccw_device *cdev)
401 {
402         struct subchannel *sch = to_subchannel(cdev->dev.parent);
403
404         /*
405          * We used to start here with a sense pgid to find out whether a device
406          * is locked by someone else. Unfortunately, the sense pgid command
407          * code has other meanings on devices predating the path grouping
408          * algorithm, so we start with sense id and box the device after an
409          * timeout (or if sense pgid during path verification detects the device
410          * is locked, as may happen on newer devices).
411          */
412         cdev->private->flags.recog_done = 0;
413         cdev->private->state = DEV_STATE_SENSE_ID;
414         if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
415                 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
416                 return;
417         }
418         ccw_device_sense_id_start(cdev);
419 }
420
421 /*
422  * Handle events for states that use the ccw request infrastructure.
423  */
424 static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
425 {
426         switch (e) {
427         case DEV_EVENT_NOTOPER:
428                 ccw_request_notoper(cdev);
429                 break;
430         case DEV_EVENT_INTERRUPT:
431                 ccw_request_handler(cdev);
432                 break;
433         case DEV_EVENT_TIMEOUT:
434                 ccw_request_timeout(cdev);
435                 break;
436         default:
437                 break;
438         }
439 }
440
441 void
442 ccw_device_verify_done(struct ccw_device *cdev, int err)
443 {
444         struct subchannel *sch;
445
446         sch = to_subchannel(cdev->dev.parent);
447         /* Update schib - pom may have changed. */
448         if (cio_update_schib(sch)) {
449                 err = -ENODEV;
450                 goto callback;
451         }
452         /* Update lpm with verified path mask. */
453         sch->lpm = sch->vpm;
454         /* Repeat path verification? */
455         if (cdev->private->flags.doverify) {
456                 ccw_device_verify_start(cdev);
457                 return;
458         }
459 callback:
460         switch (err) {
461         case 0:
462                 ccw_device_done(cdev, DEV_STATE_ONLINE);
463                 /* Deliver fake irb to device driver, if needed. */
464                 if (cdev->private->flags.fake_irb) {
465                         memset(&cdev->private->irb, 0, sizeof(struct irb));
466                         cdev->private->irb.scsw.cmd.cc = 1;
467                         cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
468                         cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
469                         cdev->private->irb.scsw.cmd.stctl =
470                                 SCSW_STCTL_STATUS_PEND;
471                         cdev->private->flags.fake_irb = 0;
472                         if (cdev->handler)
473                                 cdev->handler(cdev, cdev->private->intparm,
474                                               &cdev->private->irb);
475                         memset(&cdev->private->irb, 0, sizeof(struct irb));
476                 }
477                 break;
478         case -ETIME:
479         case -EUSERS:
480                 /* Reset oper notify indication after verify error. */
481                 cdev->private->flags.donotify = 0;
482                 ccw_device_done(cdev, DEV_STATE_BOXED);
483                 break;
484         case -EACCES:
485                 /* Reset oper notify indication after verify error. */
486                 cdev->private->flags.donotify = 0;
487                 ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
488                 break;
489         default:
490                 /* Reset oper notify indication after verify error. */
491                 cdev->private->flags.donotify = 0;
492                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
493                 break;
494         }
495 }
496
497 /*
498  * Get device online.
499  */
500 int
501 ccw_device_online(struct ccw_device *cdev)
502 {
503         struct subchannel *sch;
504         int ret;
505
506         if ((cdev->private->state != DEV_STATE_OFFLINE) &&
507             (cdev->private->state != DEV_STATE_BOXED))
508                 return -EINVAL;
509         sch = to_subchannel(cdev->dev.parent);
510         ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
511         if (ret != 0) {
512                 /* Couldn't enable the subchannel for i/o. Sick device. */
513                 if (ret == -ENODEV)
514                         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
515                 return ret;
516         }
517         /* Start initial path verification. */
518         cdev->private->state = DEV_STATE_VERIFY;
519         ccw_device_verify_start(cdev);
520         return 0;
521 }
522
523 void
524 ccw_device_disband_done(struct ccw_device *cdev, int err)
525 {
526         switch (err) {
527         case 0:
528                 ccw_device_done(cdev, DEV_STATE_OFFLINE);
529                 break;
530         case -ETIME:
531                 ccw_device_done(cdev, DEV_STATE_BOXED);
532                 break;
533         default:
534                 cdev->private->flags.donotify = 0;
535                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
536                 break;
537         }
538 }
539
540 /*
541  * Shutdown device.
542  */
543 int
544 ccw_device_offline(struct ccw_device *cdev)
545 {
546         struct subchannel *sch;
547
548         /* Allow ccw_device_offline while disconnected. */
549         if (cdev->private->state == DEV_STATE_DISCONNECTED ||
550             cdev->private->state == DEV_STATE_NOT_OPER) {
551                 cdev->private->flags.donotify = 0;
552                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
553                 return 0;
554         }
555         if (cdev->private->state == DEV_STATE_BOXED) {
556                 ccw_device_done(cdev, DEV_STATE_BOXED);
557                 return 0;
558         }
559         if (ccw_device_is_orphan(cdev)) {
560                 ccw_device_done(cdev, DEV_STATE_OFFLINE);
561                 return 0;
562         }
563         sch = to_subchannel(cdev->dev.parent);
564         if (cio_update_schib(sch))
565                 return -ENODEV;
566         if (scsw_actl(&sch->schib.scsw) != 0)
567                 return -EBUSY;
568         if (cdev->private->state != DEV_STATE_ONLINE)
569                 return -EINVAL;
570         /* Are we doing path grouping? */
571         if (!cdev->private->flags.pgroup) {
572                 /* No, set state offline immediately. */
573                 ccw_device_done(cdev, DEV_STATE_OFFLINE);
574                 return 0;
575         }
576         /* Start Set Path Group commands. */
577         cdev->private->state = DEV_STATE_DISBAND_PGID;
578         ccw_device_disband_start(cdev);
579         return 0;
580 }
581
582 /*
583  * Handle not operational event in non-special state.
584  */
585 static void ccw_device_generic_notoper(struct ccw_device *cdev,
586                                        enum dev_event dev_event)
587 {
588         if (!ccw_device_notify(cdev, CIO_GONE))
589                 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
590         else
591                 ccw_device_set_disconnected(cdev);
592 }
593
594 /*
595  * Handle path verification event in offline state.
596  */
597 static void ccw_device_offline_verify(struct ccw_device *cdev,
598                                       enum dev_event dev_event)
599 {
600         struct subchannel *sch = to_subchannel(cdev->dev.parent);
601
602         css_schedule_eval(sch->schid);
603 }
604
605 /*
606  * Handle path verification event.
607  */
608 static void
609 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
610 {
611         struct subchannel *sch;
612
613         if (cdev->private->state == DEV_STATE_W4SENSE) {
614                 cdev->private->flags.doverify = 1;
615                 return;
616         }
617         sch = to_subchannel(cdev->dev.parent);
618         /*
619          * Since we might not just be coming from an interrupt from the
620          * subchannel we have to update the schib.
621          */
622         if (cio_update_schib(sch)) {
623                 ccw_device_verify_done(cdev, -ENODEV);
624                 return;
625         }
626
627         if (scsw_actl(&sch->schib.scsw) != 0 ||
628             (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
629             (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
630                 /*
631                  * No final status yet or final status not yet delivered
632                  * to the device driver. Can't do path verfication now,
633                  * delay until final status was delivered.
634                  */
635                 cdev->private->flags.doverify = 1;
636                 return;
637         }
638         /* Device is idle, we can do the path verification. */
639         cdev->private->state = DEV_STATE_VERIFY;
640         ccw_device_verify_start(cdev);
641 }
642
643 /*
644  * Got an interrupt for a normal io (state online).
645  */
646 static void
647 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
648 {
649         struct irb *irb;
650         int is_cmd;
651
652         irb = (struct irb *) __LC_IRB;
653         is_cmd = !scsw_is_tm(&irb->scsw);
654         /* Check for unsolicited interrupt. */
655         if (!scsw_is_solicited(&irb->scsw)) {
656                 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
657                     !irb->esw.esw0.erw.cons) {
658                         /* Unit check but no sense data. Need basic sense. */
659                         if (ccw_device_do_sense(cdev, irb) != 0)
660                                 goto call_handler_unsol;
661                         memcpy(&cdev->private->irb, irb, sizeof(struct irb));
662                         cdev->private->state = DEV_STATE_W4SENSE;
663                         cdev->private->intparm = 0;
664                         return;
665                 }
666 call_handler_unsol:
667                 if (cdev->handler)
668                         cdev->handler (cdev, 0, irb);
669                 if (cdev->private->flags.doverify)
670                         ccw_device_online_verify(cdev, 0);
671                 return;
672         }
673         /* Accumulate status and find out if a basic sense is needed. */
674         ccw_device_accumulate_irb(cdev, irb);
675         if (is_cmd && cdev->private->flags.dosense) {
676                 if (ccw_device_do_sense(cdev, irb) == 0) {
677                         cdev->private->state = DEV_STATE_W4SENSE;
678                 }
679                 return;
680         }
681         /* Call the handler. */
682         if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
683                 /* Start delayed path verification. */
684                 ccw_device_online_verify(cdev, 0);
685 }
686
687 /*
688  * Got an timeout in online state.
689  */
690 static void
691 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
692 {
693         int ret;
694
695         ccw_device_set_timeout(cdev, 0);
696         ret = ccw_device_cancel_halt_clear(cdev);
697         if (ret == -EBUSY) {
698                 ccw_device_set_timeout(cdev, 3*HZ);
699                 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
700                 return;
701         }
702         if (ret == -ENODEV)
703                 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
704         else if (cdev->handler)
705                 cdev->handler(cdev, cdev->private->intparm,
706                               ERR_PTR(-ETIMEDOUT));
707 }
708
709 /*
710  * Got an interrupt for a basic sense.
711  */
712 static void
713 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
714 {
715         struct irb *irb;
716
717         irb = (struct irb *) __LC_IRB;
718         /* Check for unsolicited interrupt. */
719         if (scsw_stctl(&irb->scsw) ==
720             (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
721                 if (scsw_cc(&irb->scsw) == 1)
722                         /* Basic sense hasn't started. Try again. */
723                         ccw_device_do_sense(cdev, irb);
724                 else {
725                         CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
726                                       "interrupt during w4sense...\n",
727                                       cdev->private->dev_id.ssid,
728                                       cdev->private->dev_id.devno);
729                         if (cdev->handler)
730                                 cdev->handler (cdev, 0, irb);
731                 }
732                 return;
733         }
734         /*
735          * Check if a halt or clear has been issued in the meanwhile. If yes,
736          * only deliver the halt/clear interrupt to the device driver as if it
737          * had killed the original request.
738          */
739         if (scsw_fctl(&irb->scsw) &
740             (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
741                 cdev->private->flags.dosense = 0;
742                 memset(&cdev->private->irb, 0, sizeof(struct irb));
743                 ccw_device_accumulate_irb(cdev, irb);
744                 goto call_handler;
745         }
746         /* Add basic sense info to irb. */
747         ccw_device_accumulate_basic_sense(cdev, irb);
748         if (cdev->private->flags.dosense) {
749                 /* Another basic sense is needed. */
750                 ccw_device_do_sense(cdev, irb);
751                 return;
752         }
753 call_handler:
754         cdev->private->state = DEV_STATE_ONLINE;
755         /* In case sensing interfered with setting the device online */
756         wake_up(&cdev->private->wait_q);
757         /* Call the handler. */
758         if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
759                 /* Start delayed path verification. */
760                 ccw_device_online_verify(cdev, 0);
761 }
762
763 static void
764 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
765 {
766         struct subchannel *sch;
767
768         sch = to_subchannel(cdev->dev.parent);
769         ccw_device_set_timeout(cdev, 0);
770         /* Start delayed path verification. */
771         ccw_device_online_verify(cdev, 0);
772         /* OK, i/o is dead now. Call interrupt handler. */
773         if (cdev->handler)
774                 cdev->handler(cdev, cdev->private->intparm,
775                               ERR_PTR(-EIO));
776 }
777
778 static void
779 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
780 {
781         int ret;
782
783         ret = ccw_device_cancel_halt_clear(cdev);
784         if (ret == -EBUSY) {
785                 ccw_device_set_timeout(cdev, 3*HZ);
786                 return;
787         }
788         /* Start delayed path verification. */
789         ccw_device_online_verify(cdev, 0);
790         if (cdev->handler)
791                 cdev->handler(cdev, cdev->private->intparm,
792                               ERR_PTR(-EIO));
793 }
794
795 void ccw_device_kill_io(struct ccw_device *cdev)
796 {
797         int ret;
798
799         ret = ccw_device_cancel_halt_clear(cdev);
800         if (ret == -EBUSY) {
801                 ccw_device_set_timeout(cdev, 3*HZ);
802                 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
803                 return;
804         }
805         /* Start delayed path verification. */
806         ccw_device_online_verify(cdev, 0);
807         if (cdev->handler)
808                 cdev->handler(cdev, cdev->private->intparm,
809                               ERR_PTR(-EIO));
810 }
811
812 static void
813 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
814 {
815         /* Start verification after current task finished. */
816         cdev->private->flags.doverify = 1;
817 }
818
819 static void
820 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
821 {
822         struct irb *irb;
823
824         switch (dev_event) {
825         case DEV_EVENT_INTERRUPT:
826                 irb = (struct irb *) __LC_IRB;
827                 /* Check for unsolicited interrupt. */
828                 if ((scsw_stctl(&irb->scsw) ==
829                      (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
830                     (!scsw_cc(&irb->scsw)))
831                         /* FIXME: we should restart stlck here, but this
832                          * is extremely unlikely ... */
833                         goto out_wakeup;
834
835                 ccw_device_accumulate_irb(cdev, irb);
836                 /* We don't care about basic sense etc. */
837                 break;
838         default: /* timeout */
839                 break;
840         }
841 out_wakeup:
842         wake_up(&cdev->private->wait_q);
843 }
844
845 static void
846 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
847 {
848         struct subchannel *sch;
849
850         sch = to_subchannel(cdev->dev.parent);
851         if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
852                 /* Couldn't enable the subchannel for i/o. Sick device. */
853                 return;
854         cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
855         ccw_device_sense_id_start(cdev);
856 }
857
858 void ccw_device_trigger_reprobe(struct ccw_device *cdev)
859 {
860         struct subchannel *sch;
861
862         if (cdev->private->state != DEV_STATE_DISCONNECTED)
863                 return;
864
865         sch = to_subchannel(cdev->dev.parent);
866         /* Update some values. */
867         if (cio_update_schib(sch))
868                 return;
869         /*
870          * The pim, pam, pom values may not be accurate, but they are the best
871          * we have before performing device selection :/
872          */
873         sch->lpm = sch->schib.pmcw.pam & sch->opm;
874         /*
875          * Use the initial configuration since we can't be shure that the old
876          * paths are valid.
877          */
878         io_subchannel_init_config(sch);
879         if (cio_commit_config(sch))
880                 return;
881
882         /* We should also udate ssd info, but this has to wait. */
883         /* Check if this is another device which appeared on the same sch. */
884         if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
885                 css_schedule_eval(sch->schid);
886         else
887                 ccw_device_start_id(cdev, 0);
888 }
889
890 static void ccw_device_disabled_irq(struct ccw_device *cdev,
891                                     enum dev_event dev_event)
892 {
893         struct subchannel *sch;
894
895         sch = to_subchannel(cdev->dev.parent);
896         /*
897          * An interrupt in a disabled state means a previous disable was not
898          * successful - should not happen, but we try to disable again.
899          */
900         cio_disable_subchannel(sch);
901 }
902
903 static void
904 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
905 {
906         retry_set_schib(cdev);
907         cdev->private->state = DEV_STATE_ONLINE;
908         dev_fsm_event(cdev, dev_event);
909 }
910
911 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
912                                        enum dev_event dev_event)
913 {
914         cmf_retry_copy_block(cdev);
915         cdev->private->state = DEV_STATE_ONLINE;
916         dev_fsm_event(cdev, dev_event);
917 }
918
919 static void
920 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
921 {
922         ccw_device_set_timeout(cdev, 0);
923         if (dev_event == DEV_EVENT_NOTOPER)
924                 cdev->private->state = DEV_STATE_NOT_OPER;
925         else
926                 cdev->private->state = DEV_STATE_OFFLINE;
927         wake_up(&cdev->private->wait_q);
928 }
929
930 static void
931 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
932 {
933         int ret;
934
935         ret = ccw_device_cancel_halt_clear(cdev);
936         switch (ret) {
937         case 0:
938                 cdev->private->state = DEV_STATE_OFFLINE;
939                 wake_up(&cdev->private->wait_q);
940                 break;
941         case -ENODEV:
942                 cdev->private->state = DEV_STATE_NOT_OPER;
943                 wake_up(&cdev->private->wait_q);
944                 break;
945         default:
946                 ccw_device_set_timeout(cdev, HZ/10);
947         }
948 }
949
950 /*
951  * No operation action. This is used e.g. to ignore a timeout event in
952  * state offline.
953  */
954 static void
955 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
956 {
957 }
958
959 /*
960  * device statemachine
961  */
962 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
963         [DEV_STATE_NOT_OPER] = {
964                 [DEV_EVENT_NOTOPER]     = ccw_device_nop,
965                 [DEV_EVENT_INTERRUPT]   = ccw_device_disabled_irq,
966                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
967                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
968         },
969         [DEV_STATE_SENSE_PGID] = {
970                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
971                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
972                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
973                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
974         },
975         [DEV_STATE_SENSE_ID] = {
976                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
977                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
978                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
979                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
980         },
981         [DEV_STATE_OFFLINE] = {
982                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
983                 [DEV_EVENT_INTERRUPT]   = ccw_device_disabled_irq,
984                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
985                 [DEV_EVENT_VERIFY]      = ccw_device_offline_verify,
986         },
987         [DEV_STATE_VERIFY] = {
988                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
989                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
990                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
991                 [DEV_EVENT_VERIFY]      = ccw_device_delay_verify,
992         },
993         [DEV_STATE_ONLINE] = {
994                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
995                 [DEV_EVENT_INTERRUPT]   = ccw_device_irq,
996                 [DEV_EVENT_TIMEOUT]     = ccw_device_online_timeout,
997                 [DEV_EVENT_VERIFY]      = ccw_device_online_verify,
998         },
999         [DEV_STATE_W4SENSE] = {
1000                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
1001                 [DEV_EVENT_INTERRUPT]   = ccw_device_w4sense,
1002                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1003                 [DEV_EVENT_VERIFY]      = ccw_device_online_verify,
1004         },
1005         [DEV_STATE_DISBAND_PGID] = {
1006                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
1007                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
1008                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
1009                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1010         },
1011         [DEV_STATE_BOXED] = {
1012                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
1013                 [DEV_EVENT_INTERRUPT]   = ccw_device_stlck_done,
1014                 [DEV_EVENT_TIMEOUT]     = ccw_device_stlck_done,
1015                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1016         },
1017         /* states to wait for i/o completion before doing something */
1018         [DEV_STATE_TIMEOUT_KILL] = {
1019                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
1020                 [DEV_EVENT_INTERRUPT]   = ccw_device_killing_irq,
1021                 [DEV_EVENT_TIMEOUT]     = ccw_device_killing_timeout,
1022                 [DEV_EVENT_VERIFY]      = ccw_device_nop, //FIXME
1023         },
1024         [DEV_STATE_QUIESCE] = {
1025                 [DEV_EVENT_NOTOPER]     = ccw_device_quiesce_done,
1026                 [DEV_EVENT_INTERRUPT]   = ccw_device_quiesce_done,
1027                 [DEV_EVENT_TIMEOUT]     = ccw_device_quiesce_timeout,
1028                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1029         },
1030         /* special states for devices gone not operational */
1031         [DEV_STATE_DISCONNECTED] = {
1032                 [DEV_EVENT_NOTOPER]     = ccw_device_nop,
1033                 [DEV_EVENT_INTERRUPT]   = ccw_device_start_id,
1034                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1035                 [DEV_EVENT_VERIFY]      = ccw_device_start_id,
1036         },
1037         [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1038                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
1039                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
1040                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
1041                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1042         },
1043         [DEV_STATE_CMFCHANGE] = {
1044                 [DEV_EVENT_NOTOPER]     = ccw_device_change_cmfstate,
1045                 [DEV_EVENT_INTERRUPT]   = ccw_device_change_cmfstate,
1046                 [DEV_EVENT_TIMEOUT]     = ccw_device_change_cmfstate,
1047                 [DEV_EVENT_VERIFY]      = ccw_device_change_cmfstate,
1048         },
1049         [DEV_STATE_CMFUPDATE] = {
1050                 [DEV_EVENT_NOTOPER]     = ccw_device_update_cmfblock,
1051                 [DEV_EVENT_INTERRUPT]   = ccw_device_update_cmfblock,
1052                 [DEV_EVENT_TIMEOUT]     = ccw_device_update_cmfblock,
1053                 [DEV_EVENT_VERIFY]      = ccw_device_update_cmfblock,
1054         },
1055 };
1056
1057 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);