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