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