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