Home | History | Annotate | Line # | Download | only in sbus
isp_sbus.c revision 1.16
      1 /* $NetBSD: isp_sbus.c,v 1.16 1999/11/21 15:01:51 pk Exp $ */
      2 /*
      3  * SBus specific probe and attach routines for Qlogic ISP SCSI adapters.
      4  *
      5  * Copyright (c) 1997 by Matthew Jacob
      6  * NASA AMES Research Center
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice immediately at the beginning of the file, without modification,
     14  *    this list of conditions, and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/kernel.h>
     39 #include <sys/malloc.h>
     40 #include <sys/queue.h>
     41 
     42 #include <machine/autoconf.h>
     43 #include <machine/cpu.h>
     44 #include <machine/param.h>
     45 #include <machine/vmparam.h>
     46 
     47 #include <dev/ic/isp_netbsd.h>
     48 #include <dev/sbus/sbusvar.h>
     49 
     50 static u_int16_t isp_sbus_rd_reg __P((struct ispsoftc *, int));
     51 static void isp_sbus_wr_reg __P((struct ispsoftc *, int, u_int16_t));
     52 static int isp_sbus_mbxdma __P((struct ispsoftc *));
     53 static int isp_sbus_dmasetup __P((struct ispsoftc *, struct scsipi_xfer *,
     54 	ispreq_t *, u_int8_t *, u_int8_t));
     55 static void isp_sbus_dmateardown __P((struct ispsoftc *, struct scsipi_xfer *,
     56 	u_int32_t));
     57 
     58 static struct ispmdvec mdvec = {
     59 	isp_sbus_rd_reg,
     60 	isp_sbus_wr_reg,
     61 	isp_sbus_mbxdma,
     62 	isp_sbus_dmasetup,
     63 	isp_sbus_dmateardown,
     64 	NULL,
     65 	NULL,
     66 	NULL,
     67 	0,
     68 	0,
     69 	0,
     70 	0,
     71 	BIU_BURST_ENABLE,
     72 	0
     73 };
     74 
     75 struct isp_sbussoftc {
     76 	struct ispsoftc	sbus_isp;
     77 	sdparam		sbus_dev;
     78 	bus_space_tag_t	sbus_bustag;
     79 	bus_dma_tag_t	sbus_dmatag;
     80 	bus_space_handle_t sbus_reg;
     81 	int		sbus_node;
     82 	int		sbus_pri;
     83 	struct ispmdvec	sbus_mdvec;
     84 	bus_dmamap_t	*sbus_dmamap;
     85 	int16_t		sbus_poff[_NREG_BLKS];
     86 };
     87 
     88 
     89 static int isp_match __P((struct device *, struct cfdata *, void *));
     90 static void isp_sbus_attach __P((struct device *, struct device *, void *));
     91 struct cfattach isp_sbus_ca = {
     92 	sizeof (struct isp_sbussoftc), isp_match, isp_sbus_attach
     93 };
     94 
     95 static int
     96 isp_match(parent, cf, aux)
     97         struct device *parent;
     98         struct cfdata *cf;
     99         void *aux;
    100 {
    101 	int rv;
    102 #ifdef DEBUG
    103 	static int oneshot = 1;
    104 #endif
    105 	struct sbus_attach_args *sa = aux;
    106 
    107 	rv = (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
    108 		strcmp("PTI,ptisp", sa->sa_name) == 0 ||
    109 		strcmp("ptisp", sa->sa_name) == 0 ||
    110 		strcmp("SUNW,isp", sa->sa_name) == 0 ||
    111 		strcmp("QLGC,isp", sa->sa_name) == 0);
    112 #ifdef DEBUG
    113 	if (rv && oneshot) {
    114 		oneshot = 0;
    115 		printf("Qlogic ISP Driver, NetBSD (sbus) Platform Version "
    116 		    "%d.%d Core Version %d.%d\n",
    117 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
    118 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
    119 	}
    120 #endif
    121 	return (rv);
    122 }
    123 
    124 static void
    125 isp_sbus_attach(parent, self, aux)
    126         struct device *parent, *self;
    127         void *aux;
    128 {
    129 	int i, freq;
    130 	struct sbus_attach_args *sa = aux;
    131 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) self;
    132 	struct ispsoftc *isp = &sbc->sbus_isp;
    133 	ISP_LOCKVAL_DECL;
    134 
    135 	printf(" for %s\n", sa->sa_name);
    136 
    137 	sbc->sbus_bustag = sa->sa_bustag;
    138 	sbc->sbus_dmatag = sa->sa_dmatag;
    139 	if (sa->sa_nintr != 0)
    140 		sbc->sbus_pri = sa->sa_pri;
    141 	sbc->sbus_mdvec = mdvec;
    142 
    143 	if (sa->sa_npromvaddrs != 0) {
    144 		sbc->sbus_reg = (bus_space_handle_t)sa->sa_promvaddrs[0];
    145 	} else {
    146 		if (sbus_bus_map(sa->sa_bustag, sa->sa_slot, sa->sa_offset,
    147 				 sa->sa_size, BUS_SPACE_MAP_LINEAR, 0,
    148 				 &sbc->sbus_reg) != 0) {
    149 			printf("%s: cannot map registers\n", self->dv_xname);
    150 			return;
    151 		}
    152 	}
    153 	sbc->sbus_node = sa->sa_node;
    154 
    155 	freq = getpropint(sa->sa_node, "clock-frequency", 0);
    156 	if (freq) {
    157 		/*
    158 		 * Convert from HZ to MHz, rounding up.
    159 		 */
    160 		freq = (freq + 500000)/1000000;
    161 #if	0
    162 		printf("%s: %d MHz\n", self->dv_xname, freq);
    163 #endif
    164 	}
    165 	sbc->sbus_mdvec.dv_clock = freq;
    166 
    167 	/*
    168 	 * XXX: Now figure out what the proper burst sizes, etc., to use.
    169 	 */
    170 	sbc->sbus_mdvec.dv_conf1 |= BIU_SBUS_CONF1_FIFO_8;
    171 
    172 	/*
    173 	 * Some early versions of the PTI SBus adapter
    174 	 * would fail in trying to download (via poking)
    175 	 * FW. We give up on them.
    176 	 */
    177 	if (strcmp("PTI,ptisp", sa->sa_name) == 0 ||
    178 	    strcmp("ptisp", sa->sa_name) == 0) {
    179 		sbc->sbus_mdvec.dv_fwlen = 0;
    180 	}
    181 
    182 	isp->isp_mdvec = &sbc->sbus_mdvec;
    183 	isp->isp_bustype = ISP_BT_SBUS;
    184 	isp->isp_type = ISP_HA_SCSI_UNKNOWN;
    185 	isp->isp_param = &sbc->sbus_dev;
    186 	bzero(isp->isp_param, sizeof (sdparam));
    187 
    188 	sbc->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
    189 	sbc->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
    190 	sbc->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
    191 	sbc->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
    192 	sbc->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
    193 
    194 	ISP_LOCK(isp);
    195 	isp_reset(isp);
    196 	if (isp->isp_state != ISP_RESETSTATE) {
    197 		ISP_UNLOCK(isp);
    198 		return;
    199 	}
    200 	isp_init(isp);
    201 	if (isp->isp_state != ISP_INITSTATE) {
    202 		isp_uninit(isp);
    203 		ISP_UNLOCK(isp);
    204 		return;
    205 	}
    206 
    207 	for (i = 0; i < isp->isp_maxcmds; i++) {
    208 		/* Allocate a DMA handle */
    209 		if (bus_dmamap_create(sbc->sbus_dmatag, MAXPHYS, 1, MAXPHYS, 0,
    210 		    BUS_DMA_NOWAIT, &sbc->sbus_dmamap[i]) != 0) {
    211 			printf("%s: DMA map create error\n",
    212 				self->dv_xname);
    213 			return;
    214 		}
    215 	}
    216 
    217 	/* Establish interrupt channel */
    218 	bus_intr_establish(sbc->sbus_bustag, sbc->sbus_pri, 0,
    219 	    (int(*)__P((void*)))isp_intr, sbc);
    220 
    221 	/*
    222 	 * do generic attach.
    223 	 */
    224 	isp_attach(isp);
    225 	if (isp->isp_state != ISP_RUNSTATE) {
    226 		isp_uninit(isp);
    227 	}
    228 	ISP_UNLOCK(isp);
    229 }
    230 
    231 static u_int16_t
    232 isp_sbus_rd_reg(isp, regoff)
    233 	struct ispsoftc *isp;
    234 	int regoff;
    235 {
    236 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    237 	int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
    238 	offset += (regoff & 0xff);
    239 	return (bus_space_read_2(sbc->sbus_bustag, sbc->sbus_reg, offset));
    240 }
    241 
    242 static void
    243 isp_sbus_wr_reg(isp, regoff, val)
    244 	struct ispsoftc *isp;
    245 	int regoff;
    246 	u_int16_t val;
    247 {
    248 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    249 	int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
    250 	offset += (regoff & 0xff);
    251 	bus_space_write_2(sbc->sbus_bustag, sbc->sbus_reg, offset, val);
    252 }
    253 
    254 static int
    255 isp_sbus_mbxdma(isp)
    256 	struct ispsoftc *isp;
    257 {
    258 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    259 	bus_dma_segment_t seg;
    260 	int rseg;
    261 	size_t n;
    262 	bus_size_t len;
    263 
    264 	if (isp->isp_rquest_dma)
    265 		return (0);
    266 
    267 	n = sizeof (ISP_SCSI_XFER_T **) * isp->isp_maxcmds;
    268 	isp->isp_xflist = (ISP_SCSI_XFER_T **) malloc(n, M_DEVBUF, M_WAITOK);
    269 	if (isp->isp_xflist == NULL) {
    270 		printf("%s: cannot alloc xflist array\n", isp->isp_name);
    271 		return (1);
    272 	}
    273 	bzero(isp->isp_xflist, n);
    274 	n = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
    275 	sbc->sbus_dmamap = (bus_dmamap_t *) malloc(n, M_DEVBUF, M_WAITOK);
    276 	if (sbc->sbus_dmamap == NULL) {
    277 		free(isp->isp_xflist, M_DEVBUF);
    278 		printf("%s: cannot alloc dmamap array\n", isp->isp_name);
    279 		return (1);
    280 	}
    281 	/*
    282 	 * Allocate and map the request queue.
    283 	 */
    284 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN);
    285 	if (bus_dmamem_alloc(sbc->sbus_dmatag, len, NBPG, 0,
    286 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
    287 		free(sbc->sbus_dmamap, M_DEVBUF);
    288 		free(isp->isp_xflist, M_DEVBUF);
    289 		return (1);
    290 	}
    291 
    292 	if (bus_dmamem_map(sbc->sbus_dmatag, &seg, rseg, len,
    293 	    (caddr_t *)&isp->isp_rquest, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    294 		free(sbc->sbus_dmamap, M_DEVBUF);
    295 		free(isp->isp_xflist, M_DEVBUF);
    296 		return (1);
    297 	}
    298 	isp->isp_rquest_dma = seg.ds_addr;
    299 
    300 	/*
    301 	 * Allocate and map the result queue.
    302 	 */
    303 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN);
    304 	if (bus_dmamem_alloc(sbc->sbus_dmatag, len, NBPG, 0,
    305 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
    306 		free(sbc->sbus_dmamap, M_DEVBUF);
    307 		free(isp->isp_xflist, M_DEVBUF);
    308 		return (1);
    309 	}
    310 	if (bus_dmamem_map(sbc->sbus_dmatag, &seg, rseg, len,
    311 	    (caddr_t *)&isp->isp_result, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    312 		free(sbc->sbus_dmamap, M_DEVBUF);
    313 		free(isp->isp_xflist, M_DEVBUF);
    314 		return (1);
    315 	}
    316 	isp->isp_result_dma = seg.ds_addr;
    317 	return (0);
    318 }
    319 
    320 /*
    321  * Map a DMA request.
    322  * We're guaranteed that rq->req_handle is a value from 1 to isp->isp_maxcmds.
    323  */
    324 
    325 static int
    326 isp_sbus_dmasetup(isp, xs, rq, iptrp, optr)
    327 	struct ispsoftc *isp;
    328 	struct scsipi_xfer *xs;
    329 	ispreq_t *rq;
    330 	u_int8_t *iptrp;
    331 	u_int8_t optr;
    332 {
    333 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    334 	bus_dmamap_t dmamap;
    335 	int dosleep = (xs->xs_control & XS_CTL_NOSLEEP) != 0;
    336 	int in = (xs->xs_control & XS_CTL_DATA_IN) != 0;
    337 
    338 	if (xs->datalen == 0) {
    339 		rq->req_seg_count = 1;
    340 		goto mbxsync;
    341 	}
    342 	assert(rq->req_handle != 0 && rq->req_handle <= isp->isp_maxcmds);
    343 	dmamap = sbc->sbus_dmamap[rq->req_handle - 1];
    344 	if (dmamap->dm_nsegs != 0) {
    345 		panic("%s: dma map already allocated\n", isp->isp_name);
    346 		/* NOTREACHED */
    347 	}
    348 	if (bus_dmamap_load(sbc->sbus_dmatag, dmamap, xs->data, xs->datalen,
    349 	    NULL, dosleep ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT) != 0) {
    350 		XS_SETERR(xs, HBA_BOTCH);
    351 		return (CMD_COMPLETE);
    352 	}
    353 	bus_dmamap_sync(sbc->sbus_dmatag, dmamap, dmamap->dm_segs[0].ds_addr,
    354 	    xs->datalen, in? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    355 	if (in) {
    356 		rq->req_flags |= REQFLAG_DATA_IN;
    357 	} else {
    358 		rq->req_flags |= REQFLAG_DATA_OUT;
    359 	}
    360 	rq->req_dataseg[0].ds_count = xs->datalen;
    361 	rq->req_dataseg[0].ds_base =  dmamap->dm_segs[0].ds_addr;
    362 	rq->req_seg_count = 1;
    363 mbxsync:
    364         ISP_SWIZZLE_REQUEST(isp, rq);
    365 #if	0
    366 	/*
    367 	 * If we ever map cacheable memory, we need to do something like this.
    368 	 */
    369         bus_dmamap_sync(sbc->sbus_dmat, sbc->sbus_rquest_dmap, 0,
    370             sbc->sbus_rquest_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
    371 #endif
    372 	return (CMD_QUEUED);
    373 }
    374 
    375 static void
    376 isp_sbus_dmateardown(isp, xs, handle)
    377 	struct ispsoftc *isp;
    378 	struct scsipi_xfer *xs;
    379 	u_int32_t handle;
    380 {
    381 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    382 	bus_dmamap_t dmamap;
    383 	assert(handle != 0 && handle <= isp->isp_maxcmds);
    384 	dmamap = sbc->sbus_dmamap[handle-1];
    385 	if (dmamap->dm_nsegs == 0) {
    386 		panic("%s: dma map not already allocated\n", isp->isp_name);
    387 		/* NOTREACHED */
    388 	}
    389 	bus_dmamap_sync(sbc->sbus_dmatag, dmamap, dmamap->dm_segs[0].ds_addr,
    390 	    xs->datalen, (xs->xs_control & XS_CTL_DATA_IN)?
    391 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    392 	bus_dmamap_unload(sbc->sbus_dmatag, dmamap);
    393 }
    394