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