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