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