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