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