Home | History | Annotate | Line # | Download | only in pci
isp_pci.c revision 1.14.4.3
      1  1.14.4.3  thorpej /*	$NetBSD: isp_pci.c,v 1.14.4.3 1997/09/16 03:50:40 thorpej 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.14.4.2  thorpej #include <dev/scsipi/scsi_all.h>
     44  1.14.4.2  thorpej #include <dev/scsipi/scsipi_all.h>
     45  1.14.4.2  thorpej #include <dev/scsipi/scsiconf.h>
     46       1.1      cgd #include <dev/pci/pcireg.h>
     47       1.1      cgd #include <dev/pci/pcivar.h>
     48       1.1      cgd #include <dev/pci/pcidevs.h>
     49       1.3      cgd #include <vm/vm.h>
     50       1.1      cgd 
     51       1.1      cgd #include <dev/ic/ispreg.h>
     52       1.1      cgd #include <dev/ic/ispvar.h>
     53       1.1      cgd #include <dev/ic/ispmbox.h>
     54       1.1      cgd #include <dev/microcode/isp/asm_pci.h>
     55       1.3      cgd 
     56       1.1      cgd static u_int16_t isp_pci_rd_reg __P((struct ispsoftc *, int));
     57       1.1      cgd static void isp_pci_wr_reg __P((struct ispsoftc *, int, u_int16_t));
     58      1.13  thorpej static int isp_pci_mbxdma __P((struct ispsoftc *));
     59  1.14.4.2  thorpej static int isp_pci_dmasetup __P((struct ispsoftc *, struct scsipi_xfer *,
     60      1.13  thorpej 	ispreq_t *, u_int8_t *, u_int8_t));
     61  1.14.4.2  thorpej static void isp_pci_dmateardown __P((struct ispsoftc *, struct scsipi_xfer *,
     62      1.13  thorpej 	u_int32_t));
     63       1.1      cgd 
     64       1.1      cgd static void isp_pci_reset1 __P((struct ispsoftc *));
     65  1.14.4.1  thorpej static void isp_pci_dumpregs __P((struct ispsoftc *));
     66       1.1      cgd 
     67       1.1      cgd static struct ispmdvec mdvec = {
     68       1.1      cgd 	isp_pci_rd_reg,
     69       1.1      cgd 	isp_pci_wr_reg,
     70       1.1      cgd 	isp_pci_mbxdma,
     71       1.1      cgd 	isp_pci_dmasetup,
     72      1.13  thorpej 	isp_pci_dmateardown,
     73       1.1      cgd 	NULL,
     74       1.1      cgd 	isp_pci_reset1,
     75  1.14.4.1  thorpej 	isp_pci_dumpregs,
     76       1.5      cgd 	ISP_RISC_CODE,
     77       1.1      cgd 	ISP_CODE_LENGTH,
     78       1.1      cgd 	ISP_CODE_ORG,
     79  1.14.4.1  thorpej 	ISP_CODE_VERSION,
     80  1.14.4.1  thorpej 	BIU_PCI_CONF1_FIFO_64 | BIU_BURST_ENABLE,
     81  1.14.4.1  thorpej 	60	/* MAGIC- all known PCI card implementations are 60MHz */
     82  1.14.4.1  thorpej };
     83  1.14.4.1  thorpej 
     84  1.14.4.1  thorpej static struct ispmdvec mdvec_2100 = {
     85  1.14.4.1  thorpej 	isp_pci_rd_reg,
     86  1.14.4.1  thorpej 	isp_pci_wr_reg,
     87  1.14.4.1  thorpej 	isp_pci_mbxdma,
     88  1.14.4.1  thorpej 	isp_pci_dmasetup,
     89  1.14.4.1  thorpej 	isp_pci_dmateardown,
     90  1.14.4.1  thorpej 	NULL,
     91  1.14.4.1  thorpej 	isp_pci_reset1,
     92  1.14.4.1  thorpej 	isp_pci_dumpregs,
     93  1.14.4.1  thorpej 	ISP2100_RISC_CODE,
     94  1.14.4.1  thorpej 	ISP2100_CODE_LENGTH,
     95  1.14.4.1  thorpej 	ISP2100_CODE_ORG,
     96  1.14.4.1  thorpej 	ISP2100_CODE_VERSION,
     97  1.14.4.1  thorpej 	BIU_PCI_CONF1_FIFO_64 | BIU_BURST_ENABLE,
     98      1.10   mjacob 	60	/* MAGIC- all known PCI card implementations are 60MHz */
     99       1.1      cgd };
    100       1.1      cgd 
    101       1.1      cgd #define	PCI_QLOGIC_ISP	\
    102       1.1      cgd 	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
    103       1.1      cgd 
    104  1.14.4.1  thorpej #ifndef	PCI_PRODUCT_QLOGIC_ISP2100
    105  1.14.4.1  thorpej #define	PCI_PRODUCT_QLOGIC_ISP2100	0x2100
    106  1.14.4.1  thorpej #endif
    107  1.14.4.1  thorpej #define	PCI_QLOGIC_ISP2100	\
    108  1.14.4.1  thorpej 	((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
    109  1.14.4.1  thorpej 
    110       1.6      cgd #define IO_MAP_REG	0x10
    111       1.6      cgd #define MEM_MAP_REG	0x14
    112       1.6      cgd 
    113       1.1      cgd 
    114       1.1      cgd #ifdef	__BROKEN_INDIRECT_CONFIG
    115       1.1      cgd static int isp_pci_probe __P((struct device *, void *, void *));
    116       1.1      cgd #else
    117       1.1      cgd static int isp_pci_probe __P((struct device *, struct cfdata *, void *));
    118       1.1      cgd #endif
    119       1.1      cgd static void isp_pci_attach __P((struct device *, struct device *, void *));
    120       1.1      cgd 
    121       1.1      cgd struct isp_pcisoftc {
    122       1.1      cgd 	struct ispsoftc		pci_isp;
    123  1.14.4.1  thorpej 	pci_chipset_tag_t	pci_pc;
    124  1.14.4.1  thorpej 	pcitag_t		pci_tag;
    125       1.6      cgd 	bus_space_tag_t		pci_st;
    126       1.6      cgd 	bus_space_handle_t	pci_sh;
    127      1.13  thorpej 	bus_dma_tag_t		pci_dmat;
    128  1.14.4.1  thorpej 	bus_dmamap_t		pci_scratch_dmap;	/* for fcp only */
    129      1.13  thorpej 	bus_dmamap_t		pci_rquest_dmap;
    130      1.13  thorpej 	bus_dmamap_t		pci_result_dmap;
    131  1.14.4.1  thorpej 	bus_dmamap_t		pci_xfer_dmap[MAXISPREQUEST];
    132       1.1      cgd 	void *			pci_ih;
    133       1.1      cgd };
    134       1.1      cgd 
    135       1.1      cgd struct cfattach isp_pci_ca = {
    136       1.1      cgd 	sizeof (struct isp_pcisoftc), isp_pci_probe, isp_pci_attach
    137       1.1      cgd };
    138       1.1      cgd 
    139       1.1      cgd static int
    140       1.1      cgd isp_pci_probe(parent, match, aux)
    141       1.1      cgd         struct device *parent;
    142       1.1      cgd #ifdef	__BROKEN_INDIRECT_CONFIG
    143       1.1      cgd         void *match, *aux;
    144       1.1      cgd #else
    145       1.1      cgd         struct cfdata *match;
    146       1.1      cgd 	void *aux;
    147       1.1      cgd #endif
    148       1.1      cgd {
    149       1.1      cgd         struct pci_attach_args *pa = aux;
    150       1.1      cgd 
    151  1.14.4.1  thorpej 	if (pa->pa_id == PCI_QLOGIC_ISP ||
    152  1.14.4.1  thorpej 	    pa->pa_id == PCI_QLOGIC_ISP2100) {
    153       1.1      cgd 		return (1);
    154       1.1      cgd 	} else {
    155       1.1      cgd 		return (0);
    156       1.1      cgd 	}
    157       1.1      cgd }
    158       1.1      cgd 
    159       1.1      cgd 
    160       1.1      cgd static void
    161       1.1      cgd isp_pci_attach(parent, self, aux)
    162       1.1      cgd         struct device *parent, *self;
    163       1.1      cgd         void *aux;
    164       1.1      cgd {
    165       1.1      cgd 	struct pci_attach_args *pa = aux;
    166       1.1      cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) self;
    167      1.11      cgd 	bus_space_tag_t st, iot, memt;
    168      1.11      cgd 	bus_space_handle_t sh, ioh, memh;
    169       1.1      cgd 	pci_intr_handle_t ih;
    170       1.1      cgd 	const char *intrstr;
    171  1.14.4.1  thorpej 	int ioh_valid, memh_valid, i;
    172       1.1      cgd 
    173      1.12      cgd 	ioh_valid = (pci_mapreg_map(pa, IO_MAP_REG,
    174      1.11      cgd 	    PCI_MAPREG_TYPE_IO, 0,
    175      1.11      cgd 	    &iot, &ioh, NULL, NULL) == 0);
    176      1.12      cgd 	memh_valid = (pci_mapreg_map(pa, MEM_MAP_REG,
    177      1.11      cgd 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
    178      1.11      cgd 	    &memt, &memh, NULL, NULL) == 0);
    179      1.11      cgd 
    180      1.11      cgd 	if (memh_valid) {
    181      1.11      cgd 		st = memt;
    182      1.11      cgd 		sh = memh;
    183      1.11      cgd 	} else if (ioh_valid) {
    184      1.11      cgd 		st = iot;
    185      1.11      cgd 		sh = ioh;
    186       1.6      cgd 	} else {
    187      1.11      cgd 		printf(": unable to map device registers\n");
    188       1.9      cgd 		return;
    189       1.1      cgd 	}
    190       1.1      cgd 	printf("\n");
    191       1.1      cgd 
    192       1.6      cgd 	pcs->pci_st = st;
    193       1.6      cgd 	pcs->pci_sh = sh;
    194      1.13  thorpej 	pcs->pci_dmat = pa->pa_dmat;
    195  1.14.4.1  thorpej 	pcs->pci_pc = pa->pa_pc;
    196  1.14.4.1  thorpej 	pcs->pci_tag = pa->pa_tag;
    197  1.14.4.1  thorpej 	if (pa->pa_id == PCI_QLOGIC_ISP) {
    198  1.14.4.1  thorpej 		pcs->pci_isp.isp_mdvec = &mdvec;
    199  1.14.4.1  thorpej 		pcs->pci_isp.isp_type = ISP_HA_SCSI_UNKNOWN;
    200  1.14.4.1  thorpej 		pcs->pci_isp.isp_param =
    201  1.14.4.1  thorpej 			malloc(sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    202  1.14.4.1  thorpej 		if (pcs->pci_isp.isp_param == NULL) {
    203  1.14.4.1  thorpej 			printf("%s: couldn't allocate sdparam table\n",
    204  1.14.4.1  thorpej 			       pcs->pci_isp.isp_name);
    205  1.14.4.1  thorpej 		}
    206  1.14.4.1  thorpej 		bzero(pcs->pci_isp.isp_param, sizeof (sdparam));
    207  1.14.4.1  thorpej 	} else if (pa->pa_id == PCI_QLOGIC_ISP2100) {
    208  1.14.4.1  thorpej 		u_int32_t data;
    209  1.14.4.1  thorpej 		pcs->pci_isp.isp_mdvec = &mdvec_2100;
    210  1.14.4.1  thorpej 		if (ioh_valid == 0) {
    211  1.14.4.1  thorpej 			printf("%s: warning, ISP2100 cannot use I/O Space"
    212  1.14.4.1  thorpej 				" Mappings\n", pcs->pci_isp.isp_name);
    213  1.14.4.1  thorpej 		} else {
    214  1.14.4.1  thorpej 			pcs->pci_st = iot;
    215  1.14.4.1  thorpej 			pcs->pci_sh = ioh;
    216  1.14.4.1  thorpej 		}
    217  1.14.4.1  thorpej 
    218  1.14.4.1  thorpej #if	0
    219  1.14.4.1  thorpej 		printf("%s: PCIREGS cmd=%x bhlc=%x\n", pcs->pci_isp.isp_name,
    220  1.14.4.1  thorpej 		 pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG),
    221  1.14.4.1  thorpej 		 pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG));
    222  1.14.4.1  thorpej #endif
    223  1.14.4.1  thorpej 		pcs->pci_isp.isp_type = ISP_HA_FC_2100;
    224  1.14.4.1  thorpej 		pcs->pci_isp.isp_param =
    225  1.14.4.1  thorpej 			malloc(sizeof (fcparam), M_DEVBUF, M_NOWAIT);
    226  1.14.4.1  thorpej 		if (pcs->pci_isp.isp_param == NULL) {
    227  1.14.4.1  thorpej 			printf("%s: couldn't allocate fcparam table\n",
    228  1.14.4.1  thorpej 			       pcs->pci_isp.isp_name);
    229  1.14.4.1  thorpej 		}
    230  1.14.4.1  thorpej 		bzero(pcs->pci_isp.isp_param, sizeof (fcparam));
    231  1.14.4.1  thorpej 
    232  1.14.4.1  thorpej 		data = pci_conf_read(pa->pa_pc, pa->pa_tag,
    233  1.14.4.1  thorpej 			PCI_COMMAND_STATUS_REG);
    234  1.14.4.1  thorpej 		data |= PCI_COMMAND_MASTER_ENABLE |
    235  1.14.4.1  thorpej 			PCI_COMMAND_INVALIDATE_ENABLE;
    236  1.14.4.1  thorpej 		pci_conf_write(pa->pa_pc, pa->pa_tag,
    237  1.14.4.1  thorpej 			PCI_COMMAND_STATUS_REG, data);
    238  1.14.4.1  thorpej 		/*
    239  1.14.4.1  thorpej 		 * Wierd- we need to clear the lsb in offset 0x30 to take the
    240  1.14.4.1  thorpej 		 * chip out of reset state.
    241  1.14.4.1  thorpej 		 */
    242  1.14.4.1  thorpej 		data = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x30);
    243  1.14.4.1  thorpej 		data &= ~1;
    244  1.14.4.1  thorpej 		pci_conf_write(pa->pa_pc, pa->pa_tag, 0x30, data);
    245  1.14.4.1  thorpej #if	0
    246  1.14.4.1  thorpej 		/*
    247  1.14.4.1  thorpej 		 * XXX: Need to get the actual revision number of the 2100 FB
    248  1.14.4.1  thorpej 		 */
    249  1.14.4.1  thorpej 		data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    250  1.14.4.1  thorpej 		data &= ~0xffff;
    251  1.14.4.1  thorpej 		data |= 0xf801;
    252  1.14.4.1  thorpej 		pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, data);
    253  1.14.4.1  thorpej 		printf("%s: setting latency to %x and cache line size to %x\n",
    254  1.14.4.1  thorpej 			pcs->pci_isp.isp_name, (data >> 8) & 0xff,
    255  1.14.4.1  thorpej 			data & 0xff);
    256  1.14.4.1  thorpej #endif
    257  1.14.4.1  thorpej 	} else {
    258  1.14.4.1  thorpej 		return;
    259  1.14.4.1  thorpej 	}
    260       1.1      cgd 	isp_reset(&pcs->pci_isp);
    261       1.1      cgd 	if (pcs->pci_isp.isp_state != ISP_RESETSTATE) {
    262  1.14.4.1  thorpej 		free(pcs->pci_isp.isp_param, M_DEVBUF);
    263       1.1      cgd 		return;
    264       1.1      cgd 	}
    265       1.1      cgd 	isp_init(&pcs->pci_isp);
    266       1.1      cgd 	if (pcs->pci_isp.isp_state != ISP_INITSTATE) {
    267       1.1      cgd 		isp_uninit(&pcs->pci_isp);
    268  1.14.4.1  thorpej 		free(pcs->pci_isp.isp_param, M_DEVBUF);
    269       1.1      cgd 		return;
    270       1.1      cgd 	}
    271       1.1      cgd 
    272       1.1      cgd 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    273       1.1      cgd 			 pa->pa_intrline, &ih)) {
    274       1.1      cgd 		printf("%s: couldn't map interrupt\n", pcs->pci_isp.isp_name);
    275       1.1      cgd 		isp_uninit(&pcs->pci_isp);
    276  1.14.4.1  thorpej 		free(pcs->pci_isp.isp_param, M_DEVBUF);
    277       1.1      cgd 		return;
    278       1.1      cgd 	}
    279       1.1      cgd 
    280       1.1      cgd 	intrstr = pci_intr_string(pa->pa_pc, ih);
    281       1.1      cgd 	if (intrstr == NULL)
    282       1.1      cgd 		intrstr = "<I dunno>";
    283       1.1      cgd 	pcs->pci_ih =
    284       1.1      cgd 	  pci_intr_establish(pa->pa_pc, ih, IPL_BIO, isp_intr, &pcs->pci_isp);
    285       1.1      cgd 	if (pcs->pci_ih == NULL) {
    286       1.1      cgd 		printf("%s: couldn't establish interrupt at %s\n",
    287       1.1      cgd 			pcs->pci_isp.isp_name, intrstr);
    288       1.1      cgd 		isp_uninit(&pcs->pci_isp);
    289  1.14.4.1  thorpej 		free(pcs->pci_isp.isp_param, M_DEVBUF);
    290       1.1      cgd 		return;
    291       1.1      cgd 	}
    292       1.1      cgd 	printf("%s: interrupting at %s\n", pcs->pci_isp.isp_name, intrstr);
    293       1.1      cgd 
    294       1.1      cgd 	/*
    295      1.13  thorpej 	 * Create the DMA maps for the data transfers.
    296      1.13  thorpej 	 */
    297  1.14.4.1  thorpej 	for (i = 0; i < RQUEST_QUEUE_LEN(&pcs->pci_isp); i++) {
    298      1.13  thorpej 		if (bus_dmamap_create(pcs->pci_dmat, MAXPHYS,
    299      1.13  thorpej 		    (MAXPHYS / NBPG) + 1, MAXPHYS, 0, BUS_DMA_NOWAIT,
    300      1.13  thorpej 		    &pcs->pci_xfer_dmap[i])) {
    301      1.13  thorpej 			printf("%s: can't create dma maps\n",
    302      1.13  thorpej 			    pcs->pci_isp.isp_name);
    303      1.13  thorpej 			isp_uninit(&pcs->pci_isp);
    304      1.13  thorpej 			return;
    305      1.13  thorpej 		}
    306      1.13  thorpej 	}
    307      1.13  thorpej 	/*
    308       1.1      cgd 	 * Do Generic attach now.
    309       1.1      cgd 	 */
    310       1.1      cgd 	isp_attach(&pcs->pci_isp);
    311       1.1      cgd 	if (pcs->pci_isp.isp_state != ISP_RUNSTATE) {
    312       1.1      cgd 		isp_uninit(&pcs->pci_isp);
    313  1.14.4.1  thorpej 		free(pcs->pci_isp.isp_param, M_DEVBUF);
    314       1.1      cgd 	}
    315       1.1      cgd }
    316       1.1      cgd 
    317  1.14.4.1  thorpej #define  PCI_BIU_REGS_OFF		BIU_REGS_OFF
    318       1.1      cgd 
    319       1.1      cgd static u_int16_t
    320       1.1      cgd isp_pci_rd_reg(isp, regoff)
    321       1.1      cgd 	struct ispsoftc *isp;
    322       1.1      cgd 	int regoff;
    323       1.1      cgd {
    324  1.14.4.1  thorpej 	u_int16_t rv;
    325       1.1      cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    326  1.14.4.1  thorpej 	int offset, oldsxp = 0;
    327  1.14.4.1  thorpej 
    328       1.1      cgd 	if ((regoff & BIU_BLOCK) != 0) {
    329       1.1      cgd 		offset = PCI_BIU_REGS_OFF;
    330       1.1      cgd 	} else if ((regoff & MBOX_BLOCK) != 0) {
    331  1.14.4.1  thorpej 		if (isp->isp_type & ISP_HA_SCSI)
    332  1.14.4.1  thorpej 			offset = PCI_MBOX_REGS_OFF;
    333  1.14.4.1  thorpej 		else
    334  1.14.4.1  thorpej 			offset = PCI_MBOX_REGS2100_OFF;
    335       1.1      cgd 	} else if ((regoff & SXP_BLOCK) != 0) {
    336       1.1      cgd 		offset = PCI_SXP_REGS_OFF;
    337       1.1      cgd 		/*
    338  1.14.4.1  thorpej 		 * We will assume that someone has paused the RISC processor.
    339       1.1      cgd 		 */
    340  1.14.4.1  thorpej 		oldsxp = isp_pci_rd_reg(isp, BIU_CONF1);
    341  1.14.4.1  thorpej 		isp_pci_wr_reg(isp, BIU_CONF1, oldsxp & ~BIU_PCI_CONF1_SXP);
    342       1.1      cgd 	} else {
    343       1.1      cgd 		offset = PCI_RISC_REGS_OFF;
    344       1.1      cgd 	}
    345       1.1      cgd 	regoff &= 0xff;
    346       1.1      cgd 	offset += regoff;
    347  1.14.4.1  thorpej 	rv = bus_space_read_2(pcs->pci_st, pcs->pci_sh, offset);
    348  1.14.4.1  thorpej 	if ((regoff & SXP_BLOCK) != 0) {
    349  1.14.4.1  thorpej 		isp_pci_wr_reg(isp, BIU_CONF1, oldsxp);
    350  1.14.4.1  thorpej 	}
    351  1.14.4.1  thorpej 	return (rv);
    352       1.1      cgd }
    353       1.1      cgd 
    354       1.1      cgd static void
    355       1.1      cgd isp_pci_wr_reg(isp, regoff, val)
    356       1.1      cgd 	struct ispsoftc *isp;
    357       1.1      cgd 	int regoff;
    358       1.1      cgd 	u_int16_t val;
    359       1.1      cgd {
    360       1.1      cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    361  1.14.4.1  thorpej 	int offset, oldsxp = 0;
    362       1.1      cgd 	if ((regoff & BIU_BLOCK) != 0) {
    363       1.1      cgd 		offset = PCI_BIU_REGS_OFF;
    364       1.1      cgd 	} else if ((regoff & MBOX_BLOCK) != 0) {
    365  1.14.4.1  thorpej 		if (isp->isp_type & ISP_HA_SCSI)
    366  1.14.4.1  thorpej 			offset = PCI_MBOX_REGS_OFF;
    367  1.14.4.1  thorpej 		else
    368  1.14.4.1  thorpej 			offset = PCI_MBOX_REGS2100_OFF;
    369       1.1      cgd 	} else if ((regoff & SXP_BLOCK) != 0) {
    370       1.1      cgd 		offset = PCI_SXP_REGS_OFF;
    371       1.1      cgd 		/*
    372  1.14.4.1  thorpej 		 * We will assume that someone has paused the RISC processor.
    373       1.1      cgd 		 */
    374  1.14.4.1  thorpej 		oldsxp = isp_pci_rd_reg(isp, BIU_CONF1);
    375  1.14.4.1  thorpej 		isp_pci_wr_reg(isp, BIU_CONF1, oldsxp & ~BIU_PCI_CONF1_SXP);
    376       1.1      cgd 	} else {
    377       1.1      cgd 		offset = PCI_RISC_REGS_OFF;
    378       1.1      cgd 	}
    379       1.1      cgd 	regoff &= 0xff;
    380       1.1      cgd 	offset += regoff;
    381       1.6      cgd 	bus_space_write_2(pcs->pci_st, pcs->pci_sh, offset, val);
    382  1.14.4.1  thorpej 	if ((regoff & SXP_BLOCK) != 0) {
    383  1.14.4.1  thorpej 		isp_pci_wr_reg(isp, BIU_CONF1, oldsxp);
    384  1.14.4.1  thorpej 	}
    385       1.1      cgd }
    386       1.1      cgd 
    387      1.13  thorpej static int
    388      1.13  thorpej isp_pci_mbxdma(isp)
    389       1.1      cgd 	struct ispsoftc *isp;
    390       1.1      cgd {
    391      1.13  thorpej 	struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp;
    392      1.13  thorpej 	bus_dma_segment_t seg;
    393      1.13  thorpej 	bus_size_t len;
    394  1.14.4.1  thorpej 	fcparam *fcp;
    395      1.13  thorpej 	int rseg;
    396      1.13  thorpej 
    397      1.13  thorpej 	/*
    398      1.13  thorpej 	 * Allocate and map the request queue.
    399      1.13  thorpej 	 */
    400  1.14.4.1  thorpej 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
    401      1.13  thorpej 	if (bus_dmamem_alloc(pci->pci_dmat, len, NBPG, 0, &seg, 1, &rseg,
    402      1.13  thorpej 	      BUS_DMA_NOWAIT) ||
    403      1.13  thorpej 	    bus_dmamem_map(pci->pci_dmat, &seg, rseg, len,
    404      1.13  thorpej 	      (caddr_t *)&isp->isp_rquest, BUS_DMA_NOWAIT|BUS_DMAMEM_NOSYNC))
    405      1.13  thorpej 		return (1);
    406      1.13  thorpej 	if (bus_dmamap_create(pci->pci_dmat, len, 1, len, 0, BUS_DMA_NOWAIT,
    407      1.13  thorpej 	      &pci->pci_rquest_dmap) ||
    408      1.13  thorpej 	    bus_dmamap_load(pci->pci_dmat, pci->pci_rquest_dmap,
    409      1.13  thorpej 	      (caddr_t)isp->isp_rquest, len, NULL, BUS_DMA_NOWAIT))
    410      1.13  thorpej 		return (1);
    411      1.13  thorpej 
    412      1.13  thorpej 	isp->isp_rquest_dma = pci->pci_rquest_dmap->dm_segs[0].ds_addr;
    413      1.13  thorpej 
    414      1.13  thorpej 	/*
    415      1.13  thorpej 	 * Allocate and map the result queue.
    416      1.13  thorpej 	 */
    417  1.14.4.1  thorpej 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
    418      1.13  thorpej 	if (bus_dmamem_alloc(pci->pci_dmat, len, NBPG, 0, &seg, 1, &rseg,
    419      1.13  thorpej 	      BUS_DMA_NOWAIT) ||
    420      1.13  thorpej 	    bus_dmamem_map(pci->pci_dmat, &seg, rseg, len,
    421      1.13  thorpej 	      (caddr_t *)&isp->isp_result, BUS_DMA_NOWAIT|BUS_DMAMEM_NOSYNC))
    422      1.13  thorpej 		return (1);
    423      1.13  thorpej 	if (bus_dmamap_create(pci->pci_dmat, len, 1, len, 0, BUS_DMA_NOWAIT,
    424      1.13  thorpej 	      &pci->pci_result_dmap) ||
    425      1.13  thorpej 	    bus_dmamap_load(pci->pci_dmat, pci->pci_result_dmap,
    426      1.13  thorpej 	      (caddr_t)isp->isp_result, len, NULL, BUS_DMA_NOWAIT))
    427      1.13  thorpej 		return (1);
    428      1.13  thorpej 	isp->isp_result_dma = pci->pci_result_dmap->dm_segs[0].ds_addr;
    429       1.1      cgd 
    430  1.14.4.1  thorpej 	if (isp->isp_type & ISP_HA_SCSI) {
    431  1.14.4.1  thorpej 		return (0);
    432  1.14.4.1  thorpej 	}
    433  1.14.4.1  thorpej 
    434  1.14.4.1  thorpej 	fcp = isp->isp_param;
    435  1.14.4.1  thorpej 	len = ISP2100_SCRLEN;
    436  1.14.4.1  thorpej 	if (bus_dmamem_alloc(pci->pci_dmat, len, NBPG, 0, &seg, 1, &rseg,
    437  1.14.4.1  thorpej 		BUS_DMA_NOWAIT) ||
    438  1.14.4.1  thorpej 	    bus_dmamem_map(pci->pci_dmat, &seg, rseg, len,
    439  1.14.4.1  thorpej 	      (caddr_t *)&fcp->isp_scratch, BUS_DMA_NOWAIT|BUS_DMAMEM_NOSYNC))
    440  1.14.4.1  thorpej 		return (1);
    441  1.14.4.1  thorpej 	if (bus_dmamap_create(pci->pci_dmat, len, 1, len, 0, BUS_DMA_NOWAIT,
    442  1.14.4.1  thorpej 	      &pci->pci_scratch_dmap) ||
    443  1.14.4.1  thorpej 	    bus_dmamap_load(pci->pci_dmat, pci->pci_scratch_dmap,
    444  1.14.4.1  thorpej 	      (caddr_t)fcp->isp_scratch, len, NULL, BUS_DMA_NOWAIT))
    445  1.14.4.1  thorpej 		return (1);
    446  1.14.4.1  thorpej 	fcp->isp_scdma = pci->pci_scratch_dmap->dm_segs[0].ds_addr;
    447      1.13  thorpej 	return (0);
    448       1.1      cgd }
    449       1.1      cgd 
    450       1.1      cgd static int
    451       1.1      cgd isp_pci_dmasetup(isp, xs, rq, iptrp, optr)
    452       1.1      cgd 	struct ispsoftc *isp;
    453  1.14.4.2  thorpej 	struct scsipi_xfer *xs;
    454       1.1      cgd 	ispreq_t *rq;
    455       1.1      cgd 	u_int8_t *iptrp;
    456       1.1      cgd 	u_int8_t optr;
    457       1.1      cgd {
    458      1.13  thorpej 	struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp;
    459  1.14.4.1  thorpej 	bus_dmamap_t dmap = pci->pci_xfer_dmap[rq->req_handle - 1];
    460       1.1      cgd 	ispcontreq_t *crq;
    461  1.14.4.3  thorpej 	int segcnt, seg, error, ovseg, seglim, drq;
    462       1.1      cgd 
    463       1.1      cgd 	if (xs->datalen == 0) {
    464       1.1      cgd 		rq->req_seg_count = 1;
    465       1.1      cgd 		return (0);
    466       1.1      cgd 	}
    467       1.1      cgd 
    468  1.14.4.3  thorpej 	if (rq->req_handle > RQUEST_QUEUE_LEN(isp) || rq->req_handle < 1) {
    469      1.14  thorpej 		panic("%s: bad handle (%d) in isp_pci_dmasetup\n",
    470      1.13  thorpej 		    isp->isp_name, rq->req_handle);
    471      1.13  thorpej 		/* NOTREACHED */
    472      1.13  thorpej 	}
    473      1.13  thorpej 
    474       1.1      cgd 	if (xs->flags & SCSI_DATA_IN) {
    475  1.14.4.3  thorpej 		drq = REQFLAG_DATA_IN;
    476       1.1      cgd 	} else {
    477  1.14.4.3  thorpej 		drq = REQFLAG_DATA_OUT;
    478       1.1      cgd 	}
    479       1.1      cgd 
    480  1.14.4.1  thorpej 	if (isp->isp_type & ISP_HA_FC) {
    481  1.14.4.1  thorpej 		seglim = ISP_RQDSEG_T2;
    482  1.14.4.1  thorpej 		((ispreqt2_t *)rq)->req_totalcnt = xs->datalen;
    483  1.14.4.3  thorpej 		((ispreqt2_t *)rq)->req_flags |= drq;
    484  1.14.4.1  thorpej 	} else {
    485  1.14.4.1  thorpej 		seglim = ISP_RQDSEG;
    486  1.14.4.3  thorpej 		rq->req_flags |= drq;
    487  1.14.4.1  thorpej 	}
    488      1.13  thorpej 	error = bus_dmamap_load(pci->pci_dmat, dmap, xs->data, xs->datalen,
    489      1.13  thorpej 	    NULL, xs->flags & SCSI_NOSLEEP ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
    490      1.13  thorpej 	if (error)
    491      1.13  thorpej 		return (error);
    492      1.13  thorpej 
    493      1.13  thorpej 	segcnt = dmap->dm_nsegs;
    494      1.13  thorpej 
    495      1.13  thorpej 	for (seg = 0, rq->req_seg_count = 0;
    496  1.14.4.1  thorpej 	     seg < segcnt && rq->req_seg_count < seglim;
    497  1.14.4.1  thorpej 	     seg++, rq->req_seg_count++) {
    498  1.14.4.1  thorpej 		if (isp->isp_type & ISP_HA_FC) {
    499  1.14.4.1  thorpej 			ispreqt2_t *rq2 = (ispreqt2_t *)rq;
    500  1.14.4.1  thorpej 			rq2->req_dataseg[rq2->req_seg_count].ds_count =
    501  1.14.4.1  thorpej 			    dmap->dm_segs[seg].ds_len;
    502  1.14.4.1  thorpej 			rq2->req_dataseg[rq2->req_seg_count].ds_base =
    503  1.14.4.1  thorpej 			    dmap->dm_segs[seg].ds_addr;
    504  1.14.4.1  thorpej 		} else {
    505  1.14.4.1  thorpej 			rq->req_dataseg[rq->req_seg_count].ds_count =
    506  1.14.4.1  thorpej 			    dmap->dm_segs[seg].ds_len;
    507  1.14.4.1  thorpej 			rq->req_dataseg[rq->req_seg_count].ds_base =
    508  1.14.4.1  thorpej 			    dmap->dm_segs[seg].ds_addr;
    509  1.14.4.1  thorpej 		}
    510       1.1      cgd 	}
    511       1.1      cgd 
    512      1.13  thorpej 	if (seg == segcnt)
    513      1.13  thorpej 		goto mapsync;
    514       1.1      cgd 
    515       1.1      cgd 	do {
    516  1.14.4.1  thorpej 		crq = (ispcontreq_t *)
    517  1.14.4.1  thorpej 			ISP_QUEUE_ENTRY(isp->isp_rquest, *iptrp);
    518  1.14.4.1  thorpej 		*iptrp = (*iptrp + 1) & (RQUEST_QUEUE_LEN(isp) - 1);
    519       1.1      cgd 		if (*iptrp == optr) {
    520       1.1      cgd 			printf("%s: Request Queue Overflow++\n",
    521       1.1      cgd 			       isp->isp_name);
    522      1.13  thorpej 			bus_dmamap_unload(pci->pci_dmat, dmap);
    523      1.13  thorpej 			return (EFBIG);
    524       1.1      cgd 		}
    525       1.1      cgd 		rq->req_header.rqs_entry_count++;
    526       1.1      cgd 		bzero((void *)crq, sizeof (*crq));
    527       1.1      cgd 		crq->req_header.rqs_entry_count = 1;
    528       1.1      cgd 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
    529      1.13  thorpej 
    530      1.13  thorpej 		for (ovseg = 0; seg < segcnt && ovseg < ISP_CDSEG;
    531      1.13  thorpej 		    rq->req_seg_count++, seg++, ovseg++) {
    532      1.13  thorpej 			crq->req_dataseg[ovseg].ds_count =
    533      1.13  thorpej 			    dmap->dm_segs[seg].ds_len;
    534      1.13  thorpej 			crq->req_dataseg[ovseg].ds_base =
    535      1.13  thorpej 			    dmap->dm_segs[seg].ds_addr;
    536       1.1      cgd 		}
    537      1.13  thorpej 	} while (seg < segcnt);
    538      1.13  thorpej 
    539  1.14.4.1  thorpej mapsync:
    540      1.13  thorpej 	bus_dmamap_sync(pci->pci_dmat, dmap, xs->flags & SCSI_DATA_IN ?
    541      1.13  thorpej 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    542       1.1      cgd 	return (0);
    543      1.13  thorpej }
    544      1.13  thorpej 
    545      1.13  thorpej static void
    546      1.13  thorpej isp_pci_dmateardown(isp, xs, handle)
    547      1.13  thorpej 	struct ispsoftc *isp;
    548  1.14.4.2  thorpej 	struct scsipi_xfer *xs;
    549      1.13  thorpej 	u_int32_t handle;
    550      1.13  thorpej {
    551      1.13  thorpej 	struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp;
    552      1.13  thorpej 	bus_dmamap_t dmap = pci->pci_xfer_dmap[handle];
    553      1.13  thorpej 
    554      1.13  thorpej 	bus_dmamap_sync(pci->pci_dmat, dmap, xs->flags & SCSI_DATA_IN ?
    555      1.13  thorpej 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    556      1.13  thorpej 	bus_dmamap_unload(pci->pci_dmat, dmap);
    557       1.1      cgd }
    558       1.1      cgd 
    559       1.1      cgd static void
    560       1.1      cgd isp_pci_reset1(isp)
    561       1.1      cgd 	struct ispsoftc *isp;
    562       1.1      cgd {
    563       1.1      cgd 	/* Make sure the BIOS is disabled */
    564       1.1      cgd 	isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
    565  1.14.4.1  thorpej }
    566  1.14.4.1  thorpej 
    567  1.14.4.1  thorpej static void
    568  1.14.4.1  thorpej isp_pci_dumpregs(isp)
    569  1.14.4.1  thorpej 	struct ispsoftc *isp;
    570  1.14.4.1  thorpej {
    571  1.14.4.1  thorpej 	struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp;
    572  1.14.4.1  thorpej 	printf("%s: PCI Status Command/Status=%x\n", pci->pci_isp.isp_name,
    573  1.14.4.1  thorpej 	    pci_conf_read(pci->pci_pc, pci->pci_tag, PCI_COMMAND_STATUS_REG));
    574       1.1      cgd }
    575