[S390] Some preparations for the dynamic subchannel mapping patch.
[linux-2.6.git] / drivers / s390 / cio / device_fsm.c
bloba487fb0e7d3d5c2d79453cc987f04d05ed960553
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;
62 void device_set_intretry(struct subchannel *sch)
64 struct ccw_device *cdev;
66 cdev = sch->dev.driver_data;
67 if (!cdev)
68 return;
69 cdev->private->flags.intretry = 1;
72 int device_trigger_verify(struct subchannel *sch)
74 struct ccw_device *cdev;
76 cdev = sch->dev.driver_data;
77 if (!cdev || !cdev->online)
78 return -EINVAL;
79 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
80 return 0;
84 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
86 static void
87 ccw_device_timeout(unsigned long data)
89 struct ccw_device *cdev;
91 cdev = (struct ccw_device *) data;
92 spin_lock_irq(cdev->ccwlock);
93 dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
94 spin_unlock_irq(cdev->ccwlock);
98 * Set timeout
100 void
101 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
103 if (expires == 0) {
104 del_timer(&cdev->private->timer);
105 return;
107 if (timer_pending(&cdev->private->timer)) {
108 if (mod_timer(&cdev->private->timer, jiffies + expires))
109 return;
111 cdev->private->timer.function = ccw_device_timeout;
112 cdev->private->timer.data = (unsigned long) cdev;
113 cdev->private->timer.expires = jiffies + expires;
114 add_timer(&cdev->private->timer);
117 /* Kill any pending timers after machine check. */
118 void
119 device_kill_pending_timer(struct subchannel *sch)
121 struct ccw_device *cdev;
123 if (!sch->dev.driver_data)
124 return;
125 cdev = sch->dev.driver_data;
126 ccw_device_set_timeout(cdev, 0);
130 * Cancel running i/o. This is called repeatedly since halt/clear are
131 * asynchronous operations. We do one try with cio_cancel, two tries
132 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
133 * Returns 0 if device now idle, -ENODEV for device not operational and
134 * -EBUSY if an interrupt is expected (either from halt/clear or from a
135 * status pending).
138 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
140 struct subchannel *sch;
141 int ret;
143 sch = to_subchannel(cdev->dev.parent);
144 ret = stsch(sch->schid, &sch->schib);
145 if (ret || !sch->schib.pmcw.dnv)
146 return -ENODEV;
147 if (!sch->schib.pmcw.ena || sch->schib.scsw.actl == 0)
148 /* Not operational or no activity -> done. */
149 return 0;
150 /* Stage 1: cancel io. */
151 if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
152 !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
153 ret = cio_cancel(sch);
154 if (ret != -EINVAL)
155 return ret;
156 /* cancel io unsuccessful. From now on it is asynchronous. */
157 cdev->private->iretry = 3; /* 3 halt retries. */
159 if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
160 /* Stage 2: halt io. */
161 if (cdev->private->iretry) {
162 cdev->private->iretry--;
163 ret = cio_halt(sch);
164 if (ret != -EBUSY)
165 return (ret == 0) ? -EBUSY : ret;
167 /* halt io unsuccessful. */
168 cdev->private->iretry = 255; /* 255 clear retries. */
170 /* Stage 3: clear io. */
171 if (cdev->private->iretry) {
172 cdev->private->iretry--;
173 ret = cio_clear (sch);
174 return (ret == 0) ? -EBUSY : ret;
176 panic("Can't stop i/o on subchannel.\n");
179 static int
180 ccw_device_handle_oper(struct ccw_device *cdev)
182 struct subchannel *sch;
184 sch = to_subchannel(cdev->dev.parent);
185 cdev->private->flags.recog_done = 1;
187 * Check if cu type and device type still match. If
188 * not, it is certainly another device and we have to
189 * de- and re-register. Also check here for non-matching devno.
191 if (cdev->id.cu_type != cdev->private->senseid.cu_type ||
192 cdev->id.cu_model != cdev->private->senseid.cu_model ||
193 cdev->id.dev_type != cdev->private->senseid.dev_type ||
194 cdev->id.dev_model != cdev->private->senseid.dev_model ||
195 cdev->private->dev_id.devno != sch->schib.pmcw.dev) {
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 inline void
211 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
213 int mask, i;
215 for (i = 0; i<8; i++) {
216 mask = 0x80 >> i;
217 if (!(sch->lpm & mask))
218 continue;
219 if (old_lpm & mask)
220 continue;
221 chpid_is_actually_online(sch->schib.pmcw.chpid[i]);
226 * Stop device recognition.
228 static void
229 ccw_device_recog_done(struct ccw_device *cdev, int state)
231 struct subchannel *sch;
232 int notify, old_lpm, same_dev;
234 sch = to_subchannel(cdev->dev.parent);
236 ccw_device_set_timeout(cdev, 0);
237 cio_disable_subchannel(sch);
239 * Now that we tried recognition, we have performed device selection
240 * through ssch() and the path information is up to date.
242 old_lpm = sch->lpm;
243 stsch(sch->schid, &sch->schib);
244 sch->lpm = sch->schib.pmcw.pam & sch->opm;
245 /* Check since device may again have become not operational. */
246 if (!sch->schib.pmcw.dnv)
247 state = DEV_STATE_NOT_OPER;
248 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
249 /* Force reprobe on all chpids. */
250 old_lpm = 0;
251 if (sch->lpm != old_lpm)
252 __recover_lost_chpids(sch, old_lpm);
253 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
254 if (state == DEV_STATE_NOT_OPER) {
255 cdev->private->flags.recog_done = 1;
256 cdev->private->state = DEV_STATE_DISCONNECTED;
257 return;
259 /* Boxed devices don't need extra treatment. */
261 notify = 0;
262 same_dev = 0; /* Keep the compiler quiet... */
263 switch (state) {
264 case DEV_STATE_NOT_OPER:
265 CIO_DEBUG(KERN_WARNING, 2,
266 "SenseID : unknown device %04x on subchannel "
267 "0.%x.%04x\n", cdev->private->dev_id.devno,
268 sch->schid.ssid, sch->schid.sch_no);
269 break;
270 case DEV_STATE_OFFLINE:
271 if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
272 same_dev = ccw_device_handle_oper(cdev);
273 notify = 1;
275 /* fill out sense information */
276 memset(&cdev->id, 0, sizeof(cdev->id));
277 cdev->id.cu_type = cdev->private->senseid.cu_type;
278 cdev->id.cu_model = cdev->private->senseid.cu_model;
279 cdev->id.dev_type = cdev->private->senseid.dev_type;
280 cdev->id.dev_model = cdev->private->senseid.dev_model;
281 if (notify) {
282 cdev->private->state = DEV_STATE_OFFLINE;
283 if (same_dev) {
284 /* Get device online again. */
285 ccw_device_online(cdev);
286 wake_up(&cdev->private->wait_q);
288 return;
290 /* Issue device info message. */
291 CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: "
292 "CU Type/Mod = %04X/%02X, Dev Type/Mod = "
293 "%04X/%02X\n",
294 cdev->private->dev_id.ssid,
295 cdev->private->dev_id.devno,
296 cdev->id.cu_type, cdev->id.cu_model,
297 cdev->id.dev_type, cdev->id.dev_model);
298 break;
299 case DEV_STATE_BOXED:
300 CIO_DEBUG(KERN_WARNING, 2,
301 "SenseID : boxed device %04x on subchannel "
302 "0.%x.%04x\n", cdev->private->dev_id.devno,
303 sch->schid.ssid, sch->schid.sch_no);
304 break;
306 cdev->private->state = state;
307 io_subchannel_recog_done(cdev);
308 if (state != DEV_STATE_NOT_OPER)
309 wake_up(&cdev->private->wait_q);
313 * Function called from device_id.c after sense id has completed.
315 void
316 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
318 switch (err) {
319 case 0:
320 ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
321 break;
322 case -ETIME: /* Sense id stopped by timeout. */
323 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
324 break;
325 default:
326 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
327 break;
331 static void
332 ccw_device_oper_notify(struct work_struct *work)
334 struct ccw_device_private *priv;
335 struct ccw_device *cdev;
336 struct subchannel *sch;
337 int ret;
339 priv = container_of(work, struct ccw_device_private, kick_work);
340 cdev = priv->cdev;
341 sch = to_subchannel(cdev->dev.parent);
342 ret = (sch->driver && sch->driver->notify) ?
343 sch->driver->notify(&sch->dev, CIO_OPER) : 0;
344 if (!ret)
345 /* Driver doesn't want device back. */
346 ccw_device_do_unreg_rereg(work);
347 else {
348 /* Reenable channel measurements, if needed. */
349 cmf_reenable(cdev);
350 wake_up(&cdev->private->wait_q);
355 * Finished with online/offline processing.
357 static void
358 ccw_device_done(struct ccw_device *cdev, int state)
360 struct subchannel *sch;
362 sch = to_subchannel(cdev->dev.parent);
364 ccw_device_set_timeout(cdev, 0);
366 if (state != DEV_STATE_ONLINE)
367 cio_disable_subchannel(sch);
369 /* Reset device status. */
370 memset(&cdev->private->irb, 0, sizeof(struct irb));
372 cdev->private->state = state;
375 if (state == DEV_STATE_BOXED)
376 CIO_DEBUG(KERN_WARNING, 2,
377 "Boxed device %04x on subchannel %04x\n",
378 cdev->private->dev_id.devno, sch->schid.sch_no);
380 if (cdev->private->flags.donotify) {
381 cdev->private->flags.donotify = 0;
382 PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
383 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
385 wake_up(&cdev->private->wait_q);
387 if (css_init_done && state != DEV_STATE_ONLINE)
388 put_device (&cdev->dev);
391 static inline int cmp_pgid(struct pgid *p1, struct pgid *p2)
393 char *c1;
394 char *c2;
396 c1 = (char *)p1;
397 c2 = (char *)p2;
399 return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
402 static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
404 int i;
405 int last;
407 last = 0;
408 for (i = 0; i < 8; i++) {
409 if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
410 /* No PGID yet */
411 continue;
412 if (cdev->private->pgid[last].inf.ps.state1 ==
413 SNID_STATE1_RESET) {
414 /* First non-zero PGID */
415 last = i;
416 continue;
418 if (cmp_pgid(&cdev->private->pgid[i],
419 &cdev->private->pgid[last]) == 0)
420 /* Non-conflicting PGIDs */
421 continue;
423 /* PGID mismatch, can't pathgroup. */
424 CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
425 "0.%x.%04x, can't pathgroup\n",
426 cdev->private->dev_id.ssid,
427 cdev->private->dev_id.devno);
428 cdev->private->options.pgroup = 0;
429 return;
431 if (cdev->private->pgid[last].inf.ps.state1 ==
432 SNID_STATE1_RESET)
433 /* No previous pgid found */
434 memcpy(&cdev->private->pgid[0], &css[0]->global_pgid,
435 sizeof(struct pgid));
436 else
437 /* Use existing pgid */
438 memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
439 sizeof(struct pgid));
443 * Function called from device_pgid.c after sense path ground has completed.
445 void
446 ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
448 struct subchannel *sch;
450 sch = to_subchannel(cdev->dev.parent);
451 switch (err) {
452 case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
453 cdev->private->options.pgroup = 0;
454 break;
455 case 0: /* success */
456 case -EACCES: /* partial success, some paths not operational */
457 /* Check if all pgids are equal or 0. */
458 __ccw_device_get_common_pgid(cdev);
459 break;
460 case -ETIME: /* Sense path group id stopped by timeout. */
461 case -EUSERS: /* device is reserved for someone else. */
462 ccw_device_done(cdev, DEV_STATE_BOXED);
463 return;
464 default:
465 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
466 return;
468 /* Start Path Group verification. */
469 cdev->private->state = DEV_STATE_VERIFY;
470 cdev->private->flags.doverify = 0;
471 ccw_device_verify_start(cdev);
475 * Start device recognition.
478 ccw_device_recognition(struct ccw_device *cdev)
480 struct subchannel *sch;
481 int ret;
483 if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
484 (cdev->private->state != DEV_STATE_BOXED))
485 return -EINVAL;
486 sch = to_subchannel(cdev->dev.parent);
487 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
488 if (ret != 0)
489 /* Couldn't enable the subchannel for i/o. Sick device. */
490 return ret;
492 /* After 60s the device recognition is considered to have failed. */
493 ccw_device_set_timeout(cdev, 60*HZ);
496 * We used to start here with a sense pgid to find out whether a device
497 * is locked by someone else. Unfortunately, the sense pgid command
498 * code has other meanings on devices predating the path grouping
499 * algorithm, so we start with sense id and box the device after an
500 * timeout (or if sense pgid during path verification detects the device
501 * is locked, as may happen on newer devices).
503 cdev->private->flags.recog_done = 0;
504 cdev->private->state = DEV_STATE_SENSE_ID;
505 ccw_device_sense_id_start(cdev);
506 return 0;
510 * Handle timeout in device recognition.
512 static void
513 ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
515 int ret;
517 ret = ccw_device_cancel_halt_clear(cdev);
518 switch (ret) {
519 case 0:
520 ccw_device_recog_done(cdev, DEV_STATE_BOXED);
521 break;
522 case -ENODEV:
523 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
524 break;
525 default:
526 ccw_device_set_timeout(cdev, 3*HZ);
531 static void
532 ccw_device_nopath_notify(struct work_struct *work)
534 struct ccw_device_private *priv;
535 struct ccw_device *cdev;
536 struct subchannel *sch;
537 int ret;
539 priv = container_of(work, struct ccw_device_private, kick_work);
540 cdev = priv->cdev;
541 sch = to_subchannel(cdev->dev.parent);
542 /* Extra sanity. */
543 if (sch->lpm)
544 return;
545 ret = (sch->driver && sch->driver->notify) ?
546 sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0;
547 if (!ret) {
548 if (get_device(&sch->dev)) {
549 /* Driver doesn't want to keep device. */
550 cio_disable_subchannel(sch);
551 if (get_device(&cdev->dev)) {
552 PREPARE_WORK(&cdev->private->kick_work,
553 ccw_device_call_sch_unregister);
554 queue_work(ccw_device_work,
555 &cdev->private->kick_work);
556 } else
557 put_device(&sch->dev);
559 } else {
560 cio_disable_subchannel(sch);
561 ccw_device_set_timeout(cdev, 0);
562 cdev->private->flags.fake_irb = 0;
563 cdev->private->state = DEV_STATE_DISCONNECTED;
564 wake_up(&cdev->private->wait_q);
568 void
569 ccw_device_verify_done(struct ccw_device *cdev, int err)
571 struct subchannel *sch;
573 sch = to_subchannel(cdev->dev.parent);
574 /* Update schib - pom may have changed. */
575 stsch(sch->schid, &sch->schib);
576 /* Update lpm with verified path mask. */
577 sch->lpm = sch->vpm;
578 /* Repeat path verification? */
579 if (cdev->private->flags.doverify) {
580 cdev->private->flags.doverify = 0;
581 ccw_device_verify_start(cdev);
582 return;
584 switch (err) {
585 case -EOPNOTSUPP: /* path grouping not supported, just set online. */
586 cdev->private->options.pgroup = 0;
587 case 0:
588 ccw_device_done(cdev, DEV_STATE_ONLINE);
589 /* Deliver fake irb to device driver, if needed. */
590 if (cdev->private->flags.fake_irb) {
591 memset(&cdev->private->irb, 0, sizeof(struct irb));
592 cdev->private->irb.scsw.cc = 1;
593 cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
594 cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
595 cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
596 cdev->private->flags.fake_irb = 0;
597 if (cdev->handler)
598 cdev->handler(cdev, cdev->private->intparm,
599 &cdev->private->irb);
600 memset(&cdev->private->irb, 0, sizeof(struct irb));
602 break;
603 case -ETIME:
604 /* Reset oper notify indication after verify error. */
605 cdev->private->flags.donotify = 0;
606 ccw_device_done(cdev, DEV_STATE_BOXED);
607 break;
608 default:
609 /* Reset oper notify indication after verify error. */
610 cdev->private->flags.donotify = 0;
611 PREPARE_WORK(&cdev->private->kick_work,
612 ccw_device_nopath_notify);
613 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
614 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
615 break;
620 * Get device online.
623 ccw_device_online(struct ccw_device *cdev)
625 struct subchannel *sch;
626 int ret;
628 if ((cdev->private->state != DEV_STATE_OFFLINE) &&
629 (cdev->private->state != DEV_STATE_BOXED))
630 return -EINVAL;
631 sch = to_subchannel(cdev->dev.parent);
632 if (css_init_done && !get_device(&cdev->dev))
633 return -ENODEV;
634 ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
635 if (ret != 0) {
636 /* Couldn't enable the subchannel for i/o. Sick device. */
637 if (ret == -ENODEV)
638 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
639 return ret;
641 /* Do we want to do path grouping? */
642 if (!cdev->private->options.pgroup) {
643 /* Start initial path verification. */
644 cdev->private->state = DEV_STATE_VERIFY;
645 cdev->private->flags.doverify = 0;
646 ccw_device_verify_start(cdev);
647 return 0;
649 /* Do a SensePGID first. */
650 cdev->private->state = DEV_STATE_SENSE_PGID;
651 ccw_device_sense_pgid_start(cdev);
652 return 0;
655 void
656 ccw_device_disband_done(struct ccw_device *cdev, int err)
658 switch (err) {
659 case 0:
660 ccw_device_done(cdev, DEV_STATE_OFFLINE);
661 break;
662 case -ETIME:
663 ccw_device_done(cdev, DEV_STATE_BOXED);
664 break;
665 default:
666 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
667 break;
672 * Shutdown device.
675 ccw_device_offline(struct ccw_device *cdev)
677 struct subchannel *sch;
679 sch = to_subchannel(cdev->dev.parent);
680 if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
681 return -ENODEV;
682 if (cdev->private->state != DEV_STATE_ONLINE) {
683 if (sch->schib.scsw.actl != 0)
684 return -EBUSY;
685 return -EINVAL;
687 if (sch->schib.scsw.actl != 0)
688 return -EBUSY;
689 /* Are we doing path grouping? */
690 if (!cdev->private->options.pgroup) {
691 /* No, set state offline immediately. */
692 ccw_device_done(cdev, DEV_STATE_OFFLINE);
693 return 0;
695 /* Start Set Path Group commands. */
696 cdev->private->state = DEV_STATE_DISBAND_PGID;
697 ccw_device_disband_start(cdev);
698 return 0;
702 * Handle timeout in device online/offline process.
704 static void
705 ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
707 int ret;
709 ret = ccw_device_cancel_halt_clear(cdev);
710 switch (ret) {
711 case 0:
712 ccw_device_done(cdev, DEV_STATE_BOXED);
713 break;
714 case -ENODEV:
715 ccw_device_done(cdev, DEV_STATE_NOT_OPER);
716 break;
717 default:
718 ccw_device_set_timeout(cdev, 3*HZ);
723 * Handle not oper event in device recognition.
725 static void
726 ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
728 ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
732 * Handle not operational event while offline.
734 static void
735 ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
737 struct subchannel *sch;
739 cdev->private->state = DEV_STATE_NOT_OPER;
740 sch = to_subchannel(cdev->dev.parent);
741 if (get_device(&cdev->dev)) {
742 PREPARE_WORK(&cdev->private->kick_work,
743 ccw_device_call_sch_unregister);
744 queue_work(ccw_device_work, &cdev->private->kick_work);
746 wake_up(&cdev->private->wait_q);
750 * Handle not operational event while online.
752 static void
753 ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
755 struct subchannel *sch;
757 sch = to_subchannel(cdev->dev.parent);
758 if (sch->driver->notify &&
759 sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) {
760 ccw_device_set_timeout(cdev, 0);
761 cdev->private->flags.fake_irb = 0;
762 cdev->private->state = DEV_STATE_DISCONNECTED;
763 wake_up(&cdev->private->wait_q);
764 return;
766 cdev->private->state = DEV_STATE_NOT_OPER;
767 cio_disable_subchannel(sch);
768 if (sch->schib.scsw.actl != 0) {
769 // FIXME: not-oper indication to device driver ?
770 ccw_device_call_handler(cdev);
772 if (get_device(&cdev->dev)) {
773 PREPARE_WORK(&cdev->private->kick_work,
774 ccw_device_call_sch_unregister);
775 queue_work(ccw_device_work, &cdev->private->kick_work);
777 wake_up(&cdev->private->wait_q);
781 * Handle path verification event.
783 static void
784 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
786 struct subchannel *sch;
788 if (cdev->private->state == DEV_STATE_W4SENSE) {
789 cdev->private->flags.doverify = 1;
790 return;
792 sch = to_subchannel(cdev->dev.parent);
794 * Since we might not just be coming from an interrupt from the
795 * subchannel we have to update the schib.
797 stsch(sch->schid, &sch->schib);
799 if (sch->schib.scsw.actl != 0 ||
800 (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
801 (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
803 * No final status yet or final status not yet delivered
804 * to the device driver. Can't do path verfication now,
805 * delay until final status was delivered.
807 cdev->private->flags.doverify = 1;
808 return;
810 /* Device is idle, we can do the path verification. */
811 cdev->private->state = DEV_STATE_VERIFY;
812 cdev->private->flags.doverify = 0;
813 ccw_device_verify_start(cdev);
817 * Got an interrupt for a normal io (state online).
819 static void
820 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
822 struct irb *irb;
824 irb = (struct irb *) __LC_IRB;
825 /* Check for unsolicited interrupt. */
826 if ((irb->scsw.stctl ==
827 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
828 && (!irb->scsw.cc)) {
829 if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
830 !irb->esw.esw0.erw.cons) {
831 /* Unit check but no sense data. Need basic sense. */
832 if (ccw_device_do_sense(cdev, irb) != 0)
833 goto call_handler_unsol;
834 memcpy(&cdev->private->irb, irb, sizeof(struct irb));
835 cdev->private->state = DEV_STATE_W4SENSE;
836 cdev->private->intparm = 0;
837 return;
839 call_handler_unsol:
840 if (cdev->handler)
841 cdev->handler (cdev, 0, irb);
842 return;
844 /* Accumulate status and find out if a basic sense is needed. */
845 ccw_device_accumulate_irb(cdev, irb);
846 if (cdev->private->flags.dosense) {
847 if (ccw_device_do_sense(cdev, irb) == 0) {
848 cdev->private->state = DEV_STATE_W4SENSE;
850 return;
852 /* Call the handler. */
853 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
854 /* Start delayed path verification. */
855 ccw_device_online_verify(cdev, 0);
859 * Got an timeout in online state.
861 static void
862 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
864 int ret;
866 ccw_device_set_timeout(cdev, 0);
867 ret = ccw_device_cancel_halt_clear(cdev);
868 if (ret == -EBUSY) {
869 ccw_device_set_timeout(cdev, 3*HZ);
870 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
871 return;
873 if (ret == -ENODEV) {
874 struct subchannel *sch;
876 sch = to_subchannel(cdev->dev.parent);
877 if (!sch->lpm) {
878 PREPARE_WORK(&cdev->private->kick_work,
879 ccw_device_nopath_notify);
880 queue_work(ccw_device_notify_work,
881 &cdev->private->kick_work);
882 } else
883 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
884 } else if (cdev->handler)
885 cdev->handler(cdev, cdev->private->intparm,
886 ERR_PTR(-ETIMEDOUT));
890 * Got an interrupt for a basic sense.
892 void
893 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
895 struct irb *irb;
897 irb = (struct irb *) __LC_IRB;
898 /* Check for unsolicited interrupt. */
899 if (irb->scsw.stctl ==
900 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
901 if (irb->scsw.cc == 1)
902 /* Basic sense hasn't started. Try again. */
903 ccw_device_do_sense(cdev, irb);
904 else {
905 printk(KERN_INFO "Huh? %s(%s): unsolicited "
906 "interrupt...\n",
907 __FUNCTION__, cdev->dev.bus_id);
908 if (cdev->handler)
909 cdev->handler (cdev, 0, irb);
911 return;
914 * Check if a halt or clear has been issued in the meanwhile. If yes,
915 * only deliver the halt/clear interrupt to the device driver as if it
916 * had killed the original request.
918 if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
919 /* Retry Basic Sense if requested. */
920 if (cdev->private->flags.intretry) {
921 cdev->private->flags.intretry = 0;
922 ccw_device_do_sense(cdev, irb);
923 return;
925 cdev->private->flags.dosense = 0;
926 memset(&cdev->private->irb, 0, sizeof(struct irb));
927 ccw_device_accumulate_irb(cdev, irb);
928 goto call_handler;
930 /* Add basic sense info to irb. */
931 ccw_device_accumulate_basic_sense(cdev, irb);
932 if (cdev->private->flags.dosense) {
933 /* Another basic sense is needed. */
934 ccw_device_do_sense(cdev, irb);
935 return;
937 call_handler:
938 cdev->private->state = DEV_STATE_ONLINE;
939 /* Call the handler. */
940 if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
941 /* Start delayed path verification. */
942 ccw_device_online_verify(cdev, 0);
945 static void
946 ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
948 struct irb *irb;
950 irb = (struct irb *) __LC_IRB;
951 /* Accumulate status. We don't do basic sense. */
952 ccw_device_accumulate_irb(cdev, irb);
953 /* Remember to clear irb to avoid residuals. */
954 memset(&cdev->private->irb, 0, sizeof(struct irb));
955 /* Try to start delayed device verification. */
956 ccw_device_online_verify(cdev, 0);
957 /* Note: Don't call handler for cio initiated clear! */
960 static void
961 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
963 struct subchannel *sch;
965 sch = to_subchannel(cdev->dev.parent);
966 ccw_device_set_timeout(cdev, 0);
967 /* OK, i/o is dead now. Call interrupt handler. */
968 cdev->private->state = DEV_STATE_ONLINE;
969 if (cdev->handler)
970 cdev->handler(cdev, cdev->private->intparm,
971 ERR_PTR(-EIO));
972 if (!sch->lpm) {
973 PREPARE_WORK(&cdev->private->kick_work,
974 ccw_device_nopath_notify);
975 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
976 } else if (cdev->private->flags.doverify)
977 /* Start delayed path verification. */
978 ccw_device_online_verify(cdev, 0);
981 static void
982 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
984 int ret;
986 ret = ccw_device_cancel_halt_clear(cdev);
987 if (ret == -EBUSY) {
988 ccw_device_set_timeout(cdev, 3*HZ);
989 return;
991 if (ret == -ENODEV) {
992 struct subchannel *sch;
994 sch = to_subchannel(cdev->dev.parent);
995 if (!sch->lpm) {
996 PREPARE_WORK(&cdev->private->kick_work,
997 ccw_device_nopath_notify);
998 queue_work(ccw_device_notify_work,
999 &cdev->private->kick_work);
1000 } else
1001 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1002 return;
1004 //FIXME: Can we get here?
1005 cdev->private->state = DEV_STATE_ONLINE;
1006 if (cdev->handler)
1007 cdev->handler(cdev, cdev->private->intparm,
1008 ERR_PTR(-EIO));
1011 void device_kill_io(struct subchannel *sch)
1013 int ret;
1014 struct ccw_device *cdev;
1016 cdev = sch->dev.driver_data;
1017 ret = ccw_device_cancel_halt_clear(cdev);
1018 if (ret == -EBUSY) {
1019 ccw_device_set_timeout(cdev, 3*HZ);
1020 cdev->private->state = DEV_STATE_TIMEOUT_KILL;
1021 return;
1023 if (ret == -ENODEV) {
1024 if (!sch->lpm) {
1025 PREPARE_WORK(&cdev->private->kick_work,
1026 ccw_device_nopath_notify);
1027 queue_work(ccw_device_notify_work,
1028 &cdev->private->kick_work);
1029 } else
1030 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1031 return;
1033 if (cdev->handler)
1034 cdev->handler(cdev, cdev->private->intparm,
1035 ERR_PTR(-EIO));
1036 if (!sch->lpm) {
1037 PREPARE_WORK(&cdev->private->kick_work,
1038 ccw_device_nopath_notify);
1039 queue_work(ccw_device_notify_work, &cdev->private->kick_work);
1040 } else
1041 /* Start delayed path verification. */
1042 ccw_device_online_verify(cdev, 0);
1045 static void
1046 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
1048 /* Start verification after current task finished. */
1049 cdev->private->flags.doverify = 1;
1052 static void
1053 ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
1055 struct irb *irb;
1057 switch (dev_event) {
1058 case DEV_EVENT_INTERRUPT:
1059 irb = (struct irb *) __LC_IRB;
1060 /* Check for unsolicited interrupt. */
1061 if ((irb->scsw.stctl ==
1062 (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
1063 (!irb->scsw.cc))
1064 /* FIXME: we should restart stlck here, but this
1065 * is extremely unlikely ... */
1066 goto out_wakeup;
1068 ccw_device_accumulate_irb(cdev, irb);
1069 /* We don't care about basic sense etc. */
1070 break;
1071 default: /* timeout */
1072 break;
1074 out_wakeup:
1075 wake_up(&cdev->private->wait_q);
1078 static void
1079 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
1081 struct subchannel *sch;
1083 sch = to_subchannel(cdev->dev.parent);
1084 if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
1085 /* Couldn't enable the subchannel for i/o. Sick device. */
1086 return;
1088 /* After 60s the device recognition is considered to have failed. */
1089 ccw_device_set_timeout(cdev, 60*HZ);
1091 cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
1092 ccw_device_sense_id_start(cdev);
1095 void
1096 device_trigger_reprobe(struct subchannel *sch)
1098 struct ccw_device *cdev;
1100 if (!sch->dev.driver_data)
1101 return;
1102 cdev = sch->dev.driver_data;
1103 if (cdev->private->state != DEV_STATE_DISCONNECTED)
1104 return;
1106 /* Update some values. */
1107 if (stsch(sch->schid, &sch->schib))
1108 return;
1109 if (!sch->schib.pmcw.dnv)
1110 return;
1112 * The pim, pam, pom values may not be accurate, but they are the best
1113 * we have before performing device selection :/
1115 sch->lpm = sch->schib.pmcw.pam & sch->opm;
1116 /* Re-set some bits in the pmcw that were lost. */
1117 sch->schib.pmcw.isc = 3;
1118 sch->schib.pmcw.csense = 1;
1119 sch->schib.pmcw.ena = 0;
1120 if ((sch->lpm & (sch->lpm - 1)) != 0)
1121 sch->schib.pmcw.mp = 1;
1122 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
1123 /* We should also udate ssd info, but this has to wait. */
1124 ccw_device_start_id(cdev, 0);
1127 static void
1128 ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event)
1130 struct subchannel *sch;
1132 sch = to_subchannel(cdev->dev.parent);
1134 * An interrupt in state offline means a previous disable was not
1135 * successful. Try again.
1137 cio_disable_subchannel(sch);
1140 static void
1141 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
1143 retry_set_schib(cdev);
1144 cdev->private->state = DEV_STATE_ONLINE;
1145 dev_fsm_event(cdev, dev_event);
1148 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
1149 enum dev_event dev_event)
1151 cmf_retry_copy_block(cdev);
1152 cdev->private->state = DEV_STATE_ONLINE;
1153 dev_fsm_event(cdev, dev_event);
1156 static void
1157 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
1159 ccw_device_set_timeout(cdev, 0);
1160 if (dev_event == DEV_EVENT_NOTOPER)
1161 cdev->private->state = DEV_STATE_NOT_OPER;
1162 else
1163 cdev->private->state = DEV_STATE_OFFLINE;
1164 wake_up(&cdev->private->wait_q);
1167 static void
1168 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
1170 int ret;
1172 ret = ccw_device_cancel_halt_clear(cdev);
1173 switch (ret) {
1174 case 0:
1175 cdev->private->state = DEV_STATE_OFFLINE;
1176 wake_up(&cdev->private->wait_q);
1177 break;
1178 case -ENODEV:
1179 cdev->private->state = DEV_STATE_NOT_OPER;
1180 wake_up(&cdev->private->wait_q);
1181 break;
1182 default:
1183 ccw_device_set_timeout(cdev, HZ/10);
1188 * No operation action. This is used e.g. to ignore a timeout event in
1189 * state offline.
1191 static void
1192 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
1197 * Bug operation action.
1199 static void
1200 ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
1202 printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n",
1203 cdev->private->state, dev_event);
1204 BUG();
1208 * device statemachine
1210 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
1211 [DEV_STATE_NOT_OPER] = {
1212 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1213 [DEV_EVENT_INTERRUPT] = ccw_device_bug,
1214 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1215 [DEV_EVENT_VERIFY] = ccw_device_nop,
1217 [DEV_STATE_SENSE_PGID] = {
1218 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1219 [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq,
1220 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1221 [DEV_EVENT_VERIFY] = ccw_device_nop,
1223 [DEV_STATE_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_OFFLINE] = {
1230 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1231 [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq,
1232 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1233 [DEV_EVENT_VERIFY] = ccw_device_nop,
1235 [DEV_STATE_VERIFY] = {
1236 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1237 [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq,
1238 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1239 [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
1241 [DEV_STATE_ONLINE] = {
1242 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1243 [DEV_EVENT_INTERRUPT] = ccw_device_irq,
1244 [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
1245 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1247 [DEV_STATE_W4SENSE] = {
1248 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1249 [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
1250 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1251 [DEV_EVENT_VERIFY] = ccw_device_online_verify,
1253 [DEV_STATE_DISBAND_PGID] = {
1254 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1255 [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq,
1256 [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout,
1257 [DEV_EVENT_VERIFY] = ccw_device_nop,
1259 [DEV_STATE_BOXED] = {
1260 [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper,
1261 [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
1262 [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
1263 [DEV_EVENT_VERIFY] = ccw_device_nop,
1265 /* states to wait for i/o completion before doing something */
1266 [DEV_STATE_CLEAR_VERIFY] = {
1267 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1268 [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
1269 [DEV_EVENT_TIMEOUT] = ccw_device_nop,
1270 [DEV_EVENT_VERIFY] = ccw_device_nop,
1272 [DEV_STATE_TIMEOUT_KILL] = {
1273 [DEV_EVENT_NOTOPER] = ccw_device_online_notoper,
1274 [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
1275 [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
1276 [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
1278 [DEV_STATE_QUIESCE] = {
1279 [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
1280 [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
1281 [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
1282 [DEV_EVENT_VERIFY] = ccw_device_nop,
1284 /* special states for devices gone not operational */
1285 [DEV_STATE_DISCONNECTED] = {
1286 [DEV_EVENT_NOTOPER] = ccw_device_nop,
1287 [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
1288 [DEV_EVENT_TIMEOUT] = ccw_device_bug,
1289 [DEV_EVENT_VERIFY] = ccw_device_start_id,
1291 [DEV_STATE_DISCONNECTED_SENSE_ID] = {
1292 [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper,
1293 [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq,
1294 [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout,
1295 [DEV_EVENT_VERIFY] = ccw_device_nop,
1297 [DEV_STATE_CMFCHANGE] = {
1298 [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
1299 [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
1300 [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
1301 [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
1303 [DEV_STATE_CMFUPDATE] = {
1304 [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
1305 [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
1306 [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
1307 [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
1312 * io_subchannel_irq is called for "real" interrupts or for status
1313 * pending conditions on msch.
1315 void
1316 io_subchannel_irq (struct device *pdev)
1318 struct ccw_device *cdev;
1320 cdev = to_subchannel(pdev)->dev.driver_data;
1322 CIO_TRACE_EVENT (3, "IRQ");
1323 CIO_TRACE_EVENT (3, pdev->bus_id);
1324 if (cdev)
1325 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1328 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);