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