USB: ftdi_sio: re-implement read processing
[linux-2.6/mini2440.git] / drivers / s390 / cio / device_fsm.c
blobe728ce447f6ee107e5482886cb0c8f41115cb363
1 /*
2 * drivers/s390/cio/device_fsm.c
3 * finite state machine for device handling
5 * Copyright IBM Corp. 2002,2008
6 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
7 * Martin Schwidefsky (schwidefsky@de.ibm.com)
8 */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/jiffies.h>
13 #include <linux/string.h>
15 #include <asm/ccwdev.h>
16 #include <asm/cio.h>
17 #include <asm/chpid.h>
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"
27 static int timeout_log_enabled;
29 static int __init ccw_timeout_log_setup(char *unused)
31 timeout_log_enabled = 1;
32 return 1;
35 __setup("ccw_timeout_log", ccw_timeout_log_setup);
37 static void ccw_timeout_log(struct ccw_device *cdev)
39 struct schib schib;
40 struct subchannel *sch;
41 struct io_subchannel_private *private;
42 union orb *orb;
43 int cc;
45 sch = to_subchannel(cdev->dev.parent);
46 private = to_io_private(sch);
47 orb = &private->orb;
48 cc = stsch(sch->schid, &schib);
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);
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");
77 print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
78 (void *)(addr_t)orb->cmd.cpa,
79 sizeof(struct ccw1), 0);
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);
93 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
95 static void
96 ccw_device_timeout(unsigned long data)
98 struct ccw_device *cdev;
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);
109 * Set timeout
111 void
112 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
114 if (expires == 0) {
115 del_timer(&cdev->private->timer);
116 return;
118 if (timer_pending(&cdev->private->timer)) {
119 if (mod_timer(&cdev->private->timer, jiffies + expires))
120 return;
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);
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).
137 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
139 struct subchannel *sch;
140 int ret;
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;
156 /* cancel io unsuccessful or not applicable (transport mode).
157 * Continue with asynchronous instructions. */
158 cdev->private->iretry = 3; /* 3 halt retries. */
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;
168 /* halt io unsuccessful. */
169 cdev->private->iretry = 255; /* 255 clear retries. */
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;
177 panic("Can't stop i/o on subchannel.\n");
180 void ccw_device_update_sense_data(struct ccw_device *cdev)
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;
189 int ccw_device_test_sense_data(struct ccw_device *cdev)
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;
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).
202 static void
203 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
205 int mask, i;
206 struct chp_id chpid;
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();
222 * Stop device recognition.
224 static void
225 ccw_device_recog_done(struct ccw_device *cdev, int state)
227 struct subchannel *sch;
228 int old_lpm;
230 sch = to_subchannel(cdev->dev.parent);
232 ccw_device_set_timeout(cdev, 0);
233 cio_disable_subchannel(sch);
235 * Now that we tried recognition, we have performed device selection
236 * through ssch() and the path information is up to date.
238 old_lpm = sch->lpm;
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;
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;
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;
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;
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);
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;
308 break;
310 cdev->private->state = state;
311 io_subchannel_recog_done(cdev);
312 wake_up(&cdev->private->wait_q);
316 * Function called from device_id.c after sense id has completed.
318 void
319 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
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;
334 int ccw_device_notify(struct ccw_device *cdev, int event)
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;
346 static void cmf_reenable_delayed(struct work_struct *work)
348 struct ccw_device_private *priv;
349 struct ccw_device *cdev;
351 priv = container_of(work, struct ccw_device_private, kick_work);
352 cdev = priv->cdev;
353 cmf_reenable(cdev);
356 static void ccw_device_oper_notify(struct ccw_device *cdev)
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;
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);
371 * Finished with online/offline processing.
373 static void
374 ccw_device_done(struct ccw_device *cdev, int state)
376 struct subchannel *sch;
378 sch = to_subchannel(cdev->dev.parent);
380 ccw_device_set_timeout(cdev, 0);
382 if (state != DEV_STATE_ONLINE)
383 cio_disable_subchannel(sch);
385 /* Reset device status. */
386 memset(&cdev->private->irb, 0, sizeof(struct irb));
388 cdev->private->state = state;
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;
397 if (state == DEV_STATE_NOT_OPER) {
398 CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
399 cdev->private->dev_id.devno, sch->schid.sch_no);
400 if (!ccw_device_notify(cdev, CIO_GONE))
401 ccw_device_schedule_sch_unregister(cdev);
402 cdev->private->flags.donotify = 0;
405 if (cdev->private->flags.donotify) {
406 cdev->private->flags.donotify = 0;
407 ccw_device_oper_notify(cdev);
409 wake_up(&cdev->private->wait_q);
412 static int cmp_pgid(struct pgid *p1, struct pgid *p2)
414 char *c1;
415 char *c2;
417 c1 = (char *)p1;
418 c2 = (char *)p2;
420 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
423 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
425 int i;
426 int last;
428 last = 0;
429 for (i = 0; i < 8; i++) {
430 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
431 /* No PGID yet */
432 continue;
433 if (cdev->private->pgid[last].inf.ps.state1 ==
434 SNID_STATE1_RESET) {
435 /* First non-zero PGID */
436 last = i;
437 continue;
439 if (cmp_pgid(&cdev->private->pgid[i],
440 &cdev->private->pgid[last]) == 0)
441 /* Non-conflicting PGIDs */
442 continue;
444 /* PGID mismatch, can't pathgroup. */
445 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
446 "0.%x.%04x, can't pathgroup\n",
447 cdev->private->dev_id.ssid,
448 cdev->private->dev_id.devno);
449 cdev->private->options.pgroup = 0;
450 return;
452 if (cdev->private->pgid[last].inf.ps.state1 ==
453 SNID_STATE1_RESET)
454 /* No previous pgid found */
455 memcpy(&cdev->private->pgid[0],
456 &channel_subsystems[0]->global_pgid,
457 sizeof(struct pgid));
458 else
459 /* Use existing pgid */
460 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
461 sizeof(struct pgid));
465 * Function called from device_pgid.c after sense path ground has completed.
467 void
468 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
470 struct subchannel *sch;
472 sch = to_subchannel(cdev->dev.parent);
473 switch (err) {
474 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
475 cdev->private->options.pgroup = 0;
476 break;
477 case 0: /* success */
478 case -EACCES: /* partial success, some paths not operational */
479 /* Check if all pgids are equal or 0. */
480 __ccw_device_get_common_pgid(cdev);
481 break;
482 case -ETIME: /* Sense path group id stopped by timeout. */
483 case -EUSERS: /* device is reserved for someone else. */
484 ccw_device_done(cdev, DEV_STATE_BOXED);
485 return;
486 default:
487 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
488 return;
490 /* Start Path Group verification. */
491 cdev->private->state = DEV_STATE_VERIFY;
492 cdev->private->flags.doverify = 0;
493 ccw_device_verify_start(cdev);
497 * Start device recognition.
500 ccw_device_recognition(struct ccw_device *cdev)
502 struct subchannel *sch;
503 int ret;
505 sch = to_subchannel(cdev->dev.parent);
506 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
507 if (ret != 0)
508 /* Couldn't enable the subchannel for i/o. Sick device. */
509 return ret;
511 /* After 60s the device recognition is considered to have failed. */
512 ccw_device_set_timeout(cdev, 60*HZ);
515 * We used to start here with a sense pgid to find out whether a device
516 * is locked by someone else. Unfortunately, the sense pgid command
517 * code has other meanings on devices predating the path grouping
518 * algorithm, so we start with sense id and box the device after an
519 * timeout (or if sense pgid during path verification detects the device
520 * is locked, as may happen on newer devices).
522 cdev->private->flags.recog_done = 0;
523 cdev->private->state = DEV_STATE_SENSE_ID;
524 ccw_device_sense_id_start(cdev);
525 return 0;
529 * Handle timeout in device recognition.
531 static void
532 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
534 int ret;
536 ret = ccw_device_cancel_halt_clear(cdev);
537 switch (ret) {
538 case 0:
539 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
540 break;
541 case -ENODEV:
542 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
543 break;
544 default:
545 ccw_device_set_timeout(cdev, 3*HZ);
550 void
551 ccw_device_verify_done(struct ccw_device *cdev, int err)
553 struct subchannel *sch;
555 sch = to_subchannel(cdev->dev.parent);
556 /* Update schib - pom may have changed. */
557 if (cio_update_schib(sch)) {
558 cdev->private->flags.donotify = 0;
559 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
560 return;
562 /* Update lpm with verified path mask. */
563 sch->lpm = sch->vpm;
564 /* Repeat path verification? */
565 if (cdev->private->flags.doverify) {
566 cdev->private->flags.doverify = 0;
567 ccw_device_verify_start(cdev);
568 return;
570 switch (err) {
571 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
572 cdev->private->options.pgroup = 0;
573 case 0:
574 ccw_device_done(cdev, DEV_STATE_ONLINE);
575 /* Deliver fake irb to device driver, if needed. */
576 if (cdev->private->flags.fake_irb) {
577 memset(&cdev->private->irb, 0, sizeof(struct irb));
578 cdev->private->irb.scsw.cmd.cc = 1;
579 cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
580 cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
581 cdev->private->irb.scsw.cmd.stctl =
582 SCSW_STCTL_STATUS_PEND;
583 cdev->private->flags.fake_irb = 0;
584 if (cdev->handler)
585 cdev->handler(cdev, cdev->private->intparm,
586 &cdev->private->irb);
587 memset(&cdev->private->irb, 0, sizeof(struct irb));
589 break;
590 case -ETIME:
591 /* Reset oper notify indication after verify error. */
592 cdev->private->flags.donotify = 0;
593 ccw_device_done(cdev, DEV_STATE_BOXED);
594 break;
595 default:
596 /* Reset oper notify indication after verify error. */
597 cdev->private->flags.donotify = 0;
598 if (cdev->online) {
599 ccw_device_set_timeout(cdev, 0);
600 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
601 } else
602 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
603 break;
608 * Get device online.
611 ccw_device_online(struct ccw_device *cdev)
613 struct subchannel *sch;
614 int ret;
616 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
617 (cdev->private->state != DEV_STATE_BOXED))
618 return -EINVAL;
619 sch = to_subchannel(cdev->dev.parent);
620 ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
621 if (ret != 0) {
622 /* Couldn't enable the subchannel for i/o. Sick device. */
623 if (ret == -ENODEV)
624 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
625 return ret;
627 /* Do we want to do path grouping? */
628 if (!cdev->private->options.pgroup) {
629 /* Start initial path verification. */
630 cdev->private->state = DEV_STATE_VERIFY;
631 cdev->private->flags.doverify = 0;
632 ccw_device_verify_start(cdev);
633 return 0;
635 /* Do a SensePGID first. */
636 cdev->private->state = DEV_STATE_SENSE_PGID;
637 ccw_device_sense_pgid_start(cdev);
638 return 0;
641 void
642 ccw_device_disband_done(struct ccw_device *cdev, int err)
644 switch (err) {
645 case 0:
646 ccw_device_done(cdev, DEV_STATE_OFFLINE);
647 break;
648 case -ETIME:
649 ccw_device_done(cdev, DEV_STATE_BOXED);
650 break;
651 default:
652 cdev->private->flags.donotify = 0;
653 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
654 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
655 break;
660 * Shutdown device.
663 ccw_device_offline(struct ccw_device *cdev)
665 struct subchannel *sch;
667 /* Allow ccw_device_offline while disconnected. */
668 if (cdev->private->state == DEV_STATE_DISCONNECTED ||
669 cdev->private->state == DEV_STATE_NOT_OPER) {
670 cdev->private->flags.donotify = 0;
671 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
672 return 0;
674 if (ccw_device_is_orphan(cdev)) {
675 ccw_device_done(cdev, DEV_STATE_OFFLINE);
676 return 0;
678 sch = to_subchannel(cdev->dev.parent);
679 if (cio_update_schib(sch))
680 return -ENODEV;
681 if (scsw_actl(&sch->schib.scsw) != 0)
682 return -EBUSY;
683 if (cdev->private->state != DEV_STATE_ONLINE)
684 return -EINVAL;
685 /* Are we doing path grouping? */
686 if (!cdev->private->options.pgroup) {
687 /* No, set state offline immediately. */
688 ccw_device_done(cdev, DEV_STATE_OFFLINE);
689 return 0;
691 /* Start Set Path Group commands. */
692 cdev->private->state = DEV_STATE_DISBAND_PGID;
693 ccw_device_disband_start(cdev);
694 return 0;
698 * Handle timeout in device online/offline process.
700 static void
701 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
703 int ret;
705 ret = ccw_device_cancel_halt_clear(cdev);
706 switch (ret) {
707 case 0:
708 ccw_device_done(cdev, DEV_STATE_BOXED);
709 break;
710 case -ENODEV:
711 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
712 break;
713 default:
714 ccw_device_set_timeout(cdev, 3*HZ);
719 * Handle not oper event in device recognition.
721 static void
722 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
724 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
728 * Handle not operational event in non-special state.
730 static void ccw_device_generic_notoper(struct ccw_device *cdev,
731 enum dev_event dev_event)
733 struct subchannel *sch;
735 ccw_device_set_notoper(cdev);
736 sch = to_subchannel(cdev->dev.parent);
737 css_schedule_eval(sch->schid);
741 * Handle path verification event in offline state.
743 static void ccw_device_offline_verify(struct ccw_device *cdev,
744 enum dev_event dev_event)
746 struct subchannel *sch = to_subchannel(cdev->dev.parent);
748 css_schedule_eval(sch->schid);
752 * Handle path verification event.
754 static void
755 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
757 struct subchannel *sch;
759 if (cdev->private->state == DEV_STATE_W4SENSE) {
760 cdev->private->flags.doverify = 1;
761 return;
763 sch = to_subchannel(cdev->dev.parent);
765 * Since we might not just be coming from an interrupt from the
766 * subchannel we have to update the schib.
768 if (cio_update_schib(sch)) {
769 ccw_device_verify_done(cdev, -ENODEV);
770 return;
773 if (scsw_actl(&sch->schib.scsw) != 0 ||
774 (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
775 (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
777 * No final status yet or final status not yet delivered
778 * to the device driver. Can't do path verfication now,
779 * delay until final status was delivered.
781 cdev->private->flags.doverify = 1;
782 return;
784 /* Device is idle, we can do the path verification. */
785 cdev->private->state = DEV_STATE_VERIFY;
786 cdev->private->flags.doverify = 0;
787 ccw_device_verify_start(cdev);
791 * Got an interrupt for a normal io (state online).
793 static void
794 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
796 struct irb *irb;
797 int is_cmd;
799 irb = (struct irb *) __LC_IRB;
800 is_cmd = !scsw_is_tm(&irb->scsw);
801 /* Check for unsolicited interrupt. */
802 if (!scsw_is_solicited(&irb->scsw)) {
803 if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
804 !irb->esw.esw0.erw.cons) {
805 /* Unit check but no sense data. Need basic sense. */
806 if (ccw_device_do_sense(cdev, irb) != 0)
807 goto call_handler_unsol;
808 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
809 cdev->private->state = DEV_STATE_W4SENSE;
810 cdev->private->intparm = 0;
811 return;
813 call_handler_unsol:
814 if (cdev->handler)
815 cdev->handler (cdev, 0, irb);
816 if (cdev->private->flags.doverify)
817 ccw_device_online_verify(cdev, 0);
818 return;
820 /* Accumulate status and find out if a basic sense is needed. */
821 ccw_device_accumulate_irb(cdev, irb);
822 if (is_cmd && cdev->private->flags.dosense) {
823 if (ccw_device_do_sense(cdev, irb) == 0) {
824 cdev->private->state = DEV_STATE_W4SENSE;
826 return;
828 /* Call the handler. */
829 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
830 /* Start delayed path verification. */
831 ccw_device_online_verify(cdev, 0);
835 * Got an timeout in online state.
837 static void
838 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
840 int ret;
842 ccw_device_set_timeout(cdev, 0);
843 ret = ccw_device_cancel_halt_clear(cdev);
844 if (ret == -EBUSY) {
845 ccw_device_set_timeout(cdev, 3*HZ);
846 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
847 return;
849 if (ret == -ENODEV)
850 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
851 else if (cdev->handler)
852 cdev->handler(cdev, cdev->private->intparm,
853 ERR_PTR(-ETIMEDOUT));
857 * Got an interrupt for a basic sense.
859 static void
860 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
862 struct irb *irb;
864 irb = (struct irb *) __LC_IRB;
865 /* Check for unsolicited interrupt. */
866 if (scsw_stctl(&irb->scsw) ==
867 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
868 if (scsw_cc(&irb->scsw) == 1)
869 /* Basic sense hasn't started. Try again. */
870 ccw_device_do_sense(cdev, irb);
871 else {
872 CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
873 "interrupt during w4sense...\n",
874 cdev->private->dev_id.ssid,
875 cdev->private->dev_id.devno);
876 if (cdev->handler)
877 cdev->handler (cdev, 0, irb);
879 return;
882 * Check if a halt or clear has been issued in the meanwhile. If yes,
883 * only deliver the halt/clear interrupt to the device driver as if it
884 * had killed the original request.
886 if (scsw_fctl(&irb->scsw) &
887 (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
888 /* Retry Basic Sense if requested. */
889 if (cdev->private->flags.intretry) {
890 cdev->private->flags.intretry = 0;
891 ccw_device_do_sense(cdev, irb);
892 return;
894 cdev->private->flags.dosense = 0;
895 memset(&cdev->private->irb, 0, sizeof(struct irb));
896 ccw_device_accumulate_irb(cdev, irb);
897 goto call_handler;
899 /* Add basic sense info to irb. */
900 ccw_device_accumulate_basic_sense(cdev, irb);
901 if (cdev->private->flags.dosense) {
902 /* Another basic sense is needed. */
903 ccw_device_do_sense(cdev, irb);
904 return;
906 call_handler:
907 cdev->private->state = DEV_STATE_ONLINE;
908 /* In case sensing interfered with setting the device online */
909 wake_up(&cdev->private->wait_q);
910 /* Call the handler. */
911 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
912 /* Start delayed path verification. */
913 ccw_device_online_verify(cdev, 0);
916 static void
917 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
919 struct irb *irb;
921 irb = (struct irb *) __LC_IRB;
922 /* Accumulate status. We don't do basic sense. */
923 ccw_device_accumulate_irb(cdev, irb);
924 /* Remember to clear irb to avoid residuals. */
925 memset(&cdev->private->irb, 0, sizeof(struct irb));
926 /* Try to start delayed device verification. */
927 ccw_device_online_verify(cdev, 0);
928 /* Note: Don't call handler for cio initiated clear! */
931 static void
932 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
934 struct subchannel *sch;
936 sch = to_subchannel(cdev->dev.parent);
937 ccw_device_set_timeout(cdev, 0);
938 /* Start delayed path verification. */
939 ccw_device_online_verify(cdev, 0);
940 /* OK, i/o is dead now. Call interrupt handler. */
941 if (cdev->handler)
942 cdev->handler(cdev, cdev->private->intparm,
943 ERR_PTR(-EIO));
946 static void
947 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
949 int ret;
951 ret = ccw_device_cancel_halt_clear(cdev);
952 if (ret == -EBUSY) {
953 ccw_device_set_timeout(cdev, 3*HZ);
954 return;
956 /* Start delayed path verification. */
957 ccw_device_online_verify(cdev, 0);
958 if (cdev->handler)
959 cdev->handler(cdev, cdev->private->intparm,
960 ERR_PTR(-EIO));
963 void ccw_device_kill_io(struct ccw_device *cdev)
965 int ret;
967 ret = ccw_device_cancel_halt_clear(cdev);
968 if (ret == -EBUSY) {
969 ccw_device_set_timeout(cdev, 3*HZ);
970 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
971 return;
973 /* Start delayed path verification. */
974 ccw_device_online_verify(cdev, 0);
975 if (cdev->handler)
976 cdev->handler(cdev, cdev->private->intparm,
977 ERR_PTR(-EIO));
980 static void
981 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
983 /* Start verification after current task finished. */
984 cdev->private->flags.doverify = 1;
987 static void
988 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
990 struct irb *irb;
992 switch (dev_event) {
993 case DEV_EVENT_INTERRUPT:
994 irb = (struct irb *) __LC_IRB;
995 /* Check for unsolicited interrupt. */
996 if ((scsw_stctl(&irb->scsw) ==
997 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
998 (!scsw_cc(&irb->scsw)))
999 /* FIXME: we should restart stlck here, but this
1000 * is extremely unlikely ... */
1001 goto out_wakeup;
1003 ccw_device_accumulate_irb(cdev, irb);
1004 /* We don't care about basic sense etc. */
1005 break;
1006 default: /* timeout */
1007 break;
1009 out_wakeup:
1010 wake_up(&cdev->private->wait_q);
1013 static void
1014 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1016 struct subchannel *sch;
1018 sch = to_subchannel(cdev->dev.parent);
1019 if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
1020 /* Couldn't enable the subchannel for i/o. Sick device. */
1021 return;
1023 /* After 60s the device recognition is considered to have failed. */
1024 ccw_device_set_timeout(cdev, 60*HZ);
1026 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1027 ccw_device_sense_id_start(cdev);
1030 void ccw_device_trigger_reprobe(struct ccw_device *cdev)
1032 struct subchannel *sch;
1034 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1035 return;
1037 sch = to_subchannel(cdev->dev.parent);
1038 /* Update some values. */
1039 if (cio_update_schib(sch))
1040 return;
1042 * The pim, pam, pom values may not be accurate, but they are the best
1043 * we have before performing device selection :/
1045 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1047 * Use the initial configuration since we can't be shure that the old
1048 * paths are valid.
1050 io_subchannel_init_config(sch);
1051 if (cio_commit_config(sch))
1052 return;
1054 /* We should also udate ssd info, but this has to wait. */
1055 /* Check if this is another device which appeared on the same sch. */
1056 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1057 PREPARE_WORK(&cdev->private->kick_work,
1058 ccw_device_move_to_orphanage);
1059 queue_work(slow_path_wq, &cdev->private->kick_work);
1060 } else
1061 ccw_device_start_id(cdev, 0);
1064 static void
1065 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1067 struct subchannel *sch;
1069 sch = to_subchannel(cdev->dev.parent);
1071 * An interrupt in state offline means a previous disable was not
1072 * successful - should not happen, but we try to disable again.
1074 cio_disable_subchannel(sch);
1077 static void
1078 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1080 retry_set_schib(cdev);
1081 cdev->private->state = DEV_STATE_ONLINE;
1082 dev_fsm_event(cdev, dev_event);
1085 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1086 enum dev_event dev_event)
1088 cmf_retry_copy_block(cdev);
1089 cdev->private->state = DEV_STATE_ONLINE;
1090 dev_fsm_event(cdev, dev_event);
1093 static void
1094 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1096 ccw_device_set_timeout(cdev, 0);
1097 if (dev_event == DEV_EVENT_NOTOPER)
1098 cdev->private->state = DEV_STATE_NOT_OPER;
1099 else
1100 cdev->private->state = DEV_STATE_OFFLINE;
1101 wake_up(&cdev->private->wait_q);
1104 static void
1105 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1107 int ret;
1109 ret = ccw_device_cancel_halt_clear(cdev);
1110 switch (ret) {
1111 case 0:
1112 cdev->private->state = DEV_STATE_OFFLINE;
1113 wake_up(&cdev->private->wait_q);
1114 break;
1115 case -ENODEV:
1116 cdev->private->state = DEV_STATE_NOT_OPER;
1117 wake_up(&cdev->private->wait_q);
1118 break;
1119 default:
1120 ccw_device_set_timeout(cdev, HZ/10);
1125 * No operation action. This is used e.g. to ignore a timeout event in
1126 * state offline.
1128 static void
1129 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1134 * Bug operation action.
1136 static void
1137 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1139 CIO_MSG_EVENT(0, "Internal state [%i][%i] not handled for device "
1140 "0.%x.%04x\n", cdev->private->state, dev_event,
1141 cdev->private->dev_id.ssid,
1142 cdev->private->dev_id.devno);
1143 BUG();
1147 * device statemachine
1149 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1150 [DEV_STATE_NOT_OPER] = {
1151 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1152 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1153 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1154 [DEV_EVENT_VERIFY] = ccw_device_nop,
1156 [DEV_STATE_SENSE_PGID] = {
1157 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1158 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1159 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1160 [DEV_EVENT_VERIFY] = ccw_device_nop,
1162 [DEV_STATE_SENSE_ID] = {
1163 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1164 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1165 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1166 [DEV_EVENT_VERIFY] = ccw_device_nop,
1168 [DEV_STATE_OFFLINE] = {
1169 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1170 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1171 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1172 [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
1174 [DEV_STATE_VERIFY] = {
1175 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1176 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1177 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1178 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1180 [DEV_STATE_ONLINE] = {
1181 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1182 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1183 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1184 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1186 [DEV_STATE_W4SENSE] = {
1187 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1188 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1189 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1190 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1192 [DEV_STATE_DISBAND_PGID] = {
1193 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1194 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1195 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1196 [DEV_EVENT_VERIFY] = ccw_device_nop,
1198 [DEV_STATE_BOXED] = {
1199 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1200 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1201 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1202 [DEV_EVENT_VERIFY] = ccw_device_nop,
1204 /* states to wait for i/o completion before doing something */
1205 [DEV_STATE_CLEAR_VERIFY] = {
1206 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1207 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1208 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1209 [DEV_EVENT_VERIFY] = ccw_device_nop,
1211 [DEV_STATE_TIMEOUT_KILL] = {
1212 [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
1213 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1214 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1215 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1217 [DEV_STATE_QUIESCE] = {
1218 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1219 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1220 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1221 [DEV_EVENT_VERIFY] = ccw_device_nop,
1223 /* special states for devices gone not operational */
1224 [DEV_STATE_DISCONNECTED] = {
1225 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1226 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1227 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1228 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1230 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1231 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1232 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1233 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1234 [DEV_EVENT_VERIFY] = ccw_device_nop,
1236 [DEV_STATE_CMFCHANGE] = {
1237 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1238 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1239 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1240 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1242 [DEV_STATE_CMFUPDATE] = {
1243 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1244 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1245 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1246 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1250 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);