Home | History | Annotate | Line # | Download | only in sbus
isp_sbus.c revision 1.66.4.1
      1 /* $NetBSD: isp_sbus.c,v 1.66.4.1 2007/03/12 05:57:07 rmind 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.66.4.1 2007/03/12 05:57:07 rmind 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 	0,
    104 	0
    105 };
    106 
    107 struct isp_sbussoftc {
    108 	struct ispsoftc	sbus_isp;
    109 	struct sbusdev	sbus_sd;
    110 	sdparam		sbus_dev;
    111 	bus_space_tag_t	sbus_bustag;
    112 	bus_space_handle_t sbus_reg;
    113 	int		sbus_node;
    114 	int		sbus_pri;
    115 	struct ispmdvec	sbus_mdvec;
    116 	bus_dmamap_t	*sbus_dmamap;
    117 	int16_t		sbus_poff[_NREG_BLKS];
    118 };
    119 
    120 
    121 static int isp_match(struct device *, struct cfdata *, void *);
    122 static void isp_sbus_attach(struct device *, struct device *, void *);
    123 CFATTACH_DECL(isp_sbus, sizeof (struct isp_sbussoftc),
    124     isp_match, isp_sbus_attach, NULL, NULL);
    125 
    126 static int
    127 isp_match(struct device *parent, struct cfdata *cf, void *aux)
    128 {
    129 	int rv;
    130 #ifdef DEBUG
    131 	static int oneshot = 1;
    132 #endif
    133 	struct sbus_attach_args *sa = aux;
    134 
    135 	rv = (strcmp(cf->cf_name, sa->sa_name) == 0 ||
    136 		strcmp("PTI,ptisp", sa->sa_name) == 0 ||
    137 		strcmp("ptisp", sa->sa_name) == 0 ||
    138 		strcmp("SUNW,isp", sa->sa_name) == 0 ||
    139 		strcmp("QLGC,isp", sa->sa_name) == 0);
    140 #ifdef DEBUG
    141 	if (rv && oneshot) {
    142 		oneshot = 0;
    143 		printf("Qlogic ISP Driver, NetBSD (sbus) Platform Version "
    144 		    "%d.%d Core Version %d.%d\n",
    145 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
    146 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
    147 	}
    148 #endif
    149 	return (rv);
    150 }
    151 
    152 
    153 static void
    154 isp_sbus_attach(struct device *parent, struct device *self, void *aux)
    155 {
    156 	int freq, ispburst, sbusburst;
    157 	struct sbus_attach_args *sa = aux;
    158 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) self;
    159 	struct ispsoftc *isp = &sbc->sbus_isp;
    160 
    161 	printf(" for %s\n", sa->sa_name);
    162 
    163 	sbc->sbus_bustag = sa->sa_bustag;
    164 	if (sa->sa_nintr != 0)
    165 		sbc->sbus_pri = sa->sa_pri;
    166 	sbc->sbus_mdvec = mdvec;
    167 
    168 	if (sa->sa_npromvaddrs) {
    169 		sbus_promaddr_to_handle(sa->sa_bustag,
    170 			sa->sa_promvaddrs[0], &sbc->sbus_reg);
    171 	} else {
    172 		if (sbus_bus_map(sa->sa_bustag,	sa->sa_slot, sa->sa_offset,
    173 			sa->sa_size, 0, &sbc->sbus_reg) != 0) {
    174 			printf("%s: cannot map registers\n", self->dv_xname);
    175 			return;
    176 		}
    177 	}
    178 	sbc->sbus_node = sa->sa_node;
    179 
    180 	freq = prom_getpropint(sa->sa_node, "clock-frequency", 0);
    181 	if (freq) {
    182 		/*
    183 		 * Convert from HZ to MHz, rounding up.
    184 		 */
    185 		freq = (freq + 500000)/1000000;
    186 #if	0
    187 		printf("%s: %d MHz\n", self->dv_xname, freq);
    188 #endif
    189 	}
    190 	sbc->sbus_mdvec.dv_clock = freq;
    191 
    192 	/*
    193 	 * Now figure out what the proper burst sizes, etc., to use.
    194 	 * Unfortunately, there is no ddi_dma_burstsizes here which
    195 	 * walks up the tree finding the limiting burst size node (if
    196 	 * any).
    197 	 */
    198 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
    199 	if (sbusburst == 0)
    200 		sbusburst = SBUS_BURST_32 - 1;
    201 	ispburst = prom_getpropint(sa->sa_node, "burst-sizes", -1);
    202 	if (ispburst == -1) {
    203 		ispburst = sbusburst;
    204 	}
    205 	ispburst &= sbusburst;
    206 	ispburst &= ~(1 << 7);
    207 	ispburst &= ~(1 << 6);
    208 	sbc->sbus_mdvec.dv_conf1 =  0;
    209 	if (ispburst & (1 << 5)) {
    210 		sbc->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_32;
    211 	} else if (ispburst & (1 << 4)) {
    212 		sbc->sbus_mdvec.dv_conf1 = BIU_SBUS_CONF1_FIFO_16;
    213 	} else if (ispburst & (1 << 3)) {
    214 		sbc->sbus_mdvec.dv_conf1 =
    215 		    BIU_SBUS_CONF1_BURST8 | BIU_SBUS_CONF1_FIFO_8;
    216 	}
    217 	if (sbc->sbus_mdvec.dv_conf1) {
    218 		sbc->sbus_mdvec.dv_conf1 |= BIU_BURST_ENABLE;
    219 	}
    220 
    221 	/*
    222 	 * Some early versions of the PTI SBus adapter
    223 	 * would fail in trying to download (via poking)
    224 	 * FW. We give up on them.
    225 	 */
    226 	if (strcmp("PTI,ptisp", sa->sa_name) == 0 ||
    227 	    strcmp("ptisp", sa->sa_name) == 0) {
    228 		sbc->sbus_mdvec.dv_ispfw = NULL;
    229 	}
    230 
    231 	isp->isp_mdvec = &sbc->sbus_mdvec;
    232 	isp->isp_bustype = ISP_BT_SBUS;
    233 	isp->isp_type = ISP_HA_SCSI_UNKNOWN;
    234 	isp->isp_param = &sbc->sbus_dev;
    235 	isp->isp_dmatag = sa->sa_dmatag;
    236 	MEMZERO(isp->isp_param, sizeof (sdparam));
    237 
    238 	sbc->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
    239 	sbc->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
    240 	sbc->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
    241 	sbc->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
    242 	sbc->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
    243 
    244 	/* Establish interrupt channel */
    245 	bus_intr_establish(sbc->sbus_bustag, sbc->sbus_pri, IPL_BIO,
    246 	    isp_sbus_intr, sbc);
    247 	sbus_establish(&sbc->sbus_sd, &sbc->sbus_isp.isp_osinfo._dev);
    248 
    249 	/*
    250 	 * Set up logging levels.
    251 	 */
    252 #ifdef	ISP_LOGDEFAULT
    253 	isp->isp_dblev = ISP_LOGDEFAULT;
    254 #else
    255 	isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
    256 	if (bootverbose)
    257 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
    258 #ifdef	SCSIDEBUG
    259 	isp->isp_dblev |= ISP_LOGDEBUG1|ISP_LOGDEBUG2;
    260 #endif
    261 #ifdef	DEBUG
    262 	isp->isp_dblev |= ISP_LOGDEBUG0;
    263 #endif
    264 #endif
    265 
    266 	isp->isp_confopts = device_cfdata(self)->cf_flags;
    267 	isp->isp_role = ISP_DEFAULT_ROLES;
    268 
    269 	/*
    270 	 * There's no tool on sparc to set NVRAM for ISPs, so ignore it.
    271 	 */
    272 	isp->isp_confopts |= ISP_CFG_NONVRAM;
    273 	ISP_LOCK(isp);
    274 	isp->isp_osinfo.no_mbox_ints = 1;
    275 	isp_reset(isp);
    276 	if (isp->isp_state != ISP_RESETSTATE) {
    277 		ISP_UNLOCK(isp);
    278 		return;
    279 	}
    280 	ENABLE_INTS(isp);
    281 	isp_init(isp);
    282 	if (isp->isp_state != ISP_INITSTATE) {
    283 		isp_uninit(isp);
    284 		ISP_UNLOCK(isp);
    285 		return;
    286 	}
    287 
    288 	/*
    289 	 * do generic attach.
    290 	 */
    291 	ISP_UNLOCK(isp);
    292 	isp_attach(isp);
    293 	if (isp->isp_state != ISP_RUNSTATE) {
    294 		ISP_LOCK(isp);
    295 		isp_uninit(isp);
    296 		ISP_UNLOCK(isp);
    297 	}
    298 }
    299 
    300 
    301 static void
    302 isp_sbus_reset1(struct ispsoftc *isp)
    303 {
    304 	if (isp->isp_osinfo.no_mbox_ints == 0) {
    305 		ENABLE_INTS(isp);
    306 	}
    307 
    308 }
    309 
    310 static int
    311 isp_sbus_intr(void *arg)
    312 {
    313 	u_int16_t isr, sema, mbox;
    314 	struct ispsoftc *isp = arg;
    315 
    316 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
    317 		isp->isp_intbogus++;
    318 		return (0);
    319 	} else {
    320 		struct isp_sbussoftc *sbc = arg;
    321 		sbc->sbus_isp.isp_osinfo.onintstack = 1;
    322 		isp_intr(isp, isr, sema, mbox);
    323 		sbc->sbus_isp.isp_osinfo.onintstack = 0;
    324 		return (1);
    325 	}
    326 }
    327 
    328 #define	IspVirt2Off(a, x)	\
    329 	(((struct isp_sbussoftc *)a)->sbus_poff[((x) & _BLK_REG_MASK) >> \
    330 	_BLK_REG_SHFT] + ((x) & 0xff))
    331 
    332 #define	BXR2(sbc, off)		\
    333 	bus_space_read_2(sbc->sbus_bustag, sbc->sbus_reg, off)
    334 
    335 static int
    336 isp_sbus_rd_isr(struct ispsoftc *isp, u_int16_t *isrp,
    337     u_int16_t *semap, u_int16_t *mbp)
    338 {
    339 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    340 	u_int16_t isr, sema;
    341 
    342 	isr = BXR2(sbc, IspVirt2Off(isp, BIU_ISR));
    343 	sema = BXR2(sbc, IspVirt2Off(isp, BIU_SEMA));
    344 	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
    345 	isr &= INT_PENDING_MASK(isp);
    346 	sema &= BIU_SEMA_LOCK;
    347 	if (isr == 0 && sema == 0) {
    348 		return (0);
    349 	}
    350 	*isrp = isr;
    351 	if ((*semap = sema) != 0) {
    352 		*mbp = BXR2(sbc, IspVirt2Off(isp, OUTMAILBOX0));
    353 	}
    354 	return (1);
    355 }
    356 
    357 static u_int16_t
    358 isp_sbus_rd_reg(struct ispsoftc *isp, int regoff)
    359 {
    360 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    361 	int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
    362 	offset += (regoff & 0xff);
    363 	return (bus_space_read_2(sbc->sbus_bustag, sbc->sbus_reg, offset));
    364 }
    365 
    366 static void
    367 isp_sbus_wr_reg(struct ispsoftc *isp, int regoff, u_int16_t val)
    368 {
    369 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    370 	int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
    371 	offset += (regoff & 0xff);
    372 	bus_space_write_2(sbc->sbus_bustag, sbc->sbus_reg, offset, val);
    373 }
    374 
    375 static int
    376 isp_sbus_mbxdma(struct ispsoftc *isp)
    377 {
    378 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    379 	bus_dma_segment_t reqseg, rspseg;
    380 	int reqrs, rsprs, i, progress;
    381 	size_t n;
    382 	bus_size_t len;
    383 
    384 	if (isp->isp_rquest_dma)
    385 		return (0);
    386 
    387 	n = isp->isp_maxcmds * sizeof (XS_T *);
    388 	isp->isp_xflist = (XS_T **) malloc(n, M_DEVBUF, M_WAITOK);
    389 	if (isp->isp_xflist == NULL) {
    390 		isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
    391 		return (1);
    392 	}
    393 	MEMZERO(isp->isp_xflist, n);
    394 	n = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
    395 	sbc->sbus_dmamap = (bus_dmamap_t *) malloc(n, M_DEVBUF, M_WAITOK);
    396 	if (sbc->sbus_dmamap == NULL) {
    397 		free(isp->isp_xflist, M_DEVBUF);
    398 		isp->isp_xflist = NULL;
    399 		isp_prt(isp, ISP_LOGERR, "cannot alloc dmamap array");
    400 		return (1);
    401 	}
    402 	for (i = 0; i < isp->isp_maxcmds; i++) {
    403 		/* Allocate a DMA handle */
    404 		if (bus_dmamap_create(isp->isp_dmatag, MAXPHYS, 1, MAXPHYS, 0,
    405 		    BUS_DMA_NOWAIT, &sbc->sbus_dmamap[i]) != 0) {
    406 			isp_prt(isp, ISP_LOGERR, "cmd DMA maps create error");
    407 			break;
    408 		}
    409 	}
    410 	if (i < isp->isp_maxcmds) {
    411 		while (--i >= 0) {
    412 			bus_dmamap_destroy(isp->isp_dmatag,
    413 			    sbc->sbus_dmamap[i]);
    414 		}
    415 		free(isp->isp_xflist, M_DEVBUF);
    416 		free(sbc->sbus_dmamap, M_DEVBUF);
    417 		isp->isp_xflist = NULL;
    418 		sbc->sbus_dmamap = NULL;
    419 		return (1);
    420 	}
    421 
    422 	/*
    423 	 * Allocate and map the request and response queues
    424 	 */
    425 	progress = 0;
    426 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
    427 	if (bus_dmamem_alloc(isp->isp_dmatag, len, 0, 0, &reqseg, 1, &reqrs,
    428 	    BUS_DMA_NOWAIT)) {
    429 		goto dmafail;
    430 	}
    431 	progress++;
    432 	if (bus_dmamem_map(isp->isp_dmatag, &reqseg, reqrs, len,
    433 	    (void **)&isp->isp_rquest, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    434 		goto dmafail;
    435 	}
    436 	progress++;
    437 	if (bus_dmamap_create(isp->isp_dmatag, len, 1, len, 0, BUS_DMA_NOWAIT,
    438 	    &isp->isp_rqdmap) != 0) {
    439 		goto dmafail;
    440 	}
    441 	progress++;
    442 	if (bus_dmamap_load(isp->isp_dmatag, isp->isp_rqdmap,
    443 	    isp->isp_rquest, len, NULL, BUS_DMA_NOWAIT) != 0) {
    444 		goto dmafail;
    445 	}
    446 	progress++;
    447 	isp->isp_rquest_dma = isp->isp_rqdmap->dm_segs[0].ds_addr;
    448 
    449 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
    450 	if (bus_dmamem_alloc(isp->isp_dmatag, len, 0, 0, &rspseg, 1, &rsprs,
    451 	    BUS_DMA_NOWAIT)) {
    452 		goto dmafail;
    453 	}
    454 	progress++;
    455 	if (bus_dmamem_map(isp->isp_dmatag, &rspseg, rsprs, len,
    456 	    (void **)&isp->isp_result, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    457 		goto dmafail;
    458 	}
    459 	progress++;
    460 	if (bus_dmamap_create(isp->isp_dmatag, len, 1, len, 0, BUS_DMA_NOWAIT,
    461 	    &isp->isp_rsdmap) != 0) {
    462 		goto dmafail;
    463 	}
    464 	progress++;
    465 	if (bus_dmamap_load(isp->isp_dmatag, isp->isp_rsdmap,
    466 	    isp->isp_result, len, NULL, BUS_DMA_NOWAIT) != 0) {
    467 		goto dmafail;
    468 	}
    469 	isp->isp_result_dma = isp->isp_rsdmap->dm_segs[0].ds_addr;
    470 
    471 	return (0);
    472 
    473 dmafail:
    474 	isp_prt(isp, ISP_LOGERR, "Mailbox DMA Setup Failure");
    475 
    476 	if (progress >= 8) {
    477 		bus_dmamap_unload(isp->isp_dmatag, isp->isp_rsdmap);
    478 	}
    479 	if (progress >= 7) {
    480 		bus_dmamap_destroy(isp->isp_dmatag, isp->isp_rsdmap);
    481 	}
    482 	if (progress >= 6) {
    483 		bus_dmamem_unmap(isp->isp_dmatag,
    484 		    isp->isp_result, ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp)));
    485 	}
    486 	if (progress >= 5) {
    487 		bus_dmamem_free(isp->isp_dmatag, &rspseg, rsprs);
    488 	}
    489 
    490 	if (progress >= 4) {
    491 		bus_dmamap_unload(isp->isp_dmatag, isp->isp_rqdmap);
    492 	}
    493 	if (progress >= 3) {
    494 		bus_dmamap_destroy(isp->isp_dmatag, isp->isp_rqdmap);
    495 	}
    496 	if (progress >= 2) {
    497 		bus_dmamem_unmap(isp->isp_dmatag,
    498 		    isp->isp_rquest, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)));
    499 	}
    500 	if (progress >= 1) {
    501 		bus_dmamem_free(isp->isp_dmatag, &reqseg, reqrs);
    502 	}
    503 
    504 	for (i = 0; i < isp->isp_maxcmds; i++) {
    505 		bus_dmamap_destroy(isp->isp_dmatag, sbc->sbus_dmamap[i]);
    506 	}
    507 	free(sbc->sbus_dmamap, M_DEVBUF);
    508 	free(isp->isp_xflist, M_DEVBUF);
    509 	isp->isp_xflist = NULL;
    510 	sbc->sbus_dmamap = NULL;
    511 	return (1);
    512 }
    513 
    514 /*
    515  * Map a DMA request.
    516  * We're guaranteed that rq->req_handle is a value from 1 to isp->isp_maxcmds.
    517  */
    518 
    519 static int
    520 isp_sbus_dmasetup(struct ispsoftc *isp, XS_T *xs, ispreq_t *rq,
    521     u_int16_t *nxtip, u_int16_t optr)
    522 {
    523 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    524 	bus_dmamap_t dmap;
    525 	ispreq_t *qep;
    526 	int error, cansleep = (xs->xs_control & XS_CTL_NOSLEEP) == 0;
    527 	int in = (xs->xs_control & XS_CTL_DATA_IN) != 0;
    528 
    529 	qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, isp->isp_reqidx);
    530 	if (xs->datalen == 0) {
    531 		rq->req_seg_count = 1;
    532 		goto mbxsync;
    533 	}
    534 
    535 	dmap = sbc->sbus_dmamap[isp_handle_index(rq->req_handle)];
    536 	if (dmap->dm_nsegs != 0) {
    537 		panic("%s: DMA map already allocated", isp->isp_name);
    538 		/* NOTREACHED */
    539 	}
    540 	error = bus_dmamap_load(isp->isp_dmatag, dmap, xs->data, xs->datalen,
    541 	    NULL, (cansleep ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT) |
    542 	    BUS_DMA_STREAMING);
    543 	if (error != 0) {
    544 		XS_SETERR(xs, HBA_BOTCH);
    545 		if (error == EAGAIN || error == ENOMEM)
    546 			return (CMD_EAGAIN);
    547 		else
    548 			return (CMD_COMPLETE);
    549 	}
    550 
    551 	bus_dmamap_sync(isp->isp_dmatag, dmap, 0, xs->datalen,
    552 	    in? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    553 
    554 	if (in) {
    555 		rq->req_flags |= REQFLAG_DATA_IN;
    556 	} else {
    557 		rq->req_flags |= REQFLAG_DATA_OUT;
    558 	}
    559 
    560 	if (XS_CDBLEN(xs) > 12) {
    561 		u_int16_t onxti;
    562 		ispcontreq_t local, *crq = &local, *cqe;
    563 
    564 		onxti = *nxtip;
    565 		cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, onxti);
    566 		*nxtip = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
    567 		if (*nxtip == optr) {
    568 			isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
    569 			bus_dmamap_unload(isp->isp_dmatag, dmap);
    570 			XS_SETERR(xs, HBA_BOTCH);
    571 			return (CMD_EAGAIN);
    572 		}
    573 		rq->req_seg_count = 2;
    574 		MEMZERO((void *)crq, sizeof (*crq));
    575 		crq->req_header.rqs_entry_count = 1;
    576 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
    577 		crq->req_dataseg[0].ds_count = xs->datalen;
    578 		crq->req_dataseg[0].ds_base = dmap->dm_segs[0].ds_addr;
    579 		isp_put_cont_req(isp, crq, cqe);
    580 		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
    581 	} else {
    582 		rq->req_seg_count = 1;
    583 		rq->req_dataseg[0].ds_count = xs->datalen;
    584 		rq->req_dataseg[0].ds_base = dmap->dm_segs[0].ds_addr;
    585 	}
    586 
    587 mbxsync:
    588 	if (XS_CDBLEN(xs) > 12) {
    589 		isp_put_extended_request(isp,
    590 		    (ispextreq_t *)rq, (ispextreq_t *) qep);
    591 	} else {
    592 		isp_put_request(isp, rq, qep);
    593 	}
    594 	return (CMD_QUEUED);
    595 }
    596 
    597 static void
    598 isp_sbus_dmateardown(struct ispsoftc *isp, XS_T *xs, u_int16_t handle)
    599 {
    600 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    601 	bus_dmamap_t dmap;
    602 
    603 	dmap = sbc->sbus_dmamap[isp_handle_index(handle)];
    604 
    605 	if (dmap->dm_nsegs == 0) {
    606 		panic("%s: DMA map not already allocated", isp->isp_name);
    607 		/* NOTREACHED */
    608 	}
    609 	bus_dmamap_sync(isp->isp_dmatag, dmap, 0,
    610 	    xs->datalen, (xs->xs_control & XS_CTL_DATA_IN)?
    611 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    612 	bus_dmamap_unload(isp->isp_dmatag, dmap);
    613 }
    614