Home | History | Annotate | Line # | Download | only in sbus
isp_sbus.c revision 1.17
      1 /* $NetBSD: isp_sbus.c,v 1.17 1999/12/04 02:56:30 mjacob 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/microcode/isp/asm_sbus.h>
     49 #include <dev/sbus/sbusvar.h>
     50 
     51 static u_int16_t isp_sbus_rd_reg __P((struct ispsoftc *, int));
     52 static void isp_sbus_wr_reg __P((struct ispsoftc *, int, u_int16_t));
     53 static int isp_sbus_mbxdma __P((struct ispsoftc *));
     54 static int isp_sbus_dmasetup __P((struct ispsoftc *, struct scsipi_xfer *,
     55 	ispreq_t *, u_int16_t *, u_int16_t));
     56 static void isp_sbus_dmateardown __P((struct ispsoftc *, struct scsipi_xfer *,
     57 	u_int32_t));
     58 
     59 #ifndef	ISP_1000_RISC_CODE
     60 #define	ISP_1000_RISC_CODE	NULL
     61 #endif
     62 #ifndef	ISP_CODE_ORG
     63 #define	ISP_CODE_ORG	0x1000
     64 #endif
     65 
     66 static struct ispmdvec mdvec = {
     67 	isp_sbus_rd_reg,
     68 	isp_sbus_wr_reg,
     69 	isp_sbus_mbxdma,
     70 	isp_sbus_dmasetup,
     71 	isp_sbus_dmateardown,
     72 	NULL,
     73 	NULL,
     74 	NULL,
     75 	ISP_1000_RISC_CODE, 0, ISP_CODE_ORG, 0,
     76 	BIU_BURST_ENABLE
     77 };
     78 
     79 struct isp_sbussoftc {
     80 	struct ispsoftc	sbus_isp;
     81 	sdparam		sbus_dev;
     82 	bus_space_tag_t	sbus_bustag;
     83 	bus_dma_tag_t	sbus_dmatag;
     84 	bus_space_handle_t sbus_reg;
     85 	int		sbus_node;
     86 	int		sbus_pri;
     87 	struct ispmdvec	sbus_mdvec;
     88 	bus_dmamap_t	*sbus_dmamap;
     89 	int16_t		sbus_poff[_NREG_BLKS];
     90 };
     91 
     92 
     93 static int isp_match __P((struct device *, struct cfdata *, void *));
     94 static void isp_sbus_attach __P((struct device *, struct device *, void *));
     95 struct cfattach isp_sbus_ca = {
     96 	sizeof (struct isp_sbussoftc), isp_match, isp_sbus_attach
     97 };
     98 
     99 static int
    100 isp_match(parent, cf, aux)
    101         struct device *parent;
    102         struct cfdata *cf;
    103         void *aux;
    104 {
    105 	int rv;
    106 #ifdef DEBUG
    107 	static int oneshot = 1;
    108 #endif
    109 	struct sbus_attach_args *sa = aux;
    110 
    111 	rv = (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
    112 		strcmp("PTI,ptisp", sa->sa_name) == 0 ||
    113 		strcmp("ptisp", sa->sa_name) == 0 ||
    114 		strcmp("SUNW,isp", sa->sa_name) == 0 ||
    115 		strcmp("QLGC,isp", sa->sa_name) == 0);
    116 #ifdef DEBUG
    117 	if (rv && oneshot) {
    118 		oneshot = 0;
    119 		printf("Qlogic ISP Driver, NetBSD (sbus) Platform Version "
    120 		    "%d.%d Core Version %d.%d\n",
    121 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
    122 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
    123 	}
    124 #endif
    125 	return (rv);
    126 }
    127 
    128 static void
    129 isp_sbus_attach(parent, self, aux)
    130         struct device *parent, *self;
    131         void *aux;
    132 {
    133 	int i, freq;
    134 	struct sbus_attach_args *sa = aux;
    135 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) self;
    136 	struct ispsoftc *isp = &sbc->sbus_isp;
    137 	ISP_LOCKVAL_DECL;
    138 
    139 	printf(" for %s\n", sa->sa_name);
    140 
    141 	sbc->sbus_bustag = sa->sa_bustag;
    142 	sbc->sbus_dmatag = sa->sa_dmatag;
    143 	if (sa->sa_nintr != 0)
    144 		sbc->sbus_pri = sa->sa_pri;
    145 	sbc->sbus_mdvec = mdvec;
    146 
    147 	if (sa->sa_npromvaddrs != 0) {
    148 		sbc->sbus_reg = (bus_space_handle_t)sa->sa_promvaddrs[0];
    149 	} else {
    150 		if (sbus_bus_map(sa->sa_bustag, sa->sa_slot, sa->sa_offset,
    151 				 sa->sa_size, BUS_SPACE_MAP_LINEAR, 0,
    152 				 &sbc->sbus_reg) != 0) {
    153 			printf("%s: cannot map registers\n", self->dv_xname);
    154 			return;
    155 		}
    156 	}
    157 	sbc->sbus_node = sa->sa_node;
    158 
    159 	freq = getpropint(sa->sa_node, "clock-frequency", 0);
    160 	if (freq) {
    161 		/*
    162 		 * Convert from HZ to MHz, rounding up.
    163 		 */
    164 		freq = (freq + 500000)/1000000;
    165 #if	0
    166 		printf("%s: %d MHz\n", self->dv_xname, freq);
    167 #endif
    168 	}
    169 	sbc->sbus_mdvec.dv_clock = freq;
    170 
    171 	/*
    172 	 * XXX: Now figure out what the proper burst sizes, etc., to use.
    173 	 */
    174 	sbc->sbus_mdvec.dv_conf1 |= BIU_SBUS_CONF1_FIFO_8;
    175 
    176 	/*
    177 	 * Some early versions of the PTI SBus adapter
    178 	 * would fail in trying to download (via poking)
    179 	 * FW. We give up on them.
    180 	 */
    181 	if (strcmp("PTI,ptisp", sa->sa_name) == 0 ||
    182 	    strcmp("ptisp", sa->sa_name) == 0) {
    183 		sbc->sbus_mdvec.dv_fwlen = 0;
    184 	}
    185 
    186 	isp->isp_mdvec = &sbc->sbus_mdvec;
    187 	isp->isp_bustype = ISP_BT_SBUS;
    188 	isp->isp_type = ISP_HA_SCSI_UNKNOWN;
    189 	isp->isp_param = &sbc->sbus_dev;
    190 	bzero(isp->isp_param, sizeof (sdparam));
    191 
    192 	sbc->sbus_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
    193 	sbc->sbus_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = SBUS_MBOX_REGS_OFF;
    194 	sbc->sbus_poff[SXP_BLOCK >> _BLK_REG_SHFT] = SBUS_SXP_REGS_OFF;
    195 	sbc->sbus_poff[RISC_BLOCK >> _BLK_REG_SHFT] = SBUS_RISC_REGS_OFF;
    196 	sbc->sbus_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
    197 
    198 	ISP_LOCK(isp);
    199 	isp_reset(isp);
    200 	if (isp->isp_state != ISP_RESETSTATE) {
    201 		ISP_UNLOCK(isp);
    202 		return;
    203 	}
    204 	isp_init(isp);
    205 	if (isp->isp_state != ISP_INITSTATE) {
    206 		isp_uninit(isp);
    207 		ISP_UNLOCK(isp);
    208 		return;
    209 	}
    210 
    211 	for (i = 0; i < isp->isp_maxcmds; i++) {
    212 		/* Allocate a DMA handle */
    213 		if (bus_dmamap_create(sbc->sbus_dmatag, MAXPHYS, 1, MAXPHYS, 0,
    214 		    BUS_DMA_NOWAIT, &sbc->sbus_dmamap[i]) != 0) {
    215 			printf("%s: DMA map create error\n",
    216 				self->dv_xname);
    217 			return;
    218 		}
    219 	}
    220 
    221 	/* Establish interrupt channel */
    222 	bus_intr_establish(sbc->sbus_bustag, sbc->sbus_pri, 0,
    223 	    (int(*)__P((void*)))isp_intr, sbc);
    224 
    225 	/*
    226 	 * do generic attach.
    227 	 */
    228 	isp_attach(isp);
    229 	if (isp->isp_state != ISP_RUNSTATE) {
    230 		isp_uninit(isp);
    231 	}
    232 	ISP_UNLOCK(isp);
    233 }
    234 
    235 static u_int16_t
    236 isp_sbus_rd_reg(isp, regoff)
    237 	struct ispsoftc *isp;
    238 	int regoff;
    239 {
    240 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    241 	int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
    242 	offset += (regoff & 0xff);
    243 	return (bus_space_read_2(sbc->sbus_bustag, sbc->sbus_reg, offset));
    244 }
    245 
    246 static void
    247 isp_sbus_wr_reg(isp, regoff, val)
    248 	struct ispsoftc *isp;
    249 	int regoff;
    250 	u_int16_t val;
    251 {
    252 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    253 	int offset = sbc->sbus_poff[(regoff & _BLK_REG_MASK) >> _BLK_REG_SHFT];
    254 	offset += (regoff & 0xff);
    255 	bus_space_write_2(sbc->sbus_bustag, sbc->sbus_reg, offset, val);
    256 }
    257 
    258 static int
    259 isp_sbus_mbxdma(isp)
    260 	struct ispsoftc *isp;
    261 {
    262 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    263 	bus_dma_segment_t seg;
    264 	int rseg;
    265 	size_t n;
    266 	bus_size_t len;
    267 
    268 	if (isp->isp_rquest_dma)
    269 		return (0);
    270 
    271 	n = sizeof (ISP_SCSI_XFER_T **) * isp->isp_maxcmds;
    272 	isp->isp_xflist = (ISP_SCSI_XFER_T **) malloc(n, M_DEVBUF, M_WAITOK);
    273 	if (isp->isp_xflist == NULL) {
    274 		printf("%s: cannot alloc xflist array\n", isp->isp_name);
    275 		return (1);
    276 	}
    277 	bzero(isp->isp_xflist, n);
    278 	n = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
    279 	sbc->sbus_dmamap = (bus_dmamap_t *) malloc(n, M_DEVBUF, M_WAITOK);
    280 	if (sbc->sbus_dmamap == NULL) {
    281 		free(isp->isp_xflist, M_DEVBUF);
    282 		printf("%s: cannot alloc dmamap array\n", isp->isp_name);
    283 		return (1);
    284 	}
    285 	/*
    286 	 * Allocate and map the request queue.
    287 	 */
    288 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN);
    289 	if (bus_dmamem_alloc(sbc->sbus_dmatag, len, NBPG, 0,
    290 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
    291 		free(sbc->sbus_dmamap, M_DEVBUF);
    292 		free(isp->isp_xflist, M_DEVBUF);
    293 		return (1);
    294 	}
    295 
    296 	if (bus_dmamem_map(sbc->sbus_dmatag, &seg, rseg, len,
    297 	    (caddr_t *)&isp->isp_rquest, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    298 		free(sbc->sbus_dmamap, M_DEVBUF);
    299 		free(isp->isp_xflist, M_DEVBUF);
    300 		return (1);
    301 	}
    302 	isp->isp_rquest_dma = seg.ds_addr;
    303 
    304 	/*
    305 	 * Allocate and map the result queue.
    306 	 */
    307 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN);
    308 	if (bus_dmamem_alloc(sbc->sbus_dmatag, len, NBPG, 0,
    309 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
    310 		free(sbc->sbus_dmamap, M_DEVBUF);
    311 		free(isp->isp_xflist, M_DEVBUF);
    312 		return (1);
    313 	}
    314 	if (bus_dmamem_map(sbc->sbus_dmatag, &seg, rseg, len,
    315 	    (caddr_t *)&isp->isp_result, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    316 		free(sbc->sbus_dmamap, M_DEVBUF);
    317 		free(isp->isp_xflist, M_DEVBUF);
    318 		return (1);
    319 	}
    320 	isp->isp_result_dma = seg.ds_addr;
    321 	return (0);
    322 }
    323 
    324 /*
    325  * Map a DMA request.
    326  * We're guaranteed that rq->req_handle is a value from 1 to isp->isp_maxcmds.
    327  */
    328 
    329 static int
    330 isp_sbus_dmasetup(isp, xs, rq, iptrp, optr)
    331 	struct ispsoftc *isp;
    332 	struct scsipi_xfer *xs;
    333 	ispreq_t *rq;
    334 	u_int16_t *iptrp;
    335 	u_int16_t optr;
    336 {
    337 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    338 	bus_dmamap_t dmamap;
    339 	int dosleep = (xs->xs_control & XS_CTL_NOSLEEP) != 0;
    340 	int in = (xs->xs_control & XS_CTL_DATA_IN) != 0;
    341 
    342 	if (xs->datalen == 0) {
    343 		rq->req_seg_count = 1;
    344 		goto mbxsync;
    345 	}
    346 	assert(rq->req_handle != 0 && rq->req_handle <= isp->isp_maxcmds);
    347 	dmamap = sbc->sbus_dmamap[rq->req_handle - 1];
    348 	if (dmamap->dm_nsegs != 0) {
    349 		panic("%s: dma map already allocated\n", isp->isp_name);
    350 		/* NOTREACHED */
    351 	}
    352 	if (bus_dmamap_load(sbc->sbus_dmatag, dmamap, xs->data, xs->datalen,
    353 	    NULL, dosleep ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT) != 0) {
    354 		XS_SETERR(xs, HBA_BOTCH);
    355 		return (CMD_COMPLETE);
    356 	}
    357 	bus_dmamap_sync(sbc->sbus_dmatag, dmamap, dmamap->dm_segs[0].ds_addr,
    358 	    xs->datalen, in? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    359 	if (in) {
    360 		rq->req_flags |= REQFLAG_DATA_IN;
    361 	} else {
    362 		rq->req_flags |= REQFLAG_DATA_OUT;
    363 	}
    364 	rq->req_dataseg[0].ds_count = xs->datalen;
    365 	rq->req_dataseg[0].ds_base =  dmamap->dm_segs[0].ds_addr;
    366 	rq->req_seg_count = 1;
    367 mbxsync:
    368         ISP_SWIZZLE_REQUEST(isp, rq);
    369 #if	0
    370 	/*
    371 	 * If we ever map cacheable memory, we need to do something like this.
    372 	 */
    373         bus_dmamap_sync(sbc->sbus_dmat, sbc->sbus_rquest_dmap, 0,
    374             sbc->sbus_rquest_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
    375 #endif
    376 	return (CMD_QUEUED);
    377 }
    378 
    379 static void
    380 isp_sbus_dmateardown(isp, xs, handle)
    381 	struct ispsoftc *isp;
    382 	struct scsipi_xfer *xs;
    383 	u_int32_t handle;
    384 {
    385 	struct isp_sbussoftc *sbc = (struct isp_sbussoftc *) isp;
    386 	bus_dmamap_t dmamap;
    387 	assert(handle != 0 && handle <= isp->isp_maxcmds);
    388 	dmamap = sbc->sbus_dmamap[handle-1];
    389 	if (dmamap->dm_nsegs == 0) {
    390 		panic("%s: dma map not already allocated\n", isp->isp_name);
    391 		/* NOTREACHED */
    392 	}
    393 	bus_dmamap_sync(sbc->sbus_dmatag, dmamap, dmamap->dm_segs[0].ds_addr,
    394 	    xs->datalen, (xs->xs_control & XS_CTL_DATA_IN)?
    395 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    396 	bus_dmamap_unload(sbc->sbus_dmatag, dmamap);
    397 }
    398