Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.55
      1 /* $NetBSD: isp.c,v 1.55 2000/07/06 01:17:38 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(2, ("%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 			loopid = 0;
   1503 			if (lim++ < FL_PORT_ID) {
   1504 				continue;
   1505 			}
   1506 			PRINTF("%s: giving up on synchronizing the port "
   1507 			    "database\n", isp->isp_name);
   1508 			return (-1);
   1509 		}
   1510 
   1511 		/*
   1512 		 * Save the pertinent info locally.
   1513 		 */
   1514 		lp->node_wwn =
   1515 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
   1516 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
   1517 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
   1518 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
   1519 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
   1520 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
   1521 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
   1522 		    (((u_int64_t)pdb.pdb_nodename[7]));
   1523 		lp->port_wwn =
   1524 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
   1525 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
   1526 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
   1527 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
   1528 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
   1529 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
   1530 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
   1531 		    (((u_int64_t)pdb.pdb_portname[7]));
   1532 		lp->roles =
   1533 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   1534 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
   1535 		lp->loopid = pdb.pdb_loopid;
   1536 		/*
   1537 		 * Do a quick check to see whether this matches the saved port
   1538 		 * database for the same loopid. We do this here to save
   1539 		 * searching later (if possible). Note that this fails over
   1540 		 * time as things shuffle on the loop- we get the current
   1541 		 * loop state (where loop id as an index matches loop id in
   1542 		 * use) and then compare it to our saved database which
   1543 		 * never shifts.
   1544 		 */
   1545 		if (target >= 0 && isp_same_lportdb(lp, &fcp->portdb[target])) {
   1546 			lp->valid = 1;
   1547 		}
   1548 	}
   1549 
   1550 	/*
   1551 	 * If we get this far, we've settled our differences with the f/w
   1552 	 * and we can say that the loop state is ready.
   1553 	 */
   1554 	fcp->isp_loopstate = LOOP_READY;
   1555 
   1556 	/*
   1557 	 * Mark all of the permanent local loop database entries as invalid.
   1558 	 */
   1559 	for (loopid = 0; loopid < FL_PORT_ID; loopid++) {
   1560 		fcp->portdb[loopid].valid = 0;
   1561 	}
   1562 
   1563 	/*
   1564 	 * Now merge our local copy of the port database into our saved copy.
   1565 	 * Notify the outer layers of new devices arriving.
   1566 	 */
   1567 	for (loopid = 0; loopid < prange; loopid++) {
   1568 		int i;
   1569 
   1570 		/*
   1571 		 * If we don't have a non-zero Port WWN, we're not here.
   1572 		 */
   1573 		if (tport[loopid].port_wwn == 0) {
   1574 			continue;
   1575 		}
   1576 
   1577 		/*
   1578 		 * If we've already marked our tmp copy as valid,
   1579 		 * this means that we've decided that it's the
   1580 		 * same as our saved data base. This didn't include
   1581 		 * the 'valid' marking so we have set that here.
   1582 		 */
   1583 		if (tport[loopid].valid) {
   1584 			fcp->portdb[loopid].valid = 1;
   1585 			continue;
   1586 		}
   1587 
   1588 		/*
   1589 		 * For the purposes of deciding whether this is the
   1590 		 * 'same' device or not, we only search for an identical
   1591 		 * Port WWN. Node WWNs may or may not be the same as
   1592 		 * the Port WWN, and there may be multiple different
   1593 		 * Port WWNs with the same Node WWN. It would be chaos
   1594 		 * to have multiple identical Port WWNs, so we don't
   1595 		 * allow that.
   1596 		 */
   1597 
   1598 		for (i = 0; i < FL_PORT_ID; i++) {
   1599 			int j;
   1600 			if (fcp->portdb[i].port_wwn == 0)
   1601 				continue;
   1602 			if (fcp->portdb[i].port_wwn != tport[loopid].port_wwn)
   1603 				continue;
   1604 			/*
   1605 			 * We found this WWN elsewhere- it's changed
   1606 			 * loopids then. We don't change it's actual
   1607 			 * position in our cached port database- we
   1608 			 * just change the actual loop ID we'd use.
   1609 			 */
   1610 			if (fcp->portdb[i].loopid != loopid) {
   1611 				PRINTF("%s: Target ID %d Loop 0x%x (Port 0x%x) "
   1612 				    "=> Loop 0x%x (Port 0x%x) \n",
   1613 				    isp->isp_name, i, fcp->portdb[i].loopid,
   1614 				    fcp->portdb[i].portid, loopid,
   1615 				    tport[loopid].portid);
   1616 			}
   1617 			fcp->portdb[i].portid = tport[loopid].portid;
   1618 			fcp->portdb[i].loopid = loopid;
   1619 			fcp->portdb[i].valid = 1;
   1620 			fcp->portdb[i].roles = tport[loopid].roles;
   1621 
   1622 			/*
   1623 			 * Now make sure this Port WWN doesn't exist elsewhere
   1624 			 * in the port database.
   1625 			 */
   1626 			for (j = i+1; j < FL_PORT_ID; j++) {
   1627 				if (fcp->portdb[i].port_wwn !=
   1628 				    fcp->portdb[j].port_wwn) {
   1629 					continue;
   1630 				}
   1631 				PRINTF("%s: Target ID %d Duplicates Target ID "
   1632 				    "%d- killing off both\n",
   1633 				    isp->isp_name, j, i);
   1634 				/*
   1635 				 * Invalidate the 'old' *and* 'new' ones.
   1636 				 * This is really harsh and not quite right,
   1637 				 * but if this happens, we really don't know
   1638 				 * who is what at this point.
   1639 				 */
   1640 				fcp->portdb[i].valid = 0;
   1641 				fcp->portdb[j].valid = 0;
   1642 			}
   1643 			break;
   1644 		}
   1645 
   1646 		/*
   1647 		 * If we didn't traverse the entire port database,
   1648 		 * then we found (and remapped) an existing entry.
   1649 		 * No need to notify anyone- go for the next one.
   1650 		 */
   1651 		if (i < FL_PORT_ID) {
   1652 			continue;
   1653 		}
   1654 
   1655 		/*
   1656 		 * We've not found this Port WWN anywhere. It's a new entry.
   1657 		 * See if we can leave it where it is (with target == loopid).
   1658 		 */
   1659 		if (fcp->portdb[loopid].port_wwn != 0) {
   1660 			for (lim = 0; lim < FL_PORT_ID; lim++) {
   1661 				if (fcp->portdb[lim].port_wwn == 0)
   1662 					break;
   1663 			}
   1664 			/* "Cannot Happen" */
   1665 			if (lim == FL_PORT_ID) {
   1666 				PRINTF("%s: remap overflow?\n", isp->isp_name);
   1667 				continue;
   1668 			}
   1669 			i = lim;
   1670 		} else {
   1671 			i = loopid;
   1672 		}
   1673 
   1674 		/*
   1675 		 * NB:	The actual loopid we use here is loopid- we may
   1676 		 *	in fact be at a completely different index (target).
   1677 		 */
   1678 		fcp->portdb[i].loopid = loopid;
   1679 		fcp->portdb[i].port_wwn = tport[loopid].port_wwn;
   1680 		fcp->portdb[i].node_wwn = tport[loopid].node_wwn;
   1681 		fcp->portdb[i].roles = tport[loopid].roles;
   1682 		fcp->portdb[i].portid = tport[loopid].portid;
   1683 		fcp->portdb[i].valid = 1;
   1684 
   1685 		/*
   1686 		 * Tell the outside world we've arrived.
   1687 		 */
   1688 		(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &i);
   1689 	}
   1690 
   1691 	/*
   1692 	 * Now find all previously used targets that are now invalid and
   1693 	 * notify the outer layers that they're gone.
   1694 	 */
   1695 	for (lp = fcp->portdb; lp < &fcp->portdb[prange]; lp++) {
   1696 		if (lp->valid || lp->port_wwn == 0)
   1697 			continue;
   1698 
   1699 		/*
   1700 		 * Tell the outside world we've gone away.
   1701 		 */
   1702 		loopid = lp - fcp->portdb;
   1703 		(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &loopid);
   1704 		MEMZERO((void *) lp, sizeof (*lp));
   1705 	}
   1706 
   1707 #ifdef	ISP2100_FABRIC
   1708 	/*
   1709 	 * Now log in any fabric devices
   1710 	 */
   1711 	for (lp = &fcp->portdb[FC_SNS_ID+1];
   1712 	     lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
   1713 		u_int32_t portid;
   1714 		mbreg_t mbs;
   1715 
   1716 		/*
   1717 		 * Anything here?
   1718 		 */
   1719 		if (lp->port_wwn == 0)
   1720 			continue;
   1721 
   1722 		/*
   1723 		 * Don't try to log into yourself.
   1724 		 */
   1725 		if ((portid = lp->portid) == fcp->isp_portid)
   1726 			continue;
   1727 
   1728 
   1729 		/*
   1730 		 * If we'd been logged in- see if we still are and we haven't
   1731 		 * changed. If so, no need to log ourselves out, etc..
   1732 		 */
   1733 		if (lp->loggedin &&
   1734 		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
   1735 			int nrole;
   1736 			u_int64_t nwwnn, nwwpn;
   1737 			nwwnn =
   1738 			    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
   1739 			    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
   1740 			    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
   1741 			    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
   1742 			    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
   1743 			    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
   1744 			    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
   1745 			    (((u_int64_t)pdb.pdb_nodename[7]));
   1746 			nwwpn =
   1747 			    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
   1748 			    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
   1749 			    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
   1750 			    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
   1751 			    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
   1752 			    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
   1753 			    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
   1754 			    (((u_int64_t)pdb.pdb_portname[7]));
   1755 			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
   1756 			    SVC3_ROLE_SHIFT;
   1757 			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
   1758 			    (u_int32_t) BITS2WORD(pdb.pdb_portid_bits) &&
   1759 			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
   1760 			    lp->roles == nrole) {
   1761 				lp->loggedin = lp->valid = 1;
   1762 				IDPRINTF(1, ("%s: retained login of Target %d "
   1763 				    "(Loop 0x%x) Port ID 0x%x\n",
   1764 				    isp->isp_name, (int) (lp - fcp->portdb),
   1765 				    (int) lp->loopid, lp->portid));
   1766 				continue;
   1767 			}
   1768 		}
   1769 
   1770 		/*
   1771 		 * Force a logout if we were logged in.
   1772 		 */
   1773 		if (lp->loggedin) {
   1774 			mbs.param[0] = MBOX_FABRIC_LOGOUT;
   1775 			mbs.param[1] = lp->loopid << 8;
   1776 			mbs.param[2] = 0;
   1777 			mbs.param[3] = 0;
   1778 			isp_mboxcmd(isp, &mbs);
   1779 			lp->loggedin = 0;
   1780 			IDPRINTF(1, ("%s: Logging out target %d at Loop ID %d "
   1781 			    "(port id 0x%x)\n", isp->isp_name,
   1782 			    (int) (lp - fcp->portdb), lp->loopid, lp->portid));
   1783 		}
   1784 
   1785 		/*
   1786 		 * And log in....
   1787 		 */
   1788 		loopid = lp - fcp->portdb;
   1789 		lp->loopid = 0;
   1790 		do {
   1791 			mbs.param[0] = MBOX_FABRIC_LOGIN;
   1792 			mbs.param[1] = loopid << 8;
   1793 			mbs.param[2] = portid >> 16;
   1794 			mbs.param[3] = portid & 0xffff;
   1795 			if (IS_2200(isp)) {
   1796 				/* only issue a PLOGI if not logged in */
   1797 				mbs.param[1] |= 0x1;
   1798 			}
   1799 			isp_mboxcmd(isp, &mbs);
   1800 			switch (mbs.param[0]) {
   1801 			case MBOX_LOOP_ID_USED:
   1802 				/*
   1803 				 * Try the next available loop id.
   1804 				 */
   1805 				loopid++;
   1806 				break;
   1807 			case MBOX_PORT_ID_USED:
   1808 				/*
   1809 				 * This port is already logged in.
   1810 				 * Snaffle the loop id it's using if it's
   1811 				 * nonzero, otherwise we're hosed.
   1812 				 */
   1813 				if (mbs.param[1] != 0) {
   1814 					loopid = mbs.param[1];
   1815 					IDPRINTF(1, ("%s: Retaining loopid 0x%x"
   1816 					    " for Target %d (port id 0x%x)\n",
   1817 					    isp->isp_name, loopid,
   1818 					    (int) (lp - fcp->portdb),
   1819 					    lp->portid));
   1820 				} else {
   1821 					loopid = MAX_FC_TARG;
   1822 					break;
   1823 				}
   1824 				/* FALLTHROUGH */
   1825 			case MBOX_COMMAND_COMPLETE:
   1826 				lp->loggedin = 1;
   1827 				lp->loopid = loopid;
   1828 				break;
   1829 			case MBOX_COMMAND_ERROR:
   1830 				PRINTF("%s: command error in PLOGI for port "
   1831 				    " 0x%x (0x%x)\n", isp->isp_name, portid,
   1832 				    mbs.param[1]);
   1833 				/* FALLTHROUGH */
   1834 			case MBOX_ALL_IDS_USED: /* We're outta IDs */
   1835 			default:
   1836 				loopid = MAX_FC_TARG;
   1837 				break;
   1838 			}
   1839 		} while (lp->loopid == 0 && loopid < MAX_FC_TARG);
   1840 
   1841 		/*
   1842 		 * If we get here and we haven't set a Loop ID,
   1843 		 * we failed to log into this device.
   1844 		 */
   1845 
   1846 		if (lp->loopid == 0) {
   1847 			continue;
   1848 		}
   1849 
   1850 		/*
   1851 		 * Make sure we can get the approriate port information.
   1852 		 */
   1853 		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
   1854 			PRINTF("%s: could not get PDB for device@port 0x%x\n",
   1855 			    isp->isp_name, lp->portid);
   1856 			goto dump_em;
   1857 		}
   1858 
   1859 		if (pdb.pdb_loopid != lp->loopid) {
   1860 			PRINTF("%s: PDB loopid info for device@port 0x%x does "
   1861 			    "not match up (0x%x)\n", isp->isp_name, lp->portid,
   1862 			    pdb.pdb_loopid);
   1863 			goto dump_em;
   1864 		}
   1865 
   1866 		if (lp->portid != (u_int32_t) BITS2WORD(pdb.pdb_portid_bits)) {
   1867 			PRINTF("%s: PDB port info for device@port 0x%x does "
   1868 			    "not match up (0x%x)\n", isp->isp_name, lp->portid,
   1869 			    BITS2WORD(pdb.pdb_portid_bits));
   1870 			goto dump_em;
   1871 		}
   1872 
   1873 		lp->roles =
   1874 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   1875 		lp->node_wwn =
   1876 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
   1877 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
   1878 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
   1879 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
   1880 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
   1881 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
   1882 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
   1883 		    (((u_int64_t)pdb.pdb_nodename[7]));
   1884 		lp->port_wwn =
   1885 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
   1886 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
   1887 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
   1888 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
   1889 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
   1890 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
   1891 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
   1892 		    (((u_int64_t)pdb.pdb_portname[7]));
   1893 		/*
   1894 		 * Check to make sure this all makes sense.
   1895 		 */
   1896 		if (lp->node_wwn && lp->port_wwn) {
   1897 			lp->valid = 1;
   1898 			loopid = lp - fcp->portdb;
   1899 			(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &loopid);
   1900 			continue;
   1901 		}
   1902 dump_em:
   1903 		lp->valid = 0;
   1904 		PRINTF("%s: Target %d (Loop 0x%x) Port ID 0x%x dumped after "
   1905 		    "login\n", isp->isp_name, loopid, lp->loopid, lp->portid);
   1906 		mbs.param[0] = MBOX_FABRIC_LOGOUT;
   1907 		mbs.param[1] = lp->loopid << 8;
   1908 		mbs.param[2] = 0;
   1909 		mbs.param[3] = 0;
   1910 		isp_mboxcmd(isp, &mbs);
   1911 	}
   1912 #endif
   1913 	/*
   1914 	 * If we get here, we've for sure seen not only a valid loop
   1915 	 * but know what is or isn't on it, so mark this for usage
   1916 	 * in ispscsicmd.
   1917 	 */
   1918 	fcp->loop_seen_once = 1;
   1919 	return (0);
   1920 }
   1921 
   1922 #ifdef	ISP2100_FABRIC
   1923 static int
   1924 isp_scan_fabric(isp)
   1925 	struct ispsoftc *isp;
   1926 {
   1927 	fcparam *fcp = isp->isp_param;
   1928 	u_int32_t portid, first_nz_portid;
   1929 	sns_screq_t *reqp;
   1930 	sns_scrsp_t *resp;
   1931 	mbreg_t mbs;
   1932 	int hicap;
   1933 
   1934 	reqp = (sns_screq_t *) fcp->isp_scratch;
   1935 	resp = (sns_scrsp_t *) (&((char *)fcp->isp_scratch)[0x100]);
   1936 	first_nz_portid = portid = fcp->isp_portid;
   1937 
   1938 	for (hicap = 0; hicap < 1024; hicap++) {
   1939 		MEMZERO((void *) reqp, SNS_GAN_REQ_SIZE);
   1940 		reqp->snscb_rblen = SNS_GAN_RESP_SIZE >> 1;
   1941 		reqp->snscb_addr[RQRSP_ADDR0015] =
   1942 			DMA_LSW(fcp->isp_scdma + 0x100);
   1943 		reqp->snscb_addr[RQRSP_ADDR1631] =
   1944 			DMA_MSW(fcp->isp_scdma + 0x100);
   1945 		reqp->snscb_sblen = 6;
   1946 		reqp->snscb_data[0] = SNS_GAN;
   1947 		reqp->snscb_data[4] = portid & 0xffff;
   1948 		reqp->snscb_data[5] = (portid >> 16) & 0xff;
   1949 		ISP_SWIZZLE_SNS_REQ(isp, reqp);
   1950 		mbs.param[0] = MBOX_SEND_SNS;
   1951 		mbs.param[1] = SNS_GAN_REQ_SIZE >> 1;
   1952 		mbs.param[2] = DMA_MSW(fcp->isp_scdma);
   1953 		mbs.param[3] = DMA_LSW(fcp->isp_scdma);
   1954 		mbs.param[6] = 0;
   1955 		mbs.param[7] = 0;
   1956 		MemoryBarrier();
   1957 		isp_mboxcmd(isp, &mbs);
   1958 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1959 			IDPRINTF(1, ("%s: SNS failed (0x%x)\n", isp->isp_name,
   1960 			    mbs.param[0]));
   1961 			return (-1);
   1962 		}
   1963 		ISP_UNSWIZZLE_SNS_RSP(isp, resp, SNS_GAN_RESP_SIZE >> 1);
   1964 		portid = (((u_int32_t) resp->snscb_port_id[0]) << 16) |
   1965 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
   1966 		    (((u_int32_t) resp->snscb_port_id[2]));
   1967 		if (isp_async(isp, ISPASYNC_FABRIC_DEV, resp)) {
   1968 			return (-1);
   1969 		}
   1970 		if (first_nz_portid == 0 && portid) {
   1971 			first_nz_portid = portid;
   1972 		}
   1973 		if (first_nz_portid == portid) {
   1974 			return (0);
   1975 		}
   1976 	}
   1977 	/*
   1978 	 * We either have a broken name server or a huge fabric if we get here.
   1979 	 */
   1980 	return (0);
   1981 }
   1982 #endif
   1983 /*
   1984  * Start a command. Locking is assumed done in the caller.
   1985  */
   1986 
   1987 int32_t
   1988 ispscsicmd(xs)
   1989 	ISP_SCSI_XFER_T *xs;
   1990 {
   1991 	struct ispsoftc *isp;
   1992 	u_int16_t iptr, optr;
   1993 	union {
   1994 		ispreq_t *_reqp;
   1995 		ispreqt2_t *_t2reqp;
   1996 	} _u;
   1997 #define	reqp	_u._reqp
   1998 #define	t2reqp	_u._t2reqp
   1999 #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
   2000 	int target, i;
   2001 
   2002 	XS_INITERR(xs);
   2003 	isp = XS_ISP(xs);
   2004 
   2005 	if (isp->isp_state != ISP_RUNSTATE) {
   2006 		PRINTF("%s: adapter not ready\n", isp->isp_name);
   2007 		XS_SETERR(xs, HBA_BOTCH);
   2008 		return (CMD_COMPLETE);
   2009 	}
   2010 
   2011 	/*
   2012 	 * Check command CDB length, etc.. We really are limited to 16 bytes
   2013 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
   2014 	 * but probably only if we're running fairly new firmware (we'll
   2015 	 * let the old f/w choke on an extended command queue entry).
   2016 	 */
   2017 
   2018 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
   2019 		PRINTF("%s: unsupported cdb length (%d, CDB[0]=0x%x)\n",
   2020 		    isp->isp_name, XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
   2021 		XS_SETERR(xs, HBA_BOTCH);
   2022 		return (CMD_COMPLETE);
   2023 	}
   2024 
   2025 	/*
   2026 	 * Check to see whether we have good firmware state still or
   2027 	 * need to refresh our port database for this target.
   2028 	 */
   2029 	target = XS_TGT(xs);
   2030 	if (IS_FC(isp)) {
   2031 		fcparam *fcp = isp->isp_param;
   2032 		struct lportdb *lp;
   2033 #if	defined(ISP2100_FABRIC)
   2034 		/*
   2035 		 * If we're not on a Fabric, we can't have a target
   2036 		 * above FL_PORT_ID-1. If we're on a fabric and
   2037 		 * connected as an F-port, we can't have a target
   2038 		 * less than FC_SNS_ID+1.
   2039 		 */
   2040 		if (fcp->isp_onfabric == 0) {
   2041 			if (target >= FL_PORT_ID) {
   2042 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2043 				return (CMD_COMPLETE);
   2044 			}
   2045 		} else {
   2046 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
   2047 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2048 				return (CMD_COMPLETE);
   2049 			}
   2050 			if (fcp->isp_topo == TOPO_F_PORT &&
   2051 			    target < FL_PORT_ID) {
   2052 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2053 				return (CMD_COMPLETE);
   2054 			}
   2055 		}
   2056 #endif
   2057 		/*
   2058 		 * Check for f/w being in ready state. If the f/w
   2059 		 * isn't in ready state, then we don't know our
   2060 		 * loop ID and the f/w hasn't completed logging
   2061 		 * into all targets on the loop. If this is the
   2062 		 * case, then bounce the command. We pretend this is
   2063 		 * a SELECTION TIMEOUT error if we've never gone to
   2064 		 * FW_READY state at all- in this case we may not
   2065 		 * be hooked to a loop at all and we shouldn't hang
   2066 		 * the machine for this. Otherwise, defer this command
   2067 		 * until later.
   2068 		 */
   2069 		if (fcp->isp_fwstate != FW_READY) {
   2070 			if (isp_fclink_test(isp, FC_FW_READY_DELAY)) {
   2071 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2072 				if (fcp->loop_seen_once) {
   2073 					return (CMD_RQLATER);
   2074 				} else {
   2075 					return (CMD_COMPLETE);
   2076 				}
   2077 			}
   2078 		}
   2079 
   2080 		/*
   2081 		 * If our loop state is such that we haven't yet received
   2082 		 * a "Port Database Changed" notification (after a LIP or
   2083 		 * a Loop Reset or firmware initialization), then defer
   2084 		 * sending commands for a little while, but only if we've
   2085 		 * seen a valid loop at one point (otherwise we can get
   2086 		 * stuck at initialization time).
   2087 		 */
   2088 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
   2089 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2090 			if (fcp->loop_seen_once) {
   2091 				return (CMD_RQLATER);
   2092 			} else {
   2093 				return (CMD_COMPLETE);
   2094 			}
   2095 		}
   2096 
   2097 		/*
   2098 		 * If our loop state is now such that we've just now
   2099 		 * received a Port Database Change notification, then
   2100 		 * we have to go off and (re)synchronize our port
   2101 		 * database.
   2102 		 */
   2103 		if (fcp->isp_loopstate == LOOP_PDB_RCVD) {
   2104 			if (isp_pdb_sync(isp, target)) {
   2105 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2106 				return (CMD_COMPLETE);
   2107 			}
   2108 		}
   2109 
   2110 		/*
   2111 		 * Now check whether we should even think about pursuing this.
   2112 		 */
   2113 		lp = &fcp->portdb[target];
   2114 		if (lp->valid == 0) {
   2115 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2116 			return (CMD_COMPLETE);
   2117 		}
   2118 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
   2119 			IDPRINTF(3, ("%s: target %d is not a target\n",
   2120 			    isp->isp_name, target));
   2121 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2122 			return (CMD_COMPLETE);
   2123 		}
   2124 		/*
   2125 		 * Now turn target into what the actual loop ID is.
   2126 		 */
   2127 		target = lp->loopid;
   2128 	}
   2129 
   2130 	/*
   2131 	 * Next check to see if any HBA or Device
   2132 	 * parameters need to be updated.
   2133 	 */
   2134 	if (isp->isp_update != 0) {
   2135 		isp_update(isp);
   2136 	}
   2137 
   2138 	if (isp_getrqentry(isp, &iptr, &optr, (void **) &reqp)) {
   2139 		IDPRINTF(2, ("%s: Request Queue Overflow\n", isp->isp_name));
   2140 		XS_SETERR(xs, HBA_BOTCH);
   2141 		return (CMD_EAGAIN);
   2142 	}
   2143 
   2144 	/*
   2145 	 * Now see if we need to synchronize the ISP with respect to anything.
   2146 	 * We do dual duty here (cough) for synchronizing for busses other
   2147 	 * than which we got here to send a command to.
   2148 	 */
   2149 	if (isp->isp_sendmarker) {
   2150 		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
   2151 		/*
   2152 		 * Check ports to send markers for...
   2153 		 */
   2154 		for (i = 0; i < n; i++) {
   2155 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
   2156 				continue;
   2157 			}
   2158 			MEMZERO((void *) reqp, sizeof (*reqp));
   2159 			reqp->req_header.rqs_entry_count = 1;
   2160 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
   2161 			reqp->req_modifier = SYNC_ALL;
   2162 			reqp->req_target = i << 7;	/* insert bus number */
   2163 			ISP_SWIZZLE_REQUEST(isp, reqp);
   2164 			MemoryBarrier();
   2165 			ISP_ADD_REQUEST(isp, iptr);
   2166 
   2167 			if (isp_getrqentry(isp, &iptr, &optr, (void **)&reqp)) {
   2168 				IDPRINTF(2, ("%s: Request Queue Overflow+\n",
   2169 				    isp->isp_name));
   2170 				XS_SETERR(xs, HBA_BOTCH);
   2171 				return (CMD_EAGAIN);
   2172 			}
   2173 		}
   2174 	}
   2175 
   2176 	MEMZERO((void *) reqp, UZSIZE);
   2177 	reqp->req_header.rqs_entry_count = 1;
   2178 	if (IS_FC(isp)) {
   2179 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
   2180 	} else {
   2181 		if (XS_CDBLEN(xs) > 12)
   2182 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
   2183 		else
   2184 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
   2185 	}
   2186 	reqp->req_header.rqs_flags = 0;
   2187 	reqp->req_header.rqs_seqno = 0;
   2188 	if (IS_FC(isp)) {
   2189 		/*
   2190 		 * See comment in isp_intr
   2191 		 */
   2192 		XS_RESID(xs) = 0;
   2193 
   2194 		/*
   2195 		 * Fibre Channel always requires some kind of tag.
   2196 		 * The Qlogic drivers seem be happy not to use a tag,
   2197 		 * but this breaks for some devices (IBM drives).
   2198 		 */
   2199 		if (XS_CANTAG(xs)) {
   2200 			t2reqp->req_flags = XS_KINDOF_TAG(xs);
   2201 		} else {
   2202 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
   2203 				t2reqp->req_flags = REQFLAG_HTAG;
   2204 			else
   2205 				t2reqp->req_flags = REQFLAG_OTAG;
   2206 		}
   2207 	} else {
   2208 		sdparam *sdp = (sdparam *)isp->isp_param;
   2209 		if ((sdp->isp_devparam[target].cur_dflags & DPARM_TQING) &&
   2210 		    XS_CANTAG(xs)) {
   2211 			reqp->req_flags = XS_KINDOF_TAG(xs);
   2212 		}
   2213 	}
   2214 	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
   2215 	if (IS_SCSI(isp)) {
   2216 		reqp->req_lun_trn = XS_LUN(xs);
   2217 		reqp->req_cdblen = XS_CDBLEN(xs);
   2218 	} else {
   2219 		if (isp->isp_maxluns > 16)
   2220 			t2reqp->req_scclun = XS_LUN(xs);
   2221 		else
   2222 			t2reqp->req_lun_trn = XS_LUN(xs);
   2223 	}
   2224 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
   2225 
   2226 	reqp->req_time = XS_TIME(xs) / 1000;
   2227 	if (reqp->req_time == 0 && XS_TIME(xs))
   2228 		reqp->req_time = 1;
   2229 
   2230 	/*
   2231 	 * Always give a bit more leeway to commands after a bus reset.
   2232 	 * XXX: DOES NOT DISTINGUISH WHICH PORT MAY HAVE BEEN SYNCED
   2233 	 */
   2234 	if (isp->isp_sendmarker && reqp->req_time < 5) {
   2235 		reqp->req_time = 5;
   2236 	}
   2237 	if (isp_save_xs(isp, xs, &reqp->req_handle)) {
   2238 		IDPRINTF(2, ("%s: out of xflist pointers\n", isp->isp_name));
   2239 		XS_SETERR(xs, HBA_BOTCH);
   2240 		return (CMD_EAGAIN);
   2241 	}
   2242 	/*
   2243 	 * Set up DMA and/or do any bus swizzling of the request entry
   2244 	 * so that the Qlogic F/W understands what is being asked of it.
   2245  	*/
   2246 	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
   2247 	if (i != CMD_QUEUED) {
   2248 		isp_destroy_handle(isp, reqp->req_handle);
   2249 		/*
   2250 		 * dmasetup sets actual error in packet, and
   2251 		 * return what we were given to return.
   2252 		 */
   2253 		return (i);
   2254 	}
   2255 	XS_SETERR(xs, HBA_NOERROR);
   2256 	IDPRINTF(5, ("%s(%d.%d.%d): START cmd 0x%x datalen %d\n",
   2257 	    isp->isp_name, XS_CHANNEL(xs), target, XS_LUN(xs),
   2258 	    reqp->req_cdb[0], XS_XFRLEN(xs)));
   2259 	MemoryBarrier();
   2260 	ISP_ADD_REQUEST(isp, iptr);
   2261 	isp->isp_nactive++;
   2262 	if (isp->isp_sendmarker)
   2263 		isp->isp_sendmarker = 0;
   2264 	return (CMD_QUEUED);
   2265 #undef	reqp
   2266 #undef	t2reqp
   2267 }
   2268 
   2269 /*
   2270  * isp control
   2271  * Locks (ints blocked) assumed held.
   2272  */
   2273 
   2274 int
   2275 isp_control(isp, ctl, arg)
   2276 	struct ispsoftc *isp;
   2277 	ispctl_t ctl;
   2278 	void *arg;
   2279 {
   2280 	ISP_SCSI_XFER_T *xs;
   2281 	mbreg_t mbs;
   2282 	int bus, tgt;
   2283 	u_int32_t handle;
   2284 
   2285 	switch (ctl) {
   2286 	default:
   2287 		PRINTF("%s: isp_control unknown control op %x\n",
   2288 		    isp->isp_name, ctl);
   2289 		break;
   2290 
   2291 	case ISPCTL_RESET_BUS:
   2292 		/*
   2293 		 * Issue a bus reset.
   2294 		 */
   2295 		mbs.param[0] = MBOX_BUS_RESET;
   2296 		mbs.param[2] = 0;
   2297 		if (IS_SCSI(isp)) {
   2298 			mbs.param[1] =
   2299 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
   2300 			if (mbs.param[1] < 2)
   2301 				mbs.param[1] = 2;
   2302 			bus = *((int *) arg);
   2303 			if (IS_DUALBUS(isp))
   2304 				mbs.param[2] = bus;
   2305 		} else {
   2306 			mbs.param[1] = 10;
   2307 			bus = 0;
   2308 		}
   2309 		isp->isp_sendmarker |= (1 << bus);
   2310 		isp_mboxcmd(isp, &mbs);
   2311 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2312 			isp_dumpregs(isp, "isp_control SCSI bus reset failed");
   2313 			break;
   2314 		}
   2315 		CFGPRINTF("%s: driver initiated bus reset of bus %d\n",
   2316 		    isp->isp_name, bus);
   2317 		return (0);
   2318 
   2319 	case ISPCTL_RESET_DEV:
   2320 		tgt = (*((int *) arg)) & 0xffff;
   2321 		bus = (*((int *) arg)) >> 16;
   2322 		mbs.param[0] = MBOX_ABORT_TARGET;
   2323 		mbs.param[1] = (tgt << 8) | (bus << 15);
   2324 		mbs.param[2] = 3;	/* 'delay', in seconds */
   2325 		isp_mboxcmd(isp, &mbs);
   2326 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2327 			PRINTF("%s: isp_control MBOX_RESET_DEV failure (code "
   2328 			    "%x)\n", isp->isp_name, mbs.param[0]);
   2329 			break;
   2330 		}
   2331 		PRINTF("%s: Target %d on Bus %d Reset Succeeded\n",
   2332 		    isp->isp_name, tgt, bus);
   2333 		isp->isp_sendmarker |= (1 << bus);
   2334 		return (0);
   2335 
   2336 	case ISPCTL_ABORT_CMD:
   2337 		xs = (ISP_SCSI_XFER_T *) arg;
   2338 		tgt = XS_TGT(xs);
   2339 		handle = isp_find_handle(isp, xs);
   2340 		if (handle == 0) {
   2341 			PRINTF("%s: isp_control- cannot find command to abort "
   2342 			    "in active list\n", isp->isp_name);
   2343 			break;
   2344 		}
   2345 		bus = XS_CHANNEL(xs);
   2346 		mbs.param[0] = MBOX_ABORT;
   2347 		if (IS_FC(isp)) {
   2348 			if (isp->isp_maxluns > 16) {
   2349 				mbs.param[1] = tgt << 8;
   2350 				mbs.param[4] = 0;
   2351 				mbs.param[5] = 0;
   2352 				mbs.param[6] = XS_LUN(xs);
   2353 			} else {
   2354 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
   2355 			}
   2356 		} else {
   2357 			mbs.param[1] =
   2358 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
   2359 		}
   2360 		mbs.param[3] = handle >> 16;
   2361 		mbs.param[2] = handle & 0xffff;
   2362 		isp_mboxcmd(isp, &mbs);
   2363 		switch (mbs.param[0]) {
   2364 		case MBOX_COMMAND_COMPLETE:
   2365 			IDPRINTF(1,
   2366 			    ("%s: command (handle 0x%x) for %d.%d.%d aborted\n",
   2367 			    isp->isp_name, handle, bus, tgt, XS_LUN(xs)));
   2368 			/* FALLTHROUGH */
   2369 		case MBOX_COMMAND_PARAM_ERROR:
   2370 			break;
   2371 		default:
   2372 			PRINTF("%s: command (handle 0x%x) abort failed (%x)\n",
   2373 			    isp->isp_name, handle, mbs.param[0]);
   2374 			break;
   2375 		}
   2376 		return (0);
   2377 
   2378 	case ISPCTL_UPDATE_PARAMS:
   2379 		isp_update(isp);
   2380 		return (0);
   2381 
   2382 	case ISPCTL_FCLINK_TEST:
   2383 		if (IS_FC(isp)) {
   2384 			return (isp_fclink_test(isp, FC_FW_READY_DELAY));
   2385 		}
   2386 		break;
   2387 
   2388 	case ISPCTL_PDB_SYNC:
   2389 		if (IS_FC(isp)) {
   2390 			return (isp_pdb_sync(isp, -1));
   2391 		}
   2392 		break;
   2393 
   2394 #ifdef	ISP_TARGET_MODE
   2395 	case ISPCTL_TOGGLE_TMODE:
   2396 		if (IS_SCSI(isp)) {
   2397 			int ena = *(int *)arg;
   2398 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
   2399 			mbs.param[1] = (ena)? ENABLE_TARGET_FLAG : 0;
   2400 			isp_mboxcmd(isp, &mbs);
   2401 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2402 				PRINTF("%s: cannot %sable target mode (0x%x)\n",
   2403 				    isp->isp_name, ena? "en" : "dis",
   2404 				    mbs.param[0]);
   2405 				break;
   2406 			}
   2407 		}
   2408 		return (0);
   2409 #endif
   2410 	}
   2411 	return (-1);
   2412 }
   2413 
   2414 /*
   2415  * Interrupt Service Routine(s).
   2416  *
   2417  * External (OS) framework has done the appropriate locking,
   2418  * and the locking will be held throughout this function.
   2419  */
   2420 
   2421 int
   2422 isp_intr(arg)
   2423 	void *arg;
   2424 {
   2425 	ISP_SCSI_XFER_T *complist[RESULT_QUEUE_LEN], *xs;
   2426 	struct ispsoftc *isp = arg;
   2427 	u_int16_t iptr, optr, isr, sema, junk;
   2428 	int i, nlooked = 0, ndone = 0;
   2429 
   2430 	if (IS_2100(isp)) {
   2431 		i = 0;
   2432 		do {
   2433 			isr = ISP_READ(isp, BIU_ISR);
   2434 			junk = ISP_READ(isp, BIU_ISR);
   2435 		} while (isr != junk && ++i < 1000);
   2436 		if (isr != junk) {
   2437 			PRINTF("%s: isr unsteady (%x, %x)\n",
   2438 			    isp->isp_name, isr, junk);
   2439 		}
   2440 		i = 0;
   2441 		do {
   2442 			sema = ISP_READ(isp, BIU_SEMA);
   2443 			junk = ISP_READ(isp, BIU_SEMA);
   2444 		} while (sema != junk && ++i < 1000);
   2445 		if (sema != junk) {
   2446 			PRINTF("%s: sema unsteady (%x, %x)\n",
   2447 			    isp->isp_name, sema, junk);
   2448 		}
   2449 	} else {
   2450 		isr = ISP_READ(isp, BIU_ISR);
   2451 		sema = ISP_READ(isp, BIU_SEMA);
   2452 	}
   2453 	IDPRINTF(5, ("%s: isp_intr isr %x sem %x\n", isp->isp_name, isr, sema));
   2454 	isr &= INT_PENDING_MASK(isp);
   2455 	sema &= BIU_SEMA_LOCK;
   2456 	if (isr == 0 && sema == 0) {
   2457 		return (0);
   2458 	}
   2459 
   2460 	if (sema) {
   2461 		u_int16_t mbox;
   2462 
   2463 		if (IS_2100(isp)) {
   2464 			i = 0;
   2465 			do {
   2466 				mbox = ISP_READ(isp, OUTMAILBOX0);
   2467 				junk = ISP_READ(isp, OUTMAILBOX0);;
   2468 			} while (junk != mbox && ++i < 1000);
   2469 			if (mbox != junk) {
   2470 				PRINTF("%s: mailbox0 unsteady (%x, %x)\n",
   2471 				    isp->isp_name, mbox, junk);
   2472 				ISP_WRITE(isp, BIU_SEMA, 0);
   2473 				ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2474 				return (1);
   2475 			}
   2476 		} else {
   2477 			mbox = ISP_READ(isp, OUTMAILBOX0);
   2478 		}
   2479 		if (mbox & 0x4000) {
   2480 			int obits, i = 0;
   2481 			if ((obits = isp->isp_mboxbsy) != 0) {
   2482 				isp->isp_mboxtmp[i++] = mbox;
   2483 				for (i = 1; i < 8; i++) {
   2484 					if ((obits & (1 << i)) == 0) {
   2485 						continue;
   2486 					}
   2487 					isp->isp_mboxtmp[i] =
   2488 					    ISP_READ(isp, MBOX_OFF(i));
   2489 				}
   2490 				MBOX_NOTIFY_COMPLETE(isp);
   2491 			} else {
   2492 				PRINTF("%s: Command Mbox 0x%x\n",
   2493 				    isp->isp_name, mbox);
   2494 			}
   2495 		} else {
   2496 			u_int32_t fhandle = isp_parse_async(isp, (int) mbox);
   2497 			IDPRINTF(4, ("%s: Async Mbox 0x%x\n",
   2498 			    isp->isp_name, mbox));
   2499 			if (fhandle > 0) {
   2500 				isp_fastpost_complete(isp, fhandle);
   2501 			}
   2502 		}
   2503 		if (IS_FC(isp) || isp->isp_state != ISP_RUNSTATE) {
   2504 			ISP_WRITE(isp, BIU_SEMA, 0);
   2505 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2506 			return (1);
   2507 		}
   2508 	}
   2509 
   2510 	/*
   2511 	 * We can't be getting this now.
   2512 	 */
   2513 	if (isp->isp_state != ISP_RUNSTATE) {
   2514 		PRINTF("%s: interrupt (isr=%x, sema=%x) when not ready\n",
   2515 		    isp->isp_name, isr, sema);
   2516 		ISP_WRITE(isp, INMAILBOX5, ISP_READ(isp, OUTMAILBOX5));
   2517 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2518 		ISP_WRITE(isp, BIU_SEMA, 0);
   2519 		return (1);
   2520 	}
   2521 
   2522 	/*
   2523 	 * You *must* read OUTMAILBOX5 prior to clearing the RISC interrupt.
   2524 	 */
   2525 	optr = isp->isp_residx;
   2526 
   2527 	if (IS_2100(isp)) {
   2528 		i = 0;
   2529 		do {
   2530 			iptr = ISP_READ(isp, OUTMAILBOX5);
   2531 			junk = ISP_READ(isp, OUTMAILBOX5);
   2532 		} while (junk != iptr && ++i < 1000);
   2533 
   2534 		if (iptr != junk) {
   2535 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2536 			PRINTF("%s: mailbox5 unsteady (%x, %x)\n",
   2537 			    isp->isp_name, iptr, junk);
   2538 			return (1);
   2539 		}
   2540 	} else {
   2541 		iptr = ISP_READ(isp, OUTMAILBOX5);
   2542 	}
   2543 
   2544 	if (sema) {
   2545 		ISP_WRITE(isp, BIU_SEMA, 0);
   2546 	}
   2547 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2548 
   2549 	if (optr == iptr && sema == 0) {
   2550 		/*
   2551 		 * There are a lot of these- reasons unknown- mostly on
   2552 		 * faster Alpha machines.
   2553 		 *
   2554 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
   2555 		 * make sure the old interrupt went away (to avoid 'ringing'
   2556 		 * effects), but that didn't stop this from occurring.
   2557 		 */
   2558 		junk = ISP_READ(isp, BIU_ISR);
   2559 		IDPRINTF(2, ("%s: null intr- isr %x (%x) iptr %x optr %x\n",
   2560 		    isp->isp_name, isr, junk, iptr, optr));
   2561 	}
   2562 
   2563 	while (optr != iptr) {
   2564 		ispstatusreq_t *sp;
   2565 		u_int16_t oop;
   2566 		int buddaboom = 0;
   2567 
   2568 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   2569 		oop = optr;
   2570 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN);
   2571 		nlooked++;
   2572 		MemoryBarrier();
   2573 		/*
   2574 		 * Do any appropriate unswizzling of what the Qlogic f/w has
   2575 		 * written into memory so it makes sense to us. This is a
   2576 		 * per-platform thing.
   2577 		 */
   2578 		ISP_UNSWIZZLE_RESPONSE(isp, sp);
   2579 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
   2580 			if (isp_handle_other_response(isp, sp, &optr) == 0) {
   2581 				continue;
   2582 			}
   2583 			/*
   2584 			 * It really has to be a bounced request just copied
   2585 			 * from the request queue to the response queue. If
   2586 			 * not, something bad has happened.
   2587 			 */
   2588 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
   2589 				PRINTF("%s: not RESPONSE in RESPONSE Queue "
   2590 				    "(type 0x%x) @ idx %d (next %d)\n",
   2591 				    isp->isp_name,
   2592 				    sp->req_header.rqs_entry_type, oop, optr);
   2593 				continue;
   2594 			}
   2595 			buddaboom = 1;
   2596 		}
   2597 
   2598 		if (sp->req_header.rqs_flags & 0xf) {
   2599 #define	_RQS_OFLAGS	\
   2600 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
   2601 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   2602 				IDPRINTF(4, ("%s: continuation segment\n",
   2603 				    isp->isp_name));
   2604 				ISP_WRITE(isp, INMAILBOX5, optr);
   2605 				continue;
   2606 			}
   2607 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   2608 				IDPRINTF(2, ("%s: internal queues full\n",
   2609 				    isp->isp_name));
   2610 				/*
   2611 				 * We'll synthesize a QUEUE FULL message below.
   2612 				 */
   2613 			}
   2614 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   2615 				PRINTF("%s: bad header\n", isp->isp_name);
   2616 				buddaboom++;
   2617 			}
   2618 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   2619 				PRINTF("%s: bad request packet\n",
   2620 				    isp->isp_name);
   2621 				buddaboom++;
   2622 			}
   2623 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
   2624 				PRINTF("%s: unknown flags in response (0x%x)\n",
   2625 				    isp->isp_name, sp->req_header.rqs_flags);
   2626 				buddaboom++;
   2627 			}
   2628 #undef	_RQS_OFLAGS
   2629 		}
   2630 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
   2631 			PRINTF("%s: bad request handle %d\n", isp->isp_name,
   2632 			    sp->req_handle);
   2633 			ISP_WRITE(isp, INMAILBOX5, optr);
   2634 			continue;
   2635 		}
   2636 		xs = isp_find_xs(isp, sp->req_handle);
   2637 		if (xs == NULL) {
   2638 			PRINTF("%s: NULL xs in xflist (handle 0x%x)\n",
   2639 			    isp->isp_name, sp->req_handle);
   2640 			ISP_WRITE(isp, INMAILBOX5, optr);
   2641 			continue;
   2642 		}
   2643 		isp_destroy_handle(isp, sp->req_handle);
   2644 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   2645 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   2646 		}
   2647 		if (buddaboom) {
   2648 			XS_SETERR(xs, HBA_BOTCH);
   2649 		}
   2650 		XS_STS(xs) = sp->req_scsi_status & 0xff;
   2651 		if (IS_SCSI(isp)) {
   2652 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   2653 				MEMCPY(XS_SNSP(xs), sp->req_sense_data,
   2654 					XS_SNSLEN(xs));
   2655 				XS_SNS_IS_VALID(xs);
   2656 			}
   2657 			/*
   2658 			 * A new synchronous rate was negotiated for this
   2659 			 * target. Mark state such that we'll go look up
   2660 			 * that which has changed later.
   2661 			 */
   2662 			if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   2663 				sdparam *sdp = isp->isp_param;
   2664 				sdp += XS_CHANNEL(xs);
   2665 				sdp->isp_devparam[XS_TGT(xs)].dev_refresh = 1;
   2666 				isp->isp_update |= (1 << XS_CHANNEL(xs));
   2667 			}
   2668 		} else {
   2669 			if (sp->req_scsi_status & RQCS_SV) {
   2670 				int amt = min(XS_SNSLEN(xs), sp->req_sense_len);
   2671 				MEMCPY(XS_SNSP(xs), sp->req_sense_data, amt);
   2672 				XS_SNS_IS_VALID(xs);
   2673 				sp->req_state_flags |= RQSF_GOT_SENSE;
   2674 			} else if (XS_STS(xs) == SCSI_CHECK) {
   2675 				IDPRINTF(2,
   2676 				    ("%s: check condition with no sense data\n",
   2677 				    isp->isp_name));
   2678 			}
   2679 		}
   2680 		if (XS_NOERR(xs) && XS_STS(xs) == SCSI_BUSY) {
   2681 			XS_SETERR(xs, HBA_TGTBSY);
   2682 		}
   2683 
   2684 		if (sp->req_header.rqs_entry_type == RQSTYPE_RESPONSE) {
   2685 			if (XS_NOERR(xs)) {
   2686 			    if (sp->req_completion_status != RQCS_COMPLETE) {
   2687 				isp_parse_status(isp, sp, xs);
   2688 			    } else {
   2689 				XS_SETERR(xs, HBA_NOERROR);
   2690 			    }
   2691 			}
   2692 		} else if (sp->req_header.rqs_entry_type == RQSTYPE_REQUEST) {
   2693 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   2694 				/*
   2695 				 * Force Queue Full status.
   2696 				 */
   2697 				XS_STS(xs) = SCSI_QFULL;
   2698 				XS_SETERR(xs, HBA_NOERROR);
   2699 			} else if (XS_NOERR(xs)) {
   2700 				XS_SETERR(xs, HBA_BOTCH);
   2701 			}
   2702 		} else {
   2703 			PRINTF("%s: unhandled respose queue type 0x%x\n",
   2704 			    isp->isp_name, sp->req_header.rqs_entry_type);
   2705 			if (XS_NOERR(xs)) {
   2706 				XS_SETERR(xs, HBA_BOTCH);
   2707 			}
   2708 		}
   2709 		if (IS_SCSI(isp)) {
   2710 			XS_RESID(xs) = sp->req_resid;
   2711 		} else if (sp->req_scsi_status & RQCS_RU) {
   2712 			XS_RESID(xs) = sp->req_resid;
   2713 			IDPRINTF(4, ("%s: cnt %d rsd %d\n", isp->isp_name,
   2714 				XS_XFRLEN(xs), sp->req_resid));
   2715 		}
   2716 		if (XS_XFRLEN(xs)) {
   2717 			ISP_DMAFREE(isp, xs, sp->req_handle);
   2718 		}
   2719 		if ((isp->isp_dblev >= 5) ||
   2720 		    (isp->isp_dblev > 2 && !XS_NOERR(xs))) {
   2721 			PRINTF("%s(%d.%d): FIN dl%d resid%d STS %x",
   2722 			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
   2723 			    XS_XFRLEN(xs), XS_RESID(xs), XS_STS(xs));
   2724 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   2725 				PRINTF(" Skey: %x", XS_SNSKEY(xs));
   2726 				if (!(XS_IS_SNS_VALID(xs))) {
   2727 					PRINTF(" BUT NOT SET");
   2728 				}
   2729 			}
   2730 			PRINTF(" XS_ERR=0x%x\n", (unsigned int) XS_ERR(xs));
   2731 		}
   2732 
   2733 		if (isp->isp_nactive > 0)
   2734 		    isp->isp_nactive--;
   2735 		complist[ndone++] = xs;	/* defer completion call until later */
   2736 	}
   2737 
   2738 	/*
   2739 	 * If we looked at any commands, then it's valid to find out
   2740 	 * what the outpointer is. It also is a trigger to update the
   2741 	 * ISP's notion of what we've seen so far.
   2742 	 */
   2743 	if (nlooked) {
   2744 		ISP_WRITE(isp, INMAILBOX5, optr);
   2745 		isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
   2746 	}
   2747 
   2748 	isp->isp_residx = optr;
   2749 	for (i = 0; i < ndone; i++) {
   2750 		xs = complist[i];
   2751 		if (xs) {
   2752 			XS_CMD_DONE(xs);
   2753 		}
   2754 	}
   2755 	return (1);
   2756 }
   2757 
   2758 /*
   2759  * Support routines.
   2760  */
   2761 
   2762 static int
   2763 isp_parse_async(isp, mbox)
   2764 	struct ispsoftc *isp;
   2765 	int mbox;
   2766 {
   2767 	int bus;
   2768 	u_int32_t fast_post_handle = 0;
   2769 
   2770 	if (IS_DUALBUS(isp)) {
   2771 		bus = ISP_READ(isp, OUTMAILBOX6);
   2772 	} else {
   2773 		bus = 0;
   2774 	}
   2775 
   2776 	switch (mbox) {
   2777 	case MBOX_COMMAND_COMPLETE:	/* sometimes these show up */
   2778 		break;
   2779 	case ASYNC_BUS_RESET:
   2780 		isp->isp_sendmarker |= (1 << bus);
   2781 #ifdef	ISP_TARGET_MODE
   2782 		isp_target_async(isp, bus, mbox);
   2783 #endif
   2784 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
   2785 		break;
   2786 	case ASYNC_SYSTEM_ERROR:
   2787 		mbox = ISP_READ(isp, OUTMAILBOX1);
   2788 		PRINTF("%s: Internal FW Error @ RISC Addr 0x%x\n",
   2789 		    isp->isp_name, mbox);
   2790 		isp_restart(isp);
   2791 		/* no point continuing after this */
   2792 		return (-1);
   2793 
   2794 	case ASYNC_RQS_XFER_ERR:
   2795 		PRINTF("%s: Request Queue Transfer Error\n", isp->isp_name);
   2796 		break;
   2797 
   2798 	case ASYNC_RSP_XFER_ERR:
   2799 		PRINTF("%s: Response Queue Transfer Error\n", isp->isp_name);
   2800 		break;
   2801 
   2802 	case ASYNC_QWAKEUP:
   2803 		/*
   2804 		 * We've just been notified that the Queue has woken up.
   2805 		 * We don't need to be chatty about this- just unlatch things
   2806 		 * and move on.
   2807 		 */
   2808 		mbox = ISP_READ(isp, OUTMAILBOX4);
   2809 		break;
   2810 
   2811 	case ASYNC_TIMEOUT_RESET:
   2812 		PRINTF("%s: timeout initiated SCSI bus reset of bus %d\n",
   2813 		    isp->isp_name, bus);
   2814 		isp->isp_sendmarker |= (1 << bus);
   2815 #ifdef	ISP_TARGET_MODE
   2816 		isp_target_async(isp, bus, mbox);
   2817 #endif
   2818 		break;
   2819 
   2820 	case ASYNC_DEVICE_RESET:
   2821 		PRINTF("%s: device reset on bus %d\n", isp->isp_name, bus);
   2822 		isp->isp_sendmarker |= (1 << bus);
   2823 #ifdef	ISP_TARGET_MODE
   2824 		isp_target_async(isp, bus, mbox);
   2825 #endif
   2826 		break;
   2827 
   2828 	case ASYNC_EXTMSG_UNDERRUN:
   2829 		PRINTF("%s: extended message underrun\n", isp->isp_name);
   2830 		break;
   2831 
   2832 	case ASYNC_SCAM_INT:
   2833 		PRINTF("%s: SCAM interrupt\n", isp->isp_name);
   2834 		break;
   2835 
   2836 	case ASYNC_HUNG_SCSI:
   2837 		PRINTF("%s: stalled SCSI Bus after DATA Overrun\n",
   2838 		    isp->isp_name);
   2839 		/* XXX: Need to issue SCSI reset at this point */
   2840 		break;
   2841 
   2842 	case ASYNC_KILLED_BUS:
   2843 		PRINTF("%s: SCSI Bus reset after DATA Overrun\n",
   2844 		    isp->isp_name);
   2845 		break;
   2846 
   2847 	case ASYNC_BUS_TRANSIT:
   2848 		mbox = ISP_READ(isp, OUTMAILBOX2);
   2849 		switch (mbox & 0x1c00) {
   2850 		case SXP_PINS_LVD_MODE:
   2851 			PRINTF("%s: Transition to LVD mode\n", isp->isp_name);
   2852 			((sdparam *)isp->isp_param)->isp_diffmode = 0;
   2853 			((sdparam *)isp->isp_param)->isp_ultramode = 0;
   2854 			((sdparam *)isp->isp_param)->isp_lvdmode = 1;
   2855 			break;
   2856 		case SXP_PINS_HVD_MODE:
   2857 			PRINTF("%s: Transition to Differential mode\n",
   2858 			    isp->isp_name);
   2859 			((sdparam *)isp->isp_param)->isp_diffmode = 1;
   2860 			((sdparam *)isp->isp_param)->isp_ultramode = 0;
   2861 			((sdparam *)isp->isp_param)->isp_lvdmode = 0;
   2862 			break;
   2863 		case SXP_PINS_SE_MODE:
   2864 			PRINTF("%s: Transition to Single Ended mode\n",
   2865 			    isp->isp_name);
   2866 			((sdparam *)isp->isp_param)->isp_diffmode = 0;
   2867 			((sdparam *)isp->isp_param)->isp_ultramode = 1;
   2868 			((sdparam *)isp->isp_param)->isp_lvdmode = 0;
   2869 			break;
   2870 		default:
   2871 			PRINTF("%s: Transition to unknown mode 0x%x\n",
   2872 			    isp->isp_name, mbox);
   2873 			break;
   2874 		}
   2875 		/*
   2876 		 * XXX: Set up to renegotiate again!
   2877 		 */
   2878 		/* Can only be for a 1080... */
   2879 		isp->isp_sendmarker |= (1 << bus);
   2880 		break;
   2881 
   2882 	case ASYNC_CMD_CMPLT:
   2883 		fast_post_handle = (ISP_READ(isp, OUTMAILBOX2) << 16) |
   2884 		    ISP_READ(isp, OUTMAILBOX1);
   2885 		IDPRINTF(4, ("%s: fast post completion of %u\n", isp->isp_name,
   2886 		    fast_post_handle));
   2887 		break;
   2888 
   2889 	case ASYNC_CTIO_DONE:
   2890 		/* Should only occur when Fast Posting Set for 2100s */
   2891 		PRINTF("%s: CTIO done\n", isp->isp_name);
   2892 		break;
   2893 
   2894 	case ASYNC_LIP_OCCURRED:
   2895 		((fcparam *) isp->isp_param)->isp_lipseq =
   2896 		    ISP_READ(isp, OUTMAILBOX1);
   2897 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
   2898 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_LIP_RCVD;
   2899 		isp->isp_sendmarker = 1;
   2900 		isp_mark_getpdb_all(isp);
   2901 		IDPRINTF(1, ("%s: LIP occurred\n", isp->isp_name));
   2902 #ifdef	ISP_TARGET_MODE
   2903 		isp_target_async(isp, bus, mbox);
   2904 #endif
   2905 		break;
   2906 
   2907 	case ASYNC_LOOP_UP:
   2908 		isp->isp_sendmarker = 1;
   2909 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
   2910 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_LIP_RCVD;
   2911 		isp_mark_getpdb_all(isp);
   2912 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
   2913 #ifdef	ISP_TARGET_MODE
   2914 		isp_target_async(isp, bus, mbox);
   2915 #endif
   2916 		break;
   2917 
   2918 	case ASYNC_LOOP_DOWN:
   2919 		isp->isp_sendmarker = 1;
   2920 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
   2921 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_NIL;
   2922 		isp_mark_getpdb_all(isp);
   2923 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
   2924 #ifdef	ISP_TARGET_MODE
   2925 		isp_target_async(isp, bus, mbox);
   2926 #endif
   2927 		break;
   2928 
   2929 	case ASYNC_LOOP_RESET:
   2930 		isp->isp_sendmarker = 1;
   2931 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
   2932 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_NIL;
   2933 		isp_mark_getpdb_all(isp);
   2934 		PRINTF("%s: Loop RESET\n", isp->isp_name);
   2935 #ifdef	ISP_TARGET_MODE
   2936 		isp_target_async(isp, bus, mbox);
   2937 #endif
   2938 		break;
   2939 
   2940 	case ASYNC_PDB_CHANGED:
   2941 		isp->isp_sendmarker = 1;
   2942 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_PDB_RCVD;
   2943 		isp_mark_getpdb_all(isp);
   2944 		IDPRINTF(1, ("%s: Port Database Changed\n", isp->isp_name));
   2945 		break;
   2946 
   2947 	case ASYNC_CHANGE_NOTIFY:
   2948 		isp_mark_getpdb_all(isp);
   2949 		/*
   2950 		 * Not correct, but it will force us to rescan the loop.
   2951 		 */
   2952 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_PDB_RCVD;
   2953 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, NULL);
   2954 		break;
   2955 
   2956 	case ASYNC_PTPMODE:
   2957 		if (((fcparam *) isp->isp_param)->isp_onfabric)
   2958 			((fcparam *) isp->isp_param)->isp_topo = TOPO_N_PORT;
   2959 		else
   2960 			((fcparam *) isp->isp_param)->isp_topo = TOPO_F_PORT;
   2961 		isp_mark_getpdb_all(isp);
   2962 		isp->isp_sendmarker = 1;
   2963 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
   2964 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_LIP_RCVD;
   2965 #ifdef	ISP_TARGET_MODE
   2966 		isp_target_async(isp, bus, mbox);
   2967 #endif
   2968 		PRINTF("%s: Point-to-Point mode\n", isp->isp_name);
   2969 		break;
   2970 
   2971 	case ASYNC_CONNMODE:
   2972 		mbox = ISP_READ(isp, OUTMAILBOX1);
   2973 		switch (mbox) {
   2974 		case ISP_CONN_LOOP:
   2975 			PRINTF("%s: Point-to-Point -> Loop mode\n",
   2976 			    isp->isp_name);
   2977 			break;
   2978 		case ISP_CONN_PTP:
   2979 			PRINTF("%s: Loop -> Point-to-Point mode\n",
   2980 			    isp->isp_name);
   2981 			break;
   2982 		case ISP_CONN_BADLIP:
   2983 			PRINTF("%s: Point-to-Point -> Loop mode (1)\n",
   2984 			    isp->isp_name);
   2985 			break;
   2986 		case ISP_CONN_FATAL:
   2987 			PRINTF("%s: FATAL CONNECTION ERROR\n", isp->isp_name);
   2988 			isp_restart(isp);
   2989 			/* no point continuing after this */
   2990 			return (-1);
   2991 
   2992 		case ISP_CONN_LOOPBACK:
   2993 			PRINTF("%s: Looped Back in Point-to-Point mode\n",
   2994 			     isp->isp_name);
   2995 		}
   2996 		break;
   2997 
   2998 	default:
   2999 		PRINTF("%s: unknown async code 0x%x\n", isp->isp_name, mbox);
   3000 		break;
   3001 	}
   3002 	return (fast_post_handle);
   3003 }
   3004 
   3005 /*
   3006  * Handle other response entries. A pointer to the request queue output
   3007  * index is here in case we want to eat several entries at once, although
   3008  * this is not used currently.
   3009  */
   3010 
   3011 static int
   3012 isp_handle_other_response(isp, sp, optrp)
   3013 	struct ispsoftc *isp;
   3014 	ispstatusreq_t *sp;
   3015 	u_int16_t *optrp;
   3016 {
   3017 	switch (sp->req_header.rqs_entry_type) {
   3018 	case RQSTYPE_ATIO:
   3019 	case RQSTYPE_CTIO:
   3020 	case RQSTYPE_ENABLE_LUN:
   3021 	case RQSTYPE_MODIFY_LUN:
   3022 	case RQSTYPE_NOTIFY:
   3023 	case RQSTYPE_NOTIFY_ACK:
   3024 	case RQSTYPE_CTIO1:
   3025 	case RQSTYPE_ATIO2:
   3026 	case RQSTYPE_CTIO2:
   3027 	case RQSTYPE_CTIO3:
   3028 #ifdef	ISP_TARGET_MODE
   3029 		return (isp_target_notify(isp, sp, optrp));
   3030 #else
   3031 		/* FALLTHROUGH */
   3032 #endif
   3033 	case RQSTYPE_REQUEST:
   3034 	default:
   3035 		PRINTF("%s: unhandled response type 0x%x\n", isp->isp_name,
   3036 		    sp->req_header.rqs_entry_type);
   3037 		return (-1);
   3038 	}
   3039 }
   3040 
   3041 static void
   3042 isp_parse_status(isp, sp, xs)
   3043 	struct ispsoftc *isp;
   3044 	ispstatusreq_t *sp;
   3045 	ISP_SCSI_XFER_T *xs;
   3046 {
   3047 	switch (sp->req_completion_status) {
   3048 	case RQCS_COMPLETE:
   3049 		XS_SETERR(xs, HBA_NOERROR);
   3050 		return;
   3051 
   3052 	case RQCS_INCOMPLETE:
   3053 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   3054 			IDPRINTF(3, ("%s: Selection Timeout for %d.%d.%d\n",
   3055 			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
   3056 			    XS_CHANNEL(xs)));
   3057 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3058 			return;
   3059 		}
   3060 		PRINTF("%s: command incomplete for %d.%d.%d, state 0x%x\n",
   3061 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
   3062 		    sp->req_state_flags);
   3063 		break;
   3064 
   3065 	case RQCS_DMA_ERROR:
   3066 		PRINTF("%s: DMA error for command on %d.%d.%d\n",
   3067 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3068 		break;
   3069 
   3070 	case RQCS_TRANSPORT_ERROR:
   3071 		PRINTF("%s: transport error for %d.%d.%d\n",
   3072 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3073 		isp_prtstst(sp);
   3074 		break;
   3075 
   3076 	case RQCS_RESET_OCCURRED:
   3077 		IDPRINTF(1, ("%s: bus reset destroyed command for %d.%d.%d\n",
   3078 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs)));
   3079 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3080 		XS_SETERR(xs, HBA_BUSRESET);
   3081 		return;
   3082 
   3083 	case RQCS_ABORTED:
   3084 		PRINTF("%s: command aborted for %d.%d.%d\n",
   3085 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3086 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3087 		XS_SETERR(xs, HBA_ABORTED);
   3088 		return;
   3089 
   3090 	case RQCS_TIMEOUT:
   3091 		IDPRINTF(2, ("%s: command timed out for %d.%d.%d\n",
   3092 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs)));
   3093 		XS_SETERR(xs, HBA_CMDTIMEOUT);
   3094 		return;
   3095 
   3096 	case RQCS_DATA_OVERRUN:
   3097 		if (IS_FC(isp)) {
   3098 			XS_RESID(xs) = sp->req_resid;
   3099 			break;
   3100 		}
   3101 		PRINTF("%s: data overrun for command on %d.%d.%d\n",
   3102 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3103 		XS_SETERR(xs, HBA_DATAOVR);
   3104 		return;
   3105 
   3106 	case RQCS_COMMAND_OVERRUN:
   3107 		PRINTF("%s: command overrun for command on %d.%d.%d\n",
   3108 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3109 		break;
   3110 
   3111 	case RQCS_STATUS_OVERRUN:
   3112 		PRINTF("%s: status overrun for command on %d.%d.%d\n",
   3113 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3114 		break;
   3115 
   3116 	case RQCS_BAD_MESSAGE:
   3117 		PRINTF("%s: msg not COMMAND COMPLETE after status %d.%d.%d\n",
   3118 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3119 		break;
   3120 
   3121 	case RQCS_NO_MESSAGE_OUT:
   3122 		PRINTF("%s: No MESSAGE OUT phase after selection on %d.%d.%d\n",
   3123 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3124 		break;
   3125 
   3126 	case RQCS_EXT_ID_FAILED:
   3127 		PRINTF("%s: EXTENDED IDENTIFY failed %d.%d.%d\n",
   3128 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3129 		break;
   3130 
   3131 	case RQCS_IDE_MSG_FAILED:
   3132 		PRINTF("%s: INITIATOR DETECTED ERROR rejected by %d.%d.%d\n",
   3133 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3134 		break;
   3135 
   3136 	case RQCS_ABORT_MSG_FAILED:
   3137 		PRINTF("%s: ABORT OPERATION rejected by %d.%d.%d\n",
   3138 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3139 		break;
   3140 
   3141 	case RQCS_REJECT_MSG_FAILED:
   3142 		PRINTF("%s: MESSAGE REJECT rejected by %d.%d.%d\n",
   3143 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3144 		break;
   3145 
   3146 	case RQCS_NOP_MSG_FAILED:
   3147 		PRINTF("%s: NOP rejected by %d.%d.%d\n",
   3148 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3149 		break;
   3150 
   3151 	case RQCS_PARITY_ERROR_MSG_FAILED:
   3152 		PRINTF("%s: MESSAGE PARITY ERROR rejected by %d.%d.%d\n",
   3153 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3154 		break;
   3155 
   3156 	case RQCS_DEVICE_RESET_MSG_FAILED:
   3157 		PRINTF("%s: BUS DEVICE RESET rejected by %d.%d.%d\n",
   3158 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3159 		break;
   3160 
   3161 	case RQCS_ID_MSG_FAILED:
   3162 		PRINTF("%s: IDENTIFY rejected by %d.%d.%d\n",
   3163 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3164 		break;
   3165 
   3166 	case RQCS_UNEXP_BUS_FREE:
   3167 		PRINTF("%s: %d.%d.%d had an unexpected bus free\n",
   3168 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3169 		break;
   3170 
   3171 	case RQCS_DATA_UNDERRUN:
   3172 		if (IS_FC(isp)) {
   3173 			XS_RESID(xs) = sp->req_resid;
   3174 		}
   3175 		XS_SETERR(xs, HBA_NOERROR);
   3176 		return;
   3177 
   3178 	case RQCS_XACT_ERR1:
   3179 		PRINTF("%s: HBA attempted queued transaction with disconnect "
   3180 		    "not set for %d.%d.%d\n", isp->isp_name, XS_CHANNEL(xs),
   3181 		    XS_TGT(xs), XS_LUN(xs));
   3182 		break;
   3183 
   3184 	case RQCS_XACT_ERR2:
   3185 		PRINTF("%s: HBA attempted queued transaction to target "
   3186 		    "routine %d on target %d, bus %d\n", isp->isp_name,
   3187 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
   3188 		break;
   3189 
   3190 	case RQCS_XACT_ERR3:
   3191 		PRINTF("%s: HBA attempted queued transaction for target %d lun "
   3192 		    "%d on bus %d when queueing disabled\n", isp->isp_name,
   3193 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3194 		break;
   3195 
   3196 	case RQCS_BAD_ENTRY:
   3197 		PRINTF("%s: invalid IOCB entry type detected\n", isp->isp_name);
   3198 		break;
   3199 
   3200 	case RQCS_QUEUE_FULL:
   3201 		IDPRINTF(3, ("%s: internal queues full for target %d lun %d "
   3202 		    "bus %d, status 0x%x\n", isp->isp_name, XS_TGT(xs),
   3203 		    XS_LUN(xs), XS_CHANNEL(xs), XS_STS(xs)));
   3204 		/*
   3205 		 * If QFULL or some other status byte is set, then this
   3206 		 * isn't an error, per se.
   3207 		 */
   3208 		if (XS_STS(xs) != 0) {
   3209 			XS_SETERR(xs, HBA_NOERROR);
   3210 			return;
   3211 		}
   3212 		break;
   3213 
   3214 	case RQCS_PHASE_SKIPPED:
   3215 		PRINTF("%s: SCSI phase skipped (e.g., COMMAND COMPLETE w/o "
   3216 		    "STATUS phase) for target %d lun %d bus %d\n",
   3217 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3218 		break;
   3219 
   3220 	case RQCS_ARQS_FAILED:
   3221 		PRINTF("%s: Auto Request Sense failed for %d.%d.%d\n",
   3222 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3223 		return;
   3224 
   3225 	case RQCS_WIDE_FAILED:
   3226 		PRINTF("%s: Wide Negotiation failed for %d.%d.%d\n",
   3227 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3228 		if (IS_SCSI(isp)) {
   3229 			sdparam *sdp = isp->isp_param;
   3230 			sdp += XS_CHANNEL(xs);
   3231 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
   3232 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3233 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3234 		}
   3235 		XS_SETERR(xs, HBA_NOERROR);
   3236 		return;
   3237 
   3238 	case RQCS_SYNCXFER_FAILED:
   3239 		PRINTF("%s: SDTR Message failed for target %d.%d.%d\n",
   3240 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3241 		if (IS_SCSI(isp)) {
   3242 			sdparam *sdp = isp->isp_param;
   3243 			sdp += XS_CHANNEL(xs);
   3244 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
   3245 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3246 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3247 		}
   3248 		break;
   3249 
   3250 	case RQCS_LVD_BUSERR:
   3251 		PRINTF("%s: Bad LVD condition while talking to %d.%d.%d\n",
   3252 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3253 		break;
   3254 
   3255 	case RQCS_PORT_UNAVAILABLE:
   3256 		/*
   3257 		 * No such port on the loop. Moral equivalent of SELTIMEO
   3258 		 */
   3259 		IDPRINTF(3, ("%s: Port Unavailable for target %d\n",
   3260 		    isp->isp_name, XS_TGT(xs)));
   3261 		XS_SETERR(xs, HBA_SELTIMEOUT);
   3262 		return;
   3263 
   3264 	case RQCS_PORT_LOGGED_OUT:
   3265 		/*
   3266 		 * It was there (maybe)- treat as a selection timeout.
   3267 		 */
   3268 		IDPRINTF(2, ("%s: port logout for target %d\n",
   3269 			isp->isp_name, XS_TGT(xs)));
   3270 		XS_SETERR(xs, HBA_SELTIMEOUT);
   3271 		return;
   3272 
   3273 	case RQCS_PORT_CHANGED:
   3274 		PRINTF("%s: port changed for target %d\n",
   3275 		    isp->isp_name, XS_TGT(xs));
   3276 		XS_SETERR(xs, HBA_SELTIMEOUT);
   3277 		return;
   3278 
   3279 	case RQCS_PORT_BUSY:
   3280 		PRINTF("%s: port busy for target %d\n",
   3281 		    isp->isp_name, XS_TGT(xs));
   3282 		XS_SETERR(xs, HBA_TGTBSY);
   3283 		return;
   3284 
   3285 	default:
   3286 		PRINTF("%s: completion status 0x%x\n",
   3287 		    isp->isp_name, sp->req_completion_status);
   3288 		break;
   3289 	}
   3290 	XS_SETERR(xs, HBA_BOTCH);
   3291 }
   3292 
   3293 static void
   3294 isp_fastpost_complete(isp, fph)
   3295 	struct ispsoftc *isp;
   3296 	u_int32_t fph;
   3297 {
   3298 	ISP_SCSI_XFER_T *xs;
   3299 
   3300 	if (fph < 1) {
   3301 		return;
   3302 	}
   3303 	xs = isp_find_xs(isp, fph);
   3304 	if (xs == NULL) {
   3305 		PRINTF("%s: command for fast posting handle 0x%x not found\n",
   3306 		    isp->isp_name, fph);
   3307 		return;
   3308 	}
   3309 	isp_destroy_handle(isp, fph);
   3310 
   3311 	/*
   3312 	 * Since we don't have a result queue entry item,
   3313 	 * we must believe that SCSI status is zero and
   3314 	 * that all data transferred.
   3315 	 */
   3316 	XS_RESID(xs) = 0;
   3317 	XS_STS(xs) = 0;
   3318 	if (XS_XFRLEN(xs)) {
   3319 		ISP_DMAFREE(isp, xs, fph);
   3320 	}
   3321 	XS_CMD_DONE(xs);
   3322 	if (isp->isp_nactive)
   3323 		isp->isp_nactive--;
   3324 }
   3325 
   3326 #define	HIBYT(x)			((x) >> 0x8)
   3327 #define	LOBYT(x)			((x)  & 0xff)
   3328 #define	ISPOPMAP(a, b)			(((a) << 8) | (b))
   3329 static u_int16_t mbpscsi[] = {
   3330 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   3331 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   3332 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   3333 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   3334 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   3335 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   3336 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   3337 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   3338 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   3339 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
   3340 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
   3341 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   3342 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   3343 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   3344 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   3345 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   3346 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   3347 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
   3348 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
   3349 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   3350 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
   3351 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
   3352 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
   3353 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
   3354 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
   3355 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
   3356 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
   3357 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   3358 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
   3359 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   3360 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   3361 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   3362 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
   3363 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   3364 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   3365 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   3366 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
   3367 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   3368 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   3369 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
   3370 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   3371 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   3372 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
   3373 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   3374 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   3375 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   3376 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   3377 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   3378 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   3379 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   3380 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   3381 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   3382 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
   3383 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
   3384 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   3385 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   3386 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   3387 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   3388 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
   3389 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   3390 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   3391 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   3392 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   3393 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   3394 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   3395 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   3396 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   3397 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
   3398 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   3399 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
   3400 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
   3401 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
   3402 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
   3403 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
   3404 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
   3405 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
   3406 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   3407 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   3408 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   3409 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   3410 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
   3411 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
   3412 	ISPOPMAP(0xdf, 0xdf),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
   3413 	ISPOPMAP(0xff, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
   3414 	ISPOPMAP(0xcf, 0xff),	/* 0x54: EXECUTE IOCB A64 */
   3415 	ISPOPMAP(0x03, 0x01),	/* 0x55: ENABLE TARGET MODE */
   3416 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   3417 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   3418 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   3419 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   3420 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
   3421 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
   3422 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
   3423 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
   3424 };
   3425 
   3426 static u_int16_t mbpfc[] = {
   3427 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   3428 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   3429 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   3430 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   3431 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   3432 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   3433 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   3434 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   3435 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   3436 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
   3437 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
   3438 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   3439 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   3440 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   3441 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   3442 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   3443 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   3444 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
   3445 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
   3446 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   3447 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
   3448 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
   3449 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
   3450 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
   3451 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
   3452 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
   3453 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
   3454 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   3455 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
   3456 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   3457 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   3458 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   3459 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
   3460 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
   3461 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   3462 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
   3463 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
   3464 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
   3465 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
   3466 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
   3467 	ISPOPMAP(0x0f, 0x1),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
   3468 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
   3469 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
   3470 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   3471 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   3472 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   3473 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   3474 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   3475 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
   3476 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
   3477 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   3478 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
   3479 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
   3480 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
   3481 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
   3482 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
   3483 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
   3484 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
   3485 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
   3486 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   3487 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   3488 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   3489 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   3490 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   3491 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
   3492 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
   3493 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
   3494 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
   3495 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   3496 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
   3497 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
   3498 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
   3499 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
   3500 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
   3501 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
   3502 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
   3503 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   3504 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   3505 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   3506 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   3507 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
   3508 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
   3509 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
   3510 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
   3511 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   3512 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
   3513 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   3514 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   3515 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   3516 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   3517 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
   3518 	ISPOPMAP(0x00, 0x00),	/* 0x5b: */
   3519 	ISPOPMAP(0x00, 0x00),	/* 0x5c: */
   3520 	ISPOPMAP(0x00, 0x00),	/* 0x5d: */
   3521 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
   3522 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
   3523 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
   3524 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
   3525 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
   3526 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   3527 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
   3528 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
   3529 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
   3530 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
   3531 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
   3532 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
   3533 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
   3534 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
   3535 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
   3536 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
   3537 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
   3538 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
   3539 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
   3540 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
   3541 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
   3542 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
   3543 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
   3544 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
   3545 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
   3546 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
   3547 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
   3548 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
   3549 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
   3550 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
   3551 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
   3552 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
   3553 	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
   3554 };
   3555 
   3556 static void
   3557 isp_mboxcmd(isp, mbp)
   3558 	struct ispsoftc *isp;
   3559 	mbreg_t *mbp;
   3560 {
   3561 	unsigned int lim, ibits, obits, box, opcode;
   3562 	u_int16_t *mcp;
   3563 
   3564 	if (IS_FC(isp)) {
   3565 		mcp = mbpfc;
   3566 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   3567 	} else {
   3568 		mcp = mbpscsi;
   3569 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   3570 	}
   3571 
   3572 	if ((opcode = mbp->param[0]) >= lim) {
   3573 		mbp->param[0] = MBOX_INVALID_COMMAND;
   3574 		PRINTF("%s: unknown command 0x%x\n", isp->isp_name, opcode);
   3575 		return;
   3576 	}
   3577 
   3578 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   3579 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   3580 
   3581 	if (ibits == 0 && obits == 0) {
   3582 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
   3583 		PRINTF("%s: no parameters for opcode 0x%x\n", isp->isp_name,
   3584 			opcode);
   3585 		return;
   3586 	}
   3587 
   3588 	for (box = 0; box < MAX_MAILBOX; box++) {
   3589 		if (ibits & (1 << box)) {
   3590 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   3591 		}
   3592 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   3593 	}
   3594 
   3595 	/*
   3596 	 * We assume that we can't overwrite a previous command.
   3597 	 */
   3598 	isp->isp_mboxbsy = obits;
   3599 
   3600 	/*
   3601 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   3602 	 */
   3603 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   3604 
   3605 	/*
   3606 	 * Give the f/w a chance to pick this up.
   3607 	 */
   3608 	SYS_DELAY(250);
   3609 
   3610 
   3611 	/*
   3612 	 * While we haven't finished the command, spin our wheels here.
   3613 	 */
   3614 	MBOX_WAIT_COMPLETE(isp);
   3615 
   3616 	/*
   3617 	 * Copy back output registers.
   3618 	 */
   3619 	for (box = 0; box < MAX_MAILBOX; box++) {
   3620 		if (obits & (1 << box)) {
   3621 			mbp->param[box] = isp->isp_mboxtmp[box];
   3622 		}
   3623 	}
   3624 
   3625 	/*
   3626 	 * Just to be chatty here...
   3627 	 */
   3628 	switch (mbp->param[0]) {
   3629 	case MBOX_COMMAND_COMPLETE:
   3630 		break;
   3631 	case MBOX_INVALID_COMMAND:
   3632 		IDPRINTF(2, ("%s: mbox cmd %x failed with INVALID_COMMAND\n",
   3633 		    isp->isp_name, opcode));
   3634 		break;
   3635 	case MBOX_HOST_INTERFACE_ERROR:
   3636 		PRINTF("%s: mbox cmd %x failed with HOST_INTERFACE_ERROR\n",
   3637 		    isp->isp_name, opcode);
   3638 		break;
   3639 	case MBOX_TEST_FAILED:
   3640 		PRINTF("%s: mbox cmd %x failed with TEST_FAILED\n",
   3641 		    isp->isp_name, opcode);
   3642 		break;
   3643 	case MBOX_COMMAND_ERROR:
   3644 		if (opcode != MBOX_ABOUT_FIRMWARE)
   3645 		    PRINTF("%s: mbox cmd %x failed with COMMAND_ERROR\n",
   3646 			isp->isp_name, opcode);
   3647 		break;
   3648 	case MBOX_COMMAND_PARAM_ERROR:
   3649 		switch (opcode) {
   3650 		case MBOX_GET_PORT_DB:
   3651 		case MBOX_GET_PORT_NAME:
   3652 		case MBOX_GET_DEV_QUEUE_PARAMS:
   3653 			break;
   3654 		default:
   3655 			PRINTF("%s: mbox cmd %x failed with "
   3656 			    "COMMAND_PARAM_ERROR\n", isp->isp_name, opcode);
   3657 		}
   3658 		break;
   3659 
   3660 	case MBOX_LOOP_ID_USED:
   3661 	case MBOX_PORT_ID_USED:
   3662 	case MBOX_ALL_IDS_USED:
   3663 		break;
   3664 
   3665 	default:
   3666 		/*
   3667 		 * The expected return of EXEC_FIRMWARE is zero.
   3668 		 */
   3669 		if ((opcode == MBOX_EXEC_FIRMWARE && mbp->param[0] != 0) ||
   3670 		    (opcode != MBOX_EXEC_FIRMWARE)) {
   3671 			PRINTF("%s: mbox cmd %x failed with error %x\n",
   3672 				isp->isp_name, opcode, mbp->param[0]);
   3673 		}
   3674 		break;
   3675 	}
   3676 }
   3677 
   3678 void
   3679 isp_lostcmd(isp, xs)
   3680 	struct ispsoftc *isp;
   3681 	ISP_SCSI_XFER_T *xs;
   3682 {
   3683 	mbreg_t mbs;
   3684 
   3685 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
   3686 	isp_mboxcmd(isp, &mbs);
   3687 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3688 		isp_dumpregs(isp, "couldn't GET FIRMWARE STATUS");
   3689 		return;
   3690 	}
   3691 	if (mbs.param[1]) {
   3692 		PRINTF("%s: %d commands on completion queue\n",
   3693 		    isp->isp_name, mbs.param[1]);
   3694 	}
   3695 	if (XS_NULL(xs))
   3696 		return;
   3697 
   3698 	mbs.param[0] = MBOX_GET_DEV_QUEUE_STATUS;
   3699 	mbs.param[1] = (XS_TGT(xs) << 8) | XS_LUN(xs); /* XXX: WHICH BUS? */
   3700 	isp_mboxcmd(isp, &mbs);
   3701 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3702 		isp_dumpregs(isp, "couldn't GET DEVICE QUEUE STATUS");
   3703 		return;
   3704 	}
   3705 	PRINTF("%s: lost command for target %d lun %d, %d active of %d, "
   3706 		"Queue State: %x\n", isp->isp_name, XS_TGT(xs),
   3707 		XS_LUN(xs), mbs.param[2], mbs.param[3], mbs.param[1]);
   3708 
   3709 	isp_dumpregs(isp, "lost command");
   3710 	/*
   3711 	 * XXX: Need to try and do something to recover.
   3712 	 */
   3713 }
   3714 
   3715 static void
   3716 isp_dumpregs(isp, msg)
   3717 	struct ispsoftc *isp;
   3718 	const char *msg;
   3719 {
   3720 	PRINTF("%s: %s\n", isp->isp_name, msg);
   3721 	if (IS_SCSI(isp))
   3722 		PRINTF("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
   3723 	else
   3724 		PRINTF("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
   3725 	PRINTF(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
   3726 	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
   3727 	PRINTF("risc_hccr=%x\n", ISP_READ(isp, HCCR));
   3728 
   3729 
   3730 	if (IS_SCSI(isp)) {
   3731 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   3732 		PRINTF("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
   3733 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
   3734 			ISP_READ(isp, CDMA_FIFO_STS));
   3735 		PRINTF("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
   3736 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
   3737 			ISP_READ(isp, DDMA_FIFO_STS));
   3738 		PRINTF("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
   3739 			ISP_READ(isp, SXP_INTERRUPT),
   3740 			ISP_READ(isp, SXP_GROSS_ERR),
   3741 			ISP_READ(isp, SXP_PINS_CTRL));
   3742 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
   3743 	}
   3744 	PRINTF("    mbox regs: %x %x %x %x %x\n",
   3745 	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
   3746 	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
   3747 	    ISP_READ(isp, OUTMAILBOX4));
   3748 	ISP_DUMPREGS(isp);
   3749 }
   3750 
   3751 static void
   3752 isp_fw_state(isp)
   3753 	struct ispsoftc *isp;
   3754 {
   3755 	mbreg_t mbs;
   3756 	if (IS_FC(isp)) {
   3757 		int once = 0;
   3758 		fcparam *fcp = isp->isp_param;
   3759 again:
   3760 		mbs.param[0] = MBOX_GET_FW_STATE;
   3761 		isp_mboxcmd(isp, &mbs);
   3762 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3763 			IDPRINTF(1, ("%s: isp_fw_state 0x%x\n", isp->isp_name,
   3764 			    mbs.param[0]));
   3765 			switch (mbs.param[0]) {
   3766 			case ASYNC_PDB_CHANGED:
   3767 				if (once++ < 10) {
   3768 					goto again;
   3769 				}
   3770 				fcp->isp_fwstate = FW_CONFIG_WAIT;
   3771 				fcp->isp_loopstate = LOOP_PDB_RCVD;
   3772 				goto again;
   3773 			case ASYNC_LIP_OCCURRED:
   3774 				fcp->isp_lipseq = mbs.param[1];
   3775 				/* FALLTHROUGH */
   3776 			case ASYNC_LOOP_UP:
   3777 				fcp->isp_fwstate = FW_CONFIG_WAIT;
   3778 				fcp->isp_loopstate = LOOP_LIP_RCVD;
   3779 				if (once++ < 10) {
   3780 					goto again;
   3781 				}
   3782 				break;
   3783 			case ASYNC_LOOP_RESET:
   3784 			case ASYNC_LOOP_DOWN:
   3785 				fcp->isp_fwstate = FW_CONFIG_WAIT;
   3786 				fcp->isp_loopstate = LOOP_NIL;
   3787 				/* FALLTHROUGH */
   3788 			case ASYNC_CHANGE_NOTIFY:
   3789 				if (once++ < 10) {
   3790 					goto again;
   3791 				}
   3792 				break;
   3793 			}
   3794 			PRINTF("%s: GET FIRMWARE STATE failed (0x%x)\n",
   3795 			    isp->isp_name, mbs.param[0]);
   3796 			return;
   3797 		}
   3798 		fcp->isp_fwstate = mbs.param[1];
   3799 	}
   3800 }
   3801 
   3802 static void
   3803 isp_update(isp)
   3804 	struct ispsoftc *isp;
   3805 {
   3806 	int bus;
   3807 
   3808 	for (bus = 0; isp->isp_update != 0; bus++) {
   3809 		if (isp->isp_update & (1 << bus)) {
   3810 			isp_update_bus(isp, bus);
   3811 			isp->isp_update ^= (1 << bus);
   3812 		}
   3813 	}
   3814 }
   3815 
   3816 static void
   3817 isp_update_bus(isp, bus)
   3818 	struct ispsoftc *isp;
   3819 	int bus;
   3820 {
   3821 	int tgt;
   3822 	mbreg_t mbs;
   3823 	sdparam *sdp;
   3824 
   3825 	if (IS_FC(isp)) {
   3826 		return;
   3827 	}
   3828 
   3829 	sdp = isp->isp_param;
   3830 	sdp += bus;
   3831 
   3832 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   3833 		u_int16_t flags, period, offset;
   3834 		int get;
   3835 
   3836 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   3837 			IDPRINTF(2, ("%s: skipping target %d bus %d update\n",
   3838 			    isp->isp_name, tgt, bus));
   3839 			continue;
   3840 		}
   3841 
   3842 		/*
   3843 		 * If the goal is to update the status of the device,
   3844 		 * take what's in dev_flags and try and set the device
   3845 		 * toward that. Otherwise, if we're just refreshing the
   3846 		 * current device state, get the current parameters.
   3847 		 */
   3848 		if (sdp->isp_devparam[tgt].dev_update) {
   3849 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   3850 			mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
   3851 			/*
   3852 			 * Insist that PARITY must be enabled if SYNC
   3853 			 * is enabled.
   3854 			 */
   3855 			if (mbs.param[2] & DPARM_SYNC) {
   3856 				mbs.param[2] |= DPARM_PARITY;
   3857 			}
   3858 			mbs.param[3] =
   3859 				(sdp->isp_devparam[tgt].sync_offset << 8) |
   3860 				(sdp->isp_devparam[tgt].sync_period);
   3861 			sdp->isp_devparam[tgt].dev_update = 0;
   3862 			/*
   3863 			 * A command completion later that has
   3864 			 * RQSTF_NEGOTIATION set will cause
   3865 			 * the dev_refresh/announce cycle.
   3866 			 *
   3867 			 * Note: It is really important to update our current
   3868 			 * flags with at least the state of TAG capabilities-
   3869 			 * otherwise we might try and send a tagged command
   3870 			 * when we have it all turned off. So change it here
   3871 			 * to say that current already matches goal.
   3872 			 */
   3873 			sdp->isp_devparam[tgt].cur_dflags &= ~DPARM_TQING;
   3874 			sdp->isp_devparam[tgt].cur_dflags |=
   3875 			    (sdp->isp_devparam[tgt].dev_flags & DPARM_TQING);
   3876 			sdp->isp_devparam[tgt].dev_refresh = 1;
   3877 			IDPRINTF(3, ("%s: bus %d set tgt %d flags 0x%x off 0x%x"
   3878 			    " period 0x%x\n", isp->isp_name, bus, tgt,
   3879 			    mbs.param[2], mbs.param[3] >> 8,
   3880 			    mbs.param[3] & 0xff));
   3881 			get = 0;
   3882 		} else if (sdp->isp_devparam[tgt].dev_refresh) {
   3883 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   3884 			sdp->isp_devparam[tgt].dev_refresh = 0;
   3885 			get = 1;
   3886 		} else {
   3887 			continue;
   3888 		}
   3889 		mbs.param[1] = (bus << 15) | (tgt << 8) ;
   3890 		isp_mboxcmd(isp, &mbs);
   3891 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3892 			PRINTF("%s: failed to %cet SCSI parameters for "
   3893 			    "target %d\n", isp->isp_name, (get)? 'g' : 's',
   3894 			    tgt);
   3895 			continue;
   3896 		}
   3897 		if (get == 0) {
   3898 			isp->isp_sendmarker |= (1 << bus);
   3899 			continue;
   3900 		}
   3901 		flags = mbs.param[2];
   3902 		period = mbs.param[3] & 0xff;
   3903 		offset = mbs.param[3] >> 8;
   3904 		sdp->isp_devparam[tgt].cur_dflags = flags;
   3905 		sdp->isp_devparam[tgt].cur_period = period;
   3906 		sdp->isp_devparam[tgt].cur_offset = offset;
   3907 		get = (bus << 16) | tgt;
   3908 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
   3909 	}
   3910 }
   3911 
   3912 static void
   3913 isp_setdfltparm(isp, channel)
   3914 	struct ispsoftc *isp;
   3915 	int channel;
   3916 {
   3917 	int tgt;
   3918 	mbreg_t mbs;
   3919 	sdparam *sdp;
   3920 
   3921 	if (IS_FC(isp)) {
   3922 		fcparam *fcp = (fcparam *) isp->isp_param;
   3923 		fcp += channel;
   3924 		if (fcp->isp_gotdparms) {
   3925 			return;
   3926 		}
   3927 		fcp->isp_gotdparms = 1;
   3928 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
   3929 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
   3930 		fcp->isp_execthrottle = ICB_DFLT_THROTTLE;
   3931 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
   3932 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
   3933 		/* Platform specific.... */
   3934 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
   3935 		fcp->isp_nodewwn = DEFAULT_WWN(isp);
   3936 		if ((fcp->isp_nodewwn >> 60) == 2) {
   3937 			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
   3938 			fcp->isp_portwwn = fcp->isp_nodewwn |
   3939 			    (((u_int64_t)(isp->isp_unit+1)) << 48);
   3940 		} else {
   3941 			fcp->isp_portwwn = fcp->isp_nodewwn;
   3942 		}
   3943 		/*
   3944 		 * Now try and read NVRAM
   3945 		 */
   3946 		if ((isp->isp_confopts & (ISP_CFG_NONVRAM|ISP_CFG_OWNWWN)) ||
   3947 		    (isp_read_nvram(isp))) {
   3948 			PRINTF("%s: Node WWN 0x%08x%08x, Port WWN 0x%08x%08x\n",
   3949 			    isp->isp_name, (u_int32_t) (fcp->isp_nodewwn >> 32),
   3950 			    (u_int32_t) (fcp->isp_nodewwn & 0xffffffff),
   3951 			    (u_int32_t) (fcp->isp_portwwn >> 32),
   3952 			    (u_int32_t) (fcp->isp_portwwn & 0xffffffff));
   3953 		}
   3954 		return;
   3955 	}
   3956 
   3957 	sdp = (sdparam *) isp->isp_param;
   3958 	sdp += channel;
   3959 
   3960 	/*
   3961 	 * Been there, done that, got the T-shirt...
   3962 	 */
   3963 	if (sdp->isp_gotdparms) {
   3964 		return;
   3965 	}
   3966 	sdp->isp_gotdparms = 1;
   3967 
   3968 	/*
   3969 	 * If we've not been told to avoid reading NVRAM, try and read it.
   3970 	 * If we're successful reading it, we can return since NVRAM will
   3971 	 * tell us the right thing to do. Otherwise, establish some reasonable
   3972 	 * defaults.
   3973 	 */
   3974 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   3975 		if (isp_read_nvram(isp) == 0) {
   3976 			return;
   3977 		}
   3978 	}
   3979 
   3980 	/*
   3981 	 * Now try and see whether we have specific values for them.
   3982 	 */
   3983 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   3984 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   3985 		isp_mboxcmd(isp, &mbs);
   3986 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3987 			IDPRINTF(2, ("could not GET ACT NEG STATE\n"));
   3988 			sdp->isp_req_ack_active_neg = 1;
   3989 			sdp->isp_data_line_active_neg = 1;
   3990 		} else {
   3991 			sdp->isp_req_ack_active_neg =
   3992 			    (mbs.param[1+channel] >> 4) & 0x1;
   3993 			sdp->isp_data_line_active_neg =
   3994 			    (mbs.param[1+channel] >> 5) & 0x1;
   3995 		}
   3996 	} else {
   3997 		sdp->isp_req_ack_active_neg = 1;
   3998 		sdp->isp_data_line_active_neg = 1;
   3999 	}
   4000 
   4001 	IDPRINTF(3, ("%s: defaulting bus %d REQ/ACK Active Negation is %d\n",
   4002 	    isp->isp_name, channel, sdp->isp_req_ack_active_neg));
   4003 	IDPRINTF(3, ("%s: defaulting bus %d DATA Active Negation is %d\n",
   4004 	    isp->isp_name, channel, sdp->isp_data_line_active_neg));
   4005 
   4006 	/*
   4007 	 * The trick here is to establish a default for the default (honk!)
   4008 	 * state (dev_flags). Then try and get the current status from
   4009 	 * the card to fill in the current state. We don't, in fact, set
   4010 	 * the default to the SAFE default state- that's not the goal state.
   4011 	 */
   4012 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4013 		sdp->isp_devparam[tgt].cur_offset = 0;
   4014 		sdp->isp_devparam[tgt].cur_period = 0;
   4015 		sdp->isp_devparam[tgt].dev_flags = DPARM_DEFAULT;
   4016 		sdp->isp_devparam[tgt].cur_dflags = 0;
   4017 		/*
   4018 		 * We default to Wide/Fast for versions less than a 1040
   4019 		 * (unless it's SBus).
   4020 		 */
   4021 		if ((isp->isp_bustype == ISP_BT_SBUS &&
   4022 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
   4023 		    (isp->isp_bustype == ISP_BT_PCI &&
   4024 		    isp->isp_type < ISP_HA_SCSI_1040) ||
   4025 		    (isp->isp_clock && isp->isp_clock < 60) ||
   4026 		    (sdp->isp_ultramode == 0)) {
   4027 			sdp->isp_devparam[tgt].sync_offset =
   4028 			    ISP_10M_SYNCPARMS >> 8;
   4029 			sdp->isp_devparam[tgt].sync_period =
   4030 			    ISP_10M_SYNCPARMS & 0xff;
   4031 		} else if (IS_ULTRA2(isp)) {
   4032 			sdp->isp_devparam[tgt].sync_offset =
   4033 			    ISP_40M_SYNCPARMS >> 8;
   4034 			sdp->isp_devparam[tgt].sync_period =
   4035 			    ISP_40M_SYNCPARMS & 0xff;
   4036 		} else {
   4037 			sdp->isp_devparam[tgt].sync_offset =
   4038 			    ISP_20M_SYNCPARMS >> 8;
   4039 			sdp->isp_devparam[tgt].sync_period =
   4040 			    ISP_20M_SYNCPARMS & 0xff;
   4041 		}
   4042 
   4043 		/*
   4044 		 * Don't get current target parameters if we've been
   4045 		 * told not to use NVRAM- it's really the same thing.
   4046 		 */
   4047 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4048 
   4049 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   4050 			mbs.param[1] = tgt << 8;
   4051 			isp_mboxcmd(isp, &mbs);
   4052 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4053 				continue;
   4054 			}
   4055 			sdp->isp_devparam[tgt].cur_dflags = mbs.param[2];
   4056 			sdp->isp_devparam[tgt].dev_flags = mbs.param[2];
   4057 			sdp->isp_devparam[tgt].cur_period = mbs.param[3] & 0xff;
   4058 			sdp->isp_devparam[tgt].cur_offset = mbs.param[3] >> 8;
   4059 
   4060 			/*
   4061 			 * The maximum period we can really see
   4062 			 * here is 100 (decimal), or 400 ns.
   4063 			 * For some unknown reason we sometimes
   4064 			 * get back wildass numbers from the
   4065 			 * boot device's parameters (alpha only).
   4066 			 */
   4067 			if ((mbs.param[3] & 0xff) <= 0x64) {
   4068 				sdp->isp_devparam[tgt].sync_period =
   4069 				    mbs.param[3] & 0xff;
   4070 				sdp->isp_devparam[tgt].sync_offset =
   4071 				    mbs.param[3] >> 8;
   4072 			}
   4073 
   4074 			/*
   4075 			 * It is not safe to run Ultra Mode with a clock < 60.
   4076 			 */
   4077 			if (((isp->isp_clock && isp->isp_clock < 60) ||
   4078 			    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
   4079 			    (sdp->isp_devparam[tgt].sync_period <=
   4080 			    (ISP_20M_SYNCPARMS & 0xff))) {
   4081 				sdp->isp_devparam[tgt].sync_offset =
   4082 				    ISP_10M_SYNCPARMS >> 8;
   4083 				sdp->isp_devparam[tgt].sync_period =
   4084 				    ISP_10M_SYNCPARMS & 0xff;
   4085 			}
   4086 		}
   4087 		IDPRINTF(3, ("%s: bus %d tgt %d flags %x offset %x period %x\n",
   4088 		    isp->isp_name, channel, tgt,
   4089 		    sdp->isp_devparam[tgt].dev_flags,
   4090 		    sdp->isp_devparam[tgt].sync_offset,
   4091 		    sdp->isp_devparam[tgt].sync_period));
   4092 	}
   4093 
   4094 	/*
   4095 	 * Establish default some more default parameters.
   4096 	 */
   4097 	sdp->isp_cmd_dma_burst_enable = 1;
   4098 	sdp->isp_data_dma_burst_enabl = 1;
   4099 	sdp->isp_fifo_threshold = 0;
   4100 	sdp->isp_initiator_id = 7;
   4101 	/* XXXX This is probably based upon clock XXXX */
   4102 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   4103 		sdp->isp_async_data_setup = 9;
   4104 	} else {
   4105 		sdp->isp_async_data_setup = 6;
   4106 	}
   4107 	sdp->isp_selection_timeout = 250;
   4108 	sdp->isp_max_queue_depth = MAXISPREQUEST;
   4109 	sdp->isp_tag_aging = 8;
   4110 	sdp->isp_bus_reset_delay = 3;
   4111 	sdp->isp_retry_count = 2;
   4112 	sdp->isp_retry_delay = 2;
   4113 
   4114 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4115 		sdp->isp_devparam[tgt].exc_throttle = 16;
   4116 		sdp->isp_devparam[tgt].dev_enable = 1;
   4117 	}
   4118 }
   4119 
   4120 /*
   4121  * Re-initialize the ISP and complete all orphaned commands
   4122  * with a 'botched' notice. The reset/init routines should
   4123  * not disturb an already active list of commands.
   4124  *
   4125  * Locks held prior to coming here.
   4126  */
   4127 
   4128 void
   4129 isp_restart(isp)
   4130 	struct ispsoftc *isp;
   4131 {
   4132 	ISP_SCSI_XFER_T *xs;
   4133 	u_int32_t handle;
   4134 
   4135 	isp_reset(isp);
   4136 	if (isp->isp_state == ISP_RESETSTATE) {
   4137 		isp_init(isp);
   4138 		if (isp->isp_state == ISP_INITSTATE) {
   4139 			isp->isp_state = ISP_RUNSTATE;
   4140 		}
   4141 	}
   4142 	if (isp->isp_state != ISP_RUNSTATE) {
   4143 		PRINTF("%s: isp_restart cannot restart ISP\n", isp->isp_name);
   4144 	}
   4145 	isp->isp_nactive = 0;
   4146 
   4147 	for (handle = 1; handle <= (int) isp->isp_maxcmds; handle++) {
   4148 		xs = isp_find_xs(isp, handle);
   4149 		if (xs == NULL) {
   4150 			continue;
   4151 		}
   4152 		isp_destroy_handle(isp, handle);
   4153 		if (XS_XFRLEN(xs)) {
   4154 			ISP_DMAFREE(isp, xs, handle);
   4155 			XS_RESID(xs) = XS_XFRLEN(xs);
   4156 		} else {
   4157 			XS_RESID(xs) = 0;
   4158 		}
   4159 		XS_SETERR(xs, HBA_BUSRESET);
   4160 		XS_CMD_DONE(xs);
   4161 	}
   4162 }
   4163 
   4164 /*
   4165  * NVRAM Routines
   4166  */
   4167 static int
   4168 isp_read_nvram(isp)
   4169 	struct ispsoftc *isp;
   4170 {
   4171 	int i, amt;
   4172 	u_int8_t csum, minversion;
   4173 	union {
   4174 		u_int8_t _x[ISP2100_NVRAM_SIZE];
   4175 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
   4176 	} _n;
   4177 #define	nvram_data	_n._x
   4178 #define	nvram_words	_n._s
   4179 
   4180 	if (IS_FC(isp)) {
   4181 		amt = ISP2100_NVRAM_SIZE;
   4182 		minversion = 1;
   4183 	} else if (IS_ULTRA2(isp)) {
   4184 		amt = ISP1080_NVRAM_SIZE;
   4185 		minversion = 0;
   4186 	} else {
   4187 		amt = ISP_NVRAM_SIZE;
   4188 		minversion = 2;
   4189 	}
   4190 
   4191 	/*
   4192 	 * Just read the first two words first to see if we have a valid
   4193 	 * NVRAM to continue reading the rest with.
   4194 	 */
   4195 	for (i = 0; i < 2; i++) {
   4196 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   4197 	}
   4198 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   4199 	    nvram_data[2] != 'P') {
   4200 		if (isp->isp_bustype != ISP_BT_SBUS) {
   4201 			PRINTF("%s: invalid NVRAM header (%x,%x,%x,%x)\n",
   4202 			    isp->isp_name, nvram_data[0], nvram_data[1],
   4203 			    nvram_data[2], nvram_data[3]);
   4204 		}
   4205 		return (-1);
   4206 	}
   4207 	for (i = 2; i < amt>>1; i++) {
   4208 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   4209 	}
   4210 	for (csum = 0, i = 0; i < amt; i++) {
   4211 		csum += nvram_data[i];
   4212 	}
   4213 	if (csum != 0) {
   4214 		PRINTF("%s: invalid NVRAM checksum\n", isp->isp_name);
   4215 		return (-1);
   4216 	}
   4217 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   4218 		PRINTF("%s: version %d NVRAM not understood\n", isp->isp_name,
   4219 		    ISP_NVRAM_VERSION(nvram_data));
   4220 		return (-1);
   4221 	}
   4222 
   4223 	if (IS_ULTRA3(isp)) {
   4224 		isp_parse_nvram_12160(isp, 0, nvram_data);
   4225 		isp_parse_nvram_12160(isp, 1, nvram_data);
   4226 	} else if (IS_1080(isp)) {
   4227 		isp_parse_nvram_1080(isp, 0, nvram_data);
   4228 	} else if (IS_1280(isp) || IS_1240(isp)) {
   4229 		isp_parse_nvram_1080(isp, 0, nvram_data);
   4230 		isp_parse_nvram_1080(isp, 1, nvram_data);
   4231 	} else if (IS_SCSI(isp)) {
   4232 		isp_parse_nvram_1020(isp, nvram_data);
   4233 	} else {
   4234 		isp_parse_nvram_2100(isp, nvram_data);
   4235 	}
   4236 	IDPRINTF(3, ("%s: NVRAM is valid\n", isp->isp_name));
   4237 	return (0);
   4238 #undef	nvram_data
   4239 #undef	nvram_words
   4240 }
   4241 
   4242 static void
   4243 isp_rdnvram_word(isp, wo, rp)
   4244 	struct ispsoftc *isp;
   4245 	int wo;
   4246 	u_int16_t *rp;
   4247 {
   4248 	int i, cbits;
   4249 	u_int16_t bit, rqst;
   4250 
   4251 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   4252 	SYS_DELAY(2);
   4253 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   4254 	SYS_DELAY(2);
   4255 
   4256 	if (IS_FC(isp)) {
   4257 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   4258 		rqst = (ISP_NVRAM_READ << 8) | wo;
   4259 		cbits = 10;
   4260 	} else if (IS_ULTRA2(isp)) {
   4261 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
   4262 		rqst = (ISP_NVRAM_READ << 8) | wo;
   4263 		cbits = 10;
   4264 	} else {
   4265 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   4266 		rqst = (ISP_NVRAM_READ << 6) | wo;
   4267 		cbits = 8;
   4268 	}
   4269 
   4270 	/*
   4271 	 * Clock the word select request out...
   4272 	 */
   4273 	for (i = cbits; i >= 0; i--) {
   4274 		if ((rqst >> i) & 1) {
   4275 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   4276 		} else {
   4277 			bit = BIU_NVRAM_SELECT;
   4278 		}
   4279 		ISP_WRITE(isp, BIU_NVRAM, bit);
   4280 		SYS_DELAY(2);
   4281 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   4282 		SYS_DELAY(2);
   4283 		ISP_WRITE(isp, BIU_NVRAM, bit);
   4284 		SYS_DELAY(2);
   4285 	}
   4286 	/*
   4287 	 * Now read the result back in (bits come back in MSB format).
   4288 	 */
   4289 	*rp = 0;
   4290 	for (i = 0; i < 16; i++) {
   4291 		u_int16_t rv;
   4292 		*rp <<= 1;
   4293 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   4294 		SYS_DELAY(2);
   4295 		rv = ISP_READ(isp, BIU_NVRAM);
   4296 		if (rv & BIU_NVRAM_DATAIN) {
   4297 			*rp |= 1;
   4298 		}
   4299 		SYS_DELAY(2);
   4300 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   4301 		SYS_DELAY(2);
   4302 	}
   4303 	ISP_WRITE(isp, BIU_NVRAM, 0);
   4304 	SYS_DELAY(2);
   4305 #if	BYTE_ORDER == BIG_ENDIAN
   4306 	*rp = ((*rp >> 8) | ((*rp & 0xff) << 8));
   4307 #endif
   4308 }
   4309 
   4310 static void
   4311 isp_parse_nvram_1020(isp, nvram_data)
   4312 	struct ispsoftc *isp;
   4313 	u_int8_t *nvram_data;
   4314 {
   4315 	int i;
   4316 	static char *tru = "true";
   4317 	static char *not = "false";
   4318 	sdparam *sdp = (sdparam *) isp->isp_param;
   4319 
   4320 	sdp->isp_fifo_threshold =
   4321 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
   4322 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
   4323 
   4324 	sdp->isp_initiator_id =
   4325 		ISP_NVRAM_INITIATOR_ID(nvram_data);
   4326 
   4327 	sdp->isp_bus_reset_delay =
   4328 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   4329 
   4330 	sdp->isp_retry_count =
   4331 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   4332 
   4333 	sdp->isp_retry_delay =
   4334 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   4335 
   4336 	sdp->isp_async_data_setup =
   4337 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   4338 
   4339 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   4340 		if (sdp->isp_async_data_setup < 9) {
   4341 			sdp->isp_async_data_setup = 9;
   4342 		}
   4343 	} else {
   4344 		if (sdp->isp_async_data_setup != 6) {
   4345 			sdp->isp_async_data_setup = 6;
   4346 		}
   4347 	}
   4348 
   4349 	sdp->isp_req_ack_active_neg =
   4350 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   4351 
   4352 	sdp->isp_data_line_active_neg =
   4353 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   4354 
   4355 	sdp->isp_data_dma_burst_enabl =
   4356 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   4357 
   4358 	sdp->isp_cmd_dma_burst_enable =
   4359 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   4360 
   4361 	sdp->isp_tag_aging =
   4362 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   4363 
   4364 	sdp->isp_selection_timeout =
   4365 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   4366 
   4367 	sdp->isp_max_queue_depth =
   4368 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   4369 
   4370 	isp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   4371 	if (isp->isp_dblev > 2) {
   4372 		PRINTF("%s: NVRAM values:\n", isp->isp_name);
   4373 		PRINTF("             Fifo Threshold = 0x%x\n",
   4374 		    sdp->isp_fifo_threshold);
   4375 		PRINTF("            Bus Reset Delay = %d\n",
   4376 		    sdp->isp_bus_reset_delay);
   4377 		PRINTF("                Retry Count = %d\n",
   4378 		    sdp->isp_retry_count);
   4379 		PRINTF("                Retry Delay = %d\n",
   4380 		    sdp->isp_retry_delay);
   4381 		PRINTF("              Tag Age Limit = %d\n",
   4382 		    sdp->isp_tag_aging);
   4383 		PRINTF("          Selection Timeout = %d\n",
   4384 		    sdp->isp_selection_timeout);
   4385 		PRINTF("            Max Queue Depth = %d\n",
   4386 		    sdp->isp_max_queue_depth);
   4387 		PRINTF("           Async Data Setup = 0x%x\n",
   4388 		    sdp->isp_async_data_setup);
   4389 		PRINTF("    REQ/ACK Active Negation = %s\n",
   4390 		    sdp->isp_req_ack_active_neg? tru : not);
   4391 		PRINTF("  Data Line Active Negation = %s\n",
   4392 		    sdp->isp_data_line_active_neg? tru : not);
   4393 		PRINTF("      Data DMA Burst Enable = %s\n",
   4394 		    sdp->isp_data_dma_burst_enabl? tru : not);
   4395 		PRINTF("       Cmd DMA Burst Enable = %s\n",
   4396 		    sdp->isp_cmd_dma_burst_enable? tru : not);
   4397 		PRINTF("                  Fast MTTR = %s\n",
   4398 		    isp->isp_fast_mttr? tru : not);
   4399 	}
   4400 	for (i = 0; i < MAX_TARGETS; i++) {
   4401 		sdp->isp_devparam[i].dev_enable =
   4402 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
   4403 		sdp->isp_devparam[i].exc_throttle =
   4404 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
   4405 		sdp->isp_devparam[i].sync_offset =
   4406 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
   4407 		sdp->isp_devparam[i].sync_period =
   4408 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
   4409 
   4410 		if (isp->isp_type < ISP_HA_SCSI_1040) {
   4411 			/*
   4412 			 * If we're not ultra, we can't possibly
   4413 			 * be a shorter period than this.
   4414 			 */
   4415 			if (sdp->isp_devparam[i].sync_period < 0x19) {
   4416 				sdp->isp_devparam[i].sync_period =
   4417 				    0x19;
   4418 			}
   4419 			if (sdp->isp_devparam[i].sync_offset > 0xc) {
   4420 				sdp->isp_devparam[i].sync_offset =
   4421 				    0x0c;
   4422 			}
   4423 		} else {
   4424 			if (sdp->isp_devparam[i].sync_offset > 0x8) {
   4425 				sdp->isp_devparam[i].sync_offset = 0x8;
   4426 			}
   4427 		}
   4428 		sdp->isp_devparam[i].dev_flags = 0;
   4429 		if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
   4430 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   4431 		if (ISP_NVRAM_TGT_QFRZ(nvram_data, i)) {
   4432 			PRINTF("%s: not supporting QFRZ option for "
   4433 			    "target %d\n", isp->isp_name, i);
   4434 		}
   4435 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   4436 		if (ISP_NVRAM_TGT_ARQ(nvram_data, i) == 0) {
   4437 			PRINTF("%s: not disabling ARQ option for "
   4438 			    "target %d\n", isp->isp_name, i);
   4439 		}
   4440 		if (ISP_NVRAM_TGT_TQING(nvram_data, i))
   4441 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   4442 		if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
   4443 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   4444 		if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
   4445 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   4446 		if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
   4447 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   4448 		if (ISP_NVRAM_TGT_DISC(nvram_data, i))
   4449 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   4450 		sdp->isp_devparam[i].cur_dflags = 0; /* we don't know */
   4451 		if (isp->isp_dblev > 2) {
   4452 			PRINTF("   Target %d: Enabled %d Throttle %d "
   4453 			    "Offset %d Period %d Flags 0x%x\n", i,
   4454 			    sdp->isp_devparam[i].dev_enable,
   4455 			    sdp->isp_devparam[i].exc_throttle,
   4456 			    sdp->isp_devparam[i].sync_offset,
   4457 			    sdp->isp_devparam[i].sync_period,
   4458 			    sdp->isp_devparam[i].dev_flags);
   4459 		}
   4460 	}
   4461 }
   4462 
   4463 static void
   4464 isp_parse_nvram_1080(isp, bus, nvram_data)
   4465 	struct ispsoftc *isp;
   4466 	int bus;
   4467 	u_int8_t *nvram_data;
   4468 {
   4469 	static char *tru = "true";
   4470 	static char *not = "false";
   4471 	int i;
   4472 	sdparam *sdp = (sdparam *) isp->isp_param;
   4473 	sdp += bus;
   4474 
   4475 	sdp->isp_fifo_threshold =
   4476 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
   4477 
   4478 	sdp->isp_initiator_id =
   4479 	    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
   4480 
   4481 	sdp->isp_bus_reset_delay =
   4482 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   4483 
   4484 	sdp->isp_retry_count =
   4485 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   4486 
   4487 	sdp->isp_retry_delay =
   4488 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   4489 
   4490 	sdp->isp_async_data_setup =
   4491 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
   4492 	    bus);
   4493 
   4494 	sdp->isp_req_ack_active_neg =
   4495 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
   4496 	    bus);
   4497 
   4498 	sdp->isp_data_line_active_neg =
   4499 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
   4500 	    bus);
   4501 
   4502 	sdp->isp_data_dma_burst_enabl =
   4503 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   4504 
   4505 	sdp->isp_cmd_dma_burst_enable =
   4506 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   4507 
   4508 	sdp->isp_selection_timeout =
   4509 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   4510 
   4511 	sdp->isp_max_queue_depth =
   4512 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   4513 
   4514 	if (isp->isp_dblev >= 3) {
   4515 		PRINTF("%s: ISP1080 bus %d NVRAM values:\n",
   4516 		    isp->isp_name, bus);
   4517 		PRINTF("               Initiator ID = %d\n",
   4518 		    sdp->isp_initiator_id);
   4519 		PRINTF("             Fifo Threshold = 0x%x\n",
   4520 		    sdp->isp_fifo_threshold);
   4521 		PRINTF("            Bus Reset Delay = %d\n",
   4522 		    sdp->isp_bus_reset_delay);
   4523 		PRINTF("                Retry Count = %d\n",
   4524 		    sdp->isp_retry_count);
   4525 		PRINTF("                Retry Delay = %d\n",
   4526 		    sdp->isp_retry_delay);
   4527 		PRINTF("              Tag Age Limit = %d\n",
   4528 		    sdp->isp_tag_aging);
   4529 		PRINTF("          Selection Timeout = %d\n",
   4530 		    sdp->isp_selection_timeout);
   4531 		PRINTF("            Max Queue Depth = %d\n",
   4532 		    sdp->isp_max_queue_depth);
   4533 		PRINTF("           Async Data Setup = 0x%x\n",
   4534 		    sdp->isp_async_data_setup);
   4535 		PRINTF("    REQ/ACK Active Negation = %s\n",
   4536 		    sdp->isp_req_ack_active_neg? tru : not);
   4537 		PRINTF("  Data Line Active Negation = %s\n",
   4538 		    sdp->isp_data_line_active_neg? tru : not);
   4539 		PRINTF("       Cmd DMA Burst Enable = %s\n",
   4540 		    sdp->isp_cmd_dma_burst_enable? tru : not);
   4541 	}
   4542 	for (i = 0; i < MAX_TARGETS; i++) {
   4543 		sdp->isp_devparam[i].dev_enable =
   4544 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
   4545 		sdp->isp_devparam[i].exc_throttle =
   4546 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
   4547 		sdp->isp_devparam[i].sync_offset =
   4548 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
   4549 		sdp->isp_devparam[i].sync_period =
   4550 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
   4551 		sdp->isp_devparam[i].dev_flags = 0;
   4552 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, i, bus))
   4553 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   4554 		if (ISP1080_NVRAM_TGT_QFRZ(nvram_data, i, bus)) {
   4555 			PRINTF("%s: not supporting QFRZ option "
   4556 			    "for target %d bus %d\n",
   4557 			    isp->isp_name, i, bus);
   4558 		}
   4559 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   4560 		if (ISP1080_NVRAM_TGT_ARQ(nvram_data, i, bus) == 0) {
   4561 			PRINTF("%s: not disabling ARQ option "
   4562 			    "for target %d bus %d\n",
   4563 			    isp->isp_name, i, bus);
   4564 		}
   4565 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, i, bus))
   4566 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   4567 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, i, bus))
   4568 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   4569 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, i, bus))
   4570 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   4571 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, i, bus))
   4572 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   4573 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, i, bus))
   4574 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   4575 		sdp->isp_devparam[i].cur_dflags = 0;
   4576 		if (isp->isp_dblev >= 3) {
   4577 			PRINTF("   Target %d: Ena %d Throttle "
   4578 			    "%d Offset %d Period %d Flags "
   4579 			    "0x%x\n", i,
   4580 			    sdp->isp_devparam[i].dev_enable,
   4581 			    sdp->isp_devparam[i].exc_throttle,
   4582 			    sdp->isp_devparam[i].sync_offset,
   4583 			    sdp->isp_devparam[i].sync_period,
   4584 			    sdp->isp_devparam[i].dev_flags);
   4585 		}
   4586 	}
   4587 }
   4588 
   4589 static void
   4590 isp_parse_nvram_12160(isp, bus, nvram_data)
   4591 	struct ispsoftc *isp;
   4592 	int bus;
   4593 	u_int8_t *nvram_data;
   4594 {
   4595 	static char *tru = "true";
   4596 	static char *not = "false";
   4597 	sdparam *sdp = (sdparam *) isp->isp_param;
   4598 	int i;
   4599 
   4600 	sdp += bus;
   4601 
   4602 	sdp->isp_fifo_threshold =
   4603 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
   4604 
   4605 	sdp->isp_initiator_id =
   4606 	    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
   4607 
   4608 	sdp->isp_bus_reset_delay =
   4609 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   4610 
   4611 	sdp->isp_retry_count =
   4612 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   4613 
   4614 	sdp->isp_retry_delay =
   4615 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   4616 
   4617 	sdp->isp_async_data_setup =
   4618 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
   4619 	    bus);
   4620 
   4621 	sdp->isp_req_ack_active_neg =
   4622 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
   4623 	    bus);
   4624 
   4625 	sdp->isp_data_line_active_neg =
   4626 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
   4627 	    bus);
   4628 
   4629 	sdp->isp_data_dma_burst_enabl =
   4630 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   4631 
   4632 	sdp->isp_cmd_dma_burst_enable =
   4633 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   4634 
   4635 	sdp->isp_selection_timeout =
   4636 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   4637 
   4638 	sdp->isp_max_queue_depth =
   4639 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   4640 
   4641 	if (isp->isp_dblev >= 3) {
   4642 		PRINTF("%s: ISP12160 bus %d NVRAM values:\n",
   4643 		    isp->isp_name, bus);
   4644 		PRINTF("               Initiator ID = %d\n",
   4645 		    sdp->isp_initiator_id);
   4646 		PRINTF("             Fifo Threshold = 0x%x\n",
   4647 		    sdp->isp_fifo_threshold);
   4648 		PRINTF("            Bus Reset Delay = %d\n",
   4649 		    sdp->isp_bus_reset_delay);
   4650 		PRINTF("                Retry Count = %d\n",
   4651 		    sdp->isp_retry_count);
   4652 		PRINTF("                Retry Delay = %d\n",
   4653 		    sdp->isp_retry_delay);
   4654 		PRINTF("              Tag Age Limit = %d\n",
   4655 		    sdp->isp_tag_aging);
   4656 		PRINTF("          Selection Timeout = %d\n",
   4657 		    sdp->isp_selection_timeout);
   4658 		PRINTF("            Max Queue Depth = %d\n",
   4659 		    sdp->isp_max_queue_depth);
   4660 		PRINTF("           Async Data Setup = 0x%x\n",
   4661 		    sdp->isp_async_data_setup);
   4662 		PRINTF("    REQ/ACK Active Negation = %s\n",
   4663 		    sdp->isp_req_ack_active_neg? tru : not);
   4664 		PRINTF("  Data Line Active Negation = %s\n",
   4665 		    sdp->isp_data_line_active_neg? tru : not);
   4666 		PRINTF("       Cmd DMA Burst Enable = %s\n",
   4667 		    sdp->isp_cmd_dma_burst_enable? tru : not);
   4668 	}
   4669 
   4670 	for (i = 0; i < MAX_TARGETS; i++) {
   4671 		sdp->isp_devparam[i].dev_enable =
   4672 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
   4673 		sdp->isp_devparam[i].exc_throttle =
   4674 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
   4675 		sdp->isp_devparam[i].sync_offset =
   4676 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
   4677 		sdp->isp_devparam[i].sync_period =
   4678 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
   4679 		sdp->isp_devparam[i].dev_flags = 0;
   4680 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, i, bus))
   4681 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   4682 		if (ISP12160_NVRAM_TGT_QFRZ(nvram_data, i, bus)) {
   4683 			PRINTF("%s: not supporting QFRZ option "
   4684 			    "for target %d bus %d\n", isp->isp_name, i, bus);
   4685 		}
   4686 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   4687 		if (ISP12160_NVRAM_TGT_ARQ(nvram_data, i, bus) == 0) {
   4688 			PRINTF("%s: not disabling ARQ option "
   4689 			    "for target %d bus %d\n", isp->isp_name, i, bus);
   4690 		}
   4691 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, i, bus))
   4692 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   4693 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, i, bus))
   4694 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   4695 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, i, bus))
   4696 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   4697 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, i, bus))
   4698 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   4699 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, i, bus))
   4700 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   4701 		sdp->isp_devparam[i].cur_dflags = 0;
   4702 		if (isp->isp_dblev >= 3) {
   4703 			PRINTF("   Target %d: Ena %d Throttle %d Offset %d "
   4704 			    "Period %d Flags 0x%x\n", i,
   4705 			    sdp->isp_devparam[i].dev_enable,
   4706 			    sdp->isp_devparam[i].exc_throttle,
   4707 			    sdp->isp_devparam[i].sync_offset,
   4708 			    sdp->isp_devparam[i].sync_period,
   4709 			    sdp->isp_devparam[i].dev_flags);
   4710 		}
   4711 	}
   4712 }
   4713 
   4714 static void
   4715 isp_parse_nvram_2100(isp, nvram_data)
   4716 	struct ispsoftc *isp;
   4717 	u_int8_t *nvram_data;
   4718 {
   4719 	fcparam *fcp = (fcparam *) isp->isp_param;
   4720 	union {
   4721 		struct {
   4722 #if	BYTE_ORDER == BIG_ENDIAN
   4723 			u_int32_t hi32;
   4724 			u_int32_t lo32;
   4725 #else
   4726 			u_int32_t lo32;
   4727 			u_int32_t hi32;
   4728 #endif
   4729 		} wd;
   4730 		u_int64_t full64;
   4731 	} wwnstore;
   4732 
   4733 	/*
   4734 	 * There is supposed to be WWNN storage as distinct
   4735 	 * from WWPN storage in NVRAM, but it doesn't appear
   4736 	 * to be used sanely.
   4737 	 */
   4738 
   4739 	wwnstore.full64 = ISP2100_NVRAM_PORT_NAME(nvram_data);
   4740 	if (wwnstore.full64 != 0LL) {
   4741 		switch ((int) (wwnstore.full64 >> 60)) {
   4742 		case 0:
   4743 			/*
   4744 			 * Broken cards with nothing in the top nibble.
   4745 			 * Pah.
   4746 			 */
   4747 			wwnstore.full64 |= (2LL << 60);
   4748 			/* FALLTHROUGH */
   4749 		case 2:
   4750 			fcp->isp_portwwn = wwnstore.full64;
   4751 			fcp->isp_nodewwn = wwnstore.full64;
   4752 			fcp->isp_nodewwn &= ~((0xfffLL) << 48);
   4753 			if (fcp->isp_nodewwn == fcp->isp_portwwn) {
   4754 				fcp->isp_portwwn |=
   4755 				    (((u_int64_t)(isp->isp_unit+1)) << 48);
   4756 			}
   4757 			break;
   4758 		default:
   4759 			fcp->isp_portwwn = wwnstore.full64;
   4760 			fcp->isp_nodewwn = wwnstore.full64;
   4761 		}
   4762 	}
   4763 	CFGPRINTF("%s: Node WWN 0x%08x%08x, Port WWN 0x%08x%08x\n",
   4764 	    isp->isp_name, (u_int32_t) (fcp->isp_nodewwn >> 32),
   4765 	    (u_int32_t) (fcp->isp_nodewwn & 0xffffffff),
   4766 	    (u_int32_t) (fcp->isp_portwwn >> 32),
   4767 	    (u_int32_t) (fcp->isp_portwwn & 0xffffffff));
   4768 
   4769 	fcp->isp_maxalloc =
   4770 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   4771 	fcp->isp_maxfrmlen =
   4772 		ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   4773 	fcp->isp_retry_delay =
   4774 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   4775 	fcp->isp_retry_count =
   4776 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   4777 	fcp->isp_loopid =
   4778 		ISP2100_NVRAM_HARDLOOPID(nvram_data);
   4779 	fcp->isp_execthrottle =
   4780 		ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   4781 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
   4782 	if (isp->isp_dblev > 2) {
   4783 		PRINTF("%s: NVRAM values:\n", isp->isp_name);
   4784 		PRINTF("  Max IOCB Allocation = %d\n",
   4785 		    fcp->isp_maxalloc);
   4786 		PRINTF("     Max Frame Length = %d\n",
   4787 		    fcp->isp_maxfrmlen);
   4788 		PRINTF("   Execution Throttle = %d\n",
   4789 		    fcp->isp_execthrottle);
   4790 		PRINTF("          Retry Count = %d\n",
   4791 		    fcp->isp_retry_count);
   4792 		PRINTF("          Retry Delay = %d\n",
   4793 		    fcp->isp_retry_delay);
   4794 		PRINTF("         Hard Loop ID = %d\n",
   4795 		    fcp->isp_loopid);
   4796 		PRINTF("              Options = 0x%x\n",
   4797 		    fcp->isp_fwoptions);
   4798 		PRINTF("          HBA Options = 0x%x\n",
   4799 		    ISP2100_NVRAM_HBA_OPTIONS(nvram_data));
   4800 	}
   4801 }
   4802