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