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