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