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