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