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