Home | History | Annotate | Line # | Download | only in pci
isp_pci.c revision 1.66.2.11
      1  1.66.2.11   nathanw /* $NetBSD: isp_pci.c,v 1.66.2.11 2002/08/13 02:19:40 nathanw Exp $ */
      2       1.56    mjacob /*
      3       1.56    mjacob  * This driver, which is contained in NetBSD in the files:
      4       1.56    mjacob  *
      5       1.56    mjacob  *	sys/dev/ic/isp.c
      6       1.62       wiz  *	sys/dev/ic/isp_inline.h
      7       1.62       wiz  *	sys/dev/ic/isp_netbsd.c
      8       1.62       wiz  *	sys/dev/ic/isp_netbsd.h
      9       1.62       wiz  *	sys/dev/ic/isp_target.c
     10       1.62       wiz  *	sys/dev/ic/isp_target.h
     11       1.62       wiz  *	sys/dev/ic/isp_tpublic.h
     12       1.62       wiz  *	sys/dev/ic/ispmbox.h
     13       1.62       wiz  *	sys/dev/ic/ispreg.h
     14       1.62       wiz  *	sys/dev/ic/ispvar.h
     15       1.56    mjacob  *	sys/microcode/isp/asm_sbus.h
     16       1.56    mjacob  *	sys/microcode/isp/asm_1040.h
     17       1.56    mjacob  *	sys/microcode/isp/asm_1080.h
     18       1.56    mjacob  *	sys/microcode/isp/asm_12160.h
     19       1.56    mjacob  *	sys/microcode/isp/asm_2100.h
     20       1.56    mjacob  *	sys/microcode/isp/asm_2200.h
     21       1.56    mjacob  *	sys/pci/isp_pci.c
     22       1.56    mjacob  *	sys/sbus/isp_sbus.c
     23       1.56    mjacob  *
     24       1.56    mjacob  * Is being actively maintained by Matthew Jacob (mjacob (at) netbsd.org).
     25       1.56    mjacob  * This driver also is shared source with FreeBSD, OpenBSD, Linux, Solaris,
     26       1.56    mjacob  * Linux versions. This tends to be an interesting maintenance problem.
     27       1.56    mjacob  *
     28       1.56    mjacob  * Please coordinate with Matthew Jacob on changes you wish to make here.
     29       1.56    mjacob  */
     30        1.1       cgd /*
     31        1.1       cgd  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
     32       1.41    mjacob  */
     33       1.41    mjacob /*
     34       1.41    mjacob  * Copyright (C) 1997, 1998, 1999 National Aeronautics & Space Administration
     35        1.1       cgd  * All rights reserved.
     36        1.1       cgd  *
     37   1.66.2.1   nathanw  * Additional Copyright (C) 2000, 2001 by Matthew Jacob
     38   1.66.2.1   nathanw  *
     39        1.1       cgd  * Redistribution and use in source and binary forms, with or without
     40        1.1       cgd  * modification, are permitted provided that the following conditions
     41        1.1       cgd  * are met:
     42        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     43       1.41    mjacob  *    notice, this list of conditions and the following disclaimer.
     44   1.66.2.1   nathanw  * 2. The name of the author may not be used to endorse or promote products
     45       1.41    mjacob  *    derived from this software without specific prior written permission
     46       1.21    mjacob  *
     47       1.41    mjacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48       1.41    mjacob  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49       1.41    mjacob  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50       1.41    mjacob  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51       1.41    mjacob  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52       1.41    mjacob  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53       1.41    mjacob  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54       1.41    mjacob  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55       1.41    mjacob  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56       1.41    mjacob  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57        1.1       cgd  */
     58   1.66.2.6   nathanw 
     59   1.66.2.6   nathanw #include <sys/cdefs.h>
     60  1.66.2.11   nathanw __KERNEL_RCSID(0, "$NetBSD: isp_pci.c,v 1.66.2.11 2002/08/13 02:19:40 nathanw Exp $");
     61        1.1       cgd 
     62       1.21    mjacob #include <dev/ic/isp_netbsd.h>
     63        1.1       cgd #include <dev/pci/pcireg.h>
     64        1.1       cgd #include <dev/pci/pcivar.h>
     65        1.1       cgd #include <dev/pci/pcidevs.h>
     66       1.60    mjacob #include <uvm/uvm_extern.h>
     67       1.65    mjacob #include <sys/reboot.h>
     68        1.3       cgd 
     69   1.66.2.1   nathanw static u_int16_t isp_pci_rd_reg(struct ispsoftc *, int);
     70   1.66.2.1   nathanw static void isp_pci_wr_reg(struct ispsoftc *, int, u_int16_t);
     71       1.52    mjacob #if !(defined(ISP_DISABLE_1080_SUPPORT) && defined(ISP_DISABLE_12160_SUPPORT))
     72   1.66.2.1   nathanw static u_int16_t isp_pci_rd_reg_1080(struct ispsoftc *, int);
     73   1.66.2.1   nathanw static void isp_pci_wr_reg_1080(struct ispsoftc *, int, u_int16_t);
     74       1.36    mjacob #endif
     75   1.66.2.4   nathanw static int
     76   1.66.2.4   nathanw isp_pci_rd_isr(struct ispsoftc *, u_int16_t *, u_int16_t *, u_int16_t *);
     77   1.66.2.4   nathanw static int
     78   1.66.2.4   nathanw isp_pci_rd_isr_2300(struct ispsoftc *, u_int16_t *, u_int16_t *, u_int16_t *);
     79   1.66.2.1   nathanw static int isp_pci_mbxdma(struct ispsoftc *);
     80   1.66.2.1   nathanw static int isp_pci_dmasetup(struct ispsoftc *, XS_T *, ispreq_t *,
     81   1.66.2.1   nathanw     u_int16_t *, u_int16_t);
     82   1.66.2.1   nathanw static void isp_pci_dmateardown(struct ispsoftc *, XS_T *, u_int16_t);
     83   1.66.2.1   nathanw static void isp_pci_reset1(struct ispsoftc *);
     84   1.66.2.1   nathanw static void isp_pci_dumpregs(struct ispsoftc *, const char *);
     85   1.66.2.1   nathanw static int isp_pci_intr(void *);
     86        1.1       cgd 
     87       1.52    mjacob #if	defined(ISP_DISABLE_1020_SUPPORT)
     88       1.47    mjacob #define	ISP_1040_RISC_CODE	NULL
     89       1.52    mjacob #else
     90   1.66.2.9   nathanw #define	ISP_1040_RISC_CODE	(u_int16_t *) isp_1040_risc_code
     91       1.52    mjacob #include <dev/microcode/isp/asm_1040.h>
     92       1.47    mjacob #endif
     93       1.52    mjacob 
     94       1.52    mjacob #if	defined(ISP_DISABLE_1080_SUPPORT)
     95       1.47    mjacob #define	ISP_1080_RISC_CODE	NULL
     96       1.52    mjacob #else
     97   1.66.2.9   nathanw #define	ISP_1080_RISC_CODE	(u_int16_t *) isp_1080_risc_code
     98       1.52    mjacob #include <dev/microcode/isp/asm_1080.h>
     99       1.47    mjacob #endif
    100       1.52    mjacob 
    101       1.52    mjacob #if	defined(ISP_DISABLE_12160_SUPPORT)
    102       1.50    mjacob #define	ISP_12160_RISC_CODE	NULL
    103       1.52    mjacob #else
    104   1.66.2.9   nathanw #define	ISP_12160_RISC_CODE	(u_int16_t *) isp_12160_risc_code
    105       1.52    mjacob #include <dev/microcode/isp/asm_12160.h>
    106       1.50    mjacob #endif
    107       1.52    mjacob 
    108       1.52    mjacob #if	defined(ISP_DISABLE_2100_SUPPORT)
    109       1.47    mjacob #define	ISP_2100_RISC_CODE	NULL
    110       1.52    mjacob #else
    111   1.66.2.9   nathanw #define	ISP_2100_RISC_CODE	(u_int16_t *) isp_2100_risc_code
    112       1.52    mjacob #include <dev/microcode/isp/asm_2100.h>
    113       1.47    mjacob #endif
    114       1.52    mjacob 
    115       1.52    mjacob #if	defined(ISP_DISABLE_2200_SUPPORT)
    116       1.47    mjacob #define	ISP_2200_RISC_CODE	NULL
    117       1.52    mjacob #else
    118   1.66.2.9   nathanw #define	ISP_2200_RISC_CODE	(u_int16_t *) isp_2200_risc_code
    119       1.52    mjacob #include <dev/microcode/isp/asm_2200.h>
    120       1.47    mjacob #endif
    121       1.47    mjacob 
    122   1.66.2.4   nathanw #if	defined(ISP_DISABLE_2300_SUPPORT)
    123   1.66.2.4   nathanw #define	ISP_2300_RISC_CODE	NULL
    124   1.66.2.4   nathanw #else
    125   1.66.2.9   nathanw #define	ISP_2300_RISC_CODE	(u_int16_t *) isp_2300_risc_code
    126   1.66.2.4   nathanw #include <dev/microcode/isp/asm_2300.h>
    127   1.66.2.4   nathanw #endif
    128   1.66.2.4   nathanw 
    129       1.36    mjacob #ifndef	ISP_DISABLE_1020_SUPPORT
    130        1.1       cgd static struct ispmdvec mdvec = {
    131   1.66.2.4   nathanw 	isp_pci_rd_isr,
    132        1.1       cgd 	isp_pci_rd_reg,
    133        1.1       cgd 	isp_pci_wr_reg,
    134        1.1       cgd 	isp_pci_mbxdma,
    135        1.1       cgd 	isp_pci_dmasetup,
    136       1.13   thorpej 	isp_pci_dmateardown,
    137        1.1       cgd 	NULL,
    138        1.1       cgd 	isp_pci_reset1,
    139       1.15    mjacob 	isp_pci_dumpregs,
    140       1.47    mjacob 	ISP_1040_RISC_CODE,
    141       1.53    mjacob 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
    142       1.15    mjacob };
    143       1.36    mjacob #endif
    144       1.36    mjacob 
    145       1.36    mjacob #ifndef	ISP_DISABLE_1080_SUPPORT
    146       1.36    mjacob static struct ispmdvec mdvec_1080 = {
    147   1.66.2.4   nathanw 	isp_pci_rd_isr,
    148       1.36    mjacob 	isp_pci_rd_reg_1080,
    149       1.36    mjacob 	isp_pci_wr_reg_1080,
    150       1.36    mjacob 	isp_pci_mbxdma,
    151       1.36    mjacob 	isp_pci_dmasetup,
    152       1.36    mjacob 	isp_pci_dmateardown,
    153       1.36    mjacob 	NULL,
    154       1.36    mjacob 	isp_pci_reset1,
    155       1.36    mjacob 	isp_pci_dumpregs,
    156       1.47    mjacob 	ISP_1080_RISC_CODE,
    157       1.53    mjacob 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
    158       1.36    mjacob };
    159       1.36    mjacob #endif
    160       1.15    mjacob 
    161       1.50    mjacob #ifndef	ISP_DISABLE_12160_SUPPORT
    162       1.50    mjacob static struct ispmdvec mdvec_12160 = {
    163   1.66.2.4   nathanw 	isp_pci_rd_isr,
    164       1.50    mjacob 	isp_pci_rd_reg_1080,
    165       1.50    mjacob 	isp_pci_wr_reg_1080,
    166       1.50    mjacob 	isp_pci_mbxdma,
    167       1.50    mjacob 	isp_pci_dmasetup,
    168       1.50    mjacob 	isp_pci_dmateardown,
    169       1.50    mjacob 	NULL,
    170       1.50    mjacob 	isp_pci_reset1,
    171       1.50    mjacob 	isp_pci_dumpregs,
    172       1.50    mjacob 	ISP_12160_RISC_CODE,
    173       1.53    mjacob 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
    174       1.50    mjacob };
    175       1.50    mjacob #endif
    176       1.50    mjacob 
    177       1.36    mjacob #ifndef	ISP_DISABLE_2100_SUPPORT
    178       1.15    mjacob static struct ispmdvec mdvec_2100 = {
    179   1.66.2.4   nathanw 	isp_pci_rd_isr,
    180       1.15    mjacob 	isp_pci_rd_reg,
    181       1.15    mjacob 	isp_pci_wr_reg,
    182       1.15    mjacob 	isp_pci_mbxdma,
    183       1.15    mjacob 	isp_pci_dmasetup,
    184       1.15    mjacob 	isp_pci_dmateardown,
    185       1.15    mjacob 	NULL,
    186       1.15    mjacob 	isp_pci_reset1,
    187       1.15    mjacob 	isp_pci_dumpregs,
    188       1.53    mjacob 	ISP_2100_RISC_CODE
    189        1.1       cgd };
    190       1.36    mjacob #endif
    191        1.1       cgd 
    192       1.41    mjacob #ifndef	ISP_DISABLE_2200_SUPPORT
    193       1.41    mjacob static struct ispmdvec mdvec_2200 = {
    194   1.66.2.4   nathanw 	isp_pci_rd_isr,
    195       1.41    mjacob 	isp_pci_rd_reg,
    196       1.41    mjacob 	isp_pci_wr_reg,
    197       1.41    mjacob 	isp_pci_mbxdma,
    198       1.41    mjacob 	isp_pci_dmasetup,
    199       1.41    mjacob 	isp_pci_dmateardown,
    200       1.41    mjacob 	NULL,
    201       1.41    mjacob 	isp_pci_reset1,
    202       1.41    mjacob 	isp_pci_dumpregs,
    203       1.53    mjacob 	ISP_2200_RISC_CODE
    204       1.41    mjacob };
    205       1.41    mjacob #endif
    206       1.41    mjacob 
    207   1.66.2.4   nathanw #ifndef	ISP_DISABLE_2300_SUPPORT
    208   1.66.2.4   nathanw static struct ispmdvec mdvec_2300 = {
    209   1.66.2.4   nathanw 	isp_pci_rd_isr_2300,
    210   1.66.2.4   nathanw 	isp_pci_rd_reg,
    211   1.66.2.4   nathanw 	isp_pci_wr_reg,
    212   1.66.2.4   nathanw 	isp_pci_mbxdma,
    213   1.66.2.4   nathanw 	isp_pci_dmasetup,
    214   1.66.2.4   nathanw 	isp_pci_dmateardown,
    215   1.66.2.4   nathanw 	NULL,
    216   1.66.2.4   nathanw 	isp_pci_reset1,
    217   1.66.2.4   nathanw 	isp_pci_dumpregs,
    218   1.66.2.4   nathanw 	ISP_2300_RISC_CODE
    219   1.66.2.4   nathanw };
    220   1.66.2.4   nathanw #endif
    221   1.66.2.4   nathanw 
    222       1.36    mjacob #ifndef	PCI_VENDOR_QLOGIC
    223       1.36    mjacob #define	PCI_VENDOR_QLOGIC	0x1077
    224       1.36    mjacob #endif
    225       1.36    mjacob 
    226       1.36    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1020
    227       1.36    mjacob #define	PCI_PRODUCT_QLOGIC_ISP1020	0x1020
    228       1.36    mjacob #endif
    229       1.36    mjacob 
    230       1.36    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1080
    231       1.36    mjacob #define	PCI_PRODUCT_QLOGIC_ISP1080	0x1080
    232       1.36    mjacob #endif
    233       1.36    mjacob 
    234       1.36    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1240
    235       1.36    mjacob #define	PCI_PRODUCT_QLOGIC_ISP1240	0x1240
    236       1.36    mjacob #endif
    237        1.1       cgd 
    238       1.48    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1280
    239       1.48    mjacob #define	PCI_PRODUCT_QLOGIC_ISP1280	0x1280
    240       1.48    mjacob #endif
    241       1.48    mjacob 
    242       1.50    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP12160
    243       1.53    mjacob #define	PCI_PRODUCT_QLOGIC_ISP12160	0x1216
    244       1.50    mjacob #endif
    245       1.50    mjacob 
    246       1.15    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2100
    247       1.15    mjacob #define	PCI_PRODUCT_QLOGIC_ISP2100	0x2100
    248       1.15    mjacob #endif
    249       1.36    mjacob 
    250       1.41    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2200
    251       1.41    mjacob #define	PCI_PRODUCT_QLOGIC_ISP2200	0x2200
    252       1.41    mjacob #endif
    253       1.41    mjacob 
    254   1.66.2.4   nathanw #ifndef	PCI_PRODUCT_QLOGIC_ISP2300
    255   1.66.2.4   nathanw #define	PCI_PRODUCT_QLOGIC_ISP2300	0x2300
    256   1.66.2.4   nathanw #endif
    257   1.66.2.4   nathanw 
    258   1.66.2.4   nathanw #ifndef	PCI_PRODUCT_QLOGIC_ISP2312
    259   1.66.2.4   nathanw #define	PCI_PRODUCT_QLOGIC_ISP2312	0x2312
    260   1.66.2.4   nathanw #endif
    261   1.66.2.4   nathanw 
    262       1.36    mjacob #define	PCI_QLOGIC_ISP	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
    263       1.36    mjacob 
    264       1.36    mjacob #define	PCI_QLOGIC_ISP1080	\
    265       1.36    mjacob 	((PCI_PRODUCT_QLOGIC_ISP1080 << 16) | PCI_VENDOR_QLOGIC)
    266       1.36    mjacob 
    267       1.36    mjacob #define	PCI_QLOGIC_ISP1240	\
    268       1.36    mjacob 	((PCI_PRODUCT_QLOGIC_ISP1240 << 16) | PCI_VENDOR_QLOGIC)
    269       1.36    mjacob 
    270       1.48    mjacob #define	PCI_QLOGIC_ISP1280	\
    271       1.48    mjacob 	((PCI_PRODUCT_QLOGIC_ISP1280 << 16) | PCI_VENDOR_QLOGIC)
    272       1.48    mjacob 
    273       1.50    mjacob #define	PCI_QLOGIC_ISP12160	\
    274       1.50    mjacob 	((PCI_PRODUCT_QLOGIC_ISP12160 << 16) | PCI_VENDOR_QLOGIC)
    275       1.50    mjacob 
    276       1.15    mjacob #define	PCI_QLOGIC_ISP2100	\
    277       1.15    mjacob 	((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
    278       1.15    mjacob 
    279       1.41    mjacob #define	PCI_QLOGIC_ISP2200	\
    280       1.41    mjacob 	((PCI_PRODUCT_QLOGIC_ISP2200 << 16) | PCI_VENDOR_QLOGIC)
    281       1.41    mjacob 
    282   1.66.2.4   nathanw #define	PCI_QLOGIC_ISP2300	\
    283   1.66.2.4   nathanw 	((PCI_PRODUCT_QLOGIC_ISP2300 << 16) | PCI_VENDOR_QLOGIC)
    284   1.66.2.4   nathanw 
    285   1.66.2.4   nathanw #define	PCI_QLOGIC_ISP2312	\
    286   1.66.2.4   nathanw 	((PCI_PRODUCT_QLOGIC_ISP2312 << 16) | PCI_VENDOR_QLOGIC)
    287   1.66.2.4   nathanw 
    288       1.44    mjacob #define	IO_MAP_REG	0x10
    289       1.44    mjacob #define	MEM_MAP_REG	0x14
    290       1.39    mjacob #define	PCIR_ROMADDR	0x30
    291       1.39    mjacob 
    292       1.39    mjacob #define	PCI_DFLT_LTNCY	0x40
    293       1.39    mjacob #define	PCI_DFLT_LNSZ	0x10
    294        1.6       cgd 
    295        1.1       cgd 
    296   1.66.2.1   nathanw static int isp_pci_probe(struct device *, struct cfdata *, void *);
    297   1.66.2.1   nathanw static void isp_pci_attach(struct device *, struct device *, void *);
    298        1.1       cgd 
    299        1.1       cgd struct isp_pcisoftc {
    300        1.1       cgd 	struct ispsoftc		pci_isp;
    301       1.15    mjacob 	pci_chipset_tag_t	pci_pc;
    302       1.15    mjacob 	pcitag_t		pci_tag;
    303        1.6       cgd 	bus_space_tag_t		pci_st;
    304        1.6       cgd 	bus_space_handle_t	pci_sh;
    305       1.45    mjacob 	bus_dmamap_t		*pci_xfer_dmap;
    306        1.1       cgd 	void *			pci_ih;
    307       1.36    mjacob 	int16_t			pci_poff[_NREG_BLKS];
    308        1.1       cgd };
    309        1.1       cgd 
    310        1.1       cgd struct cfattach isp_pci_ca = {
    311        1.1       cgd 	sizeof (struct isp_pcisoftc), isp_pci_probe, isp_pci_attach
    312        1.1       cgd };
    313        1.1       cgd 
    314       1.55    mjacob #ifdef	DEBUG
    315       1.63    mjacob const char vstring[] =
    316       1.53    mjacob     "Qlogic ISP Driver, NetBSD (pci) Platform Version %d.%d Core Version %d.%d";
    317       1.55    mjacob #endif
    318       1.53    mjacob 
    319        1.1       cgd static int
    320   1.66.2.1   nathanw isp_pci_probe(struct device *parent, struct cfdata *match, void *aux)
    321       1.44    mjacob {
    322       1.44    mjacob 	struct pci_attach_args *pa = aux;
    323       1.44    mjacob 	switch (pa->pa_id) {
    324       1.36    mjacob #ifndef	ISP_DISABLE_1020_SUPPORT
    325       1.36    mjacob 	case PCI_QLOGIC_ISP:
    326       1.36    mjacob 		return (1);
    327       1.36    mjacob #endif
    328       1.36    mjacob #ifndef	ISP_DISABLE_1080_SUPPORT
    329       1.36    mjacob 	case PCI_QLOGIC_ISP1080:
    330       1.40    mjacob 	case PCI_QLOGIC_ISP1240:
    331       1.48    mjacob 	case PCI_QLOGIC_ISP1280:
    332       1.36    mjacob 		return (1);
    333       1.36    mjacob #endif
    334       1.50    mjacob #ifndef	ISP_DISABLE_12160_SUPPORT
    335       1.50    mjacob 	case PCI_QLOGIC_ISP12160:
    336       1.50    mjacob 		return (1);
    337       1.50    mjacob #endif
    338       1.36    mjacob #ifndef	ISP_DISABLE_2100_SUPPORT
    339       1.36    mjacob 	case PCI_QLOGIC_ISP2100:
    340        1.1       cgd 		return (1);
    341       1.36    mjacob #endif
    342       1.41    mjacob #ifndef	ISP_DISABLE_2200_SUPPORT
    343       1.41    mjacob 	case PCI_QLOGIC_ISP2200:
    344       1.41    mjacob 		return (1);
    345       1.41    mjacob #endif
    346   1.66.2.4   nathanw #ifndef	ISP_DISABLE_2300_SUPPORT
    347   1.66.2.4   nathanw 	case PCI_QLOGIC_ISP2300:
    348   1.66.2.4   nathanw 	case PCI_QLOGIC_ISP2312:
    349   1.66.2.4   nathanw 		return (1);
    350   1.66.2.4   nathanw #endif
    351       1.36    mjacob 	default:
    352        1.1       cgd 		return (0);
    353        1.1       cgd 	}
    354        1.1       cgd }
    355        1.1       cgd 
    356        1.1       cgd 
    357       1.44    mjacob static void
    358   1.66.2.1   nathanw isp_pci_attach(struct device *parent, struct device *self, void *aux)
    359        1.1       cgd {
    360       1.29    mjacob #ifdef	DEBUG
    361       1.27   thorpej 	static char oneshot = 1;
    362       1.27   thorpej #endif
    363  1.66.2.10   nathanw 	static const char nomem[] = "\n%s: no mem for sdparam table\n";
    364       1.47    mjacob 	u_int32_t data, rev, linesz = PCI_DFLT_LNSZ;
    365        1.1       cgd 	struct pci_attach_args *pa = aux;
    366        1.1       cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) self;
    367       1.21    mjacob 	struct ispsoftc *isp = &pcs->pci_isp;
    368       1.11       cgd 	bus_space_tag_t st, iot, memt;
    369       1.11       cgd 	bus_space_handle_t sh, ioh, memh;
    370        1.1       cgd 	pci_intr_handle_t ih;
    371  1.66.2.10   nathanw 	char *dstring;
    372        1.1       cgd 	const char *intrstr;
    373       1.53    mjacob 	int ioh_valid, memh_valid;
    374        1.1       cgd 
    375       1.12       cgd 	ioh_valid = (pci_mapreg_map(pa, IO_MAP_REG,
    376       1.11       cgd 	    PCI_MAPREG_TYPE_IO, 0,
    377       1.11       cgd 	    &iot, &ioh, NULL, NULL) == 0);
    378       1.12       cgd 	memh_valid = (pci_mapreg_map(pa, MEM_MAP_REG,
    379       1.11       cgd 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
    380       1.11       cgd 	    &memt, &memh, NULL, NULL) == 0);
    381       1.11       cgd 	if (memh_valid) {
    382       1.11       cgd 		st = memt;
    383       1.11       cgd 		sh = memh;
    384       1.11       cgd 	} else if (ioh_valid) {
    385       1.11       cgd 		st = iot;
    386       1.11       cgd 		sh = ioh;
    387        1.6       cgd 	} else {
    388       1.11       cgd 		printf(": unable to map device registers\n");
    389        1.9       cgd 		return;
    390        1.1       cgd 	}
    391  1.66.2.10   nathanw 	dstring = "\n";
    392        1.1       cgd 
    393        1.6       cgd 	pcs->pci_st = st;
    394        1.6       cgd 	pcs->pci_sh = sh;
    395       1.15    mjacob 	pcs->pci_pc = pa->pa_pc;
    396       1.15    mjacob 	pcs->pci_tag = pa->pa_tag;
    397       1.36    mjacob 	pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
    398       1.36    mjacob 	pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
    399       1.36    mjacob 	pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
    400       1.36    mjacob 	pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
    401       1.36    mjacob 	pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
    402       1.47    mjacob 	rev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG) & 0xff;
    403       1.36    mjacob 
    404       1.36    mjacob #ifndef	ISP_DISABLE_1020_SUPPORT
    405       1.15    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP) {
    406  1.66.2.10   nathanw 		dstring = ": QLogic 1020 Ultra Wide SCSI HBA\n";
    407       1.21    mjacob 		isp->isp_mdvec = &mdvec;
    408       1.21    mjacob 		isp->isp_type = ISP_HA_SCSI_UNKNOWN;
    409       1.21    mjacob 		isp->isp_param = malloc(sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    410       1.21    mjacob 		if (isp->isp_param == NULL) {
    411       1.40    mjacob 			printf(nomem, isp->isp_name);
    412       1.21    mjacob 			return;
    413       1.15    mjacob 		}
    414   1.66.2.3   nathanw 		memset(isp->isp_param, 0, sizeof (sdparam));
    415       1.36    mjacob 	}
    416       1.36    mjacob #endif
    417       1.36    mjacob #ifndef	ISP_DISABLE_1080_SUPPORT
    418       1.36    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP1080) {
    419  1.66.2.10   nathanw 		dstring = ": QLogic 1080 Ultra-2 Wide SCSI HBA\n";
    420       1.36    mjacob 		isp->isp_mdvec = &mdvec_1080;
    421       1.36    mjacob 		isp->isp_type = ISP_HA_SCSI_1080;
    422       1.36    mjacob 		isp->isp_param = malloc(sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    423       1.36    mjacob 		if (isp->isp_param == NULL) {
    424       1.40    mjacob 			printf(nomem, isp->isp_name);
    425       1.36    mjacob 			return;
    426       1.36    mjacob 		}
    427   1.66.2.3   nathanw 		memset(isp->isp_param, 0, sizeof (sdparam));
    428       1.36    mjacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
    429       1.36    mjacob 		    ISP1080_DMA_REGS_OFF;
    430       1.36    mjacob 	}
    431       1.40    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP1240) {
    432  1.66.2.10   nathanw 		dstring = ": QLogic Dual Channel Ultra Wide SCSI HBA\n";
    433       1.40    mjacob 		isp->isp_mdvec = &mdvec_1080;
    434       1.48    mjacob 		isp->isp_type = ISP_HA_SCSI_1240;
    435       1.48    mjacob 		isp->isp_param =
    436       1.48    mjacob 		    malloc(2 * sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    437       1.48    mjacob 		if (isp->isp_param == NULL) {
    438       1.48    mjacob 			printf(nomem, isp->isp_name);
    439       1.48    mjacob 			return;
    440       1.48    mjacob 		}
    441   1.66.2.3   nathanw 		memset(isp->isp_param, 0, 2 * sizeof (sdparam));
    442       1.48    mjacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
    443       1.48    mjacob 		    ISP1080_DMA_REGS_OFF;
    444       1.48    mjacob 	}
    445       1.48    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP1280) {
    446  1.66.2.10   nathanw 		dstring = ": QLogic Dual Channel Ultra-2 Wide SCSI HBA\n";
    447       1.48    mjacob 		isp->isp_mdvec = &mdvec_1080;
    448       1.48    mjacob 		isp->isp_type = ISP_HA_SCSI_1280;
    449       1.40    mjacob 		isp->isp_param =
    450       1.40    mjacob 		    malloc(2 * sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    451       1.40    mjacob 		if (isp->isp_param == NULL) {
    452       1.40    mjacob 			printf(nomem, isp->isp_name);
    453       1.40    mjacob 			return;
    454       1.40    mjacob 		}
    455   1.66.2.3   nathanw 		memset(isp->isp_param, 0, 2 * sizeof (sdparam));
    456       1.40    mjacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
    457       1.40    mjacob 		    ISP1080_DMA_REGS_OFF;
    458       1.40    mjacob 	}
    459       1.36    mjacob #endif
    460       1.50    mjacob #ifndef	ISP_DISABLE_12160_SUPPORT
    461       1.50    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP12160) {
    462  1.66.2.10   nathanw 		dstring = ": QLogic Dual Channel Ultra-3 Wide SCSI HBA\n";
    463       1.50    mjacob 		isp->isp_mdvec = &mdvec_12160;
    464       1.50    mjacob 		isp->isp_type = ISP_HA_SCSI_12160;
    465       1.50    mjacob 		isp->isp_param =
    466       1.50    mjacob 		    malloc(2 * sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    467       1.50    mjacob 		if (isp->isp_param == NULL) {
    468       1.50    mjacob 			printf(nomem, isp->isp_name);
    469       1.50    mjacob 			return;
    470       1.50    mjacob 		}
    471   1.66.2.3   nathanw 		memset(isp->isp_param, 0, 2 * sizeof (sdparam));
    472       1.50    mjacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
    473       1.50    mjacob 		    ISP1080_DMA_REGS_OFF;
    474       1.50    mjacob 	}
    475       1.50    mjacob #endif
    476       1.36    mjacob #ifndef	ISP_DISABLE_2100_SUPPORT
    477       1.36    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP2100) {
    478  1.66.2.10   nathanw 		dstring = ": QLogic FC-AL HBA\n";
    479       1.21    mjacob 		isp->isp_mdvec = &mdvec_2100;
    480       1.21    mjacob 		isp->isp_type = ISP_HA_FC_2100;
    481       1.21    mjacob 		isp->isp_param = malloc(sizeof (fcparam), M_DEVBUF, M_NOWAIT);
    482       1.21    mjacob 		if (isp->isp_param == NULL) {
    483       1.40    mjacob 			printf(nomem, isp->isp_name);
    484       1.21    mjacob 			return;
    485       1.15    mjacob 		}
    486   1.66.2.3   nathanw 		memset(isp->isp_param, 0, sizeof (fcparam));
    487       1.36    mjacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
    488       1.36    mjacob 		    PCI_MBOX_REGS2100_OFF;
    489       1.47    mjacob 		if (rev < 3) {
    490       1.39    mjacob 			/*
    491       1.39    mjacob 			 * XXX: Need to get the actual revision
    492       1.39    mjacob 			 * XXX: number of the 2100 FB. At any rate,
    493       1.39    mjacob 			 * XXX: lower cache line size for early revision
    494       1.39    mjacob 			 * XXX; boards.
    495       1.39    mjacob 			 */
    496       1.39    mjacob 			linesz = 1;
    497       1.39    mjacob 		}
    498       1.15    mjacob 	}
    499       1.36    mjacob #endif
    500       1.41    mjacob #ifndef	ISP_DISABLE_2200_SUPPORT
    501       1.41    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP2200) {
    502  1.66.2.10   nathanw 		dstring = ": QLogic FC-AL and Fabric HBA\n";
    503       1.41    mjacob 		isp->isp_mdvec = &mdvec_2200;
    504       1.41    mjacob 		isp->isp_type = ISP_HA_FC_2200;
    505       1.41    mjacob 		isp->isp_param = malloc(sizeof (fcparam), M_DEVBUF, M_NOWAIT);
    506       1.41    mjacob 		if (isp->isp_param == NULL) {
    507       1.41    mjacob 			printf(nomem, isp->isp_name);
    508       1.41    mjacob 			return;
    509       1.41    mjacob 		}
    510   1.66.2.3   nathanw 		memset(isp->isp_param, 0, sizeof (fcparam));
    511       1.41    mjacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
    512       1.41    mjacob 		    PCI_MBOX_REGS2100_OFF;
    513       1.41    mjacob 		data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG);
    514       1.41    mjacob 	}
    515       1.41    mjacob #endif
    516   1.66.2.4   nathanw #ifndef	ISP_DISABLE_2300_SUPPORT
    517   1.66.2.4   nathanw 	if (pa->pa_id == PCI_QLOGIC_ISP2300 ||
    518   1.66.2.4   nathanw 	    pa->pa_id == PCI_QLOGIC_ISP2312) {
    519   1.66.2.4   nathanw 		isp->isp_mdvec = &mdvec_2300;
    520   1.66.2.8   nathanw 		if (pa->pa_id == PCI_QLOGIC_ISP2300) {
    521  1.66.2.10   nathanw 			dstring = ": QLogic FC-AL and 2Gbps Fabric HBA\n";
    522   1.66.2.8   nathanw 			isp->isp_type = ISP_HA_FC_2300;
    523   1.66.2.8   nathanw 		} else {
    524  1.66.2.10   nathanw 			dstring =
    525  1.66.2.10   nathanw 			    ": QLogic Dual Port FC-AL and 2Gbps Fabric HBA\n";
    526   1.66.2.8   nathanw 			isp->isp_type = ISP_HA_FC_2312;
    527   1.66.2.8   nathanw 			isp->isp_port = pa->pa_function;
    528   1.66.2.8   nathanw 		}
    529   1.66.2.4   nathanw 		isp->isp_param = malloc(sizeof (fcparam), M_DEVBUF, M_NOWAIT);
    530   1.66.2.4   nathanw 		if (isp->isp_param == NULL) {
    531   1.66.2.4   nathanw 			printf(nomem, isp->isp_name);
    532   1.66.2.4   nathanw 			return;
    533   1.66.2.4   nathanw 		}
    534   1.66.2.4   nathanw 		memset(isp->isp_param, 0, sizeof (fcparam));
    535   1.66.2.4   nathanw 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
    536   1.66.2.4   nathanw 		    PCI_MBOX_REGS2300_OFF;
    537   1.66.2.4   nathanw 		data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG);
    538   1.66.2.4   nathanw 	}
    539   1.66.2.4   nathanw #endif
    540       1.53    mjacob 	/*
    541       1.53    mjacob 	 * Set up logging levels.
    542       1.53    mjacob 	 */
    543       1.53    mjacob #ifdef	ISP_LOGDEFAULT
    544       1.53    mjacob 	isp->isp_dblev = ISP_LOGDEFAULT;
    545       1.53    mjacob #else
    546       1.65    mjacob 	isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
    547       1.65    mjacob 	if (bootverbose)
    548       1.65    mjacob 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
    549       1.53    mjacob #ifdef	SCSIDEBUG
    550  1.66.2.10   nathanw 	isp->isp_dblev |= ISP_LOGDEBUG0|ISP_LOGDEBUG1|ISP_LOGDEBUG2;
    551       1.53    mjacob #endif
    552       1.53    mjacob #endif
    553  1.66.2.10   nathanw 	if (isp->isp_dblev & ISP_LOGCONFIG) {
    554  1.66.2.10   nathanw 		printf("\n");
    555  1.66.2.10   nathanw 	} else {
    556  1.66.2.10   nathanw 		printf(dstring);
    557  1.66.2.10   nathanw 	}
    558       1.57    mjacob 
    559       1.54    mjacob #ifdef	DEBUG
    560       1.53    mjacob 	if (oneshot) {
    561       1.53    mjacob 		oneshot = 0;
    562       1.53    mjacob 		isp_prt(isp, ISP_LOGCONFIG, vstring,
    563       1.53    mjacob 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
    564       1.53    mjacob 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
    565       1.53    mjacob 	}
    566       1.54    mjacob #endif
    567       1.53    mjacob 
    568   1.66.2.7   nathanw 	isp->isp_dmatag = pa->pa_dmat;
    569       1.47    mjacob 	isp->isp_revision = rev;
    570       1.36    mjacob 
    571       1.35    mjacob 	/*
    572       1.35    mjacob 	 * Make sure that command register set sanely.
    573       1.35    mjacob 	 */
    574       1.35    mjacob 	data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    575       1.35    mjacob 	data |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_INVALIDATE_ENABLE;
    576       1.36    mjacob 
    577       1.35    mjacob 	/*
    578       1.35    mjacob 	 * Not so sure about these- but I think it's important that they get
    579       1.35    mjacob 	 * enabled......
    580       1.35    mjacob 	 */
    581       1.35    mjacob 	data |= PCI_COMMAND_PARITY_ENABLE | PCI_COMMAND_SERR_ENABLE;
    582   1.66.2.8   nathanw 	if (IS_2300(isp)) {	/* per QLogic errata */
    583   1.66.2.8   nathanw 		data &= ~PCI_COMMAND_INVALIDATE_ENABLE;
    584   1.66.2.8   nathanw 	}
    585  1.66.2.10   nathanw 	if (IS_23XX(isp)) {
    586  1.66.2.10   nathanw 		isp->isp_touched = 1;
    587  1.66.2.10   nathanw 	}
    588       1.35    mjacob 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, data);
    589       1.36    mjacob 
    590       1.35    mjacob 	/*
    591       1.39    mjacob 	 * Make sure that the latency timer, cache line size,
    592       1.39    mjacob 	 * and ROM is disabled.
    593       1.35    mjacob 	 */
    594       1.35    mjacob 	data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    595       1.35    mjacob 	data &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    596       1.35    mjacob 	data &= ~(PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT);
    597       1.39    mjacob 	data |= (PCI_DFLT_LTNCY	<< PCI_LATTIMER_SHIFT);
    598       1.39    mjacob 	data |= (linesz << PCI_CACHELINE_SHIFT);
    599       1.35    mjacob 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, data);
    600       1.39    mjacob 
    601       1.39    mjacob 	data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCIR_ROMADDR);
    602       1.39    mjacob 	data &= ~1;
    603       1.39    mjacob 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCIR_ROMADDR, data);
    604       1.35    mjacob 
    605       1.64  sommerfe 	if (pci_intr_map(pa, &ih)) {
    606       1.21    mjacob 		printf("%s: couldn't map interrupt\n", isp->isp_name);
    607       1.21    mjacob 		free(isp->isp_param, M_DEVBUF);
    608        1.1       cgd 		return;
    609        1.1       cgd 	}
    610        1.1       cgd 	intrstr = pci_intr_string(pa->pa_pc, ih);
    611        1.1       cgd 	if (intrstr == NULL)
    612        1.1       cgd 		intrstr = "<I dunno>";
    613       1.44    mjacob 	pcs->pci_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
    614       1.44    mjacob 	    isp_pci_intr, isp);
    615        1.1       cgd 	if (pcs->pci_ih == NULL) {
    616        1.1       cgd 		printf("%s: couldn't establish interrupt at %s\n",
    617       1.21    mjacob 			isp->isp_name, intrstr);
    618       1.36    mjacob 		free(isp->isp_param, M_DEVBUF);
    619       1.36    mjacob 		return;
    620       1.36    mjacob 	}
    621       1.53    mjacob 
    622       1.36    mjacob 	printf("%s: interrupting at %s\n", isp->isp_name, intrstr);
    623       1.36    mjacob 
    624       1.43    mjacob 	if (IS_FC(isp)) {
    625       1.53    mjacob 		DEFAULT_NODEWWN(isp) = 0x400000007F000002;
    626       1.57    mjacob 		DEFAULT_PORTWWN(isp) = 0x400000007F000002;
    627       1.43    mjacob 	}
    628       1.41    mjacob 
    629       1.49    mjacob 	isp->isp_confopts = self->dv_cfdata->cf_flags;
    630       1.66    mjacob 	isp->isp_role = ISP_DEFAULT_ROLES;
    631       1.36    mjacob 	ISP_LOCK(isp);
    632       1.53    mjacob 	isp->isp_osinfo.no_mbox_ints = 1;
    633       1.36    mjacob 	isp_reset(isp);
    634       1.36    mjacob 	if (isp->isp_state != ISP_RESETSTATE) {
    635       1.36    mjacob 		ISP_UNLOCK(isp);
    636       1.36    mjacob 		free(isp->isp_param, M_DEVBUF);
    637       1.36    mjacob 		return;
    638       1.36    mjacob 	}
    639       1.53    mjacob 	ENABLE_INTS(isp);
    640       1.36    mjacob 	isp_init(isp);
    641       1.36    mjacob 	if (isp->isp_state != ISP_INITSTATE) {
    642       1.21    mjacob 		isp_uninit(isp);
    643       1.22    mjacob 		ISP_UNLOCK(isp);
    644       1.21    mjacob 		free(isp->isp_param, M_DEVBUF);
    645        1.1       cgd 		return;
    646        1.1       cgd 	}
    647        1.1       cgd 	/*
    648       1.53    mjacob 	 * Do platform attach.
    649        1.1       cgd 	 */
    650       1.53    mjacob 	ISP_UNLOCK(isp);
    651       1.21    mjacob 	isp_attach(isp);
    652       1.21    mjacob 	if (isp->isp_state != ISP_RUNSTATE) {
    653       1.53    mjacob 		ISP_LOCK(isp);
    654       1.21    mjacob 		isp_uninit(isp);
    655       1.21    mjacob 		free(isp->isp_param, M_DEVBUF);
    656       1.53    mjacob 		ISP_UNLOCK(isp);
    657        1.1       cgd 	}
    658        1.1       cgd }
    659        1.1       cgd 
    660   1.66.2.4   nathanw #define	IspVirt2Off(a, x)	\
    661   1.66.2.4   nathanw 	(((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
    662   1.66.2.4   nathanw 	_BLK_REG_SHFT] + ((x) & 0xff))
    663   1.66.2.4   nathanw 
    664   1.66.2.4   nathanw #define	BXR2(pcs, off)		\
    665   1.66.2.4   nathanw 	bus_space_read_2(pcs->pci_st, pcs->pci_sh, off)
    666   1.66.2.4   nathanw #define	BXW2(pcs, off, v)	\
    667   1.66.2.4   nathanw 	bus_space_write_2(pcs->pci_st, pcs->pci_sh, off, v)
    668   1.66.2.4   nathanw 
    669   1.66.2.4   nathanw 
    670   1.66.2.4   nathanw static INLINE int
    671   1.66.2.4   nathanw isp_pci_rd_debounced(struct ispsoftc *isp, int off, u_int16_t *rp)
    672   1.66.2.4   nathanw {
    673   1.66.2.4   nathanw 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    674   1.66.2.4   nathanw 	u_int16_t val0, val1;
    675   1.66.2.4   nathanw 	int i = 0;
    676   1.66.2.4   nathanw 
    677   1.66.2.4   nathanw 	do {
    678   1.66.2.4   nathanw 		val0 = BXR2(pcs, IspVirt2Off(isp, off));
    679   1.66.2.4   nathanw 		val1 = BXR2(pcs, IspVirt2Off(isp, off));
    680   1.66.2.4   nathanw 	} while (val0 != val1 && ++i < 1000);
    681   1.66.2.4   nathanw 	if (val0 != val1) {
    682   1.66.2.4   nathanw 		return (1);
    683   1.66.2.4   nathanw 	}
    684   1.66.2.4   nathanw 	*rp = val0;
    685   1.66.2.4   nathanw 	return (0);
    686   1.66.2.4   nathanw }
    687   1.66.2.4   nathanw 
    688   1.66.2.4   nathanw static int
    689   1.66.2.4   nathanw isp_pci_rd_isr(struct ispsoftc *isp, u_int16_t *isrp,
    690   1.66.2.4   nathanw     u_int16_t *semap, u_int16_t *mbp)
    691   1.66.2.4   nathanw {
    692   1.66.2.4   nathanw 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    693   1.66.2.4   nathanw 	u_int16_t isr, sema;
    694   1.66.2.4   nathanw 
    695   1.66.2.4   nathanw 	if (IS_2100(isp)) {
    696   1.66.2.4   nathanw 		if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
    697   1.66.2.4   nathanw 		    return (0);
    698   1.66.2.4   nathanw 		}
    699   1.66.2.4   nathanw 		if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
    700   1.66.2.4   nathanw 		    return (0);
    701   1.66.2.4   nathanw 		}
    702   1.66.2.4   nathanw 	} else {
    703   1.66.2.4   nathanw 		isr = BXR2(pcs, IspVirt2Off(isp, BIU_ISR));
    704   1.66.2.4   nathanw 		sema = BXR2(pcs, IspVirt2Off(isp, BIU_SEMA));
    705   1.66.2.4   nathanw 	}
    706   1.66.2.4   nathanw 	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
    707   1.66.2.4   nathanw 	isr &= INT_PENDING_MASK(isp);
    708   1.66.2.4   nathanw 	sema &= BIU_SEMA_LOCK;
    709   1.66.2.4   nathanw 	if (isr == 0 && sema == 0) {
    710   1.66.2.4   nathanw 		return (0);
    711   1.66.2.4   nathanw 	}
    712   1.66.2.4   nathanw 	*isrp = isr;
    713   1.66.2.4   nathanw 	if ((*semap = sema) != 0) {
    714   1.66.2.4   nathanw 		if (IS_2100(isp)) {
    715   1.66.2.4   nathanw 			if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
    716   1.66.2.4   nathanw 				return (0);
    717   1.66.2.4   nathanw 			}
    718   1.66.2.4   nathanw 		} else {
    719   1.66.2.4   nathanw 			*mbp = BXR2(pcs, IspVirt2Off(isp, OUTMAILBOX0));
    720   1.66.2.4   nathanw 		}
    721   1.66.2.4   nathanw 	}
    722   1.66.2.4   nathanw 	return (1);
    723   1.66.2.4   nathanw }
    724   1.66.2.4   nathanw 
    725   1.66.2.4   nathanw #ifndef	ISP_DISABLE_2300_SUPPORT
    726   1.66.2.4   nathanw static int
    727   1.66.2.4   nathanw isp_pci_rd_isr_2300(struct ispsoftc *isp, u_int16_t *isrp,
    728   1.66.2.4   nathanw     u_int16_t *semap, u_int16_t *mbox0p)
    729   1.66.2.4   nathanw {
    730   1.66.2.4   nathanw 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    731   1.66.2.4   nathanw 	u_int32_t r2hisr;
    732   1.66.2.4   nathanw 
    733   1.66.2.5   nathanw 	if (!(BXR2(pcs, IspVirt2Off(isp, BIU_ISR)) & BIU2100_ISR_RISC_INT)) {
    734   1.66.2.5   nathanw 		*isrp = 0;
    735   1.66.2.5   nathanw 		return (0);
    736   1.66.2.5   nathanw 	}
    737   1.66.2.4   nathanw 	r2hisr = bus_space_read_4(pcs->pci_st, pcs->pci_sh,
    738   1.66.2.4   nathanw 	    IspVirt2Off(pcs, BIU_R2HSTSLO));
    739   1.66.2.4   nathanw 	isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
    740   1.66.2.4   nathanw 	if ((r2hisr & BIU_R2HST_INTR) == 0) {
    741   1.66.2.4   nathanw 		*isrp = 0;
    742   1.66.2.4   nathanw 		return (0);
    743   1.66.2.4   nathanw 	}
    744   1.66.2.4   nathanw 	switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
    745   1.66.2.4   nathanw 	case ISPR2HST_ROM_MBX_OK:
    746   1.66.2.4   nathanw 	case ISPR2HST_ROM_MBX_FAIL:
    747   1.66.2.4   nathanw 	case ISPR2HST_MBX_OK:
    748   1.66.2.4   nathanw 	case ISPR2HST_MBX_FAIL:
    749   1.66.2.4   nathanw 	case ISPR2HST_ASYNC_EVENT:
    750  1.66.2.11   nathanw 		*isrp = r2hisr & 0xffff;
    751  1.66.2.11   nathanw 		*mbox0p = (r2hisr >> 16);
    752  1.66.2.11   nathanw 		*semap = 1;
    753  1.66.2.11   nathanw 		return (1);
    754   1.66.2.8   nathanw 	case ISPR2HST_RIO_16:
    755  1.66.2.11   nathanw 		*isrp = r2hisr & 0xffff;
    756  1.66.2.11   nathanw 		*mbox0p = ASYNC_RIO1;
    757  1.66.2.11   nathanw 		*semap = 1;
    758  1.66.2.11   nathanw 		return (1);
    759   1.66.2.4   nathanw 	case ISPR2HST_FPOST:
    760  1.66.2.11   nathanw 		*isrp = r2hisr & 0xffff;
    761  1.66.2.11   nathanw 		*mbox0p = ASYNC_CMD_CMPLT;
    762  1.66.2.11   nathanw 		*semap = 1;
    763  1.66.2.11   nathanw 		return (1);
    764   1.66.2.4   nathanw 	case ISPR2HST_FPOST_CTIO:
    765   1.66.2.4   nathanw 		*isrp = r2hisr & 0xffff;
    766  1.66.2.11   nathanw 		*mbox0p = ASYNC_CTIO_DONE;
    767   1.66.2.4   nathanw 		*semap = 1;
    768   1.66.2.4   nathanw 		return (1);
    769   1.66.2.4   nathanw 	case ISPR2HST_RSPQ_UPDATE:
    770   1.66.2.4   nathanw 		*isrp = r2hisr & 0xffff;
    771   1.66.2.4   nathanw 		*mbox0p = 0;
    772   1.66.2.4   nathanw 		*semap = 0;
    773   1.66.2.4   nathanw 		return (1);
    774   1.66.2.4   nathanw 	default:
    775   1.66.2.4   nathanw 		return (0);
    776   1.66.2.4   nathanw 	}
    777   1.66.2.4   nathanw }
    778   1.66.2.4   nathanw #endif
    779   1.66.2.4   nathanw 
    780        1.1       cgd static u_int16_t
    781   1.66.2.1   nathanw isp_pci_rd_reg(struct ispsoftc *isp, int regoff)
    782        1.1       cgd {
    783       1.15    mjacob 	u_int16_t rv;
    784        1.1       cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    785   1.66.2.4   nathanw 	int oldconf = 0;
    786       1.15    mjacob 
    787       1.36    mjacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
    788        1.1       cgd 		/*
    789       1.15    mjacob 		 * We will assume that someone has paused the RISC processor.
    790        1.1       cgd 		 */
    791   1.66.2.4   nathanw 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    792   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
    793   1.66.2.4   nathanw 		    oldconf | BIU_PCI_CONF1_SXP);
    794   1.66.2.4   nathanw 	}
    795   1.66.2.4   nathanw 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
    796       1.36    mjacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
    797   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
    798       1.15    mjacob 	}
    799       1.15    mjacob 	return (rv);
    800        1.1       cgd }
    801        1.1       cgd 
    802        1.1       cgd static void
    803   1.66.2.1   nathanw isp_pci_wr_reg(struct ispsoftc *isp, int regoff, u_int16_t val)
    804        1.1       cgd {
    805        1.1       cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    806   1.66.2.4   nathanw 	int oldconf = 0;
    807       1.36    mjacob 
    808       1.36    mjacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
    809        1.1       cgd 		/*
    810       1.15    mjacob 		 * We will assume that someone has paused the RISC processor.
    811        1.1       cgd 		 */
    812   1.66.2.4   nathanw 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    813   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
    814   1.66.2.4   nathanw 		    oldconf | BIU_PCI_CONF1_SXP);
    815   1.66.2.4   nathanw 	}
    816   1.66.2.4   nathanw 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
    817       1.36    mjacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
    818   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
    819       1.36    mjacob 	}
    820       1.36    mjacob }
    821       1.36    mjacob 
    822       1.52    mjacob #if !(defined(ISP_DISABLE_1080_SUPPORT) && defined(ISP_DISABLE_12160_SUPPORT))
    823       1.36    mjacob static u_int16_t
    824   1.66.2.1   nathanw isp_pci_rd_reg_1080(struct ispsoftc *isp, int regoff)
    825       1.36    mjacob {
    826       1.48    mjacob 	u_int16_t rv, oc = 0;
    827       1.36    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    828       1.36    mjacob 
    829   1.66.2.4   nathanw 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
    830   1.66.2.4   nathanw 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
    831       1.48    mjacob 		u_int16_t tc;
    832       1.36    mjacob 		/*
    833       1.36    mjacob 		 * We will assume that someone has paused the RISC processor.
    834       1.36    mjacob 		 */
    835   1.66.2.4   nathanw 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    836       1.48    mjacob 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
    837   1.66.2.4   nathanw 		if (regoff & SXP_BANK1_SELECT)
    838   1.66.2.4   nathanw 			tc |= BIU_PCI1080_CONF1_SXP1;
    839   1.66.2.4   nathanw 		else
    840       1.48    mjacob 			tc |= BIU_PCI1080_CONF1_SXP0;
    841   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
    842       1.36    mjacob 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
    843   1.66.2.4   nathanw 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    844   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
    845   1.66.2.4   nathanw 		    oc | BIU_PCI1080_CONF1_DMA);
    846   1.66.2.4   nathanw 	}
    847   1.66.2.4   nathanw 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
    848       1.48    mjacob 	if (oc) {
    849   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
    850       1.36    mjacob 	}
    851       1.36    mjacob 	return (rv);
    852       1.36    mjacob }
    853       1.36    mjacob 
    854       1.36    mjacob static void
    855   1.66.2.1   nathanw isp_pci_wr_reg_1080(struct ispsoftc *isp, int regoff, u_int16_t val)
    856       1.36    mjacob {
    857       1.36    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    858   1.66.2.4   nathanw 	int oc = 0;
    859       1.36    mjacob 
    860   1.66.2.4   nathanw 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
    861   1.66.2.4   nathanw 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
    862       1.48    mjacob 		u_int16_t tc;
    863       1.36    mjacob 		/*
    864       1.36    mjacob 		 * We will assume that someone has paused the RISC processor.
    865       1.36    mjacob 		 */
    866   1.66.2.4   nathanw 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    867       1.48    mjacob 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
    868   1.66.2.4   nathanw 		if (regoff & SXP_BANK1_SELECT)
    869   1.66.2.4   nathanw 			tc |= BIU_PCI1080_CONF1_SXP1;
    870   1.66.2.4   nathanw 		else
    871       1.48    mjacob 			tc |= BIU_PCI1080_CONF1_SXP0;
    872   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
    873       1.36    mjacob 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
    874   1.66.2.4   nathanw 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    875   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
    876   1.66.2.4   nathanw 		    oc | BIU_PCI1080_CONF1_DMA);
    877   1.66.2.4   nathanw 	}
    878   1.66.2.4   nathanw 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
    879       1.48    mjacob 	if (oc) {
    880   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
    881       1.15    mjacob 	}
    882        1.1       cgd }
    883       1.36    mjacob #endif
    884        1.1       cgd 
    885       1.13   thorpej static int
    886   1.66.2.1   nathanw isp_pci_mbxdma(struct ispsoftc *isp)
    887        1.1       cgd {
    888       1.53    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
    889   1.66.2.7   nathanw 	bus_dma_tag_t dmat = isp->isp_dmatag;
    890       1.53    mjacob 	bus_dma_segment_t sg;
    891       1.13   thorpej 	bus_size_t len;
    892       1.15    mjacob 	fcparam *fcp;
    893       1.53    mjacob 	int rs, i;
    894       1.13   thorpej 
    895       1.43    mjacob 	if (isp->isp_rquest_dma)	/* been here before? */
    896       1.43    mjacob 		return (0);
    897       1.43    mjacob 
    898   1.66.2.2   nathanw 	len = isp->isp_maxcmds * sizeof (XS_T *);
    899       1.53    mjacob 	isp->isp_xflist = (XS_T **) malloc(len, M_DEVBUF, M_WAITOK);
    900       1.43    mjacob 	if (isp->isp_xflist == NULL) {
    901       1.53    mjacob 		isp_prt(isp, ISP_LOGERR, "cannot malloc xflist array");
    902       1.43    mjacob 		return (1);
    903       1.43    mjacob 	}
    904   1.66.2.3   nathanw 	memset(isp->isp_xflist, 0, len);
    905       1.45    mjacob 	len = isp->isp_maxcmds * sizeof (bus_dmamap_t);
    906       1.53    mjacob 	pcs->pci_xfer_dmap = (bus_dmamap_t *) malloc(len, M_DEVBUF, M_WAITOK);
    907       1.53    mjacob 	if (pcs->pci_xfer_dmap == NULL) {
    908       1.53    mjacob 		free(isp->isp_xflist, M_DEVBUF);
    909       1.53    mjacob 		isp->isp_xflist = NULL;
    910       1.53    mjacob 		isp_prt(isp, ISP_LOGERR, "cannot malloc dma map array");
    911       1.53    mjacob 		return (1);
    912       1.53    mjacob 	}
    913       1.53    mjacob 	for (i = 0; i < isp->isp_maxcmds; i++) {
    914       1.59   thorpej 		if (bus_dmamap_create(dmat, MAXPHYS, (MAXPHYS / PAGE_SIZE) + 1,
    915       1.53    mjacob 		    MAXPHYS, 0, BUS_DMA_NOWAIT, &pcs->pci_xfer_dmap[i])) {
    916       1.53    mjacob 			isp_prt(isp, ISP_LOGERR, "cannot create dma maps");
    917       1.53    mjacob 			break;
    918       1.53    mjacob 		}
    919       1.53    mjacob 	}
    920       1.53    mjacob 	if (i < isp->isp_maxcmds) {
    921       1.53    mjacob 		while (--i >= 0) {
    922       1.53    mjacob 			bus_dmamap_destroy(dmat, pcs->pci_xfer_dmap[i]);
    923       1.53    mjacob 		}
    924       1.53    mjacob 		free(isp->isp_xflist, M_DEVBUF);
    925       1.53    mjacob 		free(pcs->pci_xfer_dmap, M_DEVBUF);
    926       1.53    mjacob 		isp->isp_xflist = NULL;
    927       1.53    mjacob 		pcs->pci_xfer_dmap = NULL;
    928       1.45    mjacob 		return (1);
    929       1.45    mjacob 	}
    930       1.43    mjacob 
    931       1.13   thorpej 	/*
    932       1.13   thorpej 	 * Allocate and map the request queue.
    933       1.13   thorpej 	 */
    934       1.53    mjacob 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
    935       1.59   thorpej 	if (bus_dmamem_alloc(dmat, len, PAGE_SIZE, 0, &sg, 1, &rs,
    936       1.59   thorpej 			     BUS_DMA_NOWAIT) ||
    937   1.66.2.7   nathanw 	    bus_dmamem_map(isp->isp_dmatag, &sg, rs, len,
    938       1.53    mjacob 	    (caddr_t *)&isp->isp_rquest, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    939       1.53    mjacob 		goto dmafail;
    940       1.53    mjacob 	}
    941       1.53    mjacob 
    942       1.53    mjacob 	if (bus_dmamap_create(dmat, len, 1, len, 0, BUS_DMA_NOWAIT,
    943   1.66.2.7   nathanw 	    &isp->isp_rqdmap) || bus_dmamap_load(dmat, isp->isp_rqdmap,
    944   1.66.2.7   nathanw 	    (caddr_t)isp->isp_rquest, len, NULL,
    945       1.53    mjacob 	    BUS_DMA_NOWAIT)) {
    946       1.53    mjacob 		goto dmafail;
    947       1.53    mjacob 	}
    948   1.66.2.7   nathanw 	isp->isp_rquest_dma = isp->isp_rqdmap->dm_segs[0].ds_addr;
    949       1.13   thorpej 
    950       1.13   thorpej 	/*
    951       1.13   thorpej 	 * Allocate and map the result queue.
    952       1.13   thorpej 	 */
    953       1.53    mjacob 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
    954       1.59   thorpej 	if (bus_dmamem_alloc(dmat, len, PAGE_SIZE, 0, &sg, 1, &rs,
    955       1.59   thorpej 			     BUS_DMA_NOWAIT) ||
    956       1.53    mjacob 	    bus_dmamem_map(dmat, &sg, rs, len, (caddr_t *)&isp->isp_result,
    957       1.53    mjacob 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    958       1.53    mjacob 		goto dmafail;
    959       1.53    mjacob 	}
    960       1.53    mjacob 	if (bus_dmamap_create(dmat, len, 1, len, 0, BUS_DMA_NOWAIT,
    961   1.66.2.7   nathanw 	    &isp->isp_rsdmap) || bus_dmamap_load(isp->isp_dmatag,
    962   1.66.2.7   nathanw 	    isp->isp_rsdmap, (caddr_t)isp->isp_result, len, NULL,
    963       1.53    mjacob 	    BUS_DMA_NOWAIT)) {
    964       1.53    mjacob 		goto dmafail;
    965       1.53    mjacob 	}
    966   1.66.2.7   nathanw 	isp->isp_result_dma = isp->isp_rsdmap->dm_segs[0].ds_addr;
    967        1.1       cgd 
    968       1.41    mjacob 	if (IS_SCSI(isp)) {
    969       1.15    mjacob 		return (0);
    970       1.15    mjacob 	}
    971        1.1       cgd 
    972       1.15    mjacob 	fcp = isp->isp_param;
    973       1.15    mjacob 	len = ISP2100_SCRLEN;
    974       1.59   thorpej 	if (bus_dmamem_alloc(dmat, len, PAGE_SIZE, 0, &sg, 1, &rs,
    975       1.59   thorpej 			     BUS_DMA_NOWAIT) ||
    976       1.53    mjacob 	    bus_dmamem_map(dmat, &sg, rs, len, (caddr_t *)&fcp->isp_scratch,
    977       1.53    mjacob 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    978       1.53    mjacob 		goto dmafail;
    979       1.53    mjacob 	}
    980       1.53    mjacob 	if (bus_dmamap_create(dmat, len, 1, len, 0, BUS_DMA_NOWAIT,
    981   1.66.2.7   nathanw 	    &isp->isp_scdmap) || bus_dmamap_load(dmat,
    982   1.66.2.7   nathanw 	    isp->isp_scdmap, (caddr_t)fcp->isp_scratch, len, NULL,
    983       1.53    mjacob 	    BUS_DMA_NOWAIT)) {
    984       1.53    mjacob 		goto dmafail;
    985       1.53    mjacob 	}
    986   1.66.2.7   nathanw 	fcp->isp_scdma = isp->isp_scdmap->dm_segs[0].ds_addr;
    987       1.13   thorpej 	return (0);
    988       1.53    mjacob dmafail:
    989       1.53    mjacob 	isp_prt(isp, ISP_LOGERR, "mailbox dma setup failure");
    990       1.53    mjacob 	for (i = 0; i < isp->isp_maxcmds; i++) {
    991       1.53    mjacob 		bus_dmamap_destroy(dmat, pcs->pci_xfer_dmap[i]);
    992       1.53    mjacob 	}
    993       1.53    mjacob 	free(isp->isp_xflist, M_DEVBUF);
    994       1.53    mjacob 	free(pcs->pci_xfer_dmap, M_DEVBUF);
    995       1.53    mjacob 	isp->isp_xflist = NULL;
    996       1.53    mjacob 	pcs->pci_xfer_dmap = NULL;
    997       1.53    mjacob 	return (1);
    998        1.1       cgd }
    999        1.1       cgd 
   1000        1.1       cgd static int
   1001   1.66.2.1   nathanw isp_pci_dmasetup(struct ispsoftc *isp, struct scsipi_xfer *xs, ispreq_t *rq,
   1002   1.66.2.7   nathanw     u_int16_t *nxtip, u_int16_t optr)
   1003        1.1       cgd {
   1004       1.53    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
   1005       1.52    mjacob 	bus_dmamap_t dmap;
   1006   1.66.2.7   nathanw 	u_int16_t starti = isp->isp_reqidx, nxti = *nxtip;
   1007   1.66.2.7   nathanw 	ispreq_t *qep;
   1008       1.17    mjacob 	int segcnt, seg, error, ovseg, seglim, drq;
   1009        1.1       cgd 
   1010   1.66.2.7   nathanw 	qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, starti);
   1011       1.53    mjacob 	dmap = pcs->pci_xfer_dmap[isp_handle_index(rq->req_handle)];
   1012        1.1       cgd 	if (xs->datalen == 0) {
   1013        1.1       cgd 		rq->req_seg_count = 1;
   1014       1.26    mjacob 		goto mbxsync;
   1015        1.1       cgd 	}
   1016       1.42   thorpej 	if (xs->xs_control & XS_CTL_DATA_IN) {
   1017       1.17    mjacob 		drq = REQFLAG_DATA_IN;
   1018        1.1       cgd 	} else {
   1019       1.17    mjacob 		drq = REQFLAG_DATA_OUT;
   1020        1.1       cgd 	}
   1021        1.1       cgd 
   1022       1.41    mjacob 	if (IS_FC(isp)) {
   1023       1.15    mjacob 		seglim = ISP_RQDSEG_T2;
   1024       1.15    mjacob 		((ispreqt2_t *)rq)->req_totalcnt = xs->datalen;
   1025       1.17    mjacob 		((ispreqt2_t *)rq)->req_flags |= drq;
   1026       1.15    mjacob 	} else {
   1027       1.17    mjacob 		rq->req_flags |= drq;
   1028       1.51    mjacob 		if (XS_CDBLEN(xs) > 12) {
   1029       1.51    mjacob 			seglim = 0;
   1030       1.51    mjacob 		} else {
   1031       1.51    mjacob 			seglim = ISP_RQDSEG;
   1032       1.51    mjacob 		}
   1033       1.15    mjacob 	}
   1034   1.66.2.7   nathanw 	error = bus_dmamap_load(isp->isp_dmatag, dmap, xs->data, xs->datalen,
   1035   1.66.2.1   nathanw 	    NULL, ((xs->xs_control & XS_CTL_NOSLEEP) ?
   1036   1.66.2.3   nathanw 	    BUS_DMA_NOWAIT : BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
   1037   1.66.2.3   nathanw 	    ((xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMA_READ : BUS_DMA_WRITE));
   1038       1.21    mjacob 	if (error) {
   1039  1.66.2.11   nathanw 		isp_prt(isp, ISP_LOGWARN, "unable to load dma (%d)", error);
   1040       1.21    mjacob 		XS_SETERR(xs, HBA_BOTCH);
   1041  1.66.2.10   nathanw 		if (error == EAGAIN || error == ENOMEM)
   1042  1.66.2.10   nathanw 			return (CMD_EAGAIN);
   1043  1.66.2.10   nathanw 		else
   1044  1.66.2.10   nathanw 			return (CMD_COMPLETE);
   1045       1.21    mjacob 	}
   1046       1.13   thorpej 
   1047       1.13   thorpej 	segcnt = dmap->dm_nsegs;
   1048       1.13   thorpej 
   1049       1.57    mjacob 	isp_prt(isp, ISP_LOGDEBUG2, "%d byte %s %p in %d segs",
   1050       1.57    mjacob 	    xs->datalen, (xs->xs_control & XS_CTL_DATA_IN)? "read to" :
   1051       1.57    mjacob 	    "write from", xs->data, segcnt);
   1052       1.57    mjacob 
   1053       1.13   thorpej 	for (seg = 0, rq->req_seg_count = 0;
   1054       1.56    mjacob 	    seglim && seg < segcnt && rq->req_seg_count < seglim;
   1055       1.44    mjacob 	    seg++, rq->req_seg_count++) {
   1056       1.41    mjacob 		if (IS_FC(isp)) {
   1057       1.15    mjacob 			ispreqt2_t *rq2 = (ispreqt2_t *)rq;
   1058       1.15    mjacob 			rq2->req_dataseg[rq2->req_seg_count].ds_count =
   1059       1.15    mjacob 			    dmap->dm_segs[seg].ds_len;
   1060       1.15    mjacob 			rq2->req_dataseg[rq2->req_seg_count].ds_base =
   1061       1.15    mjacob 			    dmap->dm_segs[seg].ds_addr;
   1062       1.15    mjacob 		} else {
   1063       1.15    mjacob 			rq->req_dataseg[rq->req_seg_count].ds_count =
   1064       1.15    mjacob 			    dmap->dm_segs[seg].ds_len;
   1065       1.15    mjacob 			rq->req_dataseg[rq->req_seg_count].ds_base =
   1066       1.15    mjacob 			    dmap->dm_segs[seg].ds_addr;
   1067       1.15    mjacob 		}
   1068       1.63    mjacob 		isp_prt(isp, ISP_LOGDEBUG2, "seg0.[%d]={0x%lx,%lu}",
   1069       1.63    mjacob 		    rq->req_seg_count, (long) dmap->dm_segs[seg].ds_addr,
   1070       1.63    mjacob 		    (unsigned long) dmap->dm_segs[seg].ds_len);
   1071        1.1       cgd 	}
   1072        1.1       cgd 
   1073   1.66.2.7   nathanw 	if (seg == segcnt) {
   1074       1.26    mjacob 		goto dmasync;
   1075   1.66.2.7   nathanw 	}
   1076        1.1       cgd 
   1077        1.1       cgd 	do {
   1078   1.66.2.7   nathanw 		u_int16_t onxti;
   1079   1.66.2.7   nathanw 		ispcontreq_t *crq, *cqe, local;
   1080   1.66.2.7   nathanw 
   1081   1.66.2.7   nathanw 		crq = &local;
   1082   1.66.2.7   nathanw 
   1083   1.66.2.7   nathanw 		cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
   1084   1.66.2.7   nathanw 		onxti = nxti;
   1085   1.66.2.7   nathanw 		nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
   1086   1.66.2.7   nathanw 		if (nxti == optr) {
   1087  1.66.2.11   nathanw 			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGERR, "Request Queue Overflow++");
   1088   1.66.2.7   nathanw 			bus_dmamap_unload(isp->isp_dmatag, dmap);
   1089       1.21    mjacob 			XS_SETERR(xs, HBA_BOTCH);
   1090       1.52    mjacob 			return (CMD_EAGAIN);
   1091        1.1       cgd 		}
   1092        1.1       cgd 		rq->req_header.rqs_entry_count++;
   1093   1.66.2.3   nathanw 		memset((void *)crq, 0, sizeof (*crq));
   1094        1.1       cgd 		crq->req_header.rqs_entry_count = 1;
   1095        1.1       cgd 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
   1096       1.13   thorpej 
   1097       1.13   thorpej 		for (ovseg = 0; seg < segcnt && ovseg < ISP_CDSEG;
   1098       1.13   thorpej 		    rq->req_seg_count++, seg++, ovseg++) {
   1099       1.13   thorpej 			crq->req_dataseg[ovseg].ds_count =
   1100       1.13   thorpej 			    dmap->dm_segs[seg].ds_len;
   1101       1.13   thorpej 			crq->req_dataseg[ovseg].ds_base =
   1102       1.13   thorpej 			    dmap->dm_segs[seg].ds_addr;
   1103       1.63    mjacob 			isp_prt(isp, ISP_LOGDEBUG2, "seg%d.[%d]={0x%lx,%lu}",
   1104       1.57    mjacob 			    rq->req_header.rqs_entry_count - 1,
   1105       1.63    mjacob 			    rq->req_seg_count, (long)dmap->dm_segs[seg].ds_addr,
   1106       1.63    mjacob 			    (unsigned long) dmap->dm_segs[seg].ds_len);
   1107        1.1       cgd 		}
   1108   1.66.2.7   nathanw 		isp_put_cont_req(isp, crq, cqe);
   1109   1.66.2.7   nathanw 		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
   1110       1.13   thorpej 	} while (seg < segcnt);
   1111       1.56    mjacob 
   1112       1.13   thorpej 
   1113       1.26    mjacob dmasync:
   1114   1.66.2.7   nathanw 	bus_dmamap_sync(isp->isp_dmatag, dmap, 0, dmap->dm_mapsize,
   1115       1.42   thorpej 	    (xs->xs_control & XS_CTL_DATA_IN) ?  BUS_DMASYNC_PREREAD :
   1116       1.30    mjacob 	    BUS_DMASYNC_PREWRITE);
   1117       1.26    mjacob 
   1118       1.26    mjacob mbxsync:
   1119   1.66.2.7   nathanw 	switch (rq->req_header.rqs_entry_type) {
   1120   1.66.2.7   nathanw 	case RQSTYPE_REQUEST:
   1121   1.66.2.7   nathanw 		isp_put_request(isp, rq, qep);
   1122   1.66.2.7   nathanw 		break;
   1123   1.66.2.7   nathanw 	case RQSTYPE_CMDONLY:
   1124   1.66.2.7   nathanw 		isp_put_extended_request(isp, (ispextreq_t *)rq,
   1125   1.66.2.7   nathanw 		    (ispextreq_t *)qep);
   1126   1.66.2.7   nathanw 		break;
   1127   1.66.2.7   nathanw 	case RQSTYPE_T2RQS:
   1128   1.66.2.7   nathanw 		isp_put_request_t2(isp, (ispreqt2_t *) rq, (ispreqt2_t *) qep);
   1129   1.66.2.7   nathanw 		break;
   1130   1.66.2.7   nathanw 	}
   1131   1.66.2.7   nathanw 	*nxtip = nxti;
   1132       1.30    mjacob 	return (CMD_QUEUED);
   1133       1.26    mjacob }
   1134       1.26    mjacob 
   1135       1.26    mjacob static int
   1136   1.66.2.1   nathanw isp_pci_intr(void *arg)
   1137       1.26    mjacob {
   1138   1.66.2.4   nathanw 	u_int16_t isr, sema, mbox;
   1139   1.66.2.4   nathanw 	struct ispsoftc *isp = arg;
   1140   1.66.2.4   nathanw 
   1141   1.66.2.4   nathanw 	isp->isp_intcnt++;
   1142   1.66.2.4   nathanw 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
   1143   1.66.2.4   nathanw 		isp->isp_intbogus++;
   1144   1.66.2.4   nathanw 		return (0);
   1145   1.66.2.4   nathanw 	} else {
   1146   1.66.2.4   nathanw 		isp->isp_osinfo.onintstack = 1;
   1147   1.66.2.4   nathanw 		isp_intr(isp, isr, sema, mbox);
   1148   1.66.2.4   nathanw 		isp->isp_osinfo.onintstack = 0;
   1149   1.66.2.4   nathanw 		return (1);
   1150   1.66.2.4   nathanw 	}
   1151       1.13   thorpej }
   1152       1.13   thorpej 
   1153       1.13   thorpej static void
   1154   1.66.2.1   nathanw isp_pci_dmateardown(struct ispsoftc *isp, XS_T *xs, u_int16_t handle)
   1155       1.13   thorpej {
   1156       1.53    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
   1157       1.53    mjacob 	bus_dmamap_t dmap = pcs->pci_xfer_dmap[isp_handle_index(handle)];
   1158   1.66.2.7   nathanw 	bus_dmamap_sync(isp->isp_dmatag, dmap, 0, dmap->dm_mapsize,
   1159       1.42   thorpej 	    xs->xs_control & XS_CTL_DATA_IN ?
   1160       1.13   thorpej 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1161   1.66.2.7   nathanw 	bus_dmamap_unload(isp->isp_dmatag, dmap);
   1162        1.1       cgd }
   1163        1.1       cgd 
   1164        1.1       cgd static void
   1165   1.66.2.1   nathanw isp_pci_reset1(struct ispsoftc *isp)
   1166        1.1       cgd {
   1167        1.1       cgd 	/* Make sure the BIOS is disabled */
   1168        1.1       cgd 	isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
   1169   1.66.2.8   nathanw 	if (isp->isp_osinfo.no_mbox_ints == 0) {
   1170   1.66.2.8   nathanw 		ENABLE_INTS(isp);
   1171   1.66.2.8   nathanw 	}
   1172   1.66.2.8   nathanw 
   1173       1.15    mjacob }
   1174       1.15    mjacob 
   1175       1.15    mjacob static void
   1176   1.66.2.1   nathanw isp_pci_dumpregs(struct ispsoftc *isp, const char *msg)
   1177       1.15    mjacob {
   1178       1.53    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
   1179       1.53    mjacob 	if (msg)
   1180       1.53    mjacob 		printf("%s: %s\n", isp->isp_name, msg);
   1181       1.53    mjacob 	if (IS_SCSI(isp))
   1182       1.53    mjacob 		printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
   1183       1.53    mjacob 	else
   1184       1.53    mjacob 		printf("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
   1185       1.53    mjacob 	printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
   1186       1.53    mjacob 	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
   1187       1.53    mjacob 	printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
   1188       1.53    mjacob 
   1189       1.53    mjacob 
   1190       1.53    mjacob 	if (IS_SCSI(isp)) {
   1191       1.53    mjacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   1192       1.53    mjacob 		printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
   1193       1.53    mjacob 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
   1194       1.53    mjacob 			ISP_READ(isp, CDMA_FIFO_STS));
   1195       1.53    mjacob 		printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
   1196       1.53    mjacob 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
   1197       1.53    mjacob 			ISP_READ(isp, DDMA_FIFO_STS));
   1198       1.53    mjacob 		printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
   1199       1.53    mjacob 			ISP_READ(isp, SXP_INTERRUPT),
   1200       1.53    mjacob 			ISP_READ(isp, SXP_GROSS_ERR),
   1201       1.53    mjacob 			ISP_READ(isp, SXP_PINS_CTRL));
   1202       1.53    mjacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
   1203       1.53    mjacob 	}
   1204       1.53    mjacob 	printf("    mbox regs: %x %x %x %x %x\n",
   1205       1.53    mjacob 	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
   1206       1.53    mjacob 	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
   1207       1.53    mjacob 	    ISP_READ(isp, OUTMAILBOX4));
   1208       1.53    mjacob 	printf("    PCI Status Command/Status=%x\n",
   1209       1.53    mjacob 	    pci_conf_read(pcs->pci_pc, pcs->pci_tag, PCI_COMMAND_STATUS_REG));
   1210        1.1       cgd }
   1211