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