[PATCH] V4L: 926: Saa7134 alsa can only be autoloaded after saa7134 is active
[linux-2.6/mini2440.git] / drivers / s390 / cio / device_fsm.c
blobc1c89f4fd4e3c3815c8fe7c9ddfaab2d4b3e4084
1 /*
2 * drivers/s390/cio/device_fsm.c
3 * finite state machine for device handling
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Cornelia Huck(cohuck@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
11 #include <linux/module.h>
12 #include <linux/config.h>
13 #include <linux/init.h>
14 #include <linux/jiffies.h>
15 #include <linux/string.h>
17 #include <asm/ccwdev.h>
18 #include <asm/cio.h>
20 #include "cio.h"
21 #include "cio_debug.h"
22 #include "css.h"
23 #include "device.h"
24 #include "chsc.h"
25 #include "ioasm.h"
27 int
28 device_is_online(struct subchannel *sch)
30 struct ccw_device *cdev;
32 if (!sch->dev.driver_data)
33 return 0;
34 cdev = sch->dev.driver_data;
35 return (cdev->private->state == DEV_STATE_ONLINE);
38 int
39 device_is_disconnected(struct subchannel *sch)
41 struct ccw_device *cdev;
43 if (!sch->dev.driver_data)
44 return 0;
45 cdev = sch->dev.driver_data;
46 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
47 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
50 void
51 device_set_disconnected(struct subchannel *sch)
53 struct ccw_device *cdev;
55 if (!sch->dev.driver_data)
56 return;
57 cdev = sch->dev.driver_data;
58 ccw_device_set_timeout(cdev, 0);
59 cdev->private->flags.fake_irb = 0;
60 cdev->private->state = DEV_STATE_DISCONNECTED;
63 void
64 device_set_waiting(struct subchannel *sch)
66 struct ccw_device *cdev;
68 if (!sch->dev.driver_data)
69 return;
70 cdev = sch->dev.driver_data;
71 ccw_device_set_timeout(cdev, 10*HZ);
72 cdev->private->state = DEV_STATE_WAIT4IO;
76 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
78 static void
79 ccw_device_timeout(unsigned long data)
81 struct ccw_device *cdev;
83 cdev = (struct ccw_device *) data;
84 spin_lock_irq(cdev->ccwlock);
85 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
86 spin_unlock_irq(cdev->ccwlock);
90 * Set timeout
92 void
93 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
95 if (expires == 0) {
96 del_timer(&cdev->private->timer);
97 return;
99 if (timer_pending(&cdev->private->timer)) {
100 if (mod_timer(&cdev->private->timer, jiffies + expires))
101 return;
103 cdev->private->timer.function = ccw_device_timeout;
104 cdev->private->timer.data = (unsigned long) cdev;
105 cdev->private->timer.expires = jiffies + expires;
106 add_timer(&cdev->private->timer);
109 /* Kill any pending timers after machine check. */
110 void
111 device_kill_pending_timer(struct subchannel *sch)
113 struct ccw_device *cdev;
115 if (!sch->dev.driver_data)
116 return;
117 cdev = sch->dev.driver_data;
118 ccw_device_set_timeout(cdev, 0);
122 * Cancel running i/o. This is called repeatedly since halt/clear are
123 * asynchronous operations. We do one try with cio_cancel, two tries
124 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
125 * Returns 0 if device now idle, -ENODEV for device not operational and
126 * -EBUSY if an interrupt is expected (either from halt/clear or from a
127 * status pending).
130 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
132 struct subchannel *sch;
133 int ret;
135 sch = to_subchannel(cdev->dev.parent);
136 ret = stsch(sch->irq, &sch->schib);
137 if (ret || !sch->schib.pmcw.dnv)
138 return -ENODEV;
139 if (!sch->schib.pmcw.ena || sch->schib.scsw.actl == 0)
140 /* Not operational or no activity -> done. */
141 return 0;
142 /* Stage 1: cancel io. */
143 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
144 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
145 ret = cio_cancel(sch);
146 if (ret != -EINVAL)
147 return ret;
148 /* cancel io unsuccessful. From now on it is asynchronous. */
149 cdev->private->iretry = 3; /* 3 halt retries. */
151 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
152 /* Stage 2: halt io. */
153 if (cdev->private->iretry) {
154 cdev->private->iretry--;
155 ret = cio_halt(sch);
156 return (ret == 0) ? -EBUSY : ret;
158 /* halt io unsuccessful. */
159 cdev->private->iretry = 255; /* 255 clear retries. */
161 /* Stage 3: clear io. */
162 if (cdev->private->iretry) {
163 cdev->private->iretry--;
164 ret = cio_clear (sch);
165 return (ret == 0) ? -EBUSY : ret;
167 panic("Can't stop i/o on subchannel.\n");
170 static int
171 ccw_device_handle_oper(struct ccw_device *cdev)
173 struct subchannel *sch;
175 sch = to_subchannel(cdev->dev.parent);
176 cdev->private->flags.recog_done = 1;
178 * Check if cu type and device type still match. If
179 * not, it is certainly another device and we have to
180 * de- and re-register. Also check here for non-matching devno.
182 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
183 cdev->id.cu_model != cdev->private->senseid.cu_model ||
184 cdev->id.dev_type != cdev->private->senseid.dev_type ||
185 cdev->id.dev_model != cdev->private->senseid.dev_model ||
186 cdev->private->devno != sch->schib.pmcw.dev) {
187 PREPARE_WORK(&cdev->private->kick_work,
188 ccw_device_do_unreg_rereg, (void *)cdev);
189 queue_work(ccw_device_work, &cdev->private->kick_work);
190 return 0;
192 cdev->private->flags.donotify = 1;
193 return 1;
197 * The machine won't give us any notification by machine check if a chpid has
198 * been varied online on the SE so we have to find out by magic (i. e. driving
199 * the channel subsystem to device selection and updating our path masks).
201 static inline void
202 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
204 int mask, i;
206 for (i = 0; i<8; i++) {
207 mask = 0x80 >> i;
208 if (!(sch->lpm & mask))
209 continue;
210 if (old_lpm & mask)
211 continue;
212 chpid_is_actually_online(sch->schib.pmcw.chpid[i]);
217 * Stop device recognition.
219 static void
220 ccw_device_recog_done(struct ccw_device *cdev, int state)
222 struct subchannel *sch;
223 int notify, old_lpm, same_dev;
225 sch = to_subchannel(cdev->dev.parent);
227 ccw_device_set_timeout(cdev, 0);
228 cio_disable_subchannel(sch);
230 * Now that we tried recognition, we have performed device selection
231 * through ssch() and the path information is up to date.
233 old_lpm = sch->lpm;
234 stsch(sch->irq, &sch->schib);
235 sch->lpm = sch->schib.pmcw.pim &
236 sch->schib.pmcw.pam &
237 sch->schib.pmcw.pom &
238 sch->opm;
239 /* Check since device may again have become not operational. */
240 if (!sch->schib.pmcw.dnv)
241 state = DEV_STATE_NOT_OPER;
242 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
243 /* Force reprobe on all chpids. */
244 old_lpm = 0;
245 if (sch->lpm != old_lpm)
246 __recover_lost_chpids(sch, old_lpm);
247 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
248 if (state == DEV_STATE_NOT_OPER) {
249 cdev->private->flags.recog_done = 1;
250 cdev->private->state = DEV_STATE_DISCONNECTED;
251 return;
253 /* Boxed devices don't need extra treatment. */
255 notify = 0;
256 same_dev = 0; /* Keep the compiler quiet... */
257 switch (state) {
258 case DEV_STATE_NOT_OPER:
259 CIO_DEBUG(KERN_WARNING, 2,
260 "SenseID : unknown device %04x on subchannel %04x\n",
261 cdev->private->devno, sch->irq);
262 break;
263 case DEV_STATE_OFFLINE:
264 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
265 same_dev = ccw_device_handle_oper(cdev);
266 notify = 1;
268 /* fill out sense information */
269 cdev->id = (struct ccw_device_id) {
270 .cu_type = cdev->private->senseid.cu_type,
271 .cu_model = cdev->private->senseid.cu_model,
272 .dev_type = cdev->private->senseid.dev_type,
273 .dev_model = cdev->private->senseid.dev_model,
275 if (notify) {
276 cdev->private->state = DEV_STATE_OFFLINE;
277 if (same_dev) {
278 /* Get device online again. */
279 ccw_device_online(cdev);
280 wake_up(&cdev->private->wait_q);
282 return;
284 /* Issue device info message. */
285 CIO_DEBUG(KERN_INFO, 2, "SenseID : device %04x reports: "
286 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
287 "%04X/%02X\n", cdev->private->devno,
288 cdev->id.cu_type, cdev->id.cu_model,
289 cdev->id.dev_type, cdev->id.dev_model);
290 break;
291 case DEV_STATE_BOXED:
292 CIO_DEBUG(KERN_WARNING, 2,
293 "SenseID : boxed device %04x on subchannel %04x\n",
294 cdev->private->devno, sch->irq);
295 break;
297 cdev->private->state = state;
298 io_subchannel_recog_done(cdev);
299 if (state != DEV_STATE_NOT_OPER)
300 wake_up(&cdev->private->wait_q);
304 * Function called from device_id.c after sense id has completed.
306 void
307 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
309 switch (err) {
310 case 0:
311 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
312 break;
313 case -ETIME: /* Sense id stopped by timeout. */
314 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
315 break;
316 default:
317 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
318 break;
322 static void
323 ccw_device_oper_notify(void *data)
325 struct ccw_device *cdev;
326 struct subchannel *sch;
327 int ret;
329 cdev = (struct ccw_device *)data;
330 sch = to_subchannel(cdev->dev.parent);
331 ret = (sch->driver && sch->driver->notify) ?
332 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
333 if (!ret)
334 /* Driver doesn't want device back. */
335 ccw_device_do_unreg_rereg((void *)cdev);
336 else
337 wake_up(&cdev->private->wait_q);
341 * Finished with online/offline processing.
343 static void
344 ccw_device_done(struct ccw_device *cdev, int state)
346 struct subchannel *sch;
348 sch = to_subchannel(cdev->dev.parent);
350 if (state != DEV_STATE_ONLINE)
351 cio_disable_subchannel(sch);
353 /* Reset device status. */
354 memset(&cdev->private->irb, 0, sizeof(struct irb));
356 cdev->private->state = state;
359 if (state == DEV_STATE_BOXED)
360 CIO_DEBUG(KERN_WARNING, 2,
361 "Boxed device %04x on subchannel %04x\n",
362 cdev->private->devno, sch->irq);
364 if (cdev->private->flags.donotify) {
365 cdev->private->flags.donotify = 0;
366 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify,
367 (void *)cdev);
368 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
370 wake_up(&cdev->private->wait_q);
372 if (css_init_done && state != DEV_STATE_ONLINE)
373 put_device (&cdev->dev);
377 * Function called from device_pgid.c after sense path ground has completed.
379 void
380 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
382 struct subchannel *sch;
384 sch = to_subchannel(cdev->dev.parent);
385 switch (err) {
386 case 0:
387 /* Start Path Group verification. */
388 sch->vpm = 0; /* Start with no path groups set. */
389 cdev->private->state = DEV_STATE_VERIFY;
390 ccw_device_verify_start(cdev);
391 break;
392 case -ETIME: /* Sense path group id stopped by timeout. */
393 case -EUSERS: /* device is reserved for someone else. */
394 ccw_device_done(cdev, DEV_STATE_BOXED);
395 break;
396 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
397 cdev->private->options.pgroup = 0;
398 ccw_device_done(cdev, DEV_STATE_ONLINE);
399 break;
400 default:
401 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
402 break;
407 * Start device recognition.
410 ccw_device_recognition(struct ccw_device *cdev)
412 struct subchannel *sch;
413 int ret;
415 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
416 (cdev->private->state != DEV_STATE_BOXED))
417 return -EINVAL;
418 sch = to_subchannel(cdev->dev.parent);
419 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
420 if (ret != 0)
421 /* Couldn't enable the subchannel for i/o. Sick device. */
422 return ret;
424 /* After 60s the device recognition is considered to have failed. */
425 ccw_device_set_timeout(cdev, 60*HZ);
428 * We used to start here with a sense pgid to find out whether a device
429 * is locked by someone else. Unfortunately, the sense pgid command
430 * code has other meanings on devices predating the path grouping
431 * algorithm, so we start with sense id and box the device after an
432 * timeout (or if sense pgid during path verification detects the device
433 * is locked, as may happen on newer devices).
435 cdev->private->flags.recog_done = 0;
436 cdev->private->state = DEV_STATE_SENSE_ID;
437 ccw_device_sense_id_start(cdev);
438 return 0;
442 * Handle timeout in device recognition.
444 static void
445 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
447 int ret;
449 ret = ccw_device_cancel_halt_clear(cdev);
450 switch (ret) {
451 case 0:
452 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
453 break;
454 case -ENODEV:
455 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
456 break;
457 default:
458 ccw_device_set_timeout(cdev, 3*HZ);
463 static void
464 ccw_device_nopath_notify(void *data)
466 struct ccw_device *cdev;
467 struct subchannel *sch;
468 int ret;
470 cdev = (struct ccw_device *)data;
471 sch = to_subchannel(cdev->dev.parent);
472 /* Extra sanity. */
473 if (sch->lpm)
474 return;
475 ret = (sch->driver && sch->driver->notify) ?
476 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
477 if (!ret) {
478 if (get_device(&sch->dev)) {
479 /* Driver doesn't want to keep device. */
480 cio_disable_subchannel(sch);
481 if (get_device(&cdev->dev)) {
482 PREPARE_WORK(&cdev->private->kick_work,
483 ccw_device_call_sch_unregister,
484 (void *)cdev);
485 queue_work(ccw_device_work,
486 &cdev->private->kick_work);
487 } else
488 put_device(&sch->dev);
490 } else {
491 cio_disable_subchannel(sch);
492 ccw_device_set_timeout(cdev, 0);
493 cdev->private->flags.fake_irb = 0;
494 cdev->private->state = DEV_STATE_DISCONNECTED;
495 wake_up(&cdev->private->wait_q);
499 void
500 ccw_device_verify_done(struct ccw_device *cdev, int err)
502 cdev->private->flags.doverify = 0;
503 switch (err) {
504 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
505 cdev->private->options.pgroup = 0;
506 case 0:
507 ccw_device_done(cdev, DEV_STATE_ONLINE);
508 /* Deliver fake irb to device driver, if needed. */
509 if (cdev->private->flags.fake_irb) {
510 memset(&cdev->private->irb, 0, sizeof(struct irb));
511 cdev->private->irb.scsw = (struct scsw) {
512 .cc = 1,
513 .fctl = SCSW_FCTL_START_FUNC,
514 .actl = SCSW_ACTL_START_PEND,
515 .stctl = SCSW_STCTL_STATUS_PEND,
517 cdev->private->flags.fake_irb = 0;
518 if (cdev->handler)
519 cdev->handler(cdev, cdev->private->intparm,
520 &cdev->private->irb);
521 memset(&cdev->private->irb, 0, sizeof(struct irb));
523 break;
524 case -ETIME:
525 ccw_device_done(cdev, DEV_STATE_BOXED);
526 break;
527 default:
528 PREPARE_WORK(&cdev->private->kick_work,
529 ccw_device_nopath_notify, (void *)cdev);
530 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
531 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
532 break;
537 * Get device online.
540 ccw_device_online(struct ccw_device *cdev)
542 struct subchannel *sch;
543 int ret;
545 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
546 (cdev->private->state != DEV_STATE_BOXED))
547 return -EINVAL;
548 sch = to_subchannel(cdev->dev.parent);
549 if (css_init_done && !get_device(&cdev->dev))
550 return -ENODEV;
551 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
552 if (ret != 0) {
553 /* Couldn't enable the subchannel for i/o. Sick device. */
554 if (ret == -ENODEV)
555 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
556 return ret;
558 /* Do we want to do path grouping? */
559 if (!cdev->private->options.pgroup) {
560 /* No, set state online immediately. */
561 ccw_device_done(cdev, DEV_STATE_ONLINE);
562 return 0;
564 /* Do a SensePGID first. */
565 cdev->private->state = DEV_STATE_SENSE_PGID;
566 ccw_device_sense_pgid_start(cdev);
567 return 0;
570 void
571 ccw_device_disband_done(struct ccw_device *cdev, int err)
573 switch (err) {
574 case 0:
575 ccw_device_done(cdev, DEV_STATE_OFFLINE);
576 break;
577 case -ETIME:
578 ccw_device_done(cdev, DEV_STATE_BOXED);
579 break;
580 default:
581 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
582 break;
587 * Shutdown device.
590 ccw_device_offline(struct ccw_device *cdev)
592 struct subchannel *sch;
594 sch = to_subchannel(cdev->dev.parent);
595 if (stsch(sch->irq, &sch->schib) || !sch->schib.pmcw.dnv)
596 return -ENODEV;
597 if (cdev->private->state != DEV_STATE_ONLINE) {
598 if (sch->schib.scsw.actl != 0)
599 return -EBUSY;
600 return -EINVAL;
602 if (sch->schib.scsw.actl != 0)
603 return -EBUSY;
604 /* Are we doing path grouping? */
605 if (!cdev->private->options.pgroup) {
606 /* No, set state offline immediately. */
607 ccw_device_done(cdev, DEV_STATE_OFFLINE);
608 return 0;
610 /* Start Set Path Group commands. */
611 cdev->private->state = DEV_STATE_DISBAND_PGID;
612 ccw_device_disband_start(cdev);
613 return 0;
617 * Handle timeout in device online/offline process.
619 static void
620 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
622 int ret;
624 ret = ccw_device_cancel_halt_clear(cdev);
625 switch (ret) {
626 case 0:
627 ccw_device_done(cdev, DEV_STATE_BOXED);
628 break;
629 case -ENODEV:
630 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
631 break;
632 default:
633 ccw_device_set_timeout(cdev, 3*HZ);
638 * Handle not oper event in device recognition.
640 static void
641 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
643 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
647 * Handle not operational event while offline.
649 static void
650 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
652 struct subchannel *sch;
654 cdev->private->state = DEV_STATE_NOT_OPER;
655 sch = to_subchannel(cdev->dev.parent);
656 if (get_device(&cdev->dev)) {
657 PREPARE_WORK(&cdev->private->kick_work,
658 ccw_device_call_sch_unregister, (void *)cdev);
659 queue_work(ccw_device_work, &cdev->private->kick_work);
661 wake_up(&cdev->private->wait_q);
665 * Handle not operational event while online.
667 static void
668 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
670 struct subchannel *sch;
672 sch = to_subchannel(cdev->dev.parent);
673 if (sch->driver->notify &&
674 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
675 ccw_device_set_timeout(cdev, 0);
676 cdev->private->flags.fake_irb = 0;
677 cdev->private->state = DEV_STATE_DISCONNECTED;
678 wake_up(&cdev->private->wait_q);
679 return;
681 cdev->private->state = DEV_STATE_NOT_OPER;
682 cio_disable_subchannel(sch);
683 if (sch->schib.scsw.actl != 0) {
684 // FIXME: not-oper indication to device driver ?
685 ccw_device_call_handler(cdev);
687 if (get_device(&cdev->dev)) {
688 PREPARE_WORK(&cdev->private->kick_work,
689 ccw_device_call_sch_unregister, (void *)cdev);
690 queue_work(ccw_device_work, &cdev->private->kick_work);
692 wake_up(&cdev->private->wait_q);
696 * Handle path verification event.
698 static void
699 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
701 struct subchannel *sch;
703 if (!cdev->private->options.pgroup)
704 return;
705 if (cdev->private->state == DEV_STATE_W4SENSE) {
706 cdev->private->flags.doverify = 1;
707 return;
709 sch = to_subchannel(cdev->dev.parent);
711 * Since we might not just be coming from an interrupt from the
712 * subchannel we have to update the schib.
714 stsch(sch->irq, &sch->schib);
716 if (sch->schib.scsw.actl != 0 ||
717 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
719 * No final status yet or final status not yet delivered
720 * to the device driver. Can't do path verfication now,
721 * delay until final status was delivered.
723 cdev->private->flags.doverify = 1;
724 return;
726 /* Device is idle, we can do the path verification. */
727 cdev->private->state = DEV_STATE_VERIFY;
728 ccw_device_verify_start(cdev);
732 * Got an interrupt for a normal io (state online).
734 static void
735 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
737 struct irb *irb;
739 irb = (struct irb *) __LC_IRB;
740 /* Check for unsolicited interrupt. */
741 if ((irb->scsw.stctl ==
742 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
743 && (!irb->scsw.cc)) {
744 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
745 !irb->esw.esw0.erw.cons) {
746 /* Unit check but no sense data. Need basic sense. */
747 if (ccw_device_do_sense(cdev, irb) != 0)
748 goto call_handler_unsol;
749 memcpy(irb, &cdev->private->irb, sizeof(struct irb));
750 cdev->private->state = DEV_STATE_W4SENSE;
751 cdev->private->intparm = 0;
752 return;
754 call_handler_unsol:
755 if (cdev->handler)
756 cdev->handler (cdev, 0, irb);
757 return;
759 /* Accumulate status and find out if a basic sense is needed. */
760 ccw_device_accumulate_irb(cdev, irb);
761 if (cdev->private->flags.dosense) {
762 if (ccw_device_do_sense(cdev, irb) == 0) {
763 cdev->private->state = DEV_STATE_W4SENSE;
765 return;
767 /* Call the handler. */
768 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
769 /* Start delayed path verification. */
770 ccw_device_online_verify(cdev, 0);
774 * Got an timeout in online state.
776 static void
777 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
779 int ret;
781 ccw_device_set_timeout(cdev, 0);
782 ret = ccw_device_cancel_halt_clear(cdev);
783 if (ret == -EBUSY) {
784 ccw_device_set_timeout(cdev, 3*HZ);
785 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
786 return;
788 if (ret == -ENODEV) {
789 struct subchannel *sch;
791 sch = to_subchannel(cdev->dev.parent);
792 if (!sch->lpm) {
793 PREPARE_WORK(&cdev->private->kick_work,
794 ccw_device_nopath_notify, (void *)cdev);
795 queue_work(ccw_device_notify_work,
796 &cdev->private->kick_work);
797 } else
798 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
799 } else if (cdev->handler)
800 cdev->handler(cdev, cdev->private->intparm,
801 ERR_PTR(-ETIMEDOUT));
805 * Got an interrupt for a basic sense.
807 void
808 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
810 struct irb *irb;
812 irb = (struct irb *) __LC_IRB;
813 /* Check for unsolicited interrupt. */
814 if (irb->scsw.stctl ==
815 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
816 if (irb->scsw.cc == 1)
817 /* Basic sense hasn't started. Try again. */
818 ccw_device_do_sense(cdev, irb);
819 else {
820 printk("Huh? %s(%s): unsolicited interrupt...\n",
821 __FUNCTION__, cdev->dev.bus_id);
822 if (cdev->handler)
823 cdev->handler (cdev, 0, irb);
825 return;
827 /* Add basic sense info to irb. */
828 ccw_device_accumulate_basic_sense(cdev, irb);
829 if (cdev->private->flags.dosense) {
830 /* Another basic sense is needed. */
831 ccw_device_do_sense(cdev, irb);
832 return;
834 cdev->private->state = DEV_STATE_ONLINE;
835 /* Call the handler. */
836 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
837 /* Start delayed path verification. */
838 ccw_device_online_verify(cdev, 0);
841 static void
842 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
844 struct irb *irb;
846 irb = (struct irb *) __LC_IRB;
847 /* Accumulate status. We don't do basic sense. */
848 ccw_device_accumulate_irb(cdev, irb);
849 /* Try to start delayed device verification. */
850 ccw_device_online_verify(cdev, 0);
851 /* Note: Don't call handler for cio initiated clear! */
854 static void
855 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
857 struct subchannel *sch;
859 sch = to_subchannel(cdev->dev.parent);
860 ccw_device_set_timeout(cdev, 0);
861 /* OK, i/o is dead now. Call interrupt handler. */
862 cdev->private->state = DEV_STATE_ONLINE;
863 if (cdev->handler)
864 cdev->handler(cdev, cdev->private->intparm,
865 ERR_PTR(-ETIMEDOUT));
866 if (!sch->lpm) {
867 PREPARE_WORK(&cdev->private->kick_work,
868 ccw_device_nopath_notify, (void *)cdev);
869 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
870 } else if (cdev->private->flags.doverify)
871 /* Start delayed path verification. */
872 ccw_device_online_verify(cdev, 0);
875 static void
876 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
878 int ret;
880 ret = ccw_device_cancel_halt_clear(cdev);
881 if (ret == -EBUSY) {
882 ccw_device_set_timeout(cdev, 3*HZ);
883 return;
885 if (ret == -ENODEV) {
886 struct subchannel *sch;
888 sch = to_subchannel(cdev->dev.parent);
889 if (!sch->lpm) {
890 PREPARE_WORK(&cdev->private->kick_work,
891 ccw_device_nopath_notify, (void *)cdev);
892 queue_work(ccw_device_notify_work,
893 &cdev->private->kick_work);
894 } else
895 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
896 return;
898 //FIXME: Can we get here?
899 cdev->private->state = DEV_STATE_ONLINE;
900 if (cdev->handler)
901 cdev->handler(cdev, cdev->private->intparm,
902 ERR_PTR(-ETIMEDOUT));
905 static void
906 ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event)
908 struct irb *irb;
909 struct subchannel *sch;
911 irb = (struct irb *) __LC_IRB;
913 * Accumulate status and find out if a basic sense is needed.
914 * This is fine since we have already adapted the lpm.
916 ccw_device_accumulate_irb(cdev, irb);
917 if (cdev->private->flags.dosense) {
918 if (ccw_device_do_sense(cdev, irb) == 0) {
919 cdev->private->state = DEV_STATE_W4SENSE;
921 return;
924 /* Iff device is idle, reset timeout. */
925 sch = to_subchannel(cdev->dev.parent);
926 if (!stsch(sch->irq, &sch->schib))
927 if (sch->schib.scsw.actl == 0)
928 ccw_device_set_timeout(cdev, 0);
929 /* Call the handler. */
930 ccw_device_call_handler(cdev);
931 if (!sch->lpm) {
932 PREPARE_WORK(&cdev->private->kick_work,
933 ccw_device_nopath_notify, (void *)cdev);
934 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
935 } else if (cdev->private->flags.doverify)
936 ccw_device_online_verify(cdev, 0);
939 static void
940 ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event)
942 int ret;
943 struct subchannel *sch;
945 sch = to_subchannel(cdev->dev.parent);
946 ccw_device_set_timeout(cdev, 0);
947 ret = ccw_device_cancel_halt_clear(cdev);
948 if (ret == -EBUSY) {
949 ccw_device_set_timeout(cdev, 3*HZ);
950 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
951 return;
953 if (ret == -ENODEV) {
954 if (!sch->lpm) {
955 PREPARE_WORK(&cdev->private->kick_work,
956 ccw_device_nopath_notify, (void *)cdev);
957 queue_work(ccw_device_notify_work,
958 &cdev->private->kick_work);
959 } else
960 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
961 return;
963 if (cdev->handler)
964 cdev->handler(cdev, cdev->private->intparm,
965 ERR_PTR(-ETIMEDOUT));
966 if (!sch->lpm) {
967 PREPARE_WORK(&cdev->private->kick_work,
968 ccw_device_nopath_notify, (void *)cdev);
969 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
970 } else if (cdev->private->flags.doverify)
971 /* Start delayed path verification. */
972 ccw_device_online_verify(cdev, 0);
975 static void
976 ccw_device_wait4io_verify(struct ccw_device *cdev, enum dev_event dev_event)
978 /* When the I/O has terminated, we have to start verification. */
979 if (cdev->private->options.pgroup)
980 cdev->private->flags.doverify = 1;
983 static void
984 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
986 struct irb *irb;
988 switch (dev_event) {
989 case DEV_EVENT_INTERRUPT:
990 irb = (struct irb *) __LC_IRB;
991 /* Check for unsolicited interrupt. */
992 if ((irb->scsw.stctl ==
993 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
994 (!irb->scsw.cc))
995 /* FIXME: we should restart stlck here, but this
996 * is extremely unlikely ... */
997 goto out_wakeup;
999 ccw_device_accumulate_irb(cdev, irb);
1000 /* We don't care about basic sense etc. */
1001 break;
1002 default: /* timeout */
1003 break;
1005 out_wakeup:
1006 wake_up(&cdev->private->wait_q);
1009 static void
1010 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1012 struct subchannel *sch;
1014 sch = to_subchannel(cdev->dev.parent);
1015 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1016 /* Couldn't enable the subchannel for i/o. Sick device. */
1017 return;
1019 /* After 60s the device recognition is considered to have failed. */
1020 ccw_device_set_timeout(cdev, 60*HZ);
1022 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1023 ccw_device_sense_id_start(cdev);
1026 void
1027 device_trigger_reprobe(struct subchannel *sch)
1029 struct ccw_device *cdev;
1031 if (!sch->dev.driver_data)
1032 return;
1033 cdev = sch->dev.driver_data;
1034 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1035 return;
1037 /* Update some values. */
1038 if (stsch(sch->irq, &sch->schib))
1039 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.pim &
1046 sch->schib.pmcw.pam &
1047 sch->schib.pmcw.pom &
1048 sch->opm;
1049 /* Re-set some bits in the pmcw that were lost. */
1050 sch->schib.pmcw.isc = 3;
1051 sch->schib.pmcw.csense = 1;
1052 sch->schib.pmcw.ena = 0;
1053 if ((sch->lpm & (sch->lpm - 1)) != 0)
1054 sch->schib.pmcw.mp = 1;
1055 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1056 /* We should also udate ssd info, but this has to wait. */
1057 ccw_device_start_id(cdev, 0);
1060 static void
1061 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1063 struct subchannel *sch;
1065 sch = to_subchannel(cdev->dev.parent);
1067 * An interrupt in state offline means a previous disable was not
1068 * successful. Try again.
1070 cio_disable_subchannel(sch);
1073 static void
1074 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1076 retry_set_schib(cdev);
1077 cdev->private->state = DEV_STATE_ONLINE;
1078 dev_fsm_event(cdev, dev_event);
1082 static void
1083 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1085 ccw_device_set_timeout(cdev, 0);
1086 if (dev_event == DEV_EVENT_NOTOPER)
1087 cdev->private->state = DEV_STATE_NOT_OPER;
1088 else
1089 cdev->private->state = DEV_STATE_OFFLINE;
1090 wake_up(&cdev->private->wait_q);
1093 static void
1094 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1096 int ret;
1098 ret = ccw_device_cancel_halt_clear(cdev);
1099 switch (ret) {
1100 case 0:
1101 cdev->private->state = DEV_STATE_OFFLINE;
1102 wake_up(&cdev->private->wait_q);
1103 break;
1104 case -ENODEV:
1105 cdev->private->state = DEV_STATE_NOT_OPER;
1106 wake_up(&cdev->private->wait_q);
1107 break;
1108 default:
1109 ccw_device_set_timeout(cdev, HZ/10);
1114 * No operation action. This is used e.g. to ignore a timeout event in
1115 * state offline.
1117 static void
1118 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1123 * Bug operation action.
1125 static void
1126 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1128 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1129 cdev->private->state, dev_event);
1130 BUG();
1134 * device statemachine
1136 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1137 [DEV_STATE_NOT_OPER] = {
1138 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1139 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1140 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1141 [DEV_EVENT_VERIFY] = ccw_device_nop,
1143 [DEV_STATE_SENSE_PGID] = {
1144 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1145 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1146 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1147 [DEV_EVENT_VERIFY] = ccw_device_nop,
1149 [DEV_STATE_SENSE_ID] = {
1150 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1151 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1152 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1153 [DEV_EVENT_VERIFY] = ccw_device_nop,
1155 [DEV_STATE_OFFLINE] = {
1156 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1157 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1158 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1159 [DEV_EVENT_VERIFY] = ccw_device_nop,
1161 [DEV_STATE_VERIFY] = {
1162 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1163 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1164 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1165 [DEV_EVENT_VERIFY] = ccw_device_nop,
1167 [DEV_STATE_ONLINE] = {
1168 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1169 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1170 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1171 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1173 [DEV_STATE_W4SENSE] = {
1174 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1175 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1176 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1177 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1179 [DEV_STATE_DISBAND_PGID] = {
1180 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1181 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1182 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1183 [DEV_EVENT_VERIFY] = ccw_device_nop,
1185 [DEV_STATE_BOXED] = {
1186 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1187 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1188 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1189 [DEV_EVENT_VERIFY] = ccw_device_nop,
1191 /* states to wait for i/o completion before doing something */
1192 [DEV_STATE_CLEAR_VERIFY] = {
1193 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1194 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1195 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1196 [DEV_EVENT_VERIFY] = ccw_device_nop,
1198 [DEV_STATE_TIMEOUT_KILL] = {
1199 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1200 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1201 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1202 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1204 [DEV_STATE_WAIT4IO] = {
1205 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1206 [DEV_EVENT_INTERRUPT] = ccw_device_wait4io_irq,
1207 [DEV_EVENT_TIMEOUT] = ccw_device_wait4io_timeout,
1208 [DEV_EVENT_VERIFY] = ccw_device_wait4io_verify,
1210 [DEV_STATE_QUIESCE] = {
1211 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1212 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1213 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1214 [DEV_EVENT_VERIFY] = ccw_device_nop,
1216 /* special states for devices gone not operational */
1217 [DEV_STATE_DISCONNECTED] = {
1218 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1219 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1220 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1221 [DEV_EVENT_VERIFY] = ccw_device_nop,
1223 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1224 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1225 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1226 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1227 [DEV_EVENT_VERIFY] = ccw_device_nop,
1229 [DEV_STATE_CMFCHANGE] = {
1230 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1231 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1232 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1233 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1238 * io_subchannel_irq is called for "real" interrupts or for status
1239 * pending conditions on msch.
1241 void
1242 io_subchannel_irq (struct device *pdev)
1244 struct ccw_device *cdev;
1246 cdev = to_subchannel(pdev)->dev.driver_data;
1248 CIO_TRACE_EVENT (3, "IRQ");
1249 CIO_TRACE_EVENT (3, pdev->bus_id);
1250 if (cdev)
1251 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1254 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);