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