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