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