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