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