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