Home | History | Annotate | Line # | Download | only in pci
isp_pci.c revision 1.10
      1 /*	$NetBSD: isp_pci.c,v 1.10 1997/04/05 02:55:28 mjacob Exp $	*/
      2 
      3 /*
      4  * PCI 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 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/malloc.h>
     38 #include <sys/kernel.h>
     39 #include <sys/queue.h>
     40 #include <sys/device.h>
     41 #include <machine/bus.h>
     42 #include <machine/intr.h>
     43 #include <scsi/scsi_all.h>
     44 #include <scsi/scsiconf.h>
     45 #include <dev/pci/pcireg.h>
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/pcidevs.h>
     48 #include <vm/vm.h>
     49 
     50 #include <dev/ic/ispreg.h>
     51 #include <dev/ic/ispvar.h>
     52 #include <dev/ic/ispmbox.h>
     53 #include <dev/microcode/isp/asm_pci.h>
     54 
     55 #ifdef	__alpha__	/* XXX */
     56 /* XXX XXX NEED REAL DMA MAPPING SUPPORT XXX XXX */
     57 extern vm_offset_t alpha_XXX_dmamap(vm_offset_t);
     58 #undef vtophys
     59 #define	vtophys(va)	alpha_XXX_dmamap((vm_offset_t) va)
     60 #endif
     61 #define	KVTOPHYS(x)	vtophys(x)
     62 
     63 
     64 static u_int16_t isp_pci_rd_reg __P((struct ispsoftc *, int));
     65 static void isp_pci_wr_reg __P((struct ispsoftc *, int, u_int16_t));
     66 static vm_offset_t
     67 isp_pci_mbxdma __P((struct ispsoftc *, vm_offset_t, u_int32_t));
     68 static int
     69 isp_pci_dmasetup __P((struct ispsoftc *, struct scsi_xfer *, ispreq_t *,
     70 		      u_int8_t *, u_int8_t));
     71 
     72 static void isp_pci_reset1 __P((struct ispsoftc *));
     73 
     74 static struct ispmdvec mdvec = {
     75 	isp_pci_rd_reg,
     76 	isp_pci_wr_reg,
     77 	isp_pci_mbxdma,
     78 	isp_pci_dmasetup,
     79 	NULL,
     80 	NULL,
     81 	isp_pci_reset1,
     82 	ISP_RISC_CODE,
     83 	ISP_CODE_LENGTH,
     84 	ISP_CODE_ORG,
     85 	BIU_PCI_CONF1_FIFO_16 | BIU_BURST_ENABLE,
     86 	60	/* MAGIC- all known PCI card implementations are 60MHz */
     87 };
     88 
     89 #define	PCI_QLOGIC_ISP	\
     90 	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
     91 
     92 #define IO_MAP_REG	0x10
     93 #define MEM_MAP_REG	0x14
     94 
     95 int isp_pci_prefer_io = 0;	/* 1 -> map via I/O (patchable data) */
     96 
     97 
     98 #ifdef	__BROKEN_INDIRECT_CONFIG
     99 static int isp_pci_probe __P((struct device *, void *, void *));
    100 #else
    101 static int isp_pci_probe __P((struct device *, struct cfdata *, void *));
    102 #endif
    103 static void isp_pci_attach __P((struct device *, struct device *, void *));
    104 
    105 struct isp_pcisoftc {
    106 	struct ispsoftc		pci_isp;
    107 	bus_space_tag_t		pci_st;
    108 	bus_space_handle_t	pci_sh;
    109 	void *			pci_ih;
    110 };
    111 
    112 struct cfattach isp_pci_ca = {
    113 	sizeof (struct isp_pcisoftc), isp_pci_probe, isp_pci_attach
    114 };
    115 
    116 static int
    117 isp_pci_probe(parent, match, aux)
    118         struct device *parent;
    119 #ifdef	__BROKEN_INDIRECT_CONFIG
    120         void *match, *aux;
    121 #else
    122         struct cfdata *match;
    123 	void *aux;
    124 #endif
    125 {
    126         struct pci_attach_args *pa = aux;
    127 
    128 	if (pa->pa_id == PCI_QLOGIC_ISP) {
    129 		return (1);
    130 	} else {
    131 		return (0);
    132 	}
    133 }
    134 
    135 
    136 static void
    137 isp_pci_attach(parent, self, aux)
    138         struct device *parent, *self;
    139         void *aux;
    140 {
    141 	struct pci_attach_args *pa = aux;
    142 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) self;
    143 	bus_addr_t busbase;
    144 	bus_size_t bussize;
    145 	bus_space_tag_t st;
    146 	bus_space_handle_t sh;
    147 	pci_intr_handle_t ih;
    148 	const char *intrstr;
    149 
    150 	if (isp_pci_prefer_io) {
    151 		if (pci_io_find(pa->pa_pc, pa->pa_tag, IO_MAP_REG, &busbase,
    152 		    &bussize)) {
    153 			printf(": unable to find PCI I/O base\n");
    154 			return;
    155 		}
    156 		st = pa->pa_iot;
    157 	} else {
    158 		if (pci_mem_find(pa->pa_pc, pa->pa_tag, MEM_MAP_REG, &busbase,
    159 		    &bussize, NULL)) {
    160 			printf(": unable to find PCI memory base\n");
    161 			return;
    162 		}
    163 		st = pa->pa_memt;
    164 	}
    165 	if (bus_space_map(st, busbase, bussize, 0, &sh)) {
    166 		printf(": unable to map %s registers\n",
    167 		    isp_pci_prefer_io ? "I/O" : "memory");
    168 		return;
    169 	}
    170 	printf("\n");
    171 
    172 	pcs->pci_st = st;
    173 	pcs->pci_sh = sh;
    174 	pcs->pci_isp.isp_mdvec = &mdvec;
    175 	isp_reset(&pcs->pci_isp);
    176 	if (pcs->pci_isp.isp_state != ISP_RESETSTATE) {
    177 		return;
    178 	}
    179 	isp_init(&pcs->pci_isp);
    180 	if (pcs->pci_isp.isp_state != ISP_INITSTATE) {
    181 		isp_uninit(&pcs->pci_isp);
    182 		return;
    183 	}
    184 
    185 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    186 			 pa->pa_intrline, &ih)) {
    187 		printf("%s: couldn't map interrupt\n", pcs->pci_isp.isp_name);
    188 		isp_uninit(&pcs->pci_isp);
    189 		return;
    190 	}
    191 
    192 	intrstr = pci_intr_string(pa->pa_pc, ih);
    193 	if (intrstr == NULL)
    194 		intrstr = "<I dunno>";
    195 	pcs->pci_ih =
    196 	  pci_intr_establish(pa->pa_pc, ih, IPL_BIO, isp_intr, &pcs->pci_isp);
    197 	if (pcs->pci_ih == NULL) {
    198 		printf("%s: couldn't establish interrupt at %s\n",
    199 			pcs->pci_isp.isp_name, intrstr);
    200 		isp_uninit(&pcs->pci_isp);
    201 		return;
    202 	}
    203 	printf("%s: interrupting at %s\n", pcs->pci_isp.isp_name, intrstr);
    204 
    205 	/*
    206 	 * Do Generic attach now.
    207 	 */
    208 	isp_attach(&pcs->pci_isp);
    209 	if (pcs->pci_isp.isp_state != ISP_RUNSTATE) {
    210 		isp_uninit(&pcs->pci_isp);
    211 	}
    212 }
    213 
    214 #define  PCI_BIU_REGS_OFF		0x00
    215 #define	 PCI_MBOX_REGS_OFF		0x70
    216 #define	 PCI_SXP_REGS_OFF		0x80
    217 #define	 PCI_RISC_REGS_OFF		0x80
    218 
    219 static u_int16_t
    220 isp_pci_rd_reg(isp, regoff)
    221 	struct ispsoftc *isp;
    222 	int regoff;
    223 {
    224 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    225 	int offset;
    226 	if ((regoff & BIU_BLOCK) != 0) {
    227 		offset = PCI_BIU_REGS_OFF;
    228 	} else if ((regoff & MBOX_BLOCK) != 0) {
    229 		offset = PCI_MBOX_REGS_OFF;
    230 	} else if ((regoff & SXP_BLOCK) != 0) {
    231 		offset = PCI_SXP_REGS_OFF;
    232 		/*
    233 		 * XXX
    234 		 */
    235 		panic("SXP Registers not accessible yet!");
    236 	} else {
    237 		offset = PCI_RISC_REGS_OFF;
    238 	}
    239 	regoff &= 0xff;
    240 	offset += regoff;
    241 	return bus_space_read_2(pcs->pci_st, pcs->pci_sh, offset);
    242 }
    243 
    244 static void
    245 isp_pci_wr_reg(isp, regoff, val)
    246 	struct ispsoftc *isp;
    247 	int regoff;
    248 	u_int16_t val;
    249 {
    250 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    251 	int offset;
    252 	if ((regoff & BIU_BLOCK) != 0) {
    253 		offset = PCI_BIU_REGS_OFF;
    254 	} else if ((regoff & MBOX_BLOCK) != 0) {
    255 		offset = PCI_MBOX_REGS_OFF;
    256 	} else if ((regoff & SXP_BLOCK) != 0) {
    257 		offset = PCI_SXP_REGS_OFF;
    258 		/*
    259 		 * XXX
    260 		 */
    261 		panic("SXP Registers not accessible yet!");
    262 	} else {
    263 		offset = PCI_RISC_REGS_OFF;
    264 	}
    265 	regoff &= 0xff;
    266 	offset += regoff;
    267 	bus_space_write_2(pcs->pci_st, pcs->pci_sh, offset, val);
    268 }
    269 
    270 static vm_offset_t
    271 isp_pci_mbxdma(isp, kva, len)
    272 	struct ispsoftc *isp;
    273 	vm_offset_t kva;
    274 	u_int32_t len;
    275 {
    276 	vm_offset_t pg, start, s1;
    277 
    278 	start = KVTOPHYS(kva);
    279 
    280 	pg = kva + NBPG;
    281 	s1 = (start >> PGSHIFT) + 1;
    282 	len -= NBPG;
    283 
    284 	while ((int32_t)len > 0) {
    285 		if (s1 != (KVTOPHYS(pg) >> PGSHIFT)) {
    286 			printf("%s: mailboxes across noncontiguous pages\n",
    287 					isp->isp_name);
    288 			return ((vm_offset_t) 0);
    289 		}
    290 		len -= NBPG;
    291 		pg += NBPG;
    292 		s1++;
    293 	}
    294 	return (start);
    295 }
    296 
    297 /*
    298  * TODO: reduce the number of segments by
    299  *	cchecking for adjacent physical page.
    300  */
    301 
    302 static int
    303 isp_pci_dmasetup(isp, xs, rq, iptrp, optr)
    304 	struct ispsoftc *isp;
    305 	struct scsi_xfer *xs;
    306 	ispreq_t *rq;
    307 	u_int8_t *iptrp;
    308 	u_int8_t optr;
    309 {
    310 	ispcontreq_t *crq;
    311 	unsigned long thiskv, nextkv;
    312 	int datalen, amt;
    313 
    314 	if (xs->datalen == 0) {
    315 		rq->req_seg_count = 1;
    316 		rq->req_flags |= REQFLAG_DATA_IN;
    317 		return (0);
    318 	}
    319 
    320 	if (xs->flags & SCSI_DATA_IN) {
    321 		rq->req_flags |= REQFLAG_DATA_IN;
    322 	} else {
    323 		rq->req_flags |= REQFLAG_DATA_OUT;
    324 	}
    325 	datalen = xs->datalen;
    326 	thiskv = (unsigned long) xs->data;
    327 
    328 	while (datalen && rq->req_seg_count < ISP_RQDSEG) {
    329 		nextkv = (thiskv + NBPG) & ~(NBPG-1);
    330 		amt = nextkv - thiskv;
    331 		if (amt > datalen)
    332 			amt = datalen;
    333 		rq->req_dataseg[rq->req_seg_count].ds_count = amt;
    334 		rq->req_dataseg[rq->req_seg_count].ds_base = KVTOPHYS(thiskv);
    335 #if	0
    336 		printf("%s: seg%d: 0x%lx..0x%lx\n", isp->isp_name,
    337 		       rq->req_seg_count, thiskv,
    338 		       thiskv + (unsigned long) amt);
    339 #endif
    340 		datalen -= amt;
    341 		thiskv = nextkv;
    342 		rq->req_seg_count++;
    343 	}
    344 
    345 	if (datalen == 0) {
    346 		return (0);
    347 	}
    348 
    349 	do {
    350 		int seg;
    351 		crq = (ispcontreq_t *) &isp->isp_rquest[*iptrp][0];
    352 		*iptrp = (*iptrp + 1) & (RQUEST_QUEUE_LEN - 1);
    353 		if (*iptrp == optr) {
    354 			printf("%s: Request Queue Overflow++\n",
    355 			       isp->isp_name);
    356 			return (1);
    357 		}
    358 		rq->req_header.rqs_entry_count++;
    359 		bzero((void *)crq, sizeof (*crq));
    360 		crq->req_header.rqs_entry_count = 1;
    361 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
    362 		seg = 0;
    363 		while (datalen && seg < ISP_CDSEG) {
    364 			nextkv = (thiskv + NBPG) & ~(NBPG-1);
    365 			amt = nextkv - thiskv;
    366 			if (amt > datalen)
    367 				amt = datalen;
    368 			crq->req_dataseg[seg].ds_count = amt;
    369 			crq->req_dataseg[seg].ds_base = KVTOPHYS(thiskv);
    370 #if	0
    371 			printf("%s: Cont%d seg%d: 0x%lx..0x%lx\n",
    372 			      isp->isp_name, rq->req_header.rqs_entry_count,
    373 			       seg, thiskv, thiskv + (unsigned long) amt);
    374 #endif
    375 			datalen -= amt;
    376 			thiskv = nextkv;
    377 			rq->req_seg_count++;
    378 			seg++;
    379 		}
    380 	} while (datalen > 0);
    381 	return (0);
    382 }
    383 
    384 static void
    385 isp_pci_reset1(isp)
    386 	struct ispsoftc *isp;
    387 {
    388 	/* Make sure the BIOS is disabled */
    389 	isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
    390 }
    391