xref: /freebsd/sys/dev/isp/isp.c (revision e24ce5adf48fc29d4d2eecbbfa7722e0ac7fd393)
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 if (IS_SCSI(isp)) {
1093 #ifndef	ISP_TARGET_MODE
1094 		isp->isp_fwattr = ISP_FW_ATTR_TMODE;
1095 #else
1096 		isp->isp_fwattr = 0;
1097 #endif
1098 	}
1099 
1100 	isp_prt(isp, ISP_LOGCONFIG, "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
1101 	    btype, isp->isp_revision, dodnld? "loaded" : "resident", isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
1102 
1103 	fwt = isp->isp_fwattr;
1104 	if (IS_24XX(isp)) {
1105 		buf = FCPARAM(isp, 0)->isp_scratch;
1106 		ISP_SNPRINTF(buf, ISP_FC_SCRLEN, "Attributes:");
1107 		if (fwt & ISP2400_FW_ATTR_CLASS2) {
1108 			fwt ^=ISP2400_FW_ATTR_CLASS2;
1109 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Class2", buf);
1110 		}
1111 		if (fwt & ISP2400_FW_ATTR_IP) {
1112 			fwt ^=ISP2400_FW_ATTR_IP;
1113 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s IP", buf);
1114 		}
1115 		if (fwt & ISP2400_FW_ATTR_MULTIID) {
1116 			fwt ^=ISP2400_FW_ATTR_MULTIID;
1117 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MultiID", buf);
1118 		}
1119 		if (fwt & ISP2400_FW_ATTR_SB2) {
1120 			fwt ^=ISP2400_FW_ATTR_SB2;
1121 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SB2", buf);
1122 		}
1123 		if (fwt & ISP2400_FW_ATTR_T10CRC) {
1124 			fwt ^=ISP2400_FW_ATTR_T10CRC;
1125 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s T10CRC", buf);
1126 		}
1127 		if (fwt & ISP2400_FW_ATTR_VI) {
1128 			fwt ^=ISP2400_FW_ATTR_VI;
1129 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI", buf);
1130 		}
1131 		if (fwt & ISP2400_FW_ATTR_MQ) {
1132 			fwt ^=ISP2400_FW_ATTR_MQ;
1133 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MQ", buf);
1134 		}
1135 		if (fwt & ISP2400_FW_ATTR_MSIX) {
1136 			fwt ^=ISP2400_FW_ATTR_MSIX;
1137 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MSIX", buf);
1138 		}
1139 		if (fwt & ISP2400_FW_ATTR_FCOE) {
1140 			fwt ^=ISP2400_FW_ATTR_FCOE;
1141 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s FCOE", buf);
1142 		}
1143 		if (fwt & ISP2400_FW_ATTR_VP0) {
1144 			fwt ^= ISP2400_FW_ATTR_VP0;
1145 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VP0_Decoupling", buf);
1146 		}
1147 		if (fwt & ISP2400_FW_ATTR_EXPFW) {
1148 			fwt ^= ISP2400_FW_ATTR_EXPFW;
1149 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (Experimental)", buf);
1150 		}
1151 		if (fwt & ISP2400_FW_ATTR_HOTFW) {
1152 			fwt ^= ISP2400_FW_ATTR_HOTFW;
1153 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s HotFW", buf);
1154 		}
1155 		fwt &= ~ISP2400_FW_ATTR_EXTNDED;
1156 		if (fwt & ISP2400_FW_ATTR_EXTVP) {
1157 			fwt ^= ISP2400_FW_ATTR_EXTVP;
1158 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ExtVP", buf);
1159 		}
1160 		if (fwt & ISP2400_FW_ATTR_VN2VN) {
1161 			fwt ^= ISP2400_FW_ATTR_VN2VN;
1162 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VN2VN", buf);
1163 		}
1164 		if (fwt & ISP2400_FW_ATTR_EXMOFF) {
1165 			fwt ^= ISP2400_FW_ATTR_EXMOFF;
1166 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s EXMOFF", buf);
1167 		}
1168 		if (fwt & ISP2400_FW_ATTR_NPMOFF) {
1169 			fwt ^= ISP2400_FW_ATTR_NPMOFF;
1170 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s NPMOFF", buf);
1171 		}
1172 		if (fwt & ISP2400_FW_ATTR_DIFCHOP) {
1173 			fwt ^= ISP2400_FW_ATTR_DIFCHOP;
1174 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s DIFCHOP", buf);
1175 		}
1176 		if (fwt & ISP2400_FW_ATTR_SRIOV) {
1177 			fwt ^= ISP2400_FW_ATTR_SRIOV;
1178 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SRIOV", buf);
1179 		}
1180 		if (fwt & ISP2400_FW_ATTR_ASICTMP) {
1181 			fwt ^= ISP2400_FW_ATTR_ASICTMP;
1182 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ASICTMP", buf);
1183 		}
1184 		if (fwt & ISP2400_FW_ATTR_ATIOMQ) {
1185 			fwt ^= ISP2400_FW_ATTR_ATIOMQ;
1186 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ATIOMQ", buf);
1187 		}
1188 		if (fwt) {
1189 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (unknown 0x%08x%08x)", buf,
1190 			    (uint32_t) (fwt >> 32), (uint32_t) fwt);
1191 		}
1192 		isp_prt(isp, ISP_LOGCONFIG, "%s", buf);
1193 	} else if (IS_FC(isp)) {
1194 		buf = FCPARAM(isp, 0)->isp_scratch;
1195 		ISP_SNPRINTF(buf, ISP_FC_SCRLEN, "Attributes:");
1196 		if (fwt & ISP_FW_ATTR_TMODE) {
1197 			fwt ^=ISP_FW_ATTR_TMODE;
1198 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s TargetMode", buf);
1199 		}
1200 		if (fwt & ISP_FW_ATTR_SCCLUN) {
1201 			fwt ^=ISP_FW_ATTR_SCCLUN;
1202 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SCC-Lun", buf);
1203 		}
1204 		if (fwt & ISP_FW_ATTR_FABRIC) {
1205 			fwt ^=ISP_FW_ATTR_FABRIC;
1206 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Fabric", buf);
1207 		}
1208 		if (fwt & ISP_FW_ATTR_CLASS2) {
1209 			fwt ^=ISP_FW_ATTR_CLASS2;
1210 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Class2", buf);
1211 		}
1212 		if (fwt & ISP_FW_ATTR_FCTAPE) {
1213 			fwt ^=ISP_FW_ATTR_FCTAPE;
1214 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s FC-Tape", buf);
1215 		}
1216 		if (fwt & ISP_FW_ATTR_IP) {
1217 			fwt ^=ISP_FW_ATTR_IP;
1218 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s IP", buf);
1219 		}
1220 		if (fwt & ISP_FW_ATTR_VI) {
1221 			fwt ^=ISP_FW_ATTR_VI;
1222 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI", buf);
1223 		}
1224 		if (fwt & ISP_FW_ATTR_VI_SOLARIS) {
1225 			fwt ^=ISP_FW_ATTR_VI_SOLARIS;
1226 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI_SOLARIS", buf);
1227 		}
1228 		if (fwt & ISP_FW_ATTR_2KLOGINS) {
1229 			fwt ^=ISP_FW_ATTR_2KLOGINS;
1230 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s 2K-Login", buf);
1231 		}
1232 		if (fwt != 0) {
1233 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (unknown 0x%08x%08x)", buf,
1234 			    (uint32_t) (fwt >> 32), (uint32_t) fwt);
1235 		}
1236 		isp_prt(isp, ISP_LOGCONFIG, "%s", buf);
1237 	}
1238 
1239 	if (IS_24XX(isp)) {
1240 		MBSINIT(&mbs, MBOX_GET_RESOURCE_COUNT, MBLOGALL, 0);
1241 		isp_mboxcmd(isp, &mbs);
1242 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1243 			ISP_RESET0(isp);
1244 			return;
1245 		}
1246 		if (isp->isp_maxcmds >= mbs.param[3]) {
1247 			isp->isp_maxcmds = mbs.param[3];
1248 		}
1249 	} else {
1250 		MBSINIT(&mbs, MBOX_GET_FIRMWARE_STATUS, MBLOGALL, 0);
1251 		isp_mboxcmd(isp, &mbs);
1252 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1253 			ISP_RESET0(isp);
1254 			return;
1255 		}
1256 		if (isp->isp_maxcmds >= mbs.param[2]) {
1257 			isp->isp_maxcmds = mbs.param[2];
1258 		}
1259 	}
1260 	isp_prt(isp, ISP_LOGCONFIG, "%d max I/O command limit set", isp->isp_maxcmds);
1261 
1262 	/*
1263 	 * If we don't have Multi-ID f/w loaded, we need to restrict channels to one.
1264 	 * Only make this check for non-SCSI cards (I'm not sure firmware attributes
1265 	 * work for them).
1266 	 */
1267 	if (IS_FC(isp) && isp->isp_nchan > 1) {
1268 		if (!ISP_CAP_MULTI_ID(isp)) {
1269 			isp_prt(isp, ISP_LOGWARN, "non-MULTIID f/w loaded, "
1270 			    "only can enable 1 of %d channels", isp->isp_nchan);
1271 			isp->isp_nchan = 1;
1272 		} else if (!ISP_CAP_VP0(isp)) {
1273 			isp_prt(isp, ISP_LOGWARN, "We can not use MULTIID "
1274 			    "feature properly without VP0_Decoupling");
1275 			isp->isp_nchan = 1;
1276 		}
1277 	}
1278 	if (IS_FC(isp)) {
1279 		for (i = 0; i < isp->isp_nchan; i++)
1280 			isp_change_fw_state(isp, i, FW_CONFIG_WAIT);
1281 	}
1282 	if (isp->isp_dead) {
1283 		isp_shutdown(isp);
1284 		ISP_DISABLE_INTS(isp);
1285 		return;
1286 	}
1287 
1288 	isp->isp_state = ISP_RESETSTATE;
1289 
1290 	/*
1291 	 * Okay- now that we have new firmware running, we now (re)set our
1292 	 * notion of how many luns we support. This is somewhat tricky because
1293 	 * if we haven't loaded firmware, we sometimes do not have an easy way
1294 	 * of knowing how many luns we support.
1295 	 *
1296 	 * Expanded lun firmware gives you 32 luns for SCSI cards and
1297 	 * 16384 luns for Fibre Channel cards.
1298 	 *
1299 	 * It turns out that even for QLogic 2100s with ROM 1.10 and above
1300 	 * we do get a firmware attributes word returned in mailbox register 6.
1301 	 *
1302 	 * Because the lun is in a different position in the Request Queue
1303 	 * Entry structure for Fibre Channel with expanded lun firmware, we
1304 	 * can only support one lun (lun zero) when we don't know what kind
1305 	 * of firmware we're running.
1306 	 */
1307 	if (IS_SCSI(isp)) {
1308 		if (dodnld) {
1309 			if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
1310 				isp->isp_maxluns = 32;
1311 			} else {
1312 				isp->isp_maxluns = 8;
1313 			}
1314 		} else {
1315 			isp->isp_maxluns = 8;
1316 		}
1317 	} else {
1318 		if (ISP_CAP_SCCFW(isp)) {
1319 			isp->isp_maxluns = 0;	/* No limit -- 2/8 bytes */
1320 		} else {
1321 			isp->isp_maxluns = 16;
1322 		}
1323 	}
1324 
1325 	/*
1326 	 * We get some default values established. As a side
1327 	 * effect, NVRAM is read here (unless overriden by
1328 	 * a configuration flag).
1329 	 */
1330 	if (do_load_defaults) {
1331 		if (IS_SCSI(isp)) {
1332 			isp_setdfltsdparm(isp);
1333 		} else {
1334 			for (i = 0; i < isp->isp_nchan; i++) {
1335 				isp_setdfltfcparm(isp, i);
1336 			}
1337 		}
1338 	}
1339 }
1340 
1341 /*
1342  * Clean firmware shutdown.
1343  */
1344 static int
1345 isp_deinit(ispsoftc_t *isp)
1346 {
1347 	mbreg_t mbs;
1348 
1349 	isp->isp_state = ISP_NILSTATE;
1350 	MBSINIT(&mbs, MBOX_STOP_FIRMWARE, MBLOGALL, 500000);
1351 	mbs.param[1] = 0;
1352 	mbs.param[2] = 0;
1353 	mbs.param[3] = 0;
1354 	mbs.param[4] = 0;
1355 	mbs.param[5] = 0;
1356 	mbs.param[6] = 0;
1357 	mbs.param[7] = 0;
1358 	mbs.param[8] = 0;
1359 	isp_mboxcmd(isp, &mbs);
1360 	return (mbs.param[0] == MBOX_COMMAND_COMPLETE ? 0 : mbs.param[0]);
1361 }
1362 
1363 /*
1364  * Initialize Parameters of Hardware to a known state.
1365  *
1366  * Locks are held before coming here.
1367  */
1368 void
1369 isp_init(ispsoftc_t *isp)
1370 {
1371 	if (IS_FC(isp)) {
1372 		if (IS_24XX(isp)) {
1373 			isp_fibre_init_2400(isp);
1374 		} else {
1375 			isp_fibre_init(isp);
1376 		}
1377 	} else {
1378 		isp_scsi_init(isp);
1379 	}
1380 	GET_NANOTIME(&isp->isp_init_time);
1381 }
1382 
1383 static void
1384 isp_scsi_init(ispsoftc_t *isp)
1385 {
1386 	sdparam *sdp_chan0, *sdp_chan1;
1387 	mbreg_t mbs;
1388 
1389 	isp->isp_state = ISP_INITSTATE;
1390 
1391 	sdp_chan0 = SDPARAM(isp, 0);
1392 	sdp_chan1 = sdp_chan0;
1393 	if (IS_DUALBUS(isp)) {
1394 		sdp_chan1 = SDPARAM(isp, 1);
1395 	}
1396 
1397 	/* First do overall per-card settings. */
1398 
1399 	/*
1400 	 * If we have fast memory timing enabled, turn it on.
1401 	 */
1402 	if (sdp_chan0->isp_fast_mttr) {
1403 		ISP_WRITE(isp, RISC_MTR, 0x1313);
1404 	}
1405 
1406 	/*
1407 	 * Set Retry Delay and Count.
1408 	 * You set both channels at the same time.
1409 	 */
1410 	MBSINIT(&mbs, MBOX_SET_RETRY_COUNT, MBLOGALL, 0);
1411 	mbs.param[1] = sdp_chan0->isp_retry_count;
1412 	mbs.param[2] = sdp_chan0->isp_retry_delay;
1413 	mbs.param[6] = sdp_chan1->isp_retry_count;
1414 	mbs.param[7] = sdp_chan1->isp_retry_delay;
1415 	isp_mboxcmd(isp, &mbs);
1416 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1417 		return;
1418 	}
1419 
1420 	/*
1421 	 * Set ASYNC DATA SETUP time. This is very important.
1422 	 */
1423 	MBSINIT(&mbs, MBOX_SET_ASYNC_DATA_SETUP_TIME, MBLOGALL, 0);
1424 	mbs.param[1] = sdp_chan0->isp_async_data_setup;
1425 	mbs.param[2] = sdp_chan1->isp_async_data_setup;
1426 	isp_mboxcmd(isp, &mbs);
1427 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1428 		return;
1429 	}
1430 
1431 	/*
1432 	 * Set ACTIVE Negation State.
1433 	 */
1434 	MBSINIT(&mbs, MBOX_SET_ACT_NEG_STATE, MBLOGNONE, 0);
1435 	mbs.param[1] =
1436 	    (sdp_chan0->isp_req_ack_active_neg << 4) |
1437 	    (sdp_chan0->isp_data_line_active_neg << 5);
1438 	mbs.param[2] =
1439 	    (sdp_chan1->isp_req_ack_active_neg << 4) |
1440 	    (sdp_chan1->isp_data_line_active_neg << 5);
1441 	isp_mboxcmd(isp, &mbs);
1442 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1443 		isp_prt(isp, ISP_LOGERR,
1444 		    "failed to set active negation state (%d,%d), (%d,%d)",
1445 		    sdp_chan0->isp_req_ack_active_neg,
1446 		    sdp_chan0->isp_data_line_active_neg,
1447 		    sdp_chan1->isp_req_ack_active_neg,
1448 		    sdp_chan1->isp_data_line_active_neg);
1449 		/*
1450 		 * But don't return.
1451 		 */
1452 	}
1453 
1454 	/*
1455 	 * Set the Tag Aging limit
1456 	 */
1457 	MBSINIT(&mbs, MBOX_SET_TAG_AGE_LIMIT, MBLOGALL, 0);
1458 	mbs.param[1] = sdp_chan0->isp_tag_aging;
1459 	mbs.param[2] = sdp_chan1->isp_tag_aging;
1460 	isp_mboxcmd(isp, &mbs);
1461 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1462 		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
1463 		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
1464 		return;
1465 	}
1466 
1467 	/*
1468 	 * Set selection timeout.
1469 	 */
1470 	MBSINIT(&mbs, MBOX_SET_SELECT_TIMEOUT, MBLOGALL, 0);
1471 	mbs.param[1] = sdp_chan0->isp_selection_timeout;
1472 	mbs.param[2] = sdp_chan1->isp_selection_timeout;
1473 	isp_mboxcmd(isp, &mbs);
1474 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1475 		return;
1476 	}
1477 
1478 	/* now do per-channel settings */
1479 	isp_scsi_channel_init(isp, 0);
1480 	if (IS_DUALBUS(isp))
1481 		isp_scsi_channel_init(isp, 1);
1482 
1483 	/*
1484 	 * Now enable request/response queues
1485 	 */
1486 
1487 	if (IS_ULTRA2(isp) || IS_1240(isp)) {
1488 		MBSINIT(&mbs, MBOX_INIT_RES_QUEUE_A64, MBLOGALL, 0);
1489 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
1490 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1491 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1492 		mbs.param[4] = 0;
1493 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1494 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1495 		isp_mboxcmd(isp, &mbs);
1496 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1497 			return;
1498 		}
1499 		isp->isp_residx = isp->isp_resodx = mbs.param[5];
1500 
1501 		MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE_A64, MBLOGALL, 0);
1502 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1503 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1504 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1505 		mbs.param[5] = 0;
1506 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1507 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1508 		isp_mboxcmd(isp, &mbs);
1509 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1510 			return;
1511 		}
1512 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1513 	} else {
1514 		MBSINIT(&mbs, MBOX_INIT_RES_QUEUE, MBLOGALL, 0);
1515 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
1516 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1517 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1518 		mbs.param[4] = 0;
1519 		isp_mboxcmd(isp, &mbs);
1520 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1521 			return;
1522 		}
1523 		isp->isp_residx = isp->isp_resodx = mbs.param[5];
1524 
1525 		MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE, MBLOGALL, 0);
1526 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1527 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1528 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1529 		mbs.param[5] = 0;
1530 		isp_mboxcmd(isp, &mbs);
1531 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1532 			return;
1533 		}
1534 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1535 	}
1536 
1537 	/*
1538 	 * Turn on LVD transitions for ULTRA2 or better and other features
1539 	 *
1540 	 * Now that we have 32 bit handles, don't do any fast posting
1541 	 * any more. For Ultra2/Ultra3 cards, we can turn on 32 bit RIO
1542 	 * operation or use fast posting. To be conservative, we'll only
1543 	 * do this for Ultra3 cards now because the other cards are so
1544 	 * rare for this author to find and test with.
1545 	 */
1546 
1547 	MBSINIT(&mbs, MBOX_SET_FW_FEATURES, MBLOGALL, 0);
1548 	if (IS_ULTRA2(isp))
1549 		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
1550 #ifdef	ISP_NO_RIO
1551 	if (IS_ULTRA3(isp))
1552 		mbs.param[1] |= FW_FEATURE_FAST_POST;
1553 #else
1554 	if (IS_ULTRA3(isp))
1555 		mbs.param[1] |= FW_FEATURE_RIO_32BIT;
1556 #endif
1557 	if (mbs.param[1] != 0) {
1558 		uint16_t sfeat = mbs.param[1];
1559 		isp_mboxcmd(isp, &mbs);
1560 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1561 			isp_prt(isp, ISP_LOGINFO,
1562 			    "Enabled FW features (0x%x)", sfeat);
1563 		}
1564 	}
1565 
1566 	isp->isp_state = ISP_RUNSTATE;
1567 }
1568 
1569 static void
1570 isp_scsi_channel_init(ispsoftc_t *isp, int chan)
1571 {
1572 	sdparam *sdp;
1573 	mbreg_t mbs;
1574 	int tgt;
1575 
1576 	sdp = SDPARAM(isp, chan);
1577 
1578 	/*
1579 	 * Set (possibly new) Initiator ID.
1580 	 */
1581 	MBSINIT(&mbs, MBOX_SET_INIT_SCSI_ID, MBLOGALL, 0);
1582 	mbs.param[1] = (chan << 7) | sdp->isp_initiator_id;
1583 	isp_mboxcmd(isp, &mbs);
1584 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1585 		return;
1586 	}
1587 	isp_prt(isp, ISP_LOGINFO, "Chan %d Initiator ID is %d",
1588 	    chan, sdp->isp_initiator_id);
1589 
1590 
1591 	/*
1592 	 * Set current per-target parameters to an initial safe minimum.
1593 	 */
1594 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1595 		int lun;
1596 		uint16_t sdf;
1597 
1598 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
1599 			continue;
1600 		}
1601 #ifndef	ISP_TARGET_MODE
1602 		sdf = sdp->isp_devparam[tgt].goal_flags;
1603 		sdf &= DPARM_SAFE_DFLT;
1604 		/*
1605 		 * It is not quite clear when this changed over so that
1606 		 * we could force narrow and async for 1000/1020 cards,
1607 		 * but assume that this is only the case for loaded
1608 		 * firmware.
1609 		 */
1610 		if (isp->isp_loaded_fw) {
1611 			sdf |= DPARM_NARROW | DPARM_ASYNC;
1612 		}
1613 #else
1614 		/*
1615 		 * The !$*!)$!$)* f/w uses the same index into some
1616 		 * internal table to decide how to respond to negotiations,
1617 		 * so if we've said "let's be safe" for ID X, and ID X
1618 		 * selects *us*, the negotiations will back to 'safe'
1619 		 * (as in narrow/async). What the f/w *should* do is
1620 		 * use the initiator id settings to decide how to respond.
1621 		 */
1622 		sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
1623 #endif
1624 		MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGNONE, 0);
1625 		mbs.param[1] = (chan << 15) | (tgt << 8);
1626 		mbs.param[2] = sdf;
1627 		if ((sdf & DPARM_SYNC) == 0) {
1628 			mbs.param[3] = 0;
1629 		} else {
1630 			mbs.param[3] =
1631 			    (sdp->isp_devparam[tgt].goal_offset << 8) |
1632 			    (sdp->isp_devparam[tgt].goal_period);
1633 		}
1634 		isp_prt(isp, ISP_LOGDEBUG0, "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
1635 		    chan, tgt, mbs.param[2], mbs.param[3] >> 8, mbs.param[3] & 0xff);
1636 		isp_mboxcmd(isp, &mbs);
1637 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1638 			sdf = DPARM_SAFE_DFLT;
1639 			MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGALL, 0);
1640 			mbs.param[1] = (tgt << 8) | (chan << 15);
1641 			mbs.param[2] = sdf;
1642 			mbs.param[3] = 0;
1643 			isp_mboxcmd(isp, &mbs);
1644 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1645 				continue;
1646 			}
1647 		}
1648 
1649 		/*
1650 		 * We don't update any information directly from the f/w
1651 		 * because we need to run at least one command to cause a
1652 		 * new state to be latched up. So, we just assume that we
1653 		 * converge to the values we just had set.
1654 		 *
1655 		 * Ensure that we don't believe tagged queuing is enabled yet.
1656 		 * It turns out that sometimes the ISP just ignores our
1657 		 * attempts to set parameters for devices that it hasn't
1658 		 * seen yet.
1659 		 */
1660 		sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
1661 		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
1662 			MBSINIT(&mbs, MBOX_SET_DEV_QUEUE_PARAMS, MBLOGALL, 0);
1663 			mbs.param[1] = (chan << 15) | (tgt << 8) | lun;
1664 			mbs.param[2] = sdp->isp_max_queue_depth;
1665 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
1666 			isp_mboxcmd(isp, &mbs);
1667 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1668 				break;
1669 			}
1670 		}
1671 	}
1672 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1673 		if (sdp->isp_devparam[tgt].dev_refresh) {
1674 			sdp->sendmarker = 1;
1675 			sdp->update = 1;
1676 			break;
1677 		}
1678 	}
1679 }
1680 
1681 /*
1682  * Fibre Channel specific initialization.
1683  */
1684 static void
1685 isp_fibre_init(ispsoftc_t *isp)
1686 {
1687 	fcparam *fcp;
1688 	isp_icb_t local, *icbp = &local;
1689 	mbreg_t mbs;
1690 
1691 	/*
1692 	 * We only support one channel on non-24XX cards
1693 	 */
1694 	fcp = FCPARAM(isp, 0);
1695 	if (fcp->role == ISP_ROLE_NONE)
1696 		return;
1697 
1698 	isp->isp_state = ISP_INITSTATE;
1699 	ISP_MEMZERO(icbp, sizeof (*icbp));
1700 	icbp->icb_version = ICB_VERSION1;
1701 	icbp->icb_fwoptions = fcp->isp_fwoptions;
1702 
1703 	/*
1704 	 * Firmware Options are either retrieved from NVRAM or
1705 	 * are patched elsewhere. We check them for sanity here
1706 	 * and make changes based on board revision, but otherwise
1707 	 * let others decide policy.
1708 	 */
1709 
1710 	/*
1711 	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1712 	 */
1713 	if (IS_2100(isp) && isp->isp_revision < 5) {
1714 		icbp->icb_fwoptions &= ~ICBOPT_FAIRNESS;
1715 	}
1716 
1717 	/*
1718 	 * We have to use FULL LOGIN even though it resets the loop too much
1719 	 * because otherwise port database entries don't get updated after
1720 	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1721 	 */
1722 	if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1723 		icbp->icb_fwoptions |= ICBOPT_FULL_LOGIN;
1724 	}
1725 
1726 	/*
1727 	 * Insist on Port Database Update Async notifications
1728 	 */
1729 	icbp->icb_fwoptions |= ICBOPT_PDBCHANGE_AE;
1730 
1731 	/*
1732 	 * Make sure that target role reflects into fwoptions.
1733 	 */
1734 	if (fcp->role & ISP_ROLE_TARGET) {
1735 		icbp->icb_fwoptions |= ICBOPT_TGT_ENABLE;
1736 	} else {
1737 		icbp->icb_fwoptions &= ~ICBOPT_TGT_ENABLE;
1738 	}
1739 
1740 	if (fcp->role & ISP_ROLE_INITIATOR) {
1741 		icbp->icb_fwoptions &= ~ICBOPT_INI_DISABLE;
1742 	} else {
1743 		icbp->icb_fwoptions |= ICBOPT_INI_DISABLE;
1744 	}
1745 
1746 	icbp->icb_maxfrmlen = DEFAULT_FRAMESIZE(isp);
1747 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN || icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1748 		isp_prt(isp, ISP_LOGERR, "bad frame length (%d) from NVRAM- using %d", DEFAULT_FRAMESIZE(isp), ICB_DFLT_FRMLEN);
1749 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1750 	}
1751 	icbp->icb_maxalloc = fcp->isp_maxalloc;
1752 	if (icbp->icb_maxalloc < 1) {
1753 		isp_prt(isp, ISP_LOGERR, "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1754 		icbp->icb_maxalloc = 16;
1755 	}
1756 	icbp->icb_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
1757 	if (icbp->icb_execthrottle < 1) {
1758 		isp_prt(isp, ISP_LOGERR, "bad execution throttle of %d- using %d", DEFAULT_EXEC_THROTTLE(isp), ICB_DFLT_THROTTLE);
1759 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1760 	}
1761 	icbp->icb_retry_delay = fcp->isp_retry_delay;
1762 	icbp->icb_retry_count = fcp->isp_retry_count;
1763 	if (fcp->isp_loopid < LOCAL_LOOP_LIM) {
1764 		icbp->icb_hardaddr = fcp->isp_loopid;
1765 		if (isp->isp_confopts & ISP_CFG_OWNLOOPID)
1766 			icbp->icb_fwoptions |= ICBOPT_HARD_ADDRESS;
1767 		else
1768 			icbp->icb_fwoptions |= ICBOPT_PREV_ADDRESS;
1769 	}
1770 
1771 	/*
1772 	 * Right now we just set extended options to prefer point-to-point
1773 	 * over loop based upon some soft config options.
1774 	 *
1775 	 * NB: for the 2300, ICBOPT_EXTENDED is required.
1776 	 */
1777 	if (IS_2100(isp)) {
1778 		/*
1779 		 * We can't have Fast Posting any more- we now
1780 		 * have 32 bit handles.
1781 		 */
1782 		icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1783 	} else if (IS_2200(isp) || IS_23XX(isp)) {
1784 		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1785 
1786 		icbp->icb_xfwoptions = fcp->isp_xfwoptions;
1787 
1788 		if (ISP_CAP_FCTAPE(isp)) {
1789 			if (isp->isp_confopts & ISP_CFG_NOFCTAPE)
1790 				icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
1791 
1792 			if (isp->isp_confopts & ISP_CFG_FCTAPE)
1793 				icbp->icb_xfwoptions |= ICBXOPT_FCTAPE;
1794 
1795 			if (icbp->icb_xfwoptions & ICBXOPT_FCTAPE) {
1796 				icbp->icb_fwoptions &= ~ICBOPT_FULL_LOGIN;	/* per documents */
1797 				icbp->icb_xfwoptions |= ICBXOPT_FCTAPE_CCQ|ICBXOPT_FCTAPE_CONFIRM;
1798 				FCPARAM(isp, 0)->fctape_enabled = 1;
1799 			} else {
1800 				FCPARAM(isp, 0)->fctape_enabled = 0;
1801 			}
1802 		} else {
1803 			icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
1804 			FCPARAM(isp, 0)->fctape_enabled = 0;
1805 		}
1806 
1807 		/*
1808 		 * Prefer or force Point-To-Point instead Loop?
1809 		 */
1810 		switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
1811 		case ISP_CFG_NPORT:
1812 			icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1813 			icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
1814 			break;
1815 		case ISP_CFG_NPORT_ONLY:
1816 			icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1817 			icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
1818 			break;
1819 		case ISP_CFG_LPORT_ONLY:
1820 			icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1821 			icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
1822 			break;
1823 		default:
1824 			/*
1825 			 * Let NVRAM settings define it if they are sane
1826 			 */
1827 			switch (icbp->icb_xfwoptions & ICBXOPT_TOPO_MASK) {
1828 			case ICBXOPT_PTP_2_LOOP:
1829 			case ICBXOPT_PTP_ONLY:
1830 			case ICBXOPT_LOOP_ONLY:
1831 			case ICBXOPT_LOOP_2_PTP:
1832 				break;
1833 			default:
1834 				icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1835 				icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
1836 			}
1837 			break;
1838 		}
1839 		if (IS_2200(isp)) {
1840 			/*
1841 			 * We can't have Fast Posting any more- we now
1842 			 * have 32 bit handles.
1843 			 *
1844 			 * RIO seemed to have to much breakage.
1845 			 *
1846 			 * Just opt for safety.
1847 			 */
1848 			icbp->icb_xfwoptions &= ~ICBXOPT_RIO_16BIT;
1849 			icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1850 		} else {
1851 			/*
1852 			 * QLogic recommends that FAST Posting be turned
1853 			 * off for 23XX cards and instead allow the HBA
1854 			 * to write response queue entries and interrupt
1855 			 * after a delay (ZIO).
1856 			 */
1857 			icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1858 			if ((fcp->isp_xfwoptions & ICBXOPT_TIMER_MASK) == ICBXOPT_ZIO) {
1859 				icbp->icb_xfwoptions |= ICBXOPT_ZIO;
1860 				icbp->icb_idelaytimer = 10;
1861 			}
1862 			icbp->icb_zfwoptions = fcp->isp_zfwoptions;
1863 			if (isp->isp_confopts & ISP_CFG_ONEGB) {
1864 				icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1865 				icbp->icb_zfwoptions |= ICBZOPT_RATE_ONEGB;
1866 			} else if (isp->isp_confopts & ISP_CFG_TWOGB) {
1867 				icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1868 				icbp->icb_zfwoptions |= ICBZOPT_RATE_TWOGB;
1869 			} else {
1870 				switch (icbp->icb_zfwoptions & ICBZOPT_RATE_MASK) {
1871 				case ICBZOPT_RATE_ONEGB:
1872 				case ICBZOPT_RATE_TWOGB:
1873 				case ICBZOPT_RATE_AUTO:
1874 					break;
1875 				default:
1876 					icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1877 					icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
1878 					break;
1879 				}
1880 			}
1881 		}
1882 	}
1883 
1884 
1885 	/*
1886 	 * For 22XX > 2.1.26 && 23XX, set some options.
1887 	 */
1888 	if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
1889 		MBSINIT(&mbs, MBOX_SET_FIRMWARE_OPTIONS, MBLOGALL, 0);
1890 		mbs.param[1] = IFCOPT1_DISF7SWTCH|IFCOPT1_LIPASYNC|IFCOPT1_LIPF8;
1891 		mbs.param[2] = 0;
1892 		mbs.param[3] = 0;
1893 		if (ISP_FW_NEWER_THAN(isp, 3, 16, 0)) {
1894 			mbs.param[1] |= IFCOPT1_EQFQASYNC|IFCOPT1_CTIO_RETRY;
1895 			if (fcp->role & ISP_ROLE_TARGET) {
1896 				if (ISP_FW_NEWER_THAN(isp, 3, 25, 0)) {
1897 					mbs.param[1] |= IFCOPT1_ENAPURE;
1898 				}
1899 				mbs.param[3] = IFCOPT3_NOPRLI;
1900 			}
1901 		}
1902 		isp_mboxcmd(isp, &mbs);
1903 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1904 			return;
1905 		}
1906 	}
1907 	icbp->icb_logintime = ICB_LOGIN_TOV;
1908 
1909 #ifdef	ISP_TARGET_MODE
1910 	if (IS_23XX(isp) && (icbp->icb_fwoptions & ICBOPT_TGT_ENABLE)) {
1911 		icbp->icb_lunenables = 0xffff;
1912 		icbp->icb_ccnt = DFLT_CMND_CNT;
1913 		icbp->icb_icnt = DFLT_INOT_CNT;
1914 		icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
1915 	}
1916 #endif
1917 	if (fcp->isp_wwnn && fcp->isp_wwpn) {
1918 		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
1919 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
1920 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1921 		isp_prt(isp, ISP_LOGDEBUG1,
1922 		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1923 		    ((uint32_t) (fcp->isp_wwnn >> 32)),
1924 		    ((uint32_t) (fcp->isp_wwnn)),
1925 		    ((uint32_t) (fcp->isp_wwpn >> 32)),
1926 		    ((uint32_t) (fcp->isp_wwpn)));
1927 	} else if (fcp->isp_wwpn) {
1928 		icbp->icb_fwoptions &= ~ICBOPT_BOTH_WWNS;
1929 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1930 		isp_prt(isp, ISP_LOGDEBUG1,
1931 		    "Setting ICB Port 0x%08x%08x",
1932 		    ((uint32_t) (fcp->isp_wwpn >> 32)),
1933 		    ((uint32_t) (fcp->isp_wwpn)));
1934 	} else {
1935 		isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
1936 		return;
1937 	}
1938 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1939 	if (icbp->icb_rqstqlen < 1) {
1940 		isp_prt(isp, ISP_LOGERR, "bad request queue length");
1941 	}
1942 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1943 	if (icbp->icb_rsltqlen < 1) {
1944 		isp_prt(isp, ISP_LOGERR, "bad result queue length");
1945 	}
1946 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1947 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1948 	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1949 	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1950 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1951 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1952 	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1953 	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1954 
1955 	if (FC_SCRATCH_ACQUIRE(isp, 0)) {
1956 		isp_prt(isp, ISP_LOGERR, sacq);
1957 		return;
1958 	}
1959 	isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
1960 	    icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
1961 
1962 	isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
1963 
1964 	/*
1965 	 * Init the firmware
1966 	 */
1967 	MBSINIT(&mbs, MBOX_INIT_FIRMWARE, MBLOGALL, 30000000);
1968 	mbs.param[1] = 0;
1969 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1970 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1971 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1972 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1973 	mbs.logval = MBLOGALL;
1974 	isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %p (%08x%08x)",
1975 	    fcp->isp_scratch, (uint32_t) ((uint64_t)fcp->isp_scdma >> 32),
1976 	    (uint32_t) fcp->isp_scdma);
1977 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp), 0);
1978 	isp_mboxcmd(isp, &mbs);
1979 	FC_SCRATCH_RELEASE(isp, 0);
1980 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1981 		isp_print_bytes(isp, "isp_fibre_init", sizeof (*icbp), icbp);
1982 		return;
1983 	}
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 ((sdp->role & ISP_ROLE_INITIATOR) == 0) {
4148 			isp_prt(isp, ISP_LOGDEBUG1,
4149 			    "%d.%d.%jx I am not an initiator",
4150 			    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4151 			XS_SETERR(xs, HBA_SELTIMEOUT);
4152 			return (CMD_COMPLETE);
4153 		}
4154 
4155 		if (isp->isp_state != ISP_RUNSTATE) {
4156 			isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
4157 			XS_SETERR(xs, HBA_BOTCH);
4158 			return (CMD_COMPLETE);
4159 		}
4160 
4161 		if (sdp->update) {
4162 			isp_spi_update(isp, XS_CHANNEL(xs));
4163 		}
4164 		lp = NULL;
4165 	}
4166 
4167  start_again:
4168 
4169 	qep = isp_getrqentry(isp);
4170 	if (qep == NULL) {
4171 		isp_prt(isp, ISP_LOG_WARN1, "Request Queue Overflow");
4172 		XS_SETERR(xs, HBA_BOTCH);
4173 		return (CMD_EAGAIN);
4174 	}
4175 	XS_SETERR(xs, HBA_NOERROR);
4176 
4177 	/*
4178 	 * Now see if we need to synchronize the ISP with respect to anything.
4179 	 * We do dual duty here (cough) for synchronizing for busses other
4180 	 * than which we got here to send a command to.
4181 	 */
4182 	reqp = (ispreq_t *) local;
4183 	ISP_MEMZERO(local, QENTRY_LEN);
4184 	if (ISP_TST_SENDMARKER(isp, XS_CHANNEL(xs))) {
4185 		if (IS_24XX(isp)) {
4186 			isp_marker_24xx_t *m = (isp_marker_24xx_t *) reqp;
4187 			m->mrk_header.rqs_entry_count = 1;
4188 			m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4189 			m->mrk_modifier = SYNC_ALL;
4190 			m->mrk_vphdl = XS_CHANNEL(xs);
4191 			isp_put_marker_24xx(isp, m, qep);
4192 		} else {
4193 			isp_marker_t *m = (isp_marker_t *) reqp;
4194 			m->mrk_header.rqs_entry_count = 1;
4195 			m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4196 			m->mrk_target = (XS_CHANNEL(xs) << 7);	/* bus # */
4197 			m->mrk_modifier = SYNC_ALL;
4198 			isp_put_marker(isp, m, qep);
4199 		}
4200 		ISP_SYNC_REQUEST(isp);
4201 		ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 0);
4202 		goto start_again;
4203 	}
4204 
4205 	reqp->req_header.rqs_entry_count = 1;
4206 
4207 	/*
4208 	 * Select and install Header Code.
4209 	 * Note that it might be overridden before going out
4210 	 * if we're on a 64 bit platform. The lower level
4211 	 * code (isp_send_cmd) will select the appropriate
4212 	 * 64 bit variant if it needs to.
4213 	 */
4214 	if (IS_24XX(isp)) {
4215 		reqp->req_header.rqs_entry_type = RQSTYPE_T7RQS;
4216 	} else if (IS_FC(isp)) {
4217 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
4218 	} else {
4219 		if (XS_CDBLEN(xs) > 12) {
4220 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
4221 		} else {
4222 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
4223 		}
4224 	}
4225 
4226 	/*
4227 	 * Set task attributes
4228 	 */
4229 	if (IS_24XX(isp)) {
4230 		int ttype;
4231 		if (XS_TAG_P(xs)) {
4232 			ttype = XS_TAG_TYPE(xs);
4233 		} else {
4234 			if (XS_CDBP(xs)[0] == 0x3) {
4235 				ttype = REQFLAG_HTAG;
4236 			} else {
4237 				ttype = REQFLAG_STAG;
4238 			}
4239 		}
4240 		if (ttype == REQFLAG_OTAG) {
4241 			ttype = FCP_CMND_TASK_ATTR_ORDERED;
4242 		} else if (ttype == REQFLAG_HTAG) {
4243 			ttype = FCP_CMND_TASK_ATTR_HEAD;
4244 		} else {
4245 			ttype = FCP_CMND_TASK_ATTR_SIMPLE;
4246 		}
4247 		((ispreqt7_t *)reqp)->req_task_attribute = ttype;
4248 	} else if (IS_FC(isp)) {
4249 		/*
4250 		 * See comment in isp_intr
4251 		 */
4252 		/* XS_SET_RESID(xs, 0); */
4253 
4254 		/*
4255 		 * Fibre Channel always requires some kind of tag.
4256 		 * The Qlogic drivers seem be happy not to use a tag,
4257 		 * but this breaks for some devices (IBM drives).
4258 		 */
4259 		if (XS_TAG_P(xs)) {
4260 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
4261 		} else {
4262 			/*
4263 			 * If we don't know what tag to use, use HEAD OF QUEUE
4264 			 * for Request Sense or Simple.
4265 			 */
4266 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
4267 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
4268 			else
4269 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
4270 		}
4271 	} else {
4272 		sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
4273 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) && XS_TAG_P(xs)) {
4274 			reqp->req_flags = XS_TAG_TYPE(xs);
4275 		}
4276 	}
4277 
4278 	tptr = &reqp->req_time;
4279 
4280 	/*
4281 	 * NB: we do not support long CDBs (yet)
4282 	 */
4283 	cdblen = XS_CDBLEN(xs);
4284 
4285 	if (IS_SCSI(isp)) {
4286 		if (cdblen > sizeof (reqp->req_cdb)) {
4287 			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4288 			XS_SETERR(xs, HBA_BOTCH);
4289 			return (CMD_COMPLETE);
4290 		}
4291 		reqp->req_target = target | (XS_CHANNEL(xs) << 7);
4292 		reqp->req_lun_trn = XS_LUN(xs);
4293 		cdbp = reqp->req_cdb;
4294 		reqp->req_cdblen = cdblen;
4295 	} else if (IS_24XX(isp)) {
4296 		ispreqt7_t *t7 = (ispreqt7_t *)local;
4297 
4298 		if (cdblen > sizeof (t7->req_cdb)) {
4299 			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4300 			XS_SETERR(xs, HBA_BOTCH);
4301 			return (CMD_COMPLETE);
4302 		}
4303 
4304 		t7->req_nphdl = lp->handle;
4305 		t7->req_tidlo = lp->portid;
4306 		t7->req_tidhi = lp->portid >> 16;
4307 		t7->req_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(xs));
4308 #if __FreeBSD_version >= 1000700
4309 		be64enc(t7->req_lun, CAM_EXTLUN_BYTE_SWIZZLE(XS_LUN(xs)));
4310 #else
4311 		if (XS_LUN(xs) >= 256) {
4312 			t7->req_lun[0] = XS_LUN(xs) >> 8;
4313 			t7->req_lun[0] |= 0x40;
4314 		}
4315 		t7->req_lun[1] = XS_LUN(xs);
4316 #endif
4317 		if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4318 			if (FCP_NEXT_CRN(isp, &t7->req_crn, xs)) {
4319 				isp_prt(isp, ISP_LOG_WARN1,
4320 				    "%d.%d.%jx cannot generate next CRN",
4321 				    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4322 				XS_SETERR(xs, HBA_BOTCH);
4323 				return (CMD_EAGAIN);
4324 			}
4325 		}
4326 		tptr = &t7->req_time;
4327 		cdbp = t7->req_cdb;
4328 	} else {
4329 		ispreqt2_t *t2 = (ispreqt2_t *)local;
4330 
4331 		if (cdblen > sizeof t2->req_cdb) {
4332 			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4333 			XS_SETERR(xs, HBA_BOTCH);
4334 			return (CMD_COMPLETE);
4335 		}
4336 		if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4337 			if (FCP_NEXT_CRN(isp, &t2->req_crn, xs)) {
4338 				isp_prt(isp, ISP_LOG_WARN1,
4339 				    "%d.%d.%jx cannot generate next CRN",
4340 				    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4341 				XS_SETERR(xs, HBA_BOTCH);
4342 				return (CMD_EAGAIN);
4343 			}
4344 		}
4345 		if (ISP_CAP_2KLOGIN(isp)) {
4346 			ispreqt2e_t *t2e = (ispreqt2e_t *)local;
4347 			t2e->req_target = lp->handle;
4348 			t2e->req_scclun = XS_LUN(xs);
4349 #if __FreeBSD_version < 1000700
4350 			if (XS_LUN(xs) >= 256)
4351 				t2e->req_scclun |= 0x4000;
4352 #endif
4353 			cdbp = t2e->req_cdb;
4354 		} else if (ISP_CAP_SCCFW(isp)) {
4355 			ispreqt2_t *t2 = (ispreqt2_t *)local;
4356 			t2->req_target = lp->handle;
4357 			t2->req_scclun = XS_LUN(xs);
4358 #if __FreeBSD_version < 1000700
4359 			if (XS_LUN(xs) >= 256)
4360 				t2->req_scclun |= 0x4000;
4361 #endif
4362 			cdbp = t2->req_cdb;
4363 		} else {
4364 			t2->req_target = lp->handle;
4365 			t2->req_lun_trn = XS_LUN(xs);
4366 			cdbp = t2->req_cdb;
4367 		}
4368 	}
4369 	ISP_MEMCPY(cdbp, XS_CDBP(xs), cdblen);
4370 
4371 	*tptr = XS_TIME(xs) / 1000;
4372 	if (*tptr == 0 && XS_TIME(xs)) {
4373 		*tptr = 1;
4374 	}
4375 	if (IS_24XX(isp) && *tptr > 0x1999) {
4376 		*tptr = 0x1999;
4377 	}
4378 
4379 	if (isp_allocate_xs(isp, xs, &handle)) {
4380 		isp_prt(isp, ISP_LOG_WARN1, "out of xflist pointers");
4381 		XS_SETERR(xs, HBA_BOTCH);
4382 		return (CMD_EAGAIN);
4383 	}
4384 	/* Whew. Thankfully the same for type 7 requests */
4385 	reqp->req_handle = handle;
4386 
4387 	/*
4388 	 * Set up DMA and/or do any platform dependent swizzling of the request entry
4389 	 * so that the Qlogic F/W understands what is being asked of it.
4390 	 *
4391 	 * The callee is responsible for adding all requests at this point.
4392 	 */
4393 	dmaresult = ISP_DMASETUP(isp, xs, reqp);
4394 	if (dmaresult != CMD_QUEUED) {
4395 		isp_destroy_handle(isp, handle);
4396 		/*
4397 		 * dmasetup sets actual error in packet, and
4398 		 * return what we were given to return.
4399 		 */
4400 		return (dmaresult);
4401 	}
4402 	isp_xs_prt(isp, xs, ISP_LOGDEBUG0, "START cmd cdb[0]=0x%x datalen %ld", XS_CDBP(xs)[0], (long) XS_XFRLEN(xs));
4403 	isp->isp_nactive++;
4404 	return (CMD_QUEUED);
4405 }
4406 
4407 /*
4408  * isp control
4409  * Locks (ints blocked) assumed held.
4410  */
4411 
4412 int
4413 isp_control(ispsoftc_t *isp, ispctl_t ctl, ...)
4414 {
4415 	XS_T *xs;
4416 	mbreg_t *mbr, mbs;
4417 	int chan, tgt;
4418 	uint32_t handle;
4419 	va_list ap;
4420 
4421 	switch (ctl) {
4422 	case ISPCTL_RESET_BUS:
4423 		/*
4424 		 * Issue a bus reset.
4425 		 */
4426 		if (IS_24XX(isp)) {
4427 			isp_prt(isp, ISP_LOGERR, "BUS RESET NOT IMPLEMENTED");
4428 			break;
4429 		} else if (IS_FC(isp)) {
4430 			mbs.param[1] = 10;
4431 			chan = 0;
4432 		} else {
4433 			va_start(ap, ctl);
4434 			chan = va_arg(ap, int);
4435 			va_end(ap);
4436 			mbs.param[1] = SDPARAM(isp, chan)->isp_bus_reset_delay;
4437 			if (mbs.param[1] < 2) {
4438 				mbs.param[1] = 2;
4439 			}
4440 			mbs.param[2] = chan;
4441 		}
4442 		MBSINIT(&mbs, MBOX_BUS_RESET, MBLOGALL, 0);
4443 		ISP_SET_SENDMARKER(isp, chan, 1);
4444 		isp_mboxcmd(isp, &mbs);
4445 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4446 			break;
4447 		}
4448 		isp_prt(isp, ISP_LOGINFO, "driver initiated bus reset of bus %d", chan);
4449 		return (0);
4450 
4451 	case ISPCTL_RESET_DEV:
4452 		va_start(ap, ctl);
4453 		chan = va_arg(ap, int);
4454 		tgt = va_arg(ap, int);
4455 		va_end(ap);
4456 		if (IS_24XX(isp)) {
4457 			uint8_t local[QENTRY_LEN];
4458 			isp24xx_tmf_t *tmf;
4459 			isp24xx_statusreq_t *sp;
4460 			fcparam *fcp = FCPARAM(isp, chan);
4461 			fcportdb_t *lp;
4462 
4463 			if (tgt < 0 || tgt >= MAX_FC_TARG) {
4464 				isp_prt(isp, ISP_LOGWARN, "Chan %d trying to reset bad target %d", chan, tgt);
4465 				break;
4466 			}
4467 			lp = &fcp->portdb[tgt];
4468 			if (lp->is_target == 0 ||
4469 			    lp->state != FC_PORTDB_STATE_VALID) {
4470 				isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4471 				break;
4472 			}
4473 
4474 			tmf = (isp24xx_tmf_t *) local;
4475 			ISP_MEMZERO(tmf, QENTRY_LEN);
4476 			tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
4477 			tmf->tmf_header.rqs_entry_count = 1;
4478 			tmf->tmf_nphdl = lp->handle;
4479 			tmf->tmf_delay = 2;
4480 			tmf->tmf_timeout = 2;
4481 			tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
4482 			tmf->tmf_tidlo = lp->portid;
4483 			tmf->tmf_tidhi = lp->portid >> 16;
4484 			tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
4485 			isp_prt(isp, ISP_LOGALL, "Chan %d Reset N-Port Handle 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4486 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
4487 			mbs.param[1] = QENTRY_LEN;
4488 			mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4489 			mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4490 			mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4491 			mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4492 
4493 			if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4494 				isp_prt(isp, ISP_LOGERR, sacq);
4495 				break;
4496 			}
4497 			isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
4498 			MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
4499 			fcp->sendmarker = 1;
4500 			isp_mboxcmd(isp, &mbs);
4501 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4502 				FC_SCRATCH_RELEASE(isp, chan);
4503 				break;
4504 			}
4505 			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4506 			sp = (isp24xx_statusreq_t *) local;
4507 			isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
4508 			FC_SCRATCH_RELEASE(isp, chan);
4509 			if (sp->req_completion_status == 0) {
4510 				return (0);
4511 			}
4512 			isp_prt(isp, ISP_LOGWARN, "Chan %d reset of target %d returned 0x%x", chan, tgt, sp->req_completion_status);
4513 			break;
4514 		} else if (IS_FC(isp)) {
4515 			if (ISP_CAP_2KLOGIN(isp)) {
4516 				mbs.param[1] = tgt;
4517 				mbs.ibits = (1 << 10);
4518 			} else {
4519 				mbs.param[1] = (tgt << 8);
4520 			}
4521 		} else {
4522 			mbs.param[1] = (chan << 15) | (tgt << 8);
4523 		}
4524 		MBSINIT(&mbs, MBOX_ABORT_TARGET, MBLOGALL, 0);
4525 		mbs.param[2] = 3;	/* 'delay', in seconds */
4526 		isp_mboxcmd(isp, &mbs);
4527 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4528 			break;
4529 		}
4530 		isp_prt(isp, ISP_LOGINFO, "Target %d on Bus %d Reset Succeeded", tgt, chan);
4531 		ISP_SET_SENDMARKER(isp, chan, 1);
4532 		return (0);
4533 
4534 	case ISPCTL_ABORT_CMD:
4535 		va_start(ap, ctl);
4536 		xs = va_arg(ap, XS_T *);
4537 		va_end(ap);
4538 
4539 		tgt = XS_TGT(xs);
4540 		chan = XS_CHANNEL(xs);
4541 
4542 		handle = isp_find_handle(isp, xs);
4543 		if (handle == 0) {
4544 			isp_prt(isp, ISP_LOGWARN, "cannot find handle for command to abort");
4545 			break;
4546 		}
4547 		if (IS_24XX(isp)) {
4548 			isp24xx_abrt_t local, *ab = &local, *ab2;
4549 			fcparam *fcp;
4550 			fcportdb_t *lp;
4551 
4552 			fcp = FCPARAM(isp, chan);
4553 			if (tgt < 0 || tgt >= MAX_FC_TARG) {
4554 				isp_prt(isp, ISP_LOGWARN, "Chan %d trying to abort bad target %d", chan, tgt);
4555 				break;
4556 			}
4557 			lp = &fcp->portdb[tgt];
4558 			if (lp->is_target == 0 ||
4559 			    lp->state != FC_PORTDB_STATE_VALID) {
4560 				isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4561 				break;
4562 			}
4563 			isp_prt(isp, ISP_LOGALL, "Chan %d Abort Cmd for N-Port 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4564 			ISP_MEMZERO(ab, QENTRY_LEN);
4565 			ab->abrt_header.rqs_entry_type = RQSTYPE_ABORT_IO;
4566 			ab->abrt_header.rqs_entry_count = 1;
4567 			ab->abrt_handle = lp->handle;
4568 			ab->abrt_cmd_handle = handle;
4569 			ab->abrt_tidlo = lp->portid;
4570 			ab->abrt_tidhi = lp->portid >> 16;
4571 			ab->abrt_vpidx = ISP_GET_VPIDX(isp, chan);
4572 
4573 			ISP_MEMZERO(&mbs, sizeof (mbs));
4574 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
4575 			mbs.param[1] = QENTRY_LEN;
4576 			mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4577 			mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4578 			mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4579 			mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4580 
4581 			if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4582 				isp_prt(isp, ISP_LOGERR, sacq);
4583 				break;
4584 			}
4585 			isp_put_24xx_abrt(isp, ab, fcp->isp_scratch);
4586 			ab2 = (isp24xx_abrt_t *) &((uint8_t *)fcp->isp_scratch)[QENTRY_LEN];
4587 			ab2->abrt_nphdl = 0xdeaf;
4588 			MEMORYBARRIER(isp, SYNC_SFORDEV, 0, 2 * QENTRY_LEN, chan);
4589 			isp_mboxcmd(isp, &mbs);
4590 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4591 				FC_SCRATCH_RELEASE(isp, chan);
4592 				break;
4593 			}
4594 			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4595 			isp_get_24xx_abrt(isp, ab2, ab);
4596 			FC_SCRATCH_RELEASE(isp, chan);
4597 			if (ab->abrt_nphdl == ISP24XX_ABRT_OKAY) {
4598 				return (0);
4599 			}
4600 			isp_prt(isp, ISP_LOGWARN, "Chan %d handle %d abort returned 0x%x", chan, tgt, ab->abrt_nphdl);
4601 			break;
4602 		} else if (IS_FC(isp)) {
4603 			if (ISP_CAP_SCCFW(isp)) {
4604 				if (ISP_CAP_2KLOGIN(isp)) {
4605 					mbs.param[1] = tgt;
4606 				} else {
4607 					mbs.param[1] = tgt << 8;
4608 				}
4609 				mbs.param[6] = XS_LUN(xs);
4610 			} else {
4611 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
4612 			}
4613 		} else {
4614 			mbs.param[1] = (chan << 15) | (tgt << 8) | XS_LUN(xs);
4615 		}
4616 		MBSINIT(&mbs, MBOX_ABORT,
4617 		    MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_ERROR), 0);
4618 		mbs.param[2] = handle;
4619 		isp_mboxcmd(isp, &mbs);
4620 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4621 			break;
4622 		}
4623 		return (0);
4624 
4625 	case ISPCTL_UPDATE_PARAMS:
4626 
4627 		va_start(ap, ctl);
4628 		chan = va_arg(ap, int);
4629 		va_end(ap);
4630 		isp_spi_update(isp, chan);
4631 		return (0);
4632 
4633 	case ISPCTL_FCLINK_TEST:
4634 
4635 		if (IS_FC(isp)) {
4636 			int usdelay;
4637 			va_start(ap, ctl);
4638 			chan = va_arg(ap, int);
4639 			usdelay = va_arg(ap, int);
4640 			va_end(ap);
4641 			if (usdelay == 0) {
4642 				usdelay =  250000;
4643 			}
4644 			return (isp_fclink_test(isp, chan, usdelay));
4645 		}
4646 		break;
4647 
4648 	case ISPCTL_SCAN_FABRIC:
4649 
4650 		if (IS_FC(isp)) {
4651 			va_start(ap, ctl);
4652 			chan = va_arg(ap, int);
4653 			va_end(ap);
4654 			return (isp_scan_fabric(isp, chan));
4655 		}
4656 		break;
4657 
4658 	case ISPCTL_SCAN_LOOP:
4659 
4660 		if (IS_FC(isp)) {
4661 			va_start(ap, ctl);
4662 			chan = va_arg(ap, int);
4663 			va_end(ap);
4664 			return (isp_scan_loop(isp, chan));
4665 		}
4666 		break;
4667 
4668 	case ISPCTL_PDB_SYNC:
4669 
4670 		if (IS_FC(isp)) {
4671 			va_start(ap, ctl);
4672 			chan = va_arg(ap, int);
4673 			va_end(ap);
4674 			return (isp_pdb_sync(isp, chan));
4675 		}
4676 		break;
4677 
4678 	case ISPCTL_SEND_LIP:
4679 
4680 		if (IS_FC(isp) && !IS_24XX(isp)) {
4681 			MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
4682 			if (ISP_CAP_2KLOGIN(isp)) {
4683 				mbs.ibits = (1 << 10);
4684 			}
4685 			isp_mboxcmd(isp, &mbs);
4686 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
4687 				return (0);
4688 			}
4689 		}
4690 		break;
4691 
4692 	case ISPCTL_GET_PDB:
4693 		if (IS_FC(isp)) {
4694 			isp_pdb_t *pdb;
4695 			va_start(ap, ctl);
4696 			chan = va_arg(ap, int);
4697 			tgt = va_arg(ap, int);
4698 			pdb = va_arg(ap, isp_pdb_t *);
4699 			va_end(ap);
4700 			return (isp_getpdb(isp, chan, tgt, pdb, 1));
4701 		}
4702 		break;
4703 
4704 	case ISPCTL_GET_NAMES:
4705 	{
4706 		uint64_t *wwnn, *wwnp;
4707 		va_start(ap, ctl);
4708 		chan = va_arg(ap, int);
4709 		tgt = va_arg(ap, int);
4710 		wwnn = va_arg(ap, uint64_t *);
4711 		wwnp = va_arg(ap, uint64_t *);
4712 		va_end(ap);
4713 		if (wwnn == NULL && wwnp == NULL) {
4714 			break;
4715 		}
4716 		if (wwnn) {
4717 			*wwnn = isp_get_wwn(isp, chan, tgt, 1);
4718 			if (*wwnn == INI_NONE) {
4719 				break;
4720 			}
4721 		}
4722 		if (wwnp) {
4723 			*wwnp = isp_get_wwn(isp, chan, tgt, 0);
4724 			if (*wwnp == INI_NONE) {
4725 				break;
4726 			}
4727 		}
4728 		return (0);
4729 	}
4730 	case ISPCTL_RUN_MBOXCMD:
4731 	{
4732 		va_start(ap, ctl);
4733 		mbr = va_arg(ap, mbreg_t *);
4734 		va_end(ap);
4735 		isp_mboxcmd(isp, mbr);
4736 		return (0);
4737 	}
4738 	case ISPCTL_PLOGX:
4739 	{
4740 		isp_plcmd_t *p;
4741 		int r;
4742 
4743 		va_start(ap, ctl);
4744 		p = va_arg(ap, isp_plcmd_t *);
4745 		va_end(ap);
4746 
4747 		if ((p->flags & PLOGX_FLG_CMD_MASK) != PLOGX_FLG_CMD_PLOGI || (p->handle != NIL_HANDLE)) {
4748 			return (isp_plogx(isp, p->channel, p->handle, p->portid, p->flags, 0));
4749 		}
4750 		do {
4751 			isp_next_handle(isp, &p->handle);
4752 			r = isp_plogx(isp, p->channel, p->handle, p->portid, p->flags, 0);
4753 			if ((r & 0xffff) == MBOX_PORT_ID_USED) {
4754 				p->handle = r >> 16;
4755 				r = 0;
4756 				break;
4757 			}
4758 		} while ((r & 0xffff) == MBOX_LOOP_ID_USED);
4759 		return (r);
4760 	}
4761 	case ISPCTL_CHANGE_ROLE:
4762 	{
4763 		int role, r;
4764 
4765 		va_start(ap, ctl);
4766 		chan = va_arg(ap, int);
4767 		role = va_arg(ap, int);
4768 		va_end(ap);
4769 		if (IS_FC(isp)) {
4770 			r = isp_fc_change_role(isp, chan, role);
4771 		} else {
4772 			SDPARAM(isp, chan)->role = role;
4773 			r = 0;
4774 		}
4775 		return (r);
4776 	}
4777 	default:
4778 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
4779 		break;
4780 
4781 	}
4782 	return (-1);
4783 }
4784 
4785 /*
4786  * Interrupt Service Routine(s).
4787  *
4788  * External (OS) framework has done the appropriate locking,
4789  * and the locking will be held throughout this function.
4790  */
4791 
4792 /*
4793  * Limit our stack depth by sticking with the max likely number
4794  * of completions on a request queue at any one time.
4795  */
4796 #ifndef	MAX_REQUESTQ_COMPLETIONS
4797 #define	MAX_REQUESTQ_COMPLETIONS	32
4798 #endif
4799 
4800 void
4801 isp_intr(ispsoftc_t *isp, uint16_t isr, uint16_t sema, uint16_t info)
4802 {
4803 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
4804 	uint32_t iptr, optr, junk;
4805 	int i, nlooked = 0, ndone = 0, continuations_expected = 0;
4806 	int etype, last_etype = 0;
4807 
4808 again:
4809 	/*
4810 	 * Is this a mailbox related interrupt?
4811 	 * The mailbox semaphore will be nonzero if so.
4812 	 */
4813 	if (sema) {
4814  fmbox:
4815 		if (info & MBOX_COMMAND_COMPLETE) {
4816 			isp->isp_intmboxc++;
4817 			if (isp->isp_mboxbsy) {
4818 				int obits = isp->isp_obits;
4819 				isp->isp_mboxtmp[0] = info;
4820 				for (i = 1; i < ISP_NMBOX(isp); i++) {
4821 					if ((obits & (1 << i)) == 0) {
4822 						continue;
4823 					}
4824 					isp->isp_mboxtmp[i] = ISP_READ(isp, MBOX_OFF(i));
4825 				}
4826 				if (isp->isp_mbxwrk0) {
4827 					if (isp_mbox_continue(isp) == 0) {
4828 						return;
4829 					}
4830 				}
4831 				MBOX_NOTIFY_COMPLETE(isp);
4832 			} else {
4833 				isp_prt(isp, ISP_LOGWARN, "mailbox cmd (0x%x) with no waiters", info);
4834 			}
4835 		} else {
4836 			i = IS_FC(isp)? isp_parse_async_fc(isp, info) : isp_parse_async(isp, info);
4837 			if (i < 0) {
4838 				return;
4839 			}
4840 		}
4841 		if ((IS_FC(isp) && info != ASYNC_RIOZIO_STALL) || isp->isp_state != ISP_RUNSTATE) {
4842 			goto out;
4843 		}
4844 	}
4845 
4846 	/*
4847 	 * We can't be getting this now.
4848 	 */
4849 	if (isp->isp_state != ISP_RUNSTATE) {
4850 		/*
4851 		 * This seems to happen to 23XX and 24XX cards- don't know why.
4852 		 */
4853 		 if (isp->isp_mboxbsy && isp->isp_lastmbxcmd == MBOX_ABOUT_FIRMWARE) {
4854 			goto fmbox;
4855 		}
4856 		isp_prt(isp, ISP_LOGINFO, "interrupt (ISR=%x SEMA=%x INFO=%x) "
4857 		    "when not ready", isr, sema, info);
4858 		/*
4859 		 * Thank you very much!  *Burrrp*!
4860 		 */
4861 		isp->isp_residx = ISP_READ(isp, isp->isp_respinrp);
4862 		isp->isp_resodx = isp->isp_residx;
4863 		ISP_WRITE(isp, isp->isp_respoutrp, isp->isp_resodx);
4864 		if (IS_24XX(isp)) {
4865 			ISP_DISABLE_INTS(isp);
4866 		}
4867 		goto out;
4868 	}
4869 
4870 #ifdef	ISP_TARGET_MODE
4871 	/*
4872 	 * Check for ATIO Queue entries.
4873 	 */
4874 	if (IS_24XX(isp) &&
4875 	    (isr == ISPR2HST_ATIO_UPDATE || isr == ISPR2HST_ATIO_RSPQ_UPDATE ||
4876 	     isr == ISPR2HST_ATIO_UPDATE2)) {
4877 		iptr = ISP_READ(isp, BIU2400_ATIO_RSPINP);
4878 		optr = isp->isp_atioodx;
4879 
4880 		while (optr != iptr) {
4881 			uint8_t qe[QENTRY_LEN];
4882 			isphdr_t *hp;
4883 			uint32_t oop;
4884 			void *addr;
4885 
4886 			oop = optr;
4887 			MEMORYBARRIER(isp, SYNC_ATIOQ, oop, QENTRY_LEN, -1);
4888 			addr = ISP_QUEUE_ENTRY(isp->isp_atioq, oop);
4889 			isp_get_hdr(isp, addr, (isphdr_t *)qe);
4890 			hp = (isphdr_t *)qe;
4891 			switch (hp->rqs_entry_type) {
4892 			case RQSTYPE_NOTIFY:
4893 			case RQSTYPE_ATIO:
4894 				(void) isp_target_notify(isp, addr, &oop);
4895 				break;
4896 			default:
4897 				isp_print_qentry(isp, "?ATIOQ entry?", oop, addr);
4898 				break;
4899 			}
4900 			optr = ISP_NXT_QENTRY(oop, RESULT_QUEUE_LEN(isp));
4901 		}
4902 		if (isp->isp_atioodx != optr) {
4903 			ISP_WRITE(isp, BIU2400_ATIO_RSPOUTP, optr);
4904 			isp->isp_atioodx = optr;
4905 		}
4906 	}
4907 #endif
4908 
4909 	/*
4910 	 * You *must* read the Response Queue In Pointer
4911 	 * prior to clearing the RISC interrupt.
4912 	 *
4913 	 * Debounce the 2300 if revision less than 2.
4914 	 */
4915 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
4916 		i = 0;
4917 		do {
4918 			iptr = ISP_READ(isp, isp->isp_respinrp);
4919 			junk = ISP_READ(isp, isp->isp_respinrp);
4920 		} while (junk != iptr && ++i < 1000);
4921 
4922 		if (iptr != junk) {
4923 			isp_prt(isp, ISP_LOGWARN, "Response Queue Out Pointer Unstable (%x, %x)", iptr, junk);
4924 			goto out;
4925 		}
4926 	} else {
4927 		iptr = ISP_READ(isp, isp->isp_respinrp);
4928 	}
4929 
4930 	optr = isp->isp_resodx;
4931 	if (optr == iptr && sema == 0) {
4932 		/*
4933 		 * There are a lot of these- reasons unknown- mostly on
4934 		 * faster Alpha machines.
4935 		 *
4936 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
4937 		 * make sure the old interrupt went away (to avoid 'ringing'
4938 		 * effects), but that didn't stop this from occurring.
4939 		 */
4940 		if (IS_24XX(isp)) {
4941 			junk = 0;
4942 		} else if (IS_23XX(isp)) {
4943 			ISP_DELAY(100);
4944 			iptr = ISP_READ(isp, isp->isp_respinrp);
4945 			junk = ISP_READ(isp, BIU_R2HSTSLO);
4946 		} else {
4947 			junk = ISP_READ(isp, BIU_ISR);
4948 		}
4949 		if (optr == iptr) {
4950 			if (IS_23XX(isp) || IS_24XX(isp)) {
4951 				;
4952 			} else {
4953 				sema = ISP_READ(isp, BIU_SEMA);
4954 				info = ISP_READ(isp, OUTMAILBOX0);
4955 				if ((sema & 0x3) && (info & 0x8000)) {
4956 					goto again;
4957 				}
4958 			}
4959 			isp->isp_intbogus++;
4960 			isp_prt(isp, ISP_LOGDEBUG1, "bogus intr- isr %x (%x) iptr %x optr %x", isr, junk, iptr, optr);
4961 		}
4962 	}
4963 	isp->isp_residx = iptr;
4964 
4965 	while (optr != iptr) {
4966 		uint8_t qe[QENTRY_LEN];
4967 		ispstatusreq_t *sp = (ispstatusreq_t *) qe;
4968 		isphdr_t *hp;
4969 		int buddaboom, scsi_status, completion_status;
4970 		int req_status_flags, req_state_flags;
4971 		uint8_t *snsp, *resp;
4972 		uint32_t rlen, slen, totslen;
4973 		long resid;
4974 		uint16_t oop;
4975 
4976 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
4977 		oop = optr;
4978 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
4979 		nlooked++;
4980  read_again:
4981 		buddaboom = req_status_flags = req_state_flags = 0;
4982 		resid = 0L;
4983 
4984 		/*
4985 		 * Synchronize our view of this response queue entry.
4986 		 */
4987 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN, -1);
4988 		isp_get_hdr(isp, hp, &sp->req_header);
4989 		etype = sp->req_header.rqs_entry_type;
4990 
4991 		if (IS_24XX(isp) && etype == RQSTYPE_RESPONSE) {
4992 			isp24xx_statusreq_t *sp2 = (isp24xx_statusreq_t *)qe;
4993 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)hp, sp2);
4994 			if (isp->isp_dblev & ISP_LOGDEBUG1) {
4995 				isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, sp2);
4996 			}
4997 			scsi_status = sp2->req_scsi_status;
4998 			completion_status = sp2->req_completion_status;
4999 			if ((scsi_status & 0xff) != 0)
5000 				req_state_flags = RQSF_GOT_STATUS;
5001 			else
5002 				req_state_flags = 0;
5003 			resid = sp2->req_resid;
5004 		} else if (etype == RQSTYPE_RESPONSE) {
5005 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
5006 			if (isp->isp_dblev & ISP_LOGDEBUG1) {
5007 				isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, sp);
5008 			}
5009 			scsi_status = sp->req_scsi_status;
5010 			completion_status = sp->req_completion_status;
5011 			req_status_flags = sp->req_status_flags;
5012 			req_state_flags = sp->req_state_flags;
5013 			resid = sp->req_resid;
5014 		} else if (etype == RQSTYPE_RIO1) {
5015 			isp_rio1_t *rio = (isp_rio1_t *) qe;
5016 			isp_get_rio1(isp, (isp_rio1_t *) hp, rio);
5017 			if (isp->isp_dblev & ISP_LOGDEBUG1) {
5018 				isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, rio);
5019 			}
5020 			for (i = 0; i < rio->req_header.rqs_seqno; i++) {
5021 				isp_fastpost_complete(isp, rio->req_handles[i]);
5022 			}
5023 			if (isp->isp_fpcchiwater < rio->req_header.rqs_seqno) {
5024 				isp->isp_fpcchiwater = rio->req_header.rqs_seqno;
5025 			}
5026 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5027 			last_etype = etype;
5028 			continue;
5029 		} else if (etype == RQSTYPE_RIO2) {
5030 			isp_prt(isp, ISP_LOGERR, "dropping RIO2 response");
5031 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5032 			last_etype = etype;
5033 			continue;
5034 		} else if (etype == RQSTYPE_STATUS_CONT) {
5035 			isp_get_cont_response(isp, (ispstatus_cont_t *) hp, (ispstatus_cont_t *) sp);
5036 			if (last_etype == RQSTYPE_RESPONSE && continuations_expected && ndone > 0 && (xs = complist[ndone-1]) != NULL) {
5037 				ispstatus_cont_t *scp = (ispstatus_cont_t *) sp;
5038 				XS_SENSE_APPEND(xs, scp->req_sense_data, sizeof (scp->req_sense_data));
5039 				isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "%d more Status Continuations expected", --continuations_expected);
5040 			} else {
5041 				isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
5042 			}
5043 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5044 			continue;
5045 		} else {
5046 			/*
5047 			 * Somebody reachable via isp_handle_other_response
5048 			 * may have updated the response queue pointers for
5049 			 * us, so we reload our goal index.
5050 			 */
5051 			int r;
5052 			uint32_t tsto = oop;
5053 			r = isp_handle_other_response(isp, etype, hp, &tsto);
5054 			if (r < 0) {
5055 				goto read_again;
5056 			}
5057 			/*
5058 			 * If somebody updated the output pointer, then reset
5059 			 * optr to be one more than the updated amount.
5060 			 */
5061 			while (tsto != oop) {
5062 				optr = ISP_NXT_QENTRY(tsto, RESULT_QUEUE_LEN(isp));
5063 			}
5064 			if (r > 0) {
5065 				ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5066 				last_etype = etype;
5067 				continue;
5068 			}
5069 
5070 			/*
5071 			 * After this point, we'll just look at the header as
5072 			 * we don't know how to deal with the rest of the
5073 			 * response.
5074 			 */
5075 
5076 			/*
5077 			 * It really has to be a bounced request just copied
5078 			 * from the request queue to the response queue. If
5079 			 * not, something bad has happened.
5080 			 */
5081 			if (etype != RQSTYPE_REQUEST) {
5082 				isp_prt(isp, ISP_LOGERR, notresp, etype, oop, optr, nlooked);
5083 				isp_print_bytes(isp, "Request Queue Entry", QENTRY_LEN, sp);
5084 				ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5085 				last_etype = etype;
5086 				continue;
5087 			}
5088 			buddaboom = 1;
5089 			scsi_status = sp->req_scsi_status;
5090 			completion_status = sp->req_completion_status;
5091 			req_status_flags = sp->req_status_flags;
5092 			req_state_flags = sp->req_state_flags;
5093 			resid = sp->req_resid;
5094 		}
5095 
5096 		if (sp->req_header.rqs_flags & RQSFLAG_MASK) {
5097 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
5098 				isp_print_bytes(isp, "unexpected continuation segment", QENTRY_LEN, sp);
5099 				last_etype = etype;
5100 				continue;
5101 			}
5102 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
5103 				isp_prt(isp, ISP_LOG_WARN1, "internal queues full");
5104 				/*
5105 				 * We'll synthesize a QUEUE FULL message below.
5106 				 */
5107 			}
5108 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
5109 				isp_print_bytes(isp, "bad header flag", QENTRY_LEN, sp);
5110 				buddaboom++;
5111 			}
5112 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
5113 				isp_print_bytes(isp, "bad request packet", QENTRY_LEN, sp);
5114 				buddaboom++;
5115 			}
5116 			if (sp->req_header.rqs_flags & RQSFLAG_BADCOUNT) {
5117 				isp_print_bytes(isp, "invalid entry count", QENTRY_LEN, sp);
5118 				buddaboom++;
5119 			}
5120 			if (sp->req_header.rqs_flags & RQSFLAG_BADORDER) {
5121 				isp_print_bytes(isp, "invalid IOCB ordering", QENTRY_LEN, sp);
5122 				last_etype = etype;
5123 				continue;
5124 			}
5125 		}
5126 
5127 		if (!ISP_VALID_HANDLE(isp, sp->req_handle)) {
5128 			isp_prt(isp, ISP_LOGERR, "bad request handle 0x%x (iocb type 0x%x)", sp->req_handle, etype);
5129 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5130 			last_etype = etype;
5131 			continue;
5132 		}
5133 		xs = isp_find_xs(isp, sp->req_handle);
5134 		if (xs == NULL) {
5135 			uint8_t ts = completion_status & 0xff;
5136 			/*
5137 			 * Only whine if this isn't the expected fallout of
5138 			 * aborting the command or resetting the target.
5139 			 */
5140 			if (etype != RQSTYPE_RESPONSE) {
5141 				isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (type 0x%x)", sp->req_handle, etype);
5142 			} else if (ts != RQCS_ABORTED && ts != RQCS_RESET_OCCURRED) {
5143 				isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (status 0x%x)", sp->req_handle, ts);
5144 			}
5145 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5146 			last_etype = etype;
5147 			continue;
5148 		}
5149 		if (req_status_flags & RQSTF_BUS_RESET) {
5150 			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%jx bus was reset",
5151 			    XS_CHANNEL(xs), XS_TGT(xs), (uintmax_t)XS_LUN(xs));
5152 			XS_SETERR(xs, HBA_BUSRESET);
5153 			ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
5154 		}
5155 		if (buddaboom) {
5156 			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%jx buddaboom",
5157 			    XS_CHANNEL(xs), XS_TGT(xs), (uintmax_t)XS_LUN(xs));
5158 			XS_SETERR(xs, HBA_BOTCH);
5159 		}
5160 
5161 		resp = NULL;
5162 		rlen = 0;
5163 		snsp = NULL;
5164 		totslen = slen = 0;
5165 		if (IS_24XX(isp) && (scsi_status & (RQCS_RV|RQCS_SV)) != 0) {
5166 			resp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5167 			rlen = ((isp24xx_statusreq_t *)sp)->req_response_len;
5168 		} else if (IS_FC(isp) && (scsi_status & RQCS_RV) != 0) {
5169 			resp = sp->req_response;
5170 			rlen = sp->req_response_len;
5171 		}
5172 		if (IS_FC(isp) && (scsi_status & RQCS_SV) != 0) {
5173 			/*
5174 			 * Fibre Channel F/W doesn't say we got status
5175 			 * if there's Sense Data instead. I guess they
5176 			 * think it goes w/o saying.
5177 			 */
5178 			req_state_flags |= RQSF_GOT_STATUS|RQSF_GOT_SENSE;
5179 			if (IS_24XX(isp)) {
5180 				snsp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5181 				snsp += rlen;
5182 				totslen = ((isp24xx_statusreq_t *)sp)->req_sense_len;
5183 				slen = (sizeof (((isp24xx_statusreq_t *)sp)->req_rsp_sense)) - rlen;
5184 				if (totslen < slen)
5185 					slen = totslen;
5186 			} else {
5187 				snsp = sp->req_sense_data;
5188 				totslen = sp->req_sense_len;
5189 				slen = sizeof (sp->req_sense_data);
5190 				if (totslen < slen)
5191 					slen = totslen;
5192 			}
5193 		} else if (IS_SCSI(isp) && (req_state_flags & RQSF_GOT_SENSE)) {
5194 			snsp = sp->req_sense_data;
5195 			totslen = sp->req_sense_len;
5196 			slen = sizeof (sp->req_sense_data);
5197 			if (totslen < slen)
5198 				slen = totslen;
5199 		}
5200 		if (req_state_flags & RQSF_GOT_STATUS) {
5201 			*XS_STSP(xs) = scsi_status & 0xff;
5202 		}
5203 
5204 		switch (etype) {
5205 		case RQSTYPE_RESPONSE:
5206 			if (resp && rlen >= 4 && resp[FCP_RSPNS_CODE_OFFSET] != 0) {
5207 				const char *ptr;
5208 				char lb[64];
5209 				const char *rnames[10] = {
5210 				    "Task Management function complete",
5211 				    "FCP_DATA length different than FCP_BURST_LEN",
5212 				    "FCP_CMND fields invalid",
5213 				    "FCP_DATA parameter mismatch with FCP_DATA_RO",
5214 				    "Task Management function rejected",
5215 				    "Task Management function failed",
5216 				    NULL,
5217 				    NULL,
5218 				    "Task Management function succeeded",
5219 				    "Task Management function incorrect logical unit number",
5220 				};
5221 				uint8_t code = resp[FCP_RSPNS_CODE_OFFSET];
5222 				if (code >= 10 || rnames[code] == NULL) {
5223 					ISP_SNPRINTF(lb, sizeof(lb),
5224 					    "Unknown FCP Response Code 0x%x",
5225 					    code);
5226 					ptr = lb;
5227 				} else {
5228 					ptr = rnames[code];
5229 				}
5230 				isp_xs_prt(isp, xs, ISP_LOGWARN,
5231 				    "FCP RESPONSE, LENGTH %u: %s CDB0=0x%02x",
5232 				    rlen, ptr, XS_CDBP(xs)[0] & 0xff);
5233 				if (code != 0 && code != 8)
5234 					XS_SETERR(xs, HBA_BOTCH);
5235 			}
5236 			if (IS_24XX(isp)) {
5237 				isp_parse_status_24xx(isp, (isp24xx_statusreq_t *)sp, xs, &resid);
5238 			} else {
5239 				isp_parse_status(isp, (void *)sp, xs, &resid);
5240 			}
5241 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) && (*XS_STSP(xs) == SCSI_BUSY)) {
5242 				XS_SETERR(xs, HBA_TGTBSY);
5243 			}
5244 			if (IS_SCSI(isp)) {
5245 				XS_SET_RESID(xs, resid);
5246 				/*
5247 				 * A new synchronous rate was negotiated for
5248 				 * this target. Mark state such that we'll go
5249 				 * look up that which has changed later.
5250 				 */
5251 				if (req_status_flags & RQSTF_NEGOTIATION) {
5252 					int t = XS_TGT(xs);
5253 					sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
5254 					sdp->isp_devparam[t].dev_refresh = 1;
5255 					sdp->update = 1;
5256 				}
5257 			} else {
5258 				if (req_status_flags & RQSF_XFER_COMPLETE) {
5259 					XS_SET_RESID(xs, 0);
5260 				} else if (scsi_status & RQCS_RESID) {
5261 					XS_SET_RESID(xs, resid);
5262 				} else {
5263 					XS_SET_RESID(xs, 0);
5264 				}
5265 			}
5266 			if (snsp && slen) {
5267 				if (totslen > slen) {
5268 					continuations_expected += ((totslen - slen + QENTRY_LEN - 5) / (QENTRY_LEN - 4));
5269 					if (ndone > (MAX_REQUESTQ_COMPLETIONS - continuations_expected - 1)) {
5270 						/* we'll lose some stats, but that's a small price to pay */
5271 						for (i = 0; i < ndone; i++) {
5272 							if (complist[i]) {
5273 								isp->isp_rsltccmplt++;
5274 								isp_done(complist[i]);
5275 							}
5276 						}
5277 						ndone = 0;
5278 					}
5279 					isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "Expecting %d more Status Continuations for total sense length of %u",
5280 					    continuations_expected, totslen);
5281 				}
5282 				XS_SAVE_SENSE(xs, snsp, totslen, slen);
5283 			} else if ((req_status_flags & RQSF_GOT_STATUS) && (scsi_status & 0xff) == SCSI_CHECK && IS_FC(isp)) {
5284 				isp_prt(isp, ISP_LOGWARN, "CHECK CONDITION w/o sense data for CDB=0x%x", XS_CDBP(xs)[0] & 0xff);
5285 				isp_print_bytes(isp, "CC with no Sense", QENTRY_LEN, qe);
5286 			}
5287 			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));
5288 			break;
5289 		case RQSTYPE_REQUEST:
5290 		case RQSTYPE_A64:
5291 		case RQSTYPE_T2RQS:
5292 		case RQSTYPE_T3RQS:
5293 		case RQSTYPE_T7RQS:
5294 			if (!IS_24XX(isp) && (sp->req_header.rqs_flags & RQSFLAG_FULL)) {
5295 				/*
5296 				 * Force Queue Full status.
5297 				 */
5298 				*XS_STSP(xs) = SCSI_QFULL;
5299 				XS_SETERR(xs, HBA_NOERROR);
5300 			} else if (XS_NOERR(xs)) {
5301 				isp_prt(isp, ISP_LOG_WARN1,
5302 				    "%d.%d.%jx badness at %s:%u",
5303 				    XS_CHANNEL(xs), XS_TGT(xs),
5304 				    (uintmax_t)XS_LUN(xs),
5305 				    __func__, __LINE__);
5306 				XS_SETERR(xs, HBA_BOTCH);
5307 			}
5308 			XS_SET_RESID(xs, XS_XFRLEN(xs));
5309 			break;
5310 		default:
5311 			isp_print_bytes(isp, "Unhandled Response Type", QENTRY_LEN, qe);
5312 			if (XS_NOERR(xs)) {
5313 				XS_SETERR(xs, HBA_BOTCH);
5314 			}
5315 			break;
5316 		}
5317 
5318 		/*
5319 		 * Free any DMA resources. As a side effect, this may
5320 		 * also do any cache flushing necessary for data coherence.
5321 		 */
5322 		if (XS_XFRLEN(xs)) {
5323 			ISP_DMAFREE(isp, xs, sp->req_handle);
5324 		}
5325 		isp_destroy_handle(isp, sp->req_handle);
5326 
5327 		if (isp->isp_nactive > 0) {
5328 		    isp->isp_nactive--;
5329 		}
5330 		complist[ndone++] = xs;	/* defer completion call until later */
5331 		ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5332 		last_etype = etype;
5333 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
5334 			break;
5335 		}
5336 	}
5337 
5338 	/*
5339 	 * If we looked at any commands, then it's valid to find out
5340 	 * what the outpointer is. It also is a trigger to update the
5341 	 * ISP's notion of what we've seen so far.
5342 	 */
5343 	if (nlooked) {
5344 		ISP_WRITE(isp, isp->isp_respoutrp, optr);
5345 		isp->isp_resodx = optr;
5346 		if (isp->isp_rscchiwater < ndone)
5347 			isp->isp_rscchiwater = ndone;
5348 	}
5349 
5350 out:
5351 
5352 	if (IS_24XX(isp)) {
5353 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
5354 	} else {
5355 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
5356 		ISP_WRITE(isp, BIU_SEMA, 0);
5357 	}
5358 
5359 	for (i = 0; i < ndone; i++) {
5360 		xs = complist[i];
5361 		if (xs) {
5362 			if (((isp->isp_dblev & (ISP_LOGDEBUG1|ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
5363 			    ((isp->isp_dblev & (ISP_LOGDEBUG0|ISP_LOG_CWARN) && ((!XS_NOERR(xs)) || (*XS_STSP(xs) != SCSI_GOOD))))) {
5364 				isp_prt_endcmd(isp, xs);
5365 			}
5366 			isp->isp_rsltccmplt++;
5367 			isp_done(xs);
5368 		}
5369 	}
5370 }
5371 
5372 /*
5373  * Support routines.
5374  */
5375 
5376 void
5377 isp_prt_endcmd(ispsoftc_t *isp, XS_T *xs)
5378 {
5379 	char cdbstr[16 * 5 + 1];
5380 	int i, lim;
5381 
5382 	lim = XS_CDBLEN(xs) > 16? 16 : XS_CDBLEN(xs);
5383 	ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "0x%02x ", XS_CDBP(xs)[0]);
5384 	for (i = 1; i < lim; i++) {
5385 		ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "%s0x%02x ", cdbstr, XS_CDBP(xs)[i]);
5386 	}
5387 	if (XS_SENSE_VALID(xs)) {
5388 		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",
5389 		    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));
5390 	} else {
5391 		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));
5392 	}
5393 }
5394 
5395 /*
5396  * Parse an ASYNC mailbox complete
5397  *
5398  * Return non-zero if the event has been acknowledged.
5399  */
5400 static int
5401 isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
5402 {
5403 	int acked = 0;
5404 	uint32_t h1 = 0, h2 = 0;
5405 	uint16_t chan = 0;
5406 
5407 	/*
5408 	 * Pick up the channel, but not if this is a ASYNC_RIO32_2,
5409 	 * where Mailboxes 6/7 have the second handle.
5410 	 */
5411 	if (mbox != ASYNC_RIO32_2) {
5412 		if (IS_DUALBUS(isp)) {
5413 			chan = ISP_READ(isp, OUTMAILBOX6);
5414 		}
5415 	}
5416 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5417 
5418 	switch (mbox) {
5419 	case ASYNC_BUS_RESET:
5420 		ISP_SET_SENDMARKER(isp, chan, 1);
5421 #ifdef	ISP_TARGET_MODE
5422 		if (isp_target_async(isp, chan, mbox)) {
5423 			acked = 1;
5424 		}
5425 #endif
5426 		isp_async(isp, ISPASYNC_BUS_RESET, chan);
5427 		break;
5428 	case ASYNC_SYSTEM_ERROR:
5429 		isp->isp_dead = 1;
5430 		isp->isp_state = ISP_CRASHED;
5431 		/*
5432 		 * Were we waiting for a mailbox command to complete?
5433 		 * If so, it's dead, so wake up the waiter.
5434 		 */
5435 		if (isp->isp_mboxbsy) {
5436 			isp->isp_obits = 1;
5437 			isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5438 			MBOX_NOTIFY_COMPLETE(isp);
5439 		}
5440 		/*
5441 		 * It's up to the handler for isp_async to reinit stuff and
5442 		 * restart the firmware
5443 		 */
5444 		isp_async(isp, ISPASYNC_FW_CRASH);
5445 		acked = 1;
5446 		break;
5447 
5448 	case ASYNC_RQS_XFER_ERR:
5449 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5450 		break;
5451 
5452 	case ASYNC_RSP_XFER_ERR:
5453 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5454 		break;
5455 
5456 	case ASYNC_QWAKEUP:
5457 		/*
5458 		 * We've just been notified that the Queue has woken up.
5459 		 * We don't need to be chatty about this- just unlatch things
5460 		 * and move on.
5461 		 */
5462 		mbox = ISP_READ(isp, isp->isp_rqstoutrp);
5463 		break;
5464 
5465 	case ASYNC_TIMEOUT_RESET:
5466 		isp_prt(isp, ISP_LOGWARN, "timeout initiated SCSI bus reset of chan %d", chan);
5467 		ISP_SET_SENDMARKER(isp, chan, 1);
5468 #ifdef	ISP_TARGET_MODE
5469 		if (isp_target_async(isp, chan, mbox)) {
5470 			acked = 1;
5471 		}
5472 #endif
5473 		break;
5474 
5475 	case ASYNC_DEVICE_RESET:
5476 		isp_prt(isp, ISP_LOGINFO, "device reset on chan %d", chan);
5477 		ISP_SET_SENDMARKER(isp, chan, 1);
5478 #ifdef	ISP_TARGET_MODE
5479 		if (isp_target_async(isp, chan, mbox)) {
5480 			acked = 1;
5481 		}
5482 #endif
5483 		break;
5484 
5485 	case ASYNC_EXTMSG_UNDERRUN:
5486 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
5487 		break;
5488 
5489 	case ASYNC_SCAM_INT:
5490 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
5491 		break;
5492 
5493 	case ASYNC_HUNG_SCSI:
5494 		isp_prt(isp, ISP_LOGERR, "stalled SCSI Bus after DATA Overrun");
5495 		/* XXX: Need to issue SCSI reset at this point */
5496 		break;
5497 
5498 	case ASYNC_KILLED_BUS:
5499 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
5500 		break;
5501 
5502 	case ASYNC_BUS_TRANSIT:
5503 		mbox = ISP_READ(isp, OUTMAILBOX2);
5504 		switch (mbox & SXP_PINS_MODE_MASK) {
5505 		case SXP_PINS_LVD_MODE:
5506 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
5507 			SDPARAM(isp, chan)->isp_diffmode = 0;
5508 			SDPARAM(isp, chan)->isp_ultramode = 0;
5509 			SDPARAM(isp, chan)->isp_lvdmode = 1;
5510 			break;
5511 		case SXP_PINS_HVD_MODE:
5512 			isp_prt(isp, ISP_LOGINFO,
5513 			    "Transition to Differential mode");
5514 			SDPARAM(isp, chan)->isp_diffmode = 1;
5515 			SDPARAM(isp, chan)->isp_ultramode = 0;
5516 			SDPARAM(isp, chan)->isp_lvdmode = 0;
5517 			break;
5518 		case SXP_PINS_SE_MODE:
5519 			isp_prt(isp, ISP_LOGINFO,
5520 			    "Transition to Single Ended mode");
5521 			SDPARAM(isp, chan)->isp_diffmode = 0;
5522 			SDPARAM(isp, chan)->isp_ultramode = 1;
5523 			SDPARAM(isp, chan)->isp_lvdmode = 0;
5524 			break;
5525 		default:
5526 			isp_prt(isp, ISP_LOGWARN,
5527 			    "Transition to Unknown Mode 0x%x", mbox);
5528 			break;
5529 		}
5530 		/*
5531 		 * XXX: Set up to renegotiate again!
5532 		 */
5533 		/* Can only be for a 1080... */
5534 		ISP_SET_SENDMARKER(isp, chan, 1);
5535 		break;
5536 
5537 	case ASYNC_CMD_CMPLT:
5538 	case ASYNC_RIO32_1:
5539 		if (!IS_ULTRA3(isp)) {
5540 			isp_prt(isp, ISP_LOGERR, "unexpected fast posting completion");
5541 			break;
5542 		}
5543 		/* FALLTHROUGH */
5544 		h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5545 		break;
5546 
5547 	case ASYNC_RIO32_2:
5548 		h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5549 		h2 = (ISP_READ(isp, OUTMAILBOX7) << 16) | ISP_READ(isp, OUTMAILBOX6);
5550 		break;
5551 
5552 	case ASYNC_RIO16_5:
5553 	case ASYNC_RIO16_4:
5554 	case ASYNC_RIO16_3:
5555 	case ASYNC_RIO16_2:
5556 	case ASYNC_RIO16_1:
5557 		isp_prt(isp, ISP_LOGERR, "unexpected 16 bit RIO handle");
5558 		break;
5559 	default:
5560 		isp_prt(isp, ISP_LOGWARN, "%s: unhandled async code 0x%x", __func__, mbox);
5561 		break;
5562 	}
5563 
5564 	if (h1 || h2) {
5565 		isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h1);
5566 		isp_fastpost_complete(isp, h1);
5567 		if (h2) {
5568 			isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h2);
5569 			isp_fastpost_complete(isp, h2);
5570 			if (isp->isp_fpcchiwater < 2) {
5571 				isp->isp_fpcchiwater = 2;
5572 			}
5573 		} else {
5574 			if (isp->isp_fpcchiwater < 1) {
5575 				isp->isp_fpcchiwater = 1;
5576 			}
5577 		}
5578 	} else {
5579 		isp->isp_intoasync++;
5580 	}
5581 	return (acked);
5582 }
5583 
5584 static int
5585 isp_parse_async_fc(ispsoftc_t *isp, uint16_t mbox)
5586 {
5587 	fcparam *fcp;
5588 	int acked = 0;
5589 	uint16_t chan;
5590 
5591 	if (IS_DUALBUS(isp)) {
5592 		chan = ISP_READ(isp, OUTMAILBOX6);
5593 	} else {
5594 		chan = 0;
5595 	}
5596 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5597 
5598 	switch (mbox) {
5599 	case ASYNC_SYSTEM_ERROR:
5600 		isp->isp_dead = 1;
5601 		isp->isp_state = ISP_CRASHED;
5602 		FCPARAM(isp, chan)->isp_loopstate = LOOP_NIL;
5603 		isp_change_fw_state(isp, chan, FW_CONFIG_WAIT);
5604 		/*
5605 		 * Were we waiting for a mailbox command to complete?
5606 		 * If so, it's dead, so wake up the waiter.
5607 		 */
5608 		if (isp->isp_mboxbsy) {
5609 			isp->isp_obits = 1;
5610 			isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5611 			MBOX_NOTIFY_COMPLETE(isp);
5612 		}
5613 		/*
5614 		 * It's up to the handler for isp_async to reinit stuff and
5615 		 * restart the firmware
5616 		 */
5617 		isp_async(isp, ISPASYNC_FW_CRASH);
5618 		acked = 1;
5619 		break;
5620 
5621 	case ASYNC_RQS_XFER_ERR:
5622 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5623 		break;
5624 
5625 	case ASYNC_RSP_XFER_ERR:
5626 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5627 		break;
5628 
5629 	case ASYNC_QWAKEUP:
5630 #ifdef	ISP_TARGET_MODE
5631 		if (IS_24XX(isp)) {
5632 			isp_prt(isp, ISP_LOGERR, "ATIO Queue Transfer Error");
5633 			break;
5634 		}
5635 #endif
5636 		isp_prt(isp, ISP_LOGERR, "%s: unexpected ASYNC_QWAKEUP code", __func__);
5637 		break;
5638 
5639 	case ASYNC_CMD_CMPLT:
5640 		isp_fastpost_complete(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1));
5641 		if (isp->isp_fpcchiwater < 1) {
5642 			isp->isp_fpcchiwater = 1;
5643 		}
5644 		break;
5645 
5646 	case ASYNC_RIOZIO_STALL:
5647 		break;
5648 
5649 	case ASYNC_CTIO_DONE:
5650 #ifdef	ISP_TARGET_MODE
5651 		if (isp_target_async(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1), mbox)) {
5652 			acked = 1;
5653 		} else {
5654 			isp->isp_fphccmplt++;
5655 		}
5656 #else
5657 		isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC CTIO done");
5658 #endif
5659 		break;
5660 	case ASYNC_LIP_ERROR:
5661 	case ASYNC_LIP_F8:
5662 	case ASYNC_LIP_OCCURRED:
5663 	case ASYNC_PTPMODE:
5664 		/*
5665 		 * These are broadcast events that have to be sent across
5666 		 * all active channels.
5667 		 */
5668 		for (chan = 0; chan < isp->isp_nchan; chan++) {
5669 			fcp = FCPARAM(isp, chan);
5670 			int topo = fcp->isp_topo;
5671 
5672 			if (fcp->role == ISP_ROLE_NONE) {
5673 				continue;
5674 			}
5675 
5676 			fcp->isp_loopstate = LOOP_NIL;
5677 			ISP_SET_SENDMARKER(isp, chan, 1);
5678 			isp_async(isp, ISPASYNC_LIP, chan);
5679 #ifdef	ISP_TARGET_MODE
5680 			if (isp_target_async(isp, chan, mbox)) {
5681 				acked = 1;
5682 			}
5683 #endif
5684 			/*
5685 			 * We've had problems with data corruption occuring on
5686 			 * commands that complete (with no apparent error) after
5687 			 * we receive a LIP. This has been observed mostly on
5688 			 * Local Loop topologies. To be safe, let's just mark
5689 			 * all active initiator commands as dead.
5690 			 */
5691 			if (topo == TOPO_NL_PORT || topo == TOPO_FL_PORT) {
5692 				int i, j;
5693 				for (i = j = 0; i < isp->isp_maxcmds; i++) {
5694 					XS_T *xs;
5695 					isp_hdl_t *hdp;
5696 
5697 					hdp = &isp->isp_xflist[i];
5698 					if (ISP_H2HT(hdp->handle) != ISP_HANDLE_INITIATOR) {
5699 						continue;
5700 					}
5701 					xs = hdp->cmd;
5702 					if (XS_CHANNEL(xs) != chan) {
5703 						continue;
5704 					}
5705 					j++;
5706 					isp_prt(isp, ISP_LOG_WARN1,
5707 					    "%d.%d.%jx bus reset set at %s:%u",
5708 					    XS_CHANNEL(xs), XS_TGT(xs),
5709 					    (uintmax_t)XS_LUN(xs),
5710 					    __func__, __LINE__);
5711 					XS_SETERR(xs, HBA_BUSRESET);
5712 				}
5713 				if (j) {
5714 					isp_prt(isp, ISP_LOGERR, lipd, chan, j);
5715 				}
5716 			}
5717 		}
5718 		break;
5719 
5720 	case ASYNC_LOOP_UP:
5721 		/*
5722 		 * This is a broadcast event that has to be sent across
5723 		 * all active channels.
5724 		 */
5725 		for (chan = 0; chan < isp->isp_nchan; chan++) {
5726 			fcp = FCPARAM(isp, chan);
5727 			if (fcp->role == ISP_ROLE_NONE)
5728 				continue;
5729 			ISP_SET_SENDMARKER(isp, chan, 1);
5730 			isp_async(isp, ISPASYNC_LOOP_UP, chan);
5731 #ifdef	ISP_TARGET_MODE
5732 			if (isp_target_async(isp, chan, mbox)) {
5733 				acked = 1;
5734 			}
5735 #endif
5736 		}
5737 		break;
5738 
5739 	case ASYNC_LOOP_DOWN:
5740 		/*
5741 		 * This is a broadcast event that has to be sent across
5742 		 * all active channels.
5743 		 */
5744 		for (chan = 0; chan < isp->isp_nchan; chan++) {
5745 			fcp = FCPARAM(isp, chan);
5746 			if (fcp->role == ISP_ROLE_NONE)
5747 				continue;
5748 			ISP_SET_SENDMARKER(isp, chan, 1);
5749 			fcp->isp_loopstate = LOOP_NIL;
5750 			isp_async(isp, ISPASYNC_LOOP_DOWN, chan);
5751 #ifdef	ISP_TARGET_MODE
5752 			if (isp_target_async(isp, chan, mbox)) {
5753 				acked = 1;
5754 			}
5755 #endif
5756 		}
5757 		break;
5758 
5759 	case ASYNC_LOOP_RESET:
5760 		/*
5761 		 * This is a broadcast event that has to be sent across
5762 		 * all active channels.
5763 		 */
5764 		for (chan = 0; chan < isp->isp_nchan; chan++) {
5765 			fcp = FCPARAM(isp, chan);
5766 			if (fcp->role == ISP_ROLE_NONE)
5767 				continue;
5768 			ISP_SET_SENDMARKER(isp, chan, 1);
5769 			fcp->isp_loopstate = LOOP_NIL;
5770 			isp_async(isp, ISPASYNC_LOOP_RESET, chan);
5771 #ifdef	ISP_TARGET_MODE
5772 			if (isp_target_async(isp, chan, mbox)) {
5773 				acked = 1;
5774 			}
5775 #endif
5776 		}
5777 		break;
5778 
5779 	case ASYNC_PDB_CHANGED:
5780 	{
5781 		int echan, nphdl, nlstate, reason;
5782 
5783 		if (IS_23XX(isp) || IS_24XX(isp)) {
5784 			nphdl = ISP_READ(isp, OUTMAILBOX1);
5785 			nlstate = ISP_READ(isp, OUTMAILBOX2);
5786 		} else {
5787 			nphdl = nlstate = 0xffff;
5788 		}
5789 		if (IS_24XX(isp))
5790 			reason = ISP_READ(isp, OUTMAILBOX3) >> 8;
5791 		else
5792 			reason = 0xff;
5793 		if (ISP_CAP_MULTI_ID(isp)) {
5794 			chan = ISP_READ(isp, OUTMAILBOX3) & 0xff;
5795 			if (chan == 0xff || nphdl == NIL_HANDLE) {
5796 				chan = 0;
5797 				echan = isp->isp_nchan - 1;
5798 			} else if (chan >= isp->isp_nchan) {
5799 				break;
5800 			} else {
5801 				echan = chan;
5802 			}
5803 		} else {
5804 			chan = echan = 0;
5805 		}
5806 		for (; chan <= echan; chan++) {
5807 			fcp = FCPARAM(isp, chan);
5808 			if (fcp->role == ISP_ROLE_NONE)
5809 				continue;
5810 			if (fcp->isp_loopstate > LOOP_LTEST_DONE)
5811 				fcp->isp_loopstate = LOOP_LTEST_DONE;
5812 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan,
5813 			    ISPASYNC_CHANGE_PDB, nphdl, nlstate, reason);
5814 		}
5815 		break;
5816 	}
5817 	case ASYNC_CHANGE_NOTIFY:
5818 	{
5819 		int portid;
5820 
5821 		portid = ((ISP_READ(isp, OUTMAILBOX1) & 0xff) << 16) |
5822 		    ISP_READ(isp, OUTMAILBOX2);
5823 		if (ISP_CAP_MULTI_ID(isp)) {
5824 			chan = ISP_READ(isp, OUTMAILBOX3) & 0xff;
5825 			if (chan >= isp->isp_nchan)
5826 				break;
5827 		} else {
5828 			chan = 0;
5829 		}
5830 		fcp = FCPARAM(isp, chan);
5831 		if (fcp->role == ISP_ROLE_NONE)
5832 			break;
5833 		if (fcp->isp_loopstate > LOOP_LTEST_DONE)
5834 			fcp->isp_loopstate = LOOP_LTEST_DONE;
5835 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan,
5836 		    ISPASYNC_CHANGE_SNS, portid);
5837 		break;
5838 	}
5839 	case ASYNC_ERR_LOGGING_DISABLED:
5840 		isp_prt(isp, ISP_LOGWARN, "Error logging disabled (reason 0x%x)",
5841 		    ISP_READ(isp, OUTMAILBOX1));
5842 		break;
5843 	case ASYNC_CONNMODE:
5844 		/*
5845 		 * This only applies to 2100 amd 2200 cards
5846 		 */
5847 		if (!IS_2200(isp) && !IS_2100(isp)) {
5848 			isp_prt(isp, ISP_LOGWARN, "bad card for ASYNC_CONNMODE event");
5849 			break;
5850 		}
5851 		chan = 0;
5852 		mbox = ISP_READ(isp, OUTMAILBOX1);
5853 		switch (mbox) {
5854 		case ISP_CONN_LOOP:
5855 			isp_prt(isp, ISP_LOGINFO,
5856 			    "Point-to-Point -> Loop mode");
5857 			break;
5858 		case ISP_CONN_PTP:
5859 			isp_prt(isp, ISP_LOGINFO,
5860 			    "Loop -> Point-to-Point mode");
5861 			break;
5862 		case ISP_CONN_BADLIP:
5863 			isp_prt(isp, ISP_LOGWARN,
5864 			    "Point-to-Point -> Loop mode (BAD LIP)");
5865 			break;
5866 		case ISP_CONN_FATAL:
5867 			isp->isp_dead = 1;
5868 			isp->isp_state = ISP_CRASHED;
5869 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
5870 			isp_async(isp, ISPASYNC_FW_CRASH);
5871 			return (-1);
5872 		case ISP_CONN_LOOPBACK:
5873 			isp_prt(isp, ISP_LOGWARN,
5874 			    "Looped Back in Point-to-Point mode");
5875 			break;
5876 		default:
5877 			isp_prt(isp, ISP_LOGWARN,
5878 			    "Unknown connection mode (0x%x)", mbox);
5879 			break;
5880 		}
5881 		ISP_SET_SENDMARKER(isp, chan, 1);
5882 		FCPARAM(isp, chan)->isp_loopstate = LOOP_NIL;
5883 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan, ISPASYNC_CHANGE_OTHER);
5884 		break;
5885 	case ASYNC_P2P_INIT_ERR:
5886 		isp_prt(isp, ISP_LOGWARN, "P2P init error (reason 0x%x)",
5887 		    ISP_READ(isp, OUTMAILBOX1));
5888 		break;
5889 	case ASYNC_RCV_ERR:
5890 		if (IS_24XX(isp)) {
5891 			isp_prt(isp, ISP_LOGWARN, "Receive Error");
5892 		} else {
5893 			isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC_RCV_ERR");
5894 		}
5895 		break;
5896 	case ASYNC_RJT_SENT:	/* same as ASYNC_QFULL_SENT */
5897 		if (IS_24XX(isp)) {
5898 			isp_prt(isp, ISP_LOGTDEBUG0, "LS_RJT sent");
5899 			break;
5900 		} else {
5901 			isp_prt(isp, ISP_LOGTDEBUG0, "QFULL sent");
5902 			break;
5903 		}
5904 	case ASYNC_FW_RESTART_COMPLETE:
5905 		isp_prt(isp, ISP_LOGDEBUG0, "FW restart complete");
5906 		break;
5907 	case ASYNC_TEMPERATURE_ALERT:
5908 		isp_prt(isp, ISP_LOGERR, "Temperature alert (subcode 0x%x)",
5909 		    ISP_READ(isp, OUTMAILBOX1));
5910 		break;
5911 	case ASYNC_AUTOLOAD_FW_COMPLETE:
5912 		isp_prt(isp, ISP_LOGDEBUG0, "Autoload FW init complete");
5913 		break;
5914 	case ASYNC_AUTOLOAD_FW_FAILURE:
5915 		isp_prt(isp, ISP_LOGERR, "Autoload FW init failure");
5916 		break;
5917 	default:
5918 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
5919 		break;
5920 	}
5921 	if (mbox != ASYNC_CTIO_DONE && mbox != ASYNC_CMD_CMPLT) {
5922 		isp->isp_intoasync++;
5923 	}
5924 	return (acked);
5925 }
5926 
5927 /*
5928  * Handle other response entries. A pointer to the request queue output
5929  * index is here in case we want to eat several entries at once, although
5930  * this is not used currently.
5931  */
5932 
5933 static int
5934 isp_handle_other_response(ispsoftc_t *isp, int type, isphdr_t *hp, uint32_t *optrp)
5935 {
5936 	isp_ridacq_t rid;
5937 	int chan, c;
5938 
5939 	switch (type) {
5940 	case RQSTYPE_STATUS_CONT:
5941 		isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
5942 		return (1);
5943 	case RQSTYPE_MARKER:
5944 		isp_prt(isp, ISP_LOG_WARN1, "Marker Response");
5945 		return (1);
5946 	case RQSTYPE_RPT_ID_ACQ:
5947 		isp_get_ridacq(isp, (isp_ridacq_t *)hp, &rid);
5948 		if (rid.ridacq_format == 0) {
5949 			for (chan = 0; chan < isp->isp_nchan; chan++) {
5950 				fcparam *fcp = FCPARAM(isp, chan);
5951 				if (fcp->role == ISP_ROLE_NONE)
5952 					continue;
5953 				c = (chan == 0) ? 127 : (chan - 1);
5954 				if (rid.ridacq_map[c / 16] & (1 << (c % 16))) {
5955 					fcp->isp_loopstate = LOOP_NIL;
5956 					isp_async(isp, ISPASYNC_CHANGE_NOTIFY,
5957 					    chan, ISPASYNC_CHANGE_OTHER);
5958 				}
5959 			}
5960 		} else {
5961 			FCPARAM(isp, rid.ridacq_vp_index)->isp_loopstate = LOOP_NIL;
5962 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY,
5963 			    rid.ridacq_vp_index, ISPASYNC_CHANGE_OTHER);
5964 		}
5965 		return (1);
5966 	case RQSTYPE_ATIO:
5967 	case RQSTYPE_CTIO:
5968 	case RQSTYPE_ENABLE_LUN:
5969 	case RQSTYPE_MODIFY_LUN:
5970 	case RQSTYPE_NOTIFY:
5971 	case RQSTYPE_NOTIFY_ACK:
5972 	case RQSTYPE_CTIO1:
5973 	case RQSTYPE_ATIO2:
5974 	case RQSTYPE_CTIO2:
5975 	case RQSTYPE_CTIO3:
5976 	case RQSTYPE_CTIO7:
5977 	case RQSTYPE_ABTS_RCVD:
5978 	case RQSTYPE_ABTS_RSP:
5979 		isp->isp_rsltccmplt++;	/* count as a response completion */
5980 #ifdef	ISP_TARGET_MODE
5981 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
5982 			return (1);
5983 		}
5984 #endif
5985 		/* FALLTHROUGH */
5986 	case RQSTYPE_REQUEST:
5987 	default:
5988 		ISP_DELAY(100);
5989 		if (type != isp_get_response_type(isp, hp)) {
5990 			/*
5991 			 * This is questionable- we're just papering over
5992 			 * something we've seen on SMP linux in target
5993 			 * mode- we don't really know what's happening
5994 			 * here that causes us to think we've gotten
5995 			 * an entry, but that either the entry isn't
5996 			 * filled out yet or our CPU read data is stale.
5997 			 */
5998 			isp_prt(isp, ISP_LOGINFO,
5999 				"unstable type in response queue");
6000 			return (-1);
6001 		}
6002 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
6003 		    isp_get_response_type(isp, hp));
6004 		return (0);
6005 	}
6006 }
6007 
6008 static void
6009 isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs, long *rp)
6010 {
6011 	switch (sp->req_completion_status & 0xff) {
6012 	case RQCS_COMPLETE:
6013 		if (XS_NOERR(xs)) {
6014 			XS_SETERR(xs, HBA_NOERROR);
6015 		}
6016 		return;
6017 
6018 	case RQCS_INCOMPLETE:
6019 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
6020 			isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Selection Timeout @ %s:%d", __func__, __LINE__);
6021 			if (XS_NOERR(xs)) {
6022 				XS_SETERR(xs, HBA_SELTIMEOUT);
6023 				*rp = XS_XFRLEN(xs);
6024 			}
6025 			return;
6026 		}
6027 		isp_xs_prt(isp, xs, ISP_LOGERR, "Command Incomplete, state 0x%x", sp->req_state_flags);
6028 		break;
6029 
6030 	case RQCS_DMA_ERROR:
6031 		isp_xs_prt(isp, xs, ISP_LOGERR, "DMA Error");
6032 		*rp = XS_XFRLEN(xs);
6033 		break;
6034 
6035 	case RQCS_TRANSPORT_ERROR:
6036 	{
6037 		char buf[172];
6038 		ISP_SNPRINTF(buf, sizeof (buf), "states=>");
6039 		if (sp->req_state_flags & RQSF_GOT_BUS) {
6040 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
6041 		}
6042 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
6043 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
6044 		}
6045 		if (sp->req_state_flags & RQSF_SENT_CDB) {
6046 			ISP_SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
6047 		}
6048 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
6049 			ISP_SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
6050 		}
6051 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
6052 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
6053 		}
6054 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
6055 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
6056 		}
6057 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
6058 			ISP_SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
6059 		}
6060 		ISP_SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
6061 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
6062 			ISP_SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
6063 		}
6064 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
6065 			ISP_SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
6066 		}
6067 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
6068 			ISP_SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
6069 		}
6070 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
6071 			ISP_SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
6072 		}
6073 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
6074 			ISP_SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
6075 		}
6076 		if (sp->req_status_flags & RQSTF_ABORTED) {
6077 			ISP_SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
6078 		}
6079 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
6080 			ISP_SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
6081 		}
6082 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
6083 			ISP_SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
6084 		}
6085 		isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error: %s", buf);
6086 		*rp = XS_XFRLEN(xs);
6087 		break;
6088 	}
6089 	case RQCS_RESET_OCCURRED:
6090 	{
6091 		int chan;
6092 		isp_xs_prt(isp, xs, ISP_LOGWARN, "Bus Reset destroyed command");
6093 		for (chan = 0; chan < isp->isp_nchan; chan++) {
6094 			FCPARAM(isp, chan)->sendmarker = 1;
6095 		}
6096 		if (XS_NOERR(xs)) {
6097 			XS_SETERR(xs, HBA_BUSRESET);
6098 		}
6099 		*rp = XS_XFRLEN(xs);
6100 		return;
6101 	}
6102 	case RQCS_ABORTED:
6103 		isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6104 		ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
6105 		if (XS_NOERR(xs)) {
6106 			XS_SETERR(xs, HBA_ABORTED);
6107 		}
6108 		return;
6109 
6110 	case RQCS_TIMEOUT:
6111 		isp_xs_prt(isp, xs, ISP_LOGWARN, "Command timed out");
6112 		/*
6113 	 	 * XXX: Check to see if we logged out of the device.
6114 		 */
6115 		if (XS_NOERR(xs)) {
6116 			XS_SETERR(xs, HBA_CMDTIMEOUT);
6117 		}
6118 		return;
6119 
6120 	case RQCS_DATA_OVERRUN:
6121 		XS_SET_RESID(xs, sp->req_resid);
6122 		isp_xs_prt(isp, xs, ISP_LOGERR, "data overrun (%ld)", (long) XS_GET_RESID(xs));
6123 		if (XS_NOERR(xs)) {
6124 			XS_SETERR(xs, HBA_DATAOVR);
6125 		}
6126 		return;
6127 
6128 	case RQCS_COMMAND_OVERRUN:
6129 		isp_xs_prt(isp, xs, ISP_LOGERR, "command overrun");
6130 		break;
6131 
6132 	case RQCS_STATUS_OVERRUN:
6133 		isp_xs_prt(isp, xs, ISP_LOGERR, "status overrun");
6134 		break;
6135 
6136 	case RQCS_BAD_MESSAGE:
6137 		isp_xs_prt(isp, xs, ISP_LOGERR, "msg not COMMAND COMPLETE after status");
6138 		break;
6139 
6140 	case RQCS_NO_MESSAGE_OUT:
6141 		isp_xs_prt(isp, xs, ISP_LOGERR, "No MESSAGE OUT phase after selection");
6142 		break;
6143 
6144 	case RQCS_EXT_ID_FAILED:
6145 		isp_xs_prt(isp, xs, ISP_LOGERR, "EXTENDED IDENTIFY failed");
6146 		break;
6147 
6148 	case RQCS_IDE_MSG_FAILED:
6149 		isp_xs_prt(isp, xs, ISP_LOGERR, "INITIATOR DETECTED ERROR rejected");
6150 		break;
6151 
6152 	case RQCS_ABORT_MSG_FAILED:
6153 		isp_xs_prt(isp, xs, ISP_LOGERR, "ABORT OPERATION rejected");
6154 		break;
6155 
6156 	case RQCS_REJECT_MSG_FAILED:
6157 		isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE REJECT rejected");
6158 		break;
6159 
6160 	case RQCS_NOP_MSG_FAILED:
6161 		isp_xs_prt(isp, xs, ISP_LOGERR, "NOP rejected");
6162 		break;
6163 
6164 	case RQCS_PARITY_ERROR_MSG_FAILED:
6165 		isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE PARITY ERROR rejected");
6166 		break;
6167 
6168 	case RQCS_DEVICE_RESET_MSG_FAILED:
6169 		isp_xs_prt(isp, xs, ISP_LOGWARN, "BUS DEVICE RESET rejected");
6170 		break;
6171 
6172 	case RQCS_ID_MSG_FAILED:
6173 		isp_xs_prt(isp, xs, ISP_LOGERR, "IDENTIFY rejected");
6174 		break;
6175 
6176 	case RQCS_UNEXP_BUS_FREE:
6177 		isp_xs_prt(isp, xs, ISP_LOGERR, "Unexpected Bus Free");
6178 		break;
6179 
6180 	case RQCS_DATA_UNDERRUN:
6181 	{
6182 		if (IS_FC(isp)) {
6183 			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6184 			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
6185 				isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6186 				if (XS_NOERR(xs)) {
6187 					XS_SETERR(xs, HBA_BOTCH);
6188 				}
6189 				return;
6190 			}
6191 		}
6192 		XS_SET_RESID(xs, sp->req_resid);
6193 		if (XS_NOERR(xs)) {
6194 			XS_SETERR(xs, HBA_NOERROR);
6195 		}
6196 		return;
6197 	}
6198 
6199 	case RQCS_XACT_ERR1:
6200 		isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued transaction with disconnect not set");
6201 		break;
6202 
6203 	case RQCS_XACT_ERR2:
6204 		isp_xs_prt(isp, xs, ISP_LOGERR,
6205 		    "HBA attempted queued transaction to target routine %jx",
6206 		    (uintmax_t)XS_LUN(xs));
6207 		break;
6208 
6209 	case RQCS_XACT_ERR3:
6210 		isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued cmd when queueing disabled");
6211 		break;
6212 
6213 	case RQCS_BAD_ENTRY:
6214 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
6215 		break;
6216 
6217 	case RQCS_QUEUE_FULL:
6218 		isp_xs_prt(isp, xs, ISP_LOG_WARN1, "internal queues full status 0x%x", *XS_STSP(xs));
6219 
6220 		/*
6221 		 * If QFULL or some other status byte is set, then this
6222 		 * isn't an error, per se.
6223 		 *
6224 		 * Unfortunately, some QLogic f/w writers have, in
6225 		 * some cases, ommitted to *set* status to QFULL.
6226 		 */
6227 #if	0
6228 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
6229 			XS_SETERR(xs, HBA_NOERROR);
6230 			return;
6231 		}
6232 
6233 #endif
6234 		*XS_STSP(xs) = SCSI_QFULL;
6235 		XS_SETERR(xs, HBA_NOERROR);
6236 		return;
6237 
6238 	case RQCS_PHASE_SKIPPED:
6239 		isp_xs_prt(isp, xs, ISP_LOGERR, "SCSI phase skipped");
6240 		break;
6241 
6242 	case RQCS_ARQS_FAILED:
6243 		isp_xs_prt(isp, xs, ISP_LOGERR, "Auto Request Sense Failed");
6244 		if (XS_NOERR(xs)) {
6245 			XS_SETERR(xs, HBA_ARQFAIL);
6246 		}
6247 		return;
6248 
6249 	case RQCS_WIDE_FAILED:
6250 		isp_xs_prt(isp, xs, ISP_LOGERR, "Wide Negotiation Failed");
6251 		if (IS_SCSI(isp)) {
6252 			sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6253 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
6254 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6255 			sdp->update = 1;
6256 		}
6257 		if (XS_NOERR(xs)) {
6258 			XS_SETERR(xs, HBA_NOERROR);
6259 		}
6260 		return;
6261 
6262 	case RQCS_SYNCXFER_FAILED:
6263 		isp_xs_prt(isp, xs, ISP_LOGERR, "SDTR Message Failed");
6264 		if (IS_SCSI(isp)) {
6265 			sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6266 			sdp += XS_CHANNEL(xs);
6267 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
6268 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6269 			sdp->update = 1;
6270 		}
6271 		break;
6272 
6273 	case RQCS_LVD_BUSERR:
6274 		isp_xs_prt(isp, xs, ISP_LOGERR, "Bad LVD condition");
6275 		break;
6276 
6277 	case RQCS_PORT_UNAVAILABLE:
6278 		/*
6279 		 * No such port on the loop. Moral equivalent of SELTIMEO
6280 		 */
6281 	case RQCS_PORT_LOGGED_OUT:
6282 	{
6283 		const char *reason;
6284 		uint8_t sts = sp->req_completion_status & 0xff;
6285 
6286 		/*
6287 		 * It was there (maybe)- treat as a selection timeout.
6288 		 */
6289 		if (sts == RQCS_PORT_UNAVAILABLE) {
6290 			reason = "unavailable";
6291 		} else {
6292 			reason = "logout";
6293 		}
6294 
6295 		isp_prt(isp, ISP_LOGINFO, "port %s for target %d", reason, XS_TGT(xs));
6296 
6297 		/*
6298 		 * If we're on a local loop, force a LIP (which is overkill)
6299 		 * to force a re-login of this unit. If we're on fabric,
6300 		 * then we'll have to log in again as a matter of course.
6301 		 */
6302 		if (FCPARAM(isp, 0)->isp_topo == TOPO_NL_PORT ||
6303 		    FCPARAM(isp, 0)->isp_topo == TOPO_FL_PORT) {
6304 			mbreg_t mbs;
6305 			MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
6306 			if (ISP_CAP_2KLOGIN(isp)) {
6307 				mbs.ibits = (1 << 10);
6308 			}
6309 			isp_mboxcmd_qnw(isp, &mbs, 1);
6310 		}
6311 		if (XS_NOERR(xs)) {
6312 			XS_SETERR(xs, HBA_SELTIMEOUT);
6313 		}
6314 		return;
6315 	}
6316 	case RQCS_PORT_CHANGED:
6317 		isp_prt(isp, ISP_LOGWARN, "port changed for target %d", XS_TGT(xs));
6318 		if (XS_NOERR(xs)) {
6319 			XS_SETERR(xs, HBA_SELTIMEOUT);
6320 		}
6321 		return;
6322 
6323 	case RQCS_PORT_BUSY:
6324 		isp_prt(isp, ISP_LOGWARN, "port busy for target %d", XS_TGT(xs));
6325 		if (XS_NOERR(xs)) {
6326 			XS_SETERR(xs, HBA_TGTBSY);
6327 		}
6328 		return;
6329 
6330 	default:
6331 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x", sp->req_completion_status);
6332 		break;
6333 	}
6334 	if (XS_NOERR(xs)) {
6335 		XS_SETERR(xs, HBA_BOTCH);
6336 	}
6337 }
6338 
6339 static void
6340 isp_parse_status_24xx(ispsoftc_t *isp, isp24xx_statusreq_t *sp, XS_T *xs, long *rp)
6341 {
6342 	int ru_marked, sv_marked;
6343 	int chan = XS_CHANNEL(xs);
6344 
6345 	switch (sp->req_completion_status) {
6346 	case RQCS_COMPLETE:
6347 		if (XS_NOERR(xs)) {
6348 			XS_SETERR(xs, HBA_NOERROR);
6349 		}
6350 		return;
6351 
6352 	case RQCS_DMA_ERROR:
6353 		isp_xs_prt(isp, xs, ISP_LOGERR, "DMA error");
6354 		break;
6355 
6356 	case RQCS_TRANSPORT_ERROR:
6357 		isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error");
6358 		break;
6359 
6360 	case RQCS_RESET_OCCURRED:
6361 		isp_xs_prt(isp, xs, ISP_LOGWARN, "reset destroyed command");
6362 		FCPARAM(isp, chan)->sendmarker = 1;
6363 		if (XS_NOERR(xs)) {
6364 			XS_SETERR(xs, HBA_BUSRESET);
6365 		}
6366 		return;
6367 
6368 	case RQCS_ABORTED:
6369 		isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6370 		FCPARAM(isp, chan)->sendmarker = 1;
6371 		if (XS_NOERR(xs)) {
6372 			XS_SETERR(xs, HBA_ABORTED);
6373 		}
6374 		return;
6375 
6376 	case RQCS_TIMEOUT:
6377 		isp_xs_prt(isp, xs, ISP_LOGWARN, "Command Timed Out");
6378 		if (XS_NOERR(xs)) {
6379 			XS_SETERR(xs, HBA_CMDTIMEOUT);
6380 		}
6381 		return;
6382 
6383 	case RQCS_DATA_OVERRUN:
6384 		XS_SET_RESID(xs, sp->req_resid);
6385 		isp_xs_prt(isp, xs, ISP_LOGERR, "Data Overrun");
6386 		if (XS_NOERR(xs)) {
6387 			XS_SETERR(xs, HBA_DATAOVR);
6388 		}
6389 		return;
6390 
6391 	case RQCS_24XX_DRE:	/* data reassembly error */
6392 		isp_prt(isp, ISP_LOGERR, "Chan %d data reassembly error for target %d", chan, XS_TGT(xs));
6393 		if (XS_NOERR(xs)) {
6394 			XS_SETERR(xs, HBA_ABORTED);
6395 		}
6396 		*rp = XS_XFRLEN(xs);
6397 		return;
6398 
6399 	case RQCS_24XX_TABORT:	/* aborted by target */
6400 		isp_prt(isp, ISP_LOGERR, "Chan %d target %d sent ABTS", chan, XS_TGT(xs));
6401 		if (XS_NOERR(xs)) {
6402 			XS_SETERR(xs, HBA_ABORTED);
6403 		}
6404 		return;
6405 
6406 	case RQCS_DATA_UNDERRUN:
6407 		ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6408 		/*
6409 		 * We can get an underrun w/o things being marked
6410 		 * if we got a non-zero status.
6411 		 */
6412 		sv_marked = (sp->req_scsi_status & (RQCS_SV|RQCS_RV)) != 0;
6413 		if ((ru_marked == 0 && sv_marked == 0) ||
6414 		    (sp->req_resid > XS_XFRLEN(xs))) {
6415 			isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6416 			if (XS_NOERR(xs)) {
6417 				XS_SETERR(xs, HBA_BOTCH);
6418 			}
6419 			return;
6420 		}
6421 		XS_SET_RESID(xs, sp->req_resid);
6422 		isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Data Underrun (%d) for command 0x%x", sp->req_resid, XS_CDBP(xs)[0] & 0xff);
6423 		if (XS_NOERR(xs)) {
6424 			XS_SETERR(xs, HBA_NOERROR);
6425 		}
6426 		return;
6427 
6428 	case RQCS_PORT_UNAVAILABLE:
6429 		/*
6430 		 * No such port on the loop. Moral equivalent of SELTIMEO
6431 		 */
6432 	case RQCS_PORT_LOGGED_OUT:
6433 	{
6434 		const char *reason;
6435 		uint8_t sts = sp->req_completion_status & 0xff;
6436 
6437 		/*
6438 		 * It was there (maybe)- treat as a selection timeout.
6439 		 */
6440 		if (sts == RQCS_PORT_UNAVAILABLE) {
6441 			reason = "unavailable";
6442 		} else {
6443 			reason = "logout";
6444 		}
6445 
6446 		isp_prt(isp, ISP_LOGINFO, "Chan %d port %s for target %d",
6447 		    chan, reason, XS_TGT(xs));
6448 
6449 		/*
6450 		 * There is no MBOX_INIT_LIP for the 24XX.
6451 		 */
6452 		if (XS_NOERR(xs)) {
6453 			XS_SETERR(xs, HBA_SELTIMEOUT);
6454 		}
6455 		return;
6456 	}
6457 	case RQCS_PORT_CHANGED:
6458 		isp_prt(isp, ISP_LOGWARN, "port changed for target %d chan %d", XS_TGT(xs), chan);
6459 		if (XS_NOERR(xs)) {
6460 			XS_SETERR(xs, HBA_SELTIMEOUT);
6461 		}
6462 		return;
6463 
6464 
6465 	case RQCS_24XX_ENOMEM:	/* f/w resource unavailable */
6466 		isp_prt(isp, ISP_LOGWARN, "f/w resource unavailable for target %d chan %d", XS_TGT(xs), chan);
6467 		if (XS_NOERR(xs)) {
6468 			*XS_STSP(xs) = SCSI_BUSY;
6469 			XS_SETERR(xs, HBA_TGTBSY);
6470 		}
6471 		return;
6472 
6473 	case RQCS_24XX_TMO:	/* task management overrun */
6474 		isp_prt(isp, ISP_LOGWARN, "command for target %d overlapped task management for chan %d", XS_TGT(xs), chan);
6475 		if (XS_NOERR(xs)) {
6476 			*XS_STSP(xs) = SCSI_BUSY;
6477 			XS_SETERR(xs, HBA_TGTBSY);
6478 		}
6479 		return;
6480 
6481 	default:
6482 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x on chan %d", sp->req_completion_status, chan);
6483 		break;
6484 	}
6485 	if (XS_NOERR(xs)) {
6486 		XS_SETERR(xs, HBA_BOTCH);
6487 	}
6488 }
6489 
6490 static void
6491 isp_fastpost_complete(ispsoftc_t *isp, uint32_t fph)
6492 {
6493 	XS_T *xs;
6494 
6495 	if (fph == 0) {
6496 		return;
6497 	}
6498 	xs = isp_find_xs(isp, fph);
6499 	if (xs == NULL) {
6500 		isp_prt(isp, ISP_LOGWARN,
6501 		    "Command for fast post handle 0x%x not found", fph);
6502 		return;
6503 	}
6504 	isp_destroy_handle(isp, fph);
6505 
6506 	/*
6507 	 * Since we don't have a result queue entry item,
6508 	 * we must believe that SCSI status is zero and
6509 	 * that all data transferred.
6510 	 */
6511 	XS_SET_RESID(xs, 0);
6512 	*XS_STSP(xs) = SCSI_GOOD;
6513 	if (XS_XFRLEN(xs)) {
6514 		ISP_DMAFREE(isp, xs, fph);
6515 	}
6516 	if (isp->isp_nactive) {
6517 		isp->isp_nactive--;
6518 	}
6519 	isp->isp_fphccmplt++;
6520 	isp_done(xs);
6521 }
6522 
6523 static int
6524 isp_mbox_continue(ispsoftc_t *isp)
6525 {
6526 	mbreg_t mbs;
6527 	uint16_t *ptr;
6528 	uint32_t offset;
6529 
6530 	switch (isp->isp_lastmbxcmd) {
6531 	case MBOX_WRITE_RAM_WORD:
6532 	case MBOX_READ_RAM_WORD:
6533 	case MBOX_WRITE_RAM_WORD_EXTENDED:
6534 	case MBOX_READ_RAM_WORD_EXTENDED:
6535 		break;
6536 	default:
6537 		return (1);
6538 	}
6539 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
6540 		isp->isp_mbxwrk0 = 0;
6541 		return (-1);
6542 	}
6543 
6544 	/*
6545 	 * Clear the previous interrupt.
6546 	 */
6547 	if (IS_24XX(isp)) {
6548 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
6549 	} else {
6550 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
6551 		ISP_WRITE(isp, BIU_SEMA, 0);
6552 	}
6553 
6554 	/*
6555 	 * Continue with next word.
6556 	 */
6557 	ISP_MEMZERO(&mbs, sizeof (mbs));
6558 	ptr = isp->isp_mbxworkp;
6559 	switch (isp->isp_lastmbxcmd) {
6560 	case MBOX_WRITE_RAM_WORD:
6561 		mbs.param[1] = isp->isp_mbxwrk1++;
6562 		mbs.param[2] = *ptr++;
6563 		break;
6564 	case MBOX_READ_RAM_WORD:
6565 		*ptr++ = isp->isp_mboxtmp[2];
6566 		mbs.param[1] = isp->isp_mbxwrk1++;
6567 		break;
6568 	case MBOX_WRITE_RAM_WORD_EXTENDED:
6569 		if (IS_24XX(isp)) {
6570 			uint32_t *lptr = (uint32_t *)ptr;
6571 			mbs.param[2] = lptr[0];
6572 			mbs.param[3] = lptr[0] >> 16;
6573 			lptr++;
6574 			ptr = (uint16_t *)lptr;
6575 		} else {
6576 			mbs.param[2] = *ptr++;
6577 		}
6578 		offset = isp->isp_mbxwrk1;
6579 		offset |= isp->isp_mbxwrk8 << 16;
6580 		mbs.param[1] = offset;
6581 		mbs.param[8] = offset >> 16;
6582 		offset++;
6583 		isp->isp_mbxwrk1 = offset;
6584 		isp->isp_mbxwrk8 = offset >> 16;
6585 		break;
6586 	case MBOX_READ_RAM_WORD_EXTENDED:
6587 		if (IS_24XX(isp)) {
6588 			uint32_t *lptr = (uint32_t *)ptr;
6589 			uint32_t val = isp->isp_mboxtmp[2];
6590 			val |= (isp->isp_mboxtmp[3]) << 16;
6591 			*lptr++ = val;
6592 			ptr = (uint16_t *)lptr;
6593 		} else {
6594 			*ptr++ = isp->isp_mboxtmp[2];
6595 		}
6596 		offset = isp->isp_mbxwrk1;
6597 		offset |= isp->isp_mbxwrk8 << 16;
6598 		mbs.param[1] = offset;
6599 		mbs.param[8] = offset >> 16;
6600 		offset++;
6601 		isp->isp_mbxwrk1 = offset;
6602 		isp->isp_mbxwrk8 = offset >> 16;
6603 		break;
6604 	}
6605 	isp->isp_mbxworkp = ptr;
6606 	isp->isp_mbxwrk0--;
6607 	mbs.param[0] = isp->isp_lastmbxcmd;
6608 	mbs.logval = MBLOGALL;
6609 	isp_mboxcmd_qnw(isp, &mbs, 0);
6610 	return (0);
6611 }
6612 
6613 #define	ISP_SCSI_IBITS(op)		(mbpscsi[((op)<<1)])
6614 #define	ISP_SCSI_OBITS(op)		(mbpscsi[((op)<<1) + 1])
6615 #define	ISP_SCSI_OPMAP(in, out)		in, out
6616 static const uint8_t mbpscsi[] = {
6617 	ISP_SCSI_OPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
6618 	ISP_SCSI_OPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
6619 	ISP_SCSI_OPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
6620 	ISP_SCSI_OPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
6621 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
6622 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
6623 	ISP_SCSI_OPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
6624 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
6625 	ISP_SCSI_OPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
6626 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x09: */
6627 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0a: */
6628 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0b: */
6629 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0c: */
6630 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0d: */
6631 	ISP_SCSI_OPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
6632 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0f: */
6633 	ISP_SCSI_OPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
6634 	ISP_SCSI_OPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
6635 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
6636 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
6637 	ISP_SCSI_OPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
6638 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
6639 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
6640 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
6641 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
6642 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
6643 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
6644 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
6645 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
6646 	ISP_SCSI_OPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
6647 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x1e: */
6648 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
6649 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
6650 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
6651 	ISP_SCSI_OPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
6652 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
6653 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
6654 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
6655 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
6656 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
6657 	ISP_SCSI_OPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
6658 	ISP_SCSI_OPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
6659 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
6660 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2b: */
6661 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2c: */
6662 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2d: */
6663 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2e: */
6664 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2f: */
6665 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
6666 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
6667 	ISP_SCSI_OPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
6668 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
6669 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
6670 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
6671 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
6672 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
6673 	ISP_SCSI_OPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
6674 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
6675 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
6676 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3b: */
6677 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3c: */
6678 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3d: */
6679 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3e: */
6680 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3f: */
6681 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
6682 	ISP_SCSI_OPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
6683 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
6684 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x43: */
6685 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x44: */
6686 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
6687 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
6688 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x47: */
6689 	ISP_SCSI_OPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
6690 	ISP_SCSI_OPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
6691 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
6692 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
6693 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4c: */
6694 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4d: */
6695 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4e: */
6696 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4f: */
6697 	ISP_SCSI_OPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
6698 	ISP_SCSI_OPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
6699 	ISP_SCSI_OPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
6700 	ISP_SCSI_OPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
6701 	ISP_SCSI_OPMAP(0xcf, 0x01),	/* 0x54: EXECUCUTE COMMAND IOCB A64 */
6702 	ISP_SCSI_OPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
6703 	ISP_SCSI_OPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
6704 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x57: */
6705 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x58: */
6706 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x59: */
6707 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
6708 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
6709 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
6710 	ISP_SCSI_OPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
6711 };
6712 #define	MAX_SCSI_OPCODE	0x5d
6713 
6714 static const char *scsi_mbcmd_names[] = {
6715 	"NO-OP",
6716 	"LOAD RAM",
6717 	"EXEC FIRMWARE",
6718 	"DUMP RAM",
6719 	"WRITE RAM WORD",
6720 	"READ RAM WORD",
6721 	"MAILBOX REG TEST",
6722 	"VERIFY CHECKSUM",
6723 	"ABOUT FIRMWARE",
6724 	NULL,
6725 	NULL,
6726 	NULL,
6727 	NULL,
6728 	NULL,
6729 	"CHECK FIRMWARE",
6730 	NULL,
6731 	"INIT REQUEST QUEUE",
6732 	"INIT RESULT QUEUE",
6733 	"EXECUTE IOCB",
6734 	"WAKE UP",
6735 	"STOP FIRMWARE",
6736 	"ABORT",
6737 	"ABORT DEVICE",
6738 	"ABORT TARGET",
6739 	"BUS RESET",
6740 	"STOP QUEUE",
6741 	"START QUEUE",
6742 	"SINGLE STEP QUEUE",
6743 	"ABORT QUEUE",
6744 	"GET DEV QUEUE STATUS",
6745 	NULL,
6746 	"GET FIRMWARE STATUS",
6747 	"GET INIT SCSI ID",
6748 	"GET SELECT TIMEOUT",
6749 	"GET RETRY COUNT",
6750 	"GET TAG AGE LIMIT",
6751 	"GET CLOCK RATE",
6752 	"GET ACT NEG STATE",
6753 	"GET ASYNC DATA SETUP TIME",
6754 	"GET PCI PARAMS",
6755 	"GET TARGET PARAMS",
6756 	"GET DEV QUEUE PARAMS",
6757 	"GET RESET DELAY PARAMS",
6758 	NULL,
6759 	NULL,
6760 	NULL,
6761 	NULL,
6762 	NULL,
6763 	"SET INIT SCSI ID",
6764 	"SET SELECT TIMEOUT",
6765 	"SET RETRY COUNT",
6766 	"SET TAG AGE LIMIT",
6767 	"SET CLOCK RATE",
6768 	"SET ACT NEG STATE",
6769 	"SET ASYNC DATA SETUP TIME",
6770 	"SET PCI CONTROL PARAMS",
6771 	"SET TARGET PARAMS",
6772 	"SET DEV QUEUE PARAMS",
6773 	"SET RESET DELAY PARAMS",
6774 	NULL,
6775 	NULL,
6776 	NULL,
6777 	NULL,
6778 	NULL,
6779 	"RETURN BIOS BLOCK ADDR",
6780 	"WRITE FOUR RAM WORDS",
6781 	"EXEC BIOS IOCB",
6782 	NULL,
6783 	NULL,
6784 	"SET SYSTEM PARAMETER",
6785 	"GET SYSTEM PARAMETER",
6786 	NULL,
6787 	"GET SCAM CONFIGURATION",
6788 	"SET SCAM CONFIGURATION",
6789 	"SET FIRMWARE FEATURES",
6790 	"GET FIRMWARE FEATURES",
6791 	NULL,
6792 	NULL,
6793 	NULL,
6794 	NULL,
6795 	"LOAD RAM A64",
6796 	"DUMP RAM A64",
6797 	"INITIALIZE REQUEST QUEUE A64",
6798 	"INITIALIZE RESPONSE QUEUE A64",
6799 	"EXECUTE IOCB A64",
6800 	"ENABLE TARGET MODE",
6801 	"GET TARGET MODE STATE",
6802 	NULL,
6803 	NULL,
6804 	NULL,
6805 	"SET DATA OVERRUN RECOVERY MODE",
6806 	"GET DATA OVERRUN RECOVERY MODE",
6807 	"SET HOST DATA",
6808 	"GET NOST DATA",
6809 };
6810 
6811 #define	ISP_FC_IBITS(op)	((mbpfc[((op)<<3) + 0] << 24) | (mbpfc[((op)<<3) + 1] << 16) | (mbpfc[((op)<<3) + 2] << 8) | (mbpfc[((op)<<3) + 3]))
6812 #define	ISP_FC_OBITS(op)	((mbpfc[((op)<<3) + 4] << 24) | (mbpfc[((op)<<3) + 5] << 16) | (mbpfc[((op)<<3) + 6] << 8) | (mbpfc[((op)<<3) + 7]))
6813 
6814 #define	ISP_FC_OPMAP(in0, out0)							  0,   0,   0, in0,    0,    0,    0, out0
6815 #define	ISP_FC_OPMAP_HALF(in1, in0, out1, out0)					  0,   0, in1, in0,    0,    0, out1, out0
6816 #define	ISP_FC_OPMAP_FULL(in3, in2, in1, in0, out3, out2, out1, out0)		in3, in2, in1, in0, out3, out2, out1, out0
6817 static const uint32_t mbpfc[] = {
6818 	ISP_FC_OPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
6819 	ISP_FC_OPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
6820 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
6821 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
6822 	ISP_FC_OPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
6823 	ISP_FC_OPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
6824 	ISP_FC_OPMAP_FULL(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
6825 	ISP_FC_OPMAP(0x07, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
6826 	ISP_FC_OPMAP_FULL(0x0, 0x0, 0x0, 0x01, 0x0, 0x3, 0x80, 0x7f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
6827 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x09: MBOX_LOAD_RISC_RAM_2100 */
6828 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
6829 	ISP_FC_OPMAP_HALF(0x1, 0xff, 0x0, 0x01),	/* 0x0b: MBOX_LOAD_RISC_RAM */
6830 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x0c: */
6831 	ISP_FC_OPMAP_HALF(0x1, 0x0f, 0x0, 0x01),	/* 0x0d: MBOX_WRITE_RAM_WORD_EXTENDED */
6832 	ISP_FC_OPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
6833 	ISP_FC_OPMAP_HALF(0x1, 0x03, 0x0, 0x0d),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED */
6834 	ISP_FC_OPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
6835 	ISP_FC_OPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
6836 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
6837 	ISP_FC_OPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
6838 	ISP_FC_OPMAP_HALF(0x1, 0xff, 0x0, 0x03),	/* 0x14: MBOX_STOP_FIRMWARE */
6839 	ISP_FC_OPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
6840 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
6841 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
6842 	ISP_FC_OPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
6843 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
6844 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
6845 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
6846 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
6847 	ISP_FC_OPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
6848 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x1e: */
6849 	ISP_FC_OPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
6850 	ISP_FC_OPMAP_HALF(0x2, 0x01, 0x7e, 0xcf),	/* 0x20: MBOX_GET_LOOP_ID */
6851 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x21: */
6852 	ISP_FC_OPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
6853 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x23: */
6854 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x24: */
6855 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x25: */
6856 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x26: */
6857 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x27: */
6858 	ISP_FC_OPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
6859 	ISP_FC_OPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
6860 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2a: */
6861 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2b: */
6862 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2c: */
6863 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2d: */
6864 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2e: */
6865 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2f: */
6866 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x30: */
6867 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x31: */
6868 	ISP_FC_OPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
6869 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x33: */
6870 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x34: */
6871 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x35: */
6872 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x36: */
6873 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x37: */
6874 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
6875 	ISP_FC_OPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
6876 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3a: */
6877 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3b: */
6878 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3c: */
6879 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3d: */
6880 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3e: */
6881 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3f: */
6882 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
6883 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
6884 	ISP_FC_OPMAP_HALF(0x0, 0x01, 0x3, 0xcf),	/* 0x42: MBOX_GET_RESOURCE_COUNT */
6885 	ISP_FC_OPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_OFFLINE_MODE */
6886 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x44: */
6887 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x45: */
6888 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x46: */
6889 	ISP_FC_OPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
6890 	ISP_FC_OPMAP(0xcf, 0x0f),	/* 0x48: MBOX_INIT_FIRMWARE_MULTI_ID */
6891 	ISP_FC_OPMAP(0xcd, 0x01),	/* 0x49: MBOX_GET_VP_DATABASE */
6892 	ISP_FC_OPMAP_HALF(0x2, 0xcd, 0x0, 0x01),	/* 0x4a: MBOX_GET_VP_DATABASE_ENTRY */
6893 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4b: */
6894 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4c: */
6895 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4d: */
6896 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4e: */
6897 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4f: */
6898 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x50: */
6899 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x51: */
6900 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x52: */
6901 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x53: */
6902 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
6903 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x55: */
6904 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x56: */
6905 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x57: */
6906 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x58: */
6907 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x59: */
6908 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x5a: */
6909 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
6910 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
6911 	ISP_FC_OPMAP(0x07, 0x1f),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
6912 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x5e: */
6913 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x5f: */
6914 	ISP_FC_OPMAP(0xcf, 0x0f),	/* 0x60: MBOX_INIT_FIRMWARE */
6915 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x61: */
6916 	ISP_FC_OPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
6917 	ISP_FC_OPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
6918 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
6919 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
6920 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
6921 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
6922 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
6923 	ISP_FC_OPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
6924 	ISP_FC_OPMAP_HALF(0x6, 0x03, 0x0, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
6925 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
6926 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
6927 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x6d: */
6928 	ISP_FC_OPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
6929 	ISP_FC_OPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
6930 	ISP_FC_OPMAP_HALF(0x02, 0x03, 0x00, 0x03),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
6931 	ISP_FC_OPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
6932 	ISP_FC_OPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
6933 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x73: */
6934 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
6935 	ISP_FC_OPMAP_HALF(0x03, 0xcf, 0x00, 0x07),	/* 0x75: GET PORT/NODE NAME LIST */
6936 	ISP_FC_OPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
6937 	ISP_FC_OPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
6938 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x78: */
6939 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x79: */
6940 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x7a: */
6941 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x7b: */
6942 	ISP_FC_OPMAP_HALF(0x03, 0x4f, 0x00, 0x07),	/* 0x7c: Get ID List */
6943 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
6944 	ISP_FC_OPMAP(0x0f, 0x01)	/* 0x7e: LUN RESET */
6945 };
6946 #define	MAX_FC_OPCODE	0x7e
6947 /*
6948  * Footnotes
6949  *
6950  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
6951  *	do not access at this time in the core driver. The caller is
6952  *	responsible for setting this register first (Gross!). The assumption
6953  *	is that we won't overflow.
6954  */
6955 
6956 static const char *fc_mbcmd_names[] = {
6957 	"NO-OP",			/* 00h */
6958 	"LOAD RAM",
6959 	"EXEC FIRMWARE",
6960 	"DUMP RAM",
6961 	"WRITE RAM WORD",
6962 	"READ RAM WORD",
6963 	"MAILBOX REG TEST",
6964 	"VERIFY CHECKSUM",
6965 	"ABOUT FIRMWARE",
6966 	"LOAD RAM (2100)",
6967 	"DUMP RAM",
6968 	"LOAD RISC RAM",
6969 	"DUMP RISC RAM",
6970 	"WRITE RAM WORD EXTENDED",
6971 	"CHECK FIRMWARE",
6972 	"READ RAM WORD EXTENDED",
6973 	"INIT REQUEST QUEUE",		/* 10h */
6974 	"INIT RESULT QUEUE",
6975 	"EXECUTE IOCB",
6976 	"WAKE UP",
6977 	"STOP FIRMWARE",
6978 	"ABORT",
6979 	"ABORT DEVICE",
6980 	"ABORT TARGET",
6981 	"BUS RESET",
6982 	"STOP QUEUE",
6983 	"START QUEUE",
6984 	"SINGLE STEP QUEUE",
6985 	"ABORT QUEUE",
6986 	"GET DEV QUEUE STATUS",
6987 	NULL,
6988 	"GET FIRMWARE STATUS",
6989 	"GET LOOP ID",			/* 20h */
6990 	NULL,
6991 	"GET TIMEOUT PARAMS",
6992 	NULL,
6993 	NULL,
6994 	NULL,
6995 	NULL,
6996 	NULL,
6997 	"GET FIRMWARE OPTIONS",
6998 	"GET PORT QUEUE PARAMS",
6999 	"GENERATE SYSTEM ERROR",
7000 	NULL,
7001 	NULL,
7002 	NULL,
7003 	NULL,
7004 	NULL,
7005 	"WRITE SFP",			/* 30h */
7006 	"READ SFP",
7007 	"SET TIMEOUT PARAMS",
7008 	NULL,
7009 	NULL,
7010 	NULL,
7011 	NULL,
7012 	NULL,
7013 	"SET FIRMWARE OPTIONS",
7014 	"SET PORT QUEUE PARAMS",
7015 	NULL,
7016 	"SET FC LED CONF",
7017 	NULL,
7018 	"RESTART NIC FIRMWARE",
7019 	"ACCESS CONTROL",
7020 	NULL,
7021 	"LOOP PORT BYPASS",		/* 40h */
7022 	"LOOP PORT ENABLE",
7023 	"GET RESOURCE COUNT",
7024 	"REQUEST NON PARTICIPATING MODE",
7025 	"DIAGNOSTIC ECHO TEST",
7026 	"DIAGNOSTIC LOOPBACK",
7027 	NULL,
7028 	"GET PORT DATABASE ENHANCED",
7029 	"INIT FIRMWARE MULTI ID",
7030 	"GET VP DATABASE",
7031 	"GET VP DATABASE ENTRY",
7032 	NULL,
7033 	NULL,
7034 	NULL,
7035 	NULL,
7036 	NULL,
7037 	"GET FCF LIST",			/* 50h */
7038 	"GET DCBX PARAMETERS",
7039 	NULL,
7040 	"HOST MEMORY COPY",
7041 	"EXECUTE IOCB A64",
7042 	NULL,
7043 	NULL,
7044 	"SEND RNID",
7045 	NULL,
7046 	"SET PARAMETERS",
7047 	"GET PARAMETERS",
7048 	"DRIVER HEARTBEAT",
7049 	"FIRMWARE HEARTBEAT",
7050 	"GET/SET DATA RATE",
7051 	"SEND RNFT",
7052 	NULL,
7053 	"INIT FIRMWARE",		/* 60h */
7054 	"GET INIT CONTROL BLOCK",
7055 	"INIT LIP",
7056 	"GET FC-AL POSITION MAP",
7057 	"GET PORT DATABASE",
7058 	"CLEAR ACA",
7059 	"TARGET RESET",
7060 	"CLEAR TASK SET",
7061 	"ABORT TASK SET",
7062 	"GET FW STATE",
7063 	"GET PORT NAME",
7064 	"GET LINK STATUS",
7065 	"INIT LIP RESET",
7066 	"GET LINK STATS & PRIVATE DATA CNTS",
7067 	"SEND SNS",
7068 	"FABRIC LOGIN",
7069 	"SEND CHANGE REQUEST",		/* 70h */
7070 	"FABRIC LOGOUT",
7071 	"INIT LIP LOGIN",
7072 	NULL,
7073 	"LOGIN LOOP PORT",
7074 	"GET PORT/NODE NAME LIST",
7075 	"SET VENDOR ID",
7076 	"INITIALIZE IP MAILBOX",
7077 	NULL,
7078 	NULL,
7079 	"GET XGMAC STATS",
7080 	NULL,
7081 	"GET ID LIST",
7082 	"SEND LFA",
7083 	"LUN RESET"
7084 };
7085 
7086 static void
7087 isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
7088 {
7089 	unsigned int ibits, obits, box, opcode;
7090 
7091 	opcode = mbp->param[0];
7092 	if (IS_FC(isp)) {
7093 		ibits = ISP_FC_IBITS(opcode);
7094 		obits = ISP_FC_OBITS(opcode);
7095 	} else {
7096 		ibits = ISP_SCSI_IBITS(opcode);
7097 		obits = ISP_SCSI_OBITS(opcode);
7098 	}
7099 	ibits |= mbp->ibits;
7100 	obits |= mbp->obits;
7101 	for (box = 0; box < ISP_NMBOX(isp); box++) {
7102 		if (ibits & (1 << box)) {
7103 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7104 		}
7105 		if (nodelay == 0) {
7106 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7107 		}
7108 	}
7109 	if (nodelay == 0) {
7110 		isp->isp_lastmbxcmd = opcode;
7111 		isp->isp_obits = obits;
7112 		isp->isp_mboxbsy = 1;
7113 	}
7114 	if (IS_24XX(isp)) {
7115 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7116 	} else {
7117 		ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7118 	}
7119 	/*
7120 	 * Oddly enough, if we're not delaying for an answer,
7121 	 * delay a bit to give the f/w a chance to pick up the
7122 	 * command.
7123 	 */
7124 	if (nodelay) {
7125 		ISP_DELAY(1000);
7126 	}
7127 }
7128 
7129 static void
7130 isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp)
7131 {
7132 	const char *cname, *xname, *sname;
7133 	char tname[16], mname[16];
7134 	unsigned int ibits, obits, box, opcode;
7135 
7136 	opcode = mbp->param[0];
7137 	if (IS_FC(isp)) {
7138 		if (opcode > MAX_FC_OPCODE) {
7139 			mbp->param[0] = MBOX_INVALID_COMMAND;
7140 			isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7141 			return;
7142 		}
7143 		cname = fc_mbcmd_names[opcode];
7144 		ibits = ISP_FC_IBITS(opcode);
7145 		obits = ISP_FC_OBITS(opcode);
7146 	} else {
7147 		if (opcode > MAX_SCSI_OPCODE) {
7148 			mbp->param[0] = MBOX_INVALID_COMMAND;
7149 			isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7150 			return;
7151 		}
7152 		cname = scsi_mbcmd_names[opcode];
7153 		ibits = ISP_SCSI_IBITS(opcode);
7154 		obits = ISP_SCSI_OBITS(opcode);
7155 	}
7156 	if (cname == NULL) {
7157 		cname = tname;
7158 		ISP_SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
7159 	}
7160 	isp_prt(isp, ISP_LOGDEBUG3, "Mailbox Command '%s'", cname);
7161 
7162 	/*
7163 	 * Pick up any additional bits that the caller might have set.
7164 	 */
7165 	ibits |= mbp->ibits;
7166 	obits |= mbp->obits;
7167 
7168 	/*
7169 	 * Mask any bits that the caller wants us to mask
7170 	 */
7171 	ibits &= mbp->ibitm;
7172 	obits &= mbp->obitm;
7173 
7174 
7175 	if (ibits == 0 && obits == 0) {
7176 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
7177 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
7178 		return;
7179 	}
7180 
7181 	/*
7182 	 * Get exclusive usage of mailbox registers.
7183 	 */
7184 	if (MBOX_ACQUIRE(isp)) {
7185 		mbp->param[0] = MBOX_REGS_BUSY;
7186 		goto out;
7187 	}
7188 
7189 	for (box = 0; box < ISP_NMBOX(isp); box++) {
7190 		if (ibits & (1 << box)) {
7191 			isp_prt(isp, ISP_LOGDEBUG3, "IN mbox %d = 0x%04x", box,
7192 			    mbp->param[box]);
7193 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7194 		}
7195 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7196 	}
7197 
7198 	isp->isp_lastmbxcmd = opcode;
7199 
7200 	/*
7201 	 * We assume that we can't overwrite a previous command.
7202 	 */
7203 	isp->isp_obits = obits;
7204 	isp->isp_mboxbsy = 1;
7205 
7206 	/*
7207 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
7208 	 */
7209 	if (IS_24XX(isp)) {
7210 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7211 	} else {
7212 		ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7213 	}
7214 
7215 	/*
7216 	 * While we haven't finished the command, spin our wheels here.
7217 	 */
7218 	MBOX_WAIT_COMPLETE(isp, mbp);
7219 
7220 	/*
7221 	 * Did the command time out?
7222 	 */
7223 	if (mbp->param[0] == MBOX_TIMEOUT) {
7224 		isp->isp_mboxbsy = 0;
7225 		MBOX_RELEASE(isp);
7226 		goto out;
7227 	}
7228 
7229 	/*
7230 	 * Copy back output registers.
7231 	 */
7232 	for (box = 0; box < ISP_NMBOX(isp); box++) {
7233 		if (obits & (1 << box)) {
7234 			mbp->param[box] = isp->isp_mboxtmp[box];
7235 			isp_prt(isp, ISP_LOGDEBUG3, "OUT mbox %d = 0x%04x", box,
7236 			    mbp->param[box]);
7237 		}
7238 	}
7239 
7240 	isp->isp_mboxbsy = 0;
7241 	MBOX_RELEASE(isp);
7242 out:
7243 	if (mbp->logval == 0 || mbp->param[0] == MBOX_COMMAND_COMPLETE)
7244 		return;
7245 
7246 	if ((mbp->param[0] & 0xbfe0) == 0 &&
7247 	    (mbp->logval & MBLOGMASK(mbp->param[0])) == 0)
7248 		return;
7249 
7250 	xname = NULL;
7251 	sname = "";
7252 	switch (mbp->param[0]) {
7253 	case MBOX_INVALID_COMMAND:
7254 		xname = "INVALID COMMAND";
7255 		break;
7256 	case MBOX_HOST_INTERFACE_ERROR:
7257 		xname = "HOST INTERFACE ERROR";
7258 		break;
7259 	case MBOX_TEST_FAILED:
7260 		xname = "TEST FAILED";
7261 		break;
7262 	case MBOX_COMMAND_ERROR:
7263 		xname = "COMMAND ERROR";
7264 		ISP_SNPRINTF(mname, sizeof(mname), " subcode 0x%x",
7265 		    mbp->param[1]);
7266 		sname = mname;
7267 		break;
7268 	case MBOX_COMMAND_PARAM_ERROR:
7269 		xname = "COMMAND PARAMETER ERROR";
7270 		break;
7271 	case MBOX_PORT_ID_USED:
7272 		xname = "PORT ID ALREADY IN USE";
7273 		break;
7274 	case MBOX_LOOP_ID_USED:
7275 		xname = "LOOP ID ALREADY IN USE";
7276 		break;
7277 	case MBOX_ALL_IDS_USED:
7278 		xname = "ALL LOOP IDS IN USE";
7279 		break;
7280 	case MBOX_NOT_LOGGED_IN:
7281 		xname = "NOT LOGGED IN";
7282 		break;
7283 	case MBOX_LINK_DOWN_ERROR:
7284 		xname = "LINK DOWN ERROR";
7285 		break;
7286 	case MBOX_LOOPBACK_ERROR:
7287 		xname = "LOOPBACK ERROR";
7288 		break;
7289 	case MBOX_CHECKSUM_ERROR:
7290 		xname = "CHECKSUM ERROR";
7291 		break;
7292 	case MBOX_INVALID_PRODUCT_KEY:
7293 		xname = "INVALID PRODUCT KEY";
7294 		break;
7295 	case MBOX_REGS_BUSY:
7296 		xname = "REGISTERS BUSY";
7297 		break;
7298 	case MBOX_TIMEOUT:
7299 		xname = "TIMEOUT";
7300 		break;
7301 	default:
7302 		ISP_SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
7303 		xname = mname;
7304 		break;
7305 	}
7306 	if (xname) {
7307 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s%s)",
7308 		    cname, xname, sname);
7309 	}
7310 }
7311 
7312 static int
7313 isp_fw_state(ispsoftc_t *isp, int chan)
7314 {
7315 	if (IS_FC(isp)) {
7316 		mbreg_t mbs;
7317 
7318 		MBSINIT(&mbs, MBOX_GET_FW_STATE, MBLOGALL, 0);
7319 		isp_mboxcmd(isp, &mbs);
7320 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
7321 			return (mbs.param[1]);
7322 		}
7323 	}
7324 	return (FW_ERROR);
7325 }
7326 
7327 static void
7328 isp_spi_update(ispsoftc_t *isp, int chan)
7329 {
7330 	int tgt;
7331 	mbreg_t mbs;
7332 	sdparam *sdp;
7333 
7334 	if (IS_FC(isp)) {
7335 		/*
7336 		 * There are no 'per-bus' settings for Fibre Channel.
7337 		 */
7338 		return;
7339 	}
7340 	sdp = SDPARAM(isp, chan);
7341 	sdp->update = 0;
7342 
7343 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7344 		uint16_t flags, period, offset;
7345 		int get;
7346 
7347 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
7348 			sdp->isp_devparam[tgt].dev_update = 0;
7349 			sdp->isp_devparam[tgt].dev_refresh = 0;
7350 			isp_prt(isp, ISP_LOGDEBUG0, "skipping target %d bus %d update", tgt, chan);
7351 			continue;
7352 		}
7353 		/*
7354 		 * If the goal is to update the status of the device,
7355 		 * take what's in goal_flags and try and set the device
7356 		 * toward that. Otherwise, if we're just refreshing the
7357 		 * current device state, get the current parameters.
7358 		 */
7359 
7360 		MBSINIT(&mbs, 0, MBLOGALL, 0);
7361 
7362 		/*
7363 		 * Refresh overrides set
7364 		 */
7365 		if (sdp->isp_devparam[tgt].dev_refresh) {
7366 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
7367 			get = 1;
7368 		} else if (sdp->isp_devparam[tgt].dev_update) {
7369 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
7370 
7371 			/*
7372 			 * Make sure goal_flags has "Renegotiate on Error"
7373 			 * on and "Freeze Queue on Error" off.
7374 			 */
7375 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
7376 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
7377 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
7378 
7379 			/*
7380 			 * Insist that PARITY must be enabled
7381 			 * if SYNC or WIDE is enabled.
7382 			 */
7383 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
7384 				mbs.param[2] |= DPARM_PARITY;
7385 			}
7386 
7387 			if (mbs.param[2] & DPARM_SYNC) {
7388 				mbs.param[3] =
7389 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
7390 				    (sdp->isp_devparam[tgt].goal_period);
7391 			}
7392 			/*
7393 			 * A command completion later that has
7394 			 * RQSTF_NEGOTIATION set can cause
7395 			 * the dev_refresh/announce cycle also.
7396 			 *
7397 			 * Note: It is really important to update our current
7398 			 * flags with at least the state of TAG capabilities-
7399 			 * otherwise we might try and send a tagged command
7400 			 * when we have it all turned off. So change it here
7401 			 * to say that current already matches goal.
7402 			 */
7403 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
7404 			sdp->isp_devparam[tgt].actv_flags |=
7405 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
7406 			isp_prt(isp, ISP_LOGDEBUG0, "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
7407 			    chan, tgt, mbs.param[2], mbs.param[3] >> 8, mbs.param[3] & 0xff);
7408 			get = 0;
7409 		} else {
7410 			continue;
7411 		}
7412 		mbs.param[1] = (chan << 15) | (tgt << 8);
7413 		isp_mboxcmd(isp, &mbs);
7414 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7415 			continue;
7416 		}
7417 		if (get == 0) {
7418 			sdp->sendmarker = 1;
7419 			sdp->isp_devparam[tgt].dev_update = 0;
7420 			sdp->isp_devparam[tgt].dev_refresh = 1;
7421 		} else {
7422 			sdp->isp_devparam[tgt].dev_refresh = 0;
7423 			flags = mbs.param[2];
7424 			period = mbs.param[3] & 0xff;
7425 			offset = mbs.param[3] >> 8;
7426 			sdp->isp_devparam[tgt].actv_flags = flags;
7427 			sdp->isp_devparam[tgt].actv_period = period;
7428 			sdp->isp_devparam[tgt].actv_offset = offset;
7429 			isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, chan, tgt);
7430 		}
7431 	}
7432 
7433 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7434 		if (sdp->isp_devparam[tgt].dev_update ||
7435 		    sdp->isp_devparam[tgt].dev_refresh) {
7436 			sdp->update = 1;
7437 			break;
7438 		}
7439 	}
7440 }
7441 
7442 static void
7443 isp_setdfltsdparm(ispsoftc_t *isp)
7444 {
7445 	int tgt;
7446 	sdparam *sdp, *sdp1;
7447 
7448 	sdp = SDPARAM(isp, 0);
7449 	sdp->role = GET_DEFAULT_ROLE(isp, 0);
7450 	if (IS_DUALBUS(isp)) {
7451 		sdp1 = sdp + 1;
7452 		sdp1->role = GET_DEFAULT_ROLE(isp, 1);
7453 	} else {
7454 		sdp1 = NULL;
7455 	}
7456 
7457 	/*
7458 	 * Establish some default parameters.
7459 	 */
7460 	sdp->isp_cmd_dma_burst_enable = 0;
7461 	sdp->isp_data_dma_burst_enabl = 1;
7462 	sdp->isp_fifo_threshold = 0;
7463 	sdp->isp_initiator_id = DEFAULT_IID(isp, 0);
7464 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
7465 		sdp->isp_async_data_setup = 9;
7466 	} else {
7467 		sdp->isp_async_data_setup = 6;
7468 	}
7469 	sdp->isp_selection_timeout = 250;
7470 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
7471 	sdp->isp_tag_aging = 8;
7472 	sdp->isp_bus_reset_delay = 5;
7473 	/*
7474 	 * Don't retry selection, busy or queue full automatically- reflect
7475 	 * these back to us.
7476 	 */
7477 	sdp->isp_retry_count = 0;
7478 	sdp->isp_retry_delay = 0;
7479 
7480 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7481 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
7482 		sdp->isp_devparam[tgt].dev_enable = 1;
7483 	}
7484 
7485 	/*
7486 	 * The trick here is to establish a default for the default (honk!)
7487 	 * state (goal_flags). Then try and get the current status from
7488 	 * the card to fill in the current state. We don't, in fact, set
7489 	 * the default to the SAFE default state- that's not the goal state.
7490 	 */
7491 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7492 		uint8_t off, per;
7493 		sdp->isp_devparam[tgt].actv_offset = 0;
7494 		sdp->isp_devparam[tgt].actv_period = 0;
7495 		sdp->isp_devparam[tgt].actv_flags = 0;
7496 
7497 		sdp->isp_devparam[tgt].goal_flags =
7498 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
7499 
7500 		/*
7501 		 * We default to Wide/Fast for versions less than a 1040
7502 		 * (unless it's SBus).
7503 		 */
7504 		if (IS_ULTRA3(isp)) {
7505 			off = ISP_80M_SYNCPARMS >> 8;
7506 			per = ISP_80M_SYNCPARMS & 0xff;
7507 		} else if (IS_ULTRA2(isp)) {
7508 			off = ISP_40M_SYNCPARMS >> 8;
7509 			per = ISP_40M_SYNCPARMS & 0xff;
7510 		} else if (IS_1240(isp)) {
7511 			off = ISP_20M_SYNCPARMS >> 8;
7512 			per = ISP_20M_SYNCPARMS & 0xff;
7513 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
7514 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
7515 		    (isp->isp_bustype == ISP_BT_PCI &&
7516 		    isp->isp_type < ISP_HA_SCSI_1040) ||
7517 		    (isp->isp_clock && isp->isp_clock < 60) ||
7518 		    (sdp->isp_ultramode == 0)) {
7519 			off = ISP_10M_SYNCPARMS >> 8;
7520 			per = ISP_10M_SYNCPARMS & 0xff;
7521 		} else {
7522 			off = ISP_20M_SYNCPARMS_1040 >> 8;
7523 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
7524 		}
7525 		sdp->isp_devparam[tgt].goal_offset =
7526 		    sdp->isp_devparam[tgt].nvrm_offset = off;
7527 		sdp->isp_devparam[tgt].goal_period =
7528 		    sdp->isp_devparam[tgt].nvrm_period = per;
7529 
7530 	}
7531 
7532 	/*
7533 	 * If we're a dual bus card, just copy the data over
7534 	 */
7535 	if (sdp1) {
7536 		*sdp1 = *sdp;
7537 		sdp1->isp_initiator_id = DEFAULT_IID(isp, 1);
7538 	}
7539 
7540 	/*
7541 	 * If we've not been told to avoid reading NVRAM, try and read it.
7542 	 * If we're successful reading it, we can then return because NVRAM
7543 	 * will tell us what the desired settings are. Otherwise, we establish
7544 	 * some reasonable 'fake' nvram and goal defaults.
7545 	 */
7546 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7547 		mbreg_t mbs;
7548 
7549 		if (isp_read_nvram(isp, 0) == 0) {
7550 			if (IS_DUALBUS(isp)) {
7551 				if (isp_read_nvram(isp, 1) == 0) {
7552 					return;
7553 				}
7554 			}
7555 		}
7556 		MBSINIT(&mbs, MBOX_GET_ACT_NEG_STATE, MBLOGNONE, 0);
7557 		isp_mboxcmd(isp, &mbs);
7558 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7559 			sdp->isp_req_ack_active_neg = 1;
7560 			sdp->isp_data_line_active_neg = 1;
7561 			if (sdp1) {
7562 				sdp1->isp_req_ack_active_neg = 1;
7563 				sdp1->isp_data_line_active_neg = 1;
7564 			}
7565 		} else {
7566 			sdp->isp_req_ack_active_neg =
7567 			    (mbs.param[1] >> 4) & 0x1;
7568 			sdp->isp_data_line_active_neg =
7569 			    (mbs.param[1] >> 5) & 0x1;
7570 			if (sdp1) {
7571 				sdp1->isp_req_ack_active_neg =
7572 				    (mbs.param[2] >> 4) & 0x1;
7573 				sdp1->isp_data_line_active_neg =
7574 				    (mbs.param[2] >> 5) & 0x1;
7575 			}
7576 		}
7577 	}
7578 
7579 }
7580 
7581 static void
7582 isp_setdfltfcparm(ispsoftc_t *isp, int chan)
7583 {
7584 	fcparam *fcp = FCPARAM(isp, chan);
7585 
7586 	/*
7587 	 * Establish some default parameters.
7588 	 */
7589 	fcp->role = GET_DEFAULT_ROLE(isp, chan);
7590 	fcp->isp_maxalloc = ICB_DFLT_ALLOC;
7591 	fcp->isp_retry_delay = ICB_DFLT_RDELAY;
7592 	fcp->isp_retry_count = ICB_DFLT_RCOUNT;
7593 	fcp->isp_loopid = DEFAULT_LOOPID(isp, chan);
7594 	fcp->isp_wwnn_nvram = DEFAULT_NODEWWN(isp, chan);
7595 	fcp->isp_wwpn_nvram = DEFAULT_PORTWWN(isp, chan);
7596 	fcp->isp_fwoptions = 0;
7597 	fcp->isp_lasthdl = NIL_HANDLE;
7598 
7599 	if (IS_24XX(isp)) {
7600 		fcp->isp_fwoptions |= ICB2400_OPT1_FAIRNESS;
7601 		fcp->isp_fwoptions |= ICB2400_OPT1_HARD_ADDRESS;
7602 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7603 			fcp->isp_fwoptions |= ICB2400_OPT1_FULL_DUPLEX;
7604 		}
7605 		fcp->isp_fwoptions |= ICB2400_OPT1_BOTH_WWNS;
7606 	} else {
7607 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
7608 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
7609 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
7610 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7611 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
7612 		}
7613 		/*
7614 		 * Make sure this is turned off now until we get
7615 		 * extended options from NVRAM
7616 		 */
7617 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
7618 	}
7619 
7620 
7621 	/*
7622 	 * Now try and read NVRAM unless told to not do so.
7623 	 * This will set fcparam's isp_wwnn_nvram && isp_wwpn_nvram.
7624 	 */
7625 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7626 		int i, j = 0;
7627 		/*
7628 		 * Give a couple of tries at reading NVRAM.
7629 		 */
7630 		for (i = 0; i < 2; i++) {
7631 			j = isp_read_nvram(isp, chan);
7632 			if (j == 0) {
7633 				break;
7634 			}
7635 		}
7636 		if (j) {
7637 			isp->isp_confopts |= ISP_CFG_NONVRAM;
7638 		}
7639 	}
7640 
7641 	fcp->isp_wwnn = ACTIVE_NODEWWN(isp, chan);
7642 	fcp->isp_wwpn = ACTIVE_PORTWWN(isp, chan);
7643 	isp_prt(isp, ISP_LOGCONFIG, "Chan %d 0x%08x%08x/0x%08x%08x Role %s",
7644 	    chan, (uint32_t) (fcp->isp_wwnn >> 32), (uint32_t) (fcp->isp_wwnn),
7645 	    (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) (fcp->isp_wwpn),
7646 	    isp_class3_roles[fcp->role]);
7647 }
7648 
7649 /*
7650  * Re-initialize the ISP and complete all orphaned commands
7651  * with a 'botched' notice. The reset/init routines should
7652  * not disturb an already active list of commands.
7653  */
7654 
7655 int
7656 isp_reinit(ispsoftc_t *isp, int do_load_defaults)
7657 {
7658 	int i, res = 0;
7659 
7660 	if (isp->isp_state == ISP_RUNSTATE)
7661 		isp_deinit(isp);
7662 	if (isp->isp_state != ISP_RESETSTATE)
7663 		isp_reset(isp, do_load_defaults);
7664 	if (isp->isp_state != ISP_RESETSTATE) {
7665 		res = EIO;
7666 		isp_prt(isp, ISP_LOGERR, "%s: cannot reset card", __func__);
7667 		ISP_DISABLE_INTS(isp);
7668 		goto cleanup;
7669 	}
7670 
7671 	isp_init(isp);
7672 	if (isp->isp_state > ISP_RESETSTATE &&
7673 	    isp->isp_state != ISP_RUNSTATE) {
7674 		res = EIO;
7675 		isp_prt(isp, ISP_LOGERR, "%s: cannot init card", __func__);
7676 		ISP_DISABLE_INTS(isp);
7677 		if (IS_FC(isp)) {
7678 			/*
7679 			 * If we're in ISP_ROLE_NONE, turn off the lasers.
7680 			 */
7681 			if (!IS_24XX(isp)) {
7682 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
7683 				ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
7684 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
7685 				ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
7686 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
7687 			}
7688 		}
7689 	}
7690 
7691 cleanup:
7692 	isp->isp_nactive = 0;
7693 	isp_clear_commands(isp);
7694 	if (IS_FC(isp)) {
7695 		for (i = 0; i < isp->isp_nchan; i++)
7696 			isp_clear_portdb(isp, i);
7697 	}
7698 	return (res);
7699 }
7700 
7701 /*
7702  * NVRAM Routines
7703  */
7704 static int
7705 isp_read_nvram(ispsoftc_t *isp, int bus)
7706 {
7707 	int i, amt, retval;
7708 	uint8_t csum, minversion;
7709 	union {
7710 		uint8_t _x[ISP2400_NVRAM_SIZE];
7711 		uint16_t _s[ISP2400_NVRAM_SIZE>>1];
7712 	} _n;
7713 #define	nvram_data	_n._x
7714 #define	nvram_words	_n._s
7715 
7716 	if (IS_24XX(isp)) {
7717 		return (isp_read_nvram_2400(isp, nvram_data));
7718 	} else if (IS_FC(isp)) {
7719 		amt = ISP2100_NVRAM_SIZE;
7720 		minversion = 1;
7721 	} else if (IS_ULTRA2(isp)) {
7722 		amt = ISP1080_NVRAM_SIZE;
7723 		minversion = 0;
7724 	} else {
7725 		amt = ISP_NVRAM_SIZE;
7726 		minversion = 2;
7727 	}
7728 
7729 	for (i = 0; i < amt>>1; i++) {
7730 		isp_rdnvram_word(isp, i, &nvram_words[i]);
7731 	}
7732 
7733 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
7734 	    nvram_data[2] != 'P') {
7735 		if (isp->isp_bustype != ISP_BT_SBUS) {
7736 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
7737 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x", nvram_data[0], nvram_data[1], nvram_data[2]);
7738 		}
7739 		retval = -1;
7740 		goto out;
7741 	}
7742 
7743 	for (csum = 0, i = 0; i < amt; i++) {
7744 		csum += nvram_data[i];
7745 	}
7746 	if (csum != 0) {
7747 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
7748 		retval = -1;
7749 		goto out;
7750 	}
7751 
7752 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
7753 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
7754 		    ISP_NVRAM_VERSION(nvram_data));
7755 		retval = -1;
7756 		goto out;
7757 	}
7758 
7759 	if (IS_ULTRA3(isp)) {
7760 		isp_parse_nvram_12160(isp, bus, nvram_data);
7761 	} else if (IS_1080(isp)) {
7762 		isp_parse_nvram_1080(isp, bus, nvram_data);
7763 	} else if (IS_1280(isp) || IS_1240(isp)) {
7764 		isp_parse_nvram_1080(isp, bus, nvram_data);
7765 	} else if (IS_SCSI(isp)) {
7766 		isp_parse_nvram_1020(isp, nvram_data);
7767 	} else {
7768 		isp_parse_nvram_2100(isp, nvram_data);
7769 	}
7770 	retval = 0;
7771 out:
7772 	return (retval);
7773 #undef	nvram_data
7774 #undef	nvram_words
7775 }
7776 
7777 static int
7778 isp_read_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
7779 {
7780 	int retval = 0;
7781 	uint32_t addr, csum, lwrds, *dptr;
7782 
7783 	if (isp->isp_port) {
7784 		addr = ISP2400_NVRAM_PORT1_ADDR;
7785 	} else {
7786 		addr = ISP2400_NVRAM_PORT0_ADDR;
7787 	}
7788 
7789 	dptr = (uint32_t *) nvram_data;
7790 	for (lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
7791 		isp_rd_2400_nvram(isp, addr++, dptr++);
7792 	}
7793 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
7794 	    nvram_data[2] != 'P') {
7795 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header (%x %x %x)",
7796 		    nvram_data[0], nvram_data[1], nvram_data[2]);
7797 		retval = -1;
7798 		goto out;
7799 	}
7800 	dptr = (uint32_t *) nvram_data;
7801 	for (csum = 0, lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
7802 		uint32_t tmp;
7803 		ISP_IOXGET_32(isp, &dptr[lwrds], tmp);
7804 		csum += tmp;
7805 	}
7806 	if (csum != 0) {
7807 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
7808 		retval = -1;
7809 		goto out;
7810 	}
7811 	isp_parse_nvram_2400(isp, nvram_data);
7812 out:
7813 	return (retval);
7814 }
7815 
7816 static void
7817 isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
7818 {
7819 	int i, cbits;
7820 	uint16_t bit, rqst, junk;
7821 
7822 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
7823 	ISP_DELAY(10);
7824 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
7825 	ISP_DELAY(10);
7826 
7827 	if (IS_FC(isp)) {
7828 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
7829 		if (IS_2312(isp) && isp->isp_port) {
7830 			wo += 128;
7831 		}
7832 		rqst = (ISP_NVRAM_READ << 8) | wo;
7833 		cbits = 10;
7834 	} else if (IS_ULTRA2(isp)) {
7835 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
7836 		rqst = (ISP_NVRAM_READ << 8) | wo;
7837 		cbits = 10;
7838 	} else {
7839 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
7840 		rqst = (ISP_NVRAM_READ << 6) | wo;
7841 		cbits = 8;
7842 	}
7843 
7844 	/*
7845 	 * Clock the word select request out...
7846 	 */
7847 	for (i = cbits; i >= 0; i--) {
7848 		if ((rqst >> i) & 1) {
7849 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
7850 		} else {
7851 			bit = BIU_NVRAM_SELECT;
7852 		}
7853 		ISP_WRITE(isp, BIU_NVRAM, bit);
7854 		ISP_DELAY(10);
7855 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
7856 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
7857 		ISP_DELAY(10);
7858 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
7859 		ISP_WRITE(isp, BIU_NVRAM, bit);
7860 		ISP_DELAY(10);
7861 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
7862 	}
7863 	/*
7864 	 * Now read the result back in (bits come back in MSB format).
7865 	 */
7866 	*rp = 0;
7867 	for (i = 0; i < 16; i++) {
7868 		uint16_t rv;
7869 		*rp <<= 1;
7870 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
7871 		ISP_DELAY(10);
7872 		rv = ISP_READ(isp, BIU_NVRAM);
7873 		if (rv & BIU_NVRAM_DATAIN) {
7874 			*rp |= 1;
7875 		}
7876 		ISP_DELAY(10);
7877 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
7878 		ISP_DELAY(10);
7879 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
7880 	}
7881 	ISP_WRITE(isp, BIU_NVRAM, 0);
7882 	ISP_DELAY(10);
7883 	junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
7884 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
7885 }
7886 
7887 static void
7888 isp_rd_2400_nvram(ispsoftc_t *isp, uint32_t addr, uint32_t *rp)
7889 {
7890 	int loops = 0;
7891 	uint32_t base = 0x7ffe0000;
7892 	uint32_t tmp = 0;
7893 
7894 	if (IS_25XX(isp)) {
7895 		base = 0x7ff00000 | 0x48000;
7896 	}
7897 	ISP_WRITE(isp, BIU2400_FLASH_ADDR, base | addr);
7898 	for (loops = 0; loops < 5000; loops++) {
7899 		ISP_DELAY(10);
7900 		tmp = ISP_READ(isp, BIU2400_FLASH_ADDR);
7901 		if ((tmp & (1U << 31)) != 0) {
7902 			break;
7903 		}
7904 	}
7905 	if (tmp & (1U << 31)) {
7906 		*rp = ISP_READ(isp, BIU2400_FLASH_DATA);
7907 		ISP_SWIZZLE_NVRAM_LONG(isp, rp);
7908 	} else {
7909 		*rp = 0xffffffff;
7910 	}
7911 }
7912 
7913 static void
7914 isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
7915 {
7916 	sdparam *sdp = SDPARAM(isp, 0);
7917 	int tgt;
7918 
7919 	sdp->isp_fifo_threshold =
7920 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
7921 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
7922 
7923 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
7924 		sdp->isp_initiator_id = ISP_NVRAM_INITIATOR_ID(nvram_data);
7925 
7926 	sdp->isp_bus_reset_delay =
7927 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
7928 
7929 	sdp->isp_retry_count =
7930 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
7931 
7932 	sdp->isp_retry_delay =
7933 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
7934 
7935 	sdp->isp_async_data_setup =
7936 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
7937 
7938 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
7939 		if (sdp->isp_async_data_setup < 9) {
7940 			sdp->isp_async_data_setup = 9;
7941 		}
7942 	} else {
7943 		if (sdp->isp_async_data_setup != 6) {
7944 			sdp->isp_async_data_setup = 6;
7945 		}
7946 	}
7947 
7948 	sdp->isp_req_ack_active_neg =
7949 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
7950 
7951 	sdp->isp_data_line_active_neg =
7952 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
7953 
7954 	sdp->isp_data_dma_burst_enabl =
7955 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
7956 
7957 	sdp->isp_cmd_dma_burst_enable =
7958 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
7959 
7960 	sdp->isp_tag_aging =
7961 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
7962 
7963 	sdp->isp_selection_timeout =
7964 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
7965 
7966 	sdp->isp_max_queue_depth =
7967 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
7968 
7969 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
7970 
7971 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7972 		sdp->isp_devparam[tgt].dev_enable =
7973 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
7974 		sdp->isp_devparam[tgt].exc_throttle =
7975 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
7976 		sdp->isp_devparam[tgt].nvrm_offset =
7977 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
7978 		sdp->isp_devparam[tgt].nvrm_period =
7979 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
7980 		/*
7981 		 * We probably shouldn't lie about this, but it
7982 		 * it makes it much safer if we limit NVRAM values
7983 		 * to sanity.
7984 		 */
7985 		if (isp->isp_type < ISP_HA_SCSI_1040) {
7986 			/*
7987 			 * If we're not ultra, we can't possibly
7988 			 * be a shorter period than this.
7989 			 */
7990 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
7991 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
7992 			}
7993 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
7994 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
7995 			}
7996 		} else {
7997 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
7998 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
7999 			}
8000 		}
8001 		sdp->isp_devparam[tgt].nvrm_flags = 0;
8002 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
8003 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8004 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8005 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
8006 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8007 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
8008 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8009 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
8010 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8011 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
8012 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8013 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
8014 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8015 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
8016 		sdp->isp_devparam[tgt].goal_offset =
8017 		    sdp->isp_devparam[tgt].nvrm_offset;
8018 		sdp->isp_devparam[tgt].goal_period =
8019 		    sdp->isp_devparam[tgt].nvrm_period;
8020 		sdp->isp_devparam[tgt].goal_flags =
8021 		    sdp->isp_devparam[tgt].nvrm_flags;
8022 	}
8023 }
8024 
8025 static void
8026 isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
8027 {
8028 	sdparam *sdp = SDPARAM(isp, bus);
8029 	int tgt;
8030 
8031 	sdp->isp_fifo_threshold =
8032 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
8033 
8034 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8035 		sdp->isp_initiator_id = ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
8036 
8037 	sdp->isp_bus_reset_delay =
8038 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
8039 
8040 	sdp->isp_retry_count =
8041 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
8042 
8043 	sdp->isp_retry_delay =
8044 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
8045 
8046 	sdp->isp_async_data_setup =
8047 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
8048 
8049 	sdp->isp_req_ack_active_neg =
8050 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
8051 
8052 	sdp->isp_data_line_active_neg =
8053 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
8054 
8055 	sdp->isp_data_dma_burst_enabl =
8056 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
8057 
8058 	sdp->isp_cmd_dma_burst_enable =
8059 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
8060 
8061 	sdp->isp_selection_timeout =
8062 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
8063 
8064 	sdp->isp_max_queue_depth =
8065 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
8066 
8067 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8068 		sdp->isp_devparam[tgt].dev_enable =
8069 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
8070 		sdp->isp_devparam[tgt].exc_throttle =
8071 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
8072 		sdp->isp_devparam[tgt].nvrm_offset =
8073 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
8074 		sdp->isp_devparam[tgt].nvrm_period =
8075 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
8076 		sdp->isp_devparam[tgt].nvrm_flags = 0;
8077 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
8078 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8079 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8080 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
8081 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8082 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
8083 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8084 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
8085 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8086 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
8087 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8088 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
8089 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8090 		sdp->isp_devparam[tgt].actv_flags = 0;
8091 		sdp->isp_devparam[tgt].goal_offset =
8092 		    sdp->isp_devparam[tgt].nvrm_offset;
8093 		sdp->isp_devparam[tgt].goal_period =
8094 		    sdp->isp_devparam[tgt].nvrm_period;
8095 		sdp->isp_devparam[tgt].goal_flags =
8096 		    sdp->isp_devparam[tgt].nvrm_flags;
8097 	}
8098 }
8099 
8100 static void
8101 isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
8102 {
8103 	sdparam *sdp = SDPARAM(isp, bus);
8104 	int tgt;
8105 
8106 	sdp->isp_fifo_threshold =
8107 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
8108 
8109 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8110 		sdp->isp_initiator_id = ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
8111 
8112 	sdp->isp_bus_reset_delay =
8113 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
8114 
8115 	sdp->isp_retry_count =
8116 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
8117 
8118 	sdp->isp_retry_delay =
8119 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
8120 
8121 	sdp->isp_async_data_setup =
8122 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
8123 
8124 	sdp->isp_req_ack_active_neg =
8125 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
8126 
8127 	sdp->isp_data_line_active_neg =
8128 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
8129 
8130 	sdp->isp_data_dma_burst_enabl =
8131 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8132 
8133 	sdp->isp_cmd_dma_burst_enable =
8134 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8135 
8136 	sdp->isp_selection_timeout =
8137 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
8138 
8139 	sdp->isp_max_queue_depth =
8140 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
8141 
8142 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8143 		sdp->isp_devparam[tgt].dev_enable =
8144 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
8145 		sdp->isp_devparam[tgt].exc_throttle =
8146 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
8147 		sdp->isp_devparam[tgt].nvrm_offset =
8148 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
8149 		sdp->isp_devparam[tgt].nvrm_period =
8150 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
8151 		sdp->isp_devparam[tgt].nvrm_flags = 0;
8152 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
8153 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8154 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8155 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
8156 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8157 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
8158 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8159 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
8160 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8161 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
8162 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8163 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
8164 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8165 		sdp->isp_devparam[tgt].actv_flags = 0;
8166 		sdp->isp_devparam[tgt].goal_offset =
8167 		    sdp->isp_devparam[tgt].nvrm_offset;
8168 		sdp->isp_devparam[tgt].goal_period =
8169 		    sdp->isp_devparam[tgt].nvrm_period;
8170 		sdp->isp_devparam[tgt].goal_flags =
8171 		    sdp->isp_devparam[tgt].nvrm_flags;
8172 	}
8173 }
8174 
8175 static void
8176 isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
8177 {
8178 	fcparam *fcp = FCPARAM(isp, 0);
8179 	uint64_t wwn;
8180 
8181 	/*
8182 	 * There is NVRAM storage for both Port and Node entities-
8183 	 * but the Node entity appears to be unused on all the cards
8184 	 * I can find. However, we should account for this being set
8185 	 * at some point in the future.
8186 	 *
8187 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
8188 	 * bits 48..60. In the case of the 2202, it appears that they do
8189 	 * use bit 48 to distinguish between the two instances on the card.
8190 	 * The 2204, which I've never seen, *probably* extends this method.
8191 	 */
8192 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
8193 	if (wwn) {
8194 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
8195 		    (uint32_t) (wwn >> 32), (uint32_t) (wwn));
8196 		if ((wwn >> 60) == 0) {
8197 			wwn |= (((uint64_t) 2)<< 60);
8198 		}
8199 	}
8200 	fcp->isp_wwpn_nvram = wwn;
8201 	if (IS_2200(isp) || IS_23XX(isp)) {
8202 		wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
8203 		if (wwn) {
8204 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
8205 			    (uint32_t) (wwn >> 32),
8206 			    (uint32_t) (wwn));
8207 			if ((wwn >> 60) == 0) {
8208 				wwn |= (((uint64_t) 2)<< 60);
8209 			}
8210 		} else {
8211 			wwn = fcp->isp_wwpn_nvram & ~((uint64_t) 0xfff << 48);
8212 		}
8213 	} else {
8214 		wwn &= ~((uint64_t) 0xfff << 48);
8215 	}
8216 	fcp->isp_wwnn_nvram = wwn;
8217 
8218 	fcp->isp_maxalloc = ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
8219 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8220 		DEFAULT_FRAMESIZE(isp) =
8221 		    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
8222 	}
8223 	fcp->isp_retry_delay = ISP2100_NVRAM_RETRY_DELAY(nvram_data);
8224 	fcp->isp_retry_count = ISP2100_NVRAM_RETRY_COUNT(nvram_data);
8225 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8226 		fcp->isp_loopid = ISP2100_NVRAM_HARDLOOPID(nvram_data);
8227 	}
8228 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8229 		DEFAULT_EXEC_THROTTLE(isp) =
8230 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
8231 	}
8232 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
8233 	isp_prt(isp, ISP_LOGDEBUG0,
8234 	    "NVRAM 0x%08x%08x 0x%08x%08x maxalloc %d maxframelen %d",
8235 	    (uint32_t) (fcp->isp_wwnn_nvram >> 32),
8236 	    (uint32_t) fcp->isp_wwnn_nvram,
8237 	    (uint32_t) (fcp->isp_wwpn_nvram >> 32),
8238 	    (uint32_t) fcp->isp_wwpn_nvram,
8239 	    ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data),
8240 	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data));
8241 	isp_prt(isp, ISP_LOGDEBUG0,
8242 	    "execthrottle %d fwoptions 0x%x hardloop %d tov %d",
8243 	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
8244 	    ISP2100_NVRAM_OPTIONS(nvram_data),
8245 	    ISP2100_NVRAM_HARDLOOPID(nvram_data),
8246 	    ISP2100_NVRAM_TOV(nvram_data));
8247 	fcp->isp_xfwoptions = ISP2100_XFW_OPTIONS(nvram_data);
8248 	fcp->isp_zfwoptions = ISP2100_ZFW_OPTIONS(nvram_data);
8249 	isp_prt(isp, ISP_LOGDEBUG0, "xfwoptions 0x%x zfw options 0x%x",
8250 	    ISP2100_XFW_OPTIONS(nvram_data), ISP2100_ZFW_OPTIONS(nvram_data));
8251 }
8252 
8253 static void
8254 isp_parse_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
8255 {
8256 	fcparam *fcp = FCPARAM(isp, 0);
8257 	uint64_t wwn;
8258 
8259 	isp_prt(isp, ISP_LOGDEBUG0,
8260 	    "NVRAM 0x%08x%08x 0x%08x%08x exchg_cnt %d maxframelen %d",
8261 	    (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data) >> 32),
8262 	    (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data)),
8263 	    (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data) >> 32),
8264 	    (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data)),
8265 	    ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data),
8266 	    ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data));
8267 	isp_prt(isp, ISP_LOGDEBUG0,
8268 	    "NVRAM execthr %d loopid %d fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x",
8269 	    ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data),
8270 	    ISP2400_NVRAM_HARDLOOPID(nvram_data),
8271 	    ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data),
8272 	    ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data),
8273 	    ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data));
8274 
8275 	wwn = ISP2400_NVRAM_PORT_NAME(nvram_data);
8276 	fcp->isp_wwpn_nvram = wwn;
8277 
8278 	wwn = ISP2400_NVRAM_NODE_NAME(nvram_data);
8279 	if (wwn) {
8280 		if ((wwn >> 60) != 2 && (wwn >> 60) != 5) {
8281 			wwn = 0;
8282 		}
8283 	}
8284 	if (wwn == 0 && (fcp->isp_wwpn_nvram >> 60) == 2) {
8285 		wwn = fcp->isp_wwpn_nvram;
8286 		wwn &= ~((uint64_t) 0xfff << 48);
8287 	}
8288 	fcp->isp_wwnn_nvram = wwn;
8289 
8290 	if (ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data)) {
8291 		fcp->isp_maxalloc = ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data);
8292 	}
8293 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8294 		DEFAULT_FRAMESIZE(isp) =
8295 		    ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data);
8296 	}
8297 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8298 		fcp->isp_loopid = ISP2400_NVRAM_HARDLOOPID(nvram_data);
8299 	}
8300 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8301 		DEFAULT_EXEC_THROTTLE(isp) =
8302 			ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data);
8303 	}
8304 	fcp->isp_fwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data);
8305 	fcp->isp_xfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data);
8306 	fcp->isp_zfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data);
8307 }
8308