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