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