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