Home | History | Annotate | Line # | Download | only in i2o
iopsp.c revision 1.4.2.1
      1 /*	$NetBSD: iopsp.c,v 1.4.2.1 2001/03/05 22:49:33 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 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/FC-AL device support for I2O.  I2O presents SCSI devices
     41  * individually; we group them by controller 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/lwp.h>
     52 #include <sys/proc.h>
     53 #include <sys/buf.h>
     54 #include <sys/endian.h>
     55 #include <sys/malloc.h>
     56 #include <sys/scsiio.h>
     57 #include <sys/lock.h>
     58 
     59 #include <machine/bswap.h>
     60 #include <machine/bus.h>
     61 
     62 #include <dev/scsipi/scsi_all.h>
     63 #include <dev/scsipi/scsi_disk.h>
     64 #include <dev/scsipi/scsipi_all.h>
     65 #include <dev/scsipi/scsiconf.h>
     66 
     67 #include <dev/i2o/i2o.h>
     68 #include <dev/i2o/iopreg.h>
     69 #include <dev/i2o/iopvar.h>
     70 #include <dev/i2o/iopspvar.h>
     71 
     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 SCSI and fibre channel ports.
     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, 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_device_identity di;
    135 			struct	i2o_param_hba_ctlr_info ci;
    136 			struct	i2o_param_hba_scsi_ctlr_info sci;
    137 			struct	i2o_param_hba_scsi_port_info spi;
    138 		} p;
    139 	} __attribute__ ((__packed__)) param;
    140 	char ident[64];
    141 	int fcal, rv;
    142 #ifdef I2OVERBOSE
    143 	int size;
    144 #endif
    145 
    146 	ia = (struct iop_attach_args *)aux;
    147 	sc = (struct iopsp_softc *)self;
    148 	iop = (struct iop_softc *)parent;
    149 	sc->sc_tid = ia->ia_tid;
    150 
    151 	/* Register us as an initiator. */
    152 	sc->sc_ii.ii_dv = self;
    153 	sc->sc_ii.ii_intr = iopsp_intr;
    154 	sc->sc_ii.ii_flags = 0;
    155 	sc->sc_ii.ii_tid = ia->ia_tid;
    156 	sc->sc_ii.ii_reconfig = iopsp_reconfig;
    157 	if (iop_initiator_register(iop, &sc->sc_ii) != 0) {
    158 		printf("%s: unable to register as an initiator",
    159 		    sc->sc_dv.dv_xname);
    160 		return;
    161 	}
    162 
    163 	rv = iop_param_op(iop, ia->ia_tid, 0, I2O_PARAM_HBA_CTLR_INFO, &param,
    164 	    sizeof(param));
    165 	if (rv != 0) {
    166 		printf("%s: unable to get parameters (0x%04x; %d)\n",
    167 	    	    sc->sc_dv.dv_xname, I2O_PARAM_HBA_CTLR_INFO, rv);
    168 		goto bad;
    169 	}
    170 
    171 	fcal = (param.p.ci.bustype == I2O_HBA_BUS_FCA);		/* XXX */
    172 
    173 	/*
    174 	 * Say what the device is.  If we can find out what the controling
    175 	 * device is, say what that is too.
    176 	 */
    177 	printf(": %s SCSI port", fcal ? "FC-AL" : "SE/LVD");
    178 	if (iop_param_op(iop, ia->ia_tid, 0, I2O_PARAM_DEVICE_IDENTITY, &param,
    179 	    sizeof(param)) == 0) {
    180 		iop_strvis(iop, param.p.di.vendorinfo,
    181 		    sizeof(param.p.di.vendorinfo), ident, sizeof(ident));
    182 		printf(" <%s, ", ident);
    183 		iop_strvis(iop, param.p.di.productinfo,
    184 		    sizeof(param.p.di.productinfo), ident, sizeof(ident));
    185 		printf("%s, ", ident);
    186 		iop_strvis(iop, param.p.di.revlevel,
    187 		    sizeof(param.p.di.revlevel), ident, sizeof(ident));
    188 		printf("%s> ", ident);
    189 	}
    190 	printf("\n");
    191 
    192 	rv = iop_param_op(iop, ia->ia_tid, 0, I2O_PARAM_HBA_SCSI_CTLR_INFO,
    193 	    &param, sizeof(param));
    194 	if (rv != 0) {
    195 		printf("%s: unable to get parameters (0x%04x; %d)\n",
    196 		    sc->sc_dv.dv_xname, I2O_PARAM_HBA_SCSI_CTLR_INFO, rv);
    197 		goto bad;
    198 	}
    199 
    200 #ifdef I2OVERBOSE
    201 	printf("%s: %d-bit, max sync rate %dMHz, initiator ID %d\n",
    202 	    sc->sc_dv.dv_xname, param.p.sci.maxdatawidth,
    203 	    (u_int32_t)le64toh(param.p.sci.maxsyncrate) / 1000,
    204 	    le32toh(param.p.sci.initiatorid));
    205 #endif
    206 
    207 	sc->sc_adapter.scsipi_cmd = iopsp_scsi_cmd;
    208 	sc->sc_adapter.scsipi_minphys = iopsp_minphys;
    209 	sc->sc_adapter.scsipi_ioctl = iopsp_ioctl;
    210 
    211 	sc_link = &sc->sc_link;
    212 	sc_link->type = BUS_SCSI;
    213 	sc_link->device = &iopsp_dev;
    214 	sc_link->adapter = &sc->sc_adapter;
    215 	sc_link->adapter_softc = sc;
    216 	sc_link->scsipi_scsi.channel = 0;
    217 	sc_link->scsipi_scsi.adapter_target = le32toh(param.p.sci.initiatorid);
    218 	sc_link->scsipi_scsi.max_target =
    219 	    fcal ? IOPSP_MAX_FCAL_TARGET : param.p.sci.maxdatawidth - 1;
    220 	sc_link->scsipi_scsi.max_lun = IOPSP_MAX_LUN;
    221 	sc_link->openings = iop->sc_maxqueuecnt / 4;		/* XXX */
    222 
    223 #ifdef I2OVERBOSE
    224 	/*
    225 	 * Allocate the target map.  Currently used for informational
    226 	 * purposes only.
    227 	 */
    228 	size = (sc_link->scsipi_scsi.max_target + 1) *
    229 	    sizeof(struct iopsp_target);
    230 	sc->sc_targetmap = malloc(size, M_DEVBUF, M_NOWAIT);
    231 	memset(sc->sc_targetmap, 0, size);
    232 #endif
    233 
    234  	/* Build the two maps, and attach to scsipi. */
    235 	if (iopsp_reconfig(self) != 0) {
    236 		printf("%s: bus scan failed\n", sc->sc_dv.dv_xname);
    237 		goto bad;
    238 	}
    239 	config_found(self, sc_link, scsiprint);
    240 	return;
    241 
    242 bad:
    243 	iop_initiator_unregister(iop, &sc->sc_ii);
    244 }
    245 
    246 /*
    247  * Scan the LCT to determine which devices we control, and enter them into
    248  * the maps.
    249  */
    250 static int
    251 iopsp_reconfig(struct device *dv)
    252 {
    253 	struct iopsp_softc *sc;
    254 	struct iop_softc *iop;
    255 	struct i2o_lct_entry *le;
    256 	struct scsipi_link *sc_link;
    257 	struct {
    258 		struct	i2o_param_op_results pr;
    259 		struct	i2o_param_read_results prr;
    260 		struct	i2o_param_scsi_device_info sdi;
    261 	} __attribute__ ((__packed__)) param;
    262 	int tid, nent, i, targ, lun, size, s, rv;
    263 	u_short *tidmap;
    264 #ifdef I2OVERBOSE
    265 	struct iopsp_target *it;
    266 	int syncrate;
    267 #endif
    268 
    269 	sc = (struct iopsp_softc *)dv;
    270 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    271 	sc_link = &sc->sc_link;
    272 
    273 	/* Anything to do? */
    274 	if (iop->sc_lct->changeindicator == sc->sc_chgindicator)
    275 		return (0);
    276 
    277 	/*
    278 	 * Allocate memory for the target/LUN -> TID map.  Use zero to
    279 	 * denote absent targets (zero is the TID of the I2O executive,
    280 	 * and we never address that here).
    281 	 */
    282 	size = (sc_link->scsipi_scsi.max_target + 1) *
    283 	    (IOPSP_MAX_LUN + 1) * sizeof(u_short);
    284 	if ((tidmap = malloc(size, M_DEVBUF, M_WAITOK)) == NULL)
    285 		return (ENOMEM);
    286 	memset(tidmap, 0, size);
    287 
    288 #ifdef I2OVERBOSE
    289 	for (i = 0; i <= sc_link->scsipi_scsi.max_target; i++)
    290 		sc->sc_targetmap[i].it_flags &= ~IT_PRESENT;
    291 #endif
    292 
    293 	nent = iop->sc_nlctent;
    294 	for (i = 0, le = iop->sc_lct->entry; i < nent; i++, le++) {
    295 		switch (le16toh(le->classid) & 4095) {
    296 		case I2O_CLASS_SCSI_PERIPHERAL:
    297 			break;
    298 		default:
    299 			continue;
    300 		}
    301 		if (((le32toh(le->usertid) >> 12) & 4095) != sc->sc_tid)
    302 			continue;
    303 
    304 		tid = le32toh(le->localtid) & 4095;
    305 
    306 		rv = iop_param_op(iop, tid, 0, I2O_PARAM_SCSI_DEVICE_INFO,
    307 		    &param, sizeof(param));
    308 		if (rv != 0) {
    309 			printf("%s: unable to get parameters (0x%04x; %d)\n",
    310 			    sc->sc_dv.dv_xname, I2O_PARAM_SCSI_DEVICE_INFO,
    311 			    rv);
    312 			continue;
    313 		}
    314 		targ = le32toh(param.sdi.identifier);
    315 		lun = param.sdi.luninfo[1];
    316 
    317 		/* If the device is in use by a DDM, ignore it. */
    318 		if ((le32toh(le->usertid) & 4095) != 4095) {
    319 #ifdef I2OVERBOSE
    320 			if (sc->sc_tidmap == NULL ||
    321 			    IOPSP_TIDMAP(sc->sc_tidmap, targ, lun) !=
    322 			    IOPSP_TID_INUSE)
    323 				printf("%s: target %d,%d (tid %d): in use by"
    324 				    " tid %d\n", sc->sc_dv.dv_xname,
    325 				    targ, lun, tid,
    326 				    le32toh(le->usertid) & 4095);
    327 #endif
    328 			IOPSP_TIDMAP(tidmap, targ, lun) = IOPSP_TID_INUSE;
    329 			continue;
    330 		}
    331 		IOPSP_TIDMAP(tidmap, targ, lun) = (u_short)tid;
    332 
    333 #ifdef I2OVERBOSE
    334 		/*
    335 		 * If we've already described this target, and nothing has
    336 		 * changed, then don't describe it again.
    337 		 */
    338 		it = &sc->sc_targetmap[targ];
    339 		it->it_flags |= IT_PRESENT;
    340 		syncrate = ((int)le64toh(param.sdi.negsyncrate) + 500) / 1000;
    341 		if (it->it_width == param.sdi.negdatawidth &&
    342 		    it->it_offset == param.sdi.negoffset &&
    343 		    it->it_syncrate == syncrate)
    344 			continue;
    345 
    346 		it->it_width = param.sdi.negdatawidth;
    347 		it->it_offset = param.sdi.negoffset;
    348 		it->it_syncrate = syncrate;
    349 
    350 		printf("%s: target %d (tid %d): %d-bit, ", sc->sc_dv.dv_xname,
    351 		    targ, tid, it->it_width);
    352 		if (it->it_syncrate == 0)
    353 			printf("asynchronous\n");
    354 		else
    355 			printf("synchronous at %dMHz, offset 0x%x\n",
    356 			    it->it_syncrate, it->it_offset);
    357 #endif
    358 	}
    359 
    360 #ifdef I2OVERBOSE
    361 	for (i = 0; i <= sc_link->scsipi_scsi.max_target; i++)
    362 		if ((sc->sc_targetmap[i].it_flags & IT_PRESENT) == 0)
    363 			sc->sc_targetmap[i].it_width = 0;
    364 #endif
    365 
    366 	/* Swap in the new map and return. */
    367 	s = splbio();
    368 	if (sc->sc_tidmap != NULL)
    369 		free(sc->sc_tidmap, M_DEVBUF);
    370 	sc->sc_tidmap = tidmap;
    371 	splx(s);
    372 	sc->sc_chgindicator = iop->sc_lct->changeindicator;
    373 	return (0);
    374 }
    375 
    376 /*
    377  * Adjust the size of an I/O request.
    378  */
    379 static void
    380 iopsp_minphys(struct buf *bp)
    381 {
    382 
    383 	if (bp->b_bcount > IOP_MAX_XFER)
    384 		bp->b_bcount = IOP_MAX_XFER;
    385 	minphys(bp);
    386 }
    387 
    388 /*
    389  * Re-scan the bus; to be called from a higher level (e.g. scsipi).
    390  */
    391 static int
    392 iopsp_rescan(struct iopsp_softc *sc)
    393 {
    394 	struct iop_softc *iop;
    395 	struct iop_msg *im;
    396 	struct i2o_hba_bus_scan *mb;
    397 	int rv;
    398 
    399 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    400 
    401 	rv = lockmgr(&iop->sc_conflock, LK_EXCLUSIVE | LK_RECURSEFAIL, NULL);
    402 	if (rv != 0) {
    403 #ifdef I2ODEBUG
    404 		printf("iopsp_rescan: unable to acquire lock\n");
    405 #endif
    406 		return (rv);
    407 	}
    408 
    409 	/* XXX If it's boot time, the bus will already have been scanned. */
    410 	if (curproc->l_proc != &proc0) {
    411 		if ((rv = iop_msg_alloc(iop, &sc->sc_ii, &im, IM_NOINTR)) != 0)
    412 			goto done;
    413 
    414 		mb = (struct i2o_hba_bus_scan *)im->im_msg;
    415 		mb->msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
    416 		mb->msgfunc = I2O_MSGFUNC(sc->sc_tid, I2O_HBA_BUS_SCAN);
    417 		mb->msgictx = sc->sc_ii.ii_ictx;
    418 		mb->msgtctx = im->im_tctx;
    419 
    420 		rv = iop_msg_enqueue(iop, im, 5*60*1000);
    421 		iop_msg_free(iop, &sc->sc_ii, im);
    422 		if (rv != 0)
    423 			goto done;
    424 
    425 		if ((rv = iop_lct_get(iop)) != 0)
    426 			goto done;
    427 	}
    428 
    429 	/* Rebuild the target/LUN -> TID map, release lock, and return. */
    430 	rv = iopsp_reconfig(&sc->sc_dv);
    431 done:
    432 	lockmgr(&iop->sc_conflock, LK_RELEASE, NULL);
    433 	return (rv);
    434 }
    435 
    436 /*
    437  * Start a SCSI command.
    438  */
    439 static int
    440 iopsp_scsi_cmd(struct scsipi_xfer *xs)
    441 {
    442 	struct scsipi_link *sc_link;
    443 	struct iopsp_softc *sc;
    444 	struct iop_msg *im;
    445 	struct iop_softc *iop;
    446 	struct i2o_scsi_scb_exec *mb;
    447 	int error, flags, tid;
    448 
    449 	sc_link = xs->sc_link;
    450 	flags = xs->xs_control;
    451 	sc = sc_link->adapter_softc;
    452 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    453 
    454 	tid = IOPSP_TIDMAP(sc->sc_tidmap, sc_link->scsipi_scsi.target,
    455 	    sc_link->scsipi_scsi.lun);
    456 	if (tid == IOPSP_TID_ABSENT || tid == IOPSP_TID_INUSE) {
    457 		xs->error = XS_SELTIMEOUT;
    458 		return (COMPLETE);
    459 	}
    460 
    461 	SC_DEBUG(sc_link, SDEV_DB2, ("iopsp_scsi_cmd\n"));
    462 
    463 	/* Need to reset the target? */
    464 	if ((flags & XS_CTL_RESET) != 0) {
    465 		if (iop_simple_cmd(iop, tid, I2O_SCSI_DEVICE_RESET,
    466 		    sc->sc_ii.ii_ictx, 1, 10*1000) != 0) {
    467 #ifdef I2ODEBUG
    468 			printf("%s: reset failed\n", sc->sc_dv.dv_xname);
    469 #endif
    470 			xs->error = XS_DRIVER_STUFFUP;
    471 		}
    472 		return (COMPLETE);
    473 	}
    474 
    475 #if defined(I2ODEBUG) || defined(SCSIDEBUG)
    476 	if (xs->cmdlen > 16)
    477 		panic("%s: CDB too large\n", sc->sc_dv.dv_xname);
    478 #endif
    479 
    480 	if (iop_msg_alloc(iop, &sc->sc_ii, &im,
    481 	    (flags & (XS_CTL_POLL | XS_CTL_NOSLEEP)) != 0 ? IM_NOWAIT : 0)) {
    482 		xs->error = XS_DRIVER_STUFFUP;
    483 		return (TRY_AGAIN_LATER);
    484 	}
    485 	im->im_dvcontext = xs;
    486 
    487 	mb = (struct i2o_scsi_scb_exec *)im->im_msg;
    488 	mb->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_exec);
    489 	mb->msgfunc = I2O_MSGFUNC(tid, I2O_SCSI_SCB_EXEC);
    490 	mb->msgictx = sc->sc_ii.ii_ictx;
    491 	mb->msgtctx = im->im_tctx;
    492 	mb->flags = xs->cmdlen | I2O_SCB_FLAG_ENABLE_DISCONNECT |
    493 	    I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
    494 	memcpy(mb->cdb, xs->cmd, xs->cmdlen);
    495 	mb->datalen = xs->datalen;
    496 
    497 	if ((xs->sc_link->quirks & SDEV_NOTAG) == 0 &&
    498 	    (xs->xs_control & XS_CTL_POLL) != 0) {
    499 		if (xs->bp != NULL && (xs->bp->b_flags & B_ASYNC) != 0)
    500 			mb->flags |= I2O_SCB_FLAG_ORDERED_QUEUE_TAG;
    501 		else
    502 			mb->flags |= I2O_SCB_FLAG_SIMPLE_QUEUE_TAG;
    503 	}
    504 
    505 	if (xs->datalen != 0) {
    506 		error = iop_msg_map(iop, im, xs->data, xs->datalen,
    507 		    (flags & XS_CTL_DATA_OUT) == 0);
    508 		if (error) {
    509 #ifdef I2ODEBUG
    510 			printf("%s: error %d mapping xfer\n",
    511 			    sc->sc_dv.dv_xname, error);
    512 #endif
    513 			xs->error = XS_DRIVER_STUFFUP;
    514 			iop_msg_free(iop, &sc->sc_ii, im);
    515 			return (COMPLETE);
    516 		}
    517 		if ((flags & XS_CTL_DATA_IN) == 0)
    518 			mb->flags |= I2O_SCB_FLAG_XFER_TO_DEVICE;
    519 		else
    520 			mb->flags |= I2O_SCB_FLAG_XFER_FROM_DEVICE;
    521 	}
    522 
    523 	/*
    524 	 * If the command is allowed to execute asynchronously, enqueue it
    525 	 * with the IOP.
    526 	 */
    527 	if ((flags & XS_CTL_POLL) == 0) {
    528 		iop_msg_enqueue(iop, im, 0);
    529 		return (SUCCESSFULLY_QUEUED);
    530 	}
    531 
    532 	if (iop_msg_send(iop, im, xs->timeout)) {
    533 		scsi_print_addr(xs->sc_link);
    534 		printf("timeout; aborting command\n");
    535 		if (iopsp_scsi_abort(sc, tid, im)) {
    536 			scsi_print_addr(xs->sc_link);
    537 			printf("abort failed\n");
    538 		}
    539 		xs->error = XS_DRIVER_STUFFUP;
    540 	}
    541 	return (COMPLETE);
    542 }
    543 
    544 /*
    545  * Abort the specified I2O_SCSI_SCB_EXEC message and its associated SCB.
    546  */
    547 static int
    548 iopsp_scsi_abort(struct iopsp_softc *sc, int atid, struct iop_msg *aim)
    549 {
    550 	struct iop_msg *im;
    551 	struct i2o_scsi_scb_abort *mb;
    552 	struct iop_softc *iop;
    553 	int rv;
    554 
    555 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    556 
    557 	rv = iop_msg_alloc(iop, &sc->sc_ii, &im, IM_NOWAIT | IM_NOINTR);
    558 	if (rv != 0)
    559 		return (rv);
    560 
    561 	mb = (struct i2o_scsi_scb_abort *)im->im_msg;
    562 	mb->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_abort);
    563 	mb->msgfunc = I2O_MSGFUNC(atid, I2O_SCSI_SCB_ABORT);
    564 	mb->msgictx = sc->sc_ii.ii_ictx;
    565 	mb->msgtctx = im->im_tctx;
    566 	mb->tctxabort = aim->im_tctx;
    567 
    568 	rv = iop_msg_send(iop, im, 1000);
    569 	iop_msg_free(iop, &sc->sc_ii, im);
    570 	return (rv);
    571 }
    572 
    573 /*
    574  * We have a message which has been processed and replied to by the IOP -
    575  * deal with it.
    576  */
    577 static void
    578 iopsp_intr(struct device *dv, struct iop_msg *im, void *reply)
    579 {
    580 	struct scsipi_xfer *xs;
    581 	struct iopsp_softc *sc;
    582 	struct i2o_scsi_reply *rb;
    583 	struct iop_softc *iop;
    584 	u_int hba_status, scsi_status, detail;
    585 	int sl;
    586 
    587 	sc = (struct iopsp_softc *)dv;
    588 	xs = (struct scsipi_xfer *)im->im_dvcontext;
    589 	iop = (struct iop_softc *)dv->dv_parent;
    590 
    591 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("iopsp_intr\n"));
    592 
    593 	if (xs->error == XS_NOERROR) {
    594 		rb = reply;
    595 		detail = le16toh(rb->detail);
    596 		hba_status = (detail >> 8) & 0xff;
    597 		scsi_status = detail & 0xff;
    598 
    599 		if (hba_status != I2O_SCSI_DSC_SUCCESS) {
    600 			switch (hba_status) {
    601 			case I2O_SCSI_DSC_ADAPTER_BUSY:
    602 			case I2O_SCSI_DSC_SCSI_BUS_RESET:
    603 			case I2O_SCSI_DSC_BUS_BUSY:
    604 				xs->error = XS_BUSY;
    605 				break;
    606 			case I2O_SCSI_DSC_SELECTION_TIMEOUT:
    607 				xs->error = XS_SELTIMEOUT;
    608 				break;
    609 			case I2O_SCSI_DSC_COMMAND_TIMEOUT:
    610 			case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
    611 			case I2O_SCSI_DSC_LUN_INVALID:
    612 			case I2O_SCSI_DSC_SCSI_TID_INVALID:
    613 				xs->error = XS_TIMEOUT;
    614 				break;
    615 			default:
    616 				xs->error = XS_DRIVER_STUFFUP;
    617 				break;
    618 			}
    619 #ifdef I2ODEBUG
    620 			printf("%s: HBA status 0x%02x\n", sc->sc_dv.dv_xname,
    621 			    hba_status);
    622 #endif
    623 		} else if (scsi_status != SCSI_OK) {
    624 			switch (scsi_status) {
    625 			case SCSI_CHECK:
    626 				xs->error = XS_SENSE;
    627 				sl = le32toh(rb->senselen);
    628 				if (xs->req_sense_length != 0 &&
    629 				    xs->req_sense_length < sl)
    630 					sl = xs->req_sense_length;
    631 				if (sl > sizeof(xs->sense.scsi_sense))
    632 					sl = le32toh(rb->senselen);
    633 				memcpy(&xs->sense.scsi_sense, rb->sense, sl);
    634 				break;
    635 			case SCSI_BUSY:
    636 				xs->error = XS_BUSY;
    637 				break;
    638 			default:
    639 				xs->error = XS_DRIVER_STUFFUP;
    640 				break;
    641 			}
    642 		} else
    643 			xs->error = XS_NOERROR;
    644 
    645 		xs->resid = le32toh(rb->datalen) - xs->datalen;
    646 		xs->status = scsi_status;
    647 	}
    648 
    649 	/* Free the message wrapper and pass the news to scsipi. */
    650 	iop_msg_unmap(iop, im);
    651 	iop_msg_free(iop, &sc->sc_ii, im);
    652 	xs->xs_status |= XS_STS_DONE;
    653 	scsipi_done(xs);
    654 }
    655 
    656 /*
    657  * ioctl hook; used here only to initiate low-level rescans.
    658  */
    659 static int
    660 iopsp_ioctl(struct scsipi_link *sc_link, u_long cmd, caddr_t data, int flag,
    661 	    struct proc *p)
    662 {
    663 	int rv;
    664 
    665 	switch (cmd) {
    666 	case SCBUSIOLLSCAN:
    667 		rv = iopsp_rescan(sc_link->adapter_softc);
    668 		break;
    669 	default:
    670 		rv = ENXIO;
    671 		break;
    672 	}
    673 
    674 	return (rv);
    675 }
    676