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