Merge branch 'master'
[linux-2.6] / drivers / s390 / cio / device_fsm.c
1 /*
2  * drivers/s390/cio/device_fsm.c
3  * finite state machine for device handling
4  *
5  *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6  *                       IBM Corporation
7  *    Author(s): Cornelia Huck(cohuck@de.ibm.com)
8  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
9  */
10
11 #include <linux/module.h>
12 #include <linux/config.h>
13 #include <linux/init.h>
14 #include <linux/jiffies.h>
15 #include <linux/string.h>
16
17 #include <asm/ccwdev.h>
18 #include <asm/cio.h>
19
20 #include "cio.h"
21 #include "cio_debug.h"
22 #include "css.h"
23 #include "device.h"
24 #include "chsc.h"
25 #include "ioasm.h"
26
27 int
28 device_is_online(struct subchannel *sch)
29 {
30         struct ccw_device *cdev;
31
32         if (!sch->dev.driver_data)
33                 return 0;
34         cdev = sch->dev.driver_data;
35         return (cdev->private->state == DEV_STATE_ONLINE);
36 }
37
38 int
39 device_is_disconnected(struct subchannel *sch)
40 {
41         struct ccw_device *cdev;
42
43         if (!sch->dev.driver_data)
44                 return 0;
45         cdev = sch->dev.driver_data;
46         return (cdev->private->state == DEV_STATE_DISCONNECTED ||
47                 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
48 }
49
50 void
51 device_set_disconnected(struct subchannel *sch)
52 {
53         struct ccw_device *cdev;
54
55         if (!sch->dev.driver_data)
56                 return;
57         cdev = sch->dev.driver_data;
58         ccw_device_set_timeout(cdev, 0);
59         cdev->private->flags.fake_irb = 0;
60         cdev->private->state = DEV_STATE_DISCONNECTED;
61 }
62
63 void
64 device_set_waiting(struct subchannel *sch)
65 {
66         struct ccw_device *cdev;
67
68         if (!sch->dev.driver_data)
69                 return;
70         cdev = sch->dev.driver_data;
71         ccw_device_set_timeout(cdev, 10*HZ);
72         cdev->private->state = DEV_STATE_WAIT4IO;
73 }
74
75 /*
76  * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
77  */
78 static void
79 ccw_device_timeout(unsigned long data)
80 {
81         struct ccw_device *cdev;
82
83         cdev = (struct ccw_device *) data;
84         spin_lock_irq(cdev->ccwlock);
85         dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
86         spin_unlock_irq(cdev->ccwlock);
87 }
88
89 /*
90  * Set timeout
91  */
92 void
93 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
94 {
95         if (expires == 0) {
96                 del_timer(&cdev->private->timer);
97                 return;
98         }
99         if (timer_pending(&cdev->private->timer)) {
100                 if (mod_timer(&cdev->private->timer, jiffies + expires))
101                         return;
102         }
103         cdev->private->timer.function = ccw_device_timeout;
104         cdev->private->timer.data = (unsigned long) cdev;
105         cdev->private->timer.expires = jiffies + expires;
106         add_timer(&cdev->private->timer);
107 }
108
109 /* Kill any pending timers after machine check. */
110 void
111 device_kill_pending_timer(struct subchannel *sch)
112 {
113         struct ccw_device *cdev;
114
115         if (!sch->dev.driver_data)
116                 return;
117         cdev = sch->dev.driver_data;
118         ccw_device_set_timeout(cdev, 0);
119 }
120
121 /*
122  * Cancel running i/o. This is called repeatedly since halt/clear are
123  * asynchronous operations. We do one try with cio_cancel, two tries
124  * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
125  * Returns 0 if device now idle, -ENODEV for device not operational and
126  * -EBUSY if an interrupt is expected (either from halt/clear or from a
127  * status pending).
128  */
129 int
130 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
131 {
132         struct subchannel *sch;
133         int ret;
134
135         sch = to_subchannel(cdev->dev.parent);
136         ret = stsch(sch->irq, &sch->schib);
137         if (ret || !sch->schib.pmcw.dnv)
138                 return -ENODEV; 
139         if (!sch->schib.pmcw.ena || sch->schib.scsw.actl == 0)
140                 /* Not operational or no activity -> done. */
141                 return 0;
142         /* Stage 1: cancel io. */
143         if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
144             !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
145                 ret = cio_cancel(sch);
146                 if (ret != -EINVAL)
147                         return ret;
148                 /* cancel io unsuccessful. From now on it is asynchronous. */
149                 cdev->private->iretry = 3;      /* 3 halt retries. */
150         }
151         if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
152                 /* Stage 2: halt io. */
153                 if (cdev->private->iretry) {
154                         cdev->private->iretry--;
155                         ret = cio_halt(sch);
156                         return (ret == 0) ? -EBUSY : ret;
157                 }
158                 /* halt io unsuccessful. */
159                 cdev->private->iretry = 255;    /* 255 clear retries. */
160         }
161         /* Stage 3: clear io. */
162         if (cdev->private->iretry) {
163                 cdev->private->iretry--;
164                 ret = cio_clear (sch);
165                 return (ret == 0) ? -EBUSY : ret;
166         }
167         panic("Can't stop i/o on subchannel.\n");
168 }
169
170 static int
171 ccw_device_handle_oper(struct ccw_device *cdev)
172 {
173         struct subchannel *sch;
174
175         sch = to_subchannel(cdev->dev.parent);
176         cdev->private->flags.recog_done = 1;
177         /*
178          * Check if cu type and device type still match. If
179          * not, it is certainly another device and we have to
180          * de- and re-register. Also check here for non-matching devno.
181          */
182         if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
183             cdev->id.cu_model != cdev->private->senseid.cu_model ||
184             cdev->id.dev_type != cdev->private->senseid.dev_type ||
185             cdev->id.dev_model != cdev->private->senseid.dev_model ||
186             cdev->private->devno != sch->schib.pmcw.dev) {
187                 PREPARE_WORK(&cdev->private->kick_work,
188                              ccw_device_do_unreg_rereg, (void *)cdev);
189                 queue_work(ccw_device_work, &cdev->private->kick_work);
190                 return 0;
191         }
192         cdev->private->flags.donotify = 1;
193         return 1;
194 }
195
196 /*
197  * The machine won't give us any notification by machine check if a chpid has
198  * been varied online on the SE so we have to find out by magic (i. e. driving
199  * the channel subsystem to device selection and updating our path masks).
200  */
201 static inline void
202 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
203 {
204         int mask, i;
205
206         for (i = 0; i<8; i++) {
207                 mask = 0x80 >> i;
208                 if (!(sch->lpm & mask))
209                         continue;
210                 if (old_lpm & mask)
211                         continue;
212                 chpid_is_actually_online(sch->schib.pmcw.chpid[i]);
213         }
214 }
215
216 /*
217  * Stop device recognition.
218  */
219 static void
220 ccw_device_recog_done(struct ccw_device *cdev, int state)
221 {
222         struct subchannel *sch;
223         int notify, old_lpm, same_dev;
224
225         sch = to_subchannel(cdev->dev.parent);
226
227         ccw_device_set_timeout(cdev, 0);
228         cio_disable_subchannel(sch);
229         /*
230          * Now that we tried recognition, we have performed device selection
231          * through ssch() and the path information is up to date.
232          */
233         old_lpm = sch->lpm;
234         stsch(sch->irq, &sch->schib);
235         sch->lpm = sch->schib.pmcw.pim &
236                 sch->schib.pmcw.pam &
237                 sch->schib.pmcw.pom &
238                 sch->opm;
239         /* Check since device may again have become not operational. */
240         if (!sch->schib.pmcw.dnv)
241                 state = DEV_STATE_NOT_OPER;
242         if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
243                 /* Force reprobe on all chpids. */
244                 old_lpm = 0;
245         if (sch->lpm != old_lpm)
246                 __recover_lost_chpids(sch, old_lpm);
247         if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
248                 if (state == DEV_STATE_NOT_OPER) {
249                         cdev->private->flags.recog_done = 1;
250                         cdev->private->state = DEV_STATE_DISCONNECTED;
251                         return;
252                 }
253                 /* Boxed devices don't need extra treatment. */
254         }
255         notify = 0;
256         same_dev = 0; /* Keep the compiler quiet... */
257         switch (state) {
258         case DEV_STATE_NOT_OPER:
259                 CIO_DEBUG(KERN_WARNING, 2,
260                           "SenseID : unknown device %04x on subchannel %04x\n",
261                           cdev->private->devno, sch->irq);
262                 break;
263         case DEV_STATE_OFFLINE:
264                 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
265                         same_dev = ccw_device_handle_oper(cdev);
266                         notify = 1;
267                 }
268                 /* fill out sense information */
269                 cdev->id = (struct ccw_device_id) {
270                         .cu_type   = cdev->private->senseid.cu_type,
271                         .cu_model  = cdev->private->senseid.cu_model,
272                         .dev_type  = cdev->private->senseid.dev_type,
273                         .dev_model = cdev->private->senseid.dev_model,
274                 };
275                 if (notify) {
276                         cdev->private->state = DEV_STATE_OFFLINE;
277                         if (same_dev) {
278                                 /* Get device online again. */
279                                 ccw_device_online(cdev);
280                                 wake_up(&cdev->private->wait_q);
281                         }
282                         return;
283                 }
284                 /* Issue device info message. */
285                 CIO_DEBUG(KERN_INFO, 2, "SenseID : device %04x reports: "
286                           "CU  Type/Mod = %04X/%02X, Dev Type/Mod = "
287                           "%04X/%02X\n", cdev->private->devno,
288                           cdev->id.cu_type, cdev->id.cu_model,
289                           cdev->id.dev_type, cdev->id.dev_model);
290                 break;
291         case DEV_STATE_BOXED:
292                 CIO_DEBUG(KERN_WARNING, 2,
293                           "SenseID : boxed device %04x on subchannel %04x\n",
294                           cdev->private->devno, sch->irq);
295                 break;
296         }
297         cdev->private->state = state;
298         io_subchannel_recog_done(cdev);
299         if (state != DEV_STATE_NOT_OPER)
300                 wake_up(&cdev->private->wait_q);
301 }
302
303 /*
304  * Function called from device_id.c after sense id has completed.
305  */
306 void
307 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
308 {
309         switch (err) {
310         case 0:
311                 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
312                 break;
313         case -ETIME:            /* Sense id stopped by timeout. */
314                 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
315                 break;
316         default:
317                 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
318                 break;
319         }
320 }
321
322 static void
323 ccw_device_oper_notify(void *data)
324 {
325         struct ccw_device *cdev;
326         struct subchannel *sch;
327         int ret;
328
329         cdev = (struct ccw_device *)data;
330         sch = to_subchannel(cdev->dev.parent);
331         ret = (sch->driver && sch->driver->notify) ?
332                 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
333         if (!ret)
334                 /* Driver doesn't want device back. */
335                 ccw_device_do_unreg_rereg((void *)cdev);
336         else
337                 wake_up(&cdev->private->wait_q);
338 }
339
340 /*
341  * Finished with online/offline processing.
342  */
343 static void
344 ccw_device_done(struct ccw_device *cdev, int state)
345 {
346         struct subchannel *sch;
347
348         sch = to_subchannel(cdev->dev.parent);
349
350         if (state != DEV_STATE_ONLINE)
351                 cio_disable_subchannel(sch);
352
353         /* Reset device status. */
354         memset(&cdev->private->irb, 0, sizeof(struct irb));
355
356         cdev->private->state = state;
357
358
359         if (state == DEV_STATE_BOXED)
360                 CIO_DEBUG(KERN_WARNING, 2,
361                           "Boxed device %04x on subchannel %04x\n",
362                           cdev->private->devno, sch->irq);
363
364         if (cdev->private->flags.donotify) {
365                 cdev->private->flags.donotify = 0;
366                 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify,
367                              (void *)cdev);
368                 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
369         }
370         wake_up(&cdev->private->wait_q);
371
372         if (css_init_done && state != DEV_STATE_ONLINE)
373                 put_device (&cdev->dev);
374 }
375
376 /*
377  * Function called from device_pgid.c after sense path ground has completed.
378  */
379 void
380 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
381 {
382         struct subchannel *sch;
383
384         sch = to_subchannel(cdev->dev.parent);
385         switch (err) {
386         case 0:
387                 /* Start Path Group verification. */
388                 sch->vpm = 0;   /* Start with no path groups set. */
389                 cdev->private->state = DEV_STATE_VERIFY;
390                 ccw_device_verify_start(cdev);
391                 break;
392         case -ETIME:            /* Sense path group id stopped by timeout. */
393         case -EUSERS:           /* device is reserved for someone else. */
394                 ccw_device_done(cdev, DEV_STATE_BOXED);
395                 break;
396         case -EOPNOTSUPP: /* path grouping not supported, just set online. */
397                 cdev->private->options.pgroup = 0;
398                 ccw_device_done(cdev, DEV_STATE_ONLINE);
399                 break;
400         default:
401                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
402                 break;
403         }
404 }
405
406 /*
407  * Start device recognition.
408  */
409 int
410 ccw_device_recognition(struct ccw_device *cdev)
411 {
412         struct subchannel *sch;
413         int ret;
414
415         if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
416             (cdev->private->state != DEV_STATE_BOXED))
417                 return -EINVAL;
418         sch = to_subchannel(cdev->dev.parent);
419         ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
420         if (ret != 0)
421                 /* Couldn't enable the subchannel for i/o. Sick device. */
422                 return ret;
423
424         /* After 60s the device recognition is considered to have failed. */
425         ccw_device_set_timeout(cdev, 60*HZ);
426
427         /*
428          * We used to start here with a sense pgid to find out whether a device
429          * is locked by someone else. Unfortunately, the sense pgid command
430          * code has other meanings on devices predating the path grouping
431          * algorithm, so we start with sense id and box the device after an
432          * timeout (or if sense pgid during path verification detects the device
433          * is locked, as may happen on newer devices).
434          */
435         cdev->private->flags.recog_done = 0;
436         cdev->private->state = DEV_STATE_SENSE_ID;
437         ccw_device_sense_id_start(cdev);
438         return 0;
439 }
440
441 /*
442  * Handle timeout in device recognition.
443  */
444 static void
445 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
446 {
447         int ret;
448
449         ret = ccw_device_cancel_halt_clear(cdev);
450         switch (ret) {
451         case 0:
452                 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
453                 break;
454         case -ENODEV:
455                 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
456                 break;
457         default:
458                 ccw_device_set_timeout(cdev, 3*HZ);
459         }
460 }
461
462
463 static void
464 ccw_device_nopath_notify(void *data)
465 {
466         struct ccw_device *cdev;
467         struct subchannel *sch;
468         int ret;
469
470         cdev = (struct ccw_device *)data;
471         sch = to_subchannel(cdev->dev.parent);
472         /* Extra sanity. */
473         if (sch->lpm)
474                 return;
475         ret = (sch->driver && sch->driver->notify) ?
476                 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
477         if (!ret) {
478                 if (get_device(&sch->dev)) {
479                         /* Driver doesn't want to keep device. */
480                         cio_disable_subchannel(sch);
481                         if (get_device(&cdev->dev)) {
482                                 PREPARE_WORK(&cdev->private->kick_work,
483                                              ccw_device_call_sch_unregister,
484                                              (void *)cdev);
485                                 queue_work(ccw_device_work,
486                                            &cdev->private->kick_work);
487                         } else
488                                 put_device(&sch->dev);
489                 }
490         } else {
491                 cio_disable_subchannel(sch);
492                 ccw_device_set_timeout(cdev, 0);
493                 cdev->private->flags.fake_irb = 0;
494                 cdev->private->state = DEV_STATE_DISCONNECTED;
495                 wake_up(&cdev->private->wait_q);
496         }
497 }
498
499 void
500 ccw_device_verify_done(struct ccw_device *cdev, int err)
501 {
502         cdev->private->flags.doverify = 0;
503         switch (err) {
504         case -EOPNOTSUPP: /* path grouping not supported, just set online. */
505                 cdev->private->options.pgroup = 0;
506         case 0:
507                 ccw_device_done(cdev, DEV_STATE_ONLINE);
508                 /* Deliver fake irb to device driver, if needed. */
509                 if (cdev->private->flags.fake_irb) {
510                         memset(&cdev->private->irb, 0, sizeof(struct irb));
511                         cdev->private->irb.scsw = (struct scsw) {
512                                 .cc = 1,
513                                 .fctl = SCSW_FCTL_START_FUNC,
514                                 .actl = SCSW_ACTL_START_PEND,
515                                 .stctl = SCSW_STCTL_STATUS_PEND,
516                         };
517                         cdev->private->flags.fake_irb = 0;
518                         if (cdev->handler)
519                                 cdev->handler(cdev, cdev->private->intparm,
520                                               &cdev->private->irb);
521                         memset(&cdev->private->irb, 0, sizeof(struct irb));
522                 }
523                 break;
524         case -ETIME:
525                 ccw_device_done(cdev, DEV_STATE_BOXED);
526                 break;
527         default:
528                 PREPARE_WORK(&cdev->private->kick_work,
529                              ccw_device_nopath_notify, (void *)cdev);
530                 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
531                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
532                 break;
533         }
534 }
535
536 /*
537  * Get device online.
538  */
539 int
540 ccw_device_online(struct ccw_device *cdev)
541 {
542         struct subchannel *sch;
543         int ret;
544
545         if ((cdev->private->state != DEV_STATE_OFFLINE) &&
546             (cdev->private->state != DEV_STATE_BOXED))
547                 return -EINVAL;
548         sch = to_subchannel(cdev->dev.parent);
549         if (css_init_done && !get_device(&cdev->dev))
550                 return -ENODEV;
551         ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
552         if (ret != 0) {
553                 /* Couldn't enable the subchannel for i/o. Sick device. */
554                 if (ret == -ENODEV)
555                         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
556                 return ret;
557         }
558         /* Do we want to do path grouping? */
559         if (!cdev->private->options.pgroup) {
560                 /* No, set state online immediately. */
561                 ccw_device_done(cdev, DEV_STATE_ONLINE);
562                 return 0;
563         }
564         /* Do a SensePGID first. */
565         cdev->private->state = DEV_STATE_SENSE_PGID;
566         ccw_device_sense_pgid_start(cdev);
567         return 0;
568 }
569
570 void
571 ccw_device_disband_done(struct ccw_device *cdev, int err)
572 {
573         switch (err) {
574         case 0:
575                 ccw_device_done(cdev, DEV_STATE_OFFLINE);
576                 break;
577         case -ETIME:
578                 ccw_device_done(cdev, DEV_STATE_BOXED);
579                 break;
580         default:
581                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
582                 break;
583         }
584 }
585
586 /*
587  * Shutdown device.
588  */
589 int
590 ccw_device_offline(struct ccw_device *cdev)
591 {
592         struct subchannel *sch;
593
594         sch = to_subchannel(cdev->dev.parent);
595         if (stsch(sch->irq, &sch->schib) || !sch->schib.pmcw.dnv)
596                 return -ENODEV;
597         if (cdev->private->state != DEV_STATE_ONLINE) {
598                 if (sch->schib.scsw.actl != 0)
599                         return -EBUSY;
600                 return -EINVAL;
601         }
602         if (sch->schib.scsw.actl != 0)
603                 return -EBUSY;
604         /* Are we doing path grouping? */
605         if (!cdev->private->options.pgroup) {
606                 /* No, set state offline immediately. */
607                 ccw_device_done(cdev, DEV_STATE_OFFLINE);
608                 return 0;
609         }
610         /* Start Set Path Group commands. */
611         cdev->private->state = DEV_STATE_DISBAND_PGID;
612         ccw_device_disband_start(cdev);
613         return 0;
614 }
615
616 /*
617  * Handle timeout in device online/offline process.
618  */
619 static void
620 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
621 {
622         int ret;
623
624         ret = ccw_device_cancel_halt_clear(cdev);
625         switch (ret) {
626         case 0:
627                 ccw_device_done(cdev, DEV_STATE_BOXED);
628                 break;
629         case -ENODEV:
630                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
631                 break;
632         default:
633                 ccw_device_set_timeout(cdev, 3*HZ);
634         }
635 }
636
637 /*
638  * Handle not oper event in device recognition.
639  */
640 static void
641 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
642 {
643         ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
644 }
645
646 /*
647  * Handle not operational event while offline.
648  */
649 static void
650 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
651 {
652         struct subchannel *sch;
653
654         cdev->private->state = DEV_STATE_NOT_OPER;
655         sch = to_subchannel(cdev->dev.parent);
656         if (get_device(&cdev->dev)) {
657                 PREPARE_WORK(&cdev->private->kick_work,
658                              ccw_device_call_sch_unregister, (void *)cdev);
659                 queue_work(ccw_device_work, &cdev->private->kick_work);
660         }
661         wake_up(&cdev->private->wait_q);
662 }
663
664 /*
665  * Handle not operational event while online.
666  */
667 static void
668 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
669 {
670         struct subchannel *sch;
671
672         sch = to_subchannel(cdev->dev.parent);
673         if (sch->driver->notify &&
674             sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
675                         ccw_device_set_timeout(cdev, 0);
676                         cdev->private->flags.fake_irb = 0;
677                         cdev->private->state = DEV_STATE_DISCONNECTED;
678                         wake_up(&cdev->private->wait_q);
679                         return;
680         }
681         cdev->private->state = DEV_STATE_NOT_OPER;
682         cio_disable_subchannel(sch);
683         if (sch->schib.scsw.actl != 0) {
684                 // FIXME: not-oper indication to device driver ?
685                 ccw_device_call_handler(cdev);
686         }
687         if (get_device(&cdev->dev)) {
688                 PREPARE_WORK(&cdev->private->kick_work,
689                              ccw_device_call_sch_unregister, (void *)cdev);
690                 queue_work(ccw_device_work, &cdev->private->kick_work);
691         }
692         wake_up(&cdev->private->wait_q);
693 }
694
695 /*
696  * Handle path verification event.
697  */
698 static void
699 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
700 {
701         struct subchannel *sch;
702
703         if (!cdev->private->options.pgroup)
704                 return;
705         if (cdev->private->state == DEV_STATE_W4SENSE) {
706                 cdev->private->flags.doverify = 1;
707                 return;
708         }
709         sch = to_subchannel(cdev->dev.parent);
710         /*
711          * Since we might not just be coming from an interrupt from the
712          * subchannel we have to update the schib.
713          */
714         stsch(sch->irq, &sch->schib);
715
716         if (sch->schib.scsw.actl != 0 ||
717             (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
718                 /*
719                  * No final status yet or final status not yet delivered
720                  * to the device driver. Can't do path verfication now,
721                  * delay until final status was delivered.
722                  */
723                 cdev->private->flags.doverify = 1;
724                 return;
725         }
726         /* Device is idle, we can do the path verification. */
727         cdev->private->state = DEV_STATE_VERIFY;
728         ccw_device_verify_start(cdev);
729 }
730
731 /*
732  * Got an interrupt for a normal io (state online).
733  */
734 static void
735 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
736 {
737         struct irb *irb;
738
739         irb = (struct irb *) __LC_IRB;
740         /* Check for unsolicited interrupt. */
741         if ((irb->scsw.stctl ==
742                         (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
743             && (!irb->scsw.cc)) {
744                 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
745                     !irb->esw.esw0.erw.cons) {
746                         /* Unit check but no sense data. Need basic sense. */
747                         if (ccw_device_do_sense(cdev, irb) != 0)
748                                 goto call_handler_unsol;
749                         memcpy(irb, &cdev->private->irb, sizeof(struct irb));
750                         cdev->private->state = DEV_STATE_W4SENSE;
751                         cdev->private->intparm = 0;
752                         return;
753                 }
754 call_handler_unsol:
755                 if (cdev->handler)
756                         cdev->handler (cdev, 0, irb);
757                 return;
758         }
759         /* Accumulate status and find out if a basic sense is needed. */
760         ccw_device_accumulate_irb(cdev, irb);
761         if (cdev->private->flags.dosense) {
762                 if (ccw_device_do_sense(cdev, irb) == 0) {
763                         cdev->private->state = DEV_STATE_W4SENSE;
764                 }
765                 return;
766         }
767         /* Call the handler. */
768         if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
769                 /* Start delayed path verification. */
770                 ccw_device_online_verify(cdev, 0);
771 }
772
773 /*
774  * Got an timeout in online state.
775  */
776 static void
777 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
778 {
779         int ret;
780
781         ccw_device_set_timeout(cdev, 0);
782         ret = ccw_device_cancel_halt_clear(cdev);
783         if (ret == -EBUSY) {
784                 ccw_device_set_timeout(cdev, 3*HZ);
785                 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
786                 return;
787         }
788         if (ret == -ENODEV) {
789                 struct subchannel *sch;
790
791                 sch = to_subchannel(cdev->dev.parent);
792                 if (!sch->lpm) {
793                         PREPARE_WORK(&cdev->private->kick_work,
794                                      ccw_device_nopath_notify, (void *)cdev);
795                         queue_work(ccw_device_notify_work,
796                                    &cdev->private->kick_work);
797                 } else
798                         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
799         } else if (cdev->handler)
800                 cdev->handler(cdev, cdev->private->intparm,
801                               ERR_PTR(-ETIMEDOUT));
802 }
803
804 /*
805  * Got an interrupt for a basic sense.
806  */
807 void
808 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
809 {
810         struct irb *irb;
811
812         irb = (struct irb *) __LC_IRB;
813         /* Check for unsolicited interrupt. */
814         if (irb->scsw.stctl ==
815                         (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
816                 if (irb->scsw.cc == 1)
817                         /* Basic sense hasn't started. Try again. */
818                         ccw_device_do_sense(cdev, irb);
819                 else {
820                         printk("Huh? %s(%s): unsolicited interrupt...\n",
821                                __FUNCTION__, cdev->dev.bus_id);
822                         if (cdev->handler)
823                                 cdev->handler (cdev, 0, irb);
824                 }
825                 return;
826         }
827         /* Add basic sense info to irb. */
828         ccw_device_accumulate_basic_sense(cdev, irb);
829         if (cdev->private->flags.dosense) {
830                 /* Another basic sense is needed. */
831                 ccw_device_do_sense(cdev, irb);
832                 return;
833         }
834         cdev->private->state = DEV_STATE_ONLINE;
835         /* Call the handler. */
836         if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
837                 /* Start delayed path verification. */
838                 ccw_device_online_verify(cdev, 0);
839 }
840
841 static void
842 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
843 {
844         struct irb *irb;
845
846         irb = (struct irb *) __LC_IRB;
847         /* Accumulate status. We don't do basic sense. */
848         ccw_device_accumulate_irb(cdev, irb);
849         /* Try to start delayed device verification. */
850         ccw_device_online_verify(cdev, 0);
851         /* Note: Don't call handler for cio initiated clear! */
852 }
853
854 static void
855 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
856 {
857         struct subchannel *sch;
858
859         sch = to_subchannel(cdev->dev.parent);
860         ccw_device_set_timeout(cdev, 0);
861         /* OK, i/o is dead now. Call interrupt handler. */
862         cdev->private->state = DEV_STATE_ONLINE;
863         if (cdev->handler)
864                 cdev->handler(cdev, cdev->private->intparm,
865                               ERR_PTR(-ETIMEDOUT));
866         if (!sch->lpm) {
867                 PREPARE_WORK(&cdev->private->kick_work,
868                              ccw_device_nopath_notify, (void *)cdev);
869                 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
870         } else if (cdev->private->flags.doverify)
871                 /* Start delayed path verification. */
872                 ccw_device_online_verify(cdev, 0);
873 }
874
875 static void
876 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
877 {
878         int ret;
879
880         ret = ccw_device_cancel_halt_clear(cdev);
881         if (ret == -EBUSY) {
882                 ccw_device_set_timeout(cdev, 3*HZ);
883                 return;
884         }
885         if (ret == -ENODEV) {
886                 struct subchannel *sch;
887
888                 sch = to_subchannel(cdev->dev.parent);
889                 if (!sch->lpm) {
890                         PREPARE_WORK(&cdev->private->kick_work,
891                                      ccw_device_nopath_notify, (void *)cdev);
892                         queue_work(ccw_device_notify_work,
893                                    &cdev->private->kick_work);
894                 } else
895                         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
896                 return;
897         }
898         //FIXME: Can we get here?
899         cdev->private->state = DEV_STATE_ONLINE;
900         if (cdev->handler)
901                 cdev->handler(cdev, cdev->private->intparm,
902                               ERR_PTR(-ETIMEDOUT));
903 }
904
905 static void
906 ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event)
907 {
908         struct irb *irb;
909         struct subchannel *sch;
910
911         irb = (struct irb *) __LC_IRB;
912         /*
913          * Accumulate status and find out if a basic sense is needed.
914          * This is fine since we have already adapted the lpm.
915          */
916         ccw_device_accumulate_irb(cdev, irb);
917         if (cdev->private->flags.dosense) {
918                 if (ccw_device_do_sense(cdev, irb) == 0) {
919                         cdev->private->state = DEV_STATE_W4SENSE;
920                 }
921                 return;
922         }
923
924         /* Iff device is idle, reset timeout. */
925         sch = to_subchannel(cdev->dev.parent);
926         if (!stsch(sch->irq, &sch->schib))
927                 if (sch->schib.scsw.actl == 0)
928                         ccw_device_set_timeout(cdev, 0);
929         /* Call the handler. */
930         ccw_device_call_handler(cdev);
931         if (!sch->lpm) {
932                 PREPARE_WORK(&cdev->private->kick_work,
933                              ccw_device_nopath_notify, (void *)cdev);
934                 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
935         } else if (cdev->private->flags.doverify)
936                 ccw_device_online_verify(cdev, 0);
937 }
938
939 static void
940 ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event)
941 {
942         int ret;
943         struct subchannel *sch;
944
945         sch = to_subchannel(cdev->dev.parent);
946         ccw_device_set_timeout(cdev, 0);
947         ret = ccw_device_cancel_halt_clear(cdev);
948         if (ret == -EBUSY) {
949                 ccw_device_set_timeout(cdev, 3*HZ);
950                 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
951                 return;
952         }
953         if (ret == -ENODEV) {
954                 if (!sch->lpm) {
955                         PREPARE_WORK(&cdev->private->kick_work,
956                                      ccw_device_nopath_notify, (void *)cdev);
957                         queue_work(ccw_device_notify_work,
958                                    &cdev->private->kick_work);
959                 } else
960                         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
961                 return;
962         }
963         if (cdev->handler)
964                 cdev->handler(cdev, cdev->private->intparm,
965                               ERR_PTR(-ETIMEDOUT));
966         if (!sch->lpm) {
967                 PREPARE_WORK(&cdev->private->kick_work,
968                              ccw_device_nopath_notify, (void *)cdev);
969                 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
970         } else if (cdev->private->flags.doverify)
971                 /* Start delayed path verification. */
972                 ccw_device_online_verify(cdev, 0);
973 }
974
975 static void
976 ccw_device_wait4io_verify(struct ccw_device *cdev, enum dev_event dev_event)
977 {
978         /* When the I/O has terminated, we have to start verification. */
979         if (cdev->private->options.pgroup)
980                 cdev->private->flags.doverify = 1;
981 }
982
983 static void
984 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
985 {
986         struct irb *irb;
987
988         switch (dev_event) {
989         case DEV_EVENT_INTERRUPT:
990                 irb = (struct irb *) __LC_IRB;
991                 /* Check for unsolicited interrupt. */
992                 if ((irb->scsw.stctl ==
993                      (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
994                     (!irb->scsw.cc))
995                         /* FIXME: we should restart stlck here, but this
996                          * is extremely unlikely ... */
997                         goto out_wakeup;
998
999                 ccw_device_accumulate_irb(cdev, irb);
1000                 /* We don't care about basic sense etc. */
1001                 break;
1002         default: /* timeout */
1003                 break;
1004         }
1005 out_wakeup:
1006         wake_up(&cdev->private->wait_q);
1007 }
1008
1009 static void
1010 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1011 {
1012         struct subchannel *sch;
1013
1014         sch = to_subchannel(cdev->dev.parent);
1015         if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1016                 /* Couldn't enable the subchannel for i/o. Sick device. */
1017                 return;
1018
1019         /* After 60s the device recognition is considered to have failed. */
1020         ccw_device_set_timeout(cdev, 60*HZ);
1021
1022         cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1023         ccw_device_sense_id_start(cdev);
1024 }
1025
1026 void
1027 device_trigger_reprobe(struct subchannel *sch)
1028 {
1029         struct ccw_device *cdev;
1030
1031         if (!sch->dev.driver_data)
1032                 return;
1033         cdev = sch->dev.driver_data;
1034         if (cdev->private->state != DEV_STATE_DISCONNECTED)
1035                 return;
1036
1037         /* Update some values. */
1038         if (stsch(sch->irq, &sch->schib))
1039                 return;
1040
1041         /*
1042          * The pim, pam, pom values may not be accurate, but they are the best
1043          * we have before performing device selection :/
1044          */
1045         sch->lpm = sch->schib.pmcw.pim &
1046                 sch->schib.pmcw.pam &
1047                 sch->schib.pmcw.pom &
1048                 sch->opm;
1049         /* Re-set some bits in the pmcw that were lost. */
1050         sch->schib.pmcw.isc = 3;
1051         sch->schib.pmcw.csense = 1;
1052         sch->schib.pmcw.ena = 0;
1053         if ((sch->lpm & (sch->lpm - 1)) != 0)
1054                 sch->schib.pmcw.mp = 1;
1055         sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1056         /* We should also udate ssd info, but this has to wait. */
1057         ccw_device_start_id(cdev, 0);
1058 }
1059
1060 static void
1061 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1062 {
1063         struct subchannel *sch;
1064
1065         sch = to_subchannel(cdev->dev.parent);
1066         /*
1067          * An interrupt in state offline means a previous disable was not
1068          * successful. Try again.
1069          */
1070         cio_disable_subchannel(sch);
1071 }
1072
1073 static void
1074 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1075 {
1076         retry_set_schib(cdev);
1077         cdev->private->state = DEV_STATE_ONLINE;
1078         dev_fsm_event(cdev, dev_event);
1079 }
1080
1081
1082 static void
1083 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1084 {
1085         ccw_device_set_timeout(cdev, 0);
1086         if (dev_event == DEV_EVENT_NOTOPER)
1087                 cdev->private->state = DEV_STATE_NOT_OPER;
1088         else
1089                 cdev->private->state = DEV_STATE_OFFLINE;
1090         wake_up(&cdev->private->wait_q);
1091 }
1092
1093 static void
1094 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1095 {
1096         int ret;
1097
1098         ret = ccw_device_cancel_halt_clear(cdev);
1099         switch (ret) {
1100         case 0:
1101                 cdev->private->state = DEV_STATE_OFFLINE;
1102                 wake_up(&cdev->private->wait_q);
1103                 break;
1104         case -ENODEV:
1105                 cdev->private->state = DEV_STATE_NOT_OPER;
1106                 wake_up(&cdev->private->wait_q);
1107                 break;
1108         default:
1109                 ccw_device_set_timeout(cdev, HZ/10);
1110         }
1111 }
1112
1113 /*
1114  * No operation action. This is used e.g. to ignore a timeout event in
1115  * state offline.
1116  */
1117 static void
1118 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1119 {
1120 }
1121
1122 /*
1123  * Bug operation action. 
1124  */
1125 static void
1126 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1127 {
1128         printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1129                cdev->private->state, dev_event);
1130         BUG();
1131 }
1132
1133 /*
1134  * device statemachine
1135  */
1136 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1137         [DEV_STATE_NOT_OPER] = {
1138                 [DEV_EVENT_NOTOPER]     = ccw_device_nop,
1139                 [DEV_EVENT_INTERRUPT]   = ccw_device_bug,
1140                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1141                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1142         },
1143         [DEV_STATE_SENSE_PGID] = {
1144                 [DEV_EVENT_NOTOPER]     = ccw_device_online_notoper,
1145                 [DEV_EVENT_INTERRUPT]   = ccw_device_sense_pgid_irq,
1146                 [DEV_EVENT_TIMEOUT]     = ccw_device_onoff_timeout,
1147                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1148         },
1149         [DEV_STATE_SENSE_ID] = {
1150                 [DEV_EVENT_NOTOPER]     = ccw_device_recog_notoper,
1151                 [DEV_EVENT_INTERRUPT]   = ccw_device_sense_id_irq,
1152                 [DEV_EVENT_TIMEOUT]     = ccw_device_recog_timeout,
1153                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1154         },
1155         [DEV_STATE_OFFLINE] = {
1156                 [DEV_EVENT_NOTOPER]     = ccw_device_offline_notoper,
1157                 [DEV_EVENT_INTERRUPT]   = ccw_device_offline_irq,
1158                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1159                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1160         },
1161         [DEV_STATE_VERIFY] = {
1162                 [DEV_EVENT_NOTOPER]     = ccw_device_online_notoper,
1163                 [DEV_EVENT_INTERRUPT]   = ccw_device_verify_irq,
1164                 [DEV_EVENT_TIMEOUT]     = ccw_device_onoff_timeout,
1165                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1166         },
1167         [DEV_STATE_ONLINE] = {
1168                 [DEV_EVENT_NOTOPER]     = ccw_device_online_notoper,
1169                 [DEV_EVENT_INTERRUPT]   = ccw_device_irq,
1170                 [DEV_EVENT_TIMEOUT]     = ccw_device_online_timeout,
1171                 [DEV_EVENT_VERIFY]      = ccw_device_online_verify,
1172         },
1173         [DEV_STATE_W4SENSE] = {
1174                 [DEV_EVENT_NOTOPER]     = ccw_device_online_notoper,
1175                 [DEV_EVENT_INTERRUPT]   = ccw_device_w4sense,
1176                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1177                 [DEV_EVENT_VERIFY]      = ccw_device_online_verify,
1178         },
1179         [DEV_STATE_DISBAND_PGID] = {
1180                 [DEV_EVENT_NOTOPER]     = ccw_device_online_notoper,
1181                 [DEV_EVENT_INTERRUPT]   = ccw_device_disband_irq,
1182                 [DEV_EVENT_TIMEOUT]     = ccw_device_onoff_timeout,
1183                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1184         },
1185         [DEV_STATE_BOXED] = {
1186                 [DEV_EVENT_NOTOPER]     = ccw_device_offline_notoper,
1187                 [DEV_EVENT_INTERRUPT]   = ccw_device_stlck_done,
1188                 [DEV_EVENT_TIMEOUT]     = ccw_device_stlck_done,
1189                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1190         },
1191         /* states to wait for i/o completion before doing something */
1192         [DEV_STATE_CLEAR_VERIFY] = {
1193                 [DEV_EVENT_NOTOPER]     = ccw_device_online_notoper,
1194                 [DEV_EVENT_INTERRUPT]   = ccw_device_clear_verify,
1195                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1196                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1197         },
1198         [DEV_STATE_TIMEOUT_KILL] = {
1199                 [DEV_EVENT_NOTOPER]     = ccw_device_online_notoper,
1200                 [DEV_EVENT_INTERRUPT]   = ccw_device_killing_irq,
1201                 [DEV_EVENT_TIMEOUT]     = ccw_device_killing_timeout,
1202                 [DEV_EVENT_VERIFY]      = ccw_device_nop, //FIXME
1203         },
1204         [DEV_STATE_WAIT4IO] = {
1205                 [DEV_EVENT_NOTOPER]     = ccw_device_online_notoper,
1206                 [DEV_EVENT_INTERRUPT]   = ccw_device_wait4io_irq,
1207                 [DEV_EVENT_TIMEOUT]     = ccw_device_wait4io_timeout,
1208                 [DEV_EVENT_VERIFY]      = ccw_device_wait4io_verify,
1209         },
1210         [DEV_STATE_QUIESCE] = {
1211                 [DEV_EVENT_NOTOPER]     = ccw_device_quiesce_done,
1212                 [DEV_EVENT_INTERRUPT]   = ccw_device_quiesce_done,
1213                 [DEV_EVENT_TIMEOUT]     = ccw_device_quiesce_timeout,
1214                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1215         },
1216         /* special states for devices gone not operational */
1217         [DEV_STATE_DISCONNECTED] = {
1218                 [DEV_EVENT_NOTOPER]     = ccw_device_nop,
1219                 [DEV_EVENT_INTERRUPT]   = ccw_device_start_id,
1220                 [DEV_EVENT_TIMEOUT]     = ccw_device_bug,
1221                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1222         },
1223         [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1224                 [DEV_EVENT_NOTOPER]     = ccw_device_recog_notoper,
1225                 [DEV_EVENT_INTERRUPT]   = ccw_device_sense_id_irq,
1226                 [DEV_EVENT_TIMEOUT]     = ccw_device_recog_timeout,
1227                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1228         },
1229         [DEV_STATE_CMFCHANGE] = {
1230                 [DEV_EVENT_NOTOPER]     = ccw_device_change_cmfstate,
1231                 [DEV_EVENT_INTERRUPT]   = ccw_device_change_cmfstate,
1232                 [DEV_EVENT_TIMEOUT]     = ccw_device_change_cmfstate,
1233                 [DEV_EVENT_VERIFY]      = ccw_device_change_cmfstate,
1234         },
1235 };
1236
1237 /*
1238  * io_subchannel_irq is called for "real" interrupts or for status
1239  * pending conditions on msch.
1240  */
1241 void
1242 io_subchannel_irq (struct device *pdev)
1243 {
1244         struct ccw_device *cdev;
1245
1246         cdev = to_subchannel(pdev)->dev.driver_data;
1247
1248         CIO_TRACE_EVENT (3, "IRQ");
1249         CIO_TRACE_EVENT (3, pdev->bus_id);
1250         if (cdev)
1251                 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1252 }
1253
1254 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);