xref: /dragonfly/sys/dev/disk/isp/isp_pci.c (revision e65bc1c3)
1 /*-
2  * Copyright (c) 1997-2008 by Matthew Jacob
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice immediately at the beginning of the file, without modification,
10  *    this list of conditions, and the following disclaimer.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/isp/isp_pci.c,v 1.159 2011/11/16 02:52:24 mjacob Exp $
27  */
28 /*
29  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
30  * FreeBSD Version.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/linker.h>
38 #include <sys/firmware.h>
39 #include <sys/bus.h>
40 #include <sys/stdint.h>
41 #include <bus/pci/pcireg.h>
42 #include <bus/pci/pcivar.h>
43 #include <sys/rman.h>
44 #include <sys/malloc.h>
45 #include <sys/uio.h>
46 
47 #include <dev/disk/isp/isp_freebsd.h>
48 
49 static uint32_t isp_pci_rd_reg(ispsoftc_t *, int);
50 static void isp_pci_wr_reg(ispsoftc_t *, int, uint32_t);
51 static uint32_t isp_pci_rd_reg_1080(ispsoftc_t *, int);
52 static void isp_pci_wr_reg_1080(ispsoftc_t *, int, uint32_t);
53 static uint32_t isp_pci_rd_reg_2400(ispsoftc_t *, int);
54 static void isp_pci_wr_reg_2400(ispsoftc_t *, int, uint32_t);
55 static int isp_pci_rd_isr(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
56 static int isp_pci_rd_isr_2300(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
57 static int isp_pci_rd_isr_2400(ispsoftc_t *, uint32_t *, uint16_t *, uint16_t *);
58 static int isp_pci_mbxdma(ispsoftc_t *);
59 static int isp_pci_dmasetup(ispsoftc_t *, XS_T *, void *);
60 
61 
62 static void isp_pci_reset0(ispsoftc_t *);
63 static void isp_pci_reset1(ispsoftc_t *);
64 static void isp_pci_dumpregs(ispsoftc_t *, const char *);
65 
66 static struct ispmdvec mdvec = {
67 	isp_pci_rd_isr,
68 	isp_pci_rd_reg,
69 	isp_pci_wr_reg,
70 	isp_pci_mbxdma,
71 	isp_pci_dmasetup,
72 	isp_common_dmateardown,
73 	isp_pci_reset0,
74 	isp_pci_reset1,
75 	isp_pci_dumpregs,
76 	NULL,
77 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
78 };
79 
80 static struct ispmdvec mdvec_1080 = {
81 	isp_pci_rd_isr,
82 	isp_pci_rd_reg_1080,
83 	isp_pci_wr_reg_1080,
84 	isp_pci_mbxdma,
85 	isp_pci_dmasetup,
86 	isp_common_dmateardown,
87 	isp_pci_reset0,
88 	isp_pci_reset1,
89 	isp_pci_dumpregs,
90 	NULL,
91 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
92 };
93 
94 static struct ispmdvec mdvec_12160 = {
95 	isp_pci_rd_isr,
96 	isp_pci_rd_reg_1080,
97 	isp_pci_wr_reg_1080,
98 	isp_pci_mbxdma,
99 	isp_pci_dmasetup,
100 	isp_common_dmateardown,
101 	isp_pci_reset0,
102 	isp_pci_reset1,
103 	isp_pci_dumpregs,
104 	NULL,
105 	BIU_BURST_ENABLE|BIU_PCI_CONF1_FIFO_64
106 };
107 
108 static struct ispmdvec mdvec_2100 = {
109 	isp_pci_rd_isr,
110 	isp_pci_rd_reg,
111 	isp_pci_wr_reg,
112 	isp_pci_mbxdma,
113 	isp_pci_dmasetup,
114 	isp_common_dmateardown,
115 	isp_pci_reset0,
116 	isp_pci_reset1,
117 	isp_pci_dumpregs
118 };
119 
120 static struct ispmdvec mdvec_2200 = {
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_common_dmateardown,
127 	isp_pci_reset0,
128 	isp_pci_reset1,
129 	isp_pci_dumpregs
130 };
131 
132 static struct ispmdvec mdvec_2300 = {
133 	isp_pci_rd_isr_2300,
134 	isp_pci_rd_reg,
135 	isp_pci_wr_reg,
136 	isp_pci_mbxdma,
137 	isp_pci_dmasetup,
138 	isp_common_dmateardown,
139 	isp_pci_reset0,
140 	isp_pci_reset1,
141 	isp_pci_dumpregs
142 };
143 
144 static struct ispmdvec mdvec_2400 = {
145 	isp_pci_rd_isr_2400,
146 	isp_pci_rd_reg_2400,
147 	isp_pci_wr_reg_2400,
148 	isp_pci_mbxdma,
149 	isp_pci_dmasetup,
150 	isp_common_dmateardown,
151 	isp_pci_reset0,
152 	isp_pci_reset1,
153 	NULL
154 };
155 
156 static struct ispmdvec mdvec_2500 = {
157 	isp_pci_rd_isr_2400,
158 	isp_pci_rd_reg_2400,
159 	isp_pci_wr_reg_2400,
160 	isp_pci_mbxdma,
161 	isp_pci_dmasetup,
162 	isp_common_dmateardown,
163 	isp_pci_reset0,
164 	isp_pci_reset1,
165 	NULL
166 };
167 
168 #ifndef	PCIM_CMD_INVEN
169 #define	PCIM_CMD_INVEN			0x10
170 #endif
171 #ifndef	PCIM_CMD_BUSMASTEREN
172 #define	PCIM_CMD_BUSMASTEREN		0x0004
173 #endif
174 #ifndef	PCIM_CMD_PERRESPEN
175 #define	PCIM_CMD_PERRESPEN		0x0040
176 #endif
177 #ifndef	PCIM_CMD_SEREN
178 #define	PCIM_CMD_SEREN			0x0100
179 #endif
180 #ifndef	PCIM_CMD_INTX_DISABLE
181 #define	PCIM_CMD_INTX_DISABLE		0x0400
182 #endif
183 
184 #ifndef	PCIR_COMMAND
185 #define	PCIR_COMMAND			0x04
186 #endif
187 
188 #ifndef	PCIR_CACHELNSZ
189 #define	PCIR_CACHELNSZ			0x0c
190 #endif
191 
192 #ifndef	PCIR_LATTIMER
193 #define	PCIR_LATTIMER			0x0d
194 #endif
195 
196 #ifndef	PCIR_ROMADDR
197 #define	PCIR_ROMADDR			0x30
198 #endif
199 
200 #ifndef	PCI_VENDOR_QLOGIC
201 #define	PCI_VENDOR_QLOGIC		0x1077
202 #endif
203 
204 #ifndef	PCI_PRODUCT_QLOGIC_ISP1020
205 #define	PCI_PRODUCT_QLOGIC_ISP1020	0x1020
206 #endif
207 
208 #ifndef	PCI_PRODUCT_QLOGIC_ISP1080
209 #define	PCI_PRODUCT_QLOGIC_ISP1080	0x1080
210 #endif
211 
212 #ifndef	PCI_PRODUCT_QLOGIC_ISP10160
213 #define	PCI_PRODUCT_QLOGIC_ISP10160	0x1016
214 #endif
215 
216 #ifndef	PCI_PRODUCT_QLOGIC_ISP12160
217 #define	PCI_PRODUCT_QLOGIC_ISP12160	0x1216
218 #endif
219 
220 #ifndef	PCI_PRODUCT_QLOGIC_ISP1240
221 #define	PCI_PRODUCT_QLOGIC_ISP1240	0x1240
222 #endif
223 
224 #ifndef	PCI_PRODUCT_QLOGIC_ISP1280
225 #define	PCI_PRODUCT_QLOGIC_ISP1280	0x1280
226 #endif
227 
228 #ifndef	PCI_PRODUCT_QLOGIC_ISP2100
229 #define	PCI_PRODUCT_QLOGIC_ISP2100	0x2100
230 #endif
231 
232 #ifndef	PCI_PRODUCT_QLOGIC_ISP2200
233 #define	PCI_PRODUCT_QLOGIC_ISP2200	0x2200
234 #endif
235 
236 #ifndef	PCI_PRODUCT_QLOGIC_ISP2300
237 #define	PCI_PRODUCT_QLOGIC_ISP2300	0x2300
238 #endif
239 
240 #ifndef	PCI_PRODUCT_QLOGIC_ISP2312
241 #define	PCI_PRODUCT_QLOGIC_ISP2312	0x2312
242 #endif
243 
244 #ifndef	PCI_PRODUCT_QLOGIC_ISP2322
245 #define	PCI_PRODUCT_QLOGIC_ISP2322	0x2322
246 #endif
247 
248 #ifndef	PCI_PRODUCT_QLOGIC_ISP2422
249 #define	PCI_PRODUCT_QLOGIC_ISP2422	0x2422
250 #endif
251 
252 #ifndef	PCI_PRODUCT_QLOGIC_ISP2432
253 #define	PCI_PRODUCT_QLOGIC_ISP2432	0x2432
254 #endif
255 
256 #ifndef	PCI_PRODUCT_QLOGIC_ISP2532
257 #define	PCI_PRODUCT_QLOGIC_ISP2532	0x2532
258 #endif
259 
260 #ifndef	PCI_PRODUCT_QLOGIC_ISP6312
261 #define	PCI_PRODUCT_QLOGIC_ISP6312	0x6312
262 #endif
263 
264 #ifndef	PCI_PRODUCT_QLOGIC_ISP6322
265 #define	PCI_PRODUCT_QLOGIC_ISP6322	0x6322
266 #endif
267 
268 #ifndef        PCI_PRODUCT_QLOGIC_ISP5432
269 #define        PCI_PRODUCT_QLOGIC_ISP5432      0x5432
270 #endif
271 
272 #define        PCI_QLOGIC_ISP5432      \
273        ((PCI_PRODUCT_QLOGIC_ISP5432 << 16) | PCI_VENDOR_QLOGIC)
274 
275 #define	PCI_QLOGIC_ISP1020	\
276 	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
277 
278 #define	PCI_QLOGIC_ISP1080	\
279 	((PCI_PRODUCT_QLOGIC_ISP1080 << 16) | PCI_VENDOR_QLOGIC)
280 
281 #define	PCI_QLOGIC_ISP10160	\
282 	((PCI_PRODUCT_QLOGIC_ISP10160 << 16) | PCI_VENDOR_QLOGIC)
283 
284 #define	PCI_QLOGIC_ISP12160	\
285 	((PCI_PRODUCT_QLOGIC_ISP12160 << 16) | PCI_VENDOR_QLOGIC)
286 
287 #define	PCI_QLOGIC_ISP1240	\
288 	((PCI_PRODUCT_QLOGIC_ISP1240 << 16) | PCI_VENDOR_QLOGIC)
289 
290 #define	PCI_QLOGIC_ISP1280	\
291 	((PCI_PRODUCT_QLOGIC_ISP1280 << 16) | PCI_VENDOR_QLOGIC)
292 
293 #define	PCI_QLOGIC_ISP2100	\
294 	((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
295 
296 #define	PCI_QLOGIC_ISP2200	\
297 	((PCI_PRODUCT_QLOGIC_ISP2200 << 16) | PCI_VENDOR_QLOGIC)
298 
299 #define	PCI_QLOGIC_ISP2300	\
300 	((PCI_PRODUCT_QLOGIC_ISP2300 << 16) | PCI_VENDOR_QLOGIC)
301 
302 #define	PCI_QLOGIC_ISP2312	\
303 	((PCI_PRODUCT_QLOGIC_ISP2312 << 16) | PCI_VENDOR_QLOGIC)
304 
305 #define	PCI_QLOGIC_ISP2322	\
306 	((PCI_PRODUCT_QLOGIC_ISP2322 << 16) | PCI_VENDOR_QLOGIC)
307 
308 #define	PCI_QLOGIC_ISP2422	\
309 	((PCI_PRODUCT_QLOGIC_ISP2422 << 16) | PCI_VENDOR_QLOGIC)
310 
311 #define	PCI_QLOGIC_ISP2432	\
312 	((PCI_PRODUCT_QLOGIC_ISP2432 << 16) | PCI_VENDOR_QLOGIC)
313 
314 #define	PCI_QLOGIC_ISP2532	\
315 	((PCI_PRODUCT_QLOGIC_ISP2532 << 16) | PCI_VENDOR_QLOGIC)
316 
317 #define	PCI_QLOGIC_ISP6312	\
318 	((PCI_PRODUCT_QLOGIC_ISP6312 << 16) | PCI_VENDOR_QLOGIC)
319 
320 #define	PCI_QLOGIC_ISP6322	\
321 	((PCI_PRODUCT_QLOGIC_ISP6322 << 16) | PCI_VENDOR_QLOGIC)
322 
323 /*
324  * Odd case for some AMI raid cards... We need to *not* attach to this.
325  */
326 #define	AMI_RAID_SUBVENDOR_ID	0x101e
327 
328 #define	IO_MAP_REG	0x10
329 #define	MEM_MAP_REG	0x14
330 
331 #define	PCI_DFLT_LTNCY	0x40
332 #define	PCI_DFLT_LNSZ	0x10
333 
334 static int isp_pci_probe (device_t);
335 static int isp_pci_attach (device_t);
336 static int isp_pci_detach (device_t);
337 
338 
339 #define	ISP_PCD(isp)	((struct isp_pcisoftc *)isp)->pci_dev
340 struct isp_pcisoftc {
341 	ispsoftc_t			pci_isp;
342 	device_t			pci_dev;
343 	struct resource *		regs;
344 	void *				irq;
345 	int				irq_type;
346 	int				iqd;
347 	int				rtp;
348 	int				rgd;
349 	void *				ih;
350 	int16_t				pci_poff[_NREG_BLKS];
351 	bus_dma_tag_t			dmat;
352 };
353 
354 static int isp_msi_enable = 1;
355 TUNABLE_INT("hw.isp.msi.enable", &isp_msi_enable);
356 
357 static device_method_t isp_pci_methods[] = {
358 	/* Device interface */
359 	DEVMETHOD(device_probe,		isp_pci_probe),
360 	DEVMETHOD(device_attach,	isp_pci_attach),
361 	DEVMETHOD(device_detach,	isp_pci_detach),
362 	DEVMETHOD_END
363 };
364 
365 static driver_t isp_pci_driver = {
366 	"isp", isp_pci_methods, sizeof (struct isp_pcisoftc)
367 };
368 static devclass_t isp_devclass;
369 DRIVER_MODULE(isp, pci, isp_pci_driver, isp_devclass, NULL, NULL);
370 
371 static int
372 isp_pci_probe(device_t dev)
373 {
374 	switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
375 	case PCI_QLOGIC_ISP1020:
376 		device_set_desc(dev, "Qlogic ISP 1020/1040 PCI SCSI Adapter");
377 		break;
378 	case PCI_QLOGIC_ISP1080:
379 		device_set_desc(dev, "Qlogic ISP 1080 PCI SCSI Adapter");
380 		break;
381 	case PCI_QLOGIC_ISP1240:
382 		device_set_desc(dev, "Qlogic ISP 1240 PCI SCSI Adapter");
383 		break;
384 	case PCI_QLOGIC_ISP1280:
385 		device_set_desc(dev, "Qlogic ISP 1280 PCI SCSI Adapter");
386 		break;
387 	case PCI_QLOGIC_ISP10160:
388 		device_set_desc(dev, "Qlogic ISP 10160 PCI SCSI Adapter");
389 		break;
390 	case PCI_QLOGIC_ISP12160:
391 		if (pci_get_subvendor(dev) == AMI_RAID_SUBVENDOR_ID) {
392 			return (ENXIO);
393 		}
394 		device_set_desc(dev, "Qlogic ISP 12160 PCI SCSI Adapter");
395 		break;
396 	case PCI_QLOGIC_ISP2100:
397 		device_set_desc(dev, "Qlogic ISP 2100 PCI FC-AL Adapter");
398 		break;
399 	case PCI_QLOGIC_ISP2200:
400 		device_set_desc(dev, "Qlogic ISP 2200 PCI FC-AL Adapter");
401 		break;
402 	case PCI_QLOGIC_ISP2300:
403 		device_set_desc(dev, "Qlogic ISP 2300 PCI FC-AL Adapter");
404 		break;
405 	case PCI_QLOGIC_ISP2312:
406 		device_set_desc(dev, "Qlogic ISP 2312 PCI FC-AL Adapter");
407 		break;
408 	case PCI_QLOGIC_ISP2322:
409 		device_set_desc(dev, "Qlogic ISP 2322 PCI FC-AL Adapter");
410 		break;
411 	case PCI_QLOGIC_ISP2422:
412 		device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
413 		break;
414 	case PCI_QLOGIC_ISP2432:
415 		device_set_desc(dev, "Qlogic ISP 2432 PCI FC-AL Adapter");
416 		break;
417 	case PCI_QLOGIC_ISP2532:
418 		device_set_desc(dev, "Qlogic ISP 2532 PCI FC-AL Adapter");
419 		break;
420 	case PCI_QLOGIC_ISP5432:
421 		device_set_desc(dev, "Qlogic ISP 5432 PCI FC-AL Adapter");
422 		break;
423 	case PCI_QLOGIC_ISP6312:
424 		device_set_desc(dev, "Qlogic ISP 6312 PCI FC-AL Adapter");
425 		break;
426 	case PCI_QLOGIC_ISP6322:
427 		device_set_desc(dev, "Qlogic ISP 6322 PCI FC-AL Adapter");
428 		break;
429 	default:
430 		return (ENXIO);
431 	}
432 	if (isp_announced == 0 && bootverbose) {
433 		kprintf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
434 		    "Core Version %d.%d\n",
435 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
436 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
437 		isp_announced++;
438 	}
439 	/*
440 	 * XXXX: Here is where we might load the f/w module
441 	 * XXXX: (or increase a reference count to it).
442 	 */
443 	return (BUS_PROBE_DEFAULT);
444 }
445 
446 static void
447 isp_get_generic_options(device_t dev, ispsoftc_t *isp, int *nvp)
448 {
449 	int tval;
450 
451 	/*
452 	 * Figure out if we're supposed to skip this one.
453 	 */
454 	tval = 0;
455 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "disable", &tval) == 0 && tval) {
456 		device_printf(dev, "disabled at user request\n");
457 		isp->isp_osinfo.disabled = 1;
458 		return;
459 	}
460 
461 	tval = 0;
462 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fwload_disable", &tval) == 0 && tval != 0) {
463 		isp->isp_confopts |= ISP_CFG_NORELOAD;
464 	}
465 	tval = 0;
466 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "ignore_nvram", &tval) == 0 && tval != 0) {
467 		isp->isp_confopts |= ISP_CFG_NONVRAM;
468 	}
469 	tval = 0;
470 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "debug", &tval);
471 	if (tval) {
472 		isp->isp_dblev = tval;
473 	} else {
474 		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
475 	}
476 	if (bootverbose) {
477 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
478 	}
479 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "vports", &tval);
480 	if (tval > 0 && tval < 127) {
481 		*nvp =  tval;
482 	} else {
483 		*nvp = 0;
484 	}
485 	tval = 1;
486 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "autoconfig", &tval);
487 	isp_autoconfig = tval;
488 	tval = 7;
489 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "quickboot_time", &tval);
490 	isp_quickboot_time = tval;
491 
492 	tval = 0;
493 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "forcemulti", &tval) == 0 && tval != 0) {
494 		isp->isp_osinfo.forcemulti = 1;
495 	}
496 }
497 
498 static void
499 isp_get_pci_options(device_t dev, int *m1, int *m2)
500 {
501 	int tval;
502 	/*
503 	 * Which we should try first - memory mapping or i/o mapping?
504 	 *
505 	 * We used to try memory first followed by i/o on alpha, otherwise
506 	 * the reverse, but we should just try memory first all the time now.
507 	 */
508 	*m1 = PCIM_CMD_MEMEN;
509 	*m2 = PCIM_CMD_PORTEN;
510 
511 	tval = 0;
512 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "prefer_iomap", &tval) == 0 && tval != 0) {
513 		*m1 = PCIM_CMD_PORTEN;
514 		*m2 = PCIM_CMD_MEMEN;
515 	}
516 	tval = 0;
517 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "prefer_memmap", &tval) == 0 && tval != 0) {
518 		*m1 = PCIM_CMD_MEMEN;
519 		*m2 = PCIM_CMD_PORTEN;
520 	}
521 }
522 
523 static void
524 isp_get_specific_options(device_t dev, int chan, ispsoftc_t *isp)
525 {
526 	char *sptr;
527 	int tval;
528 
529 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "iid", &tval)) {
530 		if (IS_FC(isp)) {
531 			ISP_FC_PC(isp, chan)->default_id = 109 - chan;
532 		} else {
533 #ifdef __sparc64__
534 			ISP_SPI_PC(isp, chan)->iid = OF_getscsinitid(dev);
535 #else
536 			ISP_SPI_PC(isp, chan)->iid = 7;
537 #endif
538 		}
539 	} else {
540 		if (IS_FC(isp)) {
541 			ISP_FC_PC(isp, chan)->default_id = tval - chan;
542 		} else {
543 			ISP_SPI_PC(isp, chan)->iid = tval;
544 		}
545 		isp->isp_confopts |= ISP_CFG_OWNLOOPID;
546 	}
547 
548 	tval = -1;
549 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "role", &tval) == 0) {
550 		switch (tval) {
551 		case ISP_ROLE_NONE:
552 		case ISP_ROLE_INITIATOR:
553 		case ISP_ROLE_TARGET:
554 		case ISP_ROLE_INITIATOR|ISP_ROLE_TARGET:
555 			device_printf(dev, "setting role to 0x%x\n", tval);
556 			break;
557 		default:
558 			tval = -1;
559 			break;
560 		}
561 	}
562 	if (tval == -1) {
563 		tval = ISP_DEFAULT_ROLES;
564 	}
565 
566 	if (IS_SCSI(isp)) {
567 		ISP_SPI_PC(isp, chan)->def_role = tval;
568 		return;
569 	}
570 	ISP_FC_PC(isp, chan)->def_role = tval;
571 
572 	tval = 0;
573 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fullduplex", &tval) == 0 && tval != 0) {
574 		isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
575 	}
576 	sptr = NULL;
577 	if (resource_string_value(device_get_name(dev), device_get_unit(dev), "topology", &sptr) == 0 && sptr != NULL) {
578 		if (strcmp(sptr, "lport") == 0) {
579 			isp->isp_confopts |= ISP_CFG_LPORT;
580 		} else if (strcmp(sptr, "nport") == 0) {
581 			isp->isp_confopts |= ISP_CFG_NPORT;
582 		} else if (strcmp(sptr, "lport-only") == 0) {
583 			isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
584 		} else if (strcmp(sptr, "nport-only") == 0) {
585 			isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
586 		}
587 	}
588 
589 	/*
590 	 * Because the resource_*_value functions can neither return
591 	 * 64 bit integer values, nor can they be directly coerced
592 	 * to interpret the right hand side of the assignment as
593 	 * you want them to interpret it, we have to force WWN
594 	 * hint replacement to specify WWN strings with a leading
595 	 * 'w' (e..g w50000000aaaa0001). Sigh.
596 	 */
597 	sptr = NULL;
598 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev), "portwwn", &sptr);
599 	if (tval == 0 && sptr != NULL && *sptr++ == 'w') {
600 		char *eptr = NULL;
601 		ISP_FC_PC(isp, chan)->def_wwpn = strtouq(sptr, &eptr, 16);
602 		if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwpn == -1) {
603 			device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
604 			ISP_FC_PC(isp, chan)->def_wwpn = 0;
605 		}
606 	}
607 
608 	sptr = NULL;
609 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev), "nodewwn", &sptr);
610 	if (tval == 0 && sptr != NULL && *sptr++ == 'w') {
611 		char *eptr = NULL;
612 		ISP_FC_PC(isp, chan)->def_wwnn = strtouq(sptr, &eptr, 16);
613 		if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwnn == 0) {
614 			device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
615 			ISP_FC_PC(isp, chan)->def_wwnn = 0;
616 		}
617 	}
618 
619 	tval = 0;
620 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "hysteresis", &tval);
621 	if (tval >= 0 && tval < 256) {
622 		ISP_FC_PC(isp, chan)->hysteresis = tval;
623 	} else {
624 		ISP_FC_PC(isp, chan)->hysteresis = isp_fabric_hysteresis;
625 	}
626 
627 	tval = -1;
628 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "loop_down_limit", &tval);
629 	if (tval >= 0 && tval < 0xffff) {
630 		ISP_FC_PC(isp, chan)->loop_down_limit = tval;
631 	} else {
632 		ISP_FC_PC(isp, chan)->loop_down_limit = isp_loop_down_limit;
633 	}
634 
635 	tval = -1;
636 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "gone_device_time", &tval);
637 	if (tval >= 0 && tval < 0xffff) {
638 		ISP_FC_PC(isp, chan)->gone_device_time = tval;
639 	} else {
640 		ISP_FC_PC(isp, chan)->gone_device_time = isp_gone_device_time;
641 	}
642 }
643 
644 static int
645 isp_pci_attach(device_t dev)
646 {
647 	int i, m1, m2, locksetup = 0;
648 	int isp_nvports = 0;
649 	uint32_t data, cmd, linesz, did;
650 	struct isp_pcisoftc *pcs;
651 	ispsoftc_t *isp;
652 	size_t psize, xsize;
653 	char fwname[32];
654 	u_int irq_flags;
655 
656 	pcs = device_get_softc(dev);
657 	if (pcs == NULL) {
658 		device_printf(dev, "cannot get softc\n");
659 		return (ENOMEM);
660 	}
661 	memset(pcs, 0, sizeof (*pcs));
662 
663 	pcs->pci_dev = dev;
664 	isp = &pcs->pci_isp;
665 	isp->isp_dev = dev;
666 	isp->isp_nchan = 1;
667 
668 	/*
669 	 * Get Generic Options
670 	 */
671 	isp_get_generic_options(dev, isp, &isp_nvports);
672 
673 	/*
674 	 * Check to see if options have us disabled
675 	 */
676 	if (isp->isp_osinfo.disabled) {
677 		/*
678 		 * But return zero to preserve unit numbering
679 		 */
680 		return (0);
681 	}
682 
683 	/*
684 	 * Get PCI options- which in this case are just mapping preferences.
685 	 */
686 	isp_get_pci_options(dev, &m1, &m2);
687 
688 	linesz = PCI_DFLT_LNSZ;
689 	pcs->irq = pcs->regs = NULL;
690 	pcs->rgd = pcs->rtp = pcs->iqd = 0;
691 
692 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
693 	if (cmd & m1) {
694 		pcs->rtp = (m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
695 		pcs->rgd = (m1 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
696 		pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd, RF_ACTIVE);
697 	}
698 	if (pcs->regs == NULL && (cmd & m2)) {
699 		pcs->rtp = (m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
700 		pcs->rgd = (m2 == PCIM_CMD_MEMEN)? MEM_MAP_REG : IO_MAP_REG;
701 		pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd, RF_ACTIVE);
702 	}
703 	if (pcs->regs == NULL) {
704 		device_printf(dev, "unable to map any ports\n");
705 		goto bad;
706 	}
707 	if (bootverbose) {
708 		device_printf(dev, "using %s space register mapping\n", (pcs->rgd == IO_MAP_REG)? "I/O" : "Memory");
709 	}
710 	isp->isp_bus_tag = rman_get_bustag(pcs->regs);
711 	isp->isp_bus_handle = rman_get_bushandle(pcs->regs);
712 
713 	pcs->pci_dev = dev;
714 	pcs->pci_poff[BIU_BLOCK >> _BLK_REG_SHFT] = BIU_REGS_OFF;
715 	pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS_OFF;
716 	pcs->pci_poff[SXP_BLOCK >> _BLK_REG_SHFT] = PCI_SXP_REGS_OFF;
717 	pcs->pci_poff[RISC_BLOCK >> _BLK_REG_SHFT] = PCI_RISC_REGS_OFF;
718 	pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = DMA_REGS_OFF;
719 
720 	switch (pci_get_devid(dev)) {
721 	case PCI_QLOGIC_ISP1020:
722 		did = 0x1040;
723 		isp->isp_mdvec = &mdvec;
724 		isp->isp_type = ISP_HA_SCSI_UNKNOWN;
725 		break;
726 	case PCI_QLOGIC_ISP1080:
727 		did = 0x1080;
728 		isp->isp_mdvec = &mdvec_1080;
729 		isp->isp_type = ISP_HA_SCSI_1080;
730 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
731 		break;
732 	case PCI_QLOGIC_ISP1240:
733 		did = 0x1080;
734 		isp->isp_mdvec = &mdvec_1080;
735 		isp->isp_type = ISP_HA_SCSI_1240;
736 		isp->isp_nchan = 2;
737 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
738 		break;
739 	case PCI_QLOGIC_ISP1280:
740 		did = 0x1080;
741 		isp->isp_mdvec = &mdvec_1080;
742 		isp->isp_type = ISP_HA_SCSI_1280;
743 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
744 		break;
745 	case PCI_QLOGIC_ISP10160:
746 		did = 0x12160;
747 		isp->isp_mdvec = &mdvec_12160;
748 		isp->isp_type = ISP_HA_SCSI_10160;
749 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
750 		break;
751 	case PCI_QLOGIC_ISP12160:
752 		did = 0x12160;
753 		isp->isp_nchan = 2;
754 		isp->isp_mdvec = &mdvec_12160;
755 		isp->isp_type = ISP_HA_SCSI_12160;
756 		pcs->pci_poff[DMA_BLOCK >> _BLK_REG_SHFT] = ISP1080_DMA_REGS_OFF;
757 		break;
758 	case PCI_QLOGIC_ISP2100:
759 		did = 0x2100;
760 		isp->isp_mdvec = &mdvec_2100;
761 		isp->isp_type = ISP_HA_FC_2100;
762 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2100_OFF;
763 		if (pci_get_revid(dev) < 3) {
764 			/*
765 			 * XXX: Need to get the actual revision
766 			 * XXX: number of the 2100 FB. At any rate,
767 			 * XXX: lower cache line size for early revision
768 			 * XXX; boards.
769 			 */
770 			linesz = 1;
771 		}
772 		break;
773 	case PCI_QLOGIC_ISP2200:
774 		did = 0x2200;
775 		isp->isp_mdvec = &mdvec_2200;
776 		isp->isp_type = ISP_HA_FC_2200;
777 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2100_OFF;
778 		break;
779 	case PCI_QLOGIC_ISP2300:
780 		did = 0x2300;
781 		isp->isp_mdvec = &mdvec_2300;
782 		isp->isp_type = ISP_HA_FC_2300;
783 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
784 		break;
785 	case PCI_QLOGIC_ISP2312:
786 	case PCI_QLOGIC_ISP6312:
787 		did = 0x2300;
788 		isp->isp_mdvec = &mdvec_2300;
789 		isp->isp_type = ISP_HA_FC_2312;
790 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
791 		break;
792 	case PCI_QLOGIC_ISP2322:
793 	case PCI_QLOGIC_ISP6322:
794 		did = 0x2322;
795 		isp->isp_mdvec = &mdvec_2300;
796 		isp->isp_type = ISP_HA_FC_2322;
797 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2300_OFF;
798 		break;
799 	case PCI_QLOGIC_ISP2422:
800 	case PCI_QLOGIC_ISP2432:
801 		did = 0x2400;
802 		isp->isp_nchan += isp_nvports;
803 		isp->isp_mdvec = &mdvec_2400;
804 		isp->isp_type = ISP_HA_FC_2400;
805 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
806 		break;
807 	case PCI_QLOGIC_ISP2532:
808 		did = 0x2500;
809 		isp->isp_nchan += isp_nvports;
810 		isp->isp_mdvec = &mdvec_2500;
811 		isp->isp_type = ISP_HA_FC_2500;
812 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
813 		break;
814 	case PCI_QLOGIC_ISP5432:
815 		did = 0x2500;
816 		isp->isp_mdvec = &mdvec_2500;
817 		isp->isp_type = ISP_HA_FC_2500;
818 		pcs->pci_poff[MBOX_BLOCK >> _BLK_REG_SHFT] = PCI_MBOX_REGS2400_OFF;
819 		break;
820 	default:
821 		device_printf(dev, "unknown device type\n");
822 		goto bad;
823 		break;
824 	}
825 	isp->isp_revision = pci_get_revid(dev);
826 
827 	if (IS_FC(isp)) {
828 		psize = sizeof (fcparam);
829 		xsize = sizeof (struct isp_fc);
830 	} else {
831 		psize = sizeof (sdparam);
832 		xsize = sizeof (struct isp_spi);
833 	}
834 	psize *= isp->isp_nchan;
835 	xsize *= isp->isp_nchan;
836 	isp->isp_param = kmalloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
837 	if (isp->isp_param == NULL) {
838 		device_printf(dev, "cannot allocate parameter data\n");
839 		goto bad;
840 	}
841 	isp->isp_osinfo.pc.ptr = kmalloc(xsize, M_DEVBUF, M_NOWAIT | M_ZERO);
842 	if (isp->isp_osinfo.pc.ptr == NULL) {
843 		device_printf(dev, "cannot allocate parameter data\n");
844 		goto bad;
845 	}
846 
847 	/*
848 	 * Now that we know who we are (roughly) get/set specific options
849 	 */
850 	for (i = 0; i < isp->isp_nchan; i++) {
851 		isp_get_specific_options(dev, i, isp);
852 	}
853 
854 	/*
855 	 * The 'it' suffix really only matters for SCSI cards in target mode.
856 	 */
857 	isp->isp_osinfo.fw = NULL;
858 	if (IS_SCSI(isp) && (ISP_SPI_PC(isp, 0)->def_role & ISP_ROLE_TARGET)) {
859 		ksnprintf(fwname, sizeof (fwname), "isp_%04x_it", did);
860 		isp->isp_osinfo.fw = firmware_get(fwname);
861 	} else if (IS_24XX(isp) && (isp->isp_nchan > 1 || isp->isp_osinfo.forcemulti)) {
862 		ksnprintf(fwname, sizeof (fwname), "isp_%04x_multi", did);
863 		isp->isp_osinfo.fw = firmware_get(fwname);
864 	}
865 	if (isp->isp_osinfo.fw == NULL) {
866 		ksnprintf(fwname, sizeof (fwname), "isp_%04x", did);
867 		isp->isp_osinfo.fw = firmware_get(fwname);
868 	}
869 	if (isp->isp_osinfo.fw != NULL) {
870 		isp->isp_mdvec->dv_ispfw = isp->isp_osinfo.fw->data;
871 	}
872 
873 	/*
874 	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER
875 	 * are set.
876 	 */
877 	cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN |
878 		PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
879 
880 	if (IS_2300(isp)) {	/* per QLogic errata */
881 		cmd &= ~PCIM_CMD_INVEN;
882 	}
883 
884 	if (IS_2322(isp) || pci_get_devid(dev) == PCI_QLOGIC_ISP6312) {
885 		cmd &= ~PCIM_CMD_INTX_DISABLE;
886 	}
887 
888 	if (IS_24XX(isp)) {
889 		cmd &= ~PCIM_CMD_INTX_DISABLE;
890 	}
891 
892 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
893 
894 	/*
895 	 * Make sure the Cache Line Size register is set sensibly.
896 	 */
897 	data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
898 	if (data == 0 || (linesz != PCI_DFLT_LNSZ && data != linesz)) {
899 		isp_prt(isp, ISP_LOGCONFIG, "set PCI line size to %d from %d", linesz, data);
900 		data = linesz;
901 		pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
902 	}
903 
904 	/*
905 	 * Make sure the Latency Timer is sane.
906 	 */
907 	data = pci_read_config(dev, PCIR_LATTIMER, 1);
908 	if (data < PCI_DFLT_LTNCY) {
909 		data = PCI_DFLT_LTNCY;
910 		isp_prt(isp, ISP_LOGCONFIG, "set PCI latency to %d", data);
911 		pci_write_config(dev, PCIR_LATTIMER, data, 1);
912 	}
913 
914 	/*
915 	 * Make sure we've disabled the ROM.
916 	 */
917 	data = pci_read_config(dev, PCIR_ROMADDR, 4);
918 	data &= ~1;
919 	pci_write_config(dev, PCIR_ROMADDR, data, 4);
920 
921 	pcs->irq_type = pci_alloc_1intr(dev, isp_msi_enable, &pcs->iqd,
922 	    &irq_flags);
923 	pcs->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &pcs->iqd,
924 	    irq_flags);
925 	if (pcs->irq == NULL) {
926 		device_printf(dev, "could not allocate interrupt\n");
927 		goto bad;
928 	}
929 
930 	/* Make sure the lock is set up. */
931 	lockinit(&isp->isp_osinfo.lock, "isp", 0, LK_CANRECURSE);
932 	locksetup++;
933 
934 	if (isp_setup_intr(dev, pcs->irq, ISP_IFLAGS, isp_platform_intr, isp, &pcs->ih, NULL)) {
935 		device_printf(dev, "could not setup interrupt\n");
936 		goto bad;
937 	}
938 
939 	/*
940 	 * Last minute checks...
941 	 */
942 	if (IS_23XX(isp) || IS_24XX(isp)) {
943 		isp->isp_port = pci_get_function(dev);
944 	}
945 
946 	/*
947 	 * Make sure we're in reset state.
948 	 */
949 	ISP_LOCK(isp);
950 	isp_reset(isp, 1);
951 	if (isp->isp_state != ISP_RESETSTATE) {
952 		ISP_UNLOCK(isp);
953 		goto bad;
954 	}
955 	isp_init(isp);
956 	if (isp->isp_state == ISP_INITSTATE) {
957 		isp->isp_state = ISP_RUNSTATE;
958 	}
959 	ISP_UNLOCK(isp);
960 	if (isp_attach(isp)) {
961 		ISP_LOCK(isp);
962 		isp_uninit(isp);
963 		ISP_UNLOCK(isp);
964 		goto bad;
965 	}
966 	return (0);
967 
968 bad:
969 	if (pcs->ih) {
970 		(void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
971 	}
972 	if (locksetup) {
973 		lockuninit(&isp->isp_osinfo.lock);
974 	}
975 	if (pcs->irq) {
976 		(void) bus_release_resource(dev, SYS_RES_IRQ, pcs->iqd, pcs->irq);
977 	}
978 	if (pcs->irq_type == PCI_INTR_TYPE_MSI) {
979 		pci_release_msi(dev);
980 	}
981 	if (pcs->regs) {
982 		(void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
983 	}
984 	if (pcs->pci_isp.isp_param) {
985 		kfree(pcs->pci_isp.isp_param, M_DEVBUF);
986 		pcs->pci_isp.isp_param = NULL;
987 	}
988 	if (pcs->pci_isp.isp_osinfo.pc.ptr) {
989 		kfree(pcs->pci_isp.isp_osinfo.pc.ptr, M_DEVBUF);
990 		pcs->pci_isp.isp_osinfo.pc.ptr = NULL;
991 	}
992 	return (ENXIO);
993 }
994 
995 static int
996 isp_pci_detach(device_t dev)
997 {
998 	struct isp_pcisoftc *pcs;
999 	ispsoftc_t *isp;
1000 	int status;
1001 
1002 	pcs = device_get_softc(dev);
1003 	if (pcs == NULL) {
1004 		return (ENXIO);
1005 	}
1006 	isp = (ispsoftc_t *) pcs;
1007 	status = isp_detach(isp);
1008 	if (status)
1009 		return (status);
1010 	ISP_LOCK(isp);
1011 	isp_uninit(isp);
1012 	if (pcs->ih) {
1013 		(void) bus_teardown_intr(dev, pcs->irq, pcs->ih);
1014 	}
1015 	ISP_UNLOCK(isp);
1016 	lockuninit(&isp->isp_osinfo.lock);
1017 	(void) bus_release_resource(dev, SYS_RES_IRQ, pcs->iqd, pcs->irq);
1018 	if (pcs->irq_type == PCI_INTR_TYPE_MSI) {
1019 		pci_release_msi(dev);
1020 	}
1021 	(void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
1022 	if (pcs->pci_isp.isp_param) {
1023 		kfree(pcs->pci_isp.isp_param, M_DEVBUF);
1024 		pcs->pci_isp.isp_param = NULL;
1025 	}
1026 	if (pcs->pci_isp.isp_osinfo.pc.ptr) {
1027 		kfree(pcs->pci_isp.isp_osinfo.pc.ptr, M_DEVBUF);
1028 		pcs->pci_isp.isp_osinfo.pc.ptr = NULL;
1029 	}
1030 	return (0);
1031 }
1032 
1033 #define	IspVirt2Off(a, x)	\
1034 	(((struct isp_pcisoftc *)a)->pci_poff[((x) & _BLK_REG_MASK) >> \
1035 	_BLK_REG_SHFT] + ((x) & 0xfff))
1036 
1037 #define	BXR2(isp, off)		\
1038 	bus_space_read_2(isp->isp_bus_tag, isp->isp_bus_handle, off)
1039 #define	BXW2(isp, off, v)	\
1040 	bus_space_write_2(isp->isp_bus_tag, isp->isp_bus_handle, off, v)
1041 #define	BXR4(isp, off)		\
1042 	bus_space_read_4(isp->isp_bus_tag, isp->isp_bus_handle, off)
1043 #define	BXW4(isp, off, v)	\
1044 	bus_space_write_4(isp->isp_bus_tag, isp->isp_bus_handle, off, v)
1045 
1046 
1047 static ISP_INLINE int
1048 isp_pci_rd_debounced(ispsoftc_t *isp, int off, uint16_t *rp)
1049 {
1050 	uint32_t val0, val1;
1051 	int i = 0;
1052 
1053 	do {
1054 		val0 = BXR2(isp, IspVirt2Off(isp, off));
1055 		val1 = BXR2(isp, IspVirt2Off(isp, off));
1056 	} while (val0 != val1 && ++i < 1000);
1057 	if (val0 != val1) {
1058 		return (1);
1059 	}
1060 	*rp = val0;
1061 	return (0);
1062 }
1063 
1064 static int
1065 isp_pci_rd_isr(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbp)
1066 {
1067 	uint16_t isr, sema;
1068 
1069 	if (IS_2100(isp)) {
1070 		if (isp_pci_rd_debounced(isp, BIU_ISR, &isr)) {
1071 		    return (0);
1072 		}
1073 		if (isp_pci_rd_debounced(isp, BIU_SEMA, &sema)) {
1074 		    return (0);
1075 		}
1076 	} else {
1077 		isr = BXR2(isp, IspVirt2Off(isp, BIU_ISR));
1078 		sema = BXR2(isp, IspVirt2Off(isp, BIU_SEMA));
1079 	}
1080 	isp_prt(isp, ISP_LOGDEBUG3, "ISR 0x%x SEMA 0x%x", isr, sema);
1081 	isr &= INT_PENDING_MASK(isp);
1082 	sema &= BIU_SEMA_LOCK;
1083 	if (isr == 0 && sema == 0) {
1084 		return (0);
1085 	}
1086 	*isrp = isr;
1087 	if ((*semap = sema) != 0) {
1088 		if (IS_2100(isp)) {
1089 			if (isp_pci_rd_debounced(isp, OUTMAILBOX0, mbp)) {
1090 				return (0);
1091 			}
1092 		} else {
1093 			*mbp = BXR2(isp, IspVirt2Off(isp, OUTMAILBOX0));
1094 		}
1095 	}
1096 	return (1);
1097 }
1098 
1099 static int
1100 isp_pci_rd_isr_2300(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1101 {
1102 	uint32_t hccr;
1103 	uint32_t r2hisr;
1104 
1105 	if (!(BXR2(isp, IspVirt2Off(isp, BIU_ISR) & BIU2100_ISR_RISC_INT))) {
1106 		*isrp = 0;
1107 		return (0);
1108 	}
1109 	r2hisr = BXR4(isp, IspVirt2Off(isp, BIU_R2HSTSLO));
1110 	isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1111 	if ((r2hisr & BIU_R2HST_INTR) == 0) {
1112 		*isrp = 0;
1113 		return (0);
1114 	}
1115 	switch (r2hisr & BIU_R2HST_ISTAT_MASK) {
1116 	case ISPR2HST_ROM_MBX_OK:
1117 	case ISPR2HST_ROM_MBX_FAIL:
1118 	case ISPR2HST_MBX_OK:
1119 	case ISPR2HST_MBX_FAIL:
1120 	case ISPR2HST_ASYNC_EVENT:
1121 		*isrp = r2hisr & 0xffff;
1122 		*mbox0p = (r2hisr >> 16);
1123 		*semap = 1;
1124 		return (1);
1125 	case ISPR2HST_RIO_16:
1126 		*isrp = r2hisr & 0xffff;
1127 		*mbox0p = ASYNC_RIO16_1;
1128 		*semap = 1;
1129 		return (1);
1130 	case ISPR2HST_FPOST:
1131 		*isrp = r2hisr & 0xffff;
1132 		*mbox0p = ASYNC_CMD_CMPLT;
1133 		*semap = 1;
1134 		return (1);
1135 	case ISPR2HST_FPOST_CTIO:
1136 		*isrp = r2hisr & 0xffff;
1137 		*mbox0p = ASYNC_CTIO_DONE;
1138 		*semap = 1;
1139 		return (1);
1140 	case ISPR2HST_RSPQ_UPDATE:
1141 		*isrp = r2hisr & 0xffff;
1142 		*mbox0p = 0;
1143 		*semap = 0;
1144 		return (1);
1145 	default:
1146 		hccr = ISP_READ(isp, HCCR);
1147 		if (hccr & HCCR_PAUSE) {
1148 			ISP_WRITE(isp, HCCR, HCCR_RESET);
1149 			isp_prt(isp, ISP_LOGERR, "RISC paused at interrupt (%x->%x)", hccr, ISP_READ(isp, HCCR));
1150 			ISP_WRITE(isp, BIU_ICR, 0);
1151 		} else {
1152 			isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1153 		}
1154 		return (0);
1155 	}
1156 }
1157 
1158 static int
1159 isp_pci_rd_isr_2400(ispsoftc_t *isp, uint32_t *isrp, uint16_t *semap, uint16_t *mbox0p)
1160 {
1161 	uint32_t r2hisr;
1162 
1163 	r2hisr = BXR4(isp, IspVirt2Off(isp, BIU2400_R2HSTSLO));
1164 	isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
1165 	if ((r2hisr & BIU2400_R2HST_INTR) == 0) {
1166 		*isrp = 0;
1167 		return (0);
1168 	}
1169 	switch (r2hisr & BIU2400_R2HST_ISTAT_MASK) {
1170 	case ISP2400R2HST_ROM_MBX_OK:
1171 	case ISP2400R2HST_ROM_MBX_FAIL:
1172 	case ISP2400R2HST_MBX_OK:
1173 	case ISP2400R2HST_MBX_FAIL:
1174 	case ISP2400R2HST_ASYNC_EVENT:
1175 		*isrp = r2hisr & 0xffff;
1176 		*mbox0p = (r2hisr >> 16);
1177 		*semap = 1;
1178 		return (1);
1179 	case ISP2400R2HST_RSPQ_UPDATE:
1180 	case ISP2400R2HST_ATIO_RSPQ_UPDATE:
1181 	case ISP2400R2HST_ATIO_RQST_UPDATE:
1182 		*isrp = r2hisr & 0xffff;
1183 		*mbox0p = 0;
1184 		*semap = 0;
1185 		return (1);
1186 	default:
1187 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
1188 		isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
1189 		return (0);
1190 	}
1191 }
1192 
1193 static uint32_t
1194 isp_pci_rd_reg(ispsoftc_t *isp, int regoff)
1195 {
1196 	uint16_t rv;
1197 	int oldconf = 0;
1198 
1199 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1200 		/*
1201 		 * We will assume that someone has paused the RISC processor.
1202 		 */
1203 		oldconf = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1204 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf | BIU_PCI_CONF1_SXP);
1205 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1206 	}
1207 	rv = BXR2(isp, IspVirt2Off(isp, regoff));
1208 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1209 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf);
1210 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1211 	}
1212 	return (rv);
1213 }
1214 
1215 static void
1216 isp_pci_wr_reg(ispsoftc_t *isp, int regoff, uint32_t val)
1217 {
1218 	int oldconf = 0;
1219 
1220 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1221 		/*
1222 		 * We will assume that someone has paused the RISC processor.
1223 		 */
1224 		oldconf = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1225 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1226 		    oldconf | BIU_PCI_CONF1_SXP);
1227 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1228 	}
1229 	BXW2(isp, IspVirt2Off(isp, regoff), val);
1230 	MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1231 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK) {
1232 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oldconf);
1233 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1234 	}
1235 
1236 }
1237 
1238 static uint32_t
1239 isp_pci_rd_reg_1080(ispsoftc_t *isp, int regoff)
1240 {
1241 	uint32_t rv, oc = 0;
1242 
1243 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1244 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1245 		uint32_t tc;
1246 		/*
1247 		 * We will assume that someone has paused the RISC processor.
1248 		 */
1249 		oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1250 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
1251 		if (regoff & SXP_BANK1_SELECT)
1252 			tc |= BIU_PCI1080_CONF1_SXP1;
1253 		else
1254 			tc |= BIU_PCI1080_CONF1_SXP0;
1255 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1), tc);
1256 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1257 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1258 		oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1259 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1260 		    oc | BIU_PCI1080_CONF1_DMA);
1261 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1262 	}
1263 	rv = BXR2(isp, IspVirt2Off(isp, regoff));
1264 	if (oc) {
1265 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1266 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1267 	}
1268 	return (rv);
1269 }
1270 
1271 static void
1272 isp_pci_wr_reg_1080(ispsoftc_t *isp, int regoff, uint32_t val)
1273 {
1274 	int oc = 0;
1275 
1276 	if ((regoff & _BLK_REG_MASK) == SXP_BLOCK ||
1277 	    (regoff & _BLK_REG_MASK) == (SXP_BLOCK|SXP_BANK1_SELECT)) {
1278 		uint32_t tc;
1279 		/*
1280 		 * We will assume that someone has paused the RISC processor.
1281 		 */
1282 		oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1283 		tc = oc & ~BIU_PCI1080_CONF1_DMA;
1284 		if (regoff & SXP_BANK1_SELECT)
1285 			tc |= BIU_PCI1080_CONF1_SXP1;
1286 		else
1287 			tc |= BIU_PCI1080_CONF1_SXP0;
1288 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1), tc);
1289 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1290 	} else if ((regoff & _BLK_REG_MASK) == DMA_BLOCK) {
1291 		oc = BXR2(isp, IspVirt2Off(isp, BIU_CONF1));
1292 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1),
1293 		    oc | BIU_PCI1080_CONF1_DMA);
1294 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1295 	}
1296 	BXW2(isp, IspVirt2Off(isp, regoff), val);
1297 	MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1298 	if (oc) {
1299 		BXW2(isp, IspVirt2Off(isp, BIU_CONF1), oc);
1300 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, BIU_CONF1), 2, -1);
1301 	}
1302 }
1303 
1304 static uint32_t
1305 isp_pci_rd_reg_2400(ispsoftc_t *isp, int regoff)
1306 {
1307 	uint32_t rv;
1308 	int block = regoff & _BLK_REG_MASK;
1309 
1310 	switch (block) {
1311 	case BIU_BLOCK:
1312 		break;
1313 	case MBOX_BLOCK:
1314 		return (BXR2(isp, IspVirt2Off(isp, regoff)));
1315 	case SXP_BLOCK:
1316 		isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK read at 0x%x", regoff);
1317 		return (0xffffffff);
1318 	case RISC_BLOCK:
1319 		isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK read at 0x%x", regoff);
1320 		return (0xffffffff);
1321 	case DMA_BLOCK:
1322 		isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK read at 0x%x", regoff);
1323 		return (0xffffffff);
1324 	default:
1325 		isp_prt(isp, ISP_LOGWARN, "unknown block read at 0x%x", regoff);
1326 		return (0xffffffff);
1327 	}
1328 
1329 
1330 	switch (regoff) {
1331 	case BIU2400_FLASH_ADDR:
1332 	case BIU2400_FLASH_DATA:
1333 	case BIU2400_ICR:
1334 	case BIU2400_ISR:
1335 	case BIU2400_CSR:
1336 	case BIU2400_REQINP:
1337 	case BIU2400_REQOUTP:
1338 	case BIU2400_RSPINP:
1339 	case BIU2400_RSPOUTP:
1340 	case BIU2400_PRI_REQINP:
1341 	case BIU2400_PRI_REQOUTP:
1342 	case BIU2400_ATIO_RSPINP:
1343 	case BIU2400_ATIO_RSPOUTP:
1344 	case BIU2400_HCCR:
1345 	case BIU2400_GPIOD:
1346 	case BIU2400_GPIOE:
1347 	case BIU2400_HSEMA:
1348 		rv = BXR4(isp, IspVirt2Off(isp, regoff));
1349 		break;
1350 	case BIU2400_R2HSTSLO:
1351 		rv = BXR4(isp, IspVirt2Off(isp, regoff));
1352 		break;
1353 	case BIU2400_R2HSTSHI:
1354 		rv = BXR4(isp, IspVirt2Off(isp, regoff)) >> 16;
1355 		break;
1356 	default:
1357 		isp_prt(isp, ISP_LOGERR,
1358 		    "isp_pci_rd_reg_2400: unknown offset %x", regoff);
1359 		rv = 0xffffffff;
1360 		break;
1361 	}
1362 	return (rv);
1363 }
1364 
1365 static void
1366 isp_pci_wr_reg_2400(ispsoftc_t *isp, int regoff, uint32_t val)
1367 {
1368 	int block = regoff & _BLK_REG_MASK;
1369 
1370 	switch (block) {
1371 	case BIU_BLOCK:
1372 		break;
1373 	case MBOX_BLOCK:
1374 		BXW2(isp, IspVirt2Off(isp, regoff), val);
1375 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 2, -1);
1376 		return;
1377 	case SXP_BLOCK:
1378 		isp_prt(isp, ISP_LOGWARN, "SXP_BLOCK write at 0x%x", regoff);
1379 		return;
1380 	case RISC_BLOCK:
1381 		isp_prt(isp, ISP_LOGWARN, "RISC_BLOCK write at 0x%x", regoff);
1382 		return;
1383 	case DMA_BLOCK:
1384 		isp_prt(isp, ISP_LOGWARN, "DMA_BLOCK write at 0x%x", regoff);
1385 		return;
1386 	default:
1387 		isp_prt(isp, ISP_LOGWARN, "unknown block write at 0x%x",
1388 		    regoff);
1389 		break;
1390 	}
1391 
1392 	switch (regoff) {
1393 	case BIU2400_FLASH_ADDR:
1394 	case BIU2400_FLASH_DATA:
1395 	case BIU2400_ICR:
1396 	case BIU2400_ISR:
1397 	case BIU2400_CSR:
1398 	case BIU2400_REQINP:
1399 	case BIU2400_REQOUTP:
1400 	case BIU2400_RSPINP:
1401 	case BIU2400_RSPOUTP:
1402 	case BIU2400_PRI_REQINP:
1403 	case BIU2400_PRI_REQOUTP:
1404 	case BIU2400_ATIO_RSPINP:
1405 	case BIU2400_ATIO_RSPOUTP:
1406 	case BIU2400_HCCR:
1407 	case BIU2400_GPIOD:
1408 	case BIU2400_GPIOE:
1409 	case BIU2400_HSEMA:
1410 		BXW4(isp, IspVirt2Off(isp, regoff), val);
1411 		MEMORYBARRIER(isp, SYNC_REG, IspVirt2Off(isp, regoff), 4, -1);
1412 		break;
1413 	default:
1414 		isp_prt(isp, ISP_LOGERR,
1415 		    "isp_pci_wr_reg_2400: bad offset 0x%x", regoff);
1416 		break;
1417 	}
1418 }
1419 
1420 
1421 struct imush {
1422 	ispsoftc_t *isp;
1423 	caddr_t vbase;
1424 	int chan;
1425 	int error;
1426 };
1427 
1428 static void imc(void *, bus_dma_segment_t *, int, int);
1429 static void imc1(void *, bus_dma_segment_t *, int, int);
1430 
1431 static void
1432 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1433 {
1434 	struct imush *imushp = (struct imush *) arg;
1435 
1436 	if (error) {
1437 		imushp->error = error;
1438 		return;
1439 	}
1440 	if (nseg != 1) {
1441 		imushp->error = EINVAL;
1442 		return;
1443 	}
1444 	isp_prt(imushp->isp, ISP_LOGDEBUG0, "request/result area @ 0x%jx/0x%jx", (uintmax_t) segs->ds_addr, (uintmax_t) segs->ds_len);
1445 	imushp->isp->isp_rquest = imushp->vbase;
1446 	imushp->isp->isp_rquest_dma = segs->ds_addr;
1447 	segs->ds_addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
1448 	imushp->vbase += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
1449 	imushp->isp->isp_result_dma = segs->ds_addr;
1450 	imushp->isp->isp_result = imushp->vbase;
1451 
1452 #ifdef	ISP_TARGET_MODE
1453 	if (IS_24XX(imushp->isp)) {
1454 		segs->ds_addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
1455 		imushp->vbase += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
1456 		imushp->isp->isp_atioq_dma = segs->ds_addr;
1457 		imushp->isp->isp_atioq = imushp->vbase;
1458 	}
1459 #endif
1460 }
1461 
1462 static void
1463 imc1(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1464 {
1465 	struct imush *imushp = (struct imush *) arg;
1466 	if (error) {
1467 		imushp->error = error;
1468 		return;
1469 	}
1470 	if (nseg != 1) {
1471 		imushp->error = EINVAL;
1472 		return;
1473 	}
1474 	isp_prt(imushp->isp, ISP_LOGDEBUG0, "scdma @ 0x%jx/0x%jx", (uintmax_t) segs->ds_addr, (uintmax_t) segs->ds_len);
1475 	FCPARAM(imushp->isp, imushp->chan)->isp_scdma = segs->ds_addr;
1476 	FCPARAM(imushp->isp, imushp->chan)->isp_scratch = imushp->vbase;
1477 }
1478 
1479 static int
1480 isp_pci_mbxdma(ispsoftc_t *isp)
1481 {
1482 	caddr_t base;
1483 	uint32_t len;
1484 	int i, error, ns, cmap = 0;
1485 	bus_size_t slim;	/* segment size */
1486 	bus_addr_t llim;	/* low limit of unavailable dma */
1487 	bus_addr_t hlim;	/* high limit of unavailable dma */
1488 	struct imush im;
1489 
1490 	/*
1491 	 * Already been here? If so, leave...
1492 	 */
1493 	if (isp->isp_rquest) {
1494 		return (0);
1495 	}
1496 	ISP_UNLOCK(isp);
1497 
1498 	if (isp->isp_maxcmds == 0) {
1499 		isp_prt(isp, ISP_LOGERR, "maxcmds not set");
1500 		ISP_LOCK(isp);
1501 		return (1);
1502 	}
1503 
1504 	hlim = BUS_SPACE_MAXADDR;
1505 	if (IS_ULTRA2(isp) || IS_FC(isp) || IS_1240(isp)) {
1506 		if (sizeof (bus_size_t) > 4) {
1507 			slim = (bus_size_t) (1ULL << 32);
1508 		} else {
1509 			slim = (bus_size_t) (1UL << 31);
1510 		}
1511 		llim = BUS_SPACE_MAXADDR;
1512 	} else {
1513 		llim = BUS_SPACE_MAXADDR_32BIT;
1514 		slim = (1UL << 24);
1515 	}
1516 
1517 	len = isp->isp_maxcmds * sizeof (struct isp_pcmd);
1518 	isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *) kmalloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1519 	if (isp->isp_osinfo.pcmd_pool == NULL) {
1520 		isp_prt(isp, ISP_LOGERR, "cannot allocate pcmds");
1521 		ISP_LOCK(isp);
1522 		return (1);
1523 	}
1524 
1525 	/*
1526 	 * XXX: We don't really support 64 bit target mode for parallel scsi yet
1527 	 */
1528 #ifdef	ISP_TARGET_MODE
1529 	if (IS_SCSI(isp) && sizeof (bus_addr_t) > 4) {
1530 		kfree(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1531 		isp_prt(isp, ISP_LOGERR, "we cannot do DAC for SPI cards yet");
1532 		ISP_LOCK(isp);
1533 		return (1);
1534 	}
1535 #endif
1536 
1537 	if (isp_dma_tag_create(NULL, 1, slim, llim, hlim, NULL, NULL, BUS_SPACE_MAXSIZE, ISP_NSEGS, slim, 0, &isp->isp_osinfo.dmat)) {
1538 		kfree(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1539 		ISP_LOCK(isp);
1540 		isp_prt(isp, ISP_LOGERR, "could not create master dma tag");
1541 		return (1);
1542 	}
1543 
1544 	len = sizeof (isp_hdl_t) * isp->isp_maxcmds;
1545 	isp->isp_xflist = (isp_hdl_t *) kmalloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1546 	if (isp->isp_xflist == NULL) {
1547 		kfree(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1548 		ISP_LOCK(isp);
1549 		isp_prt(isp, ISP_LOGERR, "cannot alloc xflist array");
1550 		return (1);
1551 	}
1552 	for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1553 		isp->isp_xflist[len].cmd = &isp->isp_xflist[len+1];
1554 	}
1555 	isp->isp_xffree = isp->isp_xflist;
1556 #ifdef	ISP_TARGET_MODE
1557 	len = sizeof (isp_hdl_t) * isp->isp_maxcmds;
1558 	isp->isp_tgtlist = (isp_hdl_t *) kmalloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1559 	if (isp->isp_tgtlist == NULL) {
1560 		kfree(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1561 		kfree(isp->isp_xflist, M_DEVBUF);
1562 		ISP_LOCK(isp);
1563 		isp_prt(isp, ISP_LOGERR, "cannot alloc tgtlist array");
1564 		return (1);
1565 	}
1566 	for (len = 0; len < isp->isp_maxcmds - 1; len++) {
1567 		isp->isp_tgtlist[len].cmd = &isp->isp_tgtlist[len+1];
1568 	}
1569 	isp->isp_tgtfree = isp->isp_tgtlist;
1570 #endif
1571 
1572 	/*
1573 	 * Allocate and map the request and result queues (and ATIO queue
1574 	 * if we're a 2400 supporting target mode).
1575 	 */
1576 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
1577 	len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1578 #ifdef	ISP_TARGET_MODE
1579 	if (IS_24XX(isp)) {
1580 		len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1581 	}
1582 #endif
1583 
1584 	ns = (len / PAGE_SIZE) + 1;
1585 
1586 	/*
1587 	 * Create a tag for the control spaces. We don't always need this
1588 	 * to be 32 bits, but we do this for simplicity and speed's sake.
1589 	 */
1590 	if (isp_dma_tag_create(isp->isp_osinfo.dmat, QENTRY_LEN, slim, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, len, ns, slim, 0, &isp->isp_osinfo.cdmat)) {
1591 		isp_prt(isp, ISP_LOGERR, "cannot create a dma tag for control spaces");
1592 		kfree(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1593 		kfree(isp->isp_xflist, M_DEVBUF);
1594 #ifdef	ISP_TARGET_MODE
1595 		kfree(isp->isp_tgtlist, M_DEVBUF);
1596 #endif
1597 		ISP_LOCK(isp);
1598 		return (1);
1599 	}
1600 
1601 	if (bus_dmamem_alloc(isp->isp_osinfo.cdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &isp->isp_osinfo.cdmap) != 0) {
1602 		isp_prt(isp, ISP_LOGERR, "cannot allocate %d bytes of CCB memory", len);
1603 		bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
1604 		kfree(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1605 		kfree(isp->isp_xflist, M_DEVBUF);
1606 #ifdef	ISP_TARGET_MODE
1607 		kfree(isp->isp_tgtlist, M_DEVBUF);
1608 #endif
1609 		ISP_LOCK(isp);
1610 		return (1);
1611 	}
1612 
1613 	im.isp = isp;
1614 	im.chan = 0;
1615 	im.vbase = base;
1616 	im.error = 0;
1617 
1618 	bus_dmamap_load(isp->isp_osinfo.cdmat, isp->isp_osinfo.cdmap, base, len, imc, &im, 0);
1619 	if (im.error) {
1620 		isp_prt(isp, ISP_LOGERR, "error %d loading dma map for control areas", im.error);
1621 		goto bad;
1622 	}
1623 
1624 	if (IS_FC(isp)) {
1625 		for (cmap = 0; cmap < isp->isp_nchan; cmap++) {
1626 			struct isp_fc *fc = ISP_FC_PC(isp, cmap);
1627 			if (isp_dma_tag_create(isp->isp_osinfo.dmat, 64, slim, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, ISP_FC_SCRLEN, 1, slim, 0, &fc->tdmat)) {
1628 				goto bad;
1629 			}
1630 			if (bus_dmamem_alloc(fc->tdmat, (void **)&base, BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &fc->tdmap) != 0) {
1631 				bus_dma_tag_destroy(fc->tdmat);
1632 				goto bad;
1633 			}
1634 			im.isp = isp;
1635 			im.chan = cmap;
1636 			im.vbase = base;
1637 			im.error = 0;
1638 			bus_dmamap_load(fc->tdmat, fc->tdmap, base, ISP_FC_SCRLEN, imc1, &im, 0);
1639 			if (im.error) {
1640 				bus_dmamem_free(fc->tdmat, base, fc->tdmap);
1641 				bus_dma_tag_destroy(fc->tdmat);
1642 				goto bad;
1643 			}
1644 		}
1645 	}
1646 
1647 	for (i = 0; i < isp->isp_maxcmds; i++) {
1648 		struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
1649 		error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
1650 		if (error) {
1651 			isp_prt(isp, ISP_LOGERR, "error %d creating per-cmd DMA maps", error);
1652 			while (--i >= 0) {
1653 				bus_dmamap_destroy(isp->isp_osinfo.dmat, isp->isp_osinfo.pcmd_pool[i].dmap);
1654 			}
1655 			goto bad;
1656 		}
1657 		callout_init(&pcmd->wdog);
1658 		if (i == isp->isp_maxcmds-1) {
1659 			pcmd->next = NULL;
1660 		} else {
1661 			pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
1662 		}
1663 	}
1664 	isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
1665 	ISP_LOCK(isp);
1666 	return (0);
1667 
1668 bad:
1669 	while (--cmap >= 0) {
1670 		struct isp_fc *fc = ISP_FC_PC(isp, cmap);
1671 		bus_dmamem_free(fc->tdmat, base, fc->tdmap);
1672 		bus_dma_tag_destroy(fc->tdmat);
1673 	}
1674 	bus_dmamem_free(isp->isp_osinfo.cdmat, base, isp->isp_osinfo.cdmap);
1675 	bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
1676 	kfree(isp->isp_xflist, M_DEVBUF);
1677 #ifdef	ISP_TARGET_MODE
1678 	kfree(isp->isp_tgtlist, M_DEVBUF);
1679 #endif
1680 	kfree(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1681 	isp->isp_rquest = NULL;
1682 	ISP_LOCK(isp);
1683 	return (1);
1684 }
1685 
1686 typedef struct {
1687 	ispsoftc_t *isp;
1688 	void *cmd_token;
1689 	void *rq;	/* original request */
1690 	int error;
1691 	bus_size_t mapsize;
1692 } mush_t;
1693 
1694 #define	MUSHERR_NOQENTRIES	-2
1695 
1696 #ifdef	ISP_TARGET_MODE
1697 static void tdma2_2(void *, bus_dma_segment_t *, int, bus_size_t, int);
1698 static void tdma2(void *, bus_dma_segment_t *, int, int);
1699 
1700 static void
1701 tdma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1702 {
1703 	mush_t *mp;
1704 	mp = (mush_t *)arg;
1705 	mp->mapsize = mapsize;
1706 	tdma2(arg, dm_segs, nseg, error);
1707 }
1708 
1709 static void
1710 tdma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1711 {
1712 	mush_t *mp;
1713 	ispsoftc_t *isp;
1714 	struct ccb_scsiio *csio;
1715 	isp_ddir_t ddir;
1716 	ispreq_t *rq;
1717 
1718 	mp = (mush_t *) arg;
1719 	if (error) {
1720 		mp->error = error;
1721 		return;
1722 	}
1723 	csio = mp->cmd_token;
1724 	isp = mp->isp;
1725 	rq = mp->rq;
1726 	if (nseg) {
1727 		if (sizeof (bus_addr_t) > 4) {
1728 			if (nseg >= ISP_NSEG64_MAX) {
1729 				isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
1730 				mp->error = EFAULT;
1731 				return;
1732 			}
1733 			if (rq->req_header.rqs_entry_type == RQSTYPE_CTIO2) {
1734 				rq->req_header.rqs_entry_type = RQSTYPE_CTIO3;
1735 			}
1736 		} else {
1737 			if (nseg >= ISP_NSEG_MAX) {
1738 				isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG_MAX);
1739 				mp->error = EFAULT;
1740 				return;
1741 			}
1742 		}
1743 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1744 			bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
1745 			ddir = ISP_TO_DEVICE;
1746 		} else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1747 			bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
1748 			ddir = ISP_FROM_DEVICE;
1749 		} else {
1750 			dm_segs = NULL;
1751 			nseg = 0;
1752 			ddir = ISP_NOXFR;
1753 		}
1754 	} else {
1755 		dm_segs = NULL;
1756 		nseg = 0;
1757 		ddir = ISP_NOXFR;
1758 	}
1759 
1760 	if (isp_send_tgt_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir, &csio->sense_data, csio->sense_len) != CMD_QUEUED) {
1761 		mp->error = MUSHERR_NOQENTRIES;
1762 	}
1763 }
1764 #endif
1765 
1766 static void dma2_2(void *, bus_dma_segment_t *, int, bus_size_t, int);
1767 static void dma2(void *, bus_dma_segment_t *, int, int);
1768 
1769 static void
1770 dma2_2(void *arg, bus_dma_segment_t *dm_segs, int nseg, bus_size_t mapsize, int error)
1771 {
1772 	mush_t *mp;
1773 	mp = (mush_t *)arg;
1774 	mp->mapsize = mapsize;
1775 	dma2(arg, dm_segs, nseg, error);
1776 }
1777 
1778 static void
1779 dma2(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1780 {
1781 	mush_t *mp;
1782 	ispsoftc_t *isp;
1783 	struct ccb_scsiio *csio;
1784 	isp_ddir_t ddir;
1785 	ispreq_t *rq;
1786 
1787 	mp = (mush_t *) arg;
1788 	if (error) {
1789 		mp->error = error;
1790 		return;
1791 	}
1792 	csio = mp->cmd_token;
1793 	isp = mp->isp;
1794 	rq = mp->rq;
1795 	if (nseg) {
1796 		if (sizeof (bus_addr_t) > 4) {
1797 			if (nseg >= ISP_NSEG64_MAX) {
1798 				isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
1799 				mp->error = EFAULT;
1800 				return;
1801 			}
1802 			if (rq->req_header.rqs_entry_type == RQSTYPE_T2RQS) {
1803 				rq->req_header.rqs_entry_type = RQSTYPE_T3RQS;
1804 			} else if (rq->req_header.rqs_entry_type == RQSTYPE_REQUEST) {
1805 				rq->req_header.rqs_entry_type = RQSTYPE_A64;
1806 			}
1807 		} else {
1808 			if (nseg >= ISP_NSEG_MAX) {
1809 				isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG_MAX);
1810 				mp->error = EFAULT;
1811 				return;
1812 			}
1813 		}
1814 		if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1815 			bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREREAD);
1816 			ddir = ISP_FROM_DEVICE;
1817 		} else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1818 			bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_PREWRITE);
1819 			ddir = ISP_TO_DEVICE;
1820 		} else {
1821 			ddir = ISP_NOXFR;
1822 		}
1823 	} else {
1824 		dm_segs = NULL;
1825 		nseg = 0;
1826 		ddir = ISP_NOXFR;
1827 	}
1828 
1829 	if (isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir) != CMD_QUEUED) {
1830 		mp->error = MUSHERR_NOQENTRIES;
1831 	}
1832 }
1833 
1834 static int
1835 isp_pci_dmasetup(ispsoftc_t *isp, struct ccb_scsiio *csio, void *ff)
1836 {
1837 	mush_t mush, *mp;
1838 	void (*eptr)(void *, bus_dma_segment_t *, int, int);
1839 	void (*eptr2)(void *, bus_dma_segment_t *, int, bus_size_t, int);
1840 
1841 	mp = &mush;
1842 	mp->isp = isp;
1843 	mp->cmd_token = csio;
1844 	mp->rq = ff;
1845 	mp->error = 0;
1846 	mp->mapsize = 0;
1847 
1848 #ifdef	ISP_TARGET_MODE
1849 	if (csio->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1850 		eptr = tdma2;
1851 		eptr2 = tdma2_2;
1852 	} else
1853 #endif
1854 	{
1855 		eptr = dma2;
1856 		eptr2 = dma2_2;
1857 	}
1858 
1859 
1860 	if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE || (csio->dxfer_len == 0)) {
1861 		(*eptr)(mp, NULL, 0, 0);
1862 	} else if ((csio->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
1863 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) == 0) {
1864 			int error;
1865 			error = bus_dmamap_load(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, csio->data_ptr, csio->dxfer_len, eptr, mp, 0);
1866 #if 0
1867 			xpt_print(csio->ccb_h.path, "%s: bus_dmamap_load " "ptr %p len %d returned %d\n", __func__, csio->data_ptr, csio->dxfer_len, error);
1868 #endif
1869 
1870 			if (error == EINPROGRESS) {
1871 				bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
1872 				mp->error = EINVAL;
1873 				isp_prt(isp, ISP_LOGERR, "deferred dma allocation not supported");
1874 			} else if (error && mp->error == 0) {
1875 #ifdef	DIAGNOSTIC
1876 				isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
1877 #endif
1878 				mp->error = error;
1879 			}
1880 		} else {
1881 			/* Pointer to physical buffer */
1882 			struct bus_dma_segment seg;
1883 			seg.ds_addr = (bus_addr_t)(vm_offset_t)csio->data_ptr;
1884 			seg.ds_len = csio->dxfer_len;
1885 			(*eptr)(mp, &seg, 1, 0);
1886 		}
1887 	} else {
1888 		struct bus_dma_segment *segs;
1889 
1890 		if ((csio->ccb_h.flags & CAM_DATA_PHYS) != 0) {
1891 			isp_prt(isp, ISP_LOGERR, "Physical segment pointers unsupported");
1892 			mp->error = EINVAL;
1893 		} else if ((csio->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
1894 			struct uio sguio;
1895 			int error;
1896 
1897 			/*
1898 			 * We're taking advantage of the fact that
1899 			 * the pointer/length sizes and layout of the iovec
1900 			 * structure are the same as the bus_dma_segment
1901 			 * structure.  This might be a little dangerous,
1902 			 * but only if they change the structures, which
1903 			 * seems unlikely.
1904 			 */
1905 			KASSERT((sizeof (sguio.uio_iov) == sizeof (csio->data_ptr) &&
1906 			    sizeof (sguio.uio_iovcnt) >= sizeof (csio->sglist_cnt) &&
1907 			    sizeof (sguio.uio_resid) >= sizeof (csio->dxfer_len)), ("Ken's assumption failed"));
1908 			sguio.uio_iov = (struct iovec *)csio->data_ptr;
1909 			sguio.uio_iovcnt = csio->sglist_cnt;
1910 			sguio.uio_resid = csio->dxfer_len;
1911 			sguio.uio_segflg = UIO_SYSSPACE;
1912 
1913 			error = bus_dmamap_load_uio(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, &sguio, eptr2, mp, 0);
1914 
1915 			if (error != 0 && mp->error == 0) {
1916 				isp_prt(isp, ISP_LOGERR, "error %d in dma mapping code", error);
1917 				mp->error = error;
1918 			}
1919 		} else {
1920 			/* Just use the segments provided */
1921 			segs = (struct bus_dma_segment *) csio->data_ptr;
1922 			(*eptr)(mp, segs, csio->sglist_cnt, 0);
1923 		}
1924 	}
1925 	if (mp->error) {
1926 		int retval = CMD_COMPLETE;
1927 		if (mp->error == MUSHERR_NOQENTRIES) {
1928 			retval = CMD_EAGAIN;
1929 		} else if (mp->error == EFBIG) {
1930 			XS_SETERR(csio, CAM_REQ_TOO_BIG);
1931 		} else if (mp->error == EINVAL) {
1932 			XS_SETERR(csio, CAM_REQ_INVALID);
1933 		} else {
1934 			XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
1935 		}
1936 		return (retval);
1937 	}
1938 	return (CMD_QUEUED);
1939 }
1940 
1941 static void
1942 isp_pci_reset0(ispsoftc_t *isp)
1943 {
1944 	ISP_DISABLE_INTS(isp);
1945 }
1946 
1947 static void
1948 isp_pci_reset1(ispsoftc_t *isp)
1949 {
1950 	if (!IS_24XX(isp)) {
1951 		/* Make sure the BIOS is disabled */
1952 		isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
1953 	}
1954 	/* and enable interrupts */
1955 	ISP_ENABLE_INTS(isp);
1956 }
1957 
1958 static void
1959 isp_pci_dumpregs(ispsoftc_t *isp, const char *msg)
1960 {
1961 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *)isp;
1962 	if (msg)
1963 		kprintf("%s: %s\n", device_get_nameunit(isp->isp_dev), msg);
1964 	else
1965 		kprintf("%s:\n", device_get_nameunit(isp->isp_dev));
1966 	if (IS_SCSI(isp))
1967 		kprintf("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
1968 	else
1969 		kprintf("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
1970 	kprintf(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
1971 	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
1972 	kprintf("risc_hccr=%x\n", ISP_READ(isp, HCCR));
1973 
1974 
1975 	if (IS_SCSI(isp)) {
1976 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
1977 		kprintf("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
1978 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
1979 			ISP_READ(isp, CDMA_FIFO_STS));
1980 		kprintf("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
1981 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
1982 			ISP_READ(isp, DDMA_FIFO_STS));
1983 		kprintf("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
1984 			ISP_READ(isp, SXP_INTERRUPT),
1985 			ISP_READ(isp, SXP_GROSS_ERR),
1986 			ISP_READ(isp, SXP_PINS_CTRL));
1987 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
1988 	}
1989 	kprintf("    mbox regs: %x %x %x %x %x\n",
1990 	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
1991 	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
1992 	    ISP_READ(isp, OUTMAILBOX4));
1993 	kprintf("    PCI Status Command/Status=%x\n",
1994 	    pci_read_config(pcs->pci_dev, PCIR_COMMAND, 1));
1995 }
1996