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