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