xref: /freebsd/sys/dev/isp/isp_pci.c (revision 7dfd9569a2f0637fb9a48157b1c1bfe5709faee3)
1 /*-
2  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
3  * FreeBSD Version.
4  *
5  * Copyright (c) 1997-2006 by Matthew Jacob
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #if __FreeBSD_version < 500000
40 #include <sys/bus.h>
41 #include <pci/pcireg.h>
42 #include <pci/pcivar.h>
43 #include <machine/bus_memio.h>
44 #include <machine/bus_pio.h>
45 #else
46 #include <sys/stdint.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #endif
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <sys/rman.h>
53 #include <sys/malloc.h>
54 
55 #include <dev/isp/isp_freebsd.h>
56 
57 #if __FreeBSD_version < 500000
58 #define	BUS_PROBE_DEFAULT	0
59 #endif
60 
61 static uint16_t isp_pci_rd_reg(ispsoftc_t *, int);
62 static void isp_pci_wr_reg(ispsoftc_t *, int, uint16_t);
63 static uint16_t isp_pci_rd_reg_1080(ispsoftc_t *, int);
64 static void isp_pci_wr_reg_1080(ispsoftc_t *, int, uint16_t);
65 static int
66 isp_pci_rd_isr(ispsoftc_t *, uint16_t *, uint16_t *, uint16_t *);
67 static int
68 isp_pci_rd_isr_2300(ispsoftc_t *, uint16_t *, uint16_t *, uint16_t *);
69 static int isp_pci_mbxdma(ispsoftc_t *);
70 static int
71 isp_pci_dmasetup(ispsoftc_t *, XS_T *, ispreq_t *, uint16_t *, uint16_t);
72 static void
73 isp_pci_dmateardown(ispsoftc_t *, XS_T *, uint16_t);
74 
75 static void isp_pci_reset1(ispsoftc_t *);
76 static void isp_pci_dumpregs(ispsoftc_t *, const char *);
77 
78 static struct ispmdvec mdvec = {
79 	isp_pci_rd_isr,
80 	isp_pci_rd_reg,
81 	isp_pci_wr_reg,
82 	isp_pci_mbxdma,
83 	isp_pci_dmasetup,
84 	isp_pci_dmateardown,
85 	NULL,
86 	isp_pci_reset1,
87 	isp_pci_dumpregs,
88 	NULL,
89 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
90 };
91 
92 static struct ispmdvec mdvec_1080 = {
93 	isp_pci_rd_isr,
94 	isp_pci_rd_reg_1080,
95 	isp_pci_wr_reg_1080,
96 	isp_pci_mbxdma,
97 	isp_pci_dmasetup,
98 	isp_pci_dmateardown,
99 	NULL,
100 	isp_pci_reset1,
101 	isp_pci_dumpregs,
102 	NULL,
103 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
104 };
105 
106 static struct ispmdvec mdvec_12160 = {
107 	isp_pci_rd_isr,
108 	isp_pci_rd_reg_1080,
109 	isp_pci_wr_reg_1080,
110 	isp_pci_mbxdma,
111 	isp_pci_dmasetup,
112 	isp_pci_dmateardown,
113 	NULL,
114 	isp_pci_reset1,
115 	isp_pci_dumpregs,
116 	NULL,
117 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
118 };
119 
120 static struct ispmdvec mdvec_2100 = {
121 	isp_pci_rd_isr,
122 	isp_pci_rd_reg,
123 	isp_pci_wr_reg,
124 	isp_pci_mbxdma,
125 	isp_pci_dmasetup,
126 	isp_pci_dmateardown,
127 	NULL,
128 	isp_pci_reset1,
129 	isp_pci_dumpregs
130 };
131 
132 static struct ispmdvec mdvec_2200 = {
133 	isp_pci_rd_isr,
134 	isp_pci_rd_reg,
135 	isp_pci_wr_reg,
136 	isp_pci_mbxdma,
137 	isp_pci_dmasetup,
138 	isp_pci_dmateardown,
139 	NULL,
140 	isp_pci_reset1,
141 	isp_pci_dumpregs
142 };
143 
144 static struct ispmdvec mdvec_2300 = {
145 	isp_pci_rd_isr_2300,
146 	isp_pci_rd_reg,
147 	isp_pci_wr_reg,
148 	isp_pci_mbxdma,
149 	isp_pci_dmasetup,
150 	isp_pci_dmateardown,
151 	NULL,
152 	isp_pci_reset1,
153 	isp_pci_dumpregs
154 };
155 
156 #ifndef	PCIM_CMD_INVEN
157 #define	PCIM_CMD_INVEN			0x10
158 #endif
159 #ifndef	PCIM_CMD_BUSMASTEREN
160 #define	PCIM_CMD_BUSMASTEREN		0x0004
161 #endif
162 #ifndef	PCIM_CMD_PERRESPEN
163 #define	PCIM_CMD_PERRESPEN		0x0040
164 #endif
165 #ifndef	PCIM_CMD_SEREN
166 #define	PCIM_CMD_SEREN			0x0100
167 #endif
168 
169 #ifndef	PCIR_COMMAND
170 #define	PCIR_COMMAND			0x04
171 #endif
172 
173 #ifndef	PCIR_CACHELNSZ
174 #define	PCIR_CACHELNSZ			0x0c
175 #endif
176 
177 #ifndef	PCIR_LATTIMER
178 #define	PCIR_LATTIMER			0x0d
179 #endif
180 
181 #ifndef	PCIR_ROMADDR
182 #define	PCIR_ROMADDR			0x30
183 #endif
184 
185 #ifndef	PCI_VENDOR_QLOGIC
186 #define	PCI_VENDOR_QLOGIC		0x1077
187 #endif
188 
189 #ifndef	PCI_PRODUCT_QLOGIC_ISP1020
190 #define	PCI_PRODUCT_QLOGIC_ISP1020	0x1020
191 #endif
192 
193 #ifndef	PCI_PRODUCT_QLOGIC_ISP1080
194 #define	PCI_PRODUCT_QLOGIC_ISP1080	0x1080
195 #endif
196 
197 #ifndef	PCI_PRODUCT_QLOGIC_ISP10160
198 #define	PCI_PRODUCT_QLOGIC_ISP10160	0x1016
199 #endif
200 
201 #ifndef	PCI_PRODUCT_QLOGIC_ISP12160
202 #define	PCI_PRODUCT_QLOGIC_ISP12160	0x1216
203 #endif
204 
205 #ifndef	PCI_PRODUCT_QLOGIC_ISP1240
206 #define	PCI_PRODUCT_QLOGIC_ISP1240	0x1240
207 #endif
208 
209 #ifndef	PCI_PRODUCT_QLOGIC_ISP1280
210 #define	PCI_PRODUCT_QLOGIC_ISP1280	0x1280
211 #endif
212 
213 #ifndef	PCI_PRODUCT_QLOGIC_ISP2100
214 #define	PCI_PRODUCT_QLOGIC_ISP2100	0x2100
215 #endif
216 
217 #ifndef	PCI_PRODUCT_QLOGIC_ISP2200
218 #define	PCI_PRODUCT_QLOGIC_ISP2200	0x2200
219 #endif
220 
221 #ifndef	PCI_PRODUCT_QLOGIC_ISP2300
222 #define	PCI_PRODUCT_QLOGIC_ISP2300	0x2300
223 #endif
224 
225 #ifndef	PCI_PRODUCT_QLOGIC_ISP2312
226 #define	PCI_PRODUCT_QLOGIC_ISP2312	0x2312
227 #endif
228 
229 #ifndef	PCI_PRODUCT_QLOGIC_ISP2322
230 #define	PCI_PRODUCT_QLOGIC_ISP2322	0x2322
231 #endif
232 
233 #ifndef	PCI_PRODUCT_QLOGIC_ISP2422
234 #define	PCI_PRODUCT_QLOGIC_ISP2422	0x2422
235 #endif
236 
237 #ifndef	PCI_PRODUCT_QLOGIC_ISP6312
238 #define	PCI_PRODUCT_QLOGIC_ISP6312	0x6312
239 #endif
240 
241 #define	PCI_QLOGIC_ISP1020	\
242 	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
243 
244 #define	PCI_QLOGIC_ISP1080	\
245 	((PCI_PRODUCT_QLOGIC_ISP1080 << 16) | PCI_VENDOR_QLOGIC)
246 
247 #define	PCI_QLOGIC_ISP10160	\
248 	((PCI_PRODUCT_QLOGIC_ISP10160 << 16) | PCI_VENDOR_QLOGIC)
249 
250 #define	PCI_QLOGIC_ISP12160	\
251 	((PCI_PRODUCT_QLOGIC_ISP12160 << 16) | PCI_VENDOR_QLOGIC)
252 
253 #define	PCI_QLOGIC_ISP1240	\
254 	((PCI_PRODUCT_QLOGIC_ISP1240 << 16) | PCI_VENDOR_QLOGIC)
255 
256 #define	PCI_QLOGIC_ISP1280	\
257 	((PCI_PRODUCT_QLOGIC_ISP1280 << 16) | PCI_VENDOR_QLOGIC)
258 
259 #define	PCI_QLOGIC_ISP2100	\
260 	((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
261 
262 #define	PCI_QLOGIC_ISP2200	\
263 	((PCI_PRODUCT_QLOGIC_ISP2200 << 16) | PCI_VENDOR_QLOGIC)
264 
265 #define	PCI_QLOGIC_ISP2300	\
266 	((PCI_PRODUCT_QLOGIC_ISP2300 << 16) | PCI_VENDOR_QLOGIC)
267 
268 #define	PCI_QLOGIC_ISP2312	\
269 	((PCI_PRODUCT_QLOGIC_ISP2312 << 16) | PCI_VENDOR_QLOGIC)
270 
271 #define	PCI_QLOGIC_ISP2322	\
272 	((PCI_PRODUCT_QLOGIC_ISP2322 << 16) | PCI_VENDOR_QLOGIC)
273 
274 #define	PCI_QLOGIC_ISP2422	\
275 	((PCI_PRODUCT_QLOGIC_ISP2422 << 16) | PCI_VENDOR_QLOGIC)
276 
277 #define	PCI_QLOGIC_ISP6312	\
278 	((PCI_PRODUCT_QLOGIC_ISP6312 << 16) | PCI_VENDOR_QLOGIC)
279 
280 /*
281  * Odd case for some AMI raid cards... We need to *not* attach to this.
282  */
283 #define	AMI_RAID_SUBVENDOR_ID	0x101e
284 
285 #define	IO_MAP_REG	0x10
286 #define	MEM_MAP_REG	0x14
287 
288 #define	PCI_DFLT_LTNCY	0x40
289 #define	PCI_DFLT_LNSZ	0x10
290 
291 static int isp_pci_probe (device_t);
292 static int isp_pci_attach (device_t);
293 
294 
295 struct isp_pcisoftc {
296 	ispsoftc_t			pci_isp;
297 	device_t			pci_dev;
298 	struct resource *		pci_reg;
299 	bus_space_tag_t			pci_st;
300 	bus_space_handle_t		pci_sh;
301 	void *				ih;
302 	int16_t				pci_poff[_NREG_BLKS];
303 	bus_dma_tag_t			dmat;
304 	bus_dmamap_t			*dmaps;
305 };
306 extern ispfwfunc *isp_get_firmware_p;
307 
308 static device_method_t isp_pci_methods[] = {
309 	/* Device interface */
310 	DEVMETHOD(device_probe,		isp_pci_probe),
311 	DEVMETHOD(device_attach,	isp_pci_attach),
312 	{ 0, 0 }
313 };
314 static void isp_pci_intr(void *);
315 
316 static driver_t isp_pci_driver = {
317 	"isp", isp_pci_methods, sizeof (struct isp_pcisoftc)
318 };
319 static devclass_t isp_devclass;
320 DRIVER_MODULE(isp, pci, isp_pci_driver, isp_devclass, 0, 0);
321 
322 static int
323 isp_pci_probe(device_t dev)
324 {
325         switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
326 	case PCI_QLOGIC_ISP1020:
327 		device_set_desc(dev, "Qlogic ISP 1020/1040 PCI SCSI Adapter");
328 		break;
329 	case PCI_QLOGIC_ISP1080:
330 		device_set_desc(dev, "Qlogic ISP 1080 PCI SCSI Adapter");
331 		break;
332 	case PCI_QLOGIC_ISP1240:
333 		device_set_desc(dev, "Qlogic ISP 1240 PCI SCSI Adapter");
334 		break;
335 	case PCI_QLOGIC_ISP1280:
336 		device_set_desc(dev, "Qlogic ISP 1280 PCI SCSI Adapter");
337 		break;
338 	case PCI_QLOGIC_ISP10160:
339 		device_set_desc(dev, "Qlogic ISP 10160 PCI SCSI Adapter");
340 		break;
341 	case PCI_QLOGIC_ISP12160:
342 		if (pci_get_subvendor(dev) == AMI_RAID_SUBVENDOR_ID) {
343 			return (ENXIO);
344 		}
345 		device_set_desc(dev, "Qlogic ISP 12160 PCI SCSI Adapter");
346 		break;
347 	case PCI_QLOGIC_ISP2100:
348 		device_set_desc(dev, "Qlogic ISP 2100 PCI FC-AL Adapter");
349 		break;
350 	case PCI_QLOGIC_ISP2200:
351 		device_set_desc(dev, "Qlogic ISP 2200 PCI FC-AL Adapter");
352 		break;
353 	case PCI_QLOGIC_ISP2300:
354 		device_set_desc(dev, "Qlogic ISP 2300 PCI FC-AL Adapter");
355 		break;
356 	case PCI_QLOGIC_ISP2312:
357 		device_set_desc(dev, "Qlogic ISP 2312 PCI FC-AL Adapter");
358 		break;
359 	case PCI_QLOGIC_ISP2322:
360 		device_set_desc(dev, "Qlogic ISP 2322 PCI FC-AL Adapter");
361 		break;
362 	case PCI_QLOGIC_ISP2422:
363 		device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
364 		break;
365 	case PCI_QLOGIC_ISP6312:
366 		device_set_desc(dev, "Qlogic ISP 6312 PCI FC-AL Adapter");
367 		break;
368 	default:
369 		return (ENXIO);
370 	}
371 	if (isp_announced == 0 && bootverbose) {
372 		printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
373 		    "Core Version %d.%d\n",
374 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
375 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
376 		isp_announced++;
377 	}
378 	/*
379 	 * XXXX: Here is where we might load the f/w module
380 	 * XXXX: (or increase a reference count to it).
381 	 */
382 	return (BUS_PROBE_DEFAULT);
383 }
384 
385 #if __FreeBSD_version < 500000
386 static void
387 isp_get_options(device_t dev, ispsoftc_t *isp)
388 {
389 	uint64_t wwn;
390 	int bitmap, unit;
391 
392 	unit = device_get_unit(dev);
393 	if (getenv_int("isp_disable", &bitmap)) {
394 		if (bitmap & (1 << unit)) {
395 			isp->isp_osinfo.disabled = 1;
396 			return;
397 		}
398 	}
399 
400 	if (getenv_int("isp_no_fwload", &bitmap)) {
401 		if (bitmap & (1 << unit))
402 			isp->isp_confopts |= ISP_CFG_NORELOAD;
403 	}
404 	if (getenv_int("isp_fwload", &bitmap)) {
405 		if (bitmap & (1 << unit))
406 			isp->isp_confopts &= ~ISP_CFG_NORELOAD;
407 	}
408 	if (getenv_int("isp_no_nvram", &bitmap)) {
409 		if (bitmap & (1 << unit))
410 			isp->isp_confopts |= ISP_CFG_NONVRAM;
411 	}
412 	if (getenv_int("isp_nvram", &bitmap)) {
413 		if (bitmap & (1 << unit))
414 			isp->isp_confopts &= ~ISP_CFG_NONVRAM;
415 	}
416 	if (getenv_int("isp_fcduplex", &bitmap)) {
417 		if (bitmap & (1 << unit))
418 			isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
419 	}
420 	if (getenv_int("isp_no_fcduplex", &bitmap)) {
421 		if (bitmap & (1 << unit))
422 			isp->isp_confopts &= ~ISP_CFG_FULL_DUPLEX;
423 	}
424 	if (getenv_int("isp_nport", &bitmap)) {
425 		if (bitmap & (1 << unit))
426 			isp->isp_confopts |= ISP_CFG_NPORT;
427 	}
428 
429 	/*
430 	 * Because the resource_*_value functions can neither return
431 	 * 64 bit integer values, nor can they be directly coerced
432 	 * to interpret the right hand side of the assignment as
433 	 * you want them to interpret it, we have to force WWN
434 	 * hint replacement to specify WWN strings with a leading
435 	 * 'w' (e..g w50000000aaaa0001). Sigh.
436 	 */
437 	if (getenv_quad("isp_portwwn", &wwn)) {
438 		isp->isp_osinfo.default_port_wwn = wwn;
439 		isp->isp_confopts |= ISP_CFG_OWNWWPN;
440 	}
441 	if (isp->isp_osinfo.default_port_wwn == 0) {
442 		isp->isp_osinfo.default_port_wwn = 0x400000007F000009ull;
443 	}
444 
445 	if (getenv_quad("isp_nodewwn", &wwn)) {
446 		isp->isp_osinfo.default_node_wwn = wwn;
447 		isp->isp_confopts |= ISP_CFG_OWNWWNN;
448 	}
449 	if (isp->isp_osinfo.default_node_wwn == 0) {
450 		isp->isp_osinfo.default_node_wwn = 0x400000007F000009ull;
451 	}
452 
453 	bitmap = 0;
454 	(void) getenv_int("isp_debug", &bitmap);
455 	if (bitmap) {
456 		isp->isp_dblev = bitmap;
457 	} else {
458 		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
459 	}
460 	if (bootverbose) {
461 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
462 	}
463 
464 #ifdef	ISP_FW_CRASH_DUMP
465 	bitmap = 0;
466 	if (getenv_int("isp_fw_dump_enable", &bitmap)) {
467 		if (bitmap & (1 << unit) {
468 			size_t amt = 0;
469 			if (IS_2200(isp)) {
470 				amt = QLA2200_RISC_IMAGE_DUMP_SIZE;
471 			} else if (IS_23XX(isp)) {
472 				amt = QLA2300_RISC_IMAGE_DUMP_SIZE;
473 			}
474 			if (amt) {
475 				FCPARAM(isp)->isp_dump_data =
476 				    malloc(amt, M_DEVBUF, M_WAITOK);
477 				bzero(FCPARAM(isp)->isp_dump_data, amt);
478 			} else {
479 				device_printf(dev,
480 				    "f/w crash dumps not supported for card\n");
481 			}
482 		}
483 	}
484 #endif
485 }
486 
487 static void
488 isp_get_pci_options(device_t dev, int *m1, int *m2)
489 {
490 	int bitmap;
491 	int unit = device_get_unit(dev);
492 
493 	*m1 = PCIM_CMD_MEMEN;
494 	*m2 = PCIM_CMD_PORTEN;
495 	if (getenv_int("isp_mem_map", &bitmap)) {
496 		if (bitmap & (1 << unit)) {
497 			*m1 = PCIM_CMD_MEMEN;
498 			*m2 = PCIM_CMD_PORTEN;
499 		}
500 	}
501 	bitmap = 0;
502 	if (getenv_int("isp_io_map", &bitmap)) {
503 		if (bitmap & (1 << unit)) {
504 			*m1 = PCIM_CMD_PORTEN;
505 			*m2 = PCIM_CMD_MEMEN;
506 		}
507 	}
508 }
509 #else
510 static void
511 isp_get_options(device_t dev, ispsoftc_t *isp)
512 {
513 	int tval;
514 	const char *sptr;
515 	/*
516 	 * Figure out if we're supposed to skip this one.
517 	 */
518 
519 	tval = 0;
520 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
521 	    "disable", &tval) == 0 && tval) {
522 		device_printf(dev, "disabled at user request\n");
523 		isp->isp_osinfo.disabled = 1;
524 		return;
525 	}
526 
527 	tval = -1;
528 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
529 	    "role", &tval) == 0 && tval != -1) {
530 		tval &= (ISP_ROLE_INITIATOR|ISP_ROLE_TARGET);
531 		isp->isp_role = tval;
532 		device_printf(dev, "setting role to 0x%x\n", isp->isp_role);
533 	} else {
534 #ifdef	ISP_TARGET_MODE
535 		isp->isp_role = ISP_ROLE_TARGET;
536 #else
537 		isp->isp_role = ISP_DEFAULT_ROLES;
538 #endif
539 	}
540 
541 	tval = 0;
542         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
543             "fwload_disable", &tval) == 0 && tval != 0) {
544 		isp->isp_confopts |= ISP_CFG_NORELOAD;
545 	}
546 	tval = 0;
547         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
548             "ignore_nvram", &tval) == 0 && tval != 0) {
549 		isp->isp_confopts |= ISP_CFG_NONVRAM;
550 	}
551 	tval = 0;
552         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
553             "fullduplex", &tval) == 0 && tval != 0) {
554 		isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
555 	}
556 #ifdef	ISP_FW_CRASH_DUMP
557 	tval = 0;
558         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
559             "fw_dump_enable", &tval) == 0 && tval != 0) {
560 		size_t amt = 0;
561 		if (IS_2200(isp)) {
562 			amt = QLA2200_RISC_IMAGE_DUMP_SIZE;
563 		} else if (IS_23XX(isp)) {
564 			amt = QLA2300_RISC_IMAGE_DUMP_SIZE;
565 		}
566 		if (amt) {
567 			FCPARAM(isp)->isp_dump_data =
568 			    malloc(amt, M_DEVBUF, M_WAITOK | M_ZERO);
569 		} else {
570 			device_printf(dev,
571 			    "f/w crash dumps not supported for this model\n");
572 		}
573 	}
574 #endif
575 
576 	sptr = 0;
577         if (resource_string_value(device_get_name(dev), device_get_unit(dev),
578             "topology", (const char **) &sptr) == 0 && sptr != 0) {
579 		if (strcmp(sptr, "lport") == 0) {
580 			isp->isp_confopts |= ISP_CFG_LPORT;
581 		} else if (strcmp(sptr, "nport") == 0) {
582 			isp->isp_confopts |= ISP_CFG_NPORT;
583 		} else if (strcmp(sptr, "lport-only") == 0) {
584 			isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
585 		} else if (strcmp(sptr, "nport-only") == 0) {
586 			isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
587 		}
588 	}
589 
590 	/*
591 	 * Because the resource_*_value functions can neither return
592 	 * 64 bit integer values, nor can they be directly coerced
593 	 * to interpret the right hand side of the assignment as
594 	 * you want them to interpret it, we have to force WWN
595 	 * hint replacement to specify WWN strings with a leading
596 	 * 'w' (e..g w50000000aaaa0001). Sigh.
597 	 */
598 	sptr = 0;
599 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
600             "portwwn", (const char **) &sptr);
601 	if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
602 		char *eptr = 0;
603 		isp->isp_osinfo.default_port_wwn = strtouq(sptr, &eptr, 16);
604 		if (eptr < sptr + 16 || isp->isp_osinfo.default_port_wwn == 0) {
605 			device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
606 			isp->isp_osinfo.default_port_wwn = 0;
607 		} else {
608 			isp->isp_confopts |= ISP_CFG_OWNWWPN;
609 		}
610 	}
611 	if (isp->isp_osinfo.default_port_wwn == 0) {
612 		isp->isp_osinfo.default_port_wwn = 0x400000007F000009ull;
613 	}
614 
615 	sptr = 0;
616 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
617             "nodewwn", (const char **) &sptr);
618 	if (tval == 0 && sptr != 0 && *sptr++ == 'w') {
619 		char *eptr = 0;
620 		isp->isp_osinfo.default_node_wwn = strtouq(sptr, &eptr, 16);
621 		if (eptr < sptr + 16 || isp->isp_osinfo.default_node_wwn == 0) {
622 			device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
623 			isp->isp_osinfo.default_node_wwn = 0;
624 		} else {
625 			isp->isp_confopts |= ISP_CFG_OWNWWNN;
626 		}
627 	}
628 	if (isp->isp_osinfo.default_node_wwn == 0) {
629 		isp->isp_osinfo.default_node_wwn = 0x400000007F000009ull;
630 	}
631 
632 	isp->isp_osinfo.default_id = -1;
633 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
634             "iid", &tval) == 0) {
635 		isp->isp_osinfo.default_id = tval;
636 		isp->isp_confopts |= ISP_CFG_OWNLOOPID;
637 	}
638 	if (isp->isp_osinfo.default_id == -1) {
639 		if (IS_FC(isp)) {
640 			isp->isp_osinfo.default_id = 109;
641 		} else {
642 			isp->isp_osinfo.default_id = 7;
643 		}
644 	}
645 
646 	/*
647 	 * Set up logging levels.
648 	 */
649 	tval = 0;
650         (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
651             "debug", &tval);
652 	if (tval) {
653 		isp->isp_dblev = tval;
654 	} else {
655 		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
656 	}
657 	if (bootverbose) {
658 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
659 	}
660 
661 }
662 
663 static void
664 isp_get_pci_options(device_t dev, int *m1, int *m2)
665 {
666 	int tval;
667 	/*
668 	 * Which we should try first - memory mapping or i/o mapping?
669 	 *
670 	 * We used to try memory first followed by i/o on alpha, otherwise
671 	 * the reverse, but we should just try memory first all the time now.
672 	 */
673 	*m1 = PCIM_CMD_MEMEN;
674 	*m2 = PCIM_CMD_PORTEN;
675 
676 	tval = 0;
677         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
678             "prefer_iomap", &tval) == 0 && tval != 0) {
679 		*m1 = PCIM_CMD_PORTEN;
680 		*m2 = PCIM_CMD_MEMEN;
681 	}
682 	tval = 0;
683         if (resource_int_value(device_get_name(dev), device_get_unit(dev),
684             "prefer_memmap", &tval) == 0 && tval != 0) {
685 		*m1 = PCIM_CMD_MEMEN;
686 		*m2 = PCIM_CMD_PORTEN;
687 	}
688 }
689 #endif
690 
691 static int
692 isp_pci_attach(device_t dev)
693 {
694 	struct resource *regs, *irq;
695 	int rtp, rgd, iqd, m1, m2;
696 	uint32_t data, cmd, linesz, psize, basetype;
697 	struct isp_pcisoftc *pcs;
698 	ispsoftc_t *isp = NULL;
699 	struct ispmdvec *mdvp;
700 #if __FreeBSD_version >= 500000
701 	int locksetup = 0;
702 #endif
703 
704 	pcs = device_get_softc(dev);
705 	if (pcs == NULL) {
706 		device_printf(dev, "cannot get softc\n");
707 		return (ENOMEM);
708 	}
709 	memset(pcs, 0, sizeof (*pcs));
710 	pcs->pci_dev = dev;
711 	isp = &pcs->pci_isp;
712 
713 	/*
714 	 * Get Generic Options
715 	 */
716 	isp_get_options(dev, isp);
717 
718 	/*
719 	 * Check to see if options have us disabled
720 	 */
721 	if (isp->isp_osinfo.disabled) {
722 		/*
723 		 * But return zero to preserve unit numbering
724 		 */
725 		return (0);
726 	}
727 
728 	/*
729 	 * Get PCI options- which in this case are just mapping preferences.
730 	 */
731 	isp_get_pci_options(dev, &m1, &m2);
732 
733 
734 	linesz = PCI_DFLT_LNSZ;
735 	irq = regs = NULL;
736 	rgd = rtp = iqd = 0;
737 
738 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
739 	if (cmd & m1) {
740 		rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
741 		rgd = (m1 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
742 		regs = bus_alloc_resource_any(dev, rtp, &rgd, RF_ACTIVE);
743 	}
744 	if (regs == NULL && (cmd & m2)) {
745 		rtp = (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
746 		rgd = (m2 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
747 		regs = bus_alloc_resource_any(dev, rtp, &rgd, RF_ACTIVE);
748 	}
749 	if (regs == NULL) {
750 		device_printf(dev, "unable to map any ports\n");
751 		goto bad;
752 	}
753 	if (bootverbose) {
754 		device_printf(dev, "using %s space register mapping\n",
755 		    (rgd == IO_MAP_REG)? "I/O" : "Memory");
756 	}
757 	pcs->pci_dev = dev;
758 	pcs->pci_reg = regs;
759 	pcs->pci_st = rman_get_bustag(regs);
760 	pcs->pci_sh = rman_get_bushandle(regs);
761 
762 	pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
763 	pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
764 	pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
765 	pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
766 	pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
767 	mdvp = &mdvec;
768 	basetype = ISP_HA_SCSI_UNKNOWN;
769 	psize = sizeof (sdparam);
770 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1020) {
771 		mdvp = &mdvec;
772 		basetype = ISP_HA_SCSI_UNKNOWN;
773 		psize = sizeof (sdparam);
774 	}
775 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1080) {
776 		mdvp = &mdvec_1080;
777 		basetype = ISP_HA_SCSI_1080;
778 		psize = sizeof (sdparam);
779 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
780 		    ISP1080_DMA_REGS_OFF;
781 	}
782 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1240) {
783 		mdvp = &mdvec_1080;
784 		basetype = ISP_HA_SCSI_1240;
785 		psize = 2 * sizeof (sdparam);
786 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
787 		    ISP1080_DMA_REGS_OFF;
788 	}
789 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP1280) {
790 		mdvp = &mdvec_1080;
791 		basetype = ISP_HA_SCSI_1280;
792 		psize = 2 * sizeof (sdparam);
793 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
794 		    ISP1080_DMA_REGS_OFF;
795 	}
796 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP10160) {
797 		mdvp = &mdvec_12160;
798 		basetype = ISP_HA_SCSI_10160;
799 		psize = sizeof (sdparam);
800 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
801 		    ISP1080_DMA_REGS_OFF;
802 	}
803 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP12160) {
804 		mdvp = &mdvec_12160;
805 		basetype = ISP_HA_SCSI_12160;
806 		psize = 2 * sizeof (sdparam);
807 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] =
808 		    ISP1080_DMA_REGS_OFF;
809 	}
810 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2100) {
811 		mdvp = &mdvec_2100;
812 		basetype = ISP_HA_FC_2100;
813 		psize = sizeof (fcparam);
814 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
815 		    PCI_MBOX_REGS2100_OFF;
816 		if (pci_get_revid(dev) < 3) {
817 			/*
818 			 * XXX: Need to get the actual revision
819 			 * XXX: number of the 2100 FB. At any rate,
820 			 * XXX: lower cache line size for early revision
821 			 * XXX; boards.
822 			 */
823 			linesz = 1;
824 		}
825 	}
826 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2200) {
827 		mdvp = &mdvec_2200;
828 		basetype = ISP_HA_FC_2200;
829 		psize = sizeof (fcparam);
830 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
831 		    PCI_MBOX_REGS2100_OFF;
832 	}
833 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2300) {
834 		mdvp = &mdvec_2300;
835 		basetype = ISP_HA_FC_2300;
836 		psize = sizeof (fcparam);
837 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
838 		    PCI_MBOX_REGS2300_OFF;
839 	}
840 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2312 ||
841 	    pci_get_devid(dev) == PCI_QLOGIC_ISP6312) {
842 		mdvp = &mdvec_2300;
843 		basetype = ISP_HA_FC_2312;
844 		psize = sizeof (fcparam);
845 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
846 		    PCI_MBOX_REGS2300_OFF;
847 	}
848 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2322) {
849 		mdvp = &mdvec_2300;
850 		basetype = ISP_HA_FC_2322;
851 		psize = sizeof (fcparam);
852 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
853 		    PCI_MBOX_REGS2300_OFF;
854 	}
855 	if (pci_get_devid(dev) == PCI_QLOGIC_ISP2422) {
856 		mdvp = &mdvec_2300;
857 		basetype = ISP_HA_FC_2422;
858 		psize = sizeof (fcparam);
859 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] =
860 		    PCI_MBOX_REGS2300_OFF;
861 	}
862 	isp = &pcs->pci_isp;
863 	isp->isp_param = malloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
864 	if (isp->isp_param == NULL) {
865 		device_printf(dev, "cannot allocate parameter data\n");
866 		goto bad;
867 	}
868 	isp->isp_mdvec = mdvp;
869 	isp->isp_type = basetype;
870 	isp->isp_revision = pci_get_revid(dev);
871 	isp->isp_dev = dev;
872 
873 	/*
874 	 * Try and find firmware for this device.
875 	 */
876 
877 	/*
878 	 * Don't even attempt to get firmware for the 2322/2422 (yet)
879 	 */
880 	if (IS_2322(isp) == 0 && IS_24XX(isp) == 0 && isp_get_firmware_p) {
881 		int device = (int) pci_get_device(dev);
882 #ifdef	ISP_TARGET_MODE
883 		(*isp_get_firmware_p)(0, 1, device, &mdvp->dv_ispfw);
884 #else
885 		(*isp_get_firmware_p)(0, 0, device, &mdvp->dv_ispfw);
886 #endif
887 	}
888 
889 	/*
890 	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER
891 	 * are set.
892 	 */
893 	cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN |
894 		PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
895 	if (IS_2300(isp)) {	/* per QLogic errata */
896 		cmd &= ~PCIM_CMD_INVEN;
897 	}
898 	if (IS_23XX(isp)) {
899 		/*
900 		 * Can't tell if ROM will hang on 'ABOUT FIRMWARE' command.
901 		 */
902 		isp->isp_touched = 1;
903 
904 	}
905 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
906 
907 	/*
908 	 * Make sure the Cache Line Size register is set sensibly.
909 	 */
910 	data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
911 	if (data != linesz) {
912 		data = PCI_DFLT_LNSZ;
913 		isp_prt(isp, ISP_LOGCONFIG, "set PCI line size to %d", data);
914 		pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
915 	}
916 
917 	/*
918 	 * Make sure the Latency Timer is sane.
919 	 */
920 	data = pci_read_config(dev, PCIR_LATTIMER, 1);
921 	if (data < PCI_DFLT_LTNCY) {
922 		data = PCI_DFLT_LTNCY;
923 		isp_prt(isp, ISP_LOGCONFIG, "set PCI latency to %d", data);
924 		pci_write_config(dev, PCIR_LATTIMER, data, 1);
925 	}
926 
927 	/*
928 	 * Make sure we've disabled the ROM.
929 	 */
930 	data = pci_read_config(dev, PCIR_ROMADDR, 4);
931 	data &= ~1;
932 	pci_write_config(dev, PCIR_ROMADDR, data, 4);
933 
934 	iqd = 0;
935 	irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
936 	    RF_ACTIVE | RF_SHAREABLE);
937 	if (irq == NULL) {
938 		device_printf(dev, "could not allocate interrupt\n");
939 		goto bad;
940 	}
941 
942 #if __FreeBSD_version >= 500000
943 	/* Make sure the lock is set up. */
944 	mtx_init(&isp->isp_osinfo.lock, "isp", NULL, MTX_DEF);
945 	locksetup++;
946 #endif
947 
948 	if (bus_setup_intr(dev, irq, ISP_IFLAGS, isp_pci_intr, isp, &pcs->ih)) {
949 		device_printf(dev, "could not setup interrupt\n");
950 		goto bad;
951 	}
952 
953 	/*
954 	 * Last minute checks...
955 	 */
956 	if (IS_23XX(isp)) {
957 		isp->isp_port = pci_get_function(dev);
958 	}
959 
960 	/*
961 	 * Make sure we're in reset state.
962 	 */
963 	ISP_LOCK(isp);
964 	isp_reset(isp);
965 	if (isp->isp_state != ISP_RESETSTATE) {
966 		ISP_UNLOCK(isp);
967 		goto bad;
968 	}
969 	isp_init(isp);
970 	if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_INITSTATE) {
971 		isp_uninit(isp);
972 		ISP_UNLOCK(isp);
973 		goto bad;
974 	}
975 	isp_attach(isp);
976 	if (isp->isp_role != ISP_ROLE_NONE && isp->isp_state != ISP_RUNSTATE) {
977 		isp_uninit(isp);
978 		ISP_UNLOCK(isp);
979 		goto bad;
980 	}
981 	/*
982 	 * XXXX: Here is where we might unload the f/w module
983 	 * XXXX: (or decrease the reference count to it).
984 	 */
985 	ISP_UNLOCK(isp);
986 	return (0);
987 
988 bad:
989 
990 	if (pcs && pcs->ih) {
991 		(void) bus_teardown_intr(dev, irq, pcs->ih);
992 	}
993 
994 #if __FreeBSD_version >= 500000
995 	if (locksetup && isp) {
996 		mtx_destroy(&isp->isp_osinfo.lock);
997 	}
998 #endif
999 
1000 	if (irq) {
1001 		(void) bus_release_resource(dev, SYS_RES_IRQ, iqd, irq);
1002 	}
1003 
1004 
1005 	if (regs) {
1006 		(void) bus_release_resource(dev, rtp, rgd, regs);
1007 	}
1008 
1009 	if (pcs) {
1010 		if (pcs->pci_isp.isp_param) {
1011 #ifdef	ISP_FW_CRASH_DUMP
1012 			if (IS_FC(isp) && FCPARAM(isp)->isp_dump_data) {
1013 				free(FCPARAM(isp)->isp_dump_data, M_DEVBUF);
1014 			}
1015 #endif
1016 			free(pcs->pci_isp.isp_param, M_DEVBUF);
1017 		}
1018 	}
1019 
1020 	/*
1021 	 * XXXX: Here is where we might unload the f/w module
1022 	 * XXXX: (or decrease the reference count to it).
1023 	 */
1024 	return (ENXIO);
1025 }
1026 
1027 static void
1028 isp_pci_intr(void *arg)
1029 {
1030 	ispsoftc_t *isp = arg;
1031 	uint16_t isr, sema, mbox;
1032 
1033 	ISP_LOCK(isp);
1034 	isp->isp_intcnt++;
1035 	if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
1036 		isp->isp_intbogus++;
1037 	} else {
1038 		int iok = isp->isp_osinfo.intsok;
1039 		isp->isp_osinfo.intsok = 0;
1040 		isp_intr(isp, isr, sema, mbox);
1041 		isp->isp_osinfo.intsok = iok;
1042 	}
1043 	ISP_UNLOCK(isp);
1044 }
1045 
1046 
1047 #define	IspVirt2Off(a, x)	\
1048 	(((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
1049 	_BLK_REG_SHFT] + ((x) & 0xff))
1050 
1051 #define	BXR2(pcs, off)		\
1052 	bus_space_read_2(pcs->pci_st, pcs->pci_sh, off)
1053 #define	BXW2(pcs, off, v)	\
1054 	bus_space_write_2(pcs->pci_st, pcs->pci_sh, off, v)
1055 
1056 
1057 static __inline int
1058 isp_pci_rd_debounced(ispsoftc_t *isp, int off, uint16_t *rp)
1059 {
1060 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1061 	uint16_t val0, val1;
1062 	int i = 0;
1063 
1064 	do {
1065 		val0 = BXR2(pcs, IspVirt2Off(isp, off));
1066 		val1 = BXR2(pcs, IspVirt2Off(isp, off));
1067 	} while (val0 != val1 && ++i < 1000);
1068 	if (val0 != val1) {
1069 		return (1);
1070 	}
1071 	*rp = val0;
1072 	return (0);
1073 }
1074 
1075 static int
1076 isp_pci_rd_isr(ispsoftc_t *isp, uint16_t *isrp,
1077     uint16_t *semap, uint16_t *mbp)
1078 {
1079 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1080 	uint16_t isr, sema;
1081 
1082 	if (IS_2100(isp)) {
1083 		if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
1084 		    return (0);
1085 		}
1086 		if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
1087 		    return (0);
1088 		}
1089 	} else {
1090 		isr = BXR2(pcs, IspVirt2Off(isp, BIU_ISR));
1091 		sema = BXR2(pcs, IspVirt2Off(isp, BIU_SEMA));
1092 	}
1093 	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
1094 	isr &= INT_PENDING_MASK(isp);
1095 	sema &= BIU_SEMA_LOCK;
1096 	if (isr == 0 && sema == 0) {
1097 		return (0);
1098 	}
1099 	*isrp = isr;
1100 	if ((*semap = sema) != 0) {
1101 		if (IS_2100(isp)) {
1102 			if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
1103 				return (0);
1104 			}
1105 		} else {
1106 			*mbp = BXR2(pcs, IspVirt2Off(isp, OUTMAILBOX0));
1107 		}
1108 	}
1109 	return (1);
1110 }
1111 
1112 static int
1113 isp_pci_rd_isr_2300(ispsoftc_t *isp, uint16_t *isrp,
1114     uint16_t *semap, uint16_t *mbox0p)
1115 {
1116 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1117 	uint32_t r2hisr;
1118 
1119 	if (!(BXR2(pcs, IspVirt2Off(isp, BIU_ISR) & BIU2100_ISR_RISC_INT))) {
1120 		*isrp = 0;
1121 		return (0);
1122 	}
1123 	r2hisr = bus_space_read_4(pcs->pci_st, pcs->pci_sh,
1124 	    IspVirt2Off(pcs, BIU_R2HSTSLO));
1125 	isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1126 	if ((r2hisr & BIU_R2HST_INTR) == 0) {
1127 		*isrp = 0;
1128 		return (0);
1129 	}
1130 	switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
1131 	case ISPR2HST_ROM_MBX_OK:
1132 	case ISPR2HST_ROM_MBX_FAIL:
1133 	case ISPR2HST_MBX_OK:
1134 	case ISPR2HST_MBX_FAIL:
1135 	case ISPR2HST_ASYNC_EVENT:
1136 		*isrp = r2hisr & 0xffff;
1137 		*mbox0p = (r2hisr >> 16);
1138 		*semap = 1;
1139 		return (1);
1140 	case ISPR2HST_RIO_16:
1141 		*isrp = r2hisr & 0xffff;
1142 		*mbox0p = ASYNC_RIO1;
1143 		*semap = 1;
1144 		return (1);
1145 	case ISPR2HST_FPOST:
1146 		*isrp = r2hisr & 0xffff;
1147 		*mbox0p = ASYNC_CMD_CMPLT;
1148 		*semap = 1;
1149 		return (1);
1150 	case ISPR2HST_FPOST_CTIO:
1151 		*isrp = r2hisr & 0xffff;
1152 		*mbox0p = ASYNC_CTIO_DONE;
1153 		*semap = 1;
1154 		return (1);
1155 	case ISPR2HST_RSPQ_UPDATE:
1156 		*isrp = r2hisr & 0xffff;
1157 		*mbox0p = 0;
1158 		*semap = 0;
1159 		return (1);
1160 	default:
1161 		return (0);
1162 	}
1163 }
1164 
1165 static uint16_t
1166 isp_pci_rd_reg(ispsoftc_t *isp, int regoff)
1167 {
1168 	uint16_t rv;
1169 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1170 	int oldconf = 0;
1171 
1172 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1173 		/*
1174 		 * We will assume that someone has paused the RISC processor.
1175 		 */
1176 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1177 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1178 		    oldconf | BIU_PCI_CONF1_SXP);
1179 	}
1180 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
1181 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1182 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
1183 	}
1184 	return (rv);
1185 }
1186 
1187 static void
1188 isp_pci_wr_reg(ispsoftc_t *isp, int regoff, uint16_t val)
1189 {
1190 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1191 	int oldconf = 0;
1192 
1193 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1194 		/*
1195 		 * We will assume that someone has paused the RISC processor.
1196 		 */
1197 		oldconf = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1198 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1199 		    oldconf | BIU_PCI_CONF1_SXP);
1200 	}
1201 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
1202 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1203 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oldconf);
1204 	}
1205 }
1206 
1207 static uint16_t
1208 isp_pci_rd_reg_1080(ispsoftc_t *isp, int regoff)
1209 {
1210 	uint16_t rv, oc = 0;
1211 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1212 
1213 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1214 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1215 		uint16_t tc;
1216 		/*
1217 		 * We will assume that someone has paused the RISC processor.
1218 		 */
1219 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1220 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
1221 		if (regoff & SXP_BANK1_SELECT)
1222 			tc |= BIU_PCI1080_CONF1_SXP1;
1223 		else
1224 			tc |= BIU_PCI1080_CONF1_SXP0;
1225 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
1226 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1227 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1228 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1229 		    oc | BIU_PCI1080_CONF1_DMA);
1230 	}
1231 	rv = BXR2(pcs, IspVirt2Off(isp, regoff));
1232 	if (oc) {
1233 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
1234 	}
1235 	return (rv);
1236 }
1237 
1238 static void
1239 isp_pci_wr_reg_1080(ispsoftc_t *isp, int regoff, uint16_t val)
1240 {
1241 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
1242 	int oc = 0;
1243 
1244 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1245 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1246 		uint16_t tc;
1247 		/*
1248 		 * We will assume that someone has paused the RISC processor.
1249 		 */
1250 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1251 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
1252 		if (regoff & SXP_BANK1_SELECT)
1253 			tc |= BIU_PCI1080_CONF1_SXP1;
1254 		else
1255 			tc |= BIU_PCI1080_CONF1_SXP0;
1256 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), tc);
1257 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1258 		oc = BXR2(pcs, IspVirt2Off(isp, BIU_CONF1));
1259 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1),
1260 		    oc | BIU_PCI1080_CONF1_DMA);
1261 	}
1262 	BXW2(pcs, IspVirt2Off(isp, regoff), val);
1263 	if (oc) {
1264 		BXW2(pcs, IspVirt2Off(isp, BIU_CONF1), oc);
1265 	}
1266 }
1267 
1268 
1269 struct imush {
1270 	ispsoftc_t *isp;
1271 	int error;
1272 };
1273 
1274 static void imc(void *, bus_dma_segment_t *, int, int);
1275 
1276 static void
1277 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1278 {
1279 	struct imush *imushp = (struct imush *) arg;
1280 	if (error) {
1281 		imushp->error = error;
1282 	} else {
1283 		ispsoftc_t *isp =imushp->isp;
1284 		bus_addr_t addr = segs->ds_addr;
1285 
1286 		isp->isp_rquest_dma = addr;
1287 		addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1288 		isp->isp_result_dma = addr;
1289 		if (IS_FC(isp)) {
1290 			addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1291 			FCPARAM(isp)->isp_scdma = addr;
1292 		}
1293 	}
1294 }
1295 
1296 /*
1297  * Should be BUS_SPACE_MAXSIZE, but MAXPHYS is larger than BUS_SPACE_MAXSIZE
1298  */
1299 #define ISP_NSEGS ((MAXPHYS / PAGE_SIZE) + 1)
1300 
1301 #if __FreeBSD_version < 500000
1302 #define	isp_dma_tag_create	bus_dma_tag_create
1303 #else
1304 #define	isp_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, z)	\
1305 	bus_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, \
1306 	    busdma_lock_mutex, &Giant, z)
1307 #endif
1308 
1309 static int
1310 isp_pci_mbxdma(ispsoftc_t *isp)
1311 {
1312 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1313 	caddr_t base;
1314 	uint32_t len;
1315 	int i, error, ns;
1316 	bus_size_t slim;	/* segment size */
1317 	bus_addr_t llim;	/* low limit of unavailable dma */
1318 	bus_addr_t hlim;	/* high limit of unavailable dma */
1319 	struct imush im;
1320 
1321 	/*
1322 	 * Already been here? If so, leave...
1323 	 */
1324 	if (isp->isp_rquest) {
1325 		return (0);
1326 	}
1327 
1328 	hlim = BUS_SPACE_MAXADDR;
1329 	if (IS_ULTRA2(isp) || IS_FC(isp) || IS_1240(isp)) {
1330 		slim = (bus_size_t) (1ULL << 32);
1331 #ifdef	ISP_TARGET_MODE
1332 		/*
1333 		 * XXX: Until Fixed Soon
1334 		 */
1335 		llim = BUS_SPACE_MAXADDR_32BIT;
1336 #else
1337 		llim = BUS_SPACE_MAXADDR;
1338 #endif
1339 	} else {
1340 		llim = BUS_SPACE_MAXADDR_32BIT;
1341 		slim = (1 << 24);
1342 	}
1343 
1344 	ISP_UNLOCK(isp);
1345 	if (isp_dma_tag_create(NULL, 1, slim, llim, hlim,
1346 	    NULL, NULL, BUS_SPACE_MAXSIZE, ISP_NSEGS, slim, 0, &pcs->dmat)) {
1347 		isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
1348 		ISP_LOCK(isp);
1349 		return(1);
1350 	}
1351 
1352 
1353 	len = sizeof (XS_T **) * isp->isp_maxcmds;
1354 	isp->isp_xflist = (XS_T **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1355 	if (isp->isp_xflist == NULL) {
1356 		isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
1357 		ISP_LOCK(isp);
1358 		return (1);
1359 	}
1360 #ifdef	ISP_TARGET_MODE
1361 	len = sizeof (void **) * isp->isp_maxcmds;
1362 	isp->isp_tgtlist = (void **) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1363 	if (isp->isp_tgtlist == NULL) {
1364 		isp_prt(isp, ISP_LOGERR, "cannot alloc tgtlist array");
1365 		ISP_LOCK(isp);
1366 		return (1);
1367 	}
1368 #endif
1369 	len = sizeof (bus_dmamap_t) * isp->isp_maxcmds;
1370 	pcs->dmaps = (bus_dmamap_t *) malloc(len, M_DEVBUF,  M_WAITOK);
1371 	if (pcs->dmaps == NULL) {
1372 		isp_prt(isp, ISP_LOGERR, "can't alloc dma map storage");
1373 		free(isp->isp_xflist, M_DEVBUF);
1374 #ifdef	ISP_TARGET_MODE
1375 		free(isp->isp_tgtlist, M_DEVBUF);
1376 #endif
1377 		ISP_LOCK(isp);
1378 		return (1);
1379 	}
1380 
1381 	/*
1382 	 * Allocate and map the request, result queues, plus FC scratch area.
1383 	 */
1384 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1385 	len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1386 	if (IS_FC(isp)) {
1387 		len += ISP2100_SCRLEN;
1388 	}
1389 
1390 	ns = (len / PAGE_SIZE) + 1;
1391 	/*
1392 	 * Create a tag for the control spaces- force it to within 32 bits.
1393 	 */
1394 	if (isp_dma_tag_create(pcs->dmat, QENTRY_LEN, slim,
1395 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1396 	    NULL, NULL, len, ns, slim, 0, &isp->isp_cdmat)) {
1397 		isp_prt(isp, ISP_LOGERR,
1398 		    "cannot create a dma tag for control spaces");
1399 		free(pcs->dmaps, M_DEVBUF);
1400 		free(isp->isp_xflist, M_DEVBUF);
1401 #ifdef	ISP_TARGET_MODE
1402 		free(isp->isp_tgtlist, M_DEVBUF);
1403 #endif
1404 		ISP_LOCK(isp);
1405 		return (1);
1406 	}
1407 
1408 	if (bus_dmamem_alloc(isp->isp_cdmat, (void **)&base, BUS_DMA_NOWAIT,
1409 	    &isp->isp_cdmap) != 0) {
1410 		isp_prt(isp, ISP_LOGERR,
1411 		    "cannot allocate %d bytes of CCB memory", len);
1412 		bus_dma_tag_destroy(isp->isp_cdmat);
1413 		free(isp->isp_xflist, M_DEVBUF);
1414 #ifdef	ISP_TARGET_MODE
1415 		free(isp->isp_tgtlist, M_DEVBUF);
1416 #endif
1417 		free(pcs->dmaps, M_DEVBUF);
1418 		ISP_LOCK(isp);
1419 		return (1);
1420 	}
1421 
1422 	for (i = 0; i < isp->isp_maxcmds; i++) {
1423 		error = bus_dmamap_create(pcs->dmat, 0, &pcs->dmaps[i]);
1424 		if (error) {
1425 			isp_prt(isp, ISP_LOGERR,
1426 			    "error %d creating per-cmd DMA maps", error);
1427 			while (--i >= 0) {
1428 				bus_dmamap_destroy(pcs->dmat, pcs->dmaps[i]);
1429 			}
1430 			goto bad;
1431 		}
1432 	}
1433 
1434 	im.isp = isp;
1435 	im.error = 0;
1436 	bus_dmamap_load(isp->isp_cdmat, isp->isp_cdmap, base, len, imc, &im, 0);
1437 	if (im.error) {
1438 		isp_prt(isp, ISP_LOGERR,
1439 		    "error %d loading dma map for control areas", im.error);
1440 		goto bad;
1441 	}
1442 
1443 	isp->isp_rquest = base;
1444 	base += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1445 	isp->isp_result = base;
1446 	if (IS_FC(isp)) {
1447 		base += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1448 		FCPARAM(isp)->isp_scratch = base;
1449 	}
1450 	ISP_LOCK(isp);
1451 	return (0);
1452 
1453 bad:
1454 	bus_dmamem_free(isp->isp_cdmat, base, isp->isp_cdmap);
1455 	bus_dma_tag_destroy(isp->isp_cdmat);
1456 	free(isp->isp_xflist, M_DEVBUF);
1457 #ifdef	ISP_TARGET_MODE
1458 	free(isp->isp_tgtlist, M_DEVBUF);
1459 #endif
1460 	free(pcs->dmaps, M_DEVBUF);
1461 	ISP_LOCK(isp);
1462 	isp->isp_rquest = NULL;
1463 	return (1);
1464 }
1465 
1466 typedef struct {
1467 	ispsoftc_t *isp;
1468 	void *cmd_token;
1469 	void *rq;
1470 	uint16_t *nxtip;
1471 	uint16_t optr;
1472 	int error;
1473 } mush_t;
1474 
1475 #define	MUSHERR_NOQENTRIES	-2
1476 
1477 #ifdef	ISP_TARGET_MODE
1478 /*
1479  * We need to handle DMA for target mode differently from initiator mode.
1480  *
1481  * DMA mapping and construction and submission of CTIO Request Entries
1482  * and rendevous for completion are very tightly coupled because we start
1483  * out by knowing (per platform) how much data we have to move, but we
1484  * don't know, up front, how many DMA mapping segments will have to be used
1485  * cover that data, so we don't know how many CTIO Request Entries we
1486  * will end up using. Further, for performance reasons we may want to
1487  * (on the last CTIO for Fibre Channel), send status too (if all went well).
1488  *
1489  * The standard vector still goes through isp_pci_dmasetup, but the callback
1490  * for the DMA mapping routines comes here instead with the whole transfer
1491  * mapped and a pointer to a partially filled in already allocated request
1492  * queue entry. We finish the job.
1493  */
1494 static void tdma_mk(void *, bus_dma_segment_t *, int, int);
1495 static void tdma_mkfc(void *, bus_dma_segment_t *, int, int);
1496 
1497 #define	STATUS_WITH_DATA	1
1498 
1499 static void
1500 tdma_mk(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1501 {
1502 	mush_t *mp;
1503 	struct ccb_scsiio *csio;
1504 	ispsoftc_t *isp;
1505 	struct isp_pcisoftc *pcs;
1506 	bus_dmamap_t *dp;
1507 	ct_entry_t *cto, *qe;
1508 	uint8_t scsi_status;
1509 	uint16_t curi, nxti, handle;
1510 	uint32_t sflags;
1511 	int32_t resid;
1512 	int nth_ctio, nctios, send_status;
1513 
1514 	mp = (mush_t *) arg;
1515 	if (error) {
1516 		mp->error = error;
1517 		return;
1518 	}
1519 
1520 	isp = mp->isp;
1521 	csio = mp->cmd_token;
1522 	cto = mp->rq;
1523 	curi = isp->isp_reqidx;
1524 	qe = (ct_entry_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, curi);
1525 
1526 	cto->ct_xfrlen = 0;
1527 	cto->ct_seg_count = 0;
1528 	cto->ct_header.rqs_entry_count = 1;
1529 	MEMZERO(cto->ct_dataseg, sizeof(cto->ct_dataseg));
1530 
1531 	if (nseg == 0) {
1532 		cto->ct_header.rqs_seqno = 1;
1533 		isp_prt(isp, ISP_LOGTDEBUG1,
1534 		    "CTIO[%x] lun%d iid%d tag %x flgs %x sts %x ssts %x res %d",
1535 		    cto->ct_fwhandle, csio->ccb_h.target_lun, cto->ct_iid,
1536 		    cto->ct_tag_val, cto->ct_flags, cto->ct_status,
1537 		    cto->ct_scsi_status, cto->ct_resid);
1538 		ISP_TDQE(isp, "tdma_mk[no data]", curi, cto);
1539 		isp_put_ctio(isp, cto, qe);
1540 		return;
1541 	}
1542 
1543 	nctios = nseg / ISP_RQDSEG;
1544 	if (nseg % ISP_RQDSEG) {
1545 		nctios++;
1546 	}
1547 
1548 	/*
1549 	 * Save syshandle, and potentially any SCSI status, which we'll
1550 	 * reinsert on the last CTIO we're going to send.
1551 	 */
1552 
1553 	handle = cto->ct_syshandle;
1554 	cto->ct_syshandle = 0;
1555 	cto->ct_header.rqs_seqno = 0;
1556 	send_status = (cto->ct_flags & CT_SENDSTATUS) != 0;
1557 
1558 	if (send_status) {
1559 		sflags = cto->ct_flags & (CT_SENDSTATUS | CT_CCINCR);
1560 		cto->ct_flags &= ~(CT_SENDSTATUS | CT_CCINCR);
1561 		/*
1562 		 * Preserve residual.
1563 		 */
1564 		resid = cto->ct_resid;
1565 
1566 		/*
1567 		 * Save actual SCSI status.
1568 		 */
1569 		scsi_status = cto->ct_scsi_status;
1570 
1571 #ifndef	STATUS_WITH_DATA
1572 		sflags |= CT_NO_DATA;
1573 		/*
1574 		 * We can't do a status at the same time as a data CTIO, so
1575 		 * we need to synthesize an extra CTIO at this level.
1576 		 */
1577 		nctios++;
1578 #endif
1579 	} else {
1580 		sflags = scsi_status = resid = 0;
1581 	}
1582 
1583 	cto->ct_resid = 0;
1584 	cto->ct_scsi_status = 0;
1585 
1586 	pcs = (struct isp_pcisoftc *)isp;
1587 	dp = &pcs->dmaps[isp_handle_index(handle)];
1588 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1589 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
1590 	} else {
1591 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
1592 	}
1593 
1594 	nxti = *mp->nxtip;
1595 
1596 	for (nth_ctio = 0; nth_ctio < nctios; nth_ctio++) {
1597 		int seglim;
1598 
1599 		seglim = nseg;
1600 		if (seglim) {
1601 			int seg;
1602 
1603 			if (seglim > ISP_RQDSEG)
1604 				seglim = ISP_RQDSEG;
1605 
1606 			for (seg = 0; seg < seglim; seg++, nseg--) {
1607 				/*
1608 				 * Unlike normal initiator commands, we don't
1609 				 * do any swizzling here.
1610 				 */
1611 				cto->ct_dataseg[seg].ds_count = dm_segs->ds_len;
1612 				cto->ct_dataseg[seg].ds_base = dm_segs->ds_addr;
1613 				cto->ct_xfrlen += dm_segs->ds_len;
1614 				dm_segs++;
1615 			}
1616 			cto->ct_seg_count = seg;
1617 		} else {
1618 			/*
1619 			 * This case should only happen when we're sending an
1620 			 * extra CTIO with final status.
1621 			 */
1622 			if (send_status == 0) {
1623 				isp_prt(isp, ISP_LOGWARN,
1624 				    "tdma_mk ran out of segments");
1625 				mp->error = EINVAL;
1626 				return;
1627 			}
1628 		}
1629 
1630 		/*
1631 		 * At this point, the fields ct_lun, ct_iid, ct_tagval,
1632 		 * ct_tagtype, and ct_timeout have been carried over
1633 		 * unchanged from what our caller had set.
1634 		 *
1635 		 * The dataseg fields and the seg_count fields we just got
1636 		 * through setting. The data direction we've preserved all
1637 		 * along and only clear it if we're now sending status.
1638 		 */
1639 
1640 		if (nth_ctio == nctios - 1) {
1641 			/*
1642 			 * We're the last in a sequence of CTIOs, so mark
1643 			 * this CTIO and save the handle to the CCB such that
1644 			 * when this CTIO completes we can free dma resources
1645 			 * and do whatever else we need to do to finish the
1646 			 * rest of the command. We *don't* give this to the
1647 			 * firmware to work on- the caller will do that.
1648 			 */
1649 
1650 			cto->ct_syshandle = handle;
1651 			cto->ct_header.rqs_seqno = 1;
1652 
1653 			if (send_status) {
1654 				cto->ct_scsi_status = scsi_status;
1655 				cto->ct_flags |= sflags;
1656 				cto->ct_resid = resid;
1657 			}
1658 			if (send_status) {
1659 				isp_prt(isp, ISP_LOGTDEBUG1,
1660 				    "CTIO[%x] lun%d iid %d tag %x ct_flags %x "
1661 				    "scsi status %x resid %d",
1662 				    cto->ct_fwhandle, csio->ccb_h.target_lun,
1663 				    cto->ct_iid, cto->ct_tag_val, cto->ct_flags,
1664 				    cto->ct_scsi_status, cto->ct_resid);
1665 			} else {
1666 				isp_prt(isp, ISP_LOGTDEBUG1,
1667 				    "CTIO[%x] lun%d iid%d tag %x ct_flags 0x%x",
1668 				    cto->ct_fwhandle, csio->ccb_h.target_lun,
1669 				    cto->ct_iid, cto->ct_tag_val,
1670 				    cto->ct_flags);
1671 			}
1672 			isp_put_ctio(isp, cto, qe);
1673 			ISP_TDQE(isp, "last tdma_mk", curi, cto);
1674 			if (nctios > 1) {
1675 				MEMORYBARRIER(isp, SYNC_REQUEST,
1676 				    curi, QENTRY_LEN);
1677 			}
1678 		} else {
1679 			ct_entry_t *oqe = qe;
1680 
1681 			/*
1682 			 * Make sure syshandle fields are clean
1683 			 */
1684 			cto->ct_syshandle = 0;
1685 			cto->ct_header.rqs_seqno = 0;
1686 
1687 			isp_prt(isp, ISP_LOGTDEBUG1,
1688 			    "CTIO[%x] lun%d for ID%d ct_flags 0x%x",
1689 			    cto->ct_fwhandle, csio->ccb_h.target_lun,
1690 			    cto->ct_iid, cto->ct_flags);
1691 
1692 			/*
1693 			 * Get a new CTIO
1694 			 */
1695 			qe = (ct_entry_t *)
1696 			    ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
1697 			nxti = ISP_NXT_QENTRY(nxti, RQUEST_QUEUE_LEN(isp));
1698 			if (nxti == mp->optr) {
1699 				isp_prt(isp, ISP_LOGTDEBUG0,
1700 				    "Queue Overflow in tdma_mk");
1701 				mp->error = MUSHERR_NOQENTRIES;
1702 				return;
1703 			}
1704 
1705 			/*
1706 			 * Now that we're done with the old CTIO,
1707 			 * flush it out to the request queue.
1708 			 */
1709 			ISP_TDQE(isp, "dma_tgt_fc", curi, cto);
1710 			isp_put_ctio(isp, cto, oqe);
1711 			if (nth_ctio != 0) {
1712 				MEMORYBARRIER(isp, SYNC_REQUEST, curi,
1713 				    QENTRY_LEN);
1714 			}
1715 			curi = ISP_NXT_QENTRY(curi, RQUEST_QUEUE_LEN(isp));
1716 
1717 			/*
1718 			 * Reset some fields in the CTIO so we can reuse
1719 			 * for the next one we'll flush to the request
1720 			 * queue.
1721 			 */
1722 			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1723 			cto->ct_header.rqs_entry_count = 1;
1724 			cto->ct_header.rqs_flags = 0;
1725 			cto->ct_status = 0;
1726 			cto->ct_scsi_status = 0;
1727 			cto->ct_xfrlen = 0;
1728 			cto->ct_resid = 0;
1729 			cto->ct_seg_count = 0;
1730 			MEMZERO(cto->ct_dataseg, sizeof(cto->ct_dataseg));
1731 		}
1732 	}
1733 	*mp->nxtip = nxti;
1734 }
1735 
1736 /*
1737  * We don't have to do multiple CTIOs here. Instead, we can just do
1738  * continuation segments as needed. This greatly simplifies the code
1739  * improves performance.
1740  */
1741 
1742 static void
1743 tdma_mkfc(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1744 {
1745 	mush_t *mp;
1746 	struct ccb_scsiio *csio;
1747 	ispsoftc_t *isp;
1748 	ct2_entry_t *cto, *qe;
1749 	uint16_t curi, nxti;
1750 	int segcnt;
1751 
1752 	mp = (mush_t *) arg;
1753 	if (error) {
1754 		mp->error = error;
1755 		return;
1756 	}
1757 
1758 	isp = mp->isp;
1759 	csio = mp->cmd_token;
1760 	cto = mp->rq;
1761 
1762 	curi = isp->isp_reqidx;
1763 	qe = (ct2_entry_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, curi);
1764 
1765 	if (nseg == 0) {
1766 		if ((cto->ct_flags & CT2_FLAG_MMASK) != CT2_FLAG_MODE1) {
1767 			isp_prt(isp, ISP_LOGWARN,
1768 			    "dma2_tgt_fc, a status CTIO2 without MODE1 "
1769 			    "set (0x%x)", cto->ct_flags);
1770 			mp->error = EINVAL;
1771 			return;
1772 		}
1773 		/*
1774 		 * We preserve ct_lun, ct_iid, ct_rxid. We set the data
1775 		 * flags to NO DATA and clear relative offset flags.
1776 		 * We preserve the ct_resid and the response area.
1777 		 */
1778 		cto->ct_header.rqs_seqno = 1;
1779 		cto->ct_seg_count = 0;
1780 		cto->ct_reloff = 0;
1781 		isp_prt(isp, ISP_LOGTDEBUG1,
1782 		    "CTIO2[%x] lun %d->iid%d flgs 0x%x sts 0x%x ssts "
1783 		    "0x%x res %d", cto->ct_rxid, csio->ccb_h.target_lun,
1784 		    cto->ct_iid, cto->ct_flags, cto->ct_status,
1785 		    cto->rsp.m1.ct_scsi_status, cto->ct_resid);
1786 		isp_put_ctio2(isp, cto, qe);
1787 		ISP_TDQE(isp, "dma2_tgt_fc[no data]", curi, qe);
1788 		return;
1789 	}
1790 
1791 	if ((cto->ct_flags & CT2_FLAG_MMASK) != CT2_FLAG_MODE0) {
1792 		isp_prt(isp, ISP_LOGERR,
1793 		    "dma2_tgt_fc, a data CTIO2 without MODE0 set "
1794 		    "(0x%x)", cto->ct_flags);
1795 		mp->error = EINVAL;
1796 		return;
1797 	}
1798 
1799 
1800 	nxti = *mp->nxtip;
1801 
1802 	/*
1803 	 * Set up the CTIO2 data segments.
1804 	 */
1805 	for (segcnt = 0; cto->ct_seg_count < ISP_RQDSEG_T2 && segcnt < nseg;
1806 	    cto->ct_seg_count++, segcnt++) {
1807 		cto->rsp.m0.ct_dataseg[cto->ct_seg_count].ds_base =
1808 		    dm_segs[segcnt].ds_addr;
1809 		cto->rsp.m0.ct_dataseg[cto->ct_seg_count].ds_count =
1810 		    dm_segs[segcnt].ds_len;
1811 		cto->rsp.m0.ct_xfrlen += dm_segs[segcnt].ds_len;
1812 #if __FreeBSD_version < 500000
1813 		isp_prt(isp, ISP_LOGTDEBUG1,
1814 		    "isp_send_ctio2: ent0[%d]0x%llx:%llu",
1815 		    cto->ct_seg_count, (uint64_t)dm_segs[segcnt].ds_addr,
1816 		    (uint64_t)dm_segs[segcnt].ds_len);
1817 #else
1818 		isp_prt(isp, ISP_LOGTDEBUG1,
1819 		    "isp_send_ctio2: ent0[%d]0x%jx:%ju",
1820 		    cto->ct_seg_count, (uintmax_t)dm_segs[segcnt].ds_addr,
1821 		    (uintmax_t)dm_segs[segcnt].ds_len);
1822 #endif
1823 	}
1824 
1825 	while (segcnt < nseg) {
1826 		uint16_t curip;
1827 		int seg;
1828 		ispcontreq_t local, *crq = &local, *qep;
1829 
1830 		qep = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
1831 		curip = nxti;
1832 		nxti = ISP_NXT_QENTRY(curip, RQUEST_QUEUE_LEN(isp));
1833 		if (nxti == mp->optr) {
1834 			ISP_UNLOCK(isp);
1835 			isp_prt(isp, ISP_LOGTDEBUG0,
1836 			    "tdma_mkfc: request queue overflow");
1837 			mp->error = MUSHERR_NOQENTRIES;
1838 			return;
1839 		}
1840 		cto->ct_header.rqs_entry_count++;
1841 		MEMZERO((void *)crq, sizeof (*crq));
1842 		crq->req_header.rqs_entry_count = 1;
1843 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
1844 		for (seg = 0; segcnt < nseg && seg < ISP_CDSEG;
1845 		    segcnt++, seg++) {
1846 			crq->req_dataseg[seg].ds_base = dm_segs[segcnt].ds_addr;
1847 			crq->req_dataseg[seg].ds_count = dm_segs[segcnt].ds_len;
1848 #if __FreeBSD_version < 500000
1849 			isp_prt(isp, ISP_LOGTDEBUG1,
1850 			    "isp_send_ctio2: ent%d[%d]%llx:%llu",
1851 			    cto->ct_header.rqs_entry_count-1, seg,
1852 			    (uint64_t)dm_segs[segcnt].ds_addr,
1853 			    (uint64_t)dm_segs[segcnt].ds_len);
1854 #else
1855 			isp_prt(isp, ISP_LOGTDEBUG1,
1856 			    "isp_send_ctio2: ent%d[%d]%jx:%ju",
1857 			    cto->ct_header.rqs_entry_count-1, seg,
1858 			    (uintmax_t)dm_segs[segcnt].ds_addr,
1859 			    (uintmax_t)dm_segs[segcnt].ds_len);
1860 #endif
1861 			cto->rsp.m0.ct_xfrlen += dm_segs[segcnt].ds_len;
1862 			cto->ct_seg_count++;
1863 		}
1864 		MEMORYBARRIER(isp, SYNC_REQUEST, curip, QENTRY_LEN);
1865 		isp_put_cont_req(isp, crq, qep);
1866 		ISP_TDQE(isp, "cont entry", curi, qep);
1867 	}
1868 
1869 	/*
1870 	 * No do final twiddling for the CTIO itself.
1871 	 */
1872 	cto->ct_header.rqs_seqno = 1;
1873 	isp_prt(isp, ISP_LOGTDEBUG1,
1874 	    "CTIO2[%x] lun %d->iid%d flgs 0x%x sts 0x%x ssts 0x%x resid %d",
1875 	    cto->ct_rxid, csio->ccb_h.target_lun, (int) cto->ct_iid,
1876 	    cto->ct_flags, cto->ct_status, cto->rsp.m1.ct_scsi_status,
1877 	    cto->ct_resid);
1878 	isp_put_ctio2(isp, cto, qe);
1879 	ISP_TDQE(isp, "last dma2_tgt_fc", curi, qe);
1880 	*mp->nxtip = nxti;
1881 }
1882 #endif
1883 
1884 static void dma2_a64(void *, bus_dma_segment_t *, int, int);
1885 static void dma2(void *, bus_dma_segment_t *, int, int);
1886 
1887 static void
1888 dma2_a64(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1889 {
1890 	mush_t *mp;
1891 	ispsoftc_t *isp;
1892 	struct ccb_scsiio *csio;
1893 	struct isp_pcisoftc *pcs;
1894 	bus_dmamap_t *dp;
1895 	bus_dma_segment_t *eseg;
1896 	ispreq64_t *rq;
1897 	int seglim, datalen;
1898 	uint16_t nxti;
1899 
1900 	mp = (mush_t *) arg;
1901 	if (error) {
1902 		mp->error = error;
1903 		return;
1904 	}
1905 
1906 	if (nseg < 1) {
1907 		isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
1908 		mp->error = EFAULT;
1909 		return;
1910 	}
1911 	csio = mp->cmd_token;
1912 	isp = mp->isp;
1913 	rq = mp->rq;
1914 	pcs = (struct isp_pcisoftc *)mp->isp;
1915 	dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
1916 	nxti = *mp->nxtip;
1917 
1918 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1919 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
1920 	} else {
1921 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
1922 	}
1923 	datalen = XS_XFRLEN(csio);
1924 
1925 	/*
1926 	 * We're passed an initial partially filled in entry that
1927 	 * has most fields filled in except for data transfer
1928 	 * related values.
1929 	 *
1930 	 * Our job is to fill in the initial request queue entry and
1931 	 * then to start allocating and filling in continuation entries
1932 	 * until we've covered the entire transfer.
1933 	 */
1934 
1935 	if (IS_FC(isp)) {
1936 		rq->req_header.rqs_entry_type = RQSTYPE_T3RQS;
1937 		seglim = ISP_RQDSEG_T3;
1938 		((ispreqt3_t *)rq)->req_totalcnt = datalen;
1939 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1940 			((ispreqt3_t *)rq)->req_flags |= REQFLAG_DATA_IN;
1941 		} else {
1942 			((ispreqt3_t *)rq)->req_flags |= REQFLAG_DATA_OUT;
1943 		}
1944 	} else {
1945 		rq->req_header.rqs_entry_type = RQSTYPE_A64;
1946 		if (csio->cdb_len > 12) {
1947 			seglim = 0;
1948 		} else {
1949 			seglim = ISP_RQDSEG_A64;
1950 		}
1951 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1952 			rq->req_flags |= REQFLAG_DATA_IN;
1953 		} else {
1954 			rq->req_flags |= REQFLAG_DATA_OUT;
1955 		}
1956 	}
1957 
1958 	eseg = dm_segs + nseg;
1959 
1960 	while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) {
1961 		if (IS_FC(isp)) {
1962 			ispreqt3_t *rq3 = (ispreqt3_t *)rq;
1963 			rq3->req_dataseg[rq3->req_seg_count].ds_base =
1964 			    DMA_LO32(dm_segs->ds_addr);
1965 			rq3->req_dataseg[rq3->req_seg_count].ds_basehi =
1966 			    DMA_HI32(dm_segs->ds_addr);
1967 			rq3->req_dataseg[rq3->req_seg_count].ds_count =
1968 			    dm_segs->ds_len;
1969 		} else {
1970 			rq->req_dataseg[rq->req_seg_count].ds_base =
1971 			    DMA_LO32(dm_segs->ds_addr);
1972 			rq->req_dataseg[rq->req_seg_count].ds_basehi =
1973 			    DMA_HI32(dm_segs->ds_addr);
1974 			rq->req_dataseg[rq->req_seg_count].ds_count =
1975 			    dm_segs->ds_len;
1976 		}
1977 		datalen -= dm_segs->ds_len;
1978 		rq->req_seg_count++;
1979 		dm_segs++;
1980 	}
1981 
1982 	while (datalen > 0 && dm_segs != eseg) {
1983 		uint16_t onxti;
1984 		ispcontreq64_t local, *crq = &local, *cqe;
1985 
1986 		cqe = (ispcontreq64_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
1987 		onxti = nxti;
1988 		nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
1989 		if (nxti == mp->optr) {
1990 			isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
1991 			mp->error = MUSHERR_NOQENTRIES;
1992 			return;
1993 		}
1994 		rq->req_header.rqs_entry_count++;
1995 		MEMZERO((void *)crq, sizeof (*crq));
1996 		crq->req_header.rqs_entry_count = 1;
1997 		crq->req_header.rqs_entry_type = RQSTYPE_A64_CONT;
1998 
1999 		seglim = 0;
2000 		while (datalen > 0 && seglim < ISP_CDSEG64 && dm_segs != eseg) {
2001 			crq->req_dataseg[seglim].ds_base =
2002 			    DMA_LO32(dm_segs->ds_addr);
2003 			crq->req_dataseg[seglim].ds_basehi =
2004 			    DMA_HI32(dm_segs->ds_addr);
2005 			crq->req_dataseg[seglim].ds_count =
2006 			    dm_segs->ds_len;
2007 			rq->req_seg_count++;
2008 			dm_segs++;
2009 			seglim++;
2010 			datalen -= dm_segs->ds_len;
2011 		}
2012 		isp_put_cont64_req(isp, crq, cqe);
2013 		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
2014 	}
2015 	*mp->nxtip = nxti;
2016 }
2017 
2018 static void
2019 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
2020 {
2021 	mush_t *mp;
2022 	ispsoftc_t *isp;
2023 	struct ccb_scsiio *csio;
2024 	struct isp_pcisoftc *pcs;
2025 	bus_dmamap_t *dp;
2026 	bus_dma_segment_t *eseg;
2027 	ispreq_t *rq;
2028 	int seglim, datalen;
2029 	uint16_t nxti;
2030 
2031 	mp = (mush_t *) arg;
2032 	if (error) {
2033 		mp->error = error;
2034 		return;
2035 	}
2036 
2037 	if (nseg < 1) {
2038 		isp_prt(mp->isp, ISP_LOGERR, "bad segment count (%d)", nseg);
2039 		mp->error = EFAULT;
2040 		return;
2041 	}
2042 	csio = mp->cmd_token;
2043 	isp = mp->isp;
2044 	rq = mp->rq;
2045 	pcs = (struct isp_pcisoftc *)mp->isp;
2046 	dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
2047 	nxti = *mp->nxtip;
2048 
2049 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2050 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREREAD);
2051 	} else {
2052 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_PREWRITE);
2053 	}
2054 
2055 	datalen = XS_XFRLEN(csio);
2056 
2057 	/*
2058 	 * We're passed an initial partially filled in entry that
2059 	 * has most fields filled in except for data transfer
2060 	 * related values.
2061 	 *
2062 	 * Our job is to fill in the initial request queue entry and
2063 	 * then to start allocating and filling in continuation entries
2064 	 * until we've covered the entire transfer.
2065 	 */
2066 
2067 	if (IS_FC(isp)) {
2068 		seglim = ISP_RQDSEG_T2;
2069 		((ispreqt2_t *)rq)->req_totalcnt = datalen;
2070 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2071 			((ispreqt2_t *)rq)->req_flags |= REQFLAG_DATA_IN;
2072 		} else {
2073 			((ispreqt2_t *)rq)->req_flags |= REQFLAG_DATA_OUT;
2074 		}
2075 	} else {
2076 		if (csio->cdb_len > 12) {
2077 			seglim = 0;
2078 		} else {
2079 			seglim = ISP_RQDSEG;
2080 		}
2081 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2082 			rq->req_flags |= REQFLAG_DATA_IN;
2083 		} else {
2084 			rq->req_flags |= REQFLAG_DATA_OUT;
2085 		}
2086 	}
2087 
2088 	eseg = dm_segs + nseg;
2089 
2090 	while (datalen != 0 && rq->req_seg_count < seglim && dm_segs != eseg) {
2091 		if (IS_FC(isp)) {
2092 			ispreqt2_t *rq2 = (ispreqt2_t *)rq;
2093 			rq2->req_dataseg[rq2->req_seg_count].ds_base =
2094 			    DMA_LO32(dm_segs->ds_addr);
2095 			rq2->req_dataseg[rq2->req_seg_count].ds_count =
2096 			    dm_segs->ds_len;
2097 		} else {
2098 			rq->req_dataseg[rq->req_seg_count].ds_base =
2099 				DMA_LO32(dm_segs->ds_addr);
2100 			rq->req_dataseg[rq->req_seg_count].ds_count =
2101 				dm_segs->ds_len;
2102 		}
2103 		datalen -= dm_segs->ds_len;
2104 		rq->req_seg_count++;
2105 		dm_segs++;
2106 	}
2107 
2108 	while (datalen > 0 && dm_segs != eseg) {
2109 		uint16_t onxti;
2110 		ispcontreq_t local, *crq = &local, *cqe;
2111 
2112 		cqe = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, nxti);
2113 		onxti = nxti;
2114 		nxti = ISP_NXT_QENTRY(onxti, RQUEST_QUEUE_LEN(isp));
2115 		if (nxti == mp->optr) {
2116 			isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow++");
2117 			mp->error = MUSHERR_NOQENTRIES;
2118 			return;
2119 		}
2120 		rq->req_header.rqs_entry_count++;
2121 		MEMZERO((void *)crq, sizeof (*crq));
2122 		crq->req_header.rqs_entry_count = 1;
2123 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
2124 
2125 		seglim = 0;
2126 		while (datalen > 0 && seglim < ISP_CDSEG && dm_segs != eseg) {
2127 			crq->req_dataseg[seglim].ds_base =
2128 			    DMA_LO32(dm_segs->ds_addr);
2129 			crq->req_dataseg[seglim].ds_count =
2130 			    dm_segs->ds_len;
2131 			rq->req_seg_count++;
2132 			dm_segs++;
2133 			seglim++;
2134 			datalen -= dm_segs->ds_len;
2135 		}
2136 		isp_put_cont_req(isp, crq, cqe);
2137 		MEMORYBARRIER(isp, SYNC_REQUEST, onxti, QENTRY_LEN);
2138 	}
2139 	*mp->nxtip = nxti;
2140 }
2141 
2142 /*
2143  * We enter with ISP_LOCK held
2144  */
2145 static int
2146 isp_pci_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, ispreq_t *rq,
2147 	uint16_t *nxtip, uint16_t optr)
2148 {
2149 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
2150 	ispreq_t *qep;
2151 	bus_dmamap_t *dp = NULL;
2152 	mush_t mush, *mp;
2153 	void (*eptr)(void *, bus_dma_segment_t *, int, int);
2154 
2155 	qep = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, isp->isp_reqidx);
2156 #ifdef	ISP_TARGET_MODE
2157 	if (csio->ccb_h.func_code == XPT_CONT_TARGET_IO) {
2158 		if (IS_FC(isp)) {
2159 			eptr = tdma_mkfc;
2160 		} else {
2161 			eptr = tdma_mk;
2162 		}
2163 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
2164 		    (csio->dxfer_len == 0)) {
2165 			mp = &mush;
2166 			mp->isp = isp;
2167 			mp->cmd_token = csio;
2168 			mp->rq = rq;	/* really a ct_entry_t or ct2_entry_t */
2169 			mp->nxtip = nxtip;
2170 			mp->optr = optr;
2171 			mp->error = 0;
2172 			ISPLOCK_2_CAMLOCK(isp);
2173 			(*eptr)(mp, NULL, 0, 0);
2174 			CAMLOCK_2_ISPLOCK(isp);
2175 			goto mbxsync;
2176 		}
2177 	} else
2178 #endif
2179 	if (sizeof (bus_addr_t) > 4) {
2180 		eptr = dma2_a64;
2181 	} else {
2182 		eptr = dma2;
2183 	}
2184 
2185 
2186 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE ||
2187 	    (csio->dxfer_len == 0)) {
2188 		rq->req_seg_count = 1;
2189 		goto mbxsync;
2190 	}
2191 
2192 	/*
2193 	 * Do a virtual grapevine step to collect info for
2194 	 * the callback dma allocation that we have to use...
2195 	 */
2196 	mp = &mush;
2197 	mp->isp = isp;
2198 	mp->cmd_token = csio;
2199 	mp->rq = rq;
2200 	mp->nxtip = nxtip;
2201 	mp->optr = optr;
2202 	mp->error = 0;
2203 
2204 	ISPLOCK_2_CAMLOCK(isp);
2205 	if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
2206 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
2207 			int error, s;
2208 			dp = &pcs->dmaps[isp_handle_index(rq->req_handle)];
2209 			s = splsoftvm();
2210 			error = bus_dmamap_load(pcs->dmat, *dp,
2211 			    csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
2212 			if (error == EINPROGRESS) {
2213 				bus_dmamap_unload(pcs->dmat, *dp);
2214 				mp->error = EINVAL;
2215 				isp_prt(isp, ISP_LOGERR,
2216 				    "deferred dma allocation not supported");
2217 			} else if (error && mp->error == 0) {
2218 #ifdef	DIAGNOSTIC
2219 				isp_prt(isp, ISP_LOGERR,
2220 				    "error %d in dma mapping code", error);
2221 #endif
2222 				mp->error = error;
2223 			}
2224 			splx(s);
2225 		} else {
2226 			/* Pointer to physical buffer */
2227 			struct bus_dma_segment seg;
2228 			seg.ds_addr = (bus_addr_t)(vm_offset_t)csio->data_ptr;
2229 			seg.ds_len = csio->dxfer_len;
2230 			(*eptr)(mp, &seg, 1, 0);
2231 		}
2232 	} else {
2233 		struct bus_dma_segment *segs;
2234 
2235 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
2236 			isp_prt(isp, ISP_LOGERR,
2237 			    "Physical segment pointers unsupported");
2238 			mp->error = EINVAL;
2239 		} else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
2240 			isp_prt(isp, ISP_LOGERR,
2241 			    "Virtual segment addresses unsupported");
2242 			mp->error = EINVAL;
2243 		} else {
2244 			/* Just use the segments provided */
2245 			segs = (struct bus_dma_segment *) csio->data_ptr;
2246 			(*eptr)(mp, segs, csio->sglist_cnt, 0);
2247 		}
2248 	}
2249 	CAMLOCK_2_ISPLOCK(isp);
2250 	if (mp->error) {
2251 		int retval = CMD_COMPLETE;
2252 		if (mp->error == MUSHERR_NOQENTRIES) {
2253 			retval = CMD_EAGAIN;
2254 		} else if (mp->error == EFBIG) {
2255 			XS_SETERR(csio, CAM_REQ_TOO_BIG);
2256 		} else if (mp->error == EINVAL) {
2257 			XS_SETERR(csio, CAM_REQ_INVALID);
2258 		} else {
2259 			XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
2260 		}
2261 		return (retval);
2262 	}
2263 mbxsync:
2264 	switch (rq->req_header.rqs_entry_type) {
2265 	case RQSTYPE_REQUEST:
2266 		isp_put_request(isp, rq, qep);
2267 		break;
2268 	case RQSTYPE_CMDONLY:
2269 		isp_put_extended_request(isp, (ispextreq_t *)rq,
2270 		    (ispextreq_t *)qep);
2271 		break;
2272 	case RQSTYPE_T2RQS:
2273 		isp_put_request_t2(isp, (ispreqt2_t *) rq, (ispreqt2_t *) qep);
2274 		break;
2275 	case RQSTYPE_A64:
2276 	case RQSTYPE_T3RQS:
2277 		isp_put_request_t3(isp, (ispreqt3_t *) rq, (ispreqt3_t *) qep);
2278 		break;
2279 	}
2280 	return (CMD_QUEUED);
2281 }
2282 
2283 static void
2284 isp_pci_dmateardown(ispsoftc_t *isp, XS_T *xs, uint16_t handle)
2285 {
2286 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
2287 	bus_dmamap_t *dp = &pcs->dmaps[isp_handle_index(handle)];
2288 	if ((xs->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2289 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_POSTREAD);
2290 	} else {
2291 		bus_dmamap_sync(pcs->dmat, *dp, BUS_DMASYNC_POSTWRITE);
2292 	}
2293 	bus_dmamap_unload(pcs->dmat, *dp);
2294 }
2295 
2296 
2297 static void
2298 isp_pci_reset1(ispsoftc_t *isp)
2299 {
2300 	/* Make sure the BIOS is disabled */
2301 	isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
2302 	/* and enable interrupts */
2303 	ENABLE_INTS(isp);
2304 }
2305 
2306 static void
2307 isp_pci_dumpregs(ispsoftc_t *isp, const char *msg)
2308 {
2309 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
2310 	if (msg)
2311 		printf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
2312 	else
2313 		printf("%s:\n", device_get_nameunit(isp->isp_dev));
2314 	if (IS_SCSI(isp))
2315 		printf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
2316 	else
2317 		printf("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
2318 	printf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
2319 	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
2320 	printf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
2321 
2322 
2323 	if (IS_SCSI(isp)) {
2324 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
2325 		printf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
2326 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
2327 			ISP_READ(isp, CDMA_FIFO_STS));
2328 		printf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
2329 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
2330 			ISP_READ(isp, DDMA_FIFO_STS));
2331 		printf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
2332 			ISP_READ(isp, SXP_INTERRUPT),
2333 			ISP_READ(isp, SXP_GROSS_ERR),
2334 			ISP_READ(isp, SXP_PINS_CTRL));
2335 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
2336 	}
2337 	printf("    mbox regs: %x %x %x %x %x\n",
2338 	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
2339 	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
2340 	    ISP_READ(isp, OUTMAILBOX4));
2341 	printf("    PCI Status Command/Status=%x\n",
2342 	    pci_read_config(pcs->pci_dev, PCIR_COMMAND, 1));
2343 }
2344