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