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