device_fsm.c 28.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * drivers/s390/cio/device_fsm.c
 * finite state machine for device handling
 *
C
Cornelia Huck 已提交
5
 *    Copyright IBM Corp. 2002,2008
6
 *    Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
L
Linus Torvalds 已提交
7 8 9 10 11
 *		 Martin Schwidefsky (schwidefsky@de.ibm.com)
 */

#include <linux/module.h>
#include <linux/init.h>
T
Tim Schmielau 已提交
12 13
#include <linux/jiffies.h>
#include <linux/string.h>
L
Linus Torvalds 已提交
14 15

#include <asm/ccwdev.h>
16
#include <asm/cio.h>
17
#include <asm/chpid.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24

#include "cio.h"
#include "cio_debug.h"
#include "css.h"
#include "device.h"
#include "chsc.h"
#include "ioasm.h"
25
#include "chp.h"
L
Linus Torvalds 已提交
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40
static int timeout_log_enabled;

static int __init ccw_timeout_log_setup(char *unused)
{
	timeout_log_enabled = 1;
	return 1;
}

__setup("ccw_timeout_log", ccw_timeout_log_setup);

static void ccw_timeout_log(struct ccw_device *cdev)
{
	struct schib schib;
	struct subchannel *sch;
41
	struct io_subchannel_private *private;
42
	union orb *orb;
43 44 45
	int cc;

	sch = to_subchannel(cdev->dev.parent);
46
	private = to_io_private(sch);
47
	orb = &private->orb;
48 49 50 51 52 53
	cc = stsch(sch->schid, &schib);

	printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
	       "device information:\n", get_clock());
	printk(KERN_WARNING "cio: orb:\n");
	print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
54
		       orb, sizeof(*orb), 0);
55 56 57 58
	printk(KERN_WARNING "cio: ccw device bus id: %s\n",
	       dev_name(&cdev->dev));
	printk(KERN_WARNING "cio: subchannel bus id: %s\n",
	       dev_name(&sch->dev));
59 60 61
	printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
	       "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	if (orb->tm.b) {
		printk(KERN_WARNING "cio: orb indicates transport mode\n");
		printk(KERN_WARNING "cio: last tcw:\n");
		print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
			       (void *)(addr_t)orb->tm.tcw,
			       sizeof(struct tcw), 0);
	} else {
		printk(KERN_WARNING "cio: orb indicates command mode\n");
		if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
		    (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
			printk(KERN_WARNING "cio: last channel program "
			       "(intern):\n");
		else
			printk(KERN_WARNING "cio: last channel program:\n");

		print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
			       (void *)(addr_t)orb->cmd.cpa,
			       sizeof(struct ccw1), 0);
	}
81 82 83 84 85 86 87 88 89 90 91
	printk(KERN_WARNING "cio: ccw device state: %d\n",
	       cdev->private->state);
	printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
	printk(KERN_WARNING "cio: schib:\n");
	print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
		       &schib, sizeof(schib), 0);
	printk(KERN_WARNING "cio: ccw device flags:\n");
	print_hex_dump(KERN_WARNING, "cio:  ", DUMP_PREFIX_NONE, 16, 1,
		       &cdev->private->flags, sizeof(cdev->private->flags), 0);
}

L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101
/*
 * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
 */
static void
ccw_device_timeout(unsigned long data)
{
	struct ccw_device *cdev;

	cdev = (struct ccw_device *) data;
	spin_lock_irq(cdev->ccwlock);
102 103
	if (timeout_log_enabled)
		ccw_timeout_log(cdev);
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
	spin_unlock_irq(cdev->ccwlock);
}

/*
 * Set timeout
 */
void
ccw_device_set_timeout(struct ccw_device *cdev, int expires)
{
	if (expires == 0) {
		del_timer(&cdev->private->timer);
		return;
	}
	if (timer_pending(&cdev->private->timer)) {
		if (mod_timer(&cdev->private->timer, jiffies + expires))
			return;
	}
	cdev->private->timer.function = ccw_device_timeout;
	cdev->private->timer.data = (unsigned long) cdev;
	cdev->private->timer.expires = jiffies + expires;
	add_timer(&cdev->private->timer);
}

/*
 * Cancel running i/o. This is called repeatedly since halt/clear are
 * asynchronous operations. We do one try with cio_cancel, two tries
 * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
 * Returns 0 if device now idle, -ENODEV for device not operational and
 * -EBUSY if an interrupt is expected (either from halt/clear or from a
 * status pending).
 */
int
ccw_device_cancel_halt_clear(struct ccw_device *cdev)
{
	struct subchannel *sch;
	int ret;

	sch = to_subchannel(cdev->dev.parent);
143
	if (cio_update_schib(sch))
L
Linus Torvalds 已提交
144
		return -ENODEV; 
145 146
	if (!sch->schib.pmcw.ena)
		/* Not operational -> done. */
L
Linus Torvalds 已提交
147 148
		return 0;
	/* Stage 1: cancel io. */
149 150
	if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
	    !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
151 152 153 154 155 156 157
		if (!scsw_is_tm(&sch->schib.scsw)) {
			ret = cio_cancel(sch);
			if (ret != -EINVAL)
				return ret;
		}
		/* cancel io unsuccessful or not applicable (transport mode).
		 * Continue with asynchronous instructions. */
L
Linus Torvalds 已提交
158 159
		cdev->private->iretry = 3;	/* 3 halt retries. */
	}
160
	if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
L
Linus Torvalds 已提交
161 162 163 164
		/* Stage 2: halt io. */
		if (cdev->private->iretry) {
			cdev->private->iretry--;
			ret = cio_halt(sch);
165 166
			if (ret != -EBUSY)
				return (ret == 0) ? -EBUSY : ret;
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179
		}
		/* halt io unsuccessful. */
		cdev->private->iretry = 255;	/* 255 clear retries. */
	}
	/* Stage 3: clear io. */
	if (cdev->private->iretry) {
		cdev->private->iretry--;
		ret = cio_clear (sch);
		return (ret == 0) ? -EBUSY : ret;
	}
	panic("Can't stop i/o on subchannel.\n");
}

180
void ccw_device_update_sense_data(struct ccw_device *cdev)
L
Linus Torvalds 已提交
181
{
182 183 184 185 186 187
	memset(&cdev->id, 0, sizeof(cdev->id));
	cdev->id.cu_type   = cdev->private->senseid.cu_type;
	cdev->id.cu_model  = cdev->private->senseid.cu_model;
	cdev->id.dev_type  = cdev->private->senseid.dev_type;
	cdev->id.dev_model = cdev->private->senseid.dev_model;
}
L
Linus Torvalds 已提交
188

189 190 191 192 193 194
int ccw_device_test_sense_data(struct ccw_device *cdev)
{
	return cdev->id.cu_type == cdev->private->senseid.cu_type &&
		cdev->id.cu_model == cdev->private->senseid.cu_model &&
		cdev->id.dev_type == cdev->private->senseid.dev_type &&
		cdev->id.dev_model == cdev->private->senseid.dev_model;
L
Linus Torvalds 已提交
195 196 197 198 199 200 201
}

/*
 * The machine won't give us any notification by machine check if a chpid has
 * been varied online on the SE so we have to find out by magic (i. e. driving
 * the channel subsystem to device selection and updating our path masks).
 */
202
static void
L
Linus Torvalds 已提交
203 204 205
__recover_lost_chpids(struct subchannel *sch, int old_lpm)
{
	int mask, i;
206
	struct chp_id chpid;
L
Linus Torvalds 已提交
207

208
	chp_id_init(&chpid);
L
Linus Torvalds 已提交
209 210 211 212 213 214
	for (i = 0; i<8; i++) {
		mask = 0x80 >> i;
		if (!(sch->lpm & mask))
			continue;
		if (old_lpm & mask)
			continue;
215
		chpid.id = sch->schib.pmcw.chpid[i];
216 217
		if (!chp_is_registered(chpid))
			css_schedule_eval_all();
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227
	}
}

/*
 * Stop device recognition.
 */
static void
ccw_device_recog_done(struct ccw_device *cdev, int state)
{
	struct subchannel *sch;
228
	int old_lpm;
L
Linus Torvalds 已提交
229 230 231

	sch = to_subchannel(cdev->dev.parent);

232 233
	if (cio_disable_subchannel(sch))
		state = DEV_STATE_NOT_OPER;
L
Linus Torvalds 已提交
234 235 236 237 238
	/*
	 * Now that we tried recognition, we have performed device selection
	 * through ssch() and the path information is up to date.
	 */
	old_lpm = sch->lpm;
239

C
Cornelia Huck 已提交
240
	/* Check since device may again have become not operational. */
241
	if (cio_update_schib(sch))
C
Cornelia Huck 已提交
242
		state = DEV_STATE_NOT_OPER;
243 244 245
	else
		sch->lpm = sch->schib.pmcw.pam & sch->opm;

L
Linus Torvalds 已提交
246 247 248 249 250
	if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
		/* Force reprobe on all chpids. */
		old_lpm = 0;
	if (sch->lpm != old_lpm)
		__recover_lost_chpids(sch, old_lpm);
251 252 253 254 255 256
	if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
	    (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
		cdev->private->flags.recog_done = 1;
		cdev->private->state = DEV_STATE_DISCONNECTED;
		wake_up(&cdev->private->wait_q);
		return;
L
Linus Torvalds 已提交
257
	}
258 259 260 261 262 263
	if (cdev->private->flags.resuming) {
		cdev->private->state = state;
		cdev->private->flags.recog_done = 1;
		wake_up(&cdev->private->wait_q);
		return;
	}
L
Linus Torvalds 已提交
264 265 266 267
	switch (state) {
	case DEV_STATE_NOT_OPER:
		break;
	case DEV_STATE_OFFLINE:
268 269 270
		if (!cdev->online) {
			ccw_device_update_sense_data(cdev);
			break;
L
Linus Torvalds 已提交
271
		}
272 273 274 275 276 277 278 279
		cdev->private->state = DEV_STATE_OFFLINE;
		cdev->private->flags.recog_done = 1;
		if (ccw_device_test_sense_data(cdev)) {
			cdev->private->flags.donotify = 1;
			ccw_device_online(cdev);
			wake_up(&cdev->private->wait_q);
		} else {
			ccw_device_update_sense_data(cdev);
280
			ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
L
Linus Torvalds 已提交
281
		}
282
		return;
L
Linus Torvalds 已提交
283
	case DEV_STATE_BOXED:
284 285 286 287 288 289
		if (cdev->id.cu_type != 0) { /* device was recognized before */
			cdev->private->flags.recog_done = 1;
			cdev->private->state = DEV_STATE_BOXED;
			wake_up(&cdev->private->wait_q);
			return;
		}
L
Linus Torvalds 已提交
290 291 292 293
		break;
	}
	cdev->private->state = state;
	io_subchannel_recog_done(cdev);
294
	wake_up(&cdev->private->wait_q);
L
Linus Torvalds 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
}

/*
 * Function called from device_id.c after sense id has completed.
 */
void
ccw_device_sense_id_done(struct ccw_device *cdev, int err)
{
	switch (err) {
	case 0:
		ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
		break;
	case -ETIME:		/* Sense id stopped by timeout. */
		ccw_device_recog_done(cdev, DEV_STATE_BOXED);
		break;
	default:
		ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
		break;
	}
}

C
Cornelia Huck 已提交
316 317 318 319 320 321
int ccw_device_notify(struct ccw_device *cdev, int event)
{
	if (!cdev->drv)
		return 0;
	if (!cdev->online)
		return 0;
322 323 324
	CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
		      cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
		      event);
C
Cornelia Huck 已提交
325 326 327
	return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
}

328 329 330
static void ccw_device_oper_notify(struct ccw_device *cdev)
{
	if (ccw_device_notify(cdev, CIO_OPER)) {
331
		/* Reenable channel measurements, if needed. */
332
		ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
333 334 335 336
		return;
	}
	/* Driver doesn't want device back. */
	ccw_device_set_notoper(cdev);
337
	ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347 348 349
}

/*
 * Finished with online/offline processing.
 */
static void
ccw_device_done(struct ccw_device *cdev, int state)
{
	struct subchannel *sch;

	sch = to_subchannel(cdev->dev.parent);

350 351
	ccw_device_set_timeout(cdev, 0);

L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359
	if (state != DEV_STATE_ONLINE)
		cio_disable_subchannel(sch);

	/* Reset device status. */
	memset(&cdev->private->irb, 0, sizeof(struct irb));

	cdev->private->state = state;

360 361
	switch (state) {
	case DEV_STATE_BOXED:
362 363
		CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
			      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 368
		break;
	case DEV_STATE_NOT_OPER:
369 370 371
		CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
			      cdev->private->dev_id.devno, sch->schid.sch_no);
		if (!ccw_device_notify(cdev, CIO_GONE))
372
			ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
373 374
		else
			ccw_device_set_disconnected(cdev);
375
		cdev->private->flags.donotify = 0;
376 377 378 379 380 381
		break;
	case DEV_STATE_DISCONNECTED:
		CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
			      "%04x\n", cdev->private->dev_id.devno,
			      sch->schid.sch_no);
		if (!ccw_device_notify(cdev, CIO_NO_PATH))
382
			ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
383 384 385 386 387 388
		else
			ccw_device_set_disconnected(cdev);
		cdev->private->flags.donotify = 0;
		break;
	default:
		break;
389
	}
L
Linus Torvalds 已提交
390 391 392

	if (cdev->private->flags.donotify) {
		cdev->private->flags.donotify = 0;
393
		ccw_device_oper_notify(cdev);
L
Linus Torvalds 已提交
394 395 396 397 398 399 400
	}
	wake_up(&cdev->private->wait_q);
}

/*
 * Start device recognition.
 */
401
void ccw_device_recognition(struct ccw_device *cdev)
L
Linus Torvalds 已提交
402
{
403
	struct subchannel *sch = to_subchannel(cdev->dev.parent);
L
Linus Torvalds 已提交
404 405 406 407 408 409 410 411 412 413 414

	/*
	 * We used to start here with a sense pgid to find out whether a device
	 * is locked by someone else. Unfortunately, the sense pgid command
	 * code has other meanings on devices predating the path grouping
	 * algorithm, so we start with sense id and box the device after an
	 * timeout (or if sense pgid during path verification detects the device
	 * is locked, as may happen on newer devices).
	 */
	cdev->private->flags.recog_done = 0;
	cdev->private->state = DEV_STATE_SENSE_ID;
415 416 417 418
	if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
		ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
		return;
	}
L
Linus Torvalds 已提交
419 420 421 422
	ccw_device_sense_id_start(cdev);
}

/*
423
 * Handle events for states that use the ccw request infrastructure.
L
Linus Torvalds 已提交
424
 */
425
static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
L
Linus Torvalds 已提交
426
{
427 428 429
	switch (e) {
	case DEV_EVENT_NOTOPER:
		ccw_request_notoper(cdev);
L
Linus Torvalds 已提交
430
		break;
431 432 433 434 435
	case DEV_EVENT_INTERRUPT:
		ccw_request_handler(cdev);
		break;
	case DEV_EVENT_TIMEOUT:
		ccw_request_timeout(cdev);
L
Linus Torvalds 已提交
436 437
		break;
	default:
438
		break;
L
Linus Torvalds 已提交
439 440 441 442 443 444
	}
}

void
ccw_device_verify_done(struct ccw_device *cdev, int err)
{
445 446 447 448
	struct subchannel *sch;

	sch = to_subchannel(cdev->dev.parent);
	/* Update schib - pom may have changed. */
449
	if (cio_update_schib(sch)) {
450 451
		err = -ENODEV;
		goto callback;
452
	}
453 454 455 456 457 458 459
	/* Update lpm with verified path mask. */
	sch->lpm = sch->vpm;
	/* Repeat path verification? */
	if (cdev->private->flags.doverify) {
		ccw_device_verify_start(cdev);
		return;
	}
460
callback:
L
Linus Torvalds 已提交
461 462 463 464 465 466
	switch (err) {
	case 0:
		ccw_device_done(cdev, DEV_STATE_ONLINE);
		/* Deliver fake irb to device driver, if needed. */
		if (cdev->private->flags.fake_irb) {
			memset(&cdev->private->irb, 0, sizeof(struct irb));
467 468 469 470 471
			cdev->private->irb.scsw.cmd.cc = 1;
			cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
			cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
			cdev->private->irb.scsw.cmd.stctl =
				SCSW_STCTL_STATUS_PEND;
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479
			cdev->private->flags.fake_irb = 0;
			if (cdev->handler)
				cdev->handler(cdev, cdev->private->intparm,
					      &cdev->private->irb);
			memset(&cdev->private->irb, 0, sizeof(struct irb));
		}
		break;
	case -ETIME:
480
	case -EUSERS:
481 482
		/* Reset oper notify indication after verify error. */
		cdev->private->flags.donotify = 0;
L
Linus Torvalds 已提交
483 484
		ccw_device_done(cdev, DEV_STATE_BOXED);
		break;
485 486 487 488 489
	case -EACCES:
		/* Reset oper notify indication after verify error. */
		cdev->private->flags.donotify = 0;
		ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
		break;
L
Linus Torvalds 已提交
490
	default:
491 492
		/* Reset oper notify indication after verify error. */
		cdev->private->flags.donotify = 0;
493
		ccw_device_done(cdev, DEV_STATE_NOT_OPER);
L
Linus Torvalds 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
		break;
	}
}

/*
 * Get device online.
 */
int
ccw_device_online(struct ccw_device *cdev)
{
	struct subchannel *sch;
	int ret;

	if ((cdev->private->state != DEV_STATE_OFFLINE) &&
	    (cdev->private->state != DEV_STATE_BOXED))
		return -EINVAL;
	sch = to_subchannel(cdev->dev.parent);
511
	ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
L
Linus Torvalds 已提交
512 513 514 515 516 517
	if (ret != 0) {
		/* Couldn't enable the subchannel for i/o. Sick device. */
		if (ret == -ENODEV)
			dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
		return ret;
	}
518 519 520
	/* Start initial path verification. */
	cdev->private->state = DEV_STATE_VERIFY;
	ccw_device_verify_start(cdev);
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534
	return 0;
}

void
ccw_device_disband_done(struct ccw_device *cdev, int err)
{
	switch (err) {
	case 0:
		ccw_device_done(cdev, DEV_STATE_OFFLINE);
		break;
	case -ETIME:
		ccw_device_done(cdev, DEV_STATE_BOXED);
		break;
	default:
535
		cdev->private->flags.donotify = 0;
L
Linus Torvalds 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548
		ccw_device_done(cdev, DEV_STATE_NOT_OPER);
		break;
	}
}

/*
 * Shutdown device.
 */
int
ccw_device_offline(struct ccw_device *cdev)
{
	struct subchannel *sch;

549 550 551 552 553 554 555
	/* Allow ccw_device_offline while disconnected. */
	if (cdev->private->state == DEV_STATE_DISCONNECTED ||
	    cdev->private->state == DEV_STATE_NOT_OPER) {
		cdev->private->flags.donotify = 0;
		ccw_device_done(cdev, DEV_STATE_NOT_OPER);
		return 0;
	}
556 557 558 559
	if (cdev->private->state == DEV_STATE_BOXED) {
		ccw_device_done(cdev, DEV_STATE_BOXED);
		return 0;
	}
560 561 562 563
	if (ccw_device_is_orphan(cdev)) {
		ccw_device_done(cdev, DEV_STATE_OFFLINE);
		return 0;
	}
L
Linus Torvalds 已提交
564
	sch = to_subchannel(cdev->dev.parent);
565
	if (cio_update_schib(sch))
L
Linus Torvalds 已提交
566
		return -ENODEV;
567
	if (scsw_actl(&sch->schib.scsw) != 0)
L
Linus Torvalds 已提交
568
		return -EBUSY;
569 570
	if (cdev->private->state != DEV_STATE_ONLINE)
		return -EINVAL;
L
Linus Torvalds 已提交
571
	/* Are we doing path grouping? */
572
	if (!cdev->private->flags.pgroup) {
L
Linus Torvalds 已提交
573 574 575 576 577 578 579 580 581 582 583
		/* No, set state offline immediately. */
		ccw_device_done(cdev, DEV_STATE_OFFLINE);
		return 0;
	}
	/* Start Set Path Group commands. */
	cdev->private->state = DEV_STATE_DISBAND_PGID;
	ccw_device_disband_start(cdev);
	return 0;
}

/*
584
 * Handle not operational event in non-special state.
L
Linus Torvalds 已提交
585
 */
586 587
static void ccw_device_generic_notoper(struct ccw_device *cdev,
				       enum dev_event dev_event)
L
Linus Torvalds 已提交
588
{
589
	if (!ccw_device_notify(cdev, CIO_GONE))
590
		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
591 592
	else
		ccw_device_set_disconnected(cdev);
L
Linus Torvalds 已提交
593 594
}

595 596 597 598 599 600 601 602 603 604 605
/*
 * Handle path verification event in offline state.
 */
static void ccw_device_offline_verify(struct ccw_device *cdev,
				      enum dev_event dev_event)
{
	struct subchannel *sch = to_subchannel(cdev->dev.parent);

	css_schedule_eval(sch->schid);
}

L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
/*
 * Handle path verification event.
 */
static void
ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct subchannel *sch;

	if (cdev->private->state == DEV_STATE_W4SENSE) {
		cdev->private->flags.doverify = 1;
		return;
	}
	sch = to_subchannel(cdev->dev.parent);
	/*
	 * Since we might not just be coming from an interrupt from the
	 * subchannel we have to update the schib.
	 */
623 624 625 626
	if (cio_update_schib(sch)) {
		ccw_device_verify_done(cdev, -ENODEV);
		return;
	}
L
Linus Torvalds 已提交
627

628 629 630
	if (scsw_actl(&sch->schib.scsw) != 0 ||
	    (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
	    (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
L
Linus Torvalds 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643
		/*
		 * No final status yet or final status not yet delivered
		 * to the device driver. Can't do path verfication now,
		 * delay until final status was delivered.
		 */
		cdev->private->flags.doverify = 1;
		return;
	}
	/* Device is idle, we can do the path verification. */
	cdev->private->state = DEV_STATE_VERIFY;
	ccw_device_verify_start(cdev);
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
/*
 * Handle path verification event in boxed state.
 */
static void ccw_device_boxed_verify(struct ccw_device *cdev,
				    enum dev_event dev_event)
{
	struct subchannel *sch = to_subchannel(cdev->dev.parent);

	if (cdev->online) {
		if (cio_enable_subchannel(sch, (u32) (addr_t) sch))
			ccw_device_done(cdev, DEV_STATE_NOT_OPER);
		else
			ccw_device_online_verify(cdev, dev_event);
	} else
		css_schedule_eval(sch->schid);
}

L
Linus Torvalds 已提交
661 662 663 664 665 666 667
/*
 * Got an interrupt for a normal io (state online).
 */
static void
ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct irb *irb;
668
	int is_cmd;
L
Linus Torvalds 已提交
669 670

	irb = (struct irb *) __LC_IRB;
671
	is_cmd = !scsw_is_tm(&irb->scsw);
L
Linus Torvalds 已提交
672
	/* Check for unsolicited interrupt. */
673
	if (!scsw_is_solicited(&irb->scsw)) {
674
		if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
L
Linus Torvalds 已提交
675 676 677 678
		    !irb->esw.esw0.erw.cons) {
			/* Unit check but no sense data. Need basic sense. */
			if (ccw_device_do_sense(cdev, irb) != 0)
				goto call_handler_unsol;
679
			memcpy(&cdev->private->irb, irb, sizeof(struct irb));
L
Linus Torvalds 已提交
680 681 682 683 684 685 686
			cdev->private->state = DEV_STATE_W4SENSE;
			cdev->private->intparm = 0;
			return;
		}
call_handler_unsol:
		if (cdev->handler)
			cdev->handler (cdev, 0, irb);
687 688
		if (cdev->private->flags.doverify)
			ccw_device_online_verify(cdev, 0);
L
Linus Torvalds 已提交
689 690 691 692
		return;
	}
	/* Accumulate status and find out if a basic sense is needed. */
	ccw_device_accumulate_irb(cdev, irb);
693
	if (is_cmd && cdev->private->flags.dosense) {
L
Linus Torvalds 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
		if (ccw_device_do_sense(cdev, irb) == 0) {
			cdev->private->state = DEV_STATE_W4SENSE;
		}
		return;
	}
	/* Call the handler. */
	if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
		/* Start delayed path verification. */
		ccw_device_online_verify(cdev, 0);
}

/*
 * Got an timeout in online state.
 */
static void
ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
{
	int ret;

	ccw_device_set_timeout(cdev, 0);
	ret = ccw_device_cancel_halt_clear(cdev);
	if (ret == -EBUSY) {
		ccw_device_set_timeout(cdev, 3*HZ);
		cdev->private->state = DEV_STATE_TIMEOUT_KILL;
		return;
	}
720 721 722
	if (ret == -ENODEV)
		dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
	else if (cdev->handler)
L
Linus Torvalds 已提交
723 724 725 726 727 728 729
		cdev->handler(cdev, cdev->private->intparm,
			      ERR_PTR(-ETIMEDOUT));
}

/*
 * Got an interrupt for a basic sense.
 */
730
static void
L
Linus Torvalds 已提交
731 732 733 734 735 736
ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct irb *irb;

	irb = (struct irb *) __LC_IRB;
	/* Check for unsolicited interrupt. */
737 738 739
	if (scsw_stctl(&irb->scsw) ==
	    (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
		if (scsw_cc(&irb->scsw) == 1)
L
Linus Torvalds 已提交
740 741 742
			/* Basic sense hasn't started. Try again. */
			ccw_device_do_sense(cdev, irb);
		else {
743
			CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
C
Cornelia Huck 已提交
744 745 746
				      "interrupt during w4sense...\n",
				      cdev->private->dev_id.ssid,
				      cdev->private->dev_id.devno);
L
Linus Torvalds 已提交
747 748 749 750 751
			if (cdev->handler)
				cdev->handler (cdev, 0, irb);
		}
		return;
	}
752 753 754 755 756
	/*
	 * Check if a halt or clear has been issued in the meanwhile. If yes,
	 * only deliver the halt/clear interrupt to the device driver as if it
	 * had killed the original request.
	 */
757 758
	if (scsw_fctl(&irb->scsw) &
	    (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
759 760 761 762 763
		cdev->private->flags.dosense = 0;
		memset(&cdev->private->irb, 0, sizeof(struct irb));
		ccw_device_accumulate_irb(cdev, irb);
		goto call_handler;
	}
L
Linus Torvalds 已提交
764 765 766 767 768 769 770
	/* Add basic sense info to irb. */
	ccw_device_accumulate_basic_sense(cdev, irb);
	if (cdev->private->flags.dosense) {
		/* Another basic sense is needed. */
		ccw_device_do_sense(cdev, irb);
		return;
	}
771
call_handler:
L
Linus Torvalds 已提交
772
	cdev->private->state = DEV_STATE_ONLINE;
773 774
	/* In case sensing interfered with setting the device online */
	wake_up(&cdev->private->wait_q);
L
Linus Torvalds 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787
	/* Call the handler. */
	if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
		/* Start delayed path verification. */
		ccw_device_online_verify(cdev, 0);
}

static void
ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct subchannel *sch;

	sch = to_subchannel(cdev->dev.parent);
	ccw_device_set_timeout(cdev, 0);
788 789
	/* Start delayed path verification. */
	ccw_device_online_verify(cdev, 0);
L
Linus Torvalds 已提交
790 791 792
	/* OK, i/o is dead now. Call interrupt handler. */
	if (cdev->handler)
		cdev->handler(cdev, cdev->private->intparm,
793
			      ERR_PTR(-EIO));
L
Linus Torvalds 已提交
794 795 796 797 798 799 800 801 802 803 804 805
}

static void
ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
{
	int ret;

	ret = ccw_device_cancel_halt_clear(cdev);
	if (ret == -EBUSY) {
		ccw_device_set_timeout(cdev, 3*HZ);
		return;
	}
806 807
	/* Start delayed path verification. */
	ccw_device_online_verify(cdev, 0);
L
Linus Torvalds 已提交
808 809
	if (cdev->handler)
		cdev->handler(cdev, cdev->private->intparm,
810
			      ERR_PTR(-EIO));
L
Linus Torvalds 已提交
811 812
}

C
Cornelia Huck 已提交
813
void ccw_device_kill_io(struct ccw_device *cdev)
L
Linus Torvalds 已提交
814 815 816 817 818 819 820 821 822
{
	int ret;

	ret = ccw_device_cancel_halt_clear(cdev);
	if (ret == -EBUSY) {
		ccw_device_set_timeout(cdev, 3*HZ);
		cdev->private->state = DEV_STATE_TIMEOUT_KILL;
		return;
	}
823 824
	/* Start delayed path verification. */
	ccw_device_online_verify(cdev, 0);
L
Linus Torvalds 已提交
825 826
	if (cdev->handler)
		cdev->handler(cdev, cdev->private->intparm,
827
			      ERR_PTR(-EIO));
L
Linus Torvalds 已提交
828 829 830
}

static void
831
ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
L
Linus Torvalds 已提交
832
{
833
	/* Start verification after current task finished. */
834
	cdev->private->flags.doverify = 1;
L
Linus Torvalds 已提交
835 836 837 838 839 840 841 842
}

static void
ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct subchannel *sch;

	sch = to_subchannel(cdev->dev.parent);
843
	if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
L
Linus Torvalds 已提交
844 845 846 847 848 849
		/* Couldn't enable the subchannel for i/o. Sick device. */
		return;
	cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
	ccw_device_sense_id_start(cdev);
}

C
Cornelia Huck 已提交
850
void ccw_device_trigger_reprobe(struct ccw_device *cdev)
L
Linus Torvalds 已提交
851
{
C
Cornelia Huck 已提交
852
	struct subchannel *sch;
L
Linus Torvalds 已提交
853 854 855 856

	if (cdev->private->state != DEV_STATE_DISCONNECTED)
		return;

C
Cornelia Huck 已提交
857
	sch = to_subchannel(cdev->dev.parent);
L
Linus Torvalds 已提交
858
	/* Update some values. */
859
	if (cio_update_schib(sch))
860
		return;
L
Linus Torvalds 已提交
861 862 863 864
	/*
	 * The pim, pam, pom values may not be accurate, but they are the best
	 * we have before performing device selection :/
	 */
865
	sch->lpm = sch->schib.pmcw.pam & sch->opm;
866 867 868 869 870
	/*
	 * Use the initial configuration since we can't be shure that the old
	 * paths are valid.
	 */
	io_subchannel_init_config(sch);
871 872
	if (cio_commit_config(sch))
		return;
873

L
Linus Torvalds 已提交
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 877 878
	if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
		css_schedule_eval(sch->schid);
	else
879
		ccw_device_start_id(cdev, 0);
L
Linus Torvalds 已提交
880 881
}

882 883
static void ccw_device_disabled_irq(struct ccw_device *cdev,
				    enum dev_event dev_event)
L
Linus Torvalds 已提交
884 885 886 887 888
{
	struct subchannel *sch;

	sch = to_subchannel(cdev->dev.parent);
	/*
889
	 * An interrupt in a disabled state means a previous disable was not
890
	 * successful - should not happen, but we try to disable again.
L
Linus Torvalds 已提交
891 892 893 894 895 896 897 898 899 900 901 902
	 */
	cio_disable_subchannel(sch);
}

static void
ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
{
	retry_set_schib(cdev);
	cdev->private->state = DEV_STATE_ONLINE;
	dev_fsm_event(cdev, dev_event);
}

903 904 905 906 907 908 909
static void ccw_device_update_cmfblock(struct ccw_device *cdev,
				       enum dev_event dev_event)
{
	cmf_retry_copy_block(cdev);
	cdev->private->state = DEV_STATE_ONLINE;
	dev_fsm_event(cdev, dev_event);
}
L
Linus Torvalds 已提交
910 911 912 913 914

static void
ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
{
	ccw_device_set_timeout(cdev, 0);
S
Sebastian Ott 已提交
915
	cdev->private->state = DEV_STATE_NOT_OPER;
L
Linus Torvalds 已提交
916 917 918 919 920 921 922 923 924
	wake_up(&cdev->private->wait_q);
}

static void
ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
{
	int ret;

	ret = ccw_device_cancel_halt_clear(cdev);
S
Sebastian Ott 已提交
925 926 927
	if (ret == -EBUSY) {
		ccw_device_set_timeout(cdev, HZ/10);
	} else {
L
Linus Torvalds 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
		cdev->private->state = DEV_STATE_NOT_OPER;
		wake_up(&cdev->private->wait_q);
	}
}

/*
 * No operation action. This is used e.g. to ignore a timeout event in
 * state offline.
 */
static void
ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
{
}

/*
 * device statemachine
 */
fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
	[DEV_STATE_NOT_OPER] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_nop,
948
		[DEV_EVENT_INTERRUPT]	= ccw_device_disabled_irq,
L
Linus Torvalds 已提交
949 950 951 952
		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_SENSE_PGID] = {
953 954 955
		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
L
Linus Torvalds 已提交
956 957 958
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_SENSE_ID] = {
959 960 961
		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
L
Linus Torvalds 已提交
962 963 964
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_OFFLINE] = {
965
		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
966
		[DEV_EVENT_INTERRUPT]	= ccw_device_disabled_irq,
L
Linus Torvalds 已提交
967
		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
968
		[DEV_EVENT_VERIFY]	= ccw_device_offline_verify,
L
Linus Torvalds 已提交
969 970
	},
	[DEV_STATE_VERIFY] = {
971 972 973
		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
974
		[DEV_EVENT_VERIFY]	= ccw_device_delay_verify,
L
Linus Torvalds 已提交
975 976
	},
	[DEV_STATE_ONLINE] = {
977
		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
L
Linus Torvalds 已提交
978 979 980 981 982
		[DEV_EVENT_INTERRUPT]	= ccw_device_irq,
		[DEV_EVENT_TIMEOUT]	= ccw_device_online_timeout,
		[DEV_EVENT_VERIFY]	= ccw_device_online_verify,
	},
	[DEV_STATE_W4SENSE] = {
983
		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
L
Linus Torvalds 已提交
984 985 986 987 988
		[DEV_EVENT_INTERRUPT]	= ccw_device_w4sense,
		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
		[DEV_EVENT_VERIFY]	= ccw_device_online_verify,
	},
	[DEV_STATE_DISBAND_PGID] = {
989 990 991
		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
L
Linus Torvalds 已提交
992 993 994
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_BOXED] = {
995
		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
996 997 998
		[DEV_EVENT_INTERRUPT]	= ccw_device_nop,
		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
		[DEV_EVENT_VERIFY]	= ccw_device_boxed_verify,
L
Linus Torvalds 已提交
999 1000 1001
	},
	/* states to wait for i/o completion before doing something */
	[DEV_STATE_TIMEOUT_KILL] = {
1002
		[DEV_EVENT_NOTOPER]	= ccw_device_generic_notoper,
L
Linus Torvalds 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		[DEV_EVENT_INTERRUPT]	= ccw_device_killing_irq,
		[DEV_EVENT_TIMEOUT]	= ccw_device_killing_timeout,
		[DEV_EVENT_VERIFY]	= ccw_device_nop, //FIXME
	},
	[DEV_STATE_QUIESCE] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_quiesce_done,
		[DEV_EVENT_INTERRUPT]	= ccw_device_quiesce_done,
		[DEV_EVENT_TIMEOUT]	= ccw_device_quiesce_timeout,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	/* special states for devices gone not operational */
	[DEV_STATE_DISCONNECTED] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_nop,
		[DEV_EVENT_INTERRUPT]	= ccw_device_start_id,
1017
		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
1018
		[DEV_EVENT_VERIFY]	= ccw_device_start_id,
L
Linus Torvalds 已提交
1019 1020
	},
	[DEV_STATE_DISCONNECTED_SENSE_ID] = {
1021 1022 1023
		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028 1029 1030 1031
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_CMFCHANGE] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_change_cmfstate,
		[DEV_EVENT_INTERRUPT]	= ccw_device_change_cmfstate,
		[DEV_EVENT_TIMEOUT]	= ccw_device_change_cmfstate,
		[DEV_EVENT_VERIFY]	= ccw_device_change_cmfstate,
	},
1032 1033 1034 1035 1036 1037
	[DEV_STATE_CMFUPDATE] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_update_cmfblock,
		[DEV_EVENT_INTERRUPT]	= ccw_device_update_cmfblock,
		[DEV_EVENT_TIMEOUT]	= ccw_device_update_cmfblock,
		[DEV_EVENT_VERIFY]	= ccw_device_update_cmfblock,
	},
1038 1039 1040 1041 1042 1043
	[DEV_STATE_STEAL_LOCK] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_request_event,
		[DEV_EVENT_INTERRUPT]	= ccw_device_request_event,
		[DEV_EVENT_TIMEOUT]	= ccw_device_request_event,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
L
Linus Torvalds 已提交
1044 1045 1046
};

EXPORT_SYMBOL_GPL(ccw_device_set_timeout);