Home | History | Annotate | Line # | Download | only in pci
isp_pci.c revision 1.10
      1  1.10  mjacob /*	$NetBSD: isp_pci.c,v 1.10 1997/04/05 02:55:28 mjacob Exp $	*/
      2   1.1     cgd 
      3   1.1     cgd /*
      4   1.1     cgd  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
      5   1.1     cgd  *
      6   1.1     cgd  * Copyright (c) 1997 by Matthew Jacob
      7   1.1     cgd  * NASA AMES Research Center
      8   1.1     cgd  * All rights reserved.
      9   1.1     cgd  *
     10   1.1     cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1     cgd  * modification, are permitted provided that the following conditions
     12   1.1     cgd  * are met:
     13   1.1     cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1     cgd  *    notice immediately at the beginning of the file, without modification,
     15   1.1     cgd  *    this list of conditions, and the following disclaimer.
     16   1.1     cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1     cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1     cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1     cgd  * 3. The name of the author may not be used to endorse or promote products
     20   1.1     cgd  *    derived from this software without specific prior written permission.
     21   1.1     cgd  *
     22   1.1     cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     23   1.1     cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1     cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1     cgd  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     26   1.1     cgd  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1     cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1     cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1     cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1     cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1     cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1     cgd  * SUCH DAMAGE.
     33   1.1     cgd  */
     34   1.1     cgd 
     35   1.1     cgd #include <sys/param.h>
     36   1.1     cgd #include <sys/systm.h>
     37   1.1     cgd #include <sys/malloc.h>
     38   1.1     cgd #include <sys/kernel.h>
     39   1.1     cgd #include <sys/queue.h>
     40   1.1     cgd #include <sys/device.h>
     41   1.1     cgd #include <machine/bus.h>
     42   1.1     cgd #include <machine/intr.h>
     43   1.1     cgd #include <scsi/scsi_all.h>
     44   1.1     cgd #include <scsi/scsiconf.h>
     45   1.1     cgd #include <dev/pci/pcireg.h>
     46   1.1     cgd #include <dev/pci/pcivar.h>
     47   1.1     cgd #include <dev/pci/pcidevs.h>
     48   1.3     cgd #include <vm/vm.h>
     49   1.1     cgd 
     50   1.1     cgd #include <dev/ic/ispreg.h>
     51   1.1     cgd #include <dev/ic/ispvar.h>
     52   1.1     cgd #include <dev/ic/ispmbox.h>
     53   1.1     cgd #include <dev/microcode/isp/asm_pci.h>
     54   1.3     cgd 
     55   1.3     cgd #ifdef	__alpha__	/* XXX */
     56   1.1     cgd /* XXX XXX NEED REAL DMA MAPPING SUPPORT XXX XXX */
     57   1.1     cgd extern vm_offset_t alpha_XXX_dmamap(vm_offset_t);
     58   1.1     cgd #undef vtophys
     59   1.1     cgd #define	vtophys(va)	alpha_XXX_dmamap((vm_offset_t) va)
     60   1.1     cgd #endif
     61   1.1     cgd #define	KVTOPHYS(x)	vtophys(x)
     62   1.1     cgd 
     63   1.1     cgd 
     64   1.1     cgd static u_int16_t isp_pci_rd_reg __P((struct ispsoftc *, int));
     65   1.1     cgd static void isp_pci_wr_reg __P((struct ispsoftc *, int, u_int16_t));
     66   1.1     cgd static vm_offset_t
     67   1.1     cgd isp_pci_mbxdma __P((struct ispsoftc *, vm_offset_t, u_int32_t));
     68   1.1     cgd static int
     69   1.1     cgd isp_pci_dmasetup __P((struct ispsoftc *, struct scsi_xfer *, ispreq_t *,
     70   1.1     cgd 		      u_int8_t *, u_int8_t));
     71   1.1     cgd 
     72   1.1     cgd static void isp_pci_reset1 __P((struct ispsoftc *));
     73   1.1     cgd 
     74   1.1     cgd static struct ispmdvec mdvec = {
     75   1.1     cgd 	isp_pci_rd_reg,
     76   1.1     cgd 	isp_pci_wr_reg,
     77   1.1     cgd 	isp_pci_mbxdma,
     78   1.1     cgd 	isp_pci_dmasetup,
     79   1.1     cgd 	NULL,
     80   1.1     cgd 	NULL,
     81   1.1     cgd 	isp_pci_reset1,
     82   1.5     cgd 	ISP_RISC_CODE,
     83   1.1     cgd 	ISP_CODE_LENGTH,
     84   1.1     cgd 	ISP_CODE_ORG,
     85  1.10  mjacob 	BIU_PCI_CONF1_FIFO_16 | BIU_BURST_ENABLE,
     86  1.10  mjacob 	60	/* MAGIC- all known PCI card implementations are 60MHz */
     87   1.1     cgd };
     88   1.1     cgd 
     89   1.1     cgd #define	PCI_QLOGIC_ISP	\
     90   1.1     cgd 	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
     91   1.1     cgd 
     92   1.6     cgd #define IO_MAP_REG	0x10
     93   1.6     cgd #define MEM_MAP_REG	0x14
     94   1.6     cgd 
     95   1.7     cgd int isp_pci_prefer_io = 0;	/* 1 -> map via I/O (patchable data) */
     96   1.1     cgd 
     97   1.1     cgd 
     98   1.1     cgd #ifdef	__BROKEN_INDIRECT_CONFIG
     99   1.1     cgd static int isp_pci_probe __P((struct device *, void *, void *));
    100   1.1     cgd #else
    101   1.1     cgd static int isp_pci_probe __P((struct device *, struct cfdata *, void *));
    102   1.1     cgd #endif
    103   1.1     cgd static void isp_pci_attach __P((struct device *, struct device *, void *));
    104   1.1     cgd 
    105   1.1     cgd struct isp_pcisoftc {
    106   1.1     cgd 	struct ispsoftc		pci_isp;
    107   1.6     cgd 	bus_space_tag_t		pci_st;
    108   1.6     cgd 	bus_space_handle_t	pci_sh;
    109   1.1     cgd 	void *			pci_ih;
    110   1.1     cgd };
    111   1.1     cgd 
    112   1.1     cgd struct cfattach isp_pci_ca = {
    113   1.1     cgd 	sizeof (struct isp_pcisoftc), isp_pci_probe, isp_pci_attach
    114   1.1     cgd };
    115   1.1     cgd 
    116   1.1     cgd static int
    117   1.1     cgd isp_pci_probe(parent, match, aux)
    118   1.1     cgd         struct device *parent;
    119   1.1     cgd #ifdef	__BROKEN_INDIRECT_CONFIG
    120   1.1     cgd         void *match, *aux;
    121   1.1     cgd #else
    122   1.1     cgd         struct cfdata *match;
    123   1.1     cgd 	void *aux;
    124   1.1     cgd #endif
    125   1.1     cgd {
    126   1.1     cgd         struct pci_attach_args *pa = aux;
    127   1.1     cgd 
    128   1.1     cgd 	if (pa->pa_id == PCI_QLOGIC_ISP) {
    129   1.1     cgd 		return (1);
    130   1.1     cgd 	} else {
    131   1.1     cgd 		return (0);
    132   1.1     cgd 	}
    133   1.1     cgd }
    134   1.1     cgd 
    135   1.1     cgd 
    136   1.1     cgd static void
    137   1.1     cgd isp_pci_attach(parent, self, aux)
    138   1.1     cgd         struct device *parent, *self;
    139   1.1     cgd         void *aux;
    140   1.1     cgd {
    141   1.1     cgd 	struct pci_attach_args *pa = aux;
    142   1.1     cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) self;
    143   1.6     cgd 	bus_addr_t busbase;
    144   1.6     cgd 	bus_size_t bussize;
    145   1.6     cgd 	bus_space_tag_t st;
    146   1.6     cgd 	bus_space_handle_t sh;
    147   1.1     cgd 	pci_intr_handle_t ih;
    148   1.1     cgd 	const char *intrstr;
    149   1.1     cgd 
    150   1.6     cgd 	if (isp_pci_prefer_io) {
    151   1.6     cgd 		if (pci_io_find(pa->pa_pc, pa->pa_tag, IO_MAP_REG, &busbase,
    152   1.6     cgd 		    &bussize)) {
    153   1.6     cgd 			printf(": unable to find PCI I/O base\n");
    154   1.6     cgd 			return;
    155   1.6     cgd 		}
    156   1.6     cgd 		st = pa->pa_iot;
    157   1.6     cgd 	} else {
    158   1.6     cgd 		if (pci_mem_find(pa->pa_pc, pa->pa_tag, MEM_MAP_REG, &busbase,
    159   1.8     cgd 		    &bussize, NULL)) {
    160   1.6     cgd 			printf(": unable to find PCI memory base\n");
    161   1.6     cgd 			return;
    162   1.6     cgd 		}
    163   1.6     cgd 		st = pa->pa_memt;
    164   1.9     cgd 	}
    165   1.9     cgd 	if (bus_space_map(st, busbase, bussize, 0, &sh)) {
    166   1.9     cgd 		printf(": unable to map %s registers\n",
    167   1.9     cgd 		    isp_pci_prefer_io ? "I/O" : "memory");
    168   1.9     cgd 		return;
    169   1.1     cgd 	}
    170   1.1     cgd 	printf("\n");
    171   1.1     cgd 
    172   1.6     cgd 	pcs->pci_st = st;
    173   1.6     cgd 	pcs->pci_sh = sh;
    174   1.1     cgd 	pcs->pci_isp.isp_mdvec = &mdvec;
    175   1.1     cgd 	isp_reset(&pcs->pci_isp);
    176   1.1     cgd 	if (pcs->pci_isp.isp_state != ISP_RESETSTATE) {
    177   1.1     cgd 		return;
    178   1.1     cgd 	}
    179   1.1     cgd 	isp_init(&pcs->pci_isp);
    180   1.1     cgd 	if (pcs->pci_isp.isp_state != ISP_INITSTATE) {
    181   1.1     cgd 		isp_uninit(&pcs->pci_isp);
    182   1.1     cgd 		return;
    183   1.1     cgd 	}
    184   1.1     cgd 
    185   1.1     cgd 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    186   1.1     cgd 			 pa->pa_intrline, &ih)) {
    187   1.1     cgd 		printf("%s: couldn't map interrupt\n", pcs->pci_isp.isp_name);
    188   1.1     cgd 		isp_uninit(&pcs->pci_isp);
    189   1.1     cgd 		return;
    190   1.1     cgd 	}
    191   1.1     cgd 
    192   1.1     cgd 	intrstr = pci_intr_string(pa->pa_pc, ih);
    193   1.1     cgd 	if (intrstr == NULL)
    194   1.1     cgd 		intrstr = "<I dunno>";
    195   1.1     cgd 	pcs->pci_ih =
    196   1.1     cgd 	  pci_intr_establish(pa->pa_pc, ih, IPL_BIO, isp_intr, &pcs->pci_isp);
    197   1.1     cgd 	if (pcs->pci_ih == NULL) {
    198   1.1     cgd 		printf("%s: couldn't establish interrupt at %s\n",
    199   1.1     cgd 			pcs->pci_isp.isp_name, intrstr);
    200   1.1     cgd 		isp_uninit(&pcs->pci_isp);
    201   1.1     cgd 		return;
    202   1.1     cgd 	}
    203   1.1     cgd 	printf("%s: interrupting at %s\n", pcs->pci_isp.isp_name, intrstr);
    204   1.1     cgd 
    205   1.1     cgd 	/*
    206   1.1     cgd 	 * Do Generic attach now.
    207   1.1     cgd 	 */
    208   1.1     cgd 	isp_attach(&pcs->pci_isp);
    209   1.1     cgd 	if (pcs->pci_isp.isp_state != ISP_RUNSTATE) {
    210   1.1     cgd 		isp_uninit(&pcs->pci_isp);
    211   1.1     cgd 	}
    212   1.1     cgd }
    213   1.1     cgd 
    214   1.1     cgd #define  PCI_BIU_REGS_OFF		0x00
    215   1.1     cgd #define	 PCI_MBOX_REGS_OFF		0x70
    216   1.1     cgd #define	 PCI_SXP_REGS_OFF		0x80
    217   1.1     cgd #define	 PCI_RISC_REGS_OFF		0x80
    218   1.1     cgd 
    219   1.1     cgd static u_int16_t
    220   1.1     cgd isp_pci_rd_reg(isp, regoff)
    221   1.1     cgd 	struct ispsoftc *isp;
    222   1.1     cgd 	int regoff;
    223   1.1     cgd {
    224   1.1     cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    225   1.1     cgd 	int offset;
    226   1.1     cgd 	if ((regoff & BIU_BLOCK) != 0) {
    227   1.1     cgd 		offset = PCI_BIU_REGS_OFF;
    228   1.1     cgd 	} else if ((regoff & MBOX_BLOCK) != 0) {
    229   1.1     cgd 		offset = PCI_MBOX_REGS_OFF;
    230   1.1     cgd 	} else if ((regoff & SXP_BLOCK) != 0) {
    231   1.1     cgd 		offset = PCI_SXP_REGS_OFF;
    232   1.1     cgd 		/*
    233   1.1     cgd 		 * XXX
    234   1.1     cgd 		 */
    235   1.1     cgd 		panic("SXP Registers not accessible yet!");
    236   1.1     cgd 	} else {
    237   1.1     cgd 		offset = PCI_RISC_REGS_OFF;
    238   1.1     cgd 	}
    239   1.1     cgd 	regoff &= 0xff;
    240   1.1     cgd 	offset += regoff;
    241   1.6     cgd 	return bus_space_read_2(pcs->pci_st, pcs->pci_sh, offset);
    242   1.1     cgd }
    243   1.1     cgd 
    244   1.1     cgd static void
    245   1.1     cgd isp_pci_wr_reg(isp, regoff, val)
    246   1.1     cgd 	struct ispsoftc *isp;
    247   1.1     cgd 	int regoff;
    248   1.1     cgd 	u_int16_t val;
    249   1.1     cgd {
    250   1.1     cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    251   1.1     cgd 	int offset;
    252   1.1     cgd 	if ((regoff & BIU_BLOCK) != 0) {
    253   1.1     cgd 		offset = PCI_BIU_REGS_OFF;
    254   1.1     cgd 	} else if ((regoff & MBOX_BLOCK) != 0) {
    255   1.1     cgd 		offset = PCI_MBOX_REGS_OFF;
    256   1.1     cgd 	} else if ((regoff & SXP_BLOCK) != 0) {
    257   1.1     cgd 		offset = PCI_SXP_REGS_OFF;
    258   1.1     cgd 		/*
    259   1.1     cgd 		 * XXX
    260   1.1     cgd 		 */
    261   1.1     cgd 		panic("SXP Registers not accessible yet!");
    262   1.1     cgd 	} else {
    263   1.1     cgd 		offset = PCI_RISC_REGS_OFF;
    264   1.1     cgd 	}
    265   1.1     cgd 	regoff &= 0xff;
    266   1.1     cgd 	offset += regoff;
    267   1.6     cgd 	bus_space_write_2(pcs->pci_st, pcs->pci_sh, offset, val);
    268   1.1     cgd }
    269   1.1     cgd 
    270   1.1     cgd static vm_offset_t
    271   1.1     cgd isp_pci_mbxdma(isp, kva, len)
    272   1.1     cgd 	struct ispsoftc *isp;
    273   1.1     cgd 	vm_offset_t kva;
    274   1.1     cgd 	u_int32_t len;
    275   1.1     cgd {
    276   1.1     cgd 	vm_offset_t pg, start, s1;
    277   1.1     cgd 
    278   1.1     cgd 	start = KVTOPHYS(kva);
    279   1.1     cgd 
    280   1.1     cgd 	pg = kva + NBPG;
    281   1.1     cgd 	s1 = (start >> PGSHIFT) + 1;
    282   1.1     cgd 	len -= NBPG;
    283   1.1     cgd 
    284   1.1     cgd 	while ((int32_t)len > 0) {
    285   1.1     cgd 		if (s1 != (KVTOPHYS(pg) >> PGSHIFT)) {
    286   1.1     cgd 			printf("%s: mailboxes across noncontiguous pages\n",
    287   1.1     cgd 					isp->isp_name);
    288   1.1     cgd 			return ((vm_offset_t) 0);
    289   1.1     cgd 		}
    290   1.1     cgd 		len -= NBPG;
    291   1.1     cgd 		pg += NBPG;
    292   1.1     cgd 		s1++;
    293   1.1     cgd 	}
    294   1.1     cgd 	return (start);
    295   1.1     cgd }
    296   1.1     cgd 
    297   1.1     cgd /*
    298   1.1     cgd  * TODO: reduce the number of segments by
    299   1.1     cgd  *	cchecking for adjacent physical page.
    300   1.1     cgd  */
    301   1.1     cgd 
    302   1.1     cgd static int
    303   1.1     cgd isp_pci_dmasetup(isp, xs, rq, iptrp, optr)
    304   1.1     cgd 	struct ispsoftc *isp;
    305   1.1     cgd 	struct scsi_xfer *xs;
    306   1.1     cgd 	ispreq_t *rq;
    307   1.1     cgd 	u_int8_t *iptrp;
    308   1.1     cgd 	u_int8_t optr;
    309   1.1     cgd {
    310   1.1     cgd 	ispcontreq_t *crq;
    311   1.1     cgd 	unsigned long thiskv, nextkv;
    312   1.1     cgd 	int datalen, amt;
    313   1.1     cgd 
    314   1.1     cgd 	if (xs->datalen == 0) {
    315   1.1     cgd 		rq->req_seg_count = 1;
    316   1.1     cgd 		rq->req_flags |= REQFLAG_DATA_IN;
    317   1.1     cgd 		return (0);
    318   1.1     cgd 	}
    319   1.1     cgd 
    320   1.1     cgd 	if (xs->flags & SCSI_DATA_IN) {
    321   1.1     cgd 		rq->req_flags |= REQFLAG_DATA_IN;
    322   1.1     cgd 	} else {
    323   1.1     cgd 		rq->req_flags |= REQFLAG_DATA_OUT;
    324   1.1     cgd 	}
    325   1.1     cgd 	datalen = xs->datalen;
    326   1.1     cgd 	thiskv = (unsigned long) xs->data;
    327   1.1     cgd 
    328   1.1     cgd 	while (datalen && rq->req_seg_count < ISP_RQDSEG) {
    329   1.1     cgd 		nextkv = (thiskv + NBPG) & ~(NBPG-1);
    330   1.1     cgd 		amt = nextkv - thiskv;
    331   1.1     cgd 		if (amt > datalen)
    332   1.1     cgd 			amt = datalen;
    333   1.1     cgd 		rq->req_dataseg[rq->req_seg_count].ds_count = amt;
    334   1.1     cgd 		rq->req_dataseg[rq->req_seg_count].ds_base = KVTOPHYS(thiskv);
    335   1.1     cgd #if	0
    336   1.1     cgd 		printf("%s: seg%d: 0x%lx..0x%lx\n", isp->isp_name,
    337   1.1     cgd 		       rq->req_seg_count, thiskv,
    338   1.1     cgd 		       thiskv + (unsigned long) amt);
    339   1.1     cgd #endif
    340   1.1     cgd 		datalen -= amt;
    341   1.1     cgd 		thiskv = nextkv;
    342   1.1     cgd 		rq->req_seg_count++;
    343   1.1     cgd 	}
    344   1.1     cgd 
    345   1.1     cgd 	if (datalen == 0) {
    346   1.1     cgd 		return (0);
    347   1.1     cgd 	}
    348   1.1     cgd 
    349   1.1     cgd 	do {
    350   1.1     cgd 		int seg;
    351   1.1     cgd 		crq = (ispcontreq_t *) &isp->isp_rquest[*iptrp][0];
    352   1.1     cgd 		*iptrp = (*iptrp + 1) & (RQUEST_QUEUE_LEN - 1);
    353   1.1     cgd 		if (*iptrp == optr) {
    354   1.1     cgd 			printf("%s: Request Queue Overflow++\n",
    355   1.1     cgd 			       isp->isp_name);
    356   1.1     cgd 			return (1);
    357   1.1     cgd 		}
    358   1.1     cgd 		rq->req_header.rqs_entry_count++;
    359   1.1     cgd 		bzero((void *)crq, sizeof (*crq));
    360   1.1     cgd 		crq->req_header.rqs_entry_count = 1;
    361   1.1     cgd 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
    362   1.1     cgd 		seg = 0;
    363   1.1     cgd 		while (datalen && seg < ISP_CDSEG) {
    364   1.1     cgd 			nextkv = (thiskv + NBPG) & ~(NBPG-1);
    365   1.1     cgd 			amt = nextkv - thiskv;
    366   1.1     cgd 			if (amt > datalen)
    367   1.1     cgd 				amt = datalen;
    368   1.1     cgd 			crq->req_dataseg[seg].ds_count = amt;
    369   1.1     cgd 			crq->req_dataseg[seg].ds_base = KVTOPHYS(thiskv);
    370   1.1     cgd #if	0
    371   1.1     cgd 			printf("%s: Cont%d seg%d: 0x%lx..0x%lx\n",
    372   1.1     cgd 			      isp->isp_name, rq->req_header.rqs_entry_count,
    373   1.1     cgd 			       seg, thiskv, thiskv + (unsigned long) amt);
    374   1.1     cgd #endif
    375   1.1     cgd 			datalen -= amt;
    376   1.1     cgd 			thiskv = nextkv;
    377   1.1     cgd 			rq->req_seg_count++;
    378   1.1     cgd 			seg++;
    379   1.1     cgd 		}
    380   1.1     cgd 	} while (datalen > 0);
    381   1.1     cgd 	return (0);
    382   1.1     cgd }
    383   1.1     cgd 
    384   1.1     cgd static void
    385   1.1     cgd isp_pci_reset1(isp)
    386   1.1     cgd 	struct ispsoftc *isp;
    387   1.1     cgd {
    388   1.1     cgd 	/* Make sure the BIOS is disabled */
    389   1.1     cgd 	isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
    390   1.1     cgd }
    391