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