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