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