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