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