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