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