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