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