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