Home | History | Annotate | Line # | Download | only in i2o
iopsp.c revision 1.5
      1 /*	$NetBSD: iopsp.c,v 1.5 2001/03/20 13:01:49 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Raw SCSI device support for I2O.  IOPs present SCSI devices individually;
     41  * we group them by controlling port.
     42  */
     43 
     44 #include "opt_i2o.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/device.h>
     50 #include <sys/queue.h>
     51 #include <sys/proc.h>
     52 #include <sys/buf.h>
     53 #include <sys/endian.h>
     54 #include <sys/malloc.h>
     55 #include <sys/scsiio.h>
     56 #include <sys/lock.h>
     57 
     58 #include <machine/bswap.h>
     59 #include <machine/bus.h>
     60 
     61 #include <dev/scsipi/scsi_all.h>
     62 #include <dev/scsipi/scsi_disk.h>
     63 #include <dev/scsipi/scsipi_all.h>
     64 #include <dev/scsipi/scsiconf.h>
     65 
     66 #include <dev/i2o/i2o.h>
     67 #include <dev/i2o/iopio.h>
     68 #include <dev/i2o/iopvar.h>
     69 #include <dev/i2o/iopspvar.h>
     70 
     71 static void	iopsp_adjqparam(struct device *, int);
     72 static void	iopsp_attach(struct device *, struct device *, void *);
     73 static void	iopsp_intr(struct device *, struct iop_msg *, void *);
     74 static int	iopsp_ioctl(struct scsipi_link *, u_long, caddr_t, int,
     75 			    struct proc *);
     76 static int	iopsp_match(struct device *, struct cfdata *, void *);
     77 static void	iopsp_minphys(struct buf *);
     78 static int	iopsp_rescan(struct iopsp_softc *);
     79 static int	iopsp_reconfig(struct device *);
     80 static int	iopsp_scsi_abort(struct iopsp_softc *, int, struct iop_msg *);
     81 static int	iopsp_scsi_cmd(struct scsipi_xfer *);
     82 
     83 static struct scsipi_device iopsp_dev = {
     84 	NULL,			/* Use default error handler */
     85 	NULL,			/* have a queue, served by this */
     86 	NULL,			/* have no async handler */
     87 	NULL,			/* Use default 'done' routine */
     88 };
     89 
     90 struct cfattach iopsp_ca = {
     91 	sizeof(struct iopsp_softc), iopsp_match, iopsp_attach
     92 };
     93 
     94 /*
     95  * Match a supported device.
     96  */
     97 static int
     98 iopsp_match(struct device *parent, struct cfdata *match, void *aux)
     99 {
    100 	struct iop_attach_args *ia;
    101 	struct {
    102 		struct	i2o_param_op_results pr;
    103 		struct	i2o_param_read_results prr;
    104 		struct	i2o_param_hba_ctlr_info ci;
    105 	} __attribute__ ((__packed__)) param;
    106 
    107 	ia = aux;
    108 
    109 	if (ia->ia_class != I2O_CLASS_BUS_ADAPTER_PORT)
    110 		return (0);
    111 
    112 	if (iop_param_op((struct iop_softc *)parent, ia->ia_tid, NULL, 0,
    113 	    I2O_PARAM_HBA_CTLR_INFO, &param, sizeof(param)) != 0)
    114 		return (0);
    115 
    116 	return (param.ci.bustype == I2O_HBA_BUS_SCSI ||
    117 	    param.ci.bustype == I2O_HBA_BUS_FCA);
    118 }
    119 
    120 /*
    121  * Attach a supported device.
    122  */
    123 static void
    124 iopsp_attach(struct device *parent, struct device *self, void *aux)
    125 {
    126 	struct iop_attach_args *ia;
    127 	struct iopsp_softc *sc;
    128 	struct scsipi_link *sc_link;
    129 	struct iop_softc *iop;
    130 	struct {
    131 		struct	i2o_param_op_results pr;
    132 		struct	i2o_param_read_results prr;
    133 		union {
    134 			struct	i2o_param_hba_ctlr_info ci;
    135 			struct	i2o_param_hba_scsi_ctlr_info sci;
    136 			struct	i2o_param_hba_scsi_port_info spi;
    137 		} p;
    138 	} __attribute__ ((__packed__)) param;
    139 	int fcal, rv;
    140 #ifdef I2OVERBOSE
    141 	int size;
    142 #endif
    143 
    144 	ia = (struct iop_attach_args *)aux;
    145 	sc = (struct iopsp_softc *)self;
    146 	iop = (struct iop_softc *)parent;
    147 
    148 	/* Register us as an initiator. */
    149 	sc->sc_ii.ii_dv = self;
    150 	sc->sc_ii.ii_intr = iopsp_intr;
    151 	sc->sc_ii.ii_flags = 0;
    152 	sc->sc_ii.ii_tid = ia->ia_tid;
    153 	sc->sc_ii.ii_reconfig = iopsp_reconfig;
    154 	sc->sc_ii.ii_adjqparam = iopsp_adjqparam;
    155 	iop_initiator_register(iop, &sc->sc_ii);
    156 
    157 	rv = iop_param_op(iop, ia->ia_tid, NULL, 0, I2O_PARAM_HBA_CTLR_INFO,
    158 	    &param, sizeof(param));
    159 	if (rv != 0) {
    160 		printf("%s: unable to get parameters (0x%04x; %d)\n",
    161 	    	    sc->sc_dv.dv_xname, I2O_PARAM_HBA_CTLR_INFO, rv);
    162 		goto bad;
    163 	}
    164 
    165 	fcal = (param.p.ci.bustype == I2O_HBA_BUS_FCA);		/* XXX */
    166 
    167 	/*
    168 	 * Say what the device is.  If we can find out what the controling
    169 	 * device is, say what that is too.
    170 	 */
    171 	printf(": SCSI port");
    172 	iop_print_ident(iop, ia->ia_tid);
    173 	printf("\n");
    174 
    175 	rv = iop_param_op(iop, ia->ia_tid, NULL, 0,
    176 	    I2O_PARAM_HBA_SCSI_CTLR_INFO, &param, sizeof(param));
    177 	if (rv != 0) {
    178 		printf("%s: unable to get parameters (0x%04x; %d)\n",
    179 		    sc->sc_dv.dv_xname, I2O_PARAM_HBA_SCSI_CTLR_INFO, rv);
    180 		goto bad;
    181 	}
    182 
    183 #ifdef I2OVERBOSE
    184 	printf("%s: %d-bit, max sync rate %dMHz, initiator ID %d\n",
    185 	    sc->sc_dv.dv_xname, param.p.sci.maxdatawidth,
    186 	    (u_int32_t)le64toh(param.p.sci.maxsyncrate) / 1000,
    187 	    le32toh(param.p.sci.initiatorid));
    188 #endif
    189 
    190 	sc->sc_adapter.scsipi_cmd = iopsp_scsi_cmd;
    191 	sc->sc_adapter.scsipi_minphys = iopsp_minphys;
    192 	sc->sc_adapter.scsipi_ioctl = iopsp_ioctl;
    193 
    194 	sc_link = &sc->sc_link;
    195 	sc_link->type = BUS_SCSI;
    196 	sc_link->device = &iopsp_dev;
    197 	sc_link->adapter = &sc->sc_adapter;
    198 	sc_link->adapter_softc = sc;
    199 	sc_link->scsipi_scsi.channel = 0;
    200 	sc_link->scsipi_scsi.adapter_target = le32toh(param.p.sci.initiatorid);
    201 	sc_link->scsipi_scsi.max_target =
    202 	    fcal ? IOPSP_MAX_FCAL_TARGET : param.p.sci.maxdatawidth - 1;
    203 	sc_link->scsipi_scsi.max_lun = IOPSP_MAX_LUN;
    204 	sc_link->openings = 1;
    205 
    206 #ifdef I2OVERBOSE
    207 	/*
    208 	 * Allocate the target map.  Currently used for informational
    209 	 * purposes only.
    210 	 */
    211 	size = (sc_link->scsipi_scsi.max_target + 1) *
    212 	    sizeof(struct iopsp_target);
    213 	sc->sc_targetmap = malloc(size, M_DEVBUF, M_NOWAIT);
    214 	memset(sc->sc_targetmap, 0, size);
    215 #endif
    216 
    217  	/* Build the two maps, and attach to scsipi. */
    218 	if (iopsp_reconfig(self) != 0) {
    219 		printf("%s: configure failed\n", sc->sc_dv.dv_xname);
    220 		goto bad;
    221 	}
    222 	config_found(self, sc_link, scsiprint);
    223 	return;
    224 
    225  bad:
    226 	iop_initiator_unregister(iop, &sc->sc_ii);
    227 }
    228 
    229 /*
    230  * Scan the LCT to determine which devices we control, and enter them into
    231  * the maps.
    232  */
    233 static int
    234 iopsp_reconfig(struct device *dv)
    235 {
    236 	struct iopsp_softc *sc;
    237 	struct iop_softc *iop;
    238 	struct i2o_lct_entry *le;
    239 	struct scsipi_link *sc_link;
    240 	struct {
    241 		struct	i2o_param_op_results pr;
    242 		struct	i2o_param_read_results prr;
    243 		struct	i2o_param_scsi_device_info sdi;
    244 	} __attribute__ ((__packed__)) param;
    245 	u_int tid, nent, i, targ, lun, size, s, rv, bptid;
    246 	u_short *tidmap;
    247 #ifdef I2OVERBOSE
    248 	struct iopsp_target *it;
    249 	int syncrate;
    250 #endif
    251 
    252 	sc = (struct iopsp_softc *)dv;
    253 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    254 	sc_link = &sc->sc_link;
    255 
    256 	/* Anything to do? */
    257 	if (iop->sc_chgind == sc->sc_chgind)
    258 		return (0);
    259 
    260 	/*
    261 	 * Allocate memory for the target/LUN -> TID map.  Use zero to
    262 	 * denote absent targets (zero is the TID of the I2O executive,
    263 	 * and we never address that here).
    264 	 */
    265 	size = (sc_link->scsipi_scsi.max_target + 1) *
    266 	    (IOPSP_MAX_LUN + 1) * sizeof(u_short);
    267 	if ((tidmap = malloc(size, M_DEVBUF, M_WAITOK)) == NULL)
    268 		return (ENOMEM);
    269 	memset(tidmap, 0, size);
    270 
    271 #ifdef I2OVERBOSE
    272 	for (i = 0; i <= sc_link->scsipi_scsi.max_target; i++)
    273 		sc->sc_targetmap[i].it_flags &= ~IT_PRESENT;
    274 #endif
    275 
    276 	/*
    277 	 * A quick hack to handle Intel's stacked bus port arrangement.
    278 	 */
    279 	bptid = sc->sc_ii.ii_tid;
    280 	nent = iop->sc_nlctent;
    281 	for (le = iop->sc_lct->entry; nent != 0; nent--, le++)
    282 		if ((le16toh(le->classid) & 4095) ==
    283 		    I2O_CLASS_BUS_ADAPTER_PORT &&
    284 		    (le32toh(le->usertid) & 4095) == bptid) {
    285 			bptid = le32toh(le->localtid) & 4095;
    286 			break;
    287 		}
    288 
    289 	nent = iop->sc_nlctent;
    290 	for (i = 0, le = iop->sc_lct->entry; i < nent; i++, le++) {
    291 		if ((le16toh(le->classid) & 4095) != I2O_CLASS_SCSI_PERIPHERAL)
    292 			continue;
    293 		if (((le32toh(le->usertid) >> 12) & 4095) != bptid)
    294 			continue;
    295 		tid = le32toh(le->localtid) & 4095;
    296 
    297 		rv = iop_param_op(iop, tid, NULL, 0, I2O_PARAM_SCSI_DEVICE_INFO,
    298 		    &param, sizeof(param));
    299 		if (rv != 0) {
    300 			printf("%s: unable to get parameters (0x%04x; %d)\n",
    301 			    sc->sc_dv.dv_xname, I2O_PARAM_SCSI_DEVICE_INFO,
    302 			    rv);
    303 			continue;
    304 		}
    305 		targ = le32toh(param.sdi.identifier);
    306 		lun = param.sdi.luninfo[1];
    307 #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
    308 		if (targ > sc_link->scsipi_scsi.max_target ||
    309 		    lun > sc_link->scsipi_scsi.max_lun) {
    310 			printf("%s: target %d,%d (tid %d): bad target/LUN\n",
    311 			    sc->sc_dv.dv_xname, targ, lun, tid);
    312 			continue;
    313 		}
    314 #endif
    315 
    316 #ifdef I2OVERBOSE
    317 		/*
    318 		 * If we've already described this target, and nothing has
    319 		 * changed, then don't describe it again.
    320 		 */
    321 		it = &sc->sc_targetmap[targ];
    322 		it->it_flags |= IT_PRESENT;
    323 		syncrate = ((int)le64toh(param.sdi.negsyncrate) + 500) / 1000;
    324 		if (it->it_width == param.sdi.negdatawidth &&
    325 		    it->it_offset == param.sdi.negoffset &&
    326 		    it->it_syncrate == syncrate)
    327 			continue;
    328 
    329 		it->it_width = param.sdi.negdatawidth;
    330 		it->it_offset = param.sdi.negoffset;
    331 		it->it_syncrate = syncrate;
    332 
    333 		printf("%s: target %d (tid %d): %d-bit, ", sc->sc_dv.dv_xname,
    334 		    targ, tid, it->it_width);
    335 		if (it->it_syncrate == 0)
    336 			printf("asynchronous\n");
    337 		else
    338 			printf("synchronous at %dMHz, offset 0x%x\n",
    339 			    it->it_syncrate, it->it_offset);
    340 #endif
    341 
    342 		/* Ignore the device if it's in use by somebody else. */
    343 		if ((le32toh(le->usertid) & 4095) != I2O_TID_NONE) {
    344 #ifdef I2OVERBOSE
    345 			if (sc->sc_tidmap == NULL ||
    346 			    IOPSP_TIDMAP(sc->sc_tidmap, targ, lun) !=
    347 			    IOPSP_TID_INUSE)
    348 				printf("%s: target %d,%d (tid %d): in use by"
    349 				    " tid %d\n", sc->sc_dv.dv_xname,
    350 				    targ, lun, tid,
    351 				    le32toh(le->usertid) & 4095);
    352 #endif
    353 			IOPSP_TIDMAP(tidmap, targ, lun) = IOPSP_TID_INUSE;
    354 		} else
    355 			IOPSP_TIDMAP(tidmap, targ, lun) = (u_short)tid;
    356 	}
    357 
    358 #ifdef I2OVERBOSE
    359 	for (i = 0; i <= sc_link->scsipi_scsi.max_target; i++)
    360 		if ((sc->sc_targetmap[i].it_flags & IT_PRESENT) == 0)
    361 			sc->sc_targetmap[i].it_width = 0;
    362 #endif
    363 
    364 	/* Swap in the new map and return. */
    365 	s = splbio();
    366 	if (sc->sc_tidmap != NULL)
    367 		free(sc->sc_tidmap, M_DEVBUF);
    368 	sc->sc_tidmap = tidmap;
    369 	splx(s);
    370 	sc->sc_chgind = iop->sc_chgind;
    371 	return (0);
    372 }
    373 
    374 /*
    375  * Adjust the size of an I/O request.
    376  */
    377 static void
    378 iopsp_minphys(struct buf *bp)
    379 {
    380 
    381 	if (bp->b_bcount > IOP_MAX_XFER)
    382 		bp->b_bcount = IOP_MAX_XFER;
    383 	minphys(bp);
    384 }
    385 
    386 /*
    387  * Re-scan the bus; to be called from a higher level (e.g. scsipi).
    388  */
    389 static int
    390 iopsp_rescan(struct iopsp_softc *sc)
    391 {
    392 	struct iop_softc *iop;
    393 	struct iop_msg *im;
    394 	struct i2o_hba_bus_scan mf;
    395 	int rv;
    396 
    397 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    398 
    399 	rv = lockmgr(&iop->sc_conflock, LK_EXCLUSIVE, NULL);
    400 	if (rv != 0) {
    401 #ifdef I2ODEBUG
    402 		printf("iopsp_rescan: unable to acquire lock\n");
    403 #endif
    404 		return (rv);
    405 	}
    406 
    407 	im = iop_msg_alloc(iop, &sc->sc_ii, IM_WAIT);
    408 
    409 	mf.msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
    410 	mf.msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid, I2O_HBA_BUS_SCAN);
    411 	mf.msgictx = sc->sc_ii.ii_ictx;
    412 	mf.msgtctx = im->im_tctx;
    413 
    414 	rv = iop_msg_post(iop, im, &mf, 5*60*1000);
    415 	iop_msg_free(iop, im);
    416 	if (rv != 0)
    417 		printf("%s: bus rescan failed (error %d)\n",
    418 		    sc->sc_dv.dv_xname, rv);
    419 
    420 	if ((rv = iop_lct_get(iop)) != 0)
    421 		goto done;
    422 
    423 	/* Rebuild the target/LUN -> TID map, release lock, and return. */
    424 	rv = iopsp_reconfig(&sc->sc_dv);
    425  done:
    426 	lockmgr(&iop->sc_conflock, LK_RELEASE, NULL);
    427 	return (rv);
    428 }
    429 
    430 /*
    431  * Start a SCSI command.
    432  */
    433 static int
    434 iopsp_scsi_cmd(struct scsipi_xfer *xs)
    435 {
    436 	struct scsipi_link *sc_link;
    437 	struct iopsp_softc *sc;
    438 	struct iop_msg *im;
    439 	struct iop_softc *iop;
    440 	struct i2o_scsi_scb_exec *mf;
    441 	int error, flags, tid, imf;
    442 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    443 
    444 	sc_link = xs->sc_link;
    445 	flags = xs->xs_control;
    446 	sc = sc_link->adapter_softc;
    447 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    448 
    449 	tid = IOPSP_TIDMAP(sc->sc_tidmap, sc_link->scsipi_scsi.target,
    450 	    sc_link->scsipi_scsi.lun);
    451 	if (tid == IOPSP_TID_ABSENT || tid == IOPSP_TID_INUSE) {
    452 		xs->error = XS_SELTIMEOUT;
    453 		return (COMPLETE);
    454 	}
    455 
    456 	SC_DEBUG(sc_link, SDEV_DB2, ("iopsp_scsi_cmd\n"));
    457 
    458 	/* Need to reset the target? */
    459 	if ((flags & XS_CTL_RESET) != 0) {
    460 		if (iop_simple_cmd(iop, tid, I2O_SCSI_DEVICE_RESET,
    461 		    sc->sc_ii.ii_ictx, 1, 10*1000) != 0) {
    462 #ifdef I2ODEBUG
    463 			printf("%s: reset failed\n", sc->sc_dv.dv_xname);
    464 #endif
    465 			xs->error = XS_DRIVER_STUFFUP;
    466 		}
    467 		return (COMPLETE);
    468 	}
    469 
    470 #if defined(I2ODEBUG) || defined(SCSIDEBUG)
    471 	if (xs->cmdlen > sizeof(mf->cdb))
    472 		panic("%s: CDB too large\n", sc->sc_dv.dv_xname);
    473 #endif
    474 
    475 	imf = (flags & (XS_CTL_POLL | XS_CTL_NOSLEEP)) != 0 ? IM_POLL : 0;
    476 	im = iop_msg_alloc(iop, &sc->sc_ii, imf);
    477 	im->im_dvcontext = xs;
    478 
    479 	mf = (struct i2o_scsi_scb_exec *)mb;
    480 	mf->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_exec);
    481 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_SCSI_SCB_EXEC);
    482 	mf->msgictx = sc->sc_ii.ii_ictx;
    483 	mf->msgtctx = im->im_tctx;
    484 	mf->flags = xs->cmdlen | I2O_SCB_FLAG_ENABLE_DISCONNECT |
    485 	    I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
    486 	mf->datalen = xs->datalen;
    487 	memcpy(mf->cdb, xs->cmd, xs->cmdlen);
    488 
    489 	if ((xs->sc_link->quirks & SDEV_NOTAG) == 0 &&
    490 	    (xs->xs_control & XS_CTL_POLL) != 0) {
    491 		if (xs->bp != NULL && (xs->bp->b_flags & B_ASYNC) != 0)
    492 			mf->flags |= I2O_SCB_FLAG_ORDERED_QUEUE_TAG;
    493 		else
    494 			mf->flags |= I2O_SCB_FLAG_SIMPLE_QUEUE_TAG;
    495 	}
    496 
    497 	if (xs->datalen != 0) {
    498 		error = iop_msg_map_bio(iop, im, mb, xs->data, xs->datalen,
    499 		    (flags & XS_CTL_DATA_OUT) != 0);
    500 		if (error) {
    501 #ifdef I2ODEBUG
    502 			printf("%s: error %d mapping xfer\n",
    503 			    sc->sc_dv.dv_xname, error);
    504 #endif
    505 			xs->error = XS_DRIVER_STUFFUP;
    506 			iop_msg_free(iop, im);
    507 			return (COMPLETE);
    508 		}
    509 		if ((flags & XS_CTL_DATA_IN) == 0)
    510 			mf->flags |= I2O_SCB_FLAG_XFER_TO_DEVICE;
    511 		else
    512 			mf->flags |= I2O_SCB_FLAG_XFER_FROM_DEVICE;
    513 	}
    514 
    515 	if ((flags & XS_CTL_POLL) == 0) {
    516 		if (iop_msg_post(iop, im, mb, 0)) {
    517 			if (xs->datalen != 0)
    518 				iop_msg_unmap(iop, im);
    519 			iop_msg_free(iop, im);
    520 			xs->error = XS_DRIVER_STUFFUP;
    521 			return (COMPLETE);
    522 		}
    523 		return (SUCCESSFULLY_QUEUED);
    524 	}
    525 
    526 	if (iop_msg_post(iop, im, mb, xs->timeout)) {
    527 		scsi_print_addr(xs->sc_link);
    528 		printf("timeout; aborting command\n");
    529 		if (iopsp_scsi_abort(sc, tid, im)) {
    530 			scsi_print_addr(xs->sc_link);
    531 			printf("abort failed\n");
    532 		}
    533 		xs->error = XS_DRIVER_STUFFUP;
    534 	}
    535 
    536 	return (COMPLETE);
    537 }
    538 
    539 /*
    540  * Abort the specified I2O_SCSI_SCB_EXEC message and its associated SCB.
    541  */
    542 static int
    543 iopsp_scsi_abort(struct iopsp_softc *sc, int atid, struct iop_msg *aim)
    544 {
    545 	struct iop_msg *im;
    546 	struct i2o_scsi_scb_abort mf;
    547 	struct iop_softc *iop;
    548 	int rv, s;
    549 
    550 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    551 	im = iop_msg_alloc(iop, &sc->sc_ii, IM_POLL);
    552 
    553 	mf.msgflags = I2O_MSGFLAGS(i2o_scsi_scb_abort);
    554 	mf.msgfunc = I2O_MSGFUNC(atid, I2O_SCSI_SCB_ABORT);
    555 	mf.msgictx = sc->sc_ii.ii_ictx;
    556 	mf.msgtctx = im->im_tctx;
    557 	mf.tctxabort = aim->im_tctx;
    558 
    559 	s = splbio();
    560 	rv = iop_msg_post(iop, im, &mf, 30000);
    561 	splx(s);
    562 	iop_msg_free(iop, im);
    563 	return (rv);
    564 }
    565 
    566 /*
    567  * We have a message which has been processed and replied to by the IOP -
    568  * deal with it.
    569  */
    570 static void
    571 iopsp_intr(struct device *dv, struct iop_msg *im, void *reply)
    572 {
    573 	struct scsipi_xfer *xs;
    574 	struct iopsp_softc *sc;
    575 	struct i2o_scsi_reply *rb;
    576  	struct iop_softc *iop;
    577 	u_int sl;
    578 
    579 	sc = (struct iopsp_softc *)dv;
    580 	xs = (struct scsipi_xfer *)im->im_dvcontext;
    581 	iop = (struct iop_softc *)dv->dv_parent;
    582 
    583 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("iopsp_intr\n"));
    584 
    585 	if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0) {
    586 		xs->error = XS_DRIVER_STUFFUP;
    587 		xs->resid = xs->datalen;
    588 	} else {
    589 		rb = reply;
    590 
    591 		if (rb->hbastatus != I2O_SCSI_DSC_SUCCESS) {
    592 			switch (rb->hbastatus) {
    593 			case I2O_SCSI_DSC_ADAPTER_BUSY:
    594 			case I2O_SCSI_DSC_SCSI_BUS_RESET:
    595 			case I2O_SCSI_DSC_BUS_BUSY:
    596 				xs->error = XS_BUSY;
    597 				break;
    598 			case I2O_SCSI_DSC_SELECTION_TIMEOUT:
    599 				xs->error = XS_SELTIMEOUT;
    600 				break;
    601 			case I2O_SCSI_DSC_COMMAND_TIMEOUT:
    602 			case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
    603 			case I2O_SCSI_DSC_LUN_INVALID:
    604 			case I2O_SCSI_DSC_SCSI_TID_INVALID:
    605 				xs->error = XS_TIMEOUT;
    606 				break;
    607 			default:
    608 				xs->error = XS_DRIVER_STUFFUP;
    609 				break;
    610 			}
    611 			printf("%s: HBA status 0x%02x\n", sc->sc_dv.dv_xname,
    612 			   rb->hbastatus);
    613 		} else if (rb->scsistatus != SCSI_OK) {
    614 			switch (rb->scsistatus) {
    615 			case SCSI_CHECK:
    616 				xs->error = XS_SENSE;
    617 				sl = le32toh(rb->senselen);
    618 				if (sl > sizeof(xs->sense.scsi_sense))
    619 					sl = sizeof(xs->sense.scsi_sense);
    620 				memcpy(&xs->sense.scsi_sense, rb->sense, sl);
    621 				break;
    622 			case SCSI_BUSY:
    623 				xs->error = XS_BUSY;
    624 				break;
    625 			default:
    626 				xs->error = XS_DRIVER_STUFFUP;
    627 				break;
    628 			}
    629 		} else
    630 			xs->error = XS_NOERROR;
    631 
    632 		xs->resid = xs->datalen - le32toh(rb->datalen);
    633 		xs->status = rb->scsistatus;
    634 	}
    635 
    636 	/* Free the message wrapper and pass the news to scsipi. */
    637 	if (xs->datalen != 0)
    638 		iop_msg_unmap(iop, im);
    639 	iop_msg_free(iop, im);
    640 	xs->xs_status |= XS_STS_DONE;
    641 	scsipi_done(xs);
    642 }
    643 
    644 /*
    645  * ioctl hook; used here only to initiate low-level rescans.
    646  */
    647 static int
    648 iopsp_ioctl(struct scsipi_link *sc_link, u_long cmd, caddr_t data, int flag,
    649 	    struct proc *p)
    650 {
    651 	int rv;
    652 
    653 	switch (cmd) {
    654 	case SCBUSIOLLSCAN:
    655 		/*
    656 		 * If it's boot time, the bus will have been scanned and the
    657 		 * maps built.  Locking would stop re-configuration, but we
    658 		 * want to fake success.
    659 		 */
    660 		if (p != &proc0)
    661 			rv = iopsp_rescan(sc_link->adapter_softc);
    662 		else
    663 			rv = 0;
    664 		break;
    665 
    666 	default:
    667 		rv = ENOTTY;
    668 		break;
    669 	}
    670 
    671 	return (rv);
    672 }
    673 
    674 /*
    675  * The number of openings available to us has changed, so inform scsipi.
    676  */
    677 static void
    678 iopsp_adjqparam(struct device *dv, int mpi)
    679 {
    680 	int s;
    681 
    682 	/* XXX */
    683 	s = splbio();
    684 	((struct iopsp_softc *)dv)->sc_link.openings = mpi;
    685 	splx(s);
    686 }
    687