[S390] cio: Introduce struct chp_id.
[linux-2.6/mini2440.git] / drivers / s390 / cio / device_fsm.c
blob5f5ee1eef07a33c88341197dd079e0471b544fd4
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 (cornelia.huck@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/jiffies.h>
14 #include <linux/string.h>
16 #include <asm/ccwdev.h>
17 #include <asm/cio.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 "chpid.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 device_set_intretry(struct subchannel *sch)
65 struct ccw_device *cdev;
67 cdev = sch->dev.driver_data;
68 if (!cdev)
69 return;
70 cdev->private->flags.intretry = 1;
73 int device_trigger_verify(struct subchannel *sch)
75 struct ccw_device *cdev;
77 cdev = sch->dev.driver_data;
78 if (!cdev || !cdev->online)
79 return -EINVAL;
80 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
81 return 0;
85 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
87 static void
88 ccw_device_timeout(unsigned long data)
90 struct ccw_device *cdev;
92 cdev = (struct ccw_device *) data;
93 spin_lock_irq(cdev->ccwlock);
94 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
95 spin_unlock_irq(cdev->ccwlock);
99 * Set timeout
101 void
102 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
104 if (expires == 0) {
105 del_timer(&cdev->private->timer);
106 return;
108 if (timer_pending(&cdev->private->timer)) {
109 if (mod_timer(&cdev->private->timer, jiffies + expires))
110 return;
112 cdev->private->timer.function = ccw_device_timeout;
113 cdev->private->timer.data = (unsigned long) cdev;
114 cdev->private->timer.expires = jiffies + expires;
115 add_timer(&cdev->private->timer);
118 /* Kill any pending timers after machine check. */
119 void
120 device_kill_pending_timer(struct subchannel *sch)
122 struct ccw_device *cdev;
124 if (!sch->dev.driver_data)
125 return;
126 cdev = sch->dev.driver_data;
127 ccw_device_set_timeout(cdev, 0);
131 * Cancel running i/o. This is called repeatedly since halt/clear are
132 * asynchronous operations. We do one try with cio_cancel, two tries
133 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
134 * Returns 0 if device now idle, -ENODEV for device not operational and
135 * -EBUSY if an interrupt is expected (either from halt/clear or from a
136 * status pending).
139 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
141 struct subchannel *sch;
142 int ret;
144 sch = to_subchannel(cdev->dev.parent);
145 ret = stsch(sch->schid, &sch->schib);
146 if (ret || !sch->schib.pmcw.dnv)
147 return -ENODEV;
148 if (!sch->schib.pmcw.ena)
149 /* Not operational -> done. */
150 return 0;
151 /* Stage 1: cancel io. */
152 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
153 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
154 ret = cio_cancel(sch);
155 if (ret != -EINVAL)
156 return ret;
157 /* cancel io unsuccessful. From now on it is asynchronous. */
158 cdev->private->iretry = 3; /* 3 halt retries. */
160 if (!(sch->schib.scsw.actl & 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 static int
181 ccw_device_handle_oper(struct ccw_device *cdev)
183 struct subchannel *sch;
185 sch = to_subchannel(cdev->dev.parent);
186 cdev->private->flags.recog_done = 1;
188 * Check if cu type and device type still match. If
189 * not, it is certainly another device and we have to
190 * de- and re-register.
192 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
193 cdev->id.cu_model != cdev->private->senseid.cu_model ||
194 cdev->id.dev_type != cdev->private->senseid.dev_type ||
195 cdev->id.dev_model != cdev->private->senseid.dev_model) {
196 PREPARE_WORK(&cdev->private->kick_work,
197 ccw_device_do_unreg_rereg);
198 queue_work(ccw_device_work, &cdev->private->kick_work);
199 return 0;
201 cdev->private->flags.donotify = 1;
202 return 1;
206 * The machine won't give us any notification by machine check if a chpid has
207 * been varied online on the SE so we have to find out by magic (i. e. driving
208 * the channel subsystem to device selection and updating our path masks).
210 static void
211 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
213 int mask, i;
214 struct chp_id chpid;
216 chp_id_init(&chpid);
217 for (i = 0; i<8; i++) {
218 mask = 0x80 >> i;
219 if (!(sch->lpm & mask))
220 continue;
221 if (old_lpm & mask)
222 continue;
223 chpid.id = sch->schib.pmcw.chpid[i];
224 chpid_is_actually_online(chpid);
229 * Stop device recognition.
231 static void
232 ccw_device_recog_done(struct ccw_device *cdev, int state)
234 struct subchannel *sch;
235 int notify, old_lpm, same_dev;
237 sch = to_subchannel(cdev->dev.parent);
239 ccw_device_set_timeout(cdev, 0);
240 cio_disable_subchannel(sch);
242 * Now that we tried recognition, we have performed device selection
243 * through ssch() and the path information is up to date.
245 old_lpm = sch->lpm;
246 stsch(sch->schid, &sch->schib);
247 sch->lpm = sch->schib.pmcw.pam & sch->opm;
248 /* Check since device may again have become not operational. */
249 if (!sch->schib.pmcw.dnv)
250 state = DEV_STATE_NOT_OPER;
251 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
252 /* Force reprobe on all chpids. */
253 old_lpm = 0;
254 if (sch->lpm != old_lpm)
255 __recover_lost_chpids(sch, old_lpm);
256 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
257 if (state == DEV_STATE_NOT_OPER) {
258 cdev->private->flags.recog_done = 1;
259 cdev->private->state = DEV_STATE_DISCONNECTED;
260 return;
262 /* Boxed devices don't need extra treatment. */
264 notify = 0;
265 same_dev = 0; /* Keep the compiler quiet... */
266 switch (state) {
267 case DEV_STATE_NOT_OPER:
268 CIO_DEBUG(KERN_WARNING, 2,
269 "SenseID : unknown device %04x on subchannel "
270 "0.%x.%04x\n", cdev->private->dev_id.devno,
271 sch->schid.ssid, sch->schid.sch_no);
272 break;
273 case DEV_STATE_OFFLINE:
274 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
275 same_dev = ccw_device_handle_oper(cdev);
276 notify = 1;
278 /* fill out sense information */
279 memset(&cdev->id, 0, sizeof(cdev->id));
280 cdev->id.cu_type = cdev->private->senseid.cu_type;
281 cdev->id.cu_model = cdev->private->senseid.cu_model;
282 cdev->id.dev_type = cdev->private->senseid.dev_type;
283 cdev->id.dev_model = cdev->private->senseid.dev_model;
284 if (notify) {
285 cdev->private->state = DEV_STATE_OFFLINE;
286 if (same_dev) {
287 /* Get device online again. */
288 ccw_device_online(cdev);
289 wake_up(&cdev->private->wait_q);
291 return;
293 /* Issue device info message. */
294 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
295 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
296 "%04X/%02X\n",
297 cdev->private->dev_id.ssid,
298 cdev->private->dev_id.devno,
299 cdev->id.cu_type, cdev->id.cu_model,
300 cdev->id.dev_type, cdev->id.dev_model);
301 break;
302 case DEV_STATE_BOXED:
303 CIO_DEBUG(KERN_WARNING, 2,
304 "SenseID : boxed device %04x on subchannel "
305 "0.%x.%04x\n", cdev->private->dev_id.devno,
306 sch->schid.ssid, sch->schid.sch_no);
307 break;
309 cdev->private->state = state;
310 io_subchannel_recog_done(cdev);
311 if (state != DEV_STATE_NOT_OPER)
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 static void
335 ccw_device_oper_notify(struct work_struct *work)
337 struct ccw_device_private *priv;
338 struct ccw_device *cdev;
339 struct subchannel *sch;
340 int ret;
341 unsigned long flags;
343 priv = container_of(work, struct ccw_device_private, kick_work);
344 cdev = priv->cdev;
345 spin_lock_irqsave(cdev->ccwlock, flags);
346 sch = to_subchannel(cdev->dev.parent);
347 if (sch->driver && sch->driver->notify) {
348 spin_unlock_irqrestore(cdev->ccwlock, flags);
349 ret = sch->driver->notify(&sch->dev, CIO_OPER);
350 spin_lock_irqsave(cdev->ccwlock, flags);
351 } else
352 ret = 0;
353 if (ret) {
354 /* Reenable channel measurements, if needed. */
355 spin_unlock_irqrestore(cdev->ccwlock, flags);
356 cmf_reenable(cdev);
357 spin_lock_irqsave(cdev->ccwlock, flags);
358 wake_up(&cdev->private->wait_q);
360 spin_unlock_irqrestore(cdev->ccwlock, flags);
361 if (!ret)
362 /* Driver doesn't want device back. */
363 ccw_device_do_unreg_rereg(work);
367 * Finished with online/offline processing.
369 static void
370 ccw_device_done(struct ccw_device *cdev, int state)
372 struct subchannel *sch;
374 sch = to_subchannel(cdev->dev.parent);
376 ccw_device_set_timeout(cdev, 0);
378 if (state != DEV_STATE_ONLINE)
379 cio_disable_subchannel(sch);
381 /* Reset device status. */
382 memset(&cdev->private->irb, 0, sizeof(struct irb));
384 cdev->private->state = state;
387 if (state == DEV_STATE_BOXED)
388 CIO_DEBUG(KERN_WARNING, 2,
389 "Boxed device %04x on subchannel %04x\n",
390 cdev->private->dev_id.devno, sch->schid.sch_no);
392 if (cdev->private->flags.donotify) {
393 cdev->private->flags.donotify = 0;
394 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
395 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
397 wake_up(&cdev->private->wait_q);
399 if (css_init_done && state != DEV_STATE_ONLINE)
400 put_device (&cdev->dev);
403 static int cmp_pgid(struct pgid *p1, struct pgid *p2)
405 char *c1;
406 char *c2;
408 c1 = (char *)p1;
409 c2 = (char *)p2;
411 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
414 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
416 int i;
417 int last;
419 last = 0;
420 for (i = 0; i < 8; i++) {
421 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
422 /* No PGID yet */
423 continue;
424 if (cdev->private->pgid[last].inf.ps.state1 ==
425 SNID_STATE1_RESET) {
426 /* First non-zero PGID */
427 last = i;
428 continue;
430 if (cmp_pgid(&cdev->private->pgid[i],
431 &cdev->private->pgid[last]) == 0)
432 /* Non-conflicting PGIDs */
433 continue;
435 /* PGID mismatch, can't pathgroup. */
436 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
437 "0.%x.%04x, can't pathgroup\n",
438 cdev->private->dev_id.ssid,
439 cdev->private->dev_id.devno);
440 cdev->private->options.pgroup = 0;
441 return;
443 if (cdev->private->pgid[last].inf.ps.state1 ==
444 SNID_STATE1_RESET)
445 /* No previous pgid found */
446 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
447 sizeof(struct pgid));
448 else
449 /* Use existing pgid */
450 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
451 sizeof(struct pgid));
455 * Function called from device_pgid.c after sense path ground has completed.
457 void
458 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
460 struct subchannel *sch;
462 sch = to_subchannel(cdev->dev.parent);
463 switch (err) {
464 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
465 cdev->private->options.pgroup = 0;
466 break;
467 case 0: /* success */
468 case -EACCES: /* partial success, some paths not operational */
469 /* Check if all pgids are equal or 0. */
470 __ccw_device_get_common_pgid(cdev);
471 break;
472 case -ETIME: /* Sense path group id stopped by timeout. */
473 case -EUSERS: /* device is reserved for someone else. */
474 ccw_device_done(cdev, DEV_STATE_BOXED);
475 return;
476 default:
477 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
478 return;
480 /* Start Path Group verification. */
481 cdev->private->state = DEV_STATE_VERIFY;
482 cdev->private->flags.doverify = 0;
483 ccw_device_verify_start(cdev);
487 * Start device recognition.
490 ccw_device_recognition(struct ccw_device *cdev)
492 struct subchannel *sch;
493 int ret;
495 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
496 (cdev->private->state != DEV_STATE_BOXED))
497 return -EINVAL;
498 sch = to_subchannel(cdev->dev.parent);
499 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
500 if (ret != 0)
501 /* Couldn't enable the subchannel for i/o. Sick device. */
502 return ret;
504 /* After 60s the device recognition is considered to have failed. */
505 ccw_device_set_timeout(cdev, 60*HZ);
508 * We used to start here with a sense pgid to find out whether a device
509 * is locked by someone else. Unfortunately, the sense pgid command
510 * code has other meanings on devices predating the path grouping
511 * algorithm, so we start with sense id and box the device after an
512 * timeout (or if sense pgid during path verification detects the device
513 * is locked, as may happen on newer devices).
515 cdev->private->flags.recog_done = 0;
516 cdev->private->state = DEV_STATE_SENSE_ID;
517 ccw_device_sense_id_start(cdev);
518 return 0;
522 * Handle timeout in device recognition.
524 static void
525 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
527 int ret;
529 ret = ccw_device_cancel_halt_clear(cdev);
530 switch (ret) {
531 case 0:
532 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
533 break;
534 case -ENODEV:
535 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
536 break;
537 default:
538 ccw_device_set_timeout(cdev, 3*HZ);
543 static void
544 ccw_device_nopath_notify(struct work_struct *work)
546 struct ccw_device_private *priv;
547 struct ccw_device *cdev;
548 struct subchannel *sch;
549 int ret;
550 unsigned long flags;
552 priv = container_of(work, struct ccw_device_private, kick_work);
553 cdev = priv->cdev;
554 spin_lock_irqsave(cdev->ccwlock, flags);
555 sch = to_subchannel(cdev->dev.parent);
556 /* Extra sanity. */
557 if (sch->lpm)
558 goto out_unlock;
559 if (sch->driver && sch->driver->notify) {
560 spin_unlock_irqrestore(cdev->ccwlock, flags);
561 ret = sch->driver->notify(&sch->dev, CIO_NO_PATH);
562 spin_lock_irqsave(cdev->ccwlock, flags);
563 } else
564 ret = 0;
565 if (!ret) {
566 if (get_device(&sch->dev)) {
567 /* Driver doesn't want to keep device. */
568 cio_disable_subchannel(sch);
569 if (get_device(&cdev->dev)) {
570 PREPARE_WORK(&cdev->private->kick_work,
571 ccw_device_call_sch_unregister);
572 queue_work(ccw_device_work,
573 &cdev->private->kick_work);
574 } else
575 put_device(&sch->dev);
577 } else {
578 cio_disable_subchannel(sch);
579 ccw_device_set_timeout(cdev, 0);
580 cdev->private->flags.fake_irb = 0;
581 cdev->private->state = DEV_STATE_DISCONNECTED;
582 wake_up(&cdev->private->wait_q);
584 out_unlock:
585 spin_unlock_irqrestore(cdev->ccwlock, flags);
588 void
589 ccw_device_verify_done(struct ccw_device *cdev, int err)
591 struct subchannel *sch;
593 sch = to_subchannel(cdev->dev.parent);
594 /* Update schib - pom may have changed. */
595 stsch(sch->schid, &sch->schib);
596 /* Update lpm with verified path mask. */
597 sch->lpm = sch->vpm;
598 /* Repeat path verification? */
599 if (cdev->private->flags.doverify) {
600 cdev->private->flags.doverify = 0;
601 ccw_device_verify_start(cdev);
602 return;
604 switch (err) {
605 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
606 cdev->private->options.pgroup = 0;
607 case 0:
608 ccw_device_done(cdev, DEV_STATE_ONLINE);
609 /* Deliver fake irb to device driver, if needed. */
610 if (cdev->private->flags.fake_irb) {
611 memset(&cdev->private->irb, 0, sizeof(struct irb));
612 cdev->private->irb.scsw.cc = 1;
613 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
614 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
615 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
616 cdev->private->flags.fake_irb = 0;
617 if (cdev->handler)
618 cdev->handler(cdev, cdev->private->intparm,
619 &cdev->private->irb);
620 memset(&cdev->private->irb, 0, sizeof(struct irb));
622 break;
623 case -ETIME:
624 /* Reset oper notify indication after verify error. */
625 cdev->private->flags.donotify = 0;
626 ccw_device_done(cdev, DEV_STATE_BOXED);
627 break;
628 default:
629 /* Reset oper notify indication after verify error. */
630 cdev->private->flags.donotify = 0;
631 if (cdev->online) {
632 PREPARE_WORK(&cdev->private->kick_work,
633 ccw_device_nopath_notify);
634 queue_work(ccw_device_notify_work,
635 &cdev->private->kick_work);
636 } else
637 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
638 break;
643 * Get device online.
646 ccw_device_online(struct ccw_device *cdev)
648 struct subchannel *sch;
649 int ret;
651 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
652 (cdev->private->state != DEV_STATE_BOXED))
653 return -EINVAL;
654 sch = to_subchannel(cdev->dev.parent);
655 if (css_init_done && !get_device(&cdev->dev))
656 return -ENODEV;
657 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
658 if (ret != 0) {
659 /* Couldn't enable the subchannel for i/o. Sick device. */
660 if (ret == -ENODEV)
661 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
662 return ret;
664 /* Do we want to do path grouping? */
665 if (!cdev->private->options.pgroup) {
666 /* Start initial path verification. */
667 cdev->private->state = DEV_STATE_VERIFY;
668 cdev->private->flags.doverify = 0;
669 ccw_device_verify_start(cdev);
670 return 0;
672 /* Do a SensePGID first. */
673 cdev->private->state = DEV_STATE_SENSE_PGID;
674 ccw_device_sense_pgid_start(cdev);
675 return 0;
678 void
679 ccw_device_disband_done(struct ccw_device *cdev, int err)
681 switch (err) {
682 case 0:
683 ccw_device_done(cdev, DEV_STATE_OFFLINE);
684 break;
685 case -ETIME:
686 ccw_device_done(cdev, DEV_STATE_BOXED);
687 break;
688 default:
689 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
690 break;
695 * Shutdown device.
698 ccw_device_offline(struct ccw_device *cdev)
700 struct subchannel *sch;
702 if (ccw_device_is_orphan(cdev)) {
703 ccw_device_done(cdev, DEV_STATE_OFFLINE);
704 return 0;
706 sch = to_subchannel(cdev->dev.parent);
707 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
708 return -ENODEV;
709 if (cdev->private->state != DEV_STATE_ONLINE) {
710 if (sch->schib.scsw.actl != 0)
711 return -EBUSY;
712 return -EINVAL;
714 if (sch->schib.scsw.actl != 0)
715 return -EBUSY;
716 /* Are we doing path grouping? */
717 if (!cdev->private->options.pgroup) {
718 /* No, set state offline immediately. */
719 ccw_device_done(cdev, DEV_STATE_OFFLINE);
720 return 0;
722 /* Start Set Path Group commands. */
723 cdev->private->state = DEV_STATE_DISBAND_PGID;
724 ccw_device_disband_start(cdev);
725 return 0;
729 * Handle timeout in device online/offline process.
731 static void
732 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
734 int ret;
736 ret = ccw_device_cancel_halt_clear(cdev);
737 switch (ret) {
738 case 0:
739 ccw_device_done(cdev, DEV_STATE_BOXED);
740 break;
741 case -ENODEV:
742 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
743 break;
744 default:
745 ccw_device_set_timeout(cdev, 3*HZ);
750 * Handle not oper event in device recognition.
752 static void
753 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
755 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
759 * Handle not operational event while offline.
761 static void
762 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
764 struct subchannel *sch;
766 cdev->private->state = DEV_STATE_NOT_OPER;
767 sch = to_subchannel(cdev->dev.parent);
768 if (get_device(&cdev->dev)) {
769 PREPARE_WORK(&cdev->private->kick_work,
770 ccw_device_call_sch_unregister);
771 queue_work(ccw_device_work, &cdev->private->kick_work);
773 wake_up(&cdev->private->wait_q);
777 * Handle not operational event while online.
779 static void
780 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
782 struct subchannel *sch;
783 int ret;
785 sch = to_subchannel(cdev->dev.parent);
786 if (sch->driver->notify) {
787 spin_unlock_irq(cdev->ccwlock);
788 ret = sch->driver->notify(&sch->dev,
789 sch->lpm ? CIO_GONE : CIO_NO_PATH);
790 spin_lock_irq(cdev->ccwlock);
791 } else
792 ret = 0;
793 if (ret) {
794 ccw_device_set_timeout(cdev, 0);
795 cdev->private->flags.fake_irb = 0;
796 cdev->private->state = DEV_STATE_DISCONNECTED;
797 wake_up(&cdev->private->wait_q);
798 return;
800 cdev->private->state = DEV_STATE_NOT_OPER;
801 cio_disable_subchannel(sch);
802 if (sch->schib.scsw.actl != 0) {
803 // FIXME: not-oper indication to device driver ?
804 ccw_device_call_handler(cdev);
806 if (get_device(&cdev->dev)) {
807 PREPARE_WORK(&cdev->private->kick_work,
808 ccw_device_call_sch_unregister);
809 queue_work(ccw_device_work, &cdev->private->kick_work);
811 wake_up(&cdev->private->wait_q);
815 * Handle path verification event.
817 static void
818 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
820 struct subchannel *sch;
822 if (cdev->private->state == DEV_STATE_W4SENSE) {
823 cdev->private->flags.doverify = 1;
824 return;
826 sch = to_subchannel(cdev->dev.parent);
828 * Since we might not just be coming from an interrupt from the
829 * subchannel we have to update the schib.
831 stsch(sch->schid, &sch->schib);
833 if (sch->schib.scsw.actl != 0 ||
834 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
835 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
837 * No final status yet or final status not yet delivered
838 * to the device driver. Can't do path verfication now,
839 * delay until final status was delivered.
841 cdev->private->flags.doverify = 1;
842 return;
844 /* Device is idle, we can do the path verification. */
845 cdev->private->state = DEV_STATE_VERIFY;
846 cdev->private->flags.doverify = 0;
847 ccw_device_verify_start(cdev);
851 * Got an interrupt for a normal io (state online).
853 static void
854 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
856 struct irb *irb;
858 irb = (struct irb *) __LC_IRB;
859 /* Check for unsolicited interrupt. */
860 if ((irb->scsw.stctl ==
861 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
862 && (!irb->scsw.cc)) {
863 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
864 !irb->esw.esw0.erw.cons) {
865 /* Unit check but no sense data. Need basic sense. */
866 if (ccw_device_do_sense(cdev, irb) != 0)
867 goto call_handler_unsol;
868 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
869 cdev->private->state = DEV_STATE_W4SENSE;
870 cdev->private->intparm = 0;
871 return;
873 call_handler_unsol:
874 if (cdev->handler)
875 cdev->handler (cdev, 0, irb);
876 if (cdev->private->flags.doverify)
877 ccw_device_online_verify(cdev, 0);
878 return;
880 /* Accumulate status and find out if a basic sense is needed. */
881 ccw_device_accumulate_irb(cdev, irb);
882 if (cdev->private->flags.dosense) {
883 if (ccw_device_do_sense(cdev, irb) == 0) {
884 cdev->private->state = DEV_STATE_W4SENSE;
886 return;
888 /* Call the handler. */
889 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
890 /* Start delayed path verification. */
891 ccw_device_online_verify(cdev, 0);
895 * Got an timeout in online state.
897 static void
898 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
900 int ret;
902 ccw_device_set_timeout(cdev, 0);
903 ret = ccw_device_cancel_halt_clear(cdev);
904 if (ret == -EBUSY) {
905 ccw_device_set_timeout(cdev, 3*HZ);
906 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
907 return;
909 if (ret == -ENODEV) {
910 struct subchannel *sch;
912 sch = to_subchannel(cdev->dev.parent);
913 if (!sch->lpm) {
914 PREPARE_WORK(&cdev->private->kick_work,
915 ccw_device_nopath_notify);
916 queue_work(ccw_device_notify_work,
917 &cdev->private->kick_work);
918 } else
919 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
920 } else if (cdev->handler)
921 cdev->handler(cdev, cdev->private->intparm,
922 ERR_PTR(-ETIMEDOUT));
926 * Got an interrupt for a basic sense.
928 static void
929 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
931 struct irb *irb;
933 irb = (struct irb *) __LC_IRB;
934 /* Check for unsolicited interrupt. */
935 if (irb->scsw.stctl ==
936 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
937 if (irb->scsw.cc == 1)
938 /* Basic sense hasn't started. Try again. */
939 ccw_device_do_sense(cdev, irb);
940 else {
941 printk(KERN_INFO "Huh? %s(%s): unsolicited "
942 "interrupt...\n",
943 __FUNCTION__, cdev->dev.bus_id);
944 if (cdev->handler)
945 cdev->handler (cdev, 0, irb);
947 return;
950 * Check if a halt or clear has been issued in the meanwhile. If yes,
951 * only deliver the halt/clear interrupt to the device driver as if it
952 * had killed the original request.
954 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
955 /* Retry Basic Sense if requested. */
956 if (cdev->private->flags.intretry) {
957 cdev->private->flags.intretry = 0;
958 ccw_device_do_sense(cdev, irb);
959 return;
961 cdev->private->flags.dosense = 0;
962 memset(&cdev->private->irb, 0, sizeof(struct irb));
963 ccw_device_accumulate_irb(cdev, irb);
964 goto call_handler;
966 /* Add basic sense info to irb. */
967 ccw_device_accumulate_basic_sense(cdev, irb);
968 if (cdev->private->flags.dosense) {
969 /* Another basic sense is needed. */
970 ccw_device_do_sense(cdev, irb);
971 return;
973 call_handler:
974 cdev->private->state = DEV_STATE_ONLINE;
975 /* Call the handler. */
976 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
977 /* Start delayed path verification. */
978 ccw_device_online_verify(cdev, 0);
981 static void
982 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
984 struct irb *irb;
986 irb = (struct irb *) __LC_IRB;
987 /* Accumulate status. We don't do basic sense. */
988 ccw_device_accumulate_irb(cdev, irb);
989 /* Remember to clear irb to avoid residuals. */
990 memset(&cdev->private->irb, 0, sizeof(struct irb));
991 /* Try to start delayed device verification. */
992 ccw_device_online_verify(cdev, 0);
993 /* Note: Don't call handler for cio initiated clear! */
996 static void
997 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
999 struct subchannel *sch;
1001 sch = to_subchannel(cdev->dev.parent);
1002 ccw_device_set_timeout(cdev, 0);
1003 /* Start delayed path verification. */
1004 ccw_device_online_verify(cdev, 0);
1005 /* OK, i/o is dead now. Call interrupt handler. */
1006 if (cdev->handler)
1007 cdev->handler(cdev, cdev->private->intparm,
1008 ERR_PTR(-EIO));
1011 static void
1012 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1014 int ret;
1016 ret = ccw_device_cancel_halt_clear(cdev);
1017 if (ret == -EBUSY) {
1018 ccw_device_set_timeout(cdev, 3*HZ);
1019 return;
1021 /* Start delayed path verification. */
1022 ccw_device_online_verify(cdev, 0);
1023 if (cdev->handler)
1024 cdev->handler(cdev, cdev->private->intparm,
1025 ERR_PTR(-EIO));
1028 void device_kill_io(struct subchannel *sch)
1030 int ret;
1031 struct ccw_device *cdev;
1033 cdev = sch->dev.driver_data;
1034 ret = ccw_device_cancel_halt_clear(cdev);
1035 if (ret == -EBUSY) {
1036 ccw_device_set_timeout(cdev, 3*HZ);
1037 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1038 return;
1040 /* Start delayed path verification. */
1041 ccw_device_online_verify(cdev, 0);
1042 if (cdev->handler)
1043 cdev->handler(cdev, cdev->private->intparm,
1044 ERR_PTR(-EIO));
1047 static void
1048 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
1050 /* Start verification after current task finished. */
1051 cdev->private->flags.doverify = 1;
1054 static void
1055 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1057 struct irb *irb;
1059 switch (dev_event) {
1060 case DEV_EVENT_INTERRUPT:
1061 irb = (struct irb *) __LC_IRB;
1062 /* Check for unsolicited interrupt. */
1063 if ((irb->scsw.stctl ==
1064 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1065 (!irb->scsw.cc))
1066 /* FIXME: we should restart stlck here, but this
1067 * is extremely unlikely ... */
1068 goto out_wakeup;
1070 ccw_device_accumulate_irb(cdev, irb);
1071 /* We don't care about basic sense etc. */
1072 break;
1073 default: /* timeout */
1074 break;
1076 out_wakeup:
1077 wake_up(&cdev->private->wait_q);
1080 static void
1081 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1083 struct subchannel *sch;
1085 sch = to_subchannel(cdev->dev.parent);
1086 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1087 /* Couldn't enable the subchannel for i/o. Sick device. */
1088 return;
1090 /* After 60s the device recognition is considered to have failed. */
1091 ccw_device_set_timeout(cdev, 60*HZ);
1093 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1094 ccw_device_sense_id_start(cdev);
1097 void
1098 device_trigger_reprobe(struct subchannel *sch)
1100 struct ccw_device *cdev;
1102 if (!sch->dev.driver_data)
1103 return;
1104 cdev = sch->dev.driver_data;
1105 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1106 return;
1108 /* Update some values. */
1109 if (stsch(sch->schid, &sch->schib))
1110 return;
1111 if (!sch->schib.pmcw.dnv)
1112 return;
1114 * The pim, pam, pom values may not be accurate, but they are the best
1115 * we have before performing device selection :/
1117 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1118 /* Re-set some bits in the pmcw that were lost. */
1119 sch->schib.pmcw.isc = 3;
1120 sch->schib.pmcw.csense = 1;
1121 sch->schib.pmcw.ena = 0;
1122 if ((sch->lpm & (sch->lpm - 1)) != 0)
1123 sch->schib.pmcw.mp = 1;
1124 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1125 /* We should also udate ssd info, but this has to wait. */
1126 /* Check if this is another device which appeared on the same sch. */
1127 if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1128 PREPARE_WORK(&cdev->private->kick_work,
1129 ccw_device_move_to_orphanage);
1130 queue_work(ccw_device_work, &cdev->private->kick_work);
1131 } else
1132 ccw_device_start_id(cdev, 0);
1135 static void
1136 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1138 struct subchannel *sch;
1140 sch = to_subchannel(cdev->dev.parent);
1142 * An interrupt in state offline means a previous disable was not
1143 * successful. Try again.
1145 cio_disable_subchannel(sch);
1148 static void
1149 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1151 retry_set_schib(cdev);
1152 cdev->private->state = DEV_STATE_ONLINE;
1153 dev_fsm_event(cdev, dev_event);
1156 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1157 enum dev_event dev_event)
1159 cmf_retry_copy_block(cdev);
1160 cdev->private->state = DEV_STATE_ONLINE;
1161 dev_fsm_event(cdev, dev_event);
1164 static void
1165 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1167 ccw_device_set_timeout(cdev, 0);
1168 if (dev_event == DEV_EVENT_NOTOPER)
1169 cdev->private->state = DEV_STATE_NOT_OPER;
1170 else
1171 cdev->private->state = DEV_STATE_OFFLINE;
1172 wake_up(&cdev->private->wait_q);
1175 static void
1176 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1178 int ret;
1180 ret = ccw_device_cancel_halt_clear(cdev);
1181 switch (ret) {
1182 case 0:
1183 cdev->private->state = DEV_STATE_OFFLINE;
1184 wake_up(&cdev->private->wait_q);
1185 break;
1186 case -ENODEV:
1187 cdev->private->state = DEV_STATE_NOT_OPER;
1188 wake_up(&cdev->private->wait_q);
1189 break;
1190 default:
1191 ccw_device_set_timeout(cdev, HZ/10);
1196 * No operation action. This is used e.g. to ignore a timeout event in
1197 * state offline.
1199 static void
1200 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1205 * Bug operation action.
1207 static void
1208 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1210 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1211 cdev->private->state, dev_event);
1212 BUG();
1216 * device statemachine
1218 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1219 [DEV_STATE_NOT_OPER] = {
1220 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1221 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1222 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1223 [DEV_EVENT_VERIFY] = ccw_device_nop,
1225 [DEV_STATE_SENSE_PGID] = {
1226 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1227 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1228 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1229 [DEV_EVENT_VERIFY] = ccw_device_nop,
1231 [DEV_STATE_SENSE_ID] = {
1232 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1233 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1234 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1235 [DEV_EVENT_VERIFY] = ccw_device_nop,
1237 [DEV_STATE_OFFLINE] = {
1238 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1239 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1240 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1241 [DEV_EVENT_VERIFY] = ccw_device_nop,
1243 [DEV_STATE_VERIFY] = {
1244 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1245 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1246 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1247 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1249 [DEV_STATE_ONLINE] = {
1250 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1251 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1252 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1253 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1255 [DEV_STATE_W4SENSE] = {
1256 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1257 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1258 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1259 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1261 [DEV_STATE_DISBAND_PGID] = {
1262 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1263 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1264 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1265 [DEV_EVENT_VERIFY] = ccw_device_nop,
1267 [DEV_STATE_BOXED] = {
1268 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1269 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1270 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1271 [DEV_EVENT_VERIFY] = ccw_device_nop,
1273 /* states to wait for i/o completion before doing something */
1274 [DEV_STATE_CLEAR_VERIFY] = {
1275 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1276 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1277 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1278 [DEV_EVENT_VERIFY] = ccw_device_nop,
1280 [DEV_STATE_TIMEOUT_KILL] = {
1281 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1282 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1283 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1284 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1286 [DEV_STATE_QUIESCE] = {
1287 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1288 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1289 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1290 [DEV_EVENT_VERIFY] = ccw_device_nop,
1292 /* special states for devices gone not operational */
1293 [DEV_STATE_DISCONNECTED] = {
1294 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1295 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1296 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1297 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1299 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1300 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1301 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1302 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1303 [DEV_EVENT_VERIFY] = ccw_device_nop,
1305 [DEV_STATE_CMFCHANGE] = {
1306 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1307 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1308 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1309 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1311 [DEV_STATE_CMFUPDATE] = {
1312 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1313 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1314 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1315 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1320 * io_subchannel_irq is called for "real" interrupts or for status
1321 * pending conditions on msch.
1323 void
1324 io_subchannel_irq (struct device *pdev)
1326 struct ccw_device *cdev;
1328 cdev = to_subchannel(pdev)->dev.driver_data;
1330 CIO_TRACE_EVENT (3, "IRQ");
1331 CIO_TRACE_EVENT (3, pdev->bus_id);
1332 if (cdev)
1333 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1336 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);