]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/s390/cio/device_fsm.c
Fix common misspellings
[linux-3.10.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_err(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         /* Function was unsuccessful */
178         CIO_MSG_EVENT(0, "0.%x.%04x: could not stop I/O\n",
179                       cdev->private->dev_id.ssid, cdev->private->dev_id.devno);
180         return -EIO;
181 }
182
183 void ccw_device_update_sense_data(struct ccw_device *cdev)
184 {
185         memset(&cdev->id, 0, sizeof(cdev->id));
186         cdev->id.cu_type   = cdev->private->senseid.cu_type;
187         cdev->id.cu_model  = cdev->private->senseid.cu_model;
188         cdev->id.dev_type  = cdev->private->senseid.dev_type;
189         cdev->id.dev_model = cdev->private->senseid.dev_model;
190 }
191
192 int ccw_device_test_sense_data(struct ccw_device *cdev)
193 {
194         return cdev->id.cu_type == cdev->private->senseid.cu_type &&
195                 cdev->id.cu_model == cdev->private->senseid.cu_model &&
196                 cdev->id.dev_type == cdev->private->senseid.dev_type &&
197                 cdev->id.dev_model == cdev->private->senseid.dev_model;
198 }
199
200 /*
201  * The machine won't give us any notification by machine check if a chpid has
202  * been varied online on the SE so we have to find out by magic (i. e. driving
203  * the channel subsystem to device selection and updating our path masks).
204  */
205 static void
206 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
207 {
208         int mask, i;
209         struct chp_id chpid;
210
211         chp_id_init(&chpid);
212         for (i = 0; i<8; i++) {
213                 mask = 0x80 >> i;
214                 if (!(sch->lpm & mask))
215                         continue;
216                 if (old_lpm & mask)
217                         continue;
218                 chpid.id = sch->schib.pmcw.chpid[i];
219                 if (!chp_is_registered(chpid))
220                         css_schedule_eval_all();
221         }
222 }
223
224 /*
225  * Stop device recognition.
226  */
227 static void
228 ccw_device_recog_done(struct ccw_device *cdev, int state)
229 {
230         struct subchannel *sch;
231         int old_lpm;
232
233         sch = to_subchannel(cdev->dev.parent);
234
235         if (cio_disable_subchannel(sch))
236                 state = DEV_STATE_NOT_OPER;
237         /*
238          * Now that we tried recognition, we have performed device selection
239          * through ssch() and the path information is up to date.
240          */
241         old_lpm = sch->lpm;
242
243         /* Check since device may again have become not operational. */
244         if (cio_update_schib(sch))
245                 state = DEV_STATE_NOT_OPER;
246         else
247                 sch->lpm = sch->schib.pmcw.pam & sch->opm;
248
249         if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
250                 /* Force reprobe on all chpids. */
251                 old_lpm = 0;
252         if (sch->lpm != old_lpm)
253                 __recover_lost_chpids(sch, old_lpm);
254         if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
255             (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
256                 cdev->private->flags.recog_done = 1;
257                 cdev->private->state = DEV_STATE_DISCONNECTED;
258                 wake_up(&cdev->private->wait_q);
259                 return;
260         }
261         if (cdev->private->flags.resuming) {
262                 cdev->private->state = state;
263                 cdev->private->flags.recog_done = 1;
264                 wake_up(&cdev->private->wait_q);
265                 return;
266         }
267         switch (state) {
268         case DEV_STATE_NOT_OPER:
269                 break;
270         case DEV_STATE_OFFLINE:
271                 if (!cdev->online) {
272                         ccw_device_update_sense_data(cdev);
273                         break;
274                 }
275                 cdev->private->state = DEV_STATE_OFFLINE;
276                 cdev->private->flags.recog_done = 1;
277                 if (ccw_device_test_sense_data(cdev)) {
278                         cdev->private->flags.donotify = 1;
279                         ccw_device_online(cdev);
280                         wake_up(&cdev->private->wait_q);
281                 } else {
282                         ccw_device_update_sense_data(cdev);
283                         ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
284                 }
285                 return;
286         case DEV_STATE_BOXED:
287                 if (cdev->id.cu_type != 0) { /* device was recognized before */
288                         cdev->private->flags.recog_done = 1;
289                         cdev->private->state = DEV_STATE_BOXED;
290                         wake_up(&cdev->private->wait_q);
291                         return;
292                 }
293                 break;
294         }
295         cdev->private->state = state;
296         io_subchannel_recog_done(cdev);
297         wake_up(&cdev->private->wait_q);
298 }
299
300 /*
301  * Function called from device_id.c after sense id has completed.
302  */
303 void
304 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
305 {
306         switch (err) {
307         case 0:
308                 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
309                 break;
310         case -ETIME:            /* Sense id stopped by timeout. */
311                 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
312                 break;
313         default:
314                 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
315                 break;
316         }
317 }
318
319 /**
320   * ccw_device_notify() - inform the device's driver about an event
321   * @cdev: device for which an event occurred
322   * @event: event that occurred
323   *
324   * Returns:
325   *   -%EINVAL if the device is offline or has no driver.
326   *   -%EOPNOTSUPP if the device's driver has no notifier registered.
327   *   %NOTIFY_OK if the driver wants to keep the device.
328   *   %NOTIFY_BAD if the driver doesn't want to keep the device.
329   */
330 int ccw_device_notify(struct ccw_device *cdev, int event)
331 {
332         int ret = -EINVAL;
333
334         if (!cdev->drv)
335                 goto out;
336         if (!cdev->online)
337                 goto out;
338         CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
339                       cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
340                       event);
341         if (!cdev->drv->notify) {
342                 ret = -EOPNOTSUPP;
343                 goto out;
344         }
345         if (cdev->drv->notify(cdev, event))
346                 ret = NOTIFY_OK;
347         else
348                 ret = NOTIFY_BAD;
349 out:
350         return ret;
351 }
352
353 static void ccw_device_oper_notify(struct ccw_device *cdev)
354 {
355         struct subchannel *sch = to_subchannel(cdev->dev.parent);
356
357         if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_OK) {
358                 /* Reenable channel measurements, if needed. */
359                 ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
360                 /* Save indication for new paths. */
361                 cdev->private->path_new_mask = sch->vpm;
362                 return;
363         }
364         /* Driver doesn't want device back. */
365         ccw_device_set_notoper(cdev);
366         ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
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         switch (state) {
390         case 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 &&
394                     ccw_device_notify(cdev, CIO_BOXED) != NOTIFY_OK)
395                         ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
396                 cdev->private->flags.donotify = 0;
397                 break;
398         case DEV_STATE_NOT_OPER:
399                 CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
400                               cdev->private->dev_id.devno, sch->schid.sch_no);
401                 if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
402                         ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
403                 else
404                         ccw_device_set_disconnected(cdev);
405                 cdev->private->flags.donotify = 0;
406                 break;
407         case DEV_STATE_DISCONNECTED:
408                 CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
409                               "%04x\n", cdev->private->dev_id.devno,
410                               sch->schid.sch_no);
411                 if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
412                         ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
413                 else
414                         ccw_device_set_disconnected(cdev);
415                 cdev->private->flags.donotify = 0;
416                 break;
417         default:
418                 break;
419         }
420
421         if (cdev->private->flags.donotify) {
422                 cdev->private->flags.donotify = 0;
423                 ccw_device_oper_notify(cdev);
424         }
425         wake_up(&cdev->private->wait_q);
426 }
427
428 /*
429  * Start device recognition.
430  */
431 void ccw_device_recognition(struct ccw_device *cdev)
432 {
433         struct subchannel *sch = to_subchannel(cdev->dev.parent);
434
435         /*
436          * We used to start here with a sense pgid to find out whether a device
437          * is locked by someone else. Unfortunately, the sense pgid command
438          * code has other meanings on devices predating the path grouping
439          * algorithm, so we start with sense id and box the device after an
440          * timeout (or if sense pgid during path verification detects the device
441          * is locked, as may happen on newer devices).
442          */
443         cdev->private->flags.recog_done = 0;
444         cdev->private->state = DEV_STATE_SENSE_ID;
445         if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
446                 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
447                 return;
448         }
449         ccw_device_sense_id_start(cdev);
450 }
451
452 /*
453  * Handle events for states that use the ccw request infrastructure.
454  */
455 static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
456 {
457         switch (e) {
458         case DEV_EVENT_NOTOPER:
459                 ccw_request_notoper(cdev);
460                 break;
461         case DEV_EVENT_INTERRUPT:
462                 ccw_request_handler(cdev);
463                 break;
464         case DEV_EVENT_TIMEOUT:
465                 ccw_request_timeout(cdev);
466                 break;
467         default:
468                 break;
469         }
470 }
471
472 static void ccw_device_report_path_events(struct ccw_device *cdev)
473 {
474         struct subchannel *sch = to_subchannel(cdev->dev.parent);
475         int path_event[8];
476         int chp, mask;
477
478         for (chp = 0, mask = 0x80; chp < 8; chp++, mask >>= 1) {
479                 path_event[chp] = PE_NONE;
480                 if (mask & cdev->private->path_gone_mask & ~(sch->vpm))
481                         path_event[chp] |= PE_PATH_GONE;
482                 if (mask & cdev->private->path_new_mask & sch->vpm)
483                         path_event[chp] |= PE_PATH_AVAILABLE;
484                 if (mask & cdev->private->pgid_reset_mask & sch->vpm)
485                         path_event[chp] |= PE_PATHGROUP_ESTABLISHED;
486         }
487         if (cdev->online && cdev->drv->path_event)
488                 cdev->drv->path_event(cdev, path_event);
489 }
490
491 static void ccw_device_reset_path_events(struct ccw_device *cdev)
492 {
493         cdev->private->path_gone_mask = 0;
494         cdev->private->path_new_mask = 0;
495         cdev->private->pgid_reset_mask = 0;
496 }
497
498 void
499 ccw_device_verify_done(struct ccw_device *cdev, int err)
500 {
501         struct subchannel *sch;
502
503         sch = to_subchannel(cdev->dev.parent);
504         /* Update schib - pom may have changed. */
505         if (cio_update_schib(sch)) {
506                 err = -ENODEV;
507                 goto callback;
508         }
509         /* Update lpm with verified path mask. */
510         sch->lpm = sch->vpm;
511         /* Repeat path verification? */
512         if (cdev->private->flags.doverify) {
513                 ccw_device_verify_start(cdev);
514                 return;
515         }
516 callback:
517         switch (err) {
518         case 0:
519                 ccw_device_done(cdev, DEV_STATE_ONLINE);
520                 /* Deliver fake irb to device driver, if needed. */
521                 if (cdev->private->flags.fake_irb) {
522                         memset(&cdev->private->irb, 0, sizeof(struct irb));
523                         cdev->private->irb.scsw.cmd.cc = 1;
524                         cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
525                         cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
526                         cdev->private->irb.scsw.cmd.stctl =
527                                 SCSW_STCTL_STATUS_PEND;
528                         cdev->private->flags.fake_irb = 0;
529                         if (cdev->handler)
530                                 cdev->handler(cdev, cdev->private->intparm,
531                                               &cdev->private->irb);
532                         memset(&cdev->private->irb, 0, sizeof(struct irb));
533                 }
534                 ccw_device_report_path_events(cdev);
535                 break;
536         case -ETIME:
537         case -EUSERS:
538                 /* Reset oper notify indication after verify error. */
539                 cdev->private->flags.donotify = 0;
540                 ccw_device_done(cdev, DEV_STATE_BOXED);
541                 break;
542         case -EACCES:
543                 /* Reset oper notify indication after verify error. */
544                 cdev->private->flags.donotify = 0;
545                 ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
546                 break;
547         default:
548                 /* Reset oper notify indication after verify error. */
549                 cdev->private->flags.donotify = 0;
550                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
551                 break;
552         }
553         ccw_device_reset_path_events(cdev);
554 }
555
556 /*
557  * Get device online.
558  */
559 int
560 ccw_device_online(struct ccw_device *cdev)
561 {
562         struct subchannel *sch;
563         int ret;
564
565         if ((cdev->private->state != DEV_STATE_OFFLINE) &&
566             (cdev->private->state != DEV_STATE_BOXED))
567                 return -EINVAL;
568         sch = to_subchannel(cdev->dev.parent);
569         ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
570         if (ret != 0) {
571                 /* Couldn't enable the subchannel for i/o. Sick device. */
572                 if (ret == -ENODEV)
573                         dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
574                 return ret;
575         }
576         /* Start initial path verification. */
577         cdev->private->state = DEV_STATE_VERIFY;
578         ccw_device_verify_start(cdev);
579         return 0;
580 }
581
582 void
583 ccw_device_disband_done(struct ccw_device *cdev, int err)
584 {
585         switch (err) {
586         case 0:
587                 ccw_device_done(cdev, DEV_STATE_OFFLINE);
588                 break;
589         case -ETIME:
590                 ccw_device_done(cdev, DEV_STATE_BOXED);
591                 break;
592         default:
593                 cdev->private->flags.donotify = 0;
594                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
595                 break;
596         }
597 }
598
599 /*
600  * Shutdown device.
601  */
602 int
603 ccw_device_offline(struct ccw_device *cdev)
604 {
605         struct subchannel *sch;
606
607         /* Allow ccw_device_offline while disconnected. */
608         if (cdev->private->state == DEV_STATE_DISCONNECTED ||
609             cdev->private->state == DEV_STATE_NOT_OPER) {
610                 cdev->private->flags.donotify = 0;
611                 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
612                 return 0;
613         }
614         if (cdev->private->state == DEV_STATE_BOXED) {
615                 ccw_device_done(cdev, DEV_STATE_BOXED);
616                 return 0;
617         }
618         if (ccw_device_is_orphan(cdev)) {
619                 ccw_device_done(cdev, DEV_STATE_OFFLINE);
620                 return 0;
621         }
622         sch = to_subchannel(cdev->dev.parent);
623         if (cio_update_schib(sch))
624                 return -ENODEV;
625         if (scsw_actl(&sch->schib.scsw) != 0)
626                 return -EBUSY;
627         if (cdev->private->state != DEV_STATE_ONLINE)
628                 return -EINVAL;
629         /* Are we doing path grouping? */
630         if (!cdev->private->flags.pgroup) {
631                 /* No, set state offline immediately. */
632                 ccw_device_done(cdev, DEV_STATE_OFFLINE);
633                 return 0;
634         }
635         /* Start Set Path Group commands. */
636         cdev->private->state = DEV_STATE_DISBAND_PGID;
637         ccw_device_disband_start(cdev);
638         return 0;
639 }
640
641 /*
642  * Handle not operational event in non-special state.
643  */
644 static void ccw_device_generic_notoper(struct ccw_device *cdev,
645                                        enum dev_event dev_event)
646 {
647         if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
648                 ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
649         else
650                 ccw_device_set_disconnected(cdev);
651 }
652
653 /*
654  * Handle path verification event in offline state.
655  */
656 static void ccw_device_offline_verify(struct ccw_device *cdev,
657                                       enum dev_event dev_event)
658 {
659         struct subchannel *sch = to_subchannel(cdev->dev.parent);
660
661         css_schedule_eval(sch->schid);
662 }
663
664 /*
665  * Handle path verification event.
666  */
667 static void
668 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
669 {
670         struct subchannel *sch;
671
672         if (cdev->private->state == DEV_STATE_W4SENSE) {
673                 cdev->private->flags.doverify = 1;
674                 return;
675         }
676         sch = to_subchannel(cdev->dev.parent);
677         /*
678          * Since we might not just be coming from an interrupt from the
679          * subchannel we have to update the schib.
680          */
681         if (cio_update_schib(sch)) {
682                 ccw_device_verify_done(cdev, -ENODEV);
683                 return;
684         }
685
686         if (scsw_actl(&sch->schib.scsw) != 0 ||
687             (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
688             (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
689                 /*
690                  * No final status yet or final status not yet delivered
691                  * to the device driver. Can't do path verification now,
692                  * delay until final status was delivered.
693                  */
694                 cdev->private->flags.doverify = 1;
695                 return;
696         }
697         /* Device is idle, we can do the path verification. */
698         cdev->private->state = DEV_STATE_VERIFY;
699         ccw_device_verify_start(cdev);
700 }
701
702 /*
703  * Handle path verification event in boxed state.
704  */
705 static void ccw_device_boxed_verify(struct ccw_device *cdev,
706                                     enum dev_event dev_event)
707 {
708         struct subchannel *sch = to_subchannel(cdev->dev.parent);
709
710         if (cdev->online) {
711                 if (cio_enable_subchannel(sch, (u32) (addr_t) sch))
712                         ccw_device_done(cdev, DEV_STATE_NOT_OPER);
713                 else
714                         ccw_device_online_verify(cdev, dev_event);
715         } else
716                 css_schedule_eval(sch->schid);
717 }
718
719 /*
720  * Got an interrupt for a normal io (state online).
721  */
722 static void
723 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
724 {
725         struct irb *irb;
726         int is_cmd;
727
728         irb = (struct irb *)&S390_lowcore.irb;
729         is_cmd = !scsw_is_tm(&irb->scsw);
730         /* Check for unsolicited interrupt. */
731         if (!scsw_is_solicited(&irb->scsw)) {
732                 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
733                     !irb->esw.esw0.erw.cons) {
734                         /* Unit check but no sense data. Need basic sense. */
735                         if (ccw_device_do_sense(cdev, irb) != 0)
736                                 goto call_handler_unsol;
737                         memcpy(&cdev->private->irb, irb, sizeof(struct irb));
738                         cdev->private->state = DEV_STATE_W4SENSE;
739                         cdev->private->intparm = 0;
740                         return;
741                 }
742 call_handler_unsol:
743                 if (cdev->handler)
744                         cdev->handler (cdev, 0, irb);
745                 if (cdev->private->flags.doverify)
746                         ccw_device_online_verify(cdev, 0);
747                 return;
748         }
749         /* Accumulate status and find out if a basic sense is needed. */
750         ccw_device_accumulate_irb(cdev, irb);
751         if (is_cmd && cdev->private->flags.dosense) {
752                 if (ccw_device_do_sense(cdev, irb) == 0) {
753                         cdev->private->state = DEV_STATE_W4SENSE;
754                 }
755                 return;
756         }
757         /* Call the handler. */
758         if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
759                 /* Start delayed path verification. */
760                 ccw_device_online_verify(cdev, 0);
761 }
762
763 /*
764  * Got an timeout in online state.
765  */
766 static void
767 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
768 {
769         int ret;
770
771         ccw_device_set_timeout(cdev, 0);
772         cdev->private->iretry = 255;
773         ret = ccw_device_cancel_halt_clear(cdev);
774         if (ret == -EBUSY) {
775                 ccw_device_set_timeout(cdev, 3*HZ);
776                 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
777                 return;
778         }
779         if (ret)
780                 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
781         else if (cdev->handler)
782                 cdev->handler(cdev, cdev->private->intparm,
783                               ERR_PTR(-ETIMEDOUT));
784 }
785
786 /*
787  * Got an interrupt for a basic sense.
788  */
789 static void
790 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
791 {
792         struct irb *irb;
793
794         irb = (struct irb *)&S390_lowcore.irb;
795         /* Check for unsolicited interrupt. */
796         if (scsw_stctl(&irb->scsw) ==
797             (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
798                 if (scsw_cc(&irb->scsw) == 1)
799                         /* Basic sense hasn't started. Try again. */
800                         ccw_device_do_sense(cdev, irb);
801                 else {
802                         CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
803                                       "interrupt during w4sense...\n",
804                                       cdev->private->dev_id.ssid,
805                                       cdev->private->dev_id.devno);
806                         if (cdev->handler)
807                                 cdev->handler (cdev, 0, irb);
808                 }
809                 return;
810         }
811         /*
812          * Check if a halt or clear has been issued in the meanwhile. If yes,
813          * only deliver the halt/clear interrupt to the device driver as if it
814          * had killed the original request.
815          */
816         if (scsw_fctl(&irb->scsw) &
817             (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
818                 cdev->private->flags.dosense = 0;
819                 memset(&cdev->private->irb, 0, sizeof(struct irb));
820                 ccw_device_accumulate_irb(cdev, irb);
821                 goto call_handler;
822         }
823         /* Add basic sense info to irb. */
824         ccw_device_accumulate_basic_sense(cdev, irb);
825         if (cdev->private->flags.dosense) {
826                 /* Another basic sense is needed. */
827                 ccw_device_do_sense(cdev, irb);
828                 return;
829         }
830 call_handler:
831         cdev->private->state = DEV_STATE_ONLINE;
832         /* In case sensing interfered with setting the device online */
833         wake_up(&cdev->private->wait_q);
834         /* Call the handler. */
835         if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
836                 /* Start delayed path verification. */
837                 ccw_device_online_verify(cdev, 0);
838 }
839
840 static void
841 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
842 {
843         struct subchannel *sch;
844
845         sch = to_subchannel(cdev->dev.parent);
846         ccw_device_set_timeout(cdev, 0);
847         /* Start delayed path verification. */
848         ccw_device_online_verify(cdev, 0);
849         /* OK, i/o is dead now. Call interrupt handler. */
850         if (cdev->handler)
851                 cdev->handler(cdev, cdev->private->intparm,
852                               ERR_PTR(-EIO));
853 }
854
855 static void
856 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
857 {
858         int ret;
859
860         ret = ccw_device_cancel_halt_clear(cdev);
861         if (ret == -EBUSY) {
862                 ccw_device_set_timeout(cdev, 3*HZ);
863                 return;
864         }
865         /* Start delayed path verification. */
866         ccw_device_online_verify(cdev, 0);
867         if (cdev->handler)
868                 cdev->handler(cdev, cdev->private->intparm,
869                               ERR_PTR(-EIO));
870 }
871
872 void ccw_device_kill_io(struct ccw_device *cdev)
873 {
874         int ret;
875
876         cdev->private->iretry = 255;
877         ret = ccw_device_cancel_halt_clear(cdev);
878         if (ret == -EBUSY) {
879                 ccw_device_set_timeout(cdev, 3*HZ);
880                 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
881                 return;
882         }
883         /* Start delayed path verification. */
884         ccw_device_online_verify(cdev, 0);
885         if (cdev->handler)
886                 cdev->handler(cdev, cdev->private->intparm,
887                               ERR_PTR(-EIO));
888 }
889
890 static void
891 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
892 {
893         /* Start verification after current task finished. */
894         cdev->private->flags.doverify = 1;
895 }
896
897 static void
898 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
899 {
900         struct subchannel *sch;
901
902         sch = to_subchannel(cdev->dev.parent);
903         if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
904                 /* Couldn't enable the subchannel for i/o. Sick device. */
905                 return;
906         cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
907         ccw_device_sense_id_start(cdev);
908 }
909
910 void ccw_device_trigger_reprobe(struct ccw_device *cdev)
911 {
912         struct subchannel *sch;
913
914         if (cdev->private->state != DEV_STATE_DISCONNECTED)
915                 return;
916
917         sch = to_subchannel(cdev->dev.parent);
918         /* Update some values. */
919         if (cio_update_schib(sch))
920                 return;
921         /*
922          * The pim, pam, pom values may not be accurate, but they are the best
923          * we have before performing device selection :/
924          */
925         sch->lpm = sch->schib.pmcw.pam & sch->opm;
926         /*
927          * Use the initial configuration since we can't be shure that the old
928          * paths are valid.
929          */
930         io_subchannel_init_config(sch);
931         if (cio_commit_config(sch))
932                 return;
933
934         /* We should also udate ssd info, but this has to wait. */
935         /* Check if this is another device which appeared on the same sch. */
936         if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
937                 css_schedule_eval(sch->schid);
938         else
939                 ccw_device_start_id(cdev, 0);
940 }
941
942 static void ccw_device_disabled_irq(struct ccw_device *cdev,
943                                     enum dev_event dev_event)
944 {
945         struct subchannel *sch;
946
947         sch = to_subchannel(cdev->dev.parent);
948         /*
949          * An interrupt in a disabled state means a previous disable was not
950          * successful - should not happen, but we try to disable again.
951          */
952         cio_disable_subchannel(sch);
953 }
954
955 static void
956 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
957 {
958         retry_set_schib(cdev);
959         cdev->private->state = DEV_STATE_ONLINE;
960         dev_fsm_event(cdev, dev_event);
961 }
962
963 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
964                                        enum dev_event dev_event)
965 {
966         cmf_retry_copy_block(cdev);
967         cdev->private->state = DEV_STATE_ONLINE;
968         dev_fsm_event(cdev, dev_event);
969 }
970
971 static void
972 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
973 {
974         ccw_device_set_timeout(cdev, 0);
975         cdev->private->state = DEV_STATE_NOT_OPER;
976         wake_up(&cdev->private->wait_q);
977 }
978
979 static void
980 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
981 {
982         int ret;
983
984         ret = ccw_device_cancel_halt_clear(cdev);
985         if (ret == -EBUSY) {
986                 ccw_device_set_timeout(cdev, HZ/10);
987         } else {
988                 cdev->private->state = DEV_STATE_NOT_OPER;
989                 wake_up(&cdev->private->wait_q);
990         }
991 }
992
993 /*
994  * No operation action. This is used e.g. to ignore a timeout event in
995  * state offline.
996  */
997 static void
998 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
999 {
1000 }
1001
1002 /*
1003  * device statemachine
1004  */
1005 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1006         [DEV_STATE_NOT_OPER] = {
1007                 [DEV_EVENT_NOTOPER]     = ccw_device_nop,
1008                 [DEV_EVENT_INTERRUPT]   = ccw_device_disabled_irq,
1009                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1010                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1011         },
1012         [DEV_STATE_SENSE_PGID] = {
1013                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
1014                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
1015                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
1016                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1017         },
1018         [DEV_STATE_SENSE_ID] = {
1019                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
1020                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
1021                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
1022                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1023         },
1024         [DEV_STATE_OFFLINE] = {
1025                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
1026                 [DEV_EVENT_INTERRUPT]   = ccw_device_disabled_irq,
1027                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1028                 [DEV_EVENT_VERIFY]      = ccw_device_offline_verify,
1029         },
1030         [DEV_STATE_VERIFY] = {
1031                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
1032                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
1033                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
1034                 [DEV_EVENT_VERIFY]      = ccw_device_delay_verify,
1035         },
1036         [DEV_STATE_ONLINE] = {
1037                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
1038                 [DEV_EVENT_INTERRUPT]   = ccw_device_irq,
1039                 [DEV_EVENT_TIMEOUT]     = ccw_device_online_timeout,
1040                 [DEV_EVENT_VERIFY]      = ccw_device_online_verify,
1041         },
1042         [DEV_STATE_W4SENSE] = {
1043                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
1044                 [DEV_EVENT_INTERRUPT]   = ccw_device_w4sense,
1045                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1046                 [DEV_EVENT_VERIFY]      = ccw_device_online_verify,
1047         },
1048         [DEV_STATE_DISBAND_PGID] = {
1049                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
1050                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
1051                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
1052                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1053         },
1054         [DEV_STATE_BOXED] = {
1055                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
1056                 [DEV_EVENT_INTERRUPT]   = ccw_device_nop,
1057                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1058                 [DEV_EVENT_VERIFY]      = ccw_device_boxed_verify,
1059         },
1060         /* states to wait for i/o completion before doing something */
1061         [DEV_STATE_TIMEOUT_KILL] = {
1062                 [DEV_EVENT_NOTOPER]     = ccw_device_generic_notoper,
1063                 [DEV_EVENT_INTERRUPT]   = ccw_device_killing_irq,
1064                 [DEV_EVENT_TIMEOUT]     = ccw_device_killing_timeout,
1065                 [DEV_EVENT_VERIFY]      = ccw_device_nop, //FIXME
1066         },
1067         [DEV_STATE_QUIESCE] = {
1068                 [DEV_EVENT_NOTOPER]     = ccw_device_quiesce_done,
1069                 [DEV_EVENT_INTERRUPT]   = ccw_device_quiesce_done,
1070                 [DEV_EVENT_TIMEOUT]     = ccw_device_quiesce_timeout,
1071                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1072         },
1073         /* special states for devices gone not operational */
1074         [DEV_STATE_DISCONNECTED] = {
1075                 [DEV_EVENT_NOTOPER]     = ccw_device_nop,
1076                 [DEV_EVENT_INTERRUPT]   = ccw_device_start_id,
1077                 [DEV_EVENT_TIMEOUT]     = ccw_device_nop,
1078                 [DEV_EVENT_VERIFY]      = ccw_device_start_id,
1079         },
1080         [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1081                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
1082                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
1083                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
1084                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1085         },
1086         [DEV_STATE_CMFCHANGE] = {
1087                 [DEV_EVENT_NOTOPER]     = ccw_device_change_cmfstate,
1088                 [DEV_EVENT_INTERRUPT]   = ccw_device_change_cmfstate,
1089                 [DEV_EVENT_TIMEOUT]     = ccw_device_change_cmfstate,
1090                 [DEV_EVENT_VERIFY]      = ccw_device_change_cmfstate,
1091         },
1092         [DEV_STATE_CMFUPDATE] = {
1093                 [DEV_EVENT_NOTOPER]     = ccw_device_update_cmfblock,
1094                 [DEV_EVENT_INTERRUPT]   = ccw_device_update_cmfblock,
1095                 [DEV_EVENT_TIMEOUT]     = ccw_device_update_cmfblock,
1096                 [DEV_EVENT_VERIFY]      = ccw_device_update_cmfblock,
1097         },
1098         [DEV_STATE_STEAL_LOCK] = {
1099                 [DEV_EVENT_NOTOPER]     = ccw_device_request_event,
1100                 [DEV_EVENT_INTERRUPT]   = ccw_device_request_event,
1101                 [DEV_EVENT_TIMEOUT]     = ccw_device_request_event,
1102                 [DEV_EVENT_VERIFY]      = ccw_device_nop,
1103         },
1104 };
1105
1106 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);