xref: /netbsd/sys/dev/ic/isp.c (revision c4a72b64)
1 /* $NetBSD: isp.c,v 1.100 2002/11/25 01:44:21 thorpej Exp $ */
2 /*
3  * This driver, which is contained in NetBSD in the files:
4  *
5  *	sys/dev/ic/isp.c
6  *	sys/dev/ic/isp_inline.h
7  *	sys/dev/ic/isp_netbsd.c
8  *	sys/dev/ic/isp_netbsd.h
9  *	sys/dev/ic/isp_target.c
10  *	sys/dev/ic/isp_target.h
11  *	sys/dev/ic/isp_tpublic.h
12  *	sys/dev/ic/ispmbox.h
13  *	sys/dev/ic/ispreg.h
14  *	sys/dev/ic/ispvar.h
15  *	sys/microcode/isp/asm_sbus.h
16  *	sys/microcode/isp/asm_1040.h
17  *	sys/microcode/isp/asm_1080.h
18  *	sys/microcode/isp/asm_12160.h
19  *	sys/microcode/isp/asm_2100.h
20  *	sys/microcode/isp/asm_2200.h
21  *	sys/pci/isp_pci.c
22  *	sys/sbus/isp_sbus.c
23  *
24  * Is being actively maintained by Matthew Jacob (mjacob@netbsd.org).
25  * This driver also is shared source with FreeBSD, OpenBSD, Linux, Solaris,
26  * Linux versions. This tends to be an interesting maintenance problem.
27  *
28  * Please coordinate with Matthew Jacob on changes you wish to make here.
29  */
30 /*
31  * Machine and OS Independent (well, as best as possible)
32  * code for the Qlogic ISP SCSI adapters.
33  *
34  * Copyright (c) 1997, 1998, 1999, 2000, 2001 by Matthew Jacob
35  * NASA/Ames Research Center
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice immediately at the beginning of the file, without modification,
43  *    this list of conditions, and the following disclaimer.
44  * 2. The name of the author may not be used to endorse or promote products
45  *    derived from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
51  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59 
60 /*
61  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
62  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
63  * ideas dredged from the Solaris driver.
64  */
65 
66 /*
67  * Include header file appropriate for platform we're building on.
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: isp.c,v 1.100 2002/11/25 01:44:21 thorpej Exp $");
72 
73 #ifdef	__NetBSD__
74 #include <dev/ic/isp_netbsd.h>
75 #endif
76 #ifdef	__FreeBSD__
77 #include <dev/isp/isp_freebsd.h>
78 #endif
79 #ifdef	__OpenBSD__
80 #include <dev/ic/isp_openbsd.h>
81 #endif
82 #ifdef	__linux__
83 #include "isp_linux.h"
84 #endif
85 #ifdef	__svr4__
86 #include "isp_solaris.h"
87 #endif
88 
89 /*
90  * General defines
91  */
92 
93 #define	MBOX_DELAY_COUNT	1000000 / 100
94 
95 /*
96  * Local static data
97  */
98 static const char portshift[] =
99     "Target %d Loop ID 0x%x (Port 0x%x) => Loop 0x%x (Port 0x%x)";
100 static const char portdup[] =
101     "Target %d duplicates Target %d- killing off both";
102 static const char retained[] =
103     "Retaining Loop ID 0x%x for Target %d (Port 0x%x)";
104 static const char lretained[] =
105     "Retained login of Target %d (Loop ID 0x%x) Port 0x%x";
106 static const char plogout[] =
107     "Logging out Target %d at Loop ID 0x%x (Port 0x%x)";
108 static const char plogierr[] =
109     "Command Error in PLOGI for Port 0x%x (0x%x)";
110 static const char nopdb[] =
111     "Could not get PDB for Device @ Port 0x%x";
112 static const char pdbmfail1[] =
113     "PDB Loop ID info for Device @ Port 0x%x does not match up (0x%x)";
114 static const char pdbmfail2[] =
115     "PDB Port info for Device @ Port 0x%x does not match up (0x%x)";
116 static const char ldumped[] =
117     "Target %d (Loop ID 0x%x) Port 0x%x dumped after login info mismatch";
118 static const char notresp[] =
119   "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
120 static const char xact1[] =
121     "HBA attempted queued transaction with disconnect not set for %d.%d.%d";
122 static const char xact2[] =
123     "HBA attempted queued transaction to target routine %d on target %d bus %d";
124 static const char xact3[] =
125     "HBA attempted queued cmd for %d.%d.%d when queueing disabled";
126 static const char pskip[] =
127     "SCSI phase skipped for target %d.%d.%d";
128 static const char topology[] =
129     "Loop ID %d, AL_PA 0x%x, Port ID 0x%x, Loop State 0x%x, Topology '%s'";
130 static const char swrej[] =
131     "Fabric Nameserver rejected %s (Reason=0x%x Expl=0x%x) for Port ID 0x%x";
132 static const char finmsg[] =
133     "(%d.%d.%d): FIN dl%d resid %d STS 0x%x SKEY %c XS_ERR=0x%x";
134 static const char sc0[] =
135     "%s CHAN %d FTHRSH %d IID %d RESETD %d RETRYC %d RETRYD %d ASD 0x%x";
136 static const char sc1[] =
137     "%s RAAN 0x%x DLAN 0x%x DDMAB 0x%x CDMAB 0x%x SELTIME %d MQD %d";
138 static const char sc2[] = "%s CHAN %d TGT %d FLAGS 0x%x 0x%x/0x%x";
139 static const char sc3[] = "Generated";
140 static const char sc4[] = "NVRAM";
141 static const char bun[] =
142     "bad underrun for %d.%d (count %d, resid %d, status %s)";
143 
144 /*
145  * Local function prototypes.
146  */
147 static int isp_parse_async(struct ispsoftc *, u_int16_t);
148 static int isp_handle_other_response(struct ispsoftc *, int, isphdr_t *,
149     u_int16_t *);
150 static void
151 isp_parse_status(struct ispsoftc *, ispstatusreq_t *, XS_T *);
152 static void isp_fastpost_complete(struct ispsoftc *, u_int16_t);
153 static int isp_mbox_continue(struct ispsoftc *);
154 static void isp_scsi_init(struct ispsoftc *);
155 static void isp_scsi_channel_init(struct ispsoftc *, int);
156 static void isp_fibre_init(struct ispsoftc *);
157 static void isp_mark_getpdb_all(struct ispsoftc *);
158 static int isp_getmap(struct ispsoftc *, fcpos_map_t *);
159 static int isp_getpdb(struct ispsoftc *, int, isp_pdb_t *);
160 static u_int64_t isp_get_portname(struct ispsoftc *, int, int);
161 static int isp_fclink_test(struct ispsoftc *, int);
162 static char *isp2100_fw_statename(int);
163 static int isp_pdb_sync(struct ispsoftc *);
164 static int isp_scan_loop(struct ispsoftc *);
165 static int isp_fabric_mbox_cmd(struct ispsoftc *, mbreg_t *);
166 static int isp_scan_fabric(struct ispsoftc *, int);
167 static void isp_register_fc4_type(struct ispsoftc *);
168 static void isp_fw_state(struct ispsoftc *);
169 static void isp_mboxcmd_qnw(struct ispsoftc *, mbreg_t *, int);
170 static void isp_mboxcmd(struct ispsoftc *, mbreg_t *, int);
171 
172 static void isp_update(struct ispsoftc *);
173 static void isp_update_bus(struct ispsoftc *, int);
174 static void isp_setdfltparm(struct ispsoftc *, int);
175 static int isp_read_nvram(struct ispsoftc *);
176 static void isp_rdnvram_word(struct ispsoftc *, int, u_int16_t *);
177 static void isp_parse_nvram_1020(struct ispsoftc *, u_int8_t *);
178 static void isp_parse_nvram_1080(struct ispsoftc *, int, u_int8_t *);
179 static void isp_parse_nvram_12160(struct ispsoftc *, int, u_int8_t *);
180 static void isp_parse_nvram_2100(struct ispsoftc *, u_int8_t *);
181 
182 /*
183  * Reset Hardware.
184  *
185  * Hit the chip over the head, download new f/w if available and set it running.
186  *
187  * Locking done elsewhere.
188  */
189 
190 void
191 isp_reset(struct ispsoftc *isp)
192 {
193 	mbreg_t mbs;
194 	u_int16_t code_org;
195 	int loops, i, dodnld = 1;
196 	char *btype = "????";
197 
198 	isp->isp_state = ISP_NILSTATE;
199 
200 	/*
201 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
202 	 * have been set in the MD code. We figure out more
203 	 * here. Possibly more refined types based upon PCI
204 	 * identification. Chip revision has been gathered.
205 	 *
206 	 * After we've fired this chip up, zero out the conf1 register
207 	 * for SCSI adapters and do other settings for the 2100.
208 	 */
209 
210 	/*
211 	 * Get the current running firmware revision out of the
212 	 * chip before we hit it over the head (if this is our
213 	 * first time through). Note that we store this as the
214 	 * 'ROM' firmware revision- which it may not be. In any
215 	 * case, we don't really use this yet, but we may in
216 	 * the future.
217 	 */
218 	if (isp->isp_touched == 0) {
219 		/*
220 		 * First see whether or not we're sitting in the ISP PROM.
221 		 * If we've just been reset, we'll have the string "ISP   "
222 		 * spread through outgoing mailbox registers 1-3. We do
223 		 * this for PCI cards because otherwise we really don't
224 		 * know what state the card is in and we could hang if
225 		 * we try this command otherwise.
226 		 *
227 		 * For SBus cards, we just do this because they almost
228 		 * certainly will be running firmware by now.
229 		 */
230 		if (ISP_READ(isp, OUTMAILBOX1) != 0x4953 ||
231 		    ISP_READ(isp, OUTMAILBOX2) != 0x5020 ||
232 		    ISP_READ(isp, OUTMAILBOX3) != 0x2020) {
233 			/*
234 			 * Just in case it was paused...
235 			 */
236 			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
237 			mbs.param[0] = MBOX_ABOUT_FIRMWARE;
238 			isp_mboxcmd(isp, &mbs, MBLOGNONE);
239 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
240 				isp->isp_romfw_rev[0] = mbs.param[1];
241 				isp->isp_romfw_rev[1] = mbs.param[2];
242 				isp->isp_romfw_rev[2] = mbs.param[3];
243 			}
244 		}
245 		isp->isp_touched = 1;
246 	}
247 
248 	DISABLE_INTS(isp);
249 
250 	/*
251 	 * Set up default request/response queue in-pointer/out-pointer
252 	 * register indices.
253 	 */
254 	if (IS_23XX(isp)) {
255 		isp->isp_rqstinrp = BIU_REQINP;
256 		isp->isp_rqstoutrp = BIU_REQOUTP;
257 		isp->isp_respinrp = BIU_RSPINP;
258 		isp->isp_respoutrp = BIU_RSPOUTP;
259 	} else {
260 		isp->isp_rqstinrp = INMAILBOX4;
261 		isp->isp_rqstoutrp = OUTMAILBOX4;
262 		isp->isp_respinrp = OUTMAILBOX5;
263 		isp->isp_respoutrp = INMAILBOX5;
264 	}
265 
266 	/*
267 	 * Put the board into PAUSE mode (so we can read the SXP registers
268 	 * or write FPM/FBM registers).
269 	 */
270 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
271 
272 	if (IS_FC(isp)) {
273 		switch (isp->isp_type) {
274 		case ISP_HA_FC_2100:
275 			btype = "2100";
276 			break;
277 		case ISP_HA_FC_2200:
278 			btype = "2200";
279 			break;
280 		case ISP_HA_FC_2300:
281 			btype = "2300";
282 			break;
283 		case ISP_HA_FC_2312:
284 			btype = "2312";
285 			break;
286 		default:
287 			break;
288 		}
289 		/*
290 		 * While we're paused, reset the FPM module and FBM fifos.
291 		 */
292 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
293 		ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
294 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
295 		ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
296 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
297 	} else if (IS_1240(isp)) {
298 		sdparam *sdp = isp->isp_param;
299 		btype = "1240";
300 		isp->isp_clock = 60;
301 		sdp->isp_ultramode = 1;
302 		sdp++;
303 		sdp->isp_ultramode = 1;
304 		/*
305 		 * XXX: Should probably do some bus sensing.
306 		 */
307 	} else if (IS_ULTRA2(isp)) {
308 		static const char m[] = "bus %d is in %s Mode";
309 		u_int16_t l;
310 		sdparam *sdp = isp->isp_param;
311 
312 		isp->isp_clock = 100;
313 
314 		if (IS_1280(isp))
315 			btype = "1280";
316 		else if (IS_1080(isp))
317 			btype = "1080";
318 		else if (IS_10160(isp))
319 			btype = "10160";
320 		else if (IS_12160(isp))
321 			btype = "12160";
322 		else
323 			btype = "<UNKLVD>";
324 
325 		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
326 		switch (l) {
327 		case ISP1080_LVD_MODE:
328 			sdp->isp_lvdmode = 1;
329 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
330 			break;
331 		case ISP1080_HVD_MODE:
332 			sdp->isp_diffmode = 1;
333 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
334 			break;
335 		case ISP1080_SE_MODE:
336 			sdp->isp_ultramode = 1;
337 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
338 			break;
339 		default:
340 			isp_prt(isp, ISP_LOGERR,
341 			    "unknown mode on bus %d (0x%x)", 0, l);
342 			break;
343 		}
344 
345 		if (IS_DUALBUS(isp)) {
346 			sdp++;
347 			l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
348 			l &= ISP1080_MODE_MASK;
349 			switch(l) {
350 			case ISP1080_LVD_MODE:
351 				sdp->isp_lvdmode = 1;
352 				isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
353 				break;
354 			case ISP1080_HVD_MODE:
355 				sdp->isp_diffmode = 1;
356 				isp_prt(isp, ISP_LOGCONFIG,
357 				    m, 1, "Differential");
358 				break;
359 			case ISP1080_SE_MODE:
360 				sdp->isp_ultramode = 1;
361 				isp_prt(isp, ISP_LOGCONFIG,
362 				    m, 1, "Single-Ended");
363 				break;
364 			default:
365 				isp_prt(isp, ISP_LOGERR,
366 				    "unknown mode on bus %d (0x%x)", 1, l);
367 				break;
368 			}
369 		}
370 	} else {
371 		sdparam *sdp = isp->isp_param;
372 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
373 		switch (i) {
374 		default:
375 			isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
376 			/* FALLTHROUGH */
377 		case 1:
378 			btype = "1020";
379 			isp->isp_type = ISP_HA_SCSI_1020;
380 			isp->isp_clock = 40;
381 			break;
382 		case 2:
383 			/*
384 			 * Some 1020A chips are Ultra Capable, but don't
385 			 * run the clock rate up for that unless told to
386 			 * do so by the Ultra Capable bits being set.
387 			 */
388 			btype = "1020A";
389 			isp->isp_type = ISP_HA_SCSI_1020A;
390 			isp->isp_clock = 40;
391 			break;
392 		case 3:
393 			btype = "1040";
394 			isp->isp_type = ISP_HA_SCSI_1040;
395 			isp->isp_clock = 60;
396 			break;
397 		case 4:
398 			btype = "1040A";
399 			isp->isp_type = ISP_HA_SCSI_1040A;
400 			isp->isp_clock = 60;
401 			break;
402 		case 5:
403 			btype = "1040B";
404 			isp->isp_type = ISP_HA_SCSI_1040B;
405 			isp->isp_clock = 60;
406 			break;
407 		case 6:
408 			btype = "1040C";
409 			isp->isp_type = ISP_HA_SCSI_1040C;
410 			isp->isp_clock = 60;
411                         break;
412 		}
413 		/*
414 		 * Now, while we're at it, gather info about ultra
415 		 * and/or differential mode.
416 		 */
417 		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
418 			isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
419 			sdp->isp_diffmode = 1;
420 		} else {
421 			sdp->isp_diffmode = 0;
422 		}
423 		i = ISP_READ(isp, RISC_PSR);
424 		if (isp->isp_bustype == ISP_BT_SBUS) {
425 			i &= RISC_PSR_SBUS_ULTRA;
426 		} else {
427 			i &= RISC_PSR_PCI_ULTRA;
428 		}
429 		if (i != 0) {
430 			isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
431 			sdp->isp_ultramode = 1;
432 			/*
433 			 * If we're in Ultra Mode, we have to be 60Mhz clock-
434 			 * even for the SBus version.
435 			 */
436 			isp->isp_clock = 60;
437 		} else {
438 			sdp->isp_ultramode = 0;
439 			/*
440 			 * Clock is known. Gronk.
441 			 */
442 		}
443 
444 		/*
445 		 * Machine dependent clock (if set) overrides
446 		 * our generic determinations.
447 		 */
448 		if (isp->isp_mdvec->dv_clock) {
449 			if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
450 				isp->isp_clock = isp->isp_mdvec->dv_clock;
451 			}
452 		}
453 
454 	}
455 
456 	/*
457 	 * Clear instrumentation
458 	 */
459 	isp->isp_intcnt = isp->isp_intbogus = 0;
460 
461 	/*
462 	 * Do MD specific pre initialization
463 	 */
464 	ISP_RESET0(isp);
465 
466 again:
467 
468 	/*
469 	 * Hit the chip over the head with hammer,
470 	 * and give the ISP a chance to recover.
471 	 */
472 
473 	if (IS_SCSI(isp)) {
474 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
475 		/*
476 		 * A slight delay...
477 		 */
478 		USEC_DELAY(100);
479 
480 		/*
481 		 * Clear data && control DMA engines.
482 		 */
483 		ISP_WRITE(isp, CDMA_CONTROL,
484 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
485 		ISP_WRITE(isp, DDMA_CONTROL,
486 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
487 
488 
489 	} else {
490 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
491 		/*
492 		 * A slight delay...
493 		 */
494 		USEC_DELAY(100);
495 
496 		/*
497 		 * Clear data && control DMA engines.
498 		 */
499 		ISP_WRITE(isp, CDMA2100_CONTROL,
500 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
501 		ISP_WRITE(isp, TDMA2100_CONTROL,
502 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
503 		ISP_WRITE(isp, RDMA2100_CONTROL,
504 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
505 	}
506 
507 	/*
508 	 * Wait for ISP to be ready to go...
509 	 */
510 	loops = MBOX_DELAY_COUNT;
511 	for (;;) {
512 		if (IS_SCSI(isp)) {
513 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
514 				break;
515 		} else {
516 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
517 				break;
518 		}
519 		USEC_DELAY(100);
520 		if (--loops < 0) {
521 			ISP_DUMPREGS(isp, "chip reset timed out");
522 			return;
523 		}
524 	}
525 
526 	/*
527 	 * After we've fired this chip up, zero out the conf1 register
528 	 * for SCSI adapters and other settings for the 2100.
529 	 */
530 
531 	if (IS_SCSI(isp)) {
532 		ISP_WRITE(isp, BIU_CONF1, 0);
533 	} else {
534 		ISP_WRITE(isp, BIU2100_CSR, 0);
535 	}
536 
537 	/*
538 	 * Reset RISC Processor
539 	 */
540 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
541 	USEC_DELAY(100);
542 	/* Clear semaphore register (just to be sure) */
543 	ISP_WRITE(isp, BIU_SEMA, 0);
544 
545 	/*
546 	 * Establish some initial burst rate stuff.
547 	 * (only for the 1XX0 boards). This really should
548 	 * be done later after fetching from NVRAM.
549 	 */
550 	if (IS_SCSI(isp)) {
551 		u_int16_t tmp = isp->isp_mdvec->dv_conf1;
552 		/*
553 		 * Busted FIFO. Turn off all but burst enables.
554 		 */
555 		if (isp->isp_type == ISP_HA_SCSI_1040A) {
556 			tmp &= BIU_BURST_ENABLE;
557 		}
558 		ISP_SETBITS(isp, BIU_CONF1, tmp);
559 		if (tmp & BIU_BURST_ENABLE) {
560 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
561 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
562 		}
563 #ifdef	PTI_CARDS
564 		if (((sdparam *) isp->isp_param)->isp_ultramode) {
565 			while (ISP_READ(isp, RISC_MTR) != 0x1313) {
566 				ISP_WRITE(isp, RISC_MTR, 0x1313);
567 				ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
568 			}
569 		} else {
570 			ISP_WRITE(isp, RISC_MTR, 0x1212);
571 		}
572 		/*
573 		 * PTI specific register
574 		 */
575 		ISP_WRITE(isp, RISC_EMB, DUAL_BANK)
576 #else
577 		ISP_WRITE(isp, RISC_MTR, 0x1212);
578 #endif
579 	} else {
580 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
581 		if (IS_2200(isp) || IS_23XX(isp)) {
582 			ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
583 		}
584 	}
585 
586 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
587 
588 	/*
589 	 * Do MD specific post initialization
590 	 */
591 	ISP_RESET1(isp);
592 
593 	/*
594 	 * Wait for everything to finish firing up.
595 	 *
596 	 * Avoid doing this on the 2312 because you can generate a PCI
597 	 * parity error (chip breakage).
598 	 */
599 	if (IS_23XX(isp)) {
600 		USEC_DELAY(5);
601 	} else {
602 		loops = MBOX_DELAY_COUNT;
603 		while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
604 			USEC_DELAY(100);
605 			if (--loops < 0) {
606 				isp_prt(isp, ISP_LOGERR,
607 				    "MBOX_BUSY never cleared on reset");
608 				return;
609 			}
610 		}
611 	}
612 
613 	/*
614 	 * Up until this point we've done everything by just reading or
615 	 * setting registers. From this point on we rely on at least *some*
616 	 * kind of firmware running in the card.
617 	 */
618 
619 	/*
620 	 * Do some sanity checking.
621 	 */
622 	mbs.param[0] = MBOX_NO_OP;
623 	isp_mboxcmd(isp, &mbs, MBLOGALL);
624 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
625 		return;
626 	}
627 
628 	if (IS_SCSI(isp)) {
629 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
630 		mbs.param[1] = 0xdead;
631 		mbs.param[2] = 0xbeef;
632 		mbs.param[3] = 0xffff;
633 		mbs.param[4] = 0x1111;
634 		mbs.param[5] = 0xa5a5;
635 		isp_mboxcmd(isp, &mbs, MBLOGALL);
636 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
637 			return;
638 		}
639 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
640 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
641 		    mbs.param[5] != 0xa5a5) {
642 			isp_prt(isp, ISP_LOGERR,
643 			    "Register Test Failed (0x%x 0x%x 0x%x 0x%x 0x%x)",
644 			    mbs.param[1], mbs.param[2], mbs.param[3],
645 			    mbs.param[4], mbs.param[5]);
646 			return;
647 		}
648 
649 	}
650 
651 	/*
652 	 * Download new Firmware, unless requested not to do so.
653 	 * This is made slightly trickier in some cases where the
654 	 * firmware of the ROM revision is newer than the revision
655 	 * compiled into the driver. So, where we used to compare
656 	 * versions of our f/w and the ROM f/w, now we just see
657 	 * whether we have f/w at all and whether a config flag
658 	 * has disabled our download.
659 	 */
660 	if ((isp->isp_mdvec->dv_ispfw == NULL) ||
661 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
662 		dodnld = 0;
663 	}
664 
665 	if (IS_23XX(isp))
666 		code_org = ISP_CODE_ORG_2300;
667 	else
668 		code_org = ISP_CODE_ORG;
669 
670 	if (dodnld) {
671 		isp->isp_mbxworkp = (void *) &isp->isp_mdvec->dv_ispfw[1];
672 		isp->isp_mbxwrk0 = isp->isp_mdvec->dv_ispfw[3] - 1;
673 		isp->isp_mbxwrk1 = code_org + 1;
674 		mbs.param[0] = MBOX_WRITE_RAM_WORD;
675 		mbs.param[1] = code_org;
676 		mbs.param[2] = isp->isp_mdvec->dv_ispfw[0];
677 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
678 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
679 			isp_prt(isp, ISP_LOGERR,
680 			    "F/W download failed at word %d",
681 			    isp->isp_mbxwrk1 - code_org);
682 			dodnld = 0;
683 			goto again;
684 		}
685 		/*
686 		 * Verify that it downloaded correctly.
687 		 */
688 		mbs.param[0] = MBOX_VERIFY_CHECKSUM;
689 		mbs.param[1] = code_org;
690 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
691 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
692 			isp_prt(isp, ISP_LOGERR, "Ram Checksum Failure");
693 			return;
694 		}
695 		isp->isp_loaded_fw = 1;
696 	} else {
697 		isp->isp_loaded_fw = 0;
698 		isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
699 	}
700 
701 	/*
702 	 * Now start it rolling.
703 	 *
704 	 * If we didn't actually download f/w,
705 	 * we still need to (re)start it.
706 	 */
707 
708 
709 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
710 	mbs.param[1] = code_org;
711 	isp_mboxcmd(isp, &mbs, MBLOGNONE);
712 	/*
713 	 * Give it a chance to start.
714 	 */
715 	USEC_DELAY(500);
716 
717 	if (IS_SCSI(isp)) {
718 		/*
719 		 * Set CLOCK RATE, but only if asked to.
720 		 */
721 		if (isp->isp_clock) {
722 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
723 			mbs.param[1] = isp->isp_clock;
724 			isp_mboxcmd(isp, &mbs, MBLOGALL);
725 			/* we will try not to care if this fails */
726 		}
727 	}
728 
729 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
730 	isp_mboxcmd(isp, &mbs, MBLOGALL);
731 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
732 		return;
733 	}
734 
735 	/*
736 	 * The SBus firmware that we are using apparently does not return
737 	 * major, minor, micro revisions in the mailbox registers, which
738 	 * is really, really, annoying.
739 	 */
740 	if (ISP_SBUS_SUPPORTED && isp->isp_bustype == ISP_BT_SBUS) {
741 		if (dodnld) {
742 #ifdef	ISP_TARGET_MODE
743 			isp->isp_fwrev[0] = 7;
744 			isp->isp_fwrev[1] = 55;
745 #else
746 			isp->isp_fwrev[0] = 1;
747 			isp->isp_fwrev[1] = 37;
748 #endif
749 			isp->isp_fwrev[2] = 0;
750 		}
751 	} else {
752 		isp->isp_fwrev[0] = mbs.param[1];
753 		isp->isp_fwrev[1] = mbs.param[2];
754 		isp->isp_fwrev[2] = mbs.param[3];
755 	}
756 	isp_prt(isp, ISP_LOGCONFIG,
757 	    "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
758 	    btype, isp->isp_revision, dodnld? "loaded" : "resident",
759 	    isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
760 
761 	if (IS_FC(isp)) {
762 		/*
763 		 * We do not believe firmware attributes for 2100 code less
764 		 * than 1.17.0, unless it's the firmware we specifically
765 		 * are loading.
766 		 *
767 		 * Note that all 22XX and 23XX f/w is greater than 1.X.0.
768 		 */
769 		if (!(ISP_FW_NEWER_THAN(isp, 1, 17, 0))) {
770 #ifdef	USE_SMALLER_2100_FIRMWARE
771 			FCPARAM(isp)->isp_fwattr = ISP_FW_ATTR_SCCLUN;
772 #else
773 			FCPARAM(isp)->isp_fwattr = 0;
774 #endif
775 		} else {
776 			FCPARAM(isp)->isp_fwattr = mbs.param[6];
777 			isp_prt(isp, ISP_LOGDEBUG0,
778 			    "Firmware Attributes = 0x%x", mbs.param[6]);
779 		}
780 		if (ISP_READ(isp, BIU2100_CSR) & BIU2100_PCI64) {
781 			isp_prt(isp, ISP_LOGCONFIG,
782 			    "Installed in 64-Bit PCI slot");
783 		}
784 	}
785 
786 	if (isp->isp_romfw_rev[0] || isp->isp_romfw_rev[1] ||
787 	    isp->isp_romfw_rev[2]) {
788 		isp_prt(isp, ISP_LOGCONFIG, "Last F/W revision was %d.%d.%d",
789 		    isp->isp_romfw_rev[0], isp->isp_romfw_rev[1],
790 		    isp->isp_romfw_rev[2]);
791 	}
792 
793 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
794 	isp_mboxcmd(isp, &mbs, MBLOGALL);
795 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
796 		return;
797 	}
798 	isp->isp_maxcmds = mbs.param[2];
799 	isp_prt(isp, ISP_LOGINFO,
800 	    "%d max I/O commands supported", mbs.param[2]);
801 	isp_fw_state(isp);
802 
803 	/*
804 	 * Set up DMA for the request and result mailboxes.
805 	 */
806 	if (ISP_MBOXDMASETUP(isp) != 0) {
807 		isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
808 		return;
809 	}
810 	isp->isp_state = ISP_RESETSTATE;
811 
812 	/*
813 	 * Okay- now that we have new firmware running, we now (re)set our
814 	 * notion of how many luns we support. This is somewhat tricky because
815 	 * if we haven't loaded firmware, we sometimes do not have an easy way
816 	 * of knowing how many luns we support.
817 	 *
818 	 * Expanded lun firmware gives you 32 luns for SCSI cards and
819 	 * 16384 luns for Fibre Channel cards.
820 	 *
821 	 * It turns out that even for QLogic 2100s with ROM 1.10 and above
822 	 * we do get a firmware attributes word returned in mailbox register 6.
823 	 *
824 	 * Because the lun is in a a different position in the Request Queue
825 	 * Entry structure for Fibre Channel with expanded lun firmware, we
826 	 * can only support one lun (lun zero) when we don't know what kind
827 	 * of firmware we're running.
828 	 *
829 	 * Note that we only do this once (the first time thru isp_reset)
830 	 * because we may be called again after firmware has been loaded once
831 	 * and released.
832 	 */
833 	if (IS_SCSI(isp)) {
834 		if (dodnld) {
835 			if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
836 				isp->isp_maxluns = 32;
837 			} else {
838 				isp->isp_maxluns = 8;
839 			}
840 		} else {
841 			isp->isp_maxluns = 8;
842 		}
843 	} else {
844 		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
845 			isp->isp_maxluns = 16384;
846 		} else {
847 			isp->isp_maxluns = 16;
848 		}
849 	}
850 }
851 
852 /*
853  * Initialize Parameters of Hardware to a known state.
854  *
855  * Locks are held before coming here.
856  */
857 
858 void
859 isp_init(struct ispsoftc *isp)
860 {
861 	/*
862 	 * Must do this first to get defaults established.
863 	 */
864 	isp_setdfltparm(isp, 0);
865 	if (IS_DUALBUS(isp)) {
866 		isp_setdfltparm(isp, 1);
867 	}
868 	if (IS_FC(isp)) {
869 		isp_fibre_init(isp);
870 	} else {
871 		isp_scsi_init(isp);
872 	}
873 }
874 
875 static void
876 isp_scsi_init(struct ispsoftc *isp)
877 {
878 	sdparam *sdp_chan0, *sdp_chan1;
879 	mbreg_t mbs;
880 
881 	sdp_chan0 = isp->isp_param;
882 	sdp_chan1 = sdp_chan0;
883 	if (IS_DUALBUS(isp)) {
884 		sdp_chan1++;
885 	}
886 
887 	/*
888 	 * If we have no role (neither target nor initiator), return.
889 	 */
890 	if (isp->isp_role == ISP_ROLE_NONE) {
891 		return;
892 	}
893 
894 	/* First do overall per-card settings. */
895 
896 	/*
897 	 * If we have fast memory timing enabled, turn it on.
898 	 */
899 	if (sdp_chan0->isp_fast_mttr) {
900 		ISP_WRITE(isp, RISC_MTR, 0x1313);
901 	}
902 
903 	/*
904 	 * Set Retry Delay and Count.
905 	 * You set both channels at the same time.
906 	 */
907 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
908 	mbs.param[1] = sdp_chan0->isp_retry_count;
909 	mbs.param[2] = sdp_chan0->isp_retry_delay;
910 	mbs.param[6] = sdp_chan1->isp_retry_count;
911 	mbs.param[7] = sdp_chan1->isp_retry_delay;
912 
913 	isp_mboxcmd(isp, &mbs, MBLOGALL);
914 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
915 		return;
916 	}
917 
918 	/*
919 	 * Set ASYNC DATA SETUP time. This is very important.
920 	 */
921 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
922 	mbs.param[1] = sdp_chan0->isp_async_data_setup;
923 	mbs.param[2] = sdp_chan1->isp_async_data_setup;
924 	isp_mboxcmd(isp, &mbs, MBLOGALL);
925 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
926 		return;
927 	}
928 
929 	/*
930 	 * Set ACTIVE Negation State.
931 	 */
932 	mbs.param[0] = MBOX_SET_ACT_NEG_STATE;
933 	mbs.param[1] =
934 	    (sdp_chan0->isp_req_ack_active_neg << 4) |
935 	    (sdp_chan0->isp_data_line_active_neg << 5);
936 	mbs.param[2] =
937 	    (sdp_chan1->isp_req_ack_active_neg << 4) |
938 	    (sdp_chan1->isp_data_line_active_neg << 5);
939 
940 	isp_mboxcmd(isp, &mbs, MBLOGNONE);
941 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
942 		isp_prt(isp, ISP_LOGERR,
943 		    "failed to set active negation state (%d,%d), (%d,%d)",
944 		    sdp_chan0->isp_req_ack_active_neg,
945 		    sdp_chan0->isp_data_line_active_neg,
946 		    sdp_chan1->isp_req_ack_active_neg,
947 		    sdp_chan1->isp_data_line_active_neg);
948 		/*
949 		 * But don't return.
950 		 */
951 	}
952 
953 	/*
954 	 * Set the Tag Aging limit
955 	 */
956 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
957 	mbs.param[1] = sdp_chan0->isp_tag_aging;
958 	mbs.param[2] = sdp_chan1->isp_tag_aging;
959 	isp_mboxcmd(isp, &mbs, MBLOGALL);
960 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
961 		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
962 		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
963 		return;
964 	}
965 
966 	/*
967 	 * Set selection timeout.
968 	 */
969 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
970 	mbs.param[1] = sdp_chan0->isp_selection_timeout;
971 	mbs.param[2] = sdp_chan1->isp_selection_timeout;
972 	isp_mboxcmd(isp, &mbs, MBLOGALL);
973 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
974 		return;
975 	}
976 
977 	/* now do per-channel settings */
978 	isp_scsi_channel_init(isp, 0);
979 	if (IS_DUALBUS(isp))
980 		isp_scsi_channel_init(isp, 1);
981 
982 	/*
983 	 * Now enable request/response queues
984 	 */
985 
986 	if (IS_ULTRA2(isp) || IS_1240(isp)) {
987 		mbs.param[0] = MBOX_INIT_RES_QUEUE_A64;
988 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
989 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
990 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
991 		mbs.param[4] = 0;
992 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
993 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
994 		isp_mboxcmd(isp, &mbs, MBLOGALL);
995 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
996 			return;
997 		}
998 		isp->isp_residx = mbs.param[5];
999 
1000 		mbs.param[0] = MBOX_INIT_REQ_QUEUE_A64;
1001 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1002 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1003 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1004 		mbs.param[5] = 0;
1005 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1006 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1007 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1008 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1009 			return;
1010 		}
1011 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1012 	} else {
1013 		mbs.param[0] = MBOX_INIT_RES_QUEUE;
1014 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
1015 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1016 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1017 		mbs.param[4] = 0;
1018 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1019 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1020 			return;
1021 		}
1022 		isp->isp_residx = mbs.param[5];
1023 
1024 		mbs.param[0] = MBOX_INIT_REQ_QUEUE;
1025 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1026 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1027 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1028 		mbs.param[5] = 0;
1029 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1030 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1031 			return;
1032 		}
1033 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1034 	}
1035 
1036 	/*
1037 	 * Turn on Fast Posting, LVD transitions
1038 	 *
1039 	 * Ultra2 F/W always has had fast posting (and LVD transitions)
1040 	 *
1041 	 * Ultra and older (i.e., SBus) cards may not. It's just safer
1042 	 * to assume not for them.
1043 	 */
1044 
1045 	mbs.param[0] = MBOX_SET_FW_FEATURES;
1046 	mbs.param[1] = 0;
1047 	if (IS_ULTRA2(isp))
1048 		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
1049 #ifndef	ISP_NO_RIO
1050 	if (IS_ULTRA2(isp) || IS_1240(isp))
1051 		mbs.param[1] |= FW_FEATURE_RIO_16BIT;
1052 #else
1053 #ifndef	ISP_NO_FASTPOST
1054 	if (IS_ULTRA2(isp) || IS_1240(isp))
1055 		mbs.param[1] |= FW_FEATURE_FAST_POST;
1056 #endif
1057 #endif
1058 	if (mbs.param[1] != 0) {
1059 		u_int16_t sfeat = mbs.param[1];
1060 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1061 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1062 			isp_prt(isp, ISP_LOGINFO,
1063 			    "Enabled FW features (0x%x)", sfeat);
1064 		}
1065 	}
1066 
1067 	/*
1068 	 * Let the outer layers decide whether to issue a SCSI bus reset.
1069 	 */
1070 	isp->isp_state = ISP_INITSTATE;
1071 }
1072 
1073 static void
1074 isp_scsi_channel_init(struct ispsoftc *isp, int channel)
1075 {
1076 	sdparam *sdp;
1077 	mbreg_t mbs;
1078 	int tgt;
1079 
1080 	sdp = isp->isp_param;
1081 	sdp += channel;
1082 
1083 	/*
1084 	 * Set (possibly new) Initiator ID.
1085 	 */
1086 	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
1087 	mbs.param[1] = (channel << 7) | sdp->isp_initiator_id;
1088 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1089 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1090 		return;
1091 	}
1092 	isp_prt(isp, ISP_LOGINFO, "Initiator ID is %d on Channel %d",
1093 	    sdp->isp_initiator_id, channel);
1094 
1095 
1096 	/*
1097 	 * Set current per-target parameters to an initial safe minimum.
1098 	 */
1099 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1100 		int lun;
1101 		u_int16_t sdf;
1102 
1103 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
1104 			continue;
1105 		}
1106 #ifndef	ISP_TARGET_MODE
1107 		sdf = sdp->isp_devparam[tgt].goal_flags;
1108 		sdf &= DPARM_SAFE_DFLT;
1109 		/*
1110 		 * It is not quite clear when this changed over so that
1111 		 * we could force narrow and async for 1000/1020 cards,
1112 		 * but assume that this is only the case for loaded
1113 		 * firmware.
1114 		 */
1115 		if (isp->isp_loaded_fw) {
1116 			sdf |= DPARM_NARROW | DPARM_ASYNC;
1117 		}
1118 #else
1119 		/*
1120 		 * The !$*!)$!$)* f/w uses the same index into some
1121 		 * internal table to decide how to respond to negotiations,
1122 		 * so if we've said "let's be safe" for ID X, and ID X
1123 		 * selects *us*, the negotiations will back to 'safe'
1124 		 * (as in narrow/async). What the f/w *should* do is
1125 		 * use the initiator id settings to decide how to respond.
1126 		 */
1127 		sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
1128 #endif
1129 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
1130 		mbs.param[1] = (channel << 15) | (tgt << 8);
1131 		mbs.param[2] = sdf;
1132 		if ((sdf & DPARM_SYNC) == 0) {
1133 			mbs.param[3] = 0;
1134 		} else {
1135 			mbs.param[3] =
1136 			    (sdp->isp_devparam[tgt].goal_offset << 8) |
1137 			    (sdp->isp_devparam[tgt].goal_period);
1138 		}
1139 		isp_prt(isp, ISP_LOGDEBUG0,
1140 		    "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
1141 		    channel, tgt, mbs.param[2], mbs.param[3] >> 8,
1142 		    mbs.param[3] & 0xff);
1143 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
1144 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1145 			sdf = DPARM_SAFE_DFLT;
1146 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
1147 			mbs.param[1] = (tgt << 8) | (channel << 15);
1148 			mbs.param[2] = sdf;
1149 			mbs.param[3] = 0;
1150 			isp_mboxcmd(isp, &mbs, MBLOGALL);
1151 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1152 				continue;
1153 			}
1154 		}
1155 
1156 		/*
1157 		 * We don't update any information directly from the f/w
1158 		 * because we need to run at least one command to cause a
1159 		 * new state to be latched up. So, we just assume that we
1160 		 * converge to the values we just had set.
1161 		 *
1162 		 * Ensure that we don't believe tagged queuing is enabled yet.
1163 		 * It turns out that sometimes the ISP just ignores our
1164 		 * attempts to set parameters for devices that it hasn't
1165 		 * seen yet.
1166 		 */
1167 		sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
1168 		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
1169 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
1170 			mbs.param[1] = (channel << 15) | (tgt << 8) | lun;
1171 			mbs.param[2] = sdp->isp_max_queue_depth;
1172 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
1173 			isp_mboxcmd(isp, &mbs, MBLOGALL);
1174 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1175 				break;
1176 			}
1177 		}
1178 	}
1179 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1180 		if (sdp->isp_devparam[tgt].dev_refresh) {
1181 			isp->isp_sendmarker |= (1 << channel);
1182 			isp->isp_update |= (1 << channel);
1183 			break;
1184 		}
1185 	}
1186 }
1187 
1188 /*
1189  * Fibre Channel specific initialization.
1190  *
1191  * Locks are held before coming here.
1192  */
1193 static void
1194 isp_fibre_init(struct ispsoftc *isp)
1195 {
1196 	fcparam *fcp;
1197 	isp_icb_t local, *icbp = &local;
1198 	mbreg_t mbs;
1199 	int loopid;
1200 	u_int64_t nwwn, pwwn;
1201 
1202 	fcp = isp->isp_param;
1203 
1204 	/*
1205 	 * Do this *before* initializing the firmware.
1206 	 */
1207 	isp_mark_getpdb_all(isp);
1208 	fcp->isp_fwstate = FW_CONFIG_WAIT;
1209 	fcp->isp_loopstate = LOOP_NIL;
1210 
1211 	/*
1212 	 * If we have no role (neither target nor initiator), return.
1213 	 */
1214 	if (isp->isp_role == ISP_ROLE_NONE) {
1215 		return;
1216 	}
1217 
1218 	loopid = fcp->isp_loopid;
1219 	MEMZERO(icbp, sizeof (*icbp));
1220 	icbp->icb_version = ICB_VERSION1;
1221 
1222 	/*
1223 	 * Firmware Options are either retrieved from NVRAM or
1224 	 * are patched elsewhere. We check them for sanity here
1225 	 * and make changes based on board revision, but otherwise
1226 	 * let others decide policy.
1227 	 */
1228 
1229 	/*
1230 	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1231 	 */
1232 	if ((isp->isp_type == ISP_HA_FC_2100) && isp->isp_revision < 5) {
1233 		fcp->isp_fwoptions &= ~ICBOPT_FAIRNESS;
1234 	}
1235 
1236 	/*
1237 	 * We have to use FULL LOGIN even though it resets the loop too much
1238 	 * because otherwise port database entries don't get updated after
1239 	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1240 	 */
1241 	if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1242 		fcp->isp_fwoptions |= ICBOPT_FULL_LOGIN;
1243 	}
1244 
1245 	/*
1246 	 * Insist on Port Database Update Async notifications
1247 	 */
1248 	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
1249 
1250 	/*
1251 	 * Make sure that target role reflects into fwoptions.
1252 	 */
1253 	if (isp->isp_role & ISP_ROLE_TARGET) {
1254 		fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE;
1255 	} else {
1256 		fcp->isp_fwoptions &= ~ICBOPT_TGT_ENABLE;
1257 	}
1258 
1259 	/*
1260 	 * Propagate all of this into the ICB structure.
1261 	 */
1262 	icbp->icb_fwoptions = fcp->isp_fwoptions;
1263 	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
1264 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
1265 	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1266 		isp_prt(isp, ISP_LOGERR,
1267 		    "bad frame length (%d) from NVRAM- using %d",
1268 		    fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
1269 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1270 	}
1271 	icbp->icb_maxalloc = fcp->isp_maxalloc;
1272 	if (icbp->icb_maxalloc < 1) {
1273 		isp_prt(isp, ISP_LOGERR,
1274 		    "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1275 		icbp->icb_maxalloc = 16;
1276 	}
1277 	icbp->icb_execthrottle = fcp->isp_execthrottle;
1278 	if (icbp->icb_execthrottle < 1) {
1279 		isp_prt(isp, ISP_LOGERR,
1280 		    "bad execution throttle of %d- using 16",
1281 		    fcp->isp_execthrottle);
1282 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1283 	}
1284 	icbp->icb_retry_delay = fcp->isp_retry_delay;
1285 	icbp->icb_retry_count = fcp->isp_retry_count;
1286 	icbp->icb_hardaddr = loopid;
1287 	/*
1288 	 * Right now we just set extended options to prefer point-to-point
1289 	 * over loop based upon some soft config options.
1290 	 *
1291 	 * NB: for the 2300, ICBOPT_EXTENDED is required.
1292 	 */
1293 	if (IS_2200(isp) || IS_23XX(isp)) {
1294 		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1295 		/*
1296 		 * Prefer or force Point-To-Point instead Loop?
1297 		 */
1298 		switch(isp->isp_confopts & ISP_CFG_PORT_PREF) {
1299 		case ISP_CFG_NPORT:
1300 			icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
1301 			break;
1302 		case ISP_CFG_NPORT_ONLY:
1303 			icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
1304 			break;
1305 		case ISP_CFG_LPORT_ONLY:
1306 			icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
1307 			break;
1308 		default:
1309 			icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
1310 			break;
1311 		}
1312 		if (IS_23XX(isp)) {
1313 			/*
1314 			 * QLogic recommends that FAST Posting be turned
1315 			 * off for 23XX cards and instead allow the HBA
1316 			 * to write response queue entries and interrupt
1317 			 * after a delay (ZIO).
1318 			 *
1319 			 * If we set ZIO, it will disable fast posting,
1320 			 * so we don't need to clear it in fwoptions.
1321 			 */
1322 			icbp->icb_xfwoptions |= ICBXOPT_ZIO;
1323 
1324 			if (isp->isp_confopts & ISP_CFG_ONEGB) {
1325 				icbp->icb_zfwoptions |= ICBZOPT_RATE_ONEGB;
1326 			} else if (isp->isp_confopts & ISP_CFG_TWOGB) {
1327 				icbp->icb_zfwoptions |= ICBZOPT_RATE_TWOGB;
1328 			} else {
1329 				icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
1330 			}
1331 		}
1332 	}
1333 
1334 #ifndef	ISP_NO_RIO_FC
1335 	/*
1336 	 * RIO seems to be enabled in 2100s for fw >= 1.17.0.
1337 	 *
1338 	 * I've had some questionable problems with RIO on 2200.
1339 	 * More specifically, on a 2204 I had problems with RIO
1340 	 * on a Linux system where I was dropping commands right
1341 	 * and left. It's not clear to me what the actual problem
1342 	 * was.
1343 	 *
1344 	 * 23XX Cards do not support RIO. Instead they support ZIO.
1345 	 */
1346 #if	0
1347 	if (!IS_23XX(isp) && ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1348 		icbp->icb_xfwoptions |= ICBXOPT_RIO_16BIT;
1349 		icbp->icb_racctimer = 4;
1350 		icbp->icb_idelaytimer = 8;
1351 	}
1352 #endif
1353 #endif
1354 
1355 	/*
1356 	 * For 22XX > 2.1.26 && 23XX, set someoptions.
1357 	 * XXX: Probably okay for newer 2100 f/w too.
1358 	 */
1359 	if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
1360 		/*
1361 		 * Turn on LIP F8 async event (1)
1362 		 * Turn on generate AE 8013 on all LIP Resets (2)
1363 		 * Disable LIP F7 switching (8)
1364 		 */
1365 		mbs.param[0] = MBOX_SET_FIRMWARE_OPTIONS;
1366 		mbs.param[1] = 0xb;
1367 		mbs.param[2] = 0;
1368 		mbs.param[3] = 0;
1369 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1370 	}
1371 	icbp->icb_logintime = 30;	/* 30 second login timeout */
1372 
1373 	if (IS_23XX(isp)) {
1374 		ISP_WRITE(isp, isp->isp_rqstinrp, 0);
1375         	ISP_WRITE(isp, isp->isp_rqstoutrp, 0);
1376         	ISP_WRITE(isp, isp->isp_respinrp, 0);
1377 		ISP_WRITE(isp, isp->isp_respoutrp, 0);
1378 	}
1379 
1380 	nwwn = ISP_NODEWWN(isp);
1381 	pwwn = ISP_PORTWWN(isp);
1382 	if (nwwn && pwwn) {
1383 		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
1384 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, nwwn);
1385 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
1386 		isp_prt(isp, ISP_LOGDEBUG1,
1387 		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1388 		    ((u_int32_t) (nwwn >> 32)),
1389 		    ((u_int32_t) (nwwn & 0xffffffff)),
1390 		    ((u_int32_t) (pwwn >> 32)),
1391 		    ((u_int32_t) (pwwn & 0xffffffff)));
1392 	} else {
1393 		isp_prt(isp, ISP_LOGDEBUG1, "Not using any WWNs");
1394 		icbp->icb_fwoptions &= ~(ICBOPT_BOTH_WWNS|ICBOPT_FULL_LOGIN);
1395 	}
1396 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1397 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1398 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1399 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1400 	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1401 	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1402 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1403 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1404 	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1405 	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1406 	isp_prt(isp, ISP_LOGDEBUG0,
1407 	    "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
1408 	    icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
1409 
1410 	FC_SCRATCH_ACQUIRE(isp);
1411 	isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
1412 
1413 	/*
1414 	 * Init the firmware
1415 	 */
1416 	mbs.param[0] = MBOX_INIT_FIRMWARE;
1417 	mbs.param[1] = 0;
1418 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1419 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1420 	mbs.param[4] = 0;
1421 	mbs.param[5] = 0;
1422 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1423 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1424 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1425 	FC_SCRATCH_RELEASE(isp);
1426 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1427 		return;
1428 	}
1429 	isp->isp_reqidx = isp->isp_reqodx = 0;
1430 	isp->isp_residx = 0;
1431 	isp->isp_sendmarker = 1;
1432 
1433 	/*
1434 	 * Whatever happens, we're now committed to being here.
1435 	 */
1436 	isp->isp_state = ISP_INITSTATE;
1437 }
1438 
1439 /*
1440  * Fibre Channel Support- get the port database for the id.
1441  *
1442  * Locks are held before coming here. Return 0 if success,
1443  * else failure.
1444  */
1445 
1446 static int
1447 isp_getmap(struct ispsoftc *isp, fcpos_map_t *map)
1448 {
1449 	fcparam *fcp = (fcparam *) isp->isp_param;
1450 	mbreg_t mbs;
1451 
1452 	mbs.param[0] = MBOX_GET_FC_AL_POSITION_MAP;
1453 	mbs.param[1] = 0;
1454 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1455 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1456 	/*
1457 	 * Unneeded. For the 2100, except for initializing f/w, registers
1458 	 * 4/5 have to not be written to.
1459 	 *	mbs.param[4] = 0;
1460 	 *	mbs.param[5] = 0;
1461 	 *
1462 	 */
1463 	mbs.param[6] = 0;
1464 	mbs.param[7] = 0;
1465 	FC_SCRATCH_ACQUIRE(isp);
1466 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1467 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1468 		MEMCPY(map, fcp->isp_scratch, sizeof (fcpos_map_t));
1469 		map->fwmap = mbs.param[1] != 0;
1470 		FC_SCRATCH_RELEASE(isp);
1471 		return (0);
1472 	}
1473 	FC_SCRATCH_RELEASE(isp);
1474 	return (-1);
1475 }
1476 
1477 static void
1478 isp_mark_getpdb_all(struct ispsoftc *isp)
1479 {
1480 	fcparam *fcp = (fcparam *) isp->isp_param;
1481 	int i;
1482 	for (i = 0; i < MAX_FC_TARG; i++) {
1483 		fcp->portdb[i].valid = fcp->portdb[i].fabric_dev = 0;
1484 	}
1485 }
1486 
1487 static int
1488 isp_getpdb(struct ispsoftc *isp, int id, isp_pdb_t *pdbp)
1489 {
1490 	fcparam *fcp = (fcparam *) isp->isp_param;
1491 	mbreg_t mbs;
1492 
1493 	mbs.param[0] = MBOX_GET_PORT_DB;
1494 	mbs.param[1] = id << 8;
1495 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1496 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1497 	/*
1498 	 * Unneeded. For the 2100, except for initializing f/w, registers
1499 	 * 4/5 have to not be written to.
1500 	 *	mbs.param[4] = 0;
1501 	 *	mbs.param[5] = 0;
1502 	 *
1503 	 */
1504 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1505 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1506 	FC_SCRATCH_ACQUIRE(isp);
1507 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1508 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1509 		isp_get_pdb(isp, (isp_pdb_t *)fcp->isp_scratch, pdbp);
1510 		FC_SCRATCH_RELEASE(isp);
1511 		return (0);
1512 	}
1513 	FC_SCRATCH_RELEASE(isp);
1514 	return (-1);
1515 }
1516 
1517 static u_int64_t
1518 isp_get_portname(struct ispsoftc *isp, int loopid, int nodename)
1519 {
1520 	u_int64_t wwn = 0;
1521 	mbreg_t mbs;
1522 
1523 	mbs.param[0] = MBOX_GET_PORT_NAME;
1524 	mbs.param[1] = loopid << 8;
1525 	if (nodename)
1526 		mbs.param[1] |= 1;
1527 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1528 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1529 		wwn =
1530 		    (((u_int64_t)(mbs.param[2] & 0xff)) << 56) |
1531 		    (((u_int64_t)(mbs.param[2] >> 8))	<< 48) |
1532 		    (((u_int64_t)(mbs.param[3] & 0xff))	<< 40) |
1533 		    (((u_int64_t)(mbs.param[3] >> 8))	<< 32) |
1534 		    (((u_int64_t)(mbs.param[6] & 0xff))	<< 24) |
1535 		    (((u_int64_t)(mbs.param[6] >> 8))	<< 16) |
1536 		    (((u_int64_t)(mbs.param[7] & 0xff))	<<  8) |
1537 		    (((u_int64_t)(mbs.param[7] >> 8)));
1538 	}
1539 	return (wwn);
1540 }
1541 
1542 /*
1543  * Make sure we have good FC link and know our Loop ID.
1544  */
1545 
1546 static int
1547 isp_fclink_test(struct ispsoftc *isp, int usdelay)
1548 {
1549 	static char *toponames[] = {
1550 		"Private Loop",
1551 		"FL Port",
1552 		"N-Port to N-Port",
1553 		"F Port",
1554 		"F Port (no FLOGI_ACC response)"
1555 	};
1556 	mbreg_t mbs;
1557 	int count, check_for_fabric;
1558 	u_int8_t lwfs;
1559 	fcparam *fcp;
1560 	struct lportdb *lp;
1561 	isp_pdb_t pdb;
1562 
1563 	fcp = isp->isp_param;
1564 
1565 	/*
1566 	 * XXX: Here is where we would start a 'loop dead' timeout
1567 	 */
1568 
1569 	/*
1570 	 * Wait up to N microseconds for F/W to go to a ready state.
1571 	 */
1572 	lwfs = FW_CONFIG_WAIT;
1573 	count = 0;
1574 	while (count < usdelay) {
1575 		u_int64_t enano;
1576 		u_int32_t wrk;
1577 		NANOTIME_T hra, hrb;
1578 
1579 		GET_NANOTIME(&hra);
1580 		isp_fw_state(isp);
1581 		if (lwfs != fcp->isp_fwstate) {
1582 			isp_prt(isp, ISP_LOGINFO, "Firmware State <%s->%s>",
1583 			    isp2100_fw_statename((int)lwfs),
1584 			    isp2100_fw_statename((int)fcp->isp_fwstate));
1585 			lwfs = fcp->isp_fwstate;
1586 		}
1587 		if (fcp->isp_fwstate == FW_READY) {
1588 			break;
1589 		}
1590 		GET_NANOTIME(&hrb);
1591 
1592 		/*
1593 		 * Get the elapsed time in nanoseconds.
1594 		 * Always guaranteed to be non-zero.
1595 		 */
1596 		enano = NANOTIME_SUB(&hrb, &hra);
1597 
1598 		isp_prt(isp, ISP_LOGDEBUG1,
1599 		    "usec%d: 0x%lx->0x%lx enano 0x%x%08x",
1600 		    count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb),
1601 		    (u_int32_t)(enano >> 32), (u_int32_t)(enano & 0xffffffff));
1602 
1603 		/*
1604 		 * If the elapsed time is less than 1 millisecond,
1605 		 * delay a period of time up to that millisecond of
1606 		 * waiting.
1607 		 *
1608 		 * This peculiar code is an attempt to try and avoid
1609 		 * invoking u_int64_t math support functions for some
1610 		 * platforms where linkage is a problem.
1611 		 */
1612 		if (enano < (1000 * 1000)) {
1613 			count += 1000;
1614 			enano = (1000 * 1000) - enano;
1615 			while (enano > (u_int64_t) 4000000000U) {
1616 				USEC_SLEEP(isp, 4000000);
1617 				enano -= (u_int64_t) 4000000000U;
1618 			}
1619 			wrk = enano;
1620 			wrk /= 1000;
1621 			USEC_SLEEP(isp, wrk);
1622 		} else {
1623 			while (enano > (u_int64_t) 4000000000U) {
1624 				count += 4000000;
1625 				enano -= (u_int64_t) 4000000000U;
1626 			}
1627 			wrk = enano;
1628 			count += (wrk / 1000);
1629 		}
1630 	}
1631 
1632 	/*
1633 	 * If we haven't gone to 'ready' state, return.
1634 	 */
1635 	if (fcp->isp_fwstate != FW_READY) {
1636 		return (-1);
1637 	}
1638 
1639 	/*
1640 	 * Get our Loop ID (if possible). We really need to have it.
1641 	 */
1642 	mbs.param[0] = MBOX_GET_LOOP_ID;
1643 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1644 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1645 		return (-1);
1646 	}
1647 	fcp->isp_loopid = mbs.param[1];
1648 	if (IS_2200(isp) || IS_23XX(isp)) {
1649 		int topo = (int) mbs.param[6];
1650 		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB)
1651 			topo = TOPO_PTP_STUB;
1652 		fcp->isp_topo = topo;
1653 	} else {
1654 		fcp->isp_topo = TOPO_NL_PORT;
1655 	}
1656 	fcp->isp_portid = fcp->isp_alpa = mbs.param[2] & 0xff;
1657 
1658 	/*
1659 	 * Check to see if we're on a fabric by trying to see if we
1660 	 * can talk to the fabric name server. This can be a bit
1661 	 * tricky because if we're a 2100, we should check always
1662 	 * (in case we're connected to an server doing aliasing).
1663 	 */
1664 	fcp->isp_onfabric = 0;
1665 
1666 	if (IS_2100(isp)) {
1667 		/*
1668 		 * Don't bother with fabric if we are using really old
1669 		 * 2100 firmware. It's just not worth it.
1670 		 */
1671 		if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
1672 			check_for_fabric = 1;
1673 		} else {
1674 			check_for_fabric = 0;
1675 		}
1676 	} else if (fcp->isp_topo == TOPO_FL_PORT ||
1677 	    fcp->isp_topo == TOPO_F_PORT) {
1678 		check_for_fabric = 1;
1679 	} else
1680 		check_for_fabric = 0;
1681 
1682 	if (check_for_fabric && isp_getpdb(isp, FL_PORT_ID, &pdb) == 0) {
1683 		int loopid = FL_PORT_ID;
1684 		if (IS_2100(isp)) {
1685 			fcp->isp_topo = TOPO_FL_PORT;
1686 		}
1687 
1688 		if (BITS2WORD(pdb.pdb_portid_bits) == 0) {
1689 			/*
1690 			 * Crock.
1691 			 */
1692 			fcp->isp_topo = TOPO_NL_PORT;
1693 			goto not_on_fabric;
1694 		}
1695 		fcp->isp_portid = mbs.param[2] | ((int) mbs.param[3] << 16);
1696 
1697 		/*
1698 		 * Save the Fabric controller's port database entry.
1699 		 */
1700 		lp = &fcp->portdb[loopid];
1701 		lp->node_wwn =
1702 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1703 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1704 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1705 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1706 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1707 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1708 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1709 		    (((u_int64_t)pdb.pdb_nodename[7]));
1710 		lp->port_wwn =
1711 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1712 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1713 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1714 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1715 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1716 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1717 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1718 		    (((u_int64_t)pdb.pdb_portname[7]));
1719 		lp->roles =
1720 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1721 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1722 		lp->loopid = pdb.pdb_loopid;
1723 		lp->loggedin = lp->valid = 1;
1724 		fcp->isp_onfabric = 1;
1725 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1726 		isp_register_fc4_type(isp);
1727 	} else {
1728 not_on_fabric:
1729 		fcp->isp_onfabric = 0;
1730 		fcp->portdb[FL_PORT_ID].valid = 0;
1731 	}
1732 
1733 	fcp->isp_gbspeed = 1;
1734 	if (IS_23XX(isp)) {
1735 		mbs.param[0] = MBOX_GET_SET_DATA_RATE;
1736 		mbs.param[1] = MBGSD_GET_RATE;
1737 		/* mbs.param[2] undefined if we're just getting rate */
1738 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1739 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1740 			if (mbs.param[1] == MBGSD_TWOGB) {
1741 				isp_prt(isp, ISP_LOGINFO, "2Gb link speed/s");
1742 				fcp->isp_gbspeed = 2;
1743 			}
1744 		}
1745 	}
1746 
1747 	isp_prt(isp, ISP_LOGCONFIG, topology, fcp->isp_loopid, fcp->isp_alpa,
1748 	    fcp->isp_portid, fcp->isp_loopstate, toponames[fcp->isp_topo]);
1749 
1750 	/*
1751 	 * Announce ourselves, too. This involves synthesizing an entry.
1752 	 */
1753 	if (fcp->isp_iid_set == 0) {
1754 		fcp->isp_iid_set = 1;
1755 		fcp->isp_iid = fcp->isp_loopid;
1756 		lp = &fcp->portdb[fcp->isp_iid];
1757 	} else {
1758 		lp = &fcp->portdb[fcp->isp_iid];
1759 		if (fcp->isp_portid != lp->portid ||
1760 		    fcp->isp_loopid != lp->loopid ||
1761 		    fcp->isp_nodewwn != ISP_NODEWWN(isp) ||
1762 		    fcp->isp_portwwn != ISP_PORTWWN(isp)) {
1763 			lp->valid = 0;
1764 			count = fcp->isp_iid;
1765 			(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1766 		}
1767 	}
1768 	lp->loopid = fcp->isp_loopid;
1769 	lp->portid = fcp->isp_portid;
1770 	lp->node_wwn = ISP_NODEWWN(isp);
1771 	lp->port_wwn = ISP_PORTWWN(isp);
1772 	switch (isp->isp_role) {
1773 	case ISP_ROLE_NONE:
1774 		lp->roles = 0;
1775 		break;
1776 	case ISP_ROLE_TARGET:
1777 		lp->roles = SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT;
1778 		break;
1779 	case ISP_ROLE_INITIATOR:
1780 		lp->roles = SVC3_INI_ROLE >> SVC3_ROLE_SHIFT;
1781 		break;
1782 	case ISP_ROLE_BOTH:
1783 		lp->roles = (SVC3_INI_ROLE|SVC3_TGT_ROLE) >> SVC3_ROLE_SHIFT;
1784 		break;
1785 	}
1786 	lp->loggedin = lp->valid = 1;
1787 	count = fcp->isp_iid;
1788 	(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1789 	return (0);
1790 }
1791 
1792 static char *
1793 isp2100_fw_statename(int state)
1794 {
1795 	switch(state) {
1796 	case FW_CONFIG_WAIT:	return "Config Wait";
1797 	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
1798 	case FW_WAIT_LOGIN:	return "Wait Login";
1799 	case FW_READY:		return "Ready";
1800 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
1801 	case FW_ERROR:		return "Error";
1802 	case FW_REINIT:		return "Re-Init";
1803 	case FW_NON_PART:	return "Nonparticipating";
1804 	default:		return "?????";
1805 	}
1806 }
1807 
1808 /*
1809  * Synchronize our soft copy of the port database with what the f/w thinks
1810  * (with a view toward possibly for a specific target....)
1811  */
1812 
1813 static int
1814 isp_pdb_sync(struct ispsoftc *isp)
1815 {
1816 	struct lportdb *lp;
1817 	fcparam *fcp = isp->isp_param;
1818 	isp_pdb_t pdb;
1819 	int loopid, base, lim;
1820 
1821 	/*
1822 	 * Make sure we're okay for doing this right now.
1823 	 */
1824 	if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
1825 	    fcp->isp_loopstate != LOOP_FSCAN_DONE &&
1826 	    fcp->isp_loopstate != LOOP_LSCAN_DONE) {
1827 		return (-1);
1828 	}
1829 
1830 	if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT ||
1831 	    fcp->isp_topo == TOPO_N_PORT) {
1832 		if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
1833 			if (isp_scan_loop(isp) != 0) {
1834 				return (-1);
1835 			}
1836 		}
1837 	}
1838 	fcp->isp_loopstate = LOOP_SYNCING_PDB;
1839 
1840 	/*
1841 	 * If we get this far, we've settled our differences with the f/w
1842 	 * (for local loop device) and we can say that the loop state is ready.
1843 	 */
1844 
1845 	if (fcp->isp_topo == TOPO_NL_PORT) {
1846 		fcp->loop_seen_once = 1;
1847 		fcp->isp_loopstate = LOOP_READY;
1848 		return (0);
1849 	}
1850 
1851 	/*
1852 	 * Find all Fabric Entities that didn't make it from one scan to the
1853 	 * next and let the world know they went away. Scan the whole database.
1854 	 */
1855 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
1856 		if (lp->was_fabric_dev && lp->fabric_dev == 0) {
1857 			loopid = lp - fcp->portdb;
1858 			lp->valid = 0;	/* should already be set */
1859 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1860 			MEMZERO((void *) lp, sizeof (*lp));
1861 			continue;
1862 		}
1863 		lp->was_fabric_dev = lp->fabric_dev;
1864 	}
1865 
1866 	if (fcp->isp_topo == TOPO_FL_PORT)
1867 		base = FC_SNS_ID+1;
1868 	else
1869 		base = 0;
1870 
1871 	if (fcp->isp_topo == TOPO_N_PORT)
1872 		lim = 1;
1873 	else
1874 		lim = MAX_FC_TARG;
1875 
1876 	/*
1877 	 * Now log in any fabric devices that the outer layer has
1878 	 * left for us to see. This seems the most sane policy
1879 	 * for the moment.
1880 	 */
1881 	for (lp = &fcp->portdb[base]; lp < &fcp->portdb[lim]; lp++) {
1882 		u_int32_t portid;
1883 		mbreg_t mbs;
1884 
1885 		loopid = lp - fcp->portdb;
1886 		if (loopid >= FL_PORT_ID && loopid <= FC_SNS_ID) {
1887 			continue;
1888 		}
1889 
1890 		/*
1891 		 * Anything here?
1892 		 */
1893 		if (lp->port_wwn == 0) {
1894 			continue;
1895 		}
1896 
1897 		/*
1898 		 * Don't try to log into yourself.
1899 		 */
1900 		if ((portid = lp->portid) == fcp->isp_portid) {
1901 			continue;
1902 		}
1903 
1904 
1905 		/*
1906 		 * If we'd been logged in- see if we still are and we haven't
1907 		 * changed. If so, no need to log ourselves out, etc..
1908 		 *
1909 		 * Unfortunately, our charming Qlogic f/w has decided to
1910 		 * return a valid port database entry for a fabric device
1911 		 * that has, in fact, gone away. And it hangs trying to
1912 		 * log it out.
1913 		 */
1914 		if (lp->loggedin && lp->force_logout == 0 &&
1915 		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
1916 			int nrole;
1917 			u_int64_t nwwnn, nwwpn;
1918 			nwwnn =
1919 			    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1920 			    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1921 			    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1922 			    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1923 			    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1924 			    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1925 			    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1926 			    (((u_int64_t)pdb.pdb_nodename[7]));
1927 			nwwpn =
1928 			    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1929 			    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1930 			    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1931 			    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1932 			    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1933 			    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1934 			    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1935 			    (((u_int64_t)pdb.pdb_portname[7]));
1936 			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
1937 			    SVC3_ROLE_SHIFT;
1938 			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
1939 			    (u_int32_t) BITS2WORD(pdb.pdb_portid_bits) &&
1940 			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
1941 			    lp->roles == nrole && lp->force_logout == 0) {
1942 				lp->loggedin = lp->valid = 1;
1943 				isp_prt(isp, ISP_LOGCONFIG, lretained,
1944 				    (int) (lp - fcp->portdb),
1945 				    (int) lp->loopid, lp->portid);
1946 				continue;
1947 			}
1948 		}
1949 
1950 		if (fcp->isp_fwstate != FW_READY ||
1951 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
1952 			return (-1);
1953 		}
1954 
1955 		/*
1956 		 * Force a logout if we were logged in.
1957 		 */
1958 		if (lp->loggedin) {
1959 			if (lp->force_logout ||
1960 			    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
1961 				mbs.param[0] = MBOX_FABRIC_LOGOUT;
1962 				mbs.param[1] = lp->loopid << 8;
1963 				mbs.param[2] = 0;
1964 				mbs.param[3] = 0;
1965 				isp_mboxcmd(isp, &mbs, MBLOGNONE);
1966 				isp_prt(isp, ISP_LOGINFO, plogout,
1967 				    (int) (lp - fcp->portdb), lp->loopid,
1968 				    lp->portid);
1969 			}
1970 			lp->force_logout = lp->loggedin = 0;
1971 			if (fcp->isp_fwstate != FW_READY ||
1972 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
1973 				return (-1);
1974 			}
1975 		}
1976 
1977 		/*
1978 		 * And log in....
1979 		 */
1980 		loopid = lp - fcp->portdb;
1981 		lp->loopid = FL_PORT_ID;
1982 		do {
1983 			mbs.param[0] = MBOX_FABRIC_LOGIN;
1984 			mbs.param[1] = loopid << 8;
1985 			mbs.param[2] = portid >> 16;
1986 			mbs.param[3] = portid & 0xffff;
1987 			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
1988 			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
1989 			if (fcp->isp_fwstate != FW_READY ||
1990 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
1991 				return (-1);
1992 			}
1993 			switch (mbs.param[0]) {
1994 			case MBOX_LOOP_ID_USED:
1995 				/*
1996 				 * Try the next available loop id.
1997 				 */
1998 				loopid++;
1999 				break;
2000 			case MBOX_PORT_ID_USED:
2001 				/*
2002 				 * This port is already logged in.
2003 				 * Snaffle the loop id it's using if it's
2004 				 * nonzero, otherwise we're hosed.
2005 				 */
2006 				if (mbs.param[1] != 0) {
2007 					loopid = mbs.param[1];
2008 					isp_prt(isp, ISP_LOGINFO, retained,
2009 					    loopid, (int) (lp - fcp->portdb),
2010 					    lp->portid);
2011 				} else {
2012 					loopid = MAX_FC_TARG;
2013 					break;
2014 				}
2015 				/* FALLTHROUGH */
2016 			case MBOX_COMMAND_COMPLETE:
2017 				lp->loggedin = 1;
2018 				lp->loopid = loopid;
2019 				break;
2020 			case MBOX_COMMAND_ERROR:
2021 				isp_prt(isp, ISP_LOGINFO, plogierr,
2022 				    portid, mbs.param[1]);
2023 				/* FALLTHROUGH */
2024 			case MBOX_ALL_IDS_USED: /* We're outta IDs */
2025 			default:
2026 				loopid = MAX_FC_TARG;
2027 				break;
2028 			}
2029 		} while (lp->loopid == FL_PORT_ID && loopid < MAX_FC_TARG);
2030 
2031 		/*
2032 		 * If we get here and we haven't set a Loop ID,
2033 		 * we failed to log into this device.
2034 		 */
2035 
2036 		if (lp->loopid == FL_PORT_ID) {
2037 			lp->loopid = 0;
2038 			continue;
2039 		}
2040 
2041 		/*
2042 		 * Make sure we can get the approriate port information.
2043 		 */
2044 		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
2045 			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
2046 			goto dump_em;
2047 		}
2048 
2049 		if (fcp->isp_fwstate != FW_READY ||
2050 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2051 			return (-1);
2052 		}
2053 
2054 		if (pdb.pdb_loopid != lp->loopid) {
2055 			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
2056 			    lp->portid, pdb.pdb_loopid);
2057 			goto dump_em;
2058 		}
2059 
2060 		if (lp->portid != (u_int32_t) BITS2WORD(pdb.pdb_portid_bits)) {
2061 			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
2062 			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
2063 			goto dump_em;
2064 		}
2065 
2066 		lp->roles =
2067 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2068 		lp->node_wwn =
2069 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
2070 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
2071 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
2072 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
2073 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
2074 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
2075 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
2076 		    (((u_int64_t)pdb.pdb_nodename[7]));
2077 		lp->port_wwn =
2078 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
2079 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
2080 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
2081 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
2082 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
2083 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
2084 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
2085 		    (((u_int64_t)pdb.pdb_portname[7]));
2086 		/*
2087 		 * Check to make sure this all makes sense.
2088 		 */
2089 		if (lp->node_wwn && lp->port_wwn) {
2090 			lp->valid = 1;
2091 			loopid = lp - fcp->portdb;
2092 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2093 			continue;
2094 		}
2095 dump_em:
2096 		lp->valid = 0;
2097 		isp_prt(isp, ISP_LOGINFO,
2098 		    ldumped, loopid, lp->loopid, lp->portid);
2099 		mbs.param[0] = MBOX_FABRIC_LOGOUT;
2100 		mbs.param[1] = lp->loopid << 8;
2101 		mbs.param[2] = 0;
2102 		mbs.param[3] = 0;
2103 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
2104 		if (fcp->isp_fwstate != FW_READY ||
2105 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2106 			return (-1);
2107 		}
2108 	}
2109 	/*
2110 	 * If we get here, we've for sure seen not only a valid loop
2111 	 * but know what is or isn't on it, so mark this for usage
2112 	 * in isp_start.
2113 	 */
2114 	fcp->loop_seen_once = 1;
2115 	fcp->isp_loopstate = LOOP_READY;
2116 	return (0);
2117 }
2118 
2119 static int
2120 isp_scan_loop(struct ispsoftc *isp)
2121 {
2122 	struct lportdb *lp;
2123 	fcparam *fcp = isp->isp_param;
2124 	isp_pdb_t pdb;
2125 	int loopid, lim, hival;
2126 
2127 	switch (fcp->isp_topo) {
2128 	case TOPO_NL_PORT:
2129 		hival = FL_PORT_ID;
2130 		break;
2131 	case TOPO_N_PORT:
2132 		hival = 2;
2133 		break;
2134 	case TOPO_FL_PORT:
2135 		hival = FC_PORT_ID;
2136 		break;
2137 	default:
2138 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
2139 		return (0);
2140 	}
2141 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
2142 
2143 	/*
2144 	 * make sure the temp port database is clean...
2145 	 */
2146 	MEMZERO((void *)fcp->tport, sizeof (fcp->tport));
2147 
2148 	/*
2149 	 * Run through the local loop ports and get port database info
2150 	 * for each loop ID.
2151 	 *
2152 	 * There's a somewhat unexplained situation where the f/w passes back
2153 	 * the wrong database entity- if that happens, just restart (up to
2154 	 * FL_PORT_ID times).
2155 	 */
2156 	for (lim = loopid = 0; loopid < hival; loopid++) {
2157 		lp = &fcp->tport[loopid];
2158 
2159 		/*
2160 		 * Don't even try for ourselves...
2161 	 	 */
2162 		if (loopid == fcp->isp_loopid)
2163 			continue;
2164 
2165 		lp->node_wwn = isp_get_portname(isp, loopid, 1);
2166 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2167 			return (-1);
2168 		if (lp->node_wwn == 0)
2169 			continue;
2170 		lp->port_wwn = isp_get_portname(isp, loopid, 0);
2171 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2172 			return (-1);
2173 		if (lp->port_wwn == 0) {
2174 			lp->node_wwn = 0;
2175 			continue;
2176 		}
2177 
2178 		/*
2179 		 * Get an entry....
2180 		 */
2181 		if (isp_getpdb(isp, loopid, &pdb) != 0) {
2182 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2183 				return (-1);
2184 			continue;
2185 		}
2186 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2187 			return (-1);
2188 		}
2189 
2190 		/*
2191 		 * If the returned database element doesn't match what we
2192 		 * asked for, restart the process entirely (up to a point...).
2193 		 */
2194 		if (pdb.pdb_loopid != loopid) {
2195 			loopid = 0;
2196 			if (lim++ < hival) {
2197 				continue;
2198 			}
2199 			isp_prt(isp, ISP_LOGWARN,
2200 			    "giving up on synchronizing the port database");
2201 			return (-1);
2202 		}
2203 
2204 		/*
2205 		 * Save the pertinent info locally.
2206 		 */
2207 		lp->node_wwn =
2208 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
2209 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
2210 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
2211 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
2212 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
2213 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
2214 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
2215 		    (((u_int64_t)pdb.pdb_nodename[7]));
2216 		lp->port_wwn =
2217 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
2218 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
2219 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
2220 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
2221 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
2222 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
2223 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
2224 		    (((u_int64_t)pdb.pdb_portname[7]));
2225 		lp->roles =
2226 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2227 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
2228 		lp->loopid = pdb.pdb_loopid;
2229 	}
2230 
2231 	/*
2232 	 * Mark all of the permanent local loop database entries as invalid
2233 	 * (except our own entry).
2234 	 */
2235 	for (loopid = 0; loopid < hival; loopid++) {
2236 		if (loopid == fcp->isp_iid) {
2237 			fcp->portdb[loopid].valid = 1;
2238 			fcp->portdb[loopid].loopid = fcp->isp_loopid;
2239 			continue;
2240 		}
2241 		fcp->portdb[loopid].valid = 0;
2242 	}
2243 
2244 	/*
2245 	 * Now merge our local copy of the port database into our saved copy.
2246 	 * Notify the outer layers of new devices arriving.
2247 	 */
2248 	for (loopid = 0; loopid < hival; loopid++) {
2249 		int i;
2250 
2251 		/*
2252 		 * If we don't have a non-zero Port WWN, we're not here.
2253 		 */
2254 		if (fcp->tport[loopid].port_wwn == 0) {
2255 			continue;
2256 		}
2257 
2258 		/*
2259 		 * Skip ourselves.
2260 		 */
2261 		if (loopid == fcp->isp_iid) {
2262 			continue;
2263 		}
2264 
2265 		/*
2266 		 * For the purposes of deciding whether this is the
2267 		 * 'same' device or not, we only search for an identical
2268 		 * Port WWN. Node WWNs may or may not be the same as
2269 		 * the Port WWN, and there may be multiple different
2270 		 * Port WWNs with the same Node WWN. It would be chaos
2271 		 * to have multiple identical Port WWNs, so we don't
2272 		 * allow that.
2273 		 */
2274 
2275 		for (i = 0; i < hival; i++) {
2276 			int j;
2277 			if (fcp->portdb[i].port_wwn == 0)
2278 				continue;
2279 			if (fcp->portdb[i].port_wwn !=
2280 			    fcp->tport[loopid].port_wwn)
2281 				continue;
2282 			/*
2283 			 * We found this WWN elsewhere- it's changed
2284 			 * loopids then. We don't change it's actual
2285 			 * position in our cached port database- we
2286 			 * just change the actual loop ID we'd use.
2287 			 */
2288 			if (fcp->portdb[i].loopid != loopid) {
2289 				isp_prt(isp, ISP_LOGINFO, portshift, i,
2290 				    fcp->portdb[i].loopid,
2291 				    fcp->portdb[i].portid, loopid,
2292 				    fcp->tport[loopid].portid);
2293 			}
2294 			fcp->portdb[i].portid = fcp->tport[loopid].portid;
2295 			fcp->portdb[i].loopid = loopid;
2296 			fcp->portdb[i].valid = 1;
2297 			fcp->portdb[i].roles = fcp->tport[loopid].roles;
2298 
2299 			/*
2300 			 * Now make sure this Port WWN doesn't exist elsewhere
2301 			 * in the port database.
2302 			 */
2303 			for (j = i+1; j < hival; j++) {
2304 				if (fcp->portdb[i].port_wwn !=
2305 				    fcp->portdb[j].port_wwn) {
2306 					continue;
2307 				}
2308 				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
2309 				/*
2310 				 * Invalidate the 'old' *and* 'new' ones.
2311 				 * This is really harsh and not quite right,
2312 				 * but if this happens, we really don't know
2313 				 * who is what at this point.
2314 				 */
2315 				fcp->portdb[i].valid = 0;
2316 				fcp->portdb[j].valid = 0;
2317 			}
2318 			break;
2319 		}
2320 
2321 		/*
2322 		 * If we didn't traverse the entire port database,
2323 		 * then we found (and remapped) an existing entry.
2324 		 * No need to notify anyone- go for the next one.
2325 		 */
2326 		if (i < hival) {
2327 			isp_prt(isp, ISP_LOGINFO, retained,
2328 			    fcp->portdb[i].loopid, i, fcp->portdb[i].portid);
2329 			continue;
2330 		}
2331 
2332 		/*
2333 		 * We've not found this Port WWN anywhere. It's a new entry.
2334 		 * See if we can leave it where it is (with target == loopid).
2335 		 */
2336 		if (fcp->portdb[loopid].port_wwn != 0) {
2337 			for (lim = 0; lim < hival; lim++) {
2338 				if (fcp->portdb[lim].port_wwn == 0)
2339 					break;
2340 			}
2341 			/* "Cannot Happen" */
2342 			if (lim == hival) {
2343 				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
2344 				continue;
2345 			}
2346 			i = lim;
2347 		} else {
2348 			i = loopid;
2349 		}
2350 
2351 		/*
2352 		 * NB:	The actual loopid we use here is loopid- we may
2353 		 *	in fact be at a completely different index (target).
2354 		 */
2355 		fcp->portdb[i].loopid = loopid;
2356 		fcp->portdb[i].port_wwn = fcp->tport[loopid].port_wwn;
2357 		fcp->portdb[i].node_wwn = fcp->tport[loopid].node_wwn;
2358 		fcp->portdb[i].roles = fcp->tport[loopid].roles;
2359 		fcp->portdb[i].portid = fcp->tport[loopid].portid;
2360 		fcp->portdb[i].valid = 1;
2361 
2362 		/*
2363 		 * Tell the outside world we've arrived.
2364 		 */
2365 		(void) isp_async(isp, ISPASYNC_PROMENADE, &i);
2366 	}
2367 
2368 	/*
2369 	 * Now find all previously used targets that are now invalid and
2370 	 * notify the outer layers that they're gone.
2371 	 */
2372 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[hival]; lp++) {
2373 		if (lp->valid || lp->port_wwn == 0) {
2374 			continue;
2375 		}
2376 
2377 		/*
2378 		 * Tell the outside world we've gone
2379 		 * away and erase our pdb entry.
2380 		 *
2381 		 */
2382 		loopid = lp - fcp->portdb;
2383 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2384 		MEMZERO((void *) lp, sizeof (*lp));
2385 	}
2386 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
2387 	return (0);
2388 }
2389 
2390 
2391 static int
2392 isp_fabric_mbox_cmd(struct ispsoftc *isp, mbreg_t *mbp)
2393 {
2394 	isp_mboxcmd(isp, mbp, MBLOGNONE);
2395 	if (mbp->param[0] != MBOX_COMMAND_COMPLETE) {
2396 		if (FCPARAM(isp)->isp_loopstate == LOOP_SCANNING_FABRIC) {
2397 			FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
2398 		}
2399 		if (mbp->param[0] == MBOX_COMMAND_ERROR) {
2400 			char tbuf[16];
2401 			char *m;
2402 			switch (mbp->param[1]) {
2403 			case 1:
2404 				m = "No Loop";
2405 				break;
2406 			case 2:
2407 				m = "Failed to allocate IOCB buffer";
2408 				break;
2409 			case 3:
2410 				m = "Failed to allocate XCB buffer";
2411 				break;
2412 			case 4:
2413 				m = "timeout or transmit failed";
2414 				break;
2415 			case 5:
2416 				m = "no fabric loop";
2417 				break;
2418 			case 6:
2419 				m = "remote device not a target";
2420 				break;
2421 			default:
2422 				SNPRINTF(tbuf, sizeof tbuf, "%x",
2423 				    mbp->param[1]);
2424 				m = tbuf;
2425 				break;
2426 			}
2427 			isp_prt(isp, ISP_LOGERR, "SNS Failed- %s", m);
2428 		}
2429 		return (-1);
2430 	}
2431 
2432 	if (FCPARAM(isp)->isp_fwstate != FW_READY ||
2433 	    FCPARAM(isp)->isp_loopstate < LOOP_SCANNING_FABRIC) {
2434 		return (-1);
2435 	}
2436 	return(0);
2437 }
2438 
2439 #ifdef	ISP_USE_GA_NXT
2440 static int
2441 isp_scan_fabric(struct ispsoftc *isp, int ftype)
2442 {
2443 	fcparam *fcp = isp->isp_param;
2444 	u_int32_t portid, first_portid, last_portid;
2445 	int hicap, last_port_same;
2446 
2447 	if (fcp->isp_onfabric == 0) {
2448 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2449 		return (0);
2450 	}
2451 
2452 	FC_SCRATCH_ACQUIRE(isp);
2453 
2454 	/*
2455 	 * Since Port IDs are 24 bits, we can check against having seen
2456 	 * anything yet with this value.
2457 	 */
2458 	last_port_same = 0;
2459 	last_portid = 0xffffffff;	/* not a port */
2460 	first_portid = portid = fcp->isp_portid;
2461 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2462 
2463 	for (hicap = 0; hicap < GA_NXT_MAX; hicap++) {
2464 		mbreg_t mbs;
2465 		sns_screq_t *rq;
2466 		sns_ga_nxt_rsp_t *rs0, *rs1;
2467 		struct lportdb lcl;
2468 		u_int8_t sc[SNS_GA_NXT_RESP_SIZE];
2469 
2470 		rq = (sns_screq_t *)sc;
2471 		MEMZERO((void *) rq, SNS_GA_NXT_REQ_SIZE);
2472 		rq->snscb_rblen = SNS_GA_NXT_RESP_SIZE >> 1;
2473 		rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+0x100);
2474 		rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+0x100);
2475 		rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+0x100);
2476 		rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+0x100);
2477 		rq->snscb_sblen = 6;
2478 		rq->snscb_data[0] = SNS_GA_NXT;
2479 		rq->snscb_data[4] = portid & 0xffff;
2480 		rq->snscb_data[5] = (portid >> 16) & 0xff;
2481 		isp_put_sns_request(isp, rq, (sns_screq_t *) fcp->isp_scratch);
2482 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GA_NXT_REQ_SIZE);
2483 		mbs.param[0] = MBOX_SEND_SNS;
2484 		mbs.param[1] = SNS_GA_NXT_REQ_SIZE >> 1;
2485 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2486 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2487 		/*
2488 		 * Leave 4 and 5 alone
2489 		 */
2490 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2491 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2492 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2493 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2494 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2495 			}
2496 			FC_SCRATCH_RELEASE(isp);
2497 			return (-1);
2498 		}
2499 		MEMORYBARRIER(isp, SYNC_SFORCPU, 0x100, SNS_GA_NXT_RESP_SIZE);
2500 		rs1 = (sns_ga_nxt_rsp_t *) sc;
2501 		rs0 = (sns_ga_nxt_rsp_t *) ((u_int8_t *)fcp->isp_scratch+0x100);
2502 		isp_get_ga_nxt_response(isp, rs0, rs1);
2503 		if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2504 			int level;
2505 			if (rs1->snscb_cthdr.ct_reason == 9 &&
2506 			    rs1->snscb_cthdr.ct_explanation == 7)
2507 				level = ISP_LOGDEBUG0;
2508 			else
2509 				level = ISP_LOGWARN;
2510 			isp_prt(isp, level, swrej, "GA_NXT",
2511 			    rs1->snscb_cthdr.ct_reason,
2512 			    rs1->snscb_cthdr.ct_explanation, portid);
2513 			FC_SCRATCH_RELEASE(isp);
2514 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2515 			return (0);
2516 		}
2517 		portid =
2518 		    (((u_int32_t) rs1->snscb_port_id[0]) << 16) |
2519 		    (((u_int32_t) rs1->snscb_port_id[1]) << 8) |
2520 		    (((u_int32_t) rs1->snscb_port_id[2]));
2521 
2522 		/*
2523 		 * XXX: We should check to make sure that this entry
2524 		 * XXX: supports the type(s) we are interested in.
2525 		 */
2526 		/*
2527 		 * Okay, we now have information about a fabric object.
2528 		 * If it is the type we're interested in, tell the outer layers
2529 		 * about it. The outer layer needs to  know: Port ID, WWNN,
2530 		 * WWPN, FC4 type, and port type.
2531 		 *
2532 		 * The lportdb structure is adequate for this.
2533 		 */
2534 		MEMZERO(&lcl, sizeof (lcl));
2535 		lcl.port_type = rs1->snscb_port_type;
2536 		lcl.fc4_type = ftype;
2537 		lcl.portid = portid;
2538 		lcl.node_wwn =
2539 		    (((u_int64_t)rs1->snscb_nodename[0]) << 56) |
2540 		    (((u_int64_t)rs1->snscb_nodename[1]) << 48) |
2541 		    (((u_int64_t)rs1->snscb_nodename[2]) << 40) |
2542 		    (((u_int64_t)rs1->snscb_nodename[3]) << 32) |
2543 		    (((u_int64_t)rs1->snscb_nodename[4]) << 24) |
2544 		    (((u_int64_t)rs1->snscb_nodename[5]) << 16) |
2545 		    (((u_int64_t)rs1->snscb_nodename[6]) <<  8) |
2546 		    (((u_int64_t)rs1->snscb_nodename[7]));
2547 		lcl.port_wwn =
2548 		    (((u_int64_t)rs1->snscb_portname[0]) << 56) |
2549 		    (((u_int64_t)rs1->snscb_portname[1]) << 48) |
2550 		    (((u_int64_t)rs1->snscb_portname[2]) << 40) |
2551 		    (((u_int64_t)rs1->snscb_portname[3]) << 32) |
2552 		    (((u_int64_t)rs1->snscb_portname[4]) << 24) |
2553 		    (((u_int64_t)rs1->snscb_portname[5]) << 16) |
2554 		    (((u_int64_t)rs1->snscb_portname[6]) <<  8) |
2555 		    (((u_int64_t)rs1->snscb_portname[7]));
2556 
2557 		/*
2558 		 * Does this fabric object support the type we want?
2559 		 * If not, skip it.
2560 		 */
2561 		if (rs1->snscb_fc4_types[ftype >> 5] & (1 << (ftype & 0x1f))) {
2562 			if (first_portid == portid) {
2563 				lcl.last_fabric_dev = 1;
2564 			} else {
2565 				lcl.last_fabric_dev = 0;
2566 			}
2567 			(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2568 		} else {
2569 			isp_prt(isp, ISP_LOGDEBUG0,
2570 			    "PortID 0x%x doesn't support FC4 type 0x%x",
2571 			    portid, ftype);
2572 		}
2573 		if (first_portid == portid) {
2574 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2575 			FC_SCRATCH_RELEASE(isp);
2576 			return (0);
2577 		}
2578 		if (portid == last_portid) {
2579 			if (last_port_same++ > 20) {
2580 				isp_prt(isp, ISP_LOGWARN,
2581 				    "tangled fabric database detected");
2582 				break;
2583 			}
2584 		} else {
2585 			last_port_same = 0 ;
2586 			last_portid = portid;
2587 		}
2588 	}
2589 	FC_SCRATCH_RELEASE(isp);
2590 	if (hicap >= GA_NXT_MAX) {
2591 		isp_prt(isp, ISP_LOGWARN, "fabric too big (> %d)", GA_NXT_MAX);
2592 	}
2593 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2594 	return (0);
2595 }
2596 #else
2597 #define	GIDLEN	((ISP2100_SCRLEN >> 1) + 16)
2598 #define	NGENT	((GIDLEN - 16) >> 2)
2599 
2600 #define	IGPOFF	(ISP2100_SCRLEN - GIDLEN)
2601 #define	GXOFF	(256)
2602 
2603 static int
2604 isp_scan_fabric(struct ispsoftc *isp, int ftype)
2605 {
2606 	fcparam *fcp = FCPARAM(isp);
2607 	mbreg_t mbs;
2608 	int i;
2609 	sns_gid_ft_req_t *rq;
2610 	sns_gid_ft_rsp_t *rs0, *rs1;
2611 
2612 	if (fcp->isp_onfabric == 0) {
2613 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2614 		return (0);
2615 	}
2616 
2617 	FC_SCRATCH_ACQUIRE(isp);
2618 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2619 
2620 	rq = (sns_gid_ft_req_t *)fcp->tport;
2621 	MEMZERO((void *) rq, SNS_GID_FT_REQ_SIZE);
2622 	rq->snscb_rblen = GIDLEN >> 1;
2623 	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+IGPOFF);
2624 	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+IGPOFF);
2625 	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+IGPOFF);
2626 	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+IGPOFF);
2627 	rq->snscb_sblen = 6;
2628 	rq->snscb_cmd = SNS_GID_FT;
2629 	rq->snscb_mword_div_2 = NGENT;
2630 	rq->snscb_fc4_type = ftype;
2631 	isp_put_gid_ft_request(isp, rq, (sns_gid_ft_req_t *) fcp->isp_scratch);
2632 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE);
2633 	mbs.param[0] = MBOX_SEND_SNS;
2634 	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
2635 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2636 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2637 
2638 	/*
2639 	 * Leave 4 and 5 alone
2640 	 */
2641 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2642 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2643 	if (isp_fabric_mbox_cmd(isp, &mbs)) {
2644 		if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2645 			fcp->isp_loopstate = LOOP_PDB_RCVD;
2646 		}
2647 		FC_SCRATCH_RELEASE(isp);
2648 		return (-1);
2649 	}
2650 	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2651 		FC_SCRATCH_RELEASE(isp);
2652 		return (-1);
2653 	}
2654 	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN);
2655 	rs1 = (sns_gid_ft_rsp_t *) fcp->tport;
2656 	rs0 = (sns_gid_ft_rsp_t *) ((u_int8_t *)fcp->isp_scratch+IGPOFF);
2657 	isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
2658 	if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2659 		int level;
2660 		if (rs1->snscb_cthdr.ct_reason == 9 &&
2661 		    rs1->snscb_cthdr.ct_explanation == 7)
2662 			level = ISP_LOGDEBUG0;
2663 		else
2664 			level = ISP_LOGWARN;
2665 		isp_prt(isp, level, swrej, "GID_FT",
2666 		    rs1->snscb_cthdr.ct_reason,
2667 		    rs1->snscb_cthdr.ct_explanation, 0);
2668 		FC_SCRATCH_RELEASE(isp);
2669 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2670 		return (0);
2671 	}
2672 
2673 	/*
2674 	 * Okay, we now have a list of Port IDs for this class of device.
2675 	 * Go through the list and for each one get the WWPN/WWNN for it
2676 	 * and tell the outer layers about it. The outer layer needs to
2677 	 * know: Port ID, WWNN, WWPN, FC4 type, and (possibly) port type.
2678 	 *
2679 	 * The lportdb structure is adequate for this.
2680 	 */
2681 	i = -1;
2682 	do {
2683 		sns_gxn_id_req_t grqbuf, *gq = &grqbuf;
2684 		sns_gxn_id_rsp_t *gs0, grsbuf, *gs1 = &grsbuf;
2685 		struct lportdb lcl;
2686 #if	0
2687 		sns_gff_id_rsp_t *fs0, ffsbuf, *fs1 = &ffsbuf;
2688 #endif
2689 
2690 		i++;
2691 		MEMZERO(&lcl, sizeof (lcl));
2692 		lcl.fc4_type = ftype;
2693 		lcl.portid =
2694 		    (((u_int32_t) rs1->snscb_ports[i].portid[0]) << 16) |
2695 		    (((u_int32_t) rs1->snscb_ports[i].portid[1]) << 8) |
2696 		    (((u_int32_t) rs1->snscb_ports[i].portid[2]));
2697 
2698 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2699 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2700 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2701 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2702 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2703 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2704 		gq->snscb_sblen = 6;
2705 		gq->snscb_cmd = SNS_GPN_ID;
2706 		gq->snscb_portid = lcl.portid;
2707 		isp_put_gxn_id_request(isp, gq,
2708 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2709 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2710 		mbs.param[0] = MBOX_SEND_SNS;
2711 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2712 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2713 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2714 		/*
2715 		 * Leave 4 and 5 alone
2716 		 */
2717 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2718 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2719 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2720 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2721 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2722 			}
2723 			FC_SCRATCH_RELEASE(isp);
2724 			return (-1);
2725 		}
2726 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2727 			FC_SCRATCH_RELEASE(isp);
2728 			return (-1);
2729 		}
2730 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2731 		gs0 = (sns_gxn_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
2732 		isp_get_gxn_id_response(isp, gs0, gs1);
2733 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2734 			isp_prt(isp, ISP_LOGWARN, swrej, "GPN_ID",
2735 			    gs1->snscb_cthdr.ct_reason,
2736 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2737 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2738 				FC_SCRATCH_RELEASE(isp);
2739 				return (-1);
2740 			}
2741 			continue;
2742 		}
2743 		lcl.port_wwn =
2744 		    (((u_int64_t)gs1->snscb_wwn[0]) << 56) |
2745 		    (((u_int64_t)gs1->snscb_wwn[1]) << 48) |
2746 		    (((u_int64_t)gs1->snscb_wwn[2]) << 40) |
2747 		    (((u_int64_t)gs1->snscb_wwn[3]) << 32) |
2748 		    (((u_int64_t)gs1->snscb_wwn[4]) << 24) |
2749 		    (((u_int64_t)gs1->snscb_wwn[5]) << 16) |
2750 		    (((u_int64_t)gs1->snscb_wwn[6]) <<  8) |
2751 		    (((u_int64_t)gs1->snscb_wwn[7]));
2752 
2753 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2754 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2755 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2756 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2757 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2758 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2759 		gq->snscb_sblen = 6;
2760 		gq->snscb_cmd = SNS_GNN_ID;
2761 		gq->snscb_portid = lcl.portid;
2762 		isp_put_gxn_id_request(isp, gq,
2763 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2764 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2765 		mbs.param[0] = MBOX_SEND_SNS;
2766 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2767 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2768 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2769 		/*
2770 		 * Leave 4 and 5 alone
2771 		 */
2772 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2773 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2774 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2775 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2776 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2777 			}
2778 			FC_SCRATCH_RELEASE(isp);
2779 			return (-1);
2780 		}
2781 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2782 			FC_SCRATCH_RELEASE(isp);
2783 			return (-1);
2784 		}
2785 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2786 		gs0 = (sns_gxn_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
2787 		isp_get_gxn_id_response(isp, gs0, gs1);
2788 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2789 			isp_prt(isp, ISP_LOGWARN, swrej, "GNN_ID",
2790 			    gs1->snscb_cthdr.ct_reason,
2791 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2792 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2793 				FC_SCRATCH_RELEASE(isp);
2794 				return (-1);
2795 			}
2796 			continue;
2797 		}
2798 		lcl.node_wwn =
2799 		    (((u_int64_t)gs1->snscb_wwn[0]) << 56) |
2800 		    (((u_int64_t)gs1->snscb_wwn[1]) << 48) |
2801 		    (((u_int64_t)gs1->snscb_wwn[2]) << 40) |
2802 		    (((u_int64_t)gs1->snscb_wwn[3]) << 32) |
2803 		    (((u_int64_t)gs1->snscb_wwn[4]) << 24) |
2804 		    (((u_int64_t)gs1->snscb_wwn[5]) << 16) |
2805 		    (((u_int64_t)gs1->snscb_wwn[6]) <<  8) |
2806 		    (((u_int64_t)gs1->snscb_wwn[7]));
2807 
2808 		/*
2809 		 * The QLogic f/w is bouncing this with a parameter error.
2810 		 */
2811 #if	0
2812 		/*
2813 		 * Try and get FC4 Features (FC-GS-3 only).
2814 		 * We can use the sns_gxn_id_req_t for this request.
2815 		 */
2816 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2817 		gq->snscb_rblen = SNS_GFF_ID_RESP_SIZE >> 1;
2818 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2819 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2820 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2821 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2822 		gq->snscb_sblen = 6;
2823 		gq->snscb_cmd = SNS_GFF_ID;
2824 		gq->snscb_portid = lcl.portid;
2825 		isp_put_gxn_id_request(isp, gq,
2826 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2827 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2828 		mbs.param[0] = MBOX_SEND_SNS;
2829 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2830 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2831 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2832 		/*
2833 		 * Leave 4 and 5 alone
2834 		 */
2835 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2836 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2837 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2838 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2839 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2840 			}
2841 			FC_SCRATCH_RELEASE(isp);
2842 			return (-1);
2843 		}
2844 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2845 			FC_SCRATCH_RELEASE(isp);
2846 			return (-1);
2847 		}
2848 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GFF_ID_RESP_SIZE);
2849 		fs0 = (sns_gff_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
2850 		isp_get_gff_id_response(isp, fs0, fs1);
2851 		if (fs1->snscb_cthdr.ct_response != FS_ACC) {
2852 			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGWARN,
2853 			    swrej, "GFF_ID",
2854 			    fs1->snscb_cthdr.ct_reason,
2855 			    fs1->snscb_cthdr.ct_explanation, lcl.portid);
2856 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2857 				FC_SCRATCH_RELEASE(isp);
2858 				return (-1);
2859 			}
2860 		} else {
2861 			int index = (ftype >> 3);
2862 			int bshft = (ftype & 0x7) * 4;
2863 			int fc4_fval =
2864 			    (fs1->snscb_fc4_features[index] >> bshft) & 0xf;
2865 			if (fc4_fval & 0x1) {
2866 				lcl.roles |=
2867 				    (SVC3_INI_ROLE >> SVC3_ROLE_SHIFT);
2868 			}
2869 			if (fc4_fval & 0x2) {
2870 				lcl.roles |=
2871 				    (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
2872 			}
2873 		}
2874 #endif
2875 
2876 		/*
2877 		 * If we really want to know what kind of port type this is,
2878 		 * we have to run another CT command. Otherwise, we'll leave
2879 		 * it as undefined.
2880 		 *
2881 		lcl.port_type = 0;
2882 		 */
2883 		if (rs1->snscb_ports[i].control & 0x80) {
2884 			lcl.last_fabric_dev = 1;
2885 		} else {
2886 			lcl.last_fabric_dev = 0;
2887 		}
2888 		(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2889 
2890 	} while ((rs1->snscb_ports[i].control & 0x80) == 0 && i < NGENT-1);
2891 
2892 	/*
2893 	 * If we're not at the last entry, our list isn't big enough.
2894 	 */
2895 	if ((rs1->snscb_ports[i].control & 0x80) == 0) {
2896 		isp_prt(isp, ISP_LOGWARN, "fabric too big for scratch area");
2897 	}
2898 
2899 	FC_SCRATCH_RELEASE(isp);
2900 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2901 	return (0);
2902 }
2903 #endif
2904 
2905 static void
2906 isp_register_fc4_type(struct ispsoftc *isp)
2907 {
2908 	fcparam *fcp = isp->isp_param;
2909 	u_int8_t local[SNS_RFT_ID_REQ_SIZE];
2910 	sns_screq_t *reqp = (sns_screq_t *) local;
2911 	mbreg_t mbs;
2912 
2913 	MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
2914 	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
2915 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
2916 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
2917 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
2918 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
2919 	reqp->snscb_sblen = 22;
2920 	reqp->snscb_data[0] = SNS_RFT_ID;
2921 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
2922 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
2923 	reqp->snscb_data[6] = (1 << FC4_SCSI);
2924 #if	0
2925 	reqp->snscb_data[6] |= (1 << FC4_IP);	/* ISO/IEC 8802-2 LLC/SNAP */
2926 #endif
2927 	FC_SCRATCH_ACQUIRE(isp);
2928 	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
2929 	mbs.param[0] = MBOX_SEND_SNS;
2930 	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
2931 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2932 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2933 	/*
2934 	 * Leave 4 and 5 alone
2935 	 */
2936 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2937 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2938 	isp_mboxcmd(isp, &mbs, MBLOGALL);
2939 	FC_SCRATCH_RELEASE(isp);
2940 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
2941 		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
2942 	}
2943 }
2944 
2945 /*
2946  * Start a command. Locking is assumed done in the caller.
2947  */
2948 
2949 int
2950 isp_start(XS_T *xs)
2951 {
2952 	struct ispsoftc *isp;
2953 	u_int16_t nxti, optr, handle;
2954 	u_int8_t local[QENTRY_LEN];
2955 	ispreq_t *reqp, *qep;
2956 	int target, i;
2957 
2958 	XS_INITERR(xs);
2959 	isp = XS_ISP(xs);
2960 
2961 	/*
2962 	 * Check to make sure we're supporting initiator role.
2963 	 */
2964 	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
2965 		XS_SETERR(xs, HBA_SELTIMEOUT);
2966 		return (CMD_COMPLETE);
2967 	}
2968 
2969 	/*
2970 	 * Now make sure we're running.
2971 	 */
2972 
2973 	if (isp->isp_state != ISP_RUNSTATE) {
2974 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
2975 		XS_SETERR(xs, HBA_BOTCH);
2976 		return (CMD_COMPLETE);
2977 	}
2978 
2979 	/*
2980 	 * Check command CDB length, etc.. We really are limited to 16 bytes
2981 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
2982 	 * but probably only if we're running fairly new firmware (we'll
2983 	 * let the old f/w choke on an extended command queue entry).
2984 	 */
2985 
2986 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
2987 		isp_prt(isp, ISP_LOGERR,
2988 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
2989 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
2990 		XS_SETERR(xs, HBA_BOTCH);
2991 		return (CMD_COMPLETE);
2992 	}
2993 
2994 	/*
2995 	 * Check to see whether we have good firmware state still or
2996 	 * need to refresh our port database for this target.
2997 	 */
2998 	target = XS_TGT(xs);
2999 	if (IS_FC(isp)) {
3000 		fcparam *fcp = isp->isp_param;
3001 		struct lportdb *lp;
3002 #ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
3003 		if (fcp->isp_fwstate != FW_READY ||
3004 		    fcp->isp_loopstate != LOOP_READY) {
3005 			return (CMD_RQLATER);
3006 		}
3007 
3008 		/*
3009 		 * If we're not on a Fabric, we can't have a target
3010 		 * above FL_PORT_ID-1.
3011 		 *
3012 		 * If we're on a fabric and *not* connected as an F-port,
3013 		 * we can't have a target less than FC_SNS_ID+1. This
3014 		 * keeps us from having to sort out the difference between
3015 		 * local public loop devices and those which we might get
3016 		 * from a switch's database.
3017 		 */
3018 		if (fcp->isp_onfabric == 0) {
3019 			if (target >= FL_PORT_ID) {
3020 				XS_SETERR(xs, HBA_SELTIMEOUT);
3021 				return (CMD_COMPLETE);
3022 			}
3023 		} else {
3024 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3025 				XS_SETERR(xs, HBA_SELTIMEOUT);
3026 				return (CMD_COMPLETE);
3027 			}
3028 			/*
3029 			 * We used to exclude having local loop ports
3030 			 * at the same time that we have fabric ports.
3031 			 * That is, we used to exclude having ports
3032 			 * at < FL_PORT_ID if we're FL-port.
3033 			 *
3034 			 * That's wrong. The only thing that could be
3035 			 * dicey is if the switch you're connected to
3036 			 * has these local loop ports appear on the
3037 			 * fabric and we somehow attach them twice.
3038 			 */
3039 		}
3040 #else
3041 		/*
3042 		 * Check for f/w being in ready state. If the f/w
3043 		 * isn't in ready state, then we don't know our
3044 		 * loop ID and the f/w hasn't completed logging
3045 		 * into all targets on the loop. If this is the
3046 		 * case, then bounce the command. We pretend this is
3047 		 * a SELECTION TIMEOUT error if we've never gone to
3048 		 * FW_READY state at all- in this case we may not
3049 		 * be hooked to a loop at all and we shouldn't hang
3050 		 * the machine for this. Otherwise, defer this command
3051 		 * until later.
3052 		 */
3053 		if (fcp->isp_fwstate != FW_READY) {
3054 			/*
3055 			 * Give ourselves at most a 250ms delay.
3056 			 */
3057 			if (isp_fclink_test(isp, 250000)) {
3058 				XS_SETERR(xs, HBA_SELTIMEOUT);
3059 				if (fcp->loop_seen_once) {
3060 					return (CMD_RQLATER);
3061 				} else {
3062 					return (CMD_COMPLETE);
3063 				}
3064 			}
3065 		}
3066 
3067 		/*
3068 		 * If we're not on a Fabric, we can't have a target
3069 		 * above FL_PORT_ID-1.
3070 		 *
3071 		 * If we're on a fabric and *not* connected as an F-port,
3072 		 * we can't have a target less than FC_SNS_ID+1. This
3073 		 * keeps us from having to sort out the difference between
3074 		 * local public loop devices and those which we might get
3075 		 * from a switch's database.
3076 		 */
3077 		if (fcp->isp_onfabric == 0) {
3078 			if (target >= FL_PORT_ID) {
3079 				XS_SETERR(xs, HBA_SELTIMEOUT);
3080 				return (CMD_COMPLETE);
3081 			}
3082 		} else {
3083 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3084 				XS_SETERR(xs, HBA_SELTIMEOUT);
3085 				return (CMD_COMPLETE);
3086 			}
3087 			if (fcp->isp_topo != TOPO_F_PORT &&
3088 			    target < FL_PORT_ID) {
3089 				XS_SETERR(xs, HBA_SELTIMEOUT);
3090 				return (CMD_COMPLETE);
3091 			}
3092 		}
3093 
3094 		/*
3095 		 * If our loop state is such that we haven't yet received
3096 		 * a "Port Database Changed" notification (after a LIP or
3097 		 * a Loop Reset or firmware initialization), then defer
3098 		 * sending commands for a little while, but only if we've
3099 		 * seen a valid loop at one point (otherwise we can get
3100 		 * stuck at initialization time).
3101 		 */
3102 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
3103 			XS_SETERR(xs, HBA_SELTIMEOUT);
3104 			if (fcp->loop_seen_once) {
3105 				return (CMD_RQLATER);
3106 			} else {
3107 				return (CMD_COMPLETE);
3108 			}
3109 		}
3110 
3111 		/*
3112 		 * If we're in the middle of loop or fabric scanning
3113 		 * or merging the port databases, retry this command later.
3114 		 */
3115 		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
3116 		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
3117 		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
3118 			return (CMD_RQLATER);
3119 		}
3120 
3121 		/*
3122 		 * If our loop state is now such that we've just now
3123 		 * received a Port Database Change notification, then
3124 		 * we have to go off and (re)scan the fabric. We back
3125 		 * out and try again later if this doesn't work.
3126 		 */
3127 		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
3128 			if (isp_scan_fabric(isp, FC4_SCSI)) {
3129 				return (CMD_RQLATER);
3130 			}
3131 			if (fcp->isp_fwstate != FW_READY ||
3132 			    fcp->isp_loopstate < LOOP_FSCAN_DONE) {
3133 				return (CMD_RQLATER);
3134 			}
3135 		}
3136 
3137 		/*
3138 		 * If our loop state is now such that we've just now
3139 		 * received a Port Database Change notification, then
3140 		 * we have to go off and (re)synchronize our port
3141 		 * database.
3142 		 */
3143 		if (fcp->isp_loopstate < LOOP_READY) {
3144 			if (isp_pdb_sync(isp)) {
3145 				return (CMD_RQLATER);
3146 			}
3147 			if (fcp->isp_fwstate != FW_READY ||
3148 			    fcp->isp_loopstate != LOOP_READY) {
3149 				return (CMD_RQLATER);
3150 			}
3151 		}
3152 
3153 		/*
3154 		 * XXX: Here's were we would cancel any loop_dead flag
3155 		 * XXX: also cancel in dead_loop timeout that's running
3156 		 */
3157 #endif
3158 
3159 		/*
3160 		 * Now check whether we should even think about pursuing this.
3161 		 */
3162 		lp = &fcp->portdb[target];
3163 		if (lp->valid == 0) {
3164 			XS_SETERR(xs, HBA_SELTIMEOUT);
3165 			return (CMD_COMPLETE);
3166 		}
3167 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
3168 			isp_prt(isp, ISP_LOGDEBUG2,
3169 			    "Target %d does not have target service", target);
3170 			XS_SETERR(xs, HBA_SELTIMEOUT);
3171 			return (CMD_COMPLETE);
3172 		}
3173 		/*
3174 		 * Now turn target into what the actual Loop ID is.
3175 		 */
3176 		target = lp->loopid;
3177 	}
3178 
3179 	/*
3180 	 * Next check to see if any HBA or Device
3181 	 * parameters need to be updated.
3182 	 */
3183 	if (isp->isp_update != 0) {
3184 		isp_update(isp);
3185 	}
3186 
3187 	if (isp_getrqentry(isp, &nxti, &optr, (void *)&qep)) {
3188 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
3189 		XS_SETERR(xs, HBA_BOTCH);
3190 		return (CMD_EAGAIN);
3191 	}
3192 
3193 	/*
3194 	 * Now see if we need to synchronize the ISP with respect to anything.
3195 	 * We do dual duty here (cough) for synchronizing for busses other
3196 	 * than which we got here to send a command to.
3197 	 */
3198 	reqp = (ispreq_t *) local;
3199 	if (isp->isp_sendmarker) {
3200 		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
3201 		/*
3202 		 * Check ports to send markers for...
3203 		 */
3204 		for (i = 0; i < n; i++) {
3205 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
3206 				continue;
3207 			}
3208 			MEMZERO((void *) reqp, QENTRY_LEN);
3209 			reqp->req_header.rqs_entry_count = 1;
3210 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
3211 			reqp->req_modifier = SYNC_ALL;
3212 			reqp->req_target = i << 7;	/* insert bus number */
3213 			isp_put_request(isp, reqp, qep);
3214 			ISP_ADD_REQUEST(isp, nxti);
3215 			isp->isp_sendmarker &= ~(1 << i);
3216 			if (isp_getrqentry(isp, &nxti, &optr, (void *) &qep)) {
3217 				isp_prt(isp, ISP_LOGDEBUG0,
3218 				    "Request Queue Overflow+");
3219 				XS_SETERR(xs, HBA_BOTCH);
3220 				return (CMD_EAGAIN);
3221 			}
3222 		}
3223 	}
3224 
3225 	MEMZERO((void *)reqp, QENTRY_LEN);
3226 	reqp->req_header.rqs_entry_count = 1;
3227 	if (IS_FC(isp)) {
3228 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
3229 	} else {
3230 		if (XS_CDBLEN(xs) > 12)
3231 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
3232 		else
3233 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
3234 	}
3235 	/* reqp->req_header.rqs_flags = 0; */
3236 	/* reqp->req_header.rqs_seqno = 0; */
3237 	if (IS_FC(isp)) {
3238 		/*
3239 		 * See comment in isp_intr
3240 		 */
3241 		/* XS_RESID(xs) = 0; */
3242 
3243 		/*
3244 		 * Fibre Channel always requires some kind of tag.
3245 		 * The Qlogic drivers seem be happy not to use a tag,
3246 		 * but this breaks for some devices (IBM drives).
3247 		 */
3248 		if (XS_TAG_P(xs)) {
3249 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
3250 		} else {
3251 			/*
3252 			 * If we don't know what tag to use, use HEAD OF QUEUE
3253 			 * for Request Sense or Simple.
3254 			 */
3255 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
3256 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
3257 			else
3258 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
3259 		}
3260 	} else {
3261 		sdparam *sdp = (sdparam *)isp->isp_param;
3262 		sdp += XS_CHANNEL(xs);
3263 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
3264 		    XS_TAG_P(xs)) {
3265 			reqp->req_flags = XS_TAG_TYPE(xs);
3266 		}
3267 	}
3268 	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
3269 	if (IS_SCSI(isp)) {
3270 		reqp->req_lun_trn = XS_LUN(xs);
3271 		reqp->req_cdblen = XS_CDBLEN(xs);
3272 	} else {
3273 		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)
3274 			((ispreqt2_t *)reqp)->req_scclun = XS_LUN(xs);
3275 		else
3276 			((ispreqt2_t *)reqp)->req_lun_trn = XS_LUN(xs);
3277 	}
3278 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
3279 
3280 	reqp->req_time = XS_TIME(xs) / 1000;
3281 	if (reqp->req_time == 0 && XS_TIME(xs)) {
3282 		reqp->req_time = 1;
3283 	}
3284 
3285 	if (isp_save_xs(isp, xs, &handle)) {
3286 		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
3287 		XS_SETERR(xs, HBA_BOTCH);
3288 		return (CMD_EAGAIN);
3289 	}
3290 	reqp->req_handle = handle;
3291 
3292 	/*
3293 	 * Set up DMA and/or do any bus swizzling of the request entry
3294 	 * so that the Qlogic F/W understands what is being asked of it.
3295 	 */
3296 	i = ISP_DMASETUP(isp, xs, reqp, &nxti, optr);
3297 	if (i != CMD_QUEUED) {
3298 		isp_destroy_handle(isp, handle);
3299 		/*
3300 		 * dmasetup sets actual error in packet, and
3301 		 * return what we were given to return.
3302 		 */
3303 		return (i);
3304 	}
3305 	XS_SETERR(xs, HBA_NOERROR);
3306 	isp_prt(isp, ISP_LOGDEBUG2,
3307 	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
3308 	    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), XS_CDBP(xs)[0],
3309 	    (long) XS_XFRLEN(xs));
3310 	ISP_ADD_REQUEST(isp, nxti);
3311 	isp->isp_nactive++;
3312 	return (CMD_QUEUED);
3313 }
3314 
3315 /*
3316  * isp control
3317  * Locks (ints blocked) assumed held.
3318  */
3319 
3320 int
3321 isp_control(struct ispsoftc *isp, ispctl_t ctl, void *arg)
3322 {
3323 	XS_T *xs;
3324 	mbreg_t mbs;
3325 	int bus, tgt;
3326 	u_int16_t handle;
3327 
3328 	switch (ctl) {
3329 	default:
3330 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
3331 		break;
3332 
3333 	case ISPCTL_RESET_BUS:
3334 		/*
3335 		 * Issue a bus reset.
3336 		 */
3337 		mbs.param[0] = MBOX_BUS_RESET;
3338 		mbs.param[2] = 0;
3339 		if (IS_SCSI(isp)) {
3340 			mbs.param[1] =
3341 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
3342 			if (mbs.param[1] < 2)
3343 				mbs.param[1] = 2;
3344 			bus = *((int *) arg);
3345 			if (IS_DUALBUS(isp))
3346 				mbs.param[2] = bus;
3347 		} else {
3348 			mbs.param[1] = 10;
3349 			bus = 0;
3350 		}
3351 		isp->isp_sendmarker |= (1 << bus);
3352 		isp_mboxcmd(isp, &mbs, MBLOGALL);
3353 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3354 			break;
3355 		}
3356 		isp_prt(isp, ISP_LOGINFO,
3357 		    "driver initiated bus reset of bus %d", bus);
3358 		return (0);
3359 
3360 	case ISPCTL_RESET_DEV:
3361 		tgt = (*((int *) arg)) & 0xffff;
3362 		bus = (*((int *) arg)) >> 16;
3363 		mbs.param[0] = MBOX_ABORT_TARGET;
3364 		mbs.param[1] = (tgt << 8) | (bus << 15);
3365 		mbs.param[2] = 3;	/* 'delay', in seconds */
3366 		isp_mboxcmd(isp, &mbs, MBLOGALL);
3367 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3368 			break;
3369 		}
3370 		isp_prt(isp, ISP_LOGINFO,
3371 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
3372 		isp->isp_sendmarker |= (1 << bus);
3373 		return (0);
3374 
3375 	case ISPCTL_ABORT_CMD:
3376 		xs = (XS_T *) arg;
3377 		tgt = XS_TGT(xs);
3378 		handle = isp_find_handle(isp, xs);
3379 		if (handle == 0) {
3380 			isp_prt(isp, ISP_LOGWARN,
3381 			    "cannot find handle for command to abort");
3382 			break;
3383 		}
3384 		bus = XS_CHANNEL(xs);
3385 		mbs.param[0] = MBOX_ABORT;
3386 		if (IS_FC(isp)) {
3387 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)  {
3388 				mbs.param[1] = tgt << 8;
3389 				mbs.param[4] = 0;
3390 				mbs.param[5] = 0;
3391 				mbs.param[6] = XS_LUN(xs);
3392 			} else {
3393 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
3394 			}
3395 		} else {
3396 			mbs.param[1] =
3397 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
3398 		}
3399 		mbs.param[3] = 0;
3400 		mbs.param[2] = handle;
3401 		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
3402 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3403 			return (0);
3404 		}
3405 		/*
3406 		 * XXX: Look for command in the REQUEST QUEUE. That is,
3407 		 * XXX: It hasen't been picked up by firmware yet.
3408 		 */
3409 		break;
3410 
3411 	case ISPCTL_UPDATE_PARAMS:
3412 
3413 		isp_update(isp);
3414 		return (0);
3415 
3416 	case ISPCTL_FCLINK_TEST:
3417 
3418 		if (IS_FC(isp)) {
3419 			int usdelay = (arg)? *((int *) arg) : 250000;
3420 			return (isp_fclink_test(isp, usdelay));
3421 		}
3422 		break;
3423 
3424 	case ISPCTL_SCAN_FABRIC:
3425 
3426 		if (IS_FC(isp)) {
3427 			int ftype = (arg)? *((int *) arg) : FC4_SCSI;
3428 			return (isp_scan_fabric(isp, ftype));
3429 		}
3430 		break;
3431 
3432 	case ISPCTL_SCAN_LOOP:
3433 
3434 		if (IS_FC(isp)) {
3435 			return (isp_scan_loop(isp));
3436 		}
3437 		break;
3438 
3439 	case ISPCTL_PDB_SYNC:
3440 
3441 		if (IS_FC(isp)) {
3442 			return (isp_pdb_sync(isp));
3443 		}
3444 		break;
3445 
3446 	case ISPCTL_SEND_LIP:
3447 
3448 		if (IS_FC(isp)) {
3449 			mbs.param[0] = MBOX_INIT_LIP;
3450 			isp_mboxcmd(isp, &mbs, MBLOGALL);
3451 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3452 				return (0);
3453 			}
3454 		}
3455 		break;
3456 
3457 	case ISPCTL_GET_POSMAP:
3458 
3459 		if (IS_FC(isp) && arg) {
3460 			return (isp_getmap(isp, arg));
3461 		}
3462 		break;
3463 
3464 	case ISPCTL_RUN_MBOXCMD:
3465 
3466 		isp_mboxcmd(isp, arg, MBLOGALL);
3467 		return(0);
3468 
3469 #ifdef	ISP_TARGET_MODE
3470 	case ISPCTL_TOGGLE_TMODE:
3471 	{
3472 
3473 		/*
3474 		 * We don't check/set against role here- that's the
3475 		 * responsibility for the outer layer to coordinate.
3476 		 */
3477 		if (IS_SCSI(isp)) {
3478 			int param = *(int *)arg;
3479 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
3480 			mbs.param[1] = param & 0xffff;
3481 			mbs.param[2] = param >> 16;
3482 			isp_mboxcmd(isp, &mbs, MBLOGALL);
3483 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3484 				break;
3485 			}
3486 		}
3487 		return (0);
3488 	}
3489 #endif
3490 	}
3491 	return (-1);
3492 }
3493 
3494 /*
3495  * Interrupt Service Routine(s).
3496  *
3497  * External (OS) framework has done the appropriate locking,
3498  * and the locking will be held throughout this function.
3499  */
3500 
3501 /*
3502  * Limit our stack depth by sticking with the max likely number
3503  * of completions on a request queue at any one time.
3504  */
3505 #ifndef	MAX_REQUESTQ_COMPLETIONS
3506 #define	MAX_REQUESTQ_COMPLETIONS	64
3507 #endif
3508 
3509 void
3510 isp_intr(struct ispsoftc *isp, u_int16_t isr, u_int16_t sema, u_int16_t mbox)
3511 {
3512 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
3513 	u_int16_t iptr, optr, junk;
3514 	int i, nlooked = 0, ndone = 0;
3515 
3516 again:
3517 	/*
3518 	 * Is this a mailbox related interrupt?
3519 	 * The mailbox semaphore will be nonzero if so.
3520 	 */
3521 	if (sema) {
3522 		if (mbox & 0x4000) {
3523 			isp->isp_intmboxc++;
3524 			if (isp->isp_mboxbsy) {
3525 				int i = 0, obits = isp->isp_obits;
3526 				isp->isp_mboxtmp[i++] = mbox;
3527 				for (i = 1; i < MAX_MAILBOX; i++) {
3528 					if ((obits & (1 << i)) == 0) {
3529 						continue;
3530 					}
3531 					isp->isp_mboxtmp[i] =
3532 					    ISP_READ(isp, MBOX_OFF(i));
3533 				}
3534 				if (isp->isp_mbxwrk0) {
3535 					if (isp_mbox_continue(isp) == 0) {
3536 						return;
3537 					}
3538 				}
3539 				MBOX_NOTIFY_COMPLETE(isp);
3540 			} else {
3541 				isp_prt(isp, ISP_LOGWARN,
3542 				    "Mbox Command Async (0x%x) with no waiters",
3543 				    mbox);
3544 			}
3545 		} else if (isp_parse_async(isp, mbox) < 0) {
3546 			return;
3547 		}
3548 		if ((IS_FC(isp) && mbox != ASYNC_RIO_RESP) ||
3549 		    isp->isp_state != ISP_RUNSTATE) {
3550 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3551 			ISP_WRITE(isp, BIU_SEMA, 0);
3552 			return;
3553 		}
3554 	}
3555 
3556 	/*
3557 	 * We can't be getting this now.
3558 	 */
3559 	if (isp->isp_state != ISP_RUNSTATE) {
3560 		isp_prt(isp, ISP_LOGWARN,
3561 		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
3562 		/*
3563 		 * Thank you very much!  *Burrrp*!
3564 		 */
3565 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
3566 		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
3567 
3568 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3569 		ISP_WRITE(isp, BIU_SEMA, 0);
3570 		return;
3571 	}
3572 
3573 	/*
3574 	 * Get the current Response Queue Out Pointer.
3575 	 *
3576 	 * If we're a 2300, we can ask what hardware what it thinks.
3577 	 */
3578 	if (IS_23XX(isp)) {
3579 		optr = ISP_READ(isp, isp->isp_respoutrp);
3580 		/*
3581 		 * Debug: to be taken out eventually
3582 		 */
3583 		if (isp->isp_residx != optr) {
3584 			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
3585 			    optr, isp->isp_residx);
3586 		}
3587 	} else {
3588 		optr = isp->isp_residx;
3589 	}
3590 
3591 	/*
3592 	 * You *must* read the Response Queue In Pointer
3593 	 * prior to clearing the RISC interrupt.
3594 	 *
3595 	 * Debounce the 2300 if revision less than 2.
3596 	 */
3597 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
3598 		i = 0;
3599 		do {
3600 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3601 			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3602 		} while (junk != iptr && ++i < 1000);
3603 
3604 		if (iptr != junk) {
3605 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3606 			isp_prt(isp, ISP_LOGWARN,
3607 			    "Response Queue Out Pointer Unstable (%x, %x)",
3608 			    iptr, junk);
3609 			return;
3610 		}
3611 	} else {
3612 		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3613 	}
3614 	isp->isp_resodx = iptr;
3615 
3616 
3617 	if (optr == iptr && sema == 0) {
3618 		/*
3619 		 * There are a lot of these- reasons unknown- mostly on
3620 		 * faster Alpha machines.
3621 		 *
3622 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
3623 		 * make sure the old interrupt went away (to avoid 'ringing'
3624 		 * effects), but that didn't stop this from occurring.
3625 		 */
3626 		if (IS_23XX(isp)) {
3627 			USEC_DELAY(100);
3628 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3629 			junk = ISP_READ(isp, BIU_R2HSTSLO);
3630 		} else {
3631 			junk = ISP_READ(isp, BIU_ISR);
3632 		}
3633 		if (optr == iptr) {
3634 			if (IS_23XX(isp)) {
3635 				;
3636 			} else {
3637 				sema = ISP_READ(isp, BIU_SEMA);
3638 				mbox = ISP_READ(isp, OUTMAILBOX0);
3639 				if ((sema & 0x3) && (mbox & 0x8000)) {
3640 					goto again;
3641 				}
3642 			}
3643 			isp->isp_intbogus++;
3644 			isp_prt(isp, ISP_LOGDEBUG1,
3645 			    "bogus intr- isr %x (%x) iptr %x optr %x",
3646 			    isr, junk, iptr, optr);
3647 		}
3648 	}
3649 	isp->isp_resodx = iptr;
3650 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3651 	ISP_WRITE(isp, BIU_SEMA, 0);
3652 
3653 	if (isp->isp_rspbsy) {
3654 		return;
3655 	}
3656 	isp->isp_rspbsy = 1;
3657 
3658 	while (optr != iptr) {
3659 		ispstatusreq_t local, *sp = &local;
3660 		isphdr_t *hp;
3661 		int type;
3662 		u_int16_t oop;
3663 		int buddaboom = 0;
3664 
3665 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
3666 		oop = optr;
3667 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
3668 		nlooked++;
3669 		/*
3670 		 * Synchronize our view of this response queue entry.
3671 		 */
3672 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
3673 
3674 		type = isp_get_response_type(isp, hp);
3675 
3676 		if (type == RQSTYPE_RESPONSE) {
3677 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3678 		} else if (type == RQSTYPE_RIO2) {
3679 			isp_rio2_t rio;
3680 			isp_get_rio2(isp, (isp_rio2_t *) hp, &rio);
3681 			for (i = 0; i < rio.req_header.rqs_seqno; i++) {
3682 				isp_fastpost_complete(isp, rio.req_handles[i]);
3683 			}
3684 			if (isp->isp_fpcchiwater < rio.req_header.rqs_seqno)
3685 				isp->isp_fpcchiwater = rio.req_header.rqs_seqno;
3686 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3687 			continue;
3688 		} else {
3689 			/*
3690 			 * Somebody reachable via isp_handle_other_response
3691 			 * may have updated the response queue pointers for
3692 			 * us, so we reload our goal index.
3693 			 */
3694 			if (isp_handle_other_response(isp, type, hp, &optr)) {
3695 				iptr = isp->isp_resodx;
3696 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3697 				continue;
3698 			}
3699 
3700 			/*
3701 			 * After this point, we'll just look at the header as
3702 			 * we don't know how to deal with the rest of the
3703 			 * response.
3704 			 */
3705 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3706 
3707 			/*
3708 			 * It really has to be a bounced request just copied
3709 			 * from the request queue to the response queue. If
3710 			 * not, something bad has happened.
3711 			 */
3712 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
3713 				isp_prt(isp, ISP_LOGERR, notresp,
3714 				    sp->req_header.rqs_entry_type, oop, optr,
3715 				    nlooked);
3716 				if (isp->isp_dblev & ISP_LOGDEBUG0) {
3717 					isp_print_bytes(isp, "Queue Entry",
3718 					    QENTRY_LEN, sp);
3719 				}
3720 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3721 				continue;
3722 			}
3723 			buddaboom = 1;
3724 		}
3725 
3726 		if (sp->req_header.rqs_flags & 0xf) {
3727 #define	_RQS_OFLAGS	\
3728 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
3729 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
3730 				isp_prt(isp, ISP_LOGWARN,
3731 				    "continuation segment");
3732 				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3733 				continue;
3734 			}
3735 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3736 				isp_prt(isp, ISP_LOGDEBUG1,
3737 				    "internal queues full");
3738 				/*
3739 				 * We'll synthesize a QUEUE FULL message below.
3740 				 */
3741 			}
3742 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
3743 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
3744 				buddaboom++;
3745 			}
3746 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
3747 				isp_prt(isp, ISP_LOGERR, "bad request packet");
3748 				buddaboom++;
3749 			}
3750 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
3751 				isp_prt(isp, ISP_LOGERR,
3752 				    "unknown flags (0x%x) in response",
3753 				    sp->req_header.rqs_flags);
3754 				buddaboom++;
3755 			}
3756 #undef	_RQS_OFLAGS
3757 		}
3758 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
3759 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3760 			isp_prt(isp, ISP_LOGERR,
3761 			    "bad request handle %d (type 0x%x, flags 0x%x)",
3762 			    sp->req_handle, sp->req_header.rqs_entry_type,
3763 			    sp->req_header.rqs_flags);
3764 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3765 			continue;
3766 		}
3767 		xs = isp_find_xs(isp, sp->req_handle);
3768 		if (xs == NULL) {
3769 			u_int8_t ts = sp->req_completion_status & 0xff;
3770 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3771 			/*
3772 			 * Only whine if this isn't the expected fallout of
3773 			 * aborting the command.
3774 			 */
3775 			if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
3776 				isp_prt(isp, ISP_LOGERR,
3777 				    "cannot find handle 0x%x (type 0x%x)",
3778 				    sp->req_handle,
3779 				    sp->req_header.rqs_entry_type);
3780 			} else if (ts != RQCS_ABORTED) {
3781 				isp_prt(isp, ISP_LOGERR,
3782 				    "cannot find handle 0x%x (status 0x%x)",
3783 				    sp->req_handle, ts);
3784 			}
3785 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3786 			continue;
3787 		}
3788 		isp_destroy_handle(isp, sp->req_handle);
3789 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
3790 			XS_SETERR(xs, HBA_BUSRESET);
3791 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3792 		}
3793 		if (buddaboom) {
3794 			XS_SETERR(xs, HBA_BOTCH);
3795 		}
3796 
3797 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
3798 			/*
3799 			 * Fibre Channel F/W doesn't say we got status
3800 			 * if there's Sense Data instead. I guess they
3801 			 * think it goes w/o saying.
3802 			 */
3803 			sp->req_state_flags |= RQSF_GOT_STATUS;
3804 		}
3805 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
3806 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
3807 		}
3808 
3809 		switch (sp->req_header.rqs_entry_type) {
3810 		case RQSTYPE_RESPONSE:
3811 			XS_SET_STATE_STAT(isp, xs, sp);
3812 			isp_parse_status(isp, sp, xs);
3813 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
3814 			    (*XS_STSP(xs) == SCSI_BUSY)) {
3815 				XS_SETERR(xs, HBA_TGTBSY);
3816 			}
3817 			if (IS_SCSI(isp)) {
3818 				XS_RESID(xs) = sp->req_resid;
3819 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3820 				    (*XS_STSP(xs) == SCSI_CHECK) &&
3821 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
3822 					XS_SAVE_SENSE(xs, sp);
3823 				}
3824 				/*
3825 				 * A new synchronous rate was negotiated for
3826 				 * this target. Mark state such that we'll go
3827 				 * look up that which has changed later.
3828 				 */
3829 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
3830 					int t = XS_TGT(xs);
3831 					sdparam *sdp = isp->isp_param;
3832 					sdp += XS_CHANNEL(xs);
3833 					sdp->isp_devparam[t].dev_refresh = 1;
3834 					isp->isp_update |=
3835 					    (1 << XS_CHANNEL(xs));
3836 				}
3837 			} else {
3838 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
3839 					XS_RESID(xs) = 0;
3840 				} else if (sp->req_scsi_status & RQCS_RESID) {
3841 					XS_RESID(xs) = sp->req_resid;
3842 				} else {
3843 					XS_RESID(xs) = 0;
3844 				}
3845 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3846 				    (*XS_STSP(xs) == SCSI_CHECK) &&
3847 				    (sp->req_scsi_status & RQCS_SV)) {
3848 					XS_SAVE_SENSE(xs, sp);
3849 					/* solely for the benefit of debug */
3850 					sp->req_state_flags |= RQSF_GOT_SENSE;
3851 				}
3852 			}
3853 			isp_prt(isp, ISP_LOGDEBUG2,
3854 			   "asked for %ld got resid %ld", (long) XS_XFRLEN(xs),
3855 			   (long) sp->req_resid);
3856 			break;
3857 		case RQSTYPE_REQUEST:
3858 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3859 				/*
3860 				 * Force Queue Full status.
3861 				 */
3862 				*XS_STSP(xs) = SCSI_QFULL;
3863 				XS_SETERR(xs, HBA_NOERROR);
3864 			} else if (XS_NOERR(xs)) {
3865 				/*
3866 				 * ????
3867 				 */
3868 				isp_prt(isp, ISP_LOGDEBUG0,
3869 				    "Request Queue Entry bounced back");
3870 				XS_SETERR(xs, HBA_BOTCH);
3871 			}
3872 			XS_RESID(xs) = XS_XFRLEN(xs);
3873 			break;
3874 		default:
3875 			isp_prt(isp, ISP_LOGWARN,
3876 			    "unhandled response queue type 0x%x",
3877 			    sp->req_header.rqs_entry_type);
3878 			if (XS_NOERR(xs)) {
3879 				XS_SETERR(xs, HBA_BOTCH);
3880 			}
3881 			break;
3882 		}
3883 
3884 		/*
3885 		 * Free any dma resources. As a side effect, this may
3886 		 * also do any cache flushing necessary for data coherence.			 */
3887 		if (XS_XFRLEN(xs)) {
3888 			ISP_DMAFREE(isp, xs, sp->req_handle);
3889 		}
3890 
3891 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
3892 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
3893 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
3894 			char skey;
3895 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
3896 				skey = XS_SNSKEY(xs) & 0xf;
3897 				if (skey < 10)
3898 					skey += '0';
3899 				else
3900 					skey += 'a' - 10;
3901 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
3902 				skey = '?';
3903 			} else {
3904 				skey = '.';
3905 			}
3906 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
3907 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
3908 			    *XS_STSP(xs), skey, XS_ERR(xs));
3909 		}
3910 
3911 		if (isp->isp_nactive > 0)
3912 		    isp->isp_nactive--;
3913 		complist[ndone++] = xs;	/* defer completion call until later */
3914 		MEMZERO(hp, QENTRY_LEN);	/* PERF */
3915 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
3916 			break;
3917 		}
3918 	}
3919 
3920 	/*
3921 	 * If we looked at any commands, then it's valid to find out
3922 	 * what the outpointer is. It also is a trigger to update the
3923 	 * ISP's notion of what we've seen so far.
3924 	 */
3925 	if (nlooked) {
3926 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3927 		/*
3928 		 * While we're at it, read the requst queue out pointer.
3929 		 */
3930 		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
3931 		if (isp->isp_rscchiwater < ndone)
3932 			isp->isp_rscchiwater = ndone;
3933 	}
3934 
3935 	isp->isp_residx = optr;
3936 	isp->isp_rspbsy = 0;
3937 	for (i = 0; i < ndone; i++) {
3938 		xs = complist[i];
3939 		if (xs) {
3940 			isp->isp_rsltccmplt++;
3941 			isp_done(xs);
3942 		}
3943 	}
3944 }
3945 
3946 /*
3947  * Support routines.
3948  */
3949 
3950 static int
3951 isp_parse_async(struct ispsoftc *isp, u_int16_t mbox)
3952 {
3953 	int rval = 0;
3954 	int bus;
3955 
3956 	if (IS_DUALBUS(isp)) {
3957 		bus = ISP_READ(isp, OUTMAILBOX6);
3958 	} else {
3959 		bus = 0;
3960 	}
3961 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
3962 
3963 	switch (mbox) {
3964 	case ASYNC_BUS_RESET:
3965 		isp->isp_sendmarker |= (1 << bus);
3966 #ifdef	ISP_TARGET_MODE
3967 		if (isp_target_async(isp, bus, mbox))
3968 			rval = -1;
3969 #endif
3970 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
3971 		break;
3972 	case ASYNC_SYSTEM_ERROR:
3973 #ifdef	ISP_FW_CRASH_DUMP
3974 		/*
3975 		 * If we have crash dumps enabled, it's up to the handler
3976 		 * for isp_async to reinit stuff and restart the firmware
3977 		 * after performing the crash dump. The reason we do things
3978 		 * this way is that we may need to activate a kernel thread
3979 		 * to do all the crash dump goop.
3980 		 */
3981 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
3982 #else
3983 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
3984 		isp_reinit(isp);
3985 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
3986 #endif
3987 		rval = -1;
3988 		break;
3989 
3990 	case ASYNC_RQS_XFER_ERR:
3991 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
3992 		break;
3993 
3994 	case ASYNC_RSP_XFER_ERR:
3995 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
3996 		break;
3997 
3998 	case ASYNC_QWAKEUP:
3999 		/*
4000 		 * We've just been notified that the Queue has woken up.
4001 		 * We don't need to be chatty about this- just unlatch things
4002 		 * and move on.
4003 		 */
4004 		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4005 		break;
4006 
4007 	case ASYNC_TIMEOUT_RESET:
4008 		isp_prt(isp, ISP_LOGWARN,
4009 		    "timeout initiated SCSI bus reset of bus %d", bus);
4010 		isp->isp_sendmarker |= (1 << bus);
4011 #ifdef	ISP_TARGET_MODE
4012 		if (isp_target_async(isp, bus, mbox))
4013 			rval = -1;
4014 #endif
4015 		break;
4016 
4017 	case ASYNC_DEVICE_RESET:
4018 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
4019 		isp->isp_sendmarker |= (1 << bus);
4020 #ifdef	ISP_TARGET_MODE
4021 		if (isp_target_async(isp, bus, mbox))
4022 			rval = -1;
4023 #endif
4024 		break;
4025 
4026 	case ASYNC_EXTMSG_UNDERRUN:
4027 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
4028 		break;
4029 
4030 	case ASYNC_SCAM_INT:
4031 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
4032 		break;
4033 
4034 	case ASYNC_HUNG_SCSI:
4035 		isp_prt(isp, ISP_LOGERR,
4036 		    "stalled SCSI Bus after DATA Overrun");
4037 		/* XXX: Need to issue SCSI reset at this point */
4038 		break;
4039 
4040 	case ASYNC_KILLED_BUS:
4041 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
4042 		break;
4043 
4044 	case ASYNC_BUS_TRANSIT:
4045 		mbox = ISP_READ(isp, OUTMAILBOX2);
4046 		switch (mbox & 0x1c00) {
4047 		case SXP_PINS_LVD_MODE:
4048 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
4049 			SDPARAM(isp)->isp_diffmode = 0;
4050 			SDPARAM(isp)->isp_ultramode = 0;
4051 			SDPARAM(isp)->isp_lvdmode = 1;
4052 			break;
4053 		case SXP_PINS_HVD_MODE:
4054 			isp_prt(isp, ISP_LOGINFO,
4055 			    "Transition to Differential mode");
4056 			SDPARAM(isp)->isp_diffmode = 1;
4057 			SDPARAM(isp)->isp_ultramode = 0;
4058 			SDPARAM(isp)->isp_lvdmode = 0;
4059 			break;
4060 		case SXP_PINS_SE_MODE:
4061 			isp_prt(isp, ISP_LOGINFO,
4062 			    "Transition to Single Ended mode");
4063 			SDPARAM(isp)->isp_diffmode = 0;
4064 			SDPARAM(isp)->isp_ultramode = 1;
4065 			SDPARAM(isp)->isp_lvdmode = 0;
4066 			break;
4067 		default:
4068 			isp_prt(isp, ISP_LOGWARN,
4069 			    "Transition to Unknown Mode 0x%x", mbox);
4070 			break;
4071 		}
4072 		/*
4073 		 * XXX: Set up to renegotiate again!
4074 		 */
4075 		/* Can only be for a 1080... */
4076 		isp->isp_sendmarker |= (1 << bus);
4077 		break;
4078 
4079 	/*
4080 	 * We can use bus, which will always be zero for FC cards,
4081 	 * as a mailbox pattern accumulator to be checked below.
4082 	 */
4083 	case ASYNC_RIO5:
4084 		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
4085 		break;
4086 
4087 	case ASYNC_RIO4:
4088 		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
4089 		break;
4090 
4091 	case ASYNC_RIO3:
4092 		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
4093 		break;
4094 
4095 	case ASYNC_RIO2:
4096 		bus = 0x106;	/* outgoing mailbox regs 1-2 */
4097 		break;
4098 
4099 	case ASYNC_RIO1:
4100 	case ASYNC_CMD_CMPLT:
4101 		bus = 0x102;	/* outgoing mailbox regs 1 */
4102 		break;
4103 
4104 	case ASYNC_RIO_RESP:
4105 		return (rval);
4106 
4107 	case ASYNC_CTIO_DONE:
4108 	{
4109 #ifdef	ISP_TARGET_MODE
4110 		int handle =
4111 		    (ISP_READ(isp, OUTMAILBOX2) << 16) |
4112 		    (ISP_READ(isp, OUTMAILBOX1));
4113 		if (isp_target_async(isp, handle, mbox))
4114 			rval = -1;
4115 #else
4116 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
4117 #endif
4118 		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
4119 		break;
4120 	}
4121 	case ASYNC_LIP_F8:
4122 	case ASYNC_LIP_OCCURRED:
4123 		FCPARAM(isp)->isp_lipseq =
4124 		    ISP_READ(isp, OUTMAILBOX1);
4125 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4126 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4127 		isp->isp_sendmarker = 1;
4128 		isp_mark_getpdb_all(isp);
4129 		isp_async(isp, ISPASYNC_LIP, NULL);
4130 #ifdef	ISP_TARGET_MODE
4131 		if (isp_target_async(isp, bus, mbox))
4132 			rval = -1;
4133 #endif
4134 		/*
4135 		 * We've had problems with data corruption occuring on
4136 		 * commands that complete (with no apparent error) after
4137 		 * we receive a LIP. This has been observed mostly on
4138 		 * Local Loop topologies. To be safe, let's just mark
4139 		 * all active commands as dead.
4140 		 */
4141 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4142 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4143 			int i, j;
4144 			for (i = j = 0; i < isp->isp_maxcmds; i++) {
4145 				XS_T *xs;
4146 				xs = isp->isp_xflist[i];
4147 				if (xs != NULL) {
4148 					j++;
4149 					XS_SETERR(xs, HBA_BUSRESET);
4150 				}
4151 			}
4152 			if (j) {
4153 				isp_prt(isp, ISP_LOGERR,
4154 				    "LIP destroyed %d active commands", j);
4155 			}
4156 		}
4157 		break;
4158 
4159 	case ASYNC_LOOP_UP:
4160 		isp->isp_sendmarker = 1;
4161 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4162 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4163 		isp_mark_getpdb_all(isp);
4164 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
4165 #ifdef	ISP_TARGET_MODE
4166 		if (isp_target_async(isp, bus, mbox))
4167 			rval = -1;
4168 #endif
4169 		break;
4170 
4171 	case ASYNC_LOOP_DOWN:
4172 		isp->isp_sendmarker = 1;
4173 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4174 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4175 		isp_mark_getpdb_all(isp);
4176 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
4177 #ifdef	ISP_TARGET_MODE
4178 		if (isp_target_async(isp, bus, mbox))
4179 			rval = -1;
4180 #endif
4181 		break;
4182 
4183 	case ASYNC_LOOP_RESET:
4184 		isp->isp_sendmarker = 1;
4185 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4186 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4187 		isp_mark_getpdb_all(isp);
4188 		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
4189 #ifdef	ISP_TARGET_MODE
4190 		if (isp_target_async(isp, bus, mbox))
4191 			rval = -1;
4192 #endif
4193 		break;
4194 
4195 	case ASYNC_PDB_CHANGED:
4196 		isp->isp_sendmarker = 1;
4197 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4198 		isp_mark_getpdb_all(isp);
4199 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
4200 		break;
4201 
4202 	case ASYNC_CHANGE_NOTIFY:
4203 		/*
4204 		 * Not correct, but it will force us to rescan the loop.
4205 		 */
4206 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4207 		isp_mark_getpdb_all(isp);
4208 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
4209 		break;
4210 
4211 	case ASYNC_PTPMODE:
4212 		if (FCPARAM(isp)->isp_onfabric)
4213 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
4214 		else
4215 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
4216 		isp_mark_getpdb_all(isp);
4217 		isp->isp_sendmarker = 1;
4218 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4219 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4220 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4221 #ifdef	ISP_TARGET_MODE
4222 		if (isp_target_async(isp, bus, mbox))
4223 			rval = -1;
4224 #endif
4225 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
4226 		break;
4227 
4228 	case ASYNC_CONNMODE:
4229 		mbox = ISP_READ(isp, OUTMAILBOX1);
4230 		isp_mark_getpdb_all(isp);
4231 		switch (mbox) {
4232 		case ISP_CONN_LOOP:
4233 			isp_prt(isp, ISP_LOGINFO,
4234 			    "Point-to-Point -> Loop mode");
4235 			break;
4236 		case ISP_CONN_PTP:
4237 			isp_prt(isp, ISP_LOGINFO,
4238 			    "Loop -> Point-to-Point mode");
4239 			break;
4240 		case ISP_CONN_BADLIP:
4241 			isp_prt(isp, ISP_LOGWARN,
4242 			    "Point-to-Point -> Loop mode (BAD LIP)");
4243 			break;
4244 		case ISP_CONN_FATAL:
4245 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
4246 			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4247 			isp_reinit(isp);
4248 			isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4249 			return (-1);
4250 		case ISP_CONN_LOOPBACK:
4251 			isp_prt(isp, ISP_LOGWARN,
4252 			    "Looped Back in Point-to-Point mode");
4253 			break;
4254 		default:
4255 			isp_prt(isp, ISP_LOGWARN,
4256 			    "Unknown connection mode (0x%x)", mbox);
4257 			break;
4258 		}
4259 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4260 		isp->isp_sendmarker = 1;
4261 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4262 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4263 		break;
4264 
4265 	default:
4266 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
4267 		break;
4268 	}
4269 
4270 	if (bus & 0x100) {
4271 		int i, nh;
4272 		u_int16_t handles[5];
4273 
4274 		for (nh = 0, i = 1; i < MAX_MAILBOX; i++) {
4275 			if ((bus & (1 << i)) == 0) {
4276 				continue;
4277 			}
4278 			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
4279 		}
4280 		for (i = 0; i < nh; i++) {
4281 			isp_fastpost_complete(isp, handles[i]);
4282 			isp_prt(isp,  ISP_LOGDEBUG3,
4283 			    "fast post completion of %u", handles[i]);
4284 		}
4285 		if (isp->isp_fpcchiwater < nh)
4286 			isp->isp_fpcchiwater = nh;
4287 	} else {
4288 		isp->isp_intoasync++;
4289 	}
4290 	return (rval);
4291 }
4292 
4293 /*
4294  * Handle other response entries. A pointer to the request queue output
4295  * index is here in case we want to eat several entries at once, although
4296  * this is not used currently.
4297  */
4298 
4299 static int
4300 isp_handle_other_response(struct ispsoftc *isp, int type,
4301     isphdr_t *hp, u_int16_t *optrp)
4302 {
4303 	switch (type) {
4304 	case RQSTYPE_STATUS_CONT:
4305 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
4306 		return (1);
4307 	case RQSTYPE_ATIO:
4308 	case RQSTYPE_CTIO:
4309 	case RQSTYPE_ENABLE_LUN:
4310 	case RQSTYPE_MODIFY_LUN:
4311 	case RQSTYPE_NOTIFY:
4312 	case RQSTYPE_NOTIFY_ACK:
4313 	case RQSTYPE_CTIO1:
4314 	case RQSTYPE_ATIO2:
4315 	case RQSTYPE_CTIO2:
4316 	case RQSTYPE_CTIO3:
4317 		isp->isp_rsltccmplt++;	/* count as a response completion */
4318 #ifdef	ISP_TARGET_MODE
4319 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
4320 			return (1);
4321 		}
4322 #else
4323 		optrp = optrp;
4324 		/* FALLTHROUGH */
4325 #endif
4326 	case RQSTYPE_REQUEST:
4327 	default:
4328 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
4329 			return (1);
4330 		}
4331 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
4332 		    isp_get_response_type(isp, hp));
4333 		return (0);
4334 	}
4335 }
4336 
4337 static void
4338 isp_parse_status(struct ispsoftc *isp, ispstatusreq_t *sp, XS_T *xs)
4339 {
4340 	switch (sp->req_completion_status & 0xff) {
4341 	case RQCS_COMPLETE:
4342 		if (XS_NOERR(xs)) {
4343 			XS_SETERR(xs, HBA_NOERROR);
4344 		}
4345 		return;
4346 
4347 	case RQCS_INCOMPLETE:
4348 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
4349 			isp_prt(isp, ISP_LOGDEBUG1,
4350 			    "Selection Timeout for %d.%d.%d",
4351 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4352 			if (XS_NOERR(xs)) {
4353 				XS_SETERR(xs, HBA_SELTIMEOUT);
4354 			}
4355 			return;
4356 		}
4357 		isp_prt(isp, ISP_LOGERR,
4358 		    "command incomplete for %d.%d.%d, state 0x%x",
4359 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
4360 		    sp->req_state_flags);
4361 		break;
4362 
4363 	case RQCS_DMA_ERROR:
4364 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
4365 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4366 		break;
4367 
4368 	case RQCS_TRANSPORT_ERROR:
4369 	{
4370 		char buf[172];
4371 		SNPRINTF(buf, sizeof (buf), "states=>");
4372 		if (sp->req_state_flags & RQSF_GOT_BUS) {
4373 			SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
4374 		}
4375 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
4376 			SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
4377 		}
4378 		if (sp->req_state_flags & RQSF_SENT_CDB) {
4379 			SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
4380 		}
4381 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
4382 			SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
4383 		}
4384 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
4385 			SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
4386 		}
4387 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
4388 			SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
4389 		}
4390 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
4391 			SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
4392 		}
4393 		SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
4394 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
4395 			SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
4396 		}
4397 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
4398 			SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
4399 		}
4400 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
4401 			SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
4402 		}
4403 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
4404 			SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
4405 		}
4406 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
4407 			SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
4408 		}
4409 		if (sp->req_status_flags & RQSTF_ABORTED) {
4410 			SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
4411 		}
4412 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
4413 			SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
4414 		}
4415 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
4416 			SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
4417 		}
4418 		isp_prt(isp, ISP_LOGERR, "%s", buf);
4419 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
4420 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
4421 		break;
4422 	}
4423 	case RQCS_RESET_OCCURRED:
4424 		isp_prt(isp, ISP_LOGWARN,
4425 		    "bus reset destroyed command for %d.%d.%d",
4426 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4427 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4428 		if (XS_NOERR(xs)) {
4429 			XS_SETERR(xs, HBA_BUSRESET);
4430 		}
4431 		return;
4432 
4433 	case RQCS_ABORTED:
4434 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
4435 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4436 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4437 		if (XS_NOERR(xs)) {
4438 			XS_SETERR(xs, HBA_ABORTED);
4439 		}
4440 		return;
4441 
4442 	case RQCS_TIMEOUT:
4443 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
4444 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4445 		/*
4446 	 	 * Check to see if we logged out the device.
4447 		 */
4448 		if (IS_FC(isp)) {
4449 			if ((sp->req_completion_status & RQSTF_LOGOUT) &&
4450 			    FCPARAM(isp)->portdb[XS_TGT(xs)].valid &&
4451 			    FCPARAM(isp)->portdb[XS_TGT(xs)].fabric_dev) {
4452 				FCPARAM(isp)->portdb[XS_TGT(xs)].relogin = 1;
4453 			}
4454 		}
4455 		if (XS_NOERR(xs)) {
4456 			XS_SETERR(xs, HBA_CMDTIMEOUT);
4457 		}
4458 		return;
4459 
4460 	case RQCS_DATA_OVERRUN:
4461 		XS_RESID(xs) = sp->req_resid;
4462 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
4463 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4464 		if (XS_NOERR(xs)) {
4465 			XS_SETERR(xs, HBA_DATAOVR);
4466 		}
4467 		return;
4468 
4469 	case RQCS_COMMAND_OVERRUN:
4470 		isp_prt(isp, ISP_LOGERR,
4471 		    "command overrun for command on %d.%d.%d",
4472 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4473 		break;
4474 
4475 	case RQCS_STATUS_OVERRUN:
4476 		isp_prt(isp, ISP_LOGERR,
4477 		    "status overrun for command on %d.%d.%d",
4478 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4479 		break;
4480 
4481 	case RQCS_BAD_MESSAGE:
4482 		isp_prt(isp, ISP_LOGERR,
4483 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
4484 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4485 		break;
4486 
4487 	case RQCS_NO_MESSAGE_OUT:
4488 		isp_prt(isp, ISP_LOGERR,
4489 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
4490 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4491 		break;
4492 
4493 	case RQCS_EXT_ID_FAILED:
4494 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
4495 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4496 		break;
4497 
4498 	case RQCS_IDE_MSG_FAILED:
4499 		isp_prt(isp, ISP_LOGERR,
4500 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
4501 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4502 		break;
4503 
4504 	case RQCS_ABORT_MSG_FAILED:
4505 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
4506 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4507 		break;
4508 
4509 	case RQCS_REJECT_MSG_FAILED:
4510 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
4511 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4512 		break;
4513 
4514 	case RQCS_NOP_MSG_FAILED:
4515 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
4516 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4517 		break;
4518 
4519 	case RQCS_PARITY_ERROR_MSG_FAILED:
4520 		isp_prt(isp, ISP_LOGERR,
4521 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
4522 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4523 		break;
4524 
4525 	case RQCS_DEVICE_RESET_MSG_FAILED:
4526 		isp_prt(isp, ISP_LOGWARN,
4527 		    "BUS DEVICE RESET rejected by %d.%d.%d",
4528 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4529 		break;
4530 
4531 	case RQCS_ID_MSG_FAILED:
4532 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
4533 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4534 		break;
4535 
4536 	case RQCS_UNEXP_BUS_FREE:
4537 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
4538 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4539 		break;
4540 
4541 	case RQCS_DATA_UNDERRUN:
4542 	{
4543 		if (IS_FC(isp)) {
4544 			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
4545 			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
4546 				isp_prt(isp, ISP_LOGWARN, bun, XS_TGT(xs),
4547 				    XS_LUN(xs), XS_XFRLEN(xs), sp->req_resid,
4548 				    (ru_marked)? "marked" : "not marked");
4549 				if (XS_NOERR(xs)) {
4550 					XS_SETERR(xs, HBA_BOTCH);
4551 				}
4552 				return;
4553 			}
4554 		}
4555 		XS_RESID(xs) = sp->req_resid;
4556 		if (XS_NOERR(xs)) {
4557 			XS_SETERR(xs, HBA_NOERROR);
4558 		}
4559 		return;
4560 	}
4561 
4562 	case RQCS_XACT_ERR1:
4563 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
4564 		    XS_TGT(xs), XS_LUN(xs));
4565 		break;
4566 
4567 	case RQCS_XACT_ERR2:
4568 		isp_prt(isp, ISP_LOGERR, xact2,
4569 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
4570 		break;
4571 
4572 	case RQCS_XACT_ERR3:
4573 		isp_prt(isp, ISP_LOGERR, xact3,
4574 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4575 		break;
4576 
4577 	case RQCS_BAD_ENTRY:
4578 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
4579 		break;
4580 
4581 	case RQCS_QUEUE_FULL:
4582 		isp_prt(isp, ISP_LOGDEBUG0,
4583 		    "internal queues full for %d.%d.%d status 0x%x",
4584 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), *XS_STSP(xs));
4585 
4586 		/*
4587 		 * If QFULL or some other status byte is set, then this
4588 		 * isn't an error, per se.
4589 		 *
4590 		 * Unfortunately, some QLogic f/w writers have, in
4591 		 * some cases, ommitted to *set* status to QFULL.
4592 		 *
4593 
4594 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
4595 			XS_SETERR(xs, HBA_NOERROR);
4596 			return;
4597 		}
4598 
4599 		 *
4600 		 *
4601 		 */
4602 
4603 		*XS_STSP(xs) = SCSI_QFULL;
4604 		XS_SETERR(xs, HBA_NOERROR);
4605 		return;
4606 
4607 	case RQCS_PHASE_SKIPPED:
4608 		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
4609 		    XS_TGT(xs), XS_LUN(xs));
4610 		break;
4611 
4612 	case RQCS_ARQS_FAILED:
4613 		isp_prt(isp, ISP_LOGERR,
4614 		    "Auto Request Sense failed for %d.%d.%d",
4615 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4616 		if (XS_NOERR(xs)) {
4617 			XS_SETERR(xs, HBA_ARQFAIL);
4618 		}
4619 		return;
4620 
4621 	case RQCS_WIDE_FAILED:
4622 		isp_prt(isp, ISP_LOGERR,
4623 		    "Wide Negotiation failed for %d.%d.%d",
4624 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4625 		if (IS_SCSI(isp)) {
4626 			sdparam *sdp = isp->isp_param;
4627 			sdp += XS_CHANNEL(xs);
4628 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
4629 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4630 			isp->isp_update |= (1 << XS_CHANNEL(xs));
4631 		}
4632 		if (XS_NOERR(xs)) {
4633 			XS_SETERR(xs, HBA_NOERROR);
4634 		}
4635 		return;
4636 
4637 	case RQCS_SYNCXFER_FAILED:
4638 		isp_prt(isp, ISP_LOGERR,
4639 		    "SDTR Message failed for target %d.%d.%d",
4640 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4641 		if (IS_SCSI(isp)) {
4642 			sdparam *sdp = isp->isp_param;
4643 			sdp += XS_CHANNEL(xs);
4644 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
4645 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4646 			isp->isp_update |= (1 << XS_CHANNEL(xs));
4647 		}
4648 		break;
4649 
4650 	case RQCS_LVD_BUSERR:
4651 		isp_prt(isp, ISP_LOGERR,
4652 		    "Bad LVD condition while talking to %d.%d.%d",
4653 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4654 		break;
4655 
4656 	case RQCS_PORT_UNAVAILABLE:
4657 		/*
4658 		 * No such port on the loop. Moral equivalent of SELTIMEO
4659 		 */
4660 	case RQCS_PORT_LOGGED_OUT:
4661 		/*
4662 		 * It was there (maybe)- treat as a selection timeout.
4663 		 */
4664 		if ((sp->req_completion_status & 0xff) == RQCS_PORT_UNAVAILABLE)
4665 			isp_prt(isp, ISP_LOGINFO,
4666 			    "port unavailable for target %d", XS_TGT(xs));
4667 		else
4668 			isp_prt(isp, ISP_LOGINFO,
4669 			    "port logout for target %d", XS_TGT(xs));
4670 		/*
4671 		 * If we're on a local loop, force a LIP (which is overkill)
4672 		 * to force a re-login of this unit. If we're on fabric,
4673 		 * then we'll have to relogin as a matter of course.
4674 		 */
4675 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4676 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4677 			mbreg_t mbs;
4678 			mbs.param[0] = MBOX_INIT_LIP;
4679 			isp_mboxcmd_qnw(isp, &mbs, 1);
4680 		}
4681 
4682 		/*
4683 		 * Probably overkill.
4684 		 */
4685 		isp->isp_sendmarker = 1;
4686 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4687 		isp_mark_getpdb_all(isp);
4688 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4689 		if (XS_NOERR(xs)) {
4690 			XS_SETERR(xs, HBA_SELTIMEOUT);
4691 		}
4692 		return;
4693 
4694 	case RQCS_PORT_CHANGED:
4695 		isp_prt(isp, ISP_LOGWARN,
4696 		    "port changed for target %d", XS_TGT(xs));
4697 		if (XS_NOERR(xs)) {
4698 			XS_SETERR(xs, HBA_SELTIMEOUT);
4699 		}
4700 		return;
4701 
4702 	case RQCS_PORT_BUSY:
4703 		isp_prt(isp, ISP_LOGWARN,
4704 		    "port busy for target %d", XS_TGT(xs));
4705 		if (XS_NOERR(xs)) {
4706 			XS_SETERR(xs, HBA_TGTBSY);
4707 		}
4708 		return;
4709 
4710 	default:
4711 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
4712 		    sp->req_completion_status);
4713 		break;
4714 	}
4715 	if (XS_NOERR(xs)) {
4716 		XS_SETERR(xs, HBA_BOTCH);
4717 	}
4718 }
4719 
4720 static void
4721 isp_fastpost_complete(struct ispsoftc *isp, u_int16_t fph)
4722 {
4723 	XS_T *xs;
4724 
4725 	if (fph == 0) {
4726 		return;
4727 	}
4728 	xs = isp_find_xs(isp, fph);
4729 	if (xs == NULL) {
4730 		isp_prt(isp, ISP_LOGWARN,
4731 		    "Command for fast post handle 0x%x not found", fph);
4732 		return;
4733 	}
4734 	isp_destroy_handle(isp, fph);
4735 
4736 	/*
4737 	 * Since we don't have a result queue entry item,
4738 	 * we must believe that SCSI status is zero and
4739 	 * that all data transferred.
4740 	 */
4741 	XS_SET_STATE_STAT(isp, xs, NULL);
4742 	XS_RESID(xs) = 0;
4743 	*XS_STSP(xs) = SCSI_GOOD;
4744 	if (XS_XFRLEN(xs)) {
4745 		ISP_DMAFREE(isp, xs, fph);
4746 	}
4747 	if (isp->isp_nactive)
4748 		isp->isp_nactive--;
4749 	isp->isp_fphccmplt++;
4750 	isp_done(xs);
4751 }
4752 
4753 static int
4754 isp_mbox_continue(struct ispsoftc *isp)
4755 {
4756 	mbreg_t mbs;
4757 	u_int16_t *ptr;
4758 
4759 	switch (isp->isp_lastmbxcmd) {
4760 	case MBOX_WRITE_RAM_WORD:
4761 	case MBOX_READ_RAM_WORD:
4762 	case MBOX_READ_RAM_WORD_EXTENDED:
4763 		break;
4764 	default:
4765 		return (1);
4766 	}
4767 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
4768 		isp->isp_mbxwrk0 = 0;
4769 		return (-1);
4770 	}
4771 
4772 
4773 	/*
4774 	 * Clear the previous interrupt.
4775 	 */
4776 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
4777 	ISP_WRITE(isp, BIU_SEMA, 0);
4778 
4779 	/*
4780 	 * Continue with next word.
4781 	 */
4782 	ptr = isp->isp_mbxworkp;
4783 	switch (isp->isp_lastmbxcmd) {
4784 	case MBOX_WRITE_RAM_WORD:
4785 		mbs.param[2] = *ptr++;
4786 		mbs.param[1] = isp->isp_mbxwrk1++;
4787 		break;
4788 	case MBOX_READ_RAM_WORD:
4789 	case MBOX_READ_RAM_WORD_EXTENDED:
4790 		*ptr++ = isp->isp_mboxtmp[2];
4791 		mbs.param[1] = isp->isp_mbxwrk1++;
4792 		break;
4793 	}
4794 	isp->isp_mbxworkp = ptr;
4795 	mbs.param[0] = isp->isp_lastmbxcmd;
4796 	isp->isp_mbxwrk0 -= 1;
4797 	isp_mboxcmd_qnw(isp, &mbs, 0);
4798 	return (0);
4799 }
4800 
4801 
4802 #define	HIBYT(x)			((x) >> 0x8)
4803 #define	LOBYT(x)			((x)  & 0xff)
4804 #define	ISPOPMAP(a, b)			(((a) << 8) | (b))
4805 static u_int16_t mbpscsi[] = {
4806 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
4807 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
4808 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
4809 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
4810 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
4811 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
4812 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
4813 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
4814 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
4815 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
4816 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
4817 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
4818 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
4819 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
4820 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
4821 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
4822 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
4823 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
4824 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
4825 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
4826 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
4827 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
4828 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
4829 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
4830 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
4831 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
4832 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
4833 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
4834 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
4835 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
4836 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
4837 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
4838 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
4839 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
4840 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
4841 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
4842 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
4843 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
4844 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
4845 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
4846 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
4847 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
4848 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
4849 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
4850 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
4851 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
4852 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
4853 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
4854 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
4855 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
4856 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
4857 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
4858 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
4859 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
4860 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
4861 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
4862 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
4863 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
4864 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
4865 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
4866 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
4867 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
4868 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
4869 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
4870 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
4871 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
4872 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
4873 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
4874 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
4875 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
4876 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
4877 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
4878 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
4879 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
4880 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
4881 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
4882 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
4883 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
4884 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
4885 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
4886 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
4887 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
4888 	ISPOPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
4889 	ISPOPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
4890 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
4891 	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
4892 	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
4893 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
4894 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
4895 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
4896 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
4897 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
4898 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
4899 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
4900 };
4901 
4902 #ifndef	ISP_STRIPPED
4903 static char *scsi_mbcmd_names[] = {
4904 	"NO-OP",
4905 	"LOAD RAM",
4906 	"EXEC FIRMWARE",
4907 	"DUMP RAM",
4908 	"WRITE RAM WORD",
4909 	"READ RAM WORD",
4910 	"MAILBOX REG TEST",
4911 	"VERIFY CHECKSUM",
4912 	"ABOUT FIRMWARE",
4913 	NULL,
4914 	NULL,
4915 	NULL,
4916 	NULL,
4917 	NULL,
4918 	"CHECK FIRMWARE",
4919 	NULL,
4920 	"INIT REQUEST QUEUE",
4921 	"INIT RESULT QUEUE",
4922 	"EXECUTE IOCB",
4923 	"WAKE UP",
4924 	"STOP FIRMWARE",
4925 	"ABORT",
4926 	"ABORT DEVICE",
4927 	"ABORT TARGET",
4928 	"BUS RESET",
4929 	"STOP QUEUE",
4930 	"START QUEUE",
4931 	"SINGLE STEP QUEUE",
4932 	"ABORT QUEUE",
4933 	"GET DEV QUEUE STATUS",
4934 	NULL,
4935 	"GET FIRMWARE STATUS",
4936 	"GET INIT SCSI ID",
4937 	"GET SELECT TIMEOUT",
4938 	"GET RETRY COUNT",
4939 	"GET TAG AGE LIMIT",
4940 	"GET CLOCK RATE",
4941 	"GET ACT NEG STATE",
4942 	"GET ASYNC DATA SETUP TIME",
4943 	"GET PCI PARAMS",
4944 	"GET TARGET PARAMS",
4945 	"GET DEV QUEUE PARAMS",
4946 	"GET RESET DELAY PARAMS",
4947 	NULL,
4948 	NULL,
4949 	NULL,
4950 	NULL,
4951 	NULL,
4952 	"SET INIT SCSI ID",
4953 	"SET SELECT TIMEOUT",
4954 	"SET RETRY COUNT",
4955 	"SET TAG AGE LIMIT",
4956 	"SET CLOCK RATE",
4957 	"SET ACT NEG STATE",
4958 	"SET ASYNC DATA SETUP TIME",
4959 	"SET PCI CONTROL PARAMS",
4960 	"SET TARGET PARAMS",
4961 	"SET DEV QUEUE PARAMS",
4962 	"SET RESET DELAY PARAMS",
4963 	NULL,
4964 	NULL,
4965 	NULL,
4966 	NULL,
4967 	NULL,
4968 	"RETURN BIOS BLOCK ADDR",
4969 	"WRITE FOUR RAM WORDS",
4970 	"EXEC BIOS IOCB",
4971 	NULL,
4972 	NULL,
4973 	"SET SYSTEM PARAMETER",
4974 	"GET SYSTEM PARAMETER",
4975 	NULL,
4976 	"GET SCAM CONFIGURATION",
4977 	"SET SCAM CONFIGURATION",
4978 	"SET FIRMWARE FEATURES",
4979 	"GET FIRMWARE FEATURES",
4980 	NULL,
4981 	NULL,
4982 	NULL,
4983 	NULL,
4984 	"LOAD RAM A64",
4985 	"DUMP RAM A64",
4986 	"INITIALIZE REQUEST QUEUE A64",
4987 	"INITIALIZE RESPONSE QUEUE A64",
4988 	"EXECUTE IOCB A64",
4989 	"ENABLE TARGET MODE",
4990 	"GET TARGET MODE STATE",
4991 	NULL,
4992 	NULL,
4993 	NULL,
4994 	"SET DATA OVERRUN RECOVERY MODE",
4995 	"GET DATA OVERRUN RECOVERY MODE",
4996 	"SET HOST DATA",
4997 	"GET NOST DATA",
4998 };
4999 #endif
5000 
5001 static u_int16_t mbpfc[] = {
5002 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
5003 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
5004 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
5005 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
5006 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
5007 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
5008 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
5009 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
5010 	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
5011 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
5012 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
5013 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
5014 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
5015 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
5016 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
5017 	ISPOPMAP(0x03, 0x07),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED(1) */
5018 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
5019 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
5020 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
5021 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
5022 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
5023 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
5024 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
5025 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
5026 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
5027 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
5028 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
5029 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
5030 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
5031 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
5032 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
5033 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
5034 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
5035 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
5036 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
5037 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
5038 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
5039 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
5040 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
5041 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
5042 	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
5043 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
5044 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
5045 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
5046 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
5047 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
5048 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
5049 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
5050 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
5051 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
5052 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
5053 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
5054 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
5055 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
5056 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
5057 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
5058 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
5059 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
5060 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
5061 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5062 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5063 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5064 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5065 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5066 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
5067 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
5068 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
5069 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
5070 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5071 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
5072 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
5073 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
5074 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
5075 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
5076 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
5077 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
5078 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5079 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5080 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5081 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5082 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
5083 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
5084 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
5085 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
5086 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5087 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
5088 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
5089 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5090 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5091 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5092 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
5093 	ISPOPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
5094 	ISPOPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
5095 	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
5096 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
5097 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
5098 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
5099 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
5100 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
5101 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
5102 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
5103 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
5104 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
5105 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
5106 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
5107 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
5108 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
5109 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
5110 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
5111 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
5112 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
5113 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
5114 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
5115 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
5116 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
5117 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
5118 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
5119 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
5120 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
5121 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
5122 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
5123 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
5124 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
5125 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
5126 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
5127 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
5128 	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
5129 };
5130 /*
5131  * Footnotes
5132  *
5133  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
5134  *	do not access at this time in the core driver. The caller is
5135  *	responsible for setting this register first (Gross!).
5136  */
5137 
5138 #ifndef	ISP_STRIPPED
5139 static char *fc_mbcmd_names[] = {
5140 	"NO-OP",
5141 	"LOAD RAM",
5142 	"EXEC FIRMWARE",
5143 	"DUMP RAM",
5144 	"WRITE RAM WORD",
5145 	"READ RAM WORD",
5146 	"MAILBOX REG TEST",
5147 	"VERIFY CHECKSUM",
5148 	"ABOUT FIRMWARE",
5149 	"LOAD RAM",
5150 	"DUMP RAM",
5151 	NULL,
5152 	NULL,
5153 	"READ RAM WORD EXTENDED",
5154 	"CHECK FIRMWARE",
5155 	NULL,
5156 	"INIT REQUEST QUEUE",
5157 	"INIT RESULT QUEUE",
5158 	"EXECUTE IOCB",
5159 	"WAKE UP",
5160 	"STOP FIRMWARE",
5161 	"ABORT",
5162 	"ABORT DEVICE",
5163 	"ABORT TARGET",
5164 	"BUS RESET",
5165 	"STOP QUEUE",
5166 	"START QUEUE",
5167 	"SINGLE STEP QUEUE",
5168 	"ABORT QUEUE",
5169 	"GET DEV QUEUE STATUS",
5170 	NULL,
5171 	"GET FIRMWARE STATUS",
5172 	"GET LOOP ID",
5173 	NULL,
5174 	"GET RETRY COUNT",
5175 	NULL,
5176 	NULL,
5177 	NULL,
5178 	NULL,
5179 	NULL,
5180 	"GET FIRMWARE OPTIONS",
5181 	"GET PORT QUEUE PARAMS",
5182 	NULL,
5183 	NULL,
5184 	NULL,
5185 	NULL,
5186 	NULL,
5187 	NULL,
5188 	NULL,
5189 	NULL,
5190 	"SET RETRY COUNT",
5191 	NULL,
5192 	NULL,
5193 	NULL,
5194 	NULL,
5195 	NULL,
5196 	"SET FIRMWARE OPTIONS",
5197 	"SET PORT QUEUE PARAMS",
5198 	NULL,
5199 	NULL,
5200 	NULL,
5201 	NULL,
5202 	NULL,
5203 	NULL,
5204 	"LOOP PORT BYPASS",
5205 	"LOOP PORT ENABLE",
5206 	"GET RESOURCE COUNTS",
5207 	"REQUEST NON PARTICIPATING MODE",
5208 	NULL,
5209 	NULL,
5210 	NULL,
5211 	"GET PORT DATABASE,, ENHANCED",
5212 	NULL,
5213 	NULL,
5214 	NULL,
5215 	NULL,
5216 	NULL,
5217 	NULL,
5218 	NULL,
5219 	NULL,
5220 	NULL,
5221 	NULL,
5222 	NULL,
5223 	NULL,
5224 	"EXECUTE IOCB A64",
5225 	NULL,
5226 	NULL,
5227 	NULL,
5228 	NULL,
5229 	NULL,
5230 	NULL,
5231 	NULL,
5232 	NULL,
5233 	"GET/SET DATA RATE",
5234 	NULL,
5235 	NULL,
5236 	"INIT FIRMWARE",
5237 	NULL,
5238 	"INIT LIP",
5239 	"GET FC-AL POSITION MAP",
5240 	"GET PORT DATABASE",
5241 	"CLEAR ACA",
5242 	"TARGET RESET",
5243 	"CLEAR TASK SET",
5244 	"ABORT TASK SET",
5245 	"GET FW STATE",
5246 	"GET PORT NAME",
5247 	"GET LINK STATUS",
5248 	"INIT LIP RESET",
5249 	NULL,
5250 	"SEND SNS",
5251 	"FABRIC LOGIN",
5252 	"SEND CHANGE REQUEST",
5253 	"FABRIC LOGOUT",
5254 	"INIT LIP LOGIN",
5255 	NULL,
5256 	"LOGIN LOOP PORT",
5257 	"GET PORT/NODE NAME LIST",
5258 	"SET VENDOR ID",
5259 	"INITIALIZE IP MAILBOX",
5260 	NULL,
5261 	NULL,
5262 	NULL,
5263 	NULL,
5264 	"Get ID List",
5265 	"SEND LFA",
5266 	"Lun RESET"
5267 };
5268 #endif
5269 
5270 static void
5271 isp_mboxcmd_qnw(struct ispsoftc *isp, mbreg_t *mbp, int nodelay)
5272 {
5273 	unsigned int lim, ibits, obits, box, opcode;
5274 	u_int16_t *mcp;
5275 
5276 	if (IS_FC(isp)) {
5277 		mcp = mbpfc;
5278 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
5279 	} else {
5280 		mcp = mbpscsi;
5281 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
5282 	}
5283 	opcode = mbp->param[0];
5284 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
5285 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
5286 	for (box = 0; box < MAX_MAILBOX; box++) {
5287 		if (ibits & (1 << box)) {
5288 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5289 		}
5290 		if (nodelay == 0) {
5291 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5292 		}
5293 	}
5294 	if (nodelay == 0) {
5295 		isp->isp_lastmbxcmd = opcode;
5296 		isp->isp_obits = obits;
5297 		isp->isp_mboxbsy = 1;
5298 	}
5299 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5300 	/*
5301 	 * Oddly enough, if we're not delaying for an answer,
5302 	 * delay a bit to give the f/w a chance to pick up the
5303 	 * command.
5304 	 */
5305 	if (nodelay) {
5306 		USEC_DELAY(1000);
5307 	}
5308 }
5309 
5310 static void
5311 isp_mboxcmd(struct ispsoftc *isp, mbreg_t *mbp, int logmask)
5312 {
5313 	char *cname, *xname, tname[16], mname[16];
5314 	unsigned int lim, ibits, obits, box, opcode;
5315 	u_int16_t *mcp;
5316 
5317 	if (IS_FC(isp)) {
5318 		mcp = mbpfc;
5319 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
5320 	} else {
5321 		mcp = mbpscsi;
5322 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
5323 	}
5324 
5325 	if ((opcode = mbp->param[0]) >= lim) {
5326 		mbp->param[0] = MBOX_INVALID_COMMAND;
5327 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
5328 		return;
5329 	}
5330 
5331 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
5332 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
5333 
5334 	if (ibits == 0 && obits == 0) {
5335 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
5336 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
5337 		return;
5338 	}
5339 
5340 	/*
5341 	 * Get exclusive usage of mailbox registers.
5342 	 */
5343 	MBOX_ACQUIRE(isp);
5344 
5345 	for (box = 0; box < MAX_MAILBOX; box++) {
5346 		if (ibits & (1 << box)) {
5347 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5348 		}
5349 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5350 	}
5351 
5352 	isp->isp_lastmbxcmd = opcode;
5353 
5354 	/*
5355 	 * We assume that we can't overwrite a previous command.
5356 	 */
5357 	isp->isp_obits = obits;
5358 	isp->isp_mboxbsy = 1;
5359 
5360 	/*
5361 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
5362 	 */
5363 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5364 
5365 	/*
5366 	 * While we haven't finished the command, spin our wheels here.
5367 	 */
5368 	MBOX_WAIT_COMPLETE(isp);
5369 
5370 	if (isp->isp_mboxbsy) {
5371 		/*
5372 		 * Command timed out.
5373 		 */
5374 		isp->isp_mboxbsy = 0;
5375 		MBOX_RELEASE(isp);
5376 		return;
5377 	}
5378 
5379 	/*
5380 	 * Copy back output registers.
5381 	 */
5382 	for (box = 0; box < MAX_MAILBOX; box++) {
5383 		if (obits & (1 << box)) {
5384 			mbp->param[box] = isp->isp_mboxtmp[box];
5385 		}
5386 	}
5387 
5388 	MBOX_RELEASE(isp);
5389 
5390 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
5391 		return;
5392 	}
5393 #ifdef	ISP_STRIPPED
5394 	cname = NULL;
5395 #else
5396 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
5397 #endif
5398 	if (cname == NULL) {
5399 		cname = tname;
5400 		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
5401 	}
5402 
5403 	/*
5404 	 * Just to be chatty here...
5405 	 */
5406 	xname = NULL;
5407 	switch (mbp->param[0]) {
5408 	case MBOX_COMMAND_COMPLETE:
5409 		break;
5410 	case MBOX_INVALID_COMMAND:
5411 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
5412 			xname = "INVALID COMMAND";
5413 		break;
5414 	case MBOX_HOST_INTERFACE_ERROR:
5415 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
5416 			xname = "HOST INTERFACE ERROR";
5417 		break;
5418 	case MBOX_TEST_FAILED:
5419 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
5420 			xname = "TEST FAILED";
5421 		break;
5422 	case MBOX_COMMAND_ERROR:
5423 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
5424 			xname = "COMMAND ERROR";
5425 		break;
5426 	case MBOX_COMMAND_PARAM_ERROR:
5427 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
5428 			xname = "COMMAND PARAMETER ERROR";
5429 		break;
5430 	case MBOX_LOOP_ID_USED:
5431 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
5432 			xname = "LOOP ID ALREADY IN USE";
5433 		break;
5434 	case MBOX_PORT_ID_USED:
5435 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
5436 			xname = "PORT ID ALREADY IN USE";
5437 		break;
5438 	case MBOX_ALL_IDS_USED:
5439 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
5440 			xname = "ALL LOOP IDS IN USE";
5441 		break;
5442 	case 0:		/* special case */
5443 		xname = "TIMEOUT";
5444 		break;
5445 	default:
5446 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
5447 		xname = mname;
5448 		break;
5449 	}
5450 	if (xname)
5451 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
5452 		    cname, xname);
5453 }
5454 
5455 static void
5456 isp_fw_state(struct ispsoftc *isp)
5457 {
5458 	if (IS_FC(isp)) {
5459 		mbreg_t mbs;
5460 		fcparam *fcp = isp->isp_param;
5461 
5462 		mbs.param[0] = MBOX_GET_FW_STATE;
5463 		isp_mboxcmd(isp, &mbs, MBLOGALL);
5464 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
5465 			fcp->isp_fwstate = mbs.param[1];
5466 		}
5467 	}
5468 }
5469 
5470 static void
5471 isp_update(struct ispsoftc *isp)
5472 {
5473 	int bus, upmask;
5474 
5475 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
5476 		if (upmask & (1 << bus)) {
5477 			isp_update_bus(isp, bus);
5478 		}
5479 		upmask &= ~(1 << bus);
5480 	}
5481 }
5482 
5483 static void
5484 isp_update_bus(struct ispsoftc *isp, int bus)
5485 {
5486 	int tgt;
5487 	mbreg_t mbs;
5488 	sdparam *sdp;
5489 
5490 	isp->isp_update &= ~(1 << bus);
5491 	if (IS_FC(isp)) {
5492 		/*
5493 		 * There are no 'per-bus' settings for Fibre Channel.
5494 		 */
5495 		return;
5496 	}
5497 	sdp = isp->isp_param;
5498 	sdp += bus;
5499 
5500 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5501 		u_int16_t flags, period, offset;
5502 		int get;
5503 
5504 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
5505 			sdp->isp_devparam[tgt].dev_update = 0;
5506 			sdp->isp_devparam[tgt].dev_refresh = 0;
5507 			isp_prt(isp, ISP_LOGDEBUG0,
5508 	 		    "skipping target %d bus %d update", tgt, bus);
5509 			continue;
5510 		}
5511 		/*
5512 		 * If the goal is to update the status of the device,
5513 		 * take what's in goal_flags and try and set the device
5514 		 * toward that. Otherwise, if we're just refreshing the
5515 		 * current device state, get the current parameters.
5516 		 */
5517 
5518 		/*
5519 		 * Refresh overrides set
5520 		 */
5521 		if (sdp->isp_devparam[tgt].dev_refresh) {
5522 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
5523 			sdp->isp_devparam[tgt].dev_refresh = 0;
5524 			get = 1;
5525 		} else if (sdp->isp_devparam[tgt].dev_update) {
5526 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
5527 			/*
5528 			 * Make sure goal_flags has "Renegotiate on Error"
5529 			 * on and "Freeze Queue on Error" off.
5530 			 */
5531 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
5532 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
5533 
5534 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
5535 
5536 			/*
5537 			 * Insist that PARITY must be enabled
5538 			 * if SYNC or WIDE is enabled.
5539 			 */
5540 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
5541 				mbs.param[2] |= DPARM_PARITY;
5542 			}
5543 
5544 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
5545 				mbs.param[3] = 0;
5546 			} else {
5547 				mbs.param[3] =
5548 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
5549 				    (sdp->isp_devparam[tgt].goal_period);
5550 			}
5551 			/*
5552 			 * A command completion later that has
5553 			 * RQSTF_NEGOTIATION set can cause
5554 			 * the dev_refresh/announce cycle also.
5555 			 *
5556 			 * Note: It is really important to update our current
5557 			 * flags with at least the state of TAG capabilities-
5558 			 * otherwise we might try and send a tagged command
5559 			 * when we have it all turned off. So change it here
5560 			 * to say that current already matches goal.
5561 			 */
5562 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
5563 			sdp->isp_devparam[tgt].actv_flags |=
5564 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
5565 			isp_prt(isp, ISP_LOGDEBUG0,
5566 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
5567 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
5568 			    mbs.param[3] & 0xff);
5569 			sdp->isp_devparam[tgt].dev_update = 0;
5570 			sdp->isp_devparam[tgt].dev_refresh = 1;
5571 			get = 0;
5572 		} else {
5573 			continue;
5574 		}
5575 		mbs.param[1] = (bus << 15) | (tgt << 8);
5576 		isp_mboxcmd(isp, &mbs, MBLOGALL);
5577 		if (get == 0) {
5578 			isp->isp_sendmarker |= (1 << bus);
5579 			continue;
5580 		}
5581 		flags = mbs.param[2];
5582 		period = mbs.param[3] & 0xff;
5583 		offset = mbs.param[3] >> 8;
5584 		sdp->isp_devparam[tgt].actv_flags = flags;
5585 		sdp->isp_devparam[tgt].actv_period = period;
5586 		sdp->isp_devparam[tgt].actv_offset = offset;
5587 		get = (bus << 16) | tgt;
5588 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
5589 	}
5590 
5591 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5592 		if (sdp->isp_devparam[tgt].dev_update ||
5593 		    sdp->isp_devparam[tgt].dev_refresh) {
5594 			isp->isp_update |= (1 << bus);
5595 			break;
5596 		}
5597 	}
5598 }
5599 
5600 #ifndef	DEFAULT_FRAMESIZE
5601 #define	DEFAULT_FRAMESIZE(isp)		ICB_DFLT_FRMLEN
5602 #endif
5603 #ifndef	DEFAULT_EXEC_THROTTLE
5604 #define	DEFAULT_EXEC_THROTTLE(isp)	ISP_EXEC_THROTTLE
5605 #endif
5606 
5607 static void
5608 isp_setdfltparm(struct ispsoftc *isp, int channel)
5609 {
5610 	int tgt;
5611 	mbreg_t mbs;
5612 	sdparam *sdp;
5613 
5614 	if (IS_FC(isp)) {
5615 		fcparam *fcp = (fcparam *) isp->isp_param;
5616 		int nvfail;
5617 
5618 		fcp += channel;
5619 		if (fcp->isp_gotdparms) {
5620 			return;
5621 		}
5622 		fcp->isp_gotdparms = 1;
5623 		fcp->isp_maxfrmlen = DEFAULT_FRAMESIZE(isp);
5624 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
5625 		fcp->isp_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
5626 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
5627 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
5628 		/* Platform specific.... */
5629 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
5630 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
5631 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
5632 		fcp->isp_fwoptions = 0;
5633 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
5634 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
5635 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
5636 #ifndef	ISP_NO_FASTPOST_FC
5637 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
5638 #endif
5639 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
5640 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
5641 
5642 		/*
5643 		 * Make sure this is turned off now until we get
5644 		 * extended options from NVRAM
5645 		 */
5646 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
5647 
5648 		/*
5649 		 * Now try and read NVRAM unless told to not do so.
5650 		 * This will set fcparam's isp_nodewwn && isp_portwwn.
5651 		 */
5652 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5653 		    	nvfail = isp_read_nvram(isp);
5654 			if (nvfail)
5655 				isp->isp_confopts |= ISP_CFG_NONVRAM;
5656 		} else {
5657 			nvfail = 1;
5658 		}
5659 		/*
5660 		 * Set node && port to override platform set defaults
5661 		 * unless the nvram read failed (or none was done),
5662 		 * or the platform code wants to use what had been
5663 		 * set in the defaults.
5664 		 */
5665 		if (nvfail) {
5666 			isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
5667 		}
5668 		if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
5669 			isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
5670 			    (u_int32_t) (DEFAULT_NODEWWN(isp) >> 32),
5671 			    (u_int32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
5672 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
5673 		} else {
5674 			/*
5675 			 * We always start out with values derived
5676 			 * from NVRAM or our platform default.
5677 			 */
5678 			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
5679 		}
5680 		if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
5681 			isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
5682 			    (u_int32_t) (DEFAULT_PORTWWN(isp) >> 32),
5683 			    (u_int32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
5684 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
5685 		} else {
5686 			/*
5687 			 * We always start out with values derived
5688 			 * from NVRAM or our platform default.
5689 			 */
5690 			ISP_PORTWWN(isp) = fcp->isp_portwwn;
5691 		}
5692 		return;
5693 	}
5694 
5695 	sdp = (sdparam *) isp->isp_param;
5696 	sdp += channel;
5697 
5698 	/*
5699 	 * Been there, done that, got the T-shirt...
5700 	 */
5701 	if (sdp->isp_gotdparms) {
5702 		return;
5703 	}
5704 	sdp->isp_gotdparms = 1;
5705 
5706 	/*
5707 	 * Establish some default parameters.
5708 	 */
5709 	sdp->isp_cmd_dma_burst_enable = 0;
5710 	sdp->isp_data_dma_burst_enabl = 1;
5711 	sdp->isp_fifo_threshold = 0;
5712 	sdp->isp_initiator_id = DEFAULT_IID(isp);
5713 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
5714 		sdp->isp_async_data_setup = 9;
5715 	} else {
5716 		sdp->isp_async_data_setup = 6;
5717 	}
5718 	sdp->isp_selection_timeout = 250;
5719 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
5720 	sdp->isp_tag_aging = 8;
5721 	sdp->isp_bus_reset_delay = 5;
5722 	/*
5723 	 * Don't retry selection, busy or queue full automatically- reflect
5724 	 * these back to us.
5725 	 */
5726 	sdp->isp_retry_count = 0;
5727 	sdp->isp_retry_delay = 0;
5728 
5729 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5730 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
5731 		sdp->isp_devparam[tgt].dev_enable = 1;
5732 	}
5733 
5734 	/*
5735 	 * If we've not been told to avoid reading NVRAM, try and read it.
5736 	 * If we're successful reading it, we can then return because NVRAM
5737 	 * will tell us what the desired settings are. Otherwise, we establish
5738 	 * some reasonable 'fake' nvram and goal defaults.
5739 	 */
5740 
5741 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5742 		if (isp_read_nvram(isp) == 0) {
5743 			return;
5744 		}
5745 	}
5746 
5747 	/*
5748 	 * Now try and see whether we have specific values for them.
5749 	 */
5750 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5751 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
5752 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
5753 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
5754 			sdp->isp_req_ack_active_neg = 1;
5755 			sdp->isp_data_line_active_neg = 1;
5756 		} else {
5757 			sdp->isp_req_ack_active_neg =
5758 			    (mbs.param[1+channel] >> 4) & 0x1;
5759 			sdp->isp_data_line_active_neg =
5760 			    (mbs.param[1+channel] >> 5) & 0x1;
5761 		}
5762 	}
5763 
5764 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
5765 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
5766 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
5767 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
5768 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
5769 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
5770 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
5771 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
5772 
5773 	/*
5774 	 * The trick here is to establish a default for the default (honk!)
5775 	 * state (goal_flags). Then try and get the current status from
5776 	 * the card to fill in the current state. We don't, in fact, set
5777 	 * the default to the SAFE default state- that's not the goal state.
5778 	 */
5779 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5780 		u_int8_t off, per;
5781 		sdp->isp_devparam[tgt].actv_offset = 0;
5782 		sdp->isp_devparam[tgt].actv_period = 0;
5783 		sdp->isp_devparam[tgt].actv_flags = 0;
5784 
5785 		sdp->isp_devparam[tgt].goal_flags =
5786 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
5787 
5788 		/*
5789 		 * We default to Wide/Fast for versions less than a 1040
5790 		 * (unless it's SBus).
5791 		 */
5792 		if (IS_ULTRA3(isp)) {
5793 			off = ISP_80M_SYNCPARMS >> 8;
5794 			per = ISP_80M_SYNCPARMS & 0xff;
5795 		} else if (IS_ULTRA2(isp)) {
5796 			off = ISP_40M_SYNCPARMS >> 8;
5797 			per = ISP_40M_SYNCPARMS & 0xff;
5798 		} else if (IS_1240(isp)) {
5799 			off = ISP_20M_SYNCPARMS >> 8;
5800 			per = ISP_20M_SYNCPARMS & 0xff;
5801 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
5802 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
5803 		    (isp->isp_bustype == ISP_BT_PCI &&
5804 		    isp->isp_type < ISP_HA_SCSI_1040) ||
5805 		    (isp->isp_clock && isp->isp_clock < 60) ||
5806 		    (sdp->isp_ultramode == 0)) {
5807 			off = ISP_10M_SYNCPARMS >> 8;
5808 			per = ISP_10M_SYNCPARMS & 0xff;
5809 		} else {
5810 			off = ISP_20M_SYNCPARMS_1040 >> 8;
5811 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
5812 		}
5813 		sdp->isp_devparam[tgt].goal_offset =
5814 		    sdp->isp_devparam[tgt].nvrm_offset = off;
5815 		sdp->isp_devparam[tgt].goal_period =
5816 		    sdp->isp_devparam[tgt].nvrm_period = per;
5817 
5818 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
5819 		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
5820 		    sdp->isp_devparam[tgt].nvrm_offset,
5821 		    sdp->isp_devparam[tgt].nvrm_period);
5822 	}
5823 }
5824 
5825 /*
5826  * Re-initialize the ISP and complete all orphaned commands
5827  * with a 'botched' notice. The reset/init routines should
5828  * not disturb an already active list of commands.
5829  *
5830  * Locks held prior to coming here.
5831  */
5832 
5833 void
5834 isp_reinit(struct ispsoftc *isp)
5835 {
5836 	XS_T *xs;
5837 	u_int16_t handle;
5838 
5839 	isp_reset(isp);
5840 	if (isp->isp_state != ISP_RESETSTATE) {
5841 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
5842 	} else if (isp->isp_role != ISP_ROLE_NONE) {
5843 		isp_init(isp);
5844 		if (isp->isp_state == ISP_INITSTATE) {
5845 			isp->isp_state = ISP_RUNSTATE;
5846 		}
5847 		if (isp->isp_state != ISP_RUNSTATE) {
5848 			isp_prt(isp, ISP_LOGERR,
5849 			    "isp_reinit cannot restart card");
5850 		}
5851 	}
5852 	isp->isp_nactive = 0;
5853 
5854 	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
5855 		xs = isp_find_xs(isp, handle);
5856 		if (xs == NULL) {
5857 			continue;
5858 		}
5859 		isp_destroy_handle(isp, handle);
5860 		if (XS_XFRLEN(xs)) {
5861 			ISP_DMAFREE(isp, xs, handle);
5862 			XS_RESID(xs) = XS_XFRLEN(xs);
5863 		} else {
5864 			XS_RESID(xs) = 0;
5865 		}
5866 		XS_SETERR(xs, HBA_BUSRESET);
5867 		isp_done(xs);
5868 	}
5869 }
5870 
5871 /*
5872  * NVRAM Routines
5873  */
5874 static int
5875 isp_read_nvram(struct ispsoftc *isp)
5876 {
5877 	int i, amt;
5878 	u_int8_t csum, minversion;
5879 	union {
5880 		u_int8_t _x[ISP2100_NVRAM_SIZE];
5881 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
5882 	} _n;
5883 #define	nvram_data	_n._x
5884 #define	nvram_words	_n._s
5885 
5886 	if (IS_FC(isp)) {
5887 		amt = ISP2100_NVRAM_SIZE;
5888 		minversion = 1;
5889 	} else if (IS_ULTRA2(isp)) {
5890 		amt = ISP1080_NVRAM_SIZE;
5891 		minversion = 0;
5892 	} else {
5893 		amt = ISP_NVRAM_SIZE;
5894 		minversion = 2;
5895 	}
5896 
5897 	/*
5898 	 * Just read the first two words first to see if we have a valid
5899 	 * NVRAM to continue reading the rest with.
5900 	 */
5901 	for (i = 0; i < 2; i++) {
5902 		isp_rdnvram_word(isp, i, &nvram_words[i]);
5903 	}
5904 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
5905 	    nvram_data[2] != 'P') {
5906 		if (isp->isp_bustype != ISP_BT_SBUS) {
5907 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
5908 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
5909 			    nvram_data[0], nvram_data[1], nvram_data[2]);
5910 		}
5911 		return (-1);
5912 	}
5913 	for (i = 2; i < amt>>1; i++) {
5914 		isp_rdnvram_word(isp, i, &nvram_words[i]);
5915 	}
5916 	for (csum = 0, i = 0; i < amt; i++) {
5917 		csum += nvram_data[i];
5918 	}
5919 	if (csum != 0) {
5920 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
5921 		return (-1);
5922 	}
5923 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
5924 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
5925 		    ISP_NVRAM_VERSION(nvram_data));
5926 		return (-1);
5927 	}
5928 
5929 	if (IS_ULTRA3(isp)) {
5930 		isp_parse_nvram_12160(isp, 0, nvram_data);
5931 		if (IS_12160(isp))
5932 			isp_parse_nvram_12160(isp, 1, nvram_data);
5933 	} else if (IS_1080(isp)) {
5934 		isp_parse_nvram_1080(isp, 0, nvram_data);
5935 	} else if (IS_1280(isp) || IS_1240(isp)) {
5936 		isp_parse_nvram_1080(isp, 0, nvram_data);
5937 		isp_parse_nvram_1080(isp, 1, nvram_data);
5938 	} else if (IS_SCSI(isp)) {
5939 		isp_parse_nvram_1020(isp, nvram_data);
5940 	} else {
5941 		isp_parse_nvram_2100(isp, nvram_data);
5942 	}
5943 	return (0);
5944 #undef	nvram_data
5945 #undef	nvram_words
5946 }
5947 
5948 static void
5949 isp_rdnvram_word(struct ispsoftc *isp, int wo, u_int16_t *rp)
5950 {
5951 	int i, cbits;
5952 	u_int16_t bit, rqst;
5953 
5954 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
5955 	USEC_DELAY(2);
5956 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
5957 	USEC_DELAY(2);
5958 
5959 	if (IS_FC(isp)) {
5960 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
5961 		if (IS_2312(isp) && isp->isp_port) {
5962 			wo += 128;
5963 		}
5964 		rqst = (ISP_NVRAM_READ << 8) | wo;
5965 		cbits = 10;
5966 	} else if (IS_ULTRA2(isp)) {
5967 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
5968 		rqst = (ISP_NVRAM_READ << 8) | wo;
5969 		cbits = 10;
5970 	} else {
5971 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
5972 		rqst = (ISP_NVRAM_READ << 6) | wo;
5973 		cbits = 8;
5974 	}
5975 
5976 	/*
5977 	 * Clock the word select request out...
5978 	 */
5979 	for (i = cbits; i >= 0; i--) {
5980 		if ((rqst >> i) & 1) {
5981 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
5982 		} else {
5983 			bit = BIU_NVRAM_SELECT;
5984 		}
5985 		ISP_WRITE(isp, BIU_NVRAM, bit);
5986 		USEC_DELAY(2);
5987 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
5988 		USEC_DELAY(2);
5989 		ISP_WRITE(isp, BIU_NVRAM, bit);
5990 		USEC_DELAY(2);
5991 	}
5992 	/*
5993 	 * Now read the result back in (bits come back in MSB format).
5994 	 */
5995 	*rp = 0;
5996 	for (i = 0; i < 16; i++) {
5997 		u_int16_t rv;
5998 		*rp <<= 1;
5999 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6000 		USEC_DELAY(2);
6001 		rv = ISP_READ(isp, BIU_NVRAM);
6002 		if (rv & BIU_NVRAM_DATAIN) {
6003 			*rp |= 1;
6004 		}
6005 		USEC_DELAY(2);
6006 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6007 		USEC_DELAY(2);
6008 	}
6009 	ISP_WRITE(isp, BIU_NVRAM, 0);
6010 	USEC_DELAY(2);
6011 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
6012 }
6013 
6014 static void
6015 isp_parse_nvram_1020(struct ispsoftc *isp, u_int8_t *nvram_data)
6016 {
6017 	sdparam *sdp = (sdparam *) isp->isp_param;
6018 	int tgt;
6019 
6020 	sdp->isp_fifo_threshold =
6021 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
6022 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
6023 
6024 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6025 		sdp->isp_initiator_id =
6026 			ISP_NVRAM_INITIATOR_ID(nvram_data);
6027 
6028 	sdp->isp_bus_reset_delay =
6029 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
6030 
6031 	sdp->isp_retry_count =
6032 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
6033 
6034 	sdp->isp_retry_delay =
6035 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
6036 
6037 	sdp->isp_async_data_setup =
6038 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
6039 
6040 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
6041 		if (sdp->isp_async_data_setup < 9) {
6042 			sdp->isp_async_data_setup = 9;
6043 		}
6044 	} else {
6045 		if (sdp->isp_async_data_setup != 6) {
6046 			sdp->isp_async_data_setup = 6;
6047 		}
6048 	}
6049 
6050 	sdp->isp_req_ack_active_neg =
6051 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
6052 
6053 	sdp->isp_data_line_active_neg =
6054 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
6055 
6056 	sdp->isp_data_dma_burst_enabl =
6057 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
6058 
6059 	sdp->isp_cmd_dma_burst_enable =
6060 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
6061 
6062 	sdp->isp_tag_aging =
6063 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
6064 
6065 	sdp->isp_selection_timeout =
6066 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
6067 
6068 	sdp->isp_max_queue_depth =
6069 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
6070 
6071 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
6072 
6073 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6074 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6075 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6076 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6077 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6078 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6079 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6080 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6081 
6082 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6083 		sdp->isp_devparam[tgt].dev_enable =
6084 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
6085 		sdp->isp_devparam[tgt].exc_throttle =
6086 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
6087 		sdp->isp_devparam[tgt].nvrm_offset =
6088 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
6089 		sdp->isp_devparam[tgt].nvrm_period =
6090 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
6091 		/*
6092 		 * We probably shouldn't lie about this, but it
6093 		 * it makes it much safer if we limit NVRAM values
6094 		 * to sanity.
6095 		 */
6096 		if (isp->isp_type < ISP_HA_SCSI_1040) {
6097 			/*
6098 			 * If we're not ultra, we can't possibly
6099 			 * be a shorter period than this.
6100 			 */
6101 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
6102 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
6103 			}
6104 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
6105 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
6106 			}
6107 		} else {
6108 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
6109 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
6110 			}
6111 		}
6112 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6113 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
6114 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6115 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6116 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
6117 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6118 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
6119 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6120 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
6121 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6122 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
6123 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6124 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
6125 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6126 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
6127 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6128 		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6129 		    sdp->isp_devparam[tgt].nvrm_offset,
6130 		    sdp->isp_devparam[tgt].nvrm_period);
6131 		sdp->isp_devparam[tgt].goal_offset =
6132 		    sdp->isp_devparam[tgt].nvrm_offset;
6133 		sdp->isp_devparam[tgt].goal_period =
6134 		    sdp->isp_devparam[tgt].nvrm_period;
6135 		sdp->isp_devparam[tgt].goal_flags =
6136 		    sdp->isp_devparam[tgt].nvrm_flags;
6137 	}
6138 }
6139 
6140 static void
6141 isp_parse_nvram_1080(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
6142 {
6143 	sdparam *sdp = (sdparam *) isp->isp_param;
6144 	int tgt;
6145 
6146 	sdp += bus;
6147 
6148 	sdp->isp_fifo_threshold =
6149 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
6150 
6151 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6152 		sdp->isp_initiator_id =
6153 		    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
6154 
6155 	sdp->isp_bus_reset_delay =
6156 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6157 
6158 	sdp->isp_retry_count =
6159 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6160 
6161 	sdp->isp_retry_delay =
6162 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6163 
6164 	sdp->isp_async_data_setup =
6165 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6166 
6167 	sdp->isp_req_ack_active_neg =
6168 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6169 
6170 	sdp->isp_data_line_active_neg =
6171 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6172 
6173 	sdp->isp_data_dma_burst_enabl =
6174 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6175 
6176 	sdp->isp_cmd_dma_burst_enable =
6177 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6178 
6179 	sdp->isp_selection_timeout =
6180 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6181 
6182 	sdp->isp_max_queue_depth =
6183 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6184 
6185 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6186 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6187 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6188 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6189 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6190 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6191 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6192 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6193 
6194 
6195 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6196 		sdp->isp_devparam[tgt].dev_enable =
6197 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6198 		sdp->isp_devparam[tgt].exc_throttle =
6199 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6200 		sdp->isp_devparam[tgt].nvrm_offset =
6201 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6202 		sdp->isp_devparam[tgt].nvrm_period =
6203 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6204 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6205 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6206 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6207 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6208 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6209 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6210 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6211 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6212 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6213 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6214 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6215 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6216 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6217 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6218 		sdp->isp_devparam[tgt].actv_flags = 0;
6219 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6220 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6221 		    sdp->isp_devparam[tgt].nvrm_offset,
6222 		    sdp->isp_devparam[tgt].nvrm_period);
6223 		sdp->isp_devparam[tgt].goal_offset =
6224 		    sdp->isp_devparam[tgt].nvrm_offset;
6225 		sdp->isp_devparam[tgt].goal_period =
6226 		    sdp->isp_devparam[tgt].nvrm_period;
6227 		sdp->isp_devparam[tgt].goal_flags =
6228 		    sdp->isp_devparam[tgt].nvrm_flags;
6229 	}
6230 }
6231 
6232 static void
6233 isp_parse_nvram_12160(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
6234 {
6235 	sdparam *sdp = (sdparam *) isp->isp_param;
6236 	int tgt;
6237 
6238 	sdp += bus;
6239 
6240 	sdp->isp_fifo_threshold =
6241 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
6242 
6243 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6244 		sdp->isp_initiator_id =
6245 		    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
6246 
6247 	sdp->isp_bus_reset_delay =
6248 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6249 
6250 	sdp->isp_retry_count =
6251 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6252 
6253 	sdp->isp_retry_delay =
6254 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6255 
6256 	sdp->isp_async_data_setup =
6257 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6258 
6259 	sdp->isp_req_ack_active_neg =
6260 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6261 
6262 	sdp->isp_data_line_active_neg =
6263 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6264 
6265 	sdp->isp_data_dma_burst_enabl =
6266 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6267 
6268 	sdp->isp_cmd_dma_burst_enable =
6269 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6270 
6271 	sdp->isp_selection_timeout =
6272 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6273 
6274 	sdp->isp_max_queue_depth =
6275 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6276 
6277 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6278 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6279 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6280 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6281 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6282 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6283 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6284 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6285 
6286 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6287 		sdp->isp_devparam[tgt].dev_enable =
6288 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6289 		sdp->isp_devparam[tgt].exc_throttle =
6290 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6291 		sdp->isp_devparam[tgt].nvrm_offset =
6292 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6293 		sdp->isp_devparam[tgt].nvrm_period =
6294 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6295 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6296 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6297 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6298 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6299 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6300 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6301 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6302 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6303 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6304 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6305 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6306 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6307 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6308 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6309 		sdp->isp_devparam[tgt].actv_flags = 0;
6310 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6311 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6312 		    sdp->isp_devparam[tgt].nvrm_offset,
6313 		    sdp->isp_devparam[tgt].nvrm_period);
6314 		sdp->isp_devparam[tgt].goal_offset =
6315 		    sdp->isp_devparam[tgt].nvrm_offset;
6316 		sdp->isp_devparam[tgt].goal_period =
6317 		    sdp->isp_devparam[tgt].nvrm_period;
6318 		sdp->isp_devparam[tgt].goal_flags =
6319 		    sdp->isp_devparam[tgt].nvrm_flags;
6320 	}
6321 }
6322 
6323 static void
6324 isp_parse_nvram_2100(struct ispsoftc *isp, u_int8_t *nvram_data)
6325 {
6326 	fcparam *fcp = (fcparam *) isp->isp_param;
6327 	u_int64_t wwn;
6328 
6329 	/*
6330 	 * There is NVRAM storage for both Port and Node entities-
6331 	 * but the Node entity appears to be unused on all the cards
6332 	 * I can find. However, we should account for this being set
6333 	 * at some point in the future.
6334 	 *
6335 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
6336 	 * bits 48..60. In the case of the 2202, it appears that they do
6337 	 * use bit 48 to distinguish between the two instances on the card.
6338 	 * The 2204, which I've never seen, *probably* extends this method.
6339 	 */
6340 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
6341 	if (wwn) {
6342 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
6343 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
6344 		if ((wwn >> 60) == 0) {
6345 			wwn |= (((u_int64_t) 2)<< 60);
6346 		}
6347 	}
6348 	fcp->isp_portwwn = wwn;
6349 	if (IS_2200(isp) || IS_23XX(isp)) {
6350 		wwn = ISP2200_NVRAM_NODE_NAME(nvram_data);
6351 		if (wwn) {
6352 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
6353 			    (u_int32_t) (wwn >> 32),
6354 			    (u_int32_t) (wwn & 0xffffffff));
6355 			if ((wwn >> 60) == 0) {
6356 				wwn |= (((u_int64_t) 2)<< 60);
6357 			}
6358 		}
6359 	} else {
6360 		wwn &= ~((u_int64_t) 0xfff << 48);
6361 	}
6362 	fcp->isp_nodewwn = wwn;
6363 
6364 	/*
6365 	 * Make sure we have both Node and Port as non-zero values.
6366 	 */
6367 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
6368 		fcp->isp_portwwn = fcp->isp_nodewwn;
6369 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
6370 		fcp->isp_nodewwn = fcp->isp_portwwn;
6371 	}
6372 
6373 	/*
6374 	 * Make the Node and Port values sane if they're NAA == 2.
6375 	 * This means to clear bits 48..56 for the Node WWN and
6376 	 * make sure that there's some non-zero value in 48..56
6377 	 * for the Port WWN.
6378 	 */
6379 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
6380 		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
6381 		    (fcp->isp_nodewwn >> 60) == 2) {
6382 			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
6383 		}
6384 		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
6385 		    (fcp->isp_portwwn >> 60) == 2) {
6386 			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
6387 		}
6388 	}
6389 
6390 	isp_prt(isp, ISP_LOGDEBUG0,
6391 	    "NVRAM: maxfrmlen %d execthrottle %d fwoptions 0x%x loopid %x",
6392 	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data),
6393 	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
6394 	    ISP2100_NVRAM_OPTIONS(nvram_data),
6395 	    ISP2100_NVRAM_HARDLOOPID(nvram_data));
6396 
6397 	fcp->isp_maxalloc =
6398 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
6399 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0)
6400 		fcp->isp_maxfrmlen =
6401 			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
6402 	fcp->isp_retry_delay =
6403 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
6404 	fcp->isp_retry_count =
6405 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
6406 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6407 		fcp->isp_loopid =
6408 			ISP2100_NVRAM_HARDLOOPID(nvram_data);
6409 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0)
6410 		fcp->isp_execthrottle =
6411 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
6412 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
6413 }
6414 
6415 #ifdef	ISP_FW_CRASH_DUMP
6416 static void isp2200_fw_dump(struct ispsoftc *);
6417 static void isp2300_fw_dump(struct ispsoftc *);
6418 
6419 static void
6420 isp2200_fw_dump(struct ispsoftc *isp)
6421 {
6422 	int i, j;
6423 	mbreg_t mbs;
6424 	u_int16_t *ptr;
6425 
6426 	ptr = FCPARAM(isp)->isp_dump_data;
6427 	if (ptr == NULL) {
6428 		isp_prt(isp, ISP_LOGERR,
6429 		   "No place to dump RISC registers and SRAM");
6430 		return;
6431 	}
6432 	if (*ptr++) {
6433 		isp_prt(isp, ISP_LOGERR,
6434 		   "dump area for RISC registers and SRAM already used");
6435 		return;
6436 	}
6437 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6438 	for (i = 0; i < 100; i++) {
6439 		USEC_DELAY(100);
6440 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6441 			break;
6442 		}
6443 	}
6444 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6445 		/*
6446 		 * PBIU Registers
6447 		 */
6448 		for (i = 0; i < 8; i++) {
6449 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6450 		}
6451 
6452 		/*
6453 		 * Mailbox Registers
6454 		 */
6455 		for (i = 0; i < 8; i++) {
6456 			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
6457 		}
6458 
6459 		/*
6460 		 * DMA Registers
6461 		 */
6462 		for (i = 0; i < 48; i++) {
6463 			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
6464 		}
6465 
6466 		/*
6467 		 * RISC H/W Registers
6468 		 */
6469 		ISP_WRITE(isp, BIU2100_CSR, 0);
6470 		for (i = 0; i < 16; i++) {
6471 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6472 		}
6473 
6474 		/*
6475 		 * RISC GP Registers
6476 		 */
6477 		for (j = 0; j < 8; j++) {
6478 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
6479 			for (i = 0; i < 16; i++) {
6480 				*ptr++ =
6481 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6482 			}
6483 		}
6484 
6485 		/*
6486 		 * Frame Buffer Hardware Registers
6487 		 */
6488 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6489 		for (i = 0; i < 16; i++) {
6490 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6491 		}
6492 
6493 		/*
6494 		 * Fibre Protocol Module 0 Hardware Registers
6495 		 */
6496 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6497 		for (i = 0; i < 64; i++) {
6498 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6499 		}
6500 
6501 		/*
6502 		 * Fibre Protocol Module 1 Hardware Registers
6503 		 */
6504 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6505 		for (i = 0; i < 64; i++) {
6506 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6507 		}
6508 	} else {
6509 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6510 		return;
6511 	}
6512 	isp_prt(isp, ISP_LOGALL,
6513 	   "isp_fw_dump: RISC registers dumped successfully");
6514 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6515 	for (i = 0; i < 100; i++) {
6516 		USEC_DELAY(100);
6517 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6518 			break;
6519 		}
6520 	}
6521 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6522 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6523 		return;
6524 	}
6525 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6526 	for (i = 0; i < 100; i++) {
6527 		USEC_DELAY(100);
6528 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6529 			break;
6530 		}
6531 	}
6532 	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6533 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
6534 		return;
6535 	}
6536 	ISP_WRITE(isp, RISC_EMB, 0xf2);
6537 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
6538 	for (i = 0; i < 100; i++) {
6539 		USEC_DELAY(100);
6540 		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6541 			break;
6542 		}
6543 	}
6544 	ENABLE_INTS(isp);
6545 	mbs.param[0] = MBOX_READ_RAM_WORD;
6546 	mbs.param[1] = 0x1000;
6547 	isp->isp_mbxworkp = (void *) ptr;
6548 	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
6549 	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
6550 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6551 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6552 		isp_prt(isp, ISP_LOGWARN,
6553 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6554 		return;
6555 	}
6556 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6557 	*ptr++ = isp->isp_mboxtmp[2];
6558 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped succesfully");
6559 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6560 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6561 }
6562 
6563 static void
6564 isp2300_fw_dump(struct ispsoftc *isp)
6565 {
6566 	int i, j;
6567 	mbreg_t mbs;
6568 	u_int16_t *ptr;
6569 
6570 	ptr = FCPARAM(isp)->isp_dump_data;
6571 	if (ptr == NULL) {
6572 		isp_prt(isp, ISP_LOGERR,
6573 		   "No place to dump RISC registers and SRAM");
6574 		return;
6575 	}
6576 	if (*ptr++) {
6577 		isp_prt(isp, ISP_LOGERR,
6578 		   "dump area for RISC registers and SRAM already used");
6579 		return;
6580 	}
6581 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6582 	for (i = 0; i < 100; i++) {
6583 		USEC_DELAY(100);
6584 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6585 			break;
6586 		}
6587 	}
6588 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6589 		/*
6590 		 * PBIU registers
6591 		 */
6592 		for (i = 0; i < 8; i++) {
6593 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6594 		}
6595 
6596 		/*
6597 		 * ReqQ-RspQ-Risc2Host Status registers
6598 		 */
6599 		for (i = 0; i < 8; i++) {
6600 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
6601 		}
6602 
6603 		/*
6604 		 * Mailbox Registers
6605 		 */
6606 		for (i = 0; i < 32; i++) {
6607 			*ptr++ =
6608 			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
6609 		}
6610 
6611 		/*
6612 		 * Auto Request Response DMA registers
6613 		 */
6614 		ISP_WRITE(isp, BIU2100_CSR, 0x40);
6615 		for (i = 0; i < 32; i++) {
6616 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6617 		}
6618 
6619 		/*
6620 		 * DMA registers
6621 		 */
6622 		ISP_WRITE(isp, BIU2100_CSR, 0x50);
6623 		for (i = 0; i < 48; i++) {
6624 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6625 		}
6626 
6627 		/*
6628 		 * RISC hardware registers
6629 		 */
6630 		ISP_WRITE(isp, BIU2100_CSR, 0);
6631 		for (i = 0; i < 16; i++) {
6632 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6633 		}
6634 
6635 		/*
6636 		 * RISC GP? registers
6637 		 */
6638 		for (j = 0; j < 8; j++) {
6639 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
6640 			for (i = 0; i < 16; i++) {
6641 				*ptr++ =
6642 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6643 			}
6644 		}
6645 
6646 		/*
6647 		 * frame buffer hardware registers
6648 		 */
6649 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6650 		for (i = 0; i < 64; i++) {
6651 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6652 		}
6653 
6654 		/*
6655 		 * FPM B0 hardware registers
6656 		 */
6657 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6658 		for (i = 0; i < 64; i++) {
6659 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6660 		}
6661 
6662 		/*
6663 		 * FPM B1 hardware registers
6664 		 */
6665 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6666 		for (i = 0; i < 64; i++) {
6667 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6668 		}
6669 	} else {
6670 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6671 		return;
6672 	}
6673 	isp_prt(isp, ISP_LOGALL,
6674 	   "isp_fw_dump: RISC registers dumped successfully");
6675 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6676 	for (i = 0; i < 100; i++) {
6677 		USEC_DELAY(100);
6678 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6679 			break;
6680 		}
6681 	}
6682 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6683 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6684 		return;
6685 	}
6686 	ENABLE_INTS(isp);
6687 	mbs.param[0] = MBOX_READ_RAM_WORD;
6688 	mbs.param[1] = 0x800;
6689 	isp->isp_mbxworkp = (void *) ptr;
6690 	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
6691 	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
6692 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6693 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6694 		isp_prt(isp, ISP_LOGWARN,
6695 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6696 		return;
6697 	}
6698 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6699 	*ptr++ = isp->isp_mboxtmp[2];
6700 
6701 	/*
6702 	 * We don't have access to mailbox registers 8.. onward
6703 	 * in our 'common' device model- so we have to set it
6704 	 * here and hope it stays the same!
6705 	 */
6706 	ISP_WRITE(isp, PCI_MBOX_REGS2300_OFF + (8 << 1), 0x1);
6707 
6708 	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
6709 	mbs.param[1] = 0;
6710 	isp->isp_mbxworkp = (void *) ptr;
6711 	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
6712 	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
6713 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6714 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6715 		isp_prt(isp, ISP_LOGWARN,
6716 		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
6717 		return;
6718 	}
6719 	ptr = isp->isp_mbxworkp;	/* finish final word */
6720 	*ptr++ = mbs.param[2];
6721 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped succesfully");
6722 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6723 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6724 }
6725 
6726 void
6727 isp_fw_dump(struct ispsoftc *isp)
6728 {
6729 	if (IS_2200(isp))
6730 		isp2200_fw_dump(isp);
6731 	else if (IS_23XX(isp))
6732 		isp2300_fw_dump(isp);
6733 }
6734 #endif
6735