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