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