device_fsm.c 35.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
/*
 * drivers/s390/cio/device_fsm.c
 * finite state machine for device handling
 *
 *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
 *			 IBM Corporation
7
 *    Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds's avatar
Linus Torvalds committed
8
9
10
11
12
 *		 Martin Schwidefsky (schwidefsky@de.ibm.com)
 */

#include <linux/module.h>
#include <linux/init.h>
Tim Schmielau's avatar
Tim Schmielau committed
13
14
#include <linux/jiffies.h>
#include <linux/string.h>
Linus Torvalds's avatar
Linus Torvalds committed
15
16

#include <asm/ccwdev.h>
17
#include <asm/cio.h>
18
#include <asm/chpid.h>
Linus Torvalds's avatar
Linus Torvalds committed
19
20
21
22
23
24
25

#include "cio.h"
#include "cio_debug.h"
#include "css.h"
#include "device.h"
#include "chsc.h"
#include "ioasm.h"
26
#include "chp.h"
Linus Torvalds's avatar
Linus Torvalds committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

int
device_is_online(struct subchannel *sch)
{
	struct ccw_device *cdev;

	if (!sch->dev.driver_data)
		return 0;
	cdev = sch->dev.driver_data;
	return (cdev->private->state == DEV_STATE_ONLINE);
}

int
device_is_disconnected(struct subchannel *sch)
{
	struct ccw_device *cdev;

	if (!sch->dev.driver_data)
		return 0;
	cdev = sch->dev.driver_data;
	return (cdev->private->state == DEV_STATE_DISCONNECTED ||
		cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
}

void
device_set_disconnected(struct subchannel *sch)
{
	struct ccw_device *cdev;

	if (!sch->dev.driver_data)
		return;
	cdev = sch->dev.driver_data;
	ccw_device_set_timeout(cdev, 0);
	cdev->private->flags.fake_irb = 0;
	cdev->private->state = DEV_STATE_DISCONNECTED;
}

64
65
66
67
68
69
70
71
72
73
void device_set_intretry(struct subchannel *sch)
{
	struct ccw_device *cdev;

	cdev = sch->dev.driver_data;
	if (!cdev)
		return;
	cdev->private->flags.intretry = 1;
}

74
75
76
77
78
79
80
81
82
83
84
int device_trigger_verify(struct subchannel *sch)
{
	struct ccw_device *cdev;

	cdev = sch->dev.driver_data;
	if (!cdev || !cdev->online)
		return -EINVAL;
	dev_fsm_event(cdev, DEV_EVENT_VERIFY);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
143
144
145
/*
 * 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);
	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);
}

/* Kill any pending timers after machine check. */
void
device_kill_pending_timer(struct subchannel *sch)
{
	struct ccw_device *cdev;

	if (!sch->dev.driver_data)
		return;
	cdev = sch->dev.driver_data;
	ccw_device_set_timeout(cdev, 0);
}

/*
 * 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);
146
	ret = stsch(sch->schid, &sch->schib);
Linus Torvalds's avatar
Linus Torvalds committed
147
148
	if (ret || !sch->schib.pmcw.dnv)
		return -ENODEV; 
149
150
	if (!sch->schib.pmcw.ena)
		/* Not operational -> done. */
Linus Torvalds's avatar
Linus Torvalds committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
		return 0;
	/* Stage 1: cancel io. */
	if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) &&
	    !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
		ret = cio_cancel(sch);
		if (ret != -EINVAL)
			return ret;
		/* cancel io unsuccessful. From now on it is asynchronous. */
		cdev->private->iretry = 3;	/* 3 halt retries. */
	}
	if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) {
		/* Stage 2: halt io. */
		if (cdev->private->iretry) {
			cdev->private->iretry--;
			ret = cio_halt(sch);
166
167
			if (ret != -EBUSY)
				return (ret == 0) ? -EBUSY : ret;
Linus Torvalds's avatar
Linus Torvalds committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
		}
		/* 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");
}

static int
ccw_device_handle_oper(struct ccw_device *cdev)
{
	struct subchannel *sch;

	sch = to_subchannel(cdev->dev.parent);
	cdev->private->flags.recog_done = 1;
	/*
	 * Check if cu type and device type still match. If
	 * not, it is certainly another device and we have to
191
	 * de- and re-register.
Linus Torvalds's avatar
Linus Torvalds committed
192
193
194
195
	 */
	if (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 ||
196
	    cdev->id.dev_model != cdev->private->senseid.dev_model) {
Linus Torvalds's avatar
Linus Torvalds committed
197
		PREPARE_WORK(&cdev->private->kick_work,
198
			     ccw_device_do_unreg_rereg);
Linus Torvalds's avatar
Linus Torvalds committed
199
200
201
202
203
204
205
206
207
208
209
210
		queue_work(ccw_device_work, &cdev->private->kick_work);
		return 0;
	}
	cdev->private->flags.donotify = 1;
	return 1;
}

/*
 * 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).
 */
211
static void
Linus Torvalds's avatar
Linus Torvalds committed
212
213
214
__recover_lost_chpids(struct subchannel *sch, int old_lpm)
{
	int mask, i;
215
	struct chp_id chpid;
Linus Torvalds's avatar
Linus Torvalds committed
216

217
	chp_id_init(&chpid);
Linus Torvalds's avatar
Linus Torvalds committed
218
219
220
221
222
223
	for (i = 0; i<8; i++) {
		mask = 0x80 >> i;
		if (!(sch->lpm & mask))
			continue;
		if (old_lpm & mask)
			continue;
224
		chpid.id = sch->schib.pmcw.chpid[i];
225
226
		if (!chp_is_registered(chpid))
			css_schedule_eval_all();
Linus Torvalds's avatar
Linus Torvalds committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
	}
}

/*
 * Stop device recognition.
 */
static void
ccw_device_recog_done(struct ccw_device *cdev, int state)
{
	struct subchannel *sch;
	int notify, old_lpm, same_dev;

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

	ccw_device_set_timeout(cdev, 0);
	cio_disable_subchannel(sch);
	/*
	 * Now that we tried recognition, we have performed device selection
	 * through ssch() and the path information is up to date.
	 */
	old_lpm = sch->lpm;
248
	stsch(sch->schid, &sch->schib);
249
	sch->lpm = sch->schib.pmcw.pam & sch->opm;
250
251
252
	/* Check since device may again have become not operational. */
	if (!sch->schib.pmcw.dnv)
		state = DEV_STATE_NOT_OPER;
Linus Torvalds's avatar
Linus Torvalds committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
	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);
	if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
		if (state == DEV_STATE_NOT_OPER) {
			cdev->private->flags.recog_done = 1;
			cdev->private->state = DEV_STATE_DISCONNECTED;
			return;
		}
		/* Boxed devices don't need extra treatment. */
	}
	notify = 0;
	same_dev = 0; /* Keep the compiler quiet... */
	switch (state) {
	case DEV_STATE_NOT_OPER:
		CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huck's avatar
Cornelia Huck committed
271
			  "cio: SenseID : unknown device %04x on subchannel "
272
			  "0.%x.%04x\n", cdev->private->dev_id.devno,
273
			  sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds's avatar
Linus Torvalds committed
274
275
276
277
278
279
280
		break;
	case DEV_STATE_OFFLINE:
		if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) {
			same_dev = ccw_device_handle_oper(cdev);
			notify = 1;
		}
		/* fill out sense information */
281
		memset(&cdev->id, 0, sizeof(cdev->id));
282
283
284
285
		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;
Linus Torvalds's avatar
Linus Torvalds committed
286
287
288
289
290
291
292
293
294
295
		if (notify) {
			cdev->private->state = DEV_STATE_OFFLINE;
			if (same_dev) {
				/* Get device online again. */
				ccw_device_online(cdev);
				wake_up(&cdev->private->wait_q);
			}
			return;
		}
		/* Issue device info message. */
Cornelia Huck's avatar
Cornelia Huck committed
296
297
		CIO_DEBUG(KERN_INFO, 2,
			  "cio: SenseID : device 0.%x.%04x reports: "
Linus Torvalds's avatar
Linus Torvalds committed
298
			  "CU  Type/Mod = %04X/%02X, Dev Type/Mod = "
299
			  "%04X/%02X\n",
300
301
			  cdev->private->dev_id.ssid,
			  cdev->private->dev_id.devno,
Linus Torvalds's avatar
Linus Torvalds committed
302
303
304
305
306
			  cdev->id.cu_type, cdev->id.cu_model,
			  cdev->id.dev_type, cdev->id.dev_model);
		break;
	case DEV_STATE_BOXED:
		CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huck's avatar
Cornelia Huck committed
307
			  "cio: SenseID : boxed device %04x on subchannel "
308
			  "0.%x.%04x\n", cdev->private->dev_id.devno,
309
			  sch->schid.ssid, sch->schid.sch_no);
Linus Torvalds's avatar
Linus Torvalds committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
		break;
	}
	cdev->private->state = state;
	io_subchannel_recog_done(cdev);
	if (state != DEV_STATE_NOT_OPER)
		wake_up(&cdev->private->wait_q);
}

/*
 * 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;
	}
}

static void
338
ccw_device_oper_notify(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
339
{
340
	struct ccw_device_private *priv;
Linus Torvalds's avatar
Linus Torvalds committed
341
342
343
	struct ccw_device *cdev;
	struct subchannel *sch;
	int ret;
344
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
345

346
347
	priv = container_of(work, struct ccw_device_private, kick_work);
	cdev = priv->cdev;
348
	spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
349
	sch = to_subchannel(cdev->dev.parent);
350
351
352
353
354
355
356
	if (sch->driver && sch->driver->notify) {
		spin_unlock_irqrestore(cdev->ccwlock, flags);
		ret = sch->driver->notify(&sch->dev, CIO_OPER);
		spin_lock_irqsave(cdev->ccwlock, flags);
	} else
		ret = 0;
	if (ret) {
357
		/* Reenable channel measurements, if needed. */
358
		spin_unlock_irqrestore(cdev->ccwlock, flags);
359
		cmf_reenable(cdev);
360
		spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
361
		wake_up(&cdev->private->wait_q);
362
	}
363
364
365
366
	spin_unlock_irqrestore(cdev->ccwlock, flags);
	if (!ret)
		/* Driver doesn't want device back. */
		ccw_device_do_unreg_rereg(work);
Linus Torvalds's avatar
Linus Torvalds committed
367
368
369
370
371
372
373
374
375
376
377
378
}

/*
 * 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);

379
380
	ccw_device_set_timeout(cdev, 0);

Linus Torvalds's avatar
Linus Torvalds committed
381
382
383
384
385
386
387
388
389
390
391
	if (state != DEV_STATE_ONLINE)
		cio_disable_subchannel(sch);

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

	cdev->private->state = state;


	if (state == DEV_STATE_BOXED)
		CIO_DEBUG(KERN_WARNING, 2,
Cornelia Huck's avatar
Cornelia Huck committed
392
			  "cio: Boxed device %04x on subchannel %04x\n",
393
			  cdev->private->dev_id.devno, sch->schid.sch_no);
Linus Torvalds's avatar
Linus Torvalds committed
394
395
396

	if (cdev->private->flags.donotify) {
		cdev->private->flags.donotify = 0;
397
		PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify);
Linus Torvalds's avatar
Linus Torvalds committed
398
399
400
401
402
403
404
405
		queue_work(ccw_device_notify_work, &cdev->private->kick_work);
	}
	wake_up(&cdev->private->wait_q);

	if (css_init_done && state != DEV_STATE_ONLINE)
		put_device (&cdev->dev);
}

406
static int cmp_pgid(struct pgid *p1, struct pgid *p2)
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
{
	char *c1;
	char *c2;

	c1 = (char *)p1;
	c2 = (char *)p2;

	return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1);
}

static void __ccw_device_get_common_pgid(struct ccw_device *cdev)
{
	int i;
	int last;

	last = 0;
	for (i = 0; i < 8; i++) {
		if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET)
			/* No PGID yet */
			continue;
		if (cdev->private->pgid[last].inf.ps.state1 ==
		    SNID_STATE1_RESET) {
			/* First non-zero PGID */
			last = i;
			continue;
		}
		if (cmp_pgid(&cdev->private->pgid[i],
			     &cdev->private->pgid[last]) == 0)
			/* Non-conflicting PGIDs */
			continue;

		/* PGID mismatch, can't pathgroup. */
		CIO_MSG_EVENT(0, "SNID - pgid mismatch for device "
			      "0.%x.%04x, can't pathgroup\n",
441
442
			      cdev->private->dev_id.ssid,
			      cdev->private->dev_id.devno);
443
444
445
446
447
448
		cdev->private->options.pgroup = 0;
		return;
	}
	if (cdev->private->pgid[last].inf.ps.state1 ==
	    SNID_STATE1_RESET)
		/* No previous pgid found */
449
450
		memcpy(&cdev->private->pgid[0],
		       &channel_subsystems[0]->global_pgid,
451
452
453
454
455
456
457
		       sizeof(struct pgid));
	else
		/* Use existing pgid */
		memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last],
		       sizeof(struct pgid));
}

Linus Torvalds's avatar
Linus Torvalds committed
458
459
460
461
462
463
464
465
466
467
/*
 * Function called from device_pgid.c after sense path ground has completed.
 */
void
ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
{
	struct subchannel *sch;

	sch = to_subchannel(cdev->dev.parent);
	switch (err) {
468
469
470
471
472
473
474
	case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
		cdev->private->options.pgroup = 0;
		break;
	case 0: /* success */
	case -EACCES: /* partial success, some paths not operational */
		/* Check if all pgids are equal or 0. */
		__ccw_device_get_common_pgid(cdev);
Linus Torvalds's avatar
Linus Torvalds committed
475
476
477
478
		break;
	case -ETIME:		/* Sense path group id stopped by timeout. */
	case -EUSERS:		/* device is reserved for someone else. */
		ccw_device_done(cdev, DEV_STATE_BOXED);
479
		return;
Linus Torvalds's avatar
Linus Torvalds committed
480
481
	default:
		ccw_device_done(cdev, DEV_STATE_NOT_OPER);
482
		return;
Linus Torvalds's avatar
Linus Torvalds committed
483
	}
484
485
	/* Start Path Group verification. */
	cdev->private->state = DEV_STATE_VERIFY;
486
	cdev->private->flags.doverify = 0;
487
	ccw_device_verify_start(cdev);
Linus Torvalds's avatar
Linus Torvalds committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
}

/*
 * Start device recognition.
 */
int
ccw_device_recognition(struct ccw_device *cdev)
{
	struct subchannel *sch;
	int ret;

	if ((cdev->private->state != DEV_STATE_NOT_OPER) &&
	    (cdev->private->state != DEV_STATE_BOXED))
		return -EINVAL;
	sch = to_subchannel(cdev->dev.parent);
	ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
	if (ret != 0)
		/* Couldn't enable the subchannel for i/o. Sick device. */
		return ret;

	/* After 60s the device recognition is considered to have failed. */
	ccw_device_set_timeout(cdev, 60*HZ);

	/*
	 * 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;
	ccw_device_sense_id_start(cdev);
	return 0;
}

/*
 * Handle timeout in device recognition.
 */
static void
ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event)
{
	int ret;

	ret = ccw_device_cancel_halt_clear(cdev);
	switch (ret) {
	case 0:
		ccw_device_recog_done(cdev, DEV_STATE_BOXED);
		break;
	case -ENODEV:
		ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
		break;
	default:
		ccw_device_set_timeout(cdev, 3*HZ);
	}
}


static void
548
ccw_device_nopath_notify(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
549
{
550
	struct ccw_device_private *priv;
Linus Torvalds's avatar
Linus Torvalds committed
551
552
553
	struct ccw_device *cdev;
	struct subchannel *sch;
	int ret;
554
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
555

556
557
	priv = container_of(work, struct ccw_device_private, kick_work);
	cdev = priv->cdev;
558
	spin_lock_irqsave(cdev->ccwlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
559
560
561
	sch = to_subchannel(cdev->dev.parent);
	/* Extra sanity. */
	if (sch->lpm)
562
563
564
565
566
567
568
		goto out_unlock;
	if (sch->driver && sch->driver->notify) {
		spin_unlock_irqrestore(cdev->ccwlock, flags);
		ret = sch->driver->notify(&sch->dev, CIO_NO_PATH);
		spin_lock_irqsave(cdev->ccwlock, flags);
	} else
		ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
569
570
571
572
573
574
	if (!ret) {
		if (get_device(&sch->dev)) {
			/* Driver doesn't want to keep device. */
			cio_disable_subchannel(sch);
			if (get_device(&cdev->dev)) {
				PREPARE_WORK(&cdev->private->kick_work,
575
					     ccw_device_call_sch_unregister);
Linus Torvalds's avatar
Linus Torvalds committed
576
577
578
579
580
581
582
583
584
585
586
587
				queue_work(ccw_device_work,
					   &cdev->private->kick_work);
			} else
				put_device(&sch->dev);
		}
	} else {
		cio_disable_subchannel(sch);
		ccw_device_set_timeout(cdev, 0);
		cdev->private->flags.fake_irb = 0;
		cdev->private->state = DEV_STATE_DISCONNECTED;
		wake_up(&cdev->private->wait_q);
	}
588
589
out_unlock:
	spin_unlock_irqrestore(cdev->ccwlock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
590
591
592
593
594
}

void
ccw_device_verify_done(struct ccw_device *cdev, int err)
{
595
596
597
598
599
600
601
602
603
604
605
606
607
	struct subchannel *sch;

	sch = to_subchannel(cdev->dev.parent);
	/* Update schib - pom may have changed. */
	stsch(sch->schid, &sch->schib);
	/* Update lpm with verified path mask. */
	sch->lpm = sch->vpm;
	/* Repeat path verification? */
	if (cdev->private->flags.doverify) {
		cdev->private->flags.doverify = 0;
		ccw_device_verify_start(cdev);
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
608
609
610
611
612
613
614
615
	switch (err) {
	case -EOPNOTSUPP: /* path grouping not supported, just set online. */
		cdev->private->options.pgroup = 0;
	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));
616
617
618
619
			cdev->private->irb.scsw.cc = 1;
			cdev->private->irb.scsw.fctl = SCSW_FCTL_START_FUNC;
			cdev->private->irb.scsw.actl = SCSW_ACTL_START_PEND;
			cdev->private->irb.scsw.stctl = SCSW_STCTL_STATUS_PEND;
Linus Torvalds's avatar
Linus Torvalds committed
620
621
622
623
624
625
626
627
			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:
628
629
		/* Reset oper notify indication after verify error. */
		cdev->private->flags.donotify = 0;
Linus Torvalds's avatar
Linus Torvalds committed
630
631
632
		ccw_device_done(cdev, DEV_STATE_BOXED);
		break;
	default:
633
634
		/* Reset oper notify indication after verify error. */
		cdev->private->flags.donotify = 0;
635
636
637
638
639
640
641
		if (cdev->online) {
			PREPARE_WORK(&cdev->private->kick_work,
				     ccw_device_nopath_notify);
			queue_work(ccw_device_notify_work,
				   &cdev->private->kick_work);
		} else
			ccw_device_done(cdev, DEV_STATE_NOT_OPER);
Linus Torvalds's avatar
Linus Torvalds committed
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
		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);
	if (css_init_done && !get_device(&cdev->dev))
		return -ENODEV;
	ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc);
	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;
	}
	/* Do we want to do path grouping? */
	if (!cdev->private->options.pgroup) {
670
671
		/* Start initial path verification. */
		cdev->private->state = DEV_STATE_VERIFY;
672
		cdev->private->flags.doverify = 0;
673
		ccw_device_verify_start(cdev);
Linus Torvalds's avatar
Linus Torvalds committed
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
		return 0;
	}
	/* Do a SensePGID first. */
	cdev->private->state = DEV_STATE_SENSE_PGID;
	ccw_device_sense_pgid_start(cdev);
	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:
693
694
695
696
697
698
		cdev->private->flags.donotify = 0;
		if (get_device(&cdev->dev)) {
			PREPARE_WORK(&cdev->private->kick_work,
				     ccw_device_call_sch_unregister);
			queue_work(ccw_device_work, &cdev->private->kick_work);
		}
Linus Torvalds's avatar
Linus Torvalds committed
699
700
701
702
703
704
705
706
707
708
709
710
711
		ccw_device_done(cdev, DEV_STATE_NOT_OPER);
		break;
	}
}

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

712
713
714
715
	if (ccw_device_is_orphan(cdev)) {
		ccw_device_done(cdev, DEV_STATE_OFFLINE);
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
716
	sch = to_subchannel(cdev->dev.parent);
717
	if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv)
Linus Torvalds's avatar
Linus Torvalds committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
		return -ENODEV;
	if (cdev->private->state != DEV_STATE_ONLINE) {
		if (sch->schib.scsw.actl != 0)
			return -EBUSY;
		return -EINVAL;
	}
	if (sch->schib.scsw.actl != 0)
		return -EBUSY;
	/* Are we doing path grouping? */
	if (!cdev->private->options.pgroup) {
		/* 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;
}

/*
 * Handle timeout in device online/offline process.
 */
static void
ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event)
{
	int ret;

	ret = ccw_device_cancel_halt_clear(cdev);
	switch (ret) {
	case 0:
		ccw_device_done(cdev, DEV_STATE_BOXED);
		break;
	case -ENODEV:
		ccw_device_done(cdev, DEV_STATE_NOT_OPER);
		break;
	default:
		ccw_device_set_timeout(cdev, 3*HZ);
	}
}

/*
 * Handle not oper event in device recognition.
 */
static void
ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event)
{
	ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
}

/*
 * Handle not operational event while offline.
 */
static void
ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct subchannel *sch;

	cdev->private->state = DEV_STATE_NOT_OPER;
	sch = to_subchannel(cdev->dev.parent);
	if (get_device(&cdev->dev)) {
		PREPARE_WORK(&cdev->private->kick_work,
780
			     ccw_device_call_sch_unregister);
Linus Torvalds's avatar
Linus Torvalds committed
781
782
783
784
785
786
787
788
789
790
791
792
		queue_work(ccw_device_work, &cdev->private->kick_work);
	}
	wake_up(&cdev->private->wait_q);
}

/*
 * Handle not operational event while online.
 */
static void
ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct subchannel *sch;
793
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
794
795

	sch = to_subchannel(cdev->dev.parent);
796
797
798
799
800
801
802
803
804
805
806
807
808
	if (sch->driver->notify) {
		spin_unlock_irq(cdev->ccwlock);
		ret = sch->driver->notify(&sch->dev,
					  sch->lpm ? CIO_GONE : CIO_NO_PATH);
		spin_lock_irq(cdev->ccwlock);
	} else
		ret = 0;
	if (ret) {
		ccw_device_set_timeout(cdev, 0);
		cdev->private->flags.fake_irb = 0;
		cdev->private->state = DEV_STATE_DISCONNECTED;
		wake_up(&cdev->private->wait_q);
		return;
Linus Torvalds's avatar
Linus Torvalds committed
809
810
811
812
813
814
815
816
817
	}
	cdev->private->state = DEV_STATE_NOT_OPER;
	cio_disable_subchannel(sch);
	if (sch->schib.scsw.actl != 0) {
		// FIXME: not-oper indication to device driver ?
		ccw_device_call_handler(cdev);
	}
	if (get_device(&cdev->dev)) {
		PREPARE_WORK(&cdev->private->kick_work,
818
			     ccw_device_call_sch_unregister);
Linus Torvalds's avatar
Linus Torvalds committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
		queue_work(ccw_device_work, &cdev->private->kick_work);
	}
	wake_up(&cdev->private->wait_q);
}

/*
 * 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.
	 */
841
	stsch(sch->schid, &sch->schib);
Linus Torvalds's avatar
Linus Torvalds committed
842
843

	if (sch->schib.scsw.actl != 0 ||
844
	    (sch->schib.scsw.stctl & SCSW_STCTL_STATUS_PEND) ||
Linus Torvalds's avatar
Linus Torvalds committed
845
846
847
848
849
850
851
852
853
854
855
	    (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) {
		/*
		 * 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;
856
	cdev->private->flags.doverify = 0;
Linus Torvalds's avatar
Linus Torvalds committed
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
	ccw_device_verify_start(cdev);
}

/*
 * 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;

	irb = (struct irb *) __LC_IRB;
	/* Check for unsolicited interrupt. */
	if ((irb->scsw.stctl ==
	    		(SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS))
	    && (!irb->scsw.cc)) {
		if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) &&
		    !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;
878
			memcpy(&cdev->private->irb, irb, sizeof(struct irb));
Linus Torvalds's avatar
Linus Torvalds committed
879
880
881
882
883
884
885
			cdev->private->state = DEV_STATE_W4SENSE;
			cdev->private->intparm = 0;
			return;
		}
call_handler_unsol:
		if (cdev->handler)
			cdev->handler (cdev, 0, irb);
886
887
		if (cdev->private->flags.doverify)
			ccw_device_online_verify(cdev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
		return;
	}
	/* Accumulate status and find out if a basic sense is needed. */
	ccw_device_accumulate_irb(cdev, irb);
	if (cdev->private->flags.dosense) {
		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;
	}
	if (ret == -ENODEV) {
		struct subchannel *sch;

		sch = to_subchannel(cdev->dev.parent);
		if (!sch->lpm) {
			PREPARE_WORK(&cdev->private->kick_work,
925
				     ccw_device_nopath_notify);
Linus Torvalds's avatar
Linus Torvalds committed
926
927
928
929
930
931
932
933
934
935
936
937
			queue_work(ccw_device_notify_work,
				   &cdev->private->kick_work);
		} else
			dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
	} else if (cdev->handler)
		cdev->handler(cdev, cdev->private->intparm,
			      ERR_PTR(-ETIMEDOUT));
}

/*
 * Got an interrupt for a basic sense.
 */
938
static void
Linus Torvalds's avatar
Linus Torvalds committed
939
940
941
942
943
944
945
946
947
948
949
950
ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct irb *irb;

	irb = (struct irb *) __LC_IRB;
	/* Check for unsolicited interrupt. */
	if (irb->scsw.stctl ==
	    		(SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
		if (irb->scsw.cc == 1)
			/* Basic sense hasn't started. Try again. */
			ccw_device_do_sense(cdev, irb);
		else {
Cornelia Huck's avatar
Cornelia Huck committed
951
952
953
954
			CIO_MSG_EVENT(2, "Huh? 0.%x.%04x: unsolicited "
				      "interrupt during w4sense...\n",
				      cdev->private->dev_id.ssid,
				      cdev->private->dev_id.devno);
Linus Torvalds's avatar
Linus Torvalds committed
955
956
957
958
959
			if (cdev->handler)
				cdev->handler (cdev, 0, irb);
		}
		return;
	}
960
961
962
963
964
965
	/*
	 * 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.
	 */
	if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
966
967
968
969
970
971
		/* Retry Basic Sense if requested. */
		if (cdev->private->flags.intretry) {
			cdev->private->flags.intretry = 0;
			ccw_device_do_sense(cdev, irb);
			return;
		}
972
973
974
975
976
		cdev->private->flags.dosense = 0;
		memset(&cdev->private->irb, 0, sizeof(struct irb));
		ccw_device_accumulate_irb(cdev, irb);
		goto call_handler;
	}
Linus Torvalds's avatar
Linus Torvalds committed
977
978
979
980
981
982
983
	/* 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;
	}
984
call_handler:
Linus Torvalds's avatar
Linus Torvalds committed
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
	cdev->private->state = DEV_STATE_ONLINE;
	/* 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_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct irb *irb;

	irb = (struct irb *) __LC_IRB;
	/* Accumulate status. We don't do basic sense. */
	ccw_device_accumulate_irb(cdev, irb);
Cornelia Huck's avatar
Cornelia Huck committed
1000
1001
	/* Remember to clear irb to avoid residuals. */
	memset(&cdev->private->irb, 0, sizeof(struct irb));
Linus Torvalds's avatar
Linus Torvalds committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
	/* Try to start delayed device verification. */
	ccw_device_online_verify(cdev, 0);
	/* Note: Don't call handler for cio initiated clear! */
}

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);
1014
1015
	/* Start delayed path verification. */
	ccw_device_online_verify(cdev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1016
1017
1018
	/* OK, i/o is dead now. Call interrupt handler. */
	if (cdev->handler)
		cdev->handler(cdev, cdev->private->intparm,
1019
			      ERR_PTR(-EIO));
Linus Torvalds's avatar
Linus Torvalds committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
}

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;
	}
1032
1033
	/* Start delayed path verification. */
	ccw_device_online_verify(cdev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1034
1035
	if (cdev->handler)
		cdev->handler(cdev, cdev->private->intparm,
1036
			      ERR_PTR(-EIO));
Linus Torvalds's avatar
Linus Torvalds committed
1037
1038
}

1039
void device_kill_io(struct subchannel *sch)
Linus Torvalds's avatar
Linus Torvalds committed
1040
1041
{
	int ret;
1042
	struct ccw_device *cdev;
Linus Torvalds's avatar
Linus Torvalds committed
1043

1044
	cdev = sch->dev.driver_data;
Linus Torvalds's avatar
Linus Torvalds committed
1045
1046
1047
1048
1049
1050
	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;
	}
1051
1052
	/* Start delayed path verification. */
	ccw_device_online_verify(cdev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1053
1054
	if (cdev->handler)
		cdev->handler(cdev, cdev->private->intparm,
1055
			      ERR_PTR(-EIO));
Linus Torvalds's avatar
Linus Torvalds committed
1056
1057
1058
}

static void
1059
ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
Linus Torvalds's avatar
Linus Torvalds committed
1060
{
1061
	/* Start verification after current task finished. */
1062
	cdev->private->flags.doverify = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
}

static void
ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
{
	struct irb *irb;

	switch (dev_event) {
	case DEV_EVENT_INTERRUPT:
		irb = (struct irb *) __LC_IRB;
		/* Check for unsolicited interrupt. */
		if ((irb->scsw.stctl ==
		     (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
		    (!irb->scsw.cc))
			/* FIXME: we should restart stlck here, but this
			 * is extremely unlikely ... */
			goto out_wakeup;

		ccw_device_accumulate_irb(cdev, irb);
		/* We don't care about basic sense etc. */
		break;
	default: /* timeout */
		break;
	}
out_wakeup:
	wake_up(&cdev->private->wait_q);
}

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

	sch = to_subchannel(cdev->dev.parent);
	if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0)
		/* Couldn't enable the subchannel for i/o. Sick device. */
		return;

	/* After 60s the device recognition is considered to have failed. */
	ccw_device_set_timeout(cdev, 60*HZ);

	cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
	ccw_device_sense_id_start(cdev);
}

void
device_trigger_reprobe(struct subchannel *sch)
{
	struct ccw_device *cdev;

	if (!sch->dev.driver_data)
		return;
	cdev = sch->dev.driver_data;
	if (cdev->private->state != DEV_STATE_DISCONNECTED)
		return;

	/* Update some values. */
1120
	if (stsch(sch->schid, &sch->schib))
Linus Torvalds's avatar
Linus Torvalds committed
1121
		return;
1122
1123
	if (!sch->schib.pmcw.dnv)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1124
1125
1126
1127
	/*
	 * The pim, pam, pom values may not be accurate, but they are the best
	 * we have before performing device selection :/
	 */
1128
	sch->lpm = sch->schib.pmcw.pam & sch->opm;
Linus Torvalds's avatar
Linus Torvalds committed
1129
1130
1131
1132
1133
1134
1135
1136
	/* Re-set some bits in the pmcw that were lost. */
	sch->schib.pmcw.isc = 3;
	sch->schib.pmcw.csense = 1;
	sch->schib.pmcw.ena = 0;
	if ((sch->lpm & (sch->lpm - 1)) != 0)
		sch->schib.pmcw.mp = 1;
	sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
	/* We should also udate ssd info, but this has to wait. */
1137
1138
1139
1140
1141
1142
1143
	/* Check if this is another device which appeared on the same sch. */
	if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
		PREPARE_WORK(&cdev->private->kick_work,
			     ccw_device_move_to_orphanage);
		queue_work(ccw_device_work, &cdev->private->kick_work);
	} else
		ccw_device_start_id(cdev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
}

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

	sch = to_subchannel(cdev->dev.parent);
	/*
	 * An interrupt in state offline means a previous disable was not
	 * successful. Try again.
	 */
	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);
}

1167
1168
1169
1170
1171
1172
1173
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);
}
Linus Torvalds's avatar
Linus Torvalds committed
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220

static void
ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
{
	ccw_device_set_timeout(cdev, 0);
	if (dev_event == DEV_EVENT_NOTOPER)
		cdev->private->state = DEV_STATE_NOT_OPER;
	else
		cdev->private->state = DEV_STATE_OFFLINE;
	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);
	switch (ret) {
	case 0:
		cdev->private->state = DEV_STATE_OFFLINE;
		wake_up(&cdev->private->wait_q);
		break;
	case -ENODEV:
		cdev->private->state = DEV_STATE_NOT_OPER;
		wake_up(&cdev->private->wait_q);
		break;
	default:
		ccw_device_set_timeout(cdev, HZ/10);
	}
}

/*
 * 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)
{
}

/*
 * Bug operation action. 
 */
static void
ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event)
{
Cornelia Huck's avatar
Cornelia Huck committed
1221
1222
	CIO_MSG_EVENT(0, "dev_jumptable[%i][%i] == NULL\n",
		      cdev->private->state, dev_event);
Linus Torvalds's avatar
Linus Torvalds committed
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
	BUG();
}

/*
 * device statemachine
 */
fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
	[DEV_STATE_NOT_OPER] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_nop,
		[DEV_EVENT_INTERRUPT]	= ccw_device_bug,
		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_SENSE_PGID] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_online_notoper,
		[DEV_EVENT_INTERRUPT]	= ccw_device_sense_pgid_irq,
		[DEV_EVENT_TIMEOUT]	= ccw_device_onoff_timeout,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_SENSE_ID] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_recog_notoper,
		[DEV_EVENT_INTERRUPT]	= ccw_device_sense_id_irq,
		[DEV_EVENT_TIMEOUT]	= ccw_device_recog_timeout,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_OFFLINE] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_offline_notoper,
		[DEV_EVENT_INTERRUPT]	= ccw_device_offline_irq,
		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_VERIFY] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_online_notoper,
		[DEV_EVENT_INTERRUPT]	= ccw_device_verify_irq,
		[DEV_EVENT_TIMEOUT]	= ccw_device_onoff_timeout,
1258
		[DEV_EVENT_VERIFY]	= ccw_device_delay_verify,
Linus Torvalds's avatar
Linus Torvalds committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
	},
	[DEV_STATE_ONLINE] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_online_notoper,
		[DEV_EVENT_INTERRUPT]	= ccw_device_irq,
		[DEV_EVENT_TIMEOUT]	= ccw_device_online_timeout,
		[DEV_EVENT_VERIFY]	= ccw_device_online_verify,
	},
	[DEV_STATE_W4SENSE] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_online_notoper,
		[DEV_EVENT_INTERRUPT]	= ccw_device_w4sense,
		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
		[DEV_EVENT_VERIFY]	= ccw_device_online_verify,
	},
	[DEV_STATE_DISBAND_PGID] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_online_notoper,
		[DEV_EVENT_INTERRUPT]	= ccw_device_disband_irq,
		[DEV_EVENT_TIMEOUT]	= ccw_device_onoff_timeout,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_BOXED] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_offline_notoper,
		[DEV_EVENT_INTERRUPT]	= ccw_device_stlck_done,
		[DEV_EVENT_TIMEOUT]	= ccw_device_stlck_done,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	/* states to wait for i/o completion before doing something */
	[DEV_STATE_CLEAR_VERIFY] = {
		[DEV_EVENT_NOTOPER]     = ccw_device_online_notoper,
		[DEV_EVENT_INTERRUPT]   = ccw_device_clear_verify,
		[DEV_EVENT_TIMEOUT]	= ccw_device_nop,
		[DEV_EVENT_VERIFY]	= ccw_device_nop,
	},
	[DEV_STATE_TIMEOUT_KILL] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_online_notoper,
		[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,
		[DEV_EVENT_TIMEOUT]	= ccw_device_bug,
1308
		[DEV_EVENT_VERIFY]	= ccw_device_start_id,
Linus Torvalds's avatar
Linus Torvalds committed
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
	},
	[DEV_STATE_DISCONNECTED_SENSE_ID] = {
		[DEV_EVENT_NOTOPER]	= ccw_device_recog_notoper,
		[DEV_EVENT_INTERRUPT]	= ccw_device_sense_id_irq,
		[DEV_EVENT_TIMEOUT]	= ccw_device_recog_timeout,
		[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,
	},
1322
1323
1324
1325
1326
1327
	[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,
	},
Linus Torvalds's avatar
Linus Torvalds committed
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
};

/*
 * io_subchannel_irq is called for "real" interrupts or for status
 * pending conditions on msch.
 */
void
io_subchannel_irq (struct device *pdev)
{
	struct ccw_device *cdev;

	cdev = to_subchannel(pdev)->dev.driver_data;

	CIO_TRACE_EVENT (3, "IRQ");
	CIO_TRACE_EVENT (3, pdev->bus_id);
	if (cdev)
		dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
}

EXPORT_SYMBOL_GPL(ccw_device_set_timeout);