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