Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.96
      1 /* $NetBSD: isp.c,v 1.96 2002/08/12 21:33:39 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.96 2002/08/12 21:33:39 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 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2845 	return (0);
   2846 }
   2847 #endif
   2848 
   2849 static void
   2850 isp_register_fc4_type(struct ispsoftc *isp)
   2851 {
   2852 	fcparam *fcp = isp->isp_param;
   2853 	u_int8_t local[SNS_RFT_ID_REQ_SIZE];
   2854 	sns_screq_t *reqp = (sns_screq_t *) local;
   2855 	mbreg_t mbs;
   2856 
   2857 	MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
   2858 	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
   2859 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
   2860 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
   2861 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
   2862 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
   2863 	reqp->snscb_sblen = 22;
   2864 	reqp->snscb_data[0] = SNS_RFT_ID;
   2865 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
   2866 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
   2867 	reqp->snscb_data[6] = (1 << FC4_SCSI);
   2868 #if	0
   2869 	reqp->snscb_data[6] |= (1 << FC4_IP);	/* ISO/IEC 8802-2 LLC/SNAP */
   2870 #endif
   2871 	FC_SCRATCH_ACQUIRE(isp);
   2872 	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
   2873 	mbs.param[0] = MBOX_SEND_SNS;
   2874 	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
   2875 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2876 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2877 	/*
   2878 	 * Leave 4 and 5 alone
   2879 	 */
   2880 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2881 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2882 	isp_mboxcmd(isp, &mbs, MBLOGALL);
   2883 	FC_SCRATCH_RELEASE(isp);
   2884 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   2885 		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
   2886 	}
   2887 }
   2888 
   2889 /*
   2890  * Start a command. Locking is assumed done in the caller.
   2891  */
   2892 
   2893 int
   2894 isp_start(XS_T *xs)
   2895 {
   2896 	struct ispsoftc *isp;
   2897 	u_int16_t nxti, optr, handle;
   2898 	u_int8_t local[QENTRY_LEN];
   2899 	ispreq_t *reqp, *qep;
   2900 	int target, i;
   2901 
   2902 	XS_INITERR(xs);
   2903 	isp = XS_ISP(xs);
   2904 
   2905 	/*
   2906 	 * Check to make sure we're supporting initiator role.
   2907 	 */
   2908 	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
   2909 		XS_SETERR(xs, HBA_SELTIMEOUT);
   2910 		return (CMD_COMPLETE);
   2911 	}
   2912 
   2913 	/*
   2914 	 * Now make sure we're running.
   2915 	 */
   2916 
   2917 	if (isp->isp_state != ISP_RUNSTATE) {
   2918 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
   2919 		XS_SETERR(xs, HBA_BOTCH);
   2920 		return (CMD_COMPLETE);
   2921 	}
   2922 
   2923 	/*
   2924 	 * Check command CDB length, etc.. We really are limited to 16 bytes
   2925 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
   2926 	 * but probably only if we're running fairly new firmware (we'll
   2927 	 * let the old f/w choke on an extended command queue entry).
   2928 	 */
   2929 
   2930 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
   2931 		isp_prt(isp, ISP_LOGERR,
   2932 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
   2933 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
   2934 		XS_SETERR(xs, HBA_BOTCH);
   2935 		return (CMD_COMPLETE);
   2936 	}
   2937 
   2938 	/*
   2939 	 * Check to see whether we have good firmware state still or
   2940 	 * need to refresh our port database for this target.
   2941 	 */
   2942 	target = XS_TGT(xs);
   2943 	if (IS_FC(isp)) {
   2944 		fcparam *fcp = isp->isp_param;
   2945 		struct lportdb *lp;
   2946 #ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
   2947 		if (fcp->isp_fwstate != FW_READY ||
   2948 		    fcp->isp_loopstate != LOOP_READY) {
   2949 			return (CMD_RQLATER);
   2950 		}
   2951 
   2952 		/*
   2953 		 * If we're not on a Fabric, we can't have a target
   2954 		 * above FL_PORT_ID-1.
   2955 		 *
   2956 		 * If we're on a fabric and *not* connected as an F-port,
   2957 		 * we can't have a target less than FC_SNS_ID+1. This
   2958 		 * keeps us from having to sort out the difference between
   2959 		 * local public loop devices and those which we might get
   2960 		 * from a switch's database.
   2961 		 */
   2962 		if (fcp->isp_onfabric == 0) {
   2963 			if (target >= FL_PORT_ID) {
   2964 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2965 				return (CMD_COMPLETE);
   2966 			}
   2967 		} else {
   2968 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
   2969 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2970 				return (CMD_COMPLETE);
   2971 			}
   2972 			/*
   2973 			 * We used to exclude having local loop ports
   2974 			 * at the same time that we have fabric ports.
   2975 			 * That is, we used to exclude having ports
   2976 			 * at < FL_PORT_ID if we're FL-port.
   2977 			 *
   2978 			 * That's wrong. The only thing that could be
   2979 			 * dicey is if the switch you're connected to
   2980 			 * has these local loop ports appear on the
   2981 			 * fabric and we somehow attach them twice.
   2982 			 */
   2983 		}
   2984 #else
   2985 		/*
   2986 		 * Check for f/w being in ready state. If the f/w
   2987 		 * isn't in ready state, then we don't know our
   2988 		 * loop ID and the f/w hasn't completed logging
   2989 		 * into all targets on the loop. If this is the
   2990 		 * case, then bounce the command. We pretend this is
   2991 		 * a SELECTION TIMEOUT error if we've never gone to
   2992 		 * FW_READY state at all- in this case we may not
   2993 		 * be hooked to a loop at all and we shouldn't hang
   2994 		 * the machine for this. Otherwise, defer this command
   2995 		 * until later.
   2996 		 */
   2997 		if (fcp->isp_fwstate != FW_READY) {
   2998 			/*
   2999 			 * Give ourselves at most a 250ms delay.
   3000 			 */
   3001 			if (isp_fclink_test(isp, 250000)) {
   3002 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3003 				if (fcp->loop_seen_once) {
   3004 					return (CMD_RQLATER);
   3005 				} else {
   3006 					return (CMD_COMPLETE);
   3007 				}
   3008 			}
   3009 		}
   3010 
   3011 		/*
   3012 		 * If we're not on a Fabric, we can't have a target
   3013 		 * above FL_PORT_ID-1.
   3014 		 *
   3015 		 * If we're on a fabric and *not* connected as an F-port,
   3016 		 * we can't have a target less than FC_SNS_ID+1. This
   3017 		 * keeps us from having to sort out the difference between
   3018 		 * local public loop devices and those which we might get
   3019 		 * from a switch's database.
   3020 		 */
   3021 		if (fcp->isp_onfabric == 0) {
   3022 			if (target >= FL_PORT_ID) {
   3023 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3024 				return (CMD_COMPLETE);
   3025 			}
   3026 		} else {
   3027 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
   3028 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3029 				return (CMD_COMPLETE);
   3030 			}
   3031 			if (fcp->isp_topo != TOPO_F_PORT &&
   3032 			    target < FL_PORT_ID) {
   3033 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3034 				return (CMD_COMPLETE);
   3035 			}
   3036 		}
   3037 
   3038 		/*
   3039 		 * If our loop state is such that we haven't yet received
   3040 		 * a "Port Database Changed" notification (after a LIP or
   3041 		 * a Loop Reset or firmware initialization), then defer
   3042 		 * sending commands for a little while, but only if we've
   3043 		 * seen a valid loop at one point (otherwise we can get
   3044 		 * stuck at initialization time).
   3045 		 */
   3046 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
   3047 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3048 			if (fcp->loop_seen_once) {
   3049 				return (CMD_RQLATER);
   3050 			} else {
   3051 				return (CMD_COMPLETE);
   3052 			}
   3053 		}
   3054 
   3055 		/*
   3056 		 * If we're in the middle of loop or fabric scanning
   3057 		 * or merging the port databases, retry this command later.
   3058 		 */
   3059 		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
   3060 		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
   3061 		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
   3062 			return (CMD_RQLATER);
   3063 		}
   3064 
   3065 		/*
   3066 		 * If our loop state is now such that we've just now
   3067 		 * received a Port Database Change notification, then
   3068 		 * we have to go off and (re)scan the fabric. We back
   3069 		 * out and try again later if this doesn't work.
   3070 		 */
   3071 		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
   3072 			if (isp_scan_fabric(isp, FC4_SCSI)) {
   3073 				return (CMD_RQLATER);
   3074 			}
   3075 			if (fcp->isp_fwstate != FW_READY ||
   3076 			    fcp->isp_loopstate < LOOP_FSCAN_DONE) {
   3077 				return (CMD_RQLATER);
   3078 			}
   3079 		}
   3080 
   3081 		/*
   3082 		 * If our loop state is now such that we've just now
   3083 		 * received a Port Database Change notification, then
   3084 		 * we have to go off and (re)synchronize our port
   3085 		 * database.
   3086 		 */
   3087 		if (fcp->isp_loopstate < LOOP_READY) {
   3088 			if (isp_pdb_sync(isp)) {
   3089 				return (CMD_RQLATER);
   3090 			}
   3091 			if (fcp->isp_fwstate != FW_READY ||
   3092 			    fcp->isp_loopstate != LOOP_READY) {
   3093 				return (CMD_RQLATER);
   3094 			}
   3095 		}
   3096 
   3097 		/*
   3098 		 * XXX: Here's were we would cancel any loop_dead flag
   3099 		 * XXX: also cancel in dead_loop timeout that's running
   3100 		 */
   3101 #endif
   3102 
   3103 		/*
   3104 		 * Now check whether we should even think about pursuing this.
   3105 		 */
   3106 		lp = &fcp->portdb[target];
   3107 		if (lp->valid == 0) {
   3108 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3109 			return (CMD_COMPLETE);
   3110 		}
   3111 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
   3112 			isp_prt(isp, ISP_LOGDEBUG2,
   3113 			    "Target %d does not have target service", target);
   3114 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3115 			return (CMD_COMPLETE);
   3116 		}
   3117 		/*
   3118 		 * Now turn target into what the actual Loop ID is.
   3119 		 */
   3120 		target = lp->loopid;
   3121 	}
   3122 
   3123 	/*
   3124 	 * Next check to see if any HBA or Device
   3125 	 * parameters need to be updated.
   3126 	 */
   3127 	if (isp->isp_update != 0) {
   3128 		isp_update(isp);
   3129 	}
   3130 
   3131 	if (isp_getrqentry(isp, &nxti, &optr, (void **)&qep)) {
   3132 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
   3133 		XS_SETERR(xs, HBA_BOTCH);
   3134 		return (CMD_EAGAIN);
   3135 	}
   3136 
   3137 	/*
   3138 	 * Now see if we need to synchronize the ISP with respect to anything.
   3139 	 * We do dual duty here (cough) for synchronizing for busses other
   3140 	 * than which we got here to send a command to.
   3141 	 */
   3142 	reqp = (ispreq_t *) local;
   3143 	if (isp->isp_sendmarker) {
   3144 		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
   3145 		/*
   3146 		 * Check ports to send markers for...
   3147 		 */
   3148 		for (i = 0; i < n; i++) {
   3149 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
   3150 				continue;
   3151 			}
   3152 			MEMZERO((void *) reqp, QENTRY_LEN);
   3153 			reqp->req_header.rqs_entry_count = 1;
   3154 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
   3155 			reqp->req_modifier = SYNC_ALL;
   3156 			reqp->req_target = i << 7;	/* insert bus number */
   3157 			isp_put_request(isp, reqp, qep);
   3158 			ISP_ADD_REQUEST(isp, nxti);
   3159 			isp->isp_sendmarker &= ~(1 << i);
   3160 			if (isp_getrqentry(isp, &nxti, &optr, (void **) &qep)) {
   3161 				isp_prt(isp, ISP_LOGDEBUG0,
   3162 				    "Request Queue Overflow+");
   3163 				XS_SETERR(xs, HBA_BOTCH);
   3164 				return (CMD_EAGAIN);
   3165 			}
   3166 		}
   3167 	}
   3168 
   3169 	MEMZERO((void *)reqp, QENTRY_LEN);
   3170 	reqp->req_header.rqs_entry_count = 1;
   3171 	if (IS_FC(isp)) {
   3172 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
   3173 	} else {
   3174 		if (XS_CDBLEN(xs) > 12)
   3175 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
   3176 		else
   3177 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
   3178 	}
   3179 	/* reqp->req_header.rqs_flags = 0; */
   3180 	/* reqp->req_header.rqs_seqno = 0; */
   3181 	if (IS_FC(isp)) {
   3182 		/*
   3183 		 * See comment in isp_intr
   3184 		 */
   3185 		/* XS_RESID(xs) = 0; */
   3186 
   3187 		/*
   3188 		 * Fibre Channel always requires some kind of tag.
   3189 		 * The Qlogic drivers seem be happy not to use a tag,
   3190 		 * but this breaks for some devices (IBM drives).
   3191 		 */
   3192 		if (XS_TAG_P(xs)) {
   3193 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
   3194 		} else {
   3195 			/*
   3196 			 * If we don't know what tag to use, use HEAD OF QUEUE
   3197 			 * for Request Sense or Simple.
   3198 			 */
   3199 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
   3200 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
   3201 			else
   3202 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
   3203 		}
   3204 	} else {
   3205 		sdparam *sdp = (sdparam *)isp->isp_param;
   3206 		sdp += XS_CHANNEL(xs);
   3207 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
   3208 		    XS_TAG_P(xs)) {
   3209 			reqp->req_flags = XS_TAG_TYPE(xs);
   3210 		}
   3211 	}
   3212 	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
   3213 	if (IS_SCSI(isp)) {
   3214 		reqp->req_lun_trn = XS_LUN(xs);
   3215 		reqp->req_cdblen = XS_CDBLEN(xs);
   3216 	} else {
   3217 		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)
   3218 			((ispreqt2_t *)reqp)->req_scclun = XS_LUN(xs);
   3219 		else
   3220 			((ispreqt2_t *)reqp)->req_lun_trn = XS_LUN(xs);
   3221 	}
   3222 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
   3223 
   3224 	reqp->req_time = XS_TIME(xs) / 1000;
   3225 	if (reqp->req_time == 0 && XS_TIME(xs)) {
   3226 		reqp->req_time = 1;
   3227 	}
   3228 
   3229 	if (isp_save_xs(isp, xs, &handle)) {
   3230 		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
   3231 		XS_SETERR(xs, HBA_BOTCH);
   3232 		return (CMD_EAGAIN);
   3233 	}
   3234 	reqp->req_handle = handle;
   3235 
   3236 	/*
   3237 	 * Set up DMA and/or do any bus swizzling of the request entry
   3238 	 * so that the Qlogic F/W understands what is being asked of it.
   3239 	 */
   3240 	i = ISP_DMASETUP(isp, xs, reqp, &nxti, optr);
   3241 	if (i != CMD_QUEUED) {
   3242 		isp_destroy_handle(isp, handle);
   3243 		/*
   3244 		 * dmasetup sets actual error in packet, and
   3245 		 * return what we were given to return.
   3246 		 */
   3247 		return (i);
   3248 	}
   3249 	XS_SETERR(xs, HBA_NOERROR);
   3250 	isp_prt(isp, ISP_LOGDEBUG2,
   3251 	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
   3252 	    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), XS_CDBP(xs)[0],
   3253 	    (long) XS_XFRLEN(xs));
   3254 	ISP_ADD_REQUEST(isp, nxti);
   3255 	isp->isp_nactive++;
   3256 	return (CMD_QUEUED);
   3257 }
   3258 
   3259 /*
   3260  * isp control
   3261  * Locks (ints blocked) assumed held.
   3262  */
   3263 
   3264 int
   3265 isp_control(struct ispsoftc *isp, ispctl_t ctl, void *arg)
   3266 {
   3267 	XS_T *xs;
   3268 	mbreg_t mbs;
   3269 	int bus, tgt;
   3270 	u_int16_t handle;
   3271 
   3272 	switch (ctl) {
   3273 	default:
   3274 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
   3275 		break;
   3276 
   3277 	case ISPCTL_RESET_BUS:
   3278 		/*
   3279 		 * Issue a bus reset.
   3280 		 */
   3281 		mbs.param[0] = MBOX_BUS_RESET;
   3282 		mbs.param[2] = 0;
   3283 		if (IS_SCSI(isp)) {
   3284 			mbs.param[1] =
   3285 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
   3286 			if (mbs.param[1] < 2)
   3287 				mbs.param[1] = 2;
   3288 			bus = *((int *) arg);
   3289 			if (IS_DUALBUS(isp))
   3290 				mbs.param[2] = bus;
   3291 		} else {
   3292 			mbs.param[1] = 10;
   3293 			bus = 0;
   3294 		}
   3295 		isp->isp_sendmarker |= (1 << bus);
   3296 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   3297 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3298 			break;
   3299 		}
   3300 		isp_prt(isp, ISP_LOGINFO,
   3301 		    "driver initiated bus reset of bus %d", bus);
   3302 		return (0);
   3303 
   3304 	case ISPCTL_RESET_DEV:
   3305 		tgt = (*((int *) arg)) & 0xffff;
   3306 		bus = (*((int *) arg)) >> 16;
   3307 		mbs.param[0] = MBOX_ABORT_TARGET;
   3308 		mbs.param[1] = (tgt << 8) | (bus << 15);
   3309 		mbs.param[2] = 3;	/* 'delay', in seconds */
   3310 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   3311 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3312 			break;
   3313 		}
   3314 		isp_prt(isp, ISP_LOGINFO,
   3315 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
   3316 		isp->isp_sendmarker |= (1 << bus);
   3317 		return (0);
   3318 
   3319 	case ISPCTL_ABORT_CMD:
   3320 		xs = (XS_T *) arg;
   3321 		tgt = XS_TGT(xs);
   3322 		handle = isp_find_handle(isp, xs);
   3323 		if (handle == 0) {
   3324 			isp_prt(isp, ISP_LOGWARN,
   3325 			    "cannot find handle for command to abort");
   3326 			break;
   3327 		}
   3328 		bus = XS_CHANNEL(xs);
   3329 		mbs.param[0] = MBOX_ABORT;
   3330 		if (IS_FC(isp)) {
   3331 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)  {
   3332 				mbs.param[1] = tgt << 8;
   3333 				mbs.param[4] = 0;
   3334 				mbs.param[5] = 0;
   3335 				mbs.param[6] = XS_LUN(xs);
   3336 			} else {
   3337 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
   3338 			}
   3339 		} else {
   3340 			mbs.param[1] =
   3341 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
   3342 		}
   3343 		mbs.param[3] = 0;
   3344 		mbs.param[2] = handle;
   3345 		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
   3346 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   3347 			return (0);
   3348 		}
   3349 		/*
   3350 		 * XXX: Look for command in the REQUEST QUEUE. That is,
   3351 		 * XXX: It hasen't been picked up by firmware yet.
   3352 		 */
   3353 		break;
   3354 
   3355 	case ISPCTL_UPDATE_PARAMS:
   3356 
   3357 		isp_update(isp);
   3358 		return (0);
   3359 
   3360 	case ISPCTL_FCLINK_TEST:
   3361 
   3362 		if (IS_FC(isp)) {
   3363 			int usdelay = (arg)? *((int *) arg) : 250000;
   3364 			return (isp_fclink_test(isp, usdelay));
   3365 		}
   3366 		break;
   3367 
   3368 	case ISPCTL_SCAN_FABRIC:
   3369 
   3370 		if (IS_FC(isp)) {
   3371 			int ftype = (arg)? *((int *) arg) : FC4_SCSI;
   3372 			return (isp_scan_fabric(isp, ftype));
   3373 		}
   3374 		break;
   3375 
   3376 	case ISPCTL_SCAN_LOOP:
   3377 
   3378 		if (IS_FC(isp)) {
   3379 			return (isp_scan_loop(isp));
   3380 		}
   3381 		break;
   3382 
   3383 	case ISPCTL_PDB_SYNC:
   3384 
   3385 		if (IS_FC(isp)) {
   3386 			return (isp_pdb_sync(isp));
   3387 		}
   3388 		break;
   3389 
   3390 	case ISPCTL_SEND_LIP:
   3391 
   3392 		if (IS_FC(isp)) {
   3393 			mbs.param[0] = MBOX_INIT_LIP;
   3394 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   3395 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   3396 				return (0);
   3397 			}
   3398 		}
   3399 		break;
   3400 
   3401 	case ISPCTL_GET_POSMAP:
   3402 
   3403 		if (IS_FC(isp) && arg) {
   3404 			return (isp_getmap(isp, arg));
   3405 		}
   3406 		break;
   3407 
   3408 	case ISPCTL_RUN_MBOXCMD:
   3409 
   3410 		isp_mboxcmd(isp, arg, MBLOGALL);
   3411 		return(0);
   3412 
   3413 #ifdef	ISP_TARGET_MODE
   3414 	case ISPCTL_TOGGLE_TMODE:
   3415 	{
   3416 
   3417 		/*
   3418 		 * We don't check/set against role here- that's the
   3419 		 * responsibility for the outer layer to coordinate.
   3420 		 */
   3421 		if (IS_SCSI(isp)) {
   3422 			int param = *(int *)arg;
   3423 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
   3424 			mbs.param[1] = param & 0xffff;
   3425 			mbs.param[2] = param >> 16;
   3426 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   3427 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3428 				break;
   3429 			}
   3430 		}
   3431 		return (0);
   3432 	}
   3433 #endif
   3434 	}
   3435 	return (-1);
   3436 }
   3437 
   3438 /*
   3439  * Interrupt Service Routine(s).
   3440  *
   3441  * External (OS) framework has done the appropriate locking,
   3442  * and the locking will be held throughout this function.
   3443  */
   3444 
   3445 /*
   3446  * Limit our stack depth by sticking with the max likely number
   3447  * of completions on a request queue at any one time.
   3448  */
   3449 #ifndef	MAX_REQUESTQ_COMPLETIONS
   3450 #define	MAX_REQUESTQ_COMPLETIONS	64
   3451 #endif
   3452 
   3453 void
   3454 isp_intr(struct ispsoftc *isp, u_int16_t isr, u_int16_t sema, u_int16_t mbox)
   3455 {
   3456 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
   3457 	u_int16_t iptr, optr, junk;
   3458 	int i, nlooked = 0, ndone = 0;
   3459 
   3460 again:
   3461 	/*
   3462 	 * Is this a mailbox related interrupt?
   3463 	 * The mailbox semaphore will be nonzero if so.
   3464 	 */
   3465 	if (sema) {
   3466 		if (mbox & 0x4000) {
   3467 			isp->isp_intmboxc++;
   3468 			if (isp->isp_mboxbsy) {
   3469 				int i = 0, obits = isp->isp_obits;
   3470 				isp->isp_mboxtmp[i++] = mbox;
   3471 				for (i = 1; i < MAX_MAILBOX; i++) {
   3472 					if ((obits & (1 << i)) == 0) {
   3473 						continue;
   3474 					}
   3475 					isp->isp_mboxtmp[i] =
   3476 					    ISP_READ(isp, MBOX_OFF(i));
   3477 				}
   3478 				if (isp->isp_mbxwrk0) {
   3479 					if (isp_mbox_continue(isp) == 0) {
   3480 						return;
   3481 					}
   3482 				}
   3483 				MBOX_NOTIFY_COMPLETE(isp);
   3484 			} else {
   3485 				isp_prt(isp, ISP_LOGWARN,
   3486 				    "Mbox Command Async (0x%x) with no waiters",
   3487 				    mbox);
   3488 			}
   3489 		} else if (isp_parse_async(isp, mbox) < 0) {
   3490 			return;
   3491 		}
   3492 		if (IS_FC(isp) || isp->isp_state != ISP_RUNSTATE) {
   3493 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3494 			ISP_WRITE(isp, BIU_SEMA, 0);
   3495 			return;
   3496 		}
   3497 	}
   3498 
   3499 	/*
   3500 	 * We can't be getting this now.
   3501 	 */
   3502 	if (isp->isp_state != ISP_RUNSTATE) {
   3503 		isp_prt(isp, ISP_LOGWARN,
   3504 		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
   3505 		/*
   3506 		 * Thank you very much!  *Burrrp*!
   3507 		 */
   3508 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
   3509 		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
   3510 
   3511 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3512 		ISP_WRITE(isp, BIU_SEMA, 0);
   3513 		return;
   3514 	}
   3515 
   3516 	/*
   3517 	 * Get the current Response Queue Out Pointer.
   3518 	 *
   3519 	 * If we're a 2300, we can ask what hardware what it thinks.
   3520 	 */
   3521 	if (IS_23XX(isp)) {
   3522 		optr = ISP_READ(isp, isp->isp_respoutrp);
   3523 		/*
   3524 		 * Debug: to be taken out eventually
   3525 		 */
   3526 		if (isp->isp_residx != optr) {
   3527 			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
   3528 			    optr, isp->isp_residx);
   3529 		}
   3530 	} else {
   3531 		optr = isp->isp_residx;
   3532 	}
   3533 
   3534 	/*
   3535 	 * You *must* read the Response Queue In Pointer
   3536 	 * prior to clearing the RISC interrupt.
   3537 	 *
   3538 	 * Debounce the 2300 if revision less than 2.
   3539 	 */
   3540 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
   3541 		i = 0;
   3542 		do {
   3543 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3544 			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3545 		} while (junk != iptr && ++i < 1000);
   3546 
   3547 		if (iptr != junk) {
   3548 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3549 			isp_prt(isp, ISP_LOGWARN,
   3550 			    "Response Queue Out Pointer Unstable (%x, %x)",
   3551 			    iptr, junk);
   3552 			return;
   3553 		}
   3554 	} else {
   3555 		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3556 	}
   3557 	isp->isp_resodx = iptr;
   3558 
   3559 
   3560 	if (optr == iptr && sema == 0) {
   3561 		/*
   3562 		 * There are a lot of these- reasons unknown- mostly on
   3563 		 * faster Alpha machines.
   3564 		 *
   3565 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
   3566 		 * make sure the old interrupt went away (to avoid 'ringing'
   3567 		 * effects), but that didn't stop this from occurring.
   3568 		 */
   3569 		if (IS_23XX(isp)) {
   3570 			USEC_DELAY(100);
   3571 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3572 			junk = ISP_READ(isp, BIU_R2HSTSLO);
   3573 		} else {
   3574 			junk = ISP_READ(isp, BIU_ISR);
   3575 		}
   3576 		if (optr == iptr) {
   3577 			if (IS_23XX(isp)) {
   3578 				;
   3579 			} else {
   3580 				sema = ISP_READ(isp, BIU_SEMA);
   3581 				mbox = ISP_READ(isp, OUTMAILBOX0);
   3582 				if ((sema & 0x3) && (mbox & 0x8000)) {
   3583 					goto again;
   3584 				}
   3585 			}
   3586 			isp->isp_intbogus++;
   3587 			isp_prt(isp, ISP_LOGDEBUG1,
   3588 			    "bogus intr- isr %x (%x) iptr %x optr %x",
   3589 			    isr, junk, iptr, optr);
   3590 		}
   3591 	}
   3592 	isp->isp_resodx = iptr;
   3593 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3594 	ISP_WRITE(isp, BIU_SEMA, 0);
   3595 
   3596 	if (isp->isp_rspbsy) {
   3597 		return;
   3598 	}
   3599 	isp->isp_rspbsy = 1;
   3600 
   3601 	while (optr != iptr) {
   3602 		ispstatusreq_t local, *sp = &local;
   3603 		isphdr_t *hp;
   3604 		int type;
   3605 		u_int16_t oop;
   3606 		int buddaboom = 0;
   3607 
   3608 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   3609 		oop = optr;
   3610 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
   3611 		nlooked++;
   3612 		/*
   3613 		 * Synchronize our view of this response queue entry.
   3614 		 */
   3615 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
   3616 
   3617 		type = isp_get_response_type(isp, hp);
   3618 
   3619 		if (type == RQSTYPE_RESPONSE) {
   3620 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
   3621 		} else if (type == RQSTYPE_RIO2) {
   3622 			isp_rio2_t rio;
   3623 			isp_get_rio2(isp, (isp_rio2_t *) hp, &rio);
   3624 			for (i = 0; i < rio.req_header.rqs_seqno; i++) {
   3625 				isp_fastpost_complete(isp, rio.req_handles[i]);
   3626 			}
   3627 			if (isp->isp_fpcchiwater < rio.req_header.rqs_seqno)
   3628 				isp->isp_fpcchiwater = rio.req_header.rqs_seqno;
   3629 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3630 			continue;
   3631 		} else {
   3632 			/*
   3633 			 * Somebody reachable via isp_handle_other_response
   3634 			 * may have updated the response queue pointers for
   3635 			 * us, so we reload our goal index.
   3636 			 */
   3637 			if (isp_handle_other_response(isp, type, hp, &optr)) {
   3638 				iptr = isp->isp_resodx;
   3639 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3640 				continue;
   3641 			}
   3642 
   3643 			/*
   3644 			 * After this point, we'll just look at the header as
   3645 			 * we don't know how to deal with the rest of the
   3646 			 * response.
   3647 			 */
   3648 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
   3649 
   3650 			/*
   3651 			 * It really has to be a bounced request just copied
   3652 			 * from the request queue to the response queue. If
   3653 			 * not, something bad has happened.
   3654 			 */
   3655 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
   3656 				isp_prt(isp, ISP_LOGERR, notresp,
   3657 				    sp->req_header.rqs_entry_type, oop, optr,
   3658 				    nlooked);
   3659 				if (isp->isp_dblev & ISP_LOGDEBUG0) {
   3660 					isp_print_bytes(isp, "Queue Entry",
   3661 					    QENTRY_LEN, sp);
   3662 				}
   3663 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3664 				continue;
   3665 			}
   3666 			buddaboom = 1;
   3667 		}
   3668 
   3669 		if (sp->req_header.rqs_flags & 0xf) {
   3670 #define	_RQS_OFLAGS	\
   3671 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
   3672 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   3673 				isp_prt(isp, ISP_LOGWARN,
   3674 				    "continuation segment");
   3675 				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3676 				continue;
   3677 			}
   3678 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   3679 				isp_prt(isp, ISP_LOGDEBUG1,
   3680 				    "internal queues full");
   3681 				/*
   3682 				 * We'll synthesize a QUEUE FULL message below.
   3683 				 */
   3684 			}
   3685 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   3686 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
   3687 				buddaboom++;
   3688 			}
   3689 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   3690 				isp_prt(isp, ISP_LOGERR, "bad request packet");
   3691 				buddaboom++;
   3692 			}
   3693 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
   3694 				isp_prt(isp, ISP_LOGERR,
   3695 				    "unknown flags (0x%x) in response",
   3696 				    sp->req_header.rqs_flags);
   3697 				buddaboom++;
   3698 			}
   3699 #undef	_RQS_OFLAGS
   3700 		}
   3701 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
   3702 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3703 			isp_prt(isp, ISP_LOGERR,
   3704 			    "bad request handle %d (type 0x%x, flags 0x%x)",
   3705 			    sp->req_handle, sp->req_header.rqs_entry_type,
   3706 			    sp->req_header.rqs_flags);
   3707 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3708 			continue;
   3709 		}
   3710 		xs = isp_find_xs(isp, sp->req_handle);
   3711 		if (xs == NULL) {
   3712 			u_int8_t ts = sp->req_completion_status & 0xff;
   3713 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3714 			/*
   3715 			 * Only whine if this isn't the expected fallout of
   3716 			 * aborting the command.
   3717 			 */
   3718 			if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
   3719 				isp_prt(isp, ISP_LOGERR,
   3720 				    "cannot find handle 0x%x (type 0x%x)",
   3721 				    sp->req_handle,
   3722 				    sp->req_header.rqs_entry_type);
   3723 			} else if (ts != RQCS_ABORTED) {
   3724 				isp_prt(isp, ISP_LOGERR,
   3725 				    "cannot find handle 0x%x (status 0x%x)",
   3726 				    sp->req_handle, ts);
   3727 			}
   3728 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3729 			continue;
   3730 		}
   3731 		isp_destroy_handle(isp, sp->req_handle);
   3732 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   3733 			XS_SETERR(xs, HBA_BUSRESET);
   3734 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3735 		}
   3736 		if (buddaboom) {
   3737 			XS_SETERR(xs, HBA_BOTCH);
   3738 		}
   3739 
   3740 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
   3741 			/*
   3742 			 * Fibre Channel F/W doesn't say we got status
   3743 			 * if there's Sense Data instead. I guess they
   3744 			 * think it goes w/o saying.
   3745 			 */
   3746 			sp->req_state_flags |= RQSF_GOT_STATUS;
   3747 		}
   3748 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   3749 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
   3750 		}
   3751 
   3752 		switch (sp->req_header.rqs_entry_type) {
   3753 		case RQSTYPE_RESPONSE:
   3754 			XS_SET_STATE_STAT(isp, xs, sp);
   3755 			isp_parse_status(isp, sp, xs);
   3756 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
   3757 			    (*XS_STSP(xs) == SCSI_BUSY)) {
   3758 				XS_SETERR(xs, HBA_TGTBSY);
   3759 			}
   3760 			if (IS_SCSI(isp)) {
   3761 				XS_RESID(xs) = sp->req_resid;
   3762 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   3763 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   3764 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
   3765 					XS_SAVE_SENSE(xs, sp);
   3766 				}
   3767 				/*
   3768 				 * A new synchronous rate was negotiated for
   3769 				 * this target. Mark state such that we'll go
   3770 				 * look up that which has changed later.
   3771 				 */
   3772 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   3773 					int t = XS_TGT(xs);
   3774 					sdparam *sdp = isp->isp_param;
   3775 					sdp += XS_CHANNEL(xs);
   3776 					sdp->isp_devparam[t].dev_refresh = 1;
   3777 					isp->isp_update |=
   3778 					    (1 << XS_CHANNEL(xs));
   3779 				}
   3780 			} else {
   3781 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
   3782 					XS_RESID(xs) = 0;
   3783 				} else if (sp->req_scsi_status & RQCS_RESID) {
   3784 					XS_RESID(xs) = sp->req_resid;
   3785 				} else {
   3786 					XS_RESID(xs) = 0;
   3787 				}
   3788 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   3789 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   3790 				    (sp->req_scsi_status & RQCS_SV)) {
   3791 					XS_SAVE_SENSE(xs, sp);
   3792 					/* solely for the benefit of debug */
   3793 					sp->req_state_flags |= RQSF_GOT_SENSE;
   3794 				}
   3795 			}
   3796 			isp_prt(isp, ISP_LOGDEBUG2,
   3797 			   "asked for %ld got resid %ld", (long) XS_XFRLEN(xs),
   3798 			   (long) sp->req_resid);
   3799 			break;
   3800 		case RQSTYPE_REQUEST:
   3801 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   3802 				/*
   3803 				 * Force Queue Full status.
   3804 				 */
   3805 				*XS_STSP(xs) = SCSI_QFULL;
   3806 				XS_SETERR(xs, HBA_NOERROR);
   3807 			} else if (XS_NOERR(xs)) {
   3808 				/*
   3809 				 * ????
   3810 				 */
   3811 				isp_prt(isp, ISP_LOGDEBUG0,
   3812 				    "Request Queue Entry bounced back");
   3813 				XS_SETERR(xs, HBA_BOTCH);
   3814 			}
   3815 			XS_RESID(xs) = XS_XFRLEN(xs);
   3816 			break;
   3817 		default:
   3818 			isp_prt(isp, ISP_LOGWARN,
   3819 			    "unhandled response queue type 0x%x",
   3820 			    sp->req_header.rqs_entry_type);
   3821 			if (XS_NOERR(xs)) {
   3822 				XS_SETERR(xs, HBA_BOTCH);
   3823 			}
   3824 			break;
   3825 		}
   3826 
   3827 		/*
   3828 		 * Free any dma resources. As a side effect, this may
   3829 		 * also do any cache flushing necessary for data coherence.			 */
   3830 		if (XS_XFRLEN(xs)) {
   3831 			ISP_DMAFREE(isp, xs, sp->req_handle);
   3832 		}
   3833 
   3834 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
   3835 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
   3836 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
   3837 			char skey;
   3838 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   3839 				skey = XS_SNSKEY(xs) & 0xf;
   3840 				if (skey < 10)
   3841 					skey += '0';
   3842 				else
   3843 					skey += 'a' - 10;
   3844 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
   3845 				skey = '?';
   3846 			} else {
   3847 				skey = '.';
   3848 			}
   3849 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
   3850 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
   3851 			    *XS_STSP(xs), skey, XS_ERR(xs));
   3852 		}
   3853 
   3854 		if (isp->isp_nactive > 0)
   3855 		    isp->isp_nactive--;
   3856 		complist[ndone++] = xs;	/* defer completion call until later */
   3857 		MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3858 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
   3859 			break;
   3860 		}
   3861 	}
   3862 
   3863 	/*
   3864 	 * If we looked at any commands, then it's valid to find out
   3865 	 * what the outpointer is. It also is a trigger to update the
   3866 	 * ISP's notion of what we've seen so far.
   3867 	 */
   3868 	if (nlooked) {
   3869 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3870 		/*
   3871 		 * While we're at it, read the requst queue out pointer.
   3872 		 */
   3873 		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
   3874 		if (isp->isp_rscchiwater < ndone)
   3875 			isp->isp_rscchiwater = ndone;
   3876 	}
   3877 
   3878 	isp->isp_residx = optr;
   3879 	isp->isp_rspbsy = 0;
   3880 	for (i = 0; i < ndone; i++) {
   3881 		xs = complist[i];
   3882 		if (xs) {
   3883 			isp->isp_rsltccmplt++;
   3884 			isp_done(xs);
   3885 		}
   3886 	}
   3887 }
   3888 
   3889 /*
   3890  * Support routines.
   3891  */
   3892 
   3893 static int
   3894 isp_parse_async(struct ispsoftc *isp, u_int16_t mbox)
   3895 {
   3896 	int rval = 0;
   3897 	int bus;
   3898 
   3899 	if (IS_DUALBUS(isp)) {
   3900 		bus = ISP_READ(isp, OUTMAILBOX6);
   3901 	} else {
   3902 		bus = 0;
   3903 	}
   3904 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
   3905 
   3906 	switch (mbox) {
   3907 	case ASYNC_BUS_RESET:
   3908 		isp->isp_sendmarker |= (1 << bus);
   3909 #ifdef	ISP_TARGET_MODE
   3910 		if (isp_target_async(isp, bus, mbox))
   3911 			rval = -1;
   3912 #endif
   3913 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
   3914 		break;
   3915 	case ASYNC_SYSTEM_ERROR:
   3916 #ifdef	ISP_FW_CRASH_DUMP
   3917 		/*
   3918 		 * If we have crash dumps enabled, it's up to the handler
   3919 		 * for isp_async to reinit stuff and restart the firmware
   3920 		 * after performing the crash dump. The reason we do things
   3921 		 * this way is that we may need to activate a kernel thread
   3922 		 * to do all the crash dump goop.
   3923 		 */
   3924 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
   3925 #else
   3926 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
   3927 		isp_reinit(isp);
   3928 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
   3929 #endif
   3930 		rval = -1;
   3931 		break;
   3932 
   3933 	case ASYNC_RQS_XFER_ERR:
   3934 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
   3935 		break;
   3936 
   3937 	case ASYNC_RSP_XFER_ERR:
   3938 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
   3939 		break;
   3940 
   3941 	case ASYNC_QWAKEUP:
   3942 		/*
   3943 		 * We've just been notified that the Queue has woken up.
   3944 		 * We don't need to be chatty about this- just unlatch things
   3945 		 * and move on.
   3946 		 */
   3947 		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
   3948 		break;
   3949 
   3950 	case ASYNC_TIMEOUT_RESET:
   3951 		isp_prt(isp, ISP_LOGWARN,
   3952 		    "timeout initiated SCSI bus reset of bus %d", bus);
   3953 		isp->isp_sendmarker |= (1 << bus);
   3954 #ifdef	ISP_TARGET_MODE
   3955 		if (isp_target_async(isp, bus, mbox))
   3956 			rval = -1;
   3957 #endif
   3958 		break;
   3959 
   3960 	case ASYNC_DEVICE_RESET:
   3961 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
   3962 		isp->isp_sendmarker |= (1 << bus);
   3963 #ifdef	ISP_TARGET_MODE
   3964 		if (isp_target_async(isp, bus, mbox))
   3965 			rval = -1;
   3966 #endif
   3967 		break;
   3968 
   3969 	case ASYNC_EXTMSG_UNDERRUN:
   3970 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
   3971 		break;
   3972 
   3973 	case ASYNC_SCAM_INT:
   3974 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
   3975 		break;
   3976 
   3977 	case ASYNC_HUNG_SCSI:
   3978 		isp_prt(isp, ISP_LOGERR,
   3979 		    "stalled SCSI Bus after DATA Overrun");
   3980 		/* XXX: Need to issue SCSI reset at this point */
   3981 		break;
   3982 
   3983 	case ASYNC_KILLED_BUS:
   3984 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
   3985 		break;
   3986 
   3987 	case ASYNC_BUS_TRANSIT:
   3988 		mbox = ISP_READ(isp, OUTMAILBOX2);
   3989 		switch (mbox & 0x1c00) {
   3990 		case SXP_PINS_LVD_MODE:
   3991 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
   3992 			SDPARAM(isp)->isp_diffmode = 0;
   3993 			SDPARAM(isp)->isp_ultramode = 0;
   3994 			SDPARAM(isp)->isp_lvdmode = 1;
   3995 			break;
   3996 		case SXP_PINS_HVD_MODE:
   3997 			isp_prt(isp, ISP_LOGINFO,
   3998 			    "Transition to Differential mode");
   3999 			SDPARAM(isp)->isp_diffmode = 1;
   4000 			SDPARAM(isp)->isp_ultramode = 0;
   4001 			SDPARAM(isp)->isp_lvdmode = 0;
   4002 			break;
   4003 		case SXP_PINS_SE_MODE:
   4004 			isp_prt(isp, ISP_LOGINFO,
   4005 			    "Transition to Single Ended mode");
   4006 			SDPARAM(isp)->isp_diffmode = 0;
   4007 			SDPARAM(isp)->isp_ultramode = 1;
   4008 			SDPARAM(isp)->isp_lvdmode = 0;
   4009 			break;
   4010 		default:
   4011 			isp_prt(isp, ISP_LOGWARN,
   4012 			    "Transition to Unknown Mode 0x%x", mbox);
   4013 			break;
   4014 		}
   4015 		/*
   4016 		 * XXX: Set up to renegotiate again!
   4017 		 */
   4018 		/* Can only be for a 1080... */
   4019 		isp->isp_sendmarker |= (1 << bus);
   4020 		break;
   4021 
   4022 	/*
   4023 	 * We can use bus, which will always be zero for FC cards,
   4024 	 * as a mailbox pattern accumulator to be checked below.
   4025 	 */
   4026 	case ASYNC_RIO5:
   4027 		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
   4028 		break;
   4029 
   4030 	case ASYNC_RIO4:
   4031 		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
   4032 		break;
   4033 
   4034 	case ASYNC_RIO3:
   4035 		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
   4036 		break;
   4037 
   4038 	case ASYNC_RIO2:
   4039 		bus = 0x106;	/* outgoing mailbox regs 1-2 */
   4040 		break;
   4041 
   4042 	case ASYNC_RIO1:
   4043 	case ASYNC_CMD_CMPLT:
   4044 		bus = 0x102;	/* outgoing mailbox regs 1 */
   4045 		break;
   4046 
   4047 	case ASYNC_RIO_RESP:
   4048 		break;
   4049 
   4050 	case ASYNC_CTIO_DONE:
   4051 	{
   4052 #ifdef	ISP_TARGET_MODE
   4053 		int handle =
   4054 		    (ISP_READ(isp, OUTMAILBOX2) << 16) |
   4055 		    (ISP_READ(isp, OUTMAILBOX1));
   4056 		if (isp_target_async(isp, handle, mbox))
   4057 			rval = -1;
   4058 #else
   4059 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
   4060 #endif
   4061 		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
   4062 		break;
   4063 	}
   4064 	case ASYNC_LIP_F8:
   4065 	case ASYNC_LIP_OCCURRED:
   4066 		FCPARAM(isp)->isp_lipseq =
   4067 		    ISP_READ(isp, OUTMAILBOX1);
   4068 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4069 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   4070 		isp->isp_sendmarker = 1;
   4071 		isp_mark_getpdb_all(isp);
   4072 		isp_async(isp, ISPASYNC_LIP, NULL);
   4073 #ifdef	ISP_TARGET_MODE
   4074 		if (isp_target_async(isp, bus, mbox))
   4075 			rval = -1;
   4076 #endif
   4077 		/*
   4078 		 * We've had problems with data corruption occuring on
   4079 		 * commands that complete (with no apparent error) after
   4080 		 * we receive a LIP. This has been observed mostly on
   4081 		 * Local Loop topologies. To be safe, let's just mark
   4082 		 * all active commands as dead.
   4083 		 */
   4084 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
   4085 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
   4086 			int i, j;
   4087 			for (i = j = 0; i < isp->isp_maxcmds; i++) {
   4088 				XS_T *xs;
   4089 				xs = isp->isp_xflist[i];
   4090 				if (xs != NULL) {
   4091 					j++;
   4092 					XS_SETERR(xs, HBA_BUSRESET);
   4093 				}
   4094 			}
   4095 			if (j) {
   4096 				isp_prt(isp, ISP_LOGERR,
   4097 				    "LIP destroyed %d active commands", j);
   4098 			}
   4099 		}
   4100 		break;
   4101 
   4102 	case ASYNC_LOOP_UP:
   4103 		isp->isp_sendmarker = 1;
   4104 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4105 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   4106 		isp_mark_getpdb_all(isp);
   4107 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
   4108 #ifdef	ISP_TARGET_MODE
   4109 		if (isp_target_async(isp, bus, mbox))
   4110 			rval = -1;
   4111 #endif
   4112 		break;
   4113 
   4114 	case ASYNC_LOOP_DOWN:
   4115 		isp->isp_sendmarker = 1;
   4116 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4117 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   4118 		isp_mark_getpdb_all(isp);
   4119 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
   4120 #ifdef	ISP_TARGET_MODE
   4121 		if (isp_target_async(isp, bus, mbox))
   4122 			rval = -1;
   4123 #endif
   4124 		break;
   4125 
   4126 	case ASYNC_LOOP_RESET:
   4127 		isp->isp_sendmarker = 1;
   4128 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4129 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   4130 		isp_mark_getpdb_all(isp);
   4131 		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
   4132 #ifdef	ISP_TARGET_MODE
   4133 		if (isp_target_async(isp, bus, mbox))
   4134 			rval = -1;
   4135 #endif
   4136 		break;
   4137 
   4138 	case ASYNC_PDB_CHANGED:
   4139 		isp->isp_sendmarker = 1;
   4140 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   4141 		isp_mark_getpdb_all(isp);
   4142 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
   4143 		break;
   4144 
   4145 	case ASYNC_CHANGE_NOTIFY:
   4146 		/*
   4147 		 * Not correct, but it will force us to rescan the loop.
   4148 		 */
   4149 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   4150 		isp_mark_getpdb_all(isp);
   4151 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
   4152 		break;
   4153 
   4154 	case ASYNC_PTPMODE:
   4155 		if (FCPARAM(isp)->isp_onfabric)
   4156 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
   4157 		else
   4158 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
   4159 		isp_mark_getpdb_all(isp);
   4160 		isp->isp_sendmarker = 1;
   4161 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4162 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   4163 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   4164 #ifdef	ISP_TARGET_MODE
   4165 		if (isp_target_async(isp, bus, mbox))
   4166 			rval = -1;
   4167 #endif
   4168 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
   4169 		break;
   4170 
   4171 	case ASYNC_CONNMODE:
   4172 		mbox = ISP_READ(isp, OUTMAILBOX1);
   4173 		isp_mark_getpdb_all(isp);
   4174 		switch (mbox) {
   4175 		case ISP_CONN_LOOP:
   4176 			isp_prt(isp, ISP_LOGINFO,
   4177 			    "Point-to-Point -> Loop mode");
   4178 			break;
   4179 		case ISP_CONN_PTP:
   4180 			isp_prt(isp, ISP_LOGINFO,
   4181 			    "Loop -> Point-to-Point mode");
   4182 			break;
   4183 		case ISP_CONN_BADLIP:
   4184 			isp_prt(isp, ISP_LOGWARN,
   4185 			    "Point-to-Point -> Loop mode (BAD LIP)");
   4186 			break;
   4187 		case ISP_CONN_FATAL:
   4188 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
   4189 			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
   4190 			isp_reinit(isp);
   4191 			isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
   4192 			return (-1);
   4193 		case ISP_CONN_LOOPBACK:
   4194 			isp_prt(isp, ISP_LOGWARN,
   4195 			    "Looped Back in Point-to-Point mode");
   4196 			break;
   4197 		default:
   4198 			isp_prt(isp, ISP_LOGWARN,
   4199 			    "Unknown connection mode (0x%x)", mbox);
   4200 			break;
   4201 		}
   4202 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   4203 		isp->isp_sendmarker = 1;
   4204 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4205 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   4206 		break;
   4207 
   4208 	default:
   4209 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
   4210 		break;
   4211 	}
   4212 
   4213 	if (bus & 0x100) {
   4214 		int i, nh;
   4215 		u_int16_t handles[5];
   4216 
   4217 		for (nh = 0, i = 1; i < MAX_MAILBOX; i++) {
   4218 			if ((bus & (1 << i)) == 0) {
   4219 				continue;
   4220 			}
   4221 			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
   4222 		}
   4223 		for (i = 0; i < nh; i++) {
   4224 			isp_fastpost_complete(isp, handles[i]);
   4225 			isp_prt(isp,  ISP_LOGDEBUG3,
   4226 			    "fast post completion of %u", handles[i]);
   4227 		}
   4228 		if (isp->isp_fpcchiwater < nh)
   4229 			isp->isp_fpcchiwater = nh;
   4230 	} else {
   4231 		isp->isp_intoasync++;
   4232 	}
   4233 	return (rval);
   4234 }
   4235 
   4236 /*
   4237  * Handle other response entries. A pointer to the request queue output
   4238  * index is here in case we want to eat several entries at once, although
   4239  * this is not used currently.
   4240  */
   4241 
   4242 static int
   4243 isp_handle_other_response(struct ispsoftc *isp, int type,
   4244     isphdr_t *hp, u_int16_t *optrp)
   4245 {
   4246 	switch (type) {
   4247 	case RQSTYPE_STATUS_CONT:
   4248 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
   4249 		return (1);
   4250 	case RQSTYPE_ATIO:
   4251 	case RQSTYPE_CTIO:
   4252 	case RQSTYPE_ENABLE_LUN:
   4253 	case RQSTYPE_MODIFY_LUN:
   4254 	case RQSTYPE_NOTIFY:
   4255 	case RQSTYPE_NOTIFY_ACK:
   4256 	case RQSTYPE_CTIO1:
   4257 	case RQSTYPE_ATIO2:
   4258 	case RQSTYPE_CTIO2:
   4259 	case RQSTYPE_CTIO3:
   4260 		isp->isp_rsltccmplt++;	/* count as a response completion */
   4261 #ifdef	ISP_TARGET_MODE
   4262 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
   4263 			return (1);
   4264 		}
   4265 #else
   4266 		optrp = optrp;
   4267 		/* FALLTHROUGH */
   4268 #endif
   4269 	case RQSTYPE_REQUEST:
   4270 	default:
   4271 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
   4272 			return (1);
   4273 		}
   4274 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
   4275 		    isp_get_response_type(isp, hp));
   4276 		return (0);
   4277 	}
   4278 }
   4279 
   4280 static void
   4281 isp_parse_status(struct ispsoftc *isp, ispstatusreq_t *sp, XS_T *xs)
   4282 {
   4283 	switch (sp->req_completion_status & 0xff) {
   4284 	case RQCS_COMPLETE:
   4285 		if (XS_NOERR(xs)) {
   4286 			XS_SETERR(xs, HBA_NOERROR);
   4287 		}
   4288 		return;
   4289 
   4290 	case RQCS_INCOMPLETE:
   4291 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   4292 			isp_prt(isp, ISP_LOGDEBUG1,
   4293 			    "Selection Timeout for %d.%d.%d",
   4294 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4295 			if (XS_NOERR(xs)) {
   4296 				XS_SETERR(xs, HBA_SELTIMEOUT);
   4297 			}
   4298 			return;
   4299 		}
   4300 		isp_prt(isp, ISP_LOGERR,
   4301 		    "command incomplete for %d.%d.%d, state 0x%x",
   4302 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
   4303 		    sp->req_state_flags);
   4304 		break;
   4305 
   4306 	case RQCS_DMA_ERROR:
   4307 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
   4308 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4309 		break;
   4310 
   4311 	case RQCS_TRANSPORT_ERROR:
   4312 	{
   4313 		char buf[172];
   4314 		buf[0] = 0;
   4315 		STRNCAT(buf, "states=>", sizeof buf);
   4316 		if (sp->req_state_flags & RQSF_GOT_BUS) {
   4317 			STRNCAT(buf, " GOT_BUS", sizeof buf);
   4318 		}
   4319 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
   4320 			STRNCAT(buf, " GOT_TGT", sizeof buf);
   4321 		}
   4322 		if (sp->req_state_flags & RQSF_SENT_CDB) {
   4323 			STRNCAT(buf, " SENT_CDB", sizeof buf);
   4324 		}
   4325 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
   4326 			STRNCAT(buf, " XFRD_DATA", sizeof buf);
   4327 		}
   4328 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   4329 			STRNCAT(buf, " GOT_STS", sizeof buf);
   4330 		}
   4331 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
   4332 			STRNCAT(buf, " GOT_SNS", sizeof buf);
   4333 		}
   4334 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
   4335 			STRNCAT(buf, " XFR_CMPLT", sizeof buf);
   4336 		}
   4337 		STRNCAT(buf, "\nstatus=>", sizeof buf);
   4338 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
   4339 			STRNCAT(buf, " Disconnect", sizeof buf);
   4340 		}
   4341 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
   4342 			STRNCAT(buf, " Sync_xfr", sizeof buf);
   4343 		}
   4344 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
   4345 			STRNCAT(buf, " Parity", sizeof buf);
   4346 		}
   4347 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   4348 			STRNCAT(buf, " Bus_Reset", sizeof buf);
   4349 		}
   4350 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
   4351 			STRNCAT(buf, " Device_Reset", sizeof buf);
   4352 		}
   4353 		if (sp->req_status_flags & RQSTF_ABORTED) {
   4354 			STRNCAT(buf, " Aborted", sizeof buf);
   4355 		}
   4356 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
   4357 			STRNCAT(buf, " Timeout", sizeof buf);
   4358 		}
   4359 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   4360 			STRNCAT(buf, " Negotiation", sizeof buf);
   4361 		}
   4362 		isp_prt(isp, ISP_LOGERR, "%s", buf);
   4363 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
   4364 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
   4365 		break;
   4366 	}
   4367 	case RQCS_RESET_OCCURRED:
   4368 		isp_prt(isp, ISP_LOGWARN,
   4369 		    "bus reset destroyed command for %d.%d.%d",
   4370 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4371 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   4372 		if (XS_NOERR(xs)) {
   4373 			XS_SETERR(xs, HBA_BUSRESET);
   4374 		}
   4375 		return;
   4376 
   4377 	case RQCS_ABORTED:
   4378 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
   4379 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4380 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   4381 		if (XS_NOERR(xs)) {
   4382 			XS_SETERR(xs, HBA_ABORTED);
   4383 		}
   4384 		return;
   4385 
   4386 	case RQCS_TIMEOUT:
   4387 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
   4388 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4389 		/*
   4390 	 	 * Check to see if we logged out the device.
   4391 		 */
   4392 		if (IS_FC(isp)) {
   4393 			if ((sp->req_completion_status & RQSTF_LOGOUT) &&
   4394 			    FCPARAM(isp)->portdb[XS_TGT(xs)].valid &&
   4395 			    FCPARAM(isp)->portdb[XS_TGT(xs)].fabric_dev) {
   4396 				FCPARAM(isp)->portdb[XS_TGT(xs)].relogin = 1;
   4397 			}
   4398 		}
   4399 		if (XS_NOERR(xs)) {
   4400 			XS_SETERR(xs, HBA_CMDTIMEOUT);
   4401 		}
   4402 		return;
   4403 
   4404 	case RQCS_DATA_OVERRUN:
   4405 		XS_RESID(xs) = sp->req_resid;
   4406 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
   4407 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4408 		if (XS_NOERR(xs)) {
   4409 			XS_SETERR(xs, HBA_DATAOVR);
   4410 		}
   4411 		return;
   4412 
   4413 	case RQCS_COMMAND_OVERRUN:
   4414 		isp_prt(isp, ISP_LOGERR,
   4415 		    "command overrun for command on %d.%d.%d",
   4416 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4417 		break;
   4418 
   4419 	case RQCS_STATUS_OVERRUN:
   4420 		isp_prt(isp, ISP_LOGERR,
   4421 		    "status overrun for command on %d.%d.%d",
   4422 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4423 		break;
   4424 
   4425 	case RQCS_BAD_MESSAGE:
   4426 		isp_prt(isp, ISP_LOGERR,
   4427 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
   4428 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4429 		break;
   4430 
   4431 	case RQCS_NO_MESSAGE_OUT:
   4432 		isp_prt(isp, ISP_LOGERR,
   4433 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
   4434 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4435 		break;
   4436 
   4437 	case RQCS_EXT_ID_FAILED:
   4438 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
   4439 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4440 		break;
   4441 
   4442 	case RQCS_IDE_MSG_FAILED:
   4443 		isp_prt(isp, ISP_LOGERR,
   4444 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
   4445 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4446 		break;
   4447 
   4448 	case RQCS_ABORT_MSG_FAILED:
   4449 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
   4450 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4451 		break;
   4452 
   4453 	case RQCS_REJECT_MSG_FAILED:
   4454 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
   4455 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4456 		break;
   4457 
   4458 	case RQCS_NOP_MSG_FAILED:
   4459 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
   4460 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4461 		break;
   4462 
   4463 	case RQCS_PARITY_ERROR_MSG_FAILED:
   4464 		isp_prt(isp, ISP_LOGERR,
   4465 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
   4466 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4467 		break;
   4468 
   4469 	case RQCS_DEVICE_RESET_MSG_FAILED:
   4470 		isp_prt(isp, ISP_LOGWARN,
   4471 		    "BUS DEVICE RESET rejected by %d.%d.%d",
   4472 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4473 		break;
   4474 
   4475 	case RQCS_ID_MSG_FAILED:
   4476 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
   4477 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4478 		break;
   4479 
   4480 	case RQCS_UNEXP_BUS_FREE:
   4481 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
   4482 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4483 		break;
   4484 
   4485 	case RQCS_DATA_UNDERRUN:
   4486 	{
   4487 		if (IS_FC(isp)) {
   4488 			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
   4489 			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
   4490 				isp_prt(isp, ISP_LOGWARN, bun, XS_TGT(xs),
   4491 				    XS_LUN(xs), XS_XFRLEN(xs), sp->req_resid,
   4492 				    (ru_marked)? "marked" : "not marked");
   4493 				if (XS_NOERR(xs)) {
   4494 					XS_SETERR(xs, HBA_BOTCH);
   4495 				}
   4496 				return;
   4497 			}
   4498 		}
   4499 		XS_RESID(xs) = sp->req_resid;
   4500 		if (XS_NOERR(xs)) {
   4501 			XS_SETERR(xs, HBA_NOERROR);
   4502 		}
   4503 		return;
   4504 	}
   4505 
   4506 	case RQCS_XACT_ERR1:
   4507 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
   4508 		    XS_TGT(xs), XS_LUN(xs));
   4509 		break;
   4510 
   4511 	case RQCS_XACT_ERR2:
   4512 		isp_prt(isp, ISP_LOGERR, xact2,
   4513 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
   4514 		break;
   4515 
   4516 	case RQCS_XACT_ERR3:
   4517 		isp_prt(isp, ISP_LOGERR, xact3,
   4518 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4519 		break;
   4520 
   4521 	case RQCS_BAD_ENTRY:
   4522 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
   4523 		break;
   4524 
   4525 	case RQCS_QUEUE_FULL:
   4526 		isp_prt(isp, ISP_LOGDEBUG0,
   4527 		    "internal queues full for %d.%d.%d status 0x%x",
   4528 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), *XS_STSP(xs));
   4529 
   4530 		/*
   4531 		 * If QFULL or some other status byte is set, then this
   4532 		 * isn't an error, per se.
   4533 		 *
   4534 		 * Unfortunately, some QLogic f/w writers have, in
   4535 		 * some cases, ommitted to *set* status to QFULL.
   4536 		 *
   4537 
   4538 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
   4539 			XS_SETERR(xs, HBA_NOERROR);
   4540 			return;
   4541 		}
   4542 
   4543 		 *
   4544 		 *
   4545 		 */
   4546 
   4547 		*XS_STSP(xs) = SCSI_QFULL;
   4548 		XS_SETERR(xs, HBA_NOERROR);
   4549 		return;
   4550 
   4551 	case RQCS_PHASE_SKIPPED:
   4552 		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
   4553 		    XS_TGT(xs), XS_LUN(xs));
   4554 		break;
   4555 
   4556 	case RQCS_ARQS_FAILED:
   4557 		isp_prt(isp, ISP_LOGERR,
   4558 		    "Auto Request Sense failed for %d.%d.%d",
   4559 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4560 		if (XS_NOERR(xs)) {
   4561 			XS_SETERR(xs, HBA_ARQFAIL);
   4562 		}
   4563 		return;
   4564 
   4565 	case RQCS_WIDE_FAILED:
   4566 		isp_prt(isp, ISP_LOGERR,
   4567 		    "Wide Negotiation failed for %d.%d.%d",
   4568 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   4569 		if (IS_SCSI(isp)) {
   4570 			sdparam *sdp = isp->isp_param;
   4571 			sdp += XS_CHANNEL(xs);
   4572 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
   4573 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   4574 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   4575 		}
   4576 		if (XS_NOERR(xs)) {
   4577 			XS_SETERR(xs, HBA_NOERROR);
   4578 		}
   4579 		return;
   4580 
   4581 	case RQCS_SYNCXFER_FAILED:
   4582 		isp_prt(isp, ISP_LOGERR,
   4583 		    "SDTR Message failed for target %d.%d.%d",
   4584 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   4585 		if (IS_SCSI(isp)) {
   4586 			sdparam *sdp = isp->isp_param;
   4587 			sdp += XS_CHANNEL(xs);
   4588 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
   4589 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   4590 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   4591 		}
   4592 		break;
   4593 
   4594 	case RQCS_LVD_BUSERR:
   4595 		isp_prt(isp, ISP_LOGERR,
   4596 		    "Bad LVD condition while talking to %d.%d.%d",
   4597 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   4598 		break;
   4599 
   4600 	case RQCS_PORT_UNAVAILABLE:
   4601 		/*
   4602 		 * No such port on the loop. Moral equivalent of SELTIMEO
   4603 		 */
   4604 	case RQCS_PORT_LOGGED_OUT:
   4605 		/*
   4606 		 * It was there (maybe)- treat as a selection timeout.
   4607 		 */
   4608 		if ((sp->req_completion_status & 0xff) == RQCS_PORT_UNAVAILABLE)
   4609 			isp_prt(isp, ISP_LOGINFO,
   4610 			    "port unavailable for target %d", XS_TGT(xs));
   4611 		else
   4612 			isp_prt(isp, ISP_LOGINFO,
   4613 			    "port logout for target %d", XS_TGT(xs));
   4614 		/*
   4615 		 * If we're on a local loop, force a LIP (which is overkill)
   4616 		 * to force a re-login of this unit. If we're on fabric,
   4617 		 * then we'll have to relogin as a matter of course.
   4618 		 */
   4619 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
   4620 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
   4621 			mbreg_t mbs;
   4622 			mbs.param[0] = MBOX_INIT_LIP;
   4623 			isp_mboxcmd_qnw(isp, &mbs, 1);
   4624 		}
   4625 
   4626 		/*
   4627 		 * Probably overkill.
   4628 		 */
   4629 		isp->isp_sendmarker = 1;
   4630 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   4631 		isp_mark_getpdb_all(isp);
   4632 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   4633 		if (XS_NOERR(xs)) {
   4634 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4635 		}
   4636 		return;
   4637 
   4638 	case RQCS_PORT_CHANGED:
   4639 		isp_prt(isp, ISP_LOGWARN,
   4640 		    "port changed for target %d", XS_TGT(xs));
   4641 		if (XS_NOERR(xs)) {
   4642 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4643 		}
   4644 		return;
   4645 
   4646 	case RQCS_PORT_BUSY:
   4647 		isp_prt(isp, ISP_LOGWARN,
   4648 		    "port busy for target %d", XS_TGT(xs));
   4649 		if (XS_NOERR(xs)) {
   4650 			XS_SETERR(xs, HBA_TGTBSY);
   4651 		}
   4652 		return;
   4653 
   4654 	default:
   4655 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
   4656 		    sp->req_completion_status);
   4657 		break;
   4658 	}
   4659 	if (XS_NOERR(xs)) {
   4660 		XS_SETERR(xs, HBA_BOTCH);
   4661 	}
   4662 }
   4663 
   4664 static void
   4665 isp_fastpost_complete(struct ispsoftc *isp, u_int16_t fph)
   4666 {
   4667 	XS_T *xs;
   4668 
   4669 	if (fph == 0) {
   4670 		return;
   4671 	}
   4672 	xs = isp_find_xs(isp, fph);
   4673 	if (xs == NULL) {
   4674 		isp_prt(isp, ISP_LOGWARN,
   4675 		    "Command for fast post handle 0x%x not found", fph);
   4676 		return;
   4677 	}
   4678 	isp_destroy_handle(isp, fph);
   4679 
   4680 	/*
   4681 	 * Since we don't have a result queue entry item,
   4682 	 * we must believe that SCSI status is zero and
   4683 	 * that all data transferred.
   4684 	 */
   4685 	XS_SET_STATE_STAT(isp, xs, NULL);
   4686 	XS_RESID(xs) = 0;
   4687 	*XS_STSP(xs) = SCSI_GOOD;
   4688 	if (XS_XFRLEN(xs)) {
   4689 		ISP_DMAFREE(isp, xs, fph);
   4690 	}
   4691 	if (isp->isp_nactive)
   4692 		isp->isp_nactive--;
   4693 	isp->isp_fphccmplt++;
   4694 	isp_done(xs);
   4695 }
   4696 
   4697 static int
   4698 isp_mbox_continue(struct ispsoftc *isp)
   4699 {
   4700 	mbreg_t mbs;
   4701 	u_int16_t *ptr;
   4702 
   4703 	switch (isp->isp_lastmbxcmd) {
   4704 	case MBOX_WRITE_RAM_WORD:
   4705 	case MBOX_READ_RAM_WORD:
   4706 	case MBOX_READ_RAM_WORD_EXTENDED:
   4707 		break;
   4708 	default:
   4709 		return (1);
   4710 	}
   4711 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
   4712 		isp->isp_mbxwrk0 = 0;
   4713 		return (-1);
   4714 	}
   4715 
   4716 
   4717 	/*
   4718 	 * Clear the previous interrupt.
   4719 	 */
   4720 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   4721 	ISP_WRITE(isp, BIU_SEMA, 0);
   4722 
   4723 	/*
   4724 	 * Continue with next word.
   4725 	 */
   4726 	ptr = isp->isp_mbxworkp;
   4727 	switch (isp->isp_lastmbxcmd) {
   4728 	case MBOX_WRITE_RAM_WORD:
   4729 		mbs.param[2] = *ptr++;
   4730 		mbs.param[1] = isp->isp_mbxwrk1++;
   4731 		break;
   4732 	case MBOX_READ_RAM_WORD:
   4733 	case MBOX_READ_RAM_WORD_EXTENDED:
   4734 		*ptr++ = isp->isp_mboxtmp[2];
   4735 		mbs.param[1] = isp->isp_mbxwrk1++;
   4736 		break;
   4737 	}
   4738 	isp->isp_mbxworkp = ptr;
   4739 	mbs.param[0] = isp->isp_lastmbxcmd;
   4740 	isp->isp_mbxwrk0 -= 1;
   4741 	isp_mboxcmd_qnw(isp, &mbs, 0);
   4742 	return (0);
   4743 }
   4744 
   4745 
   4746 #define	HIBYT(x)			((x) >> 0x8)
   4747 #define	LOBYT(x)			((x)  & 0xff)
   4748 #define	ISPOPMAP(a, b)			(((a) << 8) | (b))
   4749 static u_int16_t mbpscsi[] = {
   4750 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   4751 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   4752 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   4753 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   4754 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   4755 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   4756 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   4757 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   4758 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   4759 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
   4760 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
   4761 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   4762 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   4763 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   4764 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   4765 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   4766 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   4767 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
   4768 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
   4769 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   4770 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
   4771 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
   4772 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
   4773 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
   4774 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
   4775 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
   4776 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
   4777 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   4778 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
   4779 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   4780 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   4781 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   4782 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
   4783 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   4784 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   4785 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   4786 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
   4787 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   4788 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   4789 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
   4790 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   4791 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   4792 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
   4793 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   4794 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   4795 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   4796 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   4797 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   4798 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   4799 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   4800 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   4801 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   4802 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
   4803 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
   4804 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   4805 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   4806 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   4807 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   4808 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
   4809 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   4810 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   4811 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   4812 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   4813 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   4814 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   4815 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   4816 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   4817 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
   4818 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   4819 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
   4820 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
   4821 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
   4822 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
   4823 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
   4824 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
   4825 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
   4826 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   4827 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   4828 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   4829 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   4830 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
   4831 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
   4832 	ISPOPMAP(0xdf, 0xdf),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
   4833 	ISPOPMAP(0xff, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
   4834 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   4835 	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
   4836 	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
   4837 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   4838 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   4839 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   4840 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
   4841 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
   4842 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
   4843 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
   4844 };
   4845 
   4846 #ifndef	ISP_STRIPPED
   4847 static char *scsi_mbcmd_names[] = {
   4848 	"NO-OP",
   4849 	"LOAD RAM",
   4850 	"EXEC FIRMWARE",
   4851 	"DUMP RAM",
   4852 	"WRITE RAM WORD",
   4853 	"READ RAM WORD",
   4854 	"MAILBOX REG TEST",
   4855 	"VERIFY CHECKSUM",
   4856 	"ABOUT FIRMWARE",
   4857 	NULL,
   4858 	NULL,
   4859 	NULL,
   4860 	NULL,
   4861 	NULL,
   4862 	"CHECK FIRMWARE",
   4863 	NULL,
   4864 	"INIT REQUEST QUEUE",
   4865 	"INIT RESULT QUEUE",
   4866 	"EXECUTE IOCB",
   4867 	"WAKE UP",
   4868 	"STOP FIRMWARE",
   4869 	"ABORT",
   4870 	"ABORT DEVICE",
   4871 	"ABORT TARGET",
   4872 	"BUS RESET",
   4873 	"STOP QUEUE",
   4874 	"START QUEUE",
   4875 	"SINGLE STEP QUEUE",
   4876 	"ABORT QUEUE",
   4877 	"GET DEV QUEUE STATUS",
   4878 	NULL,
   4879 	"GET FIRMWARE STATUS",
   4880 	"GET INIT SCSI ID",
   4881 	"GET SELECT TIMEOUT",
   4882 	"GET RETRY COUNT",
   4883 	"GET TAG AGE LIMIT",
   4884 	"GET CLOCK RATE",
   4885 	"GET ACT NEG STATE",
   4886 	"GET ASYNC DATA SETUP TIME",
   4887 	"GET PCI PARAMS",
   4888 	"GET TARGET PARAMS",
   4889 	"GET DEV QUEUE PARAMS",
   4890 	"GET RESET DELAY PARAMS",
   4891 	NULL,
   4892 	NULL,
   4893 	NULL,
   4894 	NULL,
   4895 	NULL,
   4896 	"SET INIT SCSI ID",
   4897 	"SET SELECT TIMEOUT",
   4898 	"SET RETRY COUNT",
   4899 	"SET TAG AGE LIMIT",
   4900 	"SET CLOCK RATE",
   4901 	"SET ACT NEG STATE",
   4902 	"SET ASYNC DATA SETUP TIME",
   4903 	"SET PCI CONTROL PARAMS",
   4904 	"SET TARGET PARAMS",
   4905 	"SET DEV QUEUE PARAMS",
   4906 	"SET RESET DELAY PARAMS",
   4907 	NULL,
   4908 	NULL,
   4909 	NULL,
   4910 	NULL,
   4911 	NULL,
   4912 	"RETURN BIOS BLOCK ADDR",
   4913 	"WRITE FOUR RAM WORDS",
   4914 	"EXEC BIOS IOCB",
   4915 	NULL,
   4916 	NULL,
   4917 	"SET SYSTEM PARAMETER",
   4918 	"GET SYSTEM PARAMETER",
   4919 	NULL,
   4920 	"GET SCAM CONFIGURATION",
   4921 	"SET SCAM CONFIGURATION",
   4922 	"SET FIRMWARE FEATURES",
   4923 	"GET FIRMWARE FEATURES",
   4924 	NULL,
   4925 	NULL,
   4926 	NULL,
   4927 	NULL,
   4928 	"LOAD RAM A64",
   4929 	"DUMP RAM A64",
   4930 	"INITIALIZE REQUEST QUEUE A64",
   4931 	"INITIALIZE RESPONSE QUEUE A64",
   4932 	"EXECUTE IOCB A64",
   4933 	"ENABLE TARGET MODE",
   4934 	"GET TARGET MODE STATE",
   4935 	NULL,
   4936 	NULL,
   4937 	NULL,
   4938 	"SET DATA OVERRUN RECOVERY MODE",
   4939 	"GET DATA OVERRUN RECOVERY MODE",
   4940 	"SET HOST DATA",
   4941 	"GET NOST DATA",
   4942 };
   4943 #endif
   4944 
   4945 static u_int16_t mbpfc[] = {
   4946 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   4947 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   4948 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   4949 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   4950 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   4951 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   4952 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   4953 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   4954 	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   4955 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
   4956 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
   4957 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   4958 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   4959 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   4960 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   4961 	ISPOPMAP(0x03, 0x07),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED(1) */
   4962 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   4963 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
   4964 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
   4965 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   4966 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
   4967 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
   4968 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
   4969 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
   4970 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
   4971 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
   4972 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
   4973 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   4974 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
   4975 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   4976 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   4977 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   4978 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
   4979 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
   4980 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   4981 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
   4982 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
   4983 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
   4984 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
   4985 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
   4986 	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
   4987 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
   4988 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
   4989 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   4990 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   4991 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   4992 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   4993 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   4994 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
   4995 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
   4996 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   4997 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
   4998 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
   4999 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
   5000 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
   5001 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
   5002 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
   5003 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
   5004 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
   5005 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   5006 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   5007 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   5008 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   5009 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   5010 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
   5011 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
   5012 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
   5013 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
   5014 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   5015 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
   5016 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
   5017 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
   5018 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
   5019 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
   5020 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
   5021 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
   5022 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   5023 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   5024 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   5025 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   5026 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
   5027 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
   5028 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
   5029 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
   5030 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   5031 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
   5032 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   5033 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   5034 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   5035 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   5036 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
   5037 	ISPOPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
   5038 	ISPOPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
   5039 	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
   5040 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
   5041 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
   5042 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
   5043 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
   5044 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
   5045 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   5046 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
   5047 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
   5048 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
   5049 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
   5050 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
   5051 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
   5052 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
   5053 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
   5054 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
   5055 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
   5056 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
   5057 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
   5058 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
   5059 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
   5060 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
   5061 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
   5062 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
   5063 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
   5064 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
   5065 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
   5066 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
   5067 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
   5068 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
   5069 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
   5070 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
   5071 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
   5072 	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
   5073 };
   5074 /*
   5075  * Footnotes
   5076  *
   5077  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
   5078  *	do not access at this time in the core driver. The caller is
   5079  *	responsible for setting this register first (Gross!).
   5080  */
   5081 
   5082 #ifndef	ISP_STRIPPED
   5083 static char *fc_mbcmd_names[] = {
   5084 	"NO-OP",
   5085 	"LOAD RAM",
   5086 	"EXEC FIRMWARE",
   5087 	"DUMP RAM",
   5088 	"WRITE RAM WORD",
   5089 	"READ RAM WORD",
   5090 	"MAILBOX REG TEST",
   5091 	"VERIFY CHECKSUM",
   5092 	"ABOUT FIRMWARE",
   5093 	"LOAD RAM",
   5094 	"DUMP RAM",
   5095 	NULL,
   5096 	NULL,
   5097 	"READ RAM WORD EXTENDED",
   5098 	"CHECK FIRMWARE",
   5099 	NULL,
   5100 	"INIT REQUEST QUEUE",
   5101 	"INIT RESULT QUEUE",
   5102 	"EXECUTE IOCB",
   5103 	"WAKE UP",
   5104 	"STOP FIRMWARE",
   5105 	"ABORT",
   5106 	"ABORT DEVICE",
   5107 	"ABORT TARGET",
   5108 	"BUS RESET",
   5109 	"STOP QUEUE",
   5110 	"START QUEUE",
   5111 	"SINGLE STEP QUEUE",
   5112 	"ABORT QUEUE",
   5113 	"GET DEV QUEUE STATUS",
   5114 	NULL,
   5115 	"GET FIRMWARE STATUS",
   5116 	"GET LOOP ID",
   5117 	NULL,
   5118 	"GET RETRY COUNT",
   5119 	NULL,
   5120 	NULL,
   5121 	NULL,
   5122 	NULL,
   5123 	NULL,
   5124 	"GET FIRMWARE OPTIONS",
   5125 	"GET PORT QUEUE PARAMS",
   5126 	NULL,
   5127 	NULL,
   5128 	NULL,
   5129 	NULL,
   5130 	NULL,
   5131 	NULL,
   5132 	NULL,
   5133 	NULL,
   5134 	"SET RETRY COUNT",
   5135 	NULL,
   5136 	NULL,
   5137 	NULL,
   5138 	NULL,
   5139 	NULL,
   5140 	"SET FIRMWARE OPTIONS",
   5141 	"SET PORT QUEUE PARAMS",
   5142 	NULL,
   5143 	NULL,
   5144 	NULL,
   5145 	NULL,
   5146 	NULL,
   5147 	NULL,
   5148 	"LOOP PORT BYPASS",
   5149 	"LOOP PORT ENABLE",
   5150 	"GET RESOURCE COUNTS",
   5151 	"REQUEST NON PARTICIPATING MODE",
   5152 	NULL,
   5153 	NULL,
   5154 	NULL,
   5155 	"GET PORT DATABASE,, ENHANCED",
   5156 	NULL,
   5157 	NULL,
   5158 	NULL,
   5159 	NULL,
   5160 	NULL,
   5161 	NULL,
   5162 	NULL,
   5163 	NULL,
   5164 	NULL,
   5165 	NULL,
   5166 	NULL,
   5167 	NULL,
   5168 	"EXECUTE IOCB A64",
   5169 	NULL,
   5170 	NULL,
   5171 	NULL,
   5172 	NULL,
   5173 	NULL,
   5174 	NULL,
   5175 	NULL,
   5176 	NULL,
   5177 	"GET/SET DATA RATE",
   5178 	NULL,
   5179 	NULL,
   5180 	"INIT FIRMWARE",
   5181 	NULL,
   5182 	"INIT LIP",
   5183 	"GET FC-AL POSITION MAP",
   5184 	"GET PORT DATABASE",
   5185 	"CLEAR ACA",
   5186 	"TARGET RESET",
   5187 	"CLEAR TASK SET",
   5188 	"ABORT TASK SET",
   5189 	"GET FW STATE",
   5190 	"GET PORT NAME",
   5191 	"GET LINK STATUS",
   5192 	"INIT LIP RESET",
   5193 	NULL,
   5194 	"SEND SNS",
   5195 	"FABRIC LOGIN",
   5196 	"SEND CHANGE REQUEST",
   5197 	"FABRIC LOGOUT",
   5198 	"INIT LIP LOGIN",
   5199 	NULL,
   5200 	"LOGIN LOOP PORT",
   5201 	"GET PORT/NODE NAME LIST",
   5202 	"SET VENDOR ID",
   5203 	"INITIALIZE IP MAILBOX",
   5204 	NULL,
   5205 	NULL,
   5206 	NULL,
   5207 	NULL,
   5208 	"Get ID List",
   5209 	"SEND LFA",
   5210 	"Lun RESET"
   5211 };
   5212 #endif
   5213 
   5214 static void
   5215 isp_mboxcmd_qnw(struct ispsoftc *isp, mbreg_t *mbp, int nodelay)
   5216 {
   5217 	unsigned int lim, ibits, obits, box, opcode;
   5218 	u_int16_t *mcp;
   5219 
   5220 	if (IS_FC(isp)) {
   5221 		mcp = mbpfc;
   5222 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   5223 	} else {
   5224 		mcp = mbpscsi;
   5225 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   5226 	}
   5227 	opcode = mbp->param[0];
   5228 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   5229 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   5230 	for (box = 0; box < MAX_MAILBOX; box++) {
   5231 		if (ibits & (1 << box)) {
   5232 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   5233 		}
   5234 		if (nodelay == 0) {
   5235 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   5236 		}
   5237 	}
   5238 	if (nodelay == 0) {
   5239 		isp->isp_lastmbxcmd = opcode;
   5240 		isp->isp_obits = obits;
   5241 		isp->isp_mboxbsy = 1;
   5242 	}
   5243 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   5244 	/*
   5245 	 * Oddly enough, if we're not delaying for an answer,
   5246 	 * delay a bit to give the f/w a chance to pick up the
   5247 	 * command.
   5248 	 */
   5249 	if (nodelay) {
   5250 		USEC_DELAY(1000);
   5251 	}
   5252 }
   5253 
   5254 static void
   5255 isp_mboxcmd(struct ispsoftc *isp, mbreg_t *mbp, int logmask)
   5256 {
   5257 	char *cname, *xname, tname[16], mname[16];
   5258 	unsigned int lim, ibits, obits, box, opcode;
   5259 	u_int16_t *mcp;
   5260 
   5261 	if (IS_FC(isp)) {
   5262 		mcp = mbpfc;
   5263 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   5264 	} else {
   5265 		mcp = mbpscsi;
   5266 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   5267 	}
   5268 
   5269 	if ((opcode = mbp->param[0]) >= lim) {
   5270 		mbp->param[0] = MBOX_INVALID_COMMAND;
   5271 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
   5272 		return;
   5273 	}
   5274 
   5275 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   5276 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   5277 
   5278 	if (ibits == 0 && obits == 0) {
   5279 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
   5280 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
   5281 		return;
   5282 	}
   5283 
   5284 	/*
   5285 	 * Get exclusive usage of mailbox registers.
   5286 	 */
   5287 	MBOX_ACQUIRE(isp);
   5288 
   5289 	for (box = 0; box < MAX_MAILBOX; box++) {
   5290 		if (ibits & (1 << box)) {
   5291 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   5292 		}
   5293 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   5294 	}
   5295 
   5296 	isp->isp_lastmbxcmd = opcode;
   5297 
   5298 	/*
   5299 	 * We assume that we can't overwrite a previous command.
   5300 	 */
   5301 	isp->isp_obits = obits;
   5302 	isp->isp_mboxbsy = 1;
   5303 
   5304 	/*
   5305 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   5306 	 */
   5307 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   5308 
   5309 	/*
   5310 	 * While we haven't finished the command, spin our wheels here.
   5311 	 */
   5312 	MBOX_WAIT_COMPLETE(isp);
   5313 
   5314 	if (isp->isp_mboxbsy) {
   5315 		/*
   5316 		 * Command timed out.
   5317 		 */
   5318 		isp->isp_mboxbsy = 0;
   5319 		MBOX_RELEASE(isp);
   5320 		return;
   5321 	}
   5322 
   5323 	/*
   5324 	 * Copy back output registers.
   5325 	 */
   5326 	for (box = 0; box < MAX_MAILBOX; box++) {
   5327 		if (obits & (1 << box)) {
   5328 			mbp->param[box] = isp->isp_mboxtmp[box];
   5329 		}
   5330 	}
   5331 
   5332 	MBOX_RELEASE(isp);
   5333 
   5334 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
   5335 		return;
   5336 	}
   5337 #ifdef	ISP_STRIPPED
   5338 	cname = NULL;
   5339 #else
   5340 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
   5341 #endif
   5342 	if (cname == NULL) {
   5343 		cname = tname;
   5344 		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
   5345 	}
   5346 
   5347 	/*
   5348 	 * Just to be chatty here...
   5349 	 */
   5350 	xname = NULL;
   5351 	switch (mbp->param[0]) {
   5352 	case MBOX_COMMAND_COMPLETE:
   5353 		break;
   5354 	case MBOX_INVALID_COMMAND:
   5355 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
   5356 			xname = "INVALID COMMAND";
   5357 		break;
   5358 	case MBOX_HOST_INTERFACE_ERROR:
   5359 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
   5360 			xname = "HOST INTERFACE ERROR";
   5361 		break;
   5362 	case MBOX_TEST_FAILED:
   5363 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
   5364 			xname = "TEST FAILED";
   5365 		break;
   5366 	case MBOX_COMMAND_ERROR:
   5367 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
   5368 			xname = "COMMAND ERROR";
   5369 		break;
   5370 	case MBOX_COMMAND_PARAM_ERROR:
   5371 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
   5372 			xname = "COMMAND PARAMETER ERROR";
   5373 		break;
   5374 	case MBOX_LOOP_ID_USED:
   5375 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
   5376 			xname = "LOOP ID ALREADY IN USE";
   5377 		break;
   5378 	case MBOX_PORT_ID_USED:
   5379 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
   5380 			xname = "PORT ID ALREADY IN USE";
   5381 		break;
   5382 	case MBOX_ALL_IDS_USED:
   5383 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
   5384 			xname = "ALL LOOP IDS IN USE";
   5385 		break;
   5386 	case 0:		/* special case */
   5387 		xname = "TIMEOUT";
   5388 		break;
   5389 	default:
   5390 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
   5391 		xname = mname;
   5392 		break;
   5393 	}
   5394 	if (xname)
   5395 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
   5396 		    cname, xname);
   5397 }
   5398 
   5399 static void
   5400 isp_fw_state(struct ispsoftc *isp)
   5401 {
   5402 	if (IS_FC(isp)) {
   5403 		mbreg_t mbs;
   5404 		fcparam *fcp = isp->isp_param;
   5405 
   5406 		mbs.param[0] = MBOX_GET_FW_STATE;
   5407 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   5408 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   5409 			fcp->isp_fwstate = mbs.param[1];
   5410 		}
   5411 	}
   5412 }
   5413 
   5414 static void
   5415 isp_update(struct ispsoftc *isp)
   5416 {
   5417 	int bus, upmask;
   5418 
   5419 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
   5420 		if (upmask & (1 << bus)) {
   5421 			isp_update_bus(isp, bus);
   5422 		}
   5423 		upmask &= ~(1 << bus);
   5424 	}
   5425 }
   5426 
   5427 static void
   5428 isp_update_bus(struct ispsoftc *isp, int bus)
   5429 {
   5430 	int tgt;
   5431 	mbreg_t mbs;
   5432 	sdparam *sdp;
   5433 
   5434 	isp->isp_update &= ~(1 << bus);
   5435 	if (IS_FC(isp)) {
   5436 		/*
   5437 		 * There are no 'per-bus' settings for Fibre Channel.
   5438 		 */
   5439 		return;
   5440 	}
   5441 	sdp = isp->isp_param;
   5442 	sdp += bus;
   5443 
   5444 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5445 		u_int16_t flags, period, offset;
   5446 		int get;
   5447 
   5448 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   5449 			sdp->isp_devparam[tgt].dev_update = 0;
   5450 			sdp->isp_devparam[tgt].dev_refresh = 0;
   5451 			isp_prt(isp, ISP_LOGDEBUG0,
   5452 	 		    "skipping target %d bus %d update", tgt, bus);
   5453 			continue;
   5454 		}
   5455 		/*
   5456 		 * If the goal is to update the status of the device,
   5457 		 * take what's in goal_flags and try and set the device
   5458 		 * toward that. Otherwise, if we're just refreshing the
   5459 		 * current device state, get the current parameters.
   5460 		 */
   5461 
   5462 		/*
   5463 		 * Refresh overrides set
   5464 		 */
   5465 		if (sdp->isp_devparam[tgt].dev_refresh) {
   5466 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   5467 			sdp->isp_devparam[tgt].dev_refresh = 0;
   5468 			get = 1;
   5469 		} else if (sdp->isp_devparam[tgt].dev_update) {
   5470 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   5471 			/*
   5472 			 * Make sure goal_flags has "Renegotiate on Error"
   5473 			 * on and "Freeze Queue on Error" off.
   5474 			 */
   5475 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
   5476 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
   5477 
   5478 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
   5479 
   5480 			/*
   5481 			 * Insist that PARITY must be enabled
   5482 			 * if SYNC or WIDE is enabled.
   5483 			 */
   5484 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
   5485 				mbs.param[2] |= DPARM_PARITY;
   5486 			}
   5487 
   5488 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
   5489 				mbs.param[3] = 0;
   5490 			} else {
   5491 				mbs.param[3] =
   5492 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
   5493 				    (sdp->isp_devparam[tgt].goal_period);
   5494 			}
   5495 			/*
   5496 			 * A command completion later that has
   5497 			 * RQSTF_NEGOTIATION set can cause
   5498 			 * the dev_refresh/announce cycle also.
   5499 			 *
   5500 			 * Note: It is really important to update our current
   5501 			 * flags with at least the state of TAG capabilities-
   5502 			 * otherwise we might try and send a tagged command
   5503 			 * when we have it all turned off. So change it here
   5504 			 * to say that current already matches goal.
   5505 			 */
   5506 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
   5507 			sdp->isp_devparam[tgt].actv_flags |=
   5508 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
   5509 			isp_prt(isp, ISP_LOGDEBUG0,
   5510 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
   5511 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
   5512 			    mbs.param[3] & 0xff);
   5513 			sdp->isp_devparam[tgt].dev_update = 0;
   5514 			sdp->isp_devparam[tgt].dev_refresh = 1;
   5515 			get = 0;
   5516 		} else {
   5517 			continue;
   5518 		}
   5519 		mbs.param[1] = (bus << 15) | (tgt << 8);
   5520 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   5521 		if (get == 0) {
   5522 			isp->isp_sendmarker |= (1 << bus);
   5523 			continue;
   5524 		}
   5525 		flags = mbs.param[2];
   5526 		period = mbs.param[3] & 0xff;
   5527 		offset = mbs.param[3] >> 8;
   5528 		sdp->isp_devparam[tgt].actv_flags = flags;
   5529 		sdp->isp_devparam[tgt].actv_period = period;
   5530 		sdp->isp_devparam[tgt].actv_offset = offset;
   5531 		get = (bus << 16) | tgt;
   5532 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
   5533 	}
   5534 
   5535 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5536 		if (sdp->isp_devparam[tgt].dev_update ||
   5537 		    sdp->isp_devparam[tgt].dev_refresh) {
   5538 			isp->isp_update |= (1 << bus);
   5539 			break;
   5540 		}
   5541 	}
   5542 }
   5543 
   5544 #ifndef	DEFAULT_FRAMESIZE
   5545 #define	DEFAULT_FRAMESIZE(isp)		ICB_DFLT_FRMLEN
   5546 #endif
   5547 #ifndef	DEFAULT_EXEC_THROTTLE
   5548 #define	DEFAULT_EXEC_THROTTLE(isp)	ISP_EXEC_THROTTLE
   5549 #endif
   5550 
   5551 static void
   5552 isp_setdfltparm(struct ispsoftc *isp, int channel)
   5553 {
   5554 	int tgt;
   5555 	mbreg_t mbs;
   5556 	sdparam *sdp;
   5557 
   5558 	if (IS_FC(isp)) {
   5559 		fcparam *fcp = (fcparam *) isp->isp_param;
   5560 		int nvfail;
   5561 
   5562 		fcp += channel;
   5563 		if (fcp->isp_gotdparms) {
   5564 			return;
   5565 		}
   5566 		fcp->isp_gotdparms = 1;
   5567 		fcp->isp_maxfrmlen = DEFAULT_FRAMESIZE(isp);
   5568 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
   5569 		fcp->isp_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
   5570 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
   5571 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
   5572 		/* Platform specific.... */
   5573 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
   5574 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
   5575 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
   5576 		fcp->isp_fwoptions = 0;
   5577 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
   5578 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
   5579 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
   5580 #ifndef	ISP_NO_FASTPOST_FC
   5581 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
   5582 #endif
   5583 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
   5584 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
   5585 
   5586 		/*
   5587 		 * Make sure this is turned off now until we get
   5588 		 * extended options from NVRAM
   5589 		 */
   5590 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
   5591 
   5592 		/*
   5593 		 * Now try and read NVRAM unless told to not do so.
   5594 		 * This will set fcparam's isp_nodewwn && isp_portwwn.
   5595 		 */
   5596 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   5597 		    	nvfail = isp_read_nvram(isp);
   5598 			if (nvfail)
   5599 				isp->isp_confopts |= ISP_CFG_NONVRAM;
   5600 		} else {
   5601 			nvfail = 1;
   5602 		}
   5603 		/*
   5604 		 * Set node && port to override platform set defaults
   5605 		 * unless the nvram read failed (or none was done),
   5606 		 * or the platform code wants to use what had been
   5607 		 * set in the defaults.
   5608 		 */
   5609 		if (nvfail) {
   5610 			isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
   5611 		}
   5612 		if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
   5613 			isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
   5614 			    (u_int32_t) (DEFAULT_NODEWWN(isp) >> 32),
   5615 			    (u_int32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
   5616 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
   5617 		} else {
   5618 			/*
   5619 			 * We always start out with values derived
   5620 			 * from NVRAM or our platform default.
   5621 			 */
   5622 			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
   5623 		}
   5624 		if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
   5625 			isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
   5626 			    (u_int32_t) (DEFAULT_PORTWWN(isp) >> 32),
   5627 			    (u_int32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
   5628 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
   5629 		} else {
   5630 			/*
   5631 			 * We always start out with values derived
   5632 			 * from NVRAM or our platform default.
   5633 			 */
   5634 			ISP_PORTWWN(isp) = fcp->isp_portwwn;
   5635 		}
   5636 		return;
   5637 	}
   5638 
   5639 	sdp = (sdparam *) isp->isp_param;
   5640 	sdp += channel;
   5641 
   5642 	/*
   5643 	 * Been there, done that, got the T-shirt...
   5644 	 */
   5645 	if (sdp->isp_gotdparms) {
   5646 		return;
   5647 	}
   5648 	sdp->isp_gotdparms = 1;
   5649 
   5650 	/*
   5651 	 * Establish some default parameters.
   5652 	 */
   5653 	sdp->isp_cmd_dma_burst_enable = 0;
   5654 	sdp->isp_data_dma_burst_enabl = 1;
   5655 	sdp->isp_fifo_threshold = 0;
   5656 	sdp->isp_initiator_id = DEFAULT_IID(isp);
   5657 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   5658 		sdp->isp_async_data_setup = 9;
   5659 	} else {
   5660 		sdp->isp_async_data_setup = 6;
   5661 	}
   5662 	sdp->isp_selection_timeout = 250;
   5663 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
   5664 	sdp->isp_tag_aging = 8;
   5665 	sdp->isp_bus_reset_delay = 5;
   5666 	/*
   5667 	 * Don't retry selection, busy or queue full automatically- reflect
   5668 	 * these back to us.
   5669 	 */
   5670 	sdp->isp_retry_count = 0;
   5671 	sdp->isp_retry_delay = 0;
   5672 
   5673 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5674 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
   5675 		sdp->isp_devparam[tgt].dev_enable = 1;
   5676 	}
   5677 
   5678 	/*
   5679 	 * If we've not been told to avoid reading NVRAM, try and read it.
   5680 	 * If we're successful reading it, we can then return because NVRAM
   5681 	 * will tell us what the desired settings are. Otherwise, we establish
   5682 	 * some reasonable 'fake' nvram and goal defaults.
   5683 	 */
   5684 
   5685 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   5686 		if (isp_read_nvram(isp) == 0) {
   5687 			return;
   5688 		}
   5689 	}
   5690 
   5691 	/*
   5692 	 * Now try and see whether we have specific values for them.
   5693 	 */
   5694 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   5695 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   5696 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
   5697 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   5698 			sdp->isp_req_ack_active_neg = 1;
   5699 			sdp->isp_data_line_active_neg = 1;
   5700 		} else {
   5701 			sdp->isp_req_ack_active_neg =
   5702 			    (mbs.param[1+channel] >> 4) & 0x1;
   5703 			sdp->isp_data_line_active_neg =
   5704 			    (mbs.param[1+channel] >> 5) & 0x1;
   5705 		}
   5706 	}
   5707 
   5708 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
   5709 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   5710 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   5711 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   5712 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
   5713 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   5714 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   5715 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   5716 
   5717 	/*
   5718 	 * The trick here is to establish a default for the default (honk!)
   5719 	 * state (goal_flags). Then try and get the current status from
   5720 	 * the card to fill in the current state. We don't, in fact, set
   5721 	 * the default to the SAFE default state- that's not the goal state.
   5722 	 */
   5723 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5724 		u_int8_t off, per;
   5725 		sdp->isp_devparam[tgt].actv_offset = 0;
   5726 		sdp->isp_devparam[tgt].actv_period = 0;
   5727 		sdp->isp_devparam[tgt].actv_flags = 0;
   5728 
   5729 		sdp->isp_devparam[tgt].goal_flags =
   5730 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
   5731 
   5732 		/*
   5733 		 * We default to Wide/Fast for versions less than a 1040
   5734 		 * (unless it's SBus).
   5735 		 */
   5736 		if (IS_ULTRA3(isp)) {
   5737 			off = ISP_80M_SYNCPARMS >> 8;
   5738 			per = ISP_80M_SYNCPARMS & 0xff;
   5739 		} else if (IS_ULTRA2(isp)) {
   5740 			off = ISP_40M_SYNCPARMS >> 8;
   5741 			per = ISP_40M_SYNCPARMS & 0xff;
   5742 		} else if (IS_1240(isp)) {
   5743 			off = ISP_20M_SYNCPARMS >> 8;
   5744 			per = ISP_20M_SYNCPARMS & 0xff;
   5745 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
   5746 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
   5747 		    (isp->isp_bustype == ISP_BT_PCI &&
   5748 		    isp->isp_type < ISP_HA_SCSI_1040) ||
   5749 		    (isp->isp_clock && isp->isp_clock < 60) ||
   5750 		    (sdp->isp_ultramode == 0)) {
   5751 			off = ISP_10M_SYNCPARMS >> 8;
   5752 			per = ISP_10M_SYNCPARMS & 0xff;
   5753 		} else {
   5754 			off = ISP_20M_SYNCPARMS_1040 >> 8;
   5755 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
   5756 		}
   5757 		sdp->isp_devparam[tgt].goal_offset =
   5758 		    sdp->isp_devparam[tgt].nvrm_offset = off;
   5759 		sdp->isp_devparam[tgt].goal_period =
   5760 		    sdp->isp_devparam[tgt].nvrm_period = per;
   5761 
   5762 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
   5763 		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5764 		    sdp->isp_devparam[tgt].nvrm_offset,
   5765 		    sdp->isp_devparam[tgt].nvrm_period);
   5766 	}
   5767 }
   5768 
   5769 /*
   5770  * Re-initialize the ISP and complete all orphaned commands
   5771  * with a 'botched' notice. The reset/init routines should
   5772  * not disturb an already active list of commands.
   5773  *
   5774  * Locks held prior to coming here.
   5775  */
   5776 
   5777 void
   5778 isp_reinit(struct ispsoftc *isp)
   5779 {
   5780 	XS_T *xs;
   5781 	u_int16_t handle;
   5782 
   5783 	isp_reset(isp);
   5784 	if (isp->isp_state != ISP_RESETSTATE) {
   5785 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
   5786 	} else if (isp->isp_role != ISP_ROLE_NONE) {
   5787 		isp_init(isp);
   5788 		if (isp->isp_state == ISP_INITSTATE) {
   5789 			isp->isp_state = ISP_RUNSTATE;
   5790 		}
   5791 		if (isp->isp_state != ISP_RUNSTATE) {
   5792 			isp_prt(isp, ISP_LOGERR,
   5793 			    "isp_reinit cannot restart card");
   5794 		}
   5795 	}
   5796 	isp->isp_nactive = 0;
   5797 
   5798 	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
   5799 		xs = isp_find_xs(isp, handle);
   5800 		if (xs == NULL) {
   5801 			continue;
   5802 		}
   5803 		isp_destroy_handle(isp, handle);
   5804 		if (XS_XFRLEN(xs)) {
   5805 			ISP_DMAFREE(isp, xs, handle);
   5806 			XS_RESID(xs) = XS_XFRLEN(xs);
   5807 		} else {
   5808 			XS_RESID(xs) = 0;
   5809 		}
   5810 		XS_SETERR(xs, HBA_BUSRESET);
   5811 		isp_done(xs);
   5812 	}
   5813 }
   5814 
   5815 /*
   5816  * NVRAM Routines
   5817  */
   5818 static int
   5819 isp_read_nvram(struct ispsoftc *isp)
   5820 {
   5821 	int i, amt;
   5822 	u_int8_t csum, minversion;
   5823 	union {
   5824 		u_int8_t _x[ISP2100_NVRAM_SIZE];
   5825 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
   5826 	} _n;
   5827 #define	nvram_data	_n._x
   5828 #define	nvram_words	_n._s
   5829 
   5830 	if (IS_FC(isp)) {
   5831 		amt = ISP2100_NVRAM_SIZE;
   5832 		minversion = 1;
   5833 	} else if (IS_ULTRA2(isp)) {
   5834 		amt = ISP1080_NVRAM_SIZE;
   5835 		minversion = 0;
   5836 	} else {
   5837 		amt = ISP_NVRAM_SIZE;
   5838 		minversion = 2;
   5839 	}
   5840 
   5841 	/*
   5842 	 * Just read the first two words first to see if we have a valid
   5843 	 * NVRAM to continue reading the rest with.
   5844 	 */
   5845 	for (i = 0; i < 2; i++) {
   5846 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   5847 	}
   5848 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   5849 	    nvram_data[2] != 'P') {
   5850 		if (isp->isp_bustype != ISP_BT_SBUS) {
   5851 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
   5852 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
   5853 			    nvram_data[0], nvram_data[1], nvram_data[2]);
   5854 		}
   5855 		return (-1);
   5856 	}
   5857 	for (i = 2; i < amt>>1; i++) {
   5858 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   5859 	}
   5860 	for (csum = 0, i = 0; i < amt; i++) {
   5861 		csum += nvram_data[i];
   5862 	}
   5863 	if (csum != 0) {
   5864 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
   5865 		return (-1);
   5866 	}
   5867 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   5868 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
   5869 		    ISP_NVRAM_VERSION(nvram_data));
   5870 		return (-1);
   5871 	}
   5872 
   5873 	if (IS_ULTRA3(isp)) {
   5874 		isp_parse_nvram_12160(isp, 0, nvram_data);
   5875 		isp_parse_nvram_12160(isp, 1, nvram_data);
   5876 	} else if (IS_1080(isp)) {
   5877 		isp_parse_nvram_1080(isp, 0, nvram_data);
   5878 	} else if (IS_1280(isp) || IS_1240(isp)) {
   5879 		isp_parse_nvram_1080(isp, 0, nvram_data);
   5880 		isp_parse_nvram_1080(isp, 1, nvram_data);
   5881 	} else if (IS_SCSI(isp)) {
   5882 		isp_parse_nvram_1020(isp, nvram_data);
   5883 	} else {
   5884 		isp_parse_nvram_2100(isp, nvram_data);
   5885 	}
   5886 	return (0);
   5887 #undef	nvram_data
   5888 #undef	nvram_words
   5889 }
   5890 
   5891 static void
   5892 isp_rdnvram_word(struct ispsoftc *isp, int wo, u_int16_t *rp)
   5893 {
   5894 	int i, cbits;
   5895 	u_int16_t bit, rqst;
   5896 
   5897 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   5898 	USEC_DELAY(2);
   5899 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   5900 	USEC_DELAY(2);
   5901 
   5902 	if (IS_FC(isp)) {
   5903 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   5904 		if (IS_2312(isp) && isp->isp_port) {
   5905 			wo += 128;
   5906 		}
   5907 		rqst = (ISP_NVRAM_READ << 8) | wo;
   5908 		cbits = 10;
   5909 	} else if (IS_ULTRA2(isp)) {
   5910 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
   5911 		rqst = (ISP_NVRAM_READ << 8) | wo;
   5912 		cbits = 10;
   5913 	} else {
   5914 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   5915 		rqst = (ISP_NVRAM_READ << 6) | wo;
   5916 		cbits = 8;
   5917 	}
   5918 
   5919 	/*
   5920 	 * Clock the word select request out...
   5921 	 */
   5922 	for (i = cbits; i >= 0; i--) {
   5923 		if ((rqst >> i) & 1) {
   5924 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   5925 		} else {
   5926 			bit = BIU_NVRAM_SELECT;
   5927 		}
   5928 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5929 		USEC_DELAY(2);
   5930 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   5931 		USEC_DELAY(2);
   5932 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5933 		USEC_DELAY(2);
   5934 	}
   5935 	/*
   5936 	 * Now read the result back in (bits come back in MSB format).
   5937 	 */
   5938 	*rp = 0;
   5939 	for (i = 0; i < 16; i++) {
   5940 		u_int16_t rv;
   5941 		*rp <<= 1;
   5942 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   5943 		USEC_DELAY(2);
   5944 		rv = ISP_READ(isp, BIU_NVRAM);
   5945 		if (rv & BIU_NVRAM_DATAIN) {
   5946 			*rp |= 1;
   5947 		}
   5948 		USEC_DELAY(2);
   5949 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   5950 		USEC_DELAY(2);
   5951 	}
   5952 	ISP_WRITE(isp, BIU_NVRAM, 0);
   5953 	USEC_DELAY(2);
   5954 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
   5955 }
   5956 
   5957 static void
   5958 isp_parse_nvram_1020(struct ispsoftc *isp, u_int8_t *nvram_data)
   5959 {
   5960 	sdparam *sdp = (sdparam *) isp->isp_param;
   5961 	int tgt;
   5962 
   5963 	sdp->isp_fifo_threshold =
   5964 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
   5965 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
   5966 
   5967 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
   5968 		sdp->isp_initiator_id =
   5969 			ISP_NVRAM_INITIATOR_ID(nvram_data);
   5970 
   5971 	sdp->isp_bus_reset_delay =
   5972 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   5973 
   5974 	sdp->isp_retry_count =
   5975 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   5976 
   5977 	sdp->isp_retry_delay =
   5978 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   5979 
   5980 	sdp->isp_async_data_setup =
   5981 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   5982 
   5983 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   5984 		if (sdp->isp_async_data_setup < 9) {
   5985 			sdp->isp_async_data_setup = 9;
   5986 		}
   5987 	} else {
   5988 		if (sdp->isp_async_data_setup != 6) {
   5989 			sdp->isp_async_data_setup = 6;
   5990 		}
   5991 	}
   5992 
   5993 	sdp->isp_req_ack_active_neg =
   5994 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   5995 
   5996 	sdp->isp_data_line_active_neg =
   5997 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   5998 
   5999 	sdp->isp_data_dma_burst_enabl =
   6000 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   6001 
   6002 	sdp->isp_cmd_dma_burst_enable =
   6003 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   6004 
   6005 	sdp->isp_tag_aging =
   6006 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   6007 
   6008 	sdp->isp_selection_timeout =
   6009 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   6010 
   6011 	sdp->isp_max_queue_depth =
   6012 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   6013 
   6014 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   6015 
   6016 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   6017 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   6018 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   6019 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   6020 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   6021 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   6022 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   6023 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   6024 
   6025 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   6026 		sdp->isp_devparam[tgt].dev_enable =
   6027 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
   6028 		sdp->isp_devparam[tgt].exc_throttle =
   6029 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
   6030 		sdp->isp_devparam[tgt].nvrm_offset =
   6031 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
   6032 		sdp->isp_devparam[tgt].nvrm_period =
   6033 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
   6034 		/*
   6035 		 * We probably shouldn't lie about this, but it
   6036 		 * it makes it much safer if we limit NVRAM values
   6037 		 * to sanity.
   6038 		 */
   6039 		if (isp->isp_type < ISP_HA_SCSI_1040) {
   6040 			/*
   6041 			 * If we're not ultra, we can't possibly
   6042 			 * be a shorter period than this.
   6043 			 */
   6044 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
   6045 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
   6046 			}
   6047 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
   6048 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
   6049 			}
   6050 		} else {
   6051 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
   6052 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
   6053 			}
   6054 		}
   6055 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   6056 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
   6057 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   6058 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   6059 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
   6060 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   6061 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
   6062 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   6063 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
   6064 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   6065 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
   6066 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   6067 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
   6068 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   6069 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
   6070 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   6071 		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   6072 		    sdp->isp_devparam[tgt].nvrm_offset,
   6073 		    sdp->isp_devparam[tgt].nvrm_period);
   6074 		sdp->isp_devparam[tgt].goal_offset =
   6075 		    sdp->isp_devparam[tgt].nvrm_offset;
   6076 		sdp->isp_devparam[tgt].goal_period =
   6077 		    sdp->isp_devparam[tgt].nvrm_period;
   6078 		sdp->isp_devparam[tgt].goal_flags =
   6079 		    sdp->isp_devparam[tgt].nvrm_flags;
   6080 	}
   6081 }
   6082 
   6083 static void
   6084 isp_parse_nvram_1080(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   6085 {
   6086 	sdparam *sdp = (sdparam *) isp->isp_param;
   6087 	int tgt;
   6088 
   6089 	sdp += bus;
   6090 
   6091 	sdp->isp_fifo_threshold =
   6092 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
   6093 
   6094 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
   6095 		sdp->isp_initiator_id =
   6096 		    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
   6097 
   6098 	sdp->isp_bus_reset_delay =
   6099 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   6100 
   6101 	sdp->isp_retry_count =
   6102 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   6103 
   6104 	sdp->isp_retry_delay =
   6105 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   6106 
   6107 	sdp->isp_async_data_setup =
   6108 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   6109 
   6110 	sdp->isp_req_ack_active_neg =
   6111 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   6112 
   6113 	sdp->isp_data_line_active_neg =
   6114 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   6115 
   6116 	sdp->isp_data_dma_burst_enabl =
   6117 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   6118 
   6119 	sdp->isp_cmd_dma_burst_enable =
   6120 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   6121 
   6122 	sdp->isp_selection_timeout =
   6123 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   6124 
   6125 	sdp->isp_max_queue_depth =
   6126 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   6127 
   6128 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   6129 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   6130 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   6131 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   6132 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   6133 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   6134 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   6135 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   6136 
   6137 
   6138 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   6139 		sdp->isp_devparam[tgt].dev_enable =
   6140 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   6141 		sdp->isp_devparam[tgt].exc_throttle =
   6142 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   6143 		sdp->isp_devparam[tgt].nvrm_offset =
   6144 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   6145 		sdp->isp_devparam[tgt].nvrm_period =
   6146 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   6147 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   6148 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   6149 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   6150 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   6151 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   6152 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   6153 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   6154 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   6155 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   6156 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   6157 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   6158 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   6159 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   6160 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   6161 		sdp->isp_devparam[tgt].actv_flags = 0;
   6162 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   6163 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   6164 		    sdp->isp_devparam[tgt].nvrm_offset,
   6165 		    sdp->isp_devparam[tgt].nvrm_period);
   6166 		sdp->isp_devparam[tgt].goal_offset =
   6167 		    sdp->isp_devparam[tgt].nvrm_offset;
   6168 		sdp->isp_devparam[tgt].goal_period =
   6169 		    sdp->isp_devparam[tgt].nvrm_period;
   6170 		sdp->isp_devparam[tgt].goal_flags =
   6171 		    sdp->isp_devparam[tgt].nvrm_flags;
   6172 	}
   6173 }
   6174 
   6175 static void
   6176 isp_parse_nvram_12160(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   6177 {
   6178 	sdparam *sdp = (sdparam *) isp->isp_param;
   6179 	int tgt;
   6180 
   6181 	sdp += bus;
   6182 
   6183 	sdp->isp_fifo_threshold =
   6184 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
   6185 
   6186 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
   6187 		sdp->isp_initiator_id =
   6188 		    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
   6189 
   6190 	sdp->isp_bus_reset_delay =
   6191 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   6192 
   6193 	sdp->isp_retry_count =
   6194 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   6195 
   6196 	sdp->isp_retry_delay =
   6197 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   6198 
   6199 	sdp->isp_async_data_setup =
   6200 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   6201 
   6202 	sdp->isp_req_ack_active_neg =
   6203 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   6204 
   6205 	sdp->isp_data_line_active_neg =
   6206 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   6207 
   6208 	sdp->isp_data_dma_burst_enabl =
   6209 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   6210 
   6211 	sdp->isp_cmd_dma_burst_enable =
   6212 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   6213 
   6214 	sdp->isp_selection_timeout =
   6215 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   6216 
   6217 	sdp->isp_max_queue_depth =
   6218 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   6219 
   6220 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   6221 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   6222 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   6223 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   6224 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   6225 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   6226 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   6227 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   6228 
   6229 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   6230 		sdp->isp_devparam[tgt].dev_enable =
   6231 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   6232 		sdp->isp_devparam[tgt].exc_throttle =
   6233 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   6234 		sdp->isp_devparam[tgt].nvrm_offset =
   6235 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   6236 		sdp->isp_devparam[tgt].nvrm_period =
   6237 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   6238 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   6239 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   6240 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   6241 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   6242 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   6243 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   6244 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   6245 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   6246 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   6247 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   6248 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   6249 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   6250 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   6251 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   6252 		sdp->isp_devparam[tgt].actv_flags = 0;
   6253 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   6254 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   6255 		    sdp->isp_devparam[tgt].nvrm_offset,
   6256 		    sdp->isp_devparam[tgt].nvrm_period);
   6257 		sdp->isp_devparam[tgt].goal_offset =
   6258 		    sdp->isp_devparam[tgt].nvrm_offset;
   6259 		sdp->isp_devparam[tgt].goal_period =
   6260 		    sdp->isp_devparam[tgt].nvrm_period;
   6261 		sdp->isp_devparam[tgt].goal_flags =
   6262 		    sdp->isp_devparam[tgt].nvrm_flags;
   6263 	}
   6264 }
   6265 
   6266 static void
   6267 isp_parse_nvram_2100(struct ispsoftc *isp, u_int8_t *nvram_data)
   6268 {
   6269 	fcparam *fcp = (fcparam *) isp->isp_param;
   6270 	u_int64_t wwn;
   6271 
   6272 	/*
   6273 	 * There is NVRAM storage for both Port and Node entities-
   6274 	 * but the Node entity appears to be unused on all the cards
   6275 	 * I can find. However, we should account for this being set
   6276 	 * at some point in the future.
   6277 	 *
   6278 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
   6279 	 * bits 48..60. In the case of the 2202, it appears that they do
   6280 	 * use bit 48 to distinguish between the two instances on the card.
   6281 	 * The 2204, which I've never seen, *probably* extends this method.
   6282 	 */
   6283 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
   6284 	if (wwn) {
   6285 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
   6286 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
   6287 		if ((wwn >> 60) == 0) {
   6288 			wwn |= (((u_int64_t) 2)<< 60);
   6289 		}
   6290 	}
   6291 	fcp->isp_portwwn = wwn;
   6292 	if (IS_2200(isp) || IS_23XX(isp)) {
   6293 		wwn = ISP2200_NVRAM_NODE_NAME(nvram_data);
   6294 		if (wwn) {
   6295 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
   6296 			    (u_int32_t) (wwn >> 32),
   6297 			    (u_int32_t) (wwn & 0xffffffff));
   6298 			if ((wwn >> 60) == 0) {
   6299 				wwn |= (((u_int64_t) 2)<< 60);
   6300 			}
   6301 		}
   6302 	} else {
   6303 		wwn &= ~((u_int64_t) 0xfff << 48);
   6304 	}
   6305 	fcp->isp_nodewwn = wwn;
   6306 
   6307 	/*
   6308 	 * Make sure we have both Node and Port as non-zero values.
   6309 	 */
   6310 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
   6311 		fcp->isp_portwwn = fcp->isp_nodewwn;
   6312 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
   6313 		fcp->isp_nodewwn = fcp->isp_portwwn;
   6314 	}
   6315 
   6316 	/*
   6317 	 * Make the Node and Port values sane if they're NAA == 2.
   6318 	 * This means to clear bits 48..56 for the Node WWN and
   6319 	 * make sure that there's some non-zero value in 48..56
   6320 	 * for the Port WWN.
   6321 	 */
   6322 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
   6323 		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
   6324 		    (fcp->isp_nodewwn >> 60) == 2) {
   6325 			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
   6326 		}
   6327 		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
   6328 		    (fcp->isp_portwwn >> 60) == 2) {
   6329 			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
   6330 		}
   6331 	}
   6332 
   6333 	isp_prt(isp, ISP_LOGDEBUG0,
   6334 	    "NVRAM: maxfrmlen %d execthrottle %d fwoptions 0x%x loopid %x",
   6335 	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data),
   6336 	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
   6337 	    ISP2100_NVRAM_OPTIONS(nvram_data),
   6338 	    ISP2100_NVRAM_HARDLOOPID(nvram_data));
   6339 
   6340 	fcp->isp_maxalloc =
   6341 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   6342 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0)
   6343 		fcp->isp_maxfrmlen =
   6344 			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   6345 	fcp->isp_retry_delay =
   6346 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   6347 	fcp->isp_retry_count =
   6348 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   6349 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
   6350 		fcp->isp_loopid =
   6351 			ISP2100_NVRAM_HARDLOOPID(nvram_data);
   6352 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0)
   6353 		fcp->isp_execthrottle =
   6354 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   6355 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
   6356 }
   6357 
   6358 #ifdef	ISP_FW_CRASH_DUMP
   6359 static void isp2200_fw_dump(struct ispsoftc *);
   6360 static void isp2300_fw_dump(struct ispsoftc *);
   6361 
   6362 static void
   6363 isp2200_fw_dump(struct ispsoftc *isp)
   6364 {
   6365 	int i, j;
   6366 	mbreg_t mbs;
   6367 	u_int16_t *ptr;
   6368 
   6369 	ptr = FCPARAM(isp)->isp_dump_data;
   6370 	if (ptr == NULL) {
   6371 		isp_prt(isp, ISP_LOGERR,
   6372 		   "No place to dump RISC registers and SRAM");
   6373 		return;
   6374 	}
   6375 	if (*ptr++) {
   6376 		isp_prt(isp, ISP_LOGERR,
   6377 		   "dump area for RISC registers and SRAM already used");
   6378 		return;
   6379 	}
   6380 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   6381 	for (i = 0; i < 100; i++) {
   6382 		USEC_DELAY(100);
   6383 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6384 			break;
   6385 		}
   6386 	}
   6387 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6388 		/*
   6389 		 * PBIU Registers
   6390 		 */
   6391 		for (i = 0; i < 8; i++) {
   6392 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
   6393 		}
   6394 
   6395 		/*
   6396 		 * Mailbox Registers
   6397 		 */
   6398 		for (i = 0; i < 8; i++) {
   6399 			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
   6400 		}
   6401 
   6402 		/*
   6403 		 * DMA Registers
   6404 		 */
   6405 		for (i = 0; i < 48; i++) {
   6406 			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
   6407 		}
   6408 
   6409 		/*
   6410 		 * RISC H/W Registers
   6411 		 */
   6412 		ISP_WRITE(isp, BIU2100_CSR, 0);
   6413 		for (i = 0; i < 16; i++) {
   6414 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
   6415 		}
   6416 
   6417 		/*
   6418 		 * RISC GP Registers
   6419 		 */
   6420 		for (j = 0; j < 8; j++) {
   6421 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
   6422 			for (i = 0; i < 16; i++) {
   6423 				*ptr++ =
   6424 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6425 			}
   6426 		}
   6427 
   6428 		/*
   6429 		 * Frame Buffer Hardware Registers
   6430 		 */
   6431 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
   6432 		for (i = 0; i < 16; i++) {
   6433 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6434 		}
   6435 
   6436 		/*
   6437 		 * Fibre Protocol Module 0 Hardware Registers
   6438 		 */
   6439 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
   6440 		for (i = 0; i < 64; i++) {
   6441 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6442 		}
   6443 
   6444 		/*
   6445 		 * Fibre Protocol Module 1 Hardware Registers
   6446 		 */
   6447 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
   6448 		for (i = 0; i < 64; i++) {
   6449 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6450 		}
   6451 	} else {
   6452 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
   6453 		return;
   6454 	}
   6455 	isp_prt(isp, ISP_LOGALL,
   6456 	   "isp_fw_dump: RISC registers dumped successfully");
   6457 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
   6458 	for (i = 0; i < 100; i++) {
   6459 		USEC_DELAY(100);
   6460 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
   6461 			break;
   6462 		}
   6463 	}
   6464 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
   6465 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
   6466 		return;
   6467 	}
   6468 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   6469 	for (i = 0; i < 100; i++) {
   6470 		USEC_DELAY(100);
   6471 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6472 			break;
   6473 		}
   6474 	}
   6475 	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
   6476 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
   6477 		return;
   6478 	}
   6479 	ISP_WRITE(isp, RISC_EMB, 0xf2);
   6480 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
   6481 	for (i = 0; i < 100; i++) {
   6482 		USEC_DELAY(100);
   6483 		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
   6484 			break;
   6485 		}
   6486 	}
   6487 	ENABLE_INTS(isp);
   6488 	mbs.param[0] = MBOX_READ_RAM_WORD;
   6489 	mbs.param[1] = 0x1000;
   6490 	isp->isp_mbxworkp = (void *) ptr;
   6491 	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
   6492 	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
   6493 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
   6494 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   6495 		isp_prt(isp, ISP_LOGWARN,
   6496 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
   6497 		return;
   6498 	}
   6499 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
   6500 	*ptr++ = isp->isp_mboxtmp[2];
   6501 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped succesfully");
   6502 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
   6503 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
   6504 }
   6505 
   6506 static void
   6507 isp2300_fw_dump(struct ispsoftc *isp)
   6508 {
   6509 	int i, j;
   6510 	mbreg_t mbs;
   6511 	u_int16_t *ptr;
   6512 
   6513 	ptr = FCPARAM(isp)->isp_dump_data;
   6514 	if (ptr == NULL) {
   6515 		isp_prt(isp, ISP_LOGERR,
   6516 		   "No place to dump RISC registers and SRAM");
   6517 		return;
   6518 	}
   6519 	if (*ptr++) {
   6520 		isp_prt(isp, ISP_LOGERR,
   6521 		   "dump area for RISC registers and SRAM already used");
   6522 		return;
   6523 	}
   6524 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   6525 	for (i = 0; i < 100; i++) {
   6526 		USEC_DELAY(100);
   6527 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6528 			break;
   6529 		}
   6530 	}
   6531 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6532 		/*
   6533 		 * PBIU registers
   6534 		 */
   6535 		for (i = 0; i < 8; i++) {
   6536 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
   6537 		}
   6538 
   6539 		/*
   6540 		 * ReqQ-RspQ-Risc2Host Status registers
   6541 		 */
   6542 		for (i = 0; i < 8; i++) {
   6543 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
   6544 		}
   6545 
   6546 		/*
   6547 		 * Mailbox Registers
   6548 		 */
   6549 		for (i = 0; i < 32; i++) {
   6550 			*ptr++ =
   6551 			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
   6552 		}
   6553 
   6554 		/*
   6555 		 * Auto Request Response DMA registers
   6556 		 */
   6557 		ISP_WRITE(isp, BIU2100_CSR, 0x40);
   6558 		for (i = 0; i < 32; i++) {
   6559 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6560 		}
   6561 
   6562 		/*
   6563 		 * DMA registers
   6564 		 */
   6565 		ISP_WRITE(isp, BIU2100_CSR, 0x50);
   6566 		for (i = 0; i < 48; i++) {
   6567 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6568 		}
   6569 
   6570 		/*
   6571 		 * RISC hardware registers
   6572 		 */
   6573 		ISP_WRITE(isp, BIU2100_CSR, 0);
   6574 		for (i = 0; i < 16; i++) {
   6575 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
   6576 		}
   6577 
   6578 		/*
   6579 		 * RISC GP? registers
   6580 		 */
   6581 		for (j = 0; j < 8; j++) {
   6582 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
   6583 			for (i = 0; i < 16; i++) {
   6584 				*ptr++ =
   6585 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6586 			}
   6587 		}
   6588 
   6589 		/*
   6590 		 * frame buffer hardware registers
   6591 		 */
   6592 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
   6593 		for (i = 0; i < 64; i++) {
   6594 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6595 		}
   6596 
   6597 		/*
   6598 		 * FPM B0 hardware registers
   6599 		 */
   6600 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
   6601 		for (i = 0; i < 64; i++) {
   6602 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6603 		}
   6604 
   6605 		/*
   6606 		 * FPM B1 hardware registers
   6607 		 */
   6608 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
   6609 		for (i = 0; i < 64; i++) {
   6610 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6611 		}
   6612 	} else {
   6613 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
   6614 		return;
   6615 	}
   6616 	isp_prt(isp, ISP_LOGALL,
   6617 	   "isp_fw_dump: RISC registers dumped successfully");
   6618 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
   6619 	for (i = 0; i < 100; i++) {
   6620 		USEC_DELAY(100);
   6621 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
   6622 			break;
   6623 		}
   6624 	}
   6625 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
   6626 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
   6627 		return;
   6628 	}
   6629 	ENABLE_INTS(isp);
   6630 	mbs.param[0] = MBOX_READ_RAM_WORD;
   6631 	mbs.param[1] = 0x800;
   6632 	isp->isp_mbxworkp = (void *) ptr;
   6633 	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
   6634 	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
   6635 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
   6636 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   6637 		isp_prt(isp, ISP_LOGWARN,
   6638 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
   6639 		return;
   6640 	}
   6641 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
   6642 	*ptr++ = isp->isp_mboxtmp[2];
   6643 
   6644 	/*
   6645 	 * We don't have access to mailbox registers 8.. onward
   6646 	 * in our 'common' device model- so we have to set it
   6647 	 * here and hope it stays the same!
   6648 	 */
   6649 	ISP_WRITE(isp, PCI_MBOX_REGS2300_OFF + (8 << 1), 0x1);
   6650 
   6651 	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
   6652 	mbs.param[1] = 0;
   6653 	isp->isp_mbxworkp = (void *) ptr;
   6654 	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
   6655 	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
   6656 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
   6657 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   6658 		isp_prt(isp, ISP_LOGWARN,
   6659 		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
   6660 		return;
   6661 	}
   6662 	ptr = isp->isp_mbxworkp;	/* finish final word */
   6663 	*ptr++ = mbs.param[2];
   6664 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped succesfully");
   6665 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
   6666 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
   6667 }
   6668 
   6669 void
   6670 isp_fw_dump(struct ispsoftc *isp)
   6671 {
   6672 	if (IS_2200(isp))
   6673 		isp2200_fw_dump(isp);
   6674 	else if (IS_23XX(isp))
   6675 		isp2300_fw_dump(isp);
   6676 }
   6677 #endif
   6678