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