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