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