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