Home | History | Annotate | Line # | Download | only in sbus
isp_sbus.c revision 1.53
      1 /* $NetBSD: isp_sbus.c,v 1.53 2002/05/18 00:48:11 mjacob Exp $ */
      2 /*
      3  * This driver, which is contained in NetBSD in the files:
      4  *
      5  *	sys/dev/ic/isp.c
      6  *	sys/dev/ic/isp_inline.h
      7  *	sys/dev/ic/isp_netbsd.c
      8  *	sys/dev/ic/isp_netbsd.h
      9  *	sys/dev/ic/isp_target.c
     10  *	sys/dev/ic/isp_target.h
     11  *	sys/dev/ic/isp_tpublic.h
     12  *	sys/dev/ic/ispmbox.h
     13  *	sys/dev/ic/ispreg.h
     14  *	sys/dev/ic/ispvar.h
     15  *	sys/microcode/isp/asm_sbus.h
     16  *	sys/microcode/isp/asm_1040.h
     17  *	sys/microcode/isp/asm_1080.h
     18  *	sys/microcode/isp/asm_12160.h
     19  *	sys/microcode/isp/asm_2100.h
     20  *	sys/microcode/isp/asm_2200.h
     21  *	sys/pci/isp_pci.c
     22  *	sys/sbus/isp_sbus.c
     23  *
     24  * Is being actively maintained by Matthew Jacob (mjacob (at) netbsd.org).
     25  * This driver also is shared source with FreeBSD, OpenBSD, Linux, Solaris,
     26  * Linux versions. This tends to be an interesting maintenance problem.
     27  *
     28  * Please coordinate with Matthew Jacob on changes you wish to make here.
     29  */
     30 /*
     31  * SBus specific probe and attach routines for Qlogic ISP SCSI adapters.
     32  *
     33  * Copyright (c) 1997, 2001 by Matthew Jacob
     34  * NASA AMES Research Center
     35  * All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice immediately at the beginning of the file, without modification,
     42  *    this list of conditions, and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     51  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  * SUCH DAMAGE.
     58  *
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 __KERNEL_RCSID(0, "$NetBSD: isp_sbus.c,v 1.53 2002/05/18 00:48:11 mjacob Exp $");
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/device.h>
     67 #include <sys/kernel.h>
     68 #include <sys/malloc.h>
     69 #include <sys/queue.h>
     70 #include <dev/ic/isp_netbsd.h>
     71 #include <machine/intr.h>
     72 #include <machine/autoconf.h>
     73 #include <dev/microcode/isp/asm_sbus.h>
     74 #include <dev/sbus/sbusvar.h>
     75 #include <sys/reboot.h>
     76 
     77 static void isp_sbus_reset1(struct ispsoftc *);
     78 static int isp_sbus_intr(void *);
     79 static int
     80 isp_sbus_rd_isr(struct ispsoftc *, u_int16_t *, u_int16_t *, u_int16_t *);
     81 static u_int16_t isp_sbus_rd_reg(struct ispsoftc *, int);
     82 static void isp_sbus_wr_reg (struct ispsoftc *, int, u_int16_t);
     83 static int isp_sbus_mbxdma(struct ispsoftc *);
     84 static int isp_sbus_dmasetup(struct ispsoftc *, XS_T *, ispreq_t *, u_int16_t *,
     85     u_int16_t);
     86 static void isp_sbus_dmateardown(struct ispsoftc *, XS_T *, u_int16_t);
     87 
     88 #ifndef	ISP_1000_RISC_CODE
     89 #define	ISP_1000_RISC_CODE	NULL
     90 #endif
     91 
     92 static struct ispmdvec mdvec = {
     93 	isp_sbus_rd_isr,
     94 	isp_sbus_rd_reg,
     95 	isp_sbus_wr_reg,
     96 	isp_sbus_mbxdma,
     97 	isp_sbus_dmasetup,
     98 	isp_sbus_dmateardown,
     99 	NULL,
    100 	isp_sbus_reset1,
    101 	NULL,
    102 	ISP_1000_RISC_CODE
    103 };
    104 
    105 struct isp_sbussoftc {
    106 	struct ispsoftc	sbus_isp;
    107 	struct sbusdev	sbus_sd;
    108 	sdparam		sbus_dev;
    109 	bus_space_tag_t	sbus_bustag;
    110 	bus_space_handle_t sbus_reg;
    111 	int		sbus_node;
    112 	int		sbus_pri;
    113 	struct ispmdvec	sbus_mdvec;
    114 	bus_dmamap_t	*sbus_dmamap;
    115 	int16_t		sbus_poff[_NREG_BLKS];
    116 };
    117 
    118 
    119 static int isp_match(struct device *, struct cfdata *, void *);
    120 static void isp_sbus_attach(struct device *, struct device *, void *);
    121 struct cfattach isp_sbus_ca = {
    122 	sizeof (struct isp_sbussoftc), isp_match, isp_sbus_attach
    123 };
    124 
    125 static int
    126 isp_match(struct device *parent, struct cfdata *cf, void *aux)
    127 {
    128 	int rv;
    129 #ifdef DEBUG
    130 	static int oneshot = 1;
    131 #endif
    132 	struct sbus_attach_args *sa = aux;
    133 
    134 	rv = (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
    135 		strcmp("PTI,ptisp", sa->sa_name) == 0 ||
    136 		strcmp("ptisp", sa->sa_name) == 0 ||
    137 		strcmp("SUNW,isp", sa->sa_name) == 0 ||
    138 		strcmp("QLGC,isp", sa->sa_name) == 0);
    139 #ifdef DEBUG
    140 	if (rv && oneshot) {
    141 		oneshot = 0;
    142 		printf("Qlogic ISP Driver, NetBSD (sbus) Platform Version "
    143 		    "%d.%d Core Version %d.%d\n",
    144 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
    145 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
    146 	}
    147 #endif
    148 	return (rv);
    149 }
    150 
    151 
    152 static void
    153 isp_sbus_attach(struct device *parent, struct device *self, void *aux)
    154 {
    155 	int freq, ispburst, sbusburst;
    156 	struct sbus_attach_args *sa = aux;
    157 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) self;
    158 	struct ispsoftc *isp = &sbc->sbus_isp;
    159 
    160 	printf(" for %s\n", sa->sa_name);
    161 
    162 	sbc->sbus_bustag = sa->sa_bustag;
    163 	if (sa->sa_nintr != 0)
    164 		sbc->sbus_pri = sa->sa_pri;
    165 	sbc->sbus_mdvec = mdvec;
    166 
    167 	if (sa->sa_npromvaddrs) {
    168 		sbus_promaddr_to_handle(sa->sa_bustag,
    169 			sa->sa_promvaddrs[0], &sbc->sbus_reg);
    170 	} else {
    171 		if (sbus_bus_map(sa->sa_bustag,	sa->sa_slot, sa->sa_offset,
    172 			sa->sa_size, 0, &sbc->sbus_reg) != 0) {
    173 			printf("%s: cannot map registers\n", self->dv_xname);
    174 			return;
    175 		}
    176 	}
    177 	sbc->sbus_node = sa->sa_node;
    178 
    179 	freq = PROM_getpropint(sa->sa_node, "clock-frequency", 0);
    180 	if (freq) {
    181 		/*
    182 		 * Convert from HZ to MHz, rounding up.
    183 		 */
    184 		freq = (freq + 500000)/1000000;
    185 #if	0
    186 		printf("%s: %d MHz\n", self->dv_xname, freq);
    187 #endif
    188 	}
    189 	sbc->sbus_mdvec.dv_clock = freq;
    190 
    191 	/*
    192 	 * Now figure out what the proper burst sizes, etc., to use.
    193 	 * Unfortunately, there is no ddi_dma_burstsizes here which
    194 	 * walks up the tree finding the limiting burst size node (if
    195 	 * any).
    196 	 */
    197 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
    198 	if (sbusburst == 0)
    199 		sbusburst = SBUS_BURST_32 - 1;
    200 	ispburst = PROM_getpropint(sa->sa_node, "burst-sizes", -1);
    201 	if (ispburst == -1) {
    202 		ispburst = sbusburst;
    203 	}
    204 	ispburst &= sbusburst;
    205 	ispburst &= ~(1 << 7);
    206 	ispburst &= ~(1 << 6);
    207 	sbc->sbus_mdvec.dv_conf1 =  0;
    208 	if (ispburst & (1 << 5)) {
    209 		sbc->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_32;
    210 	} else if (ispburst & (1 << 4)) {
    211 		sbc->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_16;
    212 	} else if (ispburst & (1 << 3)) {
    213 		sbc->sbus_mdvec.dv_conf1 =
    214 		    BIU_SBUS_CONF1_BURST8 | BIU_SBUS_CONF1_FIFO_8;
    215 	}
    216 	if (sbc->sbus_mdvec.dv_conf1) {
    217 		sbc->sbus_mdvec.dv_conf1 |= BIU_BURST_ENABLE;
    218 	}
    219 
    220 	/*
    221 	 * Some early versions of the PTI SBus adapter
    222 	 * would fail in trying to download (via poking)
    223 	 * FW. We give up on them.
    224 	 */
    225 	if (strcmp("PTI,ptisp", sa->sa_name) == 0 ||
    226 	    strcmp("ptisp", sa->sa_name) == 0) {
    227 		sbc->sbus_mdvec.dv_ispfw = NULL;
    228 	}
    229 
    230 	isp->isp_mdvec = &sbc->sbus_mdvec;
    231 	isp->isp_bustype = ISP_BT_SBUS;
    232 	isp->isp_type = ISP_HA_SCSI_UNKNOWN;
    233 	isp->isp_param = &sbc->sbus_dev;
    234 	isp->isp_dmatag = sa->sa_dmatag;
    235 	MEMZERO(isp->isp_param, sizeof (sdparam));
    236 
    237 	sbc->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
    238 	sbc->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
    239 	sbc->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
    240 	sbc->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
    241 	sbc->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
    242 
    243 	/* Establish interrupt channel */
    244 	bus_intr_establish(sbc->sbus_bustag, sbc->sbus_pri, IPL_BIO, 0,
    245 	    isp_sbus_intr, sbc);
    246 	sbus_establish(&sbc->sbus_sd, &sbc->sbus_isp.isp_osinfo._dev);
    247 
    248 	/*
    249 	 * Set up logging levels.
    250 	 */
    251 #ifdef	ISP_LOGDEFAULT
    252 	isp->isp_dblev = ISP_LOGDEFAULT;
    253 #else
    254 	isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
    255 	if (bootverbose)
    256 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
    257 #ifdef	SCSIDEBUG
    258 	isp->isp_dblev |= ISP_LOGDEBUG1|ISP_LOGDEBUG2;
    259 #endif
    260 #ifdef	DEBUG
    261 	isp->isp_dblev |= ISP_LOGDEBUG0;
    262 #endif
    263 #endif
    264 
    265 	isp->isp_confopts = self->dv_cfdata->cf_flags;
    266 	isp->isp_role = ISP_DEFAULT_ROLES;
    267 
    268 	/*
    269 	 * There's no tool on sparc to set NVRAM for ISPs, so ignore it.
    270 	 */
    271 	isp->isp_confopts |= ISP_CFG_NONVRAM;
    272 	ISP_LOCK(isp);
    273 	isp->isp_osinfo.no_mbox_ints = 1;
    274 	isp_reset(isp);
    275 	if (isp->isp_state != ISP_RESETSTATE) {
    276 		ISP_UNLOCK(isp);
    277 		return;
    278 	}
    279 	ENABLE_INTS(isp);
    280 	isp_init(isp);
    281 	if (isp->isp_state != ISP_INITSTATE) {
    282 		isp_uninit(isp);
    283 		ISP_UNLOCK(isp);
    284 		return;
    285 	}
    286 
    287 	/*
    288 	 * do generic attach.
    289 	 */
    290 	ISP_UNLOCK(isp);
    291 	isp_attach(isp);
    292 	if (isp->isp_state != ISP_RUNSTATE) {
    293 		ISP_LOCK(isp);
    294 		isp_uninit(isp);
    295 		ISP_UNLOCK(isp);
    296 	}
    297 }
    298 
    299 
    300 static void
    301 isp_sbus_reset1(struct ispsoftc *isp)
    302 {
    303 	if (isp->isp_osinfo.no_mbox_ints == 0) {
    304 		ENABLE_INTS(isp);
    305 	}
    306 
    307 }
    308 
    309 static int
    310 isp_sbus_intr(void *arg)
    311 {
    312 	u_int16_t isr, sema, mbox;
    313 	struct ispsoftc *isp = arg;
    314 
    315 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
    316 		isp->isp_intbogus++;
    317 		return (0);
    318 	} else {
    319 		struct isp_sbussoftc *sbc = arg;
    320 		sbc->sbus_isp.isp_osinfo.onintstack = 1;
    321 		isp_intr(isp, isr, sema, mbox);
    322 		sbc->sbus_isp.isp_osinfo.onintstack = 0;
    323 		return (1);
    324 	}
    325 }
    326 
    327 #define	IspVirt2Off(a, x)	\
    328 	(((struct isp_sbussoftc *)a)->sbus_poff[((x) & _BLK_REG_MASK) >> \
    329 	_BLK_REG_SHFT] + ((x) & 0xff))
    330 
    331 #define	BXR2(sbc, off)		\
    332 	bus_space_read_2(sbc->sbus_bustag, sbc->sbus_reg, off)
    333 
    334 static int
    335 isp_sbus_rd_isr(struct ispsoftc *isp, u_int16_t *isrp,
    336     u_int16_t *semap, u_int16_t *mbp)
    337 {
    338 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    339 	u_int16_t isr, sema;
    340 
    341 	isr = BXR2(sbc, IspVirt2Off(isp, BIU_ISR));
    342 	sema = BXR2(sbc, IspVirt2Off(isp, BIU_SEMA));
    343 	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
    344 	isr &= INT_PENDING_MASK(isp);
    345 	sema &= BIU_SEMA_LOCK;
    346 	if (isr == 0 && sema == 0) {
    347 		return (0);
    348 	}
    349 	*isrp = isr;
    350 	if ((*semap = sema) != 0) {
    351 		*mbp = BXR2(sbc, IspVirt2Off(isp, OUTMAILBOX0));
    352 	}
    353 	return (1);
    354 }
    355 
    356 static u_int16_t
    357 isp_sbus_rd_reg(struct ispsoftc *isp, int regoff)
    358 {
    359 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    360 	int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
    361 	offset += (regoff & 0xff);
    362 	return (bus_space_read_2(sbc->sbus_bustag, sbc->sbus_reg, offset));
    363 }
    364 
    365 static void
    366 isp_sbus_wr_reg(struct ispsoftc *isp, int regoff, u_int16_t val)
    367 {
    368 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    369 	int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
    370 	offset += (regoff & 0xff);
    371 	bus_space_write_2(sbc->sbus_bustag, sbc->sbus_reg, offset, val);
    372 }
    373 
    374 static int
    375 isp_sbus_mbxdma(struct ispsoftc *isp)
    376 {
    377 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    378 	bus_dma_segment_t reqseg, rspseg;
    379 	int reqrs, rsprs, i, progress;
    380 	size_t n;
    381 	bus_size_t len;
    382 
    383 	if (isp->isp_rquest_dma)
    384 		return (0);
    385 
    386 	n = isp->isp_maxcmds * sizeof (XS_T *);
    387 	isp->isp_xflist = (XS_T **) malloc(n, M_DEVBUF, M_WAITOK);
    388 	if (isp->isp_xflist == NULL) {
    389 		isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
    390 		return (1);
    391 	}
    392 	MEMZERO(isp->isp_xflist, n);
    393 	n = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
    394 	sbc->sbus_dmamap = (bus_dmamap_t *) malloc(n, M_DEVBUF, M_WAITOK);
    395 	if (sbc->sbus_dmamap == NULL) {
    396 		free(isp->isp_xflist, M_DEVBUF);
    397 		isp->isp_xflist = NULL;
    398 		isp_prt(isp, ISP_LOGERR, "cannot alloc dmamap array");
    399 		return (1);
    400 	}
    401 	for (i = 0; i < isp->isp_maxcmds; i++) {
    402 		/* Allocate a DMA handle */
    403 		if (bus_dmamap_create(isp->isp_dmatag, MAXPHYS, 1, MAXPHYS, 0,
    404 		    BUS_DMA_NOWAIT, &sbc->sbus_dmamap[i]) != 0) {
    405 			isp_prt(isp, ISP_LOGERR, "cmd DMA maps create error");
    406 			break;
    407 		}
    408 	}
    409 	if (i < isp->isp_maxcmds) {
    410 		while (--i >= 0) {
    411 			bus_dmamap_destroy(isp->isp_dmatag,
    412 			    sbc->sbus_dmamap[i]);
    413 		}
    414 		free(isp->isp_xflist, M_DEVBUF);
    415 		free(sbc->sbus_dmamap, M_DEVBUF);
    416 		isp->isp_xflist = NULL;
    417 		sbc->sbus_dmamap = NULL;
    418 		return (1);
    419 	}
    420 
    421 	/*
    422 	 * Allocate and map the request and response queues
    423 	 */
    424 	progress = 0;
    425 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
    426 	if (bus_dmamem_alloc(isp->isp_dmatag, len, 0, 0, &reqseg, 1, &reqrs,
    427 	    BUS_DMA_NOWAIT)) {
    428 		goto dmafail;
    429 	}
    430 	progress++;
    431 	if (bus_dmamem_map(isp->isp_dmatag, &reqseg, reqrs, len,
    432 	    (caddr_t *)&isp->isp_rquest, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    433 		goto dmafail;
    434 	}
    435 	progress++;
    436 	if (bus_dmamap_create(isp->isp_dmatag, len, 1, len, 0, BUS_DMA_NOWAIT,
    437 	    &isp->isp_rqdmap) != 0) {
    438 		goto dmafail;
    439 	}
    440 	progress++;
    441 	if (bus_dmamap_load(isp->isp_dmatag, isp->isp_rqdmap,
    442 	    isp->isp_rquest, len, NULL, BUS_DMA_NOWAIT) != 0) {
    443 		goto dmafail;
    444 	}
    445 	progress++;
    446 	isp->isp_rquest_dma = isp->isp_rqdmap->dm_segs[0].ds_addr;
    447 
    448 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
    449 	if (bus_dmamem_alloc(isp->isp_dmatag, len, 0, 0, &rspseg, 1, &rsprs,
    450 	    BUS_DMA_NOWAIT)) {
    451 		goto dmafail;
    452 	}
    453 	progress++;
    454 	if (bus_dmamem_map(isp->isp_dmatag, &rspseg, rsprs, len,
    455 	    (caddr_t *)&isp->isp_result, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    456 		goto dmafail;
    457 	}
    458 	progress++;
    459 	if (bus_dmamap_create(isp->isp_dmatag, len, 1, len, 0, BUS_DMA_NOWAIT,
    460 	    &isp->isp_rsdmap) != 0) {
    461 		goto dmafail;
    462 	}
    463 	progress++;
    464 	if (bus_dmamap_load(isp->isp_dmatag, isp->isp_rsdmap,
    465 	    isp->isp_result, len, NULL, BUS_DMA_NOWAIT) != 0) {
    466 		goto dmafail;
    467 	}
    468 	isp->isp_result_dma = isp->isp_rsdmap->dm_segs[0].ds_addr;
    469 
    470 	return (0);
    471 
    472 dmafail:
    473 	isp_prt(isp, ISP_LOGERR, "Mailbox DMA Setup Failure");
    474 
    475 	if (progress >= 8) {
    476 		bus_dmamap_unload(isp->isp_dmatag, isp->isp_rsdmap);
    477 	}
    478 	if (progress >= 7) {
    479 		bus_dmamap_destroy(isp->isp_dmatag, isp->isp_rsdmap);
    480 	}
    481 	if (progress >= 6) {
    482 		bus_dmamem_unmap(isp->isp_dmatag,
    483 		    isp->isp_result, ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp)));
    484 	}
    485 	if (progress >= 5) {
    486 		bus_dmamem_free(isp->isp_dmatag, &rspseg, rsprs);
    487 	}
    488 
    489 	if (progress >= 4) {
    490 		bus_dmamap_unload(isp->isp_dmatag, isp->isp_rqdmap);
    491 	}
    492 	if (progress >= 3) {
    493 		bus_dmamap_destroy(isp->isp_dmatag, isp->isp_rqdmap);
    494 	}
    495 	if (progress >= 2) {
    496 		bus_dmamem_unmap(isp->isp_dmatag,
    497 		    isp->isp_rquest, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)));
    498 	}
    499 	if (progress >= 1) {
    500 		bus_dmamem_free(isp->isp_dmatag, &reqseg, reqrs);
    501 	}
    502 
    503 	for (i = 0; i < isp->isp_maxcmds; i++) {
    504 		bus_dmamap_destroy(isp->isp_dmatag, sbc->sbus_dmamap[i]);
    505 	}
    506 	free(sbc->sbus_dmamap, M_DEVBUF);
    507 	free(isp->isp_xflist, M_DEVBUF);
    508 	isp->isp_xflist = NULL;
    509 	sbc->sbus_dmamap = NULL;
    510 	return (1);
    511 }
    512 
    513 /*
    514  * Map a DMA request.
    515  * We're guaranteed that rq->req_handle is a value from 1 to isp->isp_maxcmds.
    516  */
    517 
    518 static int
    519 isp_sbus_dmasetup(struct ispsoftc *isp, XS_T *xs, ispreq_t *rq,
    520     u_int16_t *nxtip, u_int16_t optr)
    521 {
    522 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    523 	bus_dmamap_t dmap;
    524 	ispreq_t *qep;
    525 	int error, cansleep = (xs->xs_control & XS_CTL_NOSLEEP) == 0;
    526 	int in = (xs->xs_control & XS_CTL_DATA_IN) != 0;
    527 
    528 	qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, isp->isp_reqidx);
    529 	if (xs->datalen == 0) {
    530 		rq->req_seg_count = 1;
    531 		goto mbxsync;
    532 	}
    533 
    534 	dmap = sbc->sbus_dmamap[isp_handle_index(rq->req_handle)];
    535 	if (dmap->dm_nsegs != 0) {
    536 		panic("%s: dma map already allocated\n", isp->isp_name);
    537 		/* NOTREACHED */
    538 	}
    539 	error = bus_dmamap_load(isp->isp_dmatag, dmap, xs->data, xs->datalen,
    540 	    NULL, (cansleep ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT) |
    541 	    BUS_DMA_STREAMING);
    542 	if (error != 0) {
    543 		XS_SETERR(xs, HBA_BOTCH);
    544 		if (error == EAGAIN || error == ENOMEM)
    545 			return (CMD_EAGAIN);
    546 		else
    547 			return (CMD_COMPLETE);
    548 	}
    549 
    550 	bus_dmamap_sync(isp->isp_dmatag, dmap, 0, xs->datalen,
    551 	    in? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    552 
    553 	if (in) {
    554 		rq->req_flags |= REQFLAG_DATA_IN;
    555 	} else {
    556 		rq->req_flags |= REQFLAG_DATA_OUT;
    557 	}
    558 
    559 	if (XS_CDBLEN(xs) > 12) {
    560 		u_int16_t onxti;
    561 		ispcontreq_t local, *crq = &local, *cqe;
    562 
    563 		onxti = *nxtip;
    564 		cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, onxti);
    565 		*nxtip = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
    566 		if (*nxtip == optr) {
    567 			isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
    568 			bus_dmamap_unload(isp->isp_dmatag, dmap);
    569 			XS_SETERR(xs, HBA_BOTCH);
    570 			return (CMD_EAGAIN);
    571 		}
    572 		rq->req_seg_count = 2;
    573 		MEMZERO((void *)crq, sizeof (*crq));
    574 		crq->req_header.rqs_entry_count = 1;
    575 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
    576 		crq->req_dataseg[0].ds_count = xs->datalen;
    577 		crq->req_dataseg[0].ds_base = dmap->dm_segs[0].ds_addr;
    578 		isp_put_cont_req(isp, crq, cqe);
    579 		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
    580 	} else {
    581 		rq->req_seg_count = 1;
    582 		rq->req_dataseg[0].ds_count = xs->datalen;
    583 		rq->req_dataseg[0].ds_base = dmap->dm_segs[0].ds_addr;
    584 	}
    585 
    586 mbxsync:
    587 	if (XS_CDBLEN(xs) > 12) {
    588 		isp_put_extended_request(isp,
    589 		    (ispextreq_t *)rq, (ispextreq_t *) qep);
    590 	} else {
    591 		isp_put_request(isp, rq, qep);
    592 	}
    593 	return (CMD_QUEUED);
    594 }
    595 
    596 static void
    597 isp_sbus_dmateardown(struct ispsoftc *isp, XS_T *xs, u_int16_t handle)
    598 {
    599 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    600 	bus_dmamap_t dmap;
    601 
    602 	dmap = sbc->sbus_dmamap[isp_handle_index(handle)];
    603 
    604 	if (dmap->dm_nsegs == 0) {
    605 		panic("%s: dma map not already allocated\n", isp->isp_name);
    606 		/* NOTREACHED */
    607 	}
    608 	bus_dmamap_sync(isp->isp_dmatag, dmap, 0,
    609 	    xs->datalen, (xs->xs_control & XS_CTL_DATA_IN)?
    610 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    611 	bus_dmamap_unload(isp->isp_dmatag, dmap);
    612 }
    613