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