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