xref: /openbmc/linux/drivers/s390/cio/device_fsm.c (revision 7dd65feb)
1 /*
2  * drivers/s390/cio/device_fsm.c
3  * finite state machine for device handling
4  *
5  *    Copyright IBM Corp. 2002,2008
6  *    Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
7  *		 Martin Schwidefsky (schwidefsky@de.ibm.com)
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/jiffies.h>
13 #include <linux/string.h>
14 
15 #include <asm/ccwdev.h>
16 #include <asm/cio.h>
17 #include <asm/chpid.h>
18 
19 #include "cio.h"
20 #include "cio_debug.h"
21 #include "css.h"
22 #include "device.h"
23 #include "chsc.h"
24 #include "ioasm.h"
25 #include "chp.h"
26 
27 static int timeout_log_enabled;
28 
29 static int __init ccw_timeout_log_setup(char *unused)
30 {
31 	timeout_log_enabled = 1;
32 	return 1;
33 }
34 
35 __setup("ccw_timeout_log", ccw_timeout_log_setup);
36 
37 static void ccw_timeout_log(struct ccw_device *cdev)
38 {
39 	struct schib schib;
40 	struct subchannel *sch;
41 	struct io_subchannel_private *private;
42 	union orb *orb;
43 	int cc;
44 
45 	sch = to_subchannel(cdev->dev.parent);
46 	private = to_io_private(sch);
47 	orb = &private->orb;
48 	cc = stsch(sch->schid, &schib);
49 
50 	printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
51 	       "device information:\n", get_clock());
52 	printk(KERN_WARNING "cio: orb:\n");
53 	print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
54 		       orb, sizeof(*orb), 0);
55 	printk(KERN_WARNING "cio: ccw device bus id: %s\n",
56 	       dev_name(&cdev->dev));
57 	printk(KERN_WARNING "cio: subchannel bus id: %s\n",
58 	       dev_name(&sch->dev));
59 	printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
60 	       "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
61 
62 	if (orb->tm.b) {
63 		printk(KERN_WARNING "cio: orb indicates transport mode\n");
64 		printk(KERN_WARNING "cio: last tcw:\n");
65 		print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
66 			       (void *)(addr_t)orb->tm.tcw,
67 			       sizeof(struct tcw), 0);
68 	} else {
69 		printk(KERN_WARNING "cio: orb indicates command mode\n");
70 		if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
71 		    (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
72 			printk(KERN_WARNING "cio: last channel program "
73 			       "(intern):\n");
74 		else
75 			printk(KERN_WARNING "cio: last channel program:\n");
76 
77 		print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
78 			       (void *)(addr_t)orb->cmd.cpa,
79 			       sizeof(struct ccw1), 0);
80 	}
81 	printk(KERN_WARNING "cio: ccw device state: %d\n",
82 	       cdev->private->state);
83 	printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
84 	printk(KERN_WARNING "cio: schib:\n");
85 	print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
86 		       &schib, sizeof(schib), 0);
87 	printk(KERN_WARNING "cio: ccw device flags:\n");
88 	print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
89 		       &cdev->private->flags, sizeof(cdev->private->flags), 0);
90 }
91 
92 /*
93  * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
94  */
95 static void
96 ccw_device_timeout(unsigned long data)
97 {
98 	struct ccw_device *cdev;
99 
100 	cdev = (struct ccw_device *) data;
101 	spin_lock_irq(cdev->ccwlock);
102 	if (timeout_log_enabled)
103 		ccw_timeout_log(cdev);
104 	dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
105 	spin_unlock_irq(cdev->ccwlock);
106 }
107 
108 /*
109  * Set timeout
110  */
111 void
112 ccw_device_set_timeout(struct ccw_device *cdev, int expires)
113 {
114 	if (expires == 0) {
115 		del_timer(&cdev->private->timer);
116 		return;
117 	}
118 	if (timer_pending(&cdev->private->timer)) {
119 		if (mod_timer(&cdev->private->timer, jiffies + expires))
120 			return;
121 	}
122 	cdev->private->timer.function = ccw_device_timeout;
123 	cdev->private->timer.data = (unsigned long) cdev;
124 	cdev->private->timer.expires = jiffies + expires;
125 	add_timer(&cdev->private->timer);
126 }
127 
128 /*
129  * Cancel running i/o. This is called repeatedly since halt/clear are
130  * asynchronous operations. We do one try with cio_cancel, two tries
131  * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
132  * Returns 0 if device now idle, -ENODEV for device not operational and
133  * -EBUSY if an interrupt is expected (either from halt/clear or from a
134  * status pending).
135  */
136 int
137 ccw_device_cancel_halt_clear(struct ccw_device *cdev)
138 {
139 	struct subchannel *sch;
140 	int ret;
141 
142 	sch = to_subchannel(cdev->dev.parent);
143 	if (cio_update_schib(sch))
144 		return -ENODEV;
145 	if (!sch->schib.pmcw.ena)
146 		/* Not operational -> done. */
147 		return 0;
148 	/* Stage 1: cancel io. */
149 	if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
150 	    !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
151 		if (!scsw_is_tm(&sch->schib.scsw)) {
152 			ret = cio_cancel(sch);
153 			if (ret != -EINVAL)
154 				return ret;
155 		}
156 		/* cancel io unsuccessful or not applicable (transport mode).
157 		 * Continue with asynchronous instructions. */
158 		cdev->private->iretry = 3;	/* 3 halt retries. */
159 	}
160 	if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
161 		/* Stage 2: halt io. */
162 		if (cdev->private->iretry) {
163 			cdev->private->iretry--;
164 			ret = cio_halt(sch);
165 			if (ret != -EBUSY)
166 				return (ret == 0) ? -EBUSY : ret;
167 		}
168 		/* halt io unsuccessful. */
169 		cdev->private->iretry = 255;	/* 255 clear retries. */
170 	}
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;
176 	}
177 	panic("Can't stop i/o on subchannel.\n");
178 }
179 
180 void ccw_device_update_sense_data(struct ccw_device *cdev)
181 {
182 	memset(&cdev->id, 0, sizeof(cdev->id));
183 	cdev->id.cu_type   = cdev->private->senseid.cu_type;
184 	cdev->id.cu_model  = cdev->private->senseid.cu_model;
185 	cdev->id.dev_type  = cdev->private->senseid.dev_type;
186 	cdev->id.dev_model = cdev->private->senseid.dev_model;
187 }
188 
189 int ccw_device_test_sense_data(struct ccw_device *cdev)
190 {
191 	return cdev->id.cu_type == cdev->private->senseid.cu_type &&
192 		cdev->id.cu_model == cdev->private->senseid.cu_model &&
193 		cdev->id.dev_type == cdev->private->senseid.dev_type &&
194 		cdev->id.dev_model == cdev->private->senseid.dev_model;
195 }
196 
197 /*
198  * The machine won't give us any notification by machine check if a chpid has
199  * been varied online on the SE so we have to find out by magic (i. e. driving
200  * the channel subsystem to device selection and updating our path masks).
201  */
202 static void
203 __recover_lost_chpids(struct subchannel *sch, int old_lpm)
204 {
205 	int mask, i;
206 	struct chp_id chpid;
207 
208 	chp_id_init(&chpid);
209 	for (i = 0; i<8; i++) {
210 		mask = 0x80 >> i;
211 		if (!(sch->lpm & mask))
212 			continue;
213 		if (old_lpm & mask)
214 			continue;
215 		chpid.id = sch->schib.pmcw.chpid[i];
216 		if (!chp_is_registered(chpid))
217 			css_schedule_eval_all();
218 	}
219 }
220 
221 /*
222  * Stop device recognition.
223  */
224 static void
225 ccw_device_recog_done(struct ccw_device *cdev, int state)
226 {
227 	struct subchannel *sch;
228 	int old_lpm;
229 
230 	sch = to_subchannel(cdev->dev.parent);
231 
232 	if (cio_disable_subchannel(sch))
233 		state = DEV_STATE_NOT_OPER;
234 	/*
235 	 * Now that we tried recognition, we have performed device selection
236 	 * through ssch() and the path information is up to date.
237 	 */
238 	old_lpm = sch->lpm;
239 
240 	/* Check since device may again have become not operational. */
241 	if (cio_update_schib(sch))
242 		state = DEV_STATE_NOT_OPER;
243 	else
244 		sch->lpm = sch->schib.pmcw.pam & sch->opm;
245 
246 	if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
247 		/* Force reprobe on all chpids. */
248 		old_lpm = 0;
249 	if (sch->lpm != old_lpm)
250 		__recover_lost_chpids(sch, old_lpm);
251 	if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
252 	    (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
253 		cdev->private->flags.recog_done = 1;
254 		cdev->private->state = DEV_STATE_DISCONNECTED;
255 		wake_up(&cdev->private->wait_q);
256 		return;
257 	}
258 	if (cdev->private->flags.resuming) {
259 		cdev->private->state = state;
260 		cdev->private->flags.recog_done = 1;
261 		wake_up(&cdev->private->wait_q);
262 		return;
263 	}
264 	switch (state) {
265 	case DEV_STATE_NOT_OPER:
266 		break;
267 	case DEV_STATE_OFFLINE:
268 		if (!cdev->online) {
269 			ccw_device_update_sense_data(cdev);
270 			break;
271 		}
272 		cdev->private->state = DEV_STATE_OFFLINE;
273 		cdev->private->flags.recog_done = 1;
274 		if (ccw_device_test_sense_data(cdev)) {
275 			cdev->private->flags.donotify = 1;
276 			ccw_device_online(cdev);
277 			wake_up(&cdev->private->wait_q);
278 		} else {
279 			ccw_device_update_sense_data(cdev);
280 			ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
281 		}
282 		return;
283 	case DEV_STATE_BOXED:
284 		if (cdev->id.cu_type != 0) { /* device was recognized before */
285 			cdev->private->flags.recog_done = 1;
286 			cdev->private->state = DEV_STATE_BOXED;
287 			wake_up(&cdev->private->wait_q);
288 			return;
289 		}
290 		break;
291 	}
292 	cdev->private->state = state;
293 	io_subchannel_recog_done(cdev);
294 	wake_up(&cdev->private->wait_q);
295 }
296 
297 /*
298  * Function called from device_id.c after sense id has completed.
299  */
300 void
301 ccw_device_sense_id_done(struct ccw_device *cdev, int err)
302 {
303 	switch (err) {
304 	case 0:
305 		ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
306 		break;
307 	case -ETIME:		/* Sense id stopped by timeout. */
308 		ccw_device_recog_done(cdev, DEV_STATE_BOXED);
309 		break;
310 	default:
311 		ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
312 		break;
313 	}
314 }
315 
316 int ccw_device_notify(struct ccw_device *cdev, int event)
317 {
318 	if (!cdev->drv)
319 		return 0;
320 	if (!cdev->online)
321 		return 0;
322 	CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
323 		      cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
324 		      event);
325 	return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
326 }
327 
328 static void ccw_device_oper_notify(struct ccw_device *cdev)
329 {
330 	if (ccw_device_notify(cdev, CIO_OPER)) {
331 		/* Reenable channel measurements, if needed. */
332 		ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
333 		return;
334 	}
335 	/* Driver doesn't want device back. */
336 	ccw_device_set_notoper(cdev);
337 	ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
338 }
339 
340 /*
341  * Finished with online/offline processing.
342  */
343 static void
344 ccw_device_done(struct ccw_device *cdev, int state)
345 {
346 	struct subchannel *sch;
347 
348 	sch = to_subchannel(cdev->dev.parent);
349 
350 	ccw_device_set_timeout(cdev, 0);
351 
352 	if (state != DEV_STATE_ONLINE)
353 		cio_disable_subchannel(sch);
354 
355 	/* Reset device status. */
356 	memset(&cdev->private->irb, 0, sizeof(struct irb));
357 
358 	cdev->private->state = state;
359 
360 	switch (state) {
361 	case DEV_STATE_BOXED:
362 		CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
363 			      cdev->private->dev_id.devno, sch->schid.sch_no);
364 		if (cdev->online && !ccw_device_notify(cdev, CIO_BOXED))
365 			ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
366 		cdev->private->flags.donotify = 0;
367 		break;
368 	case DEV_STATE_NOT_OPER:
369 		CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
370 			      cdev->private->dev_id.devno, sch->schid.sch_no);
371 		if (!ccw_device_notify(cdev, CIO_GONE))
372 			ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
373 		else
374 			ccw_device_set_disconnected(cdev);
375 		cdev->private->flags.donotify = 0;
376 		break;
377 	case DEV_STATE_DISCONNECTED:
378 		CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
379 			      "%04x\n", cdev->private->dev_id.devno,
380 			      sch->schid.sch_no);
381 		if (!ccw_device_notify(cdev, CIO_NO_PATH))
382 			ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
383 		else
384 			ccw_device_set_disconnected(cdev);
385 		cdev->private->flags.donotify = 0;
386 		break;
387 	default:
388 		break;
389 	}
390 
391 	if (cdev->private->flags.donotify) {
392 		cdev->private->flags.donotify = 0;
393 		ccw_device_oper_notify(cdev);
394 	}
395 	wake_up(&cdev->private->wait_q);
396 }
397 
398 /*
399  * Start device recognition.
400  */
401 void ccw_device_recognition(struct ccw_device *cdev)
402 {
403 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
404 
405 	/*
406 	 * We used to start here with a sense pgid to find out whether a device
407 	 * is locked by someone else. Unfortunately, the sense pgid command
408 	 * code has other meanings on devices predating the path grouping
409 	 * algorithm, so we start with sense id and box the device after an
410 	 * timeout (or if sense pgid during path verification detects the device
411 	 * is locked, as may happen on newer devices).
412 	 */
413 	cdev->private->flags.recog_done = 0;
414 	cdev->private->state = DEV_STATE_SENSE_ID;
415 	if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
416 		ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
417 		return;
418 	}
419 	ccw_device_sense_id_start(cdev);
420 }
421 
422 /*
423  * Handle events for states that use the ccw request infrastructure.
424  */
425 static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
426 {
427 	switch (e) {
428 	case DEV_EVENT_NOTOPER:
429 		ccw_request_notoper(cdev);
430 		break;
431 	case DEV_EVENT_INTERRUPT:
432 		ccw_request_handler(cdev);
433 		break;
434 	case DEV_EVENT_TIMEOUT:
435 		ccw_request_timeout(cdev);
436 		break;
437 	default:
438 		break;
439 	}
440 }
441 
442 void
443 ccw_device_verify_done(struct ccw_device *cdev, int err)
444 {
445 	struct subchannel *sch;
446 
447 	sch = to_subchannel(cdev->dev.parent);
448 	/* Update schib - pom may have changed. */
449 	if (cio_update_schib(sch)) {
450 		err = -ENODEV;
451 		goto callback;
452 	}
453 	/* Update lpm with verified path mask. */
454 	sch->lpm = sch->vpm;
455 	/* Repeat path verification? */
456 	if (cdev->private->flags.doverify) {
457 		ccw_device_verify_start(cdev);
458 		return;
459 	}
460 callback:
461 	switch (err) {
462 	case 0:
463 		ccw_device_done(cdev, DEV_STATE_ONLINE);
464 		/* Deliver fake irb to device driver, if needed. */
465 		if (cdev->private->flags.fake_irb) {
466 			memset(&cdev->private->irb, 0, sizeof(struct irb));
467 			cdev->private->irb.scsw.cmd.cc = 1;
468 			cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
469 			cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
470 			cdev->private->irb.scsw.cmd.stctl =
471 				SCSW_STCTL_STATUS_PEND;
472 			cdev->private->flags.fake_irb = 0;
473 			if (cdev->handler)
474 				cdev->handler(cdev, cdev->private->intparm,
475 					      &cdev->private->irb);
476 			memset(&cdev->private->irb, 0, sizeof(struct irb));
477 		}
478 		break;
479 	case -ETIME:
480 	case -EUSERS:
481 		/* Reset oper notify indication after verify error. */
482 		cdev->private->flags.donotify = 0;
483 		ccw_device_done(cdev, DEV_STATE_BOXED);
484 		break;
485 	case -EACCES:
486 		/* Reset oper notify indication after verify error. */
487 		cdev->private->flags.donotify = 0;
488 		ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
489 		break;
490 	default:
491 		/* Reset oper notify indication after verify error. */
492 		cdev->private->flags.donotify = 0;
493 		ccw_device_done(cdev, DEV_STATE_NOT_OPER);
494 		break;
495 	}
496 }
497 
498 /*
499  * Get device online.
500  */
501 int
502 ccw_device_online(struct ccw_device *cdev)
503 {
504 	struct subchannel *sch;
505 	int ret;
506 
507 	if ((cdev->private->state != DEV_STATE_OFFLINE) &&
508 	    (cdev->private->state != DEV_STATE_BOXED))
509 		return -EINVAL;
510 	sch = to_subchannel(cdev->dev.parent);
511 	ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
512 	if (ret != 0) {
513 		/* Couldn't enable the subchannel for i/o. Sick device. */
514 		if (ret == -ENODEV)
515 			dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
516 		return ret;
517 	}
518 	/* Start initial path verification. */
519 	cdev->private->state = DEV_STATE_VERIFY;
520 	ccw_device_verify_start(cdev);
521 	return 0;
522 }
523 
524 void
525 ccw_device_disband_done(struct ccw_device *cdev, int err)
526 {
527 	switch (err) {
528 	case 0:
529 		ccw_device_done(cdev, DEV_STATE_OFFLINE);
530 		break;
531 	case -ETIME:
532 		ccw_device_done(cdev, DEV_STATE_BOXED);
533 		break;
534 	default:
535 		cdev->private->flags.donotify = 0;
536 		ccw_device_done(cdev, DEV_STATE_NOT_OPER);
537 		break;
538 	}
539 }
540 
541 /*
542  * Shutdown device.
543  */
544 int
545 ccw_device_offline(struct ccw_device *cdev)
546 {
547 	struct subchannel *sch;
548 
549 	/* Allow ccw_device_offline while disconnected. */
550 	if (cdev->private->state == DEV_STATE_DISCONNECTED ||
551 	    cdev->private->state == DEV_STATE_NOT_OPER) {
552 		cdev->private->flags.donotify = 0;
553 		ccw_device_done(cdev, DEV_STATE_NOT_OPER);
554 		return 0;
555 	}
556 	if (cdev->private->state == DEV_STATE_BOXED) {
557 		ccw_device_done(cdev, DEV_STATE_BOXED);
558 		return 0;
559 	}
560 	if (ccw_device_is_orphan(cdev)) {
561 		ccw_device_done(cdev, DEV_STATE_OFFLINE);
562 		return 0;
563 	}
564 	sch = to_subchannel(cdev->dev.parent);
565 	if (cio_update_schib(sch))
566 		return -ENODEV;
567 	if (scsw_actl(&sch->schib.scsw) != 0)
568 		return -EBUSY;
569 	if (cdev->private->state != DEV_STATE_ONLINE)
570 		return -EINVAL;
571 	/* Are we doing path grouping? */
572 	if (!cdev->private->flags.pgroup) {
573 		/* No, set state offline immediately. */
574 		ccw_device_done(cdev, DEV_STATE_OFFLINE);
575 		return 0;
576 	}
577 	/* Start Set Path Group commands. */
578 	cdev->private->state = DEV_STATE_DISBAND_PGID;
579 	ccw_device_disband_start(cdev);
580 	return 0;
581 }
582 
583 /*
584  * Handle not operational event in non-special state.
585  */
586 static void ccw_device_generic_notoper(struct ccw_device *cdev,
587 				       enum dev_event dev_event)
588 {
589 	if (!ccw_device_notify(cdev, CIO_GONE))
590 		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
591 	else
592 		ccw_device_set_disconnected(cdev);
593 }
594 
595 /*
596  * Handle path verification event in offline state.
597  */
598 static void ccw_device_offline_verify(struct ccw_device *cdev,
599 				      enum dev_event dev_event)
600 {
601 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
602 
603 	css_schedule_eval(sch->schid);
604 }
605 
606 /*
607  * Handle path verification event.
608  */
609 static void
610 ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
611 {
612 	struct subchannel *sch;
613 
614 	if (cdev->private->state == DEV_STATE_W4SENSE) {
615 		cdev->private->flags.doverify = 1;
616 		return;
617 	}
618 	sch = to_subchannel(cdev->dev.parent);
619 	/*
620 	 * Since we might not just be coming from an interrupt from the
621 	 * subchannel we have to update the schib.
622 	 */
623 	if (cio_update_schib(sch)) {
624 		ccw_device_verify_done(cdev, -ENODEV);
625 		return;
626 	}
627 
628 	if (scsw_actl(&sch->schib.scsw) != 0 ||
629 	    (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
630 	    (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
631 		/*
632 		 * No final status yet or final status not yet delivered
633 		 * to the device driver. Can't do path verfication now,
634 		 * delay until final status was delivered.
635 		 */
636 		cdev->private->flags.doverify = 1;
637 		return;
638 	}
639 	/* Device is idle, we can do the path verification. */
640 	cdev->private->state = DEV_STATE_VERIFY;
641 	ccw_device_verify_start(cdev);
642 }
643 
644 /*
645  * Handle path verification event in boxed state.
646  */
647 static void ccw_device_boxed_verify(struct ccw_device *cdev,
648 				    enum dev_event dev_event)
649 {
650 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
651 
652 	if (cdev->online) {
653 		if (cio_enable_subchannel(sch, (u32) (addr_t) sch))
654 			ccw_device_done(cdev, DEV_STATE_NOT_OPER);
655 		else
656 			ccw_device_online_verify(cdev, dev_event);
657 	} else
658 		css_schedule_eval(sch->schid);
659 }
660 
661 /*
662  * Got an interrupt for a normal io (state online).
663  */
664 static void
665 ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
666 {
667 	struct irb *irb;
668 	int is_cmd;
669 
670 	irb = (struct irb *) __LC_IRB;
671 	is_cmd = !scsw_is_tm(&irb->scsw);
672 	/* Check for unsolicited interrupt. */
673 	if (!scsw_is_solicited(&irb->scsw)) {
674 		if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
675 		    !irb->esw.esw0.erw.cons) {
676 			/* Unit check but no sense data. Need basic sense. */
677 			if (ccw_device_do_sense(cdev, irb) != 0)
678 				goto call_handler_unsol;
679 			memcpy(&cdev->private->irb, irb, sizeof(struct irb));
680 			cdev->private->state = DEV_STATE_W4SENSE;
681 			cdev->private->intparm = 0;
682 			return;
683 		}
684 call_handler_unsol:
685 		if (cdev->handler)
686 			cdev->handler (cdev, 0, irb);
687 		if (cdev->private->flags.doverify)
688 			ccw_device_online_verify(cdev, 0);
689 		return;
690 	}
691 	/* Accumulate status and find out if a basic sense is needed. */
692 	ccw_device_accumulate_irb(cdev, irb);
693 	if (is_cmd && cdev->private->flags.dosense) {
694 		if (ccw_device_do_sense(cdev, irb) == 0) {
695 			cdev->private->state = DEV_STATE_W4SENSE;
696 		}
697 		return;
698 	}
699 	/* Call the handler. */
700 	if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
701 		/* Start delayed path verification. */
702 		ccw_device_online_verify(cdev, 0);
703 }
704 
705 /*
706  * Got an timeout in online state.
707  */
708 static void
709 ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
710 {
711 	int ret;
712 
713 	ccw_device_set_timeout(cdev, 0);
714 	ret = ccw_device_cancel_halt_clear(cdev);
715 	if (ret == -EBUSY) {
716 		ccw_device_set_timeout(cdev, 3*HZ);
717 		cdev->private->state = DEV_STATE_TIMEOUT_KILL;
718 		return;
719 	}
720 	if (ret == -ENODEV)
721 		dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
722 	else if (cdev->handler)
723 		cdev->handler(cdev, cdev->private->intparm,
724 			      ERR_PTR(-ETIMEDOUT));
725 }
726 
727 /*
728  * Got an interrupt for a basic sense.
729  */
730 static void
731 ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
732 {
733 	struct irb *irb;
734 
735 	irb = (struct irb *) __LC_IRB;
736 	/* Check for unsolicited interrupt. */
737 	if (scsw_stctl(&irb->scsw) ==
738 	    (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
739 		if (scsw_cc(&irb->scsw) == 1)
740 			/* Basic sense hasn't started. Try again. */
741 			ccw_device_do_sense(cdev, irb);
742 		else {
743 			CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
744 				      "interrupt during w4sense...\n",
745 				      cdev->private->dev_id.ssid,
746 				      cdev->private->dev_id.devno);
747 			if (cdev->handler)
748 				cdev->handler (cdev, 0, irb);
749 		}
750 		return;
751 	}
752 	/*
753 	 * Check if a halt or clear has been issued in the meanwhile. If yes,
754 	 * only deliver the halt/clear interrupt to the device driver as if it
755 	 * had killed the original request.
756 	 */
757 	if (scsw_fctl(&irb->scsw) &
758 	    (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
759 		cdev->private->flags.dosense = 0;
760 		memset(&cdev->private->irb, 0, sizeof(struct irb));
761 		ccw_device_accumulate_irb(cdev, irb);
762 		goto call_handler;
763 	}
764 	/* Add basic sense info to irb. */
765 	ccw_device_accumulate_basic_sense(cdev, irb);
766 	if (cdev->private->flags.dosense) {
767 		/* Another basic sense is needed. */
768 		ccw_device_do_sense(cdev, irb);
769 		return;
770 	}
771 call_handler:
772 	cdev->private->state = DEV_STATE_ONLINE;
773 	/* In case sensing interfered with setting the device online */
774 	wake_up(&cdev->private->wait_q);
775 	/* Call the handler. */
776 	if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
777 		/* Start delayed path verification. */
778 		ccw_device_online_verify(cdev, 0);
779 }
780 
781 static void
782 ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
783 {
784 	struct subchannel *sch;
785 
786 	sch = to_subchannel(cdev->dev.parent);
787 	ccw_device_set_timeout(cdev, 0);
788 	/* Start delayed path verification. */
789 	ccw_device_online_verify(cdev, 0);
790 	/* OK, i/o is dead now. Call interrupt handler. */
791 	if (cdev->handler)
792 		cdev->handler(cdev, cdev->private->intparm,
793 			      ERR_PTR(-EIO));
794 }
795 
796 static void
797 ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
798 {
799 	int ret;
800 
801 	ret = ccw_device_cancel_halt_clear(cdev);
802 	if (ret == -EBUSY) {
803 		ccw_device_set_timeout(cdev, 3*HZ);
804 		return;
805 	}
806 	/* Start delayed path verification. */
807 	ccw_device_online_verify(cdev, 0);
808 	if (cdev->handler)
809 		cdev->handler(cdev, cdev->private->intparm,
810 			      ERR_PTR(-EIO));
811 }
812 
813 void ccw_device_kill_io(struct ccw_device *cdev)
814 {
815 	int ret;
816 
817 	ret = ccw_device_cancel_halt_clear(cdev);
818 	if (ret == -EBUSY) {
819 		ccw_device_set_timeout(cdev, 3*HZ);
820 		cdev->private->state = DEV_STATE_TIMEOUT_KILL;
821 		return;
822 	}
823 	/* Start delayed path verification. */
824 	ccw_device_online_verify(cdev, 0);
825 	if (cdev->handler)
826 		cdev->handler(cdev, cdev->private->intparm,
827 			      ERR_PTR(-EIO));
828 }
829 
830 static void
831 ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
832 {
833 	/* Start verification after current task finished. */
834 	cdev->private->flags.doverify = 1;
835 }
836 
837 static void
838 ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
839 {
840 	struct subchannel *sch;
841 
842 	sch = to_subchannel(cdev->dev.parent);
843 	if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
844 		/* Couldn't enable the subchannel for i/o. Sick device. */
845 		return;
846 	cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
847 	ccw_device_sense_id_start(cdev);
848 }
849 
850 void ccw_device_trigger_reprobe(struct ccw_device *cdev)
851 {
852 	struct subchannel *sch;
853 
854 	if (cdev->private->state != DEV_STATE_DISCONNECTED)
855 		return;
856 
857 	sch = to_subchannel(cdev->dev.parent);
858 	/* Update some values. */
859 	if (cio_update_schib(sch))
860 		return;
861 	/*
862 	 * The pim, pam, pom values may not be accurate, but they are the best
863 	 * we have before performing device selection :/
864 	 */
865 	sch->lpm = sch->schib.pmcw.pam & sch->opm;
866 	/*
867 	 * Use the initial configuration since we can't be shure that the old
868 	 * paths are valid.
869 	 */
870 	io_subchannel_init_config(sch);
871 	if (cio_commit_config(sch))
872 		return;
873 
874 	/* We should also udate ssd info, but this has to wait. */
875 	/* Check if this is another device which appeared on the same sch. */
876 	if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
877 		css_schedule_eval(sch->schid);
878 	else
879 		ccw_device_start_id(cdev, 0);
880 }
881 
882 static void ccw_device_disabled_irq(struct ccw_device *cdev,
883 				    enum dev_event dev_event)
884 {
885 	struct subchannel *sch;
886 
887 	sch = to_subchannel(cdev->dev.parent);
888 	/*
889 	 * An interrupt in a disabled state means a previous disable was not
890 	 * successful - should not happen, but we try to disable again.
891 	 */
892 	cio_disable_subchannel(sch);
893 }
894 
895 static void
896 ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
897 {
898 	retry_set_schib(cdev);
899 	cdev->private->state = DEV_STATE_ONLINE;
900 	dev_fsm_event(cdev, dev_event);
901 }
902 
903 static void ccw_device_update_cmfblock(struct ccw_device *cdev,
904 				       enum dev_event dev_event)
905 {
906 	cmf_retry_copy_block(cdev);
907 	cdev->private->state = DEV_STATE_ONLINE;
908 	dev_fsm_event(cdev, dev_event);
909 }
910 
911 static void
912 ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
913 {
914 	ccw_device_set_timeout(cdev, 0);
915 	cdev->private->state = DEV_STATE_NOT_OPER;
916 	wake_up(&cdev->private->wait_q);
917 }
918 
919 static void
920 ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
921 {
922 	int ret;
923 
924 	ret = ccw_device_cancel_halt_clear(cdev);
925 	if (ret == -EBUSY) {
926 		ccw_device_set_timeout(cdev, HZ/10);
927 	} else {
928 		cdev->private->state = DEV_STATE_NOT_OPER;
929 		wake_up(&cdev->private->wait_q);
930 	}
931 }
932 
933 /*
934  * No operation action. This is used e.g. to ignore a timeout event in
935  * state offline.
936  */
937 static void
938 ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
939 {
940 }
941 
942 /*
943  * device statemachine
944  */
945 fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
946 	[DEV_STATE_NOT_OPER] = {
947 		[DEV_EVENT_NOTOPER]	= ccw_device_nop,
948 		[DEV_EVENT_INTERRUPT]	= ccw_device_disabled_irq,
949 		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
950 		[DEV_EVENT_VERIFY]	= ccw_device_nop,
951 	},
952 	[DEV_STATE_SENSE_PGID] = {
953 		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
954 		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
955 		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
956 		[DEV_EVENT_VERIFY]	= ccw_device_nop,
957 	},
958 	[DEV_STATE_SENSE_ID] = {
959 		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
960 		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
961 		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
962 		[DEV_EVENT_VERIFY]	= ccw_device_nop,
963 	},
964 	[DEV_STATE_OFFLINE] = {
965 		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
966 		[DEV_EVENT_INTERRUPT]	= ccw_device_disabled_irq,
967 		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
968 		[DEV_EVENT_VERIFY]	= ccw_device_offline_verify,
969 	},
970 	[DEV_STATE_VERIFY] = {
971 		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
972 		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
973 		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
974 		[DEV_EVENT_VERIFY]	= ccw_device_delay_verify,
975 	},
976 	[DEV_STATE_ONLINE] = {
977 		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
978 		[DEV_EVENT_INTERRUPT]	= ccw_device_irq,
979 		[DEV_EVENT_TIMEOUT]	= ccw_device_online_timeout,
980 		[DEV_EVENT_VERIFY]	= ccw_device_online_verify,
981 	},
982 	[DEV_STATE_W4SENSE] = {
983 		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
984 		[DEV_EVENT_INTERRUPT]	= ccw_device_w4sense,
985 		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
986 		[DEV_EVENT_VERIFY]	= ccw_device_online_verify,
987 	},
988 	[DEV_STATE_DISBAND_PGID] = {
989 		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
990 		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
991 		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
992 		[DEV_EVENT_VERIFY]	= ccw_device_nop,
993 	},
994 	[DEV_STATE_BOXED] = {
995 		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
996 		[DEV_EVENT_INTERRUPT]	= ccw_device_nop,
997 		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
998 		[DEV_EVENT_VERIFY]	= ccw_device_boxed_verify,
999 	},
1000 	/* states to wait for i/o completion before doing something */
1001 	[DEV_STATE_TIMEOUT_KILL] = {
1002 		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
1003 		[DEV_EVENT_INTERRUPT]	= ccw_device_killing_irq,
1004 		[DEV_EVENT_TIMEOUT]	= ccw_device_killing_timeout,
1005 		[DEV_EVENT_VERIFY]	= ccw_device_nop, //FIXME
1006 	},
1007 	[DEV_STATE_QUIESCE] = {
1008 		[DEV_EVENT_NOTOPER]	= ccw_device_quiesce_done,
1009 		[DEV_EVENT_INTERRUPT]	= ccw_device_quiesce_done,
1010 		[DEV_EVENT_TIMEOUT]	= ccw_device_quiesce_timeout,
1011 		[DEV_EVENT_VERIFY]	= ccw_device_nop,
1012 	},
1013 	/* special states for devices gone not operational */
1014 	[DEV_STATE_DISCONNECTED] = {
1015 		[DEV_EVENT_NOTOPER]	= ccw_device_nop,
1016 		[DEV_EVENT_INTERRUPT]	= ccw_device_start_id,
1017 		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
1018 		[DEV_EVENT_VERIFY]	= ccw_device_start_id,
1019 	},
1020 	[DEV_STATE_DISCONNECTED_SENSE_ID] = {
1021 		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
1022 		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
1023 		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
1024 		[DEV_EVENT_VERIFY]	= ccw_device_nop,
1025 	},
1026 	[DEV_STATE_CMFCHANGE] = {
1027 		[DEV_EVENT_NOTOPER]	= ccw_device_change_cmfstate,
1028 		[DEV_EVENT_INTERRUPT]	= ccw_device_change_cmfstate,
1029 		[DEV_EVENT_TIMEOUT]	= ccw_device_change_cmfstate,
1030 		[DEV_EVENT_VERIFY]	= ccw_device_change_cmfstate,
1031 	},
1032 	[DEV_STATE_CMFUPDATE] = {
1033 		[DEV_EVENT_NOTOPER]	= ccw_device_update_cmfblock,
1034 		[DEV_EVENT_INTERRUPT]	= ccw_device_update_cmfblock,
1035 		[DEV_EVENT_TIMEOUT]	= ccw_device_update_cmfblock,
1036 		[DEV_EVENT_VERIFY]	= ccw_device_update_cmfblock,
1037 	},
1038 	[DEV_STATE_STEAL_LOCK] = {
1039 		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
1040 		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
1041 		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
1042 		[DEV_EVENT_VERIFY]	= ccw_device_nop,
1043 	},
1044 };
1045 
1046 EXPORT_SYMBOL_GPL(ccw_device_set_timeout);
1047