summaryrefslogtreecommitdiff
path: root/sys/dev/i2o/iopsp.c
blob: 3db66922fb6d502b6f3c4cbaa3dfe41c28592bc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*	$OpenBSD: iopsp.c,v 1.14 2009/02/16 21:19:06 miod Exp $	*/
/*	$NetBSD$	*/

/*-
 * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Doran.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Raw SCSI device support for I2O.  IOPs present SCSI devices individually;
 * we group them by controlling port.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/queue.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/endian.h>
#include <sys/malloc.h>
#include <sys/scsiio.h>
#include <sys/lock.h>

#include <machine/bus.h>

#include <scsi/scsi_all.h>
#include <scsi/scsi_disk.h>
#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <scsi/scsi_message.h>

#include <dev/i2o/i2o.h>
#include <dev/i2o/iopio.h>
#include <dev/i2o/iopvar.h>
#include <dev/i2o/iopspvar.h>

struct cfdriver iopsp_cd = {
	NULL, "iopsp", DV_DULL
};

int	iopsp_match(struct device *, void *, void *);
void	iopsp_attach(struct device *, struct device *, void *);

struct cfattach iopsp_ca = {
	sizeof(struct iopsp_softc), iopsp_match, iopsp_attach
};

int	iopsp_scsi_cmd(struct scsi_xfer *);
void	iopspminphys(struct buf *bp, struct scsi_link *sl);

struct scsi_adapter iopsp_switch = {
	iopsp_scsi_cmd, iopspminphys, 0, 0,
};

struct scsi_device iopsp_dev = {
	NULL, NULL, NULL, NULL
};

void	iopsp_adjqparam(struct device *, int);
void	iopsp_intr(struct device *, struct iop_msg *, void *);
int	iopsp_rescan(struct iopsp_softc *);
int	iopsp_reconfig(struct device *);

/*
 * Match a supported device.
 */
int
iopsp_match(struct device *parent, void *match, void *aux)
{
	struct iop_attach_args *ia = aux;
	struct {
		struct	i2o_param_op_results pr;
		struct	i2o_param_read_results prr;
		struct	i2o_param_hba_ctlr_info ci;
	} __packed param;
	int rv;

	if (ia->ia_class != I2O_CLASS_BUS_ADAPTER_PORT)
		return (0);

	if ((rv = iop_param_op((struct iop_softc *)parent, ia->ia_tid, NULL, 0,
	    I2O_PARAM_HBA_CTLR_INFO, &param, sizeof(param)))) {
#ifdef I2ODEBUG
		printf("iopsp_match: iop_param_op failed, status = %d\n", rv);
#endif
		return (0);
	}

#ifdef I2ODEBUG
	printf("iopsp_match: bustype = %d\n", param.ci.bustype);
#endif

	return (param.ci.bustype == I2O_HBA_BUS_SCSI ||
	    param.ci.bustype == I2O_HBA_BUS_FCA);
}

/*
 * Attach a supported device.
 */
void
iopsp_attach(struct device *parent, struct device *self, void *aux)
{
	struct iop_softc *iop = (struct iop_softc *)parent;
	struct iopsp_softc *sc = (struct iopsp_softc *)self;
	struct iop_attach_args *ia = (struct iop_attach_args *)aux;
	struct scsibus_attach_args saa;
	struct {
		struct	i2o_param_op_results pr;
		struct	i2o_param_read_results prr;
		union {
			struct	i2o_param_hba_ctlr_info ci;
			struct	i2o_param_hba_scsi_ctlr_info sci;
			struct	i2o_param_hba_scsi_port_info spi;
		} p;
	} __packed param;
	int fcal, rv;
#ifdef I2OVERBOSE
	int size;
#endif

	/* Register us as an initiator. */
	sc->sc_ii.ii_dv = self;
	sc->sc_ii.ii_intr = iopsp_intr;
	sc->sc_ii.ii_flags = 0;
	sc->sc_ii.ii_tid = ia->ia_tid;
	sc->sc_ii.ii_reconfig = iopsp_reconfig;
	sc->sc_ii.ii_adjqparam = iopsp_adjqparam;
	iop_initiator_register(iop, &sc->sc_ii);

	rv = iop_param_op(iop, ia->ia_tid, NULL, 0, I2O_PARAM_HBA_CTLR_INFO,
	    &param, sizeof(param));
	if (rv != 0) {
		printf("%s: unable to get parameters (0x%04x; %d)\n",
	    	    sc->sc_dv.dv_xname, I2O_PARAM_HBA_CTLR_INFO, rv);
		goto bad;
	}

	fcal = (param.p.ci.bustype == I2O_HBA_BUS_FCA);		/* XXX */

	/* 
	 * Say what the device is.  If we can find out what the controling
	 * device is, say what that is too.
	 */
	printf(": SCSI port");
	iop_print_ident(iop, ia->ia_tid);
	printf("\n");

	rv = iop_param_op(iop, ia->ia_tid, NULL, 0,
	    I2O_PARAM_HBA_SCSI_CTLR_INFO, &param, sizeof(param));
	if (rv != 0) {
		printf("%s: unable to get parameters (0x%04x; %d)\n",
		    sc->sc_dv.dv_xname, I2O_PARAM_HBA_SCSI_CTLR_INFO, rv);
		goto bad;
	}

#ifdef I2OVERBOSE
	printf("%s: %d-bit, max sync rate %dMHz, initiator ID %d\n",
	    sc->sc_dv.dv_xname, param.p.sci.maxdatawidth,
	    (u_int32_t)letoh64(param.p.sci.maxsyncrate) / 1000,
	    letoh32(param.p.sci.initiatorid));
#endif

	sc->sc_link.adapter_softc = sc;
	sc->sc_link.adapter = &iopsp_switch;
	sc->sc_link.adapter_target = letoh32(param.p.sci.initiatorid);
	sc->sc_link.device = &iopsp_dev;
	sc->sc_link.openings = 1;
	sc->sc_link.adapter_buswidth = fcal?
	    IOPSP_MAX_FCAL_TARGET : param.p.sci.maxdatawidth;
	sc->sc_link.luns = IOPSP_MAX_LUN;

#ifdef I2OVERBOSE
	/*
	 * Allocate the target map.  Currently used for informational
	 * purposes only.
	 */
	size = sc->sc_link.adapter_buswidth * sizeof(struct iopsp_target);
	sc->sc_targetmap = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
#endif

 	/* Build the two maps, and attach to scsi. */
	if (iopsp_reconfig(self) != 0) {
		printf("%s: configure failed\n", sc->sc_dv.dv_xname);
		goto bad;
	}

	bzero(&saa, sizeof(saa));
	saa.saa_sc_link = &sc->sc_link;

	config_found(&sc->sc_dv, &saa, scsiprint);
	return;

 bad:
	iop_initiator_unregister(iop, &sc->sc_ii);
}

/*
 * Scan the LCT to determine which devices we control, and enter them into
 * the maps.
 */
int
iopsp_reconfig(struct device *dv)
{
	struct iopsp_softc *sc = (struct iopsp_softc *)dv;
	struct iop_softc *iop = (struct iop_softc *)sc->sc_dv.dv_parent;
	struct i2o_lct_entry *le;
	struct {
		struct	i2o_param_op_results pr;
		struct	i2o_param_read_results prr;
		struct	i2o_param_scsi_device_info sdi;
	} __packed param;
	u_int tid, nent, i, targ, lun, size, s, rv, bptid;
	u_short *tidmap;
#ifdef I2OVERBOSE
	struct iopsp_target *it;
	int syncrate;	
#endif

	/* Anything to do? */
	if (iop->sc_chgind == sc->sc_chgind)
		return (0);

	/*
	 * Allocate memory for the target/LUN -> TID map.  Use zero to
	 * denote absent targets (zero is the TID of the I2O executive,
	 * and we never address that here).
	 */
	size = sc->sc_link.adapter_buswidth * IOPSP_MAX_LUN * sizeof(u_short);
	if (!(tidmap = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO)))
		return (ENOMEM);

#ifdef I2OVERBOSE
	for (i = 0; i < sc->sc_link.adapter_buswidth; i++)
		sc->sc_targetmap[i].it_flags &= ~IT_PRESENT;
#endif

	/*
	 * A quick hack to handle Intel's stacked bus port arrangement.
	 */
	bptid = sc->sc_ii.ii_tid;
	nent = iop->sc_nlctent;
	for (le = iop->sc_lct->entry; nent != 0; nent--, le++)
		if ((letoh16(le->classid) & 4095) ==
		    I2O_CLASS_BUS_ADAPTER_PORT &&
		    (letoh32(le->usertid) & 4095) == bptid) {
			bptid = letoh16(le->localtid) & 4095;
			break;
		}

	nent = iop->sc_nlctent;
	for (i = 0, le = iop->sc_lct->entry; i < nent; i++, le++) {
		if ((letoh16(le->classid) & I2O_CLASS_MASK) !=
		    I2O_CLASS_SCSI_PERIPHERAL ||
		    ((letoh32(le->usertid) >> 12) & 4095) != bptid)
			continue;
		tid = letoh16(le->localtid) & I2O_LCT_ENTRY_TID_MASK;

		rv = iop_param_op(iop, tid, NULL, 0, I2O_PARAM_SCSI_DEVICE_INFO,
		    &param, sizeof(param));
		if (rv != 0) {
			printf("%s: unable to get parameters (0x%04x; %d)\n",
			    sc->sc_dv.dv_xname, I2O_PARAM_SCSI_DEVICE_INFO,
			    rv);
			continue;
		}
		targ = letoh32(param.sdi.identifier);
		lun = param.sdi.luninfo[1];
#if defined(DIAGNOSTIC) || defined(I2ODEBUG)
		if (targ >= sc->sc_link.adapter_buswidth ||
		    lun >= sc->sc_link.adapter_buswidth) {
			printf("%s: target %d,%d (tid %d): bad target/LUN\n",
			    sc->sc_dv.dv_xname, targ, lun, tid);
			continue;
		}
#endif

#ifdef I2OVERBOSE
		/*
		 * If we've already described this target, and nothing has
		 * changed, then don't describe it again.
		 */
		it = &sc->sc_targetmap[targ];
		it->it_flags |= IT_PRESENT;
		syncrate = (int)((letoh64(param.sdi.negsyncrate) + 500) / 1000);
		if (it->it_width == param.sdi.negdatawidth &&
		    it->it_offset == param.sdi.negoffset &&
		    it->it_syncrate == syncrate)
			continue;

		it->it_width = param.sdi.negdatawidth;
		it->it_offset = param.sdi.negoffset;
		it->it_syncrate = syncrate;

		printf("%s: target %d (tid %d): %d-bit, ", sc->sc_dv.dv_xname,
		    targ, tid, it->it_width);
		if (it->it_syncrate == 0)
			printf("asynchronous\n");
		else
			printf("synchronous at %dMHz, offset 0x%x\n",
			    it->it_syncrate, it->it_offset);
#endif

		/* Ignore the device if it's in use by somebody else. */
		if ((letoh32(le->usertid) & 4095) != I2O_TID_NONE) {
#ifdef I2OVERBOSE
			if (sc->sc_tidmap == NULL ||
			    IOPSP_TIDMAP(sc->sc_tidmap, targ, lun) !=
			    IOPSP_TID_INUSE)
				printf("%s: target %d,%d (tid %d): in use by"
				    " tid %d\n", sc->sc_dv.dv_xname,
				    targ, lun, tid,
				    letoh32(le->usertid) & 4095);
#endif
			IOPSP_TIDMAP(tidmap, targ, lun) = IOPSP_TID_INUSE;
		} else
			IOPSP_TIDMAP(tidmap, targ, lun) = (u_short)tid;
	}

#ifdef I2OVERBOSE
	for (i = 0; i < sc->sc_link.adapter_buswidth; i++)
		if ((sc->sc_targetmap[i].it_flags & IT_PRESENT) == 0)
			sc->sc_targetmap[i].it_width = 0;
#endif

	/* Swap in the new map and return. */
	s = splbio();
	if (sc->sc_tidmap != NULL)
		free(sc->sc_tidmap, M_DEVBUF);
	sc->sc_tidmap = tidmap;
	splx(s);
	sc->sc_chgind = iop->sc_chgind;
	return (0);
}

/*
 * Re-scan the bus; to be called from a higher level (e.g. scsi).
 */
int
iopsp_rescan(struct iopsp_softc *sc)
{
	struct iop_softc *iop;
	struct iop_msg *im;
	struct i2o_hba_bus_scan mf;
	int rv;

	iop = (struct iop_softc *)sc->sc_dv.dv_parent;

	rv = lockmgr(&iop->sc_conflock, LK_EXCLUSIVE, NULL);
	if (rv != 0) {
#ifdef I2ODEBUG
		printf("iopsp_rescan: unable to acquire lock\n");
#endif
		return (rv);
	}

	im = iop_msg_alloc(iop, &sc->sc_ii, IM_WAIT);

	mf.msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
	mf.msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid, I2O_HBA_BUS_SCAN);
	mf.msgictx = sc->sc_ii.ii_ictx;
	mf.msgtctx = im->im_tctx;

	rv = iop_msg_post(iop, im, &mf, 5*60*1000);
	iop_msg_free(iop, im);
	if (rv != 0)
		printf("%s: bus rescan failed (error %d)\n",
		    sc->sc_dv.dv_xname, rv);

	if ((rv = iop_lct_get(iop)) == 0)
		rv = iopsp_reconfig(&sc->sc_dv);

	lockmgr(&iop->sc_conflock, LK_RELEASE, NULL);
	return (rv);
}

void
iopspminphys(struct buf *bp, struct scsi_link *sl)
{
	if (bp->b_bcount > IOP_MAX_XFER)
		bp->b_bcount = IOP_MAX_XFER;
	minphys(bp);
}

/*
 * Start a SCSI command.
 */
int
iopsp_scsi_cmd(xs)
	struct scsi_xfer *xs;
{
	struct scsi_link *link = xs->sc_link;
	struct iopsp_softc *sc = (struct iopsp_softc *)link->adapter_softc;
	struct iop_softc *iop = (struct iop_softc *)sc->sc_dv.dv_parent;
	struct iop_msg *im;
	struct i2o_scsi_scb_exec *mf;
	int error, tid, s;
	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];

	tid = IOPSP_TIDMAP(sc->sc_tidmap, link->target, link->lun);
	if (tid == IOPSP_TID_ABSENT || tid == IOPSP_TID_INUSE) {
		xs->error = XS_SELTIMEOUT;
		s = splbio();
		scsi_done(xs);
		splx(s);
		return (COMPLETE);
	}

	SC_DEBUG(xs->sc_link, SDEV_DB2, ("iopsp_scsi_cmd: run_xfer\n"));

	/* Need to reset the target? */
	if ((xs->flags & XS_RESET) != 0) {
		if (iop_simple_cmd(iop, tid, I2O_SCSI_DEVICE_RESET,
		    sc->sc_ii.ii_ictx, 1, 30*1000) != 0) {
#ifdef I2ODEBUG
			printf("%s: reset failed\n",
			    sc->sc_dv.dv_xname);
#endif
			xs->error = XS_DRIVER_STUFFUP;
		} else
			xs->error = XS_NOERROR;

		s = splbio();
		scsi_done(xs);
		splx(s);
		return (COMPLETE);
	}

#if defined(I2ODEBUG) || defined(SCSIDEBUG)
	if (xs->cmdlen > sizeof(mf->cdb))
		panic("%s: CDB too large", sc->sc_dv.dv_xname);
#endif

	im = iop_msg_alloc(iop, &sc->sc_ii, IM_POLL_INTR |
	    IM_NOSTATUS | ((xs->flags & SCSI_POLL) != 0 ? IM_POLL : 0));
	im->im_dvcontext = xs;

	mf = (struct i2o_scsi_scb_exec *)mb;
	mf->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_exec);
	mf->msgfunc = I2O_MSGFUNC(tid, I2O_SCSI_SCB_EXEC);
	mf->msgictx = sc->sc_ii.ii_ictx;
	mf->msgtctx = im->im_tctx;
	mf->flags = xs->cmdlen | I2O_SCB_FLAG_ENABLE_DISCONNECT |
	    I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
	mf->datalen = xs->datalen;
	memcpy(mf->cdb, xs->cmd, xs->cmdlen);

#if 0
	switch (xs->xs_tag_type) {
	case MSG_ORDERED_Q_TAG:
		mf->flags |= I2O_SCB_FLAG_ORDERED_QUEUE_TAG;
		break;
	case MSG_SIMPLE_Q_TAG:
		mf->flags |= I2O_SCB_FLAG_SIMPLE_QUEUE_TAG;
		break;
	case MSG_HEAD_OF_Q_TAG:
		mf->flags |= I2O_SCB_FLAG_HEAD_QUEUE_TAG;
		break;
	default:
		break;
	}
#endif

	if (xs->datalen != 0) {
		error = iop_msg_map_bio(iop, im, mb, xs->data,
		    xs->datalen, (xs->flags & SCSI_DATA_OUT) == 0);
		if (error) {
			xs->error = XS_DRIVER_STUFFUP;
			iop_msg_free(iop, im);
			s = splbio();
			scsi_done(xs);
			splx(s);
			return (COMPLETE);
		}
		if ((xs->flags & SCSI_DATA_IN) == 0)
			mf->flags |= I2O_SCB_FLAG_XFER_TO_DEVICE;
		else
			mf->flags |= I2O_SCB_FLAG_XFER_FROM_DEVICE;
	}

	s = splbio();
	sc->sc_curqd++;
	splx(s);

	if (iop_msg_post(iop, im, mb, xs->timeout)) {
		s = splbio();
		sc->sc_curqd--;
		splx(s);
		if (xs->datalen != 0)
			iop_msg_unmap(iop, im);
		iop_msg_free(iop, im);
		xs->error = XS_DRIVER_STUFFUP;
		s = splbio();
		scsi_done(xs);
		splx(s);
		return (COMPLETE);
	}

	return (xs->flags & SCSI_POLL? COMPLETE : SUCCESSFULLY_QUEUED);
}

#ifdef notyet
/*
 * Abort the specified I2O_SCSI_SCB_EXEC message and its associated SCB.
 */
int
iopsp_scsi_abort(struct iopsp_softc *sc, int atid, struct iop_msg *aim)
{
	struct iop_msg *im;
	struct i2o_scsi_scb_abort mf;
	struct iop_softc *iop;
	int rv, s;

	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
	im = iop_msg_alloc(iop, &sc->sc_ii, IM_POLL);

	mf.msgflags = I2O_MSGFLAGS(i2o_scsi_scb_abort);
	mf.msgfunc = I2O_MSGFUNC(atid, I2O_SCSI_SCB_ABORT);
	mf.msgictx = sc->sc_ii.ii_ictx;
	mf.msgtctx = im->im_tctx;
	mf.tctxabort = aim->im_tctx;

	s = splbio();
	rv = iop_msg_post(iop, im, &mf, 30000);
	splx(s);
	iop_msg_free(iop, im);
	return (rv);
}
#endif

/*
 * We have a message which has been processed and replied to by the IOP -
 * deal with it.
 */
void
iopsp_intr(struct device *dv, struct iop_msg *im, void *reply)
{
	struct scsi_xfer *xs;
	struct iopsp_softc *sc;
	struct i2o_scsi_reply *rb;
 	struct iop_softc *iop;
	u_int sl;

	sc = (struct iopsp_softc *)dv;
	xs = (struct scsi_xfer *)im->im_dvcontext;
	iop = (struct iop_softc *)dv->dv_parent;
	rb = reply;

	SC_DEBUG(xs->sc_link, SDEV_DB2, ("iopsp_intr\n"));

	if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0) {
		xs->error = XS_DRIVER_STUFFUP;
		xs->resid = xs->datalen;
	} else {
		if (rb->hbastatus != I2O_SCSI_DSC_SUCCESS) {
			switch (rb->hbastatus) {
			case I2O_SCSI_DSC_ADAPTER_BUSY:
			case I2O_SCSI_DSC_SCSI_BUS_RESET:
			case I2O_SCSI_DSC_BUS_BUSY:
				xs->error = XS_BUSY;
				break;
			case I2O_SCSI_DSC_SELECTION_TIMEOUT:
				xs->error = XS_SELTIMEOUT;
				break;
			case I2O_SCSI_DSC_COMMAND_TIMEOUT:
			case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
			case I2O_SCSI_DSC_LUN_INVALID:
			case I2O_SCSI_DSC_SCSI_TID_INVALID:
				xs->error = XS_TIMEOUT;
				break;
			default:
				xs->error = XS_DRIVER_STUFFUP;
				break;
			}
			printf("%s: HBA status 0x%02x\n", sc->sc_dv.dv_xname,
			   rb->hbastatus);
		} else if (rb->scsistatus != SCSI_OK) {
			switch (rb->scsistatus) {
			case SCSI_CHECK:
				xs->error = XS_SENSE;
				sl = letoh32(rb->senselen);
				if (sl > sizeof(xs->sense))
					sl = sizeof(xs->sense);
				bcopy(rb->sense, &xs->sense, sl);
				break;
			case SCSI_QUEUE_FULL:
			case SCSI_BUSY:
				xs->error = XS_BUSY;
				break;
			default:
				xs->error = XS_DRIVER_STUFFUP;
				break;
			}
		} else
			xs->error = XS_NOERROR;

		xs->resid = xs->datalen - letoh32(rb->datalen);
		xs->status = rb->scsistatus;
	}

	/* Free the message wrapper and pass the news to scsi. */
	if (xs->datalen != 0)
		iop_msg_unmap(iop, im);
	iop_msg_free(iop, im);

	if (--sc->sc_curqd == sc->sc_link.openings)
		wakeup(&sc->sc_curqd);

	scsi_done(xs);
}

/*
 * The number of openings available to us has changed, so inform scsi.
 */
void
iopsp_adjqparam(struct device *dv, int mpi)
{
	struct iopsp_softc *sc = (struct iopsp_softc *)dv;
	int s;

	s = splbio();
	sc->sc_link.openings = mpi;
	if (mpi < sc->sc_curqd)
		tsleep(&sc->sc_curqd, PWAIT, "iopspdrn", 0);
	splx(s);
}