Home | History | Annotate | Line # | Download | only in pci
isp_pci.c revision 1.66.2.14
      1  1.66.2.11   nathanw /* $NetBSD: isp_pci.c,v 1.66.2.14 2002/12/11 06:38:19 thorpej 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.14 2002/12/11 06:38:19 thorpej 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.66.2.13   nathanw #ifndef	PCI_PRODUCT_QLOGIC_ISP10160
    243  1.66.2.13   nathanw #define	PCI_PRODUCT_QLOGIC_ISP10160	0x1016
    244  1.66.2.13   nathanw #endif
    245  1.66.2.13   nathanw 
    246       1.50    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP12160
    247       1.53    mjacob #define	PCI_PRODUCT_QLOGIC_ISP12160	0x1216
    248       1.50    mjacob #endif
    249       1.50    mjacob 
    250       1.15    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2100
    251       1.15    mjacob #define	PCI_PRODUCT_QLOGIC_ISP2100	0x2100
    252       1.15    mjacob #endif
    253       1.36    mjacob 
    254       1.41    mjacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2200
    255       1.41    mjacob #define	PCI_PRODUCT_QLOGIC_ISP2200	0x2200
    256       1.41    mjacob #endif
    257       1.41    mjacob 
    258   1.66.2.4   nathanw #ifndef	PCI_PRODUCT_QLOGIC_ISP2300
    259   1.66.2.4   nathanw #define	PCI_PRODUCT_QLOGIC_ISP2300	0x2300
    260   1.66.2.4   nathanw #endif
    261   1.66.2.4   nathanw 
    262   1.66.2.4   nathanw #ifndef	PCI_PRODUCT_QLOGIC_ISP2312
    263   1.66.2.4   nathanw #define	PCI_PRODUCT_QLOGIC_ISP2312	0x2312
    264   1.66.2.4   nathanw #endif
    265   1.66.2.4   nathanw 
    266       1.36    mjacob #define	PCI_QLOGIC_ISP	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
    267       1.36    mjacob 
    268       1.36    mjacob #define	PCI_QLOGIC_ISP1080	\
    269       1.36    mjacob 	((PCI_PRODUCT_QLOGIC_ISP1080 << 16) | PCI_VENDOR_QLOGIC)
    270       1.36    mjacob 
    271       1.36    mjacob #define	PCI_QLOGIC_ISP1240	\
    272       1.36    mjacob 	((PCI_PRODUCT_QLOGIC_ISP1240 << 16) | PCI_VENDOR_QLOGIC)
    273       1.36    mjacob 
    274       1.48    mjacob #define	PCI_QLOGIC_ISP1280	\
    275       1.48    mjacob 	((PCI_PRODUCT_QLOGIC_ISP1280 << 16) | PCI_VENDOR_QLOGIC)
    276       1.48    mjacob 
    277  1.66.2.13   nathanw #define	PCI_QLOGIC_ISP10160	\
    278  1.66.2.13   nathanw 	((PCI_PRODUCT_QLOGIC_ISP10160 << 16) | PCI_VENDOR_QLOGIC)
    279  1.66.2.13   nathanw 
    280       1.50    mjacob #define	PCI_QLOGIC_ISP12160	\
    281       1.50    mjacob 	((PCI_PRODUCT_QLOGIC_ISP12160 << 16) | PCI_VENDOR_QLOGIC)
    282       1.50    mjacob 
    283       1.15    mjacob #define	PCI_QLOGIC_ISP2100	\
    284       1.15    mjacob 	((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
    285       1.15    mjacob 
    286       1.41    mjacob #define	PCI_QLOGIC_ISP2200	\
    287       1.41    mjacob 	((PCI_PRODUCT_QLOGIC_ISP2200 << 16) | PCI_VENDOR_QLOGIC)
    288       1.41    mjacob 
    289   1.66.2.4   nathanw #define	PCI_QLOGIC_ISP2300	\
    290   1.66.2.4   nathanw 	((PCI_PRODUCT_QLOGIC_ISP2300 << 16) | PCI_VENDOR_QLOGIC)
    291   1.66.2.4   nathanw 
    292   1.66.2.4   nathanw #define	PCI_QLOGIC_ISP2312	\
    293   1.66.2.4   nathanw 	((PCI_PRODUCT_QLOGIC_ISP2312 << 16) | PCI_VENDOR_QLOGIC)
    294   1.66.2.4   nathanw 
    295       1.44    mjacob #define	IO_MAP_REG	0x10
    296       1.44    mjacob #define	MEM_MAP_REG	0x14
    297       1.39    mjacob #define	PCIR_ROMADDR	0x30
    298       1.39    mjacob 
    299       1.39    mjacob #define	PCI_DFLT_LTNCY	0x40
    300       1.39    mjacob #define	PCI_DFLT_LNSZ	0x10
    301        1.6       cgd 
    302        1.1       cgd 
    303   1.66.2.1   nathanw static int isp_pci_probe(struct device *, struct cfdata *, void *);
    304   1.66.2.1   nathanw static void isp_pci_attach(struct device *, struct device *, void *);
    305        1.1       cgd 
    306        1.1       cgd struct isp_pcisoftc {
    307        1.1       cgd 	struct ispsoftc		pci_isp;
    308       1.15    mjacob 	pci_chipset_tag_t	pci_pc;
    309       1.15    mjacob 	pcitag_t		pci_tag;
    310        1.6       cgd 	bus_space_tag_t		pci_st;
    311        1.6       cgd 	bus_space_handle_t	pci_sh;
    312       1.45    mjacob 	bus_dmamap_t		*pci_xfer_dmap;
    313        1.1       cgd 	void *			pci_ih;
    314       1.36    mjacob 	int16_t			pci_poff[_NREG_BLKS];
    315        1.1       cgd };
    316        1.1       cgd 
    317  1.66.2.12   nathanw CFATTACH_DECL(isp_pci, sizeof (struct isp_pcisoftc),
    318  1.66.2.12   nathanw     isp_pci_probe, isp_pci_attach, NULL, NULL);
    319        1.1       cgd 
    320       1.55    mjacob #ifdef	DEBUG
    321       1.63    mjacob const char vstring[] =
    322       1.53    mjacob     "Qlogic ISP Driver, NetBSD (pci) Platform Version %d.%d Core Version %d.%d";
    323       1.55    mjacob #endif
    324       1.53    mjacob 
    325        1.1       cgd static int
    326   1.66.2.1   nathanw isp_pci_probe(struct device *parent, struct cfdata *match, void *aux)
    327       1.44    mjacob {
    328       1.44    mjacob 	struct pci_attach_args *pa = aux;
    329       1.44    mjacob 	switch (pa->pa_id) {
    330       1.36    mjacob #ifndef	ISP_DISABLE_1020_SUPPORT
    331       1.36    mjacob 	case PCI_QLOGIC_ISP:
    332       1.36    mjacob 		return (1);
    333       1.36    mjacob #endif
    334       1.36    mjacob #ifndef	ISP_DISABLE_1080_SUPPORT
    335       1.36    mjacob 	case PCI_QLOGIC_ISP1080:
    336       1.40    mjacob 	case PCI_QLOGIC_ISP1240:
    337       1.48    mjacob 	case PCI_QLOGIC_ISP1280:
    338       1.36    mjacob 		return (1);
    339       1.36    mjacob #endif
    340       1.50    mjacob #ifndef	ISP_DISABLE_12160_SUPPORT
    341  1.66.2.13   nathanw 	case PCI_QLOGIC_ISP10160:
    342       1.50    mjacob 	case PCI_QLOGIC_ISP12160:
    343       1.50    mjacob 		return (1);
    344       1.50    mjacob #endif
    345       1.36    mjacob #ifndef	ISP_DISABLE_2100_SUPPORT
    346       1.36    mjacob 	case PCI_QLOGIC_ISP2100:
    347        1.1       cgd 		return (1);
    348       1.36    mjacob #endif
    349       1.41    mjacob #ifndef	ISP_DISABLE_2200_SUPPORT
    350       1.41    mjacob 	case PCI_QLOGIC_ISP2200:
    351       1.41    mjacob 		return (1);
    352       1.41    mjacob #endif
    353   1.66.2.4   nathanw #ifndef	ISP_DISABLE_2300_SUPPORT
    354   1.66.2.4   nathanw 	case PCI_QLOGIC_ISP2300:
    355   1.66.2.4   nathanw 	case PCI_QLOGIC_ISP2312:
    356   1.66.2.4   nathanw 		return (1);
    357   1.66.2.4   nathanw #endif
    358       1.36    mjacob 	default:
    359        1.1       cgd 		return (0);
    360        1.1       cgd 	}
    361        1.1       cgd }
    362        1.1       cgd 
    363        1.1       cgd 
    364       1.44    mjacob static void
    365   1.66.2.1   nathanw isp_pci_attach(struct device *parent, struct device *self, void *aux)
    366        1.1       cgd {
    367       1.29    mjacob #ifdef	DEBUG
    368       1.27   thorpej 	static char oneshot = 1;
    369       1.27   thorpej #endif
    370  1.66.2.10   nathanw 	static const char nomem[] = "\n%s: no mem for sdparam table\n";
    371       1.47    mjacob 	u_int32_t data, rev, linesz = PCI_DFLT_LNSZ;
    372        1.1       cgd 	struct pci_attach_args *pa = aux;
    373        1.1       cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) self;
    374       1.21    mjacob 	struct ispsoftc *isp = &pcs->pci_isp;
    375       1.11       cgd 	bus_space_tag_t st, iot, memt;
    376       1.11       cgd 	bus_space_handle_t sh, ioh, memh;
    377        1.1       cgd 	pci_intr_handle_t ih;
    378  1.66.2.10   nathanw 	char *dstring;
    379        1.1       cgd 	const char *intrstr;
    380       1.53    mjacob 	int ioh_valid, memh_valid;
    381        1.1       cgd 
    382       1.12       cgd 	ioh_valid = (pci_mapreg_map(pa, IO_MAP_REG,
    383       1.11       cgd 	    PCI_MAPREG_TYPE_IO, 0,
    384       1.11       cgd 	    &iot, &ioh, NULL, NULL) == 0);
    385       1.12       cgd 	memh_valid = (pci_mapreg_map(pa, MEM_MAP_REG,
    386       1.11       cgd 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
    387       1.11       cgd 	    &memt, &memh, NULL, NULL) == 0);
    388       1.11       cgd 	if (memh_valid) {
    389       1.11       cgd 		st = memt;
    390       1.11       cgd 		sh = memh;
    391       1.11       cgd 	} else if (ioh_valid) {
    392       1.11       cgd 		st = iot;
    393       1.11       cgd 		sh = ioh;
    394        1.6       cgd 	} else {
    395       1.11       cgd 		printf(": unable to map device registers\n");
    396        1.9       cgd 		return;
    397        1.1       cgd 	}
    398  1.66.2.10   nathanw 	dstring = "\n";
    399        1.1       cgd 
    400        1.6       cgd 	pcs->pci_st = st;
    401        1.6       cgd 	pcs->pci_sh = sh;
    402       1.15    mjacob 	pcs->pci_pc = pa->pa_pc;
    403       1.15    mjacob 	pcs->pci_tag = pa->pa_tag;
    404       1.36    mjacob 	pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
    405       1.36    mjacob 	pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
    406       1.36    mjacob 	pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
    407       1.36    mjacob 	pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
    408       1.36    mjacob 	pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
    409       1.47    mjacob 	rev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG) & 0xff;
    410       1.36    mjacob 
    411       1.36    mjacob #ifndef	ISP_DISABLE_1020_SUPPORT
    412       1.15    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP) {
    413  1.66.2.10   nathanw 		dstring = ": QLogic 1020 Ultra Wide SCSI HBA\n";
    414       1.21    mjacob 		isp->isp_mdvec = &mdvec;
    415       1.21    mjacob 		isp->isp_type = ISP_HA_SCSI_UNKNOWN;
    416       1.21    mjacob 		isp->isp_param = malloc(sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    417       1.21    mjacob 		if (isp->isp_param == NULL) {
    418       1.40    mjacob 			printf(nomem, isp->isp_name);
    419       1.21    mjacob 			return;
    420       1.15    mjacob 		}
    421   1.66.2.3   nathanw 		memset(isp->isp_param, 0, sizeof (sdparam));
    422       1.36    mjacob 	}
    423       1.36    mjacob #endif
    424       1.36    mjacob #ifndef	ISP_DISABLE_1080_SUPPORT
    425       1.36    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP1080) {
    426  1.66.2.10   nathanw 		dstring = ": QLogic 1080 Ultra-2 Wide SCSI HBA\n";
    427       1.36    mjacob 		isp->isp_mdvec = &mdvec_1080;
    428       1.36    mjacob 		isp->isp_type = ISP_HA_SCSI_1080;
    429       1.36    mjacob 		isp->isp_param = malloc(sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    430       1.36    mjacob 		if (isp->isp_param == NULL) {
    431       1.40    mjacob 			printf(nomem, isp->isp_name);
    432       1.36    mjacob 			return;
    433       1.36    mjacob 		}
    434   1.66.2.3   nathanw 		memset(isp->isp_param, 0, sizeof (sdparam));
    435       1.36    mjacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
    436       1.36    mjacob 		    ISP1080_DMA_REGS_OFF;
    437       1.36    mjacob 	}
    438       1.40    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP1240) {
    439  1.66.2.10   nathanw 		dstring = ": QLogic Dual Channel Ultra Wide SCSI HBA\n";
    440       1.40    mjacob 		isp->isp_mdvec = &mdvec_1080;
    441       1.48    mjacob 		isp->isp_type = ISP_HA_SCSI_1240;
    442       1.48    mjacob 		isp->isp_param =
    443       1.48    mjacob 		    malloc(2 * sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    444       1.48    mjacob 		if (isp->isp_param == NULL) {
    445       1.48    mjacob 			printf(nomem, isp->isp_name);
    446       1.48    mjacob 			return;
    447       1.48    mjacob 		}
    448   1.66.2.3   nathanw 		memset(isp->isp_param, 0, 2 * sizeof (sdparam));
    449       1.48    mjacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
    450       1.48    mjacob 		    ISP1080_DMA_REGS_OFF;
    451       1.48    mjacob 	}
    452       1.48    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP1280) {
    453  1.66.2.10   nathanw 		dstring = ": QLogic Dual Channel Ultra-2 Wide SCSI HBA\n";
    454       1.48    mjacob 		isp->isp_mdvec = &mdvec_1080;
    455       1.48    mjacob 		isp->isp_type = ISP_HA_SCSI_1280;
    456       1.40    mjacob 		isp->isp_param =
    457       1.40    mjacob 		    malloc(2 * sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    458       1.40    mjacob 		if (isp->isp_param == NULL) {
    459       1.40    mjacob 			printf(nomem, isp->isp_name);
    460       1.40    mjacob 			return;
    461       1.40    mjacob 		}
    462   1.66.2.3   nathanw 		memset(isp->isp_param, 0, 2 * sizeof (sdparam));
    463       1.40    mjacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
    464       1.40    mjacob 		    ISP1080_DMA_REGS_OFF;
    465       1.40    mjacob 	}
    466       1.36    mjacob #endif
    467       1.50    mjacob #ifndef	ISP_DISABLE_12160_SUPPORT
    468  1.66.2.13   nathanw 	if (pa->pa_id == PCI_QLOGIC_ISP10160) {
    469  1.66.2.13   nathanw 		dstring = ": QLogic Ultra-3 Wide SCSI HBA\n";
    470  1.66.2.13   nathanw 		isp->isp_mdvec = &mdvec_12160;
    471  1.66.2.13   nathanw 		isp->isp_type = ISP_HA_SCSI_10160;
    472  1.66.2.13   nathanw 		isp->isp_param = malloc(sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    473  1.66.2.13   nathanw 		if (isp->isp_param == NULL) {
    474  1.66.2.13   nathanw 			printf(nomem, isp->isp_name);
    475  1.66.2.13   nathanw 			return;
    476  1.66.2.13   nathanw 		}
    477  1.66.2.13   nathanw 		memset(isp->isp_param, 0, sizeof (sdparam));
    478  1.66.2.13   nathanw 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
    479  1.66.2.13   nathanw 		    ISP1080_DMA_REGS_OFF;
    480  1.66.2.13   nathanw 	}
    481       1.50    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP12160) {
    482  1.66.2.10   nathanw 		dstring = ": QLogic Dual Channel Ultra-3 Wide SCSI HBA\n";
    483       1.50    mjacob 		isp->isp_mdvec = &mdvec_12160;
    484       1.50    mjacob 		isp->isp_type = ISP_HA_SCSI_12160;
    485       1.50    mjacob 		isp->isp_param =
    486       1.50    mjacob 		    malloc(2 * sizeof (sdparam), M_DEVBUF, M_NOWAIT);
    487       1.50    mjacob 		if (isp->isp_param == NULL) {
    488       1.50    mjacob 			printf(nomem, isp->isp_name);
    489       1.50    mjacob 			return;
    490       1.50    mjacob 		}
    491   1.66.2.3   nathanw 		memset(isp->isp_param, 0, 2 * sizeof (sdparam));
    492       1.50    mjacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
    493       1.50    mjacob 		    ISP1080_DMA_REGS_OFF;
    494       1.50    mjacob 	}
    495       1.50    mjacob #endif
    496       1.36    mjacob #ifndef	ISP_DISABLE_2100_SUPPORT
    497       1.36    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP2100) {
    498  1.66.2.10   nathanw 		dstring = ": QLogic FC-AL HBA\n";
    499       1.21    mjacob 		isp->isp_mdvec = &mdvec_2100;
    500       1.21    mjacob 		isp->isp_type = ISP_HA_FC_2100;
    501       1.21    mjacob 		isp->isp_param = malloc(sizeof (fcparam), M_DEVBUF, M_NOWAIT);
    502       1.21    mjacob 		if (isp->isp_param == NULL) {
    503       1.40    mjacob 			printf(nomem, isp->isp_name);
    504       1.21    mjacob 			return;
    505       1.15    mjacob 		}
    506   1.66.2.3   nathanw 		memset(isp->isp_param, 0, sizeof (fcparam));
    507       1.36    mjacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
    508       1.36    mjacob 		    PCI_MBOX_REGS2100_OFF;
    509       1.47    mjacob 		if (rev < 3) {
    510       1.39    mjacob 			/*
    511       1.39    mjacob 			 * XXX: Need to get the actual revision
    512       1.39    mjacob 			 * XXX: number of the 2100 FB. At any rate,
    513       1.39    mjacob 			 * XXX: lower cache line size for early revision
    514       1.39    mjacob 			 * XXX; boards.
    515       1.39    mjacob 			 */
    516       1.39    mjacob 			linesz = 1;
    517       1.39    mjacob 		}
    518       1.15    mjacob 	}
    519       1.36    mjacob #endif
    520       1.41    mjacob #ifndef	ISP_DISABLE_2200_SUPPORT
    521       1.41    mjacob 	if (pa->pa_id == PCI_QLOGIC_ISP2200) {
    522  1.66.2.10   nathanw 		dstring = ": QLogic FC-AL and Fabric HBA\n";
    523       1.41    mjacob 		isp->isp_mdvec = &mdvec_2200;
    524       1.41    mjacob 		isp->isp_type = ISP_HA_FC_2200;
    525       1.41    mjacob 		isp->isp_param = malloc(sizeof (fcparam), M_DEVBUF, M_NOWAIT);
    526       1.41    mjacob 		if (isp->isp_param == NULL) {
    527       1.41    mjacob 			printf(nomem, isp->isp_name);
    528       1.41    mjacob 			return;
    529       1.41    mjacob 		}
    530   1.66.2.3   nathanw 		memset(isp->isp_param, 0, sizeof (fcparam));
    531       1.41    mjacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
    532       1.41    mjacob 		    PCI_MBOX_REGS2100_OFF;
    533       1.41    mjacob 		data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG);
    534       1.41    mjacob 	}
    535       1.41    mjacob #endif
    536   1.66.2.4   nathanw #ifndef	ISP_DISABLE_2300_SUPPORT
    537   1.66.2.4   nathanw 	if (pa->pa_id == PCI_QLOGIC_ISP2300 ||
    538   1.66.2.4   nathanw 	    pa->pa_id == PCI_QLOGIC_ISP2312) {
    539   1.66.2.4   nathanw 		isp->isp_mdvec = &mdvec_2300;
    540   1.66.2.8   nathanw 		if (pa->pa_id == PCI_QLOGIC_ISP2300) {
    541  1.66.2.10   nathanw 			dstring = ": QLogic FC-AL and 2Gbps Fabric HBA\n";
    542   1.66.2.8   nathanw 			isp->isp_type = ISP_HA_FC_2300;
    543   1.66.2.8   nathanw 		} else {
    544  1.66.2.10   nathanw 			dstring =
    545  1.66.2.10   nathanw 			    ": QLogic Dual Port FC-AL and 2Gbps Fabric HBA\n";
    546   1.66.2.8   nathanw 			isp->isp_type = ISP_HA_FC_2312;
    547   1.66.2.8   nathanw 			isp->isp_port = pa->pa_function;
    548   1.66.2.8   nathanw 		}
    549   1.66.2.4   nathanw 		isp->isp_param = malloc(sizeof (fcparam), M_DEVBUF, M_NOWAIT);
    550   1.66.2.4   nathanw 		if (isp->isp_param == NULL) {
    551   1.66.2.4   nathanw 			printf(nomem, isp->isp_name);
    552   1.66.2.4   nathanw 			return;
    553   1.66.2.4   nathanw 		}
    554   1.66.2.4   nathanw 		memset(isp->isp_param, 0, sizeof (fcparam));
    555   1.66.2.4   nathanw 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
    556   1.66.2.4   nathanw 		    PCI_MBOX_REGS2300_OFF;
    557   1.66.2.4   nathanw 		data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG);
    558   1.66.2.4   nathanw 	}
    559   1.66.2.4   nathanw #endif
    560       1.53    mjacob 	/*
    561       1.53    mjacob 	 * Set up logging levels.
    562       1.53    mjacob 	 */
    563       1.53    mjacob #ifdef	ISP_LOGDEFAULT
    564       1.53    mjacob 	isp->isp_dblev = ISP_LOGDEFAULT;
    565       1.53    mjacob #else
    566       1.65    mjacob 	isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
    567       1.65    mjacob 	if (bootverbose)
    568       1.65    mjacob 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
    569       1.53    mjacob #ifdef	SCSIDEBUG
    570  1.66.2.10   nathanw 	isp->isp_dblev |= ISP_LOGDEBUG0|ISP_LOGDEBUG1|ISP_LOGDEBUG2;
    571       1.53    mjacob #endif
    572       1.53    mjacob #endif
    573  1.66.2.10   nathanw 	if (isp->isp_dblev & ISP_LOGCONFIG) {
    574  1.66.2.10   nathanw 		printf("\n");
    575  1.66.2.10   nathanw 	} else {
    576  1.66.2.10   nathanw 		printf(dstring);
    577  1.66.2.10   nathanw 	}
    578       1.57    mjacob 
    579       1.54    mjacob #ifdef	DEBUG
    580       1.53    mjacob 	if (oneshot) {
    581       1.53    mjacob 		oneshot = 0;
    582       1.53    mjacob 		isp_prt(isp, ISP_LOGCONFIG, vstring,
    583       1.53    mjacob 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
    584       1.53    mjacob 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
    585       1.53    mjacob 	}
    586       1.54    mjacob #endif
    587       1.53    mjacob 
    588   1.66.2.7   nathanw 	isp->isp_dmatag = pa->pa_dmat;
    589       1.47    mjacob 	isp->isp_revision = rev;
    590       1.36    mjacob 
    591       1.35    mjacob 	/*
    592       1.35    mjacob 	 * Make sure that command register set sanely.
    593       1.35    mjacob 	 */
    594       1.35    mjacob 	data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    595       1.35    mjacob 	data |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_INVALIDATE_ENABLE;
    596       1.36    mjacob 
    597       1.35    mjacob 	/*
    598       1.35    mjacob 	 * Not so sure about these- but I think it's important that they get
    599       1.35    mjacob 	 * enabled......
    600       1.35    mjacob 	 */
    601       1.35    mjacob 	data |= PCI_COMMAND_PARITY_ENABLE | PCI_COMMAND_SERR_ENABLE;
    602   1.66.2.8   nathanw 	if (IS_2300(isp)) {	/* per QLogic errata */
    603   1.66.2.8   nathanw 		data &= ~PCI_COMMAND_INVALIDATE_ENABLE;
    604   1.66.2.8   nathanw 	}
    605  1.66.2.10   nathanw 	if (IS_23XX(isp)) {
    606  1.66.2.10   nathanw 		isp->isp_touched = 1;
    607  1.66.2.10   nathanw 	}
    608       1.35    mjacob 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, data);
    609       1.36    mjacob 
    610       1.35    mjacob 	/*
    611       1.39    mjacob 	 * Make sure that the latency timer, cache line size,
    612       1.39    mjacob 	 * and ROM is disabled.
    613       1.35    mjacob 	 */
    614       1.35    mjacob 	data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    615       1.35    mjacob 	data &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    616       1.35    mjacob 	data &= ~(PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT);
    617       1.39    mjacob 	data |= (PCI_DFLT_LTNCY	<< PCI_LATTIMER_SHIFT);
    618       1.39    mjacob 	data |= (linesz << PCI_CACHELINE_SHIFT);
    619       1.35    mjacob 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, data);
    620       1.39    mjacob 
    621       1.39    mjacob 	data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCIR_ROMADDR);
    622       1.39    mjacob 	data &= ~1;
    623       1.39    mjacob 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCIR_ROMADDR, data);
    624       1.35    mjacob 
    625       1.64  sommerfe 	if (pci_intr_map(pa, &ih)) {
    626       1.21    mjacob 		printf("%s: couldn't map interrupt\n", isp->isp_name);
    627       1.21    mjacob 		free(isp->isp_param, M_DEVBUF);
    628        1.1       cgd 		return;
    629        1.1       cgd 	}
    630        1.1       cgd 	intrstr = pci_intr_string(pa->pa_pc, ih);
    631        1.1       cgd 	if (intrstr == NULL)
    632        1.1       cgd 		intrstr = "<I dunno>";
    633       1.44    mjacob 	pcs->pci_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
    634       1.44    mjacob 	    isp_pci_intr, isp);
    635        1.1       cgd 	if (pcs->pci_ih == NULL) {
    636        1.1       cgd 		printf("%s: couldn't establish interrupt at %s\n",
    637       1.21    mjacob 			isp->isp_name, intrstr);
    638       1.36    mjacob 		free(isp->isp_param, M_DEVBUF);
    639       1.36    mjacob 		return;
    640       1.36    mjacob 	}
    641       1.53    mjacob 
    642       1.36    mjacob 	printf("%s: interrupting at %s\n", isp->isp_name, intrstr);
    643       1.36    mjacob 
    644       1.43    mjacob 	if (IS_FC(isp)) {
    645  1.66.2.14   thorpej 		DEFAULT_NODEWWN(isp) = 0x400000007F000002ULL;
    646  1.66.2.14   thorpej 		DEFAULT_PORTWWN(isp) = 0x400000007F000002ULL;
    647       1.43    mjacob 	}
    648       1.41    mjacob 
    649       1.49    mjacob 	isp->isp_confopts = self->dv_cfdata->cf_flags;
    650       1.66    mjacob 	isp->isp_role = ISP_DEFAULT_ROLES;
    651       1.36    mjacob 	ISP_LOCK(isp);
    652       1.53    mjacob 	isp->isp_osinfo.no_mbox_ints = 1;
    653       1.36    mjacob 	isp_reset(isp);
    654       1.36    mjacob 	if (isp->isp_state != ISP_RESETSTATE) {
    655       1.36    mjacob 		ISP_UNLOCK(isp);
    656       1.36    mjacob 		free(isp->isp_param, M_DEVBUF);
    657       1.36    mjacob 		return;
    658       1.36    mjacob 	}
    659       1.53    mjacob 	ENABLE_INTS(isp);
    660       1.36    mjacob 	isp_init(isp);
    661       1.36    mjacob 	if (isp->isp_state != ISP_INITSTATE) {
    662       1.21    mjacob 		isp_uninit(isp);
    663       1.22    mjacob 		ISP_UNLOCK(isp);
    664       1.21    mjacob 		free(isp->isp_param, M_DEVBUF);
    665        1.1       cgd 		return;
    666        1.1       cgd 	}
    667        1.1       cgd 	/*
    668       1.53    mjacob 	 * Do platform attach.
    669        1.1       cgd 	 */
    670       1.53    mjacob 	ISP_UNLOCK(isp);
    671       1.21    mjacob 	isp_attach(isp);
    672       1.21    mjacob 	if (isp->isp_state != ISP_RUNSTATE) {
    673       1.53    mjacob 		ISP_LOCK(isp);
    674       1.21    mjacob 		isp_uninit(isp);
    675       1.21    mjacob 		free(isp->isp_param, M_DEVBUF);
    676       1.53    mjacob 		ISP_UNLOCK(isp);
    677        1.1       cgd 	}
    678        1.1       cgd }
    679        1.1       cgd 
    680   1.66.2.4   nathanw #define	IspVirt2Off(a, x)	\
    681   1.66.2.4   nathanw 	(((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
    682   1.66.2.4   nathanw 	_BLK_REG_SHFT] + ((x) & 0xff))
    683   1.66.2.4   nathanw 
    684   1.66.2.4   nathanw #define	BXR2(pcs, off)		\
    685   1.66.2.4   nathanw 	bus_space_read_2(pcs->pci_st, pcs->pci_sh, off)
    686   1.66.2.4   nathanw #define	BXW2(pcs, off, v)	\
    687   1.66.2.4   nathanw 	bus_space_write_2(pcs->pci_st, pcs->pci_sh, off, v)
    688   1.66.2.4   nathanw 
    689   1.66.2.4   nathanw 
    690   1.66.2.4   nathanw static INLINE int
    691   1.66.2.4   nathanw isp_pci_rd_debounced(struct ispsoftc *isp, int off, u_int16_t *rp)
    692   1.66.2.4   nathanw {
    693   1.66.2.4   nathanw 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    694   1.66.2.4   nathanw 	u_int16_t val0, val1;
    695   1.66.2.4   nathanw 	int i = 0;
    696   1.66.2.4   nathanw 
    697   1.66.2.4   nathanw 	do {
    698   1.66.2.4   nathanw 		val0 = BXR2(pcs, IspVirt2Off(isp, off));
    699   1.66.2.4   nathanw 		val1 = BXR2(pcs, IspVirt2Off(isp, off));
    700   1.66.2.4   nathanw 	} while (val0 != val1 && ++i < 1000);
    701   1.66.2.4   nathanw 	if (val0 != val1) {
    702   1.66.2.4   nathanw 		return (1);
    703   1.66.2.4   nathanw 	}
    704   1.66.2.4   nathanw 	*rp = val0;
    705   1.66.2.4   nathanw 	return (0);
    706   1.66.2.4   nathanw }
    707   1.66.2.4   nathanw 
    708   1.66.2.4   nathanw static int
    709   1.66.2.4   nathanw isp_pci_rd_isr(struct ispsoftc *isp, u_int16_t *isrp,
    710   1.66.2.4   nathanw     u_int16_t *semap, u_int16_t *mbp)
    711   1.66.2.4   nathanw {
    712   1.66.2.4   nathanw 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    713   1.66.2.4   nathanw 	u_int16_t isr, sema;
    714   1.66.2.4   nathanw 
    715   1.66.2.4   nathanw 	if (IS_2100(isp)) {
    716   1.66.2.4   nathanw 		if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
    717   1.66.2.4   nathanw 		    return (0);
    718   1.66.2.4   nathanw 		}
    719   1.66.2.4   nathanw 		if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
    720   1.66.2.4   nathanw 		    return (0);
    721   1.66.2.4   nathanw 		}
    722   1.66.2.4   nathanw 	} else {
    723   1.66.2.4   nathanw 		isr = BXR2(pcs, IspVirt2Off(isp, BIU_ISR));
    724   1.66.2.4   nathanw 		sema = BXR2(pcs, IspVirt2Off(isp, BIU_SEMA));
    725   1.66.2.4   nathanw 	}
    726   1.66.2.4   nathanw 	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
    727   1.66.2.4   nathanw 	isr &= INT_PENDING_MASK(isp);
    728   1.66.2.4   nathanw 	sema &= BIU_SEMA_LOCK;
    729   1.66.2.4   nathanw 	if (isr == 0 && sema == 0) {
    730   1.66.2.4   nathanw 		return (0);
    731   1.66.2.4   nathanw 	}
    732   1.66.2.4   nathanw 	*isrp = isr;
    733   1.66.2.4   nathanw 	if ((*semap = sema) != 0) {
    734   1.66.2.4   nathanw 		if (IS_2100(isp)) {
    735   1.66.2.4   nathanw 			if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
    736   1.66.2.4   nathanw 				return (0);
    737   1.66.2.4   nathanw 			}
    738   1.66.2.4   nathanw 		} else {
    739   1.66.2.4   nathanw 			*mbp = BXR2(pcs, IspVirt2Off(isp, OUTMAILBOX0));
    740   1.66.2.4   nathanw 		}
    741   1.66.2.4   nathanw 	}
    742   1.66.2.4   nathanw 	return (1);
    743   1.66.2.4   nathanw }
    744   1.66.2.4   nathanw 
    745   1.66.2.4   nathanw #ifndef	ISP_DISABLE_2300_SUPPORT
    746   1.66.2.4   nathanw static int
    747   1.66.2.4   nathanw isp_pci_rd_isr_2300(struct ispsoftc *isp, u_int16_t *isrp,
    748   1.66.2.4   nathanw     u_int16_t *semap, u_int16_t *mbox0p)
    749   1.66.2.4   nathanw {
    750   1.66.2.4   nathanw 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    751   1.66.2.4   nathanw 	u_int32_t r2hisr;
    752   1.66.2.4   nathanw 
    753   1.66.2.5   nathanw 	if (!(BXR2(pcs, IspVirt2Off(isp, BIU_ISR)) & BIU2100_ISR_RISC_INT)) {
    754   1.66.2.5   nathanw 		*isrp = 0;
    755   1.66.2.5   nathanw 		return (0);
    756   1.66.2.5   nathanw 	}
    757   1.66.2.4   nathanw 	r2hisr = bus_space_read_4(pcs->pci_st, pcs->pci_sh,
    758   1.66.2.4   nathanw 	    IspVirt2Off(pcs, BIU_R2HSTSLO));
    759   1.66.2.4   nathanw 	isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
    760   1.66.2.4   nathanw 	if ((r2hisr & BIU_R2HST_INTR) == 0) {
    761   1.66.2.4   nathanw 		*isrp = 0;
    762   1.66.2.4   nathanw 		return (0);
    763   1.66.2.4   nathanw 	}
    764   1.66.2.4   nathanw 	switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
    765   1.66.2.4   nathanw 	case ISPR2HST_ROM_MBX_OK:
    766   1.66.2.4   nathanw 	case ISPR2HST_ROM_MBX_FAIL:
    767   1.66.2.4   nathanw 	case ISPR2HST_MBX_OK:
    768   1.66.2.4   nathanw 	case ISPR2HST_MBX_FAIL:
    769   1.66.2.4   nathanw 	case ISPR2HST_ASYNC_EVENT:
    770  1.66.2.11   nathanw 		*isrp = r2hisr & 0xffff;
    771  1.66.2.11   nathanw 		*mbox0p = (r2hisr >> 16);
    772  1.66.2.11   nathanw 		*semap = 1;
    773  1.66.2.11   nathanw 		return (1);
    774   1.66.2.8   nathanw 	case ISPR2HST_RIO_16:
    775  1.66.2.11   nathanw 		*isrp = r2hisr & 0xffff;
    776  1.66.2.11   nathanw 		*mbox0p = ASYNC_RIO1;
    777  1.66.2.11   nathanw 		*semap = 1;
    778  1.66.2.11   nathanw 		return (1);
    779   1.66.2.4   nathanw 	case ISPR2HST_FPOST:
    780  1.66.2.11   nathanw 		*isrp = r2hisr & 0xffff;
    781  1.66.2.11   nathanw 		*mbox0p = ASYNC_CMD_CMPLT;
    782  1.66.2.11   nathanw 		*semap = 1;
    783  1.66.2.11   nathanw 		return (1);
    784   1.66.2.4   nathanw 	case ISPR2HST_FPOST_CTIO:
    785   1.66.2.4   nathanw 		*isrp = r2hisr & 0xffff;
    786  1.66.2.11   nathanw 		*mbox0p = ASYNC_CTIO_DONE;
    787   1.66.2.4   nathanw 		*semap = 1;
    788   1.66.2.4   nathanw 		return (1);
    789   1.66.2.4   nathanw 	case ISPR2HST_RSPQ_UPDATE:
    790   1.66.2.4   nathanw 		*isrp = r2hisr & 0xffff;
    791   1.66.2.4   nathanw 		*mbox0p = 0;
    792   1.66.2.4   nathanw 		*semap = 0;
    793   1.66.2.4   nathanw 		return (1);
    794   1.66.2.4   nathanw 	default:
    795   1.66.2.4   nathanw 		return (0);
    796   1.66.2.4   nathanw 	}
    797   1.66.2.4   nathanw }
    798   1.66.2.4   nathanw #endif
    799   1.66.2.4   nathanw 
    800        1.1       cgd static u_int16_t
    801   1.66.2.1   nathanw isp_pci_rd_reg(struct ispsoftc *isp, int regoff)
    802        1.1       cgd {
    803       1.15    mjacob 	u_int16_t rv;
    804        1.1       cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    805   1.66.2.4   nathanw 	int oldconf = 0;
    806       1.15    mjacob 
    807       1.36    mjacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
    808        1.1       cgd 		/*
    809       1.15    mjacob 		 * We will assume that someone has paused the RISC processor.
    810        1.1       cgd 		 */
    811   1.66.2.4   nathanw 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    812   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
    813   1.66.2.4   nathanw 		    oldconf | BIU_PCI_CONF1_SXP);
    814   1.66.2.4   nathanw 	}
    815   1.66.2.4   nathanw 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
    816       1.36    mjacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
    817   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
    818       1.15    mjacob 	}
    819       1.15    mjacob 	return (rv);
    820        1.1       cgd }
    821        1.1       cgd 
    822        1.1       cgd static void
    823   1.66.2.1   nathanw isp_pci_wr_reg(struct ispsoftc *isp, int regoff, u_int16_t val)
    824        1.1       cgd {
    825        1.1       cgd 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    826   1.66.2.4   nathanw 	int oldconf = 0;
    827       1.36    mjacob 
    828       1.36    mjacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
    829        1.1       cgd 		/*
    830       1.15    mjacob 		 * We will assume that someone has paused the RISC processor.
    831        1.1       cgd 		 */
    832   1.66.2.4   nathanw 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    833   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
    834   1.66.2.4   nathanw 		    oldconf | BIU_PCI_CONF1_SXP);
    835   1.66.2.4   nathanw 	}
    836   1.66.2.4   nathanw 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
    837       1.36    mjacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
    838   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
    839       1.36    mjacob 	}
    840       1.36    mjacob }
    841       1.36    mjacob 
    842       1.52    mjacob #if !(defined(ISP_DISABLE_1080_SUPPORT) && defined(ISP_DISABLE_12160_SUPPORT))
    843       1.36    mjacob static u_int16_t
    844   1.66.2.1   nathanw isp_pci_rd_reg_1080(struct ispsoftc *isp, int regoff)
    845       1.36    mjacob {
    846       1.48    mjacob 	u_int16_t rv, oc = 0;
    847       1.36    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    848       1.36    mjacob 
    849   1.66.2.4   nathanw 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
    850   1.66.2.4   nathanw 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
    851       1.48    mjacob 		u_int16_t tc;
    852       1.36    mjacob 		/*
    853       1.36    mjacob 		 * We will assume that someone has paused the RISC processor.
    854       1.36    mjacob 		 */
    855   1.66.2.4   nathanw 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    856       1.48    mjacob 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
    857   1.66.2.4   nathanw 		if (regoff & SXP_BANK1_SELECT)
    858   1.66.2.4   nathanw 			tc |= BIU_PCI1080_CONF1_SXP1;
    859   1.66.2.4   nathanw 		else
    860       1.48    mjacob 			tc |= BIU_PCI1080_CONF1_SXP0;
    861   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
    862       1.36    mjacob 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
    863   1.66.2.4   nathanw 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    864   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
    865   1.66.2.4   nathanw 		    oc | BIU_PCI1080_CONF1_DMA);
    866   1.66.2.4   nathanw 	}
    867   1.66.2.4   nathanw 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
    868       1.48    mjacob 	if (oc) {
    869   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
    870       1.36    mjacob 	}
    871       1.36    mjacob 	return (rv);
    872       1.36    mjacob }
    873       1.36    mjacob 
    874       1.36    mjacob static void
    875   1.66.2.1   nathanw isp_pci_wr_reg_1080(struct ispsoftc *isp, int regoff, u_int16_t val)
    876       1.36    mjacob {
    877       1.36    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
    878   1.66.2.4   nathanw 	int oc = 0;
    879       1.36    mjacob 
    880   1.66.2.4   nathanw 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
    881   1.66.2.4   nathanw 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
    882       1.48    mjacob 		u_int16_t tc;
    883       1.36    mjacob 		/*
    884       1.36    mjacob 		 * We will assume that someone has paused the RISC processor.
    885       1.36    mjacob 		 */
    886   1.66.2.4   nathanw 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    887       1.48    mjacob 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
    888   1.66.2.4   nathanw 		if (regoff & SXP_BANK1_SELECT)
    889   1.66.2.4   nathanw 			tc |= BIU_PCI1080_CONF1_SXP1;
    890   1.66.2.4   nathanw 		else
    891       1.48    mjacob 			tc |= BIU_PCI1080_CONF1_SXP0;
    892   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
    893       1.36    mjacob 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
    894   1.66.2.4   nathanw 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
    895   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
    896   1.66.2.4   nathanw 		    oc | BIU_PCI1080_CONF1_DMA);
    897   1.66.2.4   nathanw 	}
    898   1.66.2.4   nathanw 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
    899       1.48    mjacob 	if (oc) {
    900   1.66.2.4   nathanw 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
    901       1.15    mjacob 	}
    902        1.1       cgd }
    903       1.36    mjacob #endif
    904        1.1       cgd 
    905       1.13   thorpej static int
    906   1.66.2.1   nathanw isp_pci_mbxdma(struct ispsoftc *isp)
    907        1.1       cgd {
    908       1.53    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
    909   1.66.2.7   nathanw 	bus_dma_tag_t dmat = isp->isp_dmatag;
    910       1.53    mjacob 	bus_dma_segment_t sg;
    911       1.13   thorpej 	bus_size_t len;
    912       1.15    mjacob 	fcparam *fcp;
    913       1.53    mjacob 	int rs, i;
    914       1.13   thorpej 
    915       1.43    mjacob 	if (isp->isp_rquest_dma)	/* been here before? */
    916       1.43    mjacob 		return (0);
    917       1.43    mjacob 
    918   1.66.2.2   nathanw 	len = isp->isp_maxcmds * sizeof (XS_T *);
    919       1.53    mjacob 	isp->isp_xflist = (XS_T **) malloc(len, M_DEVBUF, M_WAITOK);
    920       1.43    mjacob 	if (isp->isp_xflist == NULL) {
    921       1.53    mjacob 		isp_prt(isp, ISP_LOGERR, "cannot malloc xflist array");
    922       1.43    mjacob 		return (1);
    923       1.43    mjacob 	}
    924   1.66.2.3   nathanw 	memset(isp->isp_xflist, 0, len);
    925       1.45    mjacob 	len = isp->isp_maxcmds * sizeof (bus_dmamap_t);
    926       1.53    mjacob 	pcs->pci_xfer_dmap = (bus_dmamap_t *) malloc(len, M_DEVBUF, M_WAITOK);
    927       1.53    mjacob 	if (pcs->pci_xfer_dmap == NULL) {
    928       1.53    mjacob 		free(isp->isp_xflist, M_DEVBUF);
    929       1.53    mjacob 		isp->isp_xflist = NULL;
    930       1.53    mjacob 		isp_prt(isp, ISP_LOGERR, "cannot malloc dma map array");
    931       1.53    mjacob 		return (1);
    932       1.53    mjacob 	}
    933       1.53    mjacob 	for (i = 0; i < isp->isp_maxcmds; i++) {
    934       1.59   thorpej 		if (bus_dmamap_create(dmat, MAXPHYS, (MAXPHYS / PAGE_SIZE) + 1,
    935       1.53    mjacob 		    MAXPHYS, 0, BUS_DMA_NOWAIT, &pcs->pci_xfer_dmap[i])) {
    936       1.53    mjacob 			isp_prt(isp, ISP_LOGERR, "cannot create dma maps");
    937       1.53    mjacob 			break;
    938       1.53    mjacob 		}
    939       1.53    mjacob 	}
    940       1.53    mjacob 	if (i < isp->isp_maxcmds) {
    941       1.53    mjacob 		while (--i >= 0) {
    942       1.53    mjacob 			bus_dmamap_destroy(dmat, pcs->pci_xfer_dmap[i]);
    943       1.53    mjacob 		}
    944       1.53    mjacob 		free(isp->isp_xflist, M_DEVBUF);
    945       1.53    mjacob 		free(pcs->pci_xfer_dmap, M_DEVBUF);
    946       1.53    mjacob 		isp->isp_xflist = NULL;
    947       1.53    mjacob 		pcs->pci_xfer_dmap = NULL;
    948       1.45    mjacob 		return (1);
    949       1.45    mjacob 	}
    950       1.43    mjacob 
    951       1.13   thorpej 	/*
    952       1.13   thorpej 	 * Allocate and map the request queue.
    953       1.13   thorpej 	 */
    954       1.53    mjacob 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
    955       1.59   thorpej 	if (bus_dmamem_alloc(dmat, len, PAGE_SIZE, 0, &sg, 1, &rs,
    956       1.59   thorpej 			     BUS_DMA_NOWAIT) ||
    957   1.66.2.7   nathanw 	    bus_dmamem_map(isp->isp_dmatag, &sg, rs, len,
    958       1.53    mjacob 	    (caddr_t *)&isp->isp_rquest, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    959       1.53    mjacob 		goto dmafail;
    960       1.53    mjacob 	}
    961       1.53    mjacob 
    962       1.53    mjacob 	if (bus_dmamap_create(dmat, len, 1, len, 0, BUS_DMA_NOWAIT,
    963   1.66.2.7   nathanw 	    &isp->isp_rqdmap) || bus_dmamap_load(dmat, isp->isp_rqdmap,
    964   1.66.2.7   nathanw 	    (caddr_t)isp->isp_rquest, len, NULL,
    965       1.53    mjacob 	    BUS_DMA_NOWAIT)) {
    966       1.53    mjacob 		goto dmafail;
    967       1.53    mjacob 	}
    968   1.66.2.7   nathanw 	isp->isp_rquest_dma = isp->isp_rqdmap->dm_segs[0].ds_addr;
    969       1.13   thorpej 
    970       1.13   thorpej 	/*
    971       1.13   thorpej 	 * Allocate and map the result queue.
    972       1.13   thorpej 	 */
    973       1.53    mjacob 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
    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 *)&isp->isp_result,
    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_rsdmap) || bus_dmamap_load(isp->isp_dmatag,
    982   1.66.2.7   nathanw 	    isp->isp_rsdmap, (caddr_t)isp->isp_result, 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 	isp->isp_result_dma = isp->isp_rsdmap->dm_segs[0].ds_addr;
    987        1.1       cgd 
    988       1.41    mjacob 	if (IS_SCSI(isp)) {
    989       1.15    mjacob 		return (0);
    990       1.15    mjacob 	}
    991        1.1       cgd 
    992       1.15    mjacob 	fcp = isp->isp_param;
    993       1.15    mjacob 	len = ISP2100_SCRLEN;
    994       1.59   thorpej 	if (bus_dmamem_alloc(dmat, len, PAGE_SIZE, 0, &sg, 1, &rs,
    995       1.59   thorpej 			     BUS_DMA_NOWAIT) ||
    996       1.53    mjacob 	    bus_dmamem_map(dmat, &sg, rs, len, (caddr_t *)&fcp->isp_scratch,
    997       1.53    mjacob 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
    998       1.53    mjacob 		goto dmafail;
    999       1.53    mjacob 	}
   1000       1.53    mjacob 	if (bus_dmamap_create(dmat, len, 1, len, 0, BUS_DMA_NOWAIT,
   1001   1.66.2.7   nathanw 	    &isp->isp_scdmap) || bus_dmamap_load(dmat,
   1002   1.66.2.7   nathanw 	    isp->isp_scdmap, (caddr_t)fcp->isp_scratch, len, NULL,
   1003       1.53    mjacob 	    BUS_DMA_NOWAIT)) {
   1004       1.53    mjacob 		goto dmafail;
   1005       1.53    mjacob 	}
   1006   1.66.2.7   nathanw 	fcp->isp_scdma = isp->isp_scdmap->dm_segs[0].ds_addr;
   1007       1.13   thorpej 	return (0);
   1008       1.53    mjacob dmafail:
   1009       1.53    mjacob 	isp_prt(isp, ISP_LOGERR, "mailbox dma setup failure");
   1010       1.53    mjacob 	for (i = 0; i < isp->isp_maxcmds; i++) {
   1011       1.53    mjacob 		bus_dmamap_destroy(dmat, pcs->pci_xfer_dmap[i]);
   1012       1.53    mjacob 	}
   1013       1.53    mjacob 	free(isp->isp_xflist, M_DEVBUF);
   1014       1.53    mjacob 	free(pcs->pci_xfer_dmap, M_DEVBUF);
   1015       1.53    mjacob 	isp->isp_xflist = NULL;
   1016       1.53    mjacob 	pcs->pci_xfer_dmap = NULL;
   1017       1.53    mjacob 	return (1);
   1018        1.1       cgd }
   1019        1.1       cgd 
   1020        1.1       cgd static int
   1021   1.66.2.1   nathanw isp_pci_dmasetup(struct ispsoftc *isp, struct scsipi_xfer *xs, ispreq_t *rq,
   1022   1.66.2.7   nathanw     u_int16_t *nxtip, u_int16_t optr)
   1023        1.1       cgd {
   1024       1.53    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
   1025       1.52    mjacob 	bus_dmamap_t dmap;
   1026   1.66.2.7   nathanw 	u_int16_t starti = isp->isp_reqidx, nxti = *nxtip;
   1027   1.66.2.7   nathanw 	ispreq_t *qep;
   1028       1.17    mjacob 	int segcnt, seg, error, ovseg, seglim, drq;
   1029        1.1       cgd 
   1030   1.66.2.7   nathanw 	qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, starti);
   1031       1.53    mjacob 	dmap = pcs->pci_xfer_dmap[isp_handle_index(rq->req_handle)];
   1032        1.1       cgd 	if (xs->datalen == 0) {
   1033        1.1       cgd 		rq->req_seg_count = 1;
   1034       1.26    mjacob 		goto mbxsync;
   1035        1.1       cgd 	}
   1036       1.42   thorpej 	if (xs->xs_control & XS_CTL_DATA_IN) {
   1037       1.17    mjacob 		drq = REQFLAG_DATA_IN;
   1038        1.1       cgd 	} else {
   1039       1.17    mjacob 		drq = REQFLAG_DATA_OUT;
   1040        1.1       cgd 	}
   1041        1.1       cgd 
   1042       1.41    mjacob 	if (IS_FC(isp)) {
   1043       1.15    mjacob 		seglim = ISP_RQDSEG_T2;
   1044       1.15    mjacob 		((ispreqt2_t *)rq)->req_totalcnt = xs->datalen;
   1045       1.17    mjacob 		((ispreqt2_t *)rq)->req_flags |= drq;
   1046       1.15    mjacob 	} else {
   1047       1.17    mjacob 		rq->req_flags |= drq;
   1048       1.51    mjacob 		if (XS_CDBLEN(xs) > 12) {
   1049       1.51    mjacob 			seglim = 0;
   1050       1.51    mjacob 		} else {
   1051       1.51    mjacob 			seglim = ISP_RQDSEG;
   1052       1.51    mjacob 		}
   1053       1.15    mjacob 	}
   1054   1.66.2.7   nathanw 	error = bus_dmamap_load(isp->isp_dmatag, dmap, xs->data, xs->datalen,
   1055   1.66.2.1   nathanw 	    NULL, ((xs->xs_control & XS_CTL_NOSLEEP) ?
   1056   1.66.2.3   nathanw 	    BUS_DMA_NOWAIT : BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
   1057   1.66.2.3   nathanw 	    ((xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMA_READ : BUS_DMA_WRITE));
   1058       1.21    mjacob 	if (error) {
   1059  1.66.2.11   nathanw 		isp_prt(isp, ISP_LOGWARN, "unable to load dma (%d)", error);
   1060       1.21    mjacob 		XS_SETERR(xs, HBA_BOTCH);
   1061  1.66.2.10   nathanw 		if (error == EAGAIN || error == ENOMEM)
   1062  1.66.2.10   nathanw 			return (CMD_EAGAIN);
   1063  1.66.2.10   nathanw 		else
   1064  1.66.2.10   nathanw 			return (CMD_COMPLETE);
   1065       1.21    mjacob 	}
   1066       1.13   thorpej 
   1067       1.13   thorpej 	segcnt = dmap->dm_nsegs;
   1068       1.13   thorpej 
   1069       1.57    mjacob 	isp_prt(isp, ISP_LOGDEBUG2, "%d byte %s %p in %d segs",
   1070       1.57    mjacob 	    xs->datalen, (xs->xs_control & XS_CTL_DATA_IN)? "read to" :
   1071       1.57    mjacob 	    "write from", xs->data, segcnt);
   1072       1.57    mjacob 
   1073       1.13   thorpej 	for (seg = 0, rq->req_seg_count = 0;
   1074       1.56    mjacob 	    seglim && seg < segcnt && rq->req_seg_count < seglim;
   1075       1.44    mjacob 	    seg++, rq->req_seg_count++) {
   1076       1.41    mjacob 		if (IS_FC(isp)) {
   1077       1.15    mjacob 			ispreqt2_t *rq2 = (ispreqt2_t *)rq;
   1078       1.15    mjacob 			rq2->req_dataseg[rq2->req_seg_count].ds_count =
   1079       1.15    mjacob 			    dmap->dm_segs[seg].ds_len;
   1080       1.15    mjacob 			rq2->req_dataseg[rq2->req_seg_count].ds_base =
   1081       1.15    mjacob 			    dmap->dm_segs[seg].ds_addr;
   1082       1.15    mjacob 		} else {
   1083       1.15    mjacob 			rq->req_dataseg[rq->req_seg_count].ds_count =
   1084       1.15    mjacob 			    dmap->dm_segs[seg].ds_len;
   1085       1.15    mjacob 			rq->req_dataseg[rq->req_seg_count].ds_base =
   1086       1.15    mjacob 			    dmap->dm_segs[seg].ds_addr;
   1087       1.15    mjacob 		}
   1088       1.63    mjacob 		isp_prt(isp, ISP_LOGDEBUG2, "seg0.[%d]={0x%lx,%lu}",
   1089       1.63    mjacob 		    rq->req_seg_count, (long) dmap->dm_segs[seg].ds_addr,
   1090       1.63    mjacob 		    (unsigned long) dmap->dm_segs[seg].ds_len);
   1091        1.1       cgd 	}
   1092        1.1       cgd 
   1093   1.66.2.7   nathanw 	if (seg == segcnt) {
   1094       1.26    mjacob 		goto dmasync;
   1095   1.66.2.7   nathanw 	}
   1096        1.1       cgd 
   1097        1.1       cgd 	do {
   1098   1.66.2.7   nathanw 		u_int16_t onxti;
   1099   1.66.2.7   nathanw 		ispcontreq_t *crq, *cqe, local;
   1100   1.66.2.7   nathanw 
   1101   1.66.2.7   nathanw 		crq = &local;
   1102   1.66.2.7   nathanw 
   1103   1.66.2.7   nathanw 		cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
   1104   1.66.2.7   nathanw 		onxti = nxti;
   1105   1.66.2.7   nathanw 		nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
   1106   1.66.2.7   nathanw 		if (nxti == optr) {
   1107  1.66.2.11   nathanw 			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGERR, "Request Queue Overflow++");
   1108   1.66.2.7   nathanw 			bus_dmamap_unload(isp->isp_dmatag, dmap);
   1109       1.21    mjacob 			XS_SETERR(xs, HBA_BOTCH);
   1110       1.52    mjacob 			return (CMD_EAGAIN);
   1111        1.1       cgd 		}
   1112        1.1       cgd 		rq->req_header.rqs_entry_count++;
   1113   1.66.2.3   nathanw 		memset((void *)crq, 0, sizeof (*crq));
   1114        1.1       cgd 		crq->req_header.rqs_entry_count = 1;
   1115        1.1       cgd 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
   1116       1.13   thorpej 
   1117       1.13   thorpej 		for (ovseg = 0; seg < segcnt && ovseg < ISP_CDSEG;
   1118       1.13   thorpej 		    rq->req_seg_count++, seg++, ovseg++) {
   1119       1.13   thorpej 			crq->req_dataseg[ovseg].ds_count =
   1120       1.13   thorpej 			    dmap->dm_segs[seg].ds_len;
   1121       1.13   thorpej 			crq->req_dataseg[ovseg].ds_base =
   1122       1.13   thorpej 			    dmap->dm_segs[seg].ds_addr;
   1123       1.63    mjacob 			isp_prt(isp, ISP_LOGDEBUG2, "seg%d.[%d]={0x%lx,%lu}",
   1124       1.57    mjacob 			    rq->req_header.rqs_entry_count - 1,
   1125       1.63    mjacob 			    rq->req_seg_count, (long)dmap->dm_segs[seg].ds_addr,
   1126       1.63    mjacob 			    (unsigned long) dmap->dm_segs[seg].ds_len);
   1127        1.1       cgd 		}
   1128   1.66.2.7   nathanw 		isp_put_cont_req(isp, crq, cqe);
   1129   1.66.2.7   nathanw 		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
   1130       1.13   thorpej 	} while (seg < segcnt);
   1131       1.56    mjacob 
   1132       1.13   thorpej 
   1133       1.26    mjacob dmasync:
   1134   1.66.2.7   nathanw 	bus_dmamap_sync(isp->isp_dmatag, dmap, 0, dmap->dm_mapsize,
   1135       1.42   thorpej 	    (xs->xs_control & XS_CTL_DATA_IN) ?  BUS_DMASYNC_PREREAD :
   1136       1.30    mjacob 	    BUS_DMASYNC_PREWRITE);
   1137       1.26    mjacob 
   1138       1.26    mjacob mbxsync:
   1139   1.66.2.7   nathanw 	switch (rq->req_header.rqs_entry_type) {
   1140   1.66.2.7   nathanw 	case RQSTYPE_REQUEST:
   1141   1.66.2.7   nathanw 		isp_put_request(isp, rq, qep);
   1142   1.66.2.7   nathanw 		break;
   1143   1.66.2.7   nathanw 	case RQSTYPE_CMDONLY:
   1144   1.66.2.7   nathanw 		isp_put_extended_request(isp, (ispextreq_t *)rq,
   1145   1.66.2.7   nathanw 		    (ispextreq_t *)qep);
   1146   1.66.2.7   nathanw 		break;
   1147   1.66.2.7   nathanw 	case RQSTYPE_T2RQS:
   1148   1.66.2.7   nathanw 		isp_put_request_t2(isp, (ispreqt2_t *) rq, (ispreqt2_t *) qep);
   1149   1.66.2.7   nathanw 		break;
   1150   1.66.2.7   nathanw 	}
   1151   1.66.2.7   nathanw 	*nxtip = nxti;
   1152       1.30    mjacob 	return (CMD_QUEUED);
   1153       1.26    mjacob }
   1154       1.26    mjacob 
   1155       1.26    mjacob static int
   1156   1.66.2.1   nathanw isp_pci_intr(void *arg)
   1157       1.26    mjacob {
   1158   1.66.2.4   nathanw 	u_int16_t isr, sema, mbox;
   1159   1.66.2.4   nathanw 	struct ispsoftc *isp = arg;
   1160   1.66.2.4   nathanw 
   1161   1.66.2.4   nathanw 	isp->isp_intcnt++;
   1162   1.66.2.4   nathanw 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
   1163   1.66.2.4   nathanw 		isp->isp_intbogus++;
   1164   1.66.2.4   nathanw 		return (0);
   1165   1.66.2.4   nathanw 	} else {
   1166   1.66.2.4   nathanw 		isp->isp_osinfo.onintstack = 1;
   1167   1.66.2.4   nathanw 		isp_intr(isp, isr, sema, mbox);
   1168   1.66.2.4   nathanw 		isp->isp_osinfo.onintstack = 0;
   1169   1.66.2.4   nathanw 		return (1);
   1170   1.66.2.4   nathanw 	}
   1171       1.13   thorpej }
   1172       1.13   thorpej 
   1173       1.13   thorpej static void
   1174   1.66.2.1   nathanw isp_pci_dmateardown(struct ispsoftc *isp, XS_T *xs, u_int16_t handle)
   1175       1.13   thorpej {
   1176       1.53    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
   1177       1.53    mjacob 	bus_dmamap_t dmap = pcs->pci_xfer_dmap[isp_handle_index(handle)];
   1178   1.66.2.7   nathanw 	bus_dmamap_sync(isp->isp_dmatag, dmap, 0, dmap->dm_mapsize,
   1179       1.42   thorpej 	    xs->xs_control & XS_CTL_DATA_IN ?
   1180       1.13   thorpej 	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1181   1.66.2.7   nathanw 	bus_dmamap_unload(isp->isp_dmatag, dmap);
   1182        1.1       cgd }
   1183        1.1       cgd 
   1184        1.1       cgd static void
   1185   1.66.2.1   nathanw isp_pci_reset1(struct ispsoftc *isp)
   1186        1.1       cgd {
   1187        1.1       cgd 	/* Make sure the BIOS is disabled */
   1188        1.1       cgd 	isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
   1189   1.66.2.8   nathanw 	if (isp->isp_osinfo.no_mbox_ints == 0) {
   1190   1.66.2.8   nathanw 		ENABLE_INTS(isp);
   1191   1.66.2.8   nathanw 	}
   1192   1.66.2.8   nathanw 
   1193       1.15    mjacob }
   1194       1.15    mjacob 
   1195       1.15    mjacob static void
   1196   1.66.2.1   nathanw isp_pci_dumpregs(struct ispsoftc *isp, const char *msg)
   1197       1.15    mjacob {
   1198       1.53    mjacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
   1199       1.53    mjacob 	if (msg)
   1200       1.53    mjacob 		printf("%s: %s\n", isp->isp_name, msg);
   1201       1.53    mjacob 	if (IS_SCSI(isp))
   1202       1.53    mjacob 		printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
   1203       1.53    mjacob 	else
   1204       1.53    mjacob 		printf("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
   1205       1.53    mjacob 	printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
   1206       1.53    mjacob 	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
   1207       1.53    mjacob 	printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
   1208       1.53    mjacob 
   1209       1.53    mjacob 
   1210       1.53    mjacob 	if (IS_SCSI(isp)) {
   1211       1.53    mjacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   1212       1.53    mjacob 		printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
   1213       1.53    mjacob 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
   1214       1.53    mjacob 			ISP_READ(isp, CDMA_FIFO_STS));
   1215       1.53    mjacob 		printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
   1216       1.53    mjacob 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
   1217       1.53    mjacob 			ISP_READ(isp, DDMA_FIFO_STS));
   1218       1.53    mjacob 		printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
   1219       1.53    mjacob 			ISP_READ(isp, SXP_INTERRUPT),
   1220       1.53    mjacob 			ISP_READ(isp, SXP_GROSS_ERR),
   1221       1.53    mjacob 			ISP_READ(isp, SXP_PINS_CTRL));
   1222       1.53    mjacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
   1223       1.53    mjacob 	}
   1224       1.53    mjacob 	printf("    mbox regs: %x %x %x %x %x\n",
   1225       1.53    mjacob 	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
   1226       1.53    mjacob 	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
   1227       1.53    mjacob 	    ISP_READ(isp, OUTMAILBOX4));
   1228       1.53    mjacob 	printf("    PCI Status Command/Status=%x\n",
   1229       1.53    mjacob 	    pci_conf_read(pcs->pci_pc, pcs->pci_tag, PCI_COMMAND_STATUS_REG));
   1230        1.1       cgd }
   1231