xref: /freebsd/sys/dev/isp/isp_pci.c (revision 73030e03ce43a5b08e7c70ececaa2a34e57471fb)
1c3aac50fSPeter Wemm /* $FreeBSD$ */
265adb54cSMatt Jacob /*
365adb54cSMatt Jacob  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
465adb54cSMatt Jacob  * FreeBSD Version.
565adb54cSMatt Jacob  *
65f5aafe1SMatt Jacob  * Copyright (c) 1997, 1998, 1999, 2000, 2001 by Matthew Jacob
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  */
29d720e6d5SJustin T. Gibbs 
30960f6939SMatt Jacob #include <sys/param.h>
31960f6939SMatt Jacob #include <sys/systm.h>
32960f6939SMatt Jacob #include <sys/kernel.h>
33960f6939SMatt Jacob #include <sys/module.h>
34960f6939SMatt Jacob #include <sys/bus.h>
3565adb54cSMatt Jacob 
3665adb54cSMatt Jacob #include <pci/pcireg.h>
3765adb54cSMatt Jacob #include <pci/pcivar.h>
3865adb54cSMatt Jacob 
39d720e6d5SJustin T. Gibbs #include <machine/bus_memio.h>
40d720e6d5SJustin T. Gibbs #include <machine/bus_pio.h>
41d720e6d5SJustin T. Gibbs #include <machine/bus.h>
42960f6939SMatt Jacob #include <machine/resource.h>
43960f6939SMatt Jacob #include <sys/rman.h>
44960f6939SMatt Jacob #include <sys/malloc.h>
45960f6939SMatt Jacob 
46960f6939SMatt Jacob #include <dev/isp/isp_freebsd.h>
47d59bd469SMatt Jacob 
48126ec864SMatt Jacob static u_int16_t isp_pci_rd_reg(struct ispsoftc *, int);
49126ec864SMatt Jacob static void isp_pci_wr_reg(struct ispsoftc *, int, u_int16_t);
50126ec864SMatt Jacob static u_int16_t isp_pci_rd_reg_1080(struct ispsoftc *, int);
51126ec864SMatt Jacob static void isp_pci_wr_reg_1080(struct ispsoftc *, int, u_int16_t);
52126ec864SMatt Jacob static int
53126ec864SMatt Jacob isp_pci_rd_isr(struct ispsoftc *, u_int16_t *, u_int16_t *, u_int16_t *);
54126ec864SMatt Jacob static int
55126ec864SMatt Jacob isp_pci_rd_isr_2300(struct ispsoftc *, u_int16_t *, u_int16_t *, u_int16_t *);
56126ec864SMatt Jacob static int isp_pci_mbxdma(struct ispsoftc *);
57126ec864SMatt Jacob static int
58126ec864SMatt Jacob isp_pci_dmasetup(struct ispsoftc *, XS_T *, ispreq_t *, u_int16_t *, u_int16_t);
59d720e6d5SJustin T. Gibbs static void
60126ec864SMatt Jacob isp_pci_dmateardown(struct ispsoftc *, XS_T *, u_int16_t);
6165adb54cSMatt Jacob 
62126ec864SMatt Jacob static void isp_pci_reset1(struct ispsoftc *);
63126ec864SMatt Jacob static void isp_pci_dumpregs(struct ispsoftc *, const char *);
6465adb54cSMatt Jacob 
6565adb54cSMatt Jacob static struct ispmdvec mdvec = {
66126ec864SMatt Jacob 	isp_pci_rd_isr,
6765adb54cSMatt Jacob 	isp_pci_rd_reg,
6865adb54cSMatt Jacob 	isp_pci_wr_reg,
6965adb54cSMatt Jacob 	isp_pci_mbxdma,
7065adb54cSMatt Jacob 	isp_pci_dmasetup,
71d720e6d5SJustin T. Gibbs 	isp_pci_dmateardown,
7265adb54cSMatt Jacob 	NULL,
7365adb54cSMatt Jacob 	isp_pci_reset1,
7465adb54cSMatt Jacob 	isp_pci_dumpregs,
7556aef503SMatt Jacob 	NULL,
76d02373f1SMatt Jacob 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
7765adb54cSMatt Jacob };
7865adb54cSMatt Jacob 
79d59bd469SMatt Jacob static struct ispmdvec mdvec_1080 = {
80126ec864SMatt Jacob 	isp_pci_rd_isr,
81d59bd469SMatt Jacob 	isp_pci_rd_reg_1080,
82d59bd469SMatt Jacob 	isp_pci_wr_reg_1080,
83d59bd469SMatt Jacob 	isp_pci_mbxdma,
84d59bd469SMatt Jacob 	isp_pci_dmasetup,
85d59bd469SMatt Jacob 	isp_pci_dmateardown,
86d59bd469SMatt Jacob 	NULL,
87d59bd469SMatt Jacob 	isp_pci_reset1,
88d59bd469SMatt Jacob 	isp_pci_dumpregs,
8956aef503SMatt Jacob 	NULL,
90d02373f1SMatt Jacob 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
91d59bd469SMatt Jacob };
92d59bd469SMatt Jacob 
93960f6939SMatt Jacob static struct ispmdvec mdvec_12160 = {
94126ec864SMatt Jacob 	isp_pci_rd_isr,
95960f6939SMatt Jacob 	isp_pci_rd_reg_1080,
96960f6939SMatt Jacob 	isp_pci_wr_reg_1080,
97960f6939SMatt Jacob 	isp_pci_mbxdma,
98960f6939SMatt Jacob 	isp_pci_dmasetup,
99960f6939SMatt Jacob 	isp_pci_dmateardown,
100960f6939SMatt Jacob 	NULL,
101960f6939SMatt Jacob 	isp_pci_reset1,
102960f6939SMatt Jacob 	isp_pci_dumpregs,
10356aef503SMatt Jacob 	NULL,
104d02373f1SMatt Jacob 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
105960f6939SMatt Jacob };
106960f6939SMatt Jacob 
10765adb54cSMatt Jacob static struct ispmdvec mdvec_2100 = {
108126ec864SMatt Jacob 	isp_pci_rd_isr,
10965adb54cSMatt Jacob 	isp_pci_rd_reg,
11065adb54cSMatt Jacob 	isp_pci_wr_reg,
11165adb54cSMatt Jacob 	isp_pci_mbxdma,
11265adb54cSMatt Jacob 	isp_pci_dmasetup,
113d720e6d5SJustin T. Gibbs 	isp_pci_dmateardown,
11465adb54cSMatt Jacob 	NULL,
11565adb54cSMatt Jacob 	isp_pci_reset1,
116d02373f1SMatt Jacob 	isp_pci_dumpregs
11765adb54cSMatt Jacob };
118222bb542SMatt Jacob 
119222bb542SMatt Jacob static struct ispmdvec mdvec_2200 = {
120126ec864SMatt Jacob 	isp_pci_rd_isr,
121126ec864SMatt Jacob 	isp_pci_rd_reg,
122126ec864SMatt Jacob 	isp_pci_wr_reg,
123126ec864SMatt Jacob 	isp_pci_mbxdma,
124126ec864SMatt Jacob 	isp_pci_dmasetup,
125126ec864SMatt Jacob 	isp_pci_dmateardown,
126126ec864SMatt Jacob 	NULL,
127126ec864SMatt Jacob 	isp_pci_reset1,
128126ec864SMatt Jacob 	isp_pci_dumpregs
129126ec864SMatt Jacob };
130126ec864SMatt Jacob 
131126ec864SMatt Jacob static struct ispmdvec mdvec_2300 = {
132126ec864SMatt Jacob 	isp_pci_rd_isr_2300,
133222bb542SMatt Jacob 	isp_pci_rd_reg,
134222bb542SMatt Jacob 	isp_pci_wr_reg,
135222bb542SMatt Jacob 	isp_pci_mbxdma,
136222bb542SMatt Jacob 	isp_pci_dmasetup,
137222bb542SMatt Jacob 	isp_pci_dmateardown,
138222bb542SMatt Jacob 	NULL,
139222bb542SMatt Jacob 	isp_pci_reset1,
140d02373f1SMatt Jacob 	isp_pci_dumpregs
141222bb542SMatt Jacob };
142d951bbcaSMatt Jacob 
14365adb54cSMatt Jacob #ifndef	PCIM_CMD_INVEN
14465adb54cSMatt Jacob #define	PCIM_CMD_INVEN			0x10
14565adb54cSMatt Jacob #endif
14665adb54cSMatt Jacob #ifndef	PCIM_CMD_BUSMASTEREN
14765adb54cSMatt Jacob #define	PCIM_CMD_BUSMASTEREN		0x0004
14865adb54cSMatt Jacob #endif
149d951bbcaSMatt Jacob #ifndef	PCIM_CMD_PERRESPEN
150d951bbcaSMatt Jacob #define	PCIM_CMD_PERRESPEN		0x0040
151d951bbcaSMatt Jacob #endif
152d951bbcaSMatt Jacob #ifndef	PCIM_CMD_SEREN
153d951bbcaSMatt Jacob #define	PCIM_CMD_SEREN			0x0100
154d951bbcaSMatt Jacob #endif
155d951bbcaSMatt Jacob 
156d951bbcaSMatt Jacob #ifndef	PCIR_COMMAND
157d951bbcaSMatt Jacob #define	PCIR_COMMAND			0x04
158d951bbcaSMatt Jacob #endif
159d951bbcaSMatt Jacob 
160d951bbcaSMatt Jacob #ifndef	PCIR_CACHELNSZ
161d951bbcaSMatt Jacob #define	PCIR_CACHELNSZ			0x0c
162d951bbcaSMatt Jacob #endif
163d951bbcaSMatt Jacob 
164d951bbcaSMatt Jacob #ifndef	PCIR_LATTIMER
165d951bbcaSMatt Jacob #define	PCIR_LATTIMER			0x0d
166d951bbcaSMatt Jacob #endif
167d951bbcaSMatt Jacob 
168ab6d0040SMatt Jacob #ifndef	PCIR_ROMADDR
169ab6d0040SMatt Jacob #define	PCIR_ROMADDR			0x30
170ab6d0040SMatt Jacob #endif
171ab6d0040SMatt Jacob 
17265adb54cSMatt Jacob #ifndef	PCI_VENDOR_QLOGIC
17365adb54cSMatt Jacob #define	PCI_VENDOR_QLOGIC		0x1077
17465adb54cSMatt Jacob #endif
17565adb54cSMatt Jacob 
17665adb54cSMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1020
17765adb54cSMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP1020	0x1020
17865adb54cSMatt Jacob #endif
17965adb54cSMatt Jacob 
180d59bd469SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1080
181d59bd469SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP1080	0x1080
182d59bd469SMatt Jacob #endif
183d59bd469SMatt Jacob 
184960f6939SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP12160
185960f6939SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP12160	0x1216
186960f6939SMatt Jacob #endif
187960f6939SMatt Jacob 
188d59bd469SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1240
189d59bd469SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP1240	0x1240
190d59bd469SMatt Jacob #endif
19165adb54cSMatt Jacob 
19222e1dc85SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1280
19322e1dc85SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP1280	0x1280
19422e1dc85SMatt Jacob #endif
19522e1dc85SMatt Jacob 
19665adb54cSMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2100
19765adb54cSMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2100	0x2100
19865adb54cSMatt Jacob #endif
19965adb54cSMatt Jacob 
200222bb542SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2200
201222bb542SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2200	0x2200
202222bb542SMatt Jacob #endif
203222bb542SMatt Jacob 
204126ec864SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2300
205126ec864SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2300	0x2300
206126ec864SMatt Jacob #endif
207126ec864SMatt Jacob 
208126ec864SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2312
209126ec864SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2312	0x2312
210126ec864SMatt Jacob #endif
211126ec864SMatt Jacob 
21256aef503SMatt Jacob #define	PCI_QLOGIC_ISP1020	\
21356aef503SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
214d59bd469SMatt Jacob 
215d59bd469SMatt Jacob #define	PCI_QLOGIC_ISP1080	\
216d59bd469SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP1080 << 16) | PCI_VENDOR_QLOGIC)
217d59bd469SMatt Jacob 
218960f6939SMatt Jacob #define	PCI_QLOGIC_ISP12160	\
219960f6939SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP12160 << 16) | PCI_VENDOR_QLOGIC)
220960f6939SMatt Jacob 
221d59bd469SMatt Jacob #define	PCI_QLOGIC_ISP1240	\
222d59bd469SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP1240 << 16) | PCI_VENDOR_QLOGIC)
223d59bd469SMatt Jacob 
22422e1dc85SMatt Jacob #define	PCI_QLOGIC_ISP1280	\
22522e1dc85SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP1280 << 16) | PCI_VENDOR_QLOGIC)
22622e1dc85SMatt Jacob 
22765adb54cSMatt Jacob #define	PCI_QLOGIC_ISP2100	\
22865adb54cSMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
22965adb54cSMatt Jacob 
230222bb542SMatt Jacob #define	PCI_QLOGIC_ISP2200	\
231222bb542SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2200 << 16) | PCI_VENDOR_QLOGIC)
232222bb542SMatt Jacob 
233126ec864SMatt Jacob #define	PCI_QLOGIC_ISP2300	\
234126ec864SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2300 << 16) | PCI_VENDOR_QLOGIC)
235126ec864SMatt Jacob 
236126ec864SMatt Jacob #define	PCI_QLOGIC_ISP2312	\
237126ec864SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2312 << 16) | PCI_VENDOR_QLOGIC)
238126ec864SMatt Jacob 
239e11a1ee8SMatt Jacob /*
240e11a1ee8SMatt Jacob  * Odd case for some AMI raid cards... We need to *not* attach to this.
241e11a1ee8SMatt Jacob  */
242e11a1ee8SMatt Jacob #define	AMI_RAID_SUBVENDOR_ID	0x101e
243e11a1ee8SMatt Jacob 
24465adb54cSMatt Jacob #define	IO_MAP_REG	0x10
24565adb54cSMatt Jacob #define	MEM_MAP_REG	0x14
24665adb54cSMatt Jacob 
247d951bbcaSMatt Jacob #define	PCI_DFLT_LTNCY	0x40
248d951bbcaSMatt Jacob #define	PCI_DFLT_LNSZ	0x10
24965adb54cSMatt Jacob 
250960f6939SMatt Jacob static int isp_pci_probe (device_t);
251960f6939SMatt Jacob static int isp_pci_attach (device_t);
25265adb54cSMatt Jacob 
2531923f739SMatt Jacob 
25465adb54cSMatt Jacob struct isp_pcisoftc {
25565adb54cSMatt Jacob 	struct ispsoftc			pci_isp;
256960f6939SMatt Jacob 	device_t			pci_dev;
257960f6939SMatt Jacob 	struct resource *		pci_reg;
25865adb54cSMatt Jacob 	bus_space_tag_t			pci_st;
25965adb54cSMatt Jacob 	bus_space_handle_t		pci_sh;
260960f6939SMatt Jacob 	void *				ih;
261d59bd469SMatt Jacob 	int16_t				pci_poff[_NREG_BLKS];
2621923f739SMatt Jacob 	bus_dma_tag_t			dmat;
263a95ae193SMatt Jacob 	bus_dmamap_t			*dmaps;
26465adb54cSMatt Jacob };
26573030e03SMatt Jacob extern ispfwfunc *isp_get_firmware_p;
26665adb54cSMatt Jacob 
267960f6939SMatt Jacob static device_method_t isp_pci_methods[] = {
268960f6939SMatt Jacob 	/* Device interface */
269960f6939SMatt Jacob 	DEVMETHOD(device_probe,		isp_pci_probe),
270960f6939SMatt Jacob 	DEVMETHOD(device_attach,	isp_pci_attach),
271960f6939SMatt Jacob 	{ 0, 0 }
27265adb54cSMatt Jacob };
273126ec864SMatt Jacob static void isp_pci_intr(void *);
27465adb54cSMatt Jacob 
275960f6939SMatt Jacob static driver_t isp_pci_driver = {
276960f6939SMatt Jacob 	"isp", isp_pci_methods, sizeof (struct isp_pcisoftc)
277960f6939SMatt Jacob };
278960f6939SMatt Jacob static devclass_t isp_devclass;
279960f6939SMatt Jacob DRIVER_MODULE(isp, pci, isp_pci_driver, isp_devclass, 0, 0);
28065adb54cSMatt Jacob 
281960f6939SMatt Jacob static int
282960f6939SMatt Jacob isp_pci_probe(device_t dev)
28365adb54cSMatt Jacob {
284960f6939SMatt Jacob         switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
28556aef503SMatt Jacob 	case PCI_QLOGIC_ISP1020:
286960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 1020/1040 PCI SCSI Adapter");
28765adb54cSMatt Jacob 		break;
288d59bd469SMatt Jacob 	case PCI_QLOGIC_ISP1080:
289960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 1080 PCI SCSI Adapter");
290c6608df3SMatt Jacob 		break;
291c6608df3SMatt Jacob 	case PCI_QLOGIC_ISP1240:
292960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 1240 PCI SCSI Adapter");
293d59bd469SMatt Jacob 		break;
29422e1dc85SMatt Jacob 	case PCI_QLOGIC_ISP1280:
295960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 1280 PCI SCSI Adapter");
296960f6939SMatt Jacob 		break;
297960f6939SMatt Jacob 	case PCI_QLOGIC_ISP12160:
298e11a1ee8SMatt Jacob 		if (pci_get_subvendor(dev) == AMI_RAID_SUBVENDOR_ID) {
299e11a1ee8SMatt Jacob 			return (ENXIO);
300e11a1ee8SMatt Jacob 		}
301960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 12160 PCI SCSI Adapter");
30222e1dc85SMatt Jacob 		break;
30365adb54cSMatt Jacob 	case PCI_QLOGIC_ISP2100:
304960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2100 PCI FC-AL Adapter");
30565adb54cSMatt Jacob 		break;
3065542fe4bSMatt Jacob 	case PCI_QLOGIC_ISP2200:
307960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2200 PCI FC-AL Adapter");
3085542fe4bSMatt Jacob 		break;
309126ec864SMatt Jacob 	case PCI_QLOGIC_ISP2300:
310126ec864SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2300 PCI FC-AL Adapter");
311126ec864SMatt Jacob 		break;
312126ec864SMatt Jacob 	case PCI_QLOGIC_ISP2312:
313126ec864SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2312 PCI FC-AL Adapter");
314126ec864SMatt Jacob 		break;
31565adb54cSMatt Jacob 	default:
316960f6939SMatt Jacob 		return (ENXIO);
31765adb54cSMatt Jacob 	}
31873030e03SMatt Jacob 	if (isp_announced == 0 && bootverbose) {
319d02373f1SMatt Jacob 		printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
320a95ae193SMatt Jacob 		    "Core Version %d.%d\n",
321d720e6d5SJustin T. Gibbs 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
322d720e6d5SJustin T. Gibbs 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
32373030e03SMatt Jacob 		isp_announced++;
32465adb54cSMatt Jacob 	}
32556aef503SMatt Jacob 	/*
32656aef503SMatt Jacob 	 * XXXX: Here is where we might load the f/w module
32756aef503SMatt Jacob 	 * XXXX: (or increase a reference count to it).
32856aef503SMatt Jacob 	 */
329960f6939SMatt Jacob 	return (0);
33065adb54cSMatt Jacob }
33165adb54cSMatt Jacob 
332960f6939SMatt Jacob static int
333960f6939SMatt Jacob isp_pci_attach(device_t dev)
33465adb54cSMatt Jacob {
335960f6939SMatt Jacob 	struct resource *regs, *irq;
3366e5c5328SMatt Jacob 	int tval, rtp, rgd, iqd, m1, m2, isp_debug, role;
337960f6939SMatt Jacob 	u_int32_t data, cmd, linesz, psize, basetype;
33865adb54cSMatt Jacob 	struct isp_pcisoftc *pcs;
3393395b056SMatt Jacob 	struct ispsoftc *isp = NULL;
340c6608df3SMatt Jacob 	struct ispmdvec *mdvp;
34122941bd7SPeter Wemm 	const char *sptr;
3423395b056SMatt Jacob 	int locksetup = 0;
34365adb54cSMatt Jacob 
344222bb542SMatt Jacob 	/*
3459ba86737SMatt Jacob 	 * Figure out if we're supposed to skip this one.
346b9b599feSMatt Jacob 	 * If we are, we actually go to ISP_ROLE_NONE.
3479ba86737SMatt Jacob 	 */
3486e5c5328SMatt Jacob 
3496e5c5328SMatt Jacob 	tval = 0;
3506e5c5328SMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3516e5c5328SMatt Jacob 	    "disable", &tval) == 0 && tval) {
352b9b599feSMatt Jacob 		device_printf(dev, "device is disabled\n");
353b9b599feSMatt Jacob 		/* but return 0 so the !$)$)*!$*) unit isn't reused */
354b9b599feSMatt Jacob 		return (0);
355b9b599feSMatt Jacob 	}
3566e5c5328SMatt Jacob 
3576e5c5328SMatt Jacob 	role = 0;
3586e5c5328SMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3596e5c5328SMatt Jacob 	    "role", &role) == 0 &&
3606e5c5328SMatt Jacob 	    ((role & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) == 0)) {
3616e5c5328SMatt Jacob 		device_printf(dev, "setting role to 0x%x\n", role);
3626e5c5328SMatt Jacob 	} else {
363b9b599feSMatt Jacob #ifdef	ISP_TARGET_MODE
364b9b599feSMatt Jacob 		role = ISP_ROLE_INITIATOR|ISP_ROLE_TARGET;
365b9b599feSMatt Jacob #else
366b9b599feSMatt Jacob 		role = ISP_DEFAULT_ROLES;
367b9b599feSMatt Jacob #endif
3689ba86737SMatt Jacob 	}
3699ba86737SMatt Jacob 
3707cc0979fSDavid Malone 	pcs = malloc(sizeof (struct isp_pcisoftc), M_DEVBUF, M_NOWAIT | M_ZERO);
371960f6939SMatt Jacob 	if (pcs == NULL) {
372960f6939SMatt Jacob 		device_printf(dev, "cannot allocate softc\n");
373960f6939SMatt Jacob 		return (ENOMEM);
374960f6939SMatt Jacob 	}
375960f6939SMatt Jacob 
3769ba86737SMatt Jacob 	/*
377222bb542SMatt Jacob 	 * Figure out which we should try first - memory mapping or i/o mapping?
378222bb542SMatt Jacob 	 */
37956aef503SMatt Jacob #ifdef	__alpha__
380960f6939SMatt Jacob 	m1 = PCIM_CMD_MEMEN;
381960f6939SMatt Jacob 	m2 = PCIM_CMD_PORTEN;
382222bb542SMatt Jacob #else
383960f6939SMatt Jacob 	m1 = PCIM_CMD_PORTEN;
384960f6939SMatt Jacob 	m2 = PCIM_CMD_MEMEN;
385222bb542SMatt Jacob #endif
3866e5c5328SMatt Jacob 
3876e5c5328SMatt Jacob 	tval = 0;
3886e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3896e5c5328SMatt Jacob             "prefer_iomap", &tval) == 0 && tval != 0) {
390960f6939SMatt Jacob 		m1 = PCIM_CMD_PORTEN;
391960f6939SMatt Jacob 		m2 = PCIM_CMD_MEMEN;
392960f6939SMatt Jacob 	}
3936e5c5328SMatt Jacob 	tval = 0;
3946e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3956e5c5328SMatt Jacob             "prefer_memmap", &tval) == 0 && tval != 0) {
3966e5c5328SMatt Jacob 		m1 = PCIM_CMD_MEMEN;
3976e5c5328SMatt Jacob 		m2 = PCIM_CMD_PORTEN;
398222bb542SMatt Jacob 	}
399222bb542SMatt Jacob 
400ab6d0040SMatt Jacob 	linesz = PCI_DFLT_LNSZ;
401960f6939SMatt Jacob 	irq = regs = NULL;
402960f6939SMatt Jacob 	rgd = rtp = iqd = 0;
403960f6939SMatt Jacob 
404960f6939SMatt Jacob 	cmd = pci_read_config(dev, PCIR_COMMAND, 1);
405960f6939SMatt Jacob 	if (cmd & m1) {
406960f6939SMatt Jacob 		rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
407960f6939SMatt Jacob 		rgd = (m1 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
408960f6939SMatt Jacob 		regs = bus_alloc_resource(dev, rtp, &rgd, 0, ~0, 1, RF_ACTIVE);
40965adb54cSMatt Jacob 	}
410960f6939SMatt Jacob 	if (regs == NULL && (cmd & m2)) {
411960f6939SMatt Jacob 		rtp = (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
412960f6939SMatt Jacob 		rgd = (m2 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
413960f6939SMatt Jacob 		regs = bus_alloc_resource(dev, rtp, &rgd, 0, ~0, 1, RF_ACTIVE);
41465adb54cSMatt Jacob 	}
415960f6939SMatt Jacob 	if (regs == NULL) {
416960f6939SMatt Jacob 		device_printf(dev, "unable to map any ports\n");
417960f6939SMatt Jacob 		goto bad;
41865adb54cSMatt Jacob 	}
419222bb542SMatt Jacob 	if (bootverbose)
420f7dddf8aSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
421960f6939SMatt Jacob 		    (rgd == IO_MAP_REG)? "I/O" : "Memory");
422960f6939SMatt Jacob 	pcs->pci_dev = dev;
423960f6939SMatt Jacob 	pcs->pci_reg = regs;
424960f6939SMatt Jacob 	pcs->pci_st = rman_get_bustag(regs);
425960f6939SMatt Jacob 	pcs->pci_sh = rman_get_bushandle(regs);
42665adb54cSMatt Jacob 
427d59bd469SMatt Jacob 	pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
428d59bd469SMatt Jacob 	pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
429d59bd469SMatt Jacob 	pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
430d59bd469SMatt Jacob 	pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
431d59bd469SMatt Jacob 	pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
432c6608df3SMatt Jacob 	mdvp = &mdvec;
433c6608df3SMatt Jacob 	basetype = ISP_HA_SCSI_UNKNOWN;
434c6608df3SMatt Jacob 	psize = sizeof (sdparam);
43556aef503SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1020) {
436c6608df3SMatt Jacob 		mdvp = &mdvec;
437c6608df3SMatt Jacob 		basetype = ISP_HA_SCSI_UNKNOWN;
438c6608df3SMatt Jacob 		psize = sizeof (sdparam);
439d59bd469SMatt Jacob 	}
440960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1080) {
441c6608df3SMatt Jacob 		mdvp = &mdvec_1080;
442c6608df3SMatt Jacob 		basetype = ISP_HA_SCSI_1080;
443c6608df3SMatt Jacob 		psize = sizeof (sdparam);
444c6608df3SMatt Jacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
445c6608df3SMatt Jacob 		    ISP1080_DMA_REGS_OFF;
446c6608df3SMatt Jacob 	}
447960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1240) {
448c6608df3SMatt Jacob 		mdvp = &mdvec_1080;
44922e1dc85SMatt Jacob 		basetype = ISP_HA_SCSI_1240;
45022e1dc85SMatt Jacob 		psize = 2 * sizeof (sdparam);
45122e1dc85SMatt Jacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
45222e1dc85SMatt Jacob 		    ISP1080_DMA_REGS_OFF;
45322e1dc85SMatt Jacob 	}
454960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1280) {
45522e1dc85SMatt Jacob 		mdvp = &mdvec_1080;
45622e1dc85SMatt Jacob 		basetype = ISP_HA_SCSI_1280;
457c6608df3SMatt Jacob 		psize = 2 * sizeof (sdparam);
458d59bd469SMatt Jacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
459d59bd469SMatt Jacob 		    ISP1080_DMA_REGS_OFF;
460d59bd469SMatt Jacob 	}
461960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP12160) {
462960f6939SMatt Jacob 		mdvp = &mdvec_12160;
463960f6939SMatt Jacob 		basetype = ISP_HA_SCSI_12160;
464960f6939SMatt Jacob 		psize = 2 * sizeof (sdparam);
465960f6939SMatt Jacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
466960f6939SMatt Jacob 		    ISP1080_DMA_REGS_OFF;
467960f6939SMatt Jacob 	}
468960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2100) {
469c6608df3SMatt Jacob 		mdvp = &mdvec_2100;
470c6608df3SMatt Jacob 		basetype = ISP_HA_FC_2100;
471c6608df3SMatt Jacob 		psize = sizeof (fcparam);
472d59bd469SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
473d59bd469SMatt Jacob 		    PCI_MBOX_REGS2100_OFF;
474960f6939SMatt Jacob 		if (pci_get_revid(dev) < 3) {
475ab6d0040SMatt Jacob 			/*
476ab6d0040SMatt Jacob 			 * XXX: Need to get the actual revision
477ab6d0040SMatt Jacob 			 * XXX: number of the 2100 FB. At any rate,
478ab6d0040SMatt Jacob 			 * XXX: lower cache line size for early revision
479ab6d0040SMatt Jacob 			 * XXX; boards.
480ab6d0040SMatt Jacob 			 */
481ab6d0040SMatt Jacob 			linesz = 1;
482ab6d0040SMatt Jacob 		}
48365adb54cSMatt Jacob 	}
484960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2200) {
485222bb542SMatt Jacob 		mdvp = &mdvec_2200;
486222bb542SMatt Jacob 		basetype = ISP_HA_FC_2200;
487222bb542SMatt Jacob 		psize = sizeof (fcparam);
488222bb542SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
489222bb542SMatt Jacob 		    PCI_MBOX_REGS2100_OFF;
490222bb542SMatt Jacob 	}
49175c1e828SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2300) {
492126ec864SMatt Jacob 		mdvp = &mdvec_2300;
493126ec864SMatt Jacob 		basetype = ISP_HA_FC_2300;
494126ec864SMatt Jacob 		psize = sizeof (fcparam);
495126ec864SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
496126ec864SMatt Jacob 		    PCI_MBOX_REGS2300_OFF;
497126ec864SMatt Jacob 	}
49875c1e828SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2312) {
49975c1e828SMatt Jacob 		mdvp = &mdvec_2300;
50075c1e828SMatt Jacob 		basetype = ISP_HA_FC_2312;
50175c1e828SMatt Jacob 		psize = sizeof (fcparam);
50275c1e828SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
50375c1e828SMatt Jacob 		    PCI_MBOX_REGS2300_OFF;
50475c1e828SMatt Jacob 	}
505c6608df3SMatt Jacob 	isp = &pcs->pci_isp;
5067cc0979fSDavid Malone 	isp->isp_param = malloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
507c6608df3SMatt Jacob 	if (isp->isp_param == NULL) {
508960f6939SMatt Jacob 		device_printf(dev, "cannot allocate parameter data\n");
509960f6939SMatt Jacob 		goto bad;
510c6608df3SMatt Jacob 	}
511c6608df3SMatt Jacob 	isp->isp_mdvec = mdvp;
512c6608df3SMatt Jacob 	isp->isp_type = basetype;
513960f6939SMatt Jacob 	isp->isp_revision = pci_get_revid(dev);
514b9b599feSMatt Jacob 	isp->isp_role = role;
5156e5c5328SMatt Jacob 	isp->isp_dev = dev;
51665adb54cSMatt Jacob 
51756aef503SMatt Jacob 	/*
51856aef503SMatt Jacob 	 * Try and find firmware for this device.
51956aef503SMatt Jacob 	 */
52056aef503SMatt Jacob 
52156aef503SMatt Jacob 	if (isp_get_firmware_p) {
52256aef503SMatt Jacob 		int device = (int) pci_get_device(dev);
52356aef503SMatt Jacob #ifdef	ISP_TARGET_MODE
52456aef503SMatt Jacob 		(*isp_get_firmware_p)(0, 1, device, &mdvp->dv_ispfw);
52556aef503SMatt Jacob #else
52656aef503SMatt Jacob 		(*isp_get_firmware_p)(0, 0, device, &mdvp->dv_ispfw);
52756aef503SMatt Jacob #endif
52856aef503SMatt Jacob 	}
52956aef503SMatt Jacob 
53056aef503SMatt Jacob 	/*
531d951bbcaSMatt Jacob 	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER
532d951bbcaSMatt Jacob 	 * are set.
533d951bbcaSMatt Jacob 	 */
534960f6939SMatt Jacob 	cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN |
535960f6939SMatt Jacob 		PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
53675c1e828SMatt Jacob 	if (IS_2300(isp)) {	/* per QLogic errata */
53775c1e828SMatt Jacob 		cmd &= ~PCIM_CMD_INVEN;
53875c1e828SMatt Jacob 	}
539fc087171SMatt Jacob 	if (IS_23XX(isp)) {
540fc087171SMatt Jacob 		/*
541fc087171SMatt Jacob 		 * Can't tell if ROM will hang on 'ABOUT FIRMWARE' command.
542fc087171SMatt Jacob 		 */
543fc087171SMatt Jacob 		isp->isp_touched = 1;
544fc087171SMatt Jacob 
545fc087171SMatt Jacob 	}
546960f6939SMatt Jacob 	pci_write_config(dev, PCIR_COMMAND, cmd, 1);
547ab6d0040SMatt Jacob 
548d951bbcaSMatt Jacob 	/*
549222bb542SMatt Jacob 	 * Make sure the Cache Line Size register is set sensibly.
550d951bbcaSMatt Jacob 	 */
551960f6939SMatt Jacob 	data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
552ab6d0040SMatt Jacob 	if (data != linesz) {
553d951bbcaSMatt Jacob 		data = PCI_DFLT_LNSZ;
554d02373f1SMatt Jacob 		isp_prt(isp, ISP_LOGCONFIG, "set PCI line size to %d", data);
555960f6939SMatt Jacob 		pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
556d951bbcaSMatt Jacob 	}
557ab6d0040SMatt Jacob 
558d951bbcaSMatt Jacob 	/*
559d951bbcaSMatt Jacob 	 * Make sure the Latency Timer is sane.
560d951bbcaSMatt Jacob 	 */
561960f6939SMatt Jacob 	data = pci_read_config(dev, PCIR_LATTIMER, 1);
562d951bbcaSMatt Jacob 	if (data < PCI_DFLT_LTNCY) {
563d951bbcaSMatt Jacob 		data = PCI_DFLT_LTNCY;
564d02373f1SMatt Jacob 		isp_prt(isp, ISP_LOGCONFIG, "set PCI latency to %d", data);
565960f6939SMatt Jacob 		pci_write_config(dev, PCIR_LATTIMER, data, 1);
566d951bbcaSMatt Jacob 	}
567ab6d0040SMatt Jacob 
568ab6d0040SMatt Jacob 	/*
569ab6d0040SMatt Jacob 	 * Make sure we've disabled the ROM.
570ab6d0040SMatt Jacob 	 */
571960f6939SMatt Jacob 	data = pci_read_config(dev, PCIR_ROMADDR, 4);
572ab6d0040SMatt Jacob 	data &= ~1;
573960f6939SMatt Jacob 	pci_write_config(dev, PCIR_ROMADDR, data, 4);
57405fbcbb0SMatt Jacob 
575960f6939SMatt Jacob 	iqd = 0;
576960f6939SMatt Jacob 	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &iqd, 0, ~0,
577960f6939SMatt Jacob 	    1, RF_ACTIVE | RF_SHAREABLE);
578960f6939SMatt Jacob 	if (irq == NULL) {
579960f6939SMatt Jacob 		device_printf(dev, "could not allocate interrupt\n");
580960f6939SMatt Jacob 		goto bad;
581960f6939SMatt Jacob 	}
582960f6939SMatt Jacob 
5836e5c5328SMatt Jacob 	tval = 0;
5846e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
5856e5c5328SMatt Jacob             "fwload_disable", &tval) == 0 && tval != 0) {
586222bb542SMatt Jacob 		isp->isp_confopts |= ISP_CFG_NORELOAD;
587222bb542SMatt Jacob 	}
5886e5c5328SMatt Jacob 	tval = 0;
5896e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
5906e5c5328SMatt Jacob             "ignore_nvram", &tval) == 0 && tval != 0) {
591222bb542SMatt Jacob 		isp->isp_confopts |= ISP_CFG_NONVRAM;
592222bb542SMatt Jacob 	}
5936e5c5328SMatt Jacob 	tval = 0;
5946e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
5956e5c5328SMatt Jacob             "fullduplex", &tval) == 0 && tval != 0) {
596222bb542SMatt Jacob 		isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
597222bb542SMatt Jacob 	}
598d134aa0bSMatt Jacob #ifdef	ISP_FW_CRASH_DUMP
599d134aa0bSMatt Jacob 	tval = 0;
600d134aa0bSMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
601d134aa0bSMatt Jacob             "fw_dump_enable", &tval) == 0 && tval != 0) {
602d134aa0bSMatt Jacob 		size_t amt = 0;
603d134aa0bSMatt Jacob 		if (IS_2200(isp)) {
604d134aa0bSMatt Jacob 			amt = QLA2200_RISC_IMAGE_DUMP_SIZE;
605d134aa0bSMatt Jacob 		} else if (IS_23XX(isp)) {
606d134aa0bSMatt Jacob 			amt = QLA2300_RISC_IMAGE_DUMP_SIZE;
607d134aa0bSMatt Jacob 		}
608d134aa0bSMatt Jacob 		if (amt) {
609d134aa0bSMatt Jacob 			FCPARAM(isp)->isp_dump_data =
610d134aa0bSMatt Jacob 			    malloc(amt, M_DEVBUF, M_WAITOK | M_ZERO);
611d134aa0bSMatt Jacob 		} else {
612d134aa0bSMatt Jacob 			device_printf(dev,
613d134aa0bSMatt Jacob 			    "f/w crash dumps not supported for this model\n");
614d134aa0bSMatt Jacob 		}
615d134aa0bSMatt Jacob 	}
616d134aa0bSMatt Jacob #endif
617222bb542SMatt Jacob 
6186e5c5328SMatt Jacob 	sptr = 0;
6196e5c5328SMatt Jacob         if (resource_string_value(device_get_name(dev), device_get_unit(dev),
620559a1ad2SMatt Jacob             "topology", (const char **) &sptr) == 0 && sptr != 0) {
6216e5c5328SMatt Jacob 		if (strcmp(sptr, "lport") == 0) {
6226e5c5328SMatt Jacob 			isp->isp_confopts |= ISP_CFG_LPORT;
6236e5c5328SMatt Jacob 		} else if (strcmp(sptr, "nport") == 0) {
6246e5c5328SMatt Jacob 			isp->isp_confopts |= ISP_CFG_NPORT;
6256e5c5328SMatt Jacob 		} else if (strcmp(sptr, "lport-only") == 0) {
6266e5c5328SMatt Jacob 			isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
6276e5c5328SMatt Jacob 		} else if (strcmp(sptr, "nport-only") == 0) {
6286e5c5328SMatt Jacob 			isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
6299637d68cSMatt Jacob 		}
6306e5c5328SMatt Jacob 	}
6316e5c5328SMatt Jacob 
6329637d68cSMatt Jacob 	/*
6336e5c5328SMatt Jacob 	 * Because the resource_*_value functions can neither return
6346e5c5328SMatt Jacob 	 * 64 bit integer values, nor can they be directly coerced
6356e5c5328SMatt Jacob 	 * to interpret the right hand side of the assignment as
6366e5c5328SMatt Jacob 	 * you want them to interpret it, we have to force WWN
6376e5c5328SMatt Jacob 	 * hint replacement to specify WWN strings with a leading
6386e5c5328SMatt Jacob 	 * 'w' (e..g w50000000aaaa0001). Sigh.
6399637d68cSMatt Jacob 	 */
6406e5c5328SMatt Jacob 	sptr = 0;
6416e5c5328SMatt Jacob 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
642559a1ad2SMatt Jacob             "portwwn", (const char **) &sptr);
6436e5c5328SMatt Jacob 	if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
6446e5c5328SMatt Jacob 		char *eptr = 0;
6456e5c5328SMatt Jacob 		isp->isp_osinfo.default_port_wwn = strtouq(sptr, &eptr, 16);
6466e5c5328SMatt Jacob 		if (eptr < sptr + 16 || isp->isp_osinfo.default_port_wwn == 0) {
6476e5c5328SMatt Jacob 			device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
6486e5c5328SMatt Jacob 			isp->isp_osinfo.default_port_wwn = 0;
6499637d68cSMatt Jacob 		} else {
6503f02619fSMatt Jacob 			isp->isp_confopts |= ISP_CFG_OWNWWPN;
651222bb542SMatt Jacob 		}
6526e5c5328SMatt Jacob 	}
6536e5c5328SMatt Jacob 	if (isp->isp_osinfo.default_port_wwn == 0) {
6546e5c5328SMatt Jacob 		isp->isp_osinfo.default_port_wwn = 0x400000007F000009ull;
6556e5c5328SMatt Jacob 	}
6566e5c5328SMatt Jacob 
6576e5c5328SMatt Jacob 	sptr = 0;
6586e5c5328SMatt Jacob 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
659559a1ad2SMatt Jacob             "nodewwn", (const char **) &sptr);
6606e5c5328SMatt Jacob 	if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
6616e5c5328SMatt Jacob 		char *eptr = 0;
6626e5c5328SMatt Jacob 		isp->isp_osinfo.default_node_wwn = strtouq(sptr, &eptr, 16);
6636e5c5328SMatt Jacob 		if (eptr < sptr + 16 || isp->isp_osinfo.default_node_wwn == 0) {
6646e5c5328SMatt Jacob 			device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
6656e5c5328SMatt Jacob 			isp->isp_osinfo.default_node_wwn = 0;
6666e5c5328SMatt Jacob 		} else {
6673f02619fSMatt Jacob 			isp->isp_confopts |= ISP_CFG_OWNWWNN;
6686e5c5328SMatt Jacob 		}
6696e5c5328SMatt Jacob 	}
6706e5c5328SMatt Jacob 	if (isp->isp_osinfo.default_node_wwn == 0) {
6716e5c5328SMatt Jacob 		isp->isp_osinfo.default_node_wwn = 0x400000007F000009ull;
6726e5c5328SMatt Jacob 	}
6736e5c5328SMatt Jacob 
674ed753e82SMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
675ed753e82SMatt Jacob             "iid", &tval) == 0) {
676ed753e82SMatt Jacob 		isp->isp_osinfo.default_id = tval;
677ed753e82SMatt Jacob 		isp->isp_confopts |= ISP_CFG_OWNLOOPID;
678ed753e82SMatt Jacob 	}
679ed753e82SMatt Jacob 	if (isp->isp_osinfo.default_id == 0) {
680ed753e82SMatt Jacob 		if (IS_FC(isp)) {
681ed753e82SMatt Jacob 			isp->isp_osinfo.default_id = 109;
682ed753e82SMatt Jacob 		} else {
683ed753e82SMatt Jacob 			isp->isp_osinfo.default_id = 7;
684ed753e82SMatt Jacob 		}
685ed753e82SMatt Jacob 	}
686ed753e82SMatt Jacob 
687ed753e82SMatt Jacob 	isp_debug = 0;
6886e5c5328SMatt Jacob         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
6896e5c5328SMatt Jacob             "debug", &isp_debug);
690f09b1922SMatt Jacob 
691f09b1922SMatt Jacob 	/* Make sure the lock is set up. */
6926008862bSJohn Baldwin 	mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
693f09b1922SMatt Jacob 	locksetup++;
694f09b1922SMatt Jacob 
6955d571944SMatt Jacob #ifdef	ISP_SMPLOCK
6965d571944SMatt Jacob #define	INTR_FLAGS	INTR_TYPE_CAM | INTR_MPSAFE | INTR_ENTROPY
697f09b1922SMatt Jacob #else
6985d571944SMatt Jacob #define	INTR_FLAGS	INTR_TYPE_CAM | INTR_ENTROPY
6995d571944SMatt Jacob #endif
7005d571944SMatt Jacob 	if (bus_setup_intr(dev, irq, INTR_FLAGS, isp_pci_intr, isp, &pcs->ih)) {
701f09b1922SMatt Jacob 		device_printf(dev, "could not setup interrupt\n");
702f09b1922SMatt Jacob 		goto bad;
703f09b1922SMatt Jacob 	}
704960f6939SMatt Jacob 
70505fbcbb0SMatt Jacob 	/*
706d02373f1SMatt Jacob 	 * Set up logging levels.
707d02373f1SMatt Jacob 	 */
708d02373f1SMatt Jacob 	if (isp_debug) {
709d02373f1SMatt Jacob 		isp->isp_dblev = isp_debug;
710d02373f1SMatt Jacob 	} else {
711d02373f1SMatt Jacob 		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
712d02373f1SMatt Jacob 	}
713d02373f1SMatt Jacob 	if (bootverbose)
714f7dddf8aSMatt Jacob 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
715d02373f1SMatt Jacob 
716d02373f1SMatt Jacob 	/*
71775c1e828SMatt Jacob 	 * Last minute checks...
71875c1e828SMatt Jacob 	 */
71975c1e828SMatt Jacob 	if (IS_2312(isp)) {
72075c1e828SMatt Jacob 		isp->isp_port = pci_get_function(dev);
72175c1e828SMatt Jacob 	}
72275c1e828SMatt Jacob 
72375c1e828SMatt Jacob 	/*
72405fbcbb0SMatt Jacob 	 * Make sure we're in reset state.
72505fbcbb0SMatt Jacob 	 */
7263395b056SMatt Jacob 	ISP_LOCK(isp);
72765adb54cSMatt Jacob 	isp_reset(isp);
72865adb54cSMatt Jacob 	if (isp->isp_state != ISP_RESETSTATE) {
7293395b056SMatt Jacob 		ISP_UNLOCK(isp);
730960f6939SMatt Jacob 		goto bad;
73165adb54cSMatt Jacob 	}
73265adb54cSMatt Jacob 	isp_init(isp);
733b9b599feSMatt Jacob 	if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_INITSTATE) {
73465adb54cSMatt Jacob 		isp_uninit(isp);
7353395b056SMatt Jacob 		ISP_UNLOCK(isp);
736960f6939SMatt Jacob 		goto bad;
737d59bd469SMatt Jacob 	}
73865adb54cSMatt Jacob 	isp_attach(isp);
739b9b599feSMatt Jacob 	if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_RUNSTATE) {
74065adb54cSMatt Jacob 		isp_uninit(isp);
7413395b056SMatt Jacob 		ISP_UNLOCK(isp);
742960f6939SMatt Jacob 		goto bad;
743960f6939SMatt Jacob 	}
74456aef503SMatt Jacob 	/*
74556aef503SMatt Jacob 	 * XXXX: Here is where we might unload the f/w module
74656aef503SMatt Jacob 	 * XXXX: (or decrease the reference count to it).
74756aef503SMatt Jacob 	 */
7483395b056SMatt Jacob 	ISP_UNLOCK(isp);
749960f6939SMatt Jacob 	return (0);
750960f6939SMatt Jacob 
751960f6939SMatt Jacob bad:
752960f6939SMatt Jacob 
753960f6939SMatt Jacob 	if (pcs && pcs->ih) {
754960f6939SMatt Jacob 		(void) bus_teardown_intr(dev, irq, pcs->ih);
755960f6939SMatt Jacob 	}
756960f6939SMatt Jacob 
7573395b056SMatt Jacob 	if (locksetup && isp) {
7583395b056SMatt Jacob 		mtx_destroy(&isp->isp_osinfo.lock);
7593395b056SMatt Jacob 	}
7603395b056SMatt Jacob 
761960f6939SMatt Jacob 	if (irq) {
762960f6939SMatt Jacob 		(void) bus_release_resource(dev, SYS_RES_IRQ, iqd, irq);
763960f6939SMatt Jacob 	}
7643395b056SMatt Jacob 
7653395b056SMatt Jacob 
766960f6939SMatt Jacob 	if (regs) {
767960f6939SMatt Jacob 		(void) bus_release_resource(dev, rtp, rgd, regs);
768960f6939SMatt Jacob 	}
7693395b056SMatt Jacob 
770960f6939SMatt Jacob 	if (pcs) {
771960f6939SMatt Jacob 		if (pcs->pci_isp.isp_param)
772960f6939SMatt Jacob 			free(pcs->pci_isp.isp_param, M_DEVBUF);
77365adb54cSMatt Jacob 		free(pcs, M_DEVBUF);
77465adb54cSMatt Jacob 	}
7753395b056SMatt Jacob 
77656aef503SMatt Jacob 	/*
77756aef503SMatt Jacob 	 * XXXX: Here is where we might unload the f/w module
77856aef503SMatt Jacob 	 * XXXX: (or decrease the reference count to it).
77956aef503SMatt Jacob 	 */
780960f6939SMatt Jacob 	return (ENXIO);
78165adb54cSMatt Jacob }
78265adb54cSMatt Jacob 
783f09b1922SMatt Jacob static void
784f09b1922SMatt Jacob isp_pci_intr(void *arg)
785f09b1922SMatt Jacob {
786f09b1922SMatt Jacob 	struct ispsoftc *isp = arg;
787126ec864SMatt Jacob 	u_int16_t isr, sema, mbox;
788126ec864SMatt Jacob 
789f09b1922SMatt Jacob 	ISP_LOCK(isp);
790126ec864SMatt Jacob 	isp->isp_intcnt++;
791126ec864SMatt Jacob 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
792126ec864SMatt Jacob 		isp->isp_intbogus++;
793126ec864SMatt Jacob 	} else {
794b96934e8SMatt Jacob 		int iok = isp->isp_osinfo.intsok;
795b96934e8SMatt Jacob 		isp->isp_osinfo.intsok = 0;
796126ec864SMatt Jacob 		isp_intr(isp, isr, sema, mbox);
797b96934e8SMatt Jacob 		isp->isp_osinfo.intsok = iok;
798126ec864SMatt Jacob 	}
799f09b1922SMatt Jacob 	ISP_UNLOCK(isp);
800f09b1922SMatt Jacob }
801f09b1922SMatt Jacob 
802126ec864SMatt Jacob 
803126ec864SMatt Jacob #define	IspVirt2Off(a, x)	\
804126ec864SMatt Jacob 	(((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
805126ec864SMatt Jacob 	_BLK_REG_SHFT] + ((x) & 0xff))
806126ec864SMatt Jacob 
807126ec864SMatt Jacob #define	BXR2(pcs, off)		\
808126ec864SMatt Jacob 	bus_space_read_2(pcs->pci_st, pcs->pci_sh, off)
809126ec864SMatt Jacob #define	BXW2(pcs, off, v)	\
810126ec864SMatt Jacob 	bus_space_write_2(pcs->pci_st, pcs->pci_sh, off, v)
811126ec864SMatt Jacob 
812126ec864SMatt Jacob 
813126ec864SMatt Jacob static INLINE int
814126ec864SMatt Jacob isp_pci_rd_debounced(struct ispsoftc *isp, int off, u_int16_t *rp)
815126ec864SMatt Jacob {
816126ec864SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
817126ec864SMatt Jacob 	u_int16_t val0, val1;
818126ec864SMatt Jacob 	int i = 0;
819126ec864SMatt Jacob 
820126ec864SMatt Jacob 	do {
821126ec864SMatt Jacob 		val0 = BXR2(pcs, IspVirt2Off(isp, off));
822126ec864SMatt Jacob 		val1 = BXR2(pcs, IspVirt2Off(isp, off));
823126ec864SMatt Jacob 	} while (val0 != val1 && ++i < 1000);
824126ec864SMatt Jacob 	if (val0 != val1) {
825126ec864SMatt Jacob 		return (1);
826126ec864SMatt Jacob 	}
827126ec864SMatt Jacob 	*rp = val0;
828126ec864SMatt Jacob 	return (0);
829126ec864SMatt Jacob }
830126ec864SMatt Jacob 
831126ec864SMatt Jacob static int
832126ec864SMatt Jacob isp_pci_rd_isr(struct ispsoftc *isp, u_int16_t *isrp,
833126ec864SMatt Jacob     u_int16_t *semap, u_int16_t *mbp)
834126ec864SMatt Jacob {
835126ec864SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
836126ec864SMatt Jacob 	u_int16_t isr, sema;
837126ec864SMatt Jacob 
838126ec864SMatt Jacob 	if (IS_2100(isp)) {
839126ec864SMatt Jacob 		if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
840126ec864SMatt Jacob 		    return (0);
841126ec864SMatt Jacob 		}
842126ec864SMatt Jacob 		if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
843126ec864SMatt Jacob 		    return (0);
844126ec864SMatt Jacob 		}
845126ec864SMatt Jacob 	} else {
846126ec864SMatt Jacob 		isr = BXR2(pcs, IspVirt2Off(isp, BIU_ISR));
847126ec864SMatt Jacob 		sema = BXR2(pcs, IspVirt2Off(isp, BIU_SEMA));
848126ec864SMatt Jacob 	}
849126ec864SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
850126ec864SMatt Jacob 	isr &= INT_PENDING_MASK(isp);
851126ec864SMatt Jacob 	sema &= BIU_SEMA_LOCK;
852126ec864SMatt Jacob 	if (isr == 0 && sema == 0) {
853126ec864SMatt Jacob 		return (0);
854126ec864SMatt Jacob 	}
855126ec864SMatt Jacob 	*isrp = isr;
856126ec864SMatt Jacob 	if ((*semap = sema) != 0) {
857126ec864SMatt Jacob 		if (IS_2100(isp)) {
858126ec864SMatt Jacob 			if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
859126ec864SMatt Jacob 				return (0);
860126ec864SMatt Jacob 			}
861126ec864SMatt Jacob 		} else {
862126ec864SMatt Jacob 			*mbp = BXR2(pcs, IspVirt2Off(isp, OUTMAILBOX0));
863126ec864SMatt Jacob 		}
864126ec864SMatt Jacob 	}
865126ec864SMatt Jacob 	return (1);
866126ec864SMatt Jacob }
867126ec864SMatt Jacob 
868126ec864SMatt Jacob static int
869126ec864SMatt Jacob isp_pci_rd_isr_2300(struct ispsoftc *isp, u_int16_t *isrp,
870126ec864SMatt Jacob     u_int16_t *semap, u_int16_t *mbox0p)
871126ec864SMatt Jacob {
872126ec864SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
873126ec864SMatt Jacob 	u_int32_t r2hisr;
874126ec864SMatt Jacob 
8753bd40330SMatt Jacob 	if (!(BXR2(pcs, IspVirt2Off(isp, BIU_ISR) & BIU2100_ISR_RISC_INT))) {
8763bd40330SMatt Jacob 		*isrp = 0;
877db4fa023SMatt Jacob 		return (0);
8783bd40330SMatt Jacob 	}
879126ec864SMatt Jacob 	r2hisr = bus_space_read_4(pcs->pci_st, pcs->pci_sh,
880126ec864SMatt Jacob 	    IspVirt2Off(pcs, BIU_R2HSTSLO));
881126ec864SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
882126ec864SMatt Jacob 	if ((r2hisr & BIU_R2HST_INTR) == 0) {
883126ec864SMatt Jacob 		*isrp = 0;
884126ec864SMatt Jacob 		return (0);
885126ec864SMatt Jacob 	}
886126ec864SMatt Jacob 	switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
887126ec864SMatt Jacob 	case ISPR2HST_ROM_MBX_OK:
888126ec864SMatt Jacob 	case ISPR2HST_ROM_MBX_FAIL:
889126ec864SMatt Jacob 	case ISPR2HST_MBX_OK:
890126ec864SMatt Jacob 	case ISPR2HST_MBX_FAIL:
891126ec864SMatt Jacob 	case ISPR2HST_ASYNC_EVENT:
89275c1e828SMatt Jacob 	case ISPR2HST_RIO_16:
893126ec864SMatt Jacob 	case ISPR2HST_FPOST:
894126ec864SMatt Jacob 	case ISPR2HST_FPOST_CTIO:
895126ec864SMatt Jacob 		*isrp = r2hisr & 0xffff;
896126ec864SMatt Jacob 		*mbox0p = (r2hisr >> 16);
897126ec864SMatt Jacob 		*semap = 1;
898126ec864SMatt Jacob 		return (1);
899126ec864SMatt Jacob 	case ISPR2HST_RSPQ_UPDATE:
900126ec864SMatt Jacob 		*isrp = r2hisr & 0xffff;
901126ec864SMatt Jacob 		*mbox0p = 0;
902126ec864SMatt Jacob 		*semap = 0;
903126ec864SMatt Jacob 		return (1);
904126ec864SMatt Jacob 	default:
905126ec864SMatt Jacob 		return (0);
906126ec864SMatt Jacob 	}
907126ec864SMatt Jacob }
908126ec864SMatt Jacob 
90965adb54cSMatt Jacob static u_int16_t
910126ec864SMatt Jacob isp_pci_rd_reg(struct ispsoftc *isp, int regoff)
91165adb54cSMatt Jacob {
91265adb54cSMatt Jacob 	u_int16_t rv;
91365adb54cSMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
914126ec864SMatt Jacob 	int oldconf = 0;
91565adb54cSMatt Jacob 
916d59bd469SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
91765adb54cSMatt Jacob 		/*
91865adb54cSMatt Jacob 		 * We will assume that someone has paused the RISC processor.
91965adb54cSMatt Jacob 		 */
920126ec864SMatt Jacob 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
921126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
922126ec864SMatt Jacob 		    oldconf | BIU_PCI_CONF1_SXP);
92365adb54cSMatt Jacob 	}
924126ec864SMatt Jacob 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
925d59bd469SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
926126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
92765adb54cSMatt Jacob 	}
92865adb54cSMatt Jacob 	return (rv);
92965adb54cSMatt Jacob }
93065adb54cSMatt Jacob 
93165adb54cSMatt Jacob static void
932126ec864SMatt Jacob isp_pci_wr_reg(struct ispsoftc *isp, int regoff, u_int16_t val)
93365adb54cSMatt Jacob {
93465adb54cSMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
935126ec864SMatt Jacob 	int oldconf = 0;
936d59bd469SMatt Jacob 
937d59bd469SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
93865adb54cSMatt Jacob 		/*
93965adb54cSMatt Jacob 		 * We will assume that someone has paused the RISC processor.
94065adb54cSMatt Jacob 		 */
941126ec864SMatt Jacob 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
942126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
943126ec864SMatt Jacob 		    oldconf | BIU_PCI_CONF1_SXP);
94465adb54cSMatt Jacob 	}
945126ec864SMatt Jacob 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
946d59bd469SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
947126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
94865adb54cSMatt Jacob 	}
94965adb54cSMatt Jacob }
95065adb54cSMatt Jacob 
951d59bd469SMatt Jacob static u_int16_t
952126ec864SMatt Jacob isp_pci_rd_reg_1080(struct ispsoftc *isp, int regoff)
953d59bd469SMatt Jacob {
95422e1dc85SMatt Jacob 	u_int16_t rv, oc = 0;
955d59bd469SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
956d59bd469SMatt Jacob 
95722e1dc85SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
95822e1dc85SMatt Jacob 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
95922e1dc85SMatt Jacob 		u_int16_t tc;
960d59bd469SMatt Jacob 		/*
961d59bd469SMatt Jacob 		 * We will assume that someone has paused the RISC processor.
962d59bd469SMatt Jacob 		 */
963126ec864SMatt Jacob 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
96422e1dc85SMatt Jacob 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
96522e1dc85SMatt Jacob 		if (regoff & SXP_BANK1_SELECT)
96622e1dc85SMatt Jacob 			tc |= BIU_PCI1080_CONF1_SXP1;
96722e1dc85SMatt Jacob 		else
96822e1dc85SMatt Jacob 			tc |= BIU_PCI1080_CONF1_SXP0;
969126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
970d59bd469SMatt Jacob 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
971126ec864SMatt Jacob 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
972126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
973126ec864SMatt Jacob 		    oc | BIU_PCI1080_CONF1_DMA);
974d59bd469SMatt Jacob 	}
975126ec864SMatt Jacob 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
97622e1dc85SMatt Jacob 	if (oc) {
977126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
978d59bd469SMatt Jacob 	}
979d59bd469SMatt Jacob 	return (rv);
980d59bd469SMatt Jacob }
981d59bd469SMatt Jacob 
982d59bd469SMatt Jacob static void
983126ec864SMatt Jacob isp_pci_wr_reg_1080(struct ispsoftc *isp, int regoff, u_int16_t val)
984d59bd469SMatt Jacob {
985d59bd469SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
986126ec864SMatt Jacob 	int oc = 0;
987d59bd469SMatt Jacob 
98822e1dc85SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
98922e1dc85SMatt Jacob 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
99022e1dc85SMatt Jacob 		u_int16_t tc;
991d59bd469SMatt Jacob 		/*
992d59bd469SMatt Jacob 		 * We will assume that someone has paused the RISC processor.
993d59bd469SMatt Jacob 		 */
994126ec864SMatt Jacob 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
99522e1dc85SMatt Jacob 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
99622e1dc85SMatt Jacob 		if (regoff & SXP_BANK1_SELECT)
99722e1dc85SMatt Jacob 			tc |= BIU_PCI1080_CONF1_SXP1;
99822e1dc85SMatt Jacob 		else
99922e1dc85SMatt Jacob 			tc |= BIU_PCI1080_CONF1_SXP0;
1000126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
1001d59bd469SMatt Jacob 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1002126ec864SMatt Jacob 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1003126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1004126ec864SMatt Jacob 		    oc | BIU_PCI1080_CONF1_DMA);
1005d59bd469SMatt Jacob 	}
1006126ec864SMatt Jacob 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
100722e1dc85SMatt Jacob 	if (oc) {
1008126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
1009d59bd469SMatt Jacob 	}
1010d59bd469SMatt Jacob }
1011d59bd469SMatt Jacob 
1012d720e6d5SJustin T. Gibbs 
1013222bb542SMatt Jacob struct imush {
1014222bb542SMatt Jacob 	struct ispsoftc *isp;
1015222bb542SMatt Jacob 	int error;
1016222bb542SMatt Jacob };
1017222bb542SMatt Jacob 
10181923f739SMatt Jacob static void imc(void *, bus_dma_segment_t *, int, int);
10191923f739SMatt Jacob 
1020d720e6d5SJustin T. Gibbs static void
10211923f739SMatt Jacob imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1022d720e6d5SJustin T. Gibbs {
1023222bb542SMatt Jacob 	struct imush *imushp = (struct imush *) arg;
1024222bb542SMatt Jacob 	if (error) {
1025222bb542SMatt Jacob 		imushp->error = error;
1026222bb542SMatt Jacob 	} else {
10271923f739SMatt Jacob 		struct ispsoftc *isp =imushp->isp;
10281923f739SMatt Jacob 		bus_addr_t addr = segs->ds_addr;
10291923f739SMatt Jacob 
10301923f739SMatt Jacob 		isp->isp_rquest_dma = addr;
10311923f739SMatt Jacob 		addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
10321923f739SMatt Jacob 		isp->isp_result_dma = addr;
10331923f739SMatt Jacob 		if (IS_FC(isp)) {
10341923f739SMatt Jacob 			addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
10351923f739SMatt Jacob 			FCPARAM(isp)->isp_scdma = addr;
10361923f739SMatt Jacob 		}
1037222bb542SMatt Jacob 	}
1038d720e6d5SJustin T. Gibbs }
1039d720e6d5SJustin T. Gibbs 
1040029f13c6SMatt Jacob /*
1041029f13c6SMatt Jacob  * Should be BUS_SPACE_MAXSIZE, but MAXPHYS is larger than BUS_SPACE_MAXSIZE
1042029f13c6SMatt Jacob  */
1043029f13c6SMatt Jacob #define ISP_NSEGS ((MAXPHYS / PAGE_SIZE) + 1)
1044d720e6d5SJustin T. Gibbs 
1045d720e6d5SJustin T. Gibbs static int
104617e318c6SMatt Jacob isp_pci_mbxdma(struct ispsoftc *isp)
1047d720e6d5SJustin T. Gibbs {
10481923f739SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1049d720e6d5SJustin T. Gibbs 	caddr_t base;
1050d720e6d5SJustin T. Gibbs 	u_int32_t len;
10511923f739SMatt Jacob 	int i, error, ns;
10521923f739SMatt Jacob 	bus_size_t bl;
1053222bb542SMatt Jacob 	struct imush im;
1054222bb542SMatt Jacob 
1055a95ae193SMatt Jacob 	/*
1056a95ae193SMatt Jacob 	 * Already been here? If so, leave...
1057a95ae193SMatt Jacob 	 */
1058a95ae193SMatt Jacob 	if (isp->isp_rquest) {
1059a95ae193SMatt Jacob 		return (0);
1060a95ae193SMatt Jacob 	}
1061a95ae193SMatt Jacob 
10621923f739SMatt Jacob 	if (IS_ULTRA2(isp) || IS_FC(isp) || IS_1240(isp)) {
10631923f739SMatt Jacob 		bl = BUS_SPACE_UNRESTRICTED;
10641923f739SMatt Jacob 	} else {
10651923f739SMatt Jacob 		bl = BUS_SPACE_MAXADDR_24BIT;
10661923f739SMatt Jacob 	}
10671923f739SMatt Jacob 
10681923f739SMatt Jacob 	if (bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR,
10691923f739SMatt Jacob 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE,
10701923f739SMatt Jacob 	    ISP_NSEGS, bl, 0, &pcs->dmat)) {
10711923f739SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
10721923f739SMatt Jacob 		return(1);
10731923f739SMatt Jacob 	}
10741923f739SMatt Jacob 
10751923f739SMatt Jacob 
1076d02373f1SMatt Jacob 	len = sizeof (XS_T **) * isp->isp_maxcmds;
10777cc0979fSDavid Malone 	isp->isp_xflist = (XS_T **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1078a95ae193SMatt Jacob 	if (isp->isp_xflist == NULL) {
1079d02373f1SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
1080a95ae193SMatt Jacob 		return (1);
1081a95ae193SMatt Jacob 	}
1082a95ae193SMatt Jacob 	len = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
10831923f739SMatt Jacob 	pcs->dmaps = (bus_dmamap_t *) malloc(len, M_DEVBUF,  M_WAITOK);
10841923f739SMatt Jacob 	if (pcs->dmaps == NULL) {
10851923f739SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "can't alloc dma map storage");
1086a95ae193SMatt Jacob 		free(isp->isp_xflist, M_DEVBUF);
1087a95ae193SMatt Jacob 		return (1);
1088a95ae193SMatt Jacob 	}
1089a95ae193SMatt Jacob 
1090d720e6d5SJustin T. Gibbs 	/*
1091d720e6d5SJustin T. Gibbs 	 * Allocate and map the request, result queues, plus FC scratch area.
1092d720e6d5SJustin T. Gibbs 	 */
1093d02373f1SMatt Jacob 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1094d02373f1SMatt Jacob 	len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1095222bb542SMatt Jacob 	if (IS_FC(isp)) {
1096d720e6d5SJustin T. Gibbs 		len += ISP2100_SCRLEN;
1097d720e6d5SJustin T. Gibbs 	}
10981923f739SMatt Jacob 
10991923f739SMatt Jacob 	ns = (len / PAGE_SIZE) + 1;
11001923f739SMatt Jacob 	if (bus_dma_tag_create(pcs->dmat, QENTRY_LEN, 0, BUS_SPACE_MAXADDR,
11011923f739SMatt Jacob 	    BUS_SPACE_MAXADDR, NULL, NULL, len, ns, bl, 0, &isp->isp_cdmat)) {
1102f7dddf8aSMatt Jacob 		isp_prt(isp, ISP_LOGERR,
1103f7dddf8aSMatt Jacob 		    "cannot create a dma tag for control spaces");
11041923f739SMatt Jacob 		free(pcs->dmaps, M_DEVBUF);
1105a95ae193SMatt Jacob 		free(isp->isp_xflist, M_DEVBUF);
1106d720e6d5SJustin T. Gibbs 		return (1);
1107d720e6d5SJustin T. Gibbs 	}
1108d720e6d5SJustin T. Gibbs 
11091923f739SMatt Jacob 	if (bus_dmamem_alloc(isp->isp_cdmat, (void **)&base, BUS_DMA_NOWAIT,
11101923f739SMatt Jacob 	    &isp->isp_cdmap) != 0) {
1111f7dddf8aSMatt Jacob 		isp_prt(isp, ISP_LOGERR,
11121923f739SMatt Jacob 		    "cannot allocate %d bytes of CCB memory", len);
11131923f739SMatt Jacob 		bus_dma_tag_destroy(isp->isp_cdmat);
1114a95ae193SMatt Jacob 		free(isp->isp_xflist, M_DEVBUF);
11151923f739SMatt Jacob 		free(pcs->dmaps, M_DEVBUF);
1116222bb542SMatt Jacob 		return (1);
1117222bb542SMatt Jacob 	}
1118d720e6d5SJustin T. Gibbs 
1119a95ae193SMatt Jacob 	for (i = 0; i < isp->isp_maxcmds; i++) {
11201923f739SMatt Jacob 		error = bus_dmamap_create(pcs->dmat, 0, &pcs->dmaps[i]);
1121d720e6d5SJustin T. Gibbs 		if (error) {
1122f7dddf8aSMatt Jacob 			isp_prt(isp, ISP_LOGERR,
1123f7dddf8aSMatt Jacob 			    "error %d creating per-cmd DMA maps", error);
11241923f739SMatt Jacob 			while (--i >= 0) {
11251923f739SMatt Jacob 				bus_dmamap_destroy(pcs->dmat, pcs->dmaps[i]);
11261923f739SMatt Jacob 			}
11271923f739SMatt Jacob 			goto bad;
1128d720e6d5SJustin T. Gibbs 		}
1129d720e6d5SJustin T. Gibbs 	}
1130a95ae193SMatt Jacob 
11311923f739SMatt Jacob 	im.isp = isp;
1132222bb542SMatt Jacob 	im.error = 0;
11331923f739SMatt Jacob 	bus_dmamap_load(isp->isp_cdmat, isp->isp_cdmap, base, len, imc, &im, 0);
1134222bb542SMatt Jacob 	if (im.error) {
1135f7dddf8aSMatt Jacob 		isp_prt(isp, ISP_LOGERR,
11361923f739SMatt Jacob 		    "error %d loading dma map for control areas", im.error);
11371923f739SMatt Jacob 		goto bad;
1138222bb542SMatt Jacob 	}
11391923f739SMatt Jacob 
11401923f739SMatt Jacob 	isp->isp_rquest = base;
11411923f739SMatt Jacob 	base += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
11421923f739SMatt Jacob 	isp->isp_result = base;
11431923f739SMatt Jacob 	if (IS_FC(isp)) {
11441923f739SMatt Jacob 		base += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
11451923f739SMatt Jacob 		FCPARAM(isp)->isp_scratch = base;
114692c49d78SMatt Jacob 	}
1147d720e6d5SJustin T. Gibbs 	return (0);
11481923f739SMatt Jacob 
11491923f739SMatt Jacob bad:
11501923f739SMatt Jacob 	bus_dmamem_free(isp->isp_cdmat, base, isp->isp_cdmap);
11511923f739SMatt Jacob 	bus_dma_tag_destroy(isp->isp_cdmat);
11521923f739SMatt Jacob 	free(isp->isp_xflist, M_DEVBUF);
11531923f739SMatt Jacob 	free(pcs->dmaps, M_DEVBUF);
11541923f739SMatt Jacob 	isp->isp_rquest = NULL;
11551923f739SMatt Jacob 	return (1);
1156d720e6d5SJustin T. Gibbs }
1157d720e6d5SJustin T. Gibbs 
1158d720e6d5SJustin T. Gibbs typedef struct {
1159d720e6d5SJustin T. Gibbs 	struct ispsoftc *isp;
11609e11e5beSMatt Jacob 	void *cmd_token;
11619e11e5beSMatt Jacob 	void *rq;
11624fd13c1bSMatt Jacob 	u_int16_t *nxtip;
11639637d68cSMatt Jacob 	u_int16_t optr;
1164d720e6d5SJustin T. Gibbs 	u_int error;
1165d720e6d5SJustin T. Gibbs } mush_t;
1166d720e6d5SJustin T. Gibbs 
11674873663cSMatt Jacob #define	MUSHERR_NOQENTRIES	-2
11684873663cSMatt Jacob 
11699e11e5beSMatt Jacob #ifdef	ISP_TARGET_MODE
11709e11e5beSMatt Jacob /*
11719e11e5beSMatt Jacob  * We need to handle DMA for target mode differently from initiator mode.
11729e11e5beSMatt Jacob  *
11739e11e5beSMatt Jacob  * DMA mapping and construction and submission of CTIO Request Entries
11749e11e5beSMatt Jacob  * and rendevous for completion are very tightly coupled because we start
11759e11e5beSMatt Jacob  * out by knowing (per platform) how much data we have to move, but we
11769e11e5beSMatt Jacob  * don't know, up front, how many DMA mapping segments will have to be used
11779e11e5beSMatt Jacob  * cover that data, so we don't know how many CTIO Request Entries we
11789e11e5beSMatt Jacob  * will end up using. Further, for performance reasons we may want to
11799e11e5beSMatt Jacob  * (on the last CTIO for Fibre Channel), send status too (if all went well).
11809e11e5beSMatt Jacob  *
11819e11e5beSMatt Jacob  * The standard vector still goes through isp_pci_dmasetup, but the callback
11829e11e5beSMatt Jacob  * for the DMA mapping routines comes here instead with the whole transfer
11839e11e5beSMatt Jacob  * mapped and a pointer to a partially filled in already allocated request
11849e11e5beSMatt Jacob  * queue entry. We finish the job.
11859e11e5beSMatt Jacob  */
1186e9a2738aSMatt Jacob static void tdma_mk(void *, bus_dma_segment_t *, int, int);
1187e9a2738aSMatt Jacob static void tdma_mkfc(void *, bus_dma_segment_t *, int, int);
1188e9a2738aSMatt Jacob 
1189e9a2738aSMatt Jacob #define	STATUS_WITH_DATA	1
11909e11e5beSMatt Jacob 
1191d720e6d5SJustin T. Gibbs static void
119205fbcbb0SMatt Jacob tdma_mk(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1193d720e6d5SJustin T. Gibbs {
1194d720e6d5SJustin T. Gibbs 	mush_t *mp;
11959e11e5beSMatt Jacob 	struct ccb_scsiio *csio;
11964fd13c1bSMatt Jacob 	struct ispsoftc *isp;
11971923f739SMatt Jacob 	struct isp_pcisoftc *pcs;
1198d720e6d5SJustin T. Gibbs 	bus_dmamap_t *dp;
11994fd13c1bSMatt Jacob 	ct_entry_t *cto, *qe;
120005fbcbb0SMatt Jacob 	u_int8_t scsi_status;
12014fd13c1bSMatt Jacob 	u_int16_t curi, nxti, handle;
1202b96934e8SMatt Jacob 	u_int32_t sflags;
120305fbcbb0SMatt Jacob 	int32_t resid;
12044fd13c1bSMatt Jacob 	int nth_ctio, nctios, send_status;
1205d720e6d5SJustin T. Gibbs 
1206d720e6d5SJustin T. Gibbs 	mp = (mush_t *) arg;
1207d720e6d5SJustin T. Gibbs 	if (error) {
1208d720e6d5SJustin T. Gibbs 		mp->error = error;
1209d720e6d5SJustin T. Gibbs 		return;
1210d720e6d5SJustin T. Gibbs 	}
12114fd13c1bSMatt Jacob 
12124fd13c1bSMatt Jacob 	isp = mp->isp;
12139e11e5beSMatt Jacob 	csio = mp->cmd_token;
12149e11e5beSMatt Jacob 	cto = mp->rq;
12154fd13c1bSMatt Jacob 	curi = isp->isp_reqidx;
12164fd13c1bSMatt Jacob 	qe = (ct_entry_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, curi);
12179e11e5beSMatt Jacob 
121865b024e1SMatt Jacob 	cto->ct_xfrlen = 0;
121965b024e1SMatt Jacob 	cto->ct_seg_count = 0;
122065b024e1SMatt Jacob 	cto->ct_header.rqs_entry_count = 1;
122105fbcbb0SMatt Jacob 	MEMZERO(cto->ct_dataseg, sizeof(cto->ct_dataseg));
122205fbcbb0SMatt Jacob 
122305fbcbb0SMatt Jacob 	if (nseg == 0) {
122405fbcbb0SMatt Jacob 		cto->ct_header.rqs_seqno = 1;
12254fd13c1bSMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG1,
1226e9a2738aSMatt Jacob 		    "CTIO[%x] lun%d iid%d tag %x flgs %x sts %x ssts %x res %d",
12275f5aafe1SMatt Jacob 		    cto->ct_fwhandle, csio->ccb_h.target_lun, cto->ct_iid,
1228e9a2738aSMatt Jacob 		    cto->ct_tag_val, cto->ct_flags, cto->ct_status,
1229e9a2738aSMatt Jacob 		    cto->ct_scsi_status, cto->ct_resid);
12304fd13c1bSMatt Jacob 		ISP_TDQE(isp, "tdma_mk[no data]", curi, cto);
12314fd13c1bSMatt Jacob 		isp_put_ctio(isp, cto, qe);
123265b024e1SMatt Jacob 		return;
123365b024e1SMatt Jacob 	}
123465b024e1SMatt Jacob 
123565b024e1SMatt Jacob 	nctios = nseg / ISP_RQDSEG;
123665b024e1SMatt Jacob 	if (nseg % ISP_RQDSEG) {
123765b024e1SMatt Jacob 		nctios++;
123865b024e1SMatt Jacob 	}
123965b024e1SMatt Jacob 
124005fbcbb0SMatt Jacob 	/*
12415f5aafe1SMatt Jacob 	 * Save syshandle, and potentially any SCSI status, which we'll
12425f5aafe1SMatt Jacob 	 * reinsert on the last CTIO we're going to send.
124305fbcbb0SMatt Jacob 	 */
12444fd13c1bSMatt Jacob 
12455f5aafe1SMatt Jacob 	handle = cto->ct_syshandle;
12465f5aafe1SMatt Jacob 	cto->ct_syshandle = 0;
124705fbcbb0SMatt Jacob 	cto->ct_header.rqs_seqno = 0;
124805fbcbb0SMatt Jacob 	send_status = (cto->ct_flags & CT_SENDSTATUS) != 0;
124905fbcbb0SMatt Jacob 
125005fbcbb0SMatt Jacob 	if (send_status) {
125105fbcbb0SMatt Jacob 		sflags = cto->ct_flags & (CT_SENDSTATUS | CT_CCINCR);
125205fbcbb0SMatt Jacob 		cto->ct_flags &= ~(CT_SENDSTATUS | CT_CCINCR);
125305fbcbb0SMatt Jacob 		/*
125405fbcbb0SMatt Jacob 		 * Preserve residual.
125505fbcbb0SMatt Jacob 		 */
125605fbcbb0SMatt Jacob 		resid = cto->ct_resid;
125705fbcbb0SMatt Jacob 
125805fbcbb0SMatt Jacob 		/*
125905fbcbb0SMatt Jacob 		 * Save actual SCSI status.
126005fbcbb0SMatt Jacob 		 */
126105fbcbb0SMatt Jacob 		scsi_status = cto->ct_scsi_status;
126205fbcbb0SMatt Jacob 
1263e9a2738aSMatt Jacob #ifndef	STATUS_WITH_DATA
1264e9a2738aSMatt Jacob 		sflags |= CT_NO_DATA;
126505fbcbb0SMatt Jacob 		/*
126605fbcbb0SMatt Jacob 		 * We can't do a status at the same time as a data CTIO, so
126705fbcbb0SMatt Jacob 		 * we need to synthesize an extra CTIO at this level.
126805fbcbb0SMatt Jacob 		 */
126905fbcbb0SMatt Jacob 		nctios++;
1270b72b1569SMatt Jacob #endif
127105fbcbb0SMatt Jacob 	} else {
127205fbcbb0SMatt Jacob 		sflags = scsi_status = resid = 0;
127305fbcbb0SMatt Jacob 	}
127405fbcbb0SMatt Jacob 
1275b96934e8SMatt Jacob 	cto->ct_resid = 0;
127605fbcbb0SMatt Jacob 	cto->ct_scsi_status = 0;
127705fbcbb0SMatt Jacob 
12781923f739SMatt Jacob 	pcs = (struct isp_pcisoftc *)isp;
12791923f739SMatt Jacob 	dp = &pcs->dmaps[isp_handle_index(handle)];
12809e11e5beSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
12811923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
1282d720e6d5SJustin T. Gibbs 	} else {
12831923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
1284d720e6d5SJustin T. Gibbs 	}
1285d720e6d5SJustin T. Gibbs 
12864fd13c1bSMatt Jacob 	nxti = *mp->nxtip;
12879e11e5beSMatt Jacob 
12884fd13c1bSMatt Jacob 	for (nth_ctio = 0; nth_ctio < nctios; nth_ctio++) {
128905fbcbb0SMatt Jacob 		int seglim;
12909e11e5beSMatt Jacob 
12919e11e5beSMatt Jacob 		seglim = nseg;
129205fbcbb0SMatt Jacob 		if (seglim) {
129305fbcbb0SMatt Jacob 			int seg;
129405fbcbb0SMatt Jacob 
12959e11e5beSMatt Jacob 			if (seglim > ISP_RQDSEG)
12969e11e5beSMatt Jacob 				seglim = ISP_RQDSEG;
12979e11e5beSMatt Jacob 
129805fbcbb0SMatt Jacob 			for (seg = 0; seg < seglim; seg++, nseg--) {
129905fbcbb0SMatt Jacob 				/*
130005fbcbb0SMatt Jacob 				 * Unlike normal initiator commands, we don't
130105fbcbb0SMatt Jacob 				 * do any swizzling here.
130205fbcbb0SMatt Jacob 				 */
13039e11e5beSMatt Jacob 				cto->ct_dataseg[seg].ds_count = dm_segs->ds_len;
130405fbcbb0SMatt Jacob 				cto->ct_dataseg[seg].ds_base = dm_segs->ds_addr;
13059e11e5beSMatt Jacob 				cto->ct_xfrlen += dm_segs->ds_len;
13069e11e5beSMatt Jacob 				dm_segs++;
13079e11e5beSMatt Jacob 			}
13089e11e5beSMatt Jacob 			cto->ct_seg_count = seg;
13099e11e5beSMatt Jacob 		} else {
131005fbcbb0SMatt Jacob 			/*
131105fbcbb0SMatt Jacob 			 * This case should only happen when we're sending an
131205fbcbb0SMatt Jacob 			 * extra CTIO with final status.
131305fbcbb0SMatt Jacob 			 */
131405fbcbb0SMatt Jacob 			if (send_status == 0) {
13154fd13c1bSMatt Jacob 				isp_prt(isp, ISP_LOGWARN,
1316f7dddf8aSMatt Jacob 				    "tdma_mk ran out of segments");
131705fbcbb0SMatt Jacob 				mp->error = EINVAL;
131805fbcbb0SMatt Jacob 				return;
13199e11e5beSMatt Jacob 			}
132005fbcbb0SMatt Jacob 		}
132105fbcbb0SMatt Jacob 
132205fbcbb0SMatt Jacob 		/*
132305fbcbb0SMatt Jacob 		 * At this point, the fields ct_lun, ct_iid, ct_tagval,
132405fbcbb0SMatt Jacob 		 * ct_tagtype, and ct_timeout have been carried over
132505fbcbb0SMatt Jacob 		 * unchanged from what our caller had set.
132605fbcbb0SMatt Jacob 		 *
132705fbcbb0SMatt Jacob 		 * The dataseg fields and the seg_count fields we just got
132805fbcbb0SMatt Jacob 		 * through setting. The data direction we've preserved all
132905fbcbb0SMatt Jacob 		 * along and only clear it if we're now sending status.
133005fbcbb0SMatt Jacob 		 */
13319e11e5beSMatt Jacob 
13324fd13c1bSMatt Jacob 		if (nth_ctio == nctios - 1) {
13339e11e5beSMatt Jacob 			/*
133405fbcbb0SMatt Jacob 			 * We're the last in a sequence of CTIOs, so mark
133505fbcbb0SMatt Jacob 			 * this CTIO and save the handle to the CCB such that
133605fbcbb0SMatt Jacob 			 * when this CTIO completes we can free dma resources
133705fbcbb0SMatt Jacob 			 * and do whatever else we need to do to finish the
1338e9a2738aSMatt Jacob 			 * rest of the command. We *don't* give this to the
1339e9a2738aSMatt Jacob 			 * firmware to work on- the caller will do that.
13409e11e5beSMatt Jacob 			 */
13414fd13c1bSMatt Jacob 
13425f5aafe1SMatt Jacob 			cto->ct_syshandle = handle;
134305fbcbb0SMatt Jacob 			cto->ct_header.rqs_seqno = 1;
134405fbcbb0SMatt Jacob 
134505fbcbb0SMatt Jacob 			if (send_status) {
13469e11e5beSMatt Jacob 				cto->ct_scsi_status = scsi_status;
1347b72b1569SMatt Jacob 				cto->ct_flags |= sflags;
134805fbcbb0SMatt Jacob 				cto->ct_resid = resid;
134942426921SMatt Jacob 			}
1350d02373f1SMatt Jacob 			if (send_status) {
13514fd13c1bSMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG1,
1352e9a2738aSMatt Jacob 				    "CTIO[%x] lun%d iid %d tag %x ct_flags %x "
13535f5aafe1SMatt Jacob 				    "scsi status %x resid %d",
13545f5aafe1SMatt Jacob 				    cto->ct_fwhandle, csio->ccb_h.target_lun,
1355e9a2738aSMatt Jacob 				    cto->ct_iid, cto->ct_tag_val, cto->ct_flags,
135605fbcbb0SMatt Jacob 				    cto->ct_scsi_status, cto->ct_resid);
1357d02373f1SMatt Jacob 			} else {
13584fd13c1bSMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG1,
1359e9a2738aSMatt Jacob 				    "CTIO[%x] lun%d iid%d tag %x ct_flags 0x%x",
13605f5aafe1SMatt Jacob 				    cto->ct_fwhandle, csio->ccb_h.target_lun,
1361e9a2738aSMatt Jacob 				    cto->ct_iid, cto->ct_tag_val,
1362e9a2738aSMatt Jacob 				    cto->ct_flags);
136305fbcbb0SMatt Jacob 			}
13644fd13c1bSMatt Jacob 			isp_put_ctio(isp, cto, qe);
13654fd13c1bSMatt Jacob 			ISP_TDQE(isp, "last tdma_mk", curi, cto);
13664fd13c1bSMatt Jacob 			if (nctios > 1) {
13674fd13c1bSMatt Jacob 				MEMORYBARRIER(isp, SYNC_REQUEST,
13684fd13c1bSMatt Jacob 				    curi, QENTRY_LEN);
13694fd13c1bSMatt Jacob 			}
13709e11e5beSMatt Jacob 		} else {
13714fd13c1bSMatt Jacob 			ct_entry_t *oqe = qe;
137205fbcbb0SMatt Jacob 
137305fbcbb0SMatt Jacob 			/*
13745f5aafe1SMatt Jacob 			 * Make sure syshandle fields are clean
137505fbcbb0SMatt Jacob 			 */
13765f5aafe1SMatt Jacob 			cto->ct_syshandle = 0;
13779e11e5beSMatt Jacob 			cto->ct_header.rqs_seqno = 0;
137805fbcbb0SMatt Jacob 
13794fd13c1bSMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG1,
13805f5aafe1SMatt Jacob 			    "CTIO[%x] lun%d for ID%d ct_flags 0x%x",
13815f5aafe1SMatt Jacob 			    cto->ct_fwhandle, csio->ccb_h.target_lun,
13825f5aafe1SMatt Jacob 			    cto->ct_iid, cto->ct_flags);
138305fbcbb0SMatt Jacob 
138405fbcbb0SMatt Jacob 			/*
138505fbcbb0SMatt Jacob 			 * Get a new CTIO
138605fbcbb0SMatt Jacob 			 */
13874fd13c1bSMatt Jacob 			qe = (ct_entry_t *)
13884fd13c1bSMatt Jacob 			    ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
13894fd13c1bSMatt Jacob 			nxti = ISP_NXT_QENTRY(nxti, RQUEST_QUEUE_LEN(isp));
13904fd13c1bSMatt Jacob 			if (nxti == mp->optr) {
13914fd13c1bSMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG0,
1392f7dddf8aSMatt Jacob 				    "Queue Overflow in tdma_mk");
13939e11e5beSMatt Jacob 				mp->error = MUSHERR_NOQENTRIES;
13949e11e5beSMatt Jacob 				return;
13959e11e5beSMatt Jacob 			}
13964fd13c1bSMatt Jacob 
13979e11e5beSMatt Jacob 			/*
13984fd13c1bSMatt Jacob 			 * Now that we're done with the old CTIO,
13994fd13c1bSMatt Jacob 			 * flush it out to the request queue.
14004fd13c1bSMatt Jacob 			 */
14014fd13c1bSMatt Jacob 			ISP_TDQE(isp, "dma_tgt_fc", curi, cto);
14024fd13c1bSMatt Jacob 			isp_put_ctio(isp, cto, oqe);
14034fd13c1bSMatt Jacob 			if (nth_ctio != 0) {
14044fd13c1bSMatt Jacob 				MEMORYBARRIER(isp, SYNC_REQUEST, curi,
14054fd13c1bSMatt Jacob 				    QENTRY_LEN);
14064fd13c1bSMatt Jacob 			}
14074fd13c1bSMatt Jacob 			curi = ISP_NXT_QENTRY(curi, RQUEST_QUEUE_LEN(isp));
14084fd13c1bSMatt Jacob 
14094fd13c1bSMatt Jacob 			/*
14104fd13c1bSMatt Jacob 			 * Reset some fields in the CTIO so we can reuse
14114fd13c1bSMatt Jacob 			 * for the next one we'll flush to the request
14124fd13c1bSMatt Jacob 			 * queue.
14139e11e5beSMatt Jacob 			 */
14149e11e5beSMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
14159e11e5beSMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
14169e11e5beSMatt Jacob 			cto->ct_header.rqs_flags = 0;
14179e11e5beSMatt Jacob 			cto->ct_status = 0;
14189e11e5beSMatt Jacob 			cto->ct_scsi_status = 0;
14199e11e5beSMatt Jacob 			cto->ct_xfrlen = 0;
14209e11e5beSMatt Jacob 			cto->ct_resid = 0;
14219e11e5beSMatt Jacob 			cto->ct_seg_count = 0;
142205fbcbb0SMatt Jacob 			MEMZERO(cto->ct_dataseg, sizeof(cto->ct_dataseg));
14239e11e5beSMatt Jacob 		}
14249e11e5beSMatt Jacob 	}
14254fd13c1bSMatt Jacob 	*mp->nxtip = nxti;
14269e11e5beSMatt Jacob }
14279e11e5beSMatt Jacob 
1428fc087171SMatt Jacob /*
1429fc087171SMatt Jacob  * We don't have to do multiple CTIOs here. Instead, we can just do
1430fc087171SMatt Jacob  * continuation segments as needed. This greatly simplifies the code
1431fc087171SMatt Jacob  * improves performance.
1432fc087171SMatt Jacob  */
1433fc087171SMatt Jacob 
14349e11e5beSMatt Jacob static void
143505fbcbb0SMatt Jacob tdma_mkfc(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
14369e11e5beSMatt Jacob {
14379e11e5beSMatt Jacob 	mush_t *mp;
14389e11e5beSMatt Jacob 	struct ccb_scsiio *csio;
14394fd13c1bSMatt Jacob 	struct ispsoftc *isp;
14404fd13c1bSMatt Jacob 	ct2_entry_t *cto, *qe;
14414fd13c1bSMatt Jacob 	u_int16_t curi, nxti;
1442fc087171SMatt Jacob 	int segcnt;
14439e11e5beSMatt Jacob 
14449e11e5beSMatt Jacob 	mp = (mush_t *) arg;
14459e11e5beSMatt Jacob 	if (error) {
14469e11e5beSMatt Jacob 		mp->error = error;
14479e11e5beSMatt Jacob 		return;
14489e11e5beSMatt Jacob 	}
14499e11e5beSMatt Jacob 
14504fd13c1bSMatt Jacob 	isp = mp->isp;
145165b024e1SMatt Jacob 	csio = mp->cmd_token;
145265b024e1SMatt Jacob 	cto = mp->rq;
1453fc087171SMatt Jacob 
14544fd13c1bSMatt Jacob 	curi = isp->isp_reqidx;
14554fd13c1bSMatt Jacob 	qe = (ct2_entry_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, curi);
145665b024e1SMatt Jacob 
145765b024e1SMatt Jacob 	if (nseg == 0) {
145865b024e1SMatt Jacob 		if ((cto->ct_flags & CT2_FLAG_MMASK) != CT2_FLAG_MODE1) {
14594fd13c1bSMatt Jacob 			isp_prt(isp, ISP_LOGWARN,
1460f7dddf8aSMatt Jacob 			    "dma2_tgt_fc, a status CTIO2 without MODE1 "
1461f7dddf8aSMatt Jacob 			    "set (0x%x)", cto->ct_flags);
146265b024e1SMatt Jacob 			mp->error = EINVAL;
146365b024e1SMatt Jacob 			return;
146465b024e1SMatt Jacob 		}
146565b024e1SMatt Jacob 		/*
146665b024e1SMatt Jacob 		 * We preserve ct_lun, ct_iid, ct_rxid. We set the data
146765b024e1SMatt Jacob 		 * flags to NO DATA and clear relative offset flags.
146865b024e1SMatt Jacob 		 * We preserve the ct_resid and the response area.
146965b024e1SMatt Jacob 		 */
1470fc087171SMatt Jacob 		cto->ct_header.rqs_seqno = 1;
147165b024e1SMatt Jacob 		cto->ct_seg_count = 0;
147265b024e1SMatt Jacob 		cto->ct_reloff = 0;
14734fd13c1bSMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG1,
14745f5aafe1SMatt Jacob 		    "CTIO2[%x] lun %d->iid%d flgs 0x%x sts 0x%x ssts "
1475d02373f1SMatt Jacob 		    "0x%x res %d", cto->ct_rxid, csio->ccb_h.target_lun,
1476d02373f1SMatt Jacob 		    cto->ct_iid, cto->ct_flags, cto->ct_status,
147765b024e1SMatt Jacob 		    cto->rsp.m1.ct_scsi_status, cto->ct_resid);
14784fd13c1bSMatt Jacob 		isp_put_ctio2(isp, cto, qe);
14794fd13c1bSMatt Jacob 		ISP_TDQE(isp, "dma2_tgt_fc[no data]", curi, qe);
14809e11e5beSMatt Jacob 		return;
14819e11e5beSMatt Jacob 	}
14829e11e5beSMatt Jacob 
148365b024e1SMatt Jacob 	if ((cto->ct_flags & CT2_FLAG_MMASK) != CT2_FLAG_MODE0) {
1484fc087171SMatt Jacob 		isp_prt(isp, ISP_LOGERR,
1485f7dddf8aSMatt Jacob 		    "dma2_tgt_fc, a data CTIO2 without MODE0 set "
1486f7dddf8aSMatt Jacob 		    "(0x%x)", cto->ct_flags);
148765b024e1SMatt Jacob 		mp->error = EINVAL;
148865b024e1SMatt Jacob 		return;
148965b024e1SMatt Jacob 	}
149065b024e1SMatt Jacob 
149165b024e1SMatt Jacob 
14924fd13c1bSMatt Jacob 	nxti = *mp->nxtip;
14934fd13c1bSMatt Jacob 
1494fc087171SMatt Jacob 	/*
1495fc087171SMatt Jacob 	 * Set up the CTIO2 data segments.
1496fc087171SMatt Jacob 	 */
1497fc087171SMatt Jacob 	for (segcnt = 0; cto->ct_seg_count < ISP_RQDSEG_T2 && segcnt < nseg;
1498fc087171SMatt Jacob 	    cto->ct_seg_count++, segcnt++) {
1499fc087171SMatt Jacob 		cto->rsp.m0.ct_dataseg[cto->ct_seg_count].ds_base =
1500fc087171SMatt Jacob 		    dm_segs[segcnt].ds_addr;
1501fc087171SMatt Jacob 		cto->rsp.m0.ct_dataseg[cto->ct_seg_count].ds_count =
1502fc087171SMatt Jacob 		    dm_segs[segcnt].ds_len;
1503fc087171SMatt Jacob 		cto->rsp.m0.ct_xfrlen += dm_segs[segcnt].ds_len;
1504fc087171SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG1, "isp_send_ctio2: ent0[%d]0x%x:%d",
1505fc087171SMatt Jacob 		    cto->ct_seg_count, dm_segs[segcnt].ds_addr,
1506fc087171SMatt Jacob 		    dm_segs[segcnt].ds_len);
1507fc087171SMatt Jacob 	}
15089e11e5beSMatt Jacob 
1509fc087171SMatt Jacob 	while (segcnt < nseg) {
1510fc087171SMatt Jacob 		u_int16_t curip;
15114fd13c1bSMatt Jacob 		int seg;
1512fc087171SMatt Jacob 		ispcontreq_t local, *crq = &local, *qep;
15139e11e5beSMatt Jacob 
1514fc087171SMatt Jacob 		qep = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
1515fc087171SMatt Jacob 		curip = nxti;
1516fc087171SMatt Jacob 		nxti = ISP_NXT_QENTRY(curip, RQUEST_QUEUE_LEN(isp));
15174fd13c1bSMatt Jacob 		if (nxti == mp->optr) {
1518fc087171SMatt Jacob 			ISP_UNLOCK(isp);
1519fc087171SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0,
1520fc087171SMatt Jacob 			    "tdma_mkfc: request queue overflow");
15219e11e5beSMatt Jacob 			mp->error = MUSHERR_NOQENTRIES;
15229e11e5beSMatt Jacob 			return;
15239e11e5beSMatt Jacob 		}
1524fc087171SMatt Jacob 		cto->ct_header.rqs_entry_count++;
1525fc087171SMatt Jacob 		MEMZERO((void *)crq, sizeof (*crq));
1526fc087171SMatt Jacob 		crq->req_header.rqs_entry_count = 1;
1527fc087171SMatt Jacob 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
1528fc087171SMatt Jacob 		for (seg = 0; segcnt < nseg && seg < ISP_CDSEG;
1529fc087171SMatt Jacob 		    segcnt++, seg++) {
1530fc087171SMatt Jacob 			crq->req_dataseg[seg].ds_base = dm_segs[segcnt].ds_addr;
1531fc087171SMatt Jacob 			crq->req_dataseg[seg].ds_count = dm_segs[segcnt].ds_len;
1532fc087171SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG1,
1533fc087171SMatt Jacob 			    "isp_send_ctio2: ent%d[%d]%x:%u",
1534fc087171SMatt Jacob 			    cto->ct_header.rqs_entry_count-1, seg,
1535fc087171SMatt Jacob 			    dm_segs[segcnt].ds_addr, dm_segs[segcnt].ds_len);
1536fc087171SMatt Jacob 			cto->rsp.m0.ct_xfrlen += dm_segs[segcnt].ds_len;
1537fc087171SMatt Jacob 			cto->ct_seg_count++;
1538fc087171SMatt Jacob 		}
1539fc087171SMatt Jacob 		MEMORYBARRIER(isp, SYNC_REQUEST, curip, QENTRY_LEN);
1540fc087171SMatt Jacob 		isp_put_cont_req(isp, crq, qep);
1541fc087171SMatt Jacob 		ISP_TDQE(isp, "cont entry", curi, qep);
1542fc087171SMatt Jacob 	}
154365b024e1SMatt Jacob 
15449e11e5beSMatt Jacob 	/*
1545fc087171SMatt Jacob 	 * No do final twiddling for the CTIO itself.
15464fd13c1bSMatt Jacob 	 */
1547fc087171SMatt Jacob 	cto->ct_header.rqs_seqno = 1;
1548fc087171SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG1,
1549fc087171SMatt Jacob 	    "CTIO2[%x] lun %d->iid%d flgs 0x%x sts 0x%x ssts 0x%x resid %d",
1550fc087171SMatt Jacob 	    cto->ct_rxid, csio->ccb_h.target_lun, (int) cto->ct_iid,
1551fc087171SMatt Jacob 	    cto->ct_flags, cto->ct_status, cto->rsp.m1.ct_scsi_status,
1552fc087171SMatt Jacob 	    cto->ct_resid);
1553fc087171SMatt Jacob 	isp_put_ctio2(isp, cto, qe);
1554fc087171SMatt Jacob 	ISP_TDQE(isp, "last dma2_tgt_fc", curi, qe);
15554fd13c1bSMatt Jacob 	*mp->nxtip = nxti;
15569e11e5beSMatt Jacob }
15579e11e5beSMatt Jacob #endif
15589e11e5beSMatt Jacob 
1559126ec864SMatt Jacob static void dma2(void *, bus_dma_segment_t *, int, int);
15609e11e5beSMatt Jacob 
15619e11e5beSMatt Jacob static void
15629e11e5beSMatt Jacob dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
15639e11e5beSMatt Jacob {
15649e11e5beSMatt Jacob 	mush_t *mp;
15654fd13c1bSMatt Jacob 	struct ispsoftc *isp;
15669e11e5beSMatt Jacob 	struct ccb_scsiio *csio;
15671923f739SMatt Jacob 	struct isp_pcisoftc *pcs;
15689e11e5beSMatt Jacob 	bus_dmamap_t *dp;
15699e11e5beSMatt Jacob 	bus_dma_segment_t *eseg;
15709e11e5beSMatt Jacob 	ispreq_t *rq;
15719e11e5beSMatt Jacob 	int seglim, datalen;
15724fd13c1bSMatt Jacob 	u_int16_t nxti;
15739e11e5beSMatt Jacob 
15749e11e5beSMatt Jacob 	mp = (mush_t *) arg;
15759e11e5beSMatt Jacob 	if (error) {
15769e11e5beSMatt Jacob 		mp->error = error;
15779e11e5beSMatt Jacob 		return;
15789e11e5beSMatt Jacob 	}
15799e11e5beSMatt Jacob 
15809e11e5beSMatt Jacob 	if (nseg < 1) {
1581f7dddf8aSMatt Jacob 		isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
15829e11e5beSMatt Jacob 		mp->error = EFAULT;
15839e11e5beSMatt Jacob 		return;
15849e11e5beSMatt Jacob 	}
15859e11e5beSMatt Jacob 	csio = mp->cmd_token;
15864fd13c1bSMatt Jacob 	isp = mp->isp;
15879e11e5beSMatt Jacob 	rq = mp->rq;
15881923f739SMatt Jacob 	pcs = (struct isp_pcisoftc *)mp->isp;
15891923f739SMatt Jacob 	dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
15904fd13c1bSMatt Jacob 	nxti = *mp->nxtip;
15919e11e5beSMatt Jacob 
15929e11e5beSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
15931923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
15949e11e5beSMatt Jacob 	} else {
15951923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
15969e11e5beSMatt Jacob 	}
15979e11e5beSMatt Jacob 
15989e11e5beSMatt Jacob 	datalen = XS_XFRLEN(csio);
15999e11e5beSMatt Jacob 
16009e11e5beSMatt Jacob 	/*
16019e11e5beSMatt Jacob 	 * We're passed an initial partially filled in entry that
16029e11e5beSMatt Jacob 	 * has most fields filled in except for data transfer
16039e11e5beSMatt Jacob 	 * related values.
16049e11e5beSMatt Jacob 	 *
16059e11e5beSMatt Jacob 	 * Our job is to fill in the initial request queue entry and
16069e11e5beSMatt Jacob 	 * then to start allocating and filling in continuation entries
16079e11e5beSMatt Jacob 	 * until we've covered the entire transfer.
16089e11e5beSMatt Jacob 	 */
16099e11e5beSMatt Jacob 
16104fd13c1bSMatt Jacob 	if (IS_FC(isp)) {
1611d720e6d5SJustin T. Gibbs 		seglim = ISP_RQDSEG_T2;
1612d720e6d5SJustin T. Gibbs 		((ispreqt2_t *)rq)->req_totalcnt = datalen;
16139e11e5beSMatt Jacob 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
16149e11e5beSMatt Jacob 			((ispreqt2_t *)rq)->req_flags |= REQFLAG_DATA_IN;
16159e11e5beSMatt Jacob 		} else {
16169e11e5beSMatt Jacob 			((ispreqt2_t *)rq)->req_flags |= REQFLAG_DATA_OUT;
16179e11e5beSMatt Jacob 		}
1618d720e6d5SJustin T. Gibbs 	} else {
1619e142669aSMatt Jacob 		if (csio->cdb_len > 12) {
1620e142669aSMatt Jacob 			seglim = 0;
1621e142669aSMatt Jacob 		} else {
1622d720e6d5SJustin T. Gibbs 			seglim = ISP_RQDSEG;
1623e142669aSMatt Jacob 		}
16249e11e5beSMatt Jacob 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
16259e11e5beSMatt Jacob 			rq->req_flags |= REQFLAG_DATA_IN;
16269e11e5beSMatt Jacob 		} else {
16279e11e5beSMatt Jacob 			rq->req_flags |= REQFLAG_DATA_OUT;
16289e11e5beSMatt Jacob 		}
1629d720e6d5SJustin T. Gibbs 	}
1630d720e6d5SJustin T. Gibbs 
1631d720e6d5SJustin T. Gibbs 	eseg = dm_segs + nseg;
1632d720e6d5SJustin T. Gibbs 
1633d720e6d5SJustin T. Gibbs 	while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) {
16344fd13c1bSMatt Jacob 		if (IS_FC(isp)) {
1635d720e6d5SJustin T. Gibbs 			ispreqt2_t *rq2 = (ispreqt2_t *)rq;
1636d720e6d5SJustin T. Gibbs 			rq2->req_dataseg[rq2->req_seg_count].ds_base =
1637d720e6d5SJustin T. Gibbs 			    dm_segs->ds_addr;
1638d720e6d5SJustin T. Gibbs 			rq2->req_dataseg[rq2->req_seg_count].ds_count =
1639d720e6d5SJustin T. Gibbs 			    dm_segs->ds_len;
1640d720e6d5SJustin T. Gibbs 		} else {
1641d720e6d5SJustin T. Gibbs 			rq->req_dataseg[rq->req_seg_count].ds_base =
1642d720e6d5SJustin T. Gibbs 				dm_segs->ds_addr;
1643d720e6d5SJustin T. Gibbs 			rq->req_dataseg[rq->req_seg_count].ds_count =
1644d720e6d5SJustin T. Gibbs 				dm_segs->ds_len;
1645d720e6d5SJustin T. Gibbs 		}
1646d720e6d5SJustin T. Gibbs 		datalen -= dm_segs->ds_len;
1647d720e6d5SJustin T. Gibbs 		rq->req_seg_count++;
1648d720e6d5SJustin T. Gibbs 		dm_segs++;
1649d720e6d5SJustin T. Gibbs 	}
1650d720e6d5SJustin T. Gibbs 
1651d720e6d5SJustin T. Gibbs 	while (datalen > 0 && dm_segs != eseg) {
16524fd13c1bSMatt Jacob 		u_int16_t onxti;
16534fd13c1bSMatt Jacob 		ispcontreq_t local, *crq = &local, *cqe;
16544fd13c1bSMatt Jacob 
16554fd13c1bSMatt Jacob 		cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
16564fd13c1bSMatt Jacob 		onxti = nxti;
16574fd13c1bSMatt Jacob 		nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
16584fd13c1bSMatt Jacob 		if (nxti == mp->optr) {
16594fd13c1bSMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
16604873663cSMatt Jacob 			mp->error = MUSHERR_NOQENTRIES;
1661d720e6d5SJustin T. Gibbs 			return;
1662d720e6d5SJustin T. Gibbs 		}
1663d720e6d5SJustin T. Gibbs 		rq->req_header.rqs_entry_count++;
16644fd13c1bSMatt Jacob 		MEMZERO((void *)crq, sizeof (*crq));
1665d720e6d5SJustin T. Gibbs 		crq->req_header.rqs_entry_count = 1;
1666d720e6d5SJustin T. Gibbs 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
1667d720e6d5SJustin T. Gibbs 
1668d720e6d5SJustin T. Gibbs 		seglim = 0;
1669d720e6d5SJustin T. Gibbs 		while (datalen > 0 && seglim < ISP_CDSEG && dm_segs != eseg) {
1670d720e6d5SJustin T. Gibbs 			crq->req_dataseg[seglim].ds_base =
1671d720e6d5SJustin T. Gibbs 			    dm_segs->ds_addr;
1672d720e6d5SJustin T. Gibbs 			crq->req_dataseg[seglim].ds_count =
1673d720e6d5SJustin T. Gibbs 			    dm_segs->ds_len;
1674d720e6d5SJustin T. Gibbs 			rq->req_seg_count++;
1675d720e6d5SJustin T. Gibbs 			dm_segs++;
1676d720e6d5SJustin T. Gibbs 			seglim++;
1677d720e6d5SJustin T. Gibbs 			datalen -= dm_segs->ds_len;
1678d720e6d5SJustin T. Gibbs 		}
16794fd13c1bSMatt Jacob 		isp_put_cont_req(isp, crq, cqe);
16804fd13c1bSMatt Jacob 		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
1681d720e6d5SJustin T. Gibbs 	}
16824fd13c1bSMatt Jacob 	*mp->nxtip = nxti;
1683d720e6d5SJustin T. Gibbs }
1684d720e6d5SJustin T. Gibbs 
1685d720e6d5SJustin T. Gibbs static int
16869e11e5beSMatt Jacob isp_pci_dmasetup(struct ispsoftc *isp, struct ccb_scsiio *csio, ispreq_t *rq,
16874fd13c1bSMatt Jacob 	u_int16_t *nxtip, u_int16_t optr)
1688d720e6d5SJustin T. Gibbs {
16891923f739SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
16904fd13c1bSMatt Jacob 	ispreq_t *qep;
16910a5f7e8bSMatt Jacob 	bus_dmamap_t *dp = NULL;
1692d720e6d5SJustin T. Gibbs 	mush_t mush, *mp;
1693126ec864SMatt Jacob 	void (*eptr)(void *, bus_dma_segment_t *, int, int);
1694d720e6d5SJustin T. Gibbs 
16954fd13c1bSMatt Jacob 	qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, isp->isp_reqidx);
169665b024e1SMatt Jacob #ifdef	ISP_TARGET_MODE
169765b024e1SMatt Jacob 	if (csio->ccb_h.func_code == XPT_CONT_TARGET_IO) {
169865b024e1SMatt Jacob 		if (IS_FC(isp)) {
169905fbcbb0SMatt Jacob 			eptr = tdma_mkfc;
170065b024e1SMatt Jacob 		} else {
170105fbcbb0SMatt Jacob 			eptr = tdma_mk;
170265b024e1SMatt Jacob 		}
170305fbcbb0SMatt Jacob 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
170405fbcbb0SMatt Jacob 		    (csio->dxfer_len == 0)) {
170565b024e1SMatt Jacob 			mp = &mush;
170665b024e1SMatt Jacob 			mp->isp = isp;
170765b024e1SMatt Jacob 			mp->cmd_token = csio;
1708e9a2738aSMatt Jacob 			mp->rq = rq;	/* really a ct_entry_t or ct2_entry_t */
17094fd13c1bSMatt Jacob 			mp->nxtip = nxtip;
171065b024e1SMatt Jacob 			mp->optr = optr;
171165b024e1SMatt Jacob 			mp->error = 0;
171265b024e1SMatt Jacob 			(*eptr)(mp, NULL, 0, 0);
17134fd13c1bSMatt Jacob 			goto mbxsync;
171465b024e1SMatt Jacob 		}
171565b024e1SMatt Jacob 	} else
171665b024e1SMatt Jacob #endif
171765b024e1SMatt Jacob 	eptr = dma2;
171865b024e1SMatt Jacob 
17194fd13c1bSMatt Jacob 
172005fbcbb0SMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
172105fbcbb0SMatt Jacob 	    (csio->dxfer_len == 0)) {
172242426921SMatt Jacob 		rq->req_seg_count = 1;
17234fd13c1bSMatt Jacob 		goto mbxsync;
172442426921SMatt Jacob 	}
172542426921SMatt Jacob 
1726d720e6d5SJustin T. Gibbs 	/*
1727d720e6d5SJustin T. Gibbs 	 * Do a virtual grapevine step to collect info for
17284873663cSMatt Jacob 	 * the callback dma allocation that we have to use...
1729d720e6d5SJustin T. Gibbs 	 */
1730d720e6d5SJustin T. Gibbs 	mp = &mush;
1731d720e6d5SJustin T. Gibbs 	mp->isp = isp;
17329e11e5beSMatt Jacob 	mp->cmd_token = csio;
1733d720e6d5SJustin T. Gibbs 	mp->rq = rq;
17344fd13c1bSMatt Jacob 	mp->nxtip = nxtip;
1735d720e6d5SJustin T. Gibbs 	mp->optr = optr;
1736d720e6d5SJustin T. Gibbs 	mp->error = 0;
1737d720e6d5SJustin T. Gibbs 
17389e11e5beSMatt Jacob 	if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
17399e11e5beSMatt Jacob 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
17404873663cSMatt Jacob 			int error, s;
17411923f739SMatt Jacob 			dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
1742d720e6d5SJustin T. Gibbs 			s = splsoftvm();
17431923f739SMatt Jacob 			error = bus_dmamap_load(pcs->dmat, *dp,
17449e11e5beSMatt Jacob 			    csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
1745d720e6d5SJustin T. Gibbs 			if (error == EINPROGRESS) {
17461923f739SMatt Jacob 				bus_dmamap_unload(pcs->dmat, *dp);
1747d720e6d5SJustin T. Gibbs 				mp->error = EINVAL;
1748f7dddf8aSMatt Jacob 				isp_prt(isp, ISP_LOGERR,
1749f7dddf8aSMatt Jacob 				    "deferred dma allocation not supported");
1750d720e6d5SJustin T. Gibbs 			} else if (error && mp->error == 0) {
17510a5f7e8bSMatt Jacob #ifdef	DIAGNOSTIC
17526e5c5328SMatt Jacob 				isp_prt(isp, ISP_LOGERR,
17536e5c5328SMatt Jacob 				    "error %d in dma mapping code", error);
17540a5f7e8bSMatt Jacob #endif
1755d720e6d5SJustin T. Gibbs 				mp->error = error;
1756d720e6d5SJustin T. Gibbs 			}
17574873663cSMatt Jacob 			splx(s);
1758d720e6d5SJustin T. Gibbs 		} else {
1759d720e6d5SJustin T. Gibbs 			/* Pointer to physical buffer */
1760d720e6d5SJustin T. Gibbs 			struct bus_dma_segment seg;
1761d720e6d5SJustin T. Gibbs 			seg.ds_addr = (bus_addr_t)csio->data_ptr;
1762d720e6d5SJustin T. Gibbs 			seg.ds_len = csio->dxfer_len;
17639e11e5beSMatt Jacob 			(*eptr)(mp, &seg, 1, 0);
1764d720e6d5SJustin T. Gibbs 		}
1765d720e6d5SJustin T. Gibbs 	} else {
1766d720e6d5SJustin T. Gibbs 		struct bus_dma_segment *segs;
1767d720e6d5SJustin T. Gibbs 
17689e11e5beSMatt Jacob 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
1769f7dddf8aSMatt Jacob 			isp_prt(isp, ISP_LOGERR,
1770f7dddf8aSMatt Jacob 			    "Physical segment pointers unsupported");
1771d720e6d5SJustin T. Gibbs 			mp->error = EINVAL;
17729e11e5beSMatt Jacob 		} else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
1773f7dddf8aSMatt Jacob 			isp_prt(isp, ISP_LOGERR,
1774f7dddf8aSMatt Jacob 			    "Virtual segment addresses unsupported");
1775d720e6d5SJustin T. Gibbs 			mp->error = EINVAL;
1776d720e6d5SJustin T. Gibbs 		} else {
1777d720e6d5SJustin T. Gibbs 			/* Just use the segments provided */
1778d720e6d5SJustin T. Gibbs 			segs = (struct bus_dma_segment *) csio->data_ptr;
17799e11e5beSMatt Jacob 			(*eptr)(mp, segs, csio->sglist_cnt, 0);
1780d720e6d5SJustin T. Gibbs 		}
1781d720e6d5SJustin T. Gibbs 	}
1782d720e6d5SJustin T. Gibbs 	if (mp->error) {
17834873663cSMatt Jacob 		int retval = CMD_COMPLETE;
17844873663cSMatt Jacob 		if (mp->error == MUSHERR_NOQENTRIES) {
17854873663cSMatt Jacob 			retval = CMD_EAGAIN;
17864873663cSMatt Jacob 		} else if (mp->error == EFBIG) {
17870a5f7e8bSMatt Jacob 			XS_SETERR(csio, CAM_REQ_TOO_BIG);
1788d720e6d5SJustin T. Gibbs 		} else if (mp->error == EINVAL) {
17890a5f7e8bSMatt Jacob 			XS_SETERR(csio, CAM_REQ_INVALID);
1790d720e6d5SJustin T. Gibbs 		} else {
17910a5f7e8bSMatt Jacob 			XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
1792d720e6d5SJustin T. Gibbs 		}
17934873663cSMatt Jacob 		return (retval);
17940a5f7e8bSMatt Jacob 	}
17954fd13c1bSMatt Jacob mbxsync:
17964fd13c1bSMatt Jacob 	switch (rq->req_header.rqs_entry_type) {
17974fd13c1bSMatt Jacob 	case RQSTYPE_REQUEST:
17984fd13c1bSMatt Jacob 		isp_put_request(isp, rq, qep);
17994fd13c1bSMatt Jacob 		break;
18004fd13c1bSMatt Jacob 	case RQSTYPE_CMDONLY:
18014fd13c1bSMatt Jacob 		isp_put_extended_request(isp, (ispextreq_t *)rq,
18024fd13c1bSMatt Jacob 		    (ispextreq_t *)qep);
18034fd13c1bSMatt Jacob 		break;
18044fd13c1bSMatt Jacob 	case RQSTYPE_T2RQS:
18054fd13c1bSMatt Jacob 		isp_put_request_t2(isp, (ispreqt2_t *) rq, (ispreqt2_t *) qep);
18064fd13c1bSMatt Jacob 		break;
18070a5f7e8bSMatt Jacob 	}
18084873663cSMatt Jacob 	return (CMD_QUEUED);
1809d720e6d5SJustin T. Gibbs }
1810d720e6d5SJustin T. Gibbs 
1811d720e6d5SJustin T. Gibbs static void
1812d8d5f2adSMatt Jacob isp_pci_dmateardown(struct ispsoftc *isp, XS_T *xs, u_int16_t handle)
1813d720e6d5SJustin T. Gibbs {
18141923f739SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
18151923f739SMatt Jacob 	bus_dmamap_t *dp = &pcs->dmaps[isp_handle_index(handle)];
1816a95ae193SMatt Jacob 	if ((xs->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
18171923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_POSTREAD);
1818d720e6d5SJustin T. Gibbs 	} else {
18191923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_POSTWRITE);
1820d720e6d5SJustin T. Gibbs 	}
18211923f739SMatt Jacob 	bus_dmamap_unload(pcs->dmat, *dp);
1822d720e6d5SJustin T. Gibbs }
1823d720e6d5SJustin T. Gibbs 
182465adb54cSMatt Jacob 
182565adb54cSMatt Jacob static void
182617e318c6SMatt Jacob isp_pci_reset1(struct ispsoftc *isp)
182765adb54cSMatt Jacob {
182865adb54cSMatt Jacob 	/* Make sure the BIOS is disabled */
182965adb54cSMatt Jacob 	isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
1830469b6b9eSMatt Jacob 	/* and enable interrupts */
1831469b6b9eSMatt Jacob 	ENABLE_INTS(isp);
183265adb54cSMatt Jacob }
183365adb54cSMatt Jacob 
183465adb54cSMatt Jacob static void
1835d02373f1SMatt Jacob isp_pci_dumpregs(struct ispsoftc *isp, const char *msg)
183665adb54cSMatt Jacob {
18371923f739SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1838d02373f1SMatt Jacob 	if (msg)
18396e5c5328SMatt Jacob 		printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
18406e5c5328SMatt Jacob 	else
18416e5c5328SMatt Jacob 		printf("%s:\n", device_get_nameunit(isp->isp_dev));
1842d02373f1SMatt Jacob 	if (IS_SCSI(isp))
1843d02373f1SMatt Jacob 		printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
1844d02373f1SMatt Jacob 	else
1845d02373f1SMatt Jacob 		printf("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
1846d02373f1SMatt Jacob 	printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
1847d02373f1SMatt Jacob 	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
1848d02373f1SMatt Jacob 	printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
1849d02373f1SMatt Jacob 
1850d02373f1SMatt Jacob 
1851d02373f1SMatt Jacob 	if (IS_SCSI(isp)) {
1852d02373f1SMatt Jacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
1853d02373f1SMatt Jacob 		printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
1854d02373f1SMatt Jacob 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
1855d02373f1SMatt Jacob 			ISP_READ(isp, CDMA_FIFO_STS));
1856d02373f1SMatt Jacob 		printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
1857d02373f1SMatt Jacob 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
1858d02373f1SMatt Jacob 			ISP_READ(isp, DDMA_FIFO_STS));
1859d02373f1SMatt Jacob 		printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
1860d02373f1SMatt Jacob 			ISP_READ(isp, SXP_INTERRUPT),
1861d02373f1SMatt Jacob 			ISP_READ(isp, SXP_GROSS_ERR),
1862d02373f1SMatt Jacob 			ISP_READ(isp, SXP_PINS_CTRL));
1863d02373f1SMatt Jacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
1864d02373f1SMatt Jacob 	}
1865d02373f1SMatt Jacob 	printf("    mbox regs: %x %x %x %x %x\n",
1866d02373f1SMatt Jacob 	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
1867d02373f1SMatt Jacob 	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
1868d02373f1SMatt Jacob 	    ISP_READ(isp, OUTMAILBOX4));
1869d02373f1SMatt Jacob 	printf("    PCI Status Command/Status=%x\n",
18701923f739SMatt Jacob 	    pci_read_config(pcs->pci_dev, PCIR_COMMAND, 1));
187165adb54cSMatt Jacob }
1872