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