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