xref: /freebsd/sys/dev/isp/isp_pci.c (revision 51effc8cd5aa6db5a0fdc22a6fc933951390d6a7)
1aad970f1SDavid E. O'Brien /*-
265adb54cSMatt Jacob  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
365adb54cSMatt Jacob  * FreeBSD Version.
465adb54cSMatt Jacob  *
5e5265237SMatt Jacob  * Copyright (c) 1997-2006 by Matthew Jacob
6e5265237SMatt Jacob  * All rights reserved.
765adb54cSMatt Jacob  *
865adb54cSMatt Jacob  * Redistribution and use in source and binary forms, with or without
965adb54cSMatt Jacob  * modification, are permitted provided that the following conditions
1065adb54cSMatt Jacob  * are met:
1165adb54cSMatt Jacob  * 1. Redistributions of source code must retain the above copyright
1265adb54cSMatt Jacob  *    notice immediately at the beginning of the file, without modification,
1365adb54cSMatt Jacob  *    this list of conditions, and the following disclaimer.
14aa57fd6fSMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
15aa57fd6fSMatt Jacob  *    derived from this software without specific prior written permission.
1665adb54cSMatt Jacob  *
1765adb54cSMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1865adb54cSMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1965adb54cSMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2065adb54cSMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2165adb54cSMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2265adb54cSMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2365adb54cSMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2465adb54cSMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2565adb54cSMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2665adb54cSMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2765adb54cSMatt Jacob  * SUCH DAMAGE.
28e5265237SMatt Jacob  *
2965adb54cSMatt Jacob  */
30d720e6d5SJustin T. Gibbs 
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
33aad970f1SDavid E. O'Brien 
34960f6939SMatt Jacob #include <sys/param.h>
35960f6939SMatt Jacob #include <sys/systm.h>
36960f6939SMatt Jacob #include <sys/kernel.h>
37960f6939SMatt Jacob #include <sys/module.h>
38960f6939SMatt Jacob #include <sys/bus.h>
3974a96f43SJohn Baldwin #include <sys/stdint.h>
4065adb54cSMatt Jacob 
4177e6a3b2SWarner Losh #include <dev/pci/pcireg.h>
4277e6a3b2SWarner Losh #include <dev/pci/pcivar.h>
4365adb54cSMatt Jacob 
44d720e6d5SJustin T. Gibbs #include <machine/bus.h>
45960f6939SMatt Jacob #include <machine/resource.h>
46960f6939SMatt Jacob #include <sys/rman.h>
47960f6939SMatt Jacob #include <sys/malloc.h>
48960f6939SMatt Jacob 
49960f6939SMatt Jacob #include <dev/isp/isp_freebsd.h>
50d59bd469SMatt Jacob 
51126ec864SMatt Jacob static u_int16_t isp_pci_rd_reg(struct ispsoftc *, int);
52126ec864SMatt Jacob static void isp_pci_wr_reg(struct ispsoftc *, int, u_int16_t);
53126ec864SMatt Jacob static u_int16_t isp_pci_rd_reg_1080(struct ispsoftc *, int);
54126ec864SMatt Jacob static void isp_pci_wr_reg_1080(struct ispsoftc *, int, u_int16_t);
55126ec864SMatt Jacob static int
56126ec864SMatt Jacob isp_pci_rd_isr(struct ispsoftc *, u_int16_t *, u_int16_t *, u_int16_t *);
57126ec864SMatt Jacob static int
58126ec864SMatt Jacob isp_pci_rd_isr_2300(struct ispsoftc *, u_int16_t *, u_int16_t *, u_int16_t *);
59126ec864SMatt Jacob static int isp_pci_mbxdma(struct ispsoftc *);
60126ec864SMatt Jacob static int
61126ec864SMatt Jacob isp_pci_dmasetup(struct ispsoftc *, XS_T *, ispreq_t *, u_int16_t *, u_int16_t);
62d720e6d5SJustin T. Gibbs static void
63126ec864SMatt Jacob isp_pci_dmateardown(struct ispsoftc *, XS_T *, u_int16_t);
6465adb54cSMatt Jacob 
65126ec864SMatt Jacob static void isp_pci_reset1(struct ispsoftc *);
66126ec864SMatt Jacob static void isp_pci_dumpregs(struct ispsoftc *, const char *);
6765adb54cSMatt Jacob 
6865adb54cSMatt Jacob static struct ispmdvec mdvec = {
69126ec864SMatt Jacob 	isp_pci_rd_isr,
7065adb54cSMatt Jacob 	isp_pci_rd_reg,
7165adb54cSMatt Jacob 	isp_pci_wr_reg,
7265adb54cSMatt Jacob 	isp_pci_mbxdma,
7365adb54cSMatt Jacob 	isp_pci_dmasetup,
74d720e6d5SJustin T. Gibbs 	isp_pci_dmateardown,
7565adb54cSMatt Jacob 	NULL,
7665adb54cSMatt Jacob 	isp_pci_reset1,
7765adb54cSMatt Jacob 	isp_pci_dumpregs,
7856aef503SMatt Jacob 	NULL,
79d02373f1SMatt Jacob 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
8065adb54cSMatt Jacob };
8165adb54cSMatt Jacob 
82d59bd469SMatt Jacob static struct ispmdvec mdvec_1080 = {
83126ec864SMatt Jacob 	isp_pci_rd_isr,
84d59bd469SMatt Jacob 	isp_pci_rd_reg_1080,
85d59bd469SMatt Jacob 	isp_pci_wr_reg_1080,
86d59bd469SMatt Jacob 	isp_pci_mbxdma,
87d59bd469SMatt Jacob 	isp_pci_dmasetup,
88d59bd469SMatt Jacob 	isp_pci_dmateardown,
89d59bd469SMatt Jacob 	NULL,
90d59bd469SMatt Jacob 	isp_pci_reset1,
91d59bd469SMatt Jacob 	isp_pci_dumpregs,
9256aef503SMatt Jacob 	NULL,
93d02373f1SMatt Jacob 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
94d59bd469SMatt Jacob };
95d59bd469SMatt Jacob 
96960f6939SMatt Jacob static struct ispmdvec mdvec_12160 = {
97126ec864SMatt Jacob 	isp_pci_rd_isr,
98960f6939SMatt Jacob 	isp_pci_rd_reg_1080,
99960f6939SMatt Jacob 	isp_pci_wr_reg_1080,
100960f6939SMatt Jacob 	isp_pci_mbxdma,
101960f6939SMatt Jacob 	isp_pci_dmasetup,
102960f6939SMatt Jacob 	isp_pci_dmateardown,
103960f6939SMatt Jacob 	NULL,
104960f6939SMatt Jacob 	isp_pci_reset1,
105960f6939SMatt Jacob 	isp_pci_dumpregs,
10656aef503SMatt Jacob 	NULL,
107d02373f1SMatt Jacob 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
108960f6939SMatt Jacob };
109960f6939SMatt Jacob 
11065adb54cSMatt Jacob static struct ispmdvec mdvec_2100 = {
111126ec864SMatt Jacob 	isp_pci_rd_isr,
11265adb54cSMatt Jacob 	isp_pci_rd_reg,
11365adb54cSMatt Jacob 	isp_pci_wr_reg,
11465adb54cSMatt Jacob 	isp_pci_mbxdma,
11565adb54cSMatt Jacob 	isp_pci_dmasetup,
116d720e6d5SJustin T. Gibbs 	isp_pci_dmateardown,
11765adb54cSMatt Jacob 	NULL,
11865adb54cSMatt Jacob 	isp_pci_reset1,
119d02373f1SMatt Jacob 	isp_pci_dumpregs
12065adb54cSMatt Jacob };
121222bb542SMatt Jacob 
122222bb542SMatt Jacob static struct ispmdvec mdvec_2200 = {
123126ec864SMatt Jacob 	isp_pci_rd_isr,
124126ec864SMatt Jacob 	isp_pci_rd_reg,
125126ec864SMatt Jacob 	isp_pci_wr_reg,
126126ec864SMatt Jacob 	isp_pci_mbxdma,
127126ec864SMatt Jacob 	isp_pci_dmasetup,
128126ec864SMatt Jacob 	isp_pci_dmateardown,
129126ec864SMatt Jacob 	NULL,
130126ec864SMatt Jacob 	isp_pci_reset1,
131126ec864SMatt Jacob 	isp_pci_dumpregs
132126ec864SMatt Jacob };
133126ec864SMatt Jacob 
134126ec864SMatt Jacob static struct ispmdvec mdvec_2300 = {
135126ec864SMatt Jacob 	isp_pci_rd_isr_2300,
136222bb542SMatt Jacob 	isp_pci_rd_reg,
137222bb542SMatt Jacob 	isp_pci_wr_reg,
138222bb542SMatt Jacob 	isp_pci_mbxdma,
139222bb542SMatt Jacob 	isp_pci_dmasetup,
140222bb542SMatt Jacob 	isp_pci_dmateardown,
141222bb542SMatt Jacob 	NULL,
142222bb542SMatt Jacob 	isp_pci_reset1,
143d02373f1SMatt Jacob 	isp_pci_dumpregs
144222bb542SMatt Jacob };
145d951bbcaSMatt Jacob 
14665adb54cSMatt Jacob #ifndef	PCIM_CMD_INVEN
14765adb54cSMatt Jacob #define	PCIM_CMD_INVEN			0x10
14865adb54cSMatt Jacob #endif
14965adb54cSMatt Jacob #ifndef	PCIM_CMD_BUSMASTEREN
15065adb54cSMatt Jacob #define	PCIM_CMD_BUSMASTEREN		0x0004
15165adb54cSMatt Jacob #endif
152d951bbcaSMatt Jacob #ifndef	PCIM_CMD_PERRESPEN
153d951bbcaSMatt Jacob #define	PCIM_CMD_PERRESPEN		0x0040
154d951bbcaSMatt Jacob #endif
155d951bbcaSMatt Jacob #ifndef	PCIM_CMD_SEREN
156d951bbcaSMatt Jacob #define	PCIM_CMD_SEREN			0x0100
157d951bbcaSMatt Jacob #endif
158d951bbcaSMatt Jacob 
159d951bbcaSMatt Jacob #ifndef	PCIR_COMMAND
160d951bbcaSMatt Jacob #define	PCIR_COMMAND			0x04
161d951bbcaSMatt Jacob #endif
162d951bbcaSMatt Jacob 
163d951bbcaSMatt Jacob #ifndef	PCIR_CACHELNSZ
164d951bbcaSMatt Jacob #define	PCIR_CACHELNSZ			0x0c
165d951bbcaSMatt Jacob #endif
166d951bbcaSMatt Jacob 
167d951bbcaSMatt Jacob #ifndef	PCIR_LATTIMER
168d951bbcaSMatt Jacob #define	PCIR_LATTIMER			0x0d
169d951bbcaSMatt Jacob #endif
170d951bbcaSMatt Jacob 
171ab6d0040SMatt Jacob #ifndef	PCIR_ROMADDR
172ab6d0040SMatt Jacob #define	PCIR_ROMADDR			0x30
173ab6d0040SMatt Jacob #endif
174ab6d0040SMatt Jacob 
17565adb54cSMatt Jacob #ifndef	PCI_VENDOR_QLOGIC
17665adb54cSMatt Jacob #define	PCI_VENDOR_QLOGIC		0x1077
17765adb54cSMatt Jacob #endif
17865adb54cSMatt Jacob 
17965adb54cSMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1020
18065adb54cSMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP1020	0x1020
18165adb54cSMatt Jacob #endif
18265adb54cSMatt Jacob 
183d59bd469SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1080
184d59bd469SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP1080	0x1080
185d59bd469SMatt Jacob #endif
186d59bd469SMatt Jacob 
187f556e83bSMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP10160
188f556e83bSMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP10160	0x1016
189f556e83bSMatt Jacob #endif
190f556e83bSMatt Jacob 
191960f6939SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP12160
192960f6939SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP12160	0x1216
193960f6939SMatt Jacob #endif
194960f6939SMatt Jacob 
195d59bd469SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1240
196d59bd469SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP1240	0x1240
197d59bd469SMatt Jacob #endif
19865adb54cSMatt Jacob 
19922e1dc85SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP1280
20022e1dc85SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP1280	0x1280
20122e1dc85SMatt Jacob #endif
20222e1dc85SMatt Jacob 
20365adb54cSMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2100
20465adb54cSMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2100	0x2100
20565adb54cSMatt Jacob #endif
20665adb54cSMatt Jacob 
207222bb542SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2200
208222bb542SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2200	0x2200
209222bb542SMatt Jacob #endif
210222bb542SMatt Jacob 
211126ec864SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2300
212126ec864SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2300	0x2300
213126ec864SMatt Jacob #endif
214126ec864SMatt Jacob 
215126ec864SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2312
216126ec864SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2312	0x2312
217126ec864SMatt Jacob #endif
218126ec864SMatt Jacob 
219e5265237SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2322
220e5265237SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2322	0x2322
221e5265237SMatt Jacob #endif
222e5265237SMatt Jacob 
2238872e3d7SMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP2422
2248872e3d7SMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP2422	0x2422
2258872e3d7SMatt Jacob #endif
2268872e3d7SMatt Jacob 
227dd1419abSMatt Jacob #ifndef	PCI_PRODUCT_QLOGIC_ISP6312
228dd1419abSMatt Jacob #define	PCI_PRODUCT_QLOGIC_ISP6312	0x6312
229dd1419abSMatt Jacob #endif
230dd1419abSMatt Jacob 
23156aef503SMatt Jacob #define	PCI_QLOGIC_ISP1020	\
23256aef503SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
233d59bd469SMatt Jacob 
234d59bd469SMatt Jacob #define	PCI_QLOGIC_ISP1080	\
235d59bd469SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP1080 << 16) | PCI_VENDOR_QLOGIC)
236d59bd469SMatt Jacob 
237f556e83bSMatt Jacob #define	PCI_QLOGIC_ISP10160	\
238f556e83bSMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP10160 << 16) | PCI_VENDOR_QLOGIC)
239f556e83bSMatt Jacob 
240960f6939SMatt Jacob #define	PCI_QLOGIC_ISP12160	\
241960f6939SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP12160 << 16) | PCI_VENDOR_QLOGIC)
242960f6939SMatt Jacob 
243d59bd469SMatt Jacob #define	PCI_QLOGIC_ISP1240	\
244d59bd469SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP1240 << 16) | PCI_VENDOR_QLOGIC)
245d59bd469SMatt Jacob 
24622e1dc85SMatt Jacob #define	PCI_QLOGIC_ISP1280	\
24722e1dc85SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP1280 << 16) | PCI_VENDOR_QLOGIC)
24822e1dc85SMatt Jacob 
24965adb54cSMatt Jacob #define	PCI_QLOGIC_ISP2100	\
25065adb54cSMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
25165adb54cSMatt Jacob 
252222bb542SMatt Jacob #define	PCI_QLOGIC_ISP2200	\
253222bb542SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2200 << 16) | PCI_VENDOR_QLOGIC)
254222bb542SMatt Jacob 
255126ec864SMatt Jacob #define	PCI_QLOGIC_ISP2300	\
256126ec864SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2300 << 16) | PCI_VENDOR_QLOGIC)
257126ec864SMatt Jacob 
258126ec864SMatt Jacob #define	PCI_QLOGIC_ISP2312	\
259126ec864SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2312 << 16) | PCI_VENDOR_QLOGIC)
260126ec864SMatt Jacob 
261e5265237SMatt Jacob #define	PCI_QLOGIC_ISP2322	\
262e5265237SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2322 << 16) | PCI_VENDOR_QLOGIC)
263e5265237SMatt Jacob 
2646c426685SMatt Jacob #define	PCI_QLOGIC_ISP2422	\
2656c426685SMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP2422 << 16) | PCI_VENDOR_QLOGIC)
2666c426685SMatt Jacob 
267dd1419abSMatt Jacob #define	PCI_QLOGIC_ISP6312	\
268dd1419abSMatt Jacob 	((PCI_PRODUCT_QLOGIC_ISP6312 << 16) | PCI_VENDOR_QLOGIC)
269dd1419abSMatt Jacob 
270e11a1ee8SMatt Jacob /*
271e11a1ee8SMatt Jacob  * Odd case for some AMI raid cards... We need to *not* attach to this.
272e11a1ee8SMatt Jacob  */
273e11a1ee8SMatt Jacob #define	AMI_RAID_SUBVENDOR_ID	0x101e
274e11a1ee8SMatt Jacob 
27565adb54cSMatt Jacob #define	IO_MAP_REG	0x10
27665adb54cSMatt Jacob #define	MEM_MAP_REG	0x14
27765adb54cSMatt Jacob 
278d951bbcaSMatt Jacob #define	PCI_DFLT_LTNCY	0x40
279d951bbcaSMatt Jacob #define	PCI_DFLT_LNSZ	0x10
28065adb54cSMatt Jacob 
281960f6939SMatt Jacob static int isp_pci_probe (device_t);
282960f6939SMatt Jacob static int isp_pci_attach (device_t);
28365adb54cSMatt Jacob 
2841923f739SMatt Jacob 
28565adb54cSMatt Jacob struct isp_pcisoftc {
28665adb54cSMatt Jacob 	struct ispsoftc			pci_isp;
287960f6939SMatt Jacob 	device_t			pci_dev;
288960f6939SMatt Jacob 	struct resource *		pci_reg;
28965adb54cSMatt Jacob 	bus_space_tag_t			pci_st;
29065adb54cSMatt Jacob 	bus_space_handle_t		pci_sh;
291960f6939SMatt Jacob 	void *				ih;
292d59bd469SMatt Jacob 	int16_t				pci_poff[_NREG_BLKS];
2931923f739SMatt Jacob 	bus_dma_tag_t			dmat;
294a95ae193SMatt Jacob 	bus_dmamap_t			*dmaps;
29565adb54cSMatt Jacob };
29673030e03SMatt Jacob extern ispfwfunc *isp_get_firmware_p;
29765adb54cSMatt Jacob 
298960f6939SMatt Jacob static device_method_t isp_pci_methods[] = {
299960f6939SMatt Jacob 	/* Device interface */
300960f6939SMatt Jacob 	DEVMETHOD(device_probe,		isp_pci_probe),
301960f6939SMatt Jacob 	DEVMETHOD(device_attach,	isp_pci_attach),
302960f6939SMatt Jacob 	{ 0, 0 }
30365adb54cSMatt Jacob };
304126ec864SMatt Jacob static void isp_pci_intr(void *);
30565adb54cSMatt Jacob 
306960f6939SMatt Jacob static driver_t isp_pci_driver = {
307960f6939SMatt Jacob 	"isp", isp_pci_methods, sizeof (struct isp_pcisoftc)
308960f6939SMatt Jacob };
309960f6939SMatt Jacob static devclass_t isp_devclass;
310960f6939SMatt Jacob DRIVER_MODULE(isp, pci, isp_pci_driver, isp_devclass, 0, 0);
31165adb54cSMatt Jacob 
312960f6939SMatt Jacob static int
313960f6939SMatt Jacob isp_pci_probe(device_t dev)
31465adb54cSMatt Jacob {
315960f6939SMatt Jacob         switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
31656aef503SMatt Jacob 	case PCI_QLOGIC_ISP1020:
317960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 1020/1040 PCI SCSI Adapter");
31865adb54cSMatt Jacob 		break;
319d59bd469SMatt Jacob 	case PCI_QLOGIC_ISP1080:
320960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 1080 PCI SCSI Adapter");
321c6608df3SMatt Jacob 		break;
322c6608df3SMatt Jacob 	case PCI_QLOGIC_ISP1240:
323960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 1240 PCI SCSI Adapter");
324d59bd469SMatt Jacob 		break;
32522e1dc85SMatt Jacob 	case PCI_QLOGIC_ISP1280:
326960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 1280 PCI SCSI Adapter");
327960f6939SMatt Jacob 		break;
328f556e83bSMatt Jacob 	case PCI_QLOGIC_ISP10160:
329f556e83bSMatt Jacob 		device_set_desc(dev, "Qlogic ISP 10160 PCI SCSI Adapter");
330f556e83bSMatt Jacob 		break;
331960f6939SMatt Jacob 	case PCI_QLOGIC_ISP12160:
332e11a1ee8SMatt Jacob 		if (pci_get_subvendor(dev) == AMI_RAID_SUBVENDOR_ID) {
333e11a1ee8SMatt Jacob 			return (ENXIO);
334e11a1ee8SMatt Jacob 		}
335960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 12160 PCI SCSI Adapter");
33622e1dc85SMatt Jacob 		break;
33765adb54cSMatt Jacob 	case PCI_QLOGIC_ISP2100:
338960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2100 PCI FC-AL Adapter");
33965adb54cSMatt Jacob 		break;
3405542fe4bSMatt Jacob 	case PCI_QLOGIC_ISP2200:
341960f6939SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2200 PCI FC-AL Adapter");
3425542fe4bSMatt Jacob 		break;
343126ec864SMatt Jacob 	case PCI_QLOGIC_ISP2300:
344126ec864SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2300 PCI FC-AL Adapter");
345126ec864SMatt Jacob 		break;
346126ec864SMatt Jacob 	case PCI_QLOGIC_ISP2312:
347126ec864SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2312 PCI FC-AL Adapter");
348126ec864SMatt Jacob 		break;
349e5265237SMatt Jacob 	case PCI_QLOGIC_ISP2322:
350e5265237SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2322 PCI FC-AL Adapter");
351e5265237SMatt Jacob 		break;
3528872e3d7SMatt Jacob 	case PCI_QLOGIC_ISP2422:
3538872e3d7SMatt Jacob 		device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
3548872e3d7SMatt Jacob 		break;
355dd1419abSMatt Jacob 	case PCI_QLOGIC_ISP6312:
356dd1419abSMatt Jacob 		device_set_desc(dev, "Qlogic ISP 6312 PCI FC-AL Adapter");
357dd1419abSMatt Jacob 		break;
35865adb54cSMatt Jacob 	default:
359960f6939SMatt Jacob 		return (ENXIO);
36065adb54cSMatt Jacob 	}
36173030e03SMatt Jacob 	if (isp_announced == 0 && bootverbose) {
362d02373f1SMatt Jacob 		printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
363a95ae193SMatt Jacob 		    "Core Version %d.%d\n",
364d720e6d5SJustin T. Gibbs 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
365d720e6d5SJustin T. Gibbs 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
36673030e03SMatt Jacob 		isp_announced++;
36765adb54cSMatt Jacob 	}
36856aef503SMatt Jacob 	/*
36956aef503SMatt Jacob 	 * XXXX: Here is where we might load the f/w module
37056aef503SMatt Jacob 	 * XXXX: (or increase a reference count to it).
37156aef503SMatt Jacob 	 */
372b77e575eSWarner Losh 	return (BUS_PROBE_DEFAULT);
37365adb54cSMatt Jacob }
37465adb54cSMatt Jacob 
375960f6939SMatt Jacob static int
376960f6939SMatt Jacob isp_pci_attach(device_t dev)
37765adb54cSMatt Jacob {
378960f6939SMatt Jacob 	struct resource *regs, *irq;
3796e5c5328SMatt Jacob 	int tval, rtp, rgd, iqd, m1, m2, isp_debug, role;
380960f6939SMatt Jacob 	u_int32_t data, cmd, linesz, psize, basetype;
38165adb54cSMatt Jacob 	struct isp_pcisoftc *pcs;
3823395b056SMatt Jacob 	struct ispsoftc *isp = NULL;
383c6608df3SMatt Jacob 	struct ispmdvec *mdvp;
38422941bd7SPeter Wemm 	const char *sptr;
3853395b056SMatt Jacob 	int locksetup = 0;
38665adb54cSMatt Jacob 
387222bb542SMatt Jacob 	/*
3889ba86737SMatt Jacob 	 * Figure out if we're supposed to skip this one.
3899ba86737SMatt Jacob 	 */
3906e5c5328SMatt Jacob 
3916e5c5328SMatt Jacob 	tval = 0;
3926e5c5328SMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
3936e5c5328SMatt Jacob 	    "disable", &tval) == 0 && tval) {
394b9b599feSMatt Jacob 		device_printf(dev, "device is disabled\n");
395b9b599feSMatt Jacob 		/* but return 0 so the !$)$)*!$*) unit isn't reused */
396b9b599feSMatt Jacob 		return (0);
397b9b599feSMatt Jacob 	}
3986e5c5328SMatt Jacob 
39987ab7d39SMatt Jacob 	role = -1;
4006e5c5328SMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
40187ab7d39SMatt Jacob 	    "role", &role) == 0 && role != -1) {
40287ab7d39SMatt Jacob 		role &= (ISP_ROLE_INITIATOR|ISP_ROLE_TARGET);
4036e5c5328SMatt Jacob 		device_printf(dev, "setting role to 0x%x\n", role);
4046e5c5328SMatt Jacob 	} else {
405b9b599feSMatt Jacob #ifdef	ISP_TARGET_MODE
40687ab7d39SMatt Jacob 		role = ISP_ROLE_TARGET;
407b9b599feSMatt Jacob #else
408b9b599feSMatt Jacob 		role = ISP_DEFAULT_ROLES;
409b9b599feSMatt Jacob #endif
4109ba86737SMatt Jacob 	}
4119ba86737SMatt Jacob 
4127cc0979fSDavid Malone 	pcs = malloc(sizeof (struct isp_pcisoftc), M_DEVBUF, M_NOWAIT | M_ZERO);
413960f6939SMatt Jacob 	if (pcs == NULL) {
414960f6939SMatt Jacob 		device_printf(dev, "cannot allocate softc\n");
415960f6939SMatt Jacob 		return (ENOMEM);
416960f6939SMatt Jacob 	}
417960f6939SMatt Jacob 
4189ba86737SMatt Jacob 	/*
41969767099SMatt Jacob 	 * Which we should try first - memory mapping or i/o mapping?
42069767099SMatt Jacob 	 *
42169767099SMatt Jacob 	 * We used to try memory first followed by i/o on alpha, otherwise
42269767099SMatt Jacob 	 * the reverse, but we should just try memory first all the time now.
423222bb542SMatt Jacob 	 */
424960f6939SMatt Jacob 	m1 = PCIM_CMD_MEMEN;
425960f6939SMatt Jacob 	m2 = PCIM_CMD_PORTEN;
4266e5c5328SMatt Jacob 
4276e5c5328SMatt Jacob 	tval = 0;
4286e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
4296e5c5328SMatt Jacob             "prefer_iomap", &tval) == 0 && tval != 0) {
430960f6939SMatt Jacob 		m1 = PCIM_CMD_PORTEN;
431960f6939SMatt Jacob 		m2 = PCIM_CMD_MEMEN;
432960f6939SMatt Jacob 	}
4336e5c5328SMatt Jacob 	tval = 0;
4346e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
4356e5c5328SMatt Jacob             "prefer_memmap", &tval) == 0 && tval != 0) {
4366e5c5328SMatt Jacob 		m1 = PCIM_CMD_MEMEN;
4376e5c5328SMatt Jacob 		m2 = PCIM_CMD_PORTEN;
438222bb542SMatt Jacob 	}
439222bb542SMatt Jacob 
440ab6d0040SMatt Jacob 	linesz = PCI_DFLT_LNSZ;
441960f6939SMatt Jacob 	irq = regs = NULL;
442960f6939SMatt Jacob 	rgd = rtp = iqd = 0;
443960f6939SMatt Jacob 
444b49c4674SMatt Jacob 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
445960f6939SMatt Jacob 	if (cmd & m1) {
446960f6939SMatt Jacob 		rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
447960f6939SMatt Jacob 		rgd = (m1 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
4485f96beb9SNate Lawson 		regs = bus_alloc_resource_any(dev, rtp, &rgd, RF_ACTIVE);
44965adb54cSMatt Jacob 	}
450960f6939SMatt Jacob 	if (regs == NULL && (cmd & m2)) {
451960f6939SMatt Jacob 		rtp = (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
452960f6939SMatt Jacob 		rgd = (m2 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
4535f96beb9SNate Lawson 		regs = bus_alloc_resource_any(dev, rtp, &rgd, RF_ACTIVE);
45465adb54cSMatt Jacob 	}
455960f6939SMatt Jacob 	if (regs == NULL) {
456960f6939SMatt Jacob 		device_printf(dev, "unable to map any ports\n");
457960f6939SMatt Jacob 		goto bad;
45865adb54cSMatt Jacob 	}
459222bb542SMatt Jacob 	if (bootverbose)
460f7dddf8aSMatt Jacob 		device_printf(dev, "using %s space register mapping\n",
461960f6939SMatt Jacob 		    (rgd == IO_MAP_REG)? "I/O" : "Memory");
462960f6939SMatt Jacob 	pcs->pci_dev = dev;
463960f6939SMatt Jacob 	pcs->pci_reg = regs;
464960f6939SMatt Jacob 	pcs->pci_st = rman_get_bustag(regs);
465960f6939SMatt Jacob 	pcs->pci_sh = rman_get_bushandle(regs);
46665adb54cSMatt Jacob 
467d59bd469SMatt Jacob 	pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
468d59bd469SMatt Jacob 	pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
469d59bd469SMatt Jacob 	pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
470d59bd469SMatt Jacob 	pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
471d59bd469SMatt Jacob 	pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
472c6608df3SMatt Jacob 	mdvp = &mdvec;
473c6608df3SMatt Jacob 	basetype = ISP_HA_SCSI_UNKNOWN;
474c6608df3SMatt Jacob 	psize = sizeof (sdparam);
47556aef503SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1020) {
476c6608df3SMatt Jacob 		mdvp = &mdvec;
477c6608df3SMatt Jacob 		basetype = ISP_HA_SCSI_UNKNOWN;
478c6608df3SMatt Jacob 		psize = sizeof (sdparam);
479d59bd469SMatt Jacob 	}
480960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1080) {
481c6608df3SMatt Jacob 		mdvp = &mdvec_1080;
482c6608df3SMatt Jacob 		basetype = ISP_HA_SCSI_1080;
483c6608df3SMatt Jacob 		psize = sizeof (sdparam);
484c6608df3SMatt Jacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
485c6608df3SMatt Jacob 		    ISP1080_DMA_REGS_OFF;
486c6608df3SMatt Jacob 	}
487960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1240) {
488c6608df3SMatt Jacob 		mdvp = &mdvec_1080;
48922e1dc85SMatt Jacob 		basetype = ISP_HA_SCSI_1240;
49022e1dc85SMatt Jacob 		psize = 2 * sizeof (sdparam);
49122e1dc85SMatt Jacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
49222e1dc85SMatt Jacob 		    ISP1080_DMA_REGS_OFF;
49322e1dc85SMatt Jacob 	}
494960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1280) {
49522e1dc85SMatt Jacob 		mdvp = &mdvec_1080;
49622e1dc85SMatt Jacob 		basetype = ISP_HA_SCSI_1280;
497c6608df3SMatt Jacob 		psize = 2 * sizeof (sdparam);
498d59bd469SMatt Jacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
499d59bd469SMatt Jacob 		    ISP1080_DMA_REGS_OFF;
500d59bd469SMatt Jacob 	}
501f556e83bSMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP10160) {
502f556e83bSMatt Jacob 		mdvp = &mdvec_12160;
503f556e83bSMatt Jacob 		basetype = ISP_HA_SCSI_10160;
504f556e83bSMatt Jacob 		psize = sizeof (sdparam);
505f556e83bSMatt Jacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
506f556e83bSMatt Jacob 		    ISP1080_DMA_REGS_OFF;
507f556e83bSMatt Jacob 	}
508960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP12160) {
509960f6939SMatt Jacob 		mdvp = &mdvec_12160;
510960f6939SMatt Jacob 		basetype = ISP_HA_SCSI_12160;
511960f6939SMatt Jacob 		psize = 2 * sizeof (sdparam);
512960f6939SMatt Jacob 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
513960f6939SMatt Jacob 		    ISP1080_DMA_REGS_OFF;
514960f6939SMatt Jacob 	}
515960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2100) {
516c6608df3SMatt Jacob 		mdvp = &mdvec_2100;
517c6608df3SMatt Jacob 		basetype = ISP_HA_FC_2100;
518c6608df3SMatt Jacob 		psize = sizeof (fcparam);
519d59bd469SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
520d59bd469SMatt Jacob 		    PCI_MBOX_REGS2100_OFF;
521960f6939SMatt Jacob 		if (pci_get_revid(dev) < 3) {
522ab6d0040SMatt Jacob 			/*
523ab6d0040SMatt Jacob 			 * XXX: Need to get the actual revision
524ab6d0040SMatt Jacob 			 * XXX: number of the 2100 FB. At any rate,
525ab6d0040SMatt Jacob 			 * XXX: lower cache line size for early revision
526ab6d0040SMatt Jacob 			 * XXX; boards.
527ab6d0040SMatt Jacob 			 */
528ab6d0040SMatt Jacob 			linesz = 1;
529ab6d0040SMatt Jacob 		}
53065adb54cSMatt Jacob 	}
531960f6939SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2200) {
532222bb542SMatt Jacob 		mdvp = &mdvec_2200;
533222bb542SMatt Jacob 		basetype = ISP_HA_FC_2200;
534222bb542SMatt Jacob 		psize = sizeof (fcparam);
535222bb542SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
536222bb542SMatt Jacob 		    PCI_MBOX_REGS2100_OFF;
537222bb542SMatt Jacob 	}
53875c1e828SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2300) {
539126ec864SMatt Jacob 		mdvp = &mdvec_2300;
540126ec864SMatt Jacob 		basetype = ISP_HA_FC_2300;
541126ec864SMatt Jacob 		psize = sizeof (fcparam);
542126ec864SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
543126ec864SMatt Jacob 		    PCI_MBOX_REGS2300_OFF;
544126ec864SMatt Jacob 	}
545dd1419abSMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2312 ||
546dd1419abSMatt Jacob 	    pci_get_devid(dev) == PCI_QLOGIC_ISP6312) {
54775c1e828SMatt Jacob 		mdvp = &mdvec_2300;
54875c1e828SMatt Jacob 		basetype = ISP_HA_FC_2312;
54975c1e828SMatt Jacob 		psize = sizeof (fcparam);
55075c1e828SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
55175c1e828SMatt Jacob 		    PCI_MBOX_REGS2300_OFF;
55275c1e828SMatt Jacob 	}
553e5265237SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2322) {
554e5265237SMatt Jacob 		mdvp = &mdvec_2300;
555e5265237SMatt Jacob 		basetype = ISP_HA_FC_2322;
556e5265237SMatt Jacob 		psize = sizeof (fcparam);
557e5265237SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
558e5265237SMatt Jacob 		    PCI_MBOX_REGS2300_OFF;
559e5265237SMatt Jacob 	}
5608872e3d7SMatt Jacob 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2422) {
5618872e3d7SMatt Jacob 		mdvp = &mdvec_2300;
5628872e3d7SMatt Jacob 		basetype = ISP_HA_FC_2422;
5638872e3d7SMatt Jacob 		psize = sizeof (fcparam);
5648872e3d7SMatt Jacob 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
5658872e3d7SMatt Jacob 		    PCI_MBOX_REGS2300_OFF;
5668872e3d7SMatt Jacob 	}
567c6608df3SMatt Jacob 	isp = &pcs->pci_isp;
5687cc0979fSDavid Malone 	isp->isp_param = malloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
569c6608df3SMatt Jacob 	if (isp->isp_param == NULL) {
570960f6939SMatt Jacob 		device_printf(dev, "cannot allocate parameter data\n");
571960f6939SMatt Jacob 		goto bad;
572c6608df3SMatt Jacob 	}
573c6608df3SMatt Jacob 	isp->isp_mdvec = mdvp;
574c6608df3SMatt Jacob 	isp->isp_type = basetype;
575960f6939SMatt Jacob 	isp->isp_revision = pci_get_revid(dev);
576b9b599feSMatt Jacob 	isp->isp_role = role;
5776e5c5328SMatt Jacob 	isp->isp_dev = dev;
57865adb54cSMatt Jacob 
57956aef503SMatt Jacob 	/*
58056aef503SMatt Jacob 	 * Try and find firmware for this device.
58156aef503SMatt Jacob 	 */
58256aef503SMatt Jacob 
583e5265237SMatt Jacob 	/*
584e5265237SMatt Jacob 	 * Don't even attempt to get firmware for the 2322/2422 (yet)
585e5265237SMatt Jacob 	 */
586e5265237SMatt Jacob 	if (IS_2322(isp) == 0 && IS_24XX(isp) == 0 && isp_get_firmware_p) {
58756aef503SMatt Jacob 		int device = (int) pci_get_device(dev);
58856aef503SMatt Jacob #ifdef	ISP_TARGET_MODE
58956aef503SMatt Jacob 		(*isp_get_firmware_p)(0, 1, device, &mdvp->dv_ispfw);
59056aef503SMatt Jacob #else
59156aef503SMatt Jacob 		(*isp_get_firmware_p)(0, 0, device, &mdvp->dv_ispfw);
59256aef503SMatt Jacob #endif
59356aef503SMatt Jacob 	}
59456aef503SMatt Jacob 
59556aef503SMatt Jacob 	/*
596d951bbcaSMatt Jacob 	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER
597d951bbcaSMatt Jacob 	 * are set.
598d951bbcaSMatt Jacob 	 */
599960f6939SMatt Jacob 	cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN |
600960f6939SMatt Jacob 		PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
60175c1e828SMatt Jacob 	if (IS_2300(isp)) {	/* per QLogic errata */
60275c1e828SMatt Jacob 		cmd &= ~PCIM_CMD_INVEN;
60375c1e828SMatt Jacob 	}
604fc087171SMatt Jacob 	if (IS_23XX(isp)) {
605fc087171SMatt Jacob 		/*
606fc087171SMatt Jacob 		 * Can't tell if ROM will hang on 'ABOUT FIRMWARE' command.
607fc087171SMatt Jacob 		 */
608fc087171SMatt Jacob 		isp->isp_touched = 1;
609fc087171SMatt Jacob 
610fc087171SMatt Jacob 	}
611b49c4674SMatt Jacob 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
612ab6d0040SMatt Jacob 
613d951bbcaSMatt Jacob 	/*
614222bb542SMatt Jacob 	 * Make sure the Cache Line Size register is set sensibly.
615d951bbcaSMatt Jacob 	 */
616960f6939SMatt Jacob 	data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
617ab6d0040SMatt Jacob 	if (data != linesz) {
618d951bbcaSMatt Jacob 		data = PCI_DFLT_LNSZ;
619d02373f1SMatt Jacob 		isp_prt(isp, ISP_LOGCONFIG, "set PCI line size to %d", data);
620960f6939SMatt Jacob 		pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
621d951bbcaSMatt Jacob 	}
622ab6d0040SMatt Jacob 
623d951bbcaSMatt Jacob 	/*
624d951bbcaSMatt Jacob 	 * Make sure the Latency Timer is sane.
625d951bbcaSMatt Jacob 	 */
626960f6939SMatt Jacob 	data = pci_read_config(dev, PCIR_LATTIMER, 1);
627d951bbcaSMatt Jacob 	if (data < PCI_DFLT_LTNCY) {
628d951bbcaSMatt Jacob 		data = PCI_DFLT_LTNCY;
629d02373f1SMatt Jacob 		isp_prt(isp, ISP_LOGCONFIG, "set PCI latency to %d", data);
630960f6939SMatt Jacob 		pci_write_config(dev, PCIR_LATTIMER, data, 1);
631d951bbcaSMatt Jacob 	}
632ab6d0040SMatt Jacob 
633ab6d0040SMatt Jacob 	/*
634ab6d0040SMatt Jacob 	 * Make sure we've disabled the ROM.
635ab6d0040SMatt Jacob 	 */
636960f6939SMatt Jacob 	data = pci_read_config(dev, PCIR_ROMADDR, 4);
637ab6d0040SMatt Jacob 	data &= ~1;
638960f6939SMatt Jacob 	pci_write_config(dev, PCIR_ROMADDR, data, 4);
63905fbcbb0SMatt Jacob 
640960f6939SMatt Jacob 	iqd = 0;
6415f96beb9SNate Lawson 	irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
6425f96beb9SNate Lawson 	    RF_ACTIVE | RF_SHAREABLE);
643960f6939SMatt Jacob 	if (irq == NULL) {
644960f6939SMatt Jacob 		device_printf(dev, "could not allocate interrupt\n");
645960f6939SMatt Jacob 		goto bad;
646960f6939SMatt Jacob 	}
647960f6939SMatt Jacob 
6486e5c5328SMatt Jacob 	tval = 0;
6496e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
6506e5c5328SMatt Jacob             "fwload_disable", &tval) == 0 && tval != 0) {
651222bb542SMatt Jacob 		isp->isp_confopts |= ISP_CFG_NORELOAD;
652222bb542SMatt Jacob 	}
6536e5c5328SMatt Jacob 	tval = 0;
6546e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
6556e5c5328SMatt Jacob             "ignore_nvram", &tval) == 0 && tval != 0) {
656222bb542SMatt Jacob 		isp->isp_confopts |= ISP_CFG_NONVRAM;
657222bb542SMatt Jacob 	}
6586e5c5328SMatt Jacob 	tval = 0;
6596e5c5328SMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
6606e5c5328SMatt Jacob             "fullduplex", &tval) == 0 && tval != 0) {
661222bb542SMatt Jacob 		isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
662222bb542SMatt Jacob 	}
663d134aa0bSMatt Jacob #ifdef	ISP_FW_CRASH_DUMP
664d134aa0bSMatt Jacob 	tval = 0;
665d134aa0bSMatt Jacob         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
666d134aa0bSMatt Jacob             "fw_dump_enable", &tval) == 0 && tval != 0) {
667d134aa0bSMatt Jacob 		size_t amt = 0;
668d134aa0bSMatt Jacob 		if (IS_2200(isp)) {
669d134aa0bSMatt Jacob 			amt = QLA2200_RISC_IMAGE_DUMP_SIZE;
670d134aa0bSMatt Jacob 		} else if (IS_23XX(isp)) {
671d134aa0bSMatt Jacob 			amt = QLA2300_RISC_IMAGE_DUMP_SIZE;
672d134aa0bSMatt Jacob 		}
673d134aa0bSMatt Jacob 		if (amt) {
674d134aa0bSMatt Jacob 			FCPARAM(isp)->isp_dump_data =
675a163d034SWarner Losh 			    malloc(amt, M_DEVBUF, M_WAITOK | M_ZERO);
676d134aa0bSMatt Jacob 		} else {
677d134aa0bSMatt Jacob 			device_printf(dev,
678d134aa0bSMatt Jacob 			    "f/w crash dumps not supported for this model\n");
679d134aa0bSMatt Jacob 		}
680d134aa0bSMatt Jacob 	}
681d134aa0bSMatt Jacob #endif
682222bb542SMatt Jacob 
6836e5c5328SMatt Jacob 	sptr = 0;
6846e5c5328SMatt Jacob         if (resource_string_value(device_get_name(dev), device_get_unit(dev),
685559a1ad2SMatt Jacob             "topology", (const char **) &sptr) == 0 && sptr != 0) {
6866e5c5328SMatt Jacob 		if (strcmp(sptr, "lport") == 0) {
6876e5c5328SMatt Jacob 			isp->isp_confopts |= ISP_CFG_LPORT;
6886e5c5328SMatt Jacob 		} else if (strcmp(sptr, "nport") == 0) {
6896e5c5328SMatt Jacob 			isp->isp_confopts |= ISP_CFG_NPORT;
6906e5c5328SMatt Jacob 		} else if (strcmp(sptr, "lport-only") == 0) {
6916e5c5328SMatt Jacob 			isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
6926e5c5328SMatt Jacob 		} else if (strcmp(sptr, "nport-only") == 0) {
6936e5c5328SMatt Jacob 			isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
6949637d68cSMatt Jacob 		}
6956e5c5328SMatt Jacob 	}
6966e5c5328SMatt Jacob 
6979637d68cSMatt Jacob 	/*
6986e5c5328SMatt Jacob 	 * Because the resource_*_value functions can neither return
6996e5c5328SMatt Jacob 	 * 64 bit integer values, nor can they be directly coerced
7006e5c5328SMatt Jacob 	 * to interpret the right hand side of the assignment as
7016e5c5328SMatt Jacob 	 * you want them to interpret it, we have to force WWN
7026e5c5328SMatt Jacob 	 * hint replacement to specify WWN strings with a leading
7036e5c5328SMatt Jacob 	 * 'w' (e..g w50000000aaaa0001). Sigh.
7049637d68cSMatt Jacob 	 */
7056e5c5328SMatt Jacob 	sptr = 0;
7066e5c5328SMatt Jacob 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
707559a1ad2SMatt Jacob             "portwwn", (const char **) &sptr);
7086e5c5328SMatt Jacob 	if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
7096e5c5328SMatt Jacob 		char *eptr = 0;
7106e5c5328SMatt Jacob 		isp->isp_osinfo.default_port_wwn = strtouq(sptr, &eptr, 16);
7116e5c5328SMatt Jacob 		if (eptr < sptr + 16 || isp->isp_osinfo.default_port_wwn == 0) {
7126e5c5328SMatt Jacob 			device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
7136e5c5328SMatt Jacob 			isp->isp_osinfo.default_port_wwn = 0;
7149637d68cSMatt Jacob 		} else {
7153f02619fSMatt Jacob 			isp->isp_confopts |= ISP_CFG_OWNWWPN;
716222bb542SMatt Jacob 		}
7176e5c5328SMatt Jacob 	}
7186e5c5328SMatt Jacob 	if (isp->isp_osinfo.default_port_wwn == 0) {
7196e5c5328SMatt Jacob 		isp->isp_osinfo.default_port_wwn = 0x400000007F000009ull;
7206e5c5328SMatt Jacob 	}
7216e5c5328SMatt Jacob 
7226e5c5328SMatt Jacob 	sptr = 0;
7236e5c5328SMatt Jacob 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
724559a1ad2SMatt Jacob             "nodewwn", (const char **) &sptr);
7256e5c5328SMatt Jacob 	if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
7266e5c5328SMatt Jacob 		char *eptr = 0;
7276e5c5328SMatt Jacob 		isp->isp_osinfo.default_node_wwn = strtouq(sptr, &eptr, 16);
7286e5c5328SMatt Jacob 		if (eptr < sptr + 16 || isp->isp_osinfo.default_node_wwn == 0) {
7296e5c5328SMatt Jacob 			device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
7306e5c5328SMatt Jacob 			isp->isp_osinfo.default_node_wwn = 0;
7316e5c5328SMatt Jacob 		} else {
7323f02619fSMatt Jacob 			isp->isp_confopts |= ISP_CFG_OWNWWNN;
7336e5c5328SMatt Jacob 		}
7346e5c5328SMatt Jacob 	}
7356e5c5328SMatt Jacob 	if (isp->isp_osinfo.default_node_wwn == 0) {
7366e5c5328SMatt Jacob 		isp->isp_osinfo.default_node_wwn = 0x400000007F000009ull;
7376e5c5328SMatt Jacob 	}
7386e5c5328SMatt Jacob 
73943722a42SMatt Jacob 	isp->isp_osinfo.default_id = -1;
740ed753e82SMatt Jacob 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
741ed753e82SMatt Jacob             "iid", &tval) == 0) {
742ed753e82SMatt Jacob 		isp->isp_osinfo.default_id = tval;
743ed753e82SMatt Jacob 		isp->isp_confopts |= ISP_CFG_OWNLOOPID;
744ed753e82SMatt Jacob 	}
74543722a42SMatt Jacob 	if (isp->isp_osinfo.default_id == -1) {
746ed753e82SMatt Jacob 		if (IS_FC(isp)) {
747ed753e82SMatt Jacob 			isp->isp_osinfo.default_id = 109;
748ed753e82SMatt Jacob 		} else {
749ed753e82SMatt Jacob 			isp->isp_osinfo.default_id = 7;
750ed753e82SMatt Jacob 		}
751ed753e82SMatt Jacob 	}
752ed753e82SMatt Jacob 
753ed753e82SMatt Jacob 	isp_debug = 0;
7546e5c5328SMatt Jacob         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
7556e5c5328SMatt Jacob             "debug", &isp_debug);
756f09b1922SMatt Jacob 
757f09b1922SMatt Jacob 	/* Make sure the lock is set up. */
7586008862bSJohn Baldwin 	mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
759f09b1922SMatt Jacob 	locksetup++;
760f09b1922SMatt Jacob 
76172429e49SMatt Jacob 	if (bus_setup_intr(dev, irq, ISP_IFLAGS, isp_pci_intr, isp, &pcs->ih)) {
762f09b1922SMatt Jacob 		device_printf(dev, "could not setup interrupt\n");
763f09b1922SMatt Jacob 		goto bad;
764f09b1922SMatt Jacob 	}
765960f6939SMatt Jacob 
76605fbcbb0SMatt Jacob 	/*
767d02373f1SMatt Jacob 	 * Set up logging levels.
768d02373f1SMatt Jacob 	 */
769d02373f1SMatt Jacob 	if (isp_debug) {
770d02373f1SMatt Jacob 		isp->isp_dblev = isp_debug;
771d02373f1SMatt Jacob 	} else {
772d02373f1SMatt Jacob 		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
773d02373f1SMatt Jacob 	}
774d02373f1SMatt Jacob 	if (bootverbose)
775f7dddf8aSMatt Jacob 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
776d02373f1SMatt Jacob 
777d02373f1SMatt Jacob 	/*
77875c1e828SMatt Jacob 	 * Last minute checks...
77975c1e828SMatt Jacob 	 */
780e5265237SMatt Jacob 	if (IS_23XX(isp)) {
78175c1e828SMatt Jacob 		isp->isp_port = pci_get_function(dev);
78275c1e828SMatt Jacob 	}
78375c1e828SMatt Jacob 
78475c1e828SMatt Jacob 	/*
78505fbcbb0SMatt Jacob 	 * Make sure we're in reset state.
78605fbcbb0SMatt Jacob 	 */
7873395b056SMatt Jacob 	ISP_LOCK(isp);
78865adb54cSMatt Jacob 	isp_reset(isp);
78965adb54cSMatt Jacob 	if (isp->isp_state != ISP_RESETSTATE) {
7903395b056SMatt Jacob 		ISP_UNLOCK(isp);
791960f6939SMatt Jacob 		goto bad;
79265adb54cSMatt Jacob 	}
79365adb54cSMatt Jacob 	isp_init(isp);
794b9b599feSMatt Jacob 	if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_INITSTATE) {
79565adb54cSMatt Jacob 		isp_uninit(isp);
7963395b056SMatt Jacob 		ISP_UNLOCK(isp);
797960f6939SMatt Jacob 		goto bad;
798d59bd469SMatt Jacob 	}
79965adb54cSMatt Jacob 	isp_attach(isp);
800b9b599feSMatt Jacob 	if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_RUNSTATE) {
80165adb54cSMatt Jacob 		isp_uninit(isp);
8023395b056SMatt Jacob 		ISP_UNLOCK(isp);
803960f6939SMatt Jacob 		goto bad;
804960f6939SMatt Jacob 	}
80556aef503SMatt Jacob 	/*
80656aef503SMatt Jacob 	 * XXXX: Here is where we might unload the f/w module
80756aef503SMatt Jacob 	 * XXXX: (or decrease the reference count to it).
80856aef503SMatt Jacob 	 */
8093395b056SMatt Jacob 	ISP_UNLOCK(isp);
810960f6939SMatt Jacob 	return (0);
811960f6939SMatt Jacob 
812960f6939SMatt Jacob bad:
813960f6939SMatt Jacob 
814960f6939SMatt Jacob 	if (pcs && pcs->ih) {
815960f6939SMatt Jacob 		(void) bus_teardown_intr(dev, irq, pcs->ih);
816960f6939SMatt Jacob 	}
817960f6939SMatt Jacob 
8183395b056SMatt Jacob 	if (locksetup && isp) {
8193395b056SMatt Jacob 		mtx_destroy(&isp->isp_osinfo.lock);
8203395b056SMatt Jacob 	}
8213395b056SMatt Jacob 
822960f6939SMatt Jacob 	if (irq) {
823960f6939SMatt Jacob 		(void) bus_release_resource(dev, SYS_RES_IRQ, iqd, irq);
824960f6939SMatt Jacob 	}
8253395b056SMatt Jacob 
8263395b056SMatt Jacob 
827960f6939SMatt Jacob 	if (regs) {
828960f6939SMatt Jacob 		(void) bus_release_resource(dev, rtp, rgd, regs);
829960f6939SMatt Jacob 	}
8303395b056SMatt Jacob 
831960f6939SMatt Jacob 	if (pcs) {
832960f6939SMatt Jacob 		if (pcs->pci_isp.isp_param)
833960f6939SMatt Jacob 			free(pcs->pci_isp.isp_param, M_DEVBUF);
83465adb54cSMatt Jacob 		free(pcs, M_DEVBUF);
83565adb54cSMatt Jacob 	}
8363395b056SMatt Jacob 
83756aef503SMatt Jacob 	/*
83856aef503SMatt Jacob 	 * XXXX: Here is where we might unload the f/w module
83956aef503SMatt Jacob 	 * XXXX: (or decrease the reference count to it).
84056aef503SMatt Jacob 	 */
841960f6939SMatt Jacob 	return (ENXIO);
84265adb54cSMatt Jacob }
84365adb54cSMatt Jacob 
844f09b1922SMatt Jacob static void
845f09b1922SMatt Jacob isp_pci_intr(void *arg)
846f09b1922SMatt Jacob {
847f09b1922SMatt Jacob 	struct ispsoftc *isp = arg;
848126ec864SMatt Jacob 	u_int16_t isr, sema, mbox;
849126ec864SMatt Jacob 
850f09b1922SMatt Jacob 	ISP_LOCK(isp);
851126ec864SMatt Jacob 	isp->isp_intcnt++;
852126ec864SMatt Jacob 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
853126ec864SMatt Jacob 		isp->isp_intbogus++;
854126ec864SMatt Jacob 	} else {
855b96934e8SMatt Jacob 		int iok = isp->isp_osinfo.intsok;
856b96934e8SMatt Jacob 		isp->isp_osinfo.intsok = 0;
857126ec864SMatt Jacob 		isp_intr(isp, isr, sema, mbox);
858b96934e8SMatt Jacob 		isp->isp_osinfo.intsok = iok;
859126ec864SMatt Jacob 	}
860f09b1922SMatt Jacob 	ISP_UNLOCK(isp);
861f09b1922SMatt Jacob }
862f09b1922SMatt Jacob 
863126ec864SMatt Jacob 
864126ec864SMatt Jacob #define	IspVirt2Off(a, x)	\
865126ec864SMatt Jacob 	(((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
866126ec864SMatt Jacob 	_BLK_REG_SHFT] + ((x) & 0xff))
867126ec864SMatt Jacob 
868126ec864SMatt Jacob #define	BXR2(pcs, off)		\
869126ec864SMatt Jacob 	bus_space_read_2(pcs->pci_st, pcs->pci_sh, off)
870126ec864SMatt Jacob #define	BXW2(pcs, off, v)	\
871126ec864SMatt Jacob 	bus_space_write_2(pcs->pci_st, pcs->pci_sh, off, v)
872126ec864SMatt Jacob 
873126ec864SMatt Jacob 
87453af7d22SMatt Jacob static __inline int
875126ec864SMatt Jacob isp_pci_rd_debounced(struct ispsoftc *isp, int off, u_int16_t *rp)
876126ec864SMatt Jacob {
877126ec864SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
878126ec864SMatt Jacob 	u_int16_t val0, val1;
879126ec864SMatt Jacob 	int i = 0;
880126ec864SMatt Jacob 
881126ec864SMatt Jacob 	do {
882126ec864SMatt Jacob 		val0 = BXR2(pcs, IspVirt2Off(isp, off));
883126ec864SMatt Jacob 		val1 = BXR2(pcs, IspVirt2Off(isp, off));
884126ec864SMatt Jacob 	} while (val0 != val1 && ++i < 1000);
885126ec864SMatt Jacob 	if (val0 != val1) {
886126ec864SMatt Jacob 		return (1);
887126ec864SMatt Jacob 	}
888126ec864SMatt Jacob 	*rp = val0;
889126ec864SMatt Jacob 	return (0);
890126ec864SMatt Jacob }
891126ec864SMatt Jacob 
892126ec864SMatt Jacob static int
893126ec864SMatt Jacob isp_pci_rd_isr(struct ispsoftc *isp, u_int16_t *isrp,
894126ec864SMatt Jacob     u_int16_t *semap, u_int16_t *mbp)
895126ec864SMatt Jacob {
896126ec864SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
897126ec864SMatt Jacob 	u_int16_t isr, sema;
898126ec864SMatt Jacob 
899126ec864SMatt Jacob 	if (IS_2100(isp)) {
900126ec864SMatt Jacob 		if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
901126ec864SMatt Jacob 		    return (0);
902126ec864SMatt Jacob 		}
903126ec864SMatt Jacob 		if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
904126ec864SMatt Jacob 		    return (0);
905126ec864SMatt Jacob 		}
906126ec864SMatt Jacob 	} else {
907126ec864SMatt Jacob 		isr = BXR2(pcs, IspVirt2Off(isp, BIU_ISR));
908126ec864SMatt Jacob 		sema = BXR2(pcs, IspVirt2Off(isp, BIU_SEMA));
909126ec864SMatt Jacob 	}
910126ec864SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
911126ec864SMatt Jacob 	isr &= INT_PENDING_MASK(isp);
912126ec864SMatt Jacob 	sema &= BIU_SEMA_LOCK;
913126ec864SMatt Jacob 	if (isr == 0 && sema == 0) {
914126ec864SMatt Jacob 		return (0);
915126ec864SMatt Jacob 	}
916126ec864SMatt Jacob 	*isrp = isr;
917126ec864SMatt Jacob 	if ((*semap = sema) != 0) {
918126ec864SMatt Jacob 		if (IS_2100(isp)) {
919126ec864SMatt Jacob 			if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
920126ec864SMatt Jacob 				return (0);
921126ec864SMatt Jacob 			}
922126ec864SMatt Jacob 		} else {
923126ec864SMatt Jacob 			*mbp = BXR2(pcs, IspVirt2Off(isp, OUTMAILBOX0));
924126ec864SMatt Jacob 		}
925126ec864SMatt Jacob 	}
926126ec864SMatt Jacob 	return (1);
927126ec864SMatt Jacob }
928126ec864SMatt Jacob 
929126ec864SMatt Jacob static int
930126ec864SMatt Jacob isp_pci_rd_isr_2300(struct ispsoftc *isp, u_int16_t *isrp,
931126ec864SMatt Jacob     u_int16_t *semap, u_int16_t *mbox0p)
932126ec864SMatt Jacob {
933126ec864SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
934126ec864SMatt Jacob 	u_int32_t r2hisr;
935126ec864SMatt Jacob 
9363bd40330SMatt Jacob 	if (!(BXR2(pcs, IspVirt2Off(isp, BIU_ISR) & BIU2100_ISR_RISC_INT))) {
9373bd40330SMatt Jacob 		*isrp = 0;
938db4fa023SMatt Jacob 		return (0);
9393bd40330SMatt Jacob 	}
940126ec864SMatt Jacob 	r2hisr = bus_space_read_4(pcs->pci_st, pcs->pci_sh,
941126ec864SMatt Jacob 	    IspVirt2Off(pcs, BIU_R2HSTSLO));
942126ec864SMatt Jacob 	isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
943126ec864SMatt Jacob 	if ((r2hisr & BIU_R2HST_INTR) == 0) {
944126ec864SMatt Jacob 		*isrp = 0;
945126ec864SMatt Jacob 		return (0);
946126ec864SMatt Jacob 	}
947126ec864SMatt Jacob 	switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
948126ec864SMatt Jacob 	case ISPR2HST_ROM_MBX_OK:
949126ec864SMatt Jacob 	case ISPR2HST_ROM_MBX_FAIL:
950126ec864SMatt Jacob 	case ISPR2HST_MBX_OK:
951126ec864SMatt Jacob 	case ISPR2HST_MBX_FAIL:
952126ec864SMatt Jacob 	case ISPR2HST_ASYNC_EVENT:
953126ec864SMatt Jacob 		*isrp = r2hisr & 0xffff;
954126ec864SMatt Jacob 		*mbox0p = (r2hisr >> 16);
955126ec864SMatt Jacob 		*semap = 1;
956126ec864SMatt Jacob 		return (1);
957fc3bbaaaSMatt Jacob 	case ISPR2HST_RIO_16:
958fc3bbaaaSMatt Jacob 		*isrp = r2hisr & 0xffff;
959fc3bbaaaSMatt Jacob 		*mbox0p = ASYNC_RIO1;
960fc3bbaaaSMatt Jacob 		*semap = 1;
961fc3bbaaaSMatt Jacob 		return (1);
962fc3bbaaaSMatt Jacob 	case ISPR2HST_FPOST:
963fc3bbaaaSMatt Jacob 		*isrp = r2hisr & 0xffff;
964fc3bbaaaSMatt Jacob 		*mbox0p = ASYNC_CMD_CMPLT;
965fc3bbaaaSMatt Jacob 		*semap = 1;
966fc3bbaaaSMatt Jacob 		return (1);
967fc3bbaaaSMatt Jacob 	case ISPR2HST_FPOST_CTIO:
968fc3bbaaaSMatt Jacob 		*isrp = r2hisr & 0xffff;
969fc3bbaaaSMatt Jacob 		*mbox0p = ASYNC_CTIO_DONE;
970fc3bbaaaSMatt Jacob 		*semap = 1;
971fc3bbaaaSMatt Jacob 		return (1);
972126ec864SMatt Jacob 	case ISPR2HST_RSPQ_UPDATE:
973126ec864SMatt Jacob 		*isrp = r2hisr & 0xffff;
974126ec864SMatt Jacob 		*mbox0p = 0;
975126ec864SMatt Jacob 		*semap = 0;
976126ec864SMatt Jacob 		return (1);
977126ec864SMatt Jacob 	default:
978126ec864SMatt Jacob 		return (0);
979126ec864SMatt Jacob 	}
980126ec864SMatt Jacob }
981126ec864SMatt Jacob 
98265adb54cSMatt Jacob static u_int16_t
983126ec864SMatt Jacob isp_pci_rd_reg(struct ispsoftc *isp, int regoff)
98465adb54cSMatt Jacob {
98565adb54cSMatt Jacob 	u_int16_t rv;
98665adb54cSMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
987126ec864SMatt Jacob 	int oldconf = 0;
98865adb54cSMatt Jacob 
989d59bd469SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
99065adb54cSMatt Jacob 		/*
99165adb54cSMatt Jacob 		 * We will assume that someone has paused the RISC processor.
99265adb54cSMatt Jacob 		 */
993126ec864SMatt Jacob 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
994126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
995126ec864SMatt Jacob 		    oldconf | BIU_PCI_CONF1_SXP);
99665adb54cSMatt Jacob 	}
997126ec864SMatt Jacob 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
998d59bd469SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
999126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
100065adb54cSMatt Jacob 	}
100165adb54cSMatt Jacob 	return (rv);
100265adb54cSMatt Jacob }
100365adb54cSMatt Jacob 
100465adb54cSMatt Jacob static void
1005126ec864SMatt Jacob isp_pci_wr_reg(struct ispsoftc *isp, int regoff, u_int16_t val)
100665adb54cSMatt Jacob {
100765adb54cSMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1008126ec864SMatt Jacob 	int oldconf = 0;
1009d59bd469SMatt Jacob 
1010d59bd469SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
101165adb54cSMatt Jacob 		/*
101265adb54cSMatt Jacob 		 * We will assume that someone has paused the RISC processor.
101365adb54cSMatt Jacob 		 */
1014126ec864SMatt Jacob 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1015126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1016126ec864SMatt Jacob 		    oldconf | BIU_PCI_CONF1_SXP);
101765adb54cSMatt Jacob 	}
1018126ec864SMatt Jacob 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
1019d59bd469SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1020126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
102165adb54cSMatt Jacob 	}
102265adb54cSMatt Jacob }
102365adb54cSMatt Jacob 
1024d59bd469SMatt Jacob static u_int16_t
1025126ec864SMatt Jacob isp_pci_rd_reg_1080(struct ispsoftc *isp, int regoff)
1026d59bd469SMatt Jacob {
102722e1dc85SMatt Jacob 	u_int16_t rv, oc = 0;
1028d59bd469SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1029d59bd469SMatt Jacob 
103022e1dc85SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
103122e1dc85SMatt Jacob 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
103222e1dc85SMatt Jacob 		u_int16_t tc;
1033d59bd469SMatt Jacob 		/*
1034d59bd469SMatt Jacob 		 * We will assume that someone has paused the RISC processor.
1035d59bd469SMatt Jacob 		 */
1036126ec864SMatt Jacob 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
103722e1dc85SMatt Jacob 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
103822e1dc85SMatt Jacob 		if (regoff & SXP_BANK1_SELECT)
103922e1dc85SMatt Jacob 			tc |= BIU_PCI1080_CONF1_SXP1;
104022e1dc85SMatt Jacob 		else
104122e1dc85SMatt Jacob 			tc |= BIU_PCI1080_CONF1_SXP0;
1042126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
1043d59bd469SMatt Jacob 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1044126ec864SMatt Jacob 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1045126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1046126ec864SMatt Jacob 		    oc | BIU_PCI1080_CONF1_DMA);
1047d59bd469SMatt Jacob 	}
1048126ec864SMatt Jacob 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
104922e1dc85SMatt Jacob 	if (oc) {
1050126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
1051d59bd469SMatt Jacob 	}
1052d59bd469SMatt Jacob 	return (rv);
1053d59bd469SMatt Jacob }
1054d59bd469SMatt Jacob 
1055d59bd469SMatt Jacob static void
1056126ec864SMatt Jacob isp_pci_wr_reg_1080(struct ispsoftc *isp, int regoff, u_int16_t val)
1057d59bd469SMatt Jacob {
1058d59bd469SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1059126ec864SMatt Jacob 	int oc = 0;
1060d59bd469SMatt Jacob 
106122e1dc85SMatt Jacob 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
106222e1dc85SMatt Jacob 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
106322e1dc85SMatt Jacob 		u_int16_t tc;
1064d59bd469SMatt Jacob 		/*
1065d59bd469SMatt Jacob 		 * We will assume that someone has paused the RISC processor.
1066d59bd469SMatt Jacob 		 */
1067126ec864SMatt Jacob 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
106822e1dc85SMatt Jacob 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
106922e1dc85SMatt Jacob 		if (regoff & SXP_BANK1_SELECT)
107022e1dc85SMatt Jacob 			tc |= BIU_PCI1080_CONF1_SXP1;
107122e1dc85SMatt Jacob 		else
107222e1dc85SMatt Jacob 			tc |= BIU_PCI1080_CONF1_SXP0;
1073126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
1074d59bd469SMatt Jacob 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1075126ec864SMatt Jacob 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1076126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1077126ec864SMatt Jacob 		    oc | BIU_PCI1080_CONF1_DMA);
1078d59bd469SMatt Jacob 	}
1079126ec864SMatt Jacob 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
108022e1dc85SMatt Jacob 	if (oc) {
1081126ec864SMatt Jacob 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
1082d59bd469SMatt Jacob 	}
1083d59bd469SMatt Jacob }
1084d59bd469SMatt Jacob 
1085d720e6d5SJustin T. Gibbs 
1086222bb542SMatt Jacob struct imush {
1087222bb542SMatt Jacob 	struct ispsoftc *isp;
1088222bb542SMatt Jacob 	int error;
1089222bb542SMatt Jacob };
1090222bb542SMatt Jacob 
10911923f739SMatt Jacob static void imc(void *, bus_dma_segment_t *, int, int);
10921923f739SMatt Jacob 
1093d720e6d5SJustin T. Gibbs static void
10941923f739SMatt Jacob imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1095d720e6d5SJustin T. Gibbs {
1096222bb542SMatt Jacob 	struct imush *imushp = (struct imush *) arg;
1097222bb542SMatt Jacob 	if (error) {
1098222bb542SMatt Jacob 		imushp->error = error;
1099222bb542SMatt Jacob 	} else {
11001923f739SMatt Jacob 		struct ispsoftc *isp =imushp->isp;
11011923f739SMatt Jacob 		bus_addr_t addr = segs->ds_addr;
11021923f739SMatt Jacob 
11031923f739SMatt Jacob 		isp->isp_rquest_dma = addr;
11041923f739SMatt Jacob 		addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
11051923f739SMatt Jacob 		isp->isp_result_dma = addr;
11061923f739SMatt Jacob 		if (IS_FC(isp)) {
11071923f739SMatt Jacob 			addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
11081923f739SMatt Jacob 			FCPARAM(isp)->isp_scdma = addr;
11091923f739SMatt Jacob 		}
1110222bb542SMatt Jacob 	}
1111d720e6d5SJustin T. Gibbs }
1112d720e6d5SJustin T. Gibbs 
1113029f13c6SMatt Jacob /*
1114029f13c6SMatt Jacob  * Should be BUS_SPACE_MAXSIZE, but MAXPHYS is larger than BUS_SPACE_MAXSIZE
1115029f13c6SMatt Jacob  */
1116029f13c6SMatt Jacob #define ISP_NSEGS ((MAXPHYS / PAGE_SIZE) + 1)
1117d720e6d5SJustin T. Gibbs 
1118d720e6d5SJustin T. Gibbs static int
111917e318c6SMatt Jacob isp_pci_mbxdma(struct ispsoftc *isp)
1120d720e6d5SJustin T. Gibbs {
11211923f739SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1122d720e6d5SJustin T. Gibbs 	caddr_t base;
1123d720e6d5SJustin T. Gibbs 	u_int32_t len;
11241923f739SMatt Jacob 	int i, error, ns;
112553af7d22SMatt Jacob 	bus_size_t slim;	/* segment size */
11264b2dc3c4SScott Long 	bus_addr_t llim;	/* low limit of unavailable dma */
112751effc8cSMatt Jacob 	bus_addr_t hlim;	/* high limit of unavailable dma */
1128222bb542SMatt Jacob 	struct imush im;
1129222bb542SMatt Jacob 
1130a95ae193SMatt Jacob 	/*
1131a95ae193SMatt Jacob 	 * Already been here? If so, leave...
1132a95ae193SMatt Jacob 	 */
1133a95ae193SMatt Jacob 	if (isp->isp_rquest) {
1134a95ae193SMatt Jacob 		return (0);
1135a95ae193SMatt Jacob 	}
1136a95ae193SMatt Jacob 
1137435a9337SMatt Jacob #ifdef	ISP_DAC_SUPPORTED
113853af7d22SMatt Jacob 	llim = hlim = BUS_SPACE_MAXADDR;
1139435a9337SMatt Jacob #else
114053af7d22SMatt Jacob 	llim = BUS_SPACE_MAXADDR_32BIT;
114153af7d22SMatt Jacob 	hlim = BUS_SPACE_MAXADDR;
1142435a9337SMatt Jacob #endif
11431923f739SMatt Jacob 	if (IS_ULTRA2(isp) || IS_FC(isp) || IS_1240(isp)) {
114453af7d22SMatt Jacob 		slim = (bus_size_t) (1ULL << 32);
11451923f739SMatt Jacob 	} else {
114653af7d22SMatt Jacob 		slim = (1 << 24);
11471923f739SMatt Jacob 	}
11481923f739SMatt Jacob 
114972429e49SMatt Jacob 	ISP_UNLOCK(isp);
115053af7d22SMatt Jacob 	if (bus_dma_tag_create(NULL, 1, slim, llim, hlim,
1151f6b1c44dSScott Long 	    NULL, NULL, BUS_SPACE_MAXSIZE, ISP_NSEGS, slim, 0,
1152f6b1c44dSScott Long 	    busdma_lock_mutex, &Giant, &pcs->dmat)) {
11531923f739SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
115472429e49SMatt Jacob 		ISP_LOCK(isp);
11551923f739SMatt Jacob 		return(1);
11561923f739SMatt Jacob 	}
11571923f739SMatt Jacob 
11581923f739SMatt Jacob 
1159d02373f1SMatt Jacob 	len = sizeof (XS_T **) * isp->isp_maxcmds;
1160a163d034SWarner Losh 	isp->isp_xflist = (XS_T **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1161a95ae193SMatt Jacob 	if (isp->isp_xflist == NULL) {
1162d02373f1SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
116372429e49SMatt Jacob 		ISP_LOCK(isp);
1164a95ae193SMatt Jacob 		return (1);
1165a95ae193SMatt Jacob 	}
116651e23558SNate Lawson #ifdef	ISP_TARGET_MODE
116751e23558SNate Lawson 	len = sizeof (void **) * isp->isp_maxcmds;
116851e23558SNate Lawson 	isp->isp_tgtlist = (void **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
116951e23558SNate Lawson 	if (isp->isp_tgtlist == NULL) {
117051e23558SNate Lawson 		isp_prt(isp, ISP_LOGERR, "cannot alloc tgtlist array");
117151e23558SNate Lawson 		ISP_LOCK(isp);
117251e23558SNate Lawson 		return (1);
117351e23558SNate Lawson 	}
117451e23558SNate Lawson #endif
1175a95ae193SMatt Jacob 	len = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
1176a163d034SWarner Losh 	pcs->dmaps = (bus_dmamap_t *) malloc(len, M_DEVBUF,  M_WAITOK);
11771923f739SMatt Jacob 	if (pcs->dmaps == NULL) {
11781923f739SMatt Jacob 		isp_prt(isp, ISP_LOGERR, "can't alloc dma map storage");
1179a95ae193SMatt Jacob 		free(isp->isp_xflist, M_DEVBUF);
118051e23558SNate Lawson #ifdef	ISP_TARGET_MODE
118151e23558SNate Lawson 		free(isp->isp_tgtlist, M_DEVBUF);
118251e23558SNate Lawson #endif
118372429e49SMatt Jacob 		ISP_LOCK(isp);
1184a95ae193SMatt Jacob 		return (1);
1185a95ae193SMatt Jacob 	}
1186a95ae193SMatt Jacob 
1187d720e6d5SJustin T. Gibbs 	/*
1188d720e6d5SJustin T. Gibbs 	 * Allocate and map the request, result queues, plus FC scratch area.
1189d720e6d5SJustin T. Gibbs 	 */
1190d02373f1SMatt Jacob 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1191d02373f1SMatt Jacob 	len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1192222bb542SMatt Jacob 	if (IS_FC(isp)) {
1193d720e6d5SJustin T. Gibbs 		len += ISP2100_SCRLEN;
1194d720e6d5SJustin T. Gibbs 	}
11951923f739SMatt Jacob 
11961923f739SMatt Jacob 	ns = (len / PAGE_SIZE) + 1;
119753af7d22SMatt Jacob 	/*
119853af7d22SMatt Jacob 	 * Create a tag for the control spaces- force it to within 32 bits.
119953af7d22SMatt Jacob 	 */
120053af7d22SMatt Jacob 	if (bus_dma_tag_create(pcs->dmat, QENTRY_LEN, slim,
120153af7d22SMatt Jacob 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1202f6b1c44dSScott Long 	    NULL, NULL, len, ns, slim, 0, busdma_lock_mutex, &Giant,
1203f6b1c44dSScott Long 	    &isp->isp_cdmat)) {
1204f7dddf8aSMatt Jacob 		isp_prt(isp, ISP_LOGERR,
1205f7dddf8aSMatt Jacob 		    "cannot create a dma tag for control spaces");
12061923f739SMatt Jacob 		free(pcs->dmaps, M_DEVBUF);
1207a95ae193SMatt Jacob 		free(isp->isp_xflist, M_DEVBUF);
120851e23558SNate Lawson #ifdef	ISP_TARGET_MODE
120951e23558SNate Lawson 		free(isp->isp_tgtlist, M_DEVBUF);
121051e23558SNate Lawson #endif
121172429e49SMatt Jacob 		ISP_LOCK(isp);
1212d720e6d5SJustin T. Gibbs 		return (1);
1213d720e6d5SJustin T. Gibbs 	}
1214d720e6d5SJustin T. Gibbs 
12151923f739SMatt Jacob 	if (bus_dmamem_alloc(isp->isp_cdmat, (void **)&base, BUS_DMA_NOWAIT,
12161923f739SMatt Jacob 	    &isp->isp_cdmap) != 0) {
1217f7dddf8aSMatt Jacob 		isp_prt(isp, ISP_LOGERR,
12181923f739SMatt Jacob 		    "cannot allocate %d bytes of CCB memory", len);
12191923f739SMatt Jacob 		bus_dma_tag_destroy(isp->isp_cdmat);
1220a95ae193SMatt Jacob 		free(isp->isp_xflist, M_DEVBUF);
122151e23558SNate Lawson #ifdef	ISP_TARGET_MODE
122251e23558SNate Lawson 		free(isp->isp_tgtlist, M_DEVBUF);
122351e23558SNate Lawson #endif
12241923f739SMatt Jacob 		free(pcs->dmaps, M_DEVBUF);
122572429e49SMatt Jacob 		ISP_LOCK(isp);
1226222bb542SMatt Jacob 		return (1);
1227222bb542SMatt Jacob 	}
1228d720e6d5SJustin T. Gibbs 
1229a95ae193SMatt Jacob 	for (i = 0; i < isp->isp_maxcmds; i++) {
12301923f739SMatt Jacob 		error = bus_dmamap_create(pcs->dmat, 0, &pcs->dmaps[i]);
1231d720e6d5SJustin T. Gibbs 		if (error) {
1232f7dddf8aSMatt Jacob 			isp_prt(isp, ISP_LOGERR,
1233f7dddf8aSMatt Jacob 			    "error %d creating per-cmd DMA maps", error);
12341923f739SMatt Jacob 			while (--i >= 0) {
12351923f739SMatt Jacob 				bus_dmamap_destroy(pcs->dmat, pcs->dmaps[i]);
12361923f739SMatt Jacob 			}
12371923f739SMatt Jacob 			goto bad;
1238d720e6d5SJustin T. Gibbs 		}
1239d720e6d5SJustin T. Gibbs 	}
1240a95ae193SMatt Jacob 
12411923f739SMatt Jacob 	im.isp = isp;
1242222bb542SMatt Jacob 	im.error = 0;
12431923f739SMatt Jacob 	bus_dmamap_load(isp->isp_cdmat, isp->isp_cdmap, base, len, imc, &im, 0);
1244222bb542SMatt Jacob 	if (im.error) {
1245f7dddf8aSMatt Jacob 		isp_prt(isp, ISP_LOGERR,
12461923f739SMatt Jacob 		    "error %d loading dma map for control areas", im.error);
12471923f739SMatt Jacob 		goto bad;
1248222bb542SMatt Jacob 	}
12491923f739SMatt Jacob 
12501923f739SMatt Jacob 	isp->isp_rquest = base;
12511923f739SMatt Jacob 	base += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
12521923f739SMatt Jacob 	isp->isp_result = base;
12531923f739SMatt Jacob 	if (IS_FC(isp)) {
12541923f739SMatt Jacob 		base += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
12551923f739SMatt Jacob 		FCPARAM(isp)->isp_scratch = base;
125692c49d78SMatt Jacob 	}
125772429e49SMatt Jacob 	ISP_LOCK(isp);
1258d720e6d5SJustin T. Gibbs 	return (0);
12591923f739SMatt Jacob 
12601923f739SMatt Jacob bad:
12611923f739SMatt Jacob 	bus_dmamem_free(isp->isp_cdmat, base, isp->isp_cdmap);
12621923f739SMatt Jacob 	bus_dma_tag_destroy(isp->isp_cdmat);
12631923f739SMatt Jacob 	free(isp->isp_xflist, M_DEVBUF);
126451e23558SNate Lawson #ifdef	ISP_TARGET_MODE
126551e23558SNate Lawson 	free(isp->isp_tgtlist, M_DEVBUF);
126651e23558SNate Lawson #endif
12671923f739SMatt Jacob 	free(pcs->dmaps, M_DEVBUF);
126872429e49SMatt Jacob 	ISP_LOCK(isp);
12691923f739SMatt Jacob 	isp->isp_rquest = NULL;
12701923f739SMatt Jacob 	return (1);
1271d720e6d5SJustin T. Gibbs }
1272d720e6d5SJustin T. Gibbs 
1273d720e6d5SJustin T. Gibbs typedef struct {
1274d720e6d5SJustin T. Gibbs 	struct ispsoftc *isp;
12759e11e5beSMatt Jacob 	void *cmd_token;
12769e11e5beSMatt Jacob 	void *rq;
12774fd13c1bSMatt Jacob 	u_int16_t *nxtip;
12789637d68cSMatt Jacob 	u_int16_t optr;
1279d720e6d5SJustin T. Gibbs 	u_int error;
1280d720e6d5SJustin T. Gibbs } mush_t;
1281d720e6d5SJustin T. Gibbs 
12824873663cSMatt Jacob #define	MUSHERR_NOQENTRIES	-2
12834873663cSMatt Jacob 
12849e11e5beSMatt Jacob #ifdef	ISP_TARGET_MODE
12859e11e5beSMatt Jacob /*
12869e11e5beSMatt Jacob  * We need to handle DMA for target mode differently from initiator mode.
12879e11e5beSMatt Jacob  *
12889e11e5beSMatt Jacob  * DMA mapping and construction and submission of CTIO Request Entries
12899e11e5beSMatt Jacob  * and rendevous for completion are very tightly coupled because we start
12909e11e5beSMatt Jacob  * out by knowing (per platform) how much data we have to move, but we
12919e11e5beSMatt Jacob  * don't know, up front, how many DMA mapping segments will have to be used
12929e11e5beSMatt Jacob  * cover that data, so we don't know how many CTIO Request Entries we
12939e11e5beSMatt Jacob  * will end up using. Further, for performance reasons we may want to
12949e11e5beSMatt Jacob  * (on the last CTIO for Fibre Channel), send status too (if all went well).
12959e11e5beSMatt Jacob  *
12969e11e5beSMatt Jacob  * The standard vector still goes through isp_pci_dmasetup, but the callback
12979e11e5beSMatt Jacob  * for the DMA mapping routines comes here instead with the whole transfer
12989e11e5beSMatt Jacob  * mapped and a pointer to a partially filled in already allocated request
12999e11e5beSMatt Jacob  * queue entry. We finish the job.
13009e11e5beSMatt Jacob  */
1301e9a2738aSMatt Jacob static void tdma_mk(void *, bus_dma_segment_t *, int, int);
1302e9a2738aSMatt Jacob static void tdma_mkfc(void *, bus_dma_segment_t *, int, int);
1303e9a2738aSMatt Jacob 
1304e9a2738aSMatt Jacob #define	STATUS_WITH_DATA	1
13059e11e5beSMatt Jacob 
1306d720e6d5SJustin T. Gibbs static void
130705fbcbb0SMatt Jacob tdma_mk(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1308d720e6d5SJustin T. Gibbs {
1309d720e6d5SJustin T. Gibbs 	mush_t *mp;
13109e11e5beSMatt Jacob 	struct ccb_scsiio *csio;
13114fd13c1bSMatt Jacob 	struct ispsoftc *isp;
13121923f739SMatt Jacob 	struct isp_pcisoftc *pcs;
1313d720e6d5SJustin T. Gibbs 	bus_dmamap_t *dp;
13144fd13c1bSMatt Jacob 	ct_entry_t *cto, *qe;
131505fbcbb0SMatt Jacob 	u_int8_t scsi_status;
13164fd13c1bSMatt Jacob 	u_int16_t curi, nxti, handle;
1317b96934e8SMatt Jacob 	u_int32_t sflags;
131805fbcbb0SMatt Jacob 	int32_t resid;
13194fd13c1bSMatt Jacob 	int nth_ctio, nctios, send_status;
1320d720e6d5SJustin T. Gibbs 
1321d720e6d5SJustin T. Gibbs 	mp = (mush_t *) arg;
1322d720e6d5SJustin T. Gibbs 	if (error) {
1323d720e6d5SJustin T. Gibbs 		mp->error = error;
1324d720e6d5SJustin T. Gibbs 		return;
1325d720e6d5SJustin T. Gibbs 	}
13264fd13c1bSMatt Jacob 
13274fd13c1bSMatt Jacob 	isp = mp->isp;
13289e11e5beSMatt Jacob 	csio = mp->cmd_token;
13299e11e5beSMatt Jacob 	cto = mp->rq;
13304fd13c1bSMatt Jacob 	curi = isp->isp_reqidx;
13314fd13c1bSMatt Jacob 	qe = (ct_entry_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, curi);
13329e11e5beSMatt Jacob 
133365b024e1SMatt Jacob 	cto->ct_xfrlen = 0;
133465b024e1SMatt Jacob 	cto->ct_seg_count = 0;
133565b024e1SMatt Jacob 	cto->ct_header.rqs_entry_count = 1;
133605fbcbb0SMatt Jacob 	MEMZERO(cto->ct_dataseg, sizeof(cto->ct_dataseg));
133705fbcbb0SMatt Jacob 
133805fbcbb0SMatt Jacob 	if (nseg == 0) {
133905fbcbb0SMatt Jacob 		cto->ct_header.rqs_seqno = 1;
13404fd13c1bSMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG1,
1341e9a2738aSMatt Jacob 		    "CTIO[%x] lun%d iid%d tag %x flgs %x sts %x ssts %x res %d",
13425f5aafe1SMatt Jacob 		    cto->ct_fwhandle, csio->ccb_h.target_lun, cto->ct_iid,
1343e9a2738aSMatt Jacob 		    cto->ct_tag_val, cto->ct_flags, cto->ct_status,
1344e9a2738aSMatt Jacob 		    cto->ct_scsi_status, cto->ct_resid);
13454fd13c1bSMatt Jacob 		ISP_TDQE(isp, "tdma_mk[no data]", curi, cto);
13464fd13c1bSMatt Jacob 		isp_put_ctio(isp, cto, qe);
134765b024e1SMatt Jacob 		return;
134865b024e1SMatt Jacob 	}
134965b024e1SMatt Jacob 
135065b024e1SMatt Jacob 	nctios = nseg / ISP_RQDSEG;
135165b024e1SMatt Jacob 	if (nseg % ISP_RQDSEG) {
135265b024e1SMatt Jacob 		nctios++;
135365b024e1SMatt Jacob 	}
135465b024e1SMatt Jacob 
135505fbcbb0SMatt Jacob 	/*
13565f5aafe1SMatt Jacob 	 * Save syshandle, and potentially any SCSI status, which we'll
13575f5aafe1SMatt Jacob 	 * reinsert on the last CTIO we're going to send.
135805fbcbb0SMatt Jacob 	 */
13594fd13c1bSMatt Jacob 
13605f5aafe1SMatt Jacob 	handle = cto->ct_syshandle;
13615f5aafe1SMatt Jacob 	cto->ct_syshandle = 0;
136205fbcbb0SMatt Jacob 	cto->ct_header.rqs_seqno = 0;
136305fbcbb0SMatt Jacob 	send_status = (cto->ct_flags & CT_SENDSTATUS) != 0;
136405fbcbb0SMatt Jacob 
136505fbcbb0SMatt Jacob 	if (send_status) {
136605fbcbb0SMatt Jacob 		sflags = cto->ct_flags & (CT_SENDSTATUS | CT_CCINCR);
136705fbcbb0SMatt Jacob 		cto->ct_flags &= ~(CT_SENDSTATUS | CT_CCINCR);
136805fbcbb0SMatt Jacob 		/*
136905fbcbb0SMatt Jacob 		 * Preserve residual.
137005fbcbb0SMatt Jacob 		 */
137105fbcbb0SMatt Jacob 		resid = cto->ct_resid;
137205fbcbb0SMatt Jacob 
137305fbcbb0SMatt Jacob 		/*
137405fbcbb0SMatt Jacob 		 * Save actual SCSI status.
137505fbcbb0SMatt Jacob 		 */
137605fbcbb0SMatt Jacob 		scsi_status = cto->ct_scsi_status;
137705fbcbb0SMatt Jacob 
1378e9a2738aSMatt Jacob #ifndef	STATUS_WITH_DATA
1379e9a2738aSMatt Jacob 		sflags |= CT_NO_DATA;
138005fbcbb0SMatt Jacob 		/*
138105fbcbb0SMatt Jacob 		 * We can't do a status at the same time as a data CTIO, so
138205fbcbb0SMatt Jacob 		 * we need to synthesize an extra CTIO at this level.
138305fbcbb0SMatt Jacob 		 */
138405fbcbb0SMatt Jacob 		nctios++;
1385b72b1569SMatt Jacob #endif
138605fbcbb0SMatt Jacob 	} else {
138705fbcbb0SMatt Jacob 		sflags = scsi_status = resid = 0;
138805fbcbb0SMatt Jacob 	}
138905fbcbb0SMatt Jacob 
1390b96934e8SMatt Jacob 	cto->ct_resid = 0;
139105fbcbb0SMatt Jacob 	cto->ct_scsi_status = 0;
139205fbcbb0SMatt Jacob 
13931923f739SMatt Jacob 	pcs = (struct isp_pcisoftc *)isp;
13941923f739SMatt Jacob 	dp = &pcs->dmaps[isp_handle_index(handle)];
13959e11e5beSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
13961923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
1397d720e6d5SJustin T. Gibbs 	} else {
13981923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
1399d720e6d5SJustin T. Gibbs 	}
1400d720e6d5SJustin T. Gibbs 
14014fd13c1bSMatt Jacob 	nxti = *mp->nxtip;
14029e11e5beSMatt Jacob 
14034fd13c1bSMatt Jacob 	for (nth_ctio = 0; nth_ctio < nctios; nth_ctio++) {
140405fbcbb0SMatt Jacob 		int seglim;
14059e11e5beSMatt Jacob 
14069e11e5beSMatt Jacob 		seglim = nseg;
140705fbcbb0SMatt Jacob 		if (seglim) {
140805fbcbb0SMatt Jacob 			int seg;
140905fbcbb0SMatt Jacob 
14109e11e5beSMatt Jacob 			if (seglim > ISP_RQDSEG)
14119e11e5beSMatt Jacob 				seglim = ISP_RQDSEG;
14129e11e5beSMatt Jacob 
141305fbcbb0SMatt Jacob 			for (seg = 0; seg < seglim; seg++, nseg--) {
141405fbcbb0SMatt Jacob 				/*
141505fbcbb0SMatt Jacob 				 * Unlike normal initiator commands, we don't
141605fbcbb0SMatt Jacob 				 * do any swizzling here.
141705fbcbb0SMatt Jacob 				 */
14189e11e5beSMatt Jacob 				cto->ct_dataseg[seg].ds_count = dm_segs->ds_len;
141905fbcbb0SMatt Jacob 				cto->ct_dataseg[seg].ds_base = dm_segs->ds_addr;
14209e11e5beSMatt Jacob 				cto->ct_xfrlen += dm_segs->ds_len;
14219e11e5beSMatt Jacob 				dm_segs++;
14229e11e5beSMatt Jacob 			}
14239e11e5beSMatt Jacob 			cto->ct_seg_count = seg;
14249e11e5beSMatt Jacob 		} else {
142505fbcbb0SMatt Jacob 			/*
142605fbcbb0SMatt Jacob 			 * This case should only happen when we're sending an
142705fbcbb0SMatt Jacob 			 * extra CTIO with final status.
142805fbcbb0SMatt Jacob 			 */
142905fbcbb0SMatt Jacob 			if (send_status == 0) {
14304fd13c1bSMatt Jacob 				isp_prt(isp, ISP_LOGWARN,
1431f7dddf8aSMatt Jacob 				    "tdma_mk ran out of segments");
143205fbcbb0SMatt Jacob 				mp->error = EINVAL;
143305fbcbb0SMatt Jacob 				return;
14349e11e5beSMatt Jacob 			}
143505fbcbb0SMatt Jacob 		}
143605fbcbb0SMatt Jacob 
143705fbcbb0SMatt Jacob 		/*
143805fbcbb0SMatt Jacob 		 * At this point, the fields ct_lun, ct_iid, ct_tagval,
143905fbcbb0SMatt Jacob 		 * ct_tagtype, and ct_timeout have been carried over
144005fbcbb0SMatt Jacob 		 * unchanged from what our caller had set.
144105fbcbb0SMatt Jacob 		 *
144205fbcbb0SMatt Jacob 		 * The dataseg fields and the seg_count fields we just got
144305fbcbb0SMatt Jacob 		 * through setting. The data direction we've preserved all
144405fbcbb0SMatt Jacob 		 * along and only clear it if we're now sending status.
144505fbcbb0SMatt Jacob 		 */
14469e11e5beSMatt Jacob 
14474fd13c1bSMatt Jacob 		if (nth_ctio == nctios - 1) {
14489e11e5beSMatt Jacob 			/*
144905fbcbb0SMatt Jacob 			 * We're the last in a sequence of CTIOs, so mark
145005fbcbb0SMatt Jacob 			 * this CTIO and save the handle to the CCB such that
145105fbcbb0SMatt Jacob 			 * when this CTIO completes we can free dma resources
145205fbcbb0SMatt Jacob 			 * and do whatever else we need to do to finish the
1453e9a2738aSMatt Jacob 			 * rest of the command. We *don't* give this to the
1454e9a2738aSMatt Jacob 			 * firmware to work on- the caller will do that.
14559e11e5beSMatt Jacob 			 */
14564fd13c1bSMatt Jacob 
14575f5aafe1SMatt Jacob 			cto->ct_syshandle = handle;
145805fbcbb0SMatt Jacob 			cto->ct_header.rqs_seqno = 1;
145905fbcbb0SMatt Jacob 
146005fbcbb0SMatt Jacob 			if (send_status) {
14619e11e5beSMatt Jacob 				cto->ct_scsi_status = scsi_status;
1462b72b1569SMatt Jacob 				cto->ct_flags |= sflags;
146305fbcbb0SMatt Jacob 				cto->ct_resid = resid;
146442426921SMatt Jacob 			}
1465d02373f1SMatt Jacob 			if (send_status) {
14664fd13c1bSMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG1,
1467e9a2738aSMatt Jacob 				    "CTIO[%x] lun%d iid %d tag %x ct_flags %x "
14685f5aafe1SMatt Jacob 				    "scsi status %x resid %d",
14695f5aafe1SMatt Jacob 				    cto->ct_fwhandle, csio->ccb_h.target_lun,
1470e9a2738aSMatt Jacob 				    cto->ct_iid, cto->ct_tag_val, cto->ct_flags,
147105fbcbb0SMatt Jacob 				    cto->ct_scsi_status, cto->ct_resid);
1472d02373f1SMatt Jacob 			} else {
14734fd13c1bSMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG1,
1474e9a2738aSMatt Jacob 				    "CTIO[%x] lun%d iid%d tag %x ct_flags 0x%x",
14755f5aafe1SMatt Jacob 				    cto->ct_fwhandle, csio->ccb_h.target_lun,
1476e9a2738aSMatt Jacob 				    cto->ct_iid, cto->ct_tag_val,
1477e9a2738aSMatt Jacob 				    cto->ct_flags);
147805fbcbb0SMatt Jacob 			}
14794fd13c1bSMatt Jacob 			isp_put_ctio(isp, cto, qe);
14804fd13c1bSMatt Jacob 			ISP_TDQE(isp, "last tdma_mk", curi, cto);
14814fd13c1bSMatt Jacob 			if (nctios > 1) {
14824fd13c1bSMatt Jacob 				MEMORYBARRIER(isp, SYNC_REQUEST,
14834fd13c1bSMatt Jacob 				    curi, QENTRY_LEN);
14844fd13c1bSMatt Jacob 			}
14859e11e5beSMatt Jacob 		} else {
14864fd13c1bSMatt Jacob 			ct_entry_t *oqe = qe;
148705fbcbb0SMatt Jacob 
148805fbcbb0SMatt Jacob 			/*
14895f5aafe1SMatt Jacob 			 * Make sure syshandle fields are clean
149005fbcbb0SMatt Jacob 			 */
14915f5aafe1SMatt Jacob 			cto->ct_syshandle = 0;
14929e11e5beSMatt Jacob 			cto->ct_header.rqs_seqno = 0;
149305fbcbb0SMatt Jacob 
14944fd13c1bSMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG1,
14955f5aafe1SMatt Jacob 			    "CTIO[%x] lun%d for ID%d ct_flags 0x%x",
14965f5aafe1SMatt Jacob 			    cto->ct_fwhandle, csio->ccb_h.target_lun,
14975f5aafe1SMatt Jacob 			    cto->ct_iid, cto->ct_flags);
149805fbcbb0SMatt Jacob 
149905fbcbb0SMatt Jacob 			/*
150005fbcbb0SMatt Jacob 			 * Get a new CTIO
150105fbcbb0SMatt Jacob 			 */
15024fd13c1bSMatt Jacob 			qe = (ct_entry_t *)
15034fd13c1bSMatt Jacob 			    ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
15044fd13c1bSMatt Jacob 			nxti = ISP_NXT_QENTRY(nxti, RQUEST_QUEUE_LEN(isp));
15054fd13c1bSMatt Jacob 			if (nxti == mp->optr) {
15064fd13c1bSMatt Jacob 				isp_prt(isp, ISP_LOGTDEBUG0,
1507f7dddf8aSMatt Jacob 				    "Queue Overflow in tdma_mk");
15089e11e5beSMatt Jacob 				mp->error = MUSHERR_NOQENTRIES;
15099e11e5beSMatt Jacob 				return;
15109e11e5beSMatt Jacob 			}
15114fd13c1bSMatt Jacob 
15129e11e5beSMatt Jacob 			/*
15134fd13c1bSMatt Jacob 			 * Now that we're done with the old CTIO,
15144fd13c1bSMatt Jacob 			 * flush it out to the request queue.
15154fd13c1bSMatt Jacob 			 */
15164fd13c1bSMatt Jacob 			ISP_TDQE(isp, "dma_tgt_fc", curi, cto);
15174fd13c1bSMatt Jacob 			isp_put_ctio(isp, cto, oqe);
15184fd13c1bSMatt Jacob 			if (nth_ctio != 0) {
15194fd13c1bSMatt Jacob 				MEMORYBARRIER(isp, SYNC_REQUEST, curi,
15204fd13c1bSMatt Jacob 				    QENTRY_LEN);
15214fd13c1bSMatt Jacob 			}
15224fd13c1bSMatt Jacob 			curi = ISP_NXT_QENTRY(curi, RQUEST_QUEUE_LEN(isp));
15234fd13c1bSMatt Jacob 
15244fd13c1bSMatt Jacob 			/*
15254fd13c1bSMatt Jacob 			 * Reset some fields in the CTIO so we can reuse
15264fd13c1bSMatt Jacob 			 * for the next one we'll flush to the request
15274fd13c1bSMatt Jacob 			 * queue.
15289e11e5beSMatt Jacob 			 */
15299e11e5beSMatt Jacob 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
15309e11e5beSMatt Jacob 			cto->ct_header.rqs_entry_count = 1;
15319e11e5beSMatt Jacob 			cto->ct_header.rqs_flags = 0;
15329e11e5beSMatt Jacob 			cto->ct_status = 0;
15339e11e5beSMatt Jacob 			cto->ct_scsi_status = 0;
15349e11e5beSMatt Jacob 			cto->ct_xfrlen = 0;
15359e11e5beSMatt Jacob 			cto->ct_resid = 0;
15369e11e5beSMatt Jacob 			cto->ct_seg_count = 0;
153705fbcbb0SMatt Jacob 			MEMZERO(cto->ct_dataseg, sizeof(cto->ct_dataseg));
15389e11e5beSMatt Jacob 		}
15399e11e5beSMatt Jacob 	}
15404fd13c1bSMatt Jacob 	*mp->nxtip = nxti;
15419e11e5beSMatt Jacob }
15429e11e5beSMatt Jacob 
1543fc087171SMatt Jacob /*
1544fc087171SMatt Jacob  * We don't have to do multiple CTIOs here. Instead, we can just do
1545fc087171SMatt Jacob  * continuation segments as needed. This greatly simplifies the code
1546fc087171SMatt Jacob  * improves performance.
1547fc087171SMatt Jacob  */
1548fc087171SMatt Jacob 
15499e11e5beSMatt Jacob static void
155005fbcbb0SMatt Jacob tdma_mkfc(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
15519e11e5beSMatt Jacob {
15529e11e5beSMatt Jacob 	mush_t *mp;
15539e11e5beSMatt Jacob 	struct ccb_scsiio *csio;
15544fd13c1bSMatt Jacob 	struct ispsoftc *isp;
15554fd13c1bSMatt Jacob 	ct2_entry_t *cto, *qe;
15564fd13c1bSMatt Jacob 	u_int16_t curi, nxti;
1557fc087171SMatt Jacob 	int segcnt;
15589e11e5beSMatt Jacob 
15599e11e5beSMatt Jacob 	mp = (mush_t *) arg;
15609e11e5beSMatt Jacob 	if (error) {
15619e11e5beSMatt Jacob 		mp->error = error;
15629e11e5beSMatt Jacob 		return;
15639e11e5beSMatt Jacob 	}
15649e11e5beSMatt Jacob 
15654fd13c1bSMatt Jacob 	isp = mp->isp;
156665b024e1SMatt Jacob 	csio = mp->cmd_token;
156765b024e1SMatt Jacob 	cto = mp->rq;
1568fc087171SMatt Jacob 
15694fd13c1bSMatt Jacob 	curi = isp->isp_reqidx;
15704fd13c1bSMatt Jacob 	qe = (ct2_entry_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, curi);
157165b024e1SMatt Jacob 
157265b024e1SMatt Jacob 	if (nseg == 0) {
157365b024e1SMatt Jacob 		if ((cto->ct_flags & CT2_FLAG_MMASK) != CT2_FLAG_MODE1) {
15744fd13c1bSMatt Jacob 			isp_prt(isp, ISP_LOGWARN,
1575f7dddf8aSMatt Jacob 			    "dma2_tgt_fc, a status CTIO2 without MODE1 "
1576f7dddf8aSMatt Jacob 			    "set (0x%x)", cto->ct_flags);
157765b024e1SMatt Jacob 			mp->error = EINVAL;
157865b024e1SMatt Jacob 			return;
157965b024e1SMatt Jacob 		}
158065b024e1SMatt Jacob 		/*
158165b024e1SMatt Jacob 		 * We preserve ct_lun, ct_iid, ct_rxid. We set the data
158265b024e1SMatt Jacob 		 * flags to NO DATA and clear relative offset flags.
158365b024e1SMatt Jacob 		 * We preserve the ct_resid and the response area.
158465b024e1SMatt Jacob 		 */
1585fc087171SMatt Jacob 		cto->ct_header.rqs_seqno = 1;
158665b024e1SMatt Jacob 		cto->ct_seg_count = 0;
158765b024e1SMatt Jacob 		cto->ct_reloff = 0;
15884fd13c1bSMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG1,
15895f5aafe1SMatt Jacob 		    "CTIO2[%x] lun %d->iid%d flgs 0x%x sts 0x%x ssts "
1590d02373f1SMatt Jacob 		    "0x%x res %d", cto->ct_rxid, csio->ccb_h.target_lun,
1591d02373f1SMatt Jacob 		    cto->ct_iid, cto->ct_flags, cto->ct_status,
159265b024e1SMatt Jacob 		    cto->rsp.m1.ct_scsi_status, cto->ct_resid);
15934fd13c1bSMatt Jacob 		isp_put_ctio2(isp, cto, qe);
15944fd13c1bSMatt Jacob 		ISP_TDQE(isp, "dma2_tgt_fc[no data]", curi, qe);
15959e11e5beSMatt Jacob 		return;
15969e11e5beSMatt Jacob 	}
15979e11e5beSMatt Jacob 
159865b024e1SMatt Jacob 	if ((cto->ct_flags & CT2_FLAG_MMASK) != CT2_FLAG_MODE0) {
1599fc087171SMatt Jacob 		isp_prt(isp, ISP_LOGERR,
1600f7dddf8aSMatt Jacob 		    "dma2_tgt_fc, a data CTIO2 without MODE0 set "
1601f7dddf8aSMatt Jacob 		    "(0x%x)", cto->ct_flags);
160265b024e1SMatt Jacob 		mp->error = EINVAL;
160365b024e1SMatt Jacob 		return;
160465b024e1SMatt Jacob 	}
160565b024e1SMatt Jacob 
160665b024e1SMatt Jacob 
16074fd13c1bSMatt Jacob 	nxti = *mp->nxtip;
16084fd13c1bSMatt Jacob 
1609fc087171SMatt Jacob 	/*
1610fc087171SMatt Jacob 	 * Set up the CTIO2 data segments.
1611fc087171SMatt Jacob 	 */
1612fc087171SMatt Jacob 	for (segcnt = 0; cto->ct_seg_count < ISP_RQDSEG_T2 && segcnt < nseg;
1613fc087171SMatt Jacob 	    cto->ct_seg_count++, segcnt++) {
1614fc087171SMatt Jacob 		cto->rsp.m0.ct_dataseg[cto->ct_seg_count].ds_base =
1615fc087171SMatt Jacob 		    dm_segs[segcnt].ds_addr;
1616fc087171SMatt Jacob 		cto->rsp.m0.ct_dataseg[cto->ct_seg_count].ds_count =
1617fc087171SMatt Jacob 		    dm_segs[segcnt].ds_len;
1618fc087171SMatt Jacob 		cto->rsp.m0.ct_xfrlen += dm_segs[segcnt].ds_len;
161976514802SMatt Jacob 		isp_prt(isp, ISP_LOGTDEBUG1,
162074a96f43SJohn Baldwin 		    "isp_send_ctio2: ent0[%d]0x%jx:%ju",
162174a96f43SJohn Baldwin 		    cto->ct_seg_count, (uintmax_t)dm_segs[segcnt].ds_addr,
162274a96f43SJohn Baldwin 		    (uintmax_t)dm_segs[segcnt].ds_len);
1623fc087171SMatt Jacob 	}
16249e11e5beSMatt Jacob 
1625fc087171SMatt Jacob 	while (segcnt < nseg) {
1626fc087171SMatt Jacob 		u_int16_t curip;
16274fd13c1bSMatt Jacob 		int seg;
1628fc087171SMatt Jacob 		ispcontreq_t local, *crq = &local, *qep;
16299e11e5beSMatt Jacob 
1630fc087171SMatt Jacob 		qep = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
1631fc087171SMatt Jacob 		curip = nxti;
1632fc087171SMatt Jacob 		nxti = ISP_NXT_QENTRY(curip, RQUEST_QUEUE_LEN(isp));
16334fd13c1bSMatt Jacob 		if (nxti == mp->optr) {
1634fc087171SMatt Jacob 			ISP_UNLOCK(isp);
1635fc087171SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG0,
1636fc087171SMatt Jacob 			    "tdma_mkfc: request queue overflow");
16379e11e5beSMatt Jacob 			mp->error = MUSHERR_NOQENTRIES;
16389e11e5beSMatt Jacob 			return;
16399e11e5beSMatt Jacob 		}
1640fc087171SMatt Jacob 		cto->ct_header.rqs_entry_count++;
1641fc087171SMatt Jacob 		MEMZERO((void *)crq, sizeof (*crq));
1642fc087171SMatt Jacob 		crq->req_header.rqs_entry_count = 1;
1643fc087171SMatt Jacob 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
1644fc087171SMatt Jacob 		for (seg = 0; segcnt < nseg && seg < ISP_CDSEG;
1645fc087171SMatt Jacob 		    segcnt++, seg++) {
1646fc087171SMatt Jacob 			crq->req_dataseg[seg].ds_base = dm_segs[segcnt].ds_addr;
1647fc087171SMatt Jacob 			crq->req_dataseg[seg].ds_count = dm_segs[segcnt].ds_len;
1648fc087171SMatt Jacob 			isp_prt(isp, ISP_LOGTDEBUG1,
164974a96f43SJohn Baldwin 			    "isp_send_ctio2: ent%d[%d]%jx:%ju",
1650fc087171SMatt Jacob 			    cto->ct_header.rqs_entry_count-1, seg,
165174a96f43SJohn Baldwin 			    (uintmax_t)dm_segs[segcnt].ds_addr,
165274a96f43SJohn Baldwin 			    (uintmax_t)dm_segs[segcnt].ds_len);
1653fc087171SMatt Jacob 			cto->rsp.m0.ct_xfrlen += dm_segs[segcnt].ds_len;
1654fc087171SMatt Jacob 			cto->ct_seg_count++;
1655fc087171SMatt Jacob 		}
1656fc087171SMatt Jacob 		MEMORYBARRIER(isp, SYNC_REQUEST, curip, QENTRY_LEN);
1657fc087171SMatt Jacob 		isp_put_cont_req(isp, crq, qep);
1658fc087171SMatt Jacob 		ISP_TDQE(isp, "cont entry", curi, qep);
1659fc087171SMatt Jacob 	}
166065b024e1SMatt Jacob 
16619e11e5beSMatt Jacob 	/*
1662fc087171SMatt Jacob 	 * No do final twiddling for the CTIO itself.
16634fd13c1bSMatt Jacob 	 */
1664fc087171SMatt Jacob 	cto->ct_header.rqs_seqno = 1;
1665fc087171SMatt Jacob 	isp_prt(isp, ISP_LOGTDEBUG1,
1666fc087171SMatt Jacob 	    "CTIO2[%x] lun %d->iid%d flgs 0x%x sts 0x%x ssts 0x%x resid %d",
1667fc087171SMatt Jacob 	    cto->ct_rxid, csio->ccb_h.target_lun, (int) cto->ct_iid,
1668fc087171SMatt Jacob 	    cto->ct_flags, cto->ct_status, cto->rsp.m1.ct_scsi_status,
1669fc087171SMatt Jacob 	    cto->ct_resid);
1670fc087171SMatt Jacob 	isp_put_ctio2(isp, cto, qe);
1671fc087171SMatt Jacob 	ISP_TDQE(isp, "last dma2_tgt_fc", curi, qe);
16724fd13c1bSMatt Jacob 	*mp->nxtip = nxti;
16739e11e5beSMatt Jacob }
16749e11e5beSMatt Jacob #endif
16759e11e5beSMatt Jacob 
1676126ec864SMatt Jacob static void dma2(void *, bus_dma_segment_t *, int, int);
16779e11e5beSMatt Jacob 
167851effc8cSMatt Jacob #if	defined(ISP_DAC_SUPPORTED) && (ISP_64BIT_CORRECTLY_DONE)
167969767099SMatt Jacob #define	LOWD(x)		((uint32_t) x)
168069767099SMatt Jacob #define	HIWD(x)		((uint32_t) (x >> 32))
168169767099SMatt Jacob 
16826de9bf77SMatt Jacob static void
16836de9bf77SMatt Jacob dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
16846de9bf77SMatt Jacob {
16856de9bf77SMatt Jacob 	mush_t *mp;
16866de9bf77SMatt Jacob 	struct ispsoftc *isp;
16876de9bf77SMatt Jacob 	struct ccb_scsiio *csio;
16886de9bf77SMatt Jacob 	struct isp_pcisoftc *pcs;
16896de9bf77SMatt Jacob 	bus_dmamap_t *dp;
16906de9bf77SMatt Jacob 	bus_dma_segment_t *eseg;
16916de9bf77SMatt Jacob 	ispreq64_t *rq;
16926de9bf77SMatt Jacob 	int seglim, datalen;
16936de9bf77SMatt Jacob 	u_int16_t nxti;
16946de9bf77SMatt Jacob 
16956de9bf77SMatt Jacob 	mp = (mush_t *) arg;
16966de9bf77SMatt Jacob 	if (error) {
16976de9bf77SMatt Jacob 		mp->error = error;
16986de9bf77SMatt Jacob 		return;
16996de9bf77SMatt Jacob 	}
17006de9bf77SMatt Jacob 
17016de9bf77SMatt Jacob 	if (nseg < 1) {
17026de9bf77SMatt Jacob 		isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
17036de9bf77SMatt Jacob 		mp->error = EFAULT;
17046de9bf77SMatt Jacob 		return;
17056de9bf77SMatt Jacob 	}
17066de9bf77SMatt Jacob 	csio = mp->cmd_token;
17076de9bf77SMatt Jacob 	isp = mp->isp;
17086de9bf77SMatt Jacob 	rq = mp->rq;
17096de9bf77SMatt Jacob 	pcs = (struct isp_pcisoftc *)mp->isp;
17106de9bf77SMatt Jacob 	dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
17116de9bf77SMatt Jacob 	nxti = *mp->nxtip;
17126de9bf77SMatt Jacob 
17136de9bf77SMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
17146de9bf77SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
17156de9bf77SMatt Jacob 	} else {
17166de9bf77SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
17176de9bf77SMatt Jacob 	}
17186de9bf77SMatt Jacob 	datalen = XS_XFRLEN(csio);
17196de9bf77SMatt Jacob 
17206de9bf77SMatt Jacob 	/*
17216de9bf77SMatt Jacob 	 * We're passed an initial partially filled in entry that
17226de9bf77SMatt Jacob 	 * has most fields filled in except for data transfer
17236de9bf77SMatt Jacob 	 * related values.
17246de9bf77SMatt Jacob 	 *
17256de9bf77SMatt Jacob 	 * Our job is to fill in the initial request queue entry and
17266de9bf77SMatt Jacob 	 * then to start allocating and filling in continuation entries
17276de9bf77SMatt Jacob 	 * until we've covered the entire transfer.
17286de9bf77SMatt Jacob 	 */
17296de9bf77SMatt Jacob 
17306de9bf77SMatt Jacob 	if (IS_FC(isp)) {
173169767099SMatt Jacob 		rq->req_header.rqs_entry_type = RQSTYPE_T3RQS;
17326de9bf77SMatt Jacob 		seglim = ISP_RQDSEG_T3;
17336de9bf77SMatt Jacob 		((ispreqt3_t *)rq)->req_totalcnt = datalen;
17346de9bf77SMatt Jacob 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
17356de9bf77SMatt Jacob 			((ispreqt3_t *)rq)->req_flags |= REQFLAG_DATA_IN;
17366de9bf77SMatt Jacob 		} else {
17376de9bf77SMatt Jacob 			((ispreqt3_t *)rq)->req_flags |= REQFLAG_DATA_OUT;
17386de9bf77SMatt Jacob 		}
17396de9bf77SMatt Jacob 	} else {
174069767099SMatt Jacob 		rq->req_header.rqs_entry_type = RQSTYPE_A64;
17416de9bf77SMatt Jacob 		if (csio->cdb_len > 12) {
17426de9bf77SMatt Jacob 			seglim = 0;
17436de9bf77SMatt Jacob 		} else {
17446de9bf77SMatt Jacob 			seglim = ISP_RQDSEG_A64;
17456de9bf77SMatt Jacob 		}
17466de9bf77SMatt Jacob 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
17476de9bf77SMatt Jacob 			rq->req_flags |= REQFLAG_DATA_IN;
17486de9bf77SMatt Jacob 		} else {
17496de9bf77SMatt Jacob 			rq->req_flags |= REQFLAG_DATA_OUT;
17506de9bf77SMatt Jacob 		}
17516de9bf77SMatt Jacob 	}
17526de9bf77SMatt Jacob 
17536de9bf77SMatt Jacob 	eseg = dm_segs + nseg;
17546de9bf77SMatt Jacob 
17556de9bf77SMatt Jacob 	while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) {
17566de9bf77SMatt Jacob 		if (IS_FC(isp)) {
17576de9bf77SMatt Jacob 			ispreqt3_t *rq3 = (ispreqt3_t *)rq;
17586de9bf77SMatt Jacob 			rq3->req_dataseg[rq3->req_seg_count].ds_base =
175969767099SMatt Jacob 			    LOWD(dm_segs->ds_addr);
176069767099SMatt Jacob 			rq3->req_dataseg[rq3->req_seg_count].ds_basehi =
176169767099SMatt Jacob 			    HIWD(dm_segs->ds_addr);
17626de9bf77SMatt Jacob 			rq3->req_dataseg[rq3->req_seg_count].ds_count =
17636de9bf77SMatt Jacob 			    dm_segs->ds_len;
17646de9bf77SMatt Jacob 		} else {
17656de9bf77SMatt Jacob 			rq->req_dataseg[rq->req_seg_count].ds_base =
176669767099SMatt Jacob 			    LOWD(dm_segs->ds_addr);
176769767099SMatt Jacob 			rq->req_dataseg[rq->req_seg_count].ds_basehi =
176869767099SMatt Jacob 			    HIWD(dm_segs->ds_addr);
17696de9bf77SMatt Jacob 			rq->req_dataseg[rq->req_seg_count].ds_count =
17706de9bf77SMatt Jacob 			    dm_segs->ds_len;
17716de9bf77SMatt Jacob 		}
17726de9bf77SMatt Jacob 		datalen -= dm_segs->ds_len;
17736de9bf77SMatt Jacob 		rq->req_seg_count++;
17746de9bf77SMatt Jacob 		dm_segs++;
17756de9bf77SMatt Jacob 	}
17766de9bf77SMatt Jacob 
17776de9bf77SMatt Jacob 	while (datalen > 0 && dm_segs != eseg) {
17786de9bf77SMatt Jacob 		u_int16_t onxti;
17796de9bf77SMatt Jacob 		ispcontreq64_t local, *crq = &local, *cqe;
17806de9bf77SMatt Jacob 
17816de9bf77SMatt Jacob 		cqe = (ispcontreq64_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
17826de9bf77SMatt Jacob 		onxti = nxti;
17836de9bf77SMatt Jacob 		nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
17846de9bf77SMatt Jacob 		if (nxti == mp->optr) {
17856de9bf77SMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
17866de9bf77SMatt Jacob 			mp->error = MUSHERR_NOQENTRIES;
17876de9bf77SMatt Jacob 			return;
17886de9bf77SMatt Jacob 		}
17896de9bf77SMatt Jacob 		rq->req_header.rqs_entry_count++;
17906de9bf77SMatt Jacob 		MEMZERO((void *)crq, sizeof (*crq));
17916de9bf77SMatt Jacob 		crq->req_header.rqs_entry_count = 1;
17926de9bf77SMatt Jacob 		crq->req_header.rqs_entry_type = RQSTYPE_A64_CONT;
17936de9bf77SMatt Jacob 
17946de9bf77SMatt Jacob 		seglim = 0;
17956de9bf77SMatt Jacob 		while (datalen > 0 && seglim < ISP_CDSEG64 && dm_segs != eseg) {
17966de9bf77SMatt Jacob 			crq->req_dataseg[seglim].ds_base =
179769767099SMatt Jacob 			    LOWD(dm_segs->ds_addr);
179869767099SMatt Jacob 			crq->req_dataseg[seglim].ds_basehi =
179969767099SMatt Jacob 			    HIWD(dm_segs->ds_addr);
18006de9bf77SMatt Jacob 			crq->req_dataseg[seglim].ds_count =
18016de9bf77SMatt Jacob 			    dm_segs->ds_len;
18026de9bf77SMatt Jacob 			rq->req_seg_count++;
18036de9bf77SMatt Jacob 			dm_segs++;
18046de9bf77SMatt Jacob 			seglim++;
18056de9bf77SMatt Jacob 			datalen -= dm_segs->ds_len;
18066de9bf77SMatt Jacob 		}
18076de9bf77SMatt Jacob 		isp_put_cont64_req(isp, crq, cqe);
18086de9bf77SMatt Jacob 		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
18096de9bf77SMatt Jacob 	}
18106de9bf77SMatt Jacob 	*mp->nxtip = nxti;
18116de9bf77SMatt Jacob }
18126de9bf77SMatt Jacob #else
18139e11e5beSMatt Jacob static void
18149e11e5beSMatt Jacob dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
18159e11e5beSMatt Jacob {
18169e11e5beSMatt Jacob 	mush_t *mp;
18174fd13c1bSMatt Jacob 	struct ispsoftc *isp;
18189e11e5beSMatt Jacob 	struct ccb_scsiio *csio;
18191923f739SMatt Jacob 	struct isp_pcisoftc *pcs;
18209e11e5beSMatt Jacob 	bus_dmamap_t *dp;
18219e11e5beSMatt Jacob 	bus_dma_segment_t *eseg;
18229e11e5beSMatt Jacob 	ispreq_t *rq;
18239e11e5beSMatt Jacob 	int seglim, datalen;
18244fd13c1bSMatt Jacob 	u_int16_t nxti;
18259e11e5beSMatt Jacob 
18269e11e5beSMatt Jacob 	mp = (mush_t *) arg;
18279e11e5beSMatt Jacob 	if (error) {
18289e11e5beSMatt Jacob 		mp->error = error;
18299e11e5beSMatt Jacob 		return;
18309e11e5beSMatt Jacob 	}
18319e11e5beSMatt Jacob 
18329e11e5beSMatt Jacob 	if (nseg < 1) {
1833f7dddf8aSMatt Jacob 		isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
18349e11e5beSMatt Jacob 		mp->error = EFAULT;
18359e11e5beSMatt Jacob 		return;
18369e11e5beSMatt Jacob 	}
18379e11e5beSMatt Jacob 	csio = mp->cmd_token;
18384fd13c1bSMatt Jacob 	isp = mp->isp;
18399e11e5beSMatt Jacob 	rq = mp->rq;
18401923f739SMatt Jacob 	pcs = (struct isp_pcisoftc *)mp->isp;
18411923f739SMatt Jacob 	dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
18424fd13c1bSMatt Jacob 	nxti = *mp->nxtip;
18439e11e5beSMatt Jacob 
18449e11e5beSMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
18451923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
18469e11e5beSMatt Jacob 	} else {
18471923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
18489e11e5beSMatt Jacob 	}
18499e11e5beSMatt Jacob 
18509e11e5beSMatt Jacob 	datalen = XS_XFRLEN(csio);
18519e11e5beSMatt Jacob 
18529e11e5beSMatt Jacob 	/*
18539e11e5beSMatt Jacob 	 * We're passed an initial partially filled in entry that
18549e11e5beSMatt Jacob 	 * has most fields filled in except for data transfer
18559e11e5beSMatt Jacob 	 * related values.
18569e11e5beSMatt Jacob 	 *
18579e11e5beSMatt Jacob 	 * Our job is to fill in the initial request queue entry and
18589e11e5beSMatt Jacob 	 * then to start allocating and filling in continuation entries
18599e11e5beSMatt Jacob 	 * until we've covered the entire transfer.
18609e11e5beSMatt Jacob 	 */
18619e11e5beSMatt Jacob 
18624fd13c1bSMatt Jacob 	if (IS_FC(isp)) {
1863d720e6d5SJustin T. Gibbs 		seglim = ISP_RQDSEG_T2;
1864d720e6d5SJustin T. Gibbs 		((ispreqt2_t *)rq)->req_totalcnt = datalen;
18659e11e5beSMatt Jacob 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
18669e11e5beSMatt Jacob 			((ispreqt2_t *)rq)->req_flags |= REQFLAG_DATA_IN;
18679e11e5beSMatt Jacob 		} else {
18689e11e5beSMatt Jacob 			((ispreqt2_t *)rq)->req_flags |= REQFLAG_DATA_OUT;
18699e11e5beSMatt Jacob 		}
1870d720e6d5SJustin T. Gibbs 	} else {
1871e142669aSMatt Jacob 		if (csio->cdb_len > 12) {
1872e142669aSMatt Jacob 			seglim = 0;
1873e142669aSMatt Jacob 		} else {
1874d720e6d5SJustin T. Gibbs 			seglim = ISP_RQDSEG;
1875e142669aSMatt Jacob 		}
18769e11e5beSMatt Jacob 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
18779e11e5beSMatt Jacob 			rq->req_flags |= REQFLAG_DATA_IN;
18789e11e5beSMatt Jacob 		} else {
18799e11e5beSMatt Jacob 			rq->req_flags |= REQFLAG_DATA_OUT;
18809e11e5beSMatt Jacob 		}
1881d720e6d5SJustin T. Gibbs 	}
1882d720e6d5SJustin T. Gibbs 
1883d720e6d5SJustin T. Gibbs 	eseg = dm_segs + nseg;
1884d720e6d5SJustin T. Gibbs 
1885d720e6d5SJustin T. Gibbs 	while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) {
18864fd13c1bSMatt Jacob 		if (IS_FC(isp)) {
1887d720e6d5SJustin T. Gibbs 			ispreqt2_t *rq2 = (ispreqt2_t *)rq;
1888d720e6d5SJustin T. Gibbs 			rq2->req_dataseg[rq2->req_seg_count].ds_base =
1889d720e6d5SJustin T. Gibbs 			    dm_segs->ds_addr;
1890d720e6d5SJustin T. Gibbs 			rq2->req_dataseg[rq2->req_seg_count].ds_count =
1891d720e6d5SJustin T. Gibbs 			    dm_segs->ds_len;
1892d720e6d5SJustin T. Gibbs 		} else {
1893d720e6d5SJustin T. Gibbs 			rq->req_dataseg[rq->req_seg_count].ds_base =
1894d720e6d5SJustin T. Gibbs 				dm_segs->ds_addr;
1895d720e6d5SJustin T. Gibbs 			rq->req_dataseg[rq->req_seg_count].ds_count =
1896d720e6d5SJustin T. Gibbs 				dm_segs->ds_len;
1897d720e6d5SJustin T. Gibbs 		}
1898d720e6d5SJustin T. Gibbs 		datalen -= dm_segs->ds_len;
1899d720e6d5SJustin T. Gibbs 		rq->req_seg_count++;
1900d720e6d5SJustin T. Gibbs 		dm_segs++;
1901d720e6d5SJustin T. Gibbs 	}
1902d720e6d5SJustin T. Gibbs 
1903d720e6d5SJustin T. Gibbs 	while (datalen > 0 && dm_segs != eseg) {
19044fd13c1bSMatt Jacob 		u_int16_t onxti;
19054fd13c1bSMatt Jacob 		ispcontreq_t local, *crq = &local, *cqe;
19064fd13c1bSMatt Jacob 
19074fd13c1bSMatt Jacob 		cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
19084fd13c1bSMatt Jacob 		onxti = nxti;
19094fd13c1bSMatt Jacob 		nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
19104fd13c1bSMatt Jacob 		if (nxti == mp->optr) {
19114fd13c1bSMatt Jacob 			isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
19124873663cSMatt Jacob 			mp->error = MUSHERR_NOQENTRIES;
1913d720e6d5SJustin T. Gibbs 			return;
1914d720e6d5SJustin T. Gibbs 		}
1915d720e6d5SJustin T. Gibbs 		rq->req_header.rqs_entry_count++;
19164fd13c1bSMatt Jacob 		MEMZERO((void *)crq, sizeof (*crq));
1917d720e6d5SJustin T. Gibbs 		crq->req_header.rqs_entry_count = 1;
1918d720e6d5SJustin T. Gibbs 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
1919d720e6d5SJustin T. Gibbs 
1920d720e6d5SJustin T. Gibbs 		seglim = 0;
1921d720e6d5SJustin T. Gibbs 		while (datalen > 0 && seglim < ISP_CDSEG && dm_segs != eseg) {
1922d720e6d5SJustin T. Gibbs 			crq->req_dataseg[seglim].ds_base =
1923d720e6d5SJustin T. Gibbs 			    dm_segs->ds_addr;
1924d720e6d5SJustin T. Gibbs 			crq->req_dataseg[seglim].ds_count =
1925d720e6d5SJustin T. Gibbs 			    dm_segs->ds_len;
1926d720e6d5SJustin T. Gibbs 			rq->req_seg_count++;
1927d720e6d5SJustin T. Gibbs 			dm_segs++;
1928d720e6d5SJustin T. Gibbs 			seglim++;
1929d720e6d5SJustin T. Gibbs 			datalen -= dm_segs->ds_len;
1930d720e6d5SJustin T. Gibbs 		}
19314fd13c1bSMatt Jacob 		isp_put_cont_req(isp, crq, cqe);
19324fd13c1bSMatt Jacob 		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
1933d720e6d5SJustin T. Gibbs 	}
19344fd13c1bSMatt Jacob 	*mp->nxtip = nxti;
1935d720e6d5SJustin T. Gibbs }
19366de9bf77SMatt Jacob #endif
1937d720e6d5SJustin T. Gibbs 
1938d720e6d5SJustin T. Gibbs static int
19399e11e5beSMatt Jacob isp_pci_dmasetup(struct ispsoftc *isp, struct ccb_scsiio *csio, ispreq_t *rq,
19404fd13c1bSMatt Jacob 	u_int16_t *nxtip, u_int16_t optr)
1941d720e6d5SJustin T. Gibbs {
19421923f739SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
19434fd13c1bSMatt Jacob 	ispreq_t *qep;
19440a5f7e8bSMatt Jacob 	bus_dmamap_t *dp = NULL;
1945d720e6d5SJustin T. Gibbs 	mush_t mush, *mp;
1946126ec864SMatt Jacob 	void (*eptr)(void *, bus_dma_segment_t *, int, int);
1947d720e6d5SJustin T. Gibbs 
19484fd13c1bSMatt Jacob 	qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, isp->isp_reqidx);
194965b024e1SMatt Jacob #ifdef	ISP_TARGET_MODE
195065b024e1SMatt Jacob 	if (csio->ccb_h.func_code == XPT_CONT_TARGET_IO) {
195165b024e1SMatt Jacob 		if (IS_FC(isp)) {
195205fbcbb0SMatt Jacob 			eptr = tdma_mkfc;
195365b024e1SMatt Jacob 		} else {
195405fbcbb0SMatt Jacob 			eptr = tdma_mk;
195565b024e1SMatt Jacob 		}
195605fbcbb0SMatt Jacob 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
195705fbcbb0SMatt Jacob 		    (csio->dxfer_len == 0)) {
195865b024e1SMatt Jacob 			mp = &mush;
195965b024e1SMatt Jacob 			mp->isp = isp;
196065b024e1SMatt Jacob 			mp->cmd_token = csio;
1961e9a2738aSMatt Jacob 			mp->rq = rq;	/* really a ct_entry_t or ct2_entry_t */
19624fd13c1bSMatt Jacob 			mp->nxtip = nxtip;
196365b024e1SMatt Jacob 			mp->optr = optr;
196465b024e1SMatt Jacob 			mp->error = 0;
196565b024e1SMatt Jacob 			(*eptr)(mp, NULL, 0, 0);
19664fd13c1bSMatt Jacob 			goto mbxsync;
196765b024e1SMatt Jacob 		}
196865b024e1SMatt Jacob 	} else
196965b024e1SMatt Jacob #endif
197065b024e1SMatt Jacob 	eptr = dma2;
197165b024e1SMatt Jacob 
19724fd13c1bSMatt Jacob 
197305fbcbb0SMatt Jacob 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
197405fbcbb0SMatt Jacob 	    (csio->dxfer_len == 0)) {
197542426921SMatt Jacob 		rq->req_seg_count = 1;
19764fd13c1bSMatt Jacob 		goto mbxsync;
197742426921SMatt Jacob 	}
197842426921SMatt Jacob 
1979d720e6d5SJustin T. Gibbs 	/*
1980d720e6d5SJustin T. Gibbs 	 * Do a virtual grapevine step to collect info for
19814873663cSMatt Jacob 	 * the callback dma allocation that we have to use...
1982d720e6d5SJustin T. Gibbs 	 */
1983d720e6d5SJustin T. Gibbs 	mp = &mush;
1984d720e6d5SJustin T. Gibbs 	mp->isp = isp;
19859e11e5beSMatt Jacob 	mp->cmd_token = csio;
1986d720e6d5SJustin T. Gibbs 	mp->rq = rq;
19874fd13c1bSMatt Jacob 	mp->nxtip = nxtip;
1988d720e6d5SJustin T. Gibbs 	mp->optr = optr;
1989d720e6d5SJustin T. Gibbs 	mp->error = 0;
1990d720e6d5SJustin T. Gibbs 
19919e11e5beSMatt Jacob 	if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
19929e11e5beSMatt Jacob 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
19934873663cSMatt Jacob 			int error, s;
19941923f739SMatt Jacob 			dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
1995d720e6d5SJustin T. Gibbs 			s = splsoftvm();
19961923f739SMatt Jacob 			error = bus_dmamap_load(pcs->dmat, *dp,
19979e11e5beSMatt Jacob 			    csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
1998d720e6d5SJustin T. Gibbs 			if (error == EINPROGRESS) {
19991923f739SMatt Jacob 				bus_dmamap_unload(pcs->dmat, *dp);
2000d720e6d5SJustin T. Gibbs 				mp->error = EINVAL;
2001f7dddf8aSMatt Jacob 				isp_prt(isp, ISP_LOGERR,
2002f7dddf8aSMatt Jacob 				    "deferred dma allocation not supported");
2003d720e6d5SJustin T. Gibbs 			} else if (error && mp->error == 0) {
20040a5f7e8bSMatt Jacob #ifdef	DIAGNOSTIC
20056e5c5328SMatt Jacob 				isp_prt(isp, ISP_LOGERR,
20066e5c5328SMatt Jacob 				    "error %d in dma mapping code", error);
20070a5f7e8bSMatt Jacob #endif
2008d720e6d5SJustin T. Gibbs 				mp->error = error;
2009d720e6d5SJustin T. Gibbs 			}
20104873663cSMatt Jacob 			splx(s);
2011d720e6d5SJustin T. Gibbs 		} else {
2012d720e6d5SJustin T. Gibbs 			/* Pointer to physical buffer */
2013d720e6d5SJustin T. Gibbs 			struct bus_dma_segment seg;
20146de9bf77SMatt Jacob 			seg.ds_addr = (bus_addr_t)(vm_offset_t)csio->data_ptr;
2015d720e6d5SJustin T. Gibbs 			seg.ds_len = csio->dxfer_len;
20169e11e5beSMatt Jacob 			(*eptr)(mp, &seg, 1, 0);
2017d720e6d5SJustin T. Gibbs 		}
2018d720e6d5SJustin T. Gibbs 	} else {
2019d720e6d5SJustin T. Gibbs 		struct bus_dma_segment *segs;
2020d720e6d5SJustin T. Gibbs 
20219e11e5beSMatt Jacob 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
2022f7dddf8aSMatt Jacob 			isp_prt(isp, ISP_LOGERR,
2023f7dddf8aSMatt Jacob 			    "Physical segment pointers unsupported");
2024d720e6d5SJustin T. Gibbs 			mp->error = EINVAL;
20259e11e5beSMatt Jacob 		} else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
2026f7dddf8aSMatt Jacob 			isp_prt(isp, ISP_LOGERR,
2027f7dddf8aSMatt Jacob 			    "Virtual segment addresses unsupported");
2028d720e6d5SJustin T. Gibbs 			mp->error = EINVAL;
2029d720e6d5SJustin T. Gibbs 		} else {
2030d720e6d5SJustin T. Gibbs 			/* Just use the segments provided */
2031d720e6d5SJustin T. Gibbs 			segs = (struct bus_dma_segment *) csio->data_ptr;
20329e11e5beSMatt Jacob 			(*eptr)(mp, segs, csio->sglist_cnt, 0);
2033d720e6d5SJustin T. Gibbs 		}
2034d720e6d5SJustin T. Gibbs 	}
2035d720e6d5SJustin T. Gibbs 	if (mp->error) {
20364873663cSMatt Jacob 		int retval = CMD_COMPLETE;
20374873663cSMatt Jacob 		if (mp->error == MUSHERR_NOQENTRIES) {
20384873663cSMatt Jacob 			retval = CMD_EAGAIN;
20394873663cSMatt Jacob 		} else if (mp->error == EFBIG) {
20400a5f7e8bSMatt Jacob 			XS_SETERR(csio, CAM_REQ_TOO_BIG);
2041d720e6d5SJustin T. Gibbs 		} else if (mp->error == EINVAL) {
20420a5f7e8bSMatt Jacob 			XS_SETERR(csio, CAM_REQ_INVALID);
2043d720e6d5SJustin T. Gibbs 		} else {
20440a5f7e8bSMatt Jacob 			XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
2045d720e6d5SJustin T. Gibbs 		}
20464873663cSMatt Jacob 		return (retval);
20470a5f7e8bSMatt Jacob 	}
20484fd13c1bSMatt Jacob mbxsync:
20494fd13c1bSMatt Jacob 	switch (rq->req_header.rqs_entry_type) {
20504fd13c1bSMatt Jacob 	case RQSTYPE_REQUEST:
20514fd13c1bSMatt Jacob 		isp_put_request(isp, rq, qep);
20524fd13c1bSMatt Jacob 		break;
20534fd13c1bSMatt Jacob 	case RQSTYPE_CMDONLY:
20544fd13c1bSMatt Jacob 		isp_put_extended_request(isp, (ispextreq_t *)rq,
20554fd13c1bSMatt Jacob 		    (ispextreq_t *)qep);
20564fd13c1bSMatt Jacob 		break;
20574fd13c1bSMatt Jacob 	case RQSTYPE_T2RQS:
20584fd13c1bSMatt Jacob 		isp_put_request_t2(isp, (ispreqt2_t *) rq, (ispreqt2_t *) qep);
20594fd13c1bSMatt Jacob 		break;
20606de9bf77SMatt Jacob 	case RQSTYPE_A64:
20616de9bf77SMatt Jacob 	case RQSTYPE_T3RQS:
20626de9bf77SMatt Jacob 		isp_put_request_t3(isp, (ispreqt3_t *) rq, (ispreqt3_t *) qep);
20636de9bf77SMatt Jacob 		break;
20640a5f7e8bSMatt Jacob 	}
20654873663cSMatt Jacob 	return (CMD_QUEUED);
2066d720e6d5SJustin T. Gibbs }
2067d720e6d5SJustin T. Gibbs 
2068d720e6d5SJustin T. Gibbs static void
2069d8d5f2adSMatt Jacob isp_pci_dmateardown(struct ispsoftc *isp, XS_T *xs, u_int16_t handle)
2070d720e6d5SJustin T. Gibbs {
20711923f739SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
20721923f739SMatt Jacob 	bus_dmamap_t *dp = &pcs->dmaps[isp_handle_index(handle)];
2073a95ae193SMatt Jacob 	if ((xs->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
20741923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_POSTREAD);
2075d720e6d5SJustin T. Gibbs 	} else {
20761923f739SMatt Jacob 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_POSTWRITE);
2077d720e6d5SJustin T. Gibbs 	}
20781923f739SMatt Jacob 	bus_dmamap_unload(pcs->dmat, *dp);
2079d720e6d5SJustin T. Gibbs }
2080d720e6d5SJustin T. Gibbs 
208165adb54cSMatt Jacob 
208265adb54cSMatt Jacob static void
208317e318c6SMatt Jacob isp_pci_reset1(struct ispsoftc *isp)
208465adb54cSMatt Jacob {
208565adb54cSMatt Jacob 	/* Make sure the BIOS is disabled */
208665adb54cSMatt Jacob 	isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
2087469b6b9eSMatt Jacob 	/* and enable interrupts */
2088469b6b9eSMatt Jacob 	ENABLE_INTS(isp);
208965adb54cSMatt Jacob }
209065adb54cSMatt Jacob 
209165adb54cSMatt Jacob static void
2092d02373f1SMatt Jacob isp_pci_dumpregs(struct ispsoftc *isp, const char *msg)
209365adb54cSMatt Jacob {
20941923f739SMatt Jacob 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
2095d02373f1SMatt Jacob 	if (msg)
20966e5c5328SMatt Jacob 		printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
20976e5c5328SMatt Jacob 	else
20986e5c5328SMatt Jacob 		printf("%s:\n", device_get_nameunit(isp->isp_dev));
2099d02373f1SMatt Jacob 	if (IS_SCSI(isp))
2100d02373f1SMatt Jacob 		printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
2101d02373f1SMatt Jacob 	else
2102d02373f1SMatt Jacob 		printf("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
2103d02373f1SMatt Jacob 	printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
2104d02373f1SMatt Jacob 	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
2105d02373f1SMatt Jacob 	printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
2106d02373f1SMatt Jacob 
2107d02373f1SMatt Jacob 
2108d02373f1SMatt Jacob 	if (IS_SCSI(isp)) {
2109d02373f1SMatt Jacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
2110d02373f1SMatt Jacob 		printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
2111d02373f1SMatt Jacob 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
2112d02373f1SMatt Jacob 			ISP_READ(isp, CDMA_FIFO_STS));
2113d02373f1SMatt Jacob 		printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
2114d02373f1SMatt Jacob 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
2115d02373f1SMatt Jacob 			ISP_READ(isp, DDMA_FIFO_STS));
2116d02373f1SMatt Jacob 		printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
2117d02373f1SMatt Jacob 			ISP_READ(isp, SXP_INTERRUPT),
2118d02373f1SMatt Jacob 			ISP_READ(isp, SXP_GROSS_ERR),
2119d02373f1SMatt Jacob 			ISP_READ(isp, SXP_PINS_CTRL));
2120d02373f1SMatt Jacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
2121d02373f1SMatt Jacob 	}
2122d02373f1SMatt Jacob 	printf("    mbox regs: %x %x %x %x %x\n",
2123d02373f1SMatt Jacob 	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
2124d02373f1SMatt Jacob 	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
2125d02373f1SMatt Jacob 	    ISP_READ(isp, OUTMAILBOX4));
2126d02373f1SMatt Jacob 	printf("    PCI Status Command/Status=%x\n",
21271923f739SMatt Jacob 	    pci_read_config(pcs->pci_dev, PCIR_COMMAND, 1));
212865adb54cSMatt Jacob }
2129