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