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