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