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