xref: /freebsd/sys/dev/isp/isp_pci.c (revision 31354813f3c6e87532189be77c2f10a017c55472)
1aad970f1SDavid E. O'Brien /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
41b760be4SAlexander Motin  * Copyright (c) 2009-2020 Alexander Motin <mav@FreeBSD.org>
52df76c16SMatt Jacob  * Copyright (c) 1997-2008 by Matthew Jacob
6e5265237SMatt Jacob  * All rights reserved.
765adb54cSMatt Jacob  *
865adb54cSMatt Jacob  * Redistribution and use in source and binary forms, with or without
965adb54cSMatt Jacob  * modification, are permitted provided that the following conditions
1065adb54cSMatt Jacob  * are met:
1165adb54cSMatt Jacob  * 1. Redistributions of source code must retain the above copyright
1265adb54cSMatt Jacob  *    notice immediately at the beginning of the file, without modification,
1365adb54cSMatt Jacob  *    this list of conditions, and the following disclaimer.
14aa57fd6fSMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
15aa57fd6fSMatt Jacob  *    derived from this software without specific prior written permission.
1665adb54cSMatt Jacob  *
1765adb54cSMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1865adb54cSMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1965adb54cSMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2065adb54cSMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2165adb54cSMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2265adb54cSMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2365adb54cSMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2465adb54cSMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2565adb54cSMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2665adb54cSMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2765adb54cSMatt Jacob  * SUCH DAMAGE.
2865adb54cSMatt Jacob  */
29799881e0SMatt Jacob /*
30799881e0SMatt Jacob  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
31799881e0SMatt Jacob  * FreeBSD Version.
32799881e0SMatt Jacob  */
33fdafd315SWarner Losh 
34960f6939SMatt Jacob #include <sys/param.h>
35960f6939SMatt Jacob #include <sys/systm.h>
36960f6939SMatt Jacob #include <sys/kernel.h>
37960f6939SMatt Jacob #include <sys/module.h>
389a5af410SMatt Jacob #include <sys/linker.h>
39960f6939SMatt Jacob #include <sys/bus.h>
4074a96f43SJohn Baldwin #include <sys/stdint.h>
4177e6a3b2SWarner Losh #include <dev/pci/pcireg.h>
4277e6a3b2SWarner Losh #include <dev/pci/pcivar.h>
43d720e6d5SJustin T. Gibbs #include <machine/bus.h>
44960f6939SMatt Jacob #include <machine/resource.h>
45960f6939SMatt Jacob #include <sys/rman.h>
46960f6939SMatt Jacob #include <sys/malloc.h>
472df76c16SMatt Jacob #include <sys/uio.h>
48960f6939SMatt Jacob #include <dev/isp/isp_freebsd.h>
49d59bd469SMatt Jacob 
5010365e5aSMatt Jacob static uint32_t isp_pci_rd_reg_2400(ispsoftc_t *, int);
5110365e5aSMatt Jacob static void isp_pci_wr_reg_2400(ispsoftc_t *, int, uint32_t);
52cd201b7bSAlexander Motin static uint32_t isp_pci_rd_reg_2600(ispsoftc_t *, int);
53cd201b7bSAlexander Motin static void isp_pci_wr_reg_2600(ispsoftc_t *, int, uint32_t);
540e6bc811SAlexander Motin static void isp_pci_run_isr_2400(ispsoftc_t *);
559cd7268eSMatt Jacob static int isp_pci_mbxdma(ispsoftc_t *);
56a1fa0267SAlexander Motin static void isp_pci_mbxdmafree(ispsoftc_t *);
57a1fa0267SAlexander Motin static int isp_pci_irqsetup(ispsoftc_t *);
58d951bbcaSMatt Jacob 
5910365e5aSMatt Jacob static struct ispmdvec mdvec_2400 = {
600e6bc811SAlexander Motin 	isp_pci_run_isr_2400,
6110365e5aSMatt Jacob 	isp_pci_rd_reg_2400,
6210365e5aSMatt Jacob 	isp_pci_wr_reg_2400,
6310365e5aSMatt Jacob 	isp_pci_mbxdma,
64f6854a0cSAlexander Motin 	isp_send_cmd,
65a1fa0267SAlexander Motin 	isp_pci_irqsetup,
6610365e5aSMatt Jacob 	NULL
6710365e5aSMatt Jacob };
6810365e5aSMatt Jacob 
692df76c16SMatt Jacob static struct ispmdvec mdvec_2500 = {
700e6bc811SAlexander Motin 	isp_pci_run_isr_2400,
712df76c16SMatt Jacob 	isp_pci_rd_reg_2400,
722df76c16SMatt Jacob 	isp_pci_wr_reg_2400,
732df76c16SMatt Jacob 	isp_pci_mbxdma,
74f6854a0cSAlexander Motin 	isp_send_cmd,
75a1fa0267SAlexander Motin 	isp_pci_irqsetup,
762df76c16SMatt Jacob 	NULL
772df76c16SMatt Jacob };
782df76c16SMatt Jacob 
79218be0b2SAlexander Motin static struct ispmdvec mdvec_2600 = {
800e6bc811SAlexander Motin 	isp_pci_run_isr_2400,
81cd201b7bSAlexander Motin 	isp_pci_rd_reg_2600,
82cd201b7bSAlexander Motin 	isp_pci_wr_reg_2600,
83218be0b2SAlexander Motin 	isp_pci_mbxdma,
84f6854a0cSAlexander Motin 	isp_send_cmd,
85a1fa0267SAlexander Motin 	isp_pci_irqsetup,
86218be0b2SAlexander Motin 	NULL
87218be0b2SAlexander Motin };
88218be0b2SAlexander Motin 
8914e084adSAlexander Motin static struct ispmdvec mdvec_2700 = {
9014e084adSAlexander Motin 	isp_pci_run_isr_2400,
9114e084adSAlexander Motin 	isp_pci_rd_reg_2600,
9214e084adSAlexander Motin 	isp_pci_wr_reg_2600,
9314e084adSAlexander Motin 	isp_pci_mbxdma,
94f6854a0cSAlexander Motin 	isp_send_cmd,
9514e084adSAlexander Motin 	isp_pci_irqsetup,
9614e084adSAlexander Motin 	NULL
9714e084adSAlexander Motin };
9814e084adSAlexander Motin 
99407abff6SJoerg Pulz static struct ispmdvec mdvec_2800 = {
100407abff6SJoerg Pulz 	isp_pci_run_isr_2400,
101407abff6SJoerg Pulz 	isp_pci_rd_reg_2600,
102407abff6SJoerg Pulz 	isp_pci_wr_reg_2600,
103407abff6SJoerg Pulz 	isp_pci_mbxdma,
104407abff6SJoerg Pulz 	isp_send_cmd,
105407abff6SJoerg Pulz 	isp_pci_irqsetup,
106407abff6SJoerg Pulz 	NULL
107407abff6SJoerg Pulz };
108407abff6SJoerg Pulz 
10965adb54cSMatt Jacob #ifndef	PCIM_CMD_INVEN
11065adb54cSMatt Jacob #define	PCIM_CMD_INVEN			0x10
11165adb54cSMatt Jacob #endif
11265adb54cSMatt Jacob #ifndef	PCIM_CMD_BUSMASTEREN
11365adb54cSMatt Jacob #define	PCIM_CMD_BUSMASTEREN		0x0004
11465adb54cSMatt Jacob #endif
115d951bbcaSMatt Jacob #ifndef	PCIM_CMD_PERRESPEN
116d951bbcaSMatt Jacob #define	PCIM_CMD_PERRESPEN		0x0040
117d951bbcaSMatt Jacob #endif
118d951bbcaSMatt Jacob #ifndef	PCIM_CMD_SEREN
119d951bbcaSMatt Jacob #define	PCIM_CMD_SEREN			0x0100
120d951bbcaSMatt Jacob #endif
1218a97c03aSMatt Jacob #ifndef	PCIM_CMD_INTX_DISABLE
1228a97c03aSMatt Jacob #define	PCIM_CMD_INTX_DISABLE		0x0400
1238a97c03aSMatt Jacob #endif
124d951bbcaSMatt Jacob 
125d951bbcaSMatt Jacob #ifndef	PCIR_COMMAND
126d951bbcaSMatt Jacob #define	PCIR_COMMAND			0x04
127d951bbcaSMatt Jacob #endif
128d951bbcaSMatt Jacob 
129d951bbcaSMatt Jacob #ifndef	PCIR_CACHELNSZ
130d951bbcaSMatt Jacob #define	PCIR_CACHELNSZ			0x0c
131d951bbcaSMatt Jacob #endif
132d951bbcaSMatt Jacob 
133d951bbcaSMatt Jacob #ifndef	PCIR_LATTIMER
134d951bbcaSMatt Jacob #define	PCIR_LATTIMER			0x0d
135d951bbcaSMatt Jacob #endif
136d951bbcaSMatt Jacob 
137ab6d0040SMatt Jacob #ifndef	PCIR_ROMADDR
138ab6d0040SMatt Jacob #define	PCIR_ROMADDR			0x30
139ab6d0040SMatt Jacob #endif
140ab6d0040SMatt Jacob 
14165adb54cSMatt Jacob #define	PCI_VENDOR_QLOGIC		0x1077
14265adb54cSMatt Jacob 
1438872e3d7SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2422	0x2422
14441675df0SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2432	0x2432
1452df76c16SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2532	0x2532
146a959d921SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP5432	0x5432
147218be0b2SAlexander Motin #define	PCI_PRODUCT_QLOGIC_ISP2031	0x2031
14867eeadd2SAlexander Motin #define	PCI_PRODUCT_QLOGIC_ISP8031	0x8031
14914e084adSAlexander Motin #define	PCI_PRODUCT_QLOGIC_ISP2684	0x2171
15014e084adSAlexander Motin #define	PCI_PRODUCT_QLOGIC_ISP2692	0x2b61
15114e084adSAlexander Motin #define	PCI_PRODUCT_QLOGIC_ISP2714	0x2071
15214e084adSAlexander Motin #define	PCI_PRODUCT_QLOGIC_ISP2722	0x2261
153407abff6SJoerg Pulz #define	PCI_PRODUCT_QLOGIC_ISP2812	0x2281
154407abff6SJoerg Pulz #define	PCI_PRODUCT_QLOGIC_ISP2814	0x2081
1559a5af410SMatt Jacob 
1566c426685SMatt Jacob #define	PCI_QLOGIC_ISP2422	\
1576c426685SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2422 << 16) | PCI_VENDOR_QLOGIC)
15841675df0SMatt Jacob #define	PCI_QLOGIC_ISP2432	\
15941675df0SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2432 << 16) | PCI_VENDOR_QLOGIC)
1602df76c16SMatt Jacob #define	PCI_QLOGIC_ISP2532	\
1612df76c16SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2532 << 16) | PCI_VENDOR_QLOGIC)
16214e084adSAlexander Motin #define	PCI_QLOGIC_ISP5432	\
16314e084adSAlexander Motin 	((PCI_PRODUCT_QLOGIC_ISP5432 << 16) | PCI_VENDOR_QLOGIC)
164218be0b2SAlexander Motin #define	PCI_QLOGIC_ISP2031	\
165218be0b2SAlexander Motin 	((PCI_PRODUCT_QLOGIC_ISP2031 << 16) | PCI_VENDOR_QLOGIC)
16667eeadd2SAlexander Motin #define	PCI_QLOGIC_ISP8031	\
16767eeadd2SAlexander Motin 	((PCI_PRODUCT_QLOGIC_ISP8031 << 16) | PCI_VENDOR_QLOGIC)
16814e084adSAlexander Motin #define	PCI_QLOGIC_ISP2684	\
16914e084adSAlexander Motin 	((PCI_PRODUCT_QLOGIC_ISP2684 << 16) | PCI_VENDOR_QLOGIC)
17014e084adSAlexander Motin #define	PCI_QLOGIC_ISP2692	\
17114e084adSAlexander Motin 	((PCI_PRODUCT_QLOGIC_ISP2692 << 16) | PCI_VENDOR_QLOGIC)
17214e084adSAlexander Motin #define	PCI_QLOGIC_ISP2714	\
17314e084adSAlexander Motin 	((PCI_PRODUCT_QLOGIC_ISP2714 << 16) | PCI_VENDOR_QLOGIC)
17414e084adSAlexander Motin #define	PCI_QLOGIC_ISP2722	\
17514e084adSAlexander Motin 	((PCI_PRODUCT_QLOGIC_ISP2722 << 16) | PCI_VENDOR_QLOGIC)
176407abff6SJoerg Pulz #define	PCI_QLOGIC_ISP2812	\
177407abff6SJoerg Pulz 	((PCI_PRODUCT_QLOGIC_ISP2812 << 16) | PCI_VENDOR_QLOGIC)
178407abff6SJoerg Pulz #define	PCI_QLOGIC_ISP2814	\
179407abff6SJoerg Pulz 	((PCI_PRODUCT_QLOGIC_ISP2814 << 16) | PCI_VENDOR_QLOGIC)
18067eeadd2SAlexander Motin 
181d951bbcaSMatt Jacob #define	PCI_DFLT_LTNCY	0x40
182d951bbcaSMatt Jacob #define	PCI_DFLT_LNSZ	0x10
18365adb54cSMatt Jacob 
184960f6939SMatt Jacob static int isp_pci_probe (device_t);
185960f6939SMatt Jacob static int isp_pci_attach (device_t);
18610365e5aSMatt Jacob static int isp_pci_detach (device_t);
18765adb54cSMatt Jacob 
1881923f739SMatt Jacob 
18965adb54cSMatt Jacob struct isp_pcisoftc {
1909cd7268eSMatt Jacob 	ispsoftc_t			pci_isp;
191e95725cbSMatt Jacob 	struct resource *		regs;
192d876d6c3SAlexander Motin 	struct resource *		regs1;
193cd201b7bSAlexander Motin 	struct resource *		regs2;
19408826086SAlexander Motin 	struct {
195e95725cbSMatt Jacob 		int				iqd;
19608826086SAlexander Motin 		struct resource *		irq;
19708826086SAlexander Motin 		void *				ih;
19808826086SAlexander Motin 	} irq[ISP_MAX_IRQS];
199e95725cbSMatt Jacob 	int				rtp;
200e95725cbSMatt Jacob 	int				rgd;
201d876d6c3SAlexander Motin 	int				rtp1;
202d876d6c3SAlexander Motin 	int				rgd1;
203cd201b7bSAlexander Motin 	int				rtp2;
204cd201b7bSAlexander Motin 	int				rgd2;
2051923f739SMatt Jacob 	bus_dma_tag_t			dmat;
2060a70657fSMatt Jacob 	int				msicount;
20765adb54cSMatt Jacob };
20865adb54cSMatt Jacob 
20910365e5aSMatt Jacob 
210960f6939SMatt Jacob static device_method_t isp_pci_methods[] = {
211960f6939SMatt Jacob 	/* Device interface */
212960f6939SMatt Jacob 	DEVMETHOD(device_probe,		isp_pci_probe),
213960f6939SMatt Jacob 	DEVMETHOD(device_attach,	isp_pci_attach),
21410365e5aSMatt Jacob 	DEVMETHOD(device_detach,	isp_pci_detach),
215960f6939SMatt Jacob 	{ 0, 0 }
21665adb54cSMatt Jacob };
21765adb54cSMatt Jacob 
218960f6939SMatt Jacob static driver_t isp_pci_driver = {
219960f6939SMatt Jacob 	"isp", isp_pci_methods, sizeof (struct isp_pcisoftc)
220960f6939SMatt Jacob };
221d1a9e9b9SJohn Baldwin 
222d1a9e9b9SJohn Baldwin DRIVER_MODULE(isp, pci, isp_pci_driver, 0, 0);
223d45ce511SEitan Adler MODULE_DEPEND(isp, cam, 1, 1, 1);
224d45ce511SEitan Adler MODULE_DEPEND(isp, firmware, 1, 1, 1);
2259e7d423dSMatt Jacob static int isp_nvports = 0;
22665adb54cSMatt Jacob 
227960f6939SMatt Jacob static int
isp_pci_probe(device_t dev)228960f6939SMatt Jacob isp_pci_probe(device_t dev)
22965adb54cSMatt Jacob {
230960f6939SMatt Jacob 	switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
2318872e3d7SMatt Jacob 	case PCI_QLOGIC_ISP2422:
2328872e3d7SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
2338872e3d7SMatt Jacob 		break;
23441675df0SMatt Jacob 	case PCI_QLOGIC_ISP2432:
23541675df0SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2432 PCI FC-AL Adapter");
23641675df0SMatt Jacob 		break;
2372df76c16SMatt Jacob 	case PCI_QLOGIC_ISP2532:
2382df76c16SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2532 PCI FC-AL Adapter");
2392df76c16SMatt Jacob 		break;
240a959d921SMatt Jacob 	case PCI_QLOGIC_ISP5432:
241a959d921SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 5432 PCI FC-AL Adapter");
242a959d921SMatt Jacob 		break;
243218be0b2SAlexander Motin 	case PCI_QLOGIC_ISP2031:
244218be0b2SAlexander Motin 		device_set_desc(dev, "Qlogic ISP 2031 PCI FC-AL Adapter");
245218be0b2SAlexander Motin 		break;
24667eeadd2SAlexander Motin 	case PCI_QLOGIC_ISP8031:
24767eeadd2SAlexander Motin 		device_set_desc(dev, "Qlogic ISP 8031 PCI FCoE Adapter");
24867eeadd2SAlexander Motin 		break;
24914e084adSAlexander Motin 	case PCI_QLOGIC_ISP2684:
25014e084adSAlexander Motin 		device_set_desc(dev, "Qlogic ISP 2684 PCI FC Adapter");
25114e084adSAlexander Motin 		break;
25214e084adSAlexander Motin 	case PCI_QLOGIC_ISP2692:
25314e084adSAlexander Motin 		device_set_desc(dev, "Qlogic ISP 2692 PCI FC Adapter");
25414e084adSAlexander Motin 		break;
25514e084adSAlexander Motin 	case PCI_QLOGIC_ISP2714:
25614e084adSAlexander Motin 		device_set_desc(dev, "Qlogic ISP 2714 PCI FC Adapter");
25714e084adSAlexander Motin 		break;
25814e084adSAlexander Motin 	case PCI_QLOGIC_ISP2722:
25914e084adSAlexander Motin 		device_set_desc(dev, "Qlogic ISP 2722 PCI FC Adapter");
26014e084adSAlexander Motin 		break;
261407abff6SJoerg Pulz 	case PCI_QLOGIC_ISP2812:
262407abff6SJoerg Pulz 		device_set_desc(dev, "Qlogic ISP 2812 PCI FC Adapter");
263407abff6SJoerg Pulz 		break;
264407abff6SJoerg Pulz 	case PCI_QLOGIC_ISP2814:
265407abff6SJoerg Pulz 		device_set_desc(dev, "Qlogic ISP 2814 PCI FC Adapter");
266407abff6SJoerg Pulz 		break;
26765adb54cSMatt Jacob 	default:
268960f6939SMatt Jacob 		return (ENXIO);
26965adb54cSMatt Jacob 	}
27073030e03SMatt Jacob 	if (isp_announced == 0 && bootverbose) {
271d02373f1SMatt Jacob 		printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
272a95ae193SMatt Jacob 		    "Core Version %d.%d\n",
273d720e6d5SJustin T. Gibbs 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
274d720e6d5SJustin T. Gibbs 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
27573030e03SMatt Jacob 		isp_announced++;
27665adb54cSMatt Jacob 	}
27756aef503SMatt Jacob 	/*
27856aef503SMatt Jacob 	 * XXXX: Here is where we might load the f/w module
27956aef503SMatt Jacob 	 * XXXX: (or increase a reference count to it).
28056aef503SMatt Jacob 	 */
281b77e575eSWarner Losh 	return (BUS_PROBE_DEFAULT);
28265adb54cSMatt Jacob }
28365adb54cSMatt Jacob 
2849cd7268eSMatt Jacob static void
isp_get_generic_options(device_t dev,ispsoftc_t * isp)2859e7d423dSMatt Jacob isp_get_generic_options(device_t dev, ispsoftc_t *isp)
2869cd7268eSMatt Jacob {
2879cd7268eSMatt Jacob 	int tval;
288f7c631bcSMatt Jacob 
2899cd7268eSMatt Jacob 	tval = 0;
2902df76c16SMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fwload_disable", &tval) == 0 && tval != 0) {
2919cd7268eSMatt Jacob 		isp->isp_confopts |= ISP_CFG_NORELOAD;
2929cd7268eSMatt Jacob 	}
2939cd7268eSMatt Jacob 	tval = 0;
294*31354813SKenneth D. Merry 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fwload_force", &tval) == 0 && tval != 0) {
295*31354813SKenneth D. Merry 		isp->isp_confopts |= ISP_CFG_FWLOAD_FORCE;
296*31354813SKenneth D. Merry 	}
297*31354813SKenneth D. Merry 	if ((isp->isp_confopts & (ISP_CFG_NORELOAD|ISP_CFG_FWLOAD_FORCE)) ==
298*31354813SKenneth D. Merry 	    (ISP_CFG_NORELOAD|ISP_CFG_FWLOAD_FORCE)) {
299*31354813SKenneth D. Merry 		device_printf(dev, "WARNING: both fwload_disable and "
300*31354813SKenneth D. Merry 		    "fwload_force set, ispfw(4) loading disabled\n");
301*31354813SKenneth D. Merry 	}
302*31354813SKenneth D. Merry 	tval = 0;
3032df76c16SMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "ignore_nvram", &tval) == 0 && tval != 0) {
3049cd7268eSMatt Jacob 		isp->isp_confopts |= ISP_CFG_NONVRAM;
3059cd7268eSMatt Jacob 	}
306336b5612SMatt Jacob 	tval = 0;
3072df76c16SMatt Jacob 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "debug", &tval);
308336b5612SMatt Jacob 	if (tval) {
309336b5612SMatt Jacob 		isp->isp_dblev = tval;
310336b5612SMatt Jacob 	} else {
311336b5612SMatt Jacob 		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
312336b5612SMatt Jacob 	}
313336b5612SMatt Jacob 	if (bootverbose) {
314336b5612SMatt Jacob 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
315336b5612SMatt Jacob 	}
3169e7d423dSMatt Jacob 	tval = -1;
3172df76c16SMatt Jacob 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "vports", &tval);
318ffe6ea05SAlexander Motin 	if (tval > 0 && tval <= 254) {
3199e7d423dSMatt Jacob 		isp_nvports = tval;
3202df76c16SMatt Jacob 	}
3212df76c16SMatt Jacob 	tval = 7;
3222df76c16SMatt Jacob 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "quickboot_time", &tval);
3232df76c16SMatt Jacob 	isp_quickboot_time = tval;
324336b5612SMatt Jacob }
325336b5612SMatt Jacob 
326336b5612SMatt Jacob static void
isp_get_specific_options(device_t dev,int chan,ispsoftc_t * isp)3272df76c16SMatt Jacob isp_get_specific_options(device_t dev, int chan, ispsoftc_t *isp)
328336b5612SMatt Jacob {
329336b5612SMatt Jacob 	const char *sptr;
3309e7d423dSMatt Jacob 	int tval = 0;
33107f56f1cSAlexander Motin 	char prefix[12], name[16];
332336b5612SMatt Jacob 
33307f56f1cSAlexander Motin 	if (chan == 0)
33407f56f1cSAlexander Motin 		prefix[0] = 0;
33507f56f1cSAlexander Motin 	else
33607f56f1cSAlexander Motin 		snprintf(prefix, sizeof(prefix), "chan%d.", chan);
33707f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%siid", prefix);
33807f56f1cSAlexander Motin 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
33907f56f1cSAlexander Motin 	    name, &tval)) {
3402df76c16SMatt Jacob 		ISP_FC_PC(isp, chan)->default_id = 109 - chan;
3412df76c16SMatt Jacob 	} else {
3422df76c16SMatt Jacob 		ISP_FC_PC(isp, chan)->default_id = tval - chan;
343336b5612SMatt Jacob 		isp->isp_confopts |= ISP_CFG_OWNLOOPID;
344336b5612SMatt Jacob 	}
3452df76c16SMatt Jacob 
3462df76c16SMatt Jacob 	tval = -1;
34707f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%srole", prefix);
34807f56f1cSAlexander Motin 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
34907f56f1cSAlexander Motin 	    name, &tval) == 0) {
3502df76c16SMatt Jacob 		switch (tval) {
3512df76c16SMatt Jacob 		case ISP_ROLE_NONE:
3522df76c16SMatt Jacob 		case ISP_ROLE_INITIATOR:
3532df76c16SMatt Jacob 		case ISP_ROLE_TARGET:
35407f56f1cSAlexander Motin 		case ISP_ROLE_BOTH:
35507f56f1cSAlexander Motin 			device_printf(dev, "Chan %d setting role to 0x%x\n", chan, tval);
3562df76c16SMatt Jacob 			break;
3572df76c16SMatt Jacob 		default:
3582df76c16SMatt Jacob 			tval = -1;
3592df76c16SMatt Jacob 			break;
360336b5612SMatt Jacob 		}
361336b5612SMatt Jacob 	}
3622df76c16SMatt Jacob 	if (tval == -1) {
3632df76c16SMatt Jacob 		tval = ISP_DEFAULT_ROLES;
3642df76c16SMatt Jacob 	}
3654ecb1d4aSMatt Jacob 	ISP_FC_PC(isp, chan)->def_role = tval;
366336b5612SMatt Jacob 
3679cd7268eSMatt Jacob 	tval = 0;
36807f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%sfullduplex", prefix);
36907f56f1cSAlexander Motin 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
37007f56f1cSAlexander Motin 	    name, &tval) == 0 && tval != 0) {
3719cd7268eSMatt Jacob 		isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
3729cd7268eSMatt Jacob 	}
3734d24901aSPedro F. Giffuni 	sptr = NULL;
37407f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%stopology", prefix);
37507f56f1cSAlexander Motin 	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
3764d24901aSPedro F. Giffuni 	    name, (const char **) &sptr) == 0 && sptr != NULL) {
3779cd7268eSMatt Jacob 		if (strcmp(sptr, "lport") == 0) {
3789cd7268eSMatt Jacob 			isp->isp_confopts |= ISP_CFG_LPORT;
3799cd7268eSMatt Jacob 		} else if (strcmp(sptr, "nport") == 0) {
3809cd7268eSMatt Jacob 			isp->isp_confopts |= ISP_CFG_NPORT;
3819cd7268eSMatt Jacob 		} else if (strcmp(sptr, "lport-only") == 0) {
3829cd7268eSMatt Jacob 			isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
3839cd7268eSMatt Jacob 		} else if (strcmp(sptr, "nport-only") == 0) {
3849cd7268eSMatt Jacob 			isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
3859cd7268eSMatt Jacob 		}
386960f6939SMatt Jacob 	}
387960f6939SMatt Jacob 
38808167db8SKenneth D. Merry #ifdef ISP_FCTAPE_OFF
38908167db8SKenneth D. Merry 	isp->isp_confopts |= ISP_CFG_NOFCTAPE;
39008167db8SKenneth D. Merry #else
39108167db8SKenneth D. Merry 	isp->isp_confopts |= ISP_CFG_FCTAPE;
39208167db8SKenneth D. Merry #endif
39308167db8SKenneth D. Merry 
394387d8239SMatt Jacob 	tval = 0;
39507f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%snofctape", prefix);
39607f56f1cSAlexander Motin 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
39707f56f1cSAlexander Motin 	    name, &tval);
398387d8239SMatt Jacob 	if (tval) {
39908167db8SKenneth D. Merry 		isp->isp_confopts &= ~ISP_CFG_FCTAPE;
400387d8239SMatt Jacob 		isp->isp_confopts |= ISP_CFG_NOFCTAPE;
401387d8239SMatt Jacob 	}
402387d8239SMatt Jacob 
403387d8239SMatt Jacob 	tval = 0;
40407f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%sfctape", prefix);
40507f56f1cSAlexander Motin 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
40607f56f1cSAlexander Motin 	    name, &tval);
407387d8239SMatt Jacob 	if (tval) {
408387d8239SMatt Jacob 		isp->isp_confopts &= ~ISP_CFG_NOFCTAPE;
409387d8239SMatt Jacob 		isp->isp_confopts |= ISP_CFG_FCTAPE;
410387d8239SMatt Jacob 	}
411387d8239SMatt Jacob 
412387d8239SMatt Jacob 
4139ba86737SMatt Jacob 	/*
4149cd7268eSMatt Jacob 	 * Because the resource_*_value functions can neither return
4159cd7268eSMatt Jacob 	 * 64 bit integer values, nor can they be directly coerced
4169cd7268eSMatt Jacob 	 * to interpret the right hand side of the assignment as
4179cd7268eSMatt Jacob 	 * you want them to interpret it, we have to force WWN
4189cd7268eSMatt Jacob 	 * hint replacement to specify WWN strings with a leading
4199cd7268eSMatt Jacob 	 * 'w' (e..g w50000000aaaa0001). Sigh.
4209cd7268eSMatt Jacob 	 */
4214d24901aSPedro F. Giffuni 	sptr = NULL;
42207f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%sportwwn", prefix);
42307f56f1cSAlexander Motin 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
42407f56f1cSAlexander Motin 	    name, (const char **) &sptr);
4254d24901aSPedro F. Giffuni 	if (tval == 0 && sptr != NULL && *sptr++ == 'w') {
4264d24901aSPedro F. Giffuni 		char *eptr = NULL;
4272df76c16SMatt Jacob 		ISP_FC_PC(isp, chan)->def_wwpn = strtouq(sptr, &eptr, 16);
4282df76c16SMatt Jacob 		if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwpn == -1) {
4299cd7268eSMatt Jacob 			device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
4302df76c16SMatt Jacob 			ISP_FC_PC(isp, chan)->def_wwpn = 0;
4319cd7268eSMatt Jacob 		}
4329cd7268eSMatt Jacob 	}
4339cd7268eSMatt Jacob 
4344d24901aSPedro F. Giffuni 	sptr = NULL;
43507f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%snodewwn", prefix);
43607f56f1cSAlexander Motin 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
43707f56f1cSAlexander Motin 	    name, (const char **) &sptr);
4384d24901aSPedro F. Giffuni 	if (tval == 0 && sptr != NULL && *sptr++ == 'w') {
4394d24901aSPedro F. Giffuni 		char *eptr = NULL;
4402df76c16SMatt Jacob 		ISP_FC_PC(isp, chan)->def_wwnn = strtouq(sptr, &eptr, 16);
4412df76c16SMatt Jacob 		if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwnn == 0) {
4429cd7268eSMatt Jacob 			device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
4432df76c16SMatt Jacob 			ISP_FC_PC(isp, chan)->def_wwnn = 0;
4449cd7268eSMatt Jacob 		}
4459cd7268eSMatt Jacob 	}
4469cd7268eSMatt Jacob 
447f7c631bcSMatt Jacob 	tval = -1;
44807f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%sloop_down_limit", prefix);
44907f56f1cSAlexander Motin 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
45007f56f1cSAlexander Motin 	    name, &tval);
45110365e5aSMatt Jacob 	if (tval >= 0 && tval < 0xffff) {
4522df76c16SMatt Jacob 		ISP_FC_PC(isp, chan)->loop_down_limit = tval;
45310365e5aSMatt Jacob 	} else {
4542df76c16SMatt Jacob 		ISP_FC_PC(isp, chan)->loop_down_limit = isp_loop_down_limit;
45510365e5aSMatt Jacob 	}
45610365e5aSMatt Jacob 
457f7c631bcSMatt Jacob 	tval = -1;
45807f56f1cSAlexander Motin 	snprintf(name, sizeof(name), "%sgone_device_time", prefix);
45907f56f1cSAlexander Motin 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
46007f56f1cSAlexander Motin 	    name, &tval);
461f7c631bcSMatt Jacob 	if (tval >= 0 && tval < 0xffff) {
4622df76c16SMatt Jacob 		ISP_FC_PC(isp, chan)->gone_device_time = tval;
463f7c631bcSMatt Jacob 	} else {
4642df76c16SMatt Jacob 		ISP_FC_PC(isp, chan)->gone_device_time = isp_gone_device_time;
465f7c631bcSMatt Jacob 	}
4669cd7268eSMatt Jacob }
4679cd7268eSMatt Jacob 
4689cd7268eSMatt Jacob static int
isp_pci_attach(device_t dev)4699cd7268eSMatt Jacob isp_pci_attach(device_t dev)
4709cd7268eSMatt Jacob {
471a1fa0267SAlexander Motin 	struct isp_pcisoftc *pcs = device_get_softc(dev);
472a1fa0267SAlexander Motin 	ispsoftc_t *isp = &pcs->pci_isp;
473a1fa0267SAlexander Motin 	int i;
47410ed63fcSJoerg Pulz 	uint32_t data, cmd, linesz;
4752df76c16SMatt Jacob 	size_t psize, xsize;
4769cd7268eSMatt Jacob 
4772df76c16SMatt Jacob 	isp->isp_dev = dev;
4782df76c16SMatt Jacob 	isp->isp_nchan = 1;
47913d9c921SAlexander Motin 	mtx_init(&isp->isp_lock, "isp", NULL, MTX_DEF);
4809cd7268eSMatt Jacob 
4819cd7268eSMatt Jacob 	/*
482336b5612SMatt Jacob 	 * Get Generic Options
4839cd7268eSMatt Jacob 	 */
4849e7d423dSMatt Jacob 	isp_nvports = 0;
4859e7d423dSMatt Jacob 	isp_get_generic_options(dev, isp);
4869cd7268eSMatt Jacob 
487ab6d0040SMatt Jacob 	linesz = PCI_DFLT_LNSZ;
48808826086SAlexander Motin 	pcs->regs = pcs->regs2 = NULL;
48908826086SAlexander Motin 	pcs->rgd = pcs->rtp = 0;
490960f6939SMatt Jacob 
4911b760be4SAlexander Motin 	isp->isp_nchan += isp_nvports;
4922df76c16SMatt Jacob 	switch (pci_get_devid(dev)) {
4932df76c16SMatt Jacob 	case PCI_QLOGIC_ISP2422:
4942df76c16SMatt Jacob 	case PCI_QLOGIC_ISP2432:
49510ed63fcSJoerg Pulz 		isp->isp_did = 0x2400;
4962df76c16SMatt Jacob 		isp->isp_mdvec = &mdvec_2400;
4972df76c16SMatt Jacob 		isp->isp_type = ISP_HA_FC_2400;
4982df76c16SMatt Jacob 		break;
4992df76c16SMatt Jacob 	case PCI_QLOGIC_ISP2532:
50010ed63fcSJoerg Pulz 		isp->isp_did = 0x2500;
5012df76c16SMatt Jacob 		isp->isp_mdvec = &mdvec_2500;
5022df76c16SMatt Jacob 		isp->isp_type = ISP_HA_FC_2500;
5032df76c16SMatt Jacob 		break;
504a959d921SMatt Jacob 	case PCI_QLOGIC_ISP5432:
50510ed63fcSJoerg Pulz 		isp->isp_did = 0x2500;
506a959d921SMatt Jacob 		isp->isp_mdvec = &mdvec_2500;
507a959d921SMatt Jacob 		isp->isp_type = ISP_HA_FC_2500;
508a959d921SMatt Jacob 		break;
509218be0b2SAlexander Motin 	case PCI_QLOGIC_ISP2031:
51067eeadd2SAlexander Motin 	case PCI_QLOGIC_ISP8031:
51110ed63fcSJoerg Pulz 		isp->isp_did = 0x2600;
512218be0b2SAlexander Motin 		isp->isp_mdvec = &mdvec_2600;
513218be0b2SAlexander Motin 		isp->isp_type = ISP_HA_FC_2600;
514218be0b2SAlexander Motin 		break;
51514e084adSAlexander Motin 	case PCI_QLOGIC_ISP2684:
51614e084adSAlexander Motin 	case PCI_QLOGIC_ISP2692:
51714e084adSAlexander Motin 	case PCI_QLOGIC_ISP2714:
51814e084adSAlexander Motin 	case PCI_QLOGIC_ISP2722:
51910ed63fcSJoerg Pulz 		isp->isp_did = 0x2700;
52014e084adSAlexander Motin 		isp->isp_mdvec = &mdvec_2700;
52114e084adSAlexander Motin 		isp->isp_type = ISP_HA_FC_2700;
52214e084adSAlexander Motin 		break;
523407abff6SJoerg Pulz 	case PCI_QLOGIC_ISP2812:
524407abff6SJoerg Pulz 	case PCI_QLOGIC_ISP2814:
52510ed63fcSJoerg Pulz 		isp->isp_did = 0x2800;
526407abff6SJoerg Pulz 		isp->isp_mdvec = &mdvec_2800;
527407abff6SJoerg Pulz 		isp->isp_type = ISP_HA_FC_2800;
528407abff6SJoerg Pulz 		break;
5292df76c16SMatt Jacob 	default:
5302df76c16SMatt Jacob 		device_printf(dev, "unknown device type\n");
5312df76c16SMatt Jacob 		goto bad;
5322df76c16SMatt Jacob 		break;
53365adb54cSMatt Jacob 	}
5342df76c16SMatt Jacob 	isp->isp_revision = pci_get_revid(dev);
5352df76c16SMatt Jacob 
536218be0b2SAlexander Motin 	if (IS_26XX(isp)) {
537218be0b2SAlexander Motin 		pcs->rtp = SYS_RES_MEMORY;
538218be0b2SAlexander Motin 		pcs->rgd = PCIR_BAR(0);
539218be0b2SAlexander Motin 		pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd,
540218be0b2SAlexander Motin 		    RF_ACTIVE);
541d876d6c3SAlexander Motin 		pcs->rtp1 = SYS_RES_MEMORY;
542d876d6c3SAlexander Motin 		pcs->rgd1 = PCIR_BAR(2);
543d876d6c3SAlexander Motin 		pcs->regs1 = bus_alloc_resource_any(dev, pcs->rtp1, &pcs->rgd1,
544d876d6c3SAlexander Motin 		    RF_ACTIVE);
545cd201b7bSAlexander Motin 		pcs->rtp2 = SYS_RES_MEMORY;
546cd201b7bSAlexander Motin 		pcs->rgd2 = PCIR_BAR(4);
547cd201b7bSAlexander Motin 		pcs->regs2 = bus_alloc_resource_any(dev, pcs->rtp2, &pcs->rgd2,
548cd201b7bSAlexander Motin 		    RF_ACTIVE);
549218be0b2SAlexander Motin 	} else {
550218be0b2SAlexander Motin 		pcs->rtp = SYS_RES_MEMORY;
551218be0b2SAlexander Motin 		pcs->rgd = PCIR_BAR(1);
552218be0b2SAlexander Motin 		pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd,
553218be0b2SAlexander Motin 		    RF_ACTIVE);
554218be0b2SAlexander Motin 		if (pcs->regs == NULL) {
555218be0b2SAlexander Motin 			pcs->rtp = SYS_RES_IOPORT;
556218be0b2SAlexander Motin 			pcs->rgd = PCIR_BAR(0);
557218be0b2SAlexander Motin 			pcs->regs = bus_alloc_resource_any(dev, pcs->rtp,
558218be0b2SAlexander Motin 			    &pcs->rgd, RF_ACTIVE);
559218be0b2SAlexander Motin 		}
560218be0b2SAlexander Motin 	}
561218be0b2SAlexander Motin 	if (pcs->regs == NULL) {
562218be0b2SAlexander Motin 		device_printf(dev, "Unable to map any ports\n");
563218be0b2SAlexander Motin 		goto bad;
564218be0b2SAlexander Motin 	}
565218be0b2SAlexander Motin 	if (bootverbose) {
566218be0b2SAlexander Motin 		device_printf(dev, "Using %s space register mapping\n",
567218be0b2SAlexander Motin 		    (pcs->rtp == SYS_RES_IOPORT)? "I/O" : "Memory");
568218be0b2SAlexander Motin 	}
569cd201b7bSAlexander Motin 	isp->isp_regs = pcs->regs;
570cd201b7bSAlexander Motin 	isp->isp_regs2 = pcs->regs2;
571218be0b2SAlexander Motin 
5721b760be4SAlexander Motin 	psize = sizeof(fcparam) * isp->isp_nchan;
5731b760be4SAlexander Motin 	xsize = sizeof(struct isp_fc) * isp->isp_nchan;
5747cc0979fSDavid Malone 	isp->isp_param = malloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
575c6608df3SMatt Jacob 	if (isp->isp_param == NULL) {
576960f6939SMatt Jacob 		device_printf(dev, "cannot allocate parameter data\n");
577960f6939SMatt Jacob 		goto bad;
578c6608df3SMatt Jacob 	}
579a6222dd7SAlexander Motin 	isp->isp_osinfo.fc = malloc(xsize, M_DEVBUF, M_NOWAIT | M_ZERO);
580a6222dd7SAlexander Motin 	if (isp->isp_osinfo.fc == NULL) {
5812df76c16SMatt Jacob 		device_printf(dev, "cannot allocate parameter data\n");
5822df76c16SMatt Jacob 		goto bad;
5832df76c16SMatt Jacob 	}
58465adb54cSMatt Jacob 
585336b5612SMatt Jacob 	/*
586336b5612SMatt Jacob 	 * Now that we know who we are (roughly) get/set specific options
587336b5612SMatt Jacob 	 */
5882df76c16SMatt Jacob 	for (i = 0; i < isp->isp_nchan; i++) {
5892df76c16SMatt Jacob 		isp_get_specific_options(dev, i, isp);
5909a5af410SMatt Jacob 	}
5919a5af410SMatt Jacob 
59256aef503SMatt Jacob 	/*
593ad0ab753SMatt Jacob 	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set.
594d951bbcaSMatt Jacob 	 */
595c68534f1SScott Long 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
5969e7d423dSMatt Jacob 	cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
5978a97c03aSMatt Jacob 	cmd &= ~PCIM_CMD_INTX_DISABLE;
598b49c4674SMatt Jacob 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
599ab6d0040SMatt Jacob 
600d951bbcaSMatt Jacob 	/*
601222bb542SMatt Jacob 	 * Make sure the Cache Line Size register is set sensibly.
602d951bbcaSMatt Jacob 	 */
603960f6939SMatt Jacob 	data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
6046a7d12e1SMatt Jacob 	if (data == 0 || (linesz != PCI_DFLT_LNSZ && data != linesz)) {
605ad0ab753SMatt Jacob 		isp_prt(isp, ISP_LOGDEBUG0, "set PCI line size to %d from %d", linesz, data);
6066a7d12e1SMatt Jacob 		data = linesz;
607960f6939SMatt Jacob 		pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
608d951bbcaSMatt Jacob 	}
609ab6d0040SMatt Jacob 
610d951bbcaSMatt Jacob 	/*
611d951bbcaSMatt Jacob 	 * Make sure the Latency Timer is sane.
612d951bbcaSMatt Jacob 	 */
613960f6939SMatt Jacob 	data = pci_read_config(dev, PCIR_LATTIMER, 1);
614d951bbcaSMatt Jacob 	if (data < PCI_DFLT_LTNCY) {
615d951bbcaSMatt Jacob 		data = PCI_DFLT_LTNCY;
616ad0ab753SMatt Jacob 		isp_prt(isp, ISP_LOGDEBUG0, "set PCI latency to %d", data);
617960f6939SMatt Jacob 		pci_write_config(dev, PCIR_LATTIMER, data, 1);
618d951bbcaSMatt Jacob 	}
619ab6d0040SMatt Jacob 
620ab6d0040SMatt Jacob 	/*
621ab6d0040SMatt Jacob 	 * Make sure we've disabled the ROM.
622ab6d0040SMatt Jacob 	 */
623960f6939SMatt Jacob 	data = pci_read_config(dev, PCIR_ROMADDR, 4);
624ab6d0040SMatt Jacob 	data &= ~1;
625960f6939SMatt Jacob 	pci_write_config(dev, PCIR_ROMADDR, data, 4);
6262df76c16SMatt Jacob 
62705fbcbb0SMatt Jacob 	/*
62875c1e828SMatt Jacob 	 * Last minute checks...
62975c1e828SMatt Jacob 	 */
63075c1e828SMatt Jacob 	isp->isp_port = pci_get_function(dev);
63175c1e828SMatt Jacob 
63275c1e828SMatt Jacob 	/*
63305fbcbb0SMatt Jacob 	 * Make sure we're in reset state.
63405fbcbb0SMatt Jacob 	 */
6353395b056SMatt Jacob 	ISP_LOCK(isp);
636dfd24649SAlexander Motin 	if (isp_reinit(isp, 1) != 0) {
6373395b056SMatt Jacob 		ISP_UNLOCK(isp);
638960f6939SMatt Jacob 		goto bad;
63965adb54cSMatt Jacob 	}
6402df76c16SMatt Jacob 	ISP_UNLOCK(isp);
6412df76c16SMatt Jacob 	if (isp_attach(isp)) {
6422df76c16SMatt Jacob 		ISP_LOCK(isp);
643a1fa0267SAlexander Motin 		isp_shutdown(isp);
6443395b056SMatt Jacob 		ISP_UNLOCK(isp);
645960f6939SMatt Jacob 		goto bad;
646d59bd469SMatt Jacob 	}
647960f6939SMatt Jacob 	return (0);
648960f6939SMatt Jacob 
649960f6939SMatt Jacob bad:
65008826086SAlexander Motin 	for (i = 0; i < isp->isp_nirq; i++) {
65108826086SAlexander Motin 		(void) bus_teardown_intr(dev, pcs->irq[i].irq, pcs->irq[i].ih);
65208826086SAlexander Motin 		(void) bus_release_resource(dev, SYS_RES_IRQ, pcs->irq[i].iqd,
65308826086SAlexander Motin 		    pcs->irq[0].irq);
654960f6939SMatt Jacob 	}
655a035b0afSMatt Jacob 	if (pcs->msicount) {
6560a70657fSMatt Jacob 		pci_release_msi(dev);
6570a70657fSMatt Jacob 	}
658cd201b7bSAlexander Motin 	if (pcs->regs)
659e95725cbSMatt Jacob 		(void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
660d876d6c3SAlexander Motin 	if (pcs->regs1)
661d876d6c3SAlexander Motin 		(void) bus_release_resource(dev, pcs->rtp1, pcs->rgd1, pcs->regs1);
662cd201b7bSAlexander Motin 	if (pcs->regs2)
663cd201b7bSAlexander Motin 		(void) bus_release_resource(dev, pcs->rtp2, pcs->rgd2, pcs->regs2);
6649cd7268eSMatt Jacob 	if (pcs->pci_isp.isp_param) {
665960f6939SMatt Jacob 		free(pcs->pci_isp.isp_param, M_DEVBUF);
6662df76c16SMatt Jacob 		pcs->pci_isp.isp_param = NULL;
6672df76c16SMatt Jacob 	}
668a6222dd7SAlexander Motin 	if (pcs->pci_isp.isp_osinfo.fc) {
669a6222dd7SAlexander Motin 		free(pcs->pci_isp.isp_osinfo.fc, M_DEVBUF);
670a6222dd7SAlexander Motin 		pcs->pci_isp.isp_osinfo.fc = NULL;
6719cd7268eSMatt Jacob 	}
67213d9c921SAlexander Motin 	mtx_destroy(&isp->isp_lock);
673960f6939SMatt Jacob 	return (ENXIO);
67465adb54cSMatt Jacob }
67565adb54cSMatt Jacob 
67610365e5aSMatt Jacob static int
isp_pci_detach(device_t dev)67710365e5aSMatt Jacob isp_pci_detach(device_t dev)
67810365e5aSMatt Jacob {
679a1fa0267SAlexander Motin 	struct isp_pcisoftc *pcs = device_get_softc(dev);
680a1fa0267SAlexander Motin 	ispsoftc_t *isp = &pcs->pci_isp;
68108826086SAlexander Motin 	int i, status;
68210365e5aSMatt Jacob 
683e95725cbSMatt Jacob 	status = isp_detach(isp);
684e95725cbSMatt Jacob 	if (status)
685e95725cbSMatt Jacob 		return (status);
686e95725cbSMatt Jacob 	ISP_LOCK(isp);
687a1fa0267SAlexander Motin 	isp_shutdown(isp);
688e95725cbSMatt Jacob 	ISP_UNLOCK(isp);
68908826086SAlexander Motin 	for (i = 0; i < isp->isp_nirq; i++) {
69008826086SAlexander Motin 		(void) bus_teardown_intr(dev, pcs->irq[i].irq, pcs->irq[i].ih);
69108826086SAlexander Motin 		(void) bus_release_resource(dev, SYS_RES_IRQ, pcs->irq[i].iqd,
69208826086SAlexander Motin 		    pcs->irq[i].irq);
69308826086SAlexander Motin 	}
694a1fa0267SAlexander Motin 	if (pcs->msicount)
695e95725cbSMatt Jacob 		pci_release_msi(dev);
696e95725cbSMatt Jacob 	(void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
697d876d6c3SAlexander Motin 	if (pcs->regs1)
698d876d6c3SAlexander Motin 		(void) bus_release_resource(dev, pcs->rtp1, pcs->rgd1, pcs->regs1);
699cd201b7bSAlexander Motin 	if (pcs->regs2)
700cd201b7bSAlexander Motin 		(void) bus_release_resource(dev, pcs->rtp2, pcs->rgd2, pcs->regs2);
701a1fa0267SAlexander Motin 	isp_pci_mbxdmafree(isp);
702e95725cbSMatt Jacob 	if (pcs->pci_isp.isp_param) {
703e95725cbSMatt Jacob 		free(pcs->pci_isp.isp_param, M_DEVBUF);
704e95725cbSMatt Jacob 		pcs->pci_isp.isp_param = NULL;
705e95725cbSMatt Jacob 	}
706a6222dd7SAlexander Motin 	if (pcs->pci_isp.isp_osinfo.fc) {
707a6222dd7SAlexander Motin 		free(pcs->pci_isp.isp_osinfo.fc, M_DEVBUF);
708a6222dd7SAlexander Motin 		pcs->pci_isp.isp_osinfo.fc = NULL;
709e95725cbSMatt Jacob 	}
71013d9c921SAlexander Motin 	mtx_destroy(&isp->isp_lock);
71110365e5aSMatt Jacob 	return (0);
71210365e5aSMatt Jacob }
71310365e5aSMatt Jacob 
714cd201b7bSAlexander Motin #define	BXR2(isp, off)		bus_read_2((isp)->isp_regs, (off))
715cd201b7bSAlexander Motin #define	BXW2(isp, off, v)	bus_write_2((isp)->isp_regs, (off), (v))
716cd201b7bSAlexander Motin #define	BXR4(isp, off)		bus_read_4((isp)->isp_regs, (off))
717cd201b7bSAlexander Motin #define	BXW4(isp, off, v)	bus_write_4((isp)->isp_regs, (off), (v))
718cd201b7bSAlexander Motin #define	B2R4(isp, off)		bus_read_4((isp)->isp_regs2, (off))
719cd201b7bSAlexander Motin #define	B2W4(isp, off, v)	bus_write_4((isp)->isp_regs2, (off), (v))
720126ec864SMatt Jacob 
7210e6bc811SAlexander Motin static void
isp_pci_run_isr_2400(ispsoftc_t * isp)7220e6bc811SAlexander Motin isp_pci_run_isr_2400(ispsoftc_t *isp)
72310365e5aSMatt Jacob {
72410365e5aSMatt Jacob 	uint32_t r2hisr;
7250e6bc811SAlexander Motin 	uint16_t isr, info;
72610365e5aSMatt Jacob 
7271b760be4SAlexander Motin 	r2hisr = BXR4(isp, BIU2400_R2HSTS);
72810365e5aSMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
7290e6bc811SAlexander Motin 	if ((r2hisr & BIU_R2HST_INTR) == 0)
7300e6bc811SAlexander Motin 		return;
7310e6bc811SAlexander Motin 	isr = r2hisr & BIU_R2HST_ISTAT_MASK;
7320e6bc811SAlexander Motin 	info = (r2hisr >> 16);
7330e6bc811SAlexander Motin 	switch (isr) {
7346ce548a1SAlexander Motin 	case ISPR2HST_ROM_MBX_OK:
7356ce548a1SAlexander Motin 	case ISPR2HST_ROM_MBX_FAIL:
7366ce548a1SAlexander Motin 	case ISPR2HST_MBX_OK:
7376ce548a1SAlexander Motin 	case ISPR2HST_MBX_FAIL:
7380e6bc811SAlexander Motin 		isp_intr_mbox(isp, info);
7390e6bc811SAlexander Motin 		break;
7406ce548a1SAlexander Motin 	case ISPR2HST_ASYNC_EVENT:
7410e6bc811SAlexander Motin 		isp_intr_async(isp, info);
7426ce548a1SAlexander Motin 		break;
7436ce548a1SAlexander Motin 	case ISPR2HST_RSPQ_UPDATE:
7440e6bc811SAlexander Motin 		isp_intr_respq(isp);
7450e6bc811SAlexander Motin 		break;
7466ce548a1SAlexander Motin 	case ISPR2HST_RSPQ_UPDATE2:
7470e6bc811SAlexander Motin #ifdef	ISP_TARGET_MODE
7486ce548a1SAlexander Motin 	case ISPR2HST_ATIO_RSPQ_UPDATE:
7490e6bc811SAlexander Motin #endif
7500e6bc811SAlexander Motin 		isp_intr_respq(isp);
7510e6bc811SAlexander Motin 		/* FALLTHROUGH */
7520e6bc811SAlexander Motin #ifdef	ISP_TARGET_MODE
7530e6bc811SAlexander Motin 	case ISPR2HST_ATIO_UPDATE:
7546ce548a1SAlexander Motin 	case ISPR2HST_ATIO_UPDATE2:
7550e6bc811SAlexander Motin 		isp_intr_atioq(isp);
7560e6bc811SAlexander Motin #endif
7576ce548a1SAlexander Motin 		break;
75810365e5aSMatt Jacob 	default:
75910365e5aSMatt Jacob 		isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
76010365e5aSMatt Jacob 	}
7610e6bc811SAlexander Motin 	ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
76210365e5aSMatt Jacob }
76310365e5aSMatt Jacob 
76410365e5aSMatt Jacob static uint32_t
isp_pci_rd_reg_2400(ispsoftc_t * isp,int regoff)76510365e5aSMatt Jacob isp_pci_rd_reg_2400(ispsoftc_t *isp, int regoff)
76610365e5aSMatt Jacob {
76710365e5aSMatt Jacob 	int block = regoff & _BLK_REG_MASK;
76810365e5aSMatt Jacob 
76910365e5aSMatt Jacob 	switch (block) {
77010365e5aSMatt Jacob 	case BIU_BLOCK:
7711b760be4SAlexander Motin 		return (BXR4(isp, regoff));
77210365e5aSMatt Jacob 	case MBOX_BLOCK:
7731b760be4SAlexander Motin 		return (BXR2(isp, regoff));
7741b760be4SAlexander Motin 	}
775cd201b7bSAlexander Motin 	isp_prt(isp, ISP_LOGERR, "unknown block read at 0x%x", regoff);
77610365e5aSMatt Jacob 	return (0xffffffff);
77710365e5aSMatt Jacob }
77810365e5aSMatt Jacob 
77910365e5aSMatt Jacob static void
isp_pci_wr_reg_2400(ispsoftc_t * isp,int regoff,uint32_t val)78010365e5aSMatt Jacob isp_pci_wr_reg_2400(ispsoftc_t *isp, int regoff, uint32_t val)
78110365e5aSMatt Jacob {
78210365e5aSMatt Jacob 	int block = regoff & _BLK_REG_MASK;
78310365e5aSMatt Jacob 
78410365e5aSMatt Jacob 	switch (block) {
78510365e5aSMatt Jacob 	case BIU_BLOCK:
7861b760be4SAlexander Motin 		BXW4(isp, regoff, val);
78707d925faSAlexander Motin #ifdef MEMORYBARRIERW
78807d925faSAlexander Motin 		if (regoff == BIU2400_REQINP ||
78907d925faSAlexander Motin 		    regoff == BIU2400_RSPOUTP ||
79007d925faSAlexander Motin 		    regoff == BIU2400_PRI_REQINP ||
79107d925faSAlexander Motin 		    regoff == BIU2400_ATIO_RSPOUTP)
7921b760be4SAlexander Motin 			MEMORYBARRIERW(isp, SYNC_REG, regoff, 4, -1)
79307d925faSAlexander Motin 		else
79407d925faSAlexander Motin #endif
7951b760be4SAlexander Motin 		MEMORYBARRIER(isp, SYNC_REG, regoff, 4, -1);
7961b760be4SAlexander Motin 		return;
7971b760be4SAlexander Motin 	case MBOX_BLOCK:
7981b760be4SAlexander Motin 		BXW2(isp, regoff, val);
7991b760be4SAlexander Motin 		MEMORYBARRIER(isp, SYNC_REG, regoff, 2, -1);
8001b760be4SAlexander Motin 		return;
801d59bd469SMatt Jacob 	}
8021b760be4SAlexander Motin 	isp_prt(isp, ISP_LOGERR, "unknown block write at 0x%x", regoff);
803d59bd469SMatt Jacob }
804d59bd469SMatt Jacob 
805cd201b7bSAlexander Motin static uint32_t
isp_pci_rd_reg_2600(ispsoftc_t * isp,int regoff)806cd201b7bSAlexander Motin isp_pci_rd_reg_2600(ispsoftc_t *isp, int regoff)
807cd201b7bSAlexander Motin {
808cd201b7bSAlexander Motin 	uint32_t rv;
809cd201b7bSAlexander Motin 
810cd201b7bSAlexander Motin 	switch (regoff) {
811cd201b7bSAlexander Motin 	case BIU2400_PRI_REQINP:
812cd201b7bSAlexander Motin 	case BIU2400_PRI_REQOUTP:
813cd201b7bSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "unknown register read at 0x%x",
814cd201b7bSAlexander Motin 		    regoff);
815cd201b7bSAlexander Motin 		rv = 0xffffffff;
816cd201b7bSAlexander Motin 		break;
817cd201b7bSAlexander Motin 	case BIU2400_REQINP:
818cd201b7bSAlexander Motin 		rv = B2R4(isp, 0x00);
819cd201b7bSAlexander Motin 		break;
820cd201b7bSAlexander Motin 	case BIU2400_REQOUTP:
821cd201b7bSAlexander Motin 		rv = B2R4(isp, 0x04);
822cd201b7bSAlexander Motin 		break;
823cd201b7bSAlexander Motin 	case BIU2400_RSPINP:
824cd201b7bSAlexander Motin 		rv = B2R4(isp, 0x08);
825cd201b7bSAlexander Motin 		break;
826cd201b7bSAlexander Motin 	case BIU2400_RSPOUTP:
827cd201b7bSAlexander Motin 		rv = B2R4(isp, 0x0c);
828cd201b7bSAlexander Motin 		break;
829cd201b7bSAlexander Motin 	case BIU2400_ATIO_RSPINP:
830cd201b7bSAlexander Motin 		rv = B2R4(isp, 0x10);
831cd201b7bSAlexander Motin 		break;
832cd201b7bSAlexander Motin 	case BIU2400_ATIO_RSPOUTP:
833cd201b7bSAlexander Motin 		rv = B2R4(isp, 0x14);
834cd201b7bSAlexander Motin 		break;
835cd201b7bSAlexander Motin 	default:
836cd201b7bSAlexander Motin 		rv = isp_pci_rd_reg_2400(isp, regoff);
837cd201b7bSAlexander Motin 		break;
838cd201b7bSAlexander Motin 	}
839cd201b7bSAlexander Motin 	return (rv);
840cd201b7bSAlexander Motin }
841cd201b7bSAlexander Motin 
842cd201b7bSAlexander Motin static void
isp_pci_wr_reg_2600(ispsoftc_t * isp,int regoff,uint32_t val)843cd201b7bSAlexander Motin isp_pci_wr_reg_2600(ispsoftc_t *isp, int regoff, uint32_t val)
844cd201b7bSAlexander Motin {
845cd201b7bSAlexander Motin 	int off;
846cd201b7bSAlexander Motin 
847cd201b7bSAlexander Motin 	switch (regoff) {
848cd201b7bSAlexander Motin 	case BIU2400_PRI_REQINP:
849cd201b7bSAlexander Motin 	case BIU2400_PRI_REQOUTP:
850cd201b7bSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "unknown register write at 0x%x",
851cd201b7bSAlexander Motin 		    regoff);
852cd201b7bSAlexander Motin 		return;
853cd201b7bSAlexander Motin 	case BIU2400_REQINP:
854cd201b7bSAlexander Motin 		off = 0x00;
855cd201b7bSAlexander Motin 		break;
856cd201b7bSAlexander Motin 	case BIU2400_REQOUTP:
857cd201b7bSAlexander Motin 		off = 0x04;
858cd201b7bSAlexander Motin 		break;
859cd201b7bSAlexander Motin 	case BIU2400_RSPINP:
860cd201b7bSAlexander Motin 		off = 0x08;
861cd201b7bSAlexander Motin 		break;
862cd201b7bSAlexander Motin 	case BIU2400_RSPOUTP:
863cd201b7bSAlexander Motin 		off = 0x0c;
864cd201b7bSAlexander Motin 		break;
865cd201b7bSAlexander Motin 	case BIU2400_ATIO_RSPINP:
866cd201b7bSAlexander Motin 		off = 0x10;
867cd201b7bSAlexander Motin 		break;
868cd201b7bSAlexander Motin 	case BIU2400_ATIO_RSPOUTP:
869cd201b7bSAlexander Motin 		off = 0x14;
870cd201b7bSAlexander Motin 		break;
871cd201b7bSAlexander Motin 	default:
872cd201b7bSAlexander Motin 		isp_pci_wr_reg_2400(isp, regoff, val);
873cd201b7bSAlexander Motin 		return;
874cd201b7bSAlexander Motin 	}
875cd201b7bSAlexander Motin 	B2W4(isp, off, val);
876cd201b7bSAlexander Motin }
877cd201b7bSAlexander Motin 
878d720e6d5SJustin T. Gibbs 
879222bb542SMatt Jacob struct imush {
88014849e2cSAlexander Motin 	bus_addr_t maddr;
881222bb542SMatt Jacob 	int error;
882222bb542SMatt Jacob };
883222bb542SMatt Jacob 
884d720e6d5SJustin T. Gibbs static void
imc(void * arg,bus_dma_segment_t * segs,int nseg,int error)8851923f739SMatt Jacob imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
886d720e6d5SJustin T. Gibbs {
887222bb542SMatt Jacob 	struct imush *imushp = (struct imush *) arg;
8882df76c16SMatt Jacob 
88914849e2cSAlexander Motin 	if (!(imushp->error = error))
89014849e2cSAlexander Motin 		imushp->maddr = segs[0].ds_addr;
891d720e6d5SJustin T. Gibbs }
892d720e6d5SJustin T. Gibbs 
893d720e6d5SJustin T. Gibbs static int
isp_pci_mbxdma(ispsoftc_t * isp)8949cd7268eSMatt Jacob isp_pci_mbxdma(ispsoftc_t *isp)
895d720e6d5SJustin T. Gibbs {
896254c652bSAlexander Motin 	bus_dma_tag_t ptag;
897d720e6d5SJustin T. Gibbs 	caddr_t base;
8981b760be4SAlexander Motin 	uint32_t len;
899254c652bSAlexander Motin 	int i, error, cmap;
90053af7d22SMatt Jacob 	bus_size_t slim;	/* segment size */
901222bb542SMatt Jacob 	struct imush im;
902dae0ba75SAlexander Motin #ifdef	ISP_TARGET_MODE
9031b760be4SAlexander Motin 	isp_ecmd_t *ecmd;
904dae0ba75SAlexander Motin #endif
905222bb542SMatt Jacob 
906a1fa0267SAlexander Motin 	/* Already been here? If so, leave... */
907a1fa0267SAlexander Motin 	if (isp->isp_xflist != NULL)
908a95ae193SMatt Jacob 		return (0);
909a1fa0267SAlexander Motin 	if (isp->isp_rquest != NULL && isp->isp_maxcmds == 0)
910a1fa0267SAlexander Motin 		return (0);
9110a70657fSMatt Jacob 	ISP_UNLOCK(isp);
91210365e5aSMatt Jacob 
913254c652bSAlexander Motin 	ptag = bus_get_dma_tag(isp->isp_osinfo.dev);
914a1fa0267SAlexander Motin 	if (sizeof (bus_size_t) > 4)
91553af7d22SMatt Jacob 		slim = (bus_size_t) (1ULL << 32);
916a1fa0267SAlexander Motin 	else
9179b434edeSMatt Jacob 		slim = (bus_size_t) (1UL << 31);
918254c652bSAlexander Motin 
919254c652bSAlexander Motin 	if (isp->isp_rquest != NULL)
920254c652bSAlexander Motin 		goto gotmaxcmds;
9211923f739SMatt Jacob 
922d720e6d5SJustin T. Gibbs 	/*
923cf770ba3SAlexander Motin 	 * Allocate and map the request queue.
924d720e6d5SJustin T. Gibbs 	 */
925d02373f1SMatt Jacob 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
926254c652bSAlexander Motin 	if (bus_dma_tag_create(ptag, QENTRY_LEN, slim,
9271b760be4SAlexander Motin 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
9289cca0e7eSAlexander Motin 	    len, 1, len, 0, NULL, NULL, &isp->isp_osinfo.reqdmat)) {
92914849e2cSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "cannot create request DMA tag");
930a1fa0267SAlexander Motin 		goto bad;
93114849e2cSAlexander Motin 	}
93214849e2cSAlexander Motin 	if (bus_dmamem_alloc(isp->isp_osinfo.reqdmat, (void **)&base,
93314849e2cSAlexander Motin 	    BUS_DMA_COHERENT, &isp->isp_osinfo.reqmap) != 0) {
93414849e2cSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "cannot allocate request DMA memory");
93514849e2cSAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.reqdmat);
936a1fa0267SAlexander Motin 		goto bad;
93714849e2cSAlexander Motin 	}
93814849e2cSAlexander Motin 	isp->isp_rquest = base;
93914849e2cSAlexander Motin 	im.error = 0;
94014849e2cSAlexander Motin 	if (bus_dmamap_load(isp->isp_osinfo.reqdmat, isp->isp_osinfo.reqmap,
9419cca0e7eSAlexander Motin 	    base, len, imc, &im, BUS_DMA_NOWAIT) || im.error) {
94214849e2cSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "error loading request DMA map %d", im.error);
943a1fa0267SAlexander Motin 		goto bad;
94414849e2cSAlexander Motin 	}
94514849e2cSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG0, "request area @ 0x%jx/0x%jx",
94614849e2cSAlexander Motin 	    (uintmax_t)im.maddr, (uintmax_t)len);
94714849e2cSAlexander Motin 	isp->isp_rquest_dma = im.maddr;
948cf770ba3SAlexander Motin 
949cf770ba3SAlexander Motin #ifdef	ISP_TARGET_MODE
950cf770ba3SAlexander Motin 	/*
951cf770ba3SAlexander Motin 	 * Allocate region for external DMA addressable command/status structures.
952cf770ba3SAlexander Motin 	 */
953cf770ba3SAlexander Motin 	len = N_XCMDS * XCMD_SIZE;
954254c652bSAlexander Motin 	if (bus_dma_tag_create(ptag, XCMD_SIZE, slim,
9551b760be4SAlexander Motin 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
956cf770ba3SAlexander Motin 	    len, 1, len, 0, NULL, NULL, &isp->isp_osinfo.ecmd_dmat)) {
957cf770ba3SAlexander Motin 		isp_prt(isp, ISP_LOGERR, "cannot create ECMD DMA tag");
958cf770ba3SAlexander Motin 		goto bad;
959cf770ba3SAlexander Motin 	}
960cf770ba3SAlexander Motin 	if (bus_dmamem_alloc(isp->isp_osinfo.ecmd_dmat, (void **)&base,
961cf770ba3SAlexander Motin 	    BUS_DMA_COHERENT, &isp->isp_osinfo.ecmd_map) != 0) {
962cf770ba3SAlexander Motin 		isp_prt(isp, ISP_LOGERR, "cannot allocate ECMD DMA memory");
963254c652bSAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.ecmd_dmat);
964cf770ba3SAlexander Motin 		goto bad;
965cf770ba3SAlexander Motin 	}
966cf770ba3SAlexander Motin 	isp->isp_osinfo.ecmd_base = (isp_ecmd_t *)base;
967cf770ba3SAlexander Motin 	im.error = 0;
968cf770ba3SAlexander Motin 	if (bus_dmamap_load(isp->isp_osinfo.ecmd_dmat, isp->isp_osinfo.ecmd_map,
969cf770ba3SAlexander Motin 	    base, len, imc, &im, BUS_DMA_NOWAIT) || im.error) {
970cf770ba3SAlexander Motin 		isp_prt(isp, ISP_LOGERR, "error loading ECMD DMA map %d", im.error);
971cf770ba3SAlexander Motin 		goto bad;
972cf770ba3SAlexander Motin 	}
973cf770ba3SAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG0, "ecmd area @ 0x%jx/0x%jx",
974cf770ba3SAlexander Motin 	    (uintmax_t)im.maddr, (uintmax_t)len);
975cf770ba3SAlexander Motin 
97614849e2cSAlexander Motin 	isp->isp_osinfo.ecmd_dma = im.maddr;
97714849e2cSAlexander Motin 	isp->isp_osinfo.ecmd_free = (isp_ecmd_t *)base;
97814849e2cSAlexander Motin 	for (ecmd = isp->isp_osinfo.ecmd_free;
97914849e2cSAlexander Motin 	    ecmd < &isp->isp_osinfo.ecmd_free[N_XCMDS]; ecmd++) {
98014849e2cSAlexander Motin 		if (ecmd == &isp->isp_osinfo.ecmd_free[N_XCMDS - 1])
98114849e2cSAlexander Motin 			ecmd->next = NULL;
98214849e2cSAlexander Motin 		else
98314849e2cSAlexander Motin 			ecmd->next = ecmd + 1;
98414849e2cSAlexander Motin 	}
985cf770ba3SAlexander Motin #endif
9862df76c16SMatt Jacob 
98753af7d22SMatt Jacob 	/*
98814849e2cSAlexander Motin 	 * Allocate and map the result queue.
98953af7d22SMatt Jacob 	 */
99014849e2cSAlexander Motin 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
991254c652bSAlexander Motin 	if (bus_dma_tag_create(ptag, QENTRY_LEN, slim,
9921b760be4SAlexander Motin 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
9939cca0e7eSAlexander Motin 	    len, 1, len, 0, NULL, NULL, &isp->isp_osinfo.respdmat)) {
99414849e2cSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "cannot create response DMA tag");
995a1fa0267SAlexander Motin 		goto bad;
996d720e6d5SJustin T. Gibbs 	}
99714849e2cSAlexander Motin 	if (bus_dmamem_alloc(isp->isp_osinfo.respdmat, (void **)&base,
99814849e2cSAlexander Motin 	    BUS_DMA_COHERENT, &isp->isp_osinfo.respmap) != 0) {
99914849e2cSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "cannot allocate response DMA memory");
100014849e2cSAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.respdmat);
1001a1fa0267SAlexander Motin 		goto bad;
1002222bb542SMatt Jacob 	}
100314849e2cSAlexander Motin 	isp->isp_result = base;
10042df76c16SMatt Jacob 	im.error = 0;
100514849e2cSAlexander Motin 	if (bus_dmamap_load(isp->isp_osinfo.respdmat, isp->isp_osinfo.respmap,
10069cca0e7eSAlexander Motin 	    base, len, imc, &im, BUS_DMA_NOWAIT) || im.error) {
100714849e2cSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "error loading response DMA map %d", im.error);
1008a1fa0267SAlexander Motin 		goto bad;
100914849e2cSAlexander Motin 	}
101014849e2cSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG0, "response area @ 0x%jx/0x%jx",
101114849e2cSAlexander Motin 	    (uintmax_t)im.maddr, (uintmax_t)len);
101214849e2cSAlexander Motin 	isp->isp_result_dma = im.maddr;
10132df76c16SMatt Jacob 
101414849e2cSAlexander Motin #ifdef	ISP_TARGET_MODE
101514849e2cSAlexander Motin 	/*
1016b8e2395eSAlexander Motin 	 * Allocate and map ATIO queue.
101714849e2cSAlexander Motin 	 */
1018b8e2395eSAlexander Motin 	len = ISP_QUEUE_SIZE(ATIO_QUEUE_LEN(isp));
1019254c652bSAlexander Motin 	if (bus_dma_tag_create(ptag, QENTRY_LEN, slim,
10201b760be4SAlexander Motin 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
10219cca0e7eSAlexander Motin 	    len, 1, len, 0, NULL, NULL, &isp->isp_osinfo.atiodmat)) {
102214849e2cSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "cannot create ATIO DMA tag");
1023a1fa0267SAlexander Motin 		goto bad;
102414849e2cSAlexander Motin 	}
102514849e2cSAlexander Motin 	if (bus_dmamem_alloc(isp->isp_osinfo.atiodmat, (void **)&base,
102614849e2cSAlexander Motin 	    BUS_DMA_COHERENT, &isp->isp_osinfo.atiomap) != 0) {
102714849e2cSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "cannot allocate ATIO DMA memory");
102814849e2cSAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.atiodmat);
1029a1fa0267SAlexander Motin 		goto bad;
103014849e2cSAlexander Motin 	}
103114849e2cSAlexander Motin 	isp->isp_atioq = base;
103214849e2cSAlexander Motin 	im.error = 0;
103314849e2cSAlexander Motin 	if (bus_dmamap_load(isp->isp_osinfo.atiodmat, isp->isp_osinfo.atiomap,
10349cca0e7eSAlexander Motin 	    base, len, imc, &im, BUS_DMA_NOWAIT) || im.error) {
103514849e2cSAlexander Motin 		isp_prt(isp, ISP_LOGERR, "error loading ATIO DMA map %d", im.error);
10362df76c16SMatt Jacob 		goto bad;
10372df76c16SMatt Jacob 	}
103814849e2cSAlexander Motin 	isp_prt(isp, ISP_LOGDEBUG0, "ATIO area @ 0x%jx/0x%jx",
103914849e2cSAlexander Motin 	    (uintmax_t)im.maddr, (uintmax_t)len);
104014849e2cSAlexander Motin 	isp->isp_atioq_dma = im.maddr;
104114849e2cSAlexander Motin #endif
10422df76c16SMatt Jacob 
1043254c652bSAlexander Motin 	if (bus_dma_tag_create(ptag, 64, slim,
10447d191fdbSAlexander Motin 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
10459cca0e7eSAlexander Motin 	    2*QENTRY_LEN, 1, 2*QENTRY_LEN, 0, NULL, NULL,
10469cca0e7eSAlexander Motin 	    &isp->isp_osinfo.iocbdmat)) {
10472df76c16SMatt Jacob 		goto bad;
10482df76c16SMatt Jacob 	}
10494ff970c4SAlexander Motin 	if (bus_dmamem_alloc(isp->isp_osinfo.iocbdmat,
10504ff970c4SAlexander Motin 	    (void **)&base, BUS_DMA_COHERENT, &isp->isp_osinfo.iocbmap) != 0)
10514ff970c4SAlexander Motin 		goto bad;
10524ff970c4SAlexander Motin 	isp->isp_iocb = base;
10534ff970c4SAlexander Motin 	im.error = 0;
10544ff970c4SAlexander Motin 	if (bus_dmamap_load(isp->isp_osinfo.iocbdmat, isp->isp_osinfo.iocbmap,
10559cca0e7eSAlexander Motin 	    base, 2*QENTRY_LEN, imc, &im, BUS_DMA_NOWAIT) || im.error)
10564ff970c4SAlexander Motin 		goto bad;
10574ff970c4SAlexander Motin 	isp->isp_iocb_dma = im.maddr;
10584ff970c4SAlexander Motin 
1059254c652bSAlexander Motin 	if (bus_dma_tag_create(ptag, 64, slim,
10604ff970c4SAlexander Motin 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
10619cca0e7eSAlexander Motin 	    ISP_FC_SCRLEN, 1, ISP_FC_SCRLEN, 0, NULL, NULL,
10629cca0e7eSAlexander Motin 	    &isp->isp_osinfo.scdmat))
10634ff970c4SAlexander Motin 		goto bad;
10647d191fdbSAlexander Motin 	for (cmap = 0; cmap < isp->isp_nchan; cmap++) {
10657d191fdbSAlexander Motin 		struct isp_fc *fc = ISP_FC_PC(isp, cmap);
10667d191fdbSAlexander Motin 		if (bus_dmamem_alloc(isp->isp_osinfo.scdmat,
106714849e2cSAlexander Motin 		    (void **)&base, BUS_DMA_COHERENT, &fc->scmap) != 0)
10682df76c16SMatt Jacob 			goto bad;
106914849e2cSAlexander Motin 		FCPARAM(isp, cmap)->isp_scratch = base;
10702df76c16SMatt Jacob 		im.error = 0;
107114849e2cSAlexander Motin 		if (bus_dmamap_load(isp->isp_osinfo.scdmat, fc->scmap,
10729cca0e7eSAlexander Motin 		    base, ISP_FC_SCRLEN, imc, &im, BUS_DMA_NOWAIT) ||
10739cca0e7eSAlexander Motin 		    im.error) {
10747d191fdbSAlexander Motin 			bus_dmamem_free(isp->isp_osinfo.scdmat,
10757d191fdbSAlexander Motin 			    base, fc->scmap);
1076a1fa0267SAlexander Motin 			FCPARAM(isp, cmap)->isp_scratch = NULL;
10772df76c16SMatt Jacob 			goto bad;
10782df76c16SMatt Jacob 		}
107914849e2cSAlexander Motin 		FCPARAM(isp, cmap)->isp_scdma = im.maddr;
1080387d8239SMatt Jacob 		for (i = 0; i < INITIAL_NEXUS_COUNT; i++) {
1081387d8239SMatt Jacob 			struct isp_nexus *n = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_NOWAIT | M_ZERO);
1082387d8239SMatt Jacob 			if (n == NULL) {
1083387d8239SMatt Jacob 				while (fc->nexus_free_list) {
1084387d8239SMatt Jacob 					n = fc->nexus_free_list;
1085387d8239SMatt Jacob 					fc->nexus_free_list = n->next;
1086387d8239SMatt Jacob 					free(n, M_DEVBUF);
1087387d8239SMatt Jacob 				}
1088387d8239SMatt Jacob 				goto bad;
1089387d8239SMatt Jacob 			}
1090387d8239SMatt Jacob 			n->next = fc->nexus_free_list;
1091387d8239SMatt Jacob 			fc->nexus_free_list = n;
1092387d8239SMatt Jacob 		}
10932df76c16SMatt Jacob 	}
10942df76c16SMatt Jacob 
1095a1fa0267SAlexander Motin 	if (isp->isp_maxcmds == 0) {
1096a1fa0267SAlexander Motin 		ISP_LOCK(isp);
1097a1fa0267SAlexander Motin 		return (0);
1098a1fa0267SAlexander Motin 	}
1099a1fa0267SAlexander Motin 
1100a1fa0267SAlexander Motin gotmaxcmds:
1101254c652bSAlexander Motin 	if (bus_dma_tag_create(ptag, 1, slim,
1102254c652bSAlexander Motin 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1103254c652bSAlexander Motin 	    (ISP_NSEG64_MAX - 1) * PAGE_SIZE, ISP_NSEG64_MAX,
1104254c652bSAlexander Motin 	    (ISP_NSEG64_MAX - 1) * PAGE_SIZE, 0,
1105254c652bSAlexander Motin 	    busdma_lock_mutex, &isp->isp_lock, &isp->isp_osinfo.dmat))
1106254c652bSAlexander Motin 		goto bad;
1107a1fa0267SAlexander Motin 	len = isp->isp_maxcmds * sizeof (struct isp_pcmd);
1108a1fa0267SAlexander Motin 	isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *)
1109a1fa0267SAlexander Motin 	    malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1110a95ae193SMatt Jacob 	for (i = 0; i < isp->isp_maxcmds; i++) {
11110a70657fSMatt Jacob 		struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
11120a70657fSMatt Jacob 		error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
1113d720e6d5SJustin T. Gibbs 		if (error) {
11142df76c16SMatt Jacob 			isp_prt(isp, ISP_LOGERR, "error %d creating per-cmd DMA maps", error);
11151923f739SMatt Jacob 			while (--i >= 0) {
1116a1fa0267SAlexander Motin 				bus_dmamap_destroy(isp->isp_osinfo.dmat,
1117a1fa0267SAlexander Motin 				    isp->isp_osinfo.pcmd_pool[i].dmap);
11181923f739SMatt Jacob 			}
11191923f739SMatt Jacob 			goto bad;
1120d720e6d5SJustin T. Gibbs 		}
112113d9c921SAlexander Motin 		callout_init_mtx(&pcmd->wdog, &isp->isp_lock, 0);
1122a1fa0267SAlexander Motin 		if (i == isp->isp_maxcmds-1)
11230a70657fSMatt Jacob 			pcmd->next = NULL;
1124a1fa0267SAlexander Motin 		else
11250a70657fSMatt Jacob 			pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
1126d720e6d5SJustin T. Gibbs 	}
11270a70657fSMatt Jacob 	isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
1128a1fa0267SAlexander Motin 
11290b19f90aSAlexander Motin 	len = sizeof(isp_hdl_t) * ISP_HANDLE_NUM(isp);
1130a1fa0267SAlexander Motin 	isp->isp_xflist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
11310b19f90aSAlexander Motin 	for (len = 0; len < ISP_HANDLE_NUM(isp) - 1; len++)
1132a1fa0267SAlexander Motin 		isp->isp_xflist[len].cmd = &isp->isp_xflist[len+1];
1133a1fa0267SAlexander Motin 	isp->isp_xffree = isp->isp_xflist;
1134a1fa0267SAlexander Motin 
113572429e49SMatt Jacob 	ISP_LOCK(isp);
1136d720e6d5SJustin T. Gibbs 	return (0);
11371923f739SMatt Jacob 
11381923f739SMatt Jacob bad:
1139a1fa0267SAlexander Motin 	isp_pci_mbxdmafree(isp);
1140a1fa0267SAlexander Motin 	ISP_LOCK(isp);
1141a1fa0267SAlexander Motin 	return (1);
1142a1fa0267SAlexander Motin }
1143a1fa0267SAlexander Motin 
1144a1fa0267SAlexander Motin static void
isp_pci_mbxdmafree(ispsoftc_t * isp)1145a1fa0267SAlexander Motin isp_pci_mbxdmafree(ispsoftc_t *isp)
1146a1fa0267SAlexander Motin {
1147a1fa0267SAlexander Motin 	int i;
1148a1fa0267SAlexander Motin 
1149a1fa0267SAlexander Motin 	if (isp->isp_xflist != NULL) {
1150a1fa0267SAlexander Motin 		free(isp->isp_xflist, M_DEVBUF);
1151a1fa0267SAlexander Motin 		isp->isp_xflist = NULL;
1152a1fa0267SAlexander Motin 	}
1153a1fa0267SAlexander Motin 	if (isp->isp_osinfo.pcmd_pool != NULL) {
1154a1fa0267SAlexander Motin 		for (i = 0; i < isp->isp_maxcmds; i++) {
1155a1fa0267SAlexander Motin 			bus_dmamap_destroy(isp->isp_osinfo.dmat,
1156a1fa0267SAlexander Motin 			    isp->isp_osinfo.pcmd_pool[i].dmap);
1157a1fa0267SAlexander Motin 		}
1158a1fa0267SAlexander Motin 		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1159a1fa0267SAlexander Motin 		isp->isp_osinfo.pcmd_pool = NULL;
1160a1fa0267SAlexander Motin 	}
1161254c652bSAlexander Motin 	if (isp->isp_osinfo.dmat) {
1162254c652bSAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.dmat);
1163254c652bSAlexander Motin 		isp->isp_osinfo.dmat = NULL;
1164254c652bSAlexander Motin 	}
1165a1fa0267SAlexander Motin 	for (i = 0; i < isp->isp_nchan; i++) {
1166a1fa0267SAlexander Motin 		struct isp_fc *fc = ISP_FC_PC(isp, i);
1167a1fa0267SAlexander Motin 		if (FCPARAM(isp, i)->isp_scdma != 0) {
1168a1fa0267SAlexander Motin 			bus_dmamap_unload(isp->isp_osinfo.scdmat,
1169a1fa0267SAlexander Motin 			    fc->scmap);
1170a1fa0267SAlexander Motin 			FCPARAM(isp, i)->isp_scdma = 0;
1171a1fa0267SAlexander Motin 		}
1172a1fa0267SAlexander Motin 		if (FCPARAM(isp, i)->isp_scratch != NULL) {
11734ff970c4SAlexander Motin 			bus_dmamem_free(isp->isp_osinfo.scdmat,
1174a1fa0267SAlexander Motin 			    FCPARAM(isp, i)->isp_scratch, fc->scmap);
1175a1fa0267SAlexander Motin 			FCPARAM(isp, i)->isp_scratch = NULL;
1176a1fa0267SAlexander Motin 		}
1177387d8239SMatt Jacob 		while (fc->nexus_free_list) {
1178387d8239SMatt Jacob 			struct isp_nexus *n = fc->nexus_free_list;
1179387d8239SMatt Jacob 			fc->nexus_free_list = n->next;
1180387d8239SMatt Jacob 			free(n, M_DEVBUF);
1181387d8239SMatt Jacob 		}
11822df76c16SMatt Jacob 	}
1183254c652bSAlexander Motin 	if (isp->isp_osinfo.scdmat) {
11847d191fdbSAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.scdmat);
1185254c652bSAlexander Motin 		isp->isp_osinfo.scdmat = NULL;
1186254c652bSAlexander Motin 	}
1187254c652bSAlexander Motin 	if (isp->isp_iocb_dma != 0) {
1188a1fa0267SAlexander Motin 		bus_dmamap_unload(isp->isp_osinfo.iocbdmat,
11894ff970c4SAlexander Motin 		    isp->isp_osinfo.iocbmap);
1190a1fa0267SAlexander Motin 		isp->isp_iocb_dma = 0;
1191a1fa0267SAlexander Motin 	}
1192a1fa0267SAlexander Motin 	if (isp->isp_iocb != NULL) {
1193a1fa0267SAlexander Motin 		bus_dmamem_free(isp->isp_osinfo.iocbdmat,
1194a1fa0267SAlexander Motin 		    isp->isp_iocb, isp->isp_osinfo.iocbmap);
11954ff970c4SAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.iocbdmat);
11967d191fdbSAlexander Motin 	}
119714849e2cSAlexander Motin #ifdef	ISP_TARGET_MODE
119814849e2cSAlexander Motin 	if (isp->isp_atioq_dma != 0) {
119914849e2cSAlexander Motin 		bus_dmamap_unload(isp->isp_osinfo.atiodmat,
120014849e2cSAlexander Motin 		    isp->isp_osinfo.atiomap);
1201a1fa0267SAlexander Motin 		isp->isp_atioq_dma = 0;
120214849e2cSAlexander Motin 	}
120314849e2cSAlexander Motin 	if (isp->isp_atioq != NULL) {
1204a1fa0267SAlexander Motin 		bus_dmamem_free(isp->isp_osinfo.atiodmat, isp->isp_atioq,
120514849e2cSAlexander Motin 		    isp->isp_osinfo.atiomap);
120614849e2cSAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.atiodmat);
1207a1fa0267SAlexander Motin 		isp->isp_atioq = NULL;
120814849e2cSAlexander Motin 	}
120914849e2cSAlexander Motin #endif
1210a1fa0267SAlexander Motin 	if (isp->isp_result_dma != 0) {
1211a1fa0267SAlexander Motin 		bus_dmamap_unload(isp->isp_osinfo.respdmat,
1212a1fa0267SAlexander Motin 		    isp->isp_osinfo.respmap);
1213a1fa0267SAlexander Motin 		isp->isp_result_dma = 0;
1214a1fa0267SAlexander Motin 	}
1215a1fa0267SAlexander Motin 	if (isp->isp_result != NULL) {
1216a1fa0267SAlexander Motin 		bus_dmamem_free(isp->isp_osinfo.respdmat, isp->isp_result,
1217a1fa0267SAlexander Motin 		    isp->isp_osinfo.respmap);
1218a1fa0267SAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.respdmat);
1219a1fa0267SAlexander Motin 		isp->isp_result = NULL;
1220a1fa0267SAlexander Motin 	}
1221cf770ba3SAlexander Motin #ifdef	ISP_TARGET_MODE
1222cf770ba3SAlexander Motin 	if (isp->isp_osinfo.ecmd_dma != 0) {
1223cf770ba3SAlexander Motin 		bus_dmamap_unload(isp->isp_osinfo.ecmd_dmat,
1224cf770ba3SAlexander Motin 		    isp->isp_osinfo.ecmd_map);
1225cf770ba3SAlexander Motin 		isp->isp_osinfo.ecmd_dma = 0;
1226cf770ba3SAlexander Motin 	}
1227cf770ba3SAlexander Motin 	if (isp->isp_osinfo.ecmd_base != NULL) {
1228cf770ba3SAlexander Motin 		bus_dmamem_free(isp->isp_osinfo.ecmd_dmat, isp->isp_osinfo.ecmd_base,
1229cf770ba3SAlexander Motin 		    isp->isp_osinfo.ecmd_map);
1230cf770ba3SAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.ecmd_dmat);
1231cf770ba3SAlexander Motin 		isp->isp_osinfo.ecmd_base = NULL;
1232cf770ba3SAlexander Motin 	}
1233cf770ba3SAlexander Motin #endif
1234a1fa0267SAlexander Motin 	if (isp->isp_rquest_dma != 0) {
1235a1fa0267SAlexander Motin 		bus_dmamap_unload(isp->isp_osinfo.reqdmat,
1236a1fa0267SAlexander Motin 		    isp->isp_osinfo.reqmap);
1237a1fa0267SAlexander Motin 		isp->isp_rquest_dma = 0;
1238a1fa0267SAlexander Motin 	}
1239a1fa0267SAlexander Motin 	if (isp->isp_rquest != NULL) {
1240a1fa0267SAlexander Motin 		bus_dmamem_free(isp->isp_osinfo.reqdmat, isp->isp_rquest,
1241a1fa0267SAlexander Motin 		    isp->isp_osinfo.reqmap);
1242a1fa0267SAlexander Motin 		bus_dma_tag_destroy(isp->isp_osinfo.reqdmat);
12431923f739SMatt Jacob 		isp->isp_rquest = NULL;
1244a1fa0267SAlexander Motin 	}
1245d720e6d5SJustin T. Gibbs }
1246d720e6d5SJustin T. Gibbs 
1247a1fa0267SAlexander Motin static int
isp_pci_irqsetup(ispsoftc_t * isp)1248a1fa0267SAlexander Motin isp_pci_irqsetup(ispsoftc_t *isp)
12493bda7a83SMatt Jacob {
125008826086SAlexander Motin 	device_t dev = isp->isp_osinfo.dev;
125108826086SAlexander Motin 	struct isp_pcisoftc *pcs = device_get_softc(dev);
125208826086SAlexander Motin 	driver_intr_t *f;
125308826086SAlexander Motin 	int i, max_irq;
12543bda7a83SMatt Jacob 
125508826086SAlexander Motin 	/* Allocate IRQs only once. */
125608826086SAlexander Motin 	if (isp->isp_nirq > 0)
1257a1fa0267SAlexander Motin 		return (0);
125808826086SAlexander Motin 
1259abdc2e31SAlexander Motin 	ISP_UNLOCK(isp);
126008826086SAlexander Motin 	if (ISP_CAP_MSIX(isp)) {
12611f8c4546SAlexander Motin 		max_irq = IS_26XX(isp) ? 3 : (IS_25XX(isp) ? 2 : 0);
126217ec7746SAlexander Motin 		resource_int_value(device_get_name(dev),
126317ec7746SAlexander Motin 		    device_get_unit(dev), "msix", &max_irq);
126417ec7746SAlexander Motin 		max_irq = imin(ISP_MAX_IRQS, max_irq);
126508826086SAlexander Motin 		pcs->msicount = imin(pci_msix_count(dev), max_irq);
126608826086SAlexander Motin 		if (pcs->msicount > 0 &&
126708826086SAlexander Motin 		    pci_alloc_msix(dev, &pcs->msicount) != 0)
126808826086SAlexander Motin 			pcs->msicount = 0;
126908826086SAlexander Motin 	}
127008826086SAlexander Motin 	if (pcs->msicount == 0) {
127117ec7746SAlexander Motin 		max_irq = 1;
127217ec7746SAlexander Motin 		resource_int_value(device_get_name(dev),
127317ec7746SAlexander Motin 		    device_get_unit(dev), "msi", &max_irq);
127417ec7746SAlexander Motin 		max_irq = imin(1, max_irq);
127517ec7746SAlexander Motin 		pcs->msicount = imin(pci_msi_count(dev), max_irq);
127608826086SAlexander Motin 		if (pcs->msicount > 0 &&
127708826086SAlexander Motin 		    pci_alloc_msi(dev, &pcs->msicount) != 0)
127808826086SAlexander Motin 			pcs->msicount = 0;
127908826086SAlexander Motin 	}
128008826086SAlexander Motin 	for (i = 0; i < MAX(1, pcs->msicount); i++) {
128108826086SAlexander Motin 		pcs->irq[i].iqd = i + (pcs->msicount > 0);
128208826086SAlexander Motin 		pcs->irq[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
128308826086SAlexander Motin 		    &pcs->irq[i].iqd, RF_ACTIVE | RF_SHAREABLE);
128408826086SAlexander Motin 		if (pcs->irq[i].irq == NULL) {
128508826086SAlexander Motin 			device_printf(dev, "could not allocate interrupt\n");
128608826086SAlexander Motin 			break;
128708826086SAlexander Motin 		}
128808826086SAlexander Motin 		if (i == 0)
128908826086SAlexander Motin 			f = isp_platform_intr;
129008826086SAlexander Motin 		else if (i == 1)
129108826086SAlexander Motin 			f = isp_platform_intr_resp;
129208826086SAlexander Motin 		else
129308826086SAlexander Motin 			f = isp_platform_intr_atio;
129408826086SAlexander Motin 		if (bus_setup_intr(dev, pcs->irq[i].irq, ISP_IFLAGS, NULL,
129508826086SAlexander Motin 		    f, isp, &pcs->irq[i].ih)) {
129608826086SAlexander Motin 			device_printf(dev, "could not setup interrupt\n");
129708826086SAlexander Motin 			(void) bus_release_resource(dev, SYS_RES_IRQ,
129808826086SAlexander Motin 			    pcs->irq[i].iqd, pcs->irq[i].irq);
129908826086SAlexander Motin 			break;
130008826086SAlexander Motin 		}
130108826086SAlexander Motin 		if (pcs->msicount > 1) {
130208826086SAlexander Motin 			bus_describe_intr(dev, pcs->irq[i].irq, pcs->irq[i].ih,
130308826086SAlexander Motin 			    "%d", i);
130408826086SAlexander Motin 		}
130508826086SAlexander Motin 		isp->isp_nirq = i + 1;
130608826086SAlexander Motin 	}
1307abdc2e31SAlexander Motin 	ISP_LOCK(isp);
130808826086SAlexander Motin 
130908826086SAlexander Motin 	return (isp->isp_nirq == 0);
131065adb54cSMatt Jacob }
1311