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