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