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