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