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