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