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