xref: /freebsd/sys/dev/isp/isp.c (revision 458b591fb0ccbeaf80f7116cd145fe896cbf7421)
1 /*-
2  *  Copyright (c) 1997-2009 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  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  *  SUCH DAMAGE.
26  *
27  */
28 
29 /*
30  * Machine and OS Independent (well, as best as possible)
31  * code for the Qlogic ISP SCSI and FC-SCSI adapters.
32  */
33 
34 /*
35  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
36  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
37  * ideas dredged from the Solaris driver.
38  */
39 
40 /*
41  * Include header file appropriate for platform we're building on.
42  */
43 #ifdef	__NetBSD__
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD$");
46 #include <dev/ic/isp_netbsd.h>
47 #endif
48 #ifdef	__FreeBSD__
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
51 #include <dev/isp/isp_freebsd.h>
52 #endif
53 #ifdef	__OpenBSD__
54 #include <dev/ic/isp_openbsd.h>
55 #endif
56 #ifdef	__linux__
57 #include "isp_linux.h"
58 #endif
59 #ifdef	__svr4__
60 #include "isp_solaris.h"
61 #endif
62 
63 /*
64  * General defines
65  */
66 #define	MBOX_DELAY_COUNT	1000000 / 100
67 #define	ISP_MARK_PORTDB(a, b, c)				\
68 	do {								\
69 		isp_prt(isp, ISP_LOG_SANCFG, 				\
70 		    "Chan %d ISP_MARK_PORTDB@LINE %d", (b), __LINE__);	\
71 		isp_mark_portdb((a), (b), (c));				\
72 	} while (0)
73 
74 /*
75  * Local static data
76  */
77 static const char fconf[] = "Chan %d PortDB[%d] changed:\n current =(0x%x@0x%06x 0x%08x%08x 0x%08x%08x)\n database=(0x%x@0x%06x 0x%08x%08x 0x%08x%08x)";
78 static const char notresp[] = "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
79 static const char topology[] = "Chan %d WWPN 0x%08x%08x PortID 0x%06x handle 0x%x, Connection '%s'";
80 static const char bun[] = "bad underrun (count %d, resid %d, status %s)";
81 static const char lipd[] = "Chan %d LIP destroyed %d active commands";
82 static const char sacq[] = "unable to acquire scratch area";
83 
84 static const uint8_t alpa_map[] = {
85 	0xef, 0xe8, 0xe4, 0xe2, 0xe1, 0xe0, 0xdc, 0xda,
86 	0xd9, 0xd6, 0xd5, 0xd4, 0xd3, 0xd2, 0xd1, 0xce,
87 	0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc7, 0xc6, 0xc5,
88 	0xc3, 0xbc, 0xba, 0xb9, 0xb6, 0xb5, 0xb4, 0xb3,
89 	0xb2, 0xb1, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9,
90 	0xa7, 0xa6, 0xa5, 0xa3, 0x9f, 0x9e, 0x9d, 0x9b,
91 	0x98, 0x97, 0x90, 0x8f, 0x88, 0x84, 0x82, 0x81,
92 	0x80, 0x7c, 0x7a, 0x79, 0x76, 0x75, 0x74, 0x73,
93 	0x72, 0x71, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x69,
94 	0x67, 0x66, 0x65, 0x63, 0x5c, 0x5a, 0x59, 0x56,
95 	0x55, 0x54, 0x53, 0x52, 0x51, 0x4e, 0x4d, 0x4c,
96 	0x4b, 0x4a, 0x49, 0x47, 0x46, 0x45, 0x43, 0x3c,
97 	0x3a, 0x39, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31,
98 	0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x27, 0x26,
99 	0x25, 0x23, 0x1f, 0x1e, 0x1d, 0x1b, 0x18, 0x17,
100 	0x10, 0x0f, 0x08, 0x04, 0x02, 0x01, 0x00
101 };
102 
103 /*
104  * Local function prototypes.
105  */
106 static int isp_parse_async(ispsoftc_t *, uint16_t);
107 static int isp_parse_async_fc(ispsoftc_t *, uint16_t);
108 static int isp_handle_other_response(ispsoftc_t *, int, isphdr_t *, uint32_t *);
109 static void isp_parse_status(ispsoftc_t *, ispstatusreq_t *, XS_T *, long *); static void
110 isp_parse_status_24xx(ispsoftc_t *, isp24xx_statusreq_t *, XS_T *, long *);
111 static void isp_fastpost_complete(ispsoftc_t *, uint32_t);
112 static int isp_mbox_continue(ispsoftc_t *);
113 static void isp_scsi_init(ispsoftc_t *);
114 static void isp_scsi_channel_init(ispsoftc_t *, int);
115 static void isp_fibre_init(ispsoftc_t *);
116 static void isp_fibre_init_2400(ispsoftc_t *);
117 static void isp_mark_portdb(ispsoftc_t *, int, int);
118 static int isp_plogx(ispsoftc_t *, int, uint16_t, uint32_t, int, int);
119 static int isp_port_login(ispsoftc_t *, uint16_t, uint32_t);
120 static int isp_port_logout(ispsoftc_t *, uint16_t, uint32_t);
121 static int isp_getpdb(ispsoftc_t *, int, uint16_t, isp_pdb_t *, int);
122 static int isp_gethandles(ispsoftc_t *, int, uint16_t *, int *, int, int);
123 static void isp_dump_chip_portdb(ispsoftc_t *, int, int);
124 static uint64_t isp_get_wwn(ispsoftc_t *, int, int, int);
125 static int isp_fclink_test(ispsoftc_t *, int, int);
126 static int isp_pdb_sync(ispsoftc_t *, int);
127 static int isp_scan_loop(ispsoftc_t *, int);
128 static int isp_gid_ft_sns(ispsoftc_t *, int);
129 static int isp_gid_ft_ct_passthru(ispsoftc_t *, int);
130 static int isp_scan_fabric(ispsoftc_t *, int);
131 static int isp_login_device(ispsoftc_t *, int, uint32_t, isp_pdb_t *, uint16_t *);
132 static int isp_register_fc4_type(ispsoftc_t *, int);
133 static int isp_register_fc4_type_24xx(ispsoftc_t *, int);
134 static uint16_t isp_next_handle(ispsoftc_t *, uint16_t *);
135 static void isp_fw_state(ispsoftc_t *, int);
136 static void isp_mboxcmd_qnw(ispsoftc_t *, mbreg_t *, int);
137 static void isp_mboxcmd(ispsoftc_t *, mbreg_t *);
138 
139 static void isp_spi_update(ispsoftc_t *, int);
140 static void isp_setdfltsdparm(ispsoftc_t *);
141 static void isp_setdfltfcparm(ispsoftc_t *, int);
142 static int isp_read_nvram(ispsoftc_t *, int);
143 static int isp_read_nvram_2400(ispsoftc_t *, uint8_t *);
144 static void isp_rdnvram_word(ispsoftc_t *, int, uint16_t *);
145 static void isp_rd_2400_nvram(ispsoftc_t *, uint32_t, uint32_t *);
146 static void isp_parse_nvram_1020(ispsoftc_t *, uint8_t *);
147 static void isp_parse_nvram_1080(ispsoftc_t *, int, uint8_t *);
148 static void isp_parse_nvram_12160(ispsoftc_t *, int, uint8_t *);
149 static void isp_parse_nvram_2100(ispsoftc_t *, uint8_t *);
150 static void isp_parse_nvram_2400(ispsoftc_t *, uint8_t *);
151 
152 /*
153  * Reset Hardware.
154  *
155  * Hit the chip over the head, download new f/w if available and set it running.
156  *
157  * Locking done elsewhere.
158  */
159 
160 void
161 isp_reset(ispsoftc_t *isp, int do_load_defaults)
162 {
163 	mbreg_t mbs;
164 	char *buf;
165 	uint64_t fwt;
166 	uint32_t code_org, val;
167 	int loops, i, dodnld = 1;
168 	const char *btype = "????";
169 	static const char dcrc[] = "Downloaded RISC Code Checksum Failure";
170 
171 	isp->isp_state = ISP_NILSTATE;
172 	if (isp->isp_dead) {
173 		isp_shutdown(isp);
174 		ISP_DISABLE_INTS(isp);
175 		return;
176 	}
177 
178 	/*
179 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
180 	 * have been set in the MD code. We figure out more
181 	 * here. Possibly more refined types based upon PCI
182 	 * identification. Chip revision has been gathered.
183 	 *
184 	 * After we've fired this chip up, zero out the conf1 register
185 	 * for SCSI adapters and do other settings for the 2100.
186 	 */
187 
188 	ISP_DISABLE_INTS(isp);
189 
190 	/*
191 	 * Pick an initial maxcmds value which will be used
192 	 * to allocate xflist pointer space. It may be changed
193 	 * later by the firmware.
194 	 */
195 	if (IS_24XX(isp)) {
196 		isp->isp_maxcmds = 4096;
197 	} else if (IS_2322(isp)) {
198 		isp->isp_maxcmds = 2048;
199 	} else if (IS_23XX(isp) || IS_2200(isp)) {
200 		isp->isp_maxcmds = 1024;
201  	} else {
202 		isp->isp_maxcmds = 512;
203 	}
204 
205 	/*
206 	 * Set up DMA for the request and response queues.
207 	 *
208 	 * We do this now so we can use the request queue
209 	 * for dma to load firmware from.
210 	 */
211 	if (ISP_MBOXDMASETUP(isp) != 0) {
212 		isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
213 		return;
214 	}
215 
216 	/*
217 	 * Set up default request/response queue in-pointer/out-pointer
218 	 * register indices.
219 	 */
220 	if (IS_24XX(isp)) {
221 		isp->isp_rqstinrp = BIU2400_REQINP;
222 		isp->isp_rqstoutrp = BIU2400_REQOUTP;
223 		isp->isp_respinrp = BIU2400_RSPINP;
224 		isp->isp_respoutrp = BIU2400_RSPOUTP;
225 	} else if (IS_23XX(isp)) {
226 		isp->isp_rqstinrp = BIU_REQINP;
227 		isp->isp_rqstoutrp = BIU_REQOUTP;
228 		isp->isp_respinrp = BIU_RSPINP;
229 		isp->isp_respoutrp = BIU_RSPOUTP;
230 	} else {
231 		isp->isp_rqstinrp = INMAILBOX4;
232 		isp->isp_rqstoutrp = OUTMAILBOX4;
233 		isp->isp_respinrp = OUTMAILBOX5;
234 		isp->isp_respoutrp = INMAILBOX5;
235 	}
236 
237 	/*
238 	 * Put the board into PAUSE mode (so we can read the SXP registers
239 	 * or write FPM/FBM registers).
240 	 */
241 	if (IS_24XX(isp)) {
242 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_HOST_INT);
243 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
244 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_PAUSE);
245 	} else {
246 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
247 	}
248 
249 	if (IS_FC(isp)) {
250 		switch (isp->isp_type) {
251 		case ISP_HA_FC_2100:
252 			btype = "2100";
253 			break;
254 		case ISP_HA_FC_2200:
255 			btype = "2200";
256 			break;
257 		case ISP_HA_FC_2300:
258 			btype = "2300";
259 			break;
260 		case ISP_HA_FC_2312:
261 			btype = "2312";
262 			break;
263 		case ISP_HA_FC_2322:
264 			btype = "2322";
265 			break;
266 		case ISP_HA_FC_2400:
267 			btype = "2422";
268 			break;
269 		case ISP_HA_FC_2500:
270 			btype = "2532";
271 			break;
272 		default:
273 			break;
274 		}
275 
276 		if (!IS_24XX(isp)) {
277 			/*
278 			 * While we're paused, reset the FPM module and FBM
279 			 * fifos.
280 			 */
281 			ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
282 			ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
283 			ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
284 			ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
285 			ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
286 		}
287 	} else if (IS_1240(isp)) {
288 		sdparam *sdp;
289 
290 		btype = "1240";
291 		isp->isp_clock = 60;
292 		sdp = SDPARAM(isp, 0);
293 		sdp->isp_ultramode = 1;
294 		sdp = SDPARAM(isp, 1);
295 		sdp->isp_ultramode = 1;
296 		/*
297 		 * XXX: Should probably do some bus sensing.
298 		 */
299 	} else if (IS_ULTRA3(isp)) {
300 		sdparam *sdp = isp->isp_param;
301 
302 		isp->isp_clock = 100;
303 
304 		if (IS_10160(isp))
305 			btype = "10160";
306 		else if (IS_12160(isp))
307 			btype = "12160";
308 		else
309 			btype = "<UNKLVD>";
310 		sdp->isp_lvdmode = 1;
311 
312 		if (IS_DUALBUS(isp)) {
313 			sdp++;
314 			sdp->isp_lvdmode = 1;
315 		}
316 	} else if (IS_ULTRA2(isp)) {
317 		static const char m[] = "bus %d is in %s Mode";
318 		uint16_t l;
319 		sdparam *sdp = SDPARAM(isp, 0);
320 
321 		isp->isp_clock = 100;
322 
323 		if (IS_1280(isp))
324 			btype = "1280";
325 		else if (IS_1080(isp))
326 			btype = "1080";
327 		else
328 			btype = "<UNKLVD>";
329 
330 		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
331 		switch (l) {
332 		case ISP1080_LVD_MODE:
333 			sdp->isp_lvdmode = 1;
334 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
335 			break;
336 		case ISP1080_HVD_MODE:
337 			sdp->isp_diffmode = 1;
338 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
339 			break;
340 		case ISP1080_SE_MODE:
341 			sdp->isp_ultramode = 1;
342 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
343 			break;
344 		default:
345 			isp_prt(isp, ISP_LOGERR,
346 			    "unknown mode on bus %d (0x%x)", 0, l);
347 			break;
348 		}
349 
350 		if (IS_DUALBUS(isp)) {
351 			sdp = SDPARAM(isp, 1);
352 			l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
353 			l &= ISP1080_MODE_MASK;
354 			switch (l) {
355 			case ISP1080_LVD_MODE:
356 				sdp->isp_lvdmode = 1;
357 				isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
358 				break;
359 			case ISP1080_HVD_MODE:
360 				sdp->isp_diffmode = 1;
361 				isp_prt(isp, ISP_LOGCONFIG,
362 				    m, 1, "Differential");
363 				break;
364 			case ISP1080_SE_MODE:
365 				sdp->isp_ultramode = 1;
366 				isp_prt(isp, ISP_LOGCONFIG,
367 				    m, 1, "Single-Ended");
368 				break;
369 			default:
370 				isp_prt(isp, ISP_LOGERR,
371 				    "unknown mode on bus %d (0x%x)", 1, l);
372 				break;
373 			}
374 		}
375 	} else {
376 		sdparam *sdp = SDPARAM(isp, 0);
377 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
378 		switch (i) {
379 		default:
380 			isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
381 			/* FALLTHROUGH */
382 		case 1:
383 			btype = "1020";
384 			isp->isp_type = ISP_HA_SCSI_1020;
385 			isp->isp_clock = 40;
386 			break;
387 		case 2:
388 			/*
389 			 * Some 1020A chips are Ultra Capable, but don't
390 			 * run the clock rate up for that unless told to
391 			 * do so by the Ultra Capable bits being set.
392 			 */
393 			btype = "1020A";
394 			isp->isp_type = ISP_HA_SCSI_1020A;
395 			isp->isp_clock = 40;
396 			break;
397 		case 3:
398 			btype = "1040";
399 			isp->isp_type = ISP_HA_SCSI_1040;
400 			isp->isp_clock = 60;
401 			break;
402 		case 4:
403 			btype = "1040A";
404 			isp->isp_type = ISP_HA_SCSI_1040A;
405 			isp->isp_clock = 60;
406 			break;
407 		case 5:
408 			btype = "1040B";
409 			isp->isp_type = ISP_HA_SCSI_1040B;
410 			isp->isp_clock = 60;
411 			break;
412 		case 6:
413 			btype = "1040C";
414 			isp->isp_type = ISP_HA_SCSI_1040C;
415 			isp->isp_clock = 60;
416                         break;
417 		}
418 		/*
419 		 * Now, while we're at it, gather info about ultra
420 		 * and/or differential mode.
421 		 */
422 		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
423 			isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
424 			sdp->isp_diffmode = 1;
425 		} else {
426 			sdp->isp_diffmode = 0;
427 		}
428 		i = ISP_READ(isp, RISC_PSR);
429 		if (isp->isp_bustype == ISP_BT_SBUS) {
430 			i &= RISC_PSR_SBUS_ULTRA;
431 		} else {
432 			i &= RISC_PSR_PCI_ULTRA;
433 		}
434 		if (i != 0) {
435 			isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
436 			sdp->isp_ultramode = 1;
437 			/*
438 			 * If we're in Ultra Mode, we have to be 60MHz clock-
439 			 * even for the SBus version.
440 			 */
441 			isp->isp_clock = 60;
442 		} else {
443 			sdp->isp_ultramode = 0;
444 			/*
445 			 * Clock is known. Gronk.
446 			 */
447 		}
448 
449 		/*
450 		 * Machine dependent clock (if set) overrides
451 		 * our generic determinations.
452 		 */
453 		if (isp->isp_mdvec->dv_clock) {
454 			if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
455 				isp->isp_clock = isp->isp_mdvec->dv_clock;
456 			}
457 		}
458 
459 	}
460 
461 	/*
462 	 * Clear instrumentation
463 	 */
464 	isp->isp_intcnt = isp->isp_intbogus = 0;
465 
466 	/*
467 	 * Do MD specific pre initialization
468 	 */
469 	ISP_RESET0(isp);
470 
471 	/*
472 	 * Hit the chip over the head with hammer,
473 	 * and give it a chance to recover.
474 	 */
475 
476 	if (IS_SCSI(isp)) {
477 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
478 		/*
479 		 * A slight delay...
480 		 */
481 		ISP_DELAY(100);
482 
483 		/*
484 		 * Clear data && control DMA engines.
485 		 */
486 		ISP_WRITE(isp, CDMA_CONTROL, DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
487 		ISP_WRITE(isp, DDMA_CONTROL, DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
488 
489 
490 	} else if (IS_24XX(isp)) {
491 		/*
492 		 * Stop DMA and wait for it to stop.
493 		 */
494 		ISP_WRITE(isp, BIU2400_CSR, BIU2400_DMA_STOP|(3 << 4));
495 		for (val = loops = 0; loops < 30000; loops++) {
496 			ISP_DELAY(10);
497 			val = ISP_READ(isp, BIU2400_CSR);
498 			if ((val & BIU2400_DMA_ACTIVE) == 0) {
499 				break;
500 			}
501 		}
502 		if (val & BIU2400_DMA_ACTIVE) {
503 			ISP_RESET0(isp);
504 			isp_prt(isp, ISP_LOGERR, "DMA Failed to Stop on Reset");
505 			return;
506 		}
507 		/*
508 		 * Hold it in SOFT_RESET and STOP state for 100us.
509 		 */
510 		ISP_WRITE(isp, BIU2400_CSR, BIU2400_SOFT_RESET|BIU2400_DMA_STOP|(3 << 4));
511 		ISP_DELAY(100);
512 		for (loops = 0; loops < 10000; loops++) {
513 			ISP_DELAY(5);
514 			val = ISP_READ(isp, OUTMAILBOX0);
515 		}
516 		for (val = loops = 0; loops < 500000; loops ++) {
517 			val = ISP_READ(isp, BIU2400_CSR);
518 			if ((val & BIU2400_SOFT_RESET) == 0) {
519 				break;
520 			}
521 		}
522 		if (val & BIU2400_SOFT_RESET) {
523 			ISP_RESET0(isp);
524 			isp_prt(isp, ISP_LOGERR, "Failed to come out of reset");
525 			return;
526 		}
527 	} else {
528 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
529 		/*
530 		 * A slight delay...
531 		 */
532 		ISP_DELAY(100);
533 
534 		/*
535 		 * Clear data && control DMA engines.
536 		 */
537 		ISP_WRITE(isp, CDMA2100_CONTROL, DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
538 		ISP_WRITE(isp, TDMA2100_CONTROL, DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
539 		ISP_WRITE(isp, RDMA2100_CONTROL, DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
540 	}
541 
542 	/*
543 	 * Wait for ISP to be ready to go...
544 	 */
545 	loops = MBOX_DELAY_COUNT;
546 	for (;;) {
547 		if (IS_SCSI(isp)) {
548 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET)) {
549 				break;
550 			}
551 		} else if (IS_24XX(isp)) {
552 			if (ISP_READ(isp, OUTMAILBOX0) == 0) {
553 				break;
554 			}
555 		} else {
556 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
557 				break;
558 		}
559 		ISP_DELAY(100);
560 		if (--loops < 0) {
561 			ISP_DUMPREGS(isp, "chip reset timed out");
562 			ISP_RESET0(isp);
563 			return;
564 		}
565 	}
566 
567 	/*
568 	 * After we've fired this chip up, zero out the conf1 register
569 	 * for SCSI adapters and other settings for the 2100.
570 	 */
571 
572 	if (IS_SCSI(isp)) {
573 		ISP_WRITE(isp, BIU_CONF1, 0);
574 	} else if (!IS_24XX(isp)) {
575 		ISP_WRITE(isp, BIU2100_CSR, 0);
576 	}
577 
578 	/*
579 	 * Reset RISC Processor
580 	 */
581 	if (IS_24XX(isp)) {
582 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
583 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RELEASE);
584 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RESET);
585 	} else {
586 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
587 		ISP_DELAY(100);
588 		ISP_WRITE(isp, BIU_SEMA, 0);
589 	}
590 
591 	/*
592 	 * Post-RISC Reset stuff.
593 	 */
594 	if (IS_24XX(isp)) {
595 		for (val = loops = 0; loops < 5000000; loops++) {
596 			ISP_DELAY(5);
597 			val = ISP_READ(isp, OUTMAILBOX0);
598 			if (val == 0) {
599 				break;
600 			}
601 		}
602 		if (val != 0) {
603 			ISP_RESET0(isp);
604 			isp_prt(isp, ISP_LOGERR, "reset didn't clear");
605 			return;
606 		}
607 	} else if (IS_SCSI(isp)) {
608 		uint16_t tmp = isp->isp_mdvec->dv_conf1;
609 		/*
610 		 * Busted FIFO. Turn off all but burst enables.
611 		 */
612 		if (isp->isp_type == ISP_HA_SCSI_1040A) {
613 			tmp &= BIU_BURST_ENABLE;
614 		}
615 		ISP_SETBITS(isp, BIU_CONF1, tmp);
616 		if (tmp & BIU_BURST_ENABLE) {
617 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
618 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
619 		}
620 		if (SDPARAM(isp, 0)->isp_ptisp) {
621 			if (SDPARAM(isp, 0)->isp_ultramode) {
622 				while (ISP_READ(isp, RISC_MTR) != 0x1313) {
623 					ISP_WRITE(isp, RISC_MTR, 0x1313);
624 					ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
625 				}
626 			} else {
627 				ISP_WRITE(isp, RISC_MTR, 0x1212);
628 			}
629 			/*
630 			 * PTI specific register
631 			 */
632 			ISP_WRITE(isp, RISC_EMB, DUAL_BANK);
633 		} else {
634 			ISP_WRITE(isp, RISC_MTR, 0x1212);
635 		}
636 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
637 	} else {
638 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
639 		if (IS_2200(isp) || IS_23XX(isp)) {
640 			ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
641 		}
642 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
643 	}
644 
645 	ISP_WRITE(isp, isp->isp_rqstinrp, 0);
646 	ISP_WRITE(isp, isp->isp_rqstoutrp, 0);
647 	ISP_WRITE(isp, isp->isp_respinrp, 0);
648 	ISP_WRITE(isp, isp->isp_respoutrp, 0);
649 	if (IS_24XX(isp)) {
650 		ISP_WRITE(isp, BIU2400_PRI_REQINP, 0);
651 		ISP_WRITE(isp, BIU2400_PRI_REQOUTP, 0);
652 		ISP_WRITE(isp, BIU2400_ATIO_RSPINP, 0);
653 		ISP_WRITE(isp, BIU2400_ATIO_RSPOUTP, 0);
654 	}
655 
656 	/*
657 	 * Do MD specific post initialization
658 	 */
659 	ISP_RESET1(isp);
660 
661 	/*
662 	 * Wait for everything to finish firing up.
663 	 *
664 	 * Avoid doing this on early 2312s because you can generate a PCI
665 	 * parity error (chip breakage).
666 	 */
667 	if (IS_2312(isp) && isp->isp_revision < 2) {
668 		ISP_DELAY(100);
669 	} else {
670 		loops = MBOX_DELAY_COUNT;
671 		while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
672 			ISP_DELAY(100);
673 			if (--loops < 0) {
674 				ISP_RESET0(isp);
675 				isp_prt(isp, ISP_LOGERR, "MBOX_BUSY never cleared on reset");
676 				return;
677 			}
678 		}
679 	}
680 
681 	/*
682 	 * Up until this point we've done everything by just reading or
683 	 * setting registers. From this point on we rely on at least *some*
684 	 * kind of firmware running in the card.
685 	 */
686 
687 	/*
688 	 * Do some sanity checking by running a NOP command.
689 	 * If it succeeds, the ROM firmware is now running.
690 	 */
691 	MBSINIT(&mbs, MBOX_NO_OP, MBLOGALL, 0);
692 	isp_mboxcmd(isp, &mbs);
693 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
694 		isp_prt(isp, ISP_LOGERR, "NOP command failed (%x)", mbs.param[0]);
695 		ISP_RESET0(isp);
696 		return;
697 	}
698 
699 	/*
700 	 * Do some operational tests
701 	 */
702 
703 	if (IS_SCSI(isp) || IS_24XX(isp)) {
704 		static const uint16_t patterns[MAX_MAILBOX] = {
705 			0x0000, 0xdead, 0xbeef, 0xffff,
706 			0xa5a5, 0x5a5a, 0x7f7f, 0x7ff7,
707 			0x3421, 0xabcd, 0xdcba, 0xfeef,
708 			0xbead, 0xdebe, 0x2222, 0x3333,
709 			0x5555, 0x6666, 0x7777, 0xaaaa,
710 			0xffff, 0xdddd, 0x9999, 0x1fbc,
711 			0x6666, 0x6677, 0x1122, 0x33ff,
712 			0x0000, 0x0001, 0x1000, 0x1010,
713 		};
714 		int nmbox = ISP_NMBOX(isp);
715 		if (IS_SCSI(isp))
716 			nmbox = 6;
717 		MBSINIT(&mbs, MBOX_MAILBOX_REG_TEST, MBLOGALL, 0);
718 		for (i = 1; i < nmbox; i++) {
719 			mbs.param[i] = patterns[i];
720 		}
721 		isp_mboxcmd(isp, &mbs);
722 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
723 			ISP_RESET0(isp);
724 			return;
725 		}
726 		for (i = 1; i < nmbox; i++) {
727 			if (mbs.param[i] != patterns[i]) {
728 				ISP_RESET0(isp);
729 				isp_prt(isp, ISP_LOGERR, "Register Test Failed at Register %d: should have 0x%04x but got 0x%04x", i, patterns[i], mbs.param[i]);
730 				return;
731 			}
732 		}
733 	}
734 
735 	/*
736 	 * Download new Firmware, unless requested not to do so.
737 	 * This is made slightly trickier in some cases where the
738 	 * firmware of the ROM revision is newer than the revision
739 	 * compiled into the driver. So, where we used to compare
740 	 * versions of our f/w and the ROM f/w, now we just see
741 	 * whether we have f/w at all and whether a config flag
742 	 * has disabled our download.
743 	 */
744 	if ((isp->isp_mdvec->dv_ispfw == NULL) || (isp->isp_confopts & ISP_CFG_NORELOAD)) {
745 		dodnld = 0;
746 	}
747 
748 	if (IS_24XX(isp)) {
749 		code_org = ISP_CODE_ORG_2400;
750 	} else if (IS_23XX(isp)) {
751 		code_org = ISP_CODE_ORG_2300;
752 	} else {
753 		code_org = ISP_CODE_ORG;
754 	}
755 
756 	if (dodnld && IS_24XX(isp)) {
757 		const uint32_t *ptr = isp->isp_mdvec->dv_ispfw;
758 		int wordload;
759 
760 		/*
761 		 * Keep loading until we run out of f/w.
762 		 */
763 		code_org = ptr[2];	/* 1st load address is our start addr */
764 		wordload = 0;
765 
766 		for (;;) {
767 			uint32_t la, wi, wl;
768 
769 			isp_prt(isp, ISP_LOGDEBUG0, "load 0x%x words of code at load address 0x%x", ptr[3], ptr[2]);
770 
771 			wi = 0;
772 			la = ptr[2];
773 			wl = ptr[3];
774 
775 			while (wi < ptr[3]) {
776 				uint32_t *cp;
777 				uint32_t nw;
778 
779 				nw = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) >> 2;
780 				if (nw > wl) {
781 					nw = wl;
782 				}
783 				cp = isp->isp_rquest;
784 				for (i = 0; i < nw; i++) {
785 					ISP_IOXPUT_32(isp,  ptr[wi++], &cp[i]);
786 					wl--;
787 				}
788 				MEMORYBARRIER(isp, SYNC_REQUEST, 0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)), -1);
789 	again:
790 				MBSINIT(&mbs, 0, MBLOGALL, 0);
791 				if (la < 0x10000 && nw < 0x10000) {
792 					mbs.param[0] = MBOX_LOAD_RISC_RAM_2100;
793 					mbs.param[1] = la;
794 					mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
795 					mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
796 					mbs.param[4] = nw;
797 					mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
798 					mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
799 					isp_prt(isp, ISP_LOGDEBUG0, "LOAD RISC RAM 2100 %u words at load address 0x%x", nw, la);
800 				} else if (wordload) {
801 					union {
802 						const uint32_t *cp;
803 						uint32_t *np;
804 					} ucd;
805 					ucd.cp = (const uint32_t *)cp;
806 					mbs.param[0] = MBOX_WRITE_RAM_WORD_EXTENDED;
807 					mbs.param[1] = la;
808 					mbs.param[2] = (*ucd.np);
809 					mbs.param[3] = (*ucd.np) >> 16;
810 					mbs.param[8] = la >> 16;
811 					isp->isp_mbxwrk0 = nw - 1;
812 					isp->isp_mbxworkp = ucd.np+1;
813 					isp->isp_mbxwrk1 = (la + 1);
814 					isp->isp_mbxwrk8 = (la + 1) >> 16;
815 					isp_prt(isp, ISP_LOGDEBUG0, "WRITE RAM WORD EXTENDED %u words at load address 0x%x", nw, la);
816 				} else {
817 					mbs.param[0] = MBOX_LOAD_RISC_RAM;
818 					mbs.param[1] = la;
819 					mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
820 					mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
821 					mbs.param[4] = nw >> 16;
822 					mbs.param[5] = nw;
823 					mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
824 					mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
825 					mbs.param[8] = la >> 16;
826 					isp_prt(isp, ISP_LOGDEBUG0, "LOAD RISC RAM %u words at load address 0x%x", nw, la);
827 				}
828 				isp_mboxcmd(isp, &mbs);
829 				if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
830 					if (mbs.param[0] == MBOX_HOST_INTERFACE_ERROR) {
831 						isp_prt(isp, ISP_LOGERR, "switching to word load");
832 						wordload = 1;
833 						goto again;
834 					}
835 					isp_prt(isp, ISP_LOGERR, "F/W Risc Ram Load Failed");
836 					ISP_RESET0(isp);
837 					return;
838 				}
839 				la += nw;
840 			}
841 
842 			if (ptr[1] == 0) {
843 				break;
844 			}
845 			ptr += ptr[3];
846 		}
847 		isp->isp_loaded_fw = 1;
848 	} else if (dodnld && IS_23XX(isp)) {
849 		const uint16_t *ptr = isp->isp_mdvec->dv_ispfw;
850 		uint16_t wi, wl, segno;
851 		uint32_t la;
852 
853 		la = code_org;
854 		segno = 0;
855 
856 		for (;;) {
857 			uint32_t nxtaddr;
858 
859 			isp_prt(isp, ISP_LOGDEBUG0, "load 0x%x words of code at load address 0x%x", ptr[3], la);
860 
861 			wi = 0;
862 			wl = ptr[3];
863 
864 			while (wi < ptr[3]) {
865 				uint16_t *cp;
866 				uint16_t nw;
867 
868 				nw = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) >> 1;
869 				if (nw > wl) {
870 					nw = wl;
871 				}
872 				if (nw > (1 << 15)) {
873 					nw = 1 << 15;
874 				}
875 				cp = isp->isp_rquest;
876 				for (i = 0; i < nw; i++) {
877 					ISP_IOXPUT_16(isp,  ptr[wi++], &cp[i]);
878 					wl--;
879 				}
880 				MEMORYBARRIER(isp, SYNC_REQUEST, 0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)), -1);
881 				MBSINIT(&mbs, 0, MBLOGALL, 0);
882 				if (la < 0x10000) {
883 					mbs.param[0] = MBOX_LOAD_RISC_RAM_2100;
884 					mbs.param[1] = la;
885 					mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
886 					mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
887 					mbs.param[4] = nw;
888 					mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
889 					mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
890 					isp_prt(isp, ISP_LOGDEBUG1, "LOAD RISC RAM 2100 %u words at load address 0x%x\n", nw, la);
891 				} else {
892 					mbs.param[0] = MBOX_LOAD_RISC_RAM;
893 					mbs.param[1] = la;
894 					mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
895 					mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
896 					mbs.param[4] = nw;
897 					mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
898 					mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
899 					mbs.param[8] = la >> 16;
900 					isp_prt(isp, ISP_LOGDEBUG1, "LOAD RISC RAM %u words at load address 0x%x\n", nw, la);
901 				}
902 				isp_mboxcmd(isp, &mbs);
903 				if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
904 					isp_prt(isp, ISP_LOGERR, "F/W Risc Ram Load Failed");
905 					ISP_RESET0(isp);
906 					return;
907 				}
908 				la += nw;
909 			}
910 
911 			if (!IS_2322(isp)) {
912 				break;
913 			}
914 
915 			if (++segno == 3) {
916 				break;
917 			}
918 
919 			/*
920 			 * If we're a 2322, the firmware actually comes in
921 			 * three chunks. We loaded the first at the code_org
922 			 * address. The other two chunks, which follow right
923 			 * after each other in memory here, get loaded at
924 			 * addresses specfied at offset 0x9..0xB.
925 			 */
926 
927 			nxtaddr = ptr[3];
928 			ptr = &ptr[nxtaddr];
929 			la = ptr[5] | ((ptr[4] & 0x3f) << 16);
930 		}
931 		isp->isp_loaded_fw = 1;
932 	} else if (dodnld) {
933 		union {
934 			const uint16_t *cp;
935 			uint16_t *np;
936 		} ucd;
937 		ucd.cp = isp->isp_mdvec->dv_ispfw;
938 		isp->isp_mbxworkp = &ucd.np[1];
939 		isp->isp_mbxwrk0 = ucd.np[3] - 1;
940 		isp->isp_mbxwrk1 = code_org + 1;
941 		MBSINIT(&mbs, MBOX_WRITE_RAM_WORD, MBLOGNONE, 0);
942 		mbs.param[1] = code_org;
943 		mbs.param[2] = ucd.np[0];
944 		isp_prt(isp, ISP_LOGDEBUG1, "WRITE RAM %u words at load address 0x%x", ucd.np[3], code_org);
945 		isp_mboxcmd(isp, &mbs);
946 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
947 			isp_prt(isp, ISP_LOGERR, "F/W download failed at word %d", isp->isp_mbxwrk1 - code_org);
948 			ISP_RESET0(isp);
949 			return;
950 		}
951 	} else {
952 		isp->isp_loaded_fw = 0;
953 		isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
954 	}
955 
956 	/*
957 	 * If we loaded firmware, verify its checksum
958 	 */
959 	if (isp->isp_loaded_fw) {
960 		MBSINIT(&mbs, MBOX_VERIFY_CHECKSUM, MBLOGNONE, 0);
961 		mbs.param[0] = MBOX_VERIFY_CHECKSUM;
962 		if (IS_24XX(isp)) {
963 			mbs.param[1] = code_org >> 16;
964 			mbs.param[2] = code_org;
965 		} else {
966 			mbs.param[1] = code_org;
967 		}
968 		isp_mboxcmd(isp, &mbs);
969 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
970 			isp_prt(isp, ISP_LOGERR, dcrc);
971 			ISP_RESET0(isp);
972 			return;
973 		}
974 	}
975 
976 	/*
977 	 * Now start it rolling.
978 	 *
979 	 * If we didn't actually download f/w,
980 	 * we still need to (re)start it.
981 	 */
982 
983 
984 	MBSINIT(&mbs, MBOX_EXEC_FIRMWARE, MBLOGALL, 5000000);
985 	if (IS_24XX(isp)) {
986 		mbs.param[1] = code_org >> 16;
987 		mbs.param[2] = code_org;
988 		if (isp->isp_loaded_fw) {
989 			mbs.param[3] = 0;
990 		} else {
991 			mbs.param[3] = 1;
992 		}
993 		if (IS_25XX(isp)) {
994 			mbs.ibits |= 0x10;
995 		}
996 	} else if (IS_2322(isp)) {
997 		mbs.param[1] = code_org;
998 		if (isp->isp_loaded_fw) {
999 			mbs.param[2] = 0;
1000 		} else {
1001 			mbs.param[2] = 1;
1002 		}
1003 	} else {
1004 		mbs.param[1] = code_org;
1005 	}
1006 	isp_mboxcmd(isp, &mbs);
1007 	if (IS_2322(isp) || IS_24XX(isp)) {
1008 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1009 			ISP_RESET0(isp);
1010 			return;
1011 		}
1012 	}
1013 
1014 	if (IS_SCSI(isp)) {
1015 		/*
1016 		 * Set CLOCK RATE, but only if asked to.
1017 		 */
1018 		if (isp->isp_clock) {
1019 			MBSINIT(&mbs, MBOX_SET_CLOCK_RATE, MBLOGALL, 0);
1020 			mbs.param[1] = isp->isp_clock;
1021 			isp_mboxcmd(isp, &mbs);
1022 			/* we will try not to care if this fails */
1023 		}
1024 	}
1025 
1026 	/*
1027 	 * Ask the chip for the current firmware version.
1028 	 * This should prove that the new firmware is working.
1029 	 */
1030 	MBSINIT(&mbs, MBOX_ABOUT_FIRMWARE, MBLOGALL, 0);
1031 	isp_mboxcmd(isp, &mbs);
1032 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1033 		ISP_RESET0(isp);
1034 		return;
1035 	}
1036 
1037 	/*
1038 	 * The SBus firmware that we are using apparently does not return
1039 	 * major, minor, micro revisions in the mailbox registers, which
1040 	 * is really, really, annoying.
1041 	 */
1042 	if (ISP_SBUS_SUPPORTED && isp->isp_bustype == ISP_BT_SBUS) {
1043 		if (dodnld) {
1044 #ifdef	ISP_TARGET_MODE
1045 			isp->isp_fwrev[0] = 7;
1046 			isp->isp_fwrev[1] = 55;
1047 #else
1048 			isp->isp_fwrev[0] = 1;
1049 			isp->isp_fwrev[1] = 37;
1050 #endif
1051 			isp->isp_fwrev[2] = 0;
1052 		}
1053 	} else {
1054 		isp->isp_fwrev[0] = mbs.param[1];
1055 		isp->isp_fwrev[1] = mbs.param[2];
1056 		isp->isp_fwrev[2] = mbs.param[3];
1057 	}
1058 
1059 	if (IS_FC(isp)) {
1060 		/*
1061 		 * We do not believe firmware attributes for 2100 code less
1062 		 * than 1.17.0, unless it's the firmware we specifically
1063 		 * are loading.
1064 		 *
1065 		 * Note that all 22XX and later f/w is greater than 1.X.0.
1066 		 */
1067 		if ((ISP_FW_OLDER_THAN(isp, 1, 17, 1))) {
1068 #ifdef	USE_SMALLER_2100_FIRMWARE
1069 			isp->isp_fwattr = ISP_FW_ATTR_SCCLUN;
1070 #else
1071 			isp->isp_fwattr = 0;
1072 #endif
1073 		} else {
1074 			isp->isp_fwattr = mbs.param[6];
1075 		}
1076 		if (IS_24XX(isp)) {
1077 			isp->isp_fwattr |= ((uint64_t) mbs.param[15]) << 16;
1078 			if (isp->isp_fwattr & ISP2400_FW_ATTR_EXTNDED) {
1079 				isp->isp_fwattr |=
1080 				    (((uint64_t) mbs.param[16]) << 32) |
1081 				    (((uint64_t) mbs.param[17]) << 48);
1082 			}
1083 		}
1084 	} else if (IS_SCSI(isp)) {
1085 #ifndef	ISP_TARGET_MODE
1086 		isp->isp_fwattr = ISP_FW_ATTR_TMODE;
1087 #else
1088 		isp->isp_fwattr = 0;
1089 #endif
1090 	}
1091 
1092 	isp_prt(isp, ISP_LOGCONFIG, "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
1093 	    btype, isp->isp_revision, dodnld? "loaded" : "resident", isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
1094 
1095 	fwt = isp->isp_fwattr;
1096 	if (IS_24XX(isp)) {
1097 		buf = FCPARAM(isp, 0)->isp_scratch;
1098 		ISP_SNPRINTF(buf, ISP_FC_SCRLEN, "Attributes:");
1099 		if (fwt & ISP2400_FW_ATTR_CLASS2) {
1100 			fwt ^=ISP2400_FW_ATTR_CLASS2;
1101 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Class2", buf);
1102 		}
1103 		if (fwt & ISP2400_FW_ATTR_IP) {
1104 			fwt ^=ISP2400_FW_ATTR_IP;
1105 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s IP", buf);
1106 		}
1107 		if (fwt & ISP2400_FW_ATTR_MULTIID) {
1108 			fwt ^=ISP2400_FW_ATTR_MULTIID;
1109 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MultiID", buf);
1110 		}
1111 		if (fwt & ISP2400_FW_ATTR_SB2) {
1112 			fwt ^=ISP2400_FW_ATTR_SB2;
1113 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SB2", buf);
1114 		}
1115 		if (fwt & ISP2400_FW_ATTR_T10CRC) {
1116 			fwt ^=ISP2400_FW_ATTR_T10CRC;
1117 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s T10CRC", buf);
1118 		}
1119 		if (fwt & ISP2400_FW_ATTR_VI) {
1120 			fwt ^=ISP2400_FW_ATTR_VI;
1121 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI", buf);
1122 		}
1123 		if (fwt & ISP2400_FW_ATTR_MQ) {
1124 			fwt ^=ISP2400_FW_ATTR_MQ;
1125 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MQ", buf);
1126 		}
1127 		if (fwt & ISP2400_FW_ATTR_MSIX) {
1128 			fwt ^=ISP2400_FW_ATTR_MSIX;
1129 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MSIX", buf);
1130 		}
1131 		if (fwt & ISP2400_FW_ATTR_FCOE) {
1132 			fwt ^=ISP2400_FW_ATTR_FCOE;
1133 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s FCOE", buf);
1134 		}
1135 		if (fwt & ISP2400_FW_ATTR_VP0) {
1136 			fwt ^= ISP2400_FW_ATTR_VP0;
1137 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VP0_Decoupling", buf);
1138 		}
1139 		if (fwt & ISP2400_FW_ATTR_EXPFW) {
1140 			fwt ^= ISP2400_FW_ATTR_EXPFW;
1141 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (Experimental)", buf);
1142 		}
1143 		if (fwt & ISP2400_FW_ATTR_HOTFW) {
1144 			fwt ^= ISP2400_FW_ATTR_HOTFW;
1145 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s HotFW", buf);
1146 		}
1147 		fwt &= ~ISP2400_FW_ATTR_EXTNDED;
1148 		if (fwt & ISP2400_FW_ATTR_EXTVP) {
1149 			fwt ^= ISP2400_FW_ATTR_EXTVP;
1150 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ExtVP", buf);
1151 		}
1152 		if (fwt & ISP2400_FW_ATTR_VN2VN) {
1153 			fwt ^= ISP2400_FW_ATTR_VN2VN;
1154 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VN2VN", buf);
1155 		}
1156 		if (fwt & ISP2400_FW_ATTR_EXMOFF) {
1157 			fwt ^= ISP2400_FW_ATTR_EXMOFF;
1158 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s EXMOFF", buf);
1159 		}
1160 		if (fwt & ISP2400_FW_ATTR_NPMOFF) {
1161 			fwt ^= ISP2400_FW_ATTR_NPMOFF;
1162 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s NPMOFF", buf);
1163 		}
1164 		if (fwt & ISP2400_FW_ATTR_DIFCHOP) {
1165 			fwt ^= ISP2400_FW_ATTR_DIFCHOP;
1166 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s DIFCHOP", buf);
1167 		}
1168 		if (fwt & ISP2400_FW_ATTR_SRIOV) {
1169 			fwt ^= ISP2400_FW_ATTR_SRIOV;
1170 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SRIOV", buf);
1171 		}
1172 		if (fwt & ISP2400_FW_ATTR_ASICTMP) {
1173 			fwt ^= ISP2400_FW_ATTR_ASICTMP;
1174 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ASICTMP", buf);
1175 		}
1176 		if (fwt & ISP2400_FW_ATTR_ATIOMQ) {
1177 			fwt ^= ISP2400_FW_ATTR_ATIOMQ;
1178 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ATIOMQ", buf);
1179 		}
1180 		if (fwt) {
1181 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (unknown 0x%08x%08x)", buf,
1182 			    (uint32_t) (fwt >> 32), (uint32_t) fwt);
1183 		}
1184 		isp_prt(isp, ISP_LOGCONFIG, "%s", buf);
1185 	} else if (IS_FC(isp)) {
1186 		buf = FCPARAM(isp, 0)->isp_scratch;
1187 		ISP_SNPRINTF(buf, ISP_FC_SCRLEN, "Attributes:");
1188 		if (fwt & ISP_FW_ATTR_TMODE) {
1189 			fwt ^=ISP_FW_ATTR_TMODE;
1190 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s TargetMode", buf);
1191 		}
1192 		if (fwt & ISP_FW_ATTR_SCCLUN) {
1193 			fwt ^=ISP_FW_ATTR_SCCLUN;
1194 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SCC-Lun", buf);
1195 		}
1196 		if (fwt & ISP_FW_ATTR_FABRIC) {
1197 			fwt ^=ISP_FW_ATTR_FABRIC;
1198 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Fabric", buf);
1199 		}
1200 		if (fwt & ISP_FW_ATTR_CLASS2) {
1201 			fwt ^=ISP_FW_ATTR_CLASS2;
1202 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Class2", buf);
1203 		}
1204 		if (fwt & ISP_FW_ATTR_FCTAPE) {
1205 			fwt ^=ISP_FW_ATTR_FCTAPE;
1206 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s FC-Tape", buf);
1207 		}
1208 		if (fwt & ISP_FW_ATTR_IP) {
1209 			fwt ^=ISP_FW_ATTR_IP;
1210 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s IP", buf);
1211 		}
1212 		if (fwt & ISP_FW_ATTR_VI) {
1213 			fwt ^=ISP_FW_ATTR_VI;
1214 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI", buf);
1215 		}
1216 		if (fwt & ISP_FW_ATTR_VI_SOLARIS) {
1217 			fwt ^=ISP_FW_ATTR_VI_SOLARIS;
1218 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI_SOLARIS", buf);
1219 		}
1220 		if (fwt & ISP_FW_ATTR_2KLOGINS) {
1221 			fwt ^=ISP_FW_ATTR_2KLOGINS;
1222 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s 2K-Login", buf);
1223 		}
1224 		if (fwt != 0) {
1225 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (unknown 0x%08x%08x)", buf,
1226 			    (uint32_t) (fwt >> 32), (uint32_t) fwt);
1227 		}
1228 		isp_prt(isp, ISP_LOGCONFIG, "%s", buf);
1229 	}
1230 
1231 	if (IS_24XX(isp)) {
1232 		MBSINIT(&mbs, MBOX_GET_RESOURCE_COUNT, MBLOGALL, 0);
1233 		isp_mboxcmd(isp, &mbs);
1234 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1235 			ISP_RESET0(isp);
1236 			return;
1237 		}
1238 		if (isp->isp_maxcmds >= mbs.param[3]) {
1239 			isp->isp_maxcmds = mbs.param[3];
1240 		}
1241 	} else {
1242 		MBSINIT(&mbs, MBOX_GET_FIRMWARE_STATUS, MBLOGALL, 0);
1243 		isp_mboxcmd(isp, &mbs);
1244 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1245 			ISP_RESET0(isp);
1246 			return;
1247 		}
1248 		if (isp->isp_maxcmds >= mbs.param[2]) {
1249 			isp->isp_maxcmds = mbs.param[2];
1250 		}
1251 	}
1252 	isp_prt(isp, ISP_LOGCONFIG, "%d max I/O command limit set", isp->isp_maxcmds);
1253 
1254 	/*
1255 	 * If we don't have Multi-ID f/w loaded, we need to restrict channels to one.
1256 	 * Only make this check for non-SCSI cards (I'm not sure firmware attributes
1257 	 * work for them).
1258 	 */
1259 	if (IS_FC(isp) && isp->isp_nchan > 1) {
1260 		if (!ISP_CAP_MULTI_ID(isp)) {
1261 			isp_prt(isp, ISP_LOGWARN, "non-MULTIID f/w loaded, "
1262 			    "only can enable 1 of %d channels", isp->isp_nchan);
1263 			isp->isp_nchan = 1;
1264 		} else if (!ISP_CAP_VP0(isp)) {
1265 			isp_prt(isp, ISP_LOGWARN, "We can not use MULTIID "
1266 			    "feature properly without VP0_Decoupling");
1267 			isp->isp_nchan = 1;
1268 		}
1269 	}
1270 	for (i = 0; i < isp->isp_nchan; i++) {
1271 		isp_fw_state(isp, i);
1272 	}
1273 	if (isp->isp_dead) {
1274 		isp_shutdown(isp);
1275 		ISP_DISABLE_INTS(isp);
1276 		return;
1277 	}
1278 
1279 	isp->isp_state = ISP_RESETSTATE;
1280 
1281 	/*
1282 	 * Okay- now that we have new firmware running, we now (re)set our
1283 	 * notion of how many luns we support. This is somewhat tricky because
1284 	 * if we haven't loaded firmware, we sometimes do not have an easy way
1285 	 * of knowing how many luns we support.
1286 	 *
1287 	 * Expanded lun firmware gives you 32 luns for SCSI cards and
1288 	 * 16384 luns for Fibre Channel cards.
1289 	 *
1290 	 * It turns out that even for QLogic 2100s with ROM 1.10 and above
1291 	 * we do get a firmware attributes word returned in mailbox register 6.
1292 	 *
1293 	 * Because the lun is in a different position in the Request Queue
1294 	 * Entry structure for Fibre Channel with expanded lun firmware, we
1295 	 * can only support one lun (lun zero) when we don't know what kind
1296 	 * of firmware we're running.
1297 	 */
1298 	if (IS_SCSI(isp)) {
1299 		if (dodnld) {
1300 			if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
1301 				isp->isp_maxluns = 32;
1302 			} else {
1303 				isp->isp_maxluns = 8;
1304 			}
1305 		} else {
1306 			isp->isp_maxluns = 8;
1307 		}
1308 	} else {
1309 		if (ISP_CAP_SCCFW(isp)) {
1310 			isp->isp_maxluns = 0;	/* No limit -- 2/8 bytes */
1311 		} else {
1312 			isp->isp_maxluns = 16;
1313 		}
1314 	}
1315 
1316 	/*
1317 	 * We get some default values established. As a side
1318 	 * effect, NVRAM is read here (unless overriden by
1319 	 * a configuration flag).
1320 	 */
1321 	if (do_load_defaults) {
1322 		if (IS_SCSI(isp)) {
1323 			isp_setdfltsdparm(isp);
1324 		} else {
1325 			for (i = 0; i < isp->isp_nchan; i++) {
1326 				isp_setdfltfcparm(isp, i);
1327 			}
1328 		}
1329 	}
1330 }
1331 
1332 /*
1333  * Initialize Parameters of Hardware to a known state.
1334  *
1335  * Locks are held before coming here.
1336  */
1337 
1338 void
1339 isp_init(ispsoftc_t *isp)
1340 {
1341 	if (IS_FC(isp)) {
1342 		if (IS_24XX(isp)) {
1343 			isp_fibre_init_2400(isp);
1344 		} else {
1345 			isp_fibre_init(isp);
1346 		}
1347 	} else {
1348 		isp_scsi_init(isp);
1349 	}
1350 	GET_NANOTIME(&isp->isp_init_time);
1351 }
1352 
1353 static void
1354 isp_scsi_init(ispsoftc_t *isp)
1355 {
1356 	sdparam *sdp_chan0, *sdp_chan1;
1357 	mbreg_t mbs;
1358 
1359 	isp->isp_state = ISP_INITSTATE;
1360 
1361 	sdp_chan0 = SDPARAM(isp, 0);
1362 	sdp_chan1 = sdp_chan0;
1363 	if (IS_DUALBUS(isp)) {
1364 		sdp_chan1 = SDPARAM(isp, 1);
1365 	}
1366 
1367 	/* First do overall per-card settings. */
1368 
1369 	/*
1370 	 * If we have fast memory timing enabled, turn it on.
1371 	 */
1372 	if (sdp_chan0->isp_fast_mttr) {
1373 		ISP_WRITE(isp, RISC_MTR, 0x1313);
1374 	}
1375 
1376 	/*
1377 	 * Set Retry Delay and Count.
1378 	 * You set both channels at the same time.
1379 	 */
1380 	MBSINIT(&mbs, MBOX_SET_RETRY_COUNT, MBLOGALL, 0);
1381 	mbs.param[1] = sdp_chan0->isp_retry_count;
1382 	mbs.param[2] = sdp_chan0->isp_retry_delay;
1383 	mbs.param[6] = sdp_chan1->isp_retry_count;
1384 	mbs.param[7] = sdp_chan1->isp_retry_delay;
1385 	isp_mboxcmd(isp, &mbs);
1386 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1387 		return;
1388 	}
1389 
1390 	/*
1391 	 * Set ASYNC DATA SETUP time. This is very important.
1392 	 */
1393 	MBSINIT(&mbs, MBOX_SET_ASYNC_DATA_SETUP_TIME, MBLOGALL, 0);
1394 	mbs.param[1] = sdp_chan0->isp_async_data_setup;
1395 	mbs.param[2] = sdp_chan1->isp_async_data_setup;
1396 	isp_mboxcmd(isp, &mbs);
1397 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1398 		return;
1399 	}
1400 
1401 	/*
1402 	 * Set ACTIVE Negation State.
1403 	 */
1404 	MBSINIT(&mbs, MBOX_SET_ACT_NEG_STATE, MBLOGNONE, 0);
1405 	mbs.param[1] =
1406 	    (sdp_chan0->isp_req_ack_active_neg << 4) |
1407 	    (sdp_chan0->isp_data_line_active_neg << 5);
1408 	mbs.param[2] =
1409 	    (sdp_chan1->isp_req_ack_active_neg << 4) |
1410 	    (sdp_chan1->isp_data_line_active_neg << 5);
1411 	isp_mboxcmd(isp, &mbs);
1412 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1413 		isp_prt(isp, ISP_LOGERR,
1414 		    "failed to set active negation state (%d,%d), (%d,%d)",
1415 		    sdp_chan0->isp_req_ack_active_neg,
1416 		    sdp_chan0->isp_data_line_active_neg,
1417 		    sdp_chan1->isp_req_ack_active_neg,
1418 		    sdp_chan1->isp_data_line_active_neg);
1419 		/*
1420 		 * But don't return.
1421 		 */
1422 	}
1423 
1424 	/*
1425 	 * Set the Tag Aging limit
1426 	 */
1427 	MBSINIT(&mbs, MBOX_SET_TAG_AGE_LIMIT, MBLOGALL, 0);
1428 	mbs.param[1] = sdp_chan0->isp_tag_aging;
1429 	mbs.param[2] = sdp_chan1->isp_tag_aging;
1430 	isp_mboxcmd(isp, &mbs);
1431 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1432 		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
1433 		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
1434 		return;
1435 	}
1436 
1437 	/*
1438 	 * Set selection timeout.
1439 	 */
1440 	MBSINIT(&mbs, MBOX_SET_SELECT_TIMEOUT, MBLOGALL, 0);
1441 	mbs.param[1] = sdp_chan0->isp_selection_timeout;
1442 	mbs.param[2] = sdp_chan1->isp_selection_timeout;
1443 	isp_mboxcmd(isp, &mbs);
1444 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1445 		return;
1446 	}
1447 
1448 	/* now do per-channel settings */
1449 	isp_scsi_channel_init(isp, 0);
1450 	if (IS_DUALBUS(isp))
1451 		isp_scsi_channel_init(isp, 1);
1452 
1453 	/*
1454 	 * Now enable request/response queues
1455 	 */
1456 
1457 	if (IS_ULTRA2(isp) || IS_1240(isp)) {
1458 		MBSINIT(&mbs, MBOX_INIT_RES_QUEUE_A64, MBLOGALL, 0);
1459 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
1460 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1461 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1462 		mbs.param[4] = 0;
1463 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1464 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1465 		isp_mboxcmd(isp, &mbs);
1466 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1467 			return;
1468 		}
1469 		isp->isp_residx = isp->isp_resodx = mbs.param[5];
1470 
1471 		MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE_A64, MBLOGALL, 0);
1472 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1473 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1474 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1475 		mbs.param[5] = 0;
1476 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1477 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1478 		isp_mboxcmd(isp, &mbs);
1479 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1480 			return;
1481 		}
1482 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1483 	} else {
1484 		MBSINIT(&mbs, MBOX_INIT_RES_QUEUE, MBLOGALL, 0);
1485 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
1486 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1487 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1488 		mbs.param[4] = 0;
1489 		isp_mboxcmd(isp, &mbs);
1490 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1491 			return;
1492 		}
1493 		isp->isp_residx = isp->isp_resodx = mbs.param[5];
1494 
1495 		MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE, MBLOGALL, 0);
1496 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1497 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1498 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1499 		mbs.param[5] = 0;
1500 		isp_mboxcmd(isp, &mbs);
1501 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1502 			return;
1503 		}
1504 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1505 	}
1506 
1507 	/*
1508 	 * Turn on LVD transitions for ULTRA2 or better and other features
1509 	 *
1510 	 * Now that we have 32 bit handles, don't do any fast posting
1511 	 * any more. For Ultra2/Ultra3 cards, we can turn on 32 bit RIO
1512 	 * operation or use fast posting. To be conservative, we'll only
1513 	 * do this for Ultra3 cards now because the other cards are so
1514 	 * rare for this author to find and test with.
1515 	 */
1516 
1517 	MBSINIT(&mbs, MBOX_SET_FW_FEATURES, MBLOGALL, 0);
1518 	if (IS_ULTRA2(isp))
1519 		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
1520 #ifdef	ISP_NO_RIO
1521 	if (IS_ULTRA3(isp))
1522 		mbs.param[1] |= FW_FEATURE_FAST_POST;
1523 #else
1524 	if (IS_ULTRA3(isp))
1525 		mbs.param[1] |= FW_FEATURE_RIO_32BIT;
1526 #endif
1527 	if (mbs.param[1] != 0) {
1528 		uint16_t sfeat = mbs.param[1];
1529 		isp_mboxcmd(isp, &mbs);
1530 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1531 			isp_prt(isp, ISP_LOGINFO,
1532 			    "Enabled FW features (0x%x)", sfeat);
1533 		}
1534 	}
1535 
1536 	isp->isp_state = ISP_RUNSTATE;
1537 }
1538 
1539 static void
1540 isp_scsi_channel_init(ispsoftc_t *isp, int chan)
1541 {
1542 	sdparam *sdp;
1543 	mbreg_t mbs;
1544 	int tgt;
1545 
1546 	sdp = SDPARAM(isp, chan);
1547 
1548 	/*
1549 	 * Set (possibly new) Initiator ID.
1550 	 */
1551 	MBSINIT(&mbs, MBOX_SET_INIT_SCSI_ID, MBLOGALL, 0);
1552 	mbs.param[1] = (chan << 7) | sdp->isp_initiator_id;
1553 	isp_mboxcmd(isp, &mbs);
1554 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1555 		return;
1556 	}
1557 	isp_prt(isp, ISP_LOGINFO, "Chan %d Initiator ID is %d",
1558 	    chan, sdp->isp_initiator_id);
1559 
1560 
1561 	/*
1562 	 * Set current per-target parameters to an initial safe minimum.
1563 	 */
1564 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1565 		int lun;
1566 		uint16_t sdf;
1567 
1568 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
1569 			continue;
1570 		}
1571 #ifndef	ISP_TARGET_MODE
1572 		sdf = sdp->isp_devparam[tgt].goal_flags;
1573 		sdf &= DPARM_SAFE_DFLT;
1574 		/*
1575 		 * It is not quite clear when this changed over so that
1576 		 * we could force narrow and async for 1000/1020 cards,
1577 		 * but assume that this is only the case for loaded
1578 		 * firmware.
1579 		 */
1580 		if (isp->isp_loaded_fw) {
1581 			sdf |= DPARM_NARROW | DPARM_ASYNC;
1582 		}
1583 #else
1584 		/*
1585 		 * The !$*!)$!$)* f/w uses the same index into some
1586 		 * internal table to decide how to respond to negotiations,
1587 		 * so if we've said "let's be safe" for ID X, and ID X
1588 		 * selects *us*, the negotiations will back to 'safe'
1589 		 * (as in narrow/async). What the f/w *should* do is
1590 		 * use the initiator id settings to decide how to respond.
1591 		 */
1592 		sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
1593 #endif
1594 		MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGNONE, 0);
1595 		mbs.param[1] = (chan << 15) | (tgt << 8);
1596 		mbs.param[2] = sdf;
1597 		if ((sdf & DPARM_SYNC) == 0) {
1598 			mbs.param[3] = 0;
1599 		} else {
1600 			mbs.param[3] =
1601 			    (sdp->isp_devparam[tgt].goal_offset << 8) |
1602 			    (sdp->isp_devparam[tgt].goal_period);
1603 		}
1604 		isp_prt(isp, ISP_LOGDEBUG0, "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
1605 		    chan, tgt, mbs.param[2], mbs.param[3] >> 8, mbs.param[3] & 0xff);
1606 		isp_mboxcmd(isp, &mbs);
1607 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1608 			sdf = DPARM_SAFE_DFLT;
1609 			MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGALL, 0);
1610 			mbs.param[1] = (tgt << 8) | (chan << 15);
1611 			mbs.param[2] = sdf;
1612 			mbs.param[3] = 0;
1613 			isp_mboxcmd(isp, &mbs);
1614 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1615 				continue;
1616 			}
1617 		}
1618 
1619 		/*
1620 		 * We don't update any information directly from the f/w
1621 		 * because we need to run at least one command to cause a
1622 		 * new state to be latched up. So, we just assume that we
1623 		 * converge to the values we just had set.
1624 		 *
1625 		 * Ensure that we don't believe tagged queuing is enabled yet.
1626 		 * It turns out that sometimes the ISP just ignores our
1627 		 * attempts to set parameters for devices that it hasn't
1628 		 * seen yet.
1629 		 */
1630 		sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
1631 		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
1632 			MBSINIT(&mbs, MBOX_SET_DEV_QUEUE_PARAMS, MBLOGALL, 0);
1633 			mbs.param[1] = (chan << 15) | (tgt << 8) | lun;
1634 			mbs.param[2] = sdp->isp_max_queue_depth;
1635 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
1636 			isp_mboxcmd(isp, &mbs);
1637 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1638 				break;
1639 			}
1640 		}
1641 	}
1642 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1643 		if (sdp->isp_devparam[tgt].dev_refresh) {
1644 			sdp->sendmarker = 1;
1645 			sdp->update = 1;
1646 			break;
1647 		}
1648 	}
1649 }
1650 
1651 /*
1652  * Fibre Channel specific initialization.
1653  */
1654 static void
1655 isp_fibre_init(ispsoftc_t *isp)
1656 {
1657 	fcparam *fcp;
1658 	isp_icb_t local, *icbp = &local;
1659 	mbreg_t mbs;
1660 
1661 	/*
1662 	 * We only support one channel on non-24XX cards
1663 	 */
1664 	fcp = FCPARAM(isp, 0);
1665 	if (fcp->role == ISP_ROLE_NONE)
1666 		return;
1667 
1668 	isp->isp_state = ISP_INITSTATE;
1669 	ISP_MEMZERO(icbp, sizeof (*icbp));
1670 	icbp->icb_version = ICB_VERSION1;
1671 	icbp->icb_fwoptions = fcp->isp_fwoptions;
1672 
1673 	/*
1674 	 * Firmware Options are either retrieved from NVRAM or
1675 	 * are patched elsewhere. We check them for sanity here
1676 	 * and make changes based on board revision, but otherwise
1677 	 * let others decide policy.
1678 	 */
1679 
1680 	/*
1681 	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1682 	 */
1683 	if (IS_2100(isp) && isp->isp_revision < 5) {
1684 		icbp->icb_fwoptions &= ~ICBOPT_FAIRNESS;
1685 	}
1686 
1687 	/*
1688 	 * We have to use FULL LOGIN even though it resets the loop too much
1689 	 * because otherwise port database entries don't get updated after
1690 	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1691 	 */
1692 	if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1693 		icbp->icb_fwoptions |= ICBOPT_FULL_LOGIN;
1694 	}
1695 
1696 	/*
1697 	 * Insist on Port Database Update Async notifications
1698 	 */
1699 	icbp->icb_fwoptions |= ICBOPT_PDBCHANGE_AE;
1700 
1701 	/*
1702 	 * Make sure that target role reflects into fwoptions.
1703 	 */
1704 	if (fcp->role & ISP_ROLE_TARGET) {
1705 		icbp->icb_fwoptions |= ICBOPT_TGT_ENABLE;
1706 	} else {
1707 		icbp->icb_fwoptions &= ~ICBOPT_TGT_ENABLE;
1708 	}
1709 
1710 	if (fcp->role & ISP_ROLE_INITIATOR) {
1711 		icbp->icb_fwoptions &= ~ICBOPT_INI_DISABLE;
1712 	} else {
1713 		icbp->icb_fwoptions |= ICBOPT_INI_DISABLE;
1714 	}
1715 
1716 	icbp->icb_maxfrmlen = DEFAULT_FRAMESIZE(isp);
1717 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN || icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1718 		isp_prt(isp, ISP_LOGERR, "bad frame length (%d) from NVRAM- using %d", DEFAULT_FRAMESIZE(isp), ICB_DFLT_FRMLEN);
1719 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1720 	}
1721 	icbp->icb_maxalloc = fcp->isp_maxalloc;
1722 	if (icbp->icb_maxalloc < 1) {
1723 		isp_prt(isp, ISP_LOGERR, "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1724 		icbp->icb_maxalloc = 16;
1725 	}
1726 	icbp->icb_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
1727 	if (icbp->icb_execthrottle < 1) {
1728 		isp_prt(isp, ISP_LOGERR, "bad execution throttle of %d- using %d", DEFAULT_EXEC_THROTTLE(isp), ICB_DFLT_THROTTLE);
1729 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1730 	}
1731 	icbp->icb_retry_delay = fcp->isp_retry_delay;
1732 	icbp->icb_retry_count = fcp->isp_retry_count;
1733 	if (fcp->isp_loopid < LOCAL_LOOP_LIM) {
1734 		icbp->icb_hardaddr = fcp->isp_loopid;
1735 		if (isp->isp_confopts & ISP_CFG_OWNLOOPID)
1736 			icbp->icb_fwoptions |= ICBOPT_HARD_ADDRESS;
1737 		else
1738 			icbp->icb_fwoptions |= ICBOPT_PREV_ADDRESS;
1739 	}
1740 
1741 	/*
1742 	 * Right now we just set extended options to prefer point-to-point
1743 	 * over loop based upon some soft config options.
1744 	 *
1745 	 * NB: for the 2300, ICBOPT_EXTENDED is required.
1746 	 */
1747 	if (IS_2100(isp)) {
1748 		/*
1749 		 * We can't have Fast Posting any more- we now
1750 		 * have 32 bit handles.
1751 		 */
1752 		icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1753 	} else if (IS_2200(isp) || IS_23XX(isp)) {
1754 		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1755 
1756 		icbp->icb_xfwoptions = fcp->isp_xfwoptions;
1757 
1758 		if (ISP_CAP_FCTAPE(isp)) {
1759 			if (isp->isp_confopts & ISP_CFG_NOFCTAPE)
1760 				icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
1761 
1762 			if (isp->isp_confopts & ISP_CFG_FCTAPE)
1763 				icbp->icb_xfwoptions |= ICBXOPT_FCTAPE;
1764 
1765 			if (icbp->icb_xfwoptions & ICBXOPT_FCTAPE) {
1766 				icbp->icb_fwoptions &= ~ICBOPT_FULL_LOGIN;	/* per documents */
1767 				icbp->icb_xfwoptions |= ICBXOPT_FCTAPE_CCQ|ICBXOPT_FCTAPE_CONFIRM;
1768 				FCPARAM(isp, 0)->fctape_enabled = 1;
1769 			} else {
1770 				FCPARAM(isp, 0)->fctape_enabled = 0;
1771 			}
1772 		} else {
1773 			icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
1774 			FCPARAM(isp, 0)->fctape_enabled = 0;
1775 		}
1776 
1777 		/*
1778 		 * Prefer or force Point-To-Point instead Loop?
1779 		 */
1780 		switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
1781 		case ISP_CFG_NPORT:
1782 			icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1783 			icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
1784 			break;
1785 		case ISP_CFG_NPORT_ONLY:
1786 			icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1787 			icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
1788 			break;
1789 		case ISP_CFG_LPORT_ONLY:
1790 			icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1791 			icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
1792 			break;
1793 		default:
1794 			/*
1795 			 * Let NVRAM settings define it if they are sane
1796 			 */
1797 			switch (icbp->icb_xfwoptions & ICBXOPT_TOPO_MASK) {
1798 			case ICBXOPT_PTP_2_LOOP:
1799 			case ICBXOPT_PTP_ONLY:
1800 			case ICBXOPT_LOOP_ONLY:
1801 			case ICBXOPT_LOOP_2_PTP:
1802 				break;
1803 			default:
1804 				icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1805 				icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
1806 			}
1807 			break;
1808 		}
1809 		if (IS_2200(isp)) {
1810 			/*
1811 			 * We can't have Fast Posting any more- we now
1812 			 * have 32 bit handles.
1813 			 *
1814 			 * RIO seemed to have to much breakage.
1815 			 *
1816 			 * Just opt for safety.
1817 			 */
1818 			icbp->icb_xfwoptions &= ~ICBXOPT_RIO_16BIT;
1819 			icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1820 		} else {
1821 			/*
1822 			 * QLogic recommends that FAST Posting be turned
1823 			 * off for 23XX cards and instead allow the HBA
1824 			 * to write response queue entries and interrupt
1825 			 * after a delay (ZIO).
1826 			 */
1827 			icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1828 			if ((fcp->isp_xfwoptions & ICBXOPT_TIMER_MASK) == ICBXOPT_ZIO) {
1829 				icbp->icb_xfwoptions |= ICBXOPT_ZIO;
1830 				icbp->icb_idelaytimer = 10;
1831 			}
1832 			icbp->icb_zfwoptions = fcp->isp_zfwoptions;
1833 			if (isp->isp_confopts & ISP_CFG_ONEGB) {
1834 				icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1835 				icbp->icb_zfwoptions |= ICBZOPT_RATE_ONEGB;
1836 			} else if (isp->isp_confopts & ISP_CFG_TWOGB) {
1837 				icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1838 				icbp->icb_zfwoptions |= ICBZOPT_RATE_TWOGB;
1839 			} else {
1840 				switch (icbp->icb_zfwoptions & ICBZOPT_RATE_MASK) {
1841 				case ICBZOPT_RATE_ONEGB:
1842 				case ICBZOPT_RATE_TWOGB:
1843 				case ICBZOPT_RATE_AUTO:
1844 					break;
1845 				default:
1846 					icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1847 					icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
1848 					break;
1849 				}
1850 			}
1851 		}
1852 	}
1853 
1854 
1855 	/*
1856 	 * For 22XX > 2.1.26 && 23XX, set some options.
1857 	 */
1858 	if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
1859 		MBSINIT(&mbs, MBOX_SET_FIRMWARE_OPTIONS, MBLOGALL, 0);
1860 		mbs.param[1] = IFCOPT1_DISF7SWTCH|IFCOPT1_LIPASYNC|IFCOPT1_LIPF8;
1861 		mbs.param[2] = 0;
1862 		mbs.param[3] = 0;
1863 		if (ISP_FW_NEWER_THAN(isp, 3, 16, 0)) {
1864 			mbs.param[1] |= IFCOPT1_EQFQASYNC|IFCOPT1_CTIO_RETRY;
1865 			if (fcp->role & ISP_ROLE_TARGET) {
1866 				if (ISP_FW_NEWER_THAN(isp, 3, 25, 0)) {
1867 					mbs.param[1] |= IFCOPT1_ENAPURE;
1868 				}
1869 				mbs.param[3] = IFCOPT3_NOPRLI;
1870 			}
1871 		}
1872 		isp_mboxcmd(isp, &mbs);
1873 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1874 			return;
1875 		}
1876 	}
1877 	icbp->icb_logintime = ICB_LOGIN_TOV;
1878 
1879 #ifdef	ISP_TARGET_MODE
1880 	if (IS_23XX(isp) && (icbp->icb_fwoptions & ICBOPT_TGT_ENABLE)) {
1881 		icbp->icb_lunenables = 0xffff;
1882 		icbp->icb_ccnt = DFLT_CMND_CNT;
1883 		icbp->icb_icnt = DFLT_INOT_CNT;
1884 		icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
1885 	}
1886 #endif
1887 	if (fcp->isp_wwnn && fcp->isp_wwpn) {
1888 		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
1889 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
1890 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1891 		isp_prt(isp, ISP_LOGDEBUG1,
1892 		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1893 		    ((uint32_t) (fcp->isp_wwnn >> 32)),
1894 		    ((uint32_t) (fcp->isp_wwnn)),
1895 		    ((uint32_t) (fcp->isp_wwpn >> 32)),
1896 		    ((uint32_t) (fcp->isp_wwpn)));
1897 	} else if (fcp->isp_wwpn) {
1898 		icbp->icb_fwoptions &= ~ICBOPT_BOTH_WWNS;
1899 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1900 		isp_prt(isp, ISP_LOGDEBUG1,
1901 		    "Setting ICB Port 0x%08x%08x",
1902 		    ((uint32_t) (fcp->isp_wwpn >> 32)),
1903 		    ((uint32_t) (fcp->isp_wwpn)));
1904 	} else {
1905 		isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
1906 		return;
1907 	}
1908 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1909 	if (icbp->icb_rqstqlen < 1) {
1910 		isp_prt(isp, ISP_LOGERR, "bad request queue length");
1911 	}
1912 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1913 	if (icbp->icb_rsltqlen < 1) {
1914 		isp_prt(isp, ISP_LOGERR, "bad result queue length");
1915 	}
1916 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1917 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1918 	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1919 	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1920 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1921 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1922 	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1923 	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1924 
1925 	if (FC_SCRATCH_ACQUIRE(isp, 0)) {
1926 		isp_prt(isp, ISP_LOGERR, sacq);
1927 		return;
1928 	}
1929 	isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
1930 	    icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
1931 
1932 	isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
1933 
1934 	/*
1935 	 * Init the firmware
1936 	 */
1937 	MBSINIT(&mbs, MBOX_INIT_FIRMWARE, MBLOGALL, 30000000);
1938 	mbs.param[1] = 0;
1939 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1940 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1941 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1942 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1943 	mbs.logval = MBLOGALL;
1944 	isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %p (%08x%08x)",
1945 	    fcp->isp_scratch, (uint32_t) ((uint64_t)fcp->isp_scdma >> 32),
1946 	    (uint32_t) fcp->isp_scdma);
1947 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp), 0);
1948 	isp_mboxcmd(isp, &mbs);
1949 	FC_SCRATCH_RELEASE(isp, 0);
1950 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1951 		isp_print_bytes(isp, "isp_fibre_init", sizeof (*icbp), icbp);
1952 		return;
1953 	}
1954 	isp->isp_reqidx = 0;
1955 	isp->isp_reqodx = 0;
1956 	isp->isp_residx = 0;
1957 	isp->isp_resodx = 0;
1958 
1959 	/*
1960 	 * Whatever happens, we're now committed to being here.
1961 	 */
1962 	isp->isp_state = ISP_RUNSTATE;
1963 }
1964 
1965 static void
1966 isp_fibre_init_2400(ispsoftc_t *isp)
1967 {
1968 	fcparam *fcp;
1969 	isp_icb_2400_t local, *icbp = &local;
1970 	mbreg_t mbs;
1971 	int chan;
1972 
1973 	/*
1974 	 * Check to see whether all channels have *some* kind of role
1975 	 */
1976 	for (chan = 0; chan < isp->isp_nchan; chan++) {
1977 		fcp = FCPARAM(isp, chan);
1978 		if (fcp->role != ISP_ROLE_NONE) {
1979 			break;
1980 		}
1981 	}
1982 	if (chan == isp->isp_nchan) {
1983 		isp_prt(isp, ISP_LOG_WARN1, "all %d channels with role 'none'", chan);
1984 		return;
1985 	}
1986 
1987 	isp->isp_state = ISP_INITSTATE;
1988 
1989 	/*
1990 	 * Start with channel 0.
1991 	 */
1992 	fcp = FCPARAM(isp, 0);
1993 
1994 	/*
1995 	 * Turn on LIP F8 async event (1)
1996 	 */
1997 	MBSINIT(&mbs, MBOX_SET_FIRMWARE_OPTIONS, MBLOGALL, 0);
1998 	mbs.param[1] = 1;
1999 	isp_mboxcmd(isp, &mbs);
2000 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2001 		return;
2002 	}
2003 
2004 	ISP_MEMZERO(icbp, sizeof (*icbp));
2005 	icbp->icb_fwoptions1 = fcp->isp_fwoptions;
2006 	icbp->icb_fwoptions2 = fcp->isp_xfwoptions;
2007 	icbp->icb_fwoptions3 = fcp->isp_zfwoptions;
2008 	if (isp->isp_nchan > 1 && ISP_CAP_VP0(isp)) {
2009 		icbp->icb_fwoptions1 &= ~ICB2400_OPT1_INI_DISABLE;
2010 		icbp->icb_fwoptions1 |= ICB2400_OPT1_TGT_ENABLE;
2011 	} else {
2012 		if (fcp->role & ISP_ROLE_TARGET)
2013 			icbp->icb_fwoptions1 |= ICB2400_OPT1_TGT_ENABLE;
2014 		else
2015 			icbp->icb_fwoptions1 &= ~ICB2400_OPT1_TGT_ENABLE;
2016 		if (fcp->role & ISP_ROLE_INITIATOR)
2017 			icbp->icb_fwoptions1 &= ~ICB2400_OPT1_INI_DISABLE;
2018 		else
2019 			icbp->icb_fwoptions1 |= ICB2400_OPT1_INI_DISABLE;
2020 	}
2021 
2022 	icbp->icb_version = ICB_VERSION1;
2023 	icbp->icb_maxfrmlen = DEFAULT_FRAMESIZE(isp);
2024 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN || icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
2025 		isp_prt(isp, ISP_LOGERR, "bad frame length (%d) from NVRAM- using %d", DEFAULT_FRAMESIZE(isp), ICB_DFLT_FRMLEN);
2026 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
2027 	}
2028 
2029 	icbp->icb_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
2030 	if (icbp->icb_execthrottle < 1) {
2031 		isp_prt(isp, ISP_LOGERR, "bad execution throttle of %d- using %d", DEFAULT_EXEC_THROTTLE(isp), ICB_DFLT_THROTTLE);
2032 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
2033 	}
2034 
2035 	/*
2036 	 * Set target exchange count. Take half if we are supporting both roles.
2037 	 */
2038 	if (icbp->icb_fwoptions1 & ICB2400_OPT1_TGT_ENABLE) {
2039 		icbp->icb_xchgcnt = isp->isp_maxcmds;
2040 		if ((icbp->icb_fwoptions1 & ICB2400_OPT1_INI_DISABLE) == 0)
2041 			icbp->icb_xchgcnt >>= 1;
2042 	}
2043 
2044 	if (fcp->isp_loopid < LOCAL_LOOP_LIM) {
2045 		icbp->icb_hardaddr = fcp->isp_loopid;
2046 		if (isp->isp_confopts & ISP_CFG_OWNLOOPID)
2047 			icbp->icb_fwoptions1 |= ICB2400_OPT1_HARD_ADDRESS;
2048 		else
2049 			icbp->icb_fwoptions1 |= ICB2400_OPT1_PREV_ADDRESS;
2050 	}
2051 
2052 	if (isp->isp_confopts & ISP_CFG_NOFCTAPE) {
2053 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_FCTAPE;
2054 	}
2055 	if (isp->isp_confopts & ISP_CFG_FCTAPE) {
2056 		icbp->icb_fwoptions2 |= ICB2400_OPT2_FCTAPE;
2057 	}
2058 
2059 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2060 		if (icbp->icb_fwoptions2 & ICB2400_OPT2_FCTAPE)
2061 			FCPARAM(isp, chan)->fctape_enabled = 1;
2062 		else
2063 			FCPARAM(isp, chan)->fctape_enabled = 0;
2064 	}
2065 
2066 	switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
2067 	case ISP_CFG_NPORT_ONLY:
2068 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
2069 		icbp->icb_fwoptions2 |= ICB2400_OPT2_PTP_ONLY;
2070 		break;
2071 	case ISP_CFG_LPORT_ONLY:
2072 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
2073 		icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_ONLY;
2074 		break;
2075 	default:
2076 		/* ISP_CFG_PTP_2_LOOP not available in 24XX/25XX */
2077 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
2078 		icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_2_PTP;
2079 		break;
2080 	}
2081 
2082 	switch (icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK) {
2083 	case ICB2400_OPT2_ZIO:
2084 	case ICB2400_OPT2_ZIO1:
2085 		icbp->icb_idelaytimer = 0;
2086 		break;
2087 	case 0:
2088 		break;
2089 	default:
2090 		isp_prt(isp, ISP_LOGWARN, "bad value %x in fwopt2 timer field", icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK);
2091 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TIMER_MASK;
2092 		break;
2093 	}
2094 
2095 	if ((icbp->icb_fwoptions3 & ICB2400_OPT3_RSPSZ_MASK) == 0) {
2096 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RSPSZ_24;
2097 	}
2098 	icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_AUTO;
2099 	if (isp->isp_confopts & ISP_CFG_ONEGB) {
2100 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_ONEGB;
2101 	} else if (isp->isp_confopts & ISP_CFG_TWOGB) {
2102 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_TWOGB;
2103 	} else if (isp->isp_confopts & ISP_CFG_FOURGB) {
2104 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_FOURGB;
2105 	} else if (IS_25XX(isp) && (isp->isp_confopts & ISP_CFG_EIGHTGB)) {
2106 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_EIGHTGB;
2107 	} else {
2108 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_AUTO;
2109 	}
2110 	icbp->icb_logintime = ICB_LOGIN_TOV;
2111 
2112 	if (fcp->isp_wwnn && fcp->isp_wwpn) {
2113 		icbp->icb_fwoptions1 |= ICB2400_OPT1_BOTH_WWNS;
2114 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
2115 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
2116 		isp_prt(isp, ISP_LOGDEBUG1, "Setting ICB Node 0x%08x%08x Port 0x%08x%08x", ((uint32_t) (fcp->isp_wwnn >> 32)), ((uint32_t) (fcp->isp_wwnn)),
2117 		    ((uint32_t) (fcp->isp_wwpn >> 32)), ((uint32_t) (fcp->isp_wwpn)));
2118 	} else if (fcp->isp_wwpn) {
2119 		icbp->icb_fwoptions1 &= ~ICB2400_OPT1_BOTH_WWNS;
2120 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
2121 		isp_prt(isp, ISP_LOGDEBUG1, "Setting ICB Node to be same as Port 0x%08x%08x", ((uint32_t) (fcp->isp_wwpn >> 32)), ((uint32_t) (fcp->isp_wwpn)));
2122 	} else {
2123 		isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
2124 		return;
2125 	}
2126 	icbp->icb_retry_count = fcp->isp_retry_count;
2127 
2128 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
2129 	if (icbp->icb_rqstqlen < 8) {
2130 		isp_prt(isp, ISP_LOGERR, "bad request queue length %d", icbp->icb_rqstqlen);
2131 		return;
2132 	}
2133 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
2134 	if (icbp->icb_rsltqlen < 8) {
2135 		isp_prt(isp, ISP_LOGERR, "bad result queue length %d",
2136 		    icbp->icb_rsltqlen);
2137 		return;
2138 	}
2139 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
2140 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
2141 	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
2142 	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
2143 
2144 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
2145 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
2146 	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
2147 	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
2148 
2149 #ifdef	ISP_TARGET_MODE
2150 	/* unconditionally set up the ATIO queue if we support target mode */
2151 	icbp->icb_atioqlen = RESULT_QUEUE_LEN(isp);
2152 	if (icbp->icb_atioqlen < 8) {
2153 		isp_prt(isp, ISP_LOGERR, "bad ATIO queue length %d", icbp->icb_atioqlen);
2154 		return;
2155 	}
2156 	icbp->icb_atioqaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_atioq_dma);
2157 	icbp->icb_atioqaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_atioq_dma);
2158 	icbp->icb_atioqaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_atioq_dma);
2159 	icbp->icb_atioqaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_atioq_dma);
2160 	isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init_2400: atioq %04x%04x%04x%04x", DMA_WD3(isp->isp_atioq_dma), DMA_WD2(isp->isp_atioq_dma),
2161 	    DMA_WD1(isp->isp_atioq_dma), DMA_WD0(isp->isp_atioq_dma));
2162 #endif
2163 
2164 	isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init_2400: fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x", icbp->icb_fwoptions1, icbp->icb_fwoptions2, icbp->icb_fwoptions3);
2165 
2166 	isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init_2400: rqst %04x%04x%04x%04x rsp %04x%04x%04x%04x", DMA_WD3(isp->isp_rquest_dma), DMA_WD2(isp->isp_rquest_dma),
2167 	    DMA_WD1(isp->isp_rquest_dma), DMA_WD0(isp->isp_rquest_dma), DMA_WD3(isp->isp_result_dma), DMA_WD2(isp->isp_result_dma),
2168 	    DMA_WD1(isp->isp_result_dma), DMA_WD0(isp->isp_result_dma));
2169 
2170 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
2171 		isp_print_bytes(isp, "isp_fibre_init_2400", sizeof (*icbp), icbp);
2172 	}
2173 
2174 	if (FC_SCRATCH_ACQUIRE(isp, 0)) {
2175 		isp_prt(isp, ISP_LOGERR, sacq);
2176 		return;
2177 	}
2178 	ISP_MEMZERO(fcp->isp_scratch, ISP_FC_SCRLEN);
2179 	isp_put_icb_2400(isp, icbp, fcp->isp_scratch);
2180 
2181 	/*
2182 	 * Now fill in information about any additional channels
2183 	 */
2184 	if (isp->isp_nchan > 1) {
2185 		isp_icb_2400_vpinfo_t vpinfo, *vdst;
2186 		vp_port_info_t pi, *pdst;
2187 		size_t amt = 0;
2188 		uint8_t *off;
2189 
2190 		vpinfo.vp_global_options = 0;
2191 		if (ISP_CAP_VP0(isp)) {
2192 			vpinfo.vp_global_options |= ICB2400_VPGOPT_VP0_DECOUPLE;
2193 			vpinfo.vp_count = isp->isp_nchan;
2194 			chan = 0;
2195 		} else {
2196 			vpinfo.vp_count = isp->isp_nchan - 1;
2197 			chan = 1;
2198 		}
2199 		off = fcp->isp_scratch;
2200 		off += ICB2400_VPINFO_OFF;
2201 		vdst = (isp_icb_2400_vpinfo_t *) off;
2202 		isp_put_icb_2400_vpinfo(isp, &vpinfo, vdst);
2203 		amt = ICB2400_VPINFO_OFF + sizeof (isp_icb_2400_vpinfo_t);
2204 		for (; chan < isp->isp_nchan; chan++) {
2205 			fcparam *fcp2;
2206 
2207 			ISP_MEMZERO(&pi, sizeof (pi));
2208 			fcp2 = FCPARAM(isp, chan);
2209 			if (fcp2->role != ISP_ROLE_NONE) {
2210 				pi.vp_port_options = ICB2400_VPOPT_ENABLED;
2211 				if (fcp2->role & ISP_ROLE_INITIATOR)
2212 					pi.vp_port_options |= ICB2400_VPOPT_INI_ENABLE;
2213 				if ((fcp2->role & ISP_ROLE_TARGET) == 0)
2214 					pi.vp_port_options |= ICB2400_VPOPT_TGT_DISABLE;
2215 			}
2216 			if (fcp2->isp_loopid < LOCAL_LOOP_LIM) {
2217 				pi.vp_port_loopid = fcp2->isp_loopid;
2218 				if (isp->isp_confopts & ISP_CFG_OWNLOOPID)
2219 					pi.vp_port_options |= ICB2400_VPOPT_HARD_ADDRESS;
2220 				else
2221 					pi.vp_port_options |= ICB2400_VPOPT_PREV_ADDRESS;
2222 			}
2223 			MAKE_NODE_NAME_FROM_WWN(pi.vp_port_portname, fcp2->isp_wwpn);
2224 			MAKE_NODE_NAME_FROM_WWN(pi.vp_port_nodename, fcp2->isp_wwnn);
2225 			off = fcp->isp_scratch;
2226 			if (ISP_CAP_VP0(isp))
2227 				off += ICB2400_VPINFO_PORT_OFF(chan);
2228 			else
2229 				off += ICB2400_VPINFO_PORT_OFF(chan - 1);
2230 			pdst = (vp_port_info_t *) off;
2231 			isp_put_vp_port_info(isp, &pi, pdst);
2232 			amt += ICB2400_VPOPT_WRITE_SIZE;
2233 		}
2234 		if (isp->isp_dblev & ISP_LOGDEBUG1) {
2235 			isp_print_bytes(isp, "isp_fibre_init_2400",
2236 			    amt - ICB2400_VPINFO_OFF,
2237 			    (char *)fcp->isp_scratch + ICB2400_VPINFO_OFF);
2238 		}
2239 	}
2240 
2241 	/*
2242 	 * Init the firmware
2243 	 */
2244 	MBSINIT(&mbs, 0, MBLOGALL, 30000000);
2245 	if (isp->isp_nchan > 1) {
2246 		mbs.param[0] = MBOX_INIT_FIRMWARE_MULTI_ID;
2247 	} else {
2248 		mbs.param[0] = MBOX_INIT_FIRMWARE;
2249 	}
2250 	mbs.param[1] = 0;
2251 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2252 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2253 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2254 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2255 	isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %04x%04x%04x%04x", DMA_WD3(fcp->isp_scdma), DMA_WD2(fcp->isp_scdma), DMA_WD1(fcp->isp_scdma), DMA_WD0(fcp->isp_scdma));
2256 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp), 0);
2257 	isp_mboxcmd(isp, &mbs);
2258 	FC_SCRATCH_RELEASE(isp, 0);
2259 
2260 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2261 		return;
2262 	}
2263 	isp->isp_reqidx = 0;
2264 	isp->isp_reqodx = 0;
2265 	isp->isp_residx = 0;
2266 	isp->isp_resodx = 0;
2267 	isp->isp_atioodx = 0;
2268 
2269 	/*
2270 	 * Whatever happens, we're now committed to being here.
2271 	 */
2272 	isp->isp_state = ISP_RUNSTATE;
2273 }
2274 
2275 static void
2276 isp_mark_portdb(ispsoftc_t *isp, int chan, int disposition)
2277 {
2278 	fcparam *fcp = FCPARAM(isp, chan);
2279 	fcportdb_t *lp;
2280 	int i;
2281 
2282 	for (i = 0; i < MAX_FC_TARG; i++) {
2283 		lp = &fcp->portdb[i];
2284 		switch (lp->state) {
2285 		case FC_PORTDB_STATE_PROBATIONAL:
2286 		case FC_PORTDB_STATE_DEAD:
2287 		case FC_PORTDB_STATE_CHANGED:
2288 		case FC_PORTDB_STATE_PENDING_VALID:
2289 		case FC_PORTDB_STATE_VALID:
2290 			if (disposition > 0)
2291 				lp->state = FC_PORTDB_STATE_PROBATIONAL;
2292 			else {
2293 				lp->state = FC_PORTDB_STATE_NIL;
2294 				isp_async(isp, ISPASYNC_DEV_GONE, chan, lp);
2295 			}
2296 			break;
2297 		case FC_PORTDB_STATE_ZOMBIE:
2298 			break;
2299 		case FC_PORTDB_STATE_NIL:
2300 		case FC_PORTDB_STATE_NEW:
2301 		default:
2302 			ISP_MEMZERO(lp, sizeof(*lp));
2303 			lp->state = FC_PORTDB_STATE_NIL;
2304 			break;
2305 		}
2306 	}
2307 }
2308 
2309 /*
2310  * Perform an IOCB PLOGI or LOGO via EXECUTE IOCB A64 for 24XX cards
2311  * or via FABRIC LOGIN/FABRIC LOGOUT for other cards.
2312  */
2313 static int
2314 isp_plogx(ispsoftc_t *isp, int chan, uint16_t handle, uint32_t portid, int flags, int gs)
2315 {
2316 	mbreg_t mbs;
2317 	uint8_t q[QENTRY_LEN];
2318 	isp_plogx_t *plp;
2319 	fcparam *fcp;
2320 	uint8_t *scp;
2321 	uint32_t sst, parm1;
2322 	int rval, lev;
2323 	const char *msg;
2324 	char buf[64];
2325 
2326 	if (!IS_24XX(isp)) {
2327 		int action = flags & PLOGX_FLG_CMD_MASK;
2328 		if (action == PLOGX_FLG_CMD_PLOGI) {
2329 			return (isp_port_login(isp, handle, portid));
2330 		} else if (action == PLOGX_FLG_CMD_LOGO) {
2331 			return (isp_port_logout(isp, handle, portid));
2332 		} else {
2333 			return (MBOX_INVALID_COMMAND);
2334 		}
2335 	}
2336 
2337 	ISP_MEMZERO(q, QENTRY_LEN);
2338 	plp = (isp_plogx_t *) q;
2339 	plp->plogx_header.rqs_entry_count = 1;
2340 	plp->plogx_header.rqs_entry_type = RQSTYPE_LOGIN;
2341 	plp->plogx_handle = 0xffffffff;
2342 	plp->plogx_nphdl = handle;
2343 	plp->plogx_vphdl = chan;
2344 	plp->plogx_portlo = portid;
2345 	plp->plogx_rspsz_porthi = (portid >> 16) & 0xff;
2346 	plp->plogx_flags = flags;
2347 
2348 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
2349 		isp_print_bytes(isp, "IOCB LOGX", QENTRY_LEN, plp);
2350 	}
2351 
2352 	if (gs == 0) {
2353 		if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2354 			isp_prt(isp, ISP_LOGERR, sacq);
2355 			return (-1);
2356 		}
2357 	}
2358 	fcp = FCPARAM(isp, chan);
2359 	scp = fcp->isp_scratch;
2360 	isp_put_plogx(isp, plp, (isp_plogx_t *) scp);
2361 
2362 	MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 500000);
2363 	mbs.param[1] = QENTRY_LEN;
2364 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2365 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2366 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2367 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2368 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
2369 	isp_mboxcmd(isp, &mbs);
2370 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2371 		rval = mbs.param[0];
2372 		goto out;
2373 	}
2374 	MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
2375 	scp += QENTRY_LEN;
2376 	isp_get_plogx(isp, (isp_plogx_t *) scp, plp);
2377 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
2378 		isp_print_bytes(isp, "IOCB LOGX response", QENTRY_LEN, plp);
2379 	}
2380 
2381 	if (plp->plogx_status == PLOGX_STATUS_OK) {
2382 		rval = 0;
2383 		goto out;
2384 	} else if (plp->plogx_status != PLOGX_STATUS_IOCBERR) {
2385 		isp_prt(isp, ISP_LOGWARN,
2386 		    "status 0x%x on port login IOCB channel %d",
2387 		    plp->plogx_status, chan);
2388 		rval = -1;
2389 		goto out;
2390 	}
2391 
2392 	sst = plp->plogx_ioparm[0].lo16 | (plp->plogx_ioparm[0].hi16 << 16);
2393 	parm1 = plp->plogx_ioparm[1].lo16 | (plp->plogx_ioparm[1].hi16 << 16);
2394 
2395 	rval = -1;
2396 	lev = ISP_LOGERR;
2397 	msg = NULL;
2398 
2399 	switch (sst) {
2400 	case PLOGX_IOCBERR_NOLINK:
2401 		msg = "no link";
2402 		break;
2403 	case PLOGX_IOCBERR_NOIOCB:
2404 		msg = "no IOCB buffer";
2405 		break;
2406 	case PLOGX_IOCBERR_NOXGHG:
2407 		msg = "no Exchange Control Block";
2408 		break;
2409 	case PLOGX_IOCBERR_FAILED:
2410 		ISP_SNPRINTF(buf, sizeof (buf), "reason 0x%x (last LOGIN state 0x%x)", parm1 & 0xff, (parm1 >> 8) & 0xff);
2411 		msg = buf;
2412 		break;
2413 	case PLOGX_IOCBERR_NOFABRIC:
2414 		msg = "no fabric";
2415 		break;
2416 	case PLOGX_IOCBERR_NOTREADY:
2417 		msg = "firmware not ready";
2418 		break;
2419 	case PLOGX_IOCBERR_NOLOGIN:
2420 		ISP_SNPRINTF(buf, sizeof (buf), "not logged in (last state 0x%x)", parm1);
2421 		msg = buf;
2422 		rval = MBOX_NOT_LOGGED_IN;
2423 		break;
2424 	case PLOGX_IOCBERR_REJECT:
2425 		ISP_SNPRINTF(buf, sizeof (buf), "LS_RJT = 0x%x", parm1);
2426 		msg = buf;
2427 		break;
2428 	case PLOGX_IOCBERR_NOPCB:
2429 		msg = "no PCB allocated";
2430 		break;
2431 	case PLOGX_IOCBERR_EINVAL:
2432 		ISP_SNPRINTF(buf, sizeof (buf), "invalid parameter at offset 0x%x", parm1);
2433 		msg = buf;
2434 		break;
2435 	case PLOGX_IOCBERR_PORTUSED:
2436 		lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
2437 		ISP_SNPRINTF(buf, sizeof (buf), "already logged in with N-Port handle 0x%x", parm1);
2438 		msg = buf;
2439 		rval = MBOX_PORT_ID_USED | (parm1 << 16);
2440 		break;
2441 	case PLOGX_IOCBERR_HNDLUSED:
2442 		lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
2443 		ISP_SNPRINTF(buf, sizeof (buf), "handle already used for PortID 0x%06x", parm1);
2444 		msg = buf;
2445 		rval = MBOX_LOOP_ID_USED;
2446 		break;
2447 	case PLOGX_IOCBERR_NOHANDLE:
2448 		msg = "no handle allocated";
2449 		break;
2450 	case PLOGX_IOCBERR_NOFLOGI:
2451 		msg = "no FLOGI_ACC";
2452 		break;
2453 	default:
2454 		ISP_SNPRINTF(buf, sizeof (buf), "status %x from %x", plp->plogx_status, flags);
2455 		msg = buf;
2456 		break;
2457 	}
2458 	if (msg) {
2459 		isp_prt(isp, ISP_LOGERR, "Chan %d PLOGX PortID 0x%06x to N-Port handle 0x%x: %s", chan, portid, handle, msg);
2460 	}
2461 out:
2462 	if (gs == 0) {
2463 		FC_SCRATCH_RELEASE(isp, chan);
2464 	}
2465 	return (rval);
2466 }
2467 
2468 static int
2469 isp_port_login(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2470 {
2471 	mbreg_t mbs;
2472 
2473 	MBSINIT(&mbs, MBOX_FABRIC_LOGIN, MBLOGNONE, 500000);
2474 	if (ISP_CAP_2KLOGIN(isp)) {
2475 		mbs.param[1] = handle;
2476 		mbs.ibits = (1 << 10);
2477 	} else {
2478 		mbs.param[1] = handle << 8;
2479 	}
2480 	mbs.param[2] = portid >> 16;
2481 	mbs.param[3] = portid;
2482 	mbs.logval = MBLOGNONE;
2483 	mbs.timeout = 500000;
2484 	isp_mboxcmd(isp, &mbs);
2485 
2486 	switch (mbs.param[0]) {
2487 	case MBOX_PORT_ID_USED:
2488 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: portid 0x%06x already logged in as %u", portid, mbs.param[1]);
2489 		return (MBOX_PORT_ID_USED | (mbs.param[1] << 16));
2490 
2491 	case MBOX_LOOP_ID_USED:
2492 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: handle 0x%x in use for port id 0x%02xXXXX", handle, mbs.param[1] & 0xff);
2493 		return (MBOX_LOOP_ID_USED);
2494 
2495 	case MBOX_COMMAND_COMPLETE:
2496 		return (0);
2497 
2498 	case MBOX_COMMAND_ERROR:
2499 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: error 0x%x in PLOGI to port 0x%06x", mbs.param[1], portid);
2500 		return (MBOX_COMMAND_ERROR);
2501 
2502 	case MBOX_ALL_IDS_USED:
2503 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: all IDs used for fabric login");
2504 		return (MBOX_ALL_IDS_USED);
2505 
2506 	default:
2507 		isp_prt(isp, ISP_LOG_SANCFG, "isp_port_login: error 0x%x on port login of 0x%06x@0x%0x", mbs.param[0], portid, handle);
2508 		return (mbs.param[0]);
2509 	}
2510 }
2511 
2512 /*
2513  * Pre-24XX fabric port logout
2514  *
2515  * Note that portid is not used
2516  */
2517 static int
2518 isp_port_logout(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2519 {
2520 	mbreg_t mbs;
2521 
2522 	MBSINIT(&mbs, MBOX_FABRIC_LOGOUT, MBLOGNONE, 500000);
2523 	if (ISP_CAP_2KLOGIN(isp)) {
2524 		mbs.param[1] = handle;
2525 		mbs.ibits = (1 << 10);
2526 	} else {
2527 		mbs.param[1] = handle << 8;
2528 	}
2529 	isp_mboxcmd(isp, &mbs);
2530 	return (mbs.param[0] == MBOX_COMMAND_COMPLETE? 0 : mbs.param[0]);
2531 }
2532 
2533 static int
2534 isp_getpdb(ispsoftc_t *isp, int chan, uint16_t id, isp_pdb_t *pdb, int dolock)
2535 {
2536 	fcparam *fcp = FCPARAM(isp, chan);
2537 	mbreg_t mbs;
2538 	union {
2539 		isp_pdb_21xx_t fred;
2540 		isp_pdb_24xx_t bill;
2541 	} un;
2542 
2543 	MBSINIT(&mbs, MBOX_GET_PORT_DB,
2544 	    MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_PARAM_ERROR), 250000);
2545 	if (IS_24XX(isp)) {
2546 		mbs.ibits = (1 << 9)|(1 << 10);
2547 		mbs.param[1] = id;
2548 		mbs.param[9] = chan;
2549 	} else if (ISP_CAP_2KLOGIN(isp)) {
2550 		mbs.param[1] = id;
2551 	} else {
2552 		mbs.param[1] = id << 8;
2553 	}
2554 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2555 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2556 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2557 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2558 	if (dolock) {
2559 		if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2560 			isp_prt(isp, ISP_LOGERR, sacq);
2561 			return (-1);
2562 		}
2563 	}
2564 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (un), chan);
2565 	isp_mboxcmd(isp, &mbs);
2566 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2567 		if (dolock) {
2568 			FC_SCRATCH_RELEASE(isp, chan);
2569 		}
2570 		return (mbs.param[0] | (mbs.param[1] << 16));
2571 	}
2572 	if (IS_24XX(isp)) {
2573 		isp_get_pdb_24xx(isp, fcp->isp_scratch, &un.bill);
2574 		pdb->handle = un.bill.pdb_handle;
2575 		pdb->prli_word3 = un.bill.pdb_prli_svc3;
2576 		pdb->portid = BITS2WORD_24XX(un.bill.pdb_portid_bits);
2577 		ISP_MEMCPY(pdb->portname, un.bill.pdb_portname, 8);
2578 		ISP_MEMCPY(pdb->nodename, un.bill.pdb_nodename, 8);
2579 		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d handle 0x%x Port 0x%06x flags 0x%x curstate %x", chan, id, pdb->portid, un.bill.pdb_flags, un.bill.pdb_curstate);
2580 		if (un.bill.pdb_curstate < PDB2400_STATE_PLOGI_DONE || un.bill.pdb_curstate > PDB2400_STATE_LOGGED_IN) {
2581 			mbs.param[0] = MBOX_NOT_LOGGED_IN;
2582 			if (dolock) {
2583 				FC_SCRATCH_RELEASE(isp, chan);
2584 			}
2585 			return (mbs.param[0]);
2586 		}
2587 	} else {
2588 		isp_get_pdb_21xx(isp, fcp->isp_scratch, &un.fred);
2589 		pdb->handle = un.fred.pdb_loopid;
2590 		pdb->prli_word3 = un.fred.pdb_prli_svc3;
2591 		pdb->portid = BITS2WORD(un.fred.pdb_portid_bits);
2592 		ISP_MEMCPY(pdb->portname, un.fred.pdb_portname, 8);
2593 		ISP_MEMCPY(pdb->nodename, un.fred.pdb_nodename, 8);
2594 	}
2595 	if (dolock) {
2596 		FC_SCRATCH_RELEASE(isp, chan);
2597 	}
2598 	return (0);
2599 }
2600 
2601 static int
2602 isp_gethandles(ispsoftc_t *isp, int chan, uint16_t *handles, int *num,
2603     int dolock, int loop)
2604 {
2605 	fcparam *fcp = FCPARAM(isp, chan);
2606 	mbreg_t mbs;
2607 	isp_pnhle_21xx_t el1, *elp1;
2608 	isp_pnhle_23xx_t el3, *elp3;
2609 	isp_pnhle_24xx_t el4, *elp4;
2610 	int i, j;
2611 	uint32_t p;
2612 	uint16_t h;
2613 
2614 	MBSINIT(&mbs, MBOX_GET_ID_LIST, MBLOGALL, 250000);
2615 	if (IS_24XX(isp)) {
2616 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2617 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2618 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2619 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2620 		mbs.param[8] = ISP_FC_SCRLEN;
2621 		mbs.param[9] = chan;
2622 	} else {
2623 		mbs.ibits = (1 << 1)|(1 << 2)|(1 << 3)|(1 << 6);
2624 		mbs.param[1] = DMA_WD1(fcp->isp_scdma);
2625 		mbs.param[2] = DMA_WD0(fcp->isp_scdma);
2626 		mbs.param[3] = DMA_WD3(fcp->isp_scdma);
2627 		mbs.param[6] = DMA_WD2(fcp->isp_scdma);
2628 	}
2629 	if (dolock) {
2630 		if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2631 			isp_prt(isp, ISP_LOGERR, sacq);
2632 			return (-1);
2633 		}
2634 	}
2635 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, ISP_FC_SCRLEN, chan);
2636 	isp_mboxcmd(isp, &mbs);
2637 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2638 		if (dolock) {
2639 			FC_SCRATCH_RELEASE(isp, chan);
2640 		}
2641 		return (mbs.param[0] | (mbs.param[1] << 16));
2642 	}
2643 	elp1 = fcp->isp_scratch;
2644 	elp3 = fcp->isp_scratch;
2645 	elp4 = fcp->isp_scratch;
2646 	for (i = 0, j = 0; i < mbs.param[1] && j < *num; i++) {
2647 		if (IS_24XX(isp)) {
2648 			isp_get_pnhle_24xx(isp, &elp4[i], &el4);
2649 			p = el4.pnhle_port_id_lo |
2650 			    (el4.pnhle_port_id_hi << 16);
2651 			h = el4.pnhle_handle;
2652 		} else if (IS_23XX(isp)) {
2653 			isp_get_pnhle_23xx(isp, &elp3[i], &el3);
2654 			p = el3.pnhle_port_id_lo |
2655 			    (el3.pnhle_port_id_hi << 16);
2656 			h = el3.pnhle_handle;
2657 		} else { /* 21xx */
2658 			isp_get_pnhle_21xx(isp, &elp1[i], &el1);
2659 			p = el1.pnhle_port_id_lo |
2660 			    ((el1.pnhle_port_id_hi_handle & 0xff) << 16);
2661 			h = el1.pnhle_port_id_hi_handle >> 8;
2662 		}
2663 		if (loop && (p >> 8) != (fcp->isp_portid >> 8))
2664 			continue;
2665 		handles[j++] = h;
2666 	}
2667 	*num = j;
2668 	if (dolock)
2669 		FC_SCRATCH_RELEASE(isp, chan);
2670 	return (0);
2671 }
2672 
2673 static void
2674 isp_dump_chip_portdb(ispsoftc_t *isp, int chan, int dolock)
2675 {
2676 	isp_pdb_t pdb;
2677 	int lim, loopid;
2678 
2679 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGINFO, "Chan %d chip port dump", chan);
2680 	if (ISP_CAP_2KLOGIN(isp)) {
2681 		lim = NPH_MAX_2K;
2682 	} else {
2683 		lim = NPH_MAX;
2684 	}
2685 	for (loopid = 0; loopid != lim; loopid++) {
2686 		if (isp_getpdb(isp, chan, loopid, &pdb, dolock)) {
2687 			continue;
2688 		}
2689 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGINFO, "Chan %d Loopid 0x%04x "
2690 		    "PortID 0x%06x WWPN 0x%02x%02x%02x%02x%02x%02x%02x%02x",
2691 		    chan, loopid, pdb.portid, pdb.portname[0], pdb.portname[1],
2692 		    pdb.portname[2], pdb.portname[3], pdb.portname[4],
2693 		    pdb.portname[5], pdb.portname[6], pdb.portname[7]);
2694 	}
2695 }
2696 
2697 static uint64_t
2698 isp_get_wwn(ispsoftc_t *isp, int chan, int loopid, int nodename)
2699 {
2700 	uint64_t wwn = INI_NONE;
2701 	fcparam *fcp = FCPARAM(isp, chan);
2702 	mbreg_t mbs;
2703 
2704 	if (fcp->isp_fwstate < FW_READY ||
2705 	    fcp->isp_loopstate < LOOP_PDB_RCVD) {
2706 		return (wwn);
2707 	}
2708 	MBSINIT(&mbs, MBOX_GET_PORT_NAME,
2709 	    MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_PARAM_ERROR), 500000);
2710 	if (ISP_CAP_2KLOGIN(isp)) {
2711 		mbs.param[1] = loopid;
2712 		if (nodename) {
2713 			mbs.param[10] = 1;
2714 		}
2715 		mbs.param[9] = chan;
2716 	} else {
2717 		mbs.ibitm = 3;
2718 		mbs.param[1] = loopid << 8;
2719 		if (nodename) {
2720 			mbs.param[1] |= 1;
2721 		}
2722 	}
2723 	isp_mboxcmd(isp, &mbs);
2724 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2725 		return (wwn);
2726 	}
2727 	if (IS_24XX(isp)) {
2728 		wwn =
2729 		    (((uint64_t)(mbs.param[2] >> 8))	<< 56) |
2730 		    (((uint64_t)(mbs.param[2] & 0xff))	<< 48) |
2731 		    (((uint64_t)(mbs.param[3] >> 8))	<< 40) |
2732 		    (((uint64_t)(mbs.param[3] & 0xff))	<< 32) |
2733 		    (((uint64_t)(mbs.param[6] >> 8))	<< 24) |
2734 		    (((uint64_t)(mbs.param[6] & 0xff))	<< 16) |
2735 		    (((uint64_t)(mbs.param[7] >> 8))	<<  8) |
2736 		    (((uint64_t)(mbs.param[7] & 0xff)));
2737 	} else {
2738 		wwn =
2739 		    (((uint64_t)(mbs.param[2] & 0xff))  << 56) |
2740 		    (((uint64_t)(mbs.param[2] >> 8))	<< 48) |
2741 		    (((uint64_t)(mbs.param[3] & 0xff))	<< 40) |
2742 		    (((uint64_t)(mbs.param[3] >> 8))	<< 32) |
2743 		    (((uint64_t)(mbs.param[6] & 0xff))	<< 24) |
2744 		    (((uint64_t)(mbs.param[6] >> 8))	<< 16) |
2745 		    (((uint64_t)(mbs.param[7] & 0xff))	<<  8) |
2746 		    (((uint64_t)(mbs.param[7] >> 8)));
2747 	}
2748 	return (wwn);
2749 }
2750 
2751 /*
2752  * Make sure we have good FC link.
2753  */
2754 
2755 static int
2756 isp_fclink_test(ispsoftc_t *isp, int chan, int usdelay)
2757 {
2758 	mbreg_t mbs;
2759 	int count, check_for_fabric, r;
2760 	uint8_t lwfs;
2761 	int loopid;
2762 	fcparam *fcp;
2763 	fcportdb_t *lp;
2764 	isp_pdb_t pdb;
2765 
2766 	fcp = FCPARAM(isp, chan);
2767 
2768 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Link Test Entry", chan);
2769 	ISP_MARK_PORTDB(isp, chan, 1);
2770 
2771 	/*
2772 	 * Wait up to N microseconds for F/W to go to a ready state.
2773 	 */
2774 	lwfs = FW_CONFIG_WAIT;
2775 	count = 0;
2776 	while (count < usdelay) {
2777 		uint64_t enano;
2778 		uint32_t wrk;
2779 		NANOTIME_T hra, hrb;
2780 
2781 		GET_NANOTIME(&hra);
2782 		isp_fw_state(isp, chan);
2783 		if (lwfs != fcp->isp_fwstate) {
2784 			isp_prt(isp, ISP_LOGCONFIG|ISP_LOG_SANCFG, "Chan %d Firmware State <%s->%s>", chan, isp_fc_fw_statename((int)lwfs), isp_fc_fw_statename((int)fcp->isp_fwstate));
2785 			lwfs = fcp->isp_fwstate;
2786 		}
2787 		if (fcp->isp_fwstate == FW_READY) {
2788 			break;
2789 		}
2790 		GET_NANOTIME(&hrb);
2791 
2792 		/*
2793 		 * Get the elapsed time in nanoseconds.
2794 		 * Always guaranteed to be non-zero.
2795 		 */
2796 		enano = NANOTIME_SUB(&hrb, &hra);
2797 
2798 		isp_prt(isp, ISP_LOGDEBUG1, "usec%d: 0x%lx->0x%lx enano 0x%x%08x", count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb), (uint32_t)(enano >> 32), (uint32_t)(enano));
2799 
2800 		/*
2801 		 * If the elapsed time is less than 1 millisecond,
2802 		 * delay a period of time up to that millisecond of
2803 		 * waiting.
2804 		 *
2805 		 * This peculiar code is an attempt to try and avoid
2806 		 * invoking uint64_t math support functions for some
2807 		 * platforms where linkage is a problem.
2808 		 */
2809 		if (enano < (1000 * 1000)) {
2810 			count += 1000;
2811 			enano = (1000 * 1000) - enano;
2812 			while (enano > (uint64_t) 4000000000U) {
2813 				ISP_SLEEP(isp, 4000000);
2814 				enano -= (uint64_t) 4000000000U;
2815 			}
2816 			wrk = enano;
2817 			wrk /= 1000;
2818 			ISP_SLEEP(isp, wrk);
2819 		} else {
2820 			while (enano > (uint64_t) 4000000000U) {
2821 				count += 4000000;
2822 				enano -= (uint64_t) 4000000000U;
2823 			}
2824 			wrk = enano;
2825 			count += (wrk / 1000);
2826 		}
2827 	}
2828 
2829 
2830 
2831 	/*
2832 	 * If we haven't gone to 'ready' state, return.
2833 	 */
2834 	if (fcp->isp_fwstate != FW_READY) {
2835 		isp_prt(isp, ISP_LOG_SANCFG, "%s: chan %d not at FW_READY state", __func__, chan);
2836 		return (-1);
2837 	}
2838 
2839 	/*
2840 	 * Get our Loop ID and Port ID.
2841 	 */
2842 	MBSINIT(&mbs, MBOX_GET_LOOP_ID, MBLOGALL, 0);
2843 	mbs.param[9] = chan;
2844 	isp_mboxcmd(isp, &mbs);
2845 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2846 		return (-1);
2847 	}
2848 
2849 	if (ISP_CAP_2KLOGIN(isp)) {
2850 		fcp->isp_loopid = mbs.param[1];
2851 	} else {
2852 		fcp->isp_loopid = mbs.param[1] & 0xff;
2853 	}
2854 
2855 	if (IS_2100(isp)) {
2856 		fcp->isp_topo = TOPO_NL_PORT;
2857 	} else {
2858 		int topo = (int) mbs.param[6];
2859 		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB) {
2860 			topo = TOPO_PTP_STUB;
2861 		}
2862 		fcp->isp_topo = topo;
2863 	}
2864 	fcp->isp_portid = mbs.param[2] | (mbs.param[3] << 16);
2865 
2866 	if (IS_2100(isp)) {
2867 		/*
2868 		 * Don't bother with fabric if we are using really old
2869 		 * 2100 firmware. It's just not worth it.
2870 		 */
2871 		if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
2872 			check_for_fabric = 1;
2873 		} else {
2874 			check_for_fabric = 0;
2875 		}
2876 	} else if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_F_PORT) {
2877 		check_for_fabric = 1;
2878 	} else {
2879 		check_for_fabric = 0;
2880 	}
2881 
2882 	/*
2883 	 * Check to make sure we got a valid loopid
2884 	 * The 24XX seems to mess this up for multiple channels.
2885 	 */
2886 	if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT) {
2887 		uint8_t alpa = fcp->isp_portid;
2888 
2889 		if (alpa == 0) {
2890 			/* "Cannot Happen" */
2891 			isp_prt(isp, ISP_LOGWARN, "Zero AL_PA for Loop Topology?");
2892 		} else {
2893 			int i;
2894 			for (i = 0; alpa_map[i]; i++) {
2895 				if (alpa_map[i] == alpa) {
2896 					break;
2897 				}
2898 			}
2899 			if (alpa_map[i] && fcp->isp_loopid != i) {
2900 				isp_prt(isp, ISP_LOG_SANCFG,
2901 				    "Chan %d deriving loopid %d from AL_PA map (AL_PA 0x%x) and ignoring returned value %d (AL_PA 0x%x)",
2902 				    chan, i, alpa_map[i], fcp->isp_loopid, alpa);
2903 				fcp->isp_loopid = i;
2904 			}
2905 		}
2906 	}
2907 
2908 
2909 	if (IS_24XX(isp)) { /* XXX SHOULDN'T THIS BE FOR 2K F/W? XXX */
2910 		loopid = NPH_FL_ID;
2911 	} else {
2912 		loopid = FL_ID;
2913 	}
2914 	if (check_for_fabric) {
2915 		r = isp_getpdb(isp, chan, loopid, &pdb, 1);
2916 		if (r && (fcp->isp_topo == TOPO_F_PORT || fcp->isp_topo == TOPO_FL_PORT)) {
2917 			isp_prt(isp, ISP_LOGWARN, "fabric topology but cannot get info about fabric controller (0x%x)", r);
2918 			fcp->isp_topo = TOPO_PTP_STUB;
2919 		}
2920 	} else {
2921 		r = -1;
2922 	}
2923 	if (r == 0) {
2924 		if (IS_2100(isp)) {
2925 			fcp->isp_topo = TOPO_FL_PORT;
2926 		}
2927 		if (pdb.portid == 0) {
2928 			/*
2929 			 * Crock.
2930 			 */
2931 			fcp->isp_topo = TOPO_NL_PORT;
2932 			goto not_on_fabric;
2933 		}
2934 
2935 		/*
2936 		 * Save the Fabric controller's port database entry.
2937 		 */
2938 		lp = &fcp->portdb[FL_ID];
2939 		lp->state = FC_PORTDB_STATE_PENDING_VALID;
2940 		MAKE_WWN_FROM_NODE_NAME(lp->node_wwn, pdb.nodename);
2941 		MAKE_WWN_FROM_NODE_NAME(lp->port_wwn, pdb.portname);
2942 		lp->prli_word3 = pdb.prli_word3;
2943 		lp->portid = pdb.portid;
2944 		lp->handle = pdb.handle;
2945 		lp->new_portid = lp->portid;
2946 		lp->new_prli_word3 = lp->prli_word3;
2947 		if (IS_24XX(isp)) {
2948 			if (check_for_fabric) {
2949 				/*
2950 				 * The mbs is still hanging out from the MBOX_GET_LOOP_ID above.
2951 				 */
2952 				fcp->isp_fabric_params = mbs.param[7];
2953 			} else {
2954 				fcp->isp_fabric_params = 0;
2955 			}
2956 			if (chan) {
2957 				fcp->isp_sns_hdl = NPH_RESERVED - chan;
2958 				r = isp_plogx(isp, chan, fcp->isp_sns_hdl, SNS_PORT_ID, PLOGX_FLG_CMD_PLOGI | PLOGX_FLG_COND_PLOGI | PLOGX_FLG_SKIP_PRLI, 0);
2959 				if (r) {
2960 					isp_prt(isp, ISP_LOGWARN, "%s: Chan %d cannot log into SNS", __func__, chan);
2961 					return (-1);
2962 				}
2963 			} else {
2964 				fcp->isp_sns_hdl = NPH_SNS_ID;
2965 			}
2966 			r = isp_register_fc4_type_24xx(isp, chan);
2967 		} else {
2968 			fcp->isp_sns_hdl = SNS_ID;
2969 			r = isp_register_fc4_type(isp, chan);
2970 		}
2971 		if (r) {
2972 			isp_prt(isp, ISP_LOGWARN|ISP_LOG_SANCFG, "%s: register fc4 type failed", __func__);
2973 			return (-1);
2974 		}
2975 	} else {
2976 not_on_fabric:
2977 		fcp->portdb[FL_ID].state = FC_PORTDB_STATE_NIL;
2978 	}
2979 
2980 	fcp->isp_gbspeed = 1;
2981 	if (IS_23XX(isp) || IS_24XX(isp)) {
2982 		MBSINIT(&mbs, MBOX_GET_SET_DATA_RATE, MBLOGALL, 3000000);
2983 		mbs.param[1] = MBGSD_GET_RATE;
2984 		/* mbs.param[2] undefined if we're just getting rate */
2985 		isp_mboxcmd(isp, &mbs);
2986 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
2987 			if (mbs.param[1] == MBGSD_EIGHTGB) {
2988 				isp_prt(isp, ISP_LOGINFO, "Chan %d 8Gb link speed", chan);
2989 				fcp->isp_gbspeed = 8;
2990 			} else if (mbs.param[1] == MBGSD_FOURGB) {
2991 				isp_prt(isp, ISP_LOGINFO, "Chan %d 4Gb link speed", chan);
2992 				fcp->isp_gbspeed = 4;
2993 			} else if (mbs.param[1] == MBGSD_TWOGB) {
2994 				isp_prt(isp, ISP_LOGINFO, "Chan %d 2Gb link speed", chan);
2995 				fcp->isp_gbspeed = 2;
2996 			} else if (mbs.param[1] == MBGSD_ONEGB) {
2997 				isp_prt(isp, ISP_LOGINFO, "Chan %d 1Gb link speed", chan);
2998 				fcp->isp_gbspeed = 1;
2999 			}
3000 		}
3001 	}
3002 
3003 	/*
3004 	 * Announce ourselves, too.
3005 	 */
3006 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGCONFIG, topology, chan, (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) fcp->isp_wwpn, fcp->isp_portid, fcp->isp_loopid, isp_fc_toponame(fcp));
3007 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Link Test Complete", chan);
3008 	return (0);
3009 }
3010 
3011 /*
3012  * Complete the synchronization of our Port Database.
3013  *
3014  * At this point, we've scanned the local loop (if any) and the fabric
3015  * and performed fabric logins on all new devices.
3016  *
3017  * Our task here is to go through our port database and remove any entities
3018  * that are still marked probational (issuing PLOGO for ones which we had
3019  * PLOGI'd into) or are dead.
3020  *
3021  * Our task here is to also check policy to decide whether devices which
3022  * have *changed* in some way should still be kept active. For example,
3023  * if a device has just changed PortID, we can either elect to treat it
3024  * as an old device or as a newly arrived device (and notify the outer
3025  * layer appropriately).
3026  *
3027  * We also do initiator map target id assignment here for new initiator
3028  * devices and refresh old ones ot make sure that they point to the correct
3029  * entities.
3030  */
3031 static int
3032 isp_pdb_sync(ispsoftc_t *isp, int chan)
3033 {
3034 	fcparam *fcp = FCPARAM(isp, chan);
3035 	fcportdb_t *lp;
3036 	uint16_t dbidx;
3037 
3038 	if (fcp->isp_loopstate == LOOP_READY) {
3039 		return (0);
3040 	}
3041 
3042 	/*
3043 	 * Make sure we're okay for doing this right now.
3044 	 */
3045 	if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
3046 	    fcp->isp_loopstate != LOOP_FSCAN_DONE &&
3047 	    fcp->isp_loopstate != LOOP_LSCAN_DONE) {
3048 		isp_prt(isp, ISP_LOGWARN, "isp_pdb_sync: bad loopstate %d",
3049 		    fcp->isp_loopstate);
3050 		return (-1);
3051 	}
3052 
3053 	if (fcp->isp_topo == TOPO_FL_PORT ||
3054 	    fcp->isp_topo == TOPO_NL_PORT ||
3055 	    fcp->isp_topo == TOPO_N_PORT) {
3056 		if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
3057 			if (isp_scan_loop(isp, chan) != 0) {
3058 				isp_prt(isp, ISP_LOGWARN,
3059 				    "isp_pdb_sync: isp_scan_loop failed");
3060 				return (-1);
3061 			}
3062 		}
3063 	}
3064 
3065 	if (fcp->isp_topo == TOPO_F_PORT || fcp->isp_topo == TOPO_FL_PORT) {
3066 		if (fcp->isp_loopstate < LOOP_FSCAN_DONE) {
3067 			if (isp_scan_fabric(isp, chan) != 0) {
3068 				isp_prt(isp, ISP_LOGWARN,
3069 				    "isp_pdb_sync: isp_scan_fabric failed");
3070 				return (-1);
3071 			}
3072 		}
3073 	}
3074 
3075 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Synchronizing PDBs", chan);
3076 
3077 	fcp->isp_loopstate = LOOP_SYNCING_PDB;
3078 
3079 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3080 		lp = &fcp->portdb[dbidx];
3081 
3082 		if (lp->state == FC_PORTDB_STATE_NIL ||
3083 		    lp->state == FC_PORTDB_STATE_VALID) {
3084 			continue;
3085 		}
3086 
3087 		switch (lp->state) {
3088 		case FC_PORTDB_STATE_PROBATIONAL:
3089 		case FC_PORTDB_STATE_DEAD:
3090 			lp->state = FC_PORTDB_STATE_NIL;
3091 			isp_async(isp, ISPASYNC_DEV_GONE, chan, lp);
3092 			if (lp->autologin == 0) {
3093 				(void) isp_plogx(isp, chan, lp->handle,
3094 				    lp->portid,
3095 				    PLOGX_FLG_CMD_LOGO |
3096 				    PLOGX_FLG_IMPLICIT |
3097 				    PLOGX_FLG_FREE_NPHDL, 0);
3098 			} else {
3099 				lp->autologin = 0;
3100 			}
3101 			lp->new_prli_word3 = 0;
3102 			lp->new_portid = 0;
3103 			/*
3104 			 * Note that we might come out of this with our state
3105 			 * set to FC_PORTDB_STATE_ZOMBIE.
3106 			 */
3107 			break;
3108 		case FC_PORTDB_STATE_NEW:
3109 			lp->portid = lp->new_portid;
3110 			lp->prli_word3 = lp->new_prli_word3;
3111 			lp->state = FC_PORTDB_STATE_VALID;
3112 			isp_async(isp, ISPASYNC_DEV_ARRIVED, chan, lp);
3113 			lp->new_prli_word3 = 0;
3114 			lp->new_portid = 0;
3115 			break;
3116 		case FC_PORTDB_STATE_CHANGED:
3117 			lp->state = FC_PORTDB_STATE_VALID;
3118 			isp_async(isp, ISPASYNC_DEV_CHANGED, chan, lp);
3119 			lp->portid = lp->new_portid;
3120 			lp->prli_word3 = lp->new_prli_word3;
3121 			lp->new_prli_word3 = 0;
3122 			lp->new_portid = 0;
3123 			break;
3124 		case FC_PORTDB_STATE_PENDING_VALID:
3125 			lp->portid = lp->new_portid;
3126 			lp->prli_word3 = lp->new_prli_word3;
3127 			lp->state = FC_PORTDB_STATE_VALID;
3128 			isp_async(isp, ISPASYNC_DEV_STAYED, chan, lp);
3129 			if (dbidx != FL_ID) {
3130 				lp->new_prli_word3 = 0;
3131 				lp->new_portid = 0;
3132 			}
3133 			break;
3134 		case FC_PORTDB_STATE_ZOMBIE:
3135 			break;
3136 		default:
3137 			isp_prt(isp, ISP_LOGWARN,
3138 			    "isp_pdb_sync: state %d for idx %d",
3139 			    lp->state, dbidx);
3140 			isp_dump_portdb(isp, chan);
3141 		}
3142 	}
3143 
3144 	/*
3145 	 * If we get here, we've for sure seen not only a valid loop
3146 	 * but know what is or isn't on it, so mark this for usage
3147 	 * in isp_start.
3148 	 */
3149 	fcp->loop_seen_once = 1;
3150 	fcp->isp_loopstate = LOOP_READY;
3151 	return (0);
3152 }
3153 
3154 /*
3155  * Scan local loop for devices.
3156  */
3157 static int
3158 isp_scan_loop(ispsoftc_t *isp, int chan)
3159 {
3160 	fcportdb_t *lp, tmp;
3161 	fcparam *fcp = FCPARAM(isp, chan);
3162 	int i, idx, lim, r;
3163 	isp_pdb_t pdb;
3164 	uint16_t handles[LOCAL_LOOP_LIM];
3165 	uint16_t handle;
3166 
3167 	if (fcp->isp_fwstate < FW_READY ||
3168 	    fcp->isp_loopstate < LOOP_PDB_RCVD) {
3169 		return (-1);
3170 	}
3171 	if (fcp->isp_loopstate > LOOP_SCANNING_LOOP) {
3172 		return (0);
3173 	}
3174 	if (fcp->isp_topo != TOPO_NL_PORT && fcp->isp_topo != TOPO_FL_PORT &&
3175 	    fcp->isp_topo != TOPO_N_PORT) {
3176 		isp_prt(isp, ISP_LOG_SANCFG,
3177 		    "Chan %d no loop topology to scan", chan);
3178 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
3179 		return (0);
3180 	}
3181 
3182 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
3183 
3184 	lim = LOCAL_LOOP_LIM;
3185 	r = isp_gethandles(isp, chan, handles, &lim, 1, 1);
3186 	if (r != 0) {
3187 		isp_prt(isp, ISP_LOG_SANCFG,
3188 		    "Chan %d getting list of handles failed with %x", chan, r);
3189 fail:
3190 		ISP_MARK_PORTDB(isp, chan, 1);
3191 		isp_prt(isp, ISP_LOG_SANCFG,
3192 		    "Chan %d FC scan loop DONE (bad)", chan);
3193 		return (-1);
3194 	}
3195 
3196 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop -- %d ports",
3197 	    chan, lim);
3198 
3199 	/*
3200 	 * Run through the list and get the port database info for each one.
3201 	 */
3202 	for (idx = 0; idx < lim; idx++) {
3203 		handle = handles[idx];
3204 
3205 		/*
3206 		 * Don't scan "special" ids.
3207 		 */
3208 		if (ISP_CAP_2KLOGIN(isp)) {
3209 			if (handle >= NPH_RESERVED - isp->isp_nchan)
3210 				continue;
3211 		} else {
3212 			if (handle >= FL_ID && handle <= SNS_ID)
3213 				continue;
3214 		}
3215 
3216 		/*
3217 		 * In older cards with older f/w GET_PORT_DATABASE has been
3218 		 * known to hang. This trick gets around that problem.
3219 		 */
3220 		if (IS_2100(isp) || IS_2200(isp)) {
3221 			uint64_t node_wwn = isp_get_wwn(isp, chan, handle, 1);
3222 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3223 				goto fail;
3224 			if (node_wwn == INI_NONE) {
3225 				continue;
3226 			}
3227 		}
3228 
3229 		/*
3230 		 * Get the port database entity for this index.
3231 		 */
3232 		r = isp_getpdb(isp, chan, handle, &pdb, 1);
3233 		if (r != 0) {
3234 			isp_prt(isp, ISP_LOGDEBUG1,
3235 			    "Chan %d FC scan loop handle %d returned %x",
3236 			    chan, handle, r);
3237 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3238 				goto fail;
3239 			continue;
3240 		}
3241 
3242 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3243 			goto fail;
3244 
3245 		/*
3246 		 * On *very* old 2100 firmware we would end up sometimes
3247 		 * with the firmware returning the port database entry
3248 		 * for something else. We used to restart this, but
3249 		 * now we just punt.
3250 		 */
3251 		if (IS_2100(isp) && pdb.handle != handle) {
3252 			isp_prt(isp, ISP_LOGWARN,
3253 			    "Chan %d getpdb() returned wrong handle %x != %x",
3254 			    chan, pdb.handle, handle);
3255 			goto fail;
3256 		}
3257 
3258 		/*
3259 		 * Save the pertinent info locally.
3260 		 */
3261 		MAKE_WWN_FROM_NODE_NAME(tmp.node_wwn, pdb.nodename);
3262 		MAKE_WWN_FROM_NODE_NAME(tmp.port_wwn, pdb.portname);
3263 		tmp.prli_word3 = pdb.prli_word3;
3264 		tmp.portid = pdb.portid;
3265 		tmp.handle = pdb.handle;
3266 
3267 		/*
3268 		 * Check to make sure it's still a valid entry. The 24XX seems
3269 		 * to return a portid but not a WWPN/WWNN or role for devices
3270 		 * which shift on a loop.
3271 		 */
3272 		if (tmp.node_wwn == 0 || tmp.port_wwn == 0 || tmp.portid == 0) {
3273 			int a, b, c;
3274 			isp_prt(isp, ISP_LOGWARN,
3275 			    "Chan %d bad pdb (WWNN %016jx, WWPN %016jx, PortID %06x, W3 0x%x, H 0x%x) @ handle 0x%x",
3276 			    chan, tmp.node_wwn, tmp.port_wwn, tmp.portid, tmp.prli_word3, tmp.handle, handle);
3277 			a = (tmp.node_wwn == 0);
3278 			b = (tmp.port_wwn == 0);
3279 			c = (tmp.portid == 0);
3280 			if (a == 0 && b == 0) {
3281 				tmp.node_wwn =
3282 				    isp_get_wwn(isp, chan, handle, 1);
3283 				tmp.port_wwn =
3284 				    isp_get_wwn(isp, chan, handle, 0);
3285 				if (tmp.node_wwn && tmp.port_wwn) {
3286 					isp_prt(isp, ISP_LOGWARN, "DODGED!");
3287 					goto cont;
3288 				}
3289 			}
3290 			isp_dump_portdb(isp, chan);
3291 			continue;
3292 		}
3293   cont:
3294 
3295 		/*
3296 		 * Now search the entire port database
3297 		 * for the same Port WWN.
3298 		 */
3299 		if (isp_find_pdb_by_wwn(isp, chan, tmp.port_wwn, &lp)) {
3300 			/*
3301 			 * Okay- we've found a non-nil entry that matches.
3302 			 * Check to make sure it's probational or a zombie.
3303 			 */
3304 			if (lp->state != FC_PORTDB_STATE_PROBATIONAL &&
3305 			    lp->state != FC_PORTDB_STATE_ZOMBIE &&
3306 			    lp->state != FC_PORTDB_STATE_VALID) {
3307 				isp_prt(isp, ISP_LOGERR,
3308 				    "Chan %d [%d] not probational/zombie (0x%x)",
3309 				    chan, FC_PORTDB_TGT(isp, chan, lp), lp->state);
3310 				isp_dump_portdb(isp, chan);
3311 				goto fail;
3312 			}
3313 
3314 			/*
3315 			 * Mark the device as something the f/w logs into
3316 			 * automatically.
3317 			 */
3318 			lp->autologin = 1;
3319 			lp->node_wwn = tmp.node_wwn;
3320 
3321 			/*
3322 			 * Check to make see if really still the same
3323 			 * device. If it is, we mark it pending valid.
3324 			 */
3325 			if (lp->portid == tmp.portid && lp->handle == tmp.handle && lp->prli_word3 == tmp.prli_word3) {
3326 				lp->new_portid = tmp.portid;
3327 				lp->new_prli_word3 = tmp.prli_word3;
3328 				lp->state = FC_PORTDB_STATE_PENDING_VALID;
3329 				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Loop Port 0x%06x@0x%04x Pending Valid", chan, tmp.portid, tmp.handle);
3330 				continue;
3331 			}
3332 
3333 			/*
3334 			 * We can wipe out the old handle value
3335 			 * here because it's no longer valid.
3336 			 */
3337 			lp->handle = tmp.handle;
3338 
3339 			/*
3340 			 * Claim that this has changed and let somebody else
3341 			 * decide what to do.
3342 			 */
3343 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Loop Port 0x%06x@0x%04x changed", chan, tmp.portid, tmp.handle);
3344 			lp->state = FC_PORTDB_STATE_CHANGED;
3345 			lp->new_portid = tmp.portid;
3346 			lp->new_prli_word3 = tmp.prli_word3;
3347 			continue;
3348 		}
3349 
3350 		/*
3351 		 * Ah. A new device entry. Find an empty slot
3352 		 * for it and save info for later disposition.
3353 		 */
3354 		for (i = 0; i < MAX_FC_TARG; i++) {
3355 			if (fcp->portdb[i].state == FC_PORTDB_STATE_NIL) {
3356 				break;
3357 			}
3358 		}
3359 		if (i == MAX_FC_TARG) {
3360 			isp_prt(isp, ISP_LOGERR,
3361 			    "Chan %d out of portdb entries", chan);
3362 			continue;
3363 		}
3364 		lp = &fcp->portdb[i];
3365 
3366 		ISP_MEMZERO(lp, sizeof (fcportdb_t));
3367 		lp->autologin = 1;
3368 		lp->state = FC_PORTDB_STATE_NEW;
3369 		lp->new_portid = tmp.portid;
3370 		lp->new_prli_word3 = tmp.prli_word3;
3371 		lp->handle = tmp.handle;
3372 		lp->port_wwn = tmp.port_wwn;
3373 		lp->node_wwn = tmp.node_wwn;
3374 		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Loop Port 0x%06x@0x%04x is New Entry", chan, tmp.portid, tmp.handle);
3375 	}
3376 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
3377 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE", chan);
3378 	return (0);
3379 }
3380 
3381 /*
3382  * Scan the fabric for devices and add them to our port database.
3383  *
3384  * Use the GID_FT command to get all Port IDs for FC4 SCSI devices it knows.
3385  *
3386  * For 2100-23XX cards, we can use the SNS mailbox command to pass simple
3387  * name server commands to the switch management server via the QLogic f/w.
3388  *
3389  * For the 24XX card, we have to use CT-Pass through run via the Execute IOCB
3390  * mailbox command.
3391  *
3392  * The net result is to leave the list of Port IDs setting untranslated in
3393  * offset IGPOFF of the FC scratch area, whereupon we'll canonicalize it to
3394  * host order at OGPOFF.
3395  */
3396 
3397 /*
3398  * Take less than half of our scratch area to store Port IDs
3399  */
3400 #define	GIDLEN	((ISP_FC_SCRLEN >> 1) - 16 - SNS_GID_FT_REQ_SIZE)
3401 #define	NGENT	((GIDLEN - 16) >> 2)
3402 
3403 #define	IGPOFF	(2 * QENTRY_LEN)
3404 #define	OGPOFF	(ISP_FC_SCRLEN >> 1)
3405 #define	ZTXOFF	(ISP_FC_SCRLEN - (1 * QENTRY_LEN))
3406 #define	CTXOFF	(ISP_FC_SCRLEN - (2 * QENTRY_LEN))
3407 #define	XTXOFF	(ISP_FC_SCRLEN - (3 * QENTRY_LEN))
3408 
3409 static int
3410 isp_gid_ft_sns(ispsoftc_t *isp, int chan)
3411 {
3412 	union {
3413 		sns_gid_ft_req_t _x;
3414 		uint8_t _y[SNS_GID_FT_REQ_SIZE];
3415 	} un;
3416 	fcparam *fcp = FCPARAM(isp, chan);
3417 	sns_gid_ft_req_t *rq = &un._x;
3418 	mbreg_t mbs;
3419 
3420 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d scanning fabric (GID_FT) via SNS", chan);
3421 
3422 	ISP_MEMZERO(rq, SNS_GID_FT_REQ_SIZE);
3423 	rq->snscb_rblen = GIDLEN >> 1;
3424 	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + IGPOFF);
3425 	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + IGPOFF);
3426 	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + IGPOFF);
3427 	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + IGPOFF);
3428 	rq->snscb_sblen = 6;
3429 	rq->snscb_cmd = SNS_GID_FT;
3430 	rq->snscb_mword_div_2 = NGENT;
3431 	rq->snscb_fc4_type = FC4_SCSI;
3432 
3433 	isp_put_gid_ft_request(isp, rq, fcp->isp_scratch);
3434 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE, chan);
3435 
3436 	MBSINIT(&mbs, MBOX_SEND_SNS, MBLOGALL, 10000000);
3437 	mbs.param[0] = MBOX_SEND_SNS;
3438 	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
3439 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
3440 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
3441 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
3442 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
3443 	isp_mboxcmd(isp, &mbs);
3444 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3445 		if (mbs.param[0] == MBOX_INVALID_COMMAND) {
3446 			return (1);
3447 		} else {
3448 			return (-1);
3449 		}
3450 	}
3451 	return (0);
3452 }
3453 
3454 static int
3455 isp_gid_ft_ct_passthru(ispsoftc_t *isp, int chan)
3456 {
3457 	mbreg_t mbs;
3458 	fcparam *fcp = FCPARAM(isp, chan);
3459 	union {
3460 		isp_ct_pt_t plocal;
3461 		ct_hdr_t clocal;
3462 		uint8_t q[QENTRY_LEN];
3463 	} un;
3464 	isp_ct_pt_t *pt;
3465 	ct_hdr_t *ct;
3466 	uint32_t *rp;
3467 	uint8_t *scp = fcp->isp_scratch;
3468 
3469 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d scanning fabric (GID_FT) via CT", chan);
3470 
3471 	if (!IS_24XX(isp)) {
3472 		return (1);
3473 	}
3474 
3475 	/*
3476 	 * Build a Passthrough IOCB in memory.
3477 	 */
3478 	pt = &un.plocal;
3479 	ISP_MEMZERO(un.q, QENTRY_LEN);
3480 	pt->ctp_header.rqs_entry_count = 1;
3481 	pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
3482 	pt->ctp_handle = 0xffffffff;
3483 	pt->ctp_nphdl = fcp->isp_sns_hdl;
3484 	pt->ctp_cmd_cnt = 1;
3485 	pt->ctp_vpidx = ISP_GET_VPIDX(isp, chan);
3486 	pt->ctp_time = 30;
3487 	pt->ctp_rsp_cnt = 1;
3488 	pt->ctp_rsp_bcnt = GIDLEN;
3489 	pt->ctp_cmd_bcnt = sizeof (*ct) + sizeof (uint32_t);
3490 	pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
3491 	pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
3492 	pt->ctp_dataseg[0].ds_count = sizeof (*ct) + sizeof (uint32_t);
3493 	pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
3494 	pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
3495 	pt->ctp_dataseg[1].ds_count = GIDLEN;
3496 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
3497 		isp_print_bytes(isp, "ct IOCB", QENTRY_LEN, pt);
3498 	}
3499 	isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
3500 
3501 	/*
3502 	 * Build the CT header and command in memory.
3503 	 *
3504 	 * Note that the CT header has to end up as Big Endian format in memory.
3505 	 */
3506 	ct = &un.clocal;
3507 	ISP_MEMZERO(ct, sizeof (*ct));
3508 	ct->ct_revision = CT_REVISION;
3509 	ct->ct_fcs_type = CT_FC_TYPE_FC;
3510 	ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
3511 	ct->ct_cmd_resp = SNS_GID_FT;
3512 	ct->ct_bcnt_resid = (GIDLEN - 16) >> 2;
3513 
3514 	isp_put_ct_hdr(isp, ct, (ct_hdr_t *) &scp[XTXOFF]);
3515 	rp = (uint32_t *) &scp[XTXOFF+sizeof (*ct)];
3516 	ISP_IOZPUT_32(isp, FC4_SCSI, rp);
3517 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
3518 		isp_print_bytes(isp, "CT HDR + payload after put",
3519 		    sizeof (*ct) + sizeof (uint32_t), &scp[XTXOFF]);
3520 	}
3521 	ISP_MEMZERO(&scp[ZTXOFF], QENTRY_LEN);
3522 	MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 500000);
3523 	mbs.param[1] = QENTRY_LEN;
3524 	mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
3525 	mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
3526 	mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
3527 	mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
3528 	MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN, chan);
3529 	isp_mboxcmd(isp, &mbs);
3530 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3531 		return (-1);
3532 	}
3533 	MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN, chan);
3534 	pt = &un.plocal;
3535 	isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
3536 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
3537 		isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
3538 	}
3539 
3540 	if (pt->ctp_status && pt->ctp_status != RQCS_DATA_UNDERRUN) {
3541 		isp_prt(isp, ISP_LOGWARN,
3542 		    "Chan %d ISP GID FT CT Passthrough returned 0x%x",
3543 		    chan, pt->ctp_status);
3544 		return (-1);
3545 	}
3546 	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN + 16, chan);
3547 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
3548 		isp_print_bytes(isp, "CT response", GIDLEN+16, &scp[IGPOFF]);
3549 	}
3550 	return (0);
3551 }
3552 
3553 static int
3554 isp_scan_fabric(ispsoftc_t *isp, int chan)
3555 {
3556 	fcparam *fcp = FCPARAM(isp, chan);
3557 	uint32_t portid;
3558 	uint16_t handle, loopid;
3559 	isp_pdb_t pdb;
3560 	int portidx, portlim, r;
3561 	sns_gid_ft_rsp_t *rs0, *rs1;
3562 
3563 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Scan Fabric", chan);
3564 	if (fcp->isp_fwstate != FW_READY || fcp->isp_loopstate < LOOP_LSCAN_DONE) {
3565 		return (-1);
3566 	}
3567 	if (fcp->isp_loopstate > LOOP_SCANNING_FABRIC) {
3568 		return (0);
3569 	}
3570 	if (fcp->isp_topo != TOPO_FL_PORT && fcp->isp_topo != TOPO_F_PORT) {
3571 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
3572 		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Scan Fabric Done (no fabric)", chan);
3573 		return (0);
3574 	}
3575 
3576 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
3577 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3578 		isp_prt(isp, ISP_LOGERR, sacq);
3579 		ISP_MARK_PORTDB(isp, chan, 1);
3580 		return (-1);
3581 	}
3582 	if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
3583 		FC_SCRATCH_RELEASE(isp, chan);
3584 		ISP_MARK_PORTDB(isp, chan, 1);
3585 		return (-1);
3586 	}
3587 
3588 	/*
3589 	 * Make sure we still are logged into the fabric controller.
3590 	 */
3591 	if (IS_24XX(isp)) {	/* XXX SHOULDN'T THIS BE TRUE FOR 2K F/W? XXX */
3592 		loopid = NPH_FL_ID;
3593 	} else {
3594 		loopid = FL_ID;
3595 	}
3596 	r = isp_getpdb(isp, chan, loopid, &pdb, 0);
3597 	if ((r & 0xffff) == MBOX_NOT_LOGGED_IN) {
3598 		isp_dump_chip_portdb(isp, chan, 0);
3599 	}
3600 	if (r) {
3601 		fcp->isp_loopstate = LOOP_PDB_RCVD;
3602 		FC_SCRATCH_RELEASE(isp, chan);
3603 		ISP_MARK_PORTDB(isp, chan, 1);
3604 		return (-1);
3605 	}
3606 
3607 	if (IS_24XX(isp)) {
3608 		r = isp_gid_ft_ct_passthru(isp, chan);
3609 	} else {
3610 		r = isp_gid_ft_sns(isp, chan);
3611 	}
3612 
3613 	if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
3614 		FC_SCRATCH_RELEASE(isp, chan);
3615 		ISP_MARK_PORTDB(isp, chan, 1);
3616 		return (-1);
3617 	}
3618 
3619 	if (r > 0) {
3620 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
3621 		FC_SCRATCH_RELEASE(isp, chan);
3622 		return (0);
3623 	} else if (r < 0) {
3624 		fcp->isp_loopstate = LOOP_PDB_RCVD;	/* try again */
3625 		FC_SCRATCH_RELEASE(isp, chan);
3626 		return (0);
3627 	}
3628 
3629 	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN, chan);
3630 	rs0 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+IGPOFF);
3631 	rs1 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+OGPOFF);
3632 	isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
3633 	if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
3634 		FC_SCRATCH_RELEASE(isp, chan);
3635 		ISP_MARK_PORTDB(isp, chan, 1);
3636 		return (-1);
3637 	}
3638 	if (rs1->snscb_cthdr.ct_cmd_resp != LS_ACC) {
3639 		int level;
3640 		if (rs1->snscb_cthdr.ct_reason == 9 && rs1->snscb_cthdr.ct_explanation == 7) {
3641 			level = ISP_LOG_SANCFG;
3642 		} else {
3643 			level = ISP_LOGWARN;
3644 		}
3645 		isp_prt(isp, level, "Chan %d Fabric Nameserver rejected GID_FT"
3646 		    " (Reason=0x%x Expl=0x%x)", chan,
3647 		    rs1->snscb_cthdr.ct_reason,
3648 		    rs1->snscb_cthdr.ct_explanation);
3649 		FC_SCRATCH_RELEASE(isp, chan);
3650 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
3651 		return (0);
3652 	}
3653 
3654 
3655 	/*
3656 	 * If we get this far, we certainly still have the fabric controller.
3657 	 */
3658 	fcp->portdb[FL_ID].state = FC_PORTDB_STATE_PENDING_VALID;
3659 
3660 	/*
3661 	 * Go through the list and remove duplicate port ids.
3662 	 */
3663 
3664 	portlim = 0;
3665 	portidx = 0;
3666 	for (portidx = 0; portidx < NGENT-1; portidx++) {
3667 		if (rs1->snscb_ports[portidx].control & 0x80) {
3668 			break;
3669 		}
3670 	}
3671 
3672 	/*
3673 	 * If we're not at the last entry, our list wasn't big enough.
3674 	 */
3675 	if ((rs1->snscb_ports[portidx].control & 0x80) == 0) {
3676 		isp_prt(isp, ISP_LOGWARN,
3677 		    "fabric too big for scratch area: increase ISP_FC_SCRLEN");
3678 	}
3679 	portlim = portidx + 1;
3680 	isp_prt(isp, ISP_LOG_SANCFG,
3681 	    "Chan %d got %d ports back from name server", chan, portlim);
3682 
3683 	for (portidx = 0; portidx < portlim; portidx++) {
3684 		int npidx;
3685 
3686 		portid =
3687 		    ((rs1->snscb_ports[portidx].portid[0]) << 16) |
3688 		    ((rs1->snscb_ports[portidx].portid[1]) << 8) |
3689 		    ((rs1->snscb_ports[portidx].portid[2]));
3690 
3691 		for (npidx = portidx + 1; npidx < portlim; npidx++) {
3692 			uint32_t new_portid =
3693 			    ((rs1->snscb_ports[npidx].portid[0]) << 16) |
3694 			    ((rs1->snscb_ports[npidx].portid[1]) << 8) |
3695 			    ((rs1->snscb_ports[npidx].portid[2]));
3696 			if (new_portid == portid) {
3697 				break;
3698 			}
3699 		}
3700 
3701 		if (npidx < portlim) {
3702 			rs1->snscb_ports[npidx].portid[0] = 0;
3703 			rs1->snscb_ports[npidx].portid[1] = 0;
3704 			rs1->snscb_ports[npidx].portid[2] = 0;
3705 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d removing duplicate PortID 0x%06x entry from list", chan, portid);
3706 		}
3707 	}
3708 
3709 	/*
3710 	 * We now have a list of Port IDs for all FC4 SCSI devices
3711 	 * that the Fabric Name server knows about.
3712 	 *
3713 	 * For each entry on this list go through our port database looking
3714 	 * for probational entries- if we find one, then an old entry is
3715 	 * maybe still this one. We get some information to find out.
3716 	 *
3717 	 * Otherwise, it's a new fabric device, and we log into it
3718 	 * (unconditionally). After searching the entire database
3719 	 * again to make sure that we never ever ever ever have more
3720 	 * than one entry that has the same PortID or the same
3721 	 * WWNN/WWPN duple, we enter the device into our database.
3722 	 */
3723 
3724 	for (portidx = 0; portidx < portlim; portidx++) {
3725 		fcportdb_t *lp;
3726 		uint64_t wwnn, wwpn;
3727 		int dbidx, nr;
3728 
3729 		portid =
3730 		    ((rs1->snscb_ports[portidx].portid[0]) << 16) |
3731 		    ((rs1->snscb_ports[portidx].portid[1]) << 8) |
3732 		    ((rs1->snscb_ports[portidx].portid[2]));
3733 
3734 		if (portid == 0) {
3735 			isp_prt(isp, ISP_LOG_SANCFG,
3736 			    "Chan %d skipping null PortID at idx %d",
3737 			    chan, portidx);
3738 			continue;
3739 		}
3740 
3741 		/*
3742 		 * Skip ourselves here and on other channels. If we're
3743 		 * multi-id, we can't check the portids in other FCPARAM
3744 		 * arenas because the resolutions here aren't synchronized.
3745 		 * The best way to do this is to exclude looking at portids
3746 		 * that have the same domain and area code as our own
3747 		 * portid.
3748 		 */
3749 		if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
3750 			if ((portid >> 8) == (fcp->isp_portid >> 8)) {
3751 				isp_prt(isp, ISP_LOG_SANCFG,
3752 				    "Chan %d skip PortID 0x%06x",
3753 				    chan, portid);
3754 				continue;
3755 			}
3756 		} else if (portid == fcp->isp_portid) {
3757 			isp_prt(isp, ISP_LOG_SANCFG,
3758 			    "Chan %d skip ourselves on @ PortID 0x%06x",
3759 			    chan, portid);
3760 			continue;
3761 		}
3762 
3763 		isp_prt(isp, ISP_LOG_SANCFG,
3764 		    "Chan %d Checking Fabric Port 0x%06x", chan, portid);
3765 
3766 		/*
3767 		 * We now search our Port Database for any
3768 		 * probational entries with this PortID. We don't
3769 		 * look for zombies here- only probational
3770 		 * entries (we've already logged out of zombies).
3771 		 */
3772 		for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3773 			lp = &fcp->portdb[dbidx];
3774 
3775 			if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
3776 				continue;
3777 			}
3778 			if (lp->portid == portid) {
3779 				break;
3780 			}
3781 		}
3782 
3783 		/*
3784 		 * We found a probational entry with this Port ID.
3785 		 */
3786 		if (dbidx < MAX_FC_TARG) {
3787 			int handle_changed = 0;
3788 
3789 			lp = &fcp->portdb[dbidx];
3790 
3791 			/*
3792 			 * See if we're still logged into it.
3793 			 *
3794 			 * If we aren't, mark it as a dead device and
3795 			 * leave the new portid in the database entry
3796 			 * for somebody further along to decide what to
3797 			 * do (policy choice).
3798 			 *
3799 			 * If we are, check to see if it's the same
3800 			 * device still (it should be). If for some
3801 			 * reason it isn't, mark it as a changed device
3802 			 * and leave the new portid and role in the
3803 			 * database entry for somebody further along to
3804 			 * decide what to do (policy choice).
3805 			 *
3806 			 */
3807 
3808 			r = isp_getpdb(isp, chan, lp->handle, &pdb, 0);
3809 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
3810 				FC_SCRATCH_RELEASE(isp, chan);
3811 				ISP_MARK_PORTDB(isp, chan, 1);
3812 				return (-1);
3813 			}
3814 			if (r != 0) {
3815 				lp->new_portid = portid;
3816 				lp->state = FC_PORTDB_STATE_DEAD;
3817 				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric PortID 0x%06x handle 0x%x is dead (%d)", chan, portid, lp->handle, r);
3818 				continue;
3819 			}
3820 
3821 
3822 			/*
3823 			 * Check to make sure that handle, portid, WWPN and
3824 			 * WWNN agree. If they don't, then the association
3825 			 * between this PortID and the stated handle has been
3826 			 * broken by the firmware.
3827 			 */
3828 			MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
3829 			MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
3830 			if (pdb.handle != lp->handle ||
3831 			    pdb.portid != portid ||
3832 			    wwpn != lp->port_wwn ||
3833 			    (lp->node_wwn != 0 && wwnn != lp->node_wwn)) {
3834 				isp_prt(isp, ISP_LOG_SANCFG,
3835 				    fconf, chan, dbidx, pdb.handle, pdb.portid,
3836 				    (uint32_t) (wwnn >> 32), (uint32_t) wwnn,
3837 				    (uint32_t) (wwpn >> 32), (uint32_t) wwpn,
3838 				    lp->handle, portid,
3839 				    (uint32_t) (lp->node_wwn >> 32),
3840 				    (uint32_t) lp->node_wwn,
3841 				    (uint32_t) (lp->port_wwn >> 32),
3842 				    (uint32_t) lp->port_wwn);
3843 				/*
3844 				 * Try to re-login to this device using a
3845 				 * new handle. If that fails, mark it dead.
3846 				 *
3847 				 * isp_login_device will check for handle and
3848 				 * portid consistency after re-login.
3849 				 *
3850 				 */
3851 				if ((fcp->role & ISP_ROLE_INITIATOR) == 0 ||
3852 				    isp_login_device(isp, chan, portid, &pdb,
3853 				     &FCPARAM(isp, 0)->isp_lasthdl)) {
3854 					lp->new_portid = portid;
3855 					lp->state = FC_PORTDB_STATE_DEAD;
3856 					if (fcp->isp_loopstate !=
3857 					    LOOP_SCANNING_FABRIC) {
3858 						FC_SCRATCH_RELEASE(isp, chan);
3859 						ISP_MARK_PORTDB(isp, chan, 1);
3860 						return (-1);
3861 					}
3862 					continue;
3863 				}
3864 				if (fcp->isp_loopstate !=
3865 				    LOOP_SCANNING_FABRIC) {
3866 					FC_SCRATCH_RELEASE(isp, chan);
3867 					ISP_MARK_PORTDB(isp, chan, 1);
3868 					return (-1);
3869 				}
3870 				MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
3871 				MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
3872 				if (wwpn != lp->port_wwn ||
3873 				    (lp->node_wwn != 0 && wwnn != lp->node_wwn)) {
3874 					isp_prt(isp, ISP_LOGWARN, "changed WWN"
3875 					    " after relogin");
3876 					lp->new_portid = portid;
3877 					lp->state = FC_PORTDB_STATE_DEAD;
3878 					continue;
3879 				}
3880 
3881 				lp->handle = pdb.handle;
3882 				handle_changed++;
3883 			}
3884 
3885 			nr = pdb.prli_word3;
3886 
3887 			/*
3888 			 * Check to see whether the portid and roles have
3889 			 * stayed the same. If they have stayed the same,
3890 			 * we believe that this is the same device and it
3891 			 * hasn't become disconnected and reconnected, so
3892 			 * mark it as pending valid.
3893 			 *
3894 			 * If they aren't the same, mark the device as a
3895 			 * changed device and save the new port id and role
3896 			 * and let somebody else decide.
3897 			 */
3898 
3899 			lp->new_portid = portid;
3900 			lp->new_prli_word3 = nr;
3901 			if (pdb.portid != lp->portid || nr != lp->prli_word3 || handle_changed) {
3902 				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x changed", chan, portid);
3903 				lp->state = FC_PORTDB_STATE_CHANGED;
3904 			} else {
3905 				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x Now Pending Valid", chan, portid);
3906 				lp->state = FC_PORTDB_STATE_PENDING_VALID;
3907 			}
3908 			continue;
3909 		}
3910 
3911 		if ((fcp->role & ISP_ROLE_INITIATOR) == 0)
3912 			continue;
3913 
3914 		/*
3915 		 * Ah- a new entry. Search the database again for all non-NIL
3916 		 * entries to make sure we never ever make a new database entry
3917 		 * with the same port id. While we're at it, mark where the
3918 		 * last free entry was.
3919 		 */
3920 
3921 		dbidx = MAX_FC_TARG;
3922 		for (lp = fcp->portdb; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
3923 			if (lp >= &fcp->portdb[FL_ID] &&
3924 			    lp <= &fcp->portdb[SNS_ID]) {
3925 				continue;
3926 			}
3927 			if (lp->state == FC_PORTDB_STATE_NIL) {
3928 				if (dbidx == MAX_FC_TARG) {
3929 					dbidx = lp - fcp->portdb;
3930 				}
3931 				continue;
3932 			}
3933 			if (lp->state == FC_PORTDB_STATE_ZOMBIE) {
3934 				continue;
3935 			}
3936 			if (lp->portid == portid) {
3937 				break;
3938 			}
3939 		}
3940 
3941 		if (lp < &fcp->portdb[MAX_FC_TARG]) {
3942 			isp_prt(isp, ISP_LOGWARN, "Chan %d PortID 0x%06x "
3943 			    "already at %d handle %d state %d",
3944 			    chan, portid, dbidx, lp->handle, lp->state);
3945 			continue;
3946 		}
3947 
3948 		/*
3949 		 * We should have the index of the first free entry seen.
3950 		 */
3951 		if (dbidx == MAX_FC_TARG) {
3952 			isp_prt(isp, ISP_LOGERR,
3953 			    "port database too small to login PortID 0x%06x"
3954 			    "- increase MAX_FC_TARG", portid);
3955 			continue;
3956 		}
3957 
3958 		/*
3959 		 * Otherwise, point to our new home.
3960 		 */
3961 		lp = &fcp->portdb[dbidx];
3962 
3963 		/*
3964 		 * Try to see if we are logged into this device,
3965 		 * and maybe log into it.
3966 		 *
3967 		 * isp_login_device will check for handle and
3968 		 * portid consistency after login.
3969 		 */
3970 		if (isp_login_device(isp, chan, portid, &pdb,
3971 		    &FCPARAM(isp, 0)->isp_lasthdl)) {
3972 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
3973 				FC_SCRATCH_RELEASE(isp, chan);
3974 				ISP_MARK_PORTDB(isp, chan, 1);
3975 				return (-1);
3976 			}
3977 			continue;
3978 		}
3979 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
3980 			FC_SCRATCH_RELEASE(isp, chan);
3981 			ISP_MARK_PORTDB(isp, chan, 1);
3982 			return (-1);
3983 		}
3984 
3985 		handle = pdb.handle;
3986 		MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
3987 		MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
3988 		nr = pdb.prli_word3;
3989 
3990 		/*
3991 		 * And go through the database *one* more time to make sure
3992 		 * that we do not make more than one entry that has the same
3993 		 * WWNN/WWPN duple
3994 		 */
3995 		for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3996 			if (dbidx >= FL_ID && dbidx <= SNS_ID) {
3997 				continue;
3998 			}
3999 			if ((fcp->portdb[dbidx].node_wwn == wwnn ||
4000 			     fcp->portdb[dbidx].node_wwn == 0) &&
4001 			    fcp->portdb[dbidx].port_wwn == wwpn) {
4002 				break;
4003 			}
4004 		}
4005 
4006 		if (dbidx == MAX_FC_TARG) {
4007 			ISP_MEMZERO(lp, sizeof (fcportdb_t));
4008 			lp->handle = handle;
4009 			lp->node_wwn = wwnn;
4010 			lp->port_wwn = wwpn;
4011 			lp->new_portid = portid;
4012 			lp->new_prli_word3 = nr;
4013 			lp->state = FC_PORTDB_STATE_NEW;
4014 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x is a New Entry", chan, portid);
4015 			continue;
4016 		}
4017 
4018     		if (fcp->portdb[dbidx].state != FC_PORTDB_STATE_ZOMBIE) {
4019 			isp_prt(isp, ISP_LOGWARN,
4020 			    "Chan %d PortID 0x%x 0x%08x%08x/0x%08x%08x %ld "
4021 			    "already at idx %d, state 0x%x", chan, portid,
4022 			    (uint32_t) (wwnn >> 32), (uint32_t) wwnn,
4023 			    (uint32_t) (wwpn >> 32), (uint32_t) wwpn,
4024 			    (long) (lp - fcp->portdb), dbidx,
4025 			    fcp->portdb[dbidx].state);
4026 			continue;
4027 		}
4028 
4029 		/*
4030 		 * We found a zombie entry that matches us.
4031 		 * Revive it. We know that WWN and WWPN
4032 		 * are the same. For fabric devices, we
4033 		 * don't care that handle is different
4034 		 * as we assign that. If role or portid
4035 		 * are different, it maybe a changed device.
4036 		 */
4037 		lp = &fcp->portdb[dbidx];
4038 		lp->handle = handle;
4039 		lp->node_wwn = wwnn;
4040 		lp->new_portid = portid;
4041 		lp->new_prli_word3 = nr;
4042 		if (lp->portid != portid || lp->prli_word3 != nr) {
4043 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Zombie Fabric Port 0x%06x Now Changed", chan, portid);
4044 			lp->state = FC_PORTDB_STATE_CHANGED;
4045 		} else {
4046 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Zombie Fabric Port 0x%06x Now Pending Valid", chan, portid);
4047 			lp->state = FC_PORTDB_STATE_PENDING_VALID;
4048 		}
4049 	}
4050 
4051 	FC_SCRATCH_RELEASE(isp, chan);
4052 	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
4053 		ISP_MARK_PORTDB(isp, chan, 1);
4054 		return (-1);
4055 	}
4056 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
4057 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Scan Fabric Done", chan);
4058 	return (0);
4059 }
4060 
4061 /*
4062  * Find an unused handle and try and use to login to a port.
4063  */
4064 static int
4065 isp_login_device(ispsoftc_t *isp, int chan, uint32_t portid, isp_pdb_t *p, uint16_t *ohp)
4066 {
4067 	int lim, i, r;
4068 	uint16_t handle;
4069 
4070 	if (ISP_CAP_2KLOGIN(isp)) {
4071 		lim = NPH_MAX_2K;
4072 	} else {
4073 		lim = NPH_MAX;
4074 	}
4075 
4076 	handle = isp_next_handle(isp, ohp);
4077 	for (i = 0; i < lim; i++) {
4078 		/*
4079 		 * See if we're still logged into something with
4080 		 * this handle and that something agrees with this
4081 		 * port id.
4082 		 */
4083 		r = isp_getpdb(isp, chan, handle, p, 0);
4084 		if (r == 0 && p->portid != portid) {
4085 			(void) isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_LOGO | PLOGX_FLG_IMPLICIT | PLOGX_FLG_FREE_NPHDL, 1);
4086 		} else if (r == 0) {
4087 			break;
4088 		}
4089 		if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4090 			return (-1);
4091 		}
4092 		/*
4093 		 * Now try and log into the device
4094 		 */
4095 		r = isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_PLOGI, 1);
4096 		if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4097 			return (-1);
4098 		}
4099 		if (r == 0) {
4100 			break;
4101 		} else if ((r & 0xffff) == MBOX_PORT_ID_USED) {
4102 			/*
4103 			 * If we get here, then the firmwware still thinks we're logged into this device, but with a different
4104 			 * handle. We need to break that association. We used to try and just substitute the handle, but then
4105 			 * failed to get any data via isp_getpdb (below).
4106 			 */
4107 			if (isp_plogx(isp, chan, r >> 16, portid, PLOGX_FLG_CMD_LOGO | PLOGX_FLG_IMPLICIT | PLOGX_FLG_FREE_NPHDL, 1)) {
4108 				isp_prt(isp, ISP_LOGERR, "baw... logout of %x failed", r >> 16);
4109 			}
4110 			if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4111 				return (-1);
4112 			}
4113 			r = isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_PLOGI, 1);
4114 			if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4115 				return (-1);
4116 			}
4117 			if (r != 0)
4118 				i = lim;
4119 			break;
4120 		} else if ((r & 0xffff) == MBOX_LOOP_ID_USED) {
4121 			/* Try the next loop id. */
4122 			handle = isp_next_handle(isp, ohp);
4123 		} else {
4124 			/* Give up. */
4125 			i = lim;
4126 			break;
4127 		}
4128 	}
4129 
4130 	if (i == lim) {
4131 		isp_prt(isp, ISP_LOGWARN, "Chan %d PLOGI 0x%06x failed", chan, portid);
4132 		return (-1);
4133 	}
4134 
4135 	/*
4136 	 * If we successfully logged into it, get the PDB for it
4137 	 * so we can crosscheck that it is still what we think it
4138 	 * is and that we also have the role it plays
4139 	 */
4140 	r = isp_getpdb(isp, chan, handle, p, 0);
4141 	if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4142 		return (-1);
4143 	}
4144 	if (r != 0) {
4145 		isp_prt(isp, ISP_LOGERR, "Chan %d new device 0x%06x@0x%x disappeared", chan, portid, handle);
4146 		return (-1);
4147 	}
4148 
4149 	if (p->handle != handle || p->portid != portid) {
4150 		isp_prt(isp, ISP_LOGERR, "Chan %d new device 0x%06x@0x%x changed (0x%06x@0x%0x)",
4151 		    chan, portid, handle, p->portid, p->handle);
4152 		return (-1);
4153 	}
4154 	return (0);
4155 }
4156 
4157 static int
4158 isp_register_fc4_type(ispsoftc_t *isp, int chan)
4159 {
4160 	fcparam *fcp = FCPARAM(isp, chan);
4161 	uint8_t local[SNS_RFT_ID_REQ_SIZE];
4162 	sns_screq_t *reqp = (sns_screq_t *) local;
4163 	mbreg_t mbs;
4164 
4165 	ISP_MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
4166 	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
4167 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
4168 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
4169 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
4170 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
4171 	reqp->snscb_sblen = 22;
4172 	reqp->snscb_data[0] = SNS_RFT_ID;
4173 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
4174 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
4175 	reqp->snscb_data[6] = (1 << FC4_SCSI);
4176 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4177 		isp_prt(isp, ISP_LOGERR, sacq);
4178 		return (-1);
4179 	}
4180 	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
4181 	MBSINIT(&mbs, MBOX_SEND_SNS, MBLOGALL, 1000000);
4182 	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
4183 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4184 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4185 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4186 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4187 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_RFT_ID_REQ_SIZE, chan);
4188 	isp_mboxcmd(isp, &mbs);
4189 	FC_SCRATCH_RELEASE(isp, chan);
4190 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
4191 		return (0);
4192 	} else {
4193 		return (-1);
4194 	}
4195 }
4196 
4197 static int
4198 isp_register_fc4_type_24xx(ispsoftc_t *isp, int chan)
4199 {
4200 	mbreg_t mbs;
4201 	fcparam *fcp = FCPARAM(isp, chan);
4202 	union {
4203 		isp_ct_pt_t plocal;
4204 		rft_id_t clocal;
4205 		uint8_t q[QENTRY_LEN];
4206 	} un;
4207 	isp_ct_pt_t *pt;
4208 	ct_hdr_t *ct;
4209 	rft_id_t *rp;
4210 	uint8_t *scp = fcp->isp_scratch;
4211 
4212 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4213 		isp_prt(isp, ISP_LOGERR, sacq);
4214 		return (-1);
4215 	}
4216 
4217 	/*
4218 	 * Build a Passthrough IOCB in memory.
4219 	 */
4220 	ISP_MEMZERO(un.q, QENTRY_LEN);
4221 	pt = &un.plocal;
4222 	pt->ctp_header.rqs_entry_count = 1;
4223 	pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
4224 	pt->ctp_handle = 0xffffffff;
4225 	pt->ctp_nphdl = fcp->isp_sns_hdl;
4226 	pt->ctp_cmd_cnt = 1;
4227 	pt->ctp_vpidx = ISP_GET_VPIDX(isp, chan);
4228 	pt->ctp_time = 1;
4229 	pt->ctp_rsp_cnt = 1;
4230 	pt->ctp_rsp_bcnt = sizeof (ct_hdr_t);
4231 	pt->ctp_cmd_bcnt = sizeof (rft_id_t);
4232 	pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
4233 	pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
4234 	pt->ctp_dataseg[0].ds_count = sizeof (rft_id_t);
4235 	pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
4236 	pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
4237 	pt->ctp_dataseg[1].ds_count = sizeof (ct_hdr_t);
4238 	isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
4239 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
4240 		isp_print_bytes(isp, "IOCB CT Request", QENTRY_LEN, pt);
4241 	}
4242 
4243 	/*
4244 	 * Build the CT header and command in memory.
4245 	 *
4246 	 * Note that the CT header has to end up as Big Endian format in memory.
4247 	 */
4248 	ISP_MEMZERO(&un.clocal, sizeof (un.clocal));
4249 	ct = &un.clocal.rftid_hdr;
4250 	ct->ct_revision = CT_REVISION;
4251 	ct->ct_fcs_type = CT_FC_TYPE_FC;
4252 	ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
4253 	ct->ct_cmd_resp = SNS_RFT_ID;
4254 	ct->ct_bcnt_resid = (sizeof (rft_id_t) - sizeof (ct_hdr_t)) >> 2;
4255 	rp = &un.clocal;
4256 	rp->rftid_portid[0] = fcp->isp_portid >> 16;
4257 	rp->rftid_portid[1] = fcp->isp_portid >> 8;
4258 	rp->rftid_portid[2] = fcp->isp_portid;
4259 	rp->rftid_fc4types[FC4_SCSI >> 5] = 1 << (FC4_SCSI & 0x1f);
4260 	isp_put_rft_id(isp, rp, (rft_id_t *) &scp[XTXOFF]);
4261 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
4262 		isp_print_bytes(isp, "CT Header", QENTRY_LEN, &scp[XTXOFF]);
4263 	}
4264 
4265 	ISP_MEMZERO(&scp[ZTXOFF], sizeof (ct_hdr_t));
4266 
4267 	MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 1000000);
4268 	mbs.param[1] = QENTRY_LEN;
4269 	mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
4270 	mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
4271 	mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
4272 	mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
4273 	MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN, chan);
4274 	isp_mboxcmd(isp, &mbs);
4275 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4276 		FC_SCRATCH_RELEASE(isp, chan);
4277 		return (-1);
4278 	}
4279 	MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN, chan);
4280 	pt = &un.plocal;
4281 	isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
4282 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
4283 		isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
4284 	}
4285 	if (pt->ctp_status) {
4286 		FC_SCRATCH_RELEASE(isp, chan);
4287 		isp_prt(isp, ISP_LOGWARN,
4288 		    "Chan %d Register FC4 Type CT Passthrough returned 0x%x",
4289 		    chan, pt->ctp_status);
4290 		return (1);
4291 	}
4292 
4293 	isp_get_ct_hdr(isp, (ct_hdr_t *) &scp[IGPOFF], ct);
4294 	FC_SCRATCH_RELEASE(isp, chan);
4295 
4296 	if (ct->ct_cmd_resp == LS_RJT) {
4297 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "Chan %d Register FC4 Type rejected", chan);
4298 		return (-1);
4299 	} else if (ct->ct_cmd_resp == LS_ACC) {
4300 		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Register FC4 Type accepted", chan);
4301 		return (0);
4302 	} else {
4303 		isp_prt(isp, ISP_LOGWARN, "Chan %d Register FC4 Type: 0x%x", chan, ct->ct_cmd_resp);
4304 		return (-1);
4305 	}
4306 }
4307 
4308 static uint16_t
4309 isp_next_handle(ispsoftc_t *isp, uint16_t *ohp)
4310 {
4311 	fcparam *fcp;
4312 	int i, chan, wrap;
4313 	uint16_t handle, minh, maxh;
4314 
4315 	handle = *ohp;
4316 	if (ISP_CAP_2KLOGIN(isp)) {
4317 		minh = 0;
4318 		maxh = NPH_RESERVED - isp->isp_nchan; /* Reserve for SNS */
4319 	} else {
4320 		minh = SNS_ID + 1;
4321 		maxh = NPH_MAX - 1;
4322 	}
4323 	wrap = 0;
4324 
4325 next:
4326 	if (handle == NIL_HANDLE) {
4327 		handle = minh;
4328 	} else {
4329 		handle++;
4330 		if (handle > maxh) {
4331 			if (++wrap >= 2) {
4332 				isp_prt(isp, ISP_LOGERR, "Out of port handles!");
4333 				return (NIL_HANDLE);
4334 			}
4335 			handle = minh;
4336 		}
4337 	}
4338 	for (chan = 0; chan < isp->isp_nchan; chan++) {
4339 		fcp = FCPARAM(isp, chan);
4340 		if (fcp->role == ISP_ROLE_NONE)
4341 			continue;
4342 		for (i = 0; i < MAX_FC_TARG; i++) {
4343 			if (fcp->portdb[i].state != FC_PORTDB_STATE_NIL &&
4344 			    fcp->portdb[i].handle == handle)
4345 				goto next;
4346 		}
4347 	}
4348 	*ohp = handle;
4349 	return (handle);
4350 }
4351 
4352 /*
4353  * Start a command. Locking is assumed done in the caller.
4354  */
4355 
4356 int
4357 isp_start(XS_T *xs)
4358 {
4359 	ispsoftc_t *isp;
4360 	uint32_t handle, cdblen;
4361 	uint8_t local[QENTRY_LEN];
4362 	ispreq_t *reqp;
4363 	void *cdbp, *qep;
4364 	uint16_t *tptr;
4365 	fcportdb_t *lp;
4366 	int target, dmaresult;
4367 
4368 	XS_INITERR(xs);
4369 	isp = XS_ISP(xs);
4370 
4371 	/*
4372 	 * Check command CDB length, etc.. We really are limited to 16 bytes
4373 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
4374 	 * but probably only if we're running fairly new firmware (we'll
4375 	 * let the old f/w choke on an extended command queue entry).
4376 	 */
4377 
4378 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
4379 		isp_prt(isp, ISP_LOGERR, "unsupported cdb length (%d, CDB[0]=0x%x)", XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
4380 		XS_SETERR(xs, HBA_BOTCH);
4381 		return (CMD_COMPLETE);
4382 	}
4383 
4384 	/*
4385 	 * Translate the target to device handle as appropriate, checking
4386 	 * for correct device state as well.
4387 	 */
4388 	target = XS_TGT(xs);
4389 	if (IS_FC(isp)) {
4390 		fcparam *fcp = FCPARAM(isp, XS_CHANNEL(xs));
4391 
4392 		if ((fcp->role & ISP_ROLE_INITIATOR) == 0) {
4393 			isp_prt(isp, ISP_LOG_WARN1,
4394 			    "%d.%d.%jx I am not an initiator",
4395 			    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4396 			XS_SETERR(xs, HBA_SELTIMEOUT);
4397 			return (CMD_COMPLETE);
4398 		}
4399 
4400 		if (isp->isp_state != ISP_RUNSTATE) {
4401 			isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
4402 			XS_SETERR(xs, HBA_BOTCH);
4403 			return (CMD_COMPLETE);
4404 		}
4405 
4406 		/*
4407 		 * Try again later.
4408 		 */
4409 		if (fcp->isp_fwstate != FW_READY || fcp->isp_loopstate != LOOP_READY) {
4410 			return (CMD_RQLATER);
4411 		}
4412 
4413 		isp_prt(isp, ISP_LOGDEBUG2, "XS_TGT(xs)=%d", target);
4414 		lp = &fcp->portdb[target];
4415 		if (target < 0 || target >= MAX_FC_TARG ||
4416 		    lp->is_target == 0) {
4417 			XS_SETERR(xs, HBA_SELTIMEOUT);
4418 			return (CMD_COMPLETE);
4419 		}
4420 		if (lp->state == FC_PORTDB_STATE_ZOMBIE) {
4421 			isp_prt(isp, ISP_LOGDEBUG1,
4422 			    "%d.%d.%jx target zombie",
4423 			    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4424 			return (CMD_RQLATER);
4425 		}
4426 		if (lp->state != FC_PORTDB_STATE_VALID) {
4427 			isp_prt(isp, ISP_LOGDEBUG1,
4428 			    "%d.%d.%jx bad db port state 0x%x",
4429 			    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs), lp->state);
4430 			XS_SETERR(xs, HBA_SELTIMEOUT);
4431 			return (CMD_COMPLETE);
4432 		}
4433 	} else {
4434 		sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
4435 		if ((sdp->role & ISP_ROLE_INITIATOR) == 0) {
4436 			isp_prt(isp, ISP_LOGDEBUG1,
4437 			    "%d.%d.%jx I am not an initiator",
4438 			    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4439 			XS_SETERR(xs, HBA_SELTIMEOUT);
4440 			return (CMD_COMPLETE);
4441 		}
4442 
4443 		if (isp->isp_state != ISP_RUNSTATE) {
4444 			isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
4445 			XS_SETERR(xs, HBA_BOTCH);
4446 			return (CMD_COMPLETE);
4447 		}
4448 
4449 		if (sdp->update) {
4450 			isp_spi_update(isp, XS_CHANNEL(xs));
4451 		}
4452 		lp = NULL;
4453 	}
4454 
4455  start_again:
4456 
4457 	qep = isp_getrqentry(isp);
4458 	if (qep == NULL) {
4459 		isp_prt(isp, ISP_LOG_WARN1, "Request Queue Overflow");
4460 		XS_SETERR(xs, HBA_BOTCH);
4461 		return (CMD_EAGAIN);
4462 	}
4463 	XS_SETERR(xs, HBA_NOERROR);
4464 
4465 	/*
4466 	 * Now see if we need to synchronize the ISP with respect to anything.
4467 	 * We do dual duty here (cough) for synchronizing for busses other
4468 	 * than which we got here to send a command to.
4469 	 */
4470 	reqp = (ispreq_t *) local;
4471 	ISP_MEMZERO(local, QENTRY_LEN);
4472 	if (ISP_TST_SENDMARKER(isp, XS_CHANNEL(xs))) {
4473 		if (IS_24XX(isp)) {
4474 			isp_marker_24xx_t *m = (isp_marker_24xx_t *) reqp;
4475 			m->mrk_header.rqs_entry_count = 1;
4476 			m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4477 			m->mrk_modifier = SYNC_ALL;
4478 			isp_put_marker_24xx(isp, m, qep);
4479 		} else {
4480 			isp_marker_t *m = (isp_marker_t *) reqp;
4481 			m->mrk_header.rqs_entry_count = 1;
4482 			m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4483 			m->mrk_target = (XS_CHANNEL(xs) << 7);	/* bus # */
4484 			m->mrk_modifier = SYNC_ALL;
4485 			isp_put_marker(isp, m, qep);
4486 		}
4487 		ISP_SYNC_REQUEST(isp);
4488 		ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 0);
4489 		goto start_again;
4490 	}
4491 
4492 	reqp->req_header.rqs_entry_count = 1;
4493 
4494 	/*
4495 	 * Select and install Header Code.
4496 	 * Note that it might be overridden before going out
4497 	 * if we're on a 64 bit platform. The lower level
4498 	 * code (isp_send_cmd) will select the appropriate
4499 	 * 64 bit variant if it needs to.
4500 	 */
4501 	if (IS_24XX(isp)) {
4502 		reqp->req_header.rqs_entry_type = RQSTYPE_T7RQS;
4503 	} else if (IS_FC(isp)) {
4504 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
4505 	} else {
4506 		if (XS_CDBLEN(xs) > 12) {
4507 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
4508 		} else {
4509 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
4510 		}
4511 	}
4512 
4513 	/*
4514 	 * Set task attributes
4515 	 */
4516 	if (IS_24XX(isp)) {
4517 		int ttype;
4518 		if (XS_TAG_P(xs)) {
4519 			ttype = XS_TAG_TYPE(xs);
4520 		} else {
4521 			if (XS_CDBP(xs)[0] == 0x3) {
4522 				ttype = REQFLAG_HTAG;
4523 			} else {
4524 				ttype = REQFLAG_STAG;
4525 			}
4526 		}
4527 		if (ttype == REQFLAG_OTAG) {
4528 			ttype = FCP_CMND_TASK_ATTR_ORDERED;
4529 		} else if (ttype == REQFLAG_HTAG) {
4530 			ttype = FCP_CMND_TASK_ATTR_HEAD;
4531 		} else {
4532 			ttype = FCP_CMND_TASK_ATTR_SIMPLE;
4533 		}
4534 		((ispreqt7_t *)reqp)->req_task_attribute = ttype;
4535 	} else if (IS_FC(isp)) {
4536 		/*
4537 		 * See comment in isp_intr
4538 		 */
4539 		/* XS_SET_RESID(xs, 0); */
4540 
4541 		/*
4542 		 * Fibre Channel always requires some kind of tag.
4543 		 * The Qlogic drivers seem be happy not to use a tag,
4544 		 * but this breaks for some devices (IBM drives).
4545 		 */
4546 		if (XS_TAG_P(xs)) {
4547 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
4548 		} else {
4549 			/*
4550 			 * If we don't know what tag to use, use HEAD OF QUEUE
4551 			 * for Request Sense or Simple.
4552 			 */
4553 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
4554 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
4555 			else
4556 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
4557 		}
4558 	} else {
4559 		sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
4560 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) && XS_TAG_P(xs)) {
4561 			reqp->req_flags = XS_TAG_TYPE(xs);
4562 		}
4563 	}
4564 
4565 	tptr = &reqp->req_time;
4566 
4567 	/*
4568 	 * NB: we do not support long CDBs (yet)
4569 	 */
4570 	cdblen = XS_CDBLEN(xs);
4571 
4572 	if (IS_SCSI(isp)) {
4573 		if (cdblen > sizeof (reqp->req_cdb)) {
4574 			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4575 			XS_SETERR(xs, HBA_BOTCH);
4576 			return (CMD_COMPLETE);
4577 		}
4578 		reqp->req_target = target | (XS_CHANNEL(xs) << 7);
4579 		reqp->req_lun_trn = XS_LUN(xs);
4580 		cdbp = reqp->req_cdb;
4581 		reqp->req_cdblen = cdblen;
4582 	} else if (IS_24XX(isp)) {
4583 		ispreqt7_t *t7 = (ispreqt7_t *)local;
4584 
4585 		if (cdblen > sizeof (t7->req_cdb)) {
4586 			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4587 			XS_SETERR(xs, HBA_BOTCH);
4588 			return (CMD_COMPLETE);
4589 		}
4590 
4591 		t7->req_nphdl = lp->handle;
4592 		t7->req_tidlo = lp->portid;
4593 		t7->req_tidhi = lp->portid >> 16;
4594 		t7->req_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(xs));
4595 #if __FreeBSD_version >= 1000700
4596 		be64enc(t7->req_lun, CAM_EXTLUN_BYTE_SWIZZLE(XS_LUN(xs)));
4597 #else
4598 		if (XS_LUN(xs) >= 256) {
4599 			t7->req_lun[0] = XS_LUN(xs) >> 8;
4600 			t7->req_lun[0] |= 0x40;
4601 		}
4602 		t7->req_lun[1] = XS_LUN(xs);
4603 #endif
4604 		if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4605 			if (FCP_NEXT_CRN(isp, &t7->req_crn, xs)) {
4606 				isp_prt(isp, ISP_LOG_WARN1,
4607 				    "%d.%d.%jx cannot generate next CRN",
4608 				    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4609 				XS_SETERR(xs, HBA_BOTCH);
4610 				return (CMD_EAGAIN);
4611 			}
4612 		}
4613 		tptr = &t7->req_time;
4614 		cdbp = t7->req_cdb;
4615 	} else {
4616 		ispreqt2_t *t2 = (ispreqt2_t *)local;
4617 
4618 		if (cdblen > sizeof t2->req_cdb) {
4619 			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4620 			XS_SETERR(xs, HBA_BOTCH);
4621 			return (CMD_COMPLETE);
4622 		}
4623 		if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4624 			if (FCP_NEXT_CRN(isp, &t2->req_crn, xs)) {
4625 				isp_prt(isp, ISP_LOG_WARN1,
4626 				    "%d.%d.%jx cannot generate next CRN",
4627 				    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4628 				XS_SETERR(xs, HBA_BOTCH);
4629 				return (CMD_EAGAIN);
4630 			}
4631 		}
4632 		if (ISP_CAP_2KLOGIN(isp)) {
4633 			ispreqt2e_t *t2e = (ispreqt2e_t *)local;
4634 			t2e->req_target = lp->handle;
4635 			t2e->req_scclun = XS_LUN(xs);
4636 #if __FreeBSD_version < 1000700
4637 			if (XS_LUN(xs) >= 256)
4638 				t2e->req_scclun |= 0x4000;
4639 #endif
4640 			cdbp = t2e->req_cdb;
4641 		} else if (ISP_CAP_SCCFW(isp)) {
4642 			ispreqt2_t *t2 = (ispreqt2_t *)local;
4643 			t2->req_target = lp->handle;
4644 			t2->req_scclun = XS_LUN(xs);
4645 #if __FreeBSD_version < 1000700
4646 			if (XS_LUN(xs) >= 256)
4647 				t2->req_scclun |= 0x4000;
4648 #endif
4649 			cdbp = t2->req_cdb;
4650 		} else {
4651 			t2->req_target = lp->handle;
4652 			t2->req_lun_trn = XS_LUN(xs);
4653 			cdbp = t2->req_cdb;
4654 		}
4655 	}
4656 	ISP_MEMCPY(cdbp, XS_CDBP(xs), cdblen);
4657 
4658 	*tptr = XS_TIME(xs) / 1000;
4659 	if (*tptr == 0 && XS_TIME(xs)) {
4660 		*tptr = 1;
4661 	}
4662 	if (IS_24XX(isp) && *tptr > 0x1999) {
4663 		*tptr = 0x1999;
4664 	}
4665 
4666 	if (isp_allocate_xs(isp, xs, &handle)) {
4667 		isp_prt(isp, ISP_LOG_WARN1, "out of xflist pointers");
4668 		XS_SETERR(xs, HBA_BOTCH);
4669 		return (CMD_EAGAIN);
4670 	}
4671 	/* Whew. Thankfully the same for type 7 requests */
4672 	reqp->req_handle = handle;
4673 
4674 	/*
4675 	 * Set up DMA and/or do any platform dependent swizzling of the request entry
4676 	 * so that the Qlogic F/W understands what is being asked of it.
4677 	 *
4678 	 * The callee is responsible for adding all requests at this point.
4679 	 */
4680 	dmaresult = ISP_DMASETUP(isp, xs, reqp);
4681 	if (dmaresult != CMD_QUEUED) {
4682 		isp_destroy_handle(isp, handle);
4683 		/*
4684 		 * dmasetup sets actual error in packet, and
4685 		 * return what we were given to return.
4686 		 */
4687 		return (dmaresult);
4688 	}
4689 	isp_xs_prt(isp, xs, ISP_LOGDEBUG0, "START cmd cdb[0]=0x%x datalen %ld", XS_CDBP(xs)[0], (long) XS_XFRLEN(xs));
4690 	isp->isp_nactive++;
4691 	return (CMD_QUEUED);
4692 }
4693 
4694 /*
4695  * isp control
4696  * Locks (ints blocked) assumed held.
4697  */
4698 
4699 int
4700 isp_control(ispsoftc_t *isp, ispctl_t ctl, ...)
4701 {
4702 	XS_T *xs;
4703 	mbreg_t *mbr, mbs;
4704 	int chan, tgt;
4705 	uint32_t handle;
4706 	va_list ap;
4707 
4708 	switch (ctl) {
4709 	case ISPCTL_RESET_BUS:
4710 		/*
4711 		 * Issue a bus reset.
4712 		 */
4713 		if (IS_24XX(isp)) {
4714 			isp_prt(isp, ISP_LOGERR, "BUS RESET NOT IMPLEMENTED");
4715 			break;
4716 		} else if (IS_FC(isp)) {
4717 			mbs.param[1] = 10;
4718 			chan = 0;
4719 		} else {
4720 			va_start(ap, ctl);
4721 			chan = va_arg(ap, int);
4722 			va_end(ap);
4723 			mbs.param[1] = SDPARAM(isp, chan)->isp_bus_reset_delay;
4724 			if (mbs.param[1] < 2) {
4725 				mbs.param[1] = 2;
4726 			}
4727 			mbs.param[2] = chan;
4728 		}
4729 		MBSINIT(&mbs, MBOX_BUS_RESET, MBLOGALL, 0);
4730 		ISP_SET_SENDMARKER(isp, chan, 1);
4731 		isp_mboxcmd(isp, &mbs);
4732 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4733 			break;
4734 		}
4735 		isp_prt(isp, ISP_LOGINFO, "driver initiated bus reset of bus %d", chan);
4736 		return (0);
4737 
4738 	case ISPCTL_RESET_DEV:
4739 		va_start(ap, ctl);
4740 		chan = va_arg(ap, int);
4741 		tgt = va_arg(ap, int);
4742 		va_end(ap);
4743 		if (IS_24XX(isp)) {
4744 			uint8_t local[QENTRY_LEN];
4745 			isp24xx_tmf_t *tmf;
4746 			isp24xx_statusreq_t *sp;
4747 			fcparam *fcp = FCPARAM(isp, chan);
4748 			fcportdb_t *lp;
4749 
4750 			if (tgt < 0 || tgt >= MAX_FC_TARG) {
4751 				isp_prt(isp, ISP_LOGWARN, "Chan %d trying to reset bad target %d", chan, tgt);
4752 				break;
4753 			}
4754 			lp = &fcp->portdb[tgt];
4755 			if (lp->is_target == 0 ||
4756 			    lp->state != FC_PORTDB_STATE_VALID) {
4757 				isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4758 				break;
4759 			}
4760 
4761 			tmf = (isp24xx_tmf_t *) local;
4762 			ISP_MEMZERO(tmf, QENTRY_LEN);
4763 			tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
4764 			tmf->tmf_header.rqs_entry_count = 1;
4765 			tmf->tmf_nphdl = lp->handle;
4766 			tmf->tmf_delay = 2;
4767 			tmf->tmf_timeout = 2;
4768 			tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
4769 			tmf->tmf_tidlo = lp->portid;
4770 			tmf->tmf_tidhi = lp->portid >> 16;
4771 			tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
4772 			isp_prt(isp, ISP_LOGALL, "Chan %d Reset N-Port Handle 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4773 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
4774 			mbs.param[1] = QENTRY_LEN;
4775 			mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4776 			mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4777 			mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4778 			mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4779 
4780 			if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4781 				isp_prt(isp, ISP_LOGERR, sacq);
4782 				break;
4783 			}
4784 			isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
4785 			MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
4786 			fcp->sendmarker = 1;
4787 			isp_mboxcmd(isp, &mbs);
4788 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4789 				FC_SCRATCH_RELEASE(isp, chan);
4790 				break;
4791 			}
4792 			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4793 			sp = (isp24xx_statusreq_t *) local;
4794 			isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
4795 			FC_SCRATCH_RELEASE(isp, chan);
4796 			if (sp->req_completion_status == 0) {
4797 				return (0);
4798 			}
4799 			isp_prt(isp, ISP_LOGWARN, "Chan %d reset of target %d returned 0x%x", chan, tgt, sp->req_completion_status);
4800 			break;
4801 		} else if (IS_FC(isp)) {
4802 			if (ISP_CAP_2KLOGIN(isp)) {
4803 				mbs.param[1] = tgt;
4804 				mbs.ibits = (1 << 10);
4805 			} else {
4806 				mbs.param[1] = (tgt << 8);
4807 			}
4808 		} else {
4809 			mbs.param[1] = (chan << 15) | (tgt << 8);
4810 		}
4811 		MBSINIT(&mbs, MBOX_ABORT_TARGET, MBLOGALL, 0);
4812 		mbs.param[2] = 3;	/* 'delay', in seconds */
4813 		isp_mboxcmd(isp, &mbs);
4814 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4815 			break;
4816 		}
4817 		isp_prt(isp, ISP_LOGINFO, "Target %d on Bus %d Reset Succeeded", tgt, chan);
4818 		ISP_SET_SENDMARKER(isp, chan, 1);
4819 		return (0);
4820 
4821 	case ISPCTL_ABORT_CMD:
4822 		va_start(ap, ctl);
4823 		xs = va_arg(ap, XS_T *);
4824 		va_end(ap);
4825 
4826 		tgt = XS_TGT(xs);
4827 		chan = XS_CHANNEL(xs);
4828 
4829 		handle = isp_find_handle(isp, xs);
4830 		if (handle == 0) {
4831 			isp_prt(isp, ISP_LOGWARN, "cannot find handle for command to abort");
4832 			break;
4833 		}
4834 		if (IS_24XX(isp)) {
4835 			isp24xx_abrt_t local, *ab = &local, *ab2;
4836 			fcparam *fcp;
4837 			fcportdb_t *lp;
4838 
4839 			fcp = FCPARAM(isp, chan);
4840 			if (tgt < 0 || tgt >= MAX_FC_TARG) {
4841 				isp_prt(isp, ISP_LOGWARN, "Chan %d trying to abort bad target %d", chan, tgt);
4842 				break;
4843 			}
4844 			lp = &fcp->portdb[tgt];
4845 			if (lp->is_target == 0 ||
4846 			    lp->state != FC_PORTDB_STATE_VALID) {
4847 				isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4848 				break;
4849 			}
4850 			isp_prt(isp, ISP_LOGALL, "Chan %d Abort Cmd for N-Port 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4851 			ISP_MEMZERO(ab, QENTRY_LEN);
4852 			ab->abrt_header.rqs_entry_type = RQSTYPE_ABORT_IO;
4853 			ab->abrt_header.rqs_entry_count = 1;
4854 			ab->abrt_handle = lp->handle;
4855 			ab->abrt_cmd_handle = handle;
4856 			ab->abrt_tidlo = lp->portid;
4857 			ab->abrt_tidhi = lp->portid >> 16;
4858 			ab->abrt_vpidx = ISP_GET_VPIDX(isp, chan);
4859 
4860 			ISP_MEMZERO(&mbs, sizeof (mbs));
4861 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
4862 			mbs.param[1] = QENTRY_LEN;
4863 			mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4864 			mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4865 			mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4866 			mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4867 
4868 			if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4869 				isp_prt(isp, ISP_LOGERR, sacq);
4870 				break;
4871 			}
4872 			isp_put_24xx_abrt(isp, ab, fcp->isp_scratch);
4873 			ab2 = (isp24xx_abrt_t *) &((uint8_t *)fcp->isp_scratch)[QENTRY_LEN];
4874 			ab2->abrt_nphdl = 0xdeaf;
4875 			MEMORYBARRIER(isp, SYNC_SFORDEV, 0, 2 * QENTRY_LEN, chan);
4876 			isp_mboxcmd(isp, &mbs);
4877 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4878 				FC_SCRATCH_RELEASE(isp, chan);
4879 				break;
4880 			}
4881 			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4882 			isp_get_24xx_abrt(isp, ab2, ab);
4883 			FC_SCRATCH_RELEASE(isp, chan);
4884 			if (ab->abrt_nphdl == ISP24XX_ABRT_OKAY) {
4885 				return (0);
4886 			}
4887 			isp_prt(isp, ISP_LOGWARN, "Chan %d handle %d abort returned 0x%x", chan, tgt, ab->abrt_nphdl);
4888 			break;
4889 		} else if (IS_FC(isp)) {
4890 			if (ISP_CAP_SCCFW(isp)) {
4891 				if (ISP_CAP_2KLOGIN(isp)) {
4892 					mbs.param[1] = tgt;
4893 				} else {
4894 					mbs.param[1] = tgt << 8;
4895 				}
4896 				mbs.param[6] = XS_LUN(xs);
4897 			} else {
4898 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
4899 			}
4900 		} else {
4901 			mbs.param[1] = (chan << 15) | (tgt << 8) | XS_LUN(xs);
4902 		}
4903 		MBSINIT(&mbs, MBOX_ABORT,
4904 		    MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_ERROR), 0);
4905 		mbs.param[2] = handle;
4906 		isp_mboxcmd(isp, &mbs);
4907 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4908 			break;
4909 		}
4910 		return (0);
4911 
4912 	case ISPCTL_UPDATE_PARAMS:
4913 
4914 		va_start(ap, ctl);
4915 		chan = va_arg(ap, int);
4916 		va_end(ap);
4917 		isp_spi_update(isp, chan);
4918 		return (0);
4919 
4920 	case ISPCTL_FCLINK_TEST:
4921 
4922 		if (IS_FC(isp)) {
4923 			int usdelay;
4924 			va_start(ap, ctl);
4925 			chan = va_arg(ap, int);
4926 			usdelay = va_arg(ap, int);
4927 			va_end(ap);
4928 			if (usdelay == 0) {
4929 				usdelay =  250000;
4930 			}
4931 			return (isp_fclink_test(isp, chan, usdelay));
4932 		}
4933 		break;
4934 
4935 	case ISPCTL_SCAN_FABRIC:
4936 
4937 		if (IS_FC(isp)) {
4938 			va_start(ap, ctl);
4939 			chan = va_arg(ap, int);
4940 			va_end(ap);
4941 			return (isp_scan_fabric(isp, chan));
4942 		}
4943 		break;
4944 
4945 	case ISPCTL_SCAN_LOOP:
4946 
4947 		if (IS_FC(isp)) {
4948 			va_start(ap, ctl);
4949 			chan = va_arg(ap, int);
4950 			va_end(ap);
4951 			return (isp_scan_loop(isp, chan));
4952 		}
4953 		break;
4954 
4955 	case ISPCTL_PDB_SYNC:
4956 
4957 		if (IS_FC(isp)) {
4958 			va_start(ap, ctl);
4959 			chan = va_arg(ap, int);
4960 			va_end(ap);
4961 			return (isp_pdb_sync(isp, chan));
4962 		}
4963 		break;
4964 
4965 	case ISPCTL_SEND_LIP:
4966 
4967 		if (IS_FC(isp) && !IS_24XX(isp)) {
4968 			MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
4969 			if (ISP_CAP_2KLOGIN(isp)) {
4970 				mbs.ibits = (1 << 10);
4971 			}
4972 			isp_mboxcmd(isp, &mbs);
4973 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
4974 				return (0);
4975 			}
4976 		}
4977 		break;
4978 
4979 	case ISPCTL_GET_PDB:
4980 		if (IS_FC(isp)) {
4981 			isp_pdb_t *pdb;
4982 			va_start(ap, ctl);
4983 			chan = va_arg(ap, int);
4984 			tgt = va_arg(ap, int);
4985 			pdb = va_arg(ap, isp_pdb_t *);
4986 			va_end(ap);
4987 			return (isp_getpdb(isp, chan, tgt, pdb, 1));
4988 		}
4989 		break;
4990 
4991 	case ISPCTL_GET_NAMES:
4992 	{
4993 		uint64_t *wwnn, *wwnp;
4994 		va_start(ap, ctl);
4995 		chan = va_arg(ap, int);
4996 		tgt = va_arg(ap, int);
4997 		wwnn = va_arg(ap, uint64_t *);
4998 		wwnp = va_arg(ap, uint64_t *);
4999 		va_end(ap);
5000 		if (wwnn == NULL && wwnp == NULL) {
5001 			break;
5002 		}
5003 		if (wwnn) {
5004 			*wwnn = isp_get_wwn(isp, chan, tgt, 1);
5005 			if (*wwnn == INI_NONE) {
5006 				break;
5007 			}
5008 		}
5009 		if (wwnp) {
5010 			*wwnp = isp_get_wwn(isp, chan, tgt, 0);
5011 			if (*wwnp == INI_NONE) {
5012 				break;
5013 			}
5014 		}
5015 		return (0);
5016 	}
5017 	case ISPCTL_RUN_MBOXCMD:
5018 	{
5019 		va_start(ap, ctl);
5020 		mbr = va_arg(ap, mbreg_t *);
5021 		va_end(ap);
5022 		isp_mboxcmd(isp, mbr);
5023 		return (0);
5024 	}
5025 	case ISPCTL_PLOGX:
5026 	{
5027 		isp_plcmd_t *p;
5028 		int r;
5029 
5030 		va_start(ap, ctl);
5031 		p = va_arg(ap, isp_plcmd_t *);
5032 		va_end(ap);
5033 
5034 		if ((p->flags & PLOGX_FLG_CMD_MASK) != PLOGX_FLG_CMD_PLOGI || (p->handle != NIL_HANDLE)) {
5035 			return (isp_plogx(isp, p->channel, p->handle, p->portid, p->flags, 0));
5036 		}
5037 		do {
5038 			isp_next_handle(isp, &p->handle);
5039 			r = isp_plogx(isp, p->channel, p->handle, p->portid, p->flags, 0);
5040 			if ((r & 0xffff) == MBOX_PORT_ID_USED) {
5041 				p->handle = r >> 16;
5042 				r = 0;
5043 				break;
5044 			}
5045 		} while ((r & 0xffff) == MBOX_LOOP_ID_USED);
5046 		return (r);
5047 	}
5048 	case ISPCTL_CHANGE_ROLE:
5049 	{
5050 		int role, r;
5051 
5052 		va_start(ap, ctl);
5053 		chan = va_arg(ap, int);
5054 		role = va_arg(ap, int);
5055 		va_end(ap);
5056 		if (IS_FC(isp)) {
5057 			r = isp_fc_change_role(isp, chan, role);
5058 		} else {
5059 			SDPARAM(isp, chan)->role = role;
5060 			r = 0;
5061 		}
5062 		return (r);
5063 	}
5064 	default:
5065 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
5066 		break;
5067 
5068 	}
5069 	return (-1);
5070 }
5071 
5072 /*
5073  * Interrupt Service Routine(s).
5074  *
5075  * External (OS) framework has done the appropriate locking,
5076  * and the locking will be held throughout this function.
5077  */
5078 
5079 /*
5080  * Limit our stack depth by sticking with the max likely number
5081  * of completions on a request queue at any one time.
5082  */
5083 #ifndef	MAX_REQUESTQ_COMPLETIONS
5084 #define	MAX_REQUESTQ_COMPLETIONS	32
5085 #endif
5086 
5087 void
5088 isp_intr(ispsoftc_t *isp, uint16_t isr, uint16_t sema, uint16_t info)
5089 {
5090 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
5091 	uint32_t iptr, optr, junk;
5092 	int i, nlooked = 0, ndone = 0, continuations_expected = 0;
5093 	int etype, last_etype = 0;
5094 
5095 again:
5096 	/*
5097 	 * Is this a mailbox related interrupt?
5098 	 * The mailbox semaphore will be nonzero if so.
5099 	 */
5100 	if (sema) {
5101  fmbox:
5102 		if (info & MBOX_COMMAND_COMPLETE) {
5103 			isp->isp_intmboxc++;
5104 			if (isp->isp_mboxbsy) {
5105 				int obits = isp->isp_obits;
5106 				isp->isp_mboxtmp[0] = info;
5107 				for (i = 1; i < ISP_NMBOX(isp); i++) {
5108 					if ((obits & (1 << i)) == 0) {
5109 						continue;
5110 					}
5111 					isp->isp_mboxtmp[i] = ISP_READ(isp, MBOX_OFF(i));
5112 				}
5113 				if (isp->isp_mbxwrk0) {
5114 					if (isp_mbox_continue(isp) == 0) {
5115 						return;
5116 					}
5117 				}
5118 				MBOX_NOTIFY_COMPLETE(isp);
5119 			} else {
5120 				isp_prt(isp, ISP_LOGWARN, "mailbox cmd (0x%x) with no waiters", info);
5121 			}
5122 		} else {
5123 			i = IS_FC(isp)? isp_parse_async_fc(isp, info) : isp_parse_async(isp, info);
5124 			if (i < 0) {
5125 				return;
5126 			}
5127 		}
5128 		if ((IS_FC(isp) && info != ASYNC_RIOZIO_STALL) || isp->isp_state != ISP_RUNSTATE) {
5129 			goto out;
5130 		}
5131 	}
5132 
5133 	/*
5134 	 * We can't be getting this now.
5135 	 */
5136 	if (isp->isp_state != ISP_RUNSTATE) {
5137 		/*
5138 		 * This seems to happen to 23XX and 24XX cards- don't know why.
5139 		 */
5140 		 if (isp->isp_mboxbsy && isp->isp_lastmbxcmd == MBOX_ABOUT_FIRMWARE) {
5141 			goto fmbox;
5142 		}
5143 		isp_prt(isp, ISP_LOGINFO, "interrupt (ISR=%x SEMA=%x INFO=%x) "
5144 		    "when not ready", isr, sema, info);
5145 		/*
5146 		 * Thank you very much!  *Burrrp*!
5147 		 */
5148 		isp->isp_residx = ISP_READ(isp, isp->isp_respinrp);
5149 		isp->isp_resodx = isp->isp_residx;
5150 		ISP_WRITE(isp, isp->isp_respoutrp, isp->isp_resodx);
5151 		if (IS_24XX(isp)) {
5152 			ISP_DISABLE_INTS(isp);
5153 		}
5154 		goto out;
5155 	}
5156 
5157 #ifdef	ISP_TARGET_MODE
5158 	/*
5159 	 * Check for ATIO Queue entries.
5160 	 */
5161 	if (IS_24XX(isp) &&
5162 	    (isr == ISPR2HST_ATIO_UPDATE || isr == ISPR2HST_ATIO_RSPQ_UPDATE ||
5163 	     isr == ISPR2HST_ATIO_UPDATE2)) {
5164 		iptr = ISP_READ(isp, BIU2400_ATIO_RSPINP);
5165 		optr = isp->isp_atioodx;
5166 
5167 		while (optr != iptr) {
5168 			uint8_t qe[QENTRY_LEN];
5169 			isphdr_t *hp;
5170 			uint32_t oop;
5171 			void *addr;
5172 
5173 			oop = optr;
5174 			MEMORYBARRIER(isp, SYNC_ATIOQ, oop, QENTRY_LEN, -1);
5175 			addr = ISP_QUEUE_ENTRY(isp->isp_atioq, oop);
5176 			isp_get_hdr(isp, addr, (isphdr_t *)qe);
5177 			hp = (isphdr_t *)qe;
5178 			switch (hp->rqs_entry_type) {
5179 			case RQSTYPE_NOTIFY:
5180 			case RQSTYPE_ATIO:
5181 				(void) isp_target_notify(isp, addr, &oop);
5182 				break;
5183 			default:
5184 				isp_print_qentry(isp, "?ATIOQ entry?", oop, addr);
5185 				break;
5186 			}
5187 			optr = ISP_NXT_QENTRY(oop, RESULT_QUEUE_LEN(isp));
5188 		}
5189 		if (isp->isp_atioodx != optr) {
5190 			ISP_WRITE(isp, BIU2400_ATIO_RSPOUTP, optr);
5191 			isp->isp_atioodx = optr;
5192 		}
5193 	}
5194 #endif
5195 
5196 	/*
5197 	 * You *must* read the Response Queue In Pointer
5198 	 * prior to clearing the RISC interrupt.
5199 	 *
5200 	 * Debounce the 2300 if revision less than 2.
5201 	 */
5202 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
5203 		i = 0;
5204 		do {
5205 			iptr = ISP_READ(isp, isp->isp_respinrp);
5206 			junk = ISP_READ(isp, isp->isp_respinrp);
5207 		} while (junk != iptr && ++i < 1000);
5208 
5209 		if (iptr != junk) {
5210 			isp_prt(isp, ISP_LOGWARN, "Response Queue Out Pointer Unstable (%x, %x)", iptr, junk);
5211 			goto out;
5212 		}
5213 	} else {
5214 		iptr = ISP_READ(isp, isp->isp_respinrp);
5215 	}
5216 
5217 	optr = isp->isp_resodx;
5218 	if (optr == iptr && sema == 0) {
5219 		/*
5220 		 * There are a lot of these- reasons unknown- mostly on
5221 		 * faster Alpha machines.
5222 		 *
5223 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
5224 		 * make sure the old interrupt went away (to avoid 'ringing'
5225 		 * effects), but that didn't stop this from occurring.
5226 		 */
5227 		if (IS_24XX(isp)) {
5228 			junk = 0;
5229 		} else if (IS_23XX(isp)) {
5230 			ISP_DELAY(100);
5231 			iptr = ISP_READ(isp, isp->isp_respinrp);
5232 			junk = ISP_READ(isp, BIU_R2HSTSLO);
5233 		} else {
5234 			junk = ISP_READ(isp, BIU_ISR);
5235 		}
5236 		if (optr == iptr) {
5237 			if (IS_23XX(isp) || IS_24XX(isp)) {
5238 				;
5239 			} else {
5240 				sema = ISP_READ(isp, BIU_SEMA);
5241 				info = ISP_READ(isp, OUTMAILBOX0);
5242 				if ((sema & 0x3) && (info & 0x8000)) {
5243 					goto again;
5244 				}
5245 			}
5246 			isp->isp_intbogus++;
5247 			isp_prt(isp, ISP_LOGDEBUG1, "bogus intr- isr %x (%x) iptr %x optr %x", isr, junk, iptr, optr);
5248 		}
5249 	}
5250 	isp->isp_residx = iptr;
5251 
5252 	while (optr != iptr) {
5253 		uint8_t qe[QENTRY_LEN];
5254 		ispstatusreq_t *sp = (ispstatusreq_t *) qe;
5255 		isphdr_t *hp;
5256 		int buddaboom, scsi_status, completion_status;
5257 		int req_status_flags, req_state_flags;
5258 		uint8_t *snsp, *resp;
5259 		uint32_t rlen, slen, totslen;
5260 		long resid;
5261 		uint16_t oop;
5262 
5263 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
5264 		oop = optr;
5265 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
5266 		nlooked++;
5267  read_again:
5268 		buddaboom = req_status_flags = req_state_flags = 0;
5269 		resid = 0L;
5270 
5271 		/*
5272 		 * Synchronize our view of this response queue entry.
5273 		 */
5274 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN, -1);
5275 		isp_get_hdr(isp, hp, &sp->req_header);
5276 		etype = sp->req_header.rqs_entry_type;
5277 
5278 		if (IS_24XX(isp) && etype == RQSTYPE_RESPONSE) {
5279 			isp24xx_statusreq_t *sp2 = (isp24xx_statusreq_t *)qe;
5280 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)hp, sp2);
5281 			if (isp->isp_dblev & ISP_LOGDEBUG1) {
5282 				isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, sp2);
5283 			}
5284 			scsi_status = sp2->req_scsi_status;
5285 			completion_status = sp2->req_completion_status;
5286 			if ((scsi_status & 0xff) != 0)
5287 				req_state_flags = RQSF_GOT_STATUS;
5288 			else
5289 				req_state_flags = 0;
5290 			resid = sp2->req_resid;
5291 		} else if (etype == RQSTYPE_RESPONSE) {
5292 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
5293 			if (isp->isp_dblev & ISP_LOGDEBUG1) {
5294 				isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, sp);
5295 			}
5296 			scsi_status = sp->req_scsi_status;
5297 			completion_status = sp->req_completion_status;
5298 			req_status_flags = sp->req_status_flags;
5299 			req_state_flags = sp->req_state_flags;
5300 			resid = sp->req_resid;
5301 		} else if (etype == RQSTYPE_RIO1) {
5302 			isp_rio1_t *rio = (isp_rio1_t *) qe;
5303 			isp_get_rio1(isp, (isp_rio1_t *) hp, rio);
5304 			if (isp->isp_dblev & ISP_LOGDEBUG1) {
5305 				isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, rio);
5306 			}
5307 			for (i = 0; i < rio->req_header.rqs_seqno; i++) {
5308 				isp_fastpost_complete(isp, rio->req_handles[i]);
5309 			}
5310 			if (isp->isp_fpcchiwater < rio->req_header.rqs_seqno) {
5311 				isp->isp_fpcchiwater = rio->req_header.rqs_seqno;
5312 			}
5313 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5314 			last_etype = etype;
5315 			continue;
5316 		} else if (etype == RQSTYPE_RIO2) {
5317 			isp_prt(isp, ISP_LOGERR, "dropping RIO2 response");
5318 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5319 			last_etype = etype;
5320 			continue;
5321 		} else if (etype == RQSTYPE_STATUS_CONT) {
5322 			isp_get_cont_response(isp, (ispstatus_cont_t *) hp, (ispstatus_cont_t *) sp);
5323 			if (last_etype == RQSTYPE_RESPONSE && continuations_expected && ndone > 0 && (xs = complist[ndone-1]) != NULL) {
5324 				ispstatus_cont_t *scp = (ispstatus_cont_t *) sp;
5325 				XS_SENSE_APPEND(xs, scp->req_sense_data, sizeof (scp->req_sense_data));
5326 				isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "%d more Status Continuations expected", --continuations_expected);
5327 			} else {
5328 				isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
5329 			}
5330 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5331 			continue;
5332 		} else {
5333 			/*
5334 			 * Somebody reachable via isp_handle_other_response
5335 			 * may have updated the response queue pointers for
5336 			 * us, so we reload our goal index.
5337 			 */
5338 			int r;
5339 			uint32_t tsto = oop;
5340 			r = isp_handle_other_response(isp, etype, hp, &tsto);
5341 			if (r < 0) {
5342 				goto read_again;
5343 			}
5344 			/*
5345 			 * If somebody updated the output pointer, then reset
5346 			 * optr to be one more than the updated amount.
5347 			 */
5348 			while (tsto != oop) {
5349 				optr = ISP_NXT_QENTRY(tsto, RESULT_QUEUE_LEN(isp));
5350 			}
5351 			if (r > 0) {
5352 				ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5353 				last_etype = etype;
5354 				continue;
5355 			}
5356 
5357 			/*
5358 			 * After this point, we'll just look at the header as
5359 			 * we don't know how to deal with the rest of the
5360 			 * response.
5361 			 */
5362 
5363 			/*
5364 			 * It really has to be a bounced request just copied
5365 			 * from the request queue to the response queue. If
5366 			 * not, something bad has happened.
5367 			 */
5368 			if (etype != RQSTYPE_REQUEST) {
5369 				isp_prt(isp, ISP_LOGERR, notresp, etype, oop, optr, nlooked);
5370 				isp_print_bytes(isp, "Request Queue Entry", QENTRY_LEN, sp);
5371 				ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5372 				last_etype = etype;
5373 				continue;
5374 			}
5375 			buddaboom = 1;
5376 			scsi_status = sp->req_scsi_status;
5377 			completion_status = sp->req_completion_status;
5378 			req_status_flags = sp->req_status_flags;
5379 			req_state_flags = sp->req_state_flags;
5380 			resid = sp->req_resid;
5381 		}
5382 
5383 		if (sp->req_header.rqs_flags & RQSFLAG_MASK) {
5384 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
5385 				isp_print_bytes(isp, "unexpected continuation segment", QENTRY_LEN, sp);
5386 				last_etype = etype;
5387 				continue;
5388 			}
5389 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
5390 				isp_prt(isp, ISP_LOG_WARN1, "internal queues full");
5391 				/*
5392 				 * We'll synthesize a QUEUE FULL message below.
5393 				 */
5394 			}
5395 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
5396 				isp_print_bytes(isp, "bad header flag", QENTRY_LEN, sp);
5397 				buddaboom++;
5398 			}
5399 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
5400 				isp_print_bytes(isp, "bad request packet", QENTRY_LEN, sp);
5401 				buddaboom++;
5402 			}
5403 			if (sp->req_header.rqs_flags & RQSFLAG_BADCOUNT) {
5404 				isp_print_bytes(isp, "invalid entry count", QENTRY_LEN, sp);
5405 				buddaboom++;
5406 			}
5407 			if (sp->req_header.rqs_flags & RQSFLAG_BADORDER) {
5408 				isp_print_bytes(isp, "invalid IOCB ordering", QENTRY_LEN, sp);
5409 				last_etype = etype;
5410 				continue;
5411 			}
5412 		}
5413 
5414 		if (!ISP_VALID_HANDLE(isp, sp->req_handle)) {
5415 			isp_prt(isp, ISP_LOGERR, "bad request handle 0x%x (iocb type 0x%x)", sp->req_handle, etype);
5416 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5417 			last_etype = etype;
5418 			continue;
5419 		}
5420 		xs = isp_find_xs(isp, sp->req_handle);
5421 		if (xs == NULL) {
5422 			uint8_t ts = completion_status & 0xff;
5423 			/*
5424 			 * Only whine if this isn't the expected fallout of
5425 			 * aborting the command or resetting the target.
5426 			 */
5427 			if (etype != RQSTYPE_RESPONSE) {
5428 				isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (type 0x%x)", sp->req_handle, etype);
5429 			} else if (ts != RQCS_ABORTED && ts != RQCS_RESET_OCCURRED) {
5430 				isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (status 0x%x)", sp->req_handle, ts);
5431 			}
5432 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5433 			last_etype = etype;
5434 			continue;
5435 		}
5436 		if (req_status_flags & RQSTF_BUS_RESET) {
5437 			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%jx bus was reset",
5438 			    XS_CHANNEL(xs), XS_TGT(xs), (uintmax_t)XS_LUN(xs));
5439 			XS_SETERR(xs, HBA_BUSRESET);
5440 			ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
5441 		}
5442 		if (buddaboom) {
5443 			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%jx buddaboom",
5444 			    XS_CHANNEL(xs), XS_TGT(xs), (uintmax_t)XS_LUN(xs));
5445 			XS_SETERR(xs, HBA_BOTCH);
5446 		}
5447 
5448 		resp = NULL;
5449 		rlen = 0;
5450 		snsp = NULL;
5451 		totslen = slen = 0;
5452 		if (IS_24XX(isp) && (scsi_status & (RQCS_RV|RQCS_SV)) != 0) {
5453 			resp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5454 			rlen = ((isp24xx_statusreq_t *)sp)->req_response_len;
5455 		} else if (IS_FC(isp) && (scsi_status & RQCS_RV) != 0) {
5456 			resp = sp->req_response;
5457 			rlen = sp->req_response_len;
5458 		}
5459 		if (IS_FC(isp) && (scsi_status & RQCS_SV) != 0) {
5460 			/*
5461 			 * Fibre Channel F/W doesn't say we got status
5462 			 * if there's Sense Data instead. I guess they
5463 			 * think it goes w/o saying.
5464 			 */
5465 			req_state_flags |= RQSF_GOT_STATUS|RQSF_GOT_SENSE;
5466 			if (IS_24XX(isp)) {
5467 				snsp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5468 				snsp += rlen;
5469 				totslen = ((isp24xx_statusreq_t *)sp)->req_sense_len;
5470 				slen = (sizeof (((isp24xx_statusreq_t *)sp)->req_rsp_sense)) - rlen;
5471 				if (totslen < slen)
5472 					slen = totslen;
5473 			} else {
5474 				snsp = sp->req_sense_data;
5475 				totslen = sp->req_sense_len;
5476 				slen = sizeof (sp->req_sense_data);
5477 				if (totslen < slen)
5478 					slen = totslen;
5479 			}
5480 		} else if (IS_SCSI(isp) && (req_state_flags & RQSF_GOT_SENSE)) {
5481 			snsp = sp->req_sense_data;
5482 			totslen = sp->req_sense_len;
5483 			slen = sizeof (sp->req_sense_data);
5484 			if (totslen < slen)
5485 				slen = totslen;
5486 		}
5487 		if (req_state_flags & RQSF_GOT_STATUS) {
5488 			*XS_STSP(xs) = scsi_status & 0xff;
5489 		}
5490 
5491 		switch (etype) {
5492 		case RQSTYPE_RESPONSE:
5493 			if (resp && rlen >= 4 && resp[FCP_RSPNS_CODE_OFFSET] != 0) {
5494 				const char *ptr;
5495 				char lb[64];
5496 				const char *rnames[10] = {
5497 				    "Task Management function complete",
5498 				    "FCP_DATA length different than FCP_BURST_LEN",
5499 				    "FCP_CMND fields invalid",
5500 				    "FCP_DATA parameter mismatch with FCP_DATA_RO",
5501 				    "Task Management function rejected",
5502 				    "Task Management function failed",
5503 				    NULL,
5504 				    NULL,
5505 				    "Task Management function succeeded",
5506 				    "Task Management function incorrect logical unit number",
5507 				};
5508 				uint8_t code = resp[FCP_RSPNS_CODE_OFFSET];
5509 				if (code >= 10 || rnames[code] == NULL) {
5510 					ISP_SNPRINTF(lb, sizeof(lb),
5511 					    "Unknown FCP Response Code 0x%x",
5512 					    code);
5513 					ptr = lb;
5514 				} else {
5515 					ptr = rnames[code];
5516 				}
5517 				isp_xs_prt(isp, xs, ISP_LOGWARN,
5518 				    "FCP RESPONSE, LENGTH %u: %s CDB0=0x%02x",
5519 				    rlen, ptr, XS_CDBP(xs)[0] & 0xff);
5520 				if (code != 0 && code != 8)
5521 					XS_SETERR(xs, HBA_BOTCH);
5522 			}
5523 			if (IS_24XX(isp)) {
5524 				isp_parse_status_24xx(isp, (isp24xx_statusreq_t *)sp, xs, &resid);
5525 			} else {
5526 				isp_parse_status(isp, (void *)sp, xs, &resid);
5527 			}
5528 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) && (*XS_STSP(xs) == SCSI_BUSY)) {
5529 				XS_SETERR(xs, HBA_TGTBSY);
5530 			}
5531 			if (IS_SCSI(isp)) {
5532 				XS_SET_RESID(xs, resid);
5533 				/*
5534 				 * A new synchronous rate was negotiated for
5535 				 * this target. Mark state such that we'll go
5536 				 * look up that which has changed later.
5537 				 */
5538 				if (req_status_flags & RQSTF_NEGOTIATION) {
5539 					int t = XS_TGT(xs);
5540 					sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
5541 					sdp->isp_devparam[t].dev_refresh = 1;
5542 					sdp->update = 1;
5543 				}
5544 			} else {
5545 				if (req_status_flags & RQSF_XFER_COMPLETE) {
5546 					XS_SET_RESID(xs, 0);
5547 				} else if (scsi_status & RQCS_RESID) {
5548 					XS_SET_RESID(xs, resid);
5549 				} else {
5550 					XS_SET_RESID(xs, 0);
5551 				}
5552 			}
5553 			if (snsp && slen) {
5554 				if (totslen > slen) {
5555 					continuations_expected += ((totslen - slen + QENTRY_LEN - 5) / (QENTRY_LEN - 4));
5556 					if (ndone > (MAX_REQUESTQ_COMPLETIONS - continuations_expected - 1)) {
5557 						/* we'll lose some stats, but that's a small price to pay */
5558 						for (i = 0; i < ndone; i++) {
5559 							if (complist[i]) {
5560 								isp->isp_rsltccmplt++;
5561 								isp_done(complist[i]);
5562 							}
5563 						}
5564 						ndone = 0;
5565 					}
5566 					isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "Expecting %d more Status Continuations for total sense length of %u",
5567 					    continuations_expected, totslen);
5568 				}
5569 				XS_SAVE_SENSE(xs, snsp, totslen, slen);
5570 			} else if ((req_status_flags & RQSF_GOT_STATUS) && (scsi_status & 0xff) == SCSI_CHECK && IS_FC(isp)) {
5571 				isp_prt(isp, ISP_LOGWARN, "CHECK CONDITION w/o sense data for CDB=0x%x", XS_CDBP(xs)[0] & 0xff);
5572 				isp_print_bytes(isp, "CC with no Sense", QENTRY_LEN, qe);
5573 			}
5574 			isp_prt(isp, ISP_LOGDEBUG2, "asked for %ld got raw resid %ld settled for %ld", (long) XS_XFRLEN(xs), resid, (long) XS_GET_RESID(xs));
5575 			break;
5576 		case RQSTYPE_REQUEST:
5577 		case RQSTYPE_A64:
5578 		case RQSTYPE_T2RQS:
5579 		case RQSTYPE_T3RQS:
5580 		case RQSTYPE_T7RQS:
5581 			if (!IS_24XX(isp) && (sp->req_header.rqs_flags & RQSFLAG_FULL)) {
5582 				/*
5583 				 * Force Queue Full status.
5584 				 */
5585 				*XS_STSP(xs) = SCSI_QFULL;
5586 				XS_SETERR(xs, HBA_NOERROR);
5587 			} else if (XS_NOERR(xs)) {
5588 				isp_prt(isp, ISP_LOG_WARN1,
5589 				    "%d.%d.%jx badness at %s:%u",
5590 				    XS_CHANNEL(xs), XS_TGT(xs),
5591 				    (uintmax_t)XS_LUN(xs),
5592 				    __func__, __LINE__);
5593 				XS_SETERR(xs, HBA_BOTCH);
5594 			}
5595 			XS_SET_RESID(xs, XS_XFRLEN(xs));
5596 			break;
5597 		default:
5598 			isp_print_bytes(isp, "Unhandled Response Type", QENTRY_LEN, qe);
5599 			if (XS_NOERR(xs)) {
5600 				XS_SETERR(xs, HBA_BOTCH);
5601 			}
5602 			break;
5603 		}
5604 
5605 		/*
5606 		 * Free any DMA resources. As a side effect, this may
5607 		 * also do any cache flushing necessary for data coherence.
5608 		 */
5609 		if (XS_XFRLEN(xs)) {
5610 			ISP_DMAFREE(isp, xs, sp->req_handle);
5611 		}
5612 		isp_destroy_handle(isp, sp->req_handle);
5613 
5614 		if (isp->isp_nactive > 0) {
5615 		    isp->isp_nactive--;
5616 		}
5617 		complist[ndone++] = xs;	/* defer completion call until later */
5618 		ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5619 		last_etype = etype;
5620 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
5621 			break;
5622 		}
5623 	}
5624 
5625 	/*
5626 	 * If we looked at any commands, then it's valid to find out
5627 	 * what the outpointer is. It also is a trigger to update the
5628 	 * ISP's notion of what we've seen so far.
5629 	 */
5630 	if (nlooked) {
5631 		ISP_WRITE(isp, isp->isp_respoutrp, optr);
5632 		isp->isp_resodx = optr;
5633 		if (isp->isp_rscchiwater < ndone)
5634 			isp->isp_rscchiwater = ndone;
5635 	}
5636 
5637 out:
5638 
5639 	if (IS_24XX(isp)) {
5640 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
5641 	} else {
5642 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
5643 		ISP_WRITE(isp, BIU_SEMA, 0);
5644 	}
5645 
5646 	for (i = 0; i < ndone; i++) {
5647 		xs = complist[i];
5648 		if (xs) {
5649 			if (((isp->isp_dblev & (ISP_LOGDEBUG1|ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
5650 			    ((isp->isp_dblev & (ISP_LOGDEBUG0|ISP_LOG_CWARN) && ((!XS_NOERR(xs)) || (*XS_STSP(xs) != SCSI_GOOD))))) {
5651 				isp_prt_endcmd(isp, xs);
5652 			}
5653 			isp->isp_rsltccmplt++;
5654 			isp_done(xs);
5655 		}
5656 	}
5657 }
5658 
5659 /*
5660  * Support routines.
5661  */
5662 
5663 void
5664 isp_prt_endcmd(ispsoftc_t *isp, XS_T *xs)
5665 {
5666 	char cdbstr[16 * 5 + 1];
5667 	int i, lim;
5668 
5669 	lim = XS_CDBLEN(xs) > 16? 16 : XS_CDBLEN(xs);
5670 	ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "0x%02x ", XS_CDBP(xs)[0]);
5671 	for (i = 1; i < lim; i++) {
5672 		ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "%s0x%02x ", cdbstr, XS_CDBP(xs)[i]);
5673 	}
5674 	if (XS_SENSE_VALID(xs)) {
5675 		isp_xs_prt(isp, xs, ISP_LOGALL, "FIN dl%d resid %ld CDB=%s SenseLength=%u/%u KEY/ASC/ASCQ=0x%02x/0x%02x/0x%02x",
5676 		    XS_XFRLEN(xs), (long) XS_GET_RESID(xs), cdbstr, XS_CUR_SNSLEN(xs), XS_TOT_SNSLEN(xs), XS_SNSKEY(xs), XS_SNSASC(xs), XS_SNSASCQ(xs));
5677 	} else {
5678 		isp_xs_prt(isp, xs, ISP_LOGALL, "FIN dl%d resid %ld CDB=%s STS 0x%x XS_ERR=0x%x", XS_XFRLEN(xs), (long) XS_GET_RESID(xs), cdbstr, *XS_STSP(xs), XS_ERR(xs));
5679 	}
5680 }
5681 
5682 /*
5683  * Parse an ASYNC mailbox complete
5684  *
5685  * Return non-zero if the event has been acknowledged.
5686  */
5687 static int
5688 isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
5689 {
5690 	int acked = 0;
5691 	uint32_t h1 = 0, h2 = 0;
5692 	uint16_t chan = 0;
5693 
5694 	/*
5695 	 * Pick up the channel, but not if this is a ASYNC_RIO32_2,
5696 	 * where Mailboxes 6/7 have the second handle.
5697 	 */
5698 	if (mbox != ASYNC_RIO32_2) {
5699 		if (IS_DUALBUS(isp)) {
5700 			chan = ISP_READ(isp, OUTMAILBOX6);
5701 		}
5702 	}
5703 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5704 
5705 	switch (mbox) {
5706 	case ASYNC_BUS_RESET:
5707 		ISP_SET_SENDMARKER(isp, chan, 1);
5708 #ifdef	ISP_TARGET_MODE
5709 		if (isp_target_async(isp, chan, mbox)) {
5710 			acked = 1;
5711 		}
5712 #endif
5713 		isp_async(isp, ISPASYNC_BUS_RESET, chan);
5714 		break;
5715 	case ASYNC_SYSTEM_ERROR:
5716 		isp->isp_dead = 1;
5717 		isp->isp_state = ISP_CRASHED;
5718 		/*
5719 		 * Were we waiting for a mailbox command to complete?
5720 		 * If so, it's dead, so wake up the waiter.
5721 		 */
5722 		if (isp->isp_mboxbsy) {
5723 			isp->isp_obits = 1;
5724 			isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5725 			MBOX_NOTIFY_COMPLETE(isp);
5726 		}
5727 		/*
5728 		 * It's up to the handler for isp_async to reinit stuff and
5729 		 * restart the firmware
5730 		 */
5731 		isp_async(isp, ISPASYNC_FW_CRASH);
5732 		acked = 1;
5733 		break;
5734 
5735 	case ASYNC_RQS_XFER_ERR:
5736 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5737 		break;
5738 
5739 	case ASYNC_RSP_XFER_ERR:
5740 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5741 		break;
5742 
5743 	case ASYNC_QWAKEUP:
5744 		/*
5745 		 * We've just been notified that the Queue has woken up.
5746 		 * We don't need to be chatty about this- just unlatch things
5747 		 * and move on.
5748 		 */
5749 		mbox = ISP_READ(isp, isp->isp_rqstoutrp);
5750 		break;
5751 
5752 	case ASYNC_TIMEOUT_RESET:
5753 		isp_prt(isp, ISP_LOGWARN, "timeout initiated SCSI bus reset of chan %d", chan);
5754 		ISP_SET_SENDMARKER(isp, chan, 1);
5755 #ifdef	ISP_TARGET_MODE
5756 		if (isp_target_async(isp, chan, mbox)) {
5757 			acked = 1;
5758 		}
5759 #endif
5760 		break;
5761 
5762 	case ASYNC_DEVICE_RESET:
5763 		isp_prt(isp, ISP_LOGINFO, "device reset on chan %d", chan);
5764 		ISP_SET_SENDMARKER(isp, chan, 1);
5765 #ifdef	ISP_TARGET_MODE
5766 		if (isp_target_async(isp, chan, mbox)) {
5767 			acked = 1;
5768 		}
5769 #endif
5770 		break;
5771 
5772 	case ASYNC_EXTMSG_UNDERRUN:
5773 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
5774 		break;
5775 
5776 	case ASYNC_SCAM_INT:
5777 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
5778 		break;
5779 
5780 	case ASYNC_HUNG_SCSI:
5781 		isp_prt(isp, ISP_LOGERR, "stalled SCSI Bus after DATA Overrun");
5782 		/* XXX: Need to issue SCSI reset at this point */
5783 		break;
5784 
5785 	case ASYNC_KILLED_BUS:
5786 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
5787 		break;
5788 
5789 	case ASYNC_BUS_TRANSIT:
5790 		mbox = ISP_READ(isp, OUTMAILBOX2);
5791 		switch (mbox & SXP_PINS_MODE_MASK) {
5792 		case SXP_PINS_LVD_MODE:
5793 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
5794 			SDPARAM(isp, chan)->isp_diffmode = 0;
5795 			SDPARAM(isp, chan)->isp_ultramode = 0;
5796 			SDPARAM(isp, chan)->isp_lvdmode = 1;
5797 			break;
5798 		case SXP_PINS_HVD_MODE:
5799 			isp_prt(isp, ISP_LOGINFO,
5800 			    "Transition to Differential mode");
5801 			SDPARAM(isp, chan)->isp_diffmode = 1;
5802 			SDPARAM(isp, chan)->isp_ultramode = 0;
5803 			SDPARAM(isp, chan)->isp_lvdmode = 0;
5804 			break;
5805 		case SXP_PINS_SE_MODE:
5806 			isp_prt(isp, ISP_LOGINFO,
5807 			    "Transition to Single Ended mode");
5808 			SDPARAM(isp, chan)->isp_diffmode = 0;
5809 			SDPARAM(isp, chan)->isp_ultramode = 1;
5810 			SDPARAM(isp, chan)->isp_lvdmode = 0;
5811 			break;
5812 		default:
5813 			isp_prt(isp, ISP_LOGWARN,
5814 			    "Transition to Unknown Mode 0x%x", mbox);
5815 			break;
5816 		}
5817 		/*
5818 		 * XXX: Set up to renegotiate again!
5819 		 */
5820 		/* Can only be for a 1080... */
5821 		ISP_SET_SENDMARKER(isp, chan, 1);
5822 		break;
5823 
5824 	case ASYNC_CMD_CMPLT:
5825 	case ASYNC_RIO32_1:
5826 		if (!IS_ULTRA3(isp)) {
5827 			isp_prt(isp, ISP_LOGERR, "unexpected fast posting completion");
5828 			break;
5829 		}
5830 		/* FALLTHROUGH */
5831 		h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5832 		break;
5833 
5834 	case ASYNC_RIO32_2:
5835 		h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5836 		h2 = (ISP_READ(isp, OUTMAILBOX7) << 16) | ISP_READ(isp, OUTMAILBOX6);
5837 		break;
5838 
5839 	case ASYNC_RIO16_5:
5840 	case ASYNC_RIO16_4:
5841 	case ASYNC_RIO16_3:
5842 	case ASYNC_RIO16_2:
5843 	case ASYNC_RIO16_1:
5844 		isp_prt(isp, ISP_LOGERR, "unexpected 16 bit RIO handle");
5845 		break;
5846 	default:
5847 		isp_prt(isp, ISP_LOGWARN, "%s: unhandled async code 0x%x", __func__, mbox);
5848 		break;
5849 	}
5850 
5851 	if (h1 || h2) {
5852 		isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h1);
5853 		isp_fastpost_complete(isp, h1);
5854 		if (h2) {
5855 			isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h2);
5856 			isp_fastpost_complete(isp, h2);
5857 			if (isp->isp_fpcchiwater < 2) {
5858 				isp->isp_fpcchiwater = 2;
5859 			}
5860 		} else {
5861 			if (isp->isp_fpcchiwater < 1) {
5862 				isp->isp_fpcchiwater = 1;
5863 			}
5864 		}
5865 	} else {
5866 		isp->isp_intoasync++;
5867 	}
5868 	return (acked);
5869 }
5870 
5871 #define	GET_24XX_BUS(isp, chan, msg)										\
5872 	if (IS_24XX(isp)) {											\
5873 		chan = ISP_READ(isp, OUTMAILBOX3) & 0xff;							\
5874 		if (chan >= isp->isp_nchan) {									\
5875 			isp_prt(isp, ISP_LOGERR, "bogus channel %u for %s at line %d",	chan, msg, __LINE__);	\
5876 			break;											\
5877 		}												\
5878 	}
5879 
5880 
5881 static int
5882 isp_parse_async_fc(ispsoftc_t *isp, uint16_t mbox)
5883 {
5884 	int acked = 0;
5885 	uint16_t chan;
5886 
5887 	if (IS_DUALBUS(isp)) {
5888 		chan = ISP_READ(isp, OUTMAILBOX6);
5889 	} else {
5890 		chan = 0;
5891 	}
5892 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5893 
5894 	switch (mbox) {
5895 	case ASYNC_SYSTEM_ERROR:
5896 		isp->isp_dead = 1;
5897 		isp->isp_state = ISP_CRASHED;
5898 		FCPARAM(isp, chan)->isp_loopstate = LOOP_NIL;
5899 		FCPARAM(isp, chan)->isp_fwstate = FW_CONFIG_WAIT;
5900 		/*
5901 		 * Were we waiting for a mailbox command to complete?
5902 		 * If so, it's dead, so wake up the waiter.
5903 		 */
5904 		if (isp->isp_mboxbsy) {
5905 			isp->isp_obits = 1;
5906 			isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5907 			MBOX_NOTIFY_COMPLETE(isp);
5908 		}
5909 		/*
5910 		 * It's up to the handler for isp_async to reinit stuff and
5911 		 * restart the firmware
5912 		 */
5913 		isp_async(isp, ISPASYNC_FW_CRASH);
5914 		acked = 1;
5915 		break;
5916 
5917 	case ASYNC_RQS_XFER_ERR:
5918 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5919 		break;
5920 
5921 	case ASYNC_RSP_XFER_ERR:
5922 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5923 		break;
5924 
5925 	case ASYNC_QWAKEUP:
5926 #ifdef	ISP_TARGET_MODE
5927 		if (IS_24XX(isp)) {
5928 			isp_prt(isp, ISP_LOGERR, "ATIO Queue Transfer Error");
5929 			break;
5930 		}
5931 #endif
5932 		isp_prt(isp, ISP_LOGERR, "%s: unexpected ASYNC_QWAKEUP code", __func__);
5933 		break;
5934 
5935 	case ASYNC_CMD_CMPLT:
5936 		isp_fastpost_complete(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1));
5937 		if (isp->isp_fpcchiwater < 1) {
5938 			isp->isp_fpcchiwater = 1;
5939 		}
5940 		break;
5941 
5942 	case ASYNC_RIOZIO_STALL:
5943 		break;
5944 
5945 	case ASYNC_CTIO_DONE:
5946 #ifdef	ISP_TARGET_MODE
5947 		if (isp_target_async(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1), mbox)) {
5948 			acked = 1;
5949 		} else {
5950 			isp->isp_fphccmplt++;
5951 		}
5952 #else
5953 		isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC CTIO done");
5954 #endif
5955 		break;
5956 	case ASYNC_LIP_ERROR:
5957 	case ASYNC_LIP_F8:
5958 	case ASYNC_LIP_OCCURRED:
5959 	case ASYNC_PTPMODE:
5960 		/*
5961 		 * These are broadcast events that have to be sent across
5962 		 * all active channels.
5963 		 */
5964 		for (chan = 0; chan < isp->isp_nchan; chan++) {
5965 			fcparam *fcp = FCPARAM(isp, chan);
5966 			int topo = fcp->isp_topo;
5967 
5968 			if (fcp->role == ISP_ROLE_NONE) {
5969 				continue;
5970 			}
5971 
5972 			fcp->isp_fwstate = FW_CONFIG_WAIT;
5973 			fcp->isp_loopstate = LOOP_LIP_RCVD;
5974 			ISP_SET_SENDMARKER(isp, chan, 1);
5975 			ISP_MARK_PORTDB(isp, chan, 1);
5976 			isp_async(isp, ISPASYNC_LIP, chan);
5977 #ifdef	ISP_TARGET_MODE
5978 			if (isp_target_async(isp, chan, mbox)) {
5979 				acked = 1;
5980 			}
5981 #endif
5982 			/*
5983 			 * We've had problems with data corruption occuring on
5984 			 * commands that complete (with no apparent error) after
5985 			 * we receive a LIP. This has been observed mostly on
5986 			 * Local Loop topologies. To be safe, let's just mark
5987 			 * all active initiator commands as dead.
5988 			 */
5989 			if (topo == TOPO_NL_PORT || topo == TOPO_FL_PORT) {
5990 				int i, j;
5991 				for (i = j = 0; i < isp->isp_maxcmds; i++) {
5992 					XS_T *xs;
5993 					isp_hdl_t *hdp;
5994 
5995 					hdp = &isp->isp_xflist[i];
5996 					if (ISP_H2HT(hdp->handle) != ISP_HANDLE_INITIATOR) {
5997 						continue;
5998 					}
5999 					xs = hdp->cmd;
6000 					if (XS_CHANNEL(xs) != chan) {
6001 						continue;
6002 					}
6003 					j++;
6004 					isp_prt(isp, ISP_LOG_WARN1,
6005 					    "%d.%d.%jx bus reset set at %s:%u",
6006 					    XS_CHANNEL(xs), XS_TGT(xs),
6007 					    (uintmax_t)XS_LUN(xs),
6008 					    __func__, __LINE__);
6009 					XS_SETERR(xs, HBA_BUSRESET);
6010 				}
6011 				if (j) {
6012 					isp_prt(isp, ISP_LOGERR, lipd, chan, j);
6013 				}
6014 			}
6015 		}
6016 		break;
6017 
6018 	case ASYNC_LOOP_UP:
6019 		/*
6020 		 * This is a broadcast event that has to be sent across
6021 		 * all active channels.
6022 		 */
6023 		for (chan = 0; chan < isp->isp_nchan; chan++) {
6024 			fcparam *fcp = FCPARAM(isp, chan);
6025 
6026 			if (fcp->role == ISP_ROLE_NONE) {
6027 				continue;
6028 			}
6029 
6030 			ISP_SET_SENDMARKER(isp, chan, 1);
6031 
6032 			fcp->isp_fwstate = FW_CONFIG_WAIT;
6033 			fcp->isp_loopstate = LOOP_LIP_RCVD;
6034 			ISP_MARK_PORTDB(isp, chan, 1);
6035 			isp_async(isp, ISPASYNC_LOOP_UP, chan);
6036 #ifdef	ISP_TARGET_MODE
6037 			if (isp_target_async(isp, chan, mbox)) {
6038 				acked = 1;
6039 			}
6040 #endif
6041 		}
6042 		break;
6043 
6044 	case ASYNC_LOOP_DOWN:
6045 		/*
6046 		 * This is a broadcast event that has to be sent across
6047 		 * all active channels.
6048 		 */
6049 		for (chan = 0; chan < isp->isp_nchan; chan++) {
6050 			fcparam *fcp = FCPARAM(isp, chan);
6051 
6052 			if (fcp->role == ISP_ROLE_NONE) {
6053 				continue;
6054 			}
6055 
6056 			ISP_SET_SENDMARKER(isp, chan, 1);
6057 			fcp->isp_fwstate = FW_CONFIG_WAIT;
6058 			fcp->isp_loopstate = LOOP_NIL;
6059 			ISP_MARK_PORTDB(isp, chan, 1);
6060 			isp_async(isp, ISPASYNC_LOOP_DOWN, chan);
6061 #ifdef	ISP_TARGET_MODE
6062 			if (isp_target_async(isp, chan, mbox)) {
6063 				acked = 1;
6064 			}
6065 #endif
6066 		}
6067 		break;
6068 
6069 	case ASYNC_LOOP_RESET:
6070 		/*
6071 		 * This is a broadcast event that has to be sent across
6072 		 * all active channels.
6073 		 */
6074 		for (chan = 0; chan < isp->isp_nchan; chan++) {
6075 			fcparam *fcp = FCPARAM(isp, chan);
6076 
6077 			if (fcp->role == ISP_ROLE_NONE) {
6078 				continue;
6079 			}
6080 
6081 			ISP_SET_SENDMARKER(isp, chan, 1);
6082 			fcp->isp_fwstate = FW_CONFIG_WAIT;
6083 			fcp->isp_loopstate = LOOP_NIL;
6084 			ISP_MARK_PORTDB(isp, chan, 1);
6085 			isp_async(isp, ISPASYNC_LOOP_RESET, chan);
6086 #ifdef	ISP_TARGET_MODE
6087 			if (isp_target_async(isp, chan, mbox)) {
6088 				acked = 1;
6089 			}
6090 #endif
6091 		}
6092 		break;
6093 
6094 	case ASYNC_PDB_CHANGED:
6095 	{
6096 		int echan, nphdl, nlstate, reason;
6097 
6098 		if (IS_24XX(isp)) {
6099 			nphdl = ISP_READ(isp, OUTMAILBOX1);
6100 			nlstate = ISP_READ(isp, OUTMAILBOX2);
6101 			reason = ISP_READ(isp, OUTMAILBOX3) >> 8;
6102 			GET_24XX_BUS(isp, chan, "ASYNC_CHANGE_NOTIFY");
6103 			echan = (nphdl == NIL_HANDLE) ?
6104 			    isp->isp_nchan - 1 : chan;
6105 		} else {
6106 			nphdl = NIL_HANDLE;
6107 			nlstate = reason = 0;
6108 			chan = echan = 0;
6109 		}
6110 		for (; chan <= echan; chan++) {
6111 			fcparam *fcp = FCPARAM(isp, chan);
6112 
6113 			if (fcp->role == ISP_ROLE_NONE) {
6114 				continue;
6115 			}
6116 			ISP_SET_SENDMARKER(isp, chan, 1);
6117 			fcp->isp_loopstate = LOOP_PDB_RCVD;
6118 			ISP_MARK_PORTDB(isp, chan, 1);
6119 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan, ISPASYNC_CHANGE_PDB, nphdl, nlstate, reason);
6120 		}
6121 		break;
6122 	}
6123 	case ASYNC_CHANGE_NOTIFY:
6124 	{
6125 		int lochan, hichan;
6126 
6127 		if (ISP_FW_NEWER_THAN(isp, 4, 0, 25) && ISP_CAP_MULTI_ID(isp)) {
6128 			GET_24XX_BUS(isp, chan, "ASYNC_CHANGE_NOTIFY");
6129 			lochan = chan;
6130 			hichan = chan + 1;
6131 		} else {
6132 			lochan = 0;
6133 			hichan = isp->isp_nchan;
6134 		}
6135 		for (chan = lochan; chan < hichan; chan++) {
6136 			fcparam *fcp = FCPARAM(isp, chan);
6137 
6138 			if (fcp->role == ISP_ROLE_NONE) {
6139 				continue;
6140 			}
6141 
6142 			if (fcp->isp_topo == TOPO_F_PORT) {
6143 				fcp->isp_loopstate = LOOP_LSCAN_DONE;
6144 			} else {
6145 				fcp->isp_loopstate = LOOP_PDB_RCVD;
6146 			}
6147 			ISP_MARK_PORTDB(isp, chan, 1);
6148 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan, ISPASYNC_CHANGE_SNS);
6149 		}
6150 		break;
6151 	}
6152 
6153 	case ASYNC_CONNMODE:
6154 		/*
6155 		 * This only applies to 2100 amd 2200 cards
6156 		 */
6157 		if (!IS_2200(isp) && !IS_2100(isp)) {
6158 			isp_prt(isp, ISP_LOGWARN, "bad card for ASYNC_CONNMODE event");
6159 			break;
6160 		}
6161 		chan = 0;
6162 		mbox = ISP_READ(isp, OUTMAILBOX1);
6163 		ISP_MARK_PORTDB(isp, chan, 1);
6164 		switch (mbox) {
6165 		case ISP_CONN_LOOP:
6166 			isp_prt(isp, ISP_LOGINFO,
6167 			    "Point-to-Point -> Loop mode");
6168 			break;
6169 		case ISP_CONN_PTP:
6170 			isp_prt(isp, ISP_LOGINFO,
6171 			    "Loop -> Point-to-Point mode");
6172 			break;
6173 		case ISP_CONN_BADLIP:
6174 			isp_prt(isp, ISP_LOGWARN,
6175 			    "Point-to-Point -> Loop mode (BAD LIP)");
6176 			break;
6177 		case ISP_CONN_FATAL:
6178 			isp->isp_dead = 1;
6179 			isp->isp_state = ISP_CRASHED;
6180 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
6181 			isp_async(isp, ISPASYNC_FW_CRASH);
6182 			return (-1);
6183 		case ISP_CONN_LOOPBACK:
6184 			isp_prt(isp, ISP_LOGWARN,
6185 			    "Looped Back in Point-to-Point mode");
6186 			break;
6187 		default:
6188 			isp_prt(isp, ISP_LOGWARN,
6189 			    "Unknown connection mode (0x%x)", mbox);
6190 			break;
6191 		}
6192 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan, ISPASYNC_CHANGE_OTHER);
6193 		FCPARAM(isp, chan)->sendmarker = 1;
6194 		FCPARAM(isp, chan)->isp_fwstate = FW_CONFIG_WAIT;
6195 		FCPARAM(isp, chan)->isp_loopstate = LOOP_LIP_RCVD;
6196 		break;
6197 
6198 	case ASYNC_RCV_ERR:
6199 		if (IS_24XX(isp)) {
6200 			isp_prt(isp, ISP_LOGWARN, "Receive Error");
6201 		} else {
6202 			isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC_RCV_ERR");
6203 		}
6204 		break;
6205 	case ASYNC_RJT_SENT:	/* same as ASYNC_QFULL_SENT */
6206 		if (IS_24XX(isp)) {
6207 			isp_prt(isp, ISP_LOGTDEBUG0, "LS_RJT sent");
6208 			break;
6209 		} else if (IS_2200(isp)) {
6210 			isp_prt(isp, ISP_LOGTDEBUG0, "QFULL sent");
6211 			break;
6212 		}
6213 		/* FALLTHROUGH */
6214 	default:
6215 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
6216 		break;
6217 	}
6218 	if (mbox != ASYNC_CTIO_DONE && mbox != ASYNC_CMD_CMPLT) {
6219 		isp->isp_intoasync++;
6220 	}
6221 	return (acked);
6222 }
6223 
6224 /*
6225  * Handle other response entries. A pointer to the request queue output
6226  * index is here in case we want to eat several entries at once, although
6227  * this is not used currently.
6228  */
6229 
6230 static int
6231 isp_handle_other_response(ispsoftc_t *isp, int type, isphdr_t *hp, uint32_t *optrp)
6232 {
6233 	switch (type) {
6234 	case RQSTYPE_STATUS_CONT:
6235 		isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
6236 		return (1);
6237 	case RQSTYPE_MARKER:
6238 		isp_prt(isp, ISP_LOG_WARN1, "Marker Response");
6239 		return (1);
6240 	case RQSTYPE_ATIO:
6241 	case RQSTYPE_CTIO:
6242 	case RQSTYPE_ENABLE_LUN:
6243 	case RQSTYPE_MODIFY_LUN:
6244 	case RQSTYPE_NOTIFY:
6245 	case RQSTYPE_NOTIFY_ACK:
6246 	case RQSTYPE_CTIO1:
6247 	case RQSTYPE_ATIO2:
6248 	case RQSTYPE_CTIO2:
6249 	case RQSTYPE_CTIO3:
6250 	case RQSTYPE_CTIO7:
6251 	case RQSTYPE_ABTS_RCVD:
6252 	case RQSTYPE_ABTS_RSP:
6253 		isp->isp_rsltccmplt++;	/* count as a response completion */
6254 #ifdef	ISP_TARGET_MODE
6255 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
6256 			return (1);
6257 		}
6258 #endif
6259 		/* FALLTHROUGH */
6260 	case RQSTYPE_RPT_ID_ACQ:
6261 		if (IS_24XX(isp)) {
6262 			isp_ridacq_t rid;
6263 			isp_get_ridacq(isp, (isp_ridacq_t *)hp, &rid);
6264 			if (rid.ridacq_format == 0) {
6265 			}
6266 			return (1);
6267 		}
6268 		/* FALLTHROUGH */
6269 	case RQSTYPE_REQUEST:
6270 	default:
6271 		ISP_DELAY(100);
6272 		if (type != isp_get_response_type(isp, hp)) {
6273 			/*
6274 			 * This is questionable- we're just papering over
6275 			 * something we've seen on SMP linux in target
6276 			 * mode- we don't really know what's happening
6277 			 * here that causes us to think we've gotten
6278 			 * an entry, but that either the entry isn't
6279 			 * filled out yet or our CPU read data is stale.
6280 			 */
6281 			isp_prt(isp, ISP_LOGINFO,
6282 				"unstable type in response queue");
6283 			return (-1);
6284 		}
6285 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
6286 		    isp_get_response_type(isp, hp));
6287 		return (0);
6288 	}
6289 }
6290 
6291 static void
6292 isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs, long *rp)
6293 {
6294 	switch (sp->req_completion_status & 0xff) {
6295 	case RQCS_COMPLETE:
6296 		if (XS_NOERR(xs)) {
6297 			XS_SETERR(xs, HBA_NOERROR);
6298 		}
6299 		return;
6300 
6301 	case RQCS_INCOMPLETE:
6302 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
6303 			isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Selection Timeout @ %s:%d", __func__, __LINE__);
6304 			if (XS_NOERR(xs)) {
6305 				XS_SETERR(xs, HBA_SELTIMEOUT);
6306 				*rp = XS_XFRLEN(xs);
6307 			}
6308 			return;
6309 		}
6310 		isp_xs_prt(isp, xs, ISP_LOGERR, "Command Incomplete, state 0x%x", sp->req_state_flags);
6311 		break;
6312 
6313 	case RQCS_DMA_ERROR:
6314 		isp_xs_prt(isp, xs, ISP_LOGERR, "DMA Error");
6315 		*rp = XS_XFRLEN(xs);
6316 		break;
6317 
6318 	case RQCS_TRANSPORT_ERROR:
6319 	{
6320 		char buf[172];
6321 		ISP_SNPRINTF(buf, sizeof (buf), "states=>");
6322 		if (sp->req_state_flags & RQSF_GOT_BUS) {
6323 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
6324 		}
6325 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
6326 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
6327 		}
6328 		if (sp->req_state_flags & RQSF_SENT_CDB) {
6329 			ISP_SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
6330 		}
6331 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
6332 			ISP_SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
6333 		}
6334 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
6335 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
6336 		}
6337 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
6338 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
6339 		}
6340 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
6341 			ISP_SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
6342 		}
6343 		ISP_SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
6344 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
6345 			ISP_SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
6346 		}
6347 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
6348 			ISP_SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
6349 		}
6350 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
6351 			ISP_SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
6352 		}
6353 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
6354 			ISP_SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
6355 		}
6356 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
6357 			ISP_SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
6358 		}
6359 		if (sp->req_status_flags & RQSTF_ABORTED) {
6360 			ISP_SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
6361 		}
6362 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
6363 			ISP_SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
6364 		}
6365 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
6366 			ISP_SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
6367 		}
6368 		isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error: %s", buf);
6369 		*rp = XS_XFRLEN(xs);
6370 		break;
6371 	}
6372 	case RQCS_RESET_OCCURRED:
6373 	{
6374 		int chan;
6375 		isp_xs_prt(isp, xs, ISP_LOGWARN, "Bus Reset destroyed command");
6376 		for (chan = 0; chan < isp->isp_nchan; chan++) {
6377 			FCPARAM(isp, chan)->sendmarker = 1;
6378 		}
6379 		if (XS_NOERR(xs)) {
6380 			XS_SETERR(xs, HBA_BUSRESET);
6381 		}
6382 		*rp = XS_XFRLEN(xs);
6383 		return;
6384 	}
6385 	case RQCS_ABORTED:
6386 		isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6387 		ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
6388 		if (XS_NOERR(xs)) {
6389 			XS_SETERR(xs, HBA_ABORTED);
6390 		}
6391 		return;
6392 
6393 	case RQCS_TIMEOUT:
6394 		isp_xs_prt(isp, xs, ISP_LOGWARN, "Command timed out");
6395 		/*
6396 	 	 * XXX: Check to see if we logged out of the device.
6397 		 */
6398 		if (XS_NOERR(xs)) {
6399 			XS_SETERR(xs, HBA_CMDTIMEOUT);
6400 		}
6401 		return;
6402 
6403 	case RQCS_DATA_OVERRUN:
6404 		XS_SET_RESID(xs, sp->req_resid);
6405 		isp_xs_prt(isp, xs, ISP_LOGERR, "data overrun (%ld)", (long) XS_GET_RESID(xs));
6406 		if (XS_NOERR(xs)) {
6407 			XS_SETERR(xs, HBA_DATAOVR);
6408 		}
6409 		return;
6410 
6411 	case RQCS_COMMAND_OVERRUN:
6412 		isp_xs_prt(isp, xs, ISP_LOGERR, "command overrun");
6413 		break;
6414 
6415 	case RQCS_STATUS_OVERRUN:
6416 		isp_xs_prt(isp, xs, ISP_LOGERR, "status overrun");
6417 		break;
6418 
6419 	case RQCS_BAD_MESSAGE:
6420 		isp_xs_prt(isp, xs, ISP_LOGERR, "msg not COMMAND COMPLETE after status");
6421 		break;
6422 
6423 	case RQCS_NO_MESSAGE_OUT:
6424 		isp_xs_prt(isp, xs, ISP_LOGERR, "No MESSAGE OUT phase after selection");
6425 		break;
6426 
6427 	case RQCS_EXT_ID_FAILED:
6428 		isp_xs_prt(isp, xs, ISP_LOGERR, "EXTENDED IDENTIFY failed");
6429 		break;
6430 
6431 	case RQCS_IDE_MSG_FAILED:
6432 		isp_xs_prt(isp, xs, ISP_LOGERR, "INITIATOR DETECTED ERROR rejected");
6433 		break;
6434 
6435 	case RQCS_ABORT_MSG_FAILED:
6436 		isp_xs_prt(isp, xs, ISP_LOGERR, "ABORT OPERATION rejected");
6437 		break;
6438 
6439 	case RQCS_REJECT_MSG_FAILED:
6440 		isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE REJECT rejected");
6441 		break;
6442 
6443 	case RQCS_NOP_MSG_FAILED:
6444 		isp_xs_prt(isp, xs, ISP_LOGERR, "NOP rejected");
6445 		break;
6446 
6447 	case RQCS_PARITY_ERROR_MSG_FAILED:
6448 		isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE PARITY ERROR rejected");
6449 		break;
6450 
6451 	case RQCS_DEVICE_RESET_MSG_FAILED:
6452 		isp_xs_prt(isp, xs, ISP_LOGWARN, "BUS DEVICE RESET rejected");
6453 		break;
6454 
6455 	case RQCS_ID_MSG_FAILED:
6456 		isp_xs_prt(isp, xs, ISP_LOGERR, "IDENTIFY rejected");
6457 		break;
6458 
6459 	case RQCS_UNEXP_BUS_FREE:
6460 		isp_xs_prt(isp, xs, ISP_LOGERR, "Unexpected Bus Free");
6461 		break;
6462 
6463 	case RQCS_DATA_UNDERRUN:
6464 	{
6465 		if (IS_FC(isp)) {
6466 			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6467 			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
6468 				isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6469 				if (XS_NOERR(xs)) {
6470 					XS_SETERR(xs, HBA_BOTCH);
6471 				}
6472 				return;
6473 			}
6474 		}
6475 		XS_SET_RESID(xs, sp->req_resid);
6476 		if (XS_NOERR(xs)) {
6477 			XS_SETERR(xs, HBA_NOERROR);
6478 		}
6479 		return;
6480 	}
6481 
6482 	case RQCS_XACT_ERR1:
6483 		isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued transaction with disconnect not set");
6484 		break;
6485 
6486 	case RQCS_XACT_ERR2:
6487 		isp_xs_prt(isp, xs, ISP_LOGERR,
6488 		    "HBA attempted queued transaction to target routine %jx",
6489 		    (uintmax_t)XS_LUN(xs));
6490 		break;
6491 
6492 	case RQCS_XACT_ERR3:
6493 		isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued cmd when queueing disabled");
6494 		break;
6495 
6496 	case RQCS_BAD_ENTRY:
6497 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
6498 		break;
6499 
6500 	case RQCS_QUEUE_FULL:
6501 		isp_xs_prt(isp, xs, ISP_LOG_WARN1, "internal queues full status 0x%x", *XS_STSP(xs));
6502 
6503 		/*
6504 		 * If QFULL or some other status byte is set, then this
6505 		 * isn't an error, per se.
6506 		 *
6507 		 * Unfortunately, some QLogic f/w writers have, in
6508 		 * some cases, ommitted to *set* status to QFULL.
6509 		 */
6510 #if	0
6511 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
6512 			XS_SETERR(xs, HBA_NOERROR);
6513 			return;
6514 		}
6515 
6516 #endif
6517 		*XS_STSP(xs) = SCSI_QFULL;
6518 		XS_SETERR(xs, HBA_NOERROR);
6519 		return;
6520 
6521 	case RQCS_PHASE_SKIPPED:
6522 		isp_xs_prt(isp, xs, ISP_LOGERR, "SCSI phase skipped");
6523 		break;
6524 
6525 	case RQCS_ARQS_FAILED:
6526 		isp_xs_prt(isp, xs, ISP_LOGERR, "Auto Request Sense Failed");
6527 		if (XS_NOERR(xs)) {
6528 			XS_SETERR(xs, HBA_ARQFAIL);
6529 		}
6530 		return;
6531 
6532 	case RQCS_WIDE_FAILED:
6533 		isp_xs_prt(isp, xs, ISP_LOGERR, "Wide Negotiation Failed");
6534 		if (IS_SCSI(isp)) {
6535 			sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6536 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
6537 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6538 			sdp->update = 1;
6539 		}
6540 		if (XS_NOERR(xs)) {
6541 			XS_SETERR(xs, HBA_NOERROR);
6542 		}
6543 		return;
6544 
6545 	case RQCS_SYNCXFER_FAILED:
6546 		isp_xs_prt(isp, xs, ISP_LOGERR, "SDTR Message Failed");
6547 		if (IS_SCSI(isp)) {
6548 			sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6549 			sdp += XS_CHANNEL(xs);
6550 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
6551 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6552 			sdp->update = 1;
6553 		}
6554 		break;
6555 
6556 	case RQCS_LVD_BUSERR:
6557 		isp_xs_prt(isp, xs, ISP_LOGERR, "Bad LVD condition");
6558 		break;
6559 
6560 	case RQCS_PORT_UNAVAILABLE:
6561 		/*
6562 		 * No such port on the loop. Moral equivalent of SELTIMEO
6563 		 */
6564 	case RQCS_PORT_LOGGED_OUT:
6565 	{
6566 		const char *reason;
6567 		uint8_t sts = sp->req_completion_status & 0xff;
6568 
6569 		/*
6570 		 * It was there (maybe)- treat as a selection timeout.
6571 		 */
6572 		if (sts == RQCS_PORT_UNAVAILABLE) {
6573 			reason = "unavailable";
6574 		} else {
6575 			reason = "logout";
6576 		}
6577 
6578 		isp_prt(isp, ISP_LOGINFO, "port %s for target %d", reason, XS_TGT(xs));
6579 
6580 		/*
6581 		 * If we're on a local loop, force a LIP (which is overkill)
6582 		 * to force a re-login of this unit. If we're on fabric,
6583 		 * then we'll have to log in again as a matter of course.
6584 		 */
6585 		if (FCPARAM(isp, 0)->isp_topo == TOPO_NL_PORT ||
6586 		    FCPARAM(isp, 0)->isp_topo == TOPO_FL_PORT) {
6587 			mbreg_t mbs;
6588 			MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
6589 			if (ISP_CAP_2KLOGIN(isp)) {
6590 				mbs.ibits = (1 << 10);
6591 			}
6592 			isp_mboxcmd_qnw(isp, &mbs, 1);
6593 		}
6594 		if (XS_NOERR(xs)) {
6595 			XS_SETERR(xs, HBA_SELTIMEOUT);
6596 		}
6597 		return;
6598 	}
6599 	case RQCS_PORT_CHANGED:
6600 		isp_prt(isp, ISP_LOGWARN, "port changed for target %d", XS_TGT(xs));
6601 		if (XS_NOERR(xs)) {
6602 			XS_SETERR(xs, HBA_SELTIMEOUT);
6603 		}
6604 		return;
6605 
6606 	case RQCS_PORT_BUSY:
6607 		isp_prt(isp, ISP_LOGWARN, "port busy for target %d", XS_TGT(xs));
6608 		if (XS_NOERR(xs)) {
6609 			XS_SETERR(xs, HBA_TGTBSY);
6610 		}
6611 		return;
6612 
6613 	default:
6614 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x", sp->req_completion_status);
6615 		break;
6616 	}
6617 	if (XS_NOERR(xs)) {
6618 		XS_SETERR(xs, HBA_BOTCH);
6619 	}
6620 }
6621 
6622 static void
6623 isp_parse_status_24xx(ispsoftc_t *isp, isp24xx_statusreq_t *sp, XS_T *xs, long *rp)
6624 {
6625 	int ru_marked, sv_marked;
6626 	int chan = XS_CHANNEL(xs);
6627 
6628 	switch (sp->req_completion_status) {
6629 	case RQCS_COMPLETE:
6630 		if (XS_NOERR(xs)) {
6631 			XS_SETERR(xs, HBA_NOERROR);
6632 		}
6633 		return;
6634 
6635 	case RQCS_DMA_ERROR:
6636 		isp_xs_prt(isp, xs, ISP_LOGERR, "DMA error");
6637 		break;
6638 
6639 	case RQCS_TRANSPORT_ERROR:
6640 		isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error");
6641 		break;
6642 
6643 	case RQCS_RESET_OCCURRED:
6644 		isp_xs_prt(isp, xs, ISP_LOGWARN, "reset destroyed command");
6645 		FCPARAM(isp, chan)->sendmarker = 1;
6646 		if (XS_NOERR(xs)) {
6647 			XS_SETERR(xs, HBA_BUSRESET);
6648 		}
6649 		return;
6650 
6651 	case RQCS_ABORTED:
6652 		isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6653 		FCPARAM(isp, chan)->sendmarker = 1;
6654 		if (XS_NOERR(xs)) {
6655 			XS_SETERR(xs, HBA_ABORTED);
6656 		}
6657 		return;
6658 
6659 	case RQCS_TIMEOUT:
6660 		isp_xs_prt(isp, xs, ISP_LOGWARN, "Command Timed Out");
6661 		if (XS_NOERR(xs)) {
6662 			XS_SETERR(xs, HBA_CMDTIMEOUT);
6663 		}
6664 		return;
6665 
6666 	case RQCS_DATA_OVERRUN:
6667 		XS_SET_RESID(xs, sp->req_resid);
6668 		isp_xs_prt(isp, xs, ISP_LOGERR, "Data Overrun");
6669 		if (XS_NOERR(xs)) {
6670 			XS_SETERR(xs, HBA_DATAOVR);
6671 		}
6672 		return;
6673 
6674 	case RQCS_24XX_DRE:	/* data reassembly error */
6675 		isp_prt(isp, ISP_LOGERR, "Chan %d data reassembly error for target %d", chan, XS_TGT(xs));
6676 		if (XS_NOERR(xs)) {
6677 			XS_SETERR(xs, HBA_ABORTED);
6678 		}
6679 		*rp = XS_XFRLEN(xs);
6680 		return;
6681 
6682 	case RQCS_24XX_TABORT:	/* aborted by target */
6683 		isp_prt(isp, ISP_LOGERR, "Chan %d target %d sent ABTS", chan, XS_TGT(xs));
6684 		if (XS_NOERR(xs)) {
6685 			XS_SETERR(xs, HBA_ABORTED);
6686 		}
6687 		return;
6688 
6689 	case RQCS_DATA_UNDERRUN:
6690 		ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6691 		/*
6692 		 * We can get an underrun w/o things being marked
6693 		 * if we got a non-zero status.
6694 		 */
6695 		sv_marked = (sp->req_scsi_status & (RQCS_SV|RQCS_RV)) != 0;
6696 		if ((ru_marked == 0 && sv_marked == 0) ||
6697 		    (sp->req_resid > XS_XFRLEN(xs))) {
6698 			isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6699 			if (XS_NOERR(xs)) {
6700 				XS_SETERR(xs, HBA_BOTCH);
6701 			}
6702 			return;
6703 		}
6704 		XS_SET_RESID(xs, sp->req_resid);
6705 		isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Data Underrun (%d) for command 0x%x", sp->req_resid, XS_CDBP(xs)[0] & 0xff);
6706 		if (XS_NOERR(xs)) {
6707 			XS_SETERR(xs, HBA_NOERROR);
6708 		}
6709 		return;
6710 
6711 	case RQCS_PORT_UNAVAILABLE:
6712 		/*
6713 		 * No such port on the loop. Moral equivalent of SELTIMEO
6714 		 */
6715 	case RQCS_PORT_LOGGED_OUT:
6716 	{
6717 		const char *reason;
6718 		uint8_t sts = sp->req_completion_status & 0xff;
6719 
6720 		/*
6721 		 * It was there (maybe)- treat as a selection timeout.
6722 		 */
6723 		if (sts == RQCS_PORT_UNAVAILABLE) {
6724 			reason = "unavailable";
6725 		} else {
6726 			reason = "logout";
6727 		}
6728 
6729 		isp_prt(isp, ISP_LOGINFO, "Chan %d port %s for target %d",
6730 		    chan, reason, XS_TGT(xs));
6731 
6732 		/*
6733 		 * There is no MBOX_INIT_LIP for the 24XX.
6734 		 */
6735 		if (XS_NOERR(xs)) {
6736 			XS_SETERR(xs, HBA_SELTIMEOUT);
6737 		}
6738 		return;
6739 	}
6740 	case RQCS_PORT_CHANGED:
6741 		isp_prt(isp, ISP_LOGWARN, "port changed for target %d chan %d", XS_TGT(xs), chan);
6742 		if (XS_NOERR(xs)) {
6743 			XS_SETERR(xs, HBA_SELTIMEOUT);
6744 		}
6745 		return;
6746 
6747 
6748 	case RQCS_24XX_ENOMEM:	/* f/w resource unavailable */
6749 		isp_prt(isp, ISP_LOGWARN, "f/w resource unavailable for target %d chan %d", XS_TGT(xs), chan);
6750 		if (XS_NOERR(xs)) {
6751 			*XS_STSP(xs) = SCSI_BUSY;
6752 			XS_SETERR(xs, HBA_TGTBSY);
6753 		}
6754 		return;
6755 
6756 	case RQCS_24XX_TMO:	/* task management overrun */
6757 		isp_prt(isp, ISP_LOGWARN, "command for target %d overlapped task management for chan %d", XS_TGT(xs), chan);
6758 		if (XS_NOERR(xs)) {
6759 			*XS_STSP(xs) = SCSI_BUSY;
6760 			XS_SETERR(xs, HBA_TGTBSY);
6761 		}
6762 		return;
6763 
6764 	default:
6765 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x on chan %d", sp->req_completion_status, chan);
6766 		break;
6767 	}
6768 	if (XS_NOERR(xs)) {
6769 		XS_SETERR(xs, HBA_BOTCH);
6770 	}
6771 }
6772 
6773 static void
6774 isp_fastpost_complete(ispsoftc_t *isp, uint32_t fph)
6775 {
6776 	XS_T *xs;
6777 
6778 	if (fph == 0) {
6779 		return;
6780 	}
6781 	xs = isp_find_xs(isp, fph);
6782 	if (xs == NULL) {
6783 		isp_prt(isp, ISP_LOGWARN,
6784 		    "Command for fast post handle 0x%x not found", fph);
6785 		return;
6786 	}
6787 	isp_destroy_handle(isp, fph);
6788 
6789 	/*
6790 	 * Since we don't have a result queue entry item,
6791 	 * we must believe that SCSI status is zero and
6792 	 * that all data transferred.
6793 	 */
6794 	XS_SET_RESID(xs, 0);
6795 	*XS_STSP(xs) = SCSI_GOOD;
6796 	if (XS_XFRLEN(xs)) {
6797 		ISP_DMAFREE(isp, xs, fph);
6798 	}
6799 	if (isp->isp_nactive) {
6800 		isp->isp_nactive--;
6801 	}
6802 	isp->isp_fphccmplt++;
6803 	isp_done(xs);
6804 }
6805 
6806 static int
6807 isp_mbox_continue(ispsoftc_t *isp)
6808 {
6809 	mbreg_t mbs;
6810 	uint16_t *ptr;
6811 	uint32_t offset;
6812 
6813 	switch (isp->isp_lastmbxcmd) {
6814 	case MBOX_WRITE_RAM_WORD:
6815 	case MBOX_READ_RAM_WORD:
6816 	case MBOX_WRITE_RAM_WORD_EXTENDED:
6817 	case MBOX_READ_RAM_WORD_EXTENDED:
6818 		break;
6819 	default:
6820 		return (1);
6821 	}
6822 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
6823 		isp->isp_mbxwrk0 = 0;
6824 		return (-1);
6825 	}
6826 
6827 	/*
6828 	 * Clear the previous interrupt.
6829 	 */
6830 	if (IS_24XX(isp)) {
6831 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
6832 	} else {
6833 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
6834 		ISP_WRITE(isp, BIU_SEMA, 0);
6835 	}
6836 
6837 	/*
6838 	 * Continue with next word.
6839 	 */
6840 	ISP_MEMZERO(&mbs, sizeof (mbs));
6841 	ptr = isp->isp_mbxworkp;
6842 	switch (isp->isp_lastmbxcmd) {
6843 	case MBOX_WRITE_RAM_WORD:
6844 		mbs.param[1] = isp->isp_mbxwrk1++;
6845 		mbs.param[2] = *ptr++;
6846 		break;
6847 	case MBOX_READ_RAM_WORD:
6848 		*ptr++ = isp->isp_mboxtmp[2];
6849 		mbs.param[1] = isp->isp_mbxwrk1++;
6850 		break;
6851 	case MBOX_WRITE_RAM_WORD_EXTENDED:
6852 		if (IS_24XX(isp)) {
6853 			uint32_t *lptr = (uint32_t *)ptr;
6854 			mbs.param[2] = lptr[0];
6855 			mbs.param[3] = lptr[0] >> 16;
6856 			lptr++;
6857 			ptr = (uint16_t *)lptr;
6858 		} else {
6859 			mbs.param[2] = *ptr++;
6860 		}
6861 		offset = isp->isp_mbxwrk1;
6862 		offset |= isp->isp_mbxwrk8 << 16;
6863 		mbs.param[1] = offset;
6864 		mbs.param[8] = offset >> 16;
6865 		offset++;
6866 		isp->isp_mbxwrk1 = offset;
6867 		isp->isp_mbxwrk8 = offset >> 16;
6868 		break;
6869 	case MBOX_READ_RAM_WORD_EXTENDED:
6870 		if (IS_24XX(isp)) {
6871 			uint32_t *lptr = (uint32_t *)ptr;
6872 			uint32_t val = isp->isp_mboxtmp[2];
6873 			val |= (isp->isp_mboxtmp[3]) << 16;
6874 			*lptr++ = val;
6875 			ptr = (uint16_t *)lptr;
6876 		} else {
6877 			*ptr++ = isp->isp_mboxtmp[2];
6878 		}
6879 		offset = isp->isp_mbxwrk1;
6880 		offset |= isp->isp_mbxwrk8 << 16;
6881 		mbs.param[1] = offset;
6882 		mbs.param[8] = offset >> 16;
6883 		offset++;
6884 		isp->isp_mbxwrk1 = offset;
6885 		isp->isp_mbxwrk8 = offset >> 16;
6886 		break;
6887 	}
6888 	isp->isp_mbxworkp = ptr;
6889 	isp->isp_mbxwrk0--;
6890 	mbs.param[0] = isp->isp_lastmbxcmd;
6891 	mbs.logval = MBLOGALL;
6892 	isp_mboxcmd_qnw(isp, &mbs, 0);
6893 	return (0);
6894 }
6895 
6896 #define	ISP_SCSI_IBITS(op)		(mbpscsi[((op)<<1)])
6897 #define	ISP_SCSI_OBITS(op)		(mbpscsi[((op)<<1) + 1])
6898 #define	ISP_SCSI_OPMAP(in, out)		in, out
6899 static const uint8_t mbpscsi[] = {
6900 	ISP_SCSI_OPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
6901 	ISP_SCSI_OPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
6902 	ISP_SCSI_OPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
6903 	ISP_SCSI_OPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
6904 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
6905 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
6906 	ISP_SCSI_OPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
6907 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
6908 	ISP_SCSI_OPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
6909 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x09: */
6910 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0a: */
6911 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0b: */
6912 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0c: */
6913 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0d: */
6914 	ISP_SCSI_OPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
6915 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0f: */
6916 	ISP_SCSI_OPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
6917 	ISP_SCSI_OPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
6918 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
6919 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
6920 	ISP_SCSI_OPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
6921 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
6922 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
6923 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
6924 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
6925 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
6926 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
6927 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
6928 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
6929 	ISP_SCSI_OPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
6930 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x1e: */
6931 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
6932 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
6933 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
6934 	ISP_SCSI_OPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
6935 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
6936 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
6937 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
6938 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
6939 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
6940 	ISP_SCSI_OPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
6941 	ISP_SCSI_OPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
6942 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
6943 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2b: */
6944 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2c: */
6945 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2d: */
6946 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2e: */
6947 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2f: */
6948 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
6949 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
6950 	ISP_SCSI_OPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
6951 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
6952 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
6953 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
6954 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
6955 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
6956 	ISP_SCSI_OPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
6957 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
6958 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
6959 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3b: */
6960 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3c: */
6961 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3d: */
6962 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3e: */
6963 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3f: */
6964 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
6965 	ISP_SCSI_OPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
6966 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
6967 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x43: */
6968 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x44: */
6969 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
6970 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
6971 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x47: */
6972 	ISP_SCSI_OPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
6973 	ISP_SCSI_OPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
6974 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
6975 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
6976 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4c: */
6977 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4d: */
6978 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4e: */
6979 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4f: */
6980 	ISP_SCSI_OPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
6981 	ISP_SCSI_OPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
6982 	ISP_SCSI_OPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
6983 	ISP_SCSI_OPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
6984 	ISP_SCSI_OPMAP(0xcf, 0x01),	/* 0x54: EXECUCUTE COMMAND IOCB A64 */
6985 	ISP_SCSI_OPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
6986 	ISP_SCSI_OPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
6987 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x57: */
6988 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x58: */
6989 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x59: */
6990 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
6991 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
6992 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
6993 	ISP_SCSI_OPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
6994 };
6995 #define	MAX_SCSI_OPCODE	0x5d
6996 
6997 static const char *scsi_mbcmd_names[] = {
6998 	"NO-OP",
6999 	"LOAD RAM",
7000 	"EXEC FIRMWARE",
7001 	"DUMP RAM",
7002 	"WRITE RAM WORD",
7003 	"READ RAM WORD",
7004 	"MAILBOX REG TEST",
7005 	"VERIFY CHECKSUM",
7006 	"ABOUT FIRMWARE",
7007 	NULL,
7008 	NULL,
7009 	NULL,
7010 	NULL,
7011 	NULL,
7012 	"CHECK FIRMWARE",
7013 	NULL,
7014 	"INIT REQUEST QUEUE",
7015 	"INIT RESULT QUEUE",
7016 	"EXECUTE IOCB",
7017 	"WAKE UP",
7018 	"STOP FIRMWARE",
7019 	"ABORT",
7020 	"ABORT DEVICE",
7021 	"ABORT TARGET",
7022 	"BUS RESET",
7023 	"STOP QUEUE",
7024 	"START QUEUE",
7025 	"SINGLE STEP QUEUE",
7026 	"ABORT QUEUE",
7027 	"GET DEV QUEUE STATUS",
7028 	NULL,
7029 	"GET FIRMWARE STATUS",
7030 	"GET INIT SCSI ID",
7031 	"GET SELECT TIMEOUT",
7032 	"GET RETRY COUNT",
7033 	"GET TAG AGE LIMIT",
7034 	"GET CLOCK RATE",
7035 	"GET ACT NEG STATE",
7036 	"GET ASYNC DATA SETUP TIME",
7037 	"GET PCI PARAMS",
7038 	"GET TARGET PARAMS",
7039 	"GET DEV QUEUE PARAMS",
7040 	"GET RESET DELAY PARAMS",
7041 	NULL,
7042 	NULL,
7043 	NULL,
7044 	NULL,
7045 	NULL,
7046 	"SET INIT SCSI ID",
7047 	"SET SELECT TIMEOUT",
7048 	"SET RETRY COUNT",
7049 	"SET TAG AGE LIMIT",
7050 	"SET CLOCK RATE",
7051 	"SET ACT NEG STATE",
7052 	"SET ASYNC DATA SETUP TIME",
7053 	"SET PCI CONTROL PARAMS",
7054 	"SET TARGET PARAMS",
7055 	"SET DEV QUEUE PARAMS",
7056 	"SET RESET DELAY PARAMS",
7057 	NULL,
7058 	NULL,
7059 	NULL,
7060 	NULL,
7061 	NULL,
7062 	"RETURN BIOS BLOCK ADDR",
7063 	"WRITE FOUR RAM WORDS",
7064 	"EXEC BIOS IOCB",
7065 	NULL,
7066 	NULL,
7067 	"SET SYSTEM PARAMETER",
7068 	"GET SYSTEM PARAMETER",
7069 	NULL,
7070 	"GET SCAM CONFIGURATION",
7071 	"SET SCAM CONFIGURATION",
7072 	"SET FIRMWARE FEATURES",
7073 	"GET FIRMWARE FEATURES",
7074 	NULL,
7075 	NULL,
7076 	NULL,
7077 	NULL,
7078 	"LOAD RAM A64",
7079 	"DUMP RAM A64",
7080 	"INITIALIZE REQUEST QUEUE A64",
7081 	"INITIALIZE RESPONSE QUEUE A64",
7082 	"EXECUTE IOCB A64",
7083 	"ENABLE TARGET MODE",
7084 	"GET TARGET MODE STATE",
7085 	NULL,
7086 	NULL,
7087 	NULL,
7088 	"SET DATA OVERRUN RECOVERY MODE",
7089 	"GET DATA OVERRUN RECOVERY MODE",
7090 	"SET HOST DATA",
7091 	"GET NOST DATA",
7092 };
7093 
7094 #define	ISP_FC_IBITS(op)	((mbpfc[((op)<<3) + 0] << 24) | (mbpfc[((op)<<3) + 1] << 16) | (mbpfc[((op)<<3) + 2] << 8) | (mbpfc[((op)<<3) + 3]))
7095 #define	ISP_FC_OBITS(op)	((mbpfc[((op)<<3) + 4] << 24) | (mbpfc[((op)<<3) + 5] << 16) | (mbpfc[((op)<<3) + 6] << 8) | (mbpfc[((op)<<3) + 7]))
7096 
7097 #define	ISP_FC_OPMAP(in0, out0)							  0,   0,   0, in0,    0,    0,    0, out0
7098 #define	ISP_FC_OPMAP_HALF(in1, in0, out1, out0)					  0,   0, in1, in0,    0,    0, out1, out0
7099 #define	ISP_FC_OPMAP_FULL(in3, in2, in1, in0, out3, out2, out1, out0)		in3, in2, in1, in0, out3, out2, out1, out0
7100 static const uint32_t mbpfc[] = {
7101 	ISP_FC_OPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
7102 	ISP_FC_OPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
7103 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
7104 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
7105 	ISP_FC_OPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
7106 	ISP_FC_OPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
7107 	ISP_FC_OPMAP_FULL(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
7108 	ISP_FC_OPMAP(0x07, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
7109 	ISP_FC_OPMAP_FULL(0x0, 0x0, 0x0, 0x01, 0x0, 0x3, 0x80, 0x7f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
7110 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x09: MBOX_LOAD_RISC_RAM_2100 */
7111 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
7112 	ISP_FC_OPMAP_HALF(0x1, 0xff, 0x0, 0x01),	/* 0x0b: MBOX_LOAD_RISC_RAM */
7113 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x0c: */
7114 	ISP_FC_OPMAP_HALF(0x1, 0x0f, 0x0, 0x01),	/* 0x0d: MBOX_WRITE_RAM_WORD_EXTENDED */
7115 	ISP_FC_OPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
7116 	ISP_FC_OPMAP_HALF(0x1, 0x03, 0x0, 0x0d),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED */
7117 	ISP_FC_OPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
7118 	ISP_FC_OPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
7119 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
7120 	ISP_FC_OPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
7121 	ISP_FC_OPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
7122 	ISP_FC_OPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
7123 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
7124 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
7125 	ISP_FC_OPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
7126 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
7127 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
7128 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
7129 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
7130 	ISP_FC_OPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
7131 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x1e: */
7132 	ISP_FC_OPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
7133 	ISP_FC_OPMAP_HALF(0x2, 0x01, 0x7e, 0xcf),	/* 0x20: MBOX_GET_LOOP_ID */
7134 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x21: */
7135 	ISP_FC_OPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
7136 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x23: */
7137 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x24: */
7138 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x25: */
7139 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x26: */
7140 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x27: */
7141 	ISP_FC_OPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
7142 	ISP_FC_OPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
7143 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2a: */
7144 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2b: */
7145 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2c: */
7146 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2d: */
7147 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2e: */
7148 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2f: */
7149 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x30: */
7150 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x31: */
7151 	ISP_FC_OPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
7152 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x33: */
7153 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x34: */
7154 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x35: */
7155 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x36: */
7156 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x37: */
7157 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
7158 	ISP_FC_OPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
7159 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3a: */
7160 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3b: */
7161 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3c: */
7162 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3d: */
7163 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3e: */
7164 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3f: */
7165 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
7166 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
7167 	ISP_FC_OPMAP_HALF(0x0, 0x01, 0x3, 0xcf),	/* 0x42: MBOX_GET_RESOURCE_COUNT */
7168 	ISP_FC_OPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_OFFLINE_MODE */
7169 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x44: */
7170 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x45: */
7171 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x46: */
7172 	ISP_FC_OPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
7173 	ISP_FC_OPMAP(0xcf, 0x0f),	/* 0x48: MBOX_INIT_FIRMWARE_MULTI_ID */
7174 	ISP_FC_OPMAP(0xcd, 0x01),	/* 0x49: MBOX_GET_VP_DATABASE */
7175 	ISP_FC_OPMAP_HALF(0x2, 0xcd, 0x0, 0x01),	/* 0x4a: MBOX_GET_VP_DATABASE_ENTRY */
7176 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4b: */
7177 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4c: */
7178 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4d: */
7179 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4e: */
7180 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4f: */
7181 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x50: */
7182 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x51: */
7183 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x52: */
7184 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x53: */
7185 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
7186 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x55: */
7187 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x56: */
7188 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x57: */
7189 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x58: */
7190 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x59: */
7191 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x5a: */
7192 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
7193 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
7194 	ISP_FC_OPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
7195 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x5e: */
7196 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x5f: */
7197 	ISP_FC_OPMAP(0xcf, 0x0f),	/* 0x60: MBOX_INIT_FIRMWARE */
7198 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x61: */
7199 	ISP_FC_OPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
7200 	ISP_FC_OPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
7201 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
7202 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
7203 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
7204 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
7205 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
7206 	ISP_FC_OPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
7207 	ISP_FC_OPMAP_HALF(0x6, 0x03, 0x0, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
7208 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
7209 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
7210 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x6d: */
7211 	ISP_FC_OPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
7212 	ISP_FC_OPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
7213 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
7214 	ISP_FC_OPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
7215 	ISP_FC_OPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
7216 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x73: */
7217 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
7218 	ISP_FC_OPMAP_HALF(0x03, 0xcf, 0x00, 0x07),	/* 0x75: GET PORT/NODE NAME LIST */
7219 	ISP_FC_OPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
7220 	ISP_FC_OPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
7221 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x78: */
7222 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x79: */
7223 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x7a: */
7224 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x7b: */
7225 	ISP_FC_OPMAP_HALF(0x03, 0x4f, 0x00, 0x07),	/* 0x7c: Get ID List */
7226 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
7227 	ISP_FC_OPMAP(0x0f, 0x01)	/* 0x7e: LUN RESET */
7228 };
7229 #define	MAX_FC_OPCODE	0x7e
7230 /*
7231  * Footnotes
7232  *
7233  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
7234  *	do not access at this time in the core driver. The caller is
7235  *	responsible for setting this register first (Gross!). The assumption
7236  *	is that we won't overflow.
7237  */
7238 
7239 static const char *fc_mbcmd_names[] = {
7240 	"NO-OP",
7241 	"LOAD RAM",
7242 	"EXEC FIRMWARE",
7243 	"DUMP RAM",
7244 	"WRITE RAM WORD",
7245 	"READ RAM WORD",
7246 	"MAILBOX REG TEST",
7247 	"VERIFY CHECKSUM",
7248 	"ABOUT FIRMWARE",
7249 	"LOAD RAM (2100)",
7250 	"DUMP RAM",
7251 	"LOAD RISC RAM",
7252 	NULL,
7253 	"WRITE RAM WORD EXTENDED",
7254 	"CHECK FIRMWARE",
7255 	"READ RAM WORD EXTENDED",
7256 	"INIT REQUEST QUEUE",
7257 	"INIT RESULT QUEUE",
7258 	"EXECUTE IOCB",
7259 	"WAKE UP",
7260 	"STOP FIRMWARE",
7261 	"ABORT",
7262 	"ABORT DEVICE",
7263 	"ABORT TARGET",
7264 	"BUS RESET",
7265 	"STOP QUEUE",
7266 	"START QUEUE",
7267 	"SINGLE STEP QUEUE",
7268 	"ABORT QUEUE",
7269 	"GET DEV QUEUE STATUS",
7270 	NULL,
7271 	"GET FIRMWARE STATUS",
7272 	"GET LOOP ID",
7273 	NULL,
7274 	"GET RETRY COUNT",
7275 	NULL,
7276 	NULL,
7277 	NULL,
7278 	NULL,
7279 	NULL,
7280 	"GET FIRMWARE OPTIONS",
7281 	"GET PORT QUEUE PARAMS",
7282 	NULL,
7283 	NULL,
7284 	NULL,
7285 	NULL,
7286 	NULL,
7287 	NULL,
7288 	NULL,
7289 	NULL,
7290 	"SET RETRY COUNT",
7291 	NULL,
7292 	NULL,
7293 	NULL,
7294 	NULL,
7295 	NULL,
7296 	"SET FIRMWARE OPTIONS",
7297 	"SET PORT QUEUE PARAMS",
7298 	NULL,
7299 	NULL,
7300 	NULL,
7301 	NULL,
7302 	NULL,
7303 	NULL,
7304 	"LOOP PORT BYPASS",
7305 	"LOOP PORT ENABLE",
7306 	"GET RESOURCE COUNT",
7307 	"REQUEST NON PARTICIPATING MODE",
7308 	NULL,
7309 	NULL,
7310 	NULL,
7311 	"GET PORT DATABASE ENHANCED",
7312 	"INIT FIRMWARE MULTI ID",
7313 	"GET VP DATABASE",
7314 	"GET VP DATABASE ENTRY",
7315 	NULL,
7316 	NULL,
7317 	NULL,
7318 	NULL,
7319 	NULL,
7320 	NULL,
7321 	NULL,
7322 	NULL,
7323 	NULL,
7324 	"EXECUTE IOCB A64",
7325 	NULL,
7326 	NULL,
7327 	NULL,
7328 	NULL,
7329 	NULL,
7330 	NULL,
7331 	"DRIVER HEARTBEAT",
7332 	NULL,
7333 	"GET/SET DATA RATE",
7334 	NULL,
7335 	NULL,
7336 	"INIT FIRMWARE",
7337 	NULL,
7338 	"INIT LIP",
7339 	"GET FC-AL POSITION MAP",
7340 	"GET PORT DATABASE",
7341 	"CLEAR ACA",
7342 	"TARGET RESET",
7343 	"CLEAR TASK SET",
7344 	"ABORT TASK SET",
7345 	"GET FW STATE",
7346 	"GET PORT NAME",
7347 	"GET LINK STATUS",
7348 	"INIT LIP RESET",
7349 	NULL,
7350 	"SEND SNS",
7351 	"FABRIC LOGIN",
7352 	"SEND CHANGE REQUEST",
7353 	"FABRIC LOGOUT",
7354 	"INIT LIP LOGIN",
7355 	NULL,
7356 	"LOGIN LOOP PORT",
7357 	"GET PORT/NODE NAME LIST",
7358 	"SET VENDOR ID",
7359 	"INITIALIZE IP MAILBOX",
7360 	NULL,
7361 	NULL,
7362 	NULL,
7363 	NULL,
7364 	"Get ID List",
7365 	"SEND LFA",
7366 	"Lun RESET"
7367 };
7368 
7369 static void
7370 isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
7371 {
7372 	unsigned int ibits, obits, box, opcode;
7373 
7374 	opcode = mbp->param[0];
7375 	if (IS_FC(isp)) {
7376 		ibits = ISP_FC_IBITS(opcode);
7377 		obits = ISP_FC_OBITS(opcode);
7378 	} else {
7379 		ibits = ISP_SCSI_IBITS(opcode);
7380 		obits = ISP_SCSI_OBITS(opcode);
7381 	}
7382 	ibits |= mbp->ibits;
7383 	obits |= mbp->obits;
7384 	for (box = 0; box < ISP_NMBOX(isp); box++) {
7385 		if (ibits & (1 << box)) {
7386 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7387 		}
7388 		if (nodelay == 0) {
7389 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7390 		}
7391 	}
7392 	if (nodelay == 0) {
7393 		isp->isp_lastmbxcmd = opcode;
7394 		isp->isp_obits = obits;
7395 		isp->isp_mboxbsy = 1;
7396 	}
7397 	if (IS_24XX(isp)) {
7398 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7399 	} else {
7400 		ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7401 	}
7402 	/*
7403 	 * Oddly enough, if we're not delaying for an answer,
7404 	 * delay a bit to give the f/w a chance to pick up the
7405 	 * command.
7406 	 */
7407 	if (nodelay) {
7408 		ISP_DELAY(1000);
7409 	}
7410 }
7411 
7412 static void
7413 isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp)
7414 {
7415 	const char *cname, *xname, *sname;
7416 	char tname[16], mname[16];
7417 	unsigned int ibits, obits, box, opcode;
7418 
7419 	opcode = mbp->param[0];
7420 	if (IS_FC(isp)) {
7421 		if (opcode > MAX_FC_OPCODE) {
7422 			mbp->param[0] = MBOX_INVALID_COMMAND;
7423 			isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7424 			return;
7425 		}
7426 		cname = fc_mbcmd_names[opcode];
7427 		ibits = ISP_FC_IBITS(opcode);
7428 		obits = ISP_FC_OBITS(opcode);
7429 	} else {
7430 		if (opcode > MAX_SCSI_OPCODE) {
7431 			mbp->param[0] = MBOX_INVALID_COMMAND;
7432 			isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7433 			return;
7434 		}
7435 		cname = scsi_mbcmd_names[opcode];
7436 		ibits = ISP_SCSI_IBITS(opcode);
7437 		obits = ISP_SCSI_OBITS(opcode);
7438 	}
7439 	if (cname == NULL) {
7440 		cname = tname;
7441 		ISP_SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
7442 	}
7443 	isp_prt(isp, ISP_LOGDEBUG3, "Mailbox Command '%s'", cname);
7444 
7445 	/*
7446 	 * Pick up any additional bits that the caller might have set.
7447 	 */
7448 	ibits |= mbp->ibits;
7449 	obits |= mbp->obits;
7450 
7451 	/*
7452 	 * Mask any bits that the caller wants us to mask
7453 	 */
7454 	ibits &= mbp->ibitm;
7455 	obits &= mbp->obitm;
7456 
7457 
7458 	if (ibits == 0 && obits == 0) {
7459 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
7460 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
7461 		return;
7462 	}
7463 
7464 	/*
7465 	 * Get exclusive usage of mailbox registers.
7466 	 */
7467 	if (MBOX_ACQUIRE(isp)) {
7468 		mbp->param[0] = MBOX_REGS_BUSY;
7469 		goto out;
7470 	}
7471 
7472 	for (box = 0; box < ISP_NMBOX(isp); box++) {
7473 		if (ibits & (1 << box)) {
7474 			isp_prt(isp, ISP_LOGDEBUG3, "IN mbox %d = 0x%04x", box,
7475 			    mbp->param[box]);
7476 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7477 		}
7478 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7479 	}
7480 
7481 	isp->isp_lastmbxcmd = opcode;
7482 
7483 	/*
7484 	 * We assume that we can't overwrite a previous command.
7485 	 */
7486 	isp->isp_obits = obits;
7487 	isp->isp_mboxbsy = 1;
7488 
7489 	/*
7490 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
7491 	 */
7492 	if (IS_24XX(isp)) {
7493 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7494 	} else {
7495 		ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7496 	}
7497 
7498 	/*
7499 	 * While we haven't finished the command, spin our wheels here.
7500 	 */
7501 	MBOX_WAIT_COMPLETE(isp, mbp);
7502 
7503 	/*
7504 	 * Did the command time out?
7505 	 */
7506 	if (mbp->param[0] == MBOX_TIMEOUT) {
7507 		isp->isp_mboxbsy = 0;
7508 		MBOX_RELEASE(isp);
7509 		goto out;
7510 	}
7511 
7512 	/*
7513 	 * Copy back output registers.
7514 	 */
7515 	for (box = 0; box < ISP_NMBOX(isp); box++) {
7516 		if (obits & (1 << box)) {
7517 			mbp->param[box] = isp->isp_mboxtmp[box];
7518 			isp_prt(isp, ISP_LOGDEBUG3, "OUT mbox %d = 0x%04x", box,
7519 			    mbp->param[box]);
7520 		}
7521 	}
7522 
7523 	isp->isp_mboxbsy = 0;
7524 	MBOX_RELEASE(isp);
7525 out:
7526 	if (mbp->logval == 0 || mbp->param[0] == MBOX_COMMAND_COMPLETE)
7527 		return;
7528 
7529 	if ((mbp->param[0] & 0xbfe0) == 0 &&
7530 	    (mbp->logval & MBLOGMASK(mbp->param[0])) == 0)
7531 		return;
7532 
7533 	xname = NULL;
7534 	sname = "";
7535 	switch (mbp->param[0]) {
7536 	case MBOX_INVALID_COMMAND:
7537 		xname = "INVALID COMMAND";
7538 		break;
7539 	case MBOX_HOST_INTERFACE_ERROR:
7540 		xname = "HOST INTERFACE ERROR";
7541 		break;
7542 	case MBOX_TEST_FAILED:
7543 		xname = "TEST FAILED";
7544 		break;
7545 	case MBOX_COMMAND_ERROR:
7546 		xname = "COMMAND ERROR";
7547 		ISP_SNPRINTF(mname, sizeof(mname), " subcode 0x%x",
7548 		    mbp->param[1]);
7549 		sname = mname;
7550 		break;
7551 	case MBOX_COMMAND_PARAM_ERROR:
7552 		xname = "COMMAND PARAMETER ERROR";
7553 		break;
7554 	case MBOX_PORT_ID_USED:
7555 		xname = "PORT ID ALREADY IN USE";
7556 		break;
7557 	case MBOX_LOOP_ID_USED:
7558 		xname = "LOOP ID ALREADY IN USE";
7559 		break;
7560 	case MBOX_ALL_IDS_USED:
7561 		xname = "ALL LOOP IDS IN USE";
7562 		break;
7563 	case MBOX_NOT_LOGGED_IN:
7564 		xname = "NOT LOGGED IN";
7565 		break;
7566 	case MBOX_LINK_DOWN_ERROR:
7567 		xname = "LINK DOWN ERROR";
7568 		break;
7569 	case MBOX_LOOPBACK_ERROR:
7570 		xname = "LOOPBACK ERROR";
7571 		break;
7572 	case MBOX_CHECKSUM_ERROR:
7573 		xname = "CHECKSUM ERROR";
7574 		break;
7575 	case MBOX_INVALID_PRODUCT_KEY:
7576 		xname = "INVALID PRODUCT KEY";
7577 		break;
7578 	case MBOX_REGS_BUSY:
7579 		xname = "REGISTERS BUSY";
7580 		break;
7581 	case MBOX_TIMEOUT:
7582 		xname = "TIMEOUT";
7583 		break;
7584 	default:
7585 		ISP_SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
7586 		xname = mname;
7587 		break;
7588 	}
7589 	if (xname) {
7590 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s%s)",
7591 		    cname, xname, sname);
7592 	}
7593 }
7594 
7595 static void
7596 isp_fw_state(ispsoftc_t *isp, int chan)
7597 {
7598 	if (IS_FC(isp)) {
7599 		mbreg_t mbs;
7600 		fcparam *fcp = FCPARAM(isp, chan);
7601 
7602 		MBSINIT(&mbs, MBOX_GET_FW_STATE, MBLOGALL, 0);
7603 		isp_mboxcmd(isp, &mbs);
7604 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
7605 			fcp->isp_fwstate = mbs.param[1];
7606 		}
7607 	}
7608 }
7609 
7610 static void
7611 isp_spi_update(ispsoftc_t *isp, int chan)
7612 {
7613 	int tgt;
7614 	mbreg_t mbs;
7615 	sdparam *sdp;
7616 
7617 	if (IS_FC(isp)) {
7618 		/*
7619 		 * There are no 'per-bus' settings for Fibre Channel.
7620 		 */
7621 		return;
7622 	}
7623 	sdp = SDPARAM(isp, chan);
7624 	sdp->update = 0;
7625 
7626 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7627 		uint16_t flags, period, offset;
7628 		int get;
7629 
7630 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
7631 			sdp->isp_devparam[tgt].dev_update = 0;
7632 			sdp->isp_devparam[tgt].dev_refresh = 0;
7633 			isp_prt(isp, ISP_LOGDEBUG0, "skipping target %d bus %d update", tgt, chan);
7634 			continue;
7635 		}
7636 		/*
7637 		 * If the goal is to update the status of the device,
7638 		 * take what's in goal_flags and try and set the device
7639 		 * toward that. Otherwise, if we're just refreshing the
7640 		 * current device state, get the current parameters.
7641 		 */
7642 
7643 		MBSINIT(&mbs, 0, MBLOGALL, 0);
7644 
7645 		/*
7646 		 * Refresh overrides set
7647 		 */
7648 		if (sdp->isp_devparam[tgt].dev_refresh) {
7649 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
7650 			get = 1;
7651 		} else if (sdp->isp_devparam[tgt].dev_update) {
7652 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
7653 
7654 			/*
7655 			 * Make sure goal_flags has "Renegotiate on Error"
7656 			 * on and "Freeze Queue on Error" off.
7657 			 */
7658 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
7659 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
7660 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
7661 
7662 			/*
7663 			 * Insist that PARITY must be enabled
7664 			 * if SYNC or WIDE is enabled.
7665 			 */
7666 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
7667 				mbs.param[2] |= DPARM_PARITY;
7668 			}
7669 
7670 			if (mbs.param[2] & DPARM_SYNC) {
7671 				mbs.param[3] =
7672 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
7673 				    (sdp->isp_devparam[tgt].goal_period);
7674 			}
7675 			/*
7676 			 * A command completion later that has
7677 			 * RQSTF_NEGOTIATION set can cause
7678 			 * the dev_refresh/announce cycle also.
7679 			 *
7680 			 * Note: It is really important to update our current
7681 			 * flags with at least the state of TAG capabilities-
7682 			 * otherwise we might try and send a tagged command
7683 			 * when we have it all turned off. So change it here
7684 			 * to say that current already matches goal.
7685 			 */
7686 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
7687 			sdp->isp_devparam[tgt].actv_flags |=
7688 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
7689 			isp_prt(isp, ISP_LOGDEBUG0, "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
7690 			    chan, tgt, mbs.param[2], mbs.param[3] >> 8, mbs.param[3] & 0xff);
7691 			get = 0;
7692 		} else {
7693 			continue;
7694 		}
7695 		mbs.param[1] = (chan << 15) | (tgt << 8);
7696 		isp_mboxcmd(isp, &mbs);
7697 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7698 			continue;
7699 		}
7700 		if (get == 0) {
7701 			sdp->sendmarker = 1;
7702 			sdp->isp_devparam[tgt].dev_update = 0;
7703 			sdp->isp_devparam[tgt].dev_refresh = 1;
7704 		} else {
7705 			sdp->isp_devparam[tgt].dev_refresh = 0;
7706 			flags = mbs.param[2];
7707 			period = mbs.param[3] & 0xff;
7708 			offset = mbs.param[3] >> 8;
7709 			sdp->isp_devparam[tgt].actv_flags = flags;
7710 			sdp->isp_devparam[tgt].actv_period = period;
7711 			sdp->isp_devparam[tgt].actv_offset = offset;
7712 			isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, chan, tgt);
7713 		}
7714 	}
7715 
7716 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7717 		if (sdp->isp_devparam[tgt].dev_update ||
7718 		    sdp->isp_devparam[tgt].dev_refresh) {
7719 			sdp->update = 1;
7720 			break;
7721 		}
7722 	}
7723 }
7724 
7725 static void
7726 isp_setdfltsdparm(ispsoftc_t *isp)
7727 {
7728 	int tgt;
7729 	sdparam *sdp, *sdp1;
7730 
7731 	sdp = SDPARAM(isp, 0);
7732 	sdp->role = GET_DEFAULT_ROLE(isp, 0);
7733 	if (IS_DUALBUS(isp)) {
7734 		sdp1 = sdp + 1;
7735 		sdp1->role = GET_DEFAULT_ROLE(isp, 1);
7736 	} else {
7737 		sdp1 = NULL;
7738 	}
7739 
7740 	/*
7741 	 * Establish some default parameters.
7742 	 */
7743 	sdp->isp_cmd_dma_burst_enable = 0;
7744 	sdp->isp_data_dma_burst_enabl = 1;
7745 	sdp->isp_fifo_threshold = 0;
7746 	sdp->isp_initiator_id = DEFAULT_IID(isp, 0);
7747 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
7748 		sdp->isp_async_data_setup = 9;
7749 	} else {
7750 		sdp->isp_async_data_setup = 6;
7751 	}
7752 	sdp->isp_selection_timeout = 250;
7753 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
7754 	sdp->isp_tag_aging = 8;
7755 	sdp->isp_bus_reset_delay = 5;
7756 	/*
7757 	 * Don't retry selection, busy or queue full automatically- reflect
7758 	 * these back to us.
7759 	 */
7760 	sdp->isp_retry_count = 0;
7761 	sdp->isp_retry_delay = 0;
7762 
7763 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7764 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
7765 		sdp->isp_devparam[tgt].dev_enable = 1;
7766 	}
7767 
7768 	/*
7769 	 * The trick here is to establish a default for the default (honk!)
7770 	 * state (goal_flags). Then try and get the current status from
7771 	 * the card to fill in the current state. We don't, in fact, set
7772 	 * the default to the SAFE default state- that's not the goal state.
7773 	 */
7774 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7775 		uint8_t off, per;
7776 		sdp->isp_devparam[tgt].actv_offset = 0;
7777 		sdp->isp_devparam[tgt].actv_period = 0;
7778 		sdp->isp_devparam[tgt].actv_flags = 0;
7779 
7780 		sdp->isp_devparam[tgt].goal_flags =
7781 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
7782 
7783 		/*
7784 		 * We default to Wide/Fast for versions less than a 1040
7785 		 * (unless it's SBus).
7786 		 */
7787 		if (IS_ULTRA3(isp)) {
7788 			off = ISP_80M_SYNCPARMS >> 8;
7789 			per = ISP_80M_SYNCPARMS & 0xff;
7790 		} else if (IS_ULTRA2(isp)) {
7791 			off = ISP_40M_SYNCPARMS >> 8;
7792 			per = ISP_40M_SYNCPARMS & 0xff;
7793 		} else if (IS_1240(isp)) {
7794 			off = ISP_20M_SYNCPARMS >> 8;
7795 			per = ISP_20M_SYNCPARMS & 0xff;
7796 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
7797 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
7798 		    (isp->isp_bustype == ISP_BT_PCI &&
7799 		    isp->isp_type < ISP_HA_SCSI_1040) ||
7800 		    (isp->isp_clock && isp->isp_clock < 60) ||
7801 		    (sdp->isp_ultramode == 0)) {
7802 			off = ISP_10M_SYNCPARMS >> 8;
7803 			per = ISP_10M_SYNCPARMS & 0xff;
7804 		} else {
7805 			off = ISP_20M_SYNCPARMS_1040 >> 8;
7806 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
7807 		}
7808 		sdp->isp_devparam[tgt].goal_offset =
7809 		    sdp->isp_devparam[tgt].nvrm_offset = off;
7810 		sdp->isp_devparam[tgt].goal_period =
7811 		    sdp->isp_devparam[tgt].nvrm_period = per;
7812 
7813 	}
7814 
7815 	/*
7816 	 * If we're a dual bus card, just copy the data over
7817 	 */
7818 	if (sdp1) {
7819 		*sdp1 = *sdp;
7820 		sdp1->isp_initiator_id = DEFAULT_IID(isp, 1);
7821 	}
7822 
7823 	/*
7824 	 * If we've not been told to avoid reading NVRAM, try and read it.
7825 	 * If we're successful reading it, we can then return because NVRAM
7826 	 * will tell us what the desired settings are. Otherwise, we establish
7827 	 * some reasonable 'fake' nvram and goal defaults.
7828 	 */
7829 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7830 		mbreg_t mbs;
7831 
7832 		if (isp_read_nvram(isp, 0) == 0) {
7833 			if (IS_DUALBUS(isp)) {
7834 				if (isp_read_nvram(isp, 1) == 0) {
7835 					return;
7836 				}
7837 			}
7838 		}
7839 		MBSINIT(&mbs, MBOX_GET_ACT_NEG_STATE, MBLOGNONE, 0);
7840 		isp_mboxcmd(isp, &mbs);
7841 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7842 			sdp->isp_req_ack_active_neg = 1;
7843 			sdp->isp_data_line_active_neg = 1;
7844 			if (sdp1) {
7845 				sdp1->isp_req_ack_active_neg = 1;
7846 				sdp1->isp_data_line_active_neg = 1;
7847 			}
7848 		} else {
7849 			sdp->isp_req_ack_active_neg =
7850 			    (mbs.param[1] >> 4) & 0x1;
7851 			sdp->isp_data_line_active_neg =
7852 			    (mbs.param[1] >> 5) & 0x1;
7853 			if (sdp1) {
7854 				sdp1->isp_req_ack_active_neg =
7855 				    (mbs.param[2] >> 4) & 0x1;
7856 				sdp1->isp_data_line_active_neg =
7857 				    (mbs.param[2] >> 5) & 0x1;
7858 			}
7859 		}
7860 	}
7861 
7862 }
7863 
7864 static void
7865 isp_setdfltfcparm(ispsoftc_t *isp, int chan)
7866 {
7867 	fcparam *fcp = FCPARAM(isp, chan);
7868 
7869 	/*
7870 	 * Establish some default parameters.
7871 	 */
7872 	fcp->role = GET_DEFAULT_ROLE(isp, chan);
7873 	fcp->isp_maxalloc = ICB_DFLT_ALLOC;
7874 	fcp->isp_retry_delay = ICB_DFLT_RDELAY;
7875 	fcp->isp_retry_count = ICB_DFLT_RCOUNT;
7876 	fcp->isp_loopid = DEFAULT_LOOPID(isp, chan);
7877 	fcp->isp_wwnn_nvram = DEFAULT_NODEWWN(isp, chan);
7878 	fcp->isp_wwpn_nvram = DEFAULT_PORTWWN(isp, chan);
7879 	fcp->isp_fwoptions = 0;
7880 	fcp->isp_lasthdl = NIL_HANDLE;
7881 
7882 	if (IS_24XX(isp)) {
7883 		fcp->isp_fwoptions |= ICB2400_OPT1_FAIRNESS;
7884 		fcp->isp_fwoptions |= ICB2400_OPT1_HARD_ADDRESS;
7885 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7886 			fcp->isp_fwoptions |= ICB2400_OPT1_FULL_DUPLEX;
7887 		}
7888 		fcp->isp_fwoptions |= ICB2400_OPT1_BOTH_WWNS;
7889 	} else {
7890 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
7891 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
7892 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
7893 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7894 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
7895 		}
7896 		/*
7897 		 * Make sure this is turned off now until we get
7898 		 * extended options from NVRAM
7899 		 */
7900 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
7901 	}
7902 
7903 
7904 	/*
7905 	 * Now try and read NVRAM unless told to not do so.
7906 	 * This will set fcparam's isp_wwnn_nvram && isp_wwpn_nvram.
7907 	 */
7908 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7909 		int i, j = 0;
7910 		/*
7911 		 * Give a couple of tries at reading NVRAM.
7912 		 */
7913 		for (i = 0; i < 2; i++) {
7914 			j = isp_read_nvram(isp, chan);
7915 			if (j == 0) {
7916 				break;
7917 			}
7918 		}
7919 		if (j) {
7920 			isp->isp_confopts |= ISP_CFG_NONVRAM;
7921 		}
7922 	}
7923 
7924 	fcp->isp_wwnn = ACTIVE_NODEWWN(isp, chan);
7925 	fcp->isp_wwpn = ACTIVE_PORTWWN(isp, chan);
7926 	isp_prt(isp, ISP_LOGCONFIG, "Chan %d 0x%08x%08x/0x%08x%08x Role %s",
7927 	    chan, (uint32_t) (fcp->isp_wwnn >> 32), (uint32_t) (fcp->isp_wwnn),
7928 	    (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) (fcp->isp_wwpn),
7929 	    isp_class3_roles[fcp->role]);
7930 }
7931 
7932 /*
7933  * Re-initialize the ISP and complete all orphaned commands
7934  * with a 'botched' notice. The reset/init routines should
7935  * not disturb an already active list of commands.
7936  */
7937 
7938 int
7939 isp_reinit(ispsoftc_t *isp, int do_load_defaults)
7940 {
7941 	int i, res = 0;
7942 
7943 	if (isp->isp_state != ISP_RESETSTATE)
7944 		isp_reset(isp, do_load_defaults);
7945 	if (isp->isp_state != ISP_RESETSTATE) {
7946 		res = EIO;
7947 		isp_prt(isp, ISP_LOGERR, "%s: cannot reset card", __func__);
7948 		ISP_DISABLE_INTS(isp);
7949 		goto cleanup;
7950 	}
7951 
7952 	isp_init(isp);
7953 	if (isp->isp_state > ISP_RESETSTATE &&
7954 	    isp->isp_state != ISP_RUNSTATE) {
7955 		res = EIO;
7956 		isp_prt(isp, ISP_LOGERR, "%s: cannot init card", __func__);
7957 		ISP_DISABLE_INTS(isp);
7958 		if (IS_FC(isp)) {
7959 			/*
7960 			 * If we're in ISP_ROLE_NONE, turn off the lasers.
7961 			 */
7962 			if (!IS_24XX(isp)) {
7963 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
7964 				ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
7965 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
7966 				ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
7967 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
7968 			}
7969 		}
7970 	}
7971 
7972  cleanup:
7973 	isp->isp_nactive = 0;
7974 	isp_clear_commands(isp);
7975 	if (IS_FC(isp)) {
7976 		for (i = 0; i < isp->isp_nchan; i++)
7977 			ISP_MARK_PORTDB(isp, i, -1);
7978 	}
7979 	return (res);
7980 }
7981 
7982 /*
7983  * NVRAM Routines
7984  */
7985 static int
7986 isp_read_nvram(ispsoftc_t *isp, int bus)
7987 {
7988 	int i, amt, retval;
7989 	uint8_t csum, minversion;
7990 	union {
7991 		uint8_t _x[ISP2400_NVRAM_SIZE];
7992 		uint16_t _s[ISP2400_NVRAM_SIZE>>1];
7993 	} _n;
7994 #define	nvram_data	_n._x
7995 #define	nvram_words	_n._s
7996 
7997 	if (IS_24XX(isp)) {
7998 		return (isp_read_nvram_2400(isp, nvram_data));
7999 	} else if (IS_FC(isp)) {
8000 		amt = ISP2100_NVRAM_SIZE;
8001 		minversion = 1;
8002 	} else if (IS_ULTRA2(isp)) {
8003 		amt = ISP1080_NVRAM_SIZE;
8004 		minversion = 0;
8005 	} else {
8006 		amt = ISP_NVRAM_SIZE;
8007 		minversion = 2;
8008 	}
8009 
8010 	for (i = 0; i < amt>>1; i++) {
8011 		isp_rdnvram_word(isp, i, &nvram_words[i]);
8012 	}
8013 
8014 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
8015 	    nvram_data[2] != 'P') {
8016 		if (isp->isp_bustype != ISP_BT_SBUS) {
8017 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
8018 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x", nvram_data[0], nvram_data[1], nvram_data[2]);
8019 		}
8020 		retval = -1;
8021 		goto out;
8022 	}
8023 
8024 	for (csum = 0, i = 0; i < amt; i++) {
8025 		csum += nvram_data[i];
8026 	}
8027 	if (csum != 0) {
8028 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
8029 		retval = -1;
8030 		goto out;
8031 	}
8032 
8033 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
8034 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
8035 		    ISP_NVRAM_VERSION(nvram_data));
8036 		retval = -1;
8037 		goto out;
8038 	}
8039 
8040 	if (IS_ULTRA3(isp)) {
8041 		isp_parse_nvram_12160(isp, bus, nvram_data);
8042 	} else if (IS_1080(isp)) {
8043 		isp_parse_nvram_1080(isp, bus, nvram_data);
8044 	} else if (IS_1280(isp) || IS_1240(isp)) {
8045 		isp_parse_nvram_1080(isp, bus, nvram_data);
8046 	} else if (IS_SCSI(isp)) {
8047 		isp_parse_nvram_1020(isp, nvram_data);
8048 	} else {
8049 		isp_parse_nvram_2100(isp, nvram_data);
8050 	}
8051 	retval = 0;
8052 out:
8053 	return (retval);
8054 #undef	nvram_data
8055 #undef	nvram_words
8056 }
8057 
8058 static int
8059 isp_read_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
8060 {
8061 	int retval = 0;
8062 	uint32_t addr, csum, lwrds, *dptr;
8063 
8064 	if (isp->isp_port) {
8065 		addr = ISP2400_NVRAM_PORT1_ADDR;
8066 	} else {
8067 		addr = ISP2400_NVRAM_PORT0_ADDR;
8068 	}
8069 
8070 	dptr = (uint32_t *) nvram_data;
8071 	for (lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
8072 		isp_rd_2400_nvram(isp, addr++, dptr++);
8073 	}
8074 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
8075 	    nvram_data[2] != 'P') {
8076 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header (%x %x %x)",
8077 		    nvram_data[0], nvram_data[1], nvram_data[2]);
8078 		retval = -1;
8079 		goto out;
8080 	}
8081 	dptr = (uint32_t *) nvram_data;
8082 	for (csum = 0, lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
8083 		uint32_t tmp;
8084 		ISP_IOXGET_32(isp, &dptr[lwrds], tmp);
8085 		csum += tmp;
8086 	}
8087 	if (csum != 0) {
8088 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
8089 		retval = -1;
8090 		goto out;
8091 	}
8092 	isp_parse_nvram_2400(isp, nvram_data);
8093 out:
8094 	return (retval);
8095 }
8096 
8097 static void
8098 isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
8099 {
8100 	int i, cbits;
8101 	uint16_t bit, rqst, junk;
8102 
8103 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
8104 	ISP_DELAY(10);
8105 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
8106 	ISP_DELAY(10);
8107 
8108 	if (IS_FC(isp)) {
8109 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
8110 		if (IS_2312(isp) && isp->isp_port) {
8111 			wo += 128;
8112 		}
8113 		rqst = (ISP_NVRAM_READ << 8) | wo;
8114 		cbits = 10;
8115 	} else if (IS_ULTRA2(isp)) {
8116 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
8117 		rqst = (ISP_NVRAM_READ << 8) | wo;
8118 		cbits = 10;
8119 	} else {
8120 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
8121 		rqst = (ISP_NVRAM_READ << 6) | wo;
8122 		cbits = 8;
8123 	}
8124 
8125 	/*
8126 	 * Clock the word select request out...
8127 	 */
8128 	for (i = cbits; i >= 0; i--) {
8129 		if ((rqst >> i) & 1) {
8130 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
8131 		} else {
8132 			bit = BIU_NVRAM_SELECT;
8133 		}
8134 		ISP_WRITE(isp, BIU_NVRAM, bit);
8135 		ISP_DELAY(10);
8136 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8137 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
8138 		ISP_DELAY(10);
8139 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8140 		ISP_WRITE(isp, BIU_NVRAM, bit);
8141 		ISP_DELAY(10);
8142 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8143 	}
8144 	/*
8145 	 * Now read the result back in (bits come back in MSB format).
8146 	 */
8147 	*rp = 0;
8148 	for (i = 0; i < 16; i++) {
8149 		uint16_t rv;
8150 		*rp <<= 1;
8151 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
8152 		ISP_DELAY(10);
8153 		rv = ISP_READ(isp, BIU_NVRAM);
8154 		if (rv & BIU_NVRAM_DATAIN) {
8155 			*rp |= 1;
8156 		}
8157 		ISP_DELAY(10);
8158 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
8159 		ISP_DELAY(10);
8160 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8161 	}
8162 	ISP_WRITE(isp, BIU_NVRAM, 0);
8163 	ISP_DELAY(10);
8164 	junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8165 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
8166 }
8167 
8168 static void
8169 isp_rd_2400_nvram(ispsoftc_t *isp, uint32_t addr, uint32_t *rp)
8170 {
8171 	int loops = 0;
8172 	uint32_t base = 0x7ffe0000;
8173 	uint32_t tmp = 0;
8174 
8175 	if (IS_25XX(isp)) {
8176 		base = 0x7ff00000 | 0x48000;
8177 	}
8178 	ISP_WRITE(isp, BIU2400_FLASH_ADDR, base | addr);
8179 	for (loops = 0; loops < 5000; loops++) {
8180 		ISP_DELAY(10);
8181 		tmp = ISP_READ(isp, BIU2400_FLASH_ADDR);
8182 		if ((tmp & (1U << 31)) != 0) {
8183 			break;
8184 		}
8185 	}
8186 	if (tmp & (1U << 31)) {
8187 		*rp = ISP_READ(isp, BIU2400_FLASH_DATA);
8188 		ISP_SWIZZLE_NVRAM_LONG(isp, rp);
8189 	} else {
8190 		*rp = 0xffffffff;
8191 	}
8192 }
8193 
8194 static void
8195 isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
8196 {
8197 	sdparam *sdp = SDPARAM(isp, 0);
8198 	int tgt;
8199 
8200 	sdp->isp_fifo_threshold =
8201 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
8202 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
8203 
8204 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8205 		sdp->isp_initiator_id = ISP_NVRAM_INITIATOR_ID(nvram_data);
8206 
8207 	sdp->isp_bus_reset_delay =
8208 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
8209 
8210 	sdp->isp_retry_count =
8211 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
8212 
8213 	sdp->isp_retry_delay =
8214 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
8215 
8216 	sdp->isp_async_data_setup =
8217 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
8218 
8219 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
8220 		if (sdp->isp_async_data_setup < 9) {
8221 			sdp->isp_async_data_setup = 9;
8222 		}
8223 	} else {
8224 		if (sdp->isp_async_data_setup != 6) {
8225 			sdp->isp_async_data_setup = 6;
8226 		}
8227 	}
8228 
8229 	sdp->isp_req_ack_active_neg =
8230 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
8231 
8232 	sdp->isp_data_line_active_neg =
8233 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
8234 
8235 	sdp->isp_data_dma_burst_enabl =
8236 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
8237 
8238 	sdp->isp_cmd_dma_burst_enable =
8239 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
8240 
8241 	sdp->isp_tag_aging =
8242 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
8243 
8244 	sdp->isp_selection_timeout =
8245 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
8246 
8247 	sdp->isp_max_queue_depth =
8248 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
8249 
8250 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
8251 
8252 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8253 		sdp->isp_devparam[tgt].dev_enable =
8254 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
8255 		sdp->isp_devparam[tgt].exc_throttle =
8256 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
8257 		sdp->isp_devparam[tgt].nvrm_offset =
8258 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
8259 		sdp->isp_devparam[tgt].nvrm_period =
8260 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
8261 		/*
8262 		 * We probably shouldn't lie about this, but it
8263 		 * it makes it much safer if we limit NVRAM values
8264 		 * to sanity.
8265 		 */
8266 		if (isp->isp_type < ISP_HA_SCSI_1040) {
8267 			/*
8268 			 * If we're not ultra, we can't possibly
8269 			 * be a shorter period than this.
8270 			 */
8271 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
8272 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
8273 			}
8274 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
8275 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
8276 			}
8277 		} else {
8278 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
8279 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
8280 			}
8281 		}
8282 		sdp->isp_devparam[tgt].nvrm_flags = 0;
8283 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
8284 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8285 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8286 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
8287 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8288 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
8289 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8290 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
8291 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8292 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
8293 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8294 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
8295 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8296 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
8297 		sdp->isp_devparam[tgt].goal_offset =
8298 		    sdp->isp_devparam[tgt].nvrm_offset;
8299 		sdp->isp_devparam[tgt].goal_period =
8300 		    sdp->isp_devparam[tgt].nvrm_period;
8301 		sdp->isp_devparam[tgt].goal_flags =
8302 		    sdp->isp_devparam[tgt].nvrm_flags;
8303 	}
8304 }
8305 
8306 static void
8307 isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
8308 {
8309 	sdparam *sdp = SDPARAM(isp, bus);
8310 	int tgt;
8311 
8312 	sdp->isp_fifo_threshold =
8313 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
8314 
8315 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8316 		sdp->isp_initiator_id = ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
8317 
8318 	sdp->isp_bus_reset_delay =
8319 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
8320 
8321 	sdp->isp_retry_count =
8322 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
8323 
8324 	sdp->isp_retry_delay =
8325 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
8326 
8327 	sdp->isp_async_data_setup =
8328 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
8329 
8330 	sdp->isp_req_ack_active_neg =
8331 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
8332 
8333 	sdp->isp_data_line_active_neg =
8334 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
8335 
8336 	sdp->isp_data_dma_burst_enabl =
8337 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
8338 
8339 	sdp->isp_cmd_dma_burst_enable =
8340 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
8341 
8342 	sdp->isp_selection_timeout =
8343 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
8344 
8345 	sdp->isp_max_queue_depth =
8346 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
8347 
8348 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8349 		sdp->isp_devparam[tgt].dev_enable =
8350 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
8351 		sdp->isp_devparam[tgt].exc_throttle =
8352 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
8353 		sdp->isp_devparam[tgt].nvrm_offset =
8354 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
8355 		sdp->isp_devparam[tgt].nvrm_period =
8356 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
8357 		sdp->isp_devparam[tgt].nvrm_flags = 0;
8358 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
8359 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8360 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8361 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
8362 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8363 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
8364 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8365 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
8366 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8367 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
8368 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8369 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
8370 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8371 		sdp->isp_devparam[tgt].actv_flags = 0;
8372 		sdp->isp_devparam[tgt].goal_offset =
8373 		    sdp->isp_devparam[tgt].nvrm_offset;
8374 		sdp->isp_devparam[tgt].goal_period =
8375 		    sdp->isp_devparam[tgt].nvrm_period;
8376 		sdp->isp_devparam[tgt].goal_flags =
8377 		    sdp->isp_devparam[tgt].nvrm_flags;
8378 	}
8379 }
8380 
8381 static void
8382 isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
8383 {
8384 	sdparam *sdp = SDPARAM(isp, bus);
8385 	int tgt;
8386 
8387 	sdp->isp_fifo_threshold =
8388 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
8389 
8390 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8391 		sdp->isp_initiator_id = ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
8392 
8393 	sdp->isp_bus_reset_delay =
8394 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
8395 
8396 	sdp->isp_retry_count =
8397 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
8398 
8399 	sdp->isp_retry_delay =
8400 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
8401 
8402 	sdp->isp_async_data_setup =
8403 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
8404 
8405 	sdp->isp_req_ack_active_neg =
8406 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
8407 
8408 	sdp->isp_data_line_active_neg =
8409 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
8410 
8411 	sdp->isp_data_dma_burst_enabl =
8412 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8413 
8414 	sdp->isp_cmd_dma_burst_enable =
8415 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8416 
8417 	sdp->isp_selection_timeout =
8418 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
8419 
8420 	sdp->isp_max_queue_depth =
8421 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
8422 
8423 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8424 		sdp->isp_devparam[tgt].dev_enable =
8425 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
8426 		sdp->isp_devparam[tgt].exc_throttle =
8427 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
8428 		sdp->isp_devparam[tgt].nvrm_offset =
8429 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
8430 		sdp->isp_devparam[tgt].nvrm_period =
8431 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
8432 		sdp->isp_devparam[tgt].nvrm_flags = 0;
8433 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
8434 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8435 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8436 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
8437 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8438 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
8439 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8440 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
8441 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8442 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
8443 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8444 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
8445 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8446 		sdp->isp_devparam[tgt].actv_flags = 0;
8447 		sdp->isp_devparam[tgt].goal_offset =
8448 		    sdp->isp_devparam[tgt].nvrm_offset;
8449 		sdp->isp_devparam[tgt].goal_period =
8450 		    sdp->isp_devparam[tgt].nvrm_period;
8451 		sdp->isp_devparam[tgt].goal_flags =
8452 		    sdp->isp_devparam[tgt].nvrm_flags;
8453 	}
8454 }
8455 
8456 static void
8457 isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
8458 {
8459 	fcparam *fcp = FCPARAM(isp, 0);
8460 	uint64_t wwn;
8461 
8462 	/*
8463 	 * There is NVRAM storage for both Port and Node entities-
8464 	 * but the Node entity appears to be unused on all the cards
8465 	 * I can find. However, we should account for this being set
8466 	 * at some point in the future.
8467 	 *
8468 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
8469 	 * bits 48..60. In the case of the 2202, it appears that they do
8470 	 * use bit 48 to distinguish between the two instances on the card.
8471 	 * The 2204, which I've never seen, *probably* extends this method.
8472 	 */
8473 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
8474 	if (wwn) {
8475 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
8476 		    (uint32_t) (wwn >> 32), (uint32_t) (wwn));
8477 		if ((wwn >> 60) == 0) {
8478 			wwn |= (((uint64_t) 2)<< 60);
8479 		}
8480 	}
8481 	fcp->isp_wwpn_nvram = wwn;
8482 	if (IS_2200(isp) || IS_23XX(isp)) {
8483 		wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
8484 		if (wwn) {
8485 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
8486 			    (uint32_t) (wwn >> 32),
8487 			    (uint32_t) (wwn));
8488 			if ((wwn >> 60) == 0) {
8489 				wwn |= (((uint64_t) 2)<< 60);
8490 			}
8491 		} else {
8492 			wwn = fcp->isp_wwpn_nvram & ~((uint64_t) 0xfff << 48);
8493 		}
8494 	} else {
8495 		wwn &= ~((uint64_t) 0xfff << 48);
8496 	}
8497 	fcp->isp_wwnn_nvram = wwn;
8498 
8499 	fcp->isp_maxalloc = ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
8500 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8501 		DEFAULT_FRAMESIZE(isp) =
8502 		    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
8503 	}
8504 	fcp->isp_retry_delay = ISP2100_NVRAM_RETRY_DELAY(nvram_data);
8505 	fcp->isp_retry_count = ISP2100_NVRAM_RETRY_COUNT(nvram_data);
8506 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8507 		fcp->isp_loopid = ISP2100_NVRAM_HARDLOOPID(nvram_data);
8508 	}
8509 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8510 		DEFAULT_EXEC_THROTTLE(isp) =
8511 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
8512 	}
8513 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
8514 	isp_prt(isp, ISP_LOGDEBUG0,
8515 	    "NVRAM 0x%08x%08x 0x%08x%08x maxalloc %d maxframelen %d",
8516 	    (uint32_t) (fcp->isp_wwnn_nvram >> 32),
8517 	    (uint32_t) fcp->isp_wwnn_nvram,
8518 	    (uint32_t) (fcp->isp_wwpn_nvram >> 32),
8519 	    (uint32_t) fcp->isp_wwpn_nvram,
8520 	    ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data),
8521 	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data));
8522 	isp_prt(isp, ISP_LOGDEBUG0,
8523 	    "execthrottle %d fwoptions 0x%x hardloop %d tov %d",
8524 	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
8525 	    ISP2100_NVRAM_OPTIONS(nvram_data),
8526 	    ISP2100_NVRAM_HARDLOOPID(nvram_data),
8527 	    ISP2100_NVRAM_TOV(nvram_data));
8528 	fcp->isp_xfwoptions = ISP2100_XFW_OPTIONS(nvram_data);
8529 	fcp->isp_zfwoptions = ISP2100_ZFW_OPTIONS(nvram_data);
8530 	isp_prt(isp, ISP_LOGDEBUG0, "xfwoptions 0x%x zfw options 0x%x",
8531 	    ISP2100_XFW_OPTIONS(nvram_data), ISP2100_ZFW_OPTIONS(nvram_data));
8532 }
8533 
8534 static void
8535 isp_parse_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
8536 {
8537 	fcparam *fcp = FCPARAM(isp, 0);
8538 	uint64_t wwn;
8539 
8540 	isp_prt(isp, ISP_LOGDEBUG0,
8541 	    "NVRAM 0x%08x%08x 0x%08x%08x exchg_cnt %d maxframelen %d",
8542 	    (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data) >> 32),
8543 	    (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data)),
8544 	    (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data) >> 32),
8545 	    (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data)),
8546 	    ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data),
8547 	    ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data));
8548 	isp_prt(isp, ISP_LOGDEBUG0,
8549 	    "NVRAM execthr %d loopid %d fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x",
8550 	    ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data),
8551 	    ISP2400_NVRAM_HARDLOOPID(nvram_data),
8552 	    ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data),
8553 	    ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data),
8554 	    ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data));
8555 
8556 	wwn = ISP2400_NVRAM_PORT_NAME(nvram_data);
8557 	fcp->isp_wwpn_nvram = wwn;
8558 
8559 	wwn = ISP2400_NVRAM_NODE_NAME(nvram_data);
8560 	if (wwn) {
8561 		if ((wwn >> 60) != 2 && (wwn >> 60) != 5) {
8562 			wwn = 0;
8563 		}
8564 	}
8565 	if (wwn == 0 && (fcp->isp_wwpn_nvram >> 60) == 2) {
8566 		wwn = fcp->isp_wwpn_nvram;
8567 		wwn &= ~((uint64_t) 0xfff << 48);
8568 	}
8569 	fcp->isp_wwnn_nvram = wwn;
8570 
8571 	if (ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data)) {
8572 		fcp->isp_maxalloc = ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data);
8573 	}
8574 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8575 		DEFAULT_FRAMESIZE(isp) =
8576 		    ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data);
8577 	}
8578 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8579 		fcp->isp_loopid = ISP2400_NVRAM_HARDLOOPID(nvram_data);
8580 	}
8581 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8582 		DEFAULT_EXEC_THROTTLE(isp) =
8583 			ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data);
8584 	}
8585 	fcp->isp_fwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data);
8586 	fcp->isp_xfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data);
8587 	fcp->isp_zfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data);
8588 }
8589