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