Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.82
      1 /* $NetBSD: isp.c,v 1.82 2001/10/20 18:37:54 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_LOGDEBUG1, "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 				XS_SETERR(xs, HBA_BOTCH);
   3315 			}
   3316 			XS_RESID(xs) = XS_XFRLEN(xs);
   3317 			break;
   3318 		default:
   3319 			isp_prt(isp, ISP_LOGWARN,
   3320 			    "unhandled response queue type 0x%x",
   3321 			    sp->req_header.rqs_entry_type);
   3322 			if (XS_NOERR(xs)) {
   3323 				XS_SETERR(xs, HBA_BOTCH);
   3324 			}
   3325 			break;
   3326 		}
   3327 
   3328 		/*
   3329 		 * Free any dma resources. As a side effect, this may
   3330 		 * also do any cache flushing necessary for data coherence.			 */
   3331 		if (XS_XFRLEN(xs)) {
   3332 			ISP_DMAFREE(isp, xs, sp->req_handle);
   3333 		}
   3334 
   3335 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
   3336 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
   3337 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
   3338 			char skey;
   3339 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   3340 				skey = XS_SNSKEY(xs) & 0xf;
   3341 				if (skey < 10)
   3342 					skey += '0';
   3343 				else
   3344 					skey += 'a' - 10;
   3345 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
   3346 				skey = '?';
   3347 			} else {
   3348 				skey = '.';
   3349 			}
   3350 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
   3351 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
   3352 			    *XS_STSP(xs), skey, XS_ERR(xs));
   3353 		}
   3354 
   3355 		if (isp->isp_nactive > 0)
   3356 		    isp->isp_nactive--;
   3357 		complist[ndone++] = xs;	/* defer completion call until later */
   3358 		MEMZERO(sp, sizeof (isphdr_t));
   3359 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
   3360 			break;
   3361 		}
   3362 	}
   3363 
   3364 	/*
   3365 	 * If we looked at any commands, then it's valid to find out
   3366 	 * what the outpointer is. It also is a trigger to update the
   3367 	 * ISP's notion of what we've seen so far.
   3368 	 */
   3369 	if (nlooked) {
   3370 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3371 		/*
   3372 		 * While we're at it, reqad the requst queue out pointer.
   3373 		 */
   3374 		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
   3375 	}
   3376 
   3377 	isp->isp_residx = optr;
   3378 	for (i = 0; i < ndone; i++) {
   3379 		xs = complist[i];
   3380 		if (xs) {
   3381 			isp_done(xs);
   3382 		}
   3383 	}
   3384 }
   3385 
   3386 /*
   3387  * Support routines.
   3388  */
   3389 
   3390 static int
   3391 isp_parse_async(struct ispsoftc *isp, int mbox)
   3392 {
   3393 	int bus;
   3394 	u_int16_t fast_post_handle = 0;
   3395 
   3396 	if (IS_DUALBUS(isp)) {
   3397 		bus = ISP_READ(isp, OUTMAILBOX6);
   3398 	} else {
   3399 		bus = 0;
   3400 	}
   3401 
   3402 	switch (mbox) {
   3403 	case ASYNC_BUS_RESET:
   3404 		isp->isp_sendmarker |= (1 << bus);
   3405 #ifdef	ISP_TARGET_MODE
   3406 		isp_target_async(isp, bus, mbox);
   3407 #endif
   3408 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
   3409 		break;
   3410 	case ASYNC_SYSTEM_ERROR:
   3411 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
   3412 		/* no point continuing after this */
   3413 		return (-1);
   3414 
   3415 	case ASYNC_RQS_XFER_ERR:
   3416 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
   3417 		break;
   3418 
   3419 	case ASYNC_RSP_XFER_ERR:
   3420 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
   3421 		break;
   3422 
   3423 	case ASYNC_QWAKEUP:
   3424 		/*
   3425 		 * We've just been notified that the Queue has woken up.
   3426 		 * We don't need to be chatty about this- just unlatch things
   3427 		 * and move on.
   3428 		 */
   3429 		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
   3430 		break;
   3431 
   3432 	case ASYNC_TIMEOUT_RESET:
   3433 		isp_prt(isp, ISP_LOGWARN,
   3434 		    "timeout initiated SCSI bus reset of bus %d", bus);
   3435 		isp->isp_sendmarker |= (1 << bus);
   3436 #ifdef	ISP_TARGET_MODE
   3437 		isp_target_async(isp, bus, mbox);
   3438 #endif
   3439 		break;
   3440 
   3441 	case ASYNC_DEVICE_RESET:
   3442 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
   3443 		isp->isp_sendmarker |= (1 << bus);
   3444 #ifdef	ISP_TARGET_MODE
   3445 		isp_target_async(isp, bus, mbox);
   3446 #endif
   3447 		break;
   3448 
   3449 	case ASYNC_EXTMSG_UNDERRUN:
   3450 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
   3451 		break;
   3452 
   3453 	case ASYNC_SCAM_INT:
   3454 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
   3455 		break;
   3456 
   3457 	case ASYNC_HUNG_SCSI:
   3458 		isp_prt(isp, ISP_LOGERR,
   3459 		    "stalled SCSI Bus after DATA Overrun");
   3460 		/* XXX: Need to issue SCSI reset at this point */
   3461 		break;
   3462 
   3463 	case ASYNC_KILLED_BUS:
   3464 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
   3465 		break;
   3466 
   3467 	case ASYNC_BUS_TRANSIT:
   3468 		mbox = ISP_READ(isp, OUTMAILBOX2);
   3469 		switch (mbox & 0x1c00) {
   3470 		case SXP_PINS_LVD_MODE:
   3471 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
   3472 			SDPARAM(isp)->isp_diffmode = 0;
   3473 			SDPARAM(isp)->isp_ultramode = 0;
   3474 			SDPARAM(isp)->isp_lvdmode = 1;
   3475 			break;
   3476 		case SXP_PINS_HVD_MODE:
   3477 			isp_prt(isp, ISP_LOGINFO,
   3478 			    "Transition to Differential mode");
   3479 			SDPARAM(isp)->isp_diffmode = 1;
   3480 			SDPARAM(isp)->isp_ultramode = 0;
   3481 			SDPARAM(isp)->isp_lvdmode = 0;
   3482 			break;
   3483 		case SXP_PINS_SE_MODE:
   3484 			isp_prt(isp, ISP_LOGINFO,
   3485 			    "Transition to Single Ended mode");
   3486 			SDPARAM(isp)->isp_diffmode = 0;
   3487 			SDPARAM(isp)->isp_ultramode = 1;
   3488 			SDPARAM(isp)->isp_lvdmode = 0;
   3489 			break;
   3490 		default:
   3491 			isp_prt(isp, ISP_LOGWARN,
   3492 			    "Transition to Unknown Mode 0x%x", mbox);
   3493 			break;
   3494 		}
   3495 		/*
   3496 		 * XXX: Set up to renegotiate again!
   3497 		 */
   3498 		/* Can only be for a 1080... */
   3499 		isp->isp_sendmarker |= (1 << bus);
   3500 		break;
   3501 
   3502 	case ASYNC_CMD_CMPLT:
   3503 		fast_post_handle = ISP_READ(isp, OUTMAILBOX1);
   3504 		isp_prt(isp, ISP_LOGDEBUG3, "fast post completion of %u",
   3505 		    fast_post_handle);
   3506 		break;
   3507 
   3508 	case ASYNC_CTIO_DONE:
   3509 #ifdef	ISP_TARGET_MODE
   3510 		/*
   3511 		 * Bus gets overloaded with the handle. Dual bus
   3512 		 * cards don't put bus# into the handle.
   3513 		 */
   3514 		bus = (ISP_READ(isp, OUTMAILBOX2) << 16) |
   3515 		    ISP_READ(isp, OUTMAILBOX1);
   3516 		isp_target_async(isp, bus, mbox);
   3517 #else
   3518 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
   3519 #endif
   3520 		break;
   3521 
   3522 	case ASYNC_LIP_F8:
   3523 	case ASYNC_LIP_OCCURRED:
   3524 		FCPARAM(isp)->isp_lipseq =
   3525 		    ISP_READ(isp, OUTMAILBOX1);
   3526 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3527 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3528 		isp->isp_sendmarker = 1;
   3529 		isp_mark_getpdb_all(isp);
   3530 		isp_async(isp, ISPASYNC_LIP, NULL);
   3531 #ifdef	ISP_TARGET_MODE
   3532 		isp_target_async(isp, bus, mbox);
   3533 #endif
   3534 		/*
   3535 		 * We've had problems with data corruption occuring on
   3536 		 * commands that complete (with no apparent error) after
   3537 		 * we receive a LIP. This has been observed mostly on
   3538 		 * Local Loop topologies. To be safe, let's just mark
   3539 		 * all active commands as dead.
   3540 		 */
   3541 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
   3542 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
   3543 			int i, j;
   3544 			for (i = j = 0; i < isp->isp_maxcmds; i++) {
   3545 				XS_T *xs;
   3546 				xs = isp->isp_xflist[i];
   3547 				if (xs != NULL) {
   3548 					j++;
   3549 					XS_SETERR(xs, HBA_BUSRESET);
   3550 				}
   3551 			}
   3552 			if (j) {
   3553 				isp_prt(isp, ISP_LOGERR,
   3554 				    "LIP destroyed %d active commands", j);
   3555 			}
   3556 		}
   3557 		break;
   3558 
   3559 	case ASYNC_LOOP_UP:
   3560 		isp->isp_sendmarker = 1;
   3561 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3562 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3563 		isp_mark_getpdb_all(isp);
   3564 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
   3565 #ifdef	ISP_TARGET_MODE
   3566 		isp_target_async(isp, bus, mbox);
   3567 #endif
   3568 		break;
   3569 
   3570 	case ASYNC_LOOP_DOWN:
   3571 		isp->isp_sendmarker = 1;
   3572 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3573 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   3574 		isp_mark_getpdb_all(isp);
   3575 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
   3576 #ifdef	ISP_TARGET_MODE
   3577 		isp_target_async(isp, bus, mbox);
   3578 #endif
   3579 		break;
   3580 
   3581 	case ASYNC_LOOP_RESET:
   3582 		isp->isp_sendmarker = 1;
   3583 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3584 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   3585 		isp_mark_getpdb_all(isp);
   3586 		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
   3587 #ifdef	ISP_TARGET_MODE
   3588 		isp_target_async(isp, bus, mbox);
   3589 #endif
   3590 		break;
   3591 
   3592 	case ASYNC_PDB_CHANGED:
   3593 		isp->isp_sendmarker = 1;
   3594 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   3595 		isp_mark_getpdb_all(isp);
   3596 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
   3597 		break;
   3598 
   3599 	case ASYNC_CHANGE_NOTIFY:
   3600 		/*
   3601 		 * Not correct, but it will force us to rescan the loop.
   3602 		 */
   3603 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   3604 		isp_mark_getpdb_all(isp);
   3605 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
   3606 		break;
   3607 
   3608 	case ASYNC_PTPMODE:
   3609 		if (FCPARAM(isp)->isp_onfabric)
   3610 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
   3611 		else
   3612 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
   3613 		isp_mark_getpdb_all(isp);
   3614 		isp->isp_sendmarker = 1;
   3615 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3616 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3617 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   3618 #ifdef	ISP_TARGET_MODE
   3619 		isp_target_async(isp, bus, mbox);
   3620 #endif
   3621 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
   3622 		break;
   3623 
   3624 	case ASYNC_CONNMODE:
   3625 		mbox = ISP_READ(isp, OUTMAILBOX1);
   3626 		isp_mark_getpdb_all(isp);
   3627 		switch (mbox) {
   3628 		case ISP_CONN_LOOP:
   3629 			isp_prt(isp, ISP_LOGINFO,
   3630 			    "Point-to-Point -> Loop mode");
   3631 			break;
   3632 		case ISP_CONN_PTP:
   3633 			isp_prt(isp, ISP_LOGINFO,
   3634 			    "Loop -> Point-to-Point mode");
   3635 			break;
   3636 		case ISP_CONN_BADLIP:
   3637 			isp_prt(isp, ISP_LOGWARN,
   3638 			    "Point-to-Point -> Loop mode (BAD LIP)");
   3639 			break;
   3640 		case ISP_CONN_FATAL:
   3641 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
   3642 			isp_reinit(isp);
   3643 #ifdef	ISP_TARGET_MODE
   3644 			isp_target_async(isp, bus, ASYNC_SYSTEM_ERROR);
   3645 #endif
   3646 			/* no point continuing after this */
   3647 			return (-1);
   3648 		case ISP_CONN_LOOPBACK:
   3649 			isp_prt(isp, ISP_LOGWARN,
   3650 			    "Looped Back in Point-to-Point mode");
   3651 			break;
   3652 		default:
   3653 			isp_prt(isp, ISP_LOGWARN,
   3654 			    "Unknown connection mode (0x%x)", mbox);
   3655 			break;
   3656 		}
   3657 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   3658 		isp->isp_sendmarker = 1;
   3659 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3660 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3661 		break;
   3662 
   3663 	default:
   3664 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
   3665 		break;
   3666 	}
   3667 	return (fast_post_handle);
   3668 }
   3669 
   3670 /*
   3671  * Handle other response entries. A pointer to the request queue output
   3672  * index is here in case we want to eat several entries at once, although
   3673  * this is not used currently.
   3674  */
   3675 
   3676 static int
   3677 isp_handle_other_response(struct ispsoftc *isp,
   3678     ispstatusreq_t *sp, u_int16_t *optrp)
   3679 {
   3680 	switch (sp->req_header.rqs_entry_type) {
   3681 	case RQSTYPE_STATUS_CONT:
   3682 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
   3683 		return (0);
   3684 	case RQSTYPE_ATIO:
   3685 	case RQSTYPE_CTIO:
   3686 	case RQSTYPE_ENABLE_LUN:
   3687 	case RQSTYPE_MODIFY_LUN:
   3688 	case RQSTYPE_NOTIFY:
   3689 	case RQSTYPE_NOTIFY_ACK:
   3690 	case RQSTYPE_CTIO1:
   3691 	case RQSTYPE_ATIO2:
   3692 	case RQSTYPE_CTIO2:
   3693 	case RQSTYPE_CTIO3:
   3694 #ifdef	ISP_TARGET_MODE
   3695 		return (isp_target_notify(isp, sp, optrp));
   3696 #else
   3697 		optrp = optrp;
   3698 		/* FALLTHROUGH */
   3699 #endif
   3700 	case RQSTYPE_REQUEST:
   3701 	default:
   3702 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, sp)) {
   3703 			return (0);
   3704 		}
   3705 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
   3706 		    sp->req_header.rqs_entry_type);
   3707 		return (-1);
   3708 	}
   3709 }
   3710 
   3711 static void
   3712 isp_parse_status(struct ispsoftc *isp, ispstatusreq_t *sp, XS_T *xs)
   3713 {
   3714 	switch (sp->req_completion_status & 0xff) {
   3715 	case RQCS_COMPLETE:
   3716 		if (XS_NOERR(xs)) {
   3717 			XS_SETERR(xs, HBA_NOERROR);
   3718 		}
   3719 		return;
   3720 
   3721 	case RQCS_INCOMPLETE:
   3722 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   3723 			isp_prt(isp, ISP_LOGDEBUG1,
   3724 			    "Selection Timeout for %d.%d.%d",
   3725 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3726 			if (XS_NOERR(xs)) {
   3727 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3728 			}
   3729 			return;
   3730 		}
   3731 		isp_prt(isp, ISP_LOGERR,
   3732 		    "command incomplete for %d.%d.%d, state 0x%x",
   3733 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
   3734 		    sp->req_state_flags);
   3735 		break;
   3736 
   3737 	case RQCS_DMA_ERROR:
   3738 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
   3739 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3740 		break;
   3741 
   3742 	case RQCS_TRANSPORT_ERROR:
   3743 	{
   3744 		char buf[172];
   3745 		buf[0] = 0;
   3746 		STRNCAT(buf, "states=>", sizeof buf);
   3747 		if (sp->req_state_flags & RQSF_GOT_BUS) {
   3748 			STRNCAT(buf, " GOT_BUS", sizeof buf);
   3749 		}
   3750 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
   3751 			STRNCAT(buf, " GOT_TGT", sizeof buf);
   3752 		}
   3753 		if (sp->req_state_flags & RQSF_SENT_CDB) {
   3754 			STRNCAT(buf, " SENT_CDB", sizeof buf);
   3755 		}
   3756 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
   3757 			STRNCAT(buf, " XFRD_DATA", sizeof buf);
   3758 		}
   3759 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   3760 			STRNCAT(buf, " GOT_STS", sizeof buf);
   3761 		}
   3762 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
   3763 			STRNCAT(buf, " GOT_SNS", sizeof buf);
   3764 		}
   3765 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
   3766 			STRNCAT(buf, " XFR_CMPLT", sizeof buf);
   3767 		}
   3768 		STRNCAT(buf, "\nstatus=>", sizeof buf);
   3769 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
   3770 			STRNCAT(buf, " Disconnect", sizeof buf);
   3771 		}
   3772 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
   3773 			STRNCAT(buf, " Sync_xfr", sizeof buf);
   3774 		}
   3775 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
   3776 			STRNCAT(buf, " Parity", sizeof buf);
   3777 		}
   3778 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   3779 			STRNCAT(buf, " Bus_Reset", sizeof buf);
   3780 		}
   3781 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
   3782 			STRNCAT(buf, " Device_Reset", sizeof buf);
   3783 		}
   3784 		if (sp->req_status_flags & RQSTF_ABORTED) {
   3785 			STRNCAT(buf, " Aborted", sizeof buf);
   3786 		}
   3787 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
   3788 			STRNCAT(buf, " Timeout", sizeof buf);
   3789 		}
   3790 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   3791 			STRNCAT(buf, " Negotiation", sizeof buf);
   3792 		}
   3793 		isp_prt(isp, ISP_LOGERR, "%s", buf);
   3794 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
   3795 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
   3796 		break;
   3797 	}
   3798 	case RQCS_RESET_OCCURRED:
   3799 		isp_prt(isp, ISP_LOGWARN,
   3800 		    "bus reset destroyed command for %d.%d.%d",
   3801 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3802 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3803 		if (XS_NOERR(xs)) {
   3804 			XS_SETERR(xs, HBA_BUSRESET);
   3805 		}
   3806 		return;
   3807 
   3808 	case RQCS_ABORTED:
   3809 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
   3810 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3811 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3812 		if (XS_NOERR(xs)) {
   3813 			XS_SETERR(xs, HBA_ABORTED);
   3814 		}
   3815 		return;
   3816 
   3817 	case RQCS_TIMEOUT:
   3818 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
   3819 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3820 		if (XS_NOERR(xs)) {
   3821 			XS_SETERR(xs, HBA_CMDTIMEOUT);
   3822 		}
   3823 		return;
   3824 
   3825 	case RQCS_DATA_OVERRUN:
   3826 		XS_RESID(xs) = sp->req_resid;
   3827 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
   3828 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3829 		if (XS_NOERR(xs)) {
   3830 			XS_SETERR(xs, HBA_DATAOVR);
   3831 		}
   3832 		return;
   3833 
   3834 	case RQCS_COMMAND_OVERRUN:
   3835 		isp_prt(isp, ISP_LOGERR,
   3836 		    "command overrun for command on %d.%d.%d",
   3837 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3838 		break;
   3839 
   3840 	case RQCS_STATUS_OVERRUN:
   3841 		isp_prt(isp, ISP_LOGERR,
   3842 		    "status overrun for command on %d.%d.%d",
   3843 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3844 		break;
   3845 
   3846 	case RQCS_BAD_MESSAGE:
   3847 		isp_prt(isp, ISP_LOGERR,
   3848 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
   3849 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3850 		break;
   3851 
   3852 	case RQCS_NO_MESSAGE_OUT:
   3853 		isp_prt(isp, ISP_LOGERR,
   3854 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
   3855 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3856 		break;
   3857 
   3858 	case RQCS_EXT_ID_FAILED:
   3859 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
   3860 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3861 		break;
   3862 
   3863 	case RQCS_IDE_MSG_FAILED:
   3864 		isp_prt(isp, ISP_LOGERR,
   3865 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
   3866 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3867 		break;
   3868 
   3869 	case RQCS_ABORT_MSG_FAILED:
   3870 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
   3871 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3872 		break;
   3873 
   3874 	case RQCS_REJECT_MSG_FAILED:
   3875 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
   3876 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3877 		break;
   3878 
   3879 	case RQCS_NOP_MSG_FAILED:
   3880 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
   3881 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3882 		break;
   3883 
   3884 	case RQCS_PARITY_ERROR_MSG_FAILED:
   3885 		isp_prt(isp, ISP_LOGERR,
   3886 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
   3887 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3888 		break;
   3889 
   3890 	case RQCS_DEVICE_RESET_MSG_FAILED:
   3891 		isp_prt(isp, ISP_LOGWARN,
   3892 		    "BUS DEVICE RESET rejected by %d.%d.%d",
   3893 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3894 		break;
   3895 
   3896 	case RQCS_ID_MSG_FAILED:
   3897 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
   3898 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3899 		break;
   3900 
   3901 	case RQCS_UNEXP_BUS_FREE:
   3902 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
   3903 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3904 		break;
   3905 
   3906 	case RQCS_DATA_UNDERRUN:
   3907 		XS_RESID(xs) = sp->req_resid;
   3908 		if (XS_NOERR(xs)) {
   3909 			XS_SETERR(xs, HBA_NOERROR);
   3910 		}
   3911 		return;
   3912 
   3913 	case RQCS_XACT_ERR1:
   3914 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
   3915 		    XS_TGT(xs), XS_LUN(xs));
   3916 		break;
   3917 
   3918 	case RQCS_XACT_ERR2:
   3919 		isp_prt(isp, ISP_LOGERR, xact2,
   3920 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
   3921 		break;
   3922 
   3923 	case RQCS_XACT_ERR3:
   3924 		isp_prt(isp, ISP_LOGERR, xact3,
   3925 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3926 		break;
   3927 
   3928 	case RQCS_BAD_ENTRY:
   3929 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
   3930 		break;
   3931 
   3932 	case RQCS_QUEUE_FULL:
   3933 		isp_prt(isp, ISP_LOGDEBUG1,
   3934 		    "internal queues full for %d.%d.%d status 0x%x", XS_TGT(xs),
   3935 		    XS_LUN(xs), XS_CHANNEL(xs), *XS_STSP(xs));
   3936 		/*
   3937 		 * If QFULL or some other status byte is set, then this
   3938 		 * isn't an error, per se.
   3939 		 */
   3940 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
   3941 			XS_SETERR(xs, HBA_NOERROR);
   3942 			return;
   3943 		}
   3944 		break;
   3945 
   3946 	case RQCS_PHASE_SKIPPED:
   3947 		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
   3948 		    XS_TGT(xs), XS_LUN(xs));
   3949 		break;
   3950 
   3951 	case RQCS_ARQS_FAILED:
   3952 		isp_prt(isp, ISP_LOGERR,
   3953 		    "Auto Request Sense failed for %d.%d.%d",
   3954 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3955 		if (XS_NOERR(xs)) {
   3956 			XS_SETERR(xs, HBA_ARQFAIL);
   3957 		}
   3958 		return;
   3959 
   3960 	case RQCS_WIDE_FAILED:
   3961 		isp_prt(isp, ISP_LOGERR,
   3962 		    "Wide Negotiation failed for %d.%d.%d",
   3963 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3964 		if (IS_SCSI(isp)) {
   3965 			sdparam *sdp = isp->isp_param;
   3966 			sdp += XS_CHANNEL(xs);
   3967 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
   3968 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3969 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3970 		}
   3971 		if (XS_NOERR(xs)) {
   3972 			XS_SETERR(xs, HBA_NOERROR);
   3973 		}
   3974 		return;
   3975 
   3976 	case RQCS_SYNCXFER_FAILED:
   3977 		isp_prt(isp, ISP_LOGERR,
   3978 		    "SDTR Message failed for target %d.%d.%d",
   3979 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3980 		if (IS_SCSI(isp)) {
   3981 			sdparam *sdp = isp->isp_param;
   3982 			sdp += XS_CHANNEL(xs);
   3983 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
   3984 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3985 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3986 		}
   3987 		break;
   3988 
   3989 	case RQCS_LVD_BUSERR:
   3990 		isp_prt(isp, ISP_LOGERR,
   3991 		    "Bad LVD condition while talking to %d.%d.%d",
   3992 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3993 		break;
   3994 
   3995 	case RQCS_PORT_UNAVAILABLE:
   3996 		/*
   3997 		 * No such port on the loop. Moral equivalent of SELTIMEO
   3998 		 */
   3999 		isp_prt(isp, ISP_LOGINFO,
   4000 		    "Port Unavailable for target %d", XS_TGT(xs));
   4001 		if (XS_NOERR(xs)) {
   4002 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4003 		}
   4004 		return;
   4005 	case RQCS_PORT_LOGGED_OUT:
   4006 		/*
   4007 		 * It was there (maybe)- treat as a selection timeout.
   4008 		 */
   4009 		isp_prt(isp, ISP_LOGINFO,
   4010 		    "port logout for target %d", XS_TGT(xs));
   4011 		if (XS_NOERR(xs)) {
   4012 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4013 		}
   4014 		return;
   4015 
   4016 	case RQCS_PORT_CHANGED:
   4017 		isp_prt(isp, ISP_LOGWARN,
   4018 		    "port changed for target %d", XS_TGT(xs));
   4019 		if (XS_NOERR(xs)) {
   4020 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4021 		}
   4022 		return;
   4023 
   4024 	case RQCS_PORT_BUSY:
   4025 		isp_prt(isp, ISP_LOGWARN,
   4026 		    "port busy for target %d", XS_TGT(xs));
   4027 		if (XS_NOERR(xs)) {
   4028 			XS_SETERR(xs, HBA_TGTBSY);
   4029 		}
   4030 		return;
   4031 
   4032 	default:
   4033 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
   4034 		    sp->req_completion_status);
   4035 		break;
   4036 	}
   4037 	if (XS_NOERR(xs)) {
   4038 		XS_SETERR(xs, HBA_BOTCH);
   4039 	}
   4040 }
   4041 
   4042 static void
   4043 isp_fastpost_complete(struct ispsoftc *isp, u_int16_t fph)
   4044 {
   4045 	XS_T *xs;
   4046 
   4047 	if (fph == 0) {
   4048 		return;
   4049 	}
   4050 	xs = isp_find_xs(isp, fph);
   4051 	if (xs == NULL) {
   4052 		isp_prt(isp, ISP_LOGWARN,
   4053 		    "Command for fast post handle 0x%x not found", fph);
   4054 		return;
   4055 	}
   4056 	isp_destroy_handle(isp, fph);
   4057 
   4058 	/*
   4059 	 * Since we don't have a result queue entry item,
   4060 	 * we must believe that SCSI status is zero and
   4061 	 * that all data transferred.
   4062 	 */
   4063 	XS_SET_STATE_STAT(isp, xs, NULL);
   4064 	XS_RESID(xs) = 0;
   4065 	*XS_STSP(xs) = SCSI_GOOD;
   4066 	if (XS_XFRLEN(xs)) {
   4067 		ISP_DMAFREE(isp, xs, fph);
   4068 	}
   4069 	if (isp->isp_nactive)
   4070 		isp->isp_nactive--;
   4071 	isp_done(xs);
   4072 }
   4073 
   4074 #define	HIBYT(x)			((x) >> 0x8)
   4075 #define	LOBYT(x)			((x)  & 0xff)
   4076 #define	ISPOPMAP(a, b)			(((a) << 8) | (b))
   4077 static u_int16_t mbpscsi[] = {
   4078 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   4079 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   4080 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   4081 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   4082 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   4083 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   4084 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   4085 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   4086 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   4087 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
   4088 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
   4089 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   4090 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   4091 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   4092 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   4093 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   4094 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   4095 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
   4096 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
   4097 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   4098 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
   4099 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
   4100 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
   4101 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
   4102 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
   4103 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
   4104 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
   4105 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   4106 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
   4107 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   4108 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   4109 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   4110 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
   4111 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   4112 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   4113 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   4114 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
   4115 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   4116 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   4117 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
   4118 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   4119 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   4120 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
   4121 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   4122 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   4123 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   4124 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   4125 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   4126 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   4127 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   4128 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   4129 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   4130 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
   4131 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
   4132 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   4133 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   4134 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   4135 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   4136 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
   4137 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   4138 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   4139 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   4140 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   4141 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   4142 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   4143 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   4144 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   4145 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
   4146 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   4147 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
   4148 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
   4149 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
   4150 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
   4151 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
   4152 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
   4153 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
   4154 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   4155 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   4156 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   4157 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   4158 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
   4159 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
   4160 	ISPOPMAP(0xdf, 0xdf),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
   4161 	ISPOPMAP(0xff, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
   4162 	ISPOPMAP(0xcf, 0xff),	/* 0x54: EXECUTE IOCB A64 */
   4163 	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
   4164 	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
   4165 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   4166 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   4167 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   4168 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
   4169 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
   4170 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
   4171 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
   4172 };
   4173 
   4174 #ifndef	ISP_STRIPPED
   4175 static char *scsi_mbcmd_names[] = {
   4176 	"NO-OP",
   4177 	"LOAD RAM",
   4178 	"EXEC FIRMWARE",
   4179 	"DUMP RAM",
   4180 	"WRITE RAM WORD",
   4181 	"READ RAM WORD",
   4182 	"MAILBOX REG TEST",
   4183 	"VERIFY CHECKSUM",
   4184 	"ABOUT FIRMWARE",
   4185 	NULL,
   4186 	NULL,
   4187 	NULL,
   4188 	NULL,
   4189 	NULL,
   4190 	"CHECK FIRMWARE",
   4191 	NULL,
   4192 	"INIT REQUEST QUEUE",
   4193 	"INIT RESULT QUEUE",
   4194 	"EXECUTE IOCB",
   4195 	"WAKE UP",
   4196 	"STOP FIRMWARE",
   4197 	"ABORT",
   4198 	"ABORT DEVICE",
   4199 	"ABORT TARGET",
   4200 	"BUS RESET",
   4201 	"STOP QUEUE",
   4202 	"START QUEUE",
   4203 	"SINGLE STEP QUEUE",
   4204 	"ABORT QUEUE",
   4205 	"GET DEV QUEUE STATUS",
   4206 	NULL,
   4207 	"GET FIRMWARE STATUS",
   4208 	"GET INIT SCSI ID",
   4209 	"GET SELECT TIMEOUT",
   4210 	"GET RETRY COUNT",
   4211 	"GET TAG AGE LIMIT",
   4212 	"GET CLOCK RATE",
   4213 	"GET ACT NEG STATE",
   4214 	"GET ASYNC DATA SETUP TIME",
   4215 	"GET PCI PARAMS",
   4216 	"GET TARGET PARAMS",
   4217 	"GET DEV QUEUE PARAMS",
   4218 	"GET RESET DELAY PARAMS",
   4219 	NULL,
   4220 	NULL,
   4221 	NULL,
   4222 	NULL,
   4223 	NULL,
   4224 	"SET INIT SCSI ID",
   4225 	"SET SELECT TIMEOUT",
   4226 	"SET RETRY COUNT",
   4227 	"SET TAG AGE LIMIT",
   4228 	"SET CLOCK RATE",
   4229 	"SET ACT NEG STATE",
   4230 	"SET ASYNC DATA SETUP TIME",
   4231 	"SET PCI CONTROL PARAMS",
   4232 	"SET TARGET PARAMS",
   4233 	"SET DEV QUEUE PARAMS",
   4234 	"SET RESET DELAY PARAMS",
   4235 	NULL,
   4236 	NULL,
   4237 	NULL,
   4238 	NULL,
   4239 	NULL,
   4240 	"RETURN BIOS BLOCK ADDR",
   4241 	"WRITE FOUR RAM WORDS",
   4242 	"EXEC BIOS IOCB",
   4243 	NULL,
   4244 	NULL,
   4245 	"SET SYSTEM PARAMETER",
   4246 	"GET SYSTEM PARAMETER",
   4247 	NULL,
   4248 	"GET SCAM CONFIGURATION",
   4249 	"SET SCAM CONFIGURATION",
   4250 	"SET FIRMWARE FEATURES",
   4251 	"GET FIRMWARE FEATURES",
   4252 	NULL,
   4253 	NULL,
   4254 	NULL,
   4255 	NULL,
   4256 	"LOAD RAM A64",
   4257 	"DUMP RAM A64",
   4258 	"INITIALIZE REQUEST QUEUE A64",
   4259 	"INITIALIZE RESPONSE QUEUE A64",
   4260 	"EXECUTE IOCB A64",
   4261 	"ENABLE TARGET MODE",
   4262 	"GET TARGET MODE STATE",
   4263 	NULL,
   4264 	NULL,
   4265 	NULL,
   4266 	"SET DATA OVERRUN RECOVERY MODE",
   4267 	"GET DATA OVERRUN RECOVERY MODE",
   4268 	"SET HOST DATA",
   4269 	"GET NOST DATA",
   4270 };
   4271 #endif
   4272 
   4273 static u_int16_t mbpfc[] = {
   4274 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   4275 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   4276 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   4277 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   4278 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   4279 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   4280 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   4281 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   4282 	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   4283 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
   4284 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
   4285 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   4286 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   4287 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   4288 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   4289 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   4290 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   4291 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
   4292 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
   4293 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   4294 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
   4295 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
   4296 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
   4297 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
   4298 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
   4299 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
   4300 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
   4301 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   4302 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
   4303 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   4304 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   4305 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   4306 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
   4307 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
   4308 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   4309 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
   4310 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
   4311 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
   4312 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
   4313 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
   4314 	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
   4315 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
   4316 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
   4317 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   4318 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   4319 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   4320 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   4321 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   4322 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
   4323 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
   4324 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   4325 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
   4326 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
   4327 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
   4328 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
   4329 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
   4330 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
   4331 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
   4332 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
   4333 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   4334 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   4335 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   4336 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   4337 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   4338 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
   4339 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
   4340 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
   4341 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
   4342 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   4343 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
   4344 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
   4345 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
   4346 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
   4347 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
   4348 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
   4349 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
   4350 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   4351 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   4352 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   4353 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   4354 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
   4355 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
   4356 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
   4357 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
   4358 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   4359 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
   4360 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   4361 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   4362 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   4363 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   4364 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
   4365 	ISPOPMAP(0x00, 0x00),	/* 0x5b: */
   4366 	ISPOPMAP(0x00, 0x00),	/* 0x5c: */
   4367 	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
   4368 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
   4369 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
   4370 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
   4371 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
   4372 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
   4373 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   4374 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
   4375 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
   4376 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
   4377 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
   4378 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
   4379 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
   4380 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
   4381 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
   4382 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
   4383 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
   4384 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
   4385 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
   4386 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
   4387 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
   4388 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
   4389 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
   4390 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
   4391 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
   4392 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
   4393 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
   4394 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
   4395 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
   4396 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
   4397 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
   4398 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
   4399 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
   4400 	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
   4401 };
   4402 
   4403 #ifndef	ISP_STRIPPED
   4404 static char *fc_mbcmd_names[] = {
   4405 	"NO-OP",
   4406 	"LOAD RAM",
   4407 	"EXEC FIRMWARE",
   4408 	"DUMP RAM",
   4409 	"WRITE RAM WORD",
   4410 	"READ RAM WORD",
   4411 	"MAILBOX REG TEST",
   4412 	"VERIFY CHECKSUM",
   4413 	"ABOUT FIRMWARE",
   4414 	"LOAD RAM",
   4415 	"DUMP RAM",
   4416 	NULL,
   4417 	NULL,
   4418 	NULL,
   4419 	"CHECK FIRMWARE",
   4420 	NULL,
   4421 	"INIT REQUEST QUEUE",
   4422 	"INIT RESULT QUEUE",
   4423 	"EXECUTE IOCB",
   4424 	"WAKE UP",
   4425 	"STOP FIRMWARE",
   4426 	"ABORT",
   4427 	"ABORT DEVICE",
   4428 	"ABORT TARGET",
   4429 	"BUS RESET",
   4430 	"STOP QUEUE",
   4431 	"START QUEUE",
   4432 	"SINGLE STEP QUEUE",
   4433 	"ABORT QUEUE",
   4434 	"GET DEV QUEUE STATUS",
   4435 	NULL,
   4436 	"GET FIRMWARE STATUS",
   4437 	"GET LOOP ID",
   4438 	NULL,
   4439 	"GET RETRY COUNT",
   4440 	NULL,
   4441 	NULL,
   4442 	NULL,
   4443 	NULL,
   4444 	NULL,
   4445 	"GET FIRMWARE OPTIONS",
   4446 	"GET PORT QUEUE PARAMS",
   4447 	NULL,
   4448 	NULL,
   4449 	NULL,
   4450 	NULL,
   4451 	NULL,
   4452 	NULL,
   4453 	NULL,
   4454 	NULL,
   4455 	"SET RETRY COUNT",
   4456 	NULL,
   4457 	NULL,
   4458 	NULL,
   4459 	NULL,
   4460 	NULL,
   4461 	"SET FIRMWARE OPTIONS",
   4462 	"SET PORT QUEUE PARAMS",
   4463 	NULL,
   4464 	NULL,
   4465 	NULL,
   4466 	NULL,
   4467 	NULL,
   4468 	NULL,
   4469 	"LOOP PORT BYPASS",
   4470 	"LOOP PORT ENABLE",
   4471 	"GET RESOURCE COUNTS",
   4472 	"REQUEST NON PARTICIPATING MODE",
   4473 	NULL,
   4474 	NULL,
   4475 	NULL,
   4476 	"GET PORT DATABASE,, ENHANCED",
   4477 	NULL,
   4478 	NULL,
   4479 	NULL,
   4480 	NULL,
   4481 	NULL,
   4482 	NULL,
   4483 	NULL,
   4484 	NULL,
   4485 	NULL,
   4486 	NULL,
   4487 	NULL,
   4488 	NULL,
   4489 	"EXECUTE IOCB A64",
   4490 	NULL,
   4491 	NULL,
   4492 	NULL,
   4493 	NULL,
   4494 	NULL,
   4495 	NULL,
   4496 	NULL,
   4497 	NULL,
   4498 	"GET/SET DATA RATE",
   4499 	NULL,
   4500 	NULL,
   4501 	"INIT FIRMWARE",
   4502 	NULL,
   4503 	"INIT LIP",
   4504 	"GET FC-AL POSITION MAP",
   4505 	"GET PORT DATABASE",
   4506 	"CLEAR ACA",
   4507 	"TARGET RESET",
   4508 	"CLEAR TASK SET",
   4509 	"ABORT TASK SET",
   4510 	"GET FW STATE",
   4511 	"GET PORT NAME",
   4512 	"GET LINK STATUS",
   4513 	"INIT LIP RESET",
   4514 	NULL,
   4515 	"SEND SNS",
   4516 	"FABRIC LOGIN",
   4517 	"SEND CHANGE REQUEST",
   4518 	"FABRIC LOGOUT",
   4519 	"INIT LIP LOGIN",
   4520 	NULL,
   4521 	"LOGIN LOOP PORT",
   4522 	"GET PORT/NODE NAME LIST",
   4523 	"SET VENDOR ID",
   4524 	"INITIALIZE IP MAILBOX",
   4525 	NULL,
   4526 	NULL,
   4527 	NULL,
   4528 	NULL,
   4529 	"Get ID List",
   4530 	"SEND LFA",
   4531 	"Lun RESET"
   4532 };
   4533 #endif
   4534 
   4535 static void
   4536 isp_mboxcmd(struct ispsoftc *isp, mbreg_t *mbp, int logmask)
   4537 {
   4538 	char *cname, *xname, tname[16], mname[16];
   4539 	unsigned int lim, ibits, obits, box, opcode;
   4540 	u_int16_t *mcp;
   4541 
   4542 	if (IS_FC(isp)) {
   4543 		mcp = mbpfc;
   4544 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   4545 	} else {
   4546 		mcp = mbpscsi;
   4547 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   4548 	}
   4549 
   4550 	if ((opcode = mbp->param[0]) >= lim) {
   4551 		mbp->param[0] = MBOX_INVALID_COMMAND;
   4552 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
   4553 		return;
   4554 	}
   4555 
   4556 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   4557 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   4558 
   4559 	if (ibits == 0 && obits == 0) {
   4560 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
   4561 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
   4562 		return;
   4563 	}
   4564 
   4565 	/*
   4566 	 * Get exclusive usage of mailbox registers.
   4567 	 */
   4568 	MBOX_ACQUIRE(isp);
   4569 
   4570 	for (box = 0; box < MAX_MAILBOX; box++) {
   4571 		if (ibits & (1 << box)) {
   4572 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   4573 		}
   4574 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   4575 	}
   4576 
   4577 	isp->isp_lastmbxcmd = opcode;
   4578 
   4579 	/*
   4580 	 * We assume that we can't overwrite a previous command.
   4581 	 */
   4582 	isp->isp_mboxbsy = obits;
   4583 
   4584 	/*
   4585 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   4586 	 */
   4587 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   4588 
   4589 	/*
   4590 	 * While we haven't finished the command, spin our wheels here.
   4591 	 */
   4592 	MBOX_WAIT_COMPLETE(isp);
   4593 
   4594 	/*
   4595 	 * Copy back output registers.
   4596 	 */
   4597 	for (box = 0; box < MAX_MAILBOX; box++) {
   4598 		if (obits & (1 << box)) {
   4599 			mbp->param[box] = isp->isp_mboxtmp[box];
   4600 		}
   4601 	}
   4602 
   4603 	MBOX_RELEASE(isp);
   4604 
   4605 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
   4606 		return;
   4607 	}
   4608 #ifdef	ISP_STRIPPED
   4609 	cname = NULL;
   4610 #else
   4611 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
   4612 #endif
   4613 	if (cname == NULL) {
   4614 		cname = tname;
   4615 		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
   4616 	}
   4617 
   4618 	/*
   4619 	 * Just to be chatty here...
   4620 	 */
   4621 	xname = NULL;
   4622 	switch (mbp->param[0]) {
   4623 	case MBOX_COMMAND_COMPLETE:
   4624 		break;
   4625 	case MBOX_INVALID_COMMAND:
   4626 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
   4627 			xname = "INVALID COMMAND";
   4628 		break;
   4629 	case MBOX_HOST_INTERFACE_ERROR:
   4630 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
   4631 			xname = "HOST INTERFACE ERROR";
   4632 		break;
   4633 	case MBOX_TEST_FAILED:
   4634 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
   4635 			xname = "TEST FAILED";
   4636 		break;
   4637 	case MBOX_COMMAND_ERROR:
   4638 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
   4639 			xname = "COMMAND ERROR";
   4640 		break;
   4641 	case MBOX_COMMAND_PARAM_ERROR:
   4642 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
   4643 			xname = "COMMAND PARAMETER ERROR";
   4644 		break;
   4645 	case MBOX_LOOP_ID_USED:
   4646 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
   4647 			xname = "LOOP ID ALREADY IN USE";
   4648 		break;
   4649 	case MBOX_PORT_ID_USED:
   4650 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
   4651 			xname = "PORT ID ALREADY IN USE";
   4652 		break;
   4653 	case MBOX_ALL_IDS_USED:
   4654 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
   4655 			xname = "ALL LOOP IDS IN USE";
   4656 		break;
   4657 	case 0:		/* special case */
   4658 		xname = "TIMEOUT";
   4659 		break;
   4660 	default:
   4661 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
   4662 		xname = mname;
   4663 		break;
   4664 	}
   4665 	if (xname)
   4666 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
   4667 		    cname, xname);
   4668 }
   4669 
   4670 static void
   4671 isp_fw_state(struct ispsoftc *isp)
   4672 {
   4673 	if (IS_FC(isp)) {
   4674 		mbreg_t mbs;
   4675 		fcparam *fcp = isp->isp_param;
   4676 
   4677 		mbs.param[0] = MBOX_GET_FW_STATE;
   4678 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4679 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   4680 			fcp->isp_fwstate = mbs.param[1];
   4681 		}
   4682 	}
   4683 }
   4684 
   4685 static void
   4686 isp_update(struct ispsoftc *isp)
   4687 {
   4688 	int bus, upmask;
   4689 
   4690 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
   4691 		if (upmask & (1 << bus)) {
   4692 			isp_update_bus(isp, bus);
   4693 		}
   4694 		upmask &= ~(1 << bus);
   4695 	}
   4696 }
   4697 
   4698 static void
   4699 isp_update_bus(struct ispsoftc *isp, int bus)
   4700 {
   4701 	int tgt;
   4702 	mbreg_t mbs;
   4703 	sdparam *sdp;
   4704 
   4705 	isp->isp_update &= ~(1 << bus);
   4706 	if (IS_FC(isp)) {
   4707 		/*
   4708 		 * There are no 'per-bus' settings for Fibre Channel.
   4709 		 */
   4710 		return;
   4711 	}
   4712 	sdp = isp->isp_param;
   4713 	sdp += bus;
   4714 
   4715 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4716 		u_int16_t flags, period, offset;
   4717 		int get;
   4718 
   4719 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   4720 			sdp->isp_devparam[tgt].dev_update = 0;
   4721 			sdp->isp_devparam[tgt].dev_refresh = 0;
   4722 			isp_prt(isp, ISP_LOGDEBUG0,
   4723 	 		    "skipping target %d bus %d update", tgt, bus);
   4724 			continue;
   4725 		}
   4726 		/*
   4727 		 * If the goal is to update the status of the device,
   4728 		 * take what's in goal_flags and try and set the device
   4729 		 * toward that. Otherwise, if we're just refreshing the
   4730 		 * current device state, get the current parameters.
   4731 		 */
   4732 
   4733 		/*
   4734 		 * Refresh overrides set
   4735 		 */
   4736 		if (sdp->isp_devparam[tgt].dev_refresh) {
   4737 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   4738 			sdp->isp_devparam[tgt].dev_refresh = 0;
   4739 			get = 1;
   4740 		} else if (sdp->isp_devparam[tgt].dev_update) {
   4741 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   4742 			/*
   4743 			 * Make sure goal_flags has "Renegotiate on Error"
   4744 			 * on and "Freeze Queue on Error" off.
   4745 			 */
   4746 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
   4747 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
   4748 
   4749 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
   4750 
   4751 			/*
   4752 			 * Insist that PARITY must be enabled
   4753 			 * if SYNC or WIDE is enabled.
   4754 			 */
   4755 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
   4756 				mbs.param[2] |= DPARM_PARITY;
   4757 			}
   4758 
   4759 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
   4760 				mbs.param[3] = 0;
   4761 			} else {
   4762 				mbs.param[3] =
   4763 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
   4764 				    (sdp->isp_devparam[tgt].goal_period);
   4765 			}
   4766 			/*
   4767 			 * A command completion later that has
   4768 			 * RQSTF_NEGOTIATION set can cause
   4769 			 * the dev_refresh/announce cycle also.
   4770 			 *
   4771 			 * Note: It is really important to update our current
   4772 			 * flags with at least the state of TAG capabilities-
   4773 			 * otherwise we might try and send a tagged command
   4774 			 * when we have it all turned off. So change it here
   4775 			 * to say that current already matches goal.
   4776 			 */
   4777 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
   4778 			sdp->isp_devparam[tgt].actv_flags |=
   4779 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
   4780 			isp_prt(isp, ISP_LOGDEBUG0,
   4781 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
   4782 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
   4783 			    mbs.param[3] & 0xff);
   4784 			sdp->isp_devparam[tgt].dev_update = 0;
   4785 			sdp->isp_devparam[tgt].dev_refresh = 1;
   4786 			get = 0;
   4787 		} else {
   4788 			continue;
   4789 		}
   4790 		mbs.param[1] = (bus << 15) | (tgt << 8);
   4791 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4792 		if (get == 0) {
   4793 			isp->isp_sendmarker |= (1 << bus);
   4794 			continue;
   4795 		}
   4796 		flags = mbs.param[2];
   4797 		period = mbs.param[3] & 0xff;
   4798 		offset = mbs.param[3] >> 8;
   4799 		sdp->isp_devparam[tgt].actv_flags = flags;
   4800 		sdp->isp_devparam[tgt].actv_period = period;
   4801 		sdp->isp_devparam[tgt].actv_offset = offset;
   4802 		get = (bus << 16) | tgt;
   4803 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
   4804 	}
   4805 
   4806 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4807 		if (sdp->isp_devparam[tgt].dev_update ||
   4808 		    sdp->isp_devparam[tgt].dev_refresh) {
   4809 			isp->isp_update |= (1 << bus);
   4810 			break;
   4811 		}
   4812 	}
   4813 }
   4814 
   4815 static void
   4816 isp_setdfltparm(struct ispsoftc *isp, int channel)
   4817 {
   4818 	int tgt;
   4819 	mbreg_t mbs;
   4820 	sdparam *sdp;
   4821 
   4822 	if (IS_FC(isp)) {
   4823 		fcparam *fcp = (fcparam *) isp->isp_param;
   4824 		int nvfail;
   4825 
   4826 		fcp += channel;
   4827 		if (fcp->isp_gotdparms) {
   4828 			return;
   4829 		}
   4830 		fcp->isp_gotdparms = 1;
   4831 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
   4832 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
   4833 		fcp->isp_execthrottle = ISP_EXEC_THROTTLE;
   4834 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
   4835 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
   4836 		/* Platform specific.... */
   4837 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
   4838 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
   4839 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
   4840 		fcp->isp_fwoptions = 0;
   4841 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
   4842 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
   4843 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
   4844 #ifndef	ISP_NO_FASTPOST_FC
   4845 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
   4846 #endif
   4847 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
   4848 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
   4849 
   4850 		/*
   4851 		 * Make sure this is turned off now until we get
   4852 		 * extended options from NVRAM
   4853 		 */
   4854 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
   4855 
   4856 		/*
   4857 		 * Now try and read NVRAM unless told to not do so.
   4858 		 * This will set fcparam's isp_nodewwn && isp_portwwn.
   4859 		 */
   4860 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4861 		    	nvfail = isp_read_nvram(isp);
   4862 			if (nvfail)
   4863 				isp->isp_confopts |= ISP_CFG_NONVRAM;
   4864 		} else {
   4865 			nvfail = 1;
   4866 		}
   4867 		/*
   4868 		 * Set node && port to override platform set defaults
   4869 		 * unless the nvram read failed (or none was done),
   4870 		 * or the platform code wants to use what had been
   4871 		 * set in the defaults.
   4872 		 */
   4873 		if (nvfail || (isp->isp_confopts & ISP_CFG_OWNWWN)) {
   4874 			isp_prt(isp, ISP_LOGCONFIG,
   4875 			    "Using Node WWN 0x%08x%08x, Port WWN 0x%08x%08x",
   4876 			    (u_int32_t) (DEFAULT_NODEWWN(isp) >> 32),
   4877 			    (u_int32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff),
   4878 			    (u_int32_t) (DEFAULT_PORTWWN(isp) >> 32),
   4879 			    (u_int32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
   4880 			isp->isp_confopts |= ISP_CFG_OWNWWN;
   4881 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
   4882 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
   4883 		} else {
   4884 			/*
   4885 			 * We always start out with values derived
   4886 			 * from NVRAM or our platform default.
   4887 			 */
   4888 			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
   4889 			ISP_PORTWWN(isp) = fcp->isp_portwwn;
   4890 		}
   4891 		return;
   4892 	}
   4893 
   4894 	sdp = (sdparam *) isp->isp_param;
   4895 	sdp += channel;
   4896 
   4897 	/*
   4898 	 * Been there, done that, got the T-shirt...
   4899 	 */
   4900 	if (sdp->isp_gotdparms) {
   4901 		return;
   4902 	}
   4903 	sdp->isp_gotdparms = 1;
   4904 
   4905 	/*
   4906 	 * Establish some default parameters.
   4907 	 */
   4908 	sdp->isp_cmd_dma_burst_enable = 0;
   4909 	sdp->isp_data_dma_burst_enabl = 1;
   4910 	sdp->isp_fifo_threshold = 0;
   4911 	sdp->isp_initiator_id = DEFAULT_IID(isp);
   4912 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   4913 		sdp->isp_async_data_setup = 9;
   4914 	} else {
   4915 		sdp->isp_async_data_setup = 6;
   4916 	}
   4917 	sdp->isp_selection_timeout = 250;
   4918 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
   4919 	sdp->isp_tag_aging = 8;
   4920 	sdp->isp_bus_reset_delay = 5;
   4921 	/*
   4922 	 * Don't retry selection, busy or queue full automatically- reflect
   4923 	 * these back to us.
   4924 	 */
   4925 	sdp->isp_retry_count = 0;
   4926 	sdp->isp_retry_delay = 0;
   4927 
   4928 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4929 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
   4930 		sdp->isp_devparam[tgt].dev_enable = 1;
   4931 	}
   4932 
   4933 	/*
   4934 	 * If we've not been told to avoid reading NVRAM, try and read it.
   4935 	 * If we're successful reading it, we can then return because NVRAM
   4936 	 * will tell us what the desired settings are. Otherwise, we establish
   4937 	 * some reasonable 'fake' nvram and goal defaults.
   4938 	 */
   4939 
   4940 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4941 		if (isp_read_nvram(isp) == 0) {
   4942 			return;
   4943 		}
   4944 	}
   4945 
   4946 	/*
   4947 	 * Now try and see whether we have specific values for them.
   4948 	 */
   4949 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4950 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   4951 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
   4952 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4953 			sdp->isp_req_ack_active_neg = 1;
   4954 			sdp->isp_data_line_active_neg = 1;
   4955 		} else {
   4956 			sdp->isp_req_ack_active_neg =
   4957 			    (mbs.param[1+channel] >> 4) & 0x1;
   4958 			sdp->isp_data_line_active_neg =
   4959 			    (mbs.param[1+channel] >> 5) & 0x1;
   4960 		}
   4961 	}
   4962 
   4963 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
   4964 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   4965 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   4966 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   4967 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
   4968 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   4969 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   4970 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   4971 
   4972 	/*
   4973 	 * The trick here is to establish a default for the default (honk!)
   4974 	 * state (goal_flags). Then try and get the current status from
   4975 	 * the card to fill in the current state. We don't, in fact, set
   4976 	 * the default to the SAFE default state- that's not the goal state.
   4977 	 */
   4978 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4979 		u_int8_t off, per;
   4980 		sdp->isp_devparam[tgt].actv_offset = 0;
   4981 		sdp->isp_devparam[tgt].actv_period = 0;
   4982 		sdp->isp_devparam[tgt].actv_flags = 0;
   4983 
   4984 		sdp->isp_devparam[tgt].goal_flags =
   4985 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
   4986 
   4987 		/*
   4988 		 * We default to Wide/Fast for versions less than a 1040
   4989 		 * (unless it's SBus).
   4990 		 */
   4991 		if (IS_ULTRA3(isp)) {
   4992 			off = ISP_80M_SYNCPARMS >> 8;
   4993 			per = ISP_80M_SYNCPARMS & 0xff;
   4994 		} else if (IS_ULTRA2(isp)) {
   4995 			off = ISP_40M_SYNCPARMS >> 8;
   4996 			per = ISP_40M_SYNCPARMS & 0xff;
   4997 		} else if (IS_1240(isp)) {
   4998 			off = ISP_20M_SYNCPARMS >> 8;
   4999 			per = ISP_20M_SYNCPARMS & 0xff;
   5000 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
   5001 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
   5002 		    (isp->isp_bustype == ISP_BT_PCI &&
   5003 		    isp->isp_type < ISP_HA_SCSI_1040) ||
   5004 		    (isp->isp_clock && isp->isp_clock < 60) ||
   5005 		    (sdp->isp_ultramode == 0)) {
   5006 			off = ISP_10M_SYNCPARMS >> 8;
   5007 			per = ISP_10M_SYNCPARMS & 0xff;
   5008 		} else {
   5009 			off = ISP_20M_SYNCPARMS_1040 >> 8;
   5010 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
   5011 		}
   5012 		sdp->isp_devparam[tgt].goal_offset =
   5013 		    sdp->isp_devparam[tgt].nvrm_offset = off;
   5014 		sdp->isp_devparam[tgt].goal_period =
   5015 		    sdp->isp_devparam[tgt].nvrm_period = per;
   5016 
   5017 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
   5018 		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5019 		    sdp->isp_devparam[tgt].nvrm_offset,
   5020 		    sdp->isp_devparam[tgt].nvrm_period);
   5021 	}
   5022 }
   5023 
   5024 /*
   5025  * Re-initialize the ISP and complete all orphaned commands
   5026  * with a 'botched' notice. The reset/init routines should
   5027  * not disturb an already active list of commands.
   5028  *
   5029  * Locks held prior to coming here.
   5030  */
   5031 
   5032 void
   5033 isp_reinit(struct ispsoftc *isp)
   5034 {
   5035 	XS_T *xs;
   5036 	u_int16_t handle;
   5037 
   5038 	isp_reset(isp);
   5039 	if (isp->isp_state != ISP_RESETSTATE) {
   5040 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
   5041 		goto skip;
   5042 	}
   5043 	isp_init(isp);
   5044 	if (isp->isp_role == ISP_ROLE_NONE) {
   5045 		goto skip;
   5046 	}
   5047 	if (isp->isp_state == ISP_INITSTATE) {
   5048 		isp->isp_state = ISP_RUNSTATE;
   5049 	}
   5050 	if (isp->isp_state != ISP_RUNSTATE) {
   5051 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot restart card");
   5052 	}
   5053 skip:
   5054 	isp->isp_nactive = 0;
   5055 
   5056 	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
   5057 		xs = isp_find_xs(isp, handle);
   5058 		if (xs == NULL) {
   5059 			continue;
   5060 		}
   5061 		isp_destroy_handle(isp, handle);
   5062 		if (XS_XFRLEN(xs)) {
   5063 			ISP_DMAFREE(isp, xs, handle);
   5064 			XS_RESID(xs) = XS_XFRLEN(xs);
   5065 		} else {
   5066 			XS_RESID(xs) = 0;
   5067 		}
   5068 		XS_SETERR(xs, HBA_BUSRESET);
   5069 		isp_done(xs);
   5070 	}
   5071 }
   5072 
   5073 /*
   5074  * NVRAM Routines
   5075  */
   5076 static int
   5077 isp_read_nvram(struct ispsoftc *isp)
   5078 {
   5079 	int i, amt;
   5080 	u_int8_t csum, minversion;
   5081 	union {
   5082 		u_int8_t _x[ISP2100_NVRAM_SIZE];
   5083 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
   5084 	} _n;
   5085 #define	nvram_data	_n._x
   5086 #define	nvram_words	_n._s
   5087 
   5088 	if (IS_FC(isp)) {
   5089 		amt = ISP2100_NVRAM_SIZE;
   5090 		minversion = 1;
   5091 	} else if (IS_ULTRA2(isp)) {
   5092 		amt = ISP1080_NVRAM_SIZE;
   5093 		minversion = 0;
   5094 	} else {
   5095 		amt = ISP_NVRAM_SIZE;
   5096 		minversion = 2;
   5097 	}
   5098 
   5099 	/*
   5100 	 * Just read the first two words first to see if we have a valid
   5101 	 * NVRAM to continue reading the rest with.
   5102 	 */
   5103 	for (i = 0; i < 2; i++) {
   5104 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   5105 	}
   5106 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   5107 	    nvram_data[2] != 'P') {
   5108 		if (isp->isp_bustype != ISP_BT_SBUS) {
   5109 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
   5110 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
   5111 			    nvram_data[0], nvram_data[1], nvram_data[2]);
   5112 		}
   5113 		return (-1);
   5114 	}
   5115 	for (i = 2; i < amt>>1; i++) {
   5116 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   5117 	}
   5118 	for (csum = 0, i = 0; i < amt; i++) {
   5119 		csum += nvram_data[i];
   5120 	}
   5121 	if (csum != 0) {
   5122 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
   5123 		return (-1);
   5124 	}
   5125 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   5126 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
   5127 		    ISP_NVRAM_VERSION(nvram_data));
   5128 		return (-1);
   5129 	}
   5130 
   5131 	if (IS_ULTRA3(isp)) {
   5132 		isp_parse_nvram_12160(isp, 0, nvram_data);
   5133 		isp_parse_nvram_12160(isp, 1, nvram_data);
   5134 	} else if (IS_1080(isp)) {
   5135 		isp_parse_nvram_1080(isp, 0, nvram_data);
   5136 	} else if (IS_1280(isp) || IS_1240(isp)) {
   5137 		isp_parse_nvram_1080(isp, 0, nvram_data);
   5138 		isp_parse_nvram_1080(isp, 1, nvram_data);
   5139 	} else if (IS_SCSI(isp)) {
   5140 		isp_parse_nvram_1020(isp, nvram_data);
   5141 	} else {
   5142 		isp_parse_nvram_2100(isp, nvram_data);
   5143 	}
   5144 	return (0);
   5145 #undef	nvram_data
   5146 #undef	nvram_words
   5147 }
   5148 
   5149 static void
   5150 isp_rdnvram_word(struct ispsoftc *isp, int wo, u_int16_t *rp)
   5151 {
   5152 	int i, cbits;
   5153 	u_int16_t bit, rqst;
   5154 
   5155 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   5156 	USEC_DELAY(2);
   5157 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   5158 	USEC_DELAY(2);
   5159 
   5160 	if (IS_FC(isp)) {
   5161 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   5162 		rqst = (ISP_NVRAM_READ << 8) | wo;
   5163 		cbits = 10;
   5164 	} else if (IS_ULTRA2(isp)) {
   5165 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
   5166 		rqst = (ISP_NVRAM_READ << 8) | wo;
   5167 		cbits = 10;
   5168 	} else {
   5169 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   5170 		rqst = (ISP_NVRAM_READ << 6) | wo;
   5171 		cbits = 8;
   5172 	}
   5173 
   5174 	/*
   5175 	 * Clock the word select request out...
   5176 	 */
   5177 	for (i = cbits; i >= 0; i--) {
   5178 		if ((rqst >> i) & 1) {
   5179 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   5180 		} else {
   5181 			bit = BIU_NVRAM_SELECT;
   5182 		}
   5183 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5184 		USEC_DELAY(2);
   5185 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   5186 		USEC_DELAY(2);
   5187 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5188 		USEC_DELAY(2);
   5189 	}
   5190 	/*
   5191 	 * Now read the result back in (bits come back in MSB format).
   5192 	 */
   5193 	*rp = 0;
   5194 	for (i = 0; i < 16; i++) {
   5195 		u_int16_t rv;
   5196 		*rp <<= 1;
   5197 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   5198 		USEC_DELAY(2);
   5199 		rv = ISP_READ(isp, BIU_NVRAM);
   5200 		if (rv & BIU_NVRAM_DATAIN) {
   5201 			*rp |= 1;
   5202 		}
   5203 		USEC_DELAY(2);
   5204 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   5205 		USEC_DELAY(2);
   5206 	}
   5207 	ISP_WRITE(isp, BIU_NVRAM, 0);
   5208 	USEC_DELAY(2);
   5209 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
   5210 }
   5211 
   5212 static void
   5213 isp_parse_nvram_1020(struct ispsoftc *isp, u_int8_t *nvram_data)
   5214 {
   5215 	sdparam *sdp = (sdparam *) isp->isp_param;
   5216 	int tgt;
   5217 
   5218 	sdp->isp_fifo_threshold =
   5219 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
   5220 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
   5221 
   5222 	sdp->isp_initiator_id =
   5223 		ISP_NVRAM_INITIATOR_ID(nvram_data);
   5224 
   5225 	sdp->isp_bus_reset_delay =
   5226 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   5227 
   5228 	sdp->isp_retry_count =
   5229 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   5230 
   5231 	sdp->isp_retry_delay =
   5232 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   5233 
   5234 	sdp->isp_async_data_setup =
   5235 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   5236 
   5237 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   5238 		if (sdp->isp_async_data_setup < 9) {
   5239 			sdp->isp_async_data_setup = 9;
   5240 		}
   5241 	} else {
   5242 		if (sdp->isp_async_data_setup != 6) {
   5243 			sdp->isp_async_data_setup = 6;
   5244 		}
   5245 	}
   5246 
   5247 	sdp->isp_req_ack_active_neg =
   5248 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   5249 
   5250 	sdp->isp_data_line_active_neg =
   5251 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   5252 
   5253 	sdp->isp_data_dma_burst_enabl =
   5254 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   5255 
   5256 	sdp->isp_cmd_dma_burst_enable =
   5257 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   5258 
   5259 	sdp->isp_tag_aging =
   5260 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   5261 
   5262 	sdp->isp_selection_timeout =
   5263 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   5264 
   5265 	sdp->isp_max_queue_depth =
   5266 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   5267 
   5268 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   5269 
   5270 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   5271 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   5272 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   5273 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   5274 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   5275 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   5276 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   5277 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   5278 
   5279 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5280 		sdp->isp_devparam[tgt].dev_enable =
   5281 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
   5282 		sdp->isp_devparam[tgt].exc_throttle =
   5283 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
   5284 		sdp->isp_devparam[tgt].nvrm_offset =
   5285 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
   5286 		sdp->isp_devparam[tgt].nvrm_period =
   5287 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
   5288 		/*
   5289 		 * We probably shouldn't lie about this, but it
   5290 		 * it makes it much safer if we limit NVRAM values
   5291 		 * to sanity.
   5292 		 */
   5293 		if (isp->isp_type < ISP_HA_SCSI_1040) {
   5294 			/*
   5295 			 * If we're not ultra, we can't possibly
   5296 			 * be a shorter period than this.
   5297 			 */
   5298 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
   5299 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
   5300 			}
   5301 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
   5302 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
   5303 			}
   5304 		} else {
   5305 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
   5306 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
   5307 			}
   5308 		}
   5309 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   5310 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
   5311 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   5312 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   5313 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
   5314 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   5315 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
   5316 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   5317 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
   5318 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   5319 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
   5320 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   5321 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
   5322 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   5323 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
   5324 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   5325 		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5326 		    sdp->isp_devparam[tgt].nvrm_offset,
   5327 		    sdp->isp_devparam[tgt].nvrm_period);
   5328 		sdp->isp_devparam[tgt].goal_offset =
   5329 		    sdp->isp_devparam[tgt].nvrm_offset;
   5330 		sdp->isp_devparam[tgt].goal_period =
   5331 		    sdp->isp_devparam[tgt].nvrm_period;
   5332 		sdp->isp_devparam[tgt].goal_flags =
   5333 		    sdp->isp_devparam[tgt].nvrm_flags;
   5334 	}
   5335 }
   5336 
   5337 static void
   5338 isp_parse_nvram_1080(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   5339 {
   5340 	sdparam *sdp = (sdparam *) isp->isp_param;
   5341 	int tgt;
   5342 
   5343 	sdp += bus;
   5344 
   5345 	sdp->isp_fifo_threshold =
   5346 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
   5347 
   5348 	sdp->isp_initiator_id =
   5349 	    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
   5350 
   5351 	sdp->isp_bus_reset_delay =
   5352 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   5353 
   5354 	sdp->isp_retry_count =
   5355 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   5356 
   5357 	sdp->isp_retry_delay =
   5358 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   5359 
   5360 	sdp->isp_async_data_setup =
   5361 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   5362 
   5363 	sdp->isp_req_ack_active_neg =
   5364 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   5365 
   5366 	sdp->isp_data_line_active_neg =
   5367 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   5368 
   5369 	sdp->isp_data_dma_burst_enabl =
   5370 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   5371 
   5372 	sdp->isp_cmd_dma_burst_enable =
   5373 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   5374 
   5375 	sdp->isp_selection_timeout =
   5376 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   5377 
   5378 	sdp->isp_max_queue_depth =
   5379 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   5380 
   5381 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   5382 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   5383 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   5384 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   5385 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   5386 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   5387 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   5388 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   5389 
   5390 
   5391 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5392 		sdp->isp_devparam[tgt].dev_enable =
   5393 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   5394 		sdp->isp_devparam[tgt].exc_throttle =
   5395 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   5396 		sdp->isp_devparam[tgt].nvrm_offset =
   5397 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   5398 		sdp->isp_devparam[tgt].nvrm_period =
   5399 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   5400 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   5401 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   5402 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   5403 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   5404 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   5405 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   5406 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   5407 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   5408 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   5409 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   5410 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   5411 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   5412 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   5413 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   5414 		sdp->isp_devparam[tgt].actv_flags = 0;
   5415 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   5416 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5417 		    sdp->isp_devparam[tgt].nvrm_offset,
   5418 		    sdp->isp_devparam[tgt].nvrm_period);
   5419 		sdp->isp_devparam[tgt].goal_offset =
   5420 		    sdp->isp_devparam[tgt].nvrm_offset;
   5421 		sdp->isp_devparam[tgt].goal_period =
   5422 		    sdp->isp_devparam[tgt].nvrm_period;
   5423 		sdp->isp_devparam[tgt].goal_flags =
   5424 		    sdp->isp_devparam[tgt].nvrm_flags;
   5425 	}
   5426 }
   5427 
   5428 static void
   5429 isp_parse_nvram_12160(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   5430 {
   5431 	sdparam *sdp = (sdparam *) isp->isp_param;
   5432 	int tgt;
   5433 
   5434 	sdp += bus;
   5435 
   5436 	sdp->isp_fifo_threshold =
   5437 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
   5438 
   5439 	sdp->isp_initiator_id =
   5440 	    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
   5441 
   5442 	sdp->isp_bus_reset_delay =
   5443 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   5444 
   5445 	sdp->isp_retry_count =
   5446 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   5447 
   5448 	sdp->isp_retry_delay =
   5449 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   5450 
   5451 	sdp->isp_async_data_setup =
   5452 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   5453 
   5454 	sdp->isp_req_ack_active_neg =
   5455 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   5456 
   5457 	sdp->isp_data_line_active_neg =
   5458 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   5459 
   5460 	sdp->isp_data_dma_burst_enabl =
   5461 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   5462 
   5463 	sdp->isp_cmd_dma_burst_enable =
   5464 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   5465 
   5466 	sdp->isp_selection_timeout =
   5467 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   5468 
   5469 	sdp->isp_max_queue_depth =
   5470 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   5471 
   5472 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   5473 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   5474 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   5475 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   5476 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   5477 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   5478 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   5479 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   5480 
   5481 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5482 		sdp->isp_devparam[tgt].dev_enable =
   5483 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   5484 		sdp->isp_devparam[tgt].exc_throttle =
   5485 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   5486 		sdp->isp_devparam[tgt].nvrm_offset =
   5487 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   5488 		sdp->isp_devparam[tgt].nvrm_period =
   5489 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   5490 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   5491 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   5492 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   5493 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   5494 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   5495 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   5496 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   5497 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   5498 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   5499 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   5500 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   5501 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   5502 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   5503 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   5504 		sdp->isp_devparam[tgt].actv_flags = 0;
   5505 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   5506 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5507 		    sdp->isp_devparam[tgt].nvrm_offset,
   5508 		    sdp->isp_devparam[tgt].nvrm_period);
   5509 		sdp->isp_devparam[tgt].goal_offset =
   5510 		    sdp->isp_devparam[tgt].nvrm_offset;
   5511 		sdp->isp_devparam[tgt].goal_period =
   5512 		    sdp->isp_devparam[tgt].nvrm_period;
   5513 		sdp->isp_devparam[tgt].goal_flags =
   5514 		    sdp->isp_devparam[tgt].nvrm_flags;
   5515 	}
   5516 }
   5517 
   5518 static void
   5519 isp_parse_nvram_2100(struct ispsoftc *isp, u_int8_t *nvram_data)
   5520 {
   5521 	fcparam *fcp = (fcparam *) isp->isp_param;
   5522 	u_int64_t wwn;
   5523 
   5524 	/*
   5525 	 * There is NVRAM storage for both Port and Node entities-
   5526 	 * but the Node entity appears to be unused on all the cards
   5527 	 * I can find. However, we should account for this being set
   5528 	 * at some point in the future.
   5529 	 *
   5530 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
   5531 	 * bits 48..60. In the case of the 2202, it appears that they do
   5532 	 * use bit 48 to distinguish between the two instances on the card.
   5533 	 * The 2204, which I've never seen, *probably* extends this method.
   5534 	 */
   5535 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
   5536 	if (wwn) {
   5537 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
   5538 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
   5539 		if ((wwn >> 60) == 0) {
   5540 			wwn |= (((u_int64_t) 2)<< 60);
   5541 		}
   5542 	}
   5543 	fcp->isp_portwwn = wwn;
   5544 	wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
   5545 	if (wwn) {
   5546 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
   5547 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
   5548 		if ((wwn >> 60) == 0) {
   5549 			wwn |= (((u_int64_t) 2)<< 60);
   5550 		}
   5551 	}
   5552 	fcp->isp_nodewwn = wwn;
   5553 
   5554 	/*
   5555 	 * Make sure we have both Node and Port as non-zero values.
   5556 	 */
   5557 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
   5558 		fcp->isp_portwwn = fcp->isp_nodewwn;
   5559 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
   5560 		fcp->isp_nodewwn = fcp->isp_portwwn;
   5561 	}
   5562 
   5563 	/*
   5564 	 * Make the Node and Port values sane if they're NAA == 2.
   5565 	 * This means to clear bits 48..56 for the Node WWN and
   5566 	 * make sure that there's some non-zero value in 48..56
   5567 	 * for the Port WWN.
   5568 	 */
   5569 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
   5570 		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
   5571 		    (fcp->isp_nodewwn >> 60) == 2) {
   5572 			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
   5573 		}
   5574 		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
   5575 		    (fcp->isp_portwwn >> 60) == 2) {
   5576 			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
   5577 		}
   5578 	}
   5579 
   5580 	fcp->isp_maxalloc =
   5581 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   5582 	fcp->isp_maxfrmlen =
   5583 		ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   5584 	fcp->isp_retry_delay =
   5585 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   5586 	fcp->isp_retry_count =
   5587 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   5588 	fcp->isp_loopid =
   5589 		ISP2100_NVRAM_HARDLOOPID(nvram_data);
   5590 	fcp->isp_execthrottle =
   5591 		ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   5592 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
   5593 	isp_prt(isp, ISP_LOGDEBUG0,
   5594 	    "NVRAM: maxfrmlen %d execthrottle %d fwoptions 0x%x",
   5595 	    fcp->isp_maxfrmlen, fcp->isp_execthrottle, fcp->isp_fwoptions);
   5596 }
   5597