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