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