Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.81
      1 /* $NetBSD: isp.c,v 1.81 2001/10/06 20:34:49 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;
   2295 	sns_screq_t *reqp;
   2296 	sns_scrsp_t *resp;
   2297 	mbreg_t mbs;
   2298 	int hicap, first_portid_seen;
   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 	first_portid = portid = fcp->isp_portid;
   2312 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
   2313 
   2314 	for (first_portid_seen = hicap = 0; hicap < 65535; hicap++) {
   2315 		MEMZERO((void *) reqp, SNS_GAN_REQ_SIZE);
   2316 		reqp->snscb_rblen = SNS_GAN_RESP_SIZE >> 1;
   2317 		reqp->snscb_addr[RQRSP_ADDR0015] =
   2318 			DMA_WD0(fcp->isp_scdma + 0x100);
   2319 		reqp->snscb_addr[RQRSP_ADDR1631] =
   2320 			DMA_WD1(fcp->isp_scdma + 0x100);
   2321 		reqp->snscb_addr[RQRSP_ADDR3247] =
   2322 			DMA_WD2(fcp->isp_scdma + 0x100);
   2323 		reqp->snscb_addr[RQRSP_ADDR4863] =
   2324 			DMA_WD3(fcp->isp_scdma + 0x100);
   2325 		reqp->snscb_sblen = 6;
   2326 		reqp->snscb_data[0] = SNS_GAN;
   2327 		reqp->snscb_data[4] = portid & 0xffff;
   2328 		reqp->snscb_data[5] = (portid >> 16) & 0xff;
   2329 		ISP_SWIZZLE_SNS_REQ(isp, reqp);
   2330 		mbs.param[0] = MBOX_SEND_SNS;
   2331 		mbs.param[1] = SNS_GAN_REQ_SIZE >> 1;
   2332 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2333 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2334 		/*
   2335 		 * Leave 4 and 5 alone
   2336 		 */
   2337 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2338 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2339 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
   2340 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2341 			if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC) {
   2342 				fcp->isp_loopstate = LOOP_PDB_RCVD;
   2343 			}
   2344 			if (mbs.param[0] == MBOX_COMMAND_ERROR) {
   2345 				char tbuf[16];
   2346 				char *m;
   2347 				switch (mbs.param[1]) {
   2348 				case 1:
   2349 					m = "No Loop";
   2350 					break;
   2351 				case 2:
   2352 					m = "Failed to allocate IOCB buffer";
   2353 					break;
   2354 				case 3:
   2355 					m = "Failed to allocate XCB buffer";
   2356 					break;
   2357 				case 4:
   2358 					m = "timeout or transmit failed";
   2359 					break;
   2360 				case 5:
   2361 					m = "no fabric loop";
   2362 					break;
   2363 				case 6:
   2364 					m = "remote device not a target";
   2365 					break;
   2366 				default:
   2367 					SNPRINTF(tbuf, sizeof tbuf, "%x",
   2368 					    mbs.param[1]);
   2369 					m = tbuf;
   2370 					break;
   2371 				}
   2372 				isp_prt(isp, ISP_LOGERR, "SNS Failed- %s", m);
   2373 			}
   2374 			return (-1);
   2375 		}
   2376 		if (fcp->isp_fwstate != FW_READY ||
   2377 		    fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
   2378 			return (-1);
   2379 		}
   2380 		ISP_UNSWIZZLE_SNS_RSP(isp, resp, SNS_GAN_RESP_SIZE >> 1);
   2381 		portid = (((u_int32_t) resp->snscb_port_id[0]) << 16) |
   2382 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
   2383 		    (((u_int32_t) resp->snscb_port_id[2]));
   2384 		(void) isp_async(isp, ISPASYNC_FABRIC_DEV, resp);
   2385 		if (first_portid == portid) {
   2386 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2387 			return (0);
   2388 		}
   2389 	}
   2390 
   2391 	isp_prt(isp, ISP_LOGWARN, "broken fabric nameserver...*wheeze*...");
   2392 
   2393 	/*
   2394 	 * We either have a broken name server or a huge fabric if we get here.
   2395 	 */
   2396 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2397 	return (0);
   2398 }
   2399 
   2400 static void
   2401 isp_register_fc4_type(struct ispsoftc *isp)
   2402 {
   2403 	fcparam *fcp = isp->isp_param;
   2404 	sns_screq_t *reqp;
   2405 	mbreg_t mbs;
   2406 
   2407 	reqp = (sns_screq_t *) fcp->isp_scratch;
   2408 	MEMZERO((void *) reqp, SNS_RFT_REQ_SIZE);
   2409 	reqp->snscb_rblen = SNS_RFT_RESP_SIZE >> 1;
   2410 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
   2411 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
   2412 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
   2413 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
   2414 	reqp->snscb_sblen = 22;
   2415 	reqp->snscb_data[0] = SNS_RFT;
   2416 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
   2417 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
   2418 	reqp->snscb_data[6] = 0x100;	/* SCS - FCP */
   2419 #if	0
   2420 	reqp->snscb_data[6] |= 20;	/* ISO/IEC 8802-2 LLC/SNAP */
   2421 #endif
   2422 	ISP_SWIZZLE_SNS_REQ(isp, reqp);
   2423 	mbs.param[0] = MBOX_SEND_SNS;
   2424 	mbs.param[1] = SNS_RFT_REQ_SIZE >> 1;
   2425 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2426 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2427 	/*
   2428 	 * Leave 4 and 5 alone
   2429 	 */
   2430 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2431 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2432 	isp_mboxcmd(isp, &mbs, MBLOGALL);
   2433 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   2434 		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
   2435 	}
   2436 }
   2437 
   2438 /*
   2439  * Start a command. Locking is assumed done in the caller.
   2440  */
   2441 
   2442 int
   2443 isp_start(XS_T *xs)
   2444 {
   2445 	struct ispsoftc *isp;
   2446 	u_int16_t iptr, optr, handle;
   2447 	union {
   2448 		ispreq_t *_reqp;
   2449 		ispreqt2_t *_t2reqp;
   2450 	} _u;
   2451 #define	reqp	_u._reqp
   2452 #define	t2reqp	_u._t2reqp
   2453 #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
   2454 	int target, i;
   2455 
   2456 	XS_INITERR(xs);
   2457 	isp = XS_ISP(xs);
   2458 
   2459 	/*
   2460 	 * Check to make sure we're supporting initiator role.
   2461 	 */
   2462 	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
   2463 		XS_SETERR(xs, HBA_SELTIMEOUT);
   2464 		return (CMD_COMPLETE);
   2465 	}
   2466 
   2467 	/*
   2468 	 * Now make sure we're running.
   2469 	 */
   2470 
   2471 	if (isp->isp_state != ISP_RUNSTATE) {
   2472 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
   2473 		XS_SETERR(xs, HBA_BOTCH);
   2474 		return (CMD_COMPLETE);
   2475 	}
   2476 
   2477 	/*
   2478 	 * Check command CDB length, etc.. We really are limited to 16 bytes
   2479 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
   2480 	 * but probably only if we're running fairly new firmware (we'll
   2481 	 * let the old f/w choke on an extended command queue entry).
   2482 	 */
   2483 
   2484 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
   2485 		isp_prt(isp, ISP_LOGERR,
   2486 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
   2487 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
   2488 		XS_SETERR(xs, HBA_BOTCH);
   2489 		return (CMD_COMPLETE);
   2490 	}
   2491 
   2492 	/*
   2493 	 * Check to see whether we have good firmware state still or
   2494 	 * need to refresh our port database for this target.
   2495 	 */
   2496 	target = XS_TGT(xs);
   2497 	if (IS_FC(isp)) {
   2498 		fcparam *fcp = isp->isp_param;
   2499 		struct lportdb *lp;
   2500 #ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
   2501 		if (fcp->isp_fwstate != FW_READY ||
   2502 		    fcp->isp_loopstate != LOOP_READY) {
   2503 			return (CMD_RQLATER);
   2504 		}
   2505 
   2506 		/*
   2507 		 * If we're not on a Fabric, we can't have a target
   2508 		 * above FL_PORT_ID-1.
   2509 		 *
   2510 		 * If we're on a fabric and *not* connected as an F-port,
   2511 		 * we can't have a target less than FC_SNS_ID+1. This
   2512 		 * keeps us from having to sort out the difference between
   2513 		 * local public loop devices and those which we might get
   2514 		 * from a switch's database.
   2515 		 */
   2516 		if (fcp->isp_onfabric == 0) {
   2517 			if (target >= FL_PORT_ID) {
   2518 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2519 				return (CMD_COMPLETE);
   2520 			}
   2521 		} else {
   2522 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
   2523 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2524 				return (CMD_COMPLETE);
   2525 			}
   2526 			/*
   2527 			 * We used to exclude having local loop ports
   2528 			 * at the same time that we have fabric ports.
   2529 			 * That is, we used to exclude having ports
   2530 			 * at < FL_PORT_ID if we're FL-port.
   2531 			 *
   2532 			 * That's wrong. The only thing that could be
   2533 			 * dicey is if the switch you're connected to
   2534 			 * has these local loop ports appear on the
   2535 			 * fabric and we somehow attach them twice.
   2536 			 */
   2537 		}
   2538 #else
   2539 		/*
   2540 		 * Check for f/w being in ready state. If the f/w
   2541 		 * isn't in ready state, then we don't know our
   2542 		 * loop ID and the f/w hasn't completed logging
   2543 		 * into all targets on the loop. If this is the
   2544 		 * case, then bounce the command. We pretend this is
   2545 		 * a SELECTION TIMEOUT error if we've never gone to
   2546 		 * FW_READY state at all- in this case we may not
   2547 		 * be hooked to a loop at all and we shouldn't hang
   2548 		 * the machine for this. Otherwise, defer this command
   2549 		 * until later.
   2550 		 */
   2551 		if (fcp->isp_fwstate != FW_READY) {
   2552 			/*
   2553 			 * Give ourselves at most a 250ms delay.
   2554 			 */
   2555 			if (isp_fclink_test(isp, 250000)) {
   2556 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2557 				if (fcp->loop_seen_once) {
   2558 					return (CMD_RQLATER);
   2559 				} else {
   2560 					return (CMD_COMPLETE);
   2561 				}
   2562 			}
   2563 		}
   2564 
   2565 		/*
   2566 		 * If we're not on a Fabric, we can't have a target
   2567 		 * above FL_PORT_ID-1.
   2568 		 *
   2569 		 * If we're on a fabric and *not* connected as an F-port,
   2570 		 * we can't have a target less than FC_SNS_ID+1. This
   2571 		 * keeps us from having to sort out the difference between
   2572 		 * local public loop devices and those which we might get
   2573 		 * from a switch's database.
   2574 		 */
   2575 		if (fcp->isp_onfabric == 0) {
   2576 			if (target >= FL_PORT_ID) {
   2577 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2578 				return (CMD_COMPLETE);
   2579 			}
   2580 		} else {
   2581 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
   2582 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2583 				return (CMD_COMPLETE);
   2584 			}
   2585 			if (fcp->isp_topo != TOPO_F_PORT &&
   2586 			    target < FL_PORT_ID) {
   2587 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2588 				return (CMD_COMPLETE);
   2589 			}
   2590 		}
   2591 
   2592 		/*
   2593 		 * If our loop state is such that we haven't yet received
   2594 		 * a "Port Database Changed" notification (after a LIP or
   2595 		 * a Loop Reset or firmware initialization), then defer
   2596 		 * sending commands for a little while, but only if we've
   2597 		 * seen a valid loop at one point (otherwise we can get
   2598 		 * stuck at initialization time).
   2599 		 */
   2600 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
   2601 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2602 			if (fcp->loop_seen_once) {
   2603 				return (CMD_RQLATER);
   2604 			} else {
   2605 				return (CMD_COMPLETE);
   2606 			}
   2607 		}
   2608 
   2609 		/*
   2610 		 * If we're in the middle of loop or fabric scanning
   2611 		 * or merging the port databases, retry this command later.
   2612 		 */
   2613 		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
   2614 		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
   2615 		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
   2616 			return (CMD_RQLATER);
   2617 		}
   2618 
   2619 		/*
   2620 		 * If our loop state is now such that we've just now
   2621 		 * received a Port Database Change notification, then
   2622 		 * we have to go off and (re)scan the fabric. We back
   2623 		 * out and try again later if this doesn't work.
   2624 		 */
   2625 		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
   2626 			if (isp_scan_fabric(isp)) {
   2627 				return (CMD_RQLATER);
   2628 			}
   2629 			if (fcp->isp_fwstate != FW_READY ||
   2630 			    fcp->isp_loopstate < LOOP_PDB_RCVD) {
   2631 				return (CMD_RQLATER);
   2632 			}
   2633 		}
   2634 
   2635 		/*
   2636 		 * If our loop state is now such that we've just now
   2637 		 * received a Port Database Change notification, then
   2638 		 * we have to go off and (re)synchronize our port
   2639 		 * database.
   2640 		 */
   2641 		if (fcp->isp_loopstate < LOOP_READY) {
   2642 			if (isp_pdb_sync(isp)) {
   2643 				return (CMD_RQLATER);
   2644 			}
   2645 			if (fcp->isp_fwstate != FW_READY ||
   2646 			    fcp->isp_loopstate != LOOP_READY) {
   2647 				return (CMD_RQLATER);
   2648 			}
   2649 		}
   2650 
   2651 		/*
   2652 		 * XXX: Here's were we would cancel any loop_dead flag
   2653 		 * XXX: also cancel in dead_loop timeout that's running
   2654 		 */
   2655 #endif
   2656 
   2657 		/*
   2658 		 * Now check whether we should even think about pursuing this.
   2659 		 */
   2660 		lp = &fcp->portdb[target];
   2661 		if (lp->valid == 0) {
   2662 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2663 			return (CMD_COMPLETE);
   2664 		}
   2665 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
   2666 			isp_prt(isp, ISP_LOGDEBUG2,
   2667 			    "Target %d does not have target service", target);
   2668 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2669 			return (CMD_COMPLETE);
   2670 		}
   2671 		/*
   2672 		 * Now turn target into what the actual Loop ID is.
   2673 		 */
   2674 		target = lp->loopid;
   2675 	}
   2676 
   2677 	/*
   2678 	 * Next check to see if any HBA or Device
   2679 	 * parameters need to be updated.
   2680 	 */
   2681 	if (isp->isp_update != 0) {
   2682 		isp_update(isp);
   2683 	}
   2684 
   2685 	if (isp_getrqentry(isp, &iptr, &optr, (void **) &reqp)) {
   2686 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
   2687 		XS_SETERR(xs, HBA_BOTCH);
   2688 		return (CMD_EAGAIN);
   2689 	}
   2690 
   2691 	/*
   2692 	 * Now see if we need to synchronize the ISP with respect to anything.
   2693 	 * We do dual duty here (cough) for synchronizing for busses other
   2694 	 * than which we got here to send a command to.
   2695 	 */
   2696 	if (isp->isp_sendmarker) {
   2697 		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
   2698 		/*
   2699 		 * Check ports to send markers for...
   2700 		 */
   2701 		for (i = 0; i < n; i++) {
   2702 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
   2703 				continue;
   2704 			}
   2705 			MEMZERO((void *) reqp, sizeof (*reqp));
   2706 			reqp->req_header.rqs_entry_count = 1;
   2707 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
   2708 			reqp->req_modifier = SYNC_ALL;
   2709 			reqp->req_target = i << 7;	/* insert bus number */
   2710 			ISP_SWIZZLE_REQUEST(isp, reqp);
   2711 			ISP_ADD_REQUEST(isp, iptr);
   2712 
   2713 			if (isp_getrqentry(isp, &iptr, &optr, (void **)&reqp)) {
   2714 				isp_prt(isp, ISP_LOGDEBUG0,
   2715 				    "Request Queue Overflow+");
   2716 				XS_SETERR(xs, HBA_BOTCH);
   2717 				return (CMD_EAGAIN);
   2718 			}
   2719 		}
   2720 	}
   2721 
   2722 	MEMZERO((void *) reqp, UZSIZE);
   2723 	reqp->req_header.rqs_entry_count = 1;
   2724 	if (IS_FC(isp)) {
   2725 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
   2726 	} else {
   2727 		if (XS_CDBLEN(xs) > 12)
   2728 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
   2729 		else
   2730 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
   2731 	}
   2732 	reqp->req_header.rqs_flags = 0;
   2733 	reqp->req_header.rqs_seqno = 0;
   2734 	if (IS_FC(isp)) {
   2735 		/*
   2736 		 * See comment in isp_intr
   2737 		 */
   2738 		XS_RESID(xs) = 0;
   2739 
   2740 		/*
   2741 		 * Fibre Channel always requires some kind of tag.
   2742 		 * The Qlogic drivers seem be happy not to use a tag,
   2743 		 * but this breaks for some devices (IBM drives).
   2744 		 */
   2745 		if (XS_TAG_P(xs)) {
   2746 			t2reqp->req_flags = XS_TAG_TYPE(xs);
   2747 		} else {
   2748 			/*
   2749 			 * If we don't know what tag to use, use HEAD OF QUEUE
   2750 			 * for Request Sense or Simple.
   2751 			 */
   2752 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
   2753 				t2reqp->req_flags = REQFLAG_HTAG;
   2754 			else
   2755 				t2reqp->req_flags = REQFLAG_STAG;
   2756 		}
   2757 	} else {
   2758 		sdparam *sdp = (sdparam *)isp->isp_param;
   2759 		sdp += XS_CHANNEL(xs);
   2760 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
   2761 		    XS_TAG_P(xs)) {
   2762 			reqp->req_flags = XS_TAG_TYPE(xs);
   2763 		}
   2764 	}
   2765 	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
   2766 	if (IS_SCSI(isp)) {
   2767 		reqp->req_lun_trn = XS_LUN(xs);
   2768 		reqp->req_cdblen = XS_CDBLEN(xs);
   2769 	} else {
   2770 		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)
   2771 			t2reqp->req_scclun = XS_LUN(xs);
   2772 		else
   2773 			t2reqp->req_lun_trn = XS_LUN(xs);
   2774 	}
   2775 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
   2776 
   2777 	reqp->req_time = XS_TIME(xs) / 1000;
   2778 	if (reqp->req_time == 0 && XS_TIME(xs))
   2779 		reqp->req_time = 1;
   2780 
   2781 	/*
   2782 	 * Always give a bit more leeway to commands after a bus reset.
   2783 	 * XXX: DOES NOT DISTINGUISH WHICH PORT MAY HAVE BEEN SYNCED
   2784 	 */
   2785 	if (isp->isp_sendmarker && reqp->req_time < 5) {
   2786 		reqp->req_time = 5;
   2787 	}
   2788 	if (isp_save_xs(isp, xs, &handle)) {
   2789 		isp_prt(isp, ISP_LOGDEBUG1, "out of xflist pointers");
   2790 		XS_SETERR(xs, HBA_BOTCH);
   2791 		return (CMD_EAGAIN);
   2792 	}
   2793 	reqp->req_handle = handle;
   2794 	/*
   2795 	 * Set up DMA and/or do any bus swizzling of the request entry
   2796 	 * so that the Qlogic F/W understands what is being asked of it.
   2797  	*/
   2798 	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
   2799 	if (i != CMD_QUEUED) {
   2800 		isp_destroy_handle(isp, handle);
   2801 		/*
   2802 		 * dmasetup sets actual error in packet, and
   2803 		 * return what we were given to return.
   2804 		 */
   2805 		return (i);
   2806 	}
   2807 	XS_SETERR(xs, HBA_NOERROR);
   2808 	isp_prt(isp, ISP_LOGDEBUG2,
   2809 	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
   2810 	    XS_CHANNEL(xs), target, XS_LUN(xs), XS_CDBP(xs)[0],
   2811 	    (long) XS_XFRLEN(xs));
   2812 	ISP_ADD_REQUEST(isp, iptr);
   2813 	isp->isp_nactive++;
   2814 	if (isp->isp_sendmarker)
   2815 		isp->isp_sendmarker = 0;
   2816 	return (CMD_QUEUED);
   2817 #undef	reqp
   2818 #undef	t2reqp
   2819 }
   2820 
   2821 /*
   2822  * isp control
   2823  * Locks (ints blocked) assumed held.
   2824  */
   2825 
   2826 int
   2827 isp_control(struct ispsoftc *isp, ispctl_t ctl, void *arg)
   2828 {
   2829 	XS_T *xs;
   2830 	mbreg_t mbs;
   2831 	int bus, tgt;
   2832 	u_int16_t handle;
   2833 
   2834 	switch (ctl) {
   2835 	default:
   2836 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
   2837 		break;
   2838 
   2839 	case ISPCTL_RESET_BUS:
   2840 		/*
   2841 		 * Issue a bus reset.
   2842 		 */
   2843 		mbs.param[0] = MBOX_BUS_RESET;
   2844 		mbs.param[2] = 0;
   2845 		if (IS_SCSI(isp)) {
   2846 			mbs.param[1] =
   2847 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
   2848 			if (mbs.param[1] < 2)
   2849 				mbs.param[1] = 2;
   2850 			bus = *((int *) arg);
   2851 			if (IS_DUALBUS(isp))
   2852 				mbs.param[2] = bus;
   2853 		} else {
   2854 			mbs.param[1] = 10;
   2855 			bus = 0;
   2856 		}
   2857 		isp->isp_sendmarker |= (1 << bus);
   2858 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   2859 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2860 			break;
   2861 		}
   2862 		isp_prt(isp, ISP_LOGINFO,
   2863 		    "driver initiated bus reset of bus %d", bus);
   2864 		return (0);
   2865 
   2866 	case ISPCTL_RESET_DEV:
   2867 		tgt = (*((int *) arg)) & 0xffff;
   2868 		bus = (*((int *) arg)) >> 16;
   2869 		mbs.param[0] = MBOX_ABORT_TARGET;
   2870 		mbs.param[1] = (tgt << 8) | (bus << 15);
   2871 		mbs.param[2] = 3;	/* 'delay', in seconds */
   2872 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   2873 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2874 			break;
   2875 		}
   2876 		isp_prt(isp, ISP_LOGINFO,
   2877 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
   2878 		isp->isp_sendmarker |= (1 << bus);
   2879 		return (0);
   2880 
   2881 	case ISPCTL_ABORT_CMD:
   2882 		xs = (XS_T *) arg;
   2883 		tgt = XS_TGT(xs);
   2884 		handle = isp_find_handle(isp, xs);
   2885 		if (handle == 0) {
   2886 			isp_prt(isp, ISP_LOGWARN,
   2887 			    "cannot find handle for command to abort");
   2888 			break;
   2889 		}
   2890 		bus = XS_CHANNEL(xs);
   2891 		mbs.param[0] = MBOX_ABORT;
   2892 		if (IS_FC(isp)) {
   2893 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)  {
   2894 				mbs.param[1] = tgt << 8;
   2895 				mbs.param[4] = 0;
   2896 				mbs.param[5] = 0;
   2897 				mbs.param[6] = XS_LUN(xs);
   2898 			} else {
   2899 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
   2900 			}
   2901 		} else {
   2902 			mbs.param[1] =
   2903 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
   2904 		}
   2905 		mbs.param[3] = 0;
   2906 		mbs.param[2] = handle;
   2907 		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
   2908 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   2909 			return (0);
   2910 		}
   2911 		/*
   2912 		 * XXX: Look for command in the REQUEST QUEUE. That is,
   2913 		 * XXX: It hasen't been picked up by firmware yet.
   2914 		 */
   2915 		break;
   2916 
   2917 	case ISPCTL_UPDATE_PARAMS:
   2918 
   2919 		isp_update(isp);
   2920 		return (0);
   2921 
   2922 	case ISPCTL_FCLINK_TEST:
   2923 
   2924 		if (IS_FC(isp)) {
   2925 			int usdelay = (arg)? *((int *) arg) : 250000;
   2926 			return (isp_fclink_test(isp, usdelay));
   2927 		}
   2928 		break;
   2929 
   2930 	case ISPCTL_SCAN_FABRIC:
   2931 
   2932 		if (IS_FC(isp)) {
   2933 			return (isp_scan_fabric(isp));
   2934 		}
   2935 		break;
   2936 
   2937 	case ISPCTL_SCAN_LOOP:
   2938 
   2939 		if (IS_FC(isp)) {
   2940 			return (isp_scan_loop(isp));
   2941 		}
   2942 		break;
   2943 
   2944 	case ISPCTL_PDB_SYNC:
   2945 
   2946 		if (IS_FC(isp)) {
   2947 			return (isp_pdb_sync(isp));
   2948 		}
   2949 		break;
   2950 
   2951 	case ISPCTL_SEND_LIP:
   2952 
   2953 		if (IS_FC(isp)) {
   2954 			mbs.param[0] = MBOX_INIT_LIP;
   2955 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   2956 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   2957 				return (0);
   2958 			}
   2959 		}
   2960 		break;
   2961 
   2962 	case ISPCTL_GET_POSMAP:
   2963 
   2964 		if (IS_FC(isp) && arg) {
   2965 			return (isp_getmap(isp, arg));
   2966 		}
   2967 		break;
   2968 
   2969 	case ISPCTL_RUN_MBOXCMD:
   2970 
   2971 		isp_mboxcmd(isp, arg, MBLOGALL);
   2972 		return(0);
   2973 
   2974 #ifdef	ISP_TARGET_MODE
   2975 	case ISPCTL_TOGGLE_TMODE:
   2976 	{
   2977 
   2978 		/*
   2979 		 * We don't check/set against role here- that's the
   2980 		 * responsibility for the outer layer to coordinate.
   2981 		 */
   2982 		if (IS_SCSI(isp)) {
   2983 			int param = *(int *)arg;
   2984 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
   2985 			mbs.param[1] = param & 0xffff;
   2986 			mbs.param[2] = param >> 16;
   2987 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   2988 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2989 				break;
   2990 			}
   2991 		}
   2992 		return (0);
   2993 	}
   2994 #endif
   2995 	}
   2996 	return (-1);
   2997 }
   2998 
   2999 /*
   3000  * Interrupt Service Routine(s).
   3001  *
   3002  * External (OS) framework has done the appropriate locking,
   3003  * and the locking will be held throughout this function.
   3004  */
   3005 
   3006 /*
   3007  * Limit our stack depth by sticking with the max likely number
   3008  * of completions on a request queue at any one time.
   3009  */
   3010 #define	MAX_REQUESTQ_COMPLETIONS	32
   3011 
   3012 void
   3013 isp_intr(struct ispsoftc *isp, u_int16_t isr, u_int16_t sema, u_int16_t mbox)
   3014 {
   3015 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
   3016 	u_int16_t iptr, optr, junk;
   3017 	int i, nlooked = 0, ndone = 0;
   3018 
   3019 	/*
   3020 	 * Is this a mailbox related interrupt?
   3021 	 * The mailbox semaphore will be nonzero if so.
   3022 	 */
   3023 	if (sema) {
   3024 		if (mbox & 0x4000) {
   3025 			int obits, i = 0;
   3026 			if ((obits = isp->isp_mboxbsy) != 0) {
   3027 				isp->isp_mboxtmp[i++] = mbox;
   3028 				for (i = 1; i < MAX_MAILBOX; i++) {
   3029 					if ((obits & (1 << i)) == 0) {
   3030 						continue;
   3031 					}
   3032 					isp->isp_mboxtmp[i] =
   3033 					    ISP_READ(isp, MBOX_OFF(i));
   3034 				}
   3035 				MBOX_NOTIFY_COMPLETE(isp);
   3036 			} else {
   3037 				isp_prt(isp, ISP_LOGWARN,
   3038 				    "Mbox Command Async (0x%x) with no waiters",
   3039 				    mbox);
   3040 			}
   3041 		} else {
   3042 			int fhandle = isp_parse_async(isp, (int) mbox);
   3043 			isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
   3044 			if (fhandle > 0) {
   3045 				isp_fastpost_complete(isp, (u_int16_t) fhandle);
   3046 			}
   3047 		}
   3048 		if (IS_FC(isp) || isp->isp_state != ISP_RUNSTATE) {
   3049 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3050 			ISP_WRITE(isp, BIU_SEMA, 0);
   3051 			return;
   3052 		}
   3053 	}
   3054 
   3055 	/*
   3056 	 * We can't be getting this now.
   3057 	 */
   3058 	if (isp->isp_state != ISP_RUNSTATE) {
   3059 		isp_prt(isp, ISP_LOGWARN,
   3060 		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
   3061 		/*
   3062 		 * Thank you very much!  *Burrrp*!
   3063 		 */
   3064 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
   3065 		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
   3066 
   3067 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3068 		ISP_WRITE(isp, BIU_SEMA, 0);
   3069 		return;
   3070 	}
   3071 
   3072 	/*
   3073 	 * Get the current Response Queue Out Pointer.
   3074 	 *
   3075 	 * If we're a 2300, we can ask what hardware what it thinks.
   3076 	 */
   3077 	if (IS_2300(isp)) {
   3078 		optr = ISP_READ(isp, isp->isp_respoutrp);
   3079 		if (isp->isp_residx != optr) {
   3080 			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
   3081 			    optr, isp->isp_residx);
   3082 		}
   3083 	} else {
   3084 		optr = isp->isp_residx;
   3085 	}
   3086 
   3087 	/*
   3088 	 * You *must* read the Response Queue In Pointer
   3089 	 * prior to clearing the RISC interrupt.
   3090 	 */
   3091 	if (IS_2100(isp) || IS_2300(isp)) {
   3092 		i = 0;
   3093 		do {
   3094 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3095 			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3096 		} while (junk != iptr && ++i < 1000);
   3097 
   3098 		if (iptr != junk) {
   3099 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3100 			isp_prt(isp, ISP_LOGWARN,
   3101 			    "Response Queue Out Pointer Unstable (%x, %x)",
   3102 			    iptr, junk);
   3103 			return;
   3104 		}
   3105 	} else {
   3106 		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3107 	}
   3108 
   3109 
   3110 	if (optr == iptr && sema == 0) {
   3111 		/*
   3112 		 * There are a lot of these- reasons unknown- mostly on
   3113 		 * faster Alpha machines.
   3114 		 *
   3115 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
   3116 		 * make sure the old interrupt went away (to avoid 'ringing'
   3117 		 * effects), but that didn't stop this from occurring.
   3118 		 */
   3119 		if (IS_2300(isp)) {
   3120 			USEC_DELAY(100);
   3121 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3122 			junk = ISP_READ(isp, BIU_R2HSTSLO);
   3123 		} else {
   3124 			junk = ISP_READ(isp, BIU_ISR);
   3125 		}
   3126 		if (optr == iptr) {
   3127 			isp_prt(isp, ISP_LOGDEBUG0,
   3128 			    "bogus intr- isr %x (%x) iptr %x optr %x",
   3129 			    isr, junk, iptr, optr);
   3130 			isp->isp_intbogus++;
   3131 		}
   3132 	}
   3133 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3134 	ISP_WRITE(isp, BIU_SEMA, 0);
   3135 
   3136 	while (optr != iptr) {
   3137 		ispstatusreq_t *sp;
   3138 		u_int16_t oop;
   3139 		int buddaboom = 0;
   3140 
   3141 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   3142 		oop = optr;
   3143 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
   3144 		nlooked++;
   3145 		/*
   3146 		 * Do any appropriate unswizzling of what the Qlogic f/w has
   3147 		 * written into memory so it makes sense to us. This is a
   3148 		 * per-platform thing. Also includes any memory barriers.
   3149 		 */
   3150 		ISP_UNSWIZZLE_RESPONSE(isp, sp, oop);
   3151 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
   3152 			if (isp_handle_other_response(isp, sp, &optr) == 0) {
   3153 				MEMZERO(sp, sizeof (isphdr_t));
   3154 				continue;
   3155 			}
   3156 			/*
   3157 			 * It really has to be a bounced request just copied
   3158 			 * from the request queue to the response queue. If
   3159 			 * not, something bad has happened.
   3160 			 */
   3161 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
   3162 				isp_prt(isp, ISP_LOGERR, notresp,
   3163 				    sp->req_header.rqs_entry_type, oop, optr,
   3164 				    nlooked);
   3165 				if (isp->isp_dblev & ISP_LOGDEBUG0) {
   3166 					isp_print_bytes(isp, "Queue Entry",
   3167 					    QENTRY_LEN, sp);
   3168 				}
   3169 				MEMZERO(sp, sizeof (isphdr_t));
   3170 				continue;
   3171 			}
   3172 			buddaboom = 1;
   3173 		}
   3174 
   3175 		if (sp->req_header.rqs_flags & 0xf) {
   3176 #define	_RQS_OFLAGS	\
   3177 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
   3178 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   3179 				isp_prt(isp, ISP_LOGWARN,
   3180 				    "continuation segment");
   3181 				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3182 				continue;
   3183 			}
   3184 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   3185 				isp_prt(isp, ISP_LOGDEBUG1,
   3186 				    "internal queues full");
   3187 				/*
   3188 				 * We'll synthesize a QUEUE FULL message below.
   3189 				 */
   3190 			}
   3191 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   3192 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
   3193 				buddaboom++;
   3194 			}
   3195 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   3196 				isp_prt(isp, ISP_LOGERR, "bad request packet");
   3197 				buddaboom++;
   3198 			}
   3199 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
   3200 				isp_prt(isp, ISP_LOGERR,
   3201 				    "unknown flags (0x%x) in response",
   3202 				    sp->req_header.rqs_flags);
   3203 				buddaboom++;
   3204 			}
   3205 #undef	_RQS_OFLAGS
   3206 		}
   3207 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
   3208 			MEMZERO(sp, sizeof (isphdr_t));
   3209 			isp_prt(isp, ISP_LOGERR,
   3210 			    "bad request handle %d (type 0x%x, flags 0x%x)",
   3211 			    sp->req_handle, sp->req_header.rqs_entry_type,
   3212 			    sp->req_header.rqs_flags);
   3213 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3214 			continue;
   3215 		}
   3216 		xs = isp_find_xs(isp, sp->req_handle);
   3217 		if (xs == NULL) {
   3218 			MEMZERO(sp, sizeof (isphdr_t));
   3219 			isp_prt(isp, ISP_LOGERR,
   3220 			    "cannot find handle 0x%x in xflist",
   3221 			    sp->req_handle);
   3222 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3223 			continue;
   3224 		}
   3225 		isp_destroy_handle(isp, sp->req_handle);
   3226 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   3227 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3228 		}
   3229 		if (buddaboom) {
   3230 			XS_SETERR(xs, HBA_BOTCH);
   3231 		}
   3232 
   3233 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
   3234 			/*
   3235 			 * Fibre Channel F/W doesn't say we got status
   3236 			 * if there's Sense Data instead. I guess they
   3237 			 * think it goes w/o saying.
   3238 			 */
   3239 			sp->req_state_flags |= RQSF_GOT_STATUS;
   3240 		}
   3241 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   3242 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
   3243 		}
   3244 
   3245 		switch (sp->req_header.rqs_entry_type) {
   3246 		case RQSTYPE_RESPONSE:
   3247 			XS_SET_STATE_STAT(isp, xs, sp);
   3248 			isp_parse_status(isp, sp, xs);
   3249 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
   3250 			    (*XS_STSP(xs) == SCSI_BUSY)) {
   3251 				XS_SETERR(xs, HBA_TGTBSY);
   3252 			}
   3253 			if (IS_SCSI(isp)) {
   3254 				XS_RESID(xs) = sp->req_resid;
   3255 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   3256 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   3257 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
   3258 					XS_SAVE_SENSE(xs, sp);
   3259 				}
   3260 				/*
   3261 				 * A new synchronous rate was negotiated for
   3262 				 * this target. Mark state such that we'll go
   3263 				 * look up that which has changed later.
   3264 				 */
   3265 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   3266 					int t = XS_TGT(xs);
   3267 					sdparam *sdp = isp->isp_param;
   3268 					sdp += XS_CHANNEL(xs);
   3269 					sdp->isp_devparam[t].dev_refresh = 1;
   3270 					isp->isp_update |=
   3271 					    (1 << XS_CHANNEL(xs));
   3272 				}
   3273 			} else {
   3274 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
   3275 					XS_RESID(xs) = 0;
   3276 				} else if (sp->req_scsi_status & RQCS_RESID) {
   3277 					XS_RESID(xs) = sp->req_resid;
   3278 				} else {
   3279 					XS_RESID(xs) = 0;
   3280 				}
   3281 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   3282 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   3283 				    (sp->req_scsi_status & RQCS_SV)) {
   3284 					XS_SAVE_SENSE(xs, sp);
   3285 					/* solely for the benefit of debug */
   3286 					sp->req_state_flags |= RQSF_GOT_SENSE;
   3287 				}
   3288 			}
   3289 			isp_prt(isp, ISP_LOGDEBUG2,
   3290 			   "asked for %ld got resid %ld", (long) XS_XFRLEN(xs),
   3291 			   (long) sp->req_resid);
   3292 			break;
   3293 		case RQSTYPE_REQUEST:
   3294 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   3295 				/*
   3296 				 * Force Queue Full status.
   3297 				 */
   3298 				*XS_STSP(xs) = SCSI_QFULL;
   3299 				XS_SETERR(xs, HBA_NOERROR);
   3300 			} else if (XS_NOERR(xs)) {
   3301 				XS_SETERR(xs, HBA_BOTCH);
   3302 			}
   3303 			XS_RESID(xs) = XS_XFRLEN(xs);
   3304 			break;
   3305 		default:
   3306 			isp_prt(isp, ISP_LOGWARN,
   3307 			    "unhandled response queue type 0x%x",
   3308 			    sp->req_header.rqs_entry_type);
   3309 			if (XS_NOERR(xs)) {
   3310 				XS_SETERR(xs, HBA_BOTCH);
   3311 			}
   3312 			break;
   3313 		}
   3314 
   3315 		/*
   3316 		 * Free any dma resources. As a side effect, this may
   3317 		 * also do any cache flushing necessary for data coherence.			 */
   3318 		if (XS_XFRLEN(xs)) {
   3319 			ISP_DMAFREE(isp, xs, sp->req_handle);
   3320 		}
   3321 
   3322 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
   3323 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
   3324 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
   3325 			char skey;
   3326 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   3327 				skey = XS_SNSKEY(xs) & 0xf;
   3328 				if (skey < 10)
   3329 					skey += '0';
   3330 				else
   3331 					skey += 'a' - 10;
   3332 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
   3333 				skey = '?';
   3334 			} else {
   3335 				skey = '.';
   3336 			}
   3337 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
   3338 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
   3339 			    *XS_STSP(xs), skey, XS_ERR(xs));
   3340 		}
   3341 
   3342 		if (isp->isp_nactive > 0)
   3343 		    isp->isp_nactive--;
   3344 		complist[ndone++] = xs;	/* defer completion call until later */
   3345 		MEMZERO(sp, sizeof (isphdr_t));
   3346 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
   3347 			break;
   3348 		}
   3349 	}
   3350 
   3351 	/*
   3352 	 * If we looked at any commands, then it's valid to find out
   3353 	 * what the outpointer is. It also is a trigger to update the
   3354 	 * ISP's notion of what we've seen so far.
   3355 	 */
   3356 	if (nlooked) {
   3357 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3358 		/*
   3359 		 * While we're at it, reqad the requst queue out pointer.
   3360 		 */
   3361 		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
   3362 	}
   3363 
   3364 	isp->isp_residx = optr;
   3365 	for (i = 0; i < ndone; i++) {
   3366 		xs = complist[i];
   3367 		if (xs) {
   3368 			isp_done(xs);
   3369 		}
   3370 	}
   3371 }
   3372 
   3373 /*
   3374  * Support routines.
   3375  */
   3376 
   3377 static int
   3378 isp_parse_async(struct ispsoftc *isp, int mbox)
   3379 {
   3380 	int bus;
   3381 	u_int16_t fast_post_handle = 0;
   3382 
   3383 	if (IS_DUALBUS(isp)) {
   3384 		bus = ISP_READ(isp, OUTMAILBOX6);
   3385 	} else {
   3386 		bus = 0;
   3387 	}
   3388 
   3389 	switch (mbox) {
   3390 	case ASYNC_BUS_RESET:
   3391 		isp->isp_sendmarker |= (1 << bus);
   3392 #ifdef	ISP_TARGET_MODE
   3393 		isp_target_async(isp, bus, mbox);
   3394 #endif
   3395 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
   3396 		break;
   3397 	case ASYNC_SYSTEM_ERROR:
   3398 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
   3399 		/* no point continuing after this */
   3400 		return (-1);
   3401 
   3402 	case ASYNC_RQS_XFER_ERR:
   3403 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
   3404 		break;
   3405 
   3406 	case ASYNC_RSP_XFER_ERR:
   3407 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
   3408 		break;
   3409 
   3410 	case ASYNC_QWAKEUP:
   3411 		/*
   3412 		 * We've just been notified that the Queue has woken up.
   3413 		 * We don't need to be chatty about this- just unlatch things
   3414 		 * and move on.
   3415 		 */
   3416 		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
   3417 		break;
   3418 
   3419 	case ASYNC_TIMEOUT_RESET:
   3420 		isp_prt(isp, ISP_LOGWARN,
   3421 		    "timeout initiated SCSI bus reset of bus %d", bus);
   3422 		isp->isp_sendmarker |= (1 << bus);
   3423 #ifdef	ISP_TARGET_MODE
   3424 		isp_target_async(isp, bus, mbox);
   3425 #endif
   3426 		break;
   3427 
   3428 	case ASYNC_DEVICE_RESET:
   3429 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
   3430 		isp->isp_sendmarker |= (1 << bus);
   3431 #ifdef	ISP_TARGET_MODE
   3432 		isp_target_async(isp, bus, mbox);
   3433 #endif
   3434 		break;
   3435 
   3436 	case ASYNC_EXTMSG_UNDERRUN:
   3437 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
   3438 		break;
   3439 
   3440 	case ASYNC_SCAM_INT:
   3441 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
   3442 		break;
   3443 
   3444 	case ASYNC_HUNG_SCSI:
   3445 		isp_prt(isp, ISP_LOGERR,
   3446 		    "stalled SCSI Bus after DATA Overrun");
   3447 		/* XXX: Need to issue SCSI reset at this point */
   3448 		break;
   3449 
   3450 	case ASYNC_KILLED_BUS:
   3451 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
   3452 		break;
   3453 
   3454 	case ASYNC_BUS_TRANSIT:
   3455 		mbox = ISP_READ(isp, OUTMAILBOX2);
   3456 		switch (mbox & 0x1c00) {
   3457 		case SXP_PINS_LVD_MODE:
   3458 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
   3459 			SDPARAM(isp)->isp_diffmode = 0;
   3460 			SDPARAM(isp)->isp_ultramode = 0;
   3461 			SDPARAM(isp)->isp_lvdmode = 1;
   3462 			break;
   3463 		case SXP_PINS_HVD_MODE:
   3464 			isp_prt(isp, ISP_LOGINFO,
   3465 			    "Transition to Differential mode");
   3466 			SDPARAM(isp)->isp_diffmode = 1;
   3467 			SDPARAM(isp)->isp_ultramode = 0;
   3468 			SDPARAM(isp)->isp_lvdmode = 0;
   3469 			break;
   3470 		case SXP_PINS_SE_MODE:
   3471 			isp_prt(isp, ISP_LOGINFO,
   3472 			    "Transition to Single Ended mode");
   3473 			SDPARAM(isp)->isp_diffmode = 0;
   3474 			SDPARAM(isp)->isp_ultramode = 1;
   3475 			SDPARAM(isp)->isp_lvdmode = 0;
   3476 			break;
   3477 		default:
   3478 			isp_prt(isp, ISP_LOGWARN,
   3479 			    "Transition to Unknown Mode 0x%x", mbox);
   3480 			break;
   3481 		}
   3482 		/*
   3483 		 * XXX: Set up to renegotiate again!
   3484 		 */
   3485 		/* Can only be for a 1080... */
   3486 		isp->isp_sendmarker |= (1 << bus);
   3487 		break;
   3488 
   3489 	case ASYNC_CMD_CMPLT:
   3490 		fast_post_handle = ISP_READ(isp, OUTMAILBOX1);
   3491 		isp_prt(isp, ISP_LOGDEBUG3, "fast post completion of %u",
   3492 		    fast_post_handle);
   3493 		break;
   3494 
   3495 	case ASYNC_CTIO_DONE:
   3496 #ifdef	ISP_TARGET_MODE
   3497 		/*
   3498 		 * Bus gets overloaded with the handle. Dual bus
   3499 		 * cards don't put bus# into the handle.
   3500 		 */
   3501 		bus = (ISP_READ(isp, OUTMAILBOX2) << 16) |
   3502 		    ISP_READ(isp, OUTMAILBOX1);
   3503 		isp_target_async(isp, bus, mbox);
   3504 #else
   3505 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
   3506 #endif
   3507 		break;
   3508 
   3509 	case ASYNC_LIP_F8:
   3510 	case ASYNC_LIP_OCCURRED:
   3511 		FCPARAM(isp)->isp_lipseq =
   3512 		    ISP_READ(isp, OUTMAILBOX1);
   3513 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3514 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3515 		isp->isp_sendmarker = 1;
   3516 		isp_mark_getpdb_all(isp);
   3517 		isp_async(isp, ISPASYNC_LIP, NULL);
   3518 #ifdef	ISP_TARGET_MODE
   3519 		isp_target_async(isp, bus, mbox);
   3520 #endif
   3521 		/*
   3522 		 * We've had problems with data corruption occuring on
   3523 		 * commands that complete (with no apparent error) after
   3524 		 * we receive a LIP. This has been observed mostly on
   3525 		 * Local Loop topologies. To be safe, let's just mark
   3526 		 * all active commands as dead.
   3527 		 */
   3528 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
   3529 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
   3530 			int i, j;
   3531 			for (i = j = 0; i < isp->isp_maxcmds; i++) {
   3532 				XS_T *xs;
   3533 				xs = isp->isp_xflist[i];
   3534 				if (xs != NULL) {
   3535 					j++;
   3536 					XS_SETERR(xs, HBA_BUSRESET);
   3537 				}
   3538 			}
   3539 			if (j) {
   3540 				isp_prt(isp, ISP_LOGERR,
   3541 				    "LIP destroyed %d active commands", j);
   3542 			}
   3543 		}
   3544 		break;
   3545 
   3546 	case ASYNC_LOOP_UP:
   3547 		isp->isp_sendmarker = 1;
   3548 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3549 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3550 		isp_mark_getpdb_all(isp);
   3551 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
   3552 #ifdef	ISP_TARGET_MODE
   3553 		isp_target_async(isp, bus, mbox);
   3554 #endif
   3555 		break;
   3556 
   3557 	case ASYNC_LOOP_DOWN:
   3558 		isp->isp_sendmarker = 1;
   3559 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3560 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   3561 		isp_mark_getpdb_all(isp);
   3562 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
   3563 #ifdef	ISP_TARGET_MODE
   3564 		isp_target_async(isp, bus, mbox);
   3565 #endif
   3566 		break;
   3567 
   3568 	case ASYNC_LOOP_RESET:
   3569 		isp->isp_sendmarker = 1;
   3570 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3571 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   3572 		isp_mark_getpdb_all(isp);
   3573 		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
   3574 #ifdef	ISP_TARGET_MODE
   3575 		isp_target_async(isp, bus, mbox);
   3576 #endif
   3577 		break;
   3578 
   3579 	case ASYNC_PDB_CHANGED:
   3580 		isp->isp_sendmarker = 1;
   3581 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   3582 		isp_mark_getpdb_all(isp);
   3583 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
   3584 		break;
   3585 
   3586 	case ASYNC_CHANGE_NOTIFY:
   3587 		/*
   3588 		 * Not correct, but it will force us to rescan the loop.
   3589 		 */
   3590 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   3591 		isp_mark_getpdb_all(isp);
   3592 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
   3593 		break;
   3594 
   3595 	case ASYNC_PTPMODE:
   3596 		if (FCPARAM(isp)->isp_onfabric)
   3597 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
   3598 		else
   3599 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
   3600 		isp_mark_getpdb_all(isp);
   3601 		isp->isp_sendmarker = 1;
   3602 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3603 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3604 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   3605 #ifdef	ISP_TARGET_MODE
   3606 		isp_target_async(isp, bus, mbox);
   3607 #endif
   3608 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
   3609 		break;
   3610 
   3611 	case ASYNC_CONNMODE:
   3612 		mbox = ISP_READ(isp, OUTMAILBOX1);
   3613 		isp_mark_getpdb_all(isp);
   3614 		switch (mbox) {
   3615 		case ISP_CONN_LOOP:
   3616 			isp_prt(isp, ISP_LOGINFO,
   3617 			    "Point-to-Point -> Loop mode");
   3618 			break;
   3619 		case ISP_CONN_PTP:
   3620 			isp_prt(isp, ISP_LOGINFO,
   3621 			    "Loop -> Point-to-Point mode");
   3622 			break;
   3623 		case ISP_CONN_BADLIP:
   3624 			isp_prt(isp, ISP_LOGWARN,
   3625 			    "Point-to-Point -> Loop mode (BAD LIP)");
   3626 			break;
   3627 		case ISP_CONN_FATAL:
   3628 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
   3629 			isp_reinit(isp);
   3630 #ifdef	ISP_TARGET_MODE
   3631 			isp_target_async(isp, bus, ASYNC_SYSTEM_ERROR);
   3632 #endif
   3633 			/* no point continuing after this */
   3634 			return (-1);
   3635 		case ISP_CONN_LOOPBACK:
   3636 			isp_prt(isp, ISP_LOGWARN,
   3637 			    "Looped Back in Point-to-Point mode");
   3638 			break;
   3639 		default:
   3640 			isp_prt(isp, ISP_LOGWARN,
   3641 			    "Unknown connection mode (0x%x)", mbox);
   3642 			break;
   3643 		}
   3644 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   3645 		isp->isp_sendmarker = 1;
   3646 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3647 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3648 		break;
   3649 
   3650 	default:
   3651 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
   3652 		break;
   3653 	}
   3654 	return (fast_post_handle);
   3655 }
   3656 
   3657 /*
   3658  * Handle other response entries. A pointer to the request queue output
   3659  * index is here in case we want to eat several entries at once, although
   3660  * this is not used currently.
   3661  */
   3662 
   3663 static int
   3664 isp_handle_other_response(struct ispsoftc *isp,
   3665     ispstatusreq_t *sp, u_int16_t *optrp)
   3666 {
   3667 	switch (sp->req_header.rqs_entry_type) {
   3668 	case RQSTYPE_STATUS_CONT:
   3669 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
   3670 		return (0);
   3671 	case RQSTYPE_ATIO:
   3672 	case RQSTYPE_CTIO:
   3673 	case RQSTYPE_ENABLE_LUN:
   3674 	case RQSTYPE_MODIFY_LUN:
   3675 	case RQSTYPE_NOTIFY:
   3676 	case RQSTYPE_NOTIFY_ACK:
   3677 	case RQSTYPE_CTIO1:
   3678 	case RQSTYPE_ATIO2:
   3679 	case RQSTYPE_CTIO2:
   3680 	case RQSTYPE_CTIO3:
   3681 #ifdef	ISP_TARGET_MODE
   3682 		return (isp_target_notify(isp, sp, optrp));
   3683 #else
   3684 		optrp = optrp;
   3685 		/* FALLTHROUGH */
   3686 #endif
   3687 	case RQSTYPE_REQUEST:
   3688 	default:
   3689 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, sp)) {
   3690 			return (0);
   3691 		}
   3692 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
   3693 		    sp->req_header.rqs_entry_type);
   3694 		return (-1);
   3695 	}
   3696 }
   3697 
   3698 static void
   3699 isp_parse_status(struct ispsoftc *isp, ispstatusreq_t *sp, XS_T *xs)
   3700 {
   3701 	switch (sp->req_completion_status & 0xff) {
   3702 	case RQCS_COMPLETE:
   3703 		if (XS_NOERR(xs)) {
   3704 			XS_SETERR(xs, HBA_NOERROR);
   3705 		}
   3706 		return;
   3707 
   3708 	case RQCS_INCOMPLETE:
   3709 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   3710 			isp_prt(isp, ISP_LOGDEBUG1,
   3711 			    "Selection Timeout for %d.%d.%d",
   3712 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3713 			if (XS_NOERR(xs)) {
   3714 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3715 			}
   3716 			return;
   3717 		}
   3718 		isp_prt(isp, ISP_LOGERR,
   3719 		    "command incomplete for %d.%d.%d, state 0x%x",
   3720 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
   3721 		    sp->req_state_flags);
   3722 		break;
   3723 
   3724 	case RQCS_DMA_ERROR:
   3725 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
   3726 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3727 		break;
   3728 
   3729 	case RQCS_TRANSPORT_ERROR:
   3730 	{
   3731 		char buf[172];
   3732 		buf[0] = 0;
   3733 		STRNCAT(buf, "states=>", sizeof buf);
   3734 		if (sp->req_state_flags & RQSF_GOT_BUS) {
   3735 			STRNCAT(buf, " GOT_BUS", sizeof buf);
   3736 		}
   3737 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
   3738 			STRNCAT(buf, " GOT_TGT", sizeof buf);
   3739 		}
   3740 		if (sp->req_state_flags & RQSF_SENT_CDB) {
   3741 			STRNCAT(buf, " SENT_CDB", sizeof buf);
   3742 		}
   3743 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
   3744 			STRNCAT(buf, " XFRD_DATA", sizeof buf);
   3745 		}
   3746 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   3747 			STRNCAT(buf, " GOT_STS", sizeof buf);
   3748 		}
   3749 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
   3750 			STRNCAT(buf, " GOT_SNS", sizeof buf);
   3751 		}
   3752 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
   3753 			STRNCAT(buf, " XFR_CMPLT", sizeof buf);
   3754 		}
   3755 		STRNCAT(buf, "\nstatus=>", sizeof buf);
   3756 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
   3757 			STRNCAT(buf, " Disconnect", sizeof buf);
   3758 		}
   3759 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
   3760 			STRNCAT(buf, " Sync_xfr", sizeof buf);
   3761 		}
   3762 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
   3763 			STRNCAT(buf, " Parity", sizeof buf);
   3764 		}
   3765 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   3766 			STRNCAT(buf, " Bus_Reset", sizeof buf);
   3767 		}
   3768 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
   3769 			STRNCAT(buf, " Device_Reset", sizeof buf);
   3770 		}
   3771 		if (sp->req_status_flags & RQSTF_ABORTED) {
   3772 			STRNCAT(buf, " Aborted", sizeof buf);
   3773 		}
   3774 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
   3775 			STRNCAT(buf, " Timeout", sizeof buf);
   3776 		}
   3777 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   3778 			STRNCAT(buf, " Negotiation", sizeof buf);
   3779 		}
   3780 		isp_prt(isp, ISP_LOGERR, "%s", buf);
   3781 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
   3782 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
   3783 		break;
   3784 	}
   3785 	case RQCS_RESET_OCCURRED:
   3786 		isp_prt(isp, ISP_LOGWARN,
   3787 		    "bus reset destroyed command for %d.%d.%d",
   3788 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3789 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3790 		if (XS_NOERR(xs)) {
   3791 			XS_SETERR(xs, HBA_BUSRESET);
   3792 		}
   3793 		return;
   3794 
   3795 	case RQCS_ABORTED:
   3796 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
   3797 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3798 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3799 		if (XS_NOERR(xs)) {
   3800 			XS_SETERR(xs, HBA_ABORTED);
   3801 		}
   3802 		return;
   3803 
   3804 	case RQCS_TIMEOUT:
   3805 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
   3806 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3807 		if (XS_NOERR(xs)) {
   3808 			XS_SETERR(xs, HBA_CMDTIMEOUT);
   3809 		}
   3810 		return;
   3811 
   3812 	case RQCS_DATA_OVERRUN:
   3813 		XS_RESID(xs) = sp->req_resid;
   3814 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
   3815 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3816 		if (XS_NOERR(xs)) {
   3817 			XS_SETERR(xs, HBA_DATAOVR);
   3818 		}
   3819 		return;
   3820 
   3821 	case RQCS_COMMAND_OVERRUN:
   3822 		isp_prt(isp, ISP_LOGERR,
   3823 		    "command overrun for command on %d.%d.%d",
   3824 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3825 		break;
   3826 
   3827 	case RQCS_STATUS_OVERRUN:
   3828 		isp_prt(isp, ISP_LOGERR,
   3829 		    "status overrun for command on %d.%d.%d",
   3830 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3831 		break;
   3832 
   3833 	case RQCS_BAD_MESSAGE:
   3834 		isp_prt(isp, ISP_LOGERR,
   3835 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
   3836 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3837 		break;
   3838 
   3839 	case RQCS_NO_MESSAGE_OUT:
   3840 		isp_prt(isp, ISP_LOGERR,
   3841 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
   3842 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3843 		break;
   3844 
   3845 	case RQCS_EXT_ID_FAILED:
   3846 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
   3847 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3848 		break;
   3849 
   3850 	case RQCS_IDE_MSG_FAILED:
   3851 		isp_prt(isp, ISP_LOGERR,
   3852 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
   3853 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3854 		break;
   3855 
   3856 	case RQCS_ABORT_MSG_FAILED:
   3857 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
   3858 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3859 		break;
   3860 
   3861 	case RQCS_REJECT_MSG_FAILED:
   3862 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
   3863 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3864 		break;
   3865 
   3866 	case RQCS_NOP_MSG_FAILED:
   3867 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
   3868 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3869 		break;
   3870 
   3871 	case RQCS_PARITY_ERROR_MSG_FAILED:
   3872 		isp_prt(isp, ISP_LOGERR,
   3873 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
   3874 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3875 		break;
   3876 
   3877 	case RQCS_DEVICE_RESET_MSG_FAILED:
   3878 		isp_prt(isp, ISP_LOGWARN,
   3879 		    "BUS DEVICE RESET rejected by %d.%d.%d",
   3880 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3881 		break;
   3882 
   3883 	case RQCS_ID_MSG_FAILED:
   3884 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
   3885 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3886 		break;
   3887 
   3888 	case RQCS_UNEXP_BUS_FREE:
   3889 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
   3890 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3891 		break;
   3892 
   3893 	case RQCS_DATA_UNDERRUN:
   3894 		XS_RESID(xs) = sp->req_resid;
   3895 		if (XS_NOERR(xs)) {
   3896 			XS_SETERR(xs, HBA_NOERROR);
   3897 		}
   3898 		return;
   3899 
   3900 	case RQCS_XACT_ERR1:
   3901 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
   3902 		    XS_TGT(xs), XS_LUN(xs));
   3903 		break;
   3904 
   3905 	case RQCS_XACT_ERR2:
   3906 		isp_prt(isp, ISP_LOGERR, xact2,
   3907 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
   3908 		break;
   3909 
   3910 	case RQCS_XACT_ERR3:
   3911 		isp_prt(isp, ISP_LOGERR, xact3,
   3912 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3913 		break;
   3914 
   3915 	case RQCS_BAD_ENTRY:
   3916 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
   3917 		break;
   3918 
   3919 	case RQCS_QUEUE_FULL:
   3920 		isp_prt(isp, ISP_LOGDEBUG1,
   3921 		    "internal queues full for %d.%d.%d status 0x%x", XS_TGT(xs),
   3922 		    XS_LUN(xs), XS_CHANNEL(xs), *XS_STSP(xs));
   3923 		/*
   3924 		 * If QFULL or some other status byte is set, then this
   3925 		 * isn't an error, per se.
   3926 		 */
   3927 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
   3928 			XS_SETERR(xs, HBA_NOERROR);
   3929 			return;
   3930 		}
   3931 		break;
   3932 
   3933 	case RQCS_PHASE_SKIPPED:
   3934 		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
   3935 		    XS_TGT(xs), XS_LUN(xs));
   3936 		break;
   3937 
   3938 	case RQCS_ARQS_FAILED:
   3939 		isp_prt(isp, ISP_LOGERR,
   3940 		    "Auto Request Sense failed for %d.%d.%d",
   3941 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3942 		if (XS_NOERR(xs)) {
   3943 			XS_SETERR(xs, HBA_ARQFAIL);
   3944 		}
   3945 		return;
   3946 
   3947 	case RQCS_WIDE_FAILED:
   3948 		isp_prt(isp, ISP_LOGERR,
   3949 		    "Wide Negotiation failed for %d.%d.%d",
   3950 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3951 		if (IS_SCSI(isp)) {
   3952 			sdparam *sdp = isp->isp_param;
   3953 			sdp += XS_CHANNEL(xs);
   3954 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
   3955 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3956 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3957 		}
   3958 		if (XS_NOERR(xs)) {
   3959 			XS_SETERR(xs, HBA_NOERROR);
   3960 		}
   3961 		return;
   3962 
   3963 	case RQCS_SYNCXFER_FAILED:
   3964 		isp_prt(isp, ISP_LOGERR,
   3965 		    "SDTR Message failed for target %d.%d.%d",
   3966 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3967 		if (IS_SCSI(isp)) {
   3968 			sdparam *sdp = isp->isp_param;
   3969 			sdp += XS_CHANNEL(xs);
   3970 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
   3971 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3972 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3973 		}
   3974 		break;
   3975 
   3976 	case RQCS_LVD_BUSERR:
   3977 		isp_prt(isp, ISP_LOGERR,
   3978 		    "Bad LVD condition while talking to %d.%d.%d",
   3979 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3980 		break;
   3981 
   3982 	case RQCS_PORT_UNAVAILABLE:
   3983 		/*
   3984 		 * No such port on the loop. Moral equivalent of SELTIMEO
   3985 		 */
   3986 		isp_prt(isp, ISP_LOGINFO,
   3987 		    "Port Unavailable for target %d", XS_TGT(xs));
   3988 		if (XS_NOERR(xs)) {
   3989 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3990 		}
   3991 		return;
   3992 	case RQCS_PORT_LOGGED_OUT:
   3993 		/*
   3994 		 * It was there (maybe)- treat as a selection timeout.
   3995 		 */
   3996 		isp_prt(isp, ISP_LOGINFO,
   3997 		    "port logout for target %d", XS_TGT(xs));
   3998 		if (XS_NOERR(xs)) {
   3999 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4000 		}
   4001 		return;
   4002 
   4003 	case RQCS_PORT_CHANGED:
   4004 		isp_prt(isp, ISP_LOGWARN,
   4005 		    "port changed for target %d", XS_TGT(xs));
   4006 		if (XS_NOERR(xs)) {
   4007 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4008 		}
   4009 		return;
   4010 
   4011 	case RQCS_PORT_BUSY:
   4012 		isp_prt(isp, ISP_LOGWARN,
   4013 		    "port busy for target %d", XS_TGT(xs));
   4014 		if (XS_NOERR(xs)) {
   4015 			XS_SETERR(xs, HBA_TGTBSY);
   4016 		}
   4017 		return;
   4018 
   4019 	default:
   4020 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
   4021 		    sp->req_completion_status);
   4022 		break;
   4023 	}
   4024 	if (XS_NOERR(xs)) {
   4025 		XS_SETERR(xs, HBA_BOTCH);
   4026 	}
   4027 }
   4028 
   4029 static void
   4030 isp_fastpost_complete(struct ispsoftc *isp, u_int16_t fph)
   4031 {
   4032 	XS_T *xs;
   4033 
   4034 	if (fph == 0) {
   4035 		return;
   4036 	}
   4037 	xs = isp_find_xs(isp, fph);
   4038 	if (xs == NULL) {
   4039 		isp_prt(isp, ISP_LOGWARN,
   4040 		    "Command for fast post handle 0x%x not found", fph);
   4041 		return;
   4042 	}
   4043 	isp_destroy_handle(isp, fph);
   4044 
   4045 	/*
   4046 	 * Since we don't have a result queue entry item,
   4047 	 * we must believe that SCSI status is zero and
   4048 	 * that all data transferred.
   4049 	 */
   4050 	XS_SET_STATE_STAT(isp, xs, NULL);
   4051 	XS_RESID(xs) = 0;
   4052 	*XS_STSP(xs) = SCSI_GOOD;
   4053 	if (XS_XFRLEN(xs)) {
   4054 		ISP_DMAFREE(isp, xs, fph);
   4055 	}
   4056 	if (isp->isp_nactive)
   4057 		isp->isp_nactive--;
   4058 	isp_done(xs);
   4059 }
   4060 
   4061 #define	HIBYT(x)			((x) >> 0x8)
   4062 #define	LOBYT(x)			((x)  & 0xff)
   4063 #define	ISPOPMAP(a, b)			(((a) << 8) | (b))
   4064 static u_int16_t mbpscsi[] = {
   4065 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   4066 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   4067 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   4068 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   4069 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   4070 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   4071 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   4072 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   4073 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   4074 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
   4075 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
   4076 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   4077 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   4078 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   4079 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   4080 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   4081 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   4082 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
   4083 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
   4084 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   4085 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
   4086 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
   4087 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
   4088 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
   4089 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
   4090 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
   4091 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
   4092 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   4093 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
   4094 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   4095 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   4096 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   4097 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
   4098 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   4099 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   4100 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   4101 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
   4102 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   4103 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   4104 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
   4105 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   4106 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   4107 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
   4108 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   4109 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   4110 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   4111 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   4112 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   4113 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   4114 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   4115 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   4116 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   4117 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
   4118 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
   4119 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   4120 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   4121 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   4122 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   4123 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
   4124 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   4125 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   4126 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   4127 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   4128 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   4129 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   4130 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   4131 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   4132 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
   4133 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   4134 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
   4135 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
   4136 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
   4137 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
   4138 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
   4139 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
   4140 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
   4141 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   4142 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   4143 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   4144 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   4145 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
   4146 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
   4147 	ISPOPMAP(0xdf, 0xdf),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
   4148 	ISPOPMAP(0xff, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
   4149 	ISPOPMAP(0xcf, 0xff),	/* 0x54: EXECUTE IOCB A64 */
   4150 	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
   4151 	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
   4152 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   4153 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   4154 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   4155 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
   4156 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
   4157 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
   4158 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
   4159 };
   4160 
   4161 #ifndef	ISP_STRIPPED
   4162 static char *scsi_mbcmd_names[] = {
   4163 	"NO-OP",
   4164 	"LOAD RAM",
   4165 	"EXEC FIRMWARE",
   4166 	"DUMP RAM",
   4167 	"WRITE RAM WORD",
   4168 	"READ RAM WORD",
   4169 	"MAILBOX REG TEST",
   4170 	"VERIFY CHECKSUM",
   4171 	"ABOUT FIRMWARE",
   4172 	NULL,
   4173 	NULL,
   4174 	NULL,
   4175 	NULL,
   4176 	NULL,
   4177 	"CHECK FIRMWARE",
   4178 	NULL,
   4179 	"INIT REQUEST QUEUE",
   4180 	"INIT RESULT QUEUE",
   4181 	"EXECUTE IOCB",
   4182 	"WAKE UP",
   4183 	"STOP FIRMWARE",
   4184 	"ABORT",
   4185 	"ABORT DEVICE",
   4186 	"ABORT TARGET",
   4187 	"BUS RESET",
   4188 	"STOP QUEUE",
   4189 	"START QUEUE",
   4190 	"SINGLE STEP QUEUE",
   4191 	"ABORT QUEUE",
   4192 	"GET DEV QUEUE STATUS",
   4193 	NULL,
   4194 	"GET FIRMWARE STATUS",
   4195 	"GET INIT SCSI ID",
   4196 	"GET SELECT TIMEOUT",
   4197 	"GET RETRY COUNT",
   4198 	"GET TAG AGE LIMIT",
   4199 	"GET CLOCK RATE",
   4200 	"GET ACT NEG STATE",
   4201 	"GET ASYNC DATA SETUP TIME",
   4202 	"GET PCI PARAMS",
   4203 	"GET TARGET PARAMS",
   4204 	"GET DEV QUEUE PARAMS",
   4205 	"GET RESET DELAY PARAMS",
   4206 	NULL,
   4207 	NULL,
   4208 	NULL,
   4209 	NULL,
   4210 	NULL,
   4211 	"SET INIT SCSI ID",
   4212 	"SET SELECT TIMEOUT",
   4213 	"SET RETRY COUNT",
   4214 	"SET TAG AGE LIMIT",
   4215 	"SET CLOCK RATE",
   4216 	"SET ACT NEG STATE",
   4217 	"SET ASYNC DATA SETUP TIME",
   4218 	"SET PCI CONTROL PARAMS",
   4219 	"SET TARGET PARAMS",
   4220 	"SET DEV QUEUE PARAMS",
   4221 	"SET RESET DELAY PARAMS",
   4222 	NULL,
   4223 	NULL,
   4224 	NULL,
   4225 	NULL,
   4226 	NULL,
   4227 	"RETURN BIOS BLOCK ADDR",
   4228 	"WRITE FOUR RAM WORDS",
   4229 	"EXEC BIOS IOCB",
   4230 	NULL,
   4231 	NULL,
   4232 	"SET SYSTEM PARAMETER",
   4233 	"GET SYSTEM PARAMETER",
   4234 	NULL,
   4235 	"GET SCAM CONFIGURATION",
   4236 	"SET SCAM CONFIGURATION",
   4237 	"SET FIRMWARE FEATURES",
   4238 	"GET FIRMWARE FEATURES",
   4239 	NULL,
   4240 	NULL,
   4241 	NULL,
   4242 	NULL,
   4243 	"LOAD RAM A64",
   4244 	"DUMP RAM A64",
   4245 	"INITIALIZE REQUEST QUEUE A64",
   4246 	"INITIALIZE RESPONSE QUEUE A64",
   4247 	"EXECUTE IOCB A64",
   4248 	"ENABLE TARGET MODE",
   4249 	"GET TARGET MODE STATE",
   4250 	NULL,
   4251 	NULL,
   4252 	NULL,
   4253 	"SET DATA OVERRUN RECOVERY MODE",
   4254 	"GET DATA OVERRUN RECOVERY MODE",
   4255 	"SET HOST DATA",
   4256 	"GET NOST DATA",
   4257 };
   4258 #endif
   4259 
   4260 static u_int16_t mbpfc[] = {
   4261 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   4262 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   4263 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   4264 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   4265 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   4266 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   4267 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   4268 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   4269 	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   4270 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
   4271 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
   4272 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   4273 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   4274 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   4275 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   4276 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   4277 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   4278 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
   4279 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
   4280 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   4281 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
   4282 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
   4283 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
   4284 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
   4285 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
   4286 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
   4287 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
   4288 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   4289 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
   4290 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   4291 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   4292 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   4293 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
   4294 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
   4295 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   4296 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
   4297 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
   4298 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
   4299 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
   4300 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
   4301 	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
   4302 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
   4303 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
   4304 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   4305 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   4306 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   4307 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   4308 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   4309 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
   4310 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
   4311 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   4312 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
   4313 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
   4314 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
   4315 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
   4316 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
   4317 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
   4318 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
   4319 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
   4320 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   4321 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   4322 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   4323 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   4324 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   4325 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
   4326 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
   4327 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
   4328 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
   4329 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   4330 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
   4331 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
   4332 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
   4333 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
   4334 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
   4335 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
   4336 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
   4337 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   4338 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   4339 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   4340 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   4341 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
   4342 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
   4343 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
   4344 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
   4345 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   4346 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
   4347 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   4348 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   4349 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   4350 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   4351 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
   4352 	ISPOPMAP(0x00, 0x00),	/* 0x5b: */
   4353 	ISPOPMAP(0x00, 0x00),	/* 0x5c: */
   4354 	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
   4355 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
   4356 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
   4357 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
   4358 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
   4359 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
   4360 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   4361 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
   4362 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
   4363 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
   4364 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
   4365 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
   4366 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
   4367 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
   4368 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
   4369 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
   4370 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
   4371 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
   4372 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
   4373 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
   4374 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
   4375 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
   4376 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
   4377 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
   4378 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
   4379 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
   4380 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
   4381 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
   4382 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
   4383 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
   4384 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
   4385 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
   4386 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
   4387 	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
   4388 };
   4389 
   4390 #ifndef	ISP_STRIPPED
   4391 static char *fc_mbcmd_names[] = {
   4392 	"NO-OP",
   4393 	"LOAD RAM",
   4394 	"EXEC FIRMWARE",
   4395 	"DUMP RAM",
   4396 	"WRITE RAM WORD",
   4397 	"READ RAM WORD",
   4398 	"MAILBOX REG TEST",
   4399 	"VERIFY CHECKSUM",
   4400 	"ABOUT FIRMWARE",
   4401 	"LOAD RAM",
   4402 	"DUMP RAM",
   4403 	NULL,
   4404 	NULL,
   4405 	NULL,
   4406 	"CHECK FIRMWARE",
   4407 	NULL,
   4408 	"INIT REQUEST QUEUE",
   4409 	"INIT RESULT QUEUE",
   4410 	"EXECUTE IOCB",
   4411 	"WAKE UP",
   4412 	"STOP FIRMWARE",
   4413 	"ABORT",
   4414 	"ABORT DEVICE",
   4415 	"ABORT TARGET",
   4416 	"BUS RESET",
   4417 	"STOP QUEUE",
   4418 	"START QUEUE",
   4419 	"SINGLE STEP QUEUE",
   4420 	"ABORT QUEUE",
   4421 	"GET DEV QUEUE STATUS",
   4422 	NULL,
   4423 	"GET FIRMWARE STATUS",
   4424 	"GET LOOP ID",
   4425 	NULL,
   4426 	"GET RETRY COUNT",
   4427 	NULL,
   4428 	NULL,
   4429 	NULL,
   4430 	NULL,
   4431 	NULL,
   4432 	"GET FIRMWARE OPTIONS",
   4433 	"GET PORT QUEUE PARAMS",
   4434 	NULL,
   4435 	NULL,
   4436 	NULL,
   4437 	NULL,
   4438 	NULL,
   4439 	NULL,
   4440 	NULL,
   4441 	NULL,
   4442 	"SET RETRY COUNT",
   4443 	NULL,
   4444 	NULL,
   4445 	NULL,
   4446 	NULL,
   4447 	NULL,
   4448 	"SET FIRMWARE OPTIONS",
   4449 	"SET PORT QUEUE PARAMS",
   4450 	NULL,
   4451 	NULL,
   4452 	NULL,
   4453 	NULL,
   4454 	NULL,
   4455 	NULL,
   4456 	"LOOP PORT BYPASS",
   4457 	"LOOP PORT ENABLE",
   4458 	"GET RESOURCE COUNTS",
   4459 	"REQUEST NON PARTICIPATING MODE",
   4460 	NULL,
   4461 	NULL,
   4462 	NULL,
   4463 	"GET PORT DATABASE,, ENHANCED",
   4464 	NULL,
   4465 	NULL,
   4466 	NULL,
   4467 	NULL,
   4468 	NULL,
   4469 	NULL,
   4470 	NULL,
   4471 	NULL,
   4472 	NULL,
   4473 	NULL,
   4474 	NULL,
   4475 	NULL,
   4476 	"EXECUTE IOCB A64",
   4477 	NULL,
   4478 	NULL,
   4479 	NULL,
   4480 	NULL,
   4481 	NULL,
   4482 	NULL,
   4483 	NULL,
   4484 	NULL,
   4485 	"GET/SET DATA RATE",
   4486 	NULL,
   4487 	NULL,
   4488 	"INIT FIRMWARE",
   4489 	NULL,
   4490 	"INIT LIP",
   4491 	"GET FC-AL POSITION MAP",
   4492 	"GET PORT DATABASE",
   4493 	"CLEAR ACA",
   4494 	"TARGET RESET",
   4495 	"CLEAR TASK SET",
   4496 	"ABORT TASK SET",
   4497 	"GET FW STATE",
   4498 	"GET PORT NAME",
   4499 	"GET LINK STATUS",
   4500 	"INIT LIP RESET",
   4501 	NULL,
   4502 	"SEND SNS",
   4503 	"FABRIC LOGIN",
   4504 	"SEND CHANGE REQUEST",
   4505 	"FABRIC LOGOUT",
   4506 	"INIT LIP LOGIN",
   4507 	NULL,
   4508 	"LOGIN LOOP PORT",
   4509 	"GET PORT/NODE NAME LIST",
   4510 	"SET VENDOR ID",
   4511 	"INITIALIZE IP MAILBOX",
   4512 	NULL,
   4513 	NULL,
   4514 	NULL,
   4515 	NULL,
   4516 	"Get ID List",
   4517 	"SEND LFA",
   4518 	"Lun RESET"
   4519 };
   4520 #endif
   4521 
   4522 static void
   4523 isp_mboxcmd(struct ispsoftc *isp, mbreg_t *mbp, int logmask)
   4524 {
   4525 	char *cname, *xname, tname[16], mname[16];
   4526 	unsigned int lim, ibits, obits, box, opcode;
   4527 	u_int16_t *mcp;
   4528 
   4529 	if (IS_FC(isp)) {
   4530 		mcp = mbpfc;
   4531 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   4532 	} else {
   4533 		mcp = mbpscsi;
   4534 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   4535 	}
   4536 
   4537 	if ((opcode = mbp->param[0]) >= lim) {
   4538 		mbp->param[0] = MBOX_INVALID_COMMAND;
   4539 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
   4540 		return;
   4541 	}
   4542 
   4543 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   4544 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   4545 
   4546 	if (ibits == 0 && obits == 0) {
   4547 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
   4548 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
   4549 		return;
   4550 	}
   4551 
   4552 	/*
   4553 	 * Get exclusive usage of mailbox registers.
   4554 	 */
   4555 	MBOX_ACQUIRE(isp);
   4556 
   4557 	for (box = 0; box < MAX_MAILBOX; box++) {
   4558 		if (ibits & (1 << box)) {
   4559 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   4560 		}
   4561 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   4562 	}
   4563 
   4564 	isp->isp_lastmbxcmd = opcode;
   4565 
   4566 	/*
   4567 	 * We assume that we can't overwrite a previous command.
   4568 	 */
   4569 	isp->isp_mboxbsy = obits;
   4570 
   4571 	/*
   4572 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   4573 	 */
   4574 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   4575 
   4576 	/*
   4577 	 * While we haven't finished the command, spin our wheels here.
   4578 	 */
   4579 	MBOX_WAIT_COMPLETE(isp);
   4580 
   4581 	/*
   4582 	 * Copy back output registers.
   4583 	 */
   4584 	for (box = 0; box < MAX_MAILBOX; box++) {
   4585 		if (obits & (1 << box)) {
   4586 			mbp->param[box] = isp->isp_mboxtmp[box];
   4587 		}
   4588 	}
   4589 
   4590 	MBOX_RELEASE(isp);
   4591 
   4592 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
   4593 		return;
   4594 	}
   4595 #ifdef	ISP_STRIPPED
   4596 	cname = NULL;
   4597 #else
   4598 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
   4599 #endif
   4600 	if (cname == NULL) {
   4601 		cname = tname;
   4602 		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
   4603 	}
   4604 
   4605 	/*
   4606 	 * Just to be chatty here...
   4607 	 */
   4608 	xname = NULL;
   4609 	switch (mbp->param[0]) {
   4610 	case MBOX_COMMAND_COMPLETE:
   4611 		break;
   4612 	case MBOX_INVALID_COMMAND:
   4613 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
   4614 			xname = "INVALID COMMAND";
   4615 		break;
   4616 	case MBOX_HOST_INTERFACE_ERROR:
   4617 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
   4618 			xname = "HOST INTERFACE ERROR";
   4619 		break;
   4620 	case MBOX_TEST_FAILED:
   4621 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
   4622 			xname = "TEST FAILED";
   4623 		break;
   4624 	case MBOX_COMMAND_ERROR:
   4625 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
   4626 			xname = "COMMAND ERROR";
   4627 		break;
   4628 	case MBOX_COMMAND_PARAM_ERROR:
   4629 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
   4630 			xname = "COMMAND PARAMETER ERROR";
   4631 		break;
   4632 	case MBOX_LOOP_ID_USED:
   4633 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
   4634 			xname = "LOOP ID ALREADY IN USE";
   4635 		break;
   4636 	case MBOX_PORT_ID_USED:
   4637 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
   4638 			xname = "PORT ID ALREADY IN USE";
   4639 		break;
   4640 	case MBOX_ALL_IDS_USED:
   4641 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
   4642 			xname = "ALL LOOP IDS IN USE";
   4643 		break;
   4644 	case 0:		/* special case */
   4645 		xname = "TIMEOUT";
   4646 		break;
   4647 	default:
   4648 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
   4649 		xname = mname;
   4650 		break;
   4651 	}
   4652 	if (xname)
   4653 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
   4654 		    cname, xname);
   4655 }
   4656 
   4657 static void
   4658 isp_fw_state(struct ispsoftc *isp)
   4659 {
   4660 	if (IS_FC(isp)) {
   4661 		mbreg_t mbs;
   4662 		fcparam *fcp = isp->isp_param;
   4663 
   4664 		mbs.param[0] = MBOX_GET_FW_STATE;
   4665 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4666 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   4667 			fcp->isp_fwstate = mbs.param[1];
   4668 		}
   4669 	}
   4670 }
   4671 
   4672 static void
   4673 isp_update(struct ispsoftc *isp)
   4674 {
   4675 	int bus, upmask;
   4676 
   4677 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
   4678 		if (upmask & (1 << bus)) {
   4679 			isp_update_bus(isp, bus);
   4680 		}
   4681 		upmask &= ~(1 << bus);
   4682 	}
   4683 }
   4684 
   4685 static void
   4686 isp_update_bus(struct ispsoftc *isp, int bus)
   4687 {
   4688 	int tgt;
   4689 	mbreg_t mbs;
   4690 	sdparam *sdp;
   4691 
   4692 	isp->isp_update &= ~(1 << bus);
   4693 	if (IS_FC(isp)) {
   4694 		/*
   4695 		 * There are no 'per-bus' settings for Fibre Channel.
   4696 		 */
   4697 		return;
   4698 	}
   4699 	sdp = isp->isp_param;
   4700 	sdp += bus;
   4701 
   4702 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4703 		u_int16_t flags, period, offset;
   4704 		int get;
   4705 
   4706 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   4707 			sdp->isp_devparam[tgt].dev_update = 0;
   4708 			sdp->isp_devparam[tgt].dev_refresh = 0;
   4709 			isp_prt(isp, ISP_LOGDEBUG0,
   4710 	 		    "skipping target %d bus %d update", tgt, bus);
   4711 			continue;
   4712 		}
   4713 		/*
   4714 		 * If the goal is to update the status of the device,
   4715 		 * take what's in goal_flags and try and set the device
   4716 		 * toward that. Otherwise, if we're just refreshing the
   4717 		 * current device state, get the current parameters.
   4718 		 */
   4719 
   4720 		/*
   4721 		 * Refresh overrides set
   4722 		 */
   4723 		if (sdp->isp_devparam[tgt].dev_refresh) {
   4724 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   4725 			sdp->isp_devparam[tgt].dev_refresh = 0;
   4726 			get = 1;
   4727 		} else if (sdp->isp_devparam[tgt].dev_update) {
   4728 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   4729 			/*
   4730 			 * Make sure goal_flags has "Renegotiate on Error"
   4731 			 * on and "Freeze Queue on Error" off.
   4732 			 */
   4733 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
   4734 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
   4735 
   4736 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
   4737 
   4738 			/*
   4739 			 * Insist that PARITY must be enabled
   4740 			 * if SYNC or WIDE is enabled.
   4741 			 */
   4742 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
   4743 				mbs.param[2] |= DPARM_PARITY;
   4744 			}
   4745 
   4746 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
   4747 				mbs.param[3] = 0;
   4748 			} else {
   4749 				mbs.param[3] =
   4750 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
   4751 				    (sdp->isp_devparam[tgt].goal_period);
   4752 			}
   4753 			/*
   4754 			 * A command completion later that has
   4755 			 * RQSTF_NEGOTIATION set can cause
   4756 			 * the dev_refresh/announce cycle also.
   4757 			 *
   4758 			 * Note: It is really important to update our current
   4759 			 * flags with at least the state of TAG capabilities-
   4760 			 * otherwise we might try and send a tagged command
   4761 			 * when we have it all turned off. So change it here
   4762 			 * to say that current already matches goal.
   4763 			 */
   4764 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
   4765 			sdp->isp_devparam[tgt].actv_flags |=
   4766 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
   4767 			isp_prt(isp, ISP_LOGDEBUG0,
   4768 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
   4769 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
   4770 			    mbs.param[3] & 0xff);
   4771 			sdp->isp_devparam[tgt].dev_update = 0;
   4772 			sdp->isp_devparam[tgt].dev_refresh = 1;
   4773 			get = 0;
   4774 		} else {
   4775 			continue;
   4776 		}
   4777 		mbs.param[1] = (bus << 15) | (tgt << 8);
   4778 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4779 		if (get == 0) {
   4780 			isp->isp_sendmarker |= (1 << bus);
   4781 			continue;
   4782 		}
   4783 		flags = mbs.param[2];
   4784 		period = mbs.param[3] & 0xff;
   4785 		offset = mbs.param[3] >> 8;
   4786 		sdp->isp_devparam[tgt].actv_flags = flags;
   4787 		sdp->isp_devparam[tgt].actv_period = period;
   4788 		sdp->isp_devparam[tgt].actv_offset = offset;
   4789 		get = (bus << 16) | tgt;
   4790 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
   4791 	}
   4792 
   4793 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4794 		if (sdp->isp_devparam[tgt].dev_update ||
   4795 		    sdp->isp_devparam[tgt].dev_refresh) {
   4796 			isp->isp_update |= (1 << bus);
   4797 			break;
   4798 		}
   4799 	}
   4800 }
   4801 
   4802 static void
   4803 isp_setdfltparm(struct ispsoftc *isp, int channel)
   4804 {
   4805 	int tgt;
   4806 	mbreg_t mbs;
   4807 	sdparam *sdp;
   4808 
   4809 	if (IS_FC(isp)) {
   4810 		fcparam *fcp = (fcparam *) isp->isp_param;
   4811 		int nvfail;
   4812 
   4813 		fcp += channel;
   4814 		if (fcp->isp_gotdparms) {
   4815 			return;
   4816 		}
   4817 		fcp->isp_gotdparms = 1;
   4818 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
   4819 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
   4820 		fcp->isp_execthrottle = ISP_EXEC_THROTTLE;
   4821 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
   4822 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
   4823 		/* Platform specific.... */
   4824 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
   4825 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
   4826 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
   4827 		fcp->isp_fwoptions = 0;
   4828 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
   4829 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
   4830 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
   4831 #ifndef	ISP_NO_FASTPOST_FC
   4832 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
   4833 #endif
   4834 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
   4835 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
   4836 
   4837 		/*
   4838 		 * Make sure this is turned off now until we get
   4839 		 * extended options from NVRAM
   4840 		 */
   4841 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
   4842 
   4843 		/*
   4844 		 * Now try and read NVRAM unless told to not do so.
   4845 		 * This will set fcparam's isp_nodewwn && isp_portwwn.
   4846 		 */
   4847 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4848 		    	nvfail = isp_read_nvram(isp);
   4849 			if (nvfail)
   4850 				isp->isp_confopts |= ISP_CFG_NONVRAM;
   4851 		} else {
   4852 			nvfail = 1;
   4853 		}
   4854 		/*
   4855 		 * Set node && port to override platform set defaults
   4856 		 * unless the nvram read failed (or none was done),
   4857 		 * or the platform code wants to use what had been
   4858 		 * set in the defaults.
   4859 		 */
   4860 		if (nvfail || (isp->isp_confopts & ISP_CFG_OWNWWN)) {
   4861 			isp_prt(isp, ISP_LOGCONFIG,
   4862 			    "Using Node WWN 0x%08x%08x, Port WWN 0x%08x%08x",
   4863 			    (u_int32_t) (DEFAULT_NODEWWN(isp) >> 32),
   4864 			    (u_int32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff),
   4865 			    (u_int32_t) (DEFAULT_PORTWWN(isp) >> 32),
   4866 			    (u_int32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
   4867 			isp->isp_confopts |= ISP_CFG_OWNWWN;
   4868 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
   4869 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
   4870 		} else {
   4871 			/*
   4872 			 * We always start out with values derived
   4873 			 * from NVRAM or our platform default.
   4874 			 */
   4875 			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
   4876 			ISP_PORTWWN(isp) = fcp->isp_portwwn;
   4877 		}
   4878 		return;
   4879 	}
   4880 
   4881 	sdp = (sdparam *) isp->isp_param;
   4882 	sdp += channel;
   4883 
   4884 	/*
   4885 	 * Been there, done that, got the T-shirt...
   4886 	 */
   4887 	if (sdp->isp_gotdparms) {
   4888 		return;
   4889 	}
   4890 	sdp->isp_gotdparms = 1;
   4891 
   4892 	/*
   4893 	 * Establish some default parameters.
   4894 	 */
   4895 	sdp->isp_cmd_dma_burst_enable = 0;
   4896 	sdp->isp_data_dma_burst_enabl = 1;
   4897 	sdp->isp_fifo_threshold = 0;
   4898 	sdp->isp_initiator_id = DEFAULT_IID(isp);
   4899 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   4900 		sdp->isp_async_data_setup = 9;
   4901 	} else {
   4902 		sdp->isp_async_data_setup = 6;
   4903 	}
   4904 	sdp->isp_selection_timeout = 250;
   4905 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
   4906 	sdp->isp_tag_aging = 8;
   4907 	sdp->isp_bus_reset_delay = 5;
   4908 	/*
   4909 	 * Don't retry selection, busy or queue full automatically- reflect
   4910 	 * these back to us.
   4911 	 */
   4912 	sdp->isp_retry_count = 0;
   4913 	sdp->isp_retry_delay = 0;
   4914 
   4915 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4916 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
   4917 		sdp->isp_devparam[tgt].dev_enable = 1;
   4918 	}
   4919 
   4920 	/*
   4921 	 * If we've not been told to avoid reading NVRAM, try and read it.
   4922 	 * If we're successful reading it, we can then return because NVRAM
   4923 	 * will tell us what the desired settings are. Otherwise, we establish
   4924 	 * some reasonable 'fake' nvram and goal defaults.
   4925 	 */
   4926 
   4927 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4928 		if (isp_read_nvram(isp) == 0) {
   4929 			return;
   4930 		}
   4931 	}
   4932 
   4933 	/*
   4934 	 * Now try and see whether we have specific values for them.
   4935 	 */
   4936 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4937 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   4938 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
   4939 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4940 			sdp->isp_req_ack_active_neg = 1;
   4941 			sdp->isp_data_line_active_neg = 1;
   4942 		} else {
   4943 			sdp->isp_req_ack_active_neg =
   4944 			    (mbs.param[1+channel] >> 4) & 0x1;
   4945 			sdp->isp_data_line_active_neg =
   4946 			    (mbs.param[1+channel] >> 5) & 0x1;
   4947 		}
   4948 	}
   4949 
   4950 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
   4951 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   4952 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   4953 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   4954 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
   4955 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   4956 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   4957 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   4958 
   4959 	/*
   4960 	 * The trick here is to establish a default for the default (honk!)
   4961 	 * state (goal_flags). Then try and get the current status from
   4962 	 * the card to fill in the current state. We don't, in fact, set
   4963 	 * the default to the SAFE default state- that's not the goal state.
   4964 	 */
   4965 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4966 		u_int8_t off, per;
   4967 		sdp->isp_devparam[tgt].actv_offset = 0;
   4968 		sdp->isp_devparam[tgt].actv_period = 0;
   4969 		sdp->isp_devparam[tgt].actv_flags = 0;
   4970 
   4971 		sdp->isp_devparam[tgt].goal_flags =
   4972 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
   4973 
   4974 		/*
   4975 		 * We default to Wide/Fast for versions less than a 1040
   4976 		 * (unless it's SBus).
   4977 		 */
   4978 		if (IS_ULTRA3(isp)) {
   4979 			off = ISP_80M_SYNCPARMS >> 8;
   4980 			per = ISP_80M_SYNCPARMS & 0xff;
   4981 		} else if (IS_ULTRA2(isp)) {
   4982 			off = ISP_40M_SYNCPARMS >> 8;
   4983 			per = ISP_40M_SYNCPARMS & 0xff;
   4984 		} else if (IS_1240(isp)) {
   4985 			off = ISP_20M_SYNCPARMS >> 8;
   4986 			per = ISP_20M_SYNCPARMS & 0xff;
   4987 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
   4988 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
   4989 		    (isp->isp_bustype == ISP_BT_PCI &&
   4990 		    isp->isp_type < ISP_HA_SCSI_1040) ||
   4991 		    (isp->isp_clock && isp->isp_clock < 60) ||
   4992 		    (sdp->isp_ultramode == 0)) {
   4993 			off = ISP_10M_SYNCPARMS >> 8;
   4994 			per = ISP_10M_SYNCPARMS & 0xff;
   4995 		} else {
   4996 			off = ISP_20M_SYNCPARMS_1040 >> 8;
   4997 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
   4998 		}
   4999 		sdp->isp_devparam[tgt].goal_offset =
   5000 		    sdp->isp_devparam[tgt].nvrm_offset = off;
   5001 		sdp->isp_devparam[tgt].goal_period =
   5002 		    sdp->isp_devparam[tgt].nvrm_period = per;
   5003 
   5004 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
   5005 		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5006 		    sdp->isp_devparam[tgt].nvrm_offset,
   5007 		    sdp->isp_devparam[tgt].nvrm_period);
   5008 	}
   5009 }
   5010 
   5011 /*
   5012  * Re-initialize the ISP and complete all orphaned commands
   5013  * with a 'botched' notice. The reset/init routines should
   5014  * not disturb an already active list of commands.
   5015  *
   5016  * Locks held prior to coming here.
   5017  */
   5018 
   5019 void
   5020 isp_reinit(struct ispsoftc *isp)
   5021 {
   5022 	XS_T *xs;
   5023 	u_int16_t handle;
   5024 
   5025 	isp_reset(isp);
   5026 	if (isp->isp_state != ISP_RESETSTATE) {
   5027 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
   5028 		goto skip;
   5029 	}
   5030 	isp_init(isp);
   5031 	if (isp->isp_role == ISP_ROLE_NONE) {
   5032 		goto skip;
   5033 	}
   5034 	if (isp->isp_state == ISP_INITSTATE) {
   5035 		isp->isp_state = ISP_RUNSTATE;
   5036 	}
   5037 	if (isp->isp_state != ISP_RUNSTATE) {
   5038 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot restart card");
   5039 	}
   5040 skip:
   5041 	isp->isp_nactive = 0;
   5042 
   5043 	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
   5044 		xs = isp_find_xs(isp, handle);
   5045 		if (xs == NULL) {
   5046 			continue;
   5047 		}
   5048 		isp_destroy_handle(isp, handle);
   5049 		if (XS_XFRLEN(xs)) {
   5050 			ISP_DMAFREE(isp, xs, handle);
   5051 			XS_RESID(xs) = XS_XFRLEN(xs);
   5052 		} else {
   5053 			XS_RESID(xs) = 0;
   5054 		}
   5055 		XS_SETERR(xs, HBA_BUSRESET);
   5056 		isp_done(xs);
   5057 	}
   5058 }
   5059 
   5060 /*
   5061  * NVRAM Routines
   5062  */
   5063 static int
   5064 isp_read_nvram(struct ispsoftc *isp)
   5065 {
   5066 	int i, amt;
   5067 	u_int8_t csum, minversion;
   5068 	union {
   5069 		u_int8_t _x[ISP2100_NVRAM_SIZE];
   5070 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
   5071 	} _n;
   5072 #define	nvram_data	_n._x
   5073 #define	nvram_words	_n._s
   5074 
   5075 	if (IS_FC(isp)) {
   5076 		amt = ISP2100_NVRAM_SIZE;
   5077 		minversion = 1;
   5078 	} else if (IS_ULTRA2(isp)) {
   5079 		amt = ISP1080_NVRAM_SIZE;
   5080 		minversion = 0;
   5081 	} else {
   5082 		amt = ISP_NVRAM_SIZE;
   5083 		minversion = 2;
   5084 	}
   5085 
   5086 	/*
   5087 	 * Just read the first two words first to see if we have a valid
   5088 	 * NVRAM to continue reading the rest with.
   5089 	 */
   5090 	for (i = 0; i < 2; i++) {
   5091 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   5092 	}
   5093 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   5094 	    nvram_data[2] != 'P') {
   5095 		if (isp->isp_bustype != ISP_BT_SBUS) {
   5096 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
   5097 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
   5098 			    nvram_data[0], nvram_data[1], nvram_data[2]);
   5099 		}
   5100 		return (-1);
   5101 	}
   5102 	for (i = 2; i < amt>>1; i++) {
   5103 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   5104 	}
   5105 	for (csum = 0, i = 0; i < amt; i++) {
   5106 		csum += nvram_data[i];
   5107 	}
   5108 	if (csum != 0) {
   5109 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
   5110 		return (-1);
   5111 	}
   5112 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   5113 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
   5114 		    ISP_NVRAM_VERSION(nvram_data));
   5115 		return (-1);
   5116 	}
   5117 
   5118 	if (IS_ULTRA3(isp)) {
   5119 		isp_parse_nvram_12160(isp, 0, nvram_data);
   5120 		isp_parse_nvram_12160(isp, 1, nvram_data);
   5121 	} else if (IS_1080(isp)) {
   5122 		isp_parse_nvram_1080(isp, 0, nvram_data);
   5123 	} else if (IS_1280(isp) || IS_1240(isp)) {
   5124 		isp_parse_nvram_1080(isp, 0, nvram_data);
   5125 		isp_parse_nvram_1080(isp, 1, nvram_data);
   5126 	} else if (IS_SCSI(isp)) {
   5127 		isp_parse_nvram_1020(isp, nvram_data);
   5128 	} else {
   5129 		isp_parse_nvram_2100(isp, nvram_data);
   5130 	}
   5131 	return (0);
   5132 #undef	nvram_data
   5133 #undef	nvram_words
   5134 }
   5135 
   5136 static void
   5137 isp_rdnvram_word(struct ispsoftc *isp, int wo, u_int16_t *rp)
   5138 {
   5139 	int i, cbits;
   5140 	u_int16_t bit, rqst;
   5141 
   5142 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   5143 	USEC_DELAY(2);
   5144 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   5145 	USEC_DELAY(2);
   5146 
   5147 	if (IS_FC(isp)) {
   5148 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   5149 		rqst = (ISP_NVRAM_READ << 8) | wo;
   5150 		cbits = 10;
   5151 	} else if (IS_ULTRA2(isp)) {
   5152 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
   5153 		rqst = (ISP_NVRAM_READ << 8) | wo;
   5154 		cbits = 10;
   5155 	} else {
   5156 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   5157 		rqst = (ISP_NVRAM_READ << 6) | wo;
   5158 		cbits = 8;
   5159 	}
   5160 
   5161 	/*
   5162 	 * Clock the word select request out...
   5163 	 */
   5164 	for (i = cbits; i >= 0; i--) {
   5165 		if ((rqst >> i) & 1) {
   5166 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   5167 		} else {
   5168 			bit = BIU_NVRAM_SELECT;
   5169 		}
   5170 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5171 		USEC_DELAY(2);
   5172 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   5173 		USEC_DELAY(2);
   5174 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5175 		USEC_DELAY(2);
   5176 	}
   5177 	/*
   5178 	 * Now read the result back in (bits come back in MSB format).
   5179 	 */
   5180 	*rp = 0;
   5181 	for (i = 0; i < 16; i++) {
   5182 		u_int16_t rv;
   5183 		*rp <<= 1;
   5184 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   5185 		USEC_DELAY(2);
   5186 		rv = ISP_READ(isp, BIU_NVRAM);
   5187 		if (rv & BIU_NVRAM_DATAIN) {
   5188 			*rp |= 1;
   5189 		}
   5190 		USEC_DELAY(2);
   5191 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   5192 		USEC_DELAY(2);
   5193 	}
   5194 	ISP_WRITE(isp, BIU_NVRAM, 0);
   5195 	USEC_DELAY(2);
   5196 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
   5197 }
   5198 
   5199 static void
   5200 isp_parse_nvram_1020(struct ispsoftc *isp, u_int8_t *nvram_data)
   5201 {
   5202 	sdparam *sdp = (sdparam *) isp->isp_param;
   5203 	int tgt;
   5204 
   5205 	sdp->isp_fifo_threshold =
   5206 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
   5207 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
   5208 
   5209 	sdp->isp_initiator_id =
   5210 		ISP_NVRAM_INITIATOR_ID(nvram_data);
   5211 
   5212 	sdp->isp_bus_reset_delay =
   5213 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   5214 
   5215 	sdp->isp_retry_count =
   5216 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   5217 
   5218 	sdp->isp_retry_delay =
   5219 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   5220 
   5221 	sdp->isp_async_data_setup =
   5222 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   5223 
   5224 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   5225 		if (sdp->isp_async_data_setup < 9) {
   5226 			sdp->isp_async_data_setup = 9;
   5227 		}
   5228 	} else {
   5229 		if (sdp->isp_async_data_setup != 6) {
   5230 			sdp->isp_async_data_setup = 6;
   5231 		}
   5232 	}
   5233 
   5234 	sdp->isp_req_ack_active_neg =
   5235 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   5236 
   5237 	sdp->isp_data_line_active_neg =
   5238 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   5239 
   5240 	sdp->isp_data_dma_burst_enabl =
   5241 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   5242 
   5243 	sdp->isp_cmd_dma_burst_enable =
   5244 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   5245 
   5246 	sdp->isp_tag_aging =
   5247 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   5248 
   5249 	sdp->isp_selection_timeout =
   5250 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   5251 
   5252 	sdp->isp_max_queue_depth =
   5253 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   5254 
   5255 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   5256 
   5257 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   5258 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   5259 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   5260 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   5261 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   5262 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   5263 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   5264 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   5265 
   5266 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5267 		sdp->isp_devparam[tgt].dev_enable =
   5268 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
   5269 		sdp->isp_devparam[tgt].exc_throttle =
   5270 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
   5271 		sdp->isp_devparam[tgt].nvrm_offset =
   5272 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
   5273 		sdp->isp_devparam[tgt].nvrm_period =
   5274 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
   5275 		/*
   5276 		 * We probably shouldn't lie about this, but it
   5277 		 * it makes it much safer if we limit NVRAM values
   5278 		 * to sanity.
   5279 		 */
   5280 		if (isp->isp_type < ISP_HA_SCSI_1040) {
   5281 			/*
   5282 			 * If we're not ultra, we can't possibly
   5283 			 * be a shorter period than this.
   5284 			 */
   5285 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
   5286 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
   5287 			}
   5288 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
   5289 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
   5290 			}
   5291 		} else {
   5292 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
   5293 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
   5294 			}
   5295 		}
   5296 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   5297 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
   5298 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   5299 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   5300 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
   5301 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   5302 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
   5303 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   5304 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
   5305 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   5306 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
   5307 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   5308 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
   5309 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   5310 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
   5311 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   5312 		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5313 		    sdp->isp_devparam[tgt].nvrm_offset,
   5314 		    sdp->isp_devparam[tgt].nvrm_period);
   5315 		sdp->isp_devparam[tgt].goal_offset =
   5316 		    sdp->isp_devparam[tgt].nvrm_offset;
   5317 		sdp->isp_devparam[tgt].goal_period =
   5318 		    sdp->isp_devparam[tgt].nvrm_period;
   5319 		sdp->isp_devparam[tgt].goal_flags =
   5320 		    sdp->isp_devparam[tgt].nvrm_flags;
   5321 	}
   5322 }
   5323 
   5324 static void
   5325 isp_parse_nvram_1080(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   5326 {
   5327 	sdparam *sdp = (sdparam *) isp->isp_param;
   5328 	int tgt;
   5329 
   5330 	sdp += bus;
   5331 
   5332 	sdp->isp_fifo_threshold =
   5333 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
   5334 
   5335 	sdp->isp_initiator_id =
   5336 	    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
   5337 
   5338 	sdp->isp_bus_reset_delay =
   5339 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   5340 
   5341 	sdp->isp_retry_count =
   5342 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   5343 
   5344 	sdp->isp_retry_delay =
   5345 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   5346 
   5347 	sdp->isp_async_data_setup =
   5348 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   5349 
   5350 	sdp->isp_req_ack_active_neg =
   5351 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   5352 
   5353 	sdp->isp_data_line_active_neg =
   5354 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   5355 
   5356 	sdp->isp_data_dma_burst_enabl =
   5357 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   5358 
   5359 	sdp->isp_cmd_dma_burst_enable =
   5360 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   5361 
   5362 	sdp->isp_selection_timeout =
   5363 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   5364 
   5365 	sdp->isp_max_queue_depth =
   5366 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   5367 
   5368 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   5369 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   5370 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   5371 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   5372 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   5373 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   5374 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   5375 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   5376 
   5377 
   5378 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5379 		sdp->isp_devparam[tgt].dev_enable =
   5380 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   5381 		sdp->isp_devparam[tgt].exc_throttle =
   5382 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   5383 		sdp->isp_devparam[tgt].nvrm_offset =
   5384 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   5385 		sdp->isp_devparam[tgt].nvrm_period =
   5386 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   5387 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   5388 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   5389 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   5390 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   5391 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   5392 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   5393 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   5394 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   5395 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   5396 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   5397 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   5398 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   5399 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   5400 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   5401 		sdp->isp_devparam[tgt].actv_flags = 0;
   5402 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   5403 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5404 		    sdp->isp_devparam[tgt].nvrm_offset,
   5405 		    sdp->isp_devparam[tgt].nvrm_period);
   5406 		sdp->isp_devparam[tgt].goal_offset =
   5407 		    sdp->isp_devparam[tgt].nvrm_offset;
   5408 		sdp->isp_devparam[tgt].goal_period =
   5409 		    sdp->isp_devparam[tgt].nvrm_period;
   5410 		sdp->isp_devparam[tgt].goal_flags =
   5411 		    sdp->isp_devparam[tgt].nvrm_flags;
   5412 	}
   5413 }
   5414 
   5415 static void
   5416 isp_parse_nvram_12160(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   5417 {
   5418 	sdparam *sdp = (sdparam *) isp->isp_param;
   5419 	int tgt;
   5420 
   5421 	sdp += bus;
   5422 
   5423 	sdp->isp_fifo_threshold =
   5424 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
   5425 
   5426 	sdp->isp_initiator_id =
   5427 	    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
   5428 
   5429 	sdp->isp_bus_reset_delay =
   5430 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   5431 
   5432 	sdp->isp_retry_count =
   5433 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   5434 
   5435 	sdp->isp_retry_delay =
   5436 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   5437 
   5438 	sdp->isp_async_data_setup =
   5439 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   5440 
   5441 	sdp->isp_req_ack_active_neg =
   5442 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   5443 
   5444 	sdp->isp_data_line_active_neg =
   5445 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   5446 
   5447 	sdp->isp_data_dma_burst_enabl =
   5448 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   5449 
   5450 	sdp->isp_cmd_dma_burst_enable =
   5451 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   5452 
   5453 	sdp->isp_selection_timeout =
   5454 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   5455 
   5456 	sdp->isp_max_queue_depth =
   5457 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   5458 
   5459 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   5460 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   5461 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   5462 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   5463 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   5464 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   5465 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   5466 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   5467 
   5468 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5469 		sdp->isp_devparam[tgt].dev_enable =
   5470 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   5471 		sdp->isp_devparam[tgt].exc_throttle =
   5472 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   5473 		sdp->isp_devparam[tgt].nvrm_offset =
   5474 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   5475 		sdp->isp_devparam[tgt].nvrm_period =
   5476 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   5477 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   5478 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   5479 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   5480 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   5481 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   5482 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   5483 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   5484 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   5485 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   5486 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   5487 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   5488 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   5489 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   5490 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   5491 		sdp->isp_devparam[tgt].actv_flags = 0;
   5492 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   5493 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5494 		    sdp->isp_devparam[tgt].nvrm_offset,
   5495 		    sdp->isp_devparam[tgt].nvrm_period);
   5496 		sdp->isp_devparam[tgt].goal_offset =
   5497 		    sdp->isp_devparam[tgt].nvrm_offset;
   5498 		sdp->isp_devparam[tgt].goal_period =
   5499 		    sdp->isp_devparam[tgt].nvrm_period;
   5500 		sdp->isp_devparam[tgt].goal_flags =
   5501 		    sdp->isp_devparam[tgt].nvrm_flags;
   5502 	}
   5503 }
   5504 
   5505 static void
   5506 isp_parse_nvram_2100(struct ispsoftc *isp, u_int8_t *nvram_data)
   5507 {
   5508 	fcparam *fcp = (fcparam *) isp->isp_param;
   5509 	u_int64_t wwn;
   5510 
   5511 	/*
   5512 	 * There is NVRAM storage for both Port and Node entities-
   5513 	 * but the Node entity appears to be unused on all the cards
   5514 	 * I can find. However, we should account for this being set
   5515 	 * at some point in the future.
   5516 	 *
   5517 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
   5518 	 * bits 48..60. In the case of the 2202, it appears that they do
   5519 	 * use bit 48 to distinguish between the two instances on the card.
   5520 	 * The 2204, which I've never seen, *probably* extends this method.
   5521 	 */
   5522 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
   5523 	if (wwn) {
   5524 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
   5525 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
   5526 		if ((wwn >> 60) == 0) {
   5527 			wwn |= (((u_int64_t) 2)<< 60);
   5528 		}
   5529 	}
   5530 	fcp->isp_portwwn = wwn;
   5531 	wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
   5532 	if (wwn) {
   5533 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
   5534 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
   5535 		if ((wwn >> 60) == 0) {
   5536 			wwn |= (((u_int64_t) 2)<< 60);
   5537 		}
   5538 	}
   5539 	fcp->isp_nodewwn = wwn;
   5540 
   5541 	/*
   5542 	 * Make sure we have both Node and Port as non-zero values.
   5543 	 */
   5544 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
   5545 		fcp->isp_portwwn = fcp->isp_nodewwn;
   5546 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
   5547 		fcp->isp_nodewwn = fcp->isp_portwwn;
   5548 	}
   5549 
   5550 	/*
   5551 	 * Make the Node and Port values sane if they're NAA == 2.
   5552 	 * This means to clear bits 48..56 for the Node WWN and
   5553 	 * make sure that there's some non-zero value in 48..56
   5554 	 * for the Port WWN.
   5555 	 */
   5556 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
   5557 		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
   5558 		    (fcp->isp_nodewwn >> 60) == 2) {
   5559 			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
   5560 		}
   5561 		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
   5562 		    (fcp->isp_portwwn >> 60) == 2) {
   5563 			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
   5564 		}
   5565 	}
   5566 
   5567 	fcp->isp_maxalloc =
   5568 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   5569 	fcp->isp_maxfrmlen =
   5570 		ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   5571 	fcp->isp_retry_delay =
   5572 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   5573 	fcp->isp_retry_count =
   5574 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   5575 	fcp->isp_loopid =
   5576 		ISP2100_NVRAM_HARDLOOPID(nvram_data);
   5577 	fcp->isp_execthrottle =
   5578 		ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   5579 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
   5580 	isp_prt(isp, ISP_LOGDEBUG0,
   5581 	    "NVRAM: maxfrmlen %d execthrottle %d fwoptions 0x%x",
   5582 	    fcp->isp_maxfrmlen, fcp->isp_execthrottle, fcp->isp_fwoptions);
   5583 }
   5584