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