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