[S390] cio: Remove grace period for vary off chpid.
[linux-2.6/zen-sources.git] / drivers / s390 / cio / device_fsm.c
blob44e4a53c5981f0cc3307eff035b8c32a96ae4e08
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"
26 int
27 device_is_online(struct subchannel *sch)
29 struct ccw_device *cdev;
31 if (!sch->dev.driver_data)
32 return 0;
33 cdev = sch->dev.driver_data;
34 return (cdev->private->state == DEV_STATE_ONLINE);
37 int
38 device_is_disconnected(struct subchannel *sch)
40 struct ccw_device *cdev;
42 if (!sch->dev.driver_data)
43 return 0;
44 cdev = sch->dev.driver_data;
45 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
46 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
49 void
50 device_set_disconnected(struct subchannel *sch)
52 struct ccw_device *cdev;
54 if (!sch->dev.driver_data)
55 return;
56 cdev = sch->dev.driver_data;
57 ccw_device_set_timeout(cdev, 0);
58 cdev->private->flags.fake_irb = 0;
59 cdev->private->state = DEV_STATE_DISCONNECTED;
63 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
65 static void
66 ccw_device_timeout(unsigned long data)
68 struct ccw_device *cdev;
70 cdev = (struct ccw_device *) data;
71 spin_lock_irq(cdev->ccwlock);
72 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
73 spin_unlock_irq(cdev->ccwlock);
77 * Set timeout
79 void
80 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
82 if (expires == 0) {
83 del_timer(&cdev->private->timer);
84 return;
86 if (timer_pending(&cdev->private->timer)) {
87 if (mod_timer(&cdev->private->timer, jiffies + expires))
88 return;
90 cdev->private->timer.function = ccw_device_timeout;
91 cdev->private->timer.data = (unsigned long) cdev;
92 cdev->private->timer.expires = jiffies + expires;
93 add_timer(&cdev->private->timer);
96 /* Kill any pending timers after machine check. */
97 void
98 device_kill_pending_timer(struct subchannel *sch)
100 struct ccw_device *cdev;
102 if (!sch->dev.driver_data)
103 return;
104 cdev = sch->dev.driver_data;
105 ccw_device_set_timeout(cdev, 0);
109 * Cancel running i/o. This is called repeatedly since halt/clear are
110 * asynchronous operations. We do one try with cio_cancel, two tries
111 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
112 * Returns 0 if device now idle, -ENODEV for device not operational and
113 * -EBUSY if an interrupt is expected (either from halt/clear or from a
114 * status pending).
117 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
119 struct subchannel *sch;
120 int ret;
122 sch = to_subchannel(cdev->dev.parent);
123 ret = stsch(sch->schid, &sch->schib);
124 if (ret || !sch->schib.pmcw.dnv)
125 return -ENODEV;
126 if (!sch->schib.pmcw.ena || sch->schib.scsw.actl == 0)
127 /* Not operational or no activity -> done. */
128 return 0;
129 /* Stage 1: cancel io. */
130 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
131 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
132 ret = cio_cancel(sch);
133 if (ret != -EINVAL)
134 return ret;
135 /* cancel io unsuccessful. From now on it is asynchronous. */
136 cdev->private->iretry = 3; /* 3 halt retries. */
138 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
139 /* Stage 2: halt io. */
140 if (cdev->private->iretry) {
141 cdev->private->iretry--;
142 ret = cio_halt(sch);
143 if (ret != -EBUSY)
144 return (ret == 0) ? -EBUSY : ret;
146 /* halt io unsuccessful. */
147 cdev->private->iretry = 255; /* 255 clear retries. */
149 /* Stage 3: clear io. */
150 if (cdev->private->iretry) {
151 cdev->private->iretry--;
152 ret = cio_clear (sch);
153 return (ret == 0) ? -EBUSY : ret;
155 panic("Can't stop i/o on subchannel.\n");
158 static int
159 ccw_device_handle_oper(struct ccw_device *cdev)
161 struct subchannel *sch;
163 sch = to_subchannel(cdev->dev.parent);
164 cdev->private->flags.recog_done = 1;
166 * Check if cu type and device type still match. If
167 * not, it is certainly another device and we have to
168 * de- and re-register. Also check here for non-matching devno.
170 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
171 cdev->id.cu_model != cdev->private->senseid.cu_model ||
172 cdev->id.dev_type != cdev->private->senseid.dev_type ||
173 cdev->id.dev_model != cdev->private->senseid.dev_model ||
174 cdev->private->dev_id.devno != sch->schib.pmcw.dev) {
175 PREPARE_WORK(&cdev->private->kick_work,
176 ccw_device_do_unreg_rereg, (void *)cdev);
177 queue_work(ccw_device_work, &cdev->private->kick_work);
178 return 0;
180 cdev->private->flags.donotify = 1;
181 return 1;
185 * The machine won't give us any notification by machine check if a chpid has
186 * been varied online on the SE so we have to find out by magic (i. e. driving
187 * the channel subsystem to device selection and updating our path masks).
189 static inline void
190 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
192 int mask, i;
194 for (i = 0; i<8; i++) {
195 mask = 0x80 >> i;
196 if (!(sch->lpm & mask))
197 continue;
198 if (old_lpm & mask)
199 continue;
200 chpid_is_actually_online(sch->schib.pmcw.chpid[i]);
205 * Stop device recognition.
207 static void
208 ccw_device_recog_done(struct ccw_device *cdev, int state)
210 struct subchannel *sch;
211 int notify, old_lpm, same_dev;
213 sch = to_subchannel(cdev->dev.parent);
215 ccw_device_set_timeout(cdev, 0);
216 cio_disable_subchannel(sch);
218 * Now that we tried recognition, we have performed device selection
219 * through ssch() and the path information is up to date.
221 old_lpm = sch->lpm;
222 stsch(sch->schid, &sch->schib);
223 sch->lpm = sch->schib.pmcw.pam & sch->opm;
224 /* Check since device may again have become not operational. */
225 if (!sch->schib.pmcw.dnv)
226 state = DEV_STATE_NOT_OPER;
227 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
228 /* Force reprobe on all chpids. */
229 old_lpm = 0;
230 if (sch->lpm != old_lpm)
231 __recover_lost_chpids(sch, old_lpm);
232 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
233 if (state == DEV_STATE_NOT_OPER) {
234 cdev->private->flags.recog_done = 1;
235 cdev->private->state = DEV_STATE_DISCONNECTED;
236 return;
238 /* Boxed devices don't need extra treatment. */
240 notify = 0;
241 same_dev = 0; /* Keep the compiler quiet... */
242 switch (state) {
243 case DEV_STATE_NOT_OPER:
244 CIO_DEBUG(KERN_WARNING, 2,
245 "SenseID : unknown device %04x on subchannel "
246 "0.%x.%04x\n", cdev->private->dev_id.devno,
247 sch->schid.ssid, sch->schid.sch_no);
248 break;
249 case DEV_STATE_OFFLINE:
250 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
251 same_dev = ccw_device_handle_oper(cdev);
252 notify = 1;
254 /* fill out sense information */
255 memset(&cdev->id, 0, sizeof(cdev->id));
256 cdev->id.cu_type = cdev->private->senseid.cu_type;
257 cdev->id.cu_model = cdev->private->senseid.cu_model;
258 cdev->id.dev_type = cdev->private->senseid.dev_type;
259 cdev->id.dev_model = cdev->private->senseid.dev_model;
260 if (notify) {
261 cdev->private->state = DEV_STATE_OFFLINE;
262 if (same_dev) {
263 /* Get device online again. */
264 ccw_device_online(cdev);
265 wake_up(&cdev->private->wait_q);
267 return;
269 /* Issue device info message. */
270 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
271 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
272 "%04X/%02X\n",
273 cdev->private->dev_id.ssid,
274 cdev->private->dev_id.devno,
275 cdev->id.cu_type, cdev->id.cu_model,
276 cdev->id.dev_type, cdev->id.dev_model);
277 break;
278 case DEV_STATE_BOXED:
279 CIO_DEBUG(KERN_WARNING, 2,
280 "SenseID : boxed device %04x on subchannel "
281 "0.%x.%04x\n", cdev->private->dev_id.devno,
282 sch->schid.ssid, sch->schid.sch_no);
283 break;
285 cdev->private->state = state;
286 io_subchannel_recog_done(cdev);
287 if (state != DEV_STATE_NOT_OPER)
288 wake_up(&cdev->private->wait_q);
292 * Function called from device_id.c after sense id has completed.
294 void
295 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
297 switch (err) {
298 case 0:
299 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
300 break;
301 case -ETIME: /* Sense id stopped by timeout. */
302 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
303 break;
304 default:
305 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
306 break;
310 static void
311 ccw_device_oper_notify(void *data)
313 struct ccw_device *cdev;
314 struct subchannel *sch;
315 int ret;
317 cdev = (struct ccw_device *)data;
318 sch = to_subchannel(cdev->dev.parent);
319 ret = (sch->driver && sch->driver->notify) ?
320 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
321 if (!ret)
322 /* Driver doesn't want device back. */
323 ccw_device_do_unreg_rereg((void *)cdev);
324 else {
325 /* Reenable channel measurements, if needed. */
326 cmf_reenable(cdev);
327 wake_up(&cdev->private->wait_q);
332 * Finished with online/offline processing.
334 static void
335 ccw_device_done(struct ccw_device *cdev, int state)
337 struct subchannel *sch;
339 sch = to_subchannel(cdev->dev.parent);
341 ccw_device_set_timeout(cdev, 0);
343 if (state != DEV_STATE_ONLINE)
344 cio_disable_subchannel(sch);
346 /* Reset device status. */
347 memset(&cdev->private->irb, 0, sizeof(struct irb));
349 cdev->private->state = state;
352 if (state == DEV_STATE_BOXED)
353 CIO_DEBUG(KERN_WARNING, 2,
354 "Boxed device %04x on subchannel %04x\n",
355 cdev->private->dev_id.devno, sch->schid.sch_no);
357 if (cdev->private->flags.donotify) {
358 cdev->private->flags.donotify = 0;
359 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify,
360 (void *)cdev);
361 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
363 wake_up(&cdev->private->wait_q);
365 if (css_init_done && state != DEV_STATE_ONLINE)
366 put_device (&cdev->dev);
369 static inline int cmp_pgid(struct pgid *p1, struct pgid *p2)
371 char *c1;
372 char *c2;
374 c1 = (char *)p1;
375 c2 = (char *)p2;
377 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
380 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
382 int i;
383 int last;
385 last = 0;
386 for (i = 0; i < 8; i++) {
387 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
388 /* No PGID yet */
389 continue;
390 if (cdev->private->pgid[last].inf.ps.state1 ==
391 SNID_STATE1_RESET) {
392 /* First non-zero PGID */
393 last = i;
394 continue;
396 if (cmp_pgid(&cdev->private->pgid[i],
397 &cdev->private->pgid[last]) == 0)
398 /* Non-conflicting PGIDs */
399 continue;
401 /* PGID mismatch, can't pathgroup. */
402 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
403 "0.%x.%04x, can't pathgroup\n",
404 cdev->private->dev_id.ssid,
405 cdev->private->dev_id.devno);
406 cdev->private->options.pgroup = 0;
407 return;
409 if (cdev->private->pgid[last].inf.ps.state1 ==
410 SNID_STATE1_RESET)
411 /* No previous pgid found */
412 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
413 sizeof(struct pgid));
414 else
415 /* Use existing pgid */
416 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
417 sizeof(struct pgid));
421 * Function called from device_pgid.c after sense path ground has completed.
423 void
424 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
426 struct subchannel *sch;
428 sch = to_subchannel(cdev->dev.parent);
429 switch (err) {
430 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
431 cdev->private->options.pgroup = 0;
432 break;
433 case 0: /* success */
434 case -EACCES: /* partial success, some paths not operational */
435 /* Check if all pgids are equal or 0. */
436 __ccw_device_get_common_pgid(cdev);
437 break;
438 case -ETIME: /* Sense path group id stopped by timeout. */
439 case -EUSERS: /* device is reserved for someone else. */
440 ccw_device_done(cdev, DEV_STATE_BOXED);
441 return;
442 default:
443 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
444 return;
446 /* Start Path Group verification. */
447 cdev->private->state = DEV_STATE_VERIFY;
448 cdev->private->flags.doverify = 0;
449 ccw_device_verify_start(cdev);
453 * Start device recognition.
456 ccw_device_recognition(struct ccw_device *cdev)
458 struct subchannel *sch;
459 int ret;
461 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
462 (cdev->private->state != DEV_STATE_BOXED))
463 return -EINVAL;
464 sch = to_subchannel(cdev->dev.parent);
465 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
466 if (ret != 0)
467 /* Couldn't enable the subchannel for i/o. Sick device. */
468 return ret;
470 /* After 60s the device recognition is considered to have failed. */
471 ccw_device_set_timeout(cdev, 60*HZ);
474 * We used to start here with a sense pgid to find out whether a device
475 * is locked by someone else. Unfortunately, the sense pgid command
476 * code has other meanings on devices predating the path grouping
477 * algorithm, so we start with sense id and box the device after an
478 * timeout (or if sense pgid during path verification detects the device
479 * is locked, as may happen on newer devices).
481 cdev->private->flags.recog_done = 0;
482 cdev->private->state = DEV_STATE_SENSE_ID;
483 ccw_device_sense_id_start(cdev);
484 return 0;
488 * Handle timeout in device recognition.
490 static void
491 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
493 int ret;
495 ret = ccw_device_cancel_halt_clear(cdev);
496 switch (ret) {
497 case 0:
498 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
499 break;
500 case -ENODEV:
501 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
502 break;
503 default:
504 ccw_device_set_timeout(cdev, 3*HZ);
509 static void
510 ccw_device_nopath_notify(void *data)
512 struct ccw_device *cdev;
513 struct subchannel *sch;
514 int ret;
516 cdev = (struct ccw_device *)data;
517 sch = to_subchannel(cdev->dev.parent);
518 /* Extra sanity. */
519 if (sch->lpm)
520 return;
521 ret = (sch->driver && sch->driver->notify) ?
522 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
523 if (!ret) {
524 if (get_device(&sch->dev)) {
525 /* Driver doesn't want to keep device. */
526 cio_disable_subchannel(sch);
527 if (get_device(&cdev->dev)) {
528 PREPARE_WORK(&cdev->private->kick_work,
529 ccw_device_call_sch_unregister,
530 (void *)cdev);
531 queue_work(ccw_device_work,
532 &cdev->private->kick_work);
533 } else
534 put_device(&sch->dev);
536 } else {
537 cio_disable_subchannel(sch);
538 ccw_device_set_timeout(cdev, 0);
539 cdev->private->flags.fake_irb = 0;
540 cdev->private->state = DEV_STATE_DISCONNECTED;
541 wake_up(&cdev->private->wait_q);
545 void
546 ccw_device_verify_done(struct ccw_device *cdev, int err)
548 struct subchannel *sch;
550 sch = to_subchannel(cdev->dev.parent);
551 /* Update schib - pom may have changed. */
552 stsch(sch->schid, &sch->schib);
553 /* Update lpm with verified path mask. */
554 sch->lpm = sch->vpm;
555 /* Repeat path verification? */
556 if (cdev->private->flags.doverify) {
557 cdev->private->flags.doverify = 0;
558 ccw_device_verify_start(cdev);
559 return;
561 switch (err) {
562 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
563 cdev->private->options.pgroup = 0;
564 case 0:
565 ccw_device_done(cdev, DEV_STATE_ONLINE);
566 /* Deliver fake irb to device driver, if needed. */
567 if (cdev->private->flags.fake_irb) {
568 memset(&cdev->private->irb, 0, sizeof(struct irb));
569 cdev->private->irb.scsw.cc = 1;
570 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
571 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
572 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
573 cdev->private->flags.fake_irb = 0;
574 if (cdev->handler)
575 cdev->handler(cdev, cdev->private->intparm,
576 &cdev->private->irb);
577 memset(&cdev->private->irb, 0, sizeof(struct irb));
579 break;
580 case -ETIME:
581 ccw_device_done(cdev, DEV_STATE_BOXED);
582 break;
583 default:
584 PREPARE_WORK(&cdev->private->kick_work,
585 ccw_device_nopath_notify, (void *)cdev);
586 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
587 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
588 break;
593 * Get device online.
596 ccw_device_online(struct ccw_device *cdev)
598 struct subchannel *sch;
599 int ret;
601 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
602 (cdev->private->state != DEV_STATE_BOXED))
603 return -EINVAL;
604 sch = to_subchannel(cdev->dev.parent);
605 if (css_init_done && !get_device(&cdev->dev))
606 return -ENODEV;
607 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
608 if (ret != 0) {
609 /* Couldn't enable the subchannel for i/o. Sick device. */
610 if (ret == -ENODEV)
611 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
612 return ret;
614 /* Do we want to do path grouping? */
615 if (!cdev->private->options.pgroup) {
616 /* Start initial path verification. */
617 cdev->private->state = DEV_STATE_VERIFY;
618 cdev->private->flags.doverify = 0;
619 ccw_device_verify_start(cdev);
620 return 0;
622 /* Do a SensePGID first. */
623 cdev->private->state = DEV_STATE_SENSE_PGID;
624 ccw_device_sense_pgid_start(cdev);
625 return 0;
628 void
629 ccw_device_disband_done(struct ccw_device *cdev, int err)
631 switch (err) {
632 case 0:
633 ccw_device_done(cdev, DEV_STATE_OFFLINE);
634 break;
635 case -ETIME:
636 ccw_device_done(cdev, DEV_STATE_BOXED);
637 break;
638 default:
639 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
640 break;
645 * Shutdown device.
648 ccw_device_offline(struct ccw_device *cdev)
650 struct subchannel *sch;
652 sch = to_subchannel(cdev->dev.parent);
653 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
654 return -ENODEV;
655 if (cdev->private->state != DEV_STATE_ONLINE) {
656 if (sch->schib.scsw.actl != 0)
657 return -EBUSY;
658 return -EINVAL;
660 if (sch->schib.scsw.actl != 0)
661 return -EBUSY;
662 /* Are we doing path grouping? */
663 if (!cdev->private->options.pgroup) {
664 /* No, set state offline immediately. */
665 ccw_device_done(cdev, DEV_STATE_OFFLINE);
666 return 0;
668 /* Start Set Path Group commands. */
669 cdev->private->state = DEV_STATE_DISBAND_PGID;
670 ccw_device_disband_start(cdev);
671 return 0;
675 * Handle timeout in device online/offline process.
677 static void
678 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
680 int ret;
682 ret = ccw_device_cancel_halt_clear(cdev);
683 switch (ret) {
684 case 0:
685 ccw_device_done(cdev, DEV_STATE_BOXED);
686 break;
687 case -ENODEV:
688 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
689 break;
690 default:
691 ccw_device_set_timeout(cdev, 3*HZ);
696 * Handle not oper event in device recognition.
698 static void
699 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
701 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
705 * Handle not operational event while offline.
707 static void
708 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
710 struct subchannel *sch;
712 cdev->private->state = DEV_STATE_NOT_OPER;
713 sch = to_subchannel(cdev->dev.parent);
714 if (get_device(&cdev->dev)) {
715 PREPARE_WORK(&cdev->private->kick_work,
716 ccw_device_call_sch_unregister, (void *)cdev);
717 queue_work(ccw_device_work, &cdev->private->kick_work);
719 wake_up(&cdev->private->wait_q);
723 * Handle not operational event while online.
725 static void
726 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
728 struct subchannel *sch;
730 sch = to_subchannel(cdev->dev.parent);
731 if (sch->driver->notify &&
732 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
733 ccw_device_set_timeout(cdev, 0);
734 cdev->private->flags.fake_irb = 0;
735 cdev->private->state = DEV_STATE_DISCONNECTED;
736 wake_up(&cdev->private->wait_q);
737 return;
739 cdev->private->state = DEV_STATE_NOT_OPER;
740 cio_disable_subchannel(sch);
741 if (sch->schib.scsw.actl != 0) {
742 // FIXME: not-oper indication to device driver ?
743 ccw_device_call_handler(cdev);
745 if (get_device(&cdev->dev)) {
746 PREPARE_WORK(&cdev->private->kick_work,
747 ccw_device_call_sch_unregister, (void *)cdev);
748 queue_work(ccw_device_work, &cdev->private->kick_work);
750 wake_up(&cdev->private->wait_q);
754 * Handle path verification event.
756 static void
757 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
759 struct subchannel *sch;
761 if (cdev->private->state == DEV_STATE_W4SENSE) {
762 cdev->private->flags.doverify = 1;
763 return;
765 sch = to_subchannel(cdev->dev.parent);
767 * Since we might not just be coming from an interrupt from the
768 * subchannel we have to update the schib.
770 stsch(sch->schid, &sch->schib);
772 if (sch->schib.scsw.actl != 0 ||
773 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
774 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
776 * No final status yet or final status not yet delivered
777 * to the device driver. Can't do path verfication now,
778 * delay until final status was delivered.
780 cdev->private->flags.doverify = 1;
781 return;
783 /* Device is idle, we can do the path verification. */
784 cdev->private->state = DEV_STATE_VERIFY;
785 cdev->private->flags.doverify = 0;
786 ccw_device_verify_start(cdev);
790 * Got an interrupt for a normal io (state online).
792 static void
793 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
795 struct irb *irb;
797 irb = (struct irb *) __LC_IRB;
798 /* Check for unsolicited interrupt. */
799 if ((irb->scsw.stctl ==
800 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
801 && (!irb->scsw.cc)) {
802 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
803 !irb->esw.esw0.erw.cons) {
804 /* Unit check but no sense data. Need basic sense. */
805 if (ccw_device_do_sense(cdev, irb) != 0)
806 goto call_handler_unsol;
807 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
808 cdev->private->state = DEV_STATE_W4SENSE;
809 cdev->private->intparm = 0;
810 return;
812 call_handler_unsol:
813 if (cdev->handler)
814 cdev->handler (cdev, 0, irb);
815 return;
817 /* Accumulate status and find out if a basic sense is needed. */
818 ccw_device_accumulate_irb(cdev, irb);
819 if (cdev->private->flags.dosense) {
820 if (ccw_device_do_sense(cdev, irb) == 0) {
821 cdev->private->state = DEV_STATE_W4SENSE;
823 return;
825 /* Call the handler. */
826 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
827 /* Start delayed path verification. */
828 ccw_device_online_verify(cdev, 0);
832 * Got an timeout in online state.
834 static void
835 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
837 int ret;
839 ccw_device_set_timeout(cdev, 0);
840 ret = ccw_device_cancel_halt_clear(cdev);
841 if (ret == -EBUSY) {
842 ccw_device_set_timeout(cdev, 3*HZ);
843 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
844 return;
846 if (ret == -ENODEV) {
847 struct subchannel *sch;
849 sch = to_subchannel(cdev->dev.parent);
850 if (!sch->lpm) {
851 PREPARE_WORK(&cdev->private->kick_work,
852 ccw_device_nopath_notify, (void *)cdev);
853 queue_work(ccw_device_notify_work,
854 &cdev->private->kick_work);
855 } else
856 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
857 } else if (cdev->handler)
858 cdev->handler(cdev, cdev->private->intparm,
859 ERR_PTR(-ETIMEDOUT));
863 * Got an interrupt for a basic sense.
865 void
866 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
868 struct irb *irb;
870 irb = (struct irb *) __LC_IRB;
871 /* Check for unsolicited interrupt. */
872 if (irb->scsw.stctl ==
873 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
874 if (irb->scsw.cc == 1)
875 /* Basic sense hasn't started. Try again. */
876 ccw_device_do_sense(cdev, irb);
877 else {
878 printk(KERN_INFO "Huh? %s(%s): unsolicited "
879 "interrupt...\n",
880 __FUNCTION__, cdev->dev.bus_id);
881 if (cdev->handler)
882 cdev->handler (cdev, 0, irb);
884 return;
887 * Check if a halt or clear has been issued in the meanwhile. If yes,
888 * only deliver the halt/clear interrupt to the device driver as if it
889 * had killed the original request.
891 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
892 cdev->private->flags.dosense = 0;
893 memset(&cdev->private->irb, 0, sizeof(struct irb));
894 ccw_device_accumulate_irb(cdev, irb);
895 goto call_handler;
897 /* Add basic sense info to irb. */
898 ccw_device_accumulate_basic_sense(cdev, irb);
899 if (cdev->private->flags.dosense) {
900 /* Another basic sense is needed. */
901 ccw_device_do_sense(cdev, irb);
902 return;
904 call_handler:
905 cdev->private->state = DEV_STATE_ONLINE;
906 /* Call the handler. */
907 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
908 /* Start delayed path verification. */
909 ccw_device_online_verify(cdev, 0);
912 static void
913 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
915 struct irb *irb;
917 irb = (struct irb *) __LC_IRB;
918 /* Accumulate status. We don't do basic sense. */
919 ccw_device_accumulate_irb(cdev, irb);
920 /* Remember to clear irb to avoid residuals. */
921 memset(&cdev->private->irb, 0, sizeof(struct irb));
922 /* Try to start delayed device verification. */
923 ccw_device_online_verify(cdev, 0);
924 /* Note: Don't call handler for cio initiated clear! */
927 static void
928 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
930 struct subchannel *sch;
932 sch = to_subchannel(cdev->dev.parent);
933 ccw_device_set_timeout(cdev, 0);
934 /* OK, i/o is dead now. Call interrupt handler. */
935 cdev->private->state = DEV_STATE_ONLINE;
936 if (cdev->handler)
937 cdev->handler(cdev, cdev->private->intparm,
938 ERR_PTR(-EIO));
939 if (!sch->lpm) {
940 PREPARE_WORK(&cdev->private->kick_work,
941 ccw_device_nopath_notify, (void *)cdev);
942 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
943 } else if (cdev->private->flags.doverify)
944 /* Start delayed path verification. */
945 ccw_device_online_verify(cdev, 0);
948 static void
949 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
951 int ret;
953 ret = ccw_device_cancel_halt_clear(cdev);
954 if (ret == -EBUSY) {
955 ccw_device_set_timeout(cdev, 3*HZ);
956 return;
958 if (ret == -ENODEV) {
959 struct subchannel *sch;
961 sch = to_subchannel(cdev->dev.parent);
962 if (!sch->lpm) {
963 PREPARE_WORK(&cdev->private->kick_work,
964 ccw_device_nopath_notify, (void *)cdev);
965 queue_work(ccw_device_notify_work,
966 &cdev->private->kick_work);
967 } else
968 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
969 return;
971 //FIXME: Can we get here?
972 cdev->private->state = DEV_STATE_ONLINE;
973 if (cdev->handler)
974 cdev->handler(cdev, cdev->private->intparm,
975 ERR_PTR(-EIO));
978 void device_kill_io(struct subchannel *sch)
980 int ret;
981 struct ccw_device *cdev;
983 cdev = sch->dev.driver_data;
984 ret = ccw_device_cancel_halt_clear(cdev);
985 if (ret == -EBUSY) {
986 ccw_device_set_timeout(cdev, 3*HZ);
987 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
988 return;
990 if (ret == -ENODEV) {
991 if (!sch->lpm) {
992 PREPARE_WORK(&cdev->private->kick_work,
993 ccw_device_nopath_notify, (void *)cdev);
994 queue_work(ccw_device_notify_work,
995 &cdev->private->kick_work);
996 } else
997 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
998 return;
1000 if (cdev->handler)
1001 cdev->handler(cdev, cdev->private->intparm,
1002 ERR_PTR(-EIO));
1003 if (!sch->lpm) {
1004 PREPARE_WORK(&cdev->private->kick_work,
1005 ccw_device_nopath_notify, (void *)cdev);
1006 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1007 } else
1008 /* Start delayed path verification. */
1009 ccw_device_online_verify(cdev, 0);
1012 static void
1013 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
1015 /* Start verification after current task finished. */
1016 cdev->private->flags.doverify = 1;
1019 static void
1020 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1022 struct irb *irb;
1024 switch (dev_event) {
1025 case DEV_EVENT_INTERRUPT:
1026 irb = (struct irb *) __LC_IRB;
1027 /* Check for unsolicited interrupt. */
1028 if ((irb->scsw.stctl ==
1029 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1030 (!irb->scsw.cc))
1031 /* FIXME: we should restart stlck here, but this
1032 * is extremely unlikely ... */
1033 goto out_wakeup;
1035 ccw_device_accumulate_irb(cdev, irb);
1036 /* We don't care about basic sense etc. */
1037 break;
1038 default: /* timeout */
1039 break;
1041 out_wakeup:
1042 wake_up(&cdev->private->wait_q);
1045 static void
1046 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1048 struct subchannel *sch;
1050 sch = to_subchannel(cdev->dev.parent);
1051 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1052 /* Couldn't enable the subchannel for i/o. Sick device. */
1053 return;
1055 /* After 60s the device recognition is considered to have failed. */
1056 ccw_device_set_timeout(cdev, 60*HZ);
1058 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1059 ccw_device_sense_id_start(cdev);
1062 void
1063 device_trigger_reprobe(struct subchannel *sch)
1065 struct ccw_device *cdev;
1067 if (!sch->dev.driver_data)
1068 return;
1069 cdev = sch->dev.driver_data;
1070 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1071 return;
1073 /* Update some values. */
1074 if (stsch(sch->schid, &sch->schib))
1075 return;
1078 * The pim, pam, pom values may not be accurate, but they are the best
1079 * we have before performing device selection :/
1081 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1082 /* Re-set some bits in the pmcw that were lost. */
1083 sch->schib.pmcw.isc = 3;
1084 sch->schib.pmcw.csense = 1;
1085 sch->schib.pmcw.ena = 0;
1086 if ((sch->lpm & (sch->lpm - 1)) != 0)
1087 sch->schib.pmcw.mp = 1;
1088 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1089 /* We should also udate ssd info, but this has to wait. */
1090 ccw_device_start_id(cdev, 0);
1093 static void
1094 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1096 struct subchannel *sch;
1098 sch = to_subchannel(cdev->dev.parent);
1100 * An interrupt in state offline means a previous disable was not
1101 * successful. Try again.
1103 cio_disable_subchannel(sch);
1106 static void
1107 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1109 retry_set_schib(cdev);
1110 cdev->private->state = DEV_STATE_ONLINE;
1111 dev_fsm_event(cdev, dev_event);
1114 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1115 enum dev_event dev_event)
1117 cmf_retry_copy_block(cdev);
1118 cdev->private->state = DEV_STATE_ONLINE;
1119 dev_fsm_event(cdev, dev_event);
1122 static void
1123 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1125 ccw_device_set_timeout(cdev, 0);
1126 if (dev_event == DEV_EVENT_NOTOPER)
1127 cdev->private->state = DEV_STATE_NOT_OPER;
1128 else
1129 cdev->private->state = DEV_STATE_OFFLINE;
1130 wake_up(&cdev->private->wait_q);
1133 static void
1134 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1136 int ret;
1138 ret = ccw_device_cancel_halt_clear(cdev);
1139 switch (ret) {
1140 case 0:
1141 cdev->private->state = DEV_STATE_OFFLINE;
1142 wake_up(&cdev->private->wait_q);
1143 break;
1144 case -ENODEV:
1145 cdev->private->state = DEV_STATE_NOT_OPER;
1146 wake_up(&cdev->private->wait_q);
1147 break;
1148 default:
1149 ccw_device_set_timeout(cdev, HZ/10);
1154 * No operation action. This is used e.g. to ignore a timeout event in
1155 * state offline.
1157 static void
1158 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1163 * Bug operation action.
1165 static void
1166 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1168 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1169 cdev->private->state, dev_event);
1170 BUG();
1174 * device statemachine
1176 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1177 [DEV_STATE_NOT_OPER] = {
1178 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1179 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1180 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1181 [DEV_EVENT_VERIFY] = ccw_device_nop,
1183 [DEV_STATE_SENSE_PGID] = {
1184 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1185 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1186 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1187 [DEV_EVENT_VERIFY] = ccw_device_nop,
1189 [DEV_STATE_SENSE_ID] = {
1190 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1191 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1192 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1193 [DEV_EVENT_VERIFY] = ccw_device_nop,
1195 [DEV_STATE_OFFLINE] = {
1196 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1197 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1198 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1199 [DEV_EVENT_VERIFY] = ccw_device_nop,
1201 [DEV_STATE_VERIFY] = {
1202 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1203 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1204 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1205 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1207 [DEV_STATE_ONLINE] = {
1208 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1209 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1210 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1211 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1213 [DEV_STATE_W4SENSE] = {
1214 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1215 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1216 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1217 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1219 [DEV_STATE_DISBAND_PGID] = {
1220 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1221 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1222 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1223 [DEV_EVENT_VERIFY] = ccw_device_nop,
1225 [DEV_STATE_BOXED] = {
1226 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1227 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1228 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1229 [DEV_EVENT_VERIFY] = ccw_device_nop,
1231 /* states to wait for i/o completion before doing something */
1232 [DEV_STATE_CLEAR_VERIFY] = {
1233 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1234 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1235 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1236 [DEV_EVENT_VERIFY] = ccw_device_nop,
1238 [DEV_STATE_TIMEOUT_KILL] = {
1239 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1240 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1241 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1242 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1244 [DEV_STATE_QUIESCE] = {
1245 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1246 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1247 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1248 [DEV_EVENT_VERIFY] = ccw_device_nop,
1250 /* special states for devices gone not operational */
1251 [DEV_STATE_DISCONNECTED] = {
1252 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1253 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1254 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1255 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1257 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1258 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1259 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1260 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1261 [DEV_EVENT_VERIFY] = ccw_device_nop,
1263 [DEV_STATE_CMFCHANGE] = {
1264 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1265 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1266 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1267 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1269 [DEV_STATE_CMFUPDATE] = {
1270 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1271 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1272 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1273 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1278 * io_subchannel_irq is called for "real" interrupts or for status
1279 * pending conditions on msch.
1281 void
1282 io_subchannel_irq (struct device *pdev)
1284 struct ccw_device *cdev;
1286 cdev = to_subchannel(pdev)->dev.driver_data;
1288 CIO_TRACE_EVENT (3, "IRQ");
1289 CIO_TRACE_EVENT (3, pdev->bus_id);
1290 if (cdev)
1291 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1294 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);