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