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