xref: /freebsd/sys/dev/isp/isp_pci.c (revision e64fe029e9d3ce476e77a478318e0c3cd201ff08)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009-2020 Alexander Motin <mav@FreeBSD.org>
5  * Copyright (c) 1997-2008 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  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
31  * FreeBSD Version.
32  */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/linker.h>
41 #include <sys/firmware.h>
42 #include <sys/bus.h>
43 #include <sys/stdint.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <sys/rman.h>
49 #include <sys/malloc.h>
50 #include <sys/uio.h>
51 #include <dev/isp/isp_freebsd.h>
52 
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 uint32_t isp_pci_rd_reg_2600(ispsoftc_t *, int);
56 static void isp_pci_wr_reg_2600(ispsoftc_t *, int, uint32_t);
57 static void isp_pci_run_isr_2400(ispsoftc_t *);
58 static int isp_pci_mbxdma(ispsoftc_t *);
59 static void isp_pci_mbxdmafree(ispsoftc_t *);
60 static int isp_pci_irqsetup(ispsoftc_t *);
61 
62 static struct ispmdvec mdvec_2400 = {
63 	isp_pci_run_isr_2400,
64 	isp_pci_rd_reg_2400,
65 	isp_pci_wr_reg_2400,
66 	isp_pci_mbxdma,
67 	isp_send_cmd,
68 	isp_pci_irqsetup,
69 	NULL
70 };
71 
72 static struct ispmdvec mdvec_2500 = {
73 	isp_pci_run_isr_2400,
74 	isp_pci_rd_reg_2400,
75 	isp_pci_wr_reg_2400,
76 	isp_pci_mbxdma,
77 	isp_send_cmd,
78 	isp_pci_irqsetup,
79 	NULL
80 };
81 
82 static struct ispmdvec mdvec_2600 = {
83 	isp_pci_run_isr_2400,
84 	isp_pci_rd_reg_2600,
85 	isp_pci_wr_reg_2600,
86 	isp_pci_mbxdma,
87 	isp_send_cmd,
88 	isp_pci_irqsetup,
89 	NULL
90 };
91 
92 static struct ispmdvec mdvec_2700 = {
93 	isp_pci_run_isr_2400,
94 	isp_pci_rd_reg_2600,
95 	isp_pci_wr_reg_2600,
96 	isp_pci_mbxdma,
97 	isp_send_cmd,
98 	isp_pci_irqsetup,
99 	NULL
100 };
101 
102 static struct ispmdvec mdvec_2800 = {
103 	isp_pci_run_isr_2400,
104 	isp_pci_rd_reg_2600,
105 	isp_pci_wr_reg_2600,
106 	isp_pci_mbxdma,
107 	isp_send_cmd,
108 	isp_pci_irqsetup,
109 	NULL
110 };
111 
112 #ifndef	PCIM_CMD_INVEN
113 #define	PCIM_CMD_INVEN			0x10
114 #endif
115 #ifndef	PCIM_CMD_BUSMASTEREN
116 #define	PCIM_CMD_BUSMASTEREN		0x0004
117 #endif
118 #ifndef	PCIM_CMD_PERRESPEN
119 #define	PCIM_CMD_PERRESPEN		0x0040
120 #endif
121 #ifndef	PCIM_CMD_SEREN
122 #define	PCIM_CMD_SEREN			0x0100
123 #endif
124 #ifndef	PCIM_CMD_INTX_DISABLE
125 #define	PCIM_CMD_INTX_DISABLE		0x0400
126 #endif
127 
128 #ifndef	PCIR_COMMAND
129 #define	PCIR_COMMAND			0x04
130 #endif
131 
132 #ifndef	PCIR_CACHELNSZ
133 #define	PCIR_CACHELNSZ			0x0c
134 #endif
135 
136 #ifndef	PCIR_LATTIMER
137 #define	PCIR_LATTIMER			0x0d
138 #endif
139 
140 #ifndef	PCIR_ROMADDR
141 #define	PCIR_ROMADDR			0x30
142 #endif
143 
144 #define	PCI_VENDOR_QLOGIC		0x1077
145 
146 #define	PCI_PRODUCT_QLOGIC_ISP2422	0x2422
147 #define	PCI_PRODUCT_QLOGIC_ISP2432	0x2432
148 #define	PCI_PRODUCT_QLOGIC_ISP2532	0x2532
149 #define	PCI_PRODUCT_QLOGIC_ISP5432	0x5432
150 #define	PCI_PRODUCT_QLOGIC_ISP2031	0x2031
151 #define	PCI_PRODUCT_QLOGIC_ISP8031	0x8031
152 #define	PCI_PRODUCT_QLOGIC_ISP2684	0x2171
153 #define	PCI_PRODUCT_QLOGIC_ISP2692	0x2b61
154 #define	PCI_PRODUCT_QLOGIC_ISP2714	0x2071
155 #define	PCI_PRODUCT_QLOGIC_ISP2722	0x2261
156 #define	PCI_PRODUCT_QLOGIC_ISP2812	0x2281
157 #define	PCI_PRODUCT_QLOGIC_ISP2814	0x2081
158 
159 #define	PCI_QLOGIC_ISP2422	\
160 	((PCI_PRODUCT_QLOGIC_ISP2422 << 16) | PCI_VENDOR_QLOGIC)
161 #define	PCI_QLOGIC_ISP2432	\
162 	((PCI_PRODUCT_QLOGIC_ISP2432 << 16) | PCI_VENDOR_QLOGIC)
163 #define	PCI_QLOGIC_ISP2532	\
164 	((PCI_PRODUCT_QLOGIC_ISP2532 << 16) | PCI_VENDOR_QLOGIC)
165 #define	PCI_QLOGIC_ISP5432	\
166 	((PCI_PRODUCT_QLOGIC_ISP5432 << 16) | PCI_VENDOR_QLOGIC)
167 #define	PCI_QLOGIC_ISP2031	\
168 	((PCI_PRODUCT_QLOGIC_ISP2031 << 16) | PCI_VENDOR_QLOGIC)
169 #define	PCI_QLOGIC_ISP8031	\
170 	((PCI_PRODUCT_QLOGIC_ISP8031 << 16) | PCI_VENDOR_QLOGIC)
171 #define	PCI_QLOGIC_ISP2684	\
172 	((PCI_PRODUCT_QLOGIC_ISP2684 << 16) | PCI_VENDOR_QLOGIC)
173 #define	PCI_QLOGIC_ISP2692	\
174 	((PCI_PRODUCT_QLOGIC_ISP2692 << 16) | PCI_VENDOR_QLOGIC)
175 #define	PCI_QLOGIC_ISP2714	\
176 	((PCI_PRODUCT_QLOGIC_ISP2714 << 16) | PCI_VENDOR_QLOGIC)
177 #define	PCI_QLOGIC_ISP2722	\
178 	((PCI_PRODUCT_QLOGIC_ISP2722 << 16) | PCI_VENDOR_QLOGIC)
179 #define	PCI_QLOGIC_ISP2812	\
180 	((PCI_PRODUCT_QLOGIC_ISP2812 << 16) | PCI_VENDOR_QLOGIC)
181 #define	PCI_QLOGIC_ISP2814	\
182 	((PCI_PRODUCT_QLOGIC_ISP2814 << 16) | PCI_VENDOR_QLOGIC)
183 
184 #define	PCI_DFLT_LTNCY	0x40
185 #define	PCI_DFLT_LNSZ	0x10
186 
187 static int isp_pci_probe (device_t);
188 static int isp_pci_attach (device_t);
189 static int isp_pci_detach (device_t);
190 
191 
192 struct isp_pcisoftc {
193 	ispsoftc_t			pci_isp;
194 	struct resource *		regs;
195 	struct resource *		regs1;
196 	struct resource *		regs2;
197 	struct {
198 		int				iqd;
199 		struct resource *		irq;
200 		void *				ih;
201 	} irq[ISP_MAX_IRQS];
202 	int				rtp;
203 	int				rgd;
204 	int				rtp1;
205 	int				rgd1;
206 	int				rtp2;
207 	int				rgd2;
208 	bus_dma_tag_t			dmat;
209 	int				msicount;
210 };
211 
212 
213 static device_method_t isp_pci_methods[] = {
214 	/* Device interface */
215 	DEVMETHOD(device_probe,		isp_pci_probe),
216 	DEVMETHOD(device_attach,	isp_pci_attach),
217 	DEVMETHOD(device_detach,	isp_pci_detach),
218 	{ 0, 0 }
219 };
220 
221 static driver_t isp_pci_driver = {
222 	"isp", isp_pci_methods, sizeof (struct isp_pcisoftc)
223 };
224 
225 DRIVER_MODULE(isp, pci, isp_pci_driver, 0, 0);
226 MODULE_DEPEND(isp, cam, 1, 1, 1);
227 MODULE_DEPEND(isp, firmware, 1, 1, 1);
228 static int isp_nvports = 0;
229 
230 static int
231 isp_pci_probe(device_t dev)
232 {
233 	switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
234 	case PCI_QLOGIC_ISP2422:
235 		device_set_desc(dev, "Qlogic ISP 2422 PCI FC-AL Adapter");
236 		break;
237 	case PCI_QLOGIC_ISP2432:
238 		device_set_desc(dev, "Qlogic ISP 2432 PCI FC-AL Adapter");
239 		break;
240 	case PCI_QLOGIC_ISP2532:
241 		device_set_desc(dev, "Qlogic ISP 2532 PCI FC-AL Adapter");
242 		break;
243 	case PCI_QLOGIC_ISP5432:
244 		device_set_desc(dev, "Qlogic ISP 5432 PCI FC-AL Adapter");
245 		break;
246 	case PCI_QLOGIC_ISP2031:
247 		device_set_desc(dev, "Qlogic ISP 2031 PCI FC-AL Adapter");
248 		break;
249 	case PCI_QLOGIC_ISP8031:
250 		device_set_desc(dev, "Qlogic ISP 8031 PCI FCoE Adapter");
251 		break;
252 	case PCI_QLOGIC_ISP2684:
253 		device_set_desc(dev, "Qlogic ISP 2684 PCI FC Adapter");
254 		break;
255 	case PCI_QLOGIC_ISP2692:
256 		device_set_desc(dev, "Qlogic ISP 2692 PCI FC Adapter");
257 		break;
258 	case PCI_QLOGIC_ISP2714:
259 		device_set_desc(dev, "Qlogic ISP 2714 PCI FC Adapter");
260 		break;
261 	case PCI_QLOGIC_ISP2722:
262 		device_set_desc(dev, "Qlogic ISP 2722 PCI FC Adapter");
263 		break;
264 	case PCI_QLOGIC_ISP2812:
265 		device_set_desc(dev, "Qlogic ISP 2812 PCI FC Adapter");
266 		break;
267 	case PCI_QLOGIC_ISP2814:
268 		device_set_desc(dev, "Qlogic ISP 2814 PCI FC Adapter");
269 		break;
270 	default:
271 		return (ENXIO);
272 	}
273 	if (isp_announced == 0 && bootverbose) {
274 		printf("Qlogic ISP Driver, FreeBSD Version %d.%d, "
275 		    "Core Version %d.%d\n",
276 		    ISP_PLATFORM_VERSION_MAJOR, ISP_PLATFORM_VERSION_MINOR,
277 		    ISP_CORE_VERSION_MAJOR, ISP_CORE_VERSION_MINOR);
278 		isp_announced++;
279 	}
280 	/*
281 	 * XXXX: Here is where we might load the f/w module
282 	 * XXXX: (or increase a reference count to it).
283 	 */
284 	return (BUS_PROBE_DEFAULT);
285 }
286 
287 static void
288 isp_get_generic_options(device_t dev, ispsoftc_t *isp)
289 {
290 	int tval;
291 
292 	tval = 0;
293 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "fwload_disable", &tval) == 0 && tval != 0) {
294 		isp->isp_confopts |= ISP_CFG_NORELOAD;
295 	}
296 	tval = 0;
297 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "ignore_nvram", &tval) == 0 && tval != 0) {
298 		isp->isp_confopts |= ISP_CFG_NONVRAM;
299 	}
300 	tval = 0;
301 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "debug", &tval);
302 	if (tval) {
303 		isp->isp_dblev = tval;
304 	} else {
305 		isp->isp_dblev = ISP_LOGWARN|ISP_LOGERR;
306 	}
307 	if (bootverbose) {
308 		isp->isp_dblev |= ISP_LOGCONFIG|ISP_LOGINFO;
309 	}
310 	tval = -1;
311 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "vports", &tval);
312 	if (tval > 0 && tval <= 254) {
313 		isp_nvports = tval;
314 	}
315 	tval = 7;
316 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "quickboot_time", &tval);
317 	isp_quickboot_time = tval;
318 }
319 
320 static void
321 isp_get_specific_options(device_t dev, int chan, ispsoftc_t *isp)
322 {
323 	const char *sptr;
324 	int tval = 0;
325 	char prefix[12], name[16];
326 
327 	if (chan == 0)
328 		prefix[0] = 0;
329 	else
330 		snprintf(prefix, sizeof(prefix), "chan%d.", chan);
331 	snprintf(name, sizeof(name), "%siid", prefix);
332 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
333 	    name, &tval)) {
334 		ISP_FC_PC(isp, chan)->default_id = 109 - chan;
335 	} else {
336 		ISP_FC_PC(isp, chan)->default_id = tval - chan;
337 		isp->isp_confopts |= ISP_CFG_OWNLOOPID;
338 	}
339 
340 	tval = -1;
341 	snprintf(name, sizeof(name), "%srole", prefix);
342 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
343 	    name, &tval) == 0) {
344 		switch (tval) {
345 		case ISP_ROLE_NONE:
346 		case ISP_ROLE_INITIATOR:
347 		case ISP_ROLE_TARGET:
348 		case ISP_ROLE_BOTH:
349 			device_printf(dev, "Chan %d setting role to 0x%x\n", chan, tval);
350 			break;
351 		default:
352 			tval = -1;
353 			break;
354 		}
355 	}
356 	if (tval == -1) {
357 		tval = ISP_DEFAULT_ROLES;
358 	}
359 	ISP_FC_PC(isp, chan)->def_role = tval;
360 
361 	tval = 0;
362 	snprintf(name, sizeof(name), "%sfullduplex", prefix);
363 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
364 	    name, &tval) == 0 && tval != 0) {
365 		isp->isp_confopts |= ISP_CFG_FULL_DUPLEX;
366 	}
367 	sptr = NULL;
368 	snprintf(name, sizeof(name), "%stopology", prefix);
369 	if (resource_string_value(device_get_name(dev), device_get_unit(dev),
370 	    name, (const char **) &sptr) == 0 && sptr != NULL) {
371 		if (strcmp(sptr, "lport") == 0) {
372 			isp->isp_confopts |= ISP_CFG_LPORT;
373 		} else if (strcmp(sptr, "nport") == 0) {
374 			isp->isp_confopts |= ISP_CFG_NPORT;
375 		} else if (strcmp(sptr, "lport-only") == 0) {
376 			isp->isp_confopts |= ISP_CFG_LPORT_ONLY;
377 		} else if (strcmp(sptr, "nport-only") == 0) {
378 			isp->isp_confopts |= ISP_CFG_NPORT_ONLY;
379 		}
380 	}
381 
382 #ifdef ISP_FCTAPE_OFF
383 	isp->isp_confopts |= ISP_CFG_NOFCTAPE;
384 #else
385 	isp->isp_confopts |= ISP_CFG_FCTAPE;
386 #endif
387 
388 	tval = 0;
389 	snprintf(name, sizeof(name), "%snofctape", prefix);
390 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
391 	    name, &tval);
392 	if (tval) {
393 		isp->isp_confopts &= ~ISP_CFG_FCTAPE;
394 		isp->isp_confopts |= ISP_CFG_NOFCTAPE;
395 	}
396 
397 	tval = 0;
398 	snprintf(name, sizeof(name), "%sfctape", prefix);
399 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
400 	    name, &tval);
401 	if (tval) {
402 		isp->isp_confopts &= ~ISP_CFG_NOFCTAPE;
403 		isp->isp_confopts |= ISP_CFG_FCTAPE;
404 	}
405 
406 
407 	/*
408 	 * Because the resource_*_value functions can neither return
409 	 * 64 bit integer values, nor can they be directly coerced
410 	 * to interpret the right hand side of the assignment as
411 	 * you want them to interpret it, we have to force WWN
412 	 * hint replacement to specify WWN strings with a leading
413 	 * 'w' (e..g w50000000aaaa0001). Sigh.
414 	 */
415 	sptr = NULL;
416 	snprintf(name, sizeof(name), "%sportwwn", prefix);
417 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
418 	    name, (const char **) &sptr);
419 	if (tval == 0 && sptr != NULL && *sptr++ == 'w') {
420 		char *eptr = NULL;
421 		ISP_FC_PC(isp, chan)->def_wwpn = strtouq(sptr, &eptr, 16);
422 		if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwpn == -1) {
423 			device_printf(dev, "mangled portwwn hint '%s'\n", sptr);
424 			ISP_FC_PC(isp, chan)->def_wwpn = 0;
425 		}
426 	}
427 
428 	sptr = NULL;
429 	snprintf(name, sizeof(name), "%snodewwn", prefix);
430 	tval = resource_string_value(device_get_name(dev), device_get_unit(dev),
431 	    name, (const char **) &sptr);
432 	if (tval == 0 && sptr != NULL && *sptr++ == 'w') {
433 		char *eptr = NULL;
434 		ISP_FC_PC(isp, chan)->def_wwnn = strtouq(sptr, &eptr, 16);
435 		if (eptr < sptr + 16 || ISP_FC_PC(isp, chan)->def_wwnn == 0) {
436 			device_printf(dev, "mangled nodewwn hint '%s'\n", sptr);
437 			ISP_FC_PC(isp, chan)->def_wwnn = 0;
438 		}
439 	}
440 
441 	tval = -1;
442 	snprintf(name, sizeof(name), "%sloop_down_limit", prefix);
443 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
444 	    name, &tval);
445 	if (tval >= 0 && tval < 0xffff) {
446 		ISP_FC_PC(isp, chan)->loop_down_limit = tval;
447 	} else {
448 		ISP_FC_PC(isp, chan)->loop_down_limit = isp_loop_down_limit;
449 	}
450 
451 	tval = -1;
452 	snprintf(name, sizeof(name), "%sgone_device_time", prefix);
453 	(void) resource_int_value(device_get_name(dev), device_get_unit(dev),
454 	    name, &tval);
455 	if (tval >= 0 && tval < 0xffff) {
456 		ISP_FC_PC(isp, chan)->gone_device_time = tval;
457 	} else {
458 		ISP_FC_PC(isp, chan)->gone_device_time = isp_gone_device_time;
459 	}
460 }
461 
462 static int
463 isp_pci_attach(device_t dev)
464 {
465 	struct isp_pcisoftc *pcs = device_get_softc(dev);
466 	ispsoftc_t *isp = &pcs->pci_isp;
467 	int i;
468 	uint32_t data, cmd, linesz, did;
469 	size_t psize, xsize;
470 	char fwname[32];
471 
472 	isp->isp_dev = dev;
473 	isp->isp_nchan = 1;
474 	mtx_init(&isp->isp_lock, "isp", NULL, MTX_DEF);
475 
476 	/*
477 	 * Get Generic Options
478 	 */
479 	isp_nvports = 0;
480 	isp_get_generic_options(dev, isp);
481 
482 	linesz = PCI_DFLT_LNSZ;
483 	pcs->regs = pcs->regs2 = NULL;
484 	pcs->rgd = pcs->rtp = 0;
485 
486 	isp->isp_nchan += isp_nvports;
487 	switch (pci_get_devid(dev)) {
488 	case PCI_QLOGIC_ISP2422:
489 	case PCI_QLOGIC_ISP2432:
490 		did = 0x2400;
491 		isp->isp_mdvec = &mdvec_2400;
492 		isp->isp_type = ISP_HA_FC_2400;
493 		break;
494 	case PCI_QLOGIC_ISP2532:
495 		did = 0x2500;
496 		isp->isp_mdvec = &mdvec_2500;
497 		isp->isp_type = ISP_HA_FC_2500;
498 		break;
499 	case PCI_QLOGIC_ISP5432:
500 		did = 0x2500;
501 		isp->isp_mdvec = &mdvec_2500;
502 		isp->isp_type = ISP_HA_FC_2500;
503 		break;
504 	case PCI_QLOGIC_ISP2031:
505 	case PCI_QLOGIC_ISP8031:
506 		did = 0x2600;
507 		isp->isp_mdvec = &mdvec_2600;
508 		isp->isp_type = ISP_HA_FC_2600;
509 		break;
510 	case PCI_QLOGIC_ISP2684:
511 	case PCI_QLOGIC_ISP2692:
512 	case PCI_QLOGIC_ISP2714:
513 	case PCI_QLOGIC_ISP2722:
514 		did = 0x2700;
515 		isp->isp_mdvec = &mdvec_2700;
516 		isp->isp_type = ISP_HA_FC_2700;
517 		break;
518 	case PCI_QLOGIC_ISP2812:
519 	case PCI_QLOGIC_ISP2814:
520 		did = 0x2800;
521 		isp->isp_mdvec = &mdvec_2800;
522 		isp->isp_type = ISP_HA_FC_2800;
523 		break;
524 	default:
525 		device_printf(dev, "unknown device type\n");
526 		goto bad;
527 		break;
528 	}
529 	isp->isp_revision = pci_get_revid(dev);
530 
531 	if (IS_26XX(isp)) {
532 		pcs->rtp = SYS_RES_MEMORY;
533 		pcs->rgd = PCIR_BAR(0);
534 		pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd,
535 		    RF_ACTIVE);
536 		pcs->rtp1 = SYS_RES_MEMORY;
537 		pcs->rgd1 = PCIR_BAR(2);
538 		pcs->regs1 = bus_alloc_resource_any(dev, pcs->rtp1, &pcs->rgd1,
539 		    RF_ACTIVE);
540 		pcs->rtp2 = SYS_RES_MEMORY;
541 		pcs->rgd2 = PCIR_BAR(4);
542 		pcs->regs2 = bus_alloc_resource_any(dev, pcs->rtp2, &pcs->rgd2,
543 		    RF_ACTIVE);
544 	} else {
545 		pcs->rtp = SYS_RES_MEMORY;
546 		pcs->rgd = PCIR_BAR(1);
547 		pcs->regs = bus_alloc_resource_any(dev, pcs->rtp, &pcs->rgd,
548 		    RF_ACTIVE);
549 		if (pcs->regs == NULL) {
550 			pcs->rtp = SYS_RES_IOPORT;
551 			pcs->rgd = PCIR_BAR(0);
552 			pcs->regs = bus_alloc_resource_any(dev, pcs->rtp,
553 			    &pcs->rgd, RF_ACTIVE);
554 		}
555 	}
556 	if (pcs->regs == NULL) {
557 		device_printf(dev, "Unable to map any ports\n");
558 		goto bad;
559 	}
560 	if (bootverbose) {
561 		device_printf(dev, "Using %s space register mapping\n",
562 		    (pcs->rtp == SYS_RES_IOPORT)? "I/O" : "Memory");
563 	}
564 	isp->isp_regs = pcs->regs;
565 	isp->isp_regs2 = pcs->regs2;
566 
567 	psize = sizeof(fcparam) * isp->isp_nchan;
568 	xsize = sizeof(struct isp_fc) * isp->isp_nchan;
569 	isp->isp_param = malloc(psize, M_DEVBUF, M_NOWAIT | M_ZERO);
570 	if (isp->isp_param == NULL) {
571 		device_printf(dev, "cannot allocate parameter data\n");
572 		goto bad;
573 	}
574 	isp->isp_osinfo.fc = malloc(xsize, M_DEVBUF, M_NOWAIT | M_ZERO);
575 	if (isp->isp_osinfo.fc == NULL) {
576 		device_printf(dev, "cannot allocate parameter data\n");
577 		goto bad;
578 	}
579 
580 	/*
581 	 * Now that we know who we are (roughly) get/set specific options
582 	 */
583 	for (i = 0; i < isp->isp_nchan; i++) {
584 		isp_get_specific_options(dev, i, isp);
585 	}
586 
587 	isp->isp_osinfo.fw = NULL;
588 	if (isp->isp_osinfo.fw == NULL) {
589 		snprintf(fwname, sizeof (fwname), "isp_%04x", did);
590 		isp->isp_osinfo.fw = firmware_get(fwname);
591 	}
592 	if (isp->isp_osinfo.fw != NULL) {
593 		isp_prt(isp, ISP_LOGCONFIG, "loaded firmware %s", fwname);
594 		isp->isp_mdvec->dv_ispfw = isp->isp_osinfo.fw->data;
595 	}
596 
597 	/*
598 	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set.
599 	 */
600 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
601 	cmd |= PCIM_CMD_SEREN | PCIM_CMD_PERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
602 	cmd &= ~PCIM_CMD_INTX_DISABLE;
603 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
604 
605 	/*
606 	 * Make sure the Cache Line Size register is set sensibly.
607 	 */
608 	data = pci_read_config(dev, PCIR_CACHELNSZ, 1);
609 	if (data == 0 || (linesz != PCI_DFLT_LNSZ && data != linesz)) {
610 		isp_prt(isp, ISP_LOGDEBUG0, "set PCI line size to %d from %d", linesz, data);
611 		data = linesz;
612 		pci_write_config(dev, PCIR_CACHELNSZ, data, 1);
613 	}
614 
615 	/*
616 	 * Make sure the Latency Timer is sane.
617 	 */
618 	data = pci_read_config(dev, PCIR_LATTIMER, 1);
619 	if (data < PCI_DFLT_LTNCY) {
620 		data = PCI_DFLT_LTNCY;
621 		isp_prt(isp, ISP_LOGDEBUG0, "set PCI latency to %d", data);
622 		pci_write_config(dev, PCIR_LATTIMER, data, 1);
623 	}
624 
625 	/*
626 	 * Make sure we've disabled the ROM.
627 	 */
628 	data = pci_read_config(dev, PCIR_ROMADDR, 4);
629 	data &= ~1;
630 	pci_write_config(dev, PCIR_ROMADDR, data, 4);
631 
632 	/*
633 	 * Last minute checks...
634 	 */
635 	isp->isp_port = pci_get_function(dev);
636 
637 	/*
638 	 * Make sure we're in reset state.
639 	 */
640 	ISP_LOCK(isp);
641 	if (isp_reinit(isp, 1) != 0) {
642 		ISP_UNLOCK(isp);
643 		goto bad;
644 	}
645 	ISP_UNLOCK(isp);
646 	if (isp_attach(isp)) {
647 		ISP_LOCK(isp);
648 		isp_shutdown(isp);
649 		ISP_UNLOCK(isp);
650 		goto bad;
651 	}
652 	return (0);
653 
654 bad:
655 	if (isp->isp_osinfo.fw == NULL && !IS_26XX(isp)) {
656 		/*
657 		 * Failure to attach at boot time might have been caused
658 		 * by a missing ispfw(4).  Except for 16Gb adapters,
659 		 * there's no loadable firmware for them.
660 		 */
661 		isp_prt(isp, ISP_LOGWARN, "See the ispfw(4) man page on "
662 		    "how to load known good firmware at boot time");
663 	}
664 	for (i = 0; i < isp->isp_nirq; i++) {
665 		(void) bus_teardown_intr(dev, pcs->irq[i].irq, pcs->irq[i].ih);
666 		(void) bus_release_resource(dev, SYS_RES_IRQ, pcs->irq[i].iqd,
667 		    pcs->irq[0].irq);
668 	}
669 	if (pcs->msicount) {
670 		pci_release_msi(dev);
671 	}
672 	if (pcs->regs)
673 		(void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
674 	if (pcs->regs1)
675 		(void) bus_release_resource(dev, pcs->rtp1, pcs->rgd1, pcs->regs1);
676 	if (pcs->regs2)
677 		(void) bus_release_resource(dev, pcs->rtp2, pcs->rgd2, pcs->regs2);
678 	if (pcs->pci_isp.isp_param) {
679 		free(pcs->pci_isp.isp_param, M_DEVBUF);
680 		pcs->pci_isp.isp_param = NULL;
681 	}
682 	if (pcs->pci_isp.isp_osinfo.fc) {
683 		free(pcs->pci_isp.isp_osinfo.fc, M_DEVBUF);
684 		pcs->pci_isp.isp_osinfo.fc = NULL;
685 	}
686 	mtx_destroy(&isp->isp_lock);
687 	return (ENXIO);
688 }
689 
690 static int
691 isp_pci_detach(device_t dev)
692 {
693 	struct isp_pcisoftc *pcs = device_get_softc(dev);
694 	ispsoftc_t *isp = &pcs->pci_isp;
695 	int i, status;
696 
697 	status = isp_detach(isp);
698 	if (status)
699 		return (status);
700 	ISP_LOCK(isp);
701 	isp_shutdown(isp);
702 	ISP_UNLOCK(isp);
703 	for (i = 0; i < isp->isp_nirq; i++) {
704 		(void) bus_teardown_intr(dev, pcs->irq[i].irq, pcs->irq[i].ih);
705 		(void) bus_release_resource(dev, SYS_RES_IRQ, pcs->irq[i].iqd,
706 		    pcs->irq[i].irq);
707 	}
708 	if (pcs->msicount)
709 		pci_release_msi(dev);
710 	(void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
711 	if (pcs->regs1)
712 		(void) bus_release_resource(dev, pcs->rtp1, pcs->rgd1, pcs->regs1);
713 	if (pcs->regs2)
714 		(void) bus_release_resource(dev, pcs->rtp2, pcs->rgd2, pcs->regs2);
715 	isp_pci_mbxdmafree(isp);
716 	if (pcs->pci_isp.isp_param) {
717 		free(pcs->pci_isp.isp_param, M_DEVBUF);
718 		pcs->pci_isp.isp_param = NULL;
719 	}
720 	if (pcs->pci_isp.isp_osinfo.fc) {
721 		free(pcs->pci_isp.isp_osinfo.fc, M_DEVBUF);
722 		pcs->pci_isp.isp_osinfo.fc = NULL;
723 	}
724 	mtx_destroy(&isp->isp_lock);
725 	return (0);
726 }
727 
728 #define	BXR2(isp, off)		bus_read_2((isp)->isp_regs, (off))
729 #define	BXW2(isp, off, v)	bus_write_2((isp)->isp_regs, (off), (v))
730 #define	BXR4(isp, off)		bus_read_4((isp)->isp_regs, (off))
731 #define	BXW4(isp, off, v)	bus_write_4((isp)->isp_regs, (off), (v))
732 #define	B2R4(isp, off)		bus_read_4((isp)->isp_regs2, (off))
733 #define	B2W4(isp, off, v)	bus_write_4((isp)->isp_regs2, (off), (v))
734 
735 static void
736 isp_pci_run_isr_2400(ispsoftc_t *isp)
737 {
738 	uint32_t r2hisr;
739 	uint16_t isr, info;
740 
741 	r2hisr = BXR4(isp, BIU2400_R2HSTS);
742 	isp_prt(isp, ISP_LOGDEBUG3, "RISC2HOST ISR 0x%x", r2hisr);
743 	if ((r2hisr & BIU_R2HST_INTR) == 0)
744 		return;
745 	isr = r2hisr & BIU_R2HST_ISTAT_MASK;
746 	info = (r2hisr >> 16);
747 	switch (isr) {
748 	case ISPR2HST_ROM_MBX_OK:
749 	case ISPR2HST_ROM_MBX_FAIL:
750 	case ISPR2HST_MBX_OK:
751 	case ISPR2HST_MBX_FAIL:
752 		isp_intr_mbox(isp, info);
753 		break;
754 	case ISPR2HST_ASYNC_EVENT:
755 		isp_intr_async(isp, info);
756 		break;
757 	case ISPR2HST_RSPQ_UPDATE:
758 		isp_intr_respq(isp);
759 		break;
760 	case ISPR2HST_RSPQ_UPDATE2:
761 #ifdef	ISP_TARGET_MODE
762 	case ISPR2HST_ATIO_RSPQ_UPDATE:
763 #endif
764 		isp_intr_respq(isp);
765 		/* FALLTHROUGH */
766 #ifdef	ISP_TARGET_MODE
767 	case ISPR2HST_ATIO_UPDATE:
768 	case ISPR2HST_ATIO_UPDATE2:
769 		isp_intr_atioq(isp);
770 #endif
771 		break;
772 	default:
773 		isp_prt(isp, ISP_LOGERR, "unknown interrupt 0x%x\n", r2hisr);
774 	}
775 	ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
776 }
777 
778 static uint32_t
779 isp_pci_rd_reg_2400(ispsoftc_t *isp, int regoff)
780 {
781 	int block = regoff & _BLK_REG_MASK;
782 
783 	switch (block) {
784 	case BIU_BLOCK:
785 		return (BXR4(isp, regoff));
786 	case MBOX_BLOCK:
787 		return (BXR2(isp, regoff));
788 	}
789 	isp_prt(isp, ISP_LOGERR, "unknown block read at 0x%x", regoff);
790 	return (0xffffffff);
791 }
792 
793 static void
794 isp_pci_wr_reg_2400(ispsoftc_t *isp, int regoff, uint32_t val)
795 {
796 	int block = regoff & _BLK_REG_MASK;
797 
798 	switch (block) {
799 	case BIU_BLOCK:
800 		BXW4(isp, regoff, val);
801 #ifdef MEMORYBARRIERW
802 		if (regoff == BIU2400_REQINP ||
803 		    regoff == BIU2400_RSPOUTP ||
804 		    regoff == BIU2400_PRI_REQINP ||
805 		    regoff == BIU2400_ATIO_RSPOUTP)
806 			MEMORYBARRIERW(isp, SYNC_REG, regoff, 4, -1)
807 		else
808 #endif
809 		MEMORYBARRIER(isp, SYNC_REG, regoff, 4, -1);
810 		return;
811 	case MBOX_BLOCK:
812 		BXW2(isp, regoff, val);
813 		MEMORYBARRIER(isp, SYNC_REG, regoff, 2, -1);
814 		return;
815 	}
816 	isp_prt(isp, ISP_LOGERR, "unknown block write at 0x%x", regoff);
817 }
818 
819 static uint32_t
820 isp_pci_rd_reg_2600(ispsoftc_t *isp, int regoff)
821 {
822 	uint32_t rv;
823 
824 	switch (regoff) {
825 	case BIU2400_PRI_REQINP:
826 	case BIU2400_PRI_REQOUTP:
827 		isp_prt(isp, ISP_LOGERR, "unknown register read at 0x%x",
828 		    regoff);
829 		rv = 0xffffffff;
830 		break;
831 	case BIU2400_REQINP:
832 		rv = B2R4(isp, 0x00);
833 		break;
834 	case BIU2400_REQOUTP:
835 		rv = B2R4(isp, 0x04);
836 		break;
837 	case BIU2400_RSPINP:
838 		rv = B2R4(isp, 0x08);
839 		break;
840 	case BIU2400_RSPOUTP:
841 		rv = B2R4(isp, 0x0c);
842 		break;
843 	case BIU2400_ATIO_RSPINP:
844 		rv = B2R4(isp, 0x10);
845 		break;
846 	case BIU2400_ATIO_RSPOUTP:
847 		rv = B2R4(isp, 0x14);
848 		break;
849 	default:
850 		rv = isp_pci_rd_reg_2400(isp, regoff);
851 		break;
852 	}
853 	return (rv);
854 }
855 
856 static void
857 isp_pci_wr_reg_2600(ispsoftc_t *isp, int regoff, uint32_t val)
858 {
859 	int off;
860 
861 	switch (regoff) {
862 	case BIU2400_PRI_REQINP:
863 	case BIU2400_PRI_REQOUTP:
864 		isp_prt(isp, ISP_LOGERR, "unknown register write at 0x%x",
865 		    regoff);
866 		return;
867 	case BIU2400_REQINP:
868 		off = 0x00;
869 		break;
870 	case BIU2400_REQOUTP:
871 		off = 0x04;
872 		break;
873 	case BIU2400_RSPINP:
874 		off = 0x08;
875 		break;
876 	case BIU2400_RSPOUTP:
877 		off = 0x0c;
878 		break;
879 	case BIU2400_ATIO_RSPINP:
880 		off = 0x10;
881 		break;
882 	case BIU2400_ATIO_RSPOUTP:
883 		off = 0x14;
884 		break;
885 	default:
886 		isp_pci_wr_reg_2400(isp, regoff, val);
887 		return;
888 	}
889 	B2W4(isp, off, val);
890 }
891 
892 
893 struct imush {
894 	bus_addr_t maddr;
895 	int error;
896 };
897 
898 static void
899 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
900 {
901 	struct imush *imushp = (struct imush *) arg;
902 
903 	if (!(imushp->error = error))
904 		imushp->maddr = segs[0].ds_addr;
905 }
906 
907 static int
908 isp_pci_mbxdma(ispsoftc_t *isp)
909 {
910 	bus_dma_tag_t ptag;
911 	caddr_t base;
912 	uint32_t len;
913 	int i, error, cmap;
914 	bus_size_t slim;	/* segment size */
915 	struct imush im;
916 #ifdef	ISP_TARGET_MODE
917 	isp_ecmd_t *ecmd;
918 #endif
919 
920 	/* Already been here? If so, leave... */
921 	if (isp->isp_xflist != NULL)
922 		return (0);
923 	if (isp->isp_rquest != NULL && isp->isp_maxcmds == 0)
924 		return (0);
925 	ISP_UNLOCK(isp);
926 
927 	ptag = bus_get_dma_tag(isp->isp_osinfo.dev);
928 	if (sizeof (bus_size_t) > 4)
929 		slim = (bus_size_t) (1ULL << 32);
930 	else
931 		slim = (bus_size_t) (1UL << 31);
932 
933 	if (isp->isp_rquest != NULL)
934 		goto gotmaxcmds;
935 
936 	/*
937 	 * Allocate and map the request queue.
938 	 */
939 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
940 	if (bus_dma_tag_create(ptag, QENTRY_LEN, slim,
941 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
942 	    len, 1, len, 0, NULL, NULL, &isp->isp_osinfo.reqdmat)) {
943 		isp_prt(isp, ISP_LOGERR, "cannot create request DMA tag");
944 		goto bad;
945 	}
946 	if (bus_dmamem_alloc(isp->isp_osinfo.reqdmat, (void **)&base,
947 	    BUS_DMA_COHERENT, &isp->isp_osinfo.reqmap) != 0) {
948 		isp_prt(isp, ISP_LOGERR, "cannot allocate request DMA memory");
949 		bus_dma_tag_destroy(isp->isp_osinfo.reqdmat);
950 		goto bad;
951 	}
952 	isp->isp_rquest = base;
953 	im.error = 0;
954 	if (bus_dmamap_load(isp->isp_osinfo.reqdmat, isp->isp_osinfo.reqmap,
955 	    base, len, imc, &im, BUS_DMA_NOWAIT) || im.error) {
956 		isp_prt(isp, ISP_LOGERR, "error loading request DMA map %d", im.error);
957 		goto bad;
958 	}
959 	isp_prt(isp, ISP_LOGDEBUG0, "request area @ 0x%jx/0x%jx",
960 	    (uintmax_t)im.maddr, (uintmax_t)len);
961 	isp->isp_rquest_dma = im.maddr;
962 
963 #ifdef	ISP_TARGET_MODE
964 	/*
965 	 * Allocate region for external DMA addressable command/status structures.
966 	 */
967 	len = N_XCMDS * XCMD_SIZE;
968 	if (bus_dma_tag_create(ptag, XCMD_SIZE, slim,
969 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
970 	    len, 1, len, 0, NULL, NULL, &isp->isp_osinfo.ecmd_dmat)) {
971 		isp_prt(isp, ISP_LOGERR, "cannot create ECMD DMA tag");
972 		goto bad;
973 	}
974 	if (bus_dmamem_alloc(isp->isp_osinfo.ecmd_dmat, (void **)&base,
975 	    BUS_DMA_COHERENT, &isp->isp_osinfo.ecmd_map) != 0) {
976 		isp_prt(isp, ISP_LOGERR, "cannot allocate ECMD DMA memory");
977 		bus_dma_tag_destroy(isp->isp_osinfo.ecmd_dmat);
978 		goto bad;
979 	}
980 	isp->isp_osinfo.ecmd_base = (isp_ecmd_t *)base;
981 	im.error = 0;
982 	if (bus_dmamap_load(isp->isp_osinfo.ecmd_dmat, isp->isp_osinfo.ecmd_map,
983 	    base, len, imc, &im, BUS_DMA_NOWAIT) || im.error) {
984 		isp_prt(isp, ISP_LOGERR, "error loading ECMD DMA map %d", im.error);
985 		goto bad;
986 	}
987 	isp_prt(isp, ISP_LOGDEBUG0, "ecmd area @ 0x%jx/0x%jx",
988 	    (uintmax_t)im.maddr, (uintmax_t)len);
989 
990 	isp->isp_osinfo.ecmd_dma = im.maddr;
991 	isp->isp_osinfo.ecmd_free = (isp_ecmd_t *)base;
992 	for (ecmd = isp->isp_osinfo.ecmd_free;
993 	    ecmd < &isp->isp_osinfo.ecmd_free[N_XCMDS]; ecmd++) {
994 		if (ecmd == &isp->isp_osinfo.ecmd_free[N_XCMDS - 1])
995 			ecmd->next = NULL;
996 		else
997 			ecmd->next = ecmd + 1;
998 	}
999 #endif
1000 
1001 	/*
1002 	 * Allocate and map the result queue.
1003 	 */
1004 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
1005 	if (bus_dma_tag_create(ptag, QENTRY_LEN, slim,
1006 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1007 	    len, 1, len, 0, NULL, NULL, &isp->isp_osinfo.respdmat)) {
1008 		isp_prt(isp, ISP_LOGERR, "cannot create response DMA tag");
1009 		goto bad;
1010 	}
1011 	if (bus_dmamem_alloc(isp->isp_osinfo.respdmat, (void **)&base,
1012 	    BUS_DMA_COHERENT, &isp->isp_osinfo.respmap) != 0) {
1013 		isp_prt(isp, ISP_LOGERR, "cannot allocate response DMA memory");
1014 		bus_dma_tag_destroy(isp->isp_osinfo.respdmat);
1015 		goto bad;
1016 	}
1017 	isp->isp_result = base;
1018 	im.error = 0;
1019 	if (bus_dmamap_load(isp->isp_osinfo.respdmat, isp->isp_osinfo.respmap,
1020 	    base, len, imc, &im, BUS_DMA_NOWAIT) || im.error) {
1021 		isp_prt(isp, ISP_LOGERR, "error loading response DMA map %d", im.error);
1022 		goto bad;
1023 	}
1024 	isp_prt(isp, ISP_LOGDEBUG0, "response area @ 0x%jx/0x%jx",
1025 	    (uintmax_t)im.maddr, (uintmax_t)len);
1026 	isp->isp_result_dma = im.maddr;
1027 
1028 #ifdef	ISP_TARGET_MODE
1029 	/*
1030 	 * Allocate and map ATIO queue.
1031 	 */
1032 	len = ISP_QUEUE_SIZE(ATIO_QUEUE_LEN(isp));
1033 	if (bus_dma_tag_create(ptag, QENTRY_LEN, slim,
1034 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1035 	    len, 1, len, 0, NULL, NULL, &isp->isp_osinfo.atiodmat)) {
1036 		isp_prt(isp, ISP_LOGERR, "cannot create ATIO DMA tag");
1037 		goto bad;
1038 	}
1039 	if (bus_dmamem_alloc(isp->isp_osinfo.atiodmat, (void **)&base,
1040 	    BUS_DMA_COHERENT, &isp->isp_osinfo.atiomap) != 0) {
1041 		isp_prt(isp, ISP_LOGERR, "cannot allocate ATIO DMA memory");
1042 		bus_dma_tag_destroy(isp->isp_osinfo.atiodmat);
1043 		goto bad;
1044 	}
1045 	isp->isp_atioq = base;
1046 	im.error = 0;
1047 	if (bus_dmamap_load(isp->isp_osinfo.atiodmat, isp->isp_osinfo.atiomap,
1048 	    base, len, imc, &im, BUS_DMA_NOWAIT) || im.error) {
1049 		isp_prt(isp, ISP_LOGERR, "error loading ATIO DMA map %d", im.error);
1050 		goto bad;
1051 	}
1052 	isp_prt(isp, ISP_LOGDEBUG0, "ATIO area @ 0x%jx/0x%jx",
1053 	    (uintmax_t)im.maddr, (uintmax_t)len);
1054 	isp->isp_atioq_dma = im.maddr;
1055 #endif
1056 
1057 	if (bus_dma_tag_create(ptag, 64, slim,
1058 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1059 	    2*QENTRY_LEN, 1, 2*QENTRY_LEN, 0, NULL, NULL,
1060 	    &isp->isp_osinfo.iocbdmat)) {
1061 		goto bad;
1062 	}
1063 	if (bus_dmamem_alloc(isp->isp_osinfo.iocbdmat,
1064 	    (void **)&base, BUS_DMA_COHERENT, &isp->isp_osinfo.iocbmap) != 0)
1065 		goto bad;
1066 	isp->isp_iocb = base;
1067 	im.error = 0;
1068 	if (bus_dmamap_load(isp->isp_osinfo.iocbdmat, isp->isp_osinfo.iocbmap,
1069 	    base, 2*QENTRY_LEN, imc, &im, BUS_DMA_NOWAIT) || im.error)
1070 		goto bad;
1071 	isp->isp_iocb_dma = im.maddr;
1072 
1073 	if (bus_dma_tag_create(ptag, 64, slim,
1074 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1075 	    ISP_FC_SCRLEN, 1, ISP_FC_SCRLEN, 0, NULL, NULL,
1076 	    &isp->isp_osinfo.scdmat))
1077 		goto bad;
1078 	for (cmap = 0; cmap < isp->isp_nchan; cmap++) {
1079 		struct isp_fc *fc = ISP_FC_PC(isp, cmap);
1080 		if (bus_dmamem_alloc(isp->isp_osinfo.scdmat,
1081 		    (void **)&base, BUS_DMA_COHERENT, &fc->scmap) != 0)
1082 			goto bad;
1083 		FCPARAM(isp, cmap)->isp_scratch = base;
1084 		im.error = 0;
1085 		if (bus_dmamap_load(isp->isp_osinfo.scdmat, fc->scmap,
1086 		    base, ISP_FC_SCRLEN, imc, &im, BUS_DMA_NOWAIT) ||
1087 		    im.error) {
1088 			bus_dmamem_free(isp->isp_osinfo.scdmat,
1089 			    base, fc->scmap);
1090 			FCPARAM(isp, cmap)->isp_scratch = NULL;
1091 			goto bad;
1092 		}
1093 		FCPARAM(isp, cmap)->isp_scdma = im.maddr;
1094 		for (i = 0; i < INITIAL_NEXUS_COUNT; i++) {
1095 			struct isp_nexus *n = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_NOWAIT | M_ZERO);
1096 			if (n == NULL) {
1097 				while (fc->nexus_free_list) {
1098 					n = fc->nexus_free_list;
1099 					fc->nexus_free_list = n->next;
1100 					free(n, M_DEVBUF);
1101 				}
1102 				goto bad;
1103 			}
1104 			n->next = fc->nexus_free_list;
1105 			fc->nexus_free_list = n;
1106 		}
1107 	}
1108 
1109 	if (isp->isp_maxcmds == 0) {
1110 		ISP_LOCK(isp);
1111 		return (0);
1112 	}
1113 
1114 gotmaxcmds:
1115 	if (bus_dma_tag_create(ptag, 1, slim,
1116 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1117 	    (ISP_NSEG64_MAX - 1) * PAGE_SIZE, ISP_NSEG64_MAX,
1118 	    (ISP_NSEG64_MAX - 1) * PAGE_SIZE, 0,
1119 	    busdma_lock_mutex, &isp->isp_lock, &isp->isp_osinfo.dmat))
1120 		goto bad;
1121 	len = isp->isp_maxcmds * sizeof (struct isp_pcmd);
1122 	isp->isp_osinfo.pcmd_pool = (struct isp_pcmd *)
1123 	    malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1124 	for (i = 0; i < isp->isp_maxcmds; i++) {
1125 		struct isp_pcmd *pcmd = &isp->isp_osinfo.pcmd_pool[i];
1126 		error = bus_dmamap_create(isp->isp_osinfo.dmat, 0, &pcmd->dmap);
1127 		if (error) {
1128 			isp_prt(isp, ISP_LOGERR, "error %d creating per-cmd DMA maps", error);
1129 			while (--i >= 0) {
1130 				bus_dmamap_destroy(isp->isp_osinfo.dmat,
1131 				    isp->isp_osinfo.pcmd_pool[i].dmap);
1132 			}
1133 			goto bad;
1134 		}
1135 		callout_init_mtx(&pcmd->wdog, &isp->isp_lock, 0);
1136 		if (i == isp->isp_maxcmds-1)
1137 			pcmd->next = NULL;
1138 		else
1139 			pcmd->next = &isp->isp_osinfo.pcmd_pool[i+1];
1140 	}
1141 	isp->isp_osinfo.pcmd_free = &isp->isp_osinfo.pcmd_pool[0];
1142 
1143 	len = sizeof(isp_hdl_t) * ISP_HANDLE_NUM(isp);
1144 	isp->isp_xflist = (isp_hdl_t *) malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1145 	for (len = 0; len < ISP_HANDLE_NUM(isp) - 1; len++)
1146 		isp->isp_xflist[len].cmd = &isp->isp_xflist[len+1];
1147 	isp->isp_xffree = isp->isp_xflist;
1148 
1149 	ISP_LOCK(isp);
1150 	return (0);
1151 
1152 bad:
1153 	isp_pci_mbxdmafree(isp);
1154 	ISP_LOCK(isp);
1155 	return (1);
1156 }
1157 
1158 static void
1159 isp_pci_mbxdmafree(ispsoftc_t *isp)
1160 {
1161 	int i;
1162 
1163 	if (isp->isp_xflist != NULL) {
1164 		free(isp->isp_xflist, M_DEVBUF);
1165 		isp->isp_xflist = NULL;
1166 	}
1167 	if (isp->isp_osinfo.pcmd_pool != NULL) {
1168 		for (i = 0; i < isp->isp_maxcmds; i++) {
1169 			bus_dmamap_destroy(isp->isp_osinfo.dmat,
1170 			    isp->isp_osinfo.pcmd_pool[i].dmap);
1171 		}
1172 		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
1173 		isp->isp_osinfo.pcmd_pool = NULL;
1174 	}
1175 	if (isp->isp_osinfo.dmat) {
1176 		bus_dma_tag_destroy(isp->isp_osinfo.dmat);
1177 		isp->isp_osinfo.dmat = NULL;
1178 	}
1179 	for (i = 0; i < isp->isp_nchan; i++) {
1180 		struct isp_fc *fc = ISP_FC_PC(isp, i);
1181 		if (FCPARAM(isp, i)->isp_scdma != 0) {
1182 			bus_dmamap_unload(isp->isp_osinfo.scdmat,
1183 			    fc->scmap);
1184 			FCPARAM(isp, i)->isp_scdma = 0;
1185 		}
1186 		if (FCPARAM(isp, i)->isp_scratch != NULL) {
1187 			bus_dmamem_free(isp->isp_osinfo.scdmat,
1188 			    FCPARAM(isp, i)->isp_scratch, fc->scmap);
1189 			FCPARAM(isp, i)->isp_scratch = NULL;
1190 		}
1191 		while (fc->nexus_free_list) {
1192 			struct isp_nexus *n = fc->nexus_free_list;
1193 			fc->nexus_free_list = n->next;
1194 			free(n, M_DEVBUF);
1195 		}
1196 	}
1197 	if (isp->isp_osinfo.scdmat) {
1198 		bus_dma_tag_destroy(isp->isp_osinfo.scdmat);
1199 		isp->isp_osinfo.scdmat = NULL;
1200 	}
1201 	if (isp->isp_iocb_dma != 0) {
1202 		bus_dmamap_unload(isp->isp_osinfo.iocbdmat,
1203 		    isp->isp_osinfo.iocbmap);
1204 		isp->isp_iocb_dma = 0;
1205 	}
1206 	if (isp->isp_iocb != NULL) {
1207 		bus_dmamem_free(isp->isp_osinfo.iocbdmat,
1208 		    isp->isp_iocb, isp->isp_osinfo.iocbmap);
1209 		bus_dma_tag_destroy(isp->isp_osinfo.iocbdmat);
1210 	}
1211 #ifdef	ISP_TARGET_MODE
1212 	if (isp->isp_atioq_dma != 0) {
1213 		bus_dmamap_unload(isp->isp_osinfo.atiodmat,
1214 		    isp->isp_osinfo.atiomap);
1215 		isp->isp_atioq_dma = 0;
1216 	}
1217 	if (isp->isp_atioq != NULL) {
1218 		bus_dmamem_free(isp->isp_osinfo.atiodmat, isp->isp_atioq,
1219 		    isp->isp_osinfo.atiomap);
1220 		bus_dma_tag_destroy(isp->isp_osinfo.atiodmat);
1221 		isp->isp_atioq = NULL;
1222 	}
1223 #endif
1224 	if (isp->isp_result_dma != 0) {
1225 		bus_dmamap_unload(isp->isp_osinfo.respdmat,
1226 		    isp->isp_osinfo.respmap);
1227 		isp->isp_result_dma = 0;
1228 	}
1229 	if (isp->isp_result != NULL) {
1230 		bus_dmamem_free(isp->isp_osinfo.respdmat, isp->isp_result,
1231 		    isp->isp_osinfo.respmap);
1232 		bus_dma_tag_destroy(isp->isp_osinfo.respdmat);
1233 		isp->isp_result = NULL;
1234 	}
1235 #ifdef	ISP_TARGET_MODE
1236 	if (isp->isp_osinfo.ecmd_dma != 0) {
1237 		bus_dmamap_unload(isp->isp_osinfo.ecmd_dmat,
1238 		    isp->isp_osinfo.ecmd_map);
1239 		isp->isp_osinfo.ecmd_dma = 0;
1240 	}
1241 	if (isp->isp_osinfo.ecmd_base != NULL) {
1242 		bus_dmamem_free(isp->isp_osinfo.ecmd_dmat, isp->isp_osinfo.ecmd_base,
1243 		    isp->isp_osinfo.ecmd_map);
1244 		bus_dma_tag_destroy(isp->isp_osinfo.ecmd_dmat);
1245 		isp->isp_osinfo.ecmd_base = NULL;
1246 	}
1247 #endif
1248 	if (isp->isp_rquest_dma != 0) {
1249 		bus_dmamap_unload(isp->isp_osinfo.reqdmat,
1250 		    isp->isp_osinfo.reqmap);
1251 		isp->isp_rquest_dma = 0;
1252 	}
1253 	if (isp->isp_rquest != NULL) {
1254 		bus_dmamem_free(isp->isp_osinfo.reqdmat, isp->isp_rquest,
1255 		    isp->isp_osinfo.reqmap);
1256 		bus_dma_tag_destroy(isp->isp_osinfo.reqdmat);
1257 		isp->isp_rquest = NULL;
1258 	}
1259 }
1260 
1261 static int
1262 isp_pci_irqsetup(ispsoftc_t *isp)
1263 {
1264 	device_t dev = isp->isp_osinfo.dev;
1265 	struct isp_pcisoftc *pcs = device_get_softc(dev);
1266 	driver_intr_t *f;
1267 	int i, max_irq;
1268 
1269 	/* Allocate IRQs only once. */
1270 	if (isp->isp_nirq > 0)
1271 		return (0);
1272 
1273 	ISP_UNLOCK(isp);
1274 	if (ISP_CAP_MSIX(isp)) {
1275 		max_irq = IS_26XX(isp) ? 3 : (IS_25XX(isp) ? 2 : 0);
1276 		resource_int_value(device_get_name(dev),
1277 		    device_get_unit(dev), "msix", &max_irq);
1278 		max_irq = imin(ISP_MAX_IRQS, max_irq);
1279 		pcs->msicount = imin(pci_msix_count(dev), max_irq);
1280 		if (pcs->msicount > 0 &&
1281 		    pci_alloc_msix(dev, &pcs->msicount) != 0)
1282 			pcs->msicount = 0;
1283 	}
1284 	if (pcs->msicount == 0) {
1285 		max_irq = 1;
1286 		resource_int_value(device_get_name(dev),
1287 		    device_get_unit(dev), "msi", &max_irq);
1288 		max_irq = imin(1, max_irq);
1289 		pcs->msicount = imin(pci_msi_count(dev), max_irq);
1290 		if (pcs->msicount > 0 &&
1291 		    pci_alloc_msi(dev, &pcs->msicount) != 0)
1292 			pcs->msicount = 0;
1293 	}
1294 	for (i = 0; i < MAX(1, pcs->msicount); i++) {
1295 		pcs->irq[i].iqd = i + (pcs->msicount > 0);
1296 		pcs->irq[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
1297 		    &pcs->irq[i].iqd, RF_ACTIVE | RF_SHAREABLE);
1298 		if (pcs->irq[i].irq == NULL) {
1299 			device_printf(dev, "could not allocate interrupt\n");
1300 			break;
1301 		}
1302 		if (i == 0)
1303 			f = isp_platform_intr;
1304 		else if (i == 1)
1305 			f = isp_platform_intr_resp;
1306 		else
1307 			f = isp_platform_intr_atio;
1308 		if (bus_setup_intr(dev, pcs->irq[i].irq, ISP_IFLAGS, NULL,
1309 		    f, isp, &pcs->irq[i].ih)) {
1310 			device_printf(dev, "could not setup interrupt\n");
1311 			(void) bus_release_resource(dev, SYS_RES_IRQ,
1312 			    pcs->irq[i].iqd, pcs->irq[i].irq);
1313 			break;
1314 		}
1315 		if (pcs->msicount > 1) {
1316 			bus_describe_intr(dev, pcs->irq[i].irq, pcs->irq[i].ih,
1317 			    "%d", i);
1318 		}
1319 		isp->isp_nirq = i + 1;
1320 	}
1321 	ISP_LOCK(isp);
1322 
1323 	return (isp->isp_nirq == 0);
1324 }
1325