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