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