Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.93
      1 /* $NetBSD: isp.c,v 1.93 2002/04/20 05:05:56 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.93 2002/04/20 05:05:56 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 	 * 16384 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 = 16384;
    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 			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
   1927 			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
   1928 			if (fcp->isp_fwstate != FW_READY ||
   1929 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
   1930 				return (-1);
   1931 			}
   1932 			switch (mbs.param[0]) {
   1933 			case MBOX_LOOP_ID_USED:
   1934 				/*
   1935 				 * Try the next available loop id.
   1936 				 */
   1937 				loopid++;
   1938 				break;
   1939 			case MBOX_PORT_ID_USED:
   1940 				/*
   1941 				 * This port is already logged in.
   1942 				 * Snaffle the loop id it's using if it's
   1943 				 * nonzero, otherwise we're hosed.
   1944 				 */
   1945 				if (mbs.param[1] != 0) {
   1946 					loopid = mbs.param[1];
   1947 					isp_prt(isp, ISP_LOGINFO, retained,
   1948 					    loopid, (int) (lp - fcp->portdb),
   1949 					    lp->portid);
   1950 				} else {
   1951 					loopid = MAX_FC_TARG;
   1952 					break;
   1953 				}
   1954 				/* FALLTHROUGH */
   1955 			case MBOX_COMMAND_COMPLETE:
   1956 				lp->loggedin = 1;
   1957 				lp->loopid = loopid;
   1958 				break;
   1959 			case MBOX_COMMAND_ERROR:
   1960 				isp_prt(isp, ISP_LOGINFO, plogierr,
   1961 				    portid, mbs.param[1]);
   1962 				/* FALLTHROUGH */
   1963 			case MBOX_ALL_IDS_USED: /* We're outta IDs */
   1964 			default:
   1965 				loopid = MAX_FC_TARG;
   1966 				break;
   1967 			}
   1968 		} while (lp->loopid == FL_PORT_ID && loopid < MAX_FC_TARG);
   1969 
   1970 		/*
   1971 		 * If we get here and we haven't set a Loop ID,
   1972 		 * we failed to log into this device.
   1973 		 */
   1974 
   1975 		if (lp->loopid == FL_PORT_ID) {
   1976 			lp->loopid = 0;
   1977 			continue;
   1978 		}
   1979 
   1980 		/*
   1981 		 * Make sure we can get the approriate port information.
   1982 		 */
   1983 		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
   1984 			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
   1985 			goto dump_em;
   1986 		}
   1987 
   1988 		if (fcp->isp_fwstate != FW_READY ||
   1989 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
   1990 			return (-1);
   1991 		}
   1992 
   1993 		if (pdb.pdb_loopid != lp->loopid) {
   1994 			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
   1995 			    lp->portid, pdb.pdb_loopid);
   1996 			goto dump_em;
   1997 		}
   1998 
   1999 		if (lp->portid != (u_int32_t) BITS2WORD(pdb.pdb_portid_bits)) {
   2000 			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
   2001 			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
   2002 			goto dump_em;
   2003 		}
   2004 
   2005 		lp->roles =
   2006 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   2007 		lp->node_wwn =
   2008 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
   2009 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
   2010 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
   2011 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
   2012 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
   2013 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
   2014 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
   2015 		    (((u_int64_t)pdb.pdb_nodename[7]));
   2016 		lp->port_wwn =
   2017 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
   2018 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
   2019 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
   2020 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
   2021 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
   2022 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
   2023 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
   2024 		    (((u_int64_t)pdb.pdb_portname[7]));
   2025 		/*
   2026 		 * Check to make sure this all makes sense.
   2027 		 */
   2028 		if (lp->node_wwn && lp->port_wwn) {
   2029 			lp->valid = 1;
   2030 			loopid = lp - fcp->portdb;
   2031 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
   2032 			continue;
   2033 		}
   2034 dump_em:
   2035 		lp->valid = 0;
   2036 		isp_prt(isp, ISP_LOGINFO,
   2037 		    ldumped, loopid, lp->loopid, lp->portid);
   2038 		mbs.param[0] = MBOX_FABRIC_LOGOUT;
   2039 		mbs.param[1] = lp->loopid << 8;
   2040 		mbs.param[2] = 0;
   2041 		mbs.param[3] = 0;
   2042 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
   2043 		if (fcp->isp_fwstate != FW_READY ||
   2044 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
   2045 			return (-1);
   2046 		}
   2047 	}
   2048 	/*
   2049 	 * If we get here, we've for sure seen not only a valid loop
   2050 	 * but know what is or isn't on it, so mark this for usage
   2051 	 * in isp_start.
   2052 	 */
   2053 	fcp->loop_seen_once = 1;
   2054 	fcp->isp_loopstate = LOOP_READY;
   2055 	return (0);
   2056 }
   2057 
   2058 static int
   2059 isp_scan_loop(struct ispsoftc *isp)
   2060 {
   2061 	struct lportdb *lp;
   2062 	fcparam *fcp = isp->isp_param;
   2063 	isp_pdb_t pdb;
   2064 	int loopid, lim, hival;
   2065 
   2066 	switch (fcp->isp_topo) {
   2067 	case TOPO_NL_PORT:
   2068 		hival = FL_PORT_ID;
   2069 		break;
   2070 	case TOPO_N_PORT:
   2071 		hival = 2;
   2072 		break;
   2073 	case TOPO_FL_PORT:
   2074 		hival = FC_PORT_ID;
   2075 		break;
   2076 	default:
   2077 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
   2078 		return (0);
   2079 	}
   2080 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
   2081 
   2082 	/*
   2083 	 * make sure the temp port database is clean...
   2084 	 */
   2085 	MEMZERO((void *)fcp->tport, sizeof (fcp->tport));
   2086 
   2087 	/*
   2088 	 * Run through the local loop ports and get port database info
   2089 	 * for each loop ID.
   2090 	 *
   2091 	 * There's a somewhat unexplained situation where the f/w passes back
   2092 	 * the wrong database entity- if that happens, just restart (up to
   2093 	 * FL_PORT_ID times).
   2094 	 */
   2095 	for (lim = loopid = 0; loopid < hival; loopid++) {
   2096 		lp = &fcp->tport[loopid];
   2097 
   2098 		/*
   2099 		 * Don't even try for ourselves...
   2100 	 	 */
   2101 		if (loopid == fcp->isp_loopid)
   2102 			continue;
   2103 
   2104 		lp->node_wwn = isp_get_portname(isp, loopid, 1);
   2105 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
   2106 			return (-1);
   2107 		if (lp->node_wwn == 0)
   2108 			continue;
   2109 		lp->port_wwn = isp_get_portname(isp, loopid, 0);
   2110 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
   2111 			return (-1);
   2112 		if (lp->port_wwn == 0) {
   2113 			lp->node_wwn = 0;
   2114 			continue;
   2115 		}
   2116 
   2117 		/*
   2118 		 * Get an entry....
   2119 		 */
   2120 		if (isp_getpdb(isp, loopid, &pdb) != 0) {
   2121 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
   2122 				return (-1);
   2123 			continue;
   2124 		}
   2125 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
   2126 			return (-1);
   2127 		}
   2128 
   2129 		/*
   2130 		 * If the returned database element doesn't match what we
   2131 		 * asked for, restart the process entirely (up to a point...).
   2132 		 */
   2133 		if (pdb.pdb_loopid != loopid) {
   2134 			loopid = 0;
   2135 			if (lim++ < hival) {
   2136 				continue;
   2137 			}
   2138 			isp_prt(isp, ISP_LOGWARN,
   2139 			    "giving up on synchronizing the port database");
   2140 			return (-1);
   2141 		}
   2142 
   2143 		/*
   2144 		 * Save the pertinent info locally.
   2145 		 */
   2146 		lp->node_wwn =
   2147 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
   2148 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
   2149 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
   2150 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
   2151 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
   2152 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
   2153 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
   2154 		    (((u_int64_t)pdb.pdb_nodename[7]));
   2155 		lp->port_wwn =
   2156 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
   2157 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
   2158 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
   2159 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
   2160 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
   2161 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
   2162 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
   2163 		    (((u_int64_t)pdb.pdb_portname[7]));
   2164 		lp->roles =
   2165 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   2166 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
   2167 		lp->loopid = pdb.pdb_loopid;
   2168 	}
   2169 
   2170 	/*
   2171 	 * Mark all of the permanent local loop database entries as invalid
   2172 	 * (except our own entry).
   2173 	 */
   2174 	for (loopid = 0; loopid < hival; loopid++) {
   2175 		if (loopid == fcp->isp_iid) {
   2176 			fcp->portdb[loopid].valid = 1;
   2177 			fcp->portdb[loopid].loopid = fcp->isp_loopid;
   2178 			continue;
   2179 		}
   2180 		fcp->portdb[loopid].valid = 0;
   2181 	}
   2182 
   2183 	/*
   2184 	 * Now merge our local copy of the port database into our saved copy.
   2185 	 * Notify the outer layers of new devices arriving.
   2186 	 */
   2187 	for (loopid = 0; loopid < hival; loopid++) {
   2188 		int i;
   2189 
   2190 		/*
   2191 		 * If we don't have a non-zero Port WWN, we're not here.
   2192 		 */
   2193 		if (fcp->tport[loopid].port_wwn == 0) {
   2194 			continue;
   2195 		}
   2196 
   2197 		/*
   2198 		 * Skip ourselves.
   2199 		 */
   2200 		if (loopid == fcp->isp_iid) {
   2201 			continue;
   2202 		}
   2203 
   2204 		/*
   2205 		 * For the purposes of deciding whether this is the
   2206 		 * 'same' device or not, we only search for an identical
   2207 		 * Port WWN. Node WWNs may or may not be the same as
   2208 		 * the Port WWN, and there may be multiple different
   2209 		 * Port WWNs with the same Node WWN. It would be chaos
   2210 		 * to have multiple identical Port WWNs, so we don't
   2211 		 * allow that.
   2212 		 */
   2213 
   2214 		for (i = 0; i < hival; i++) {
   2215 			int j;
   2216 			if (fcp->portdb[i].port_wwn == 0)
   2217 				continue;
   2218 			if (fcp->portdb[i].port_wwn !=
   2219 			    fcp->tport[loopid].port_wwn)
   2220 				continue;
   2221 			/*
   2222 			 * We found this WWN elsewhere- it's changed
   2223 			 * loopids then. We don't change it's actual
   2224 			 * position in our cached port database- we
   2225 			 * just change the actual loop ID we'd use.
   2226 			 */
   2227 			if (fcp->portdb[i].loopid != loopid) {
   2228 				isp_prt(isp, ISP_LOGINFO, portshift, i,
   2229 				    fcp->portdb[i].loopid,
   2230 				    fcp->portdb[i].portid, loopid,
   2231 				    fcp->tport[loopid].portid);
   2232 			}
   2233 			fcp->portdb[i].portid = fcp->tport[loopid].portid;
   2234 			fcp->portdb[i].loopid = loopid;
   2235 			fcp->portdb[i].valid = 1;
   2236 			fcp->portdb[i].roles = fcp->tport[loopid].roles;
   2237 
   2238 			/*
   2239 			 * Now make sure this Port WWN doesn't exist elsewhere
   2240 			 * in the port database.
   2241 			 */
   2242 			for (j = i+1; j < hival; j++) {
   2243 				if (fcp->portdb[i].port_wwn !=
   2244 				    fcp->portdb[j].port_wwn) {
   2245 					continue;
   2246 				}
   2247 				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
   2248 				/*
   2249 				 * Invalidate the 'old' *and* 'new' ones.
   2250 				 * This is really harsh and not quite right,
   2251 				 * but if this happens, we really don't know
   2252 				 * who is what at this point.
   2253 				 */
   2254 				fcp->portdb[i].valid = 0;
   2255 				fcp->portdb[j].valid = 0;
   2256 			}
   2257 			break;
   2258 		}
   2259 
   2260 		/*
   2261 		 * If we didn't traverse the entire port database,
   2262 		 * then we found (and remapped) an existing entry.
   2263 		 * No need to notify anyone- go for the next one.
   2264 		 */
   2265 		if (i < hival) {
   2266 			isp_prt(isp, ISP_LOGINFO, retained,
   2267 			    fcp->portdb[i].loopid, i, fcp->portdb[i].portid);
   2268 			continue;
   2269 		}
   2270 
   2271 		/*
   2272 		 * We've not found this Port WWN anywhere. It's a new entry.
   2273 		 * See if we can leave it where it is (with target == loopid).
   2274 		 */
   2275 		if (fcp->portdb[loopid].port_wwn != 0) {
   2276 			for (lim = 0; lim < hival; lim++) {
   2277 				if (fcp->portdb[lim].port_wwn == 0)
   2278 					break;
   2279 			}
   2280 			/* "Cannot Happen" */
   2281 			if (lim == hival) {
   2282 				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
   2283 				continue;
   2284 			}
   2285 			i = lim;
   2286 		} else {
   2287 			i = loopid;
   2288 		}
   2289 
   2290 		/*
   2291 		 * NB:	The actual loopid we use here is loopid- we may
   2292 		 *	in fact be at a completely different index (target).
   2293 		 */
   2294 		fcp->portdb[i].loopid = loopid;
   2295 		fcp->portdb[i].port_wwn = fcp->tport[loopid].port_wwn;
   2296 		fcp->portdb[i].node_wwn = fcp->tport[loopid].node_wwn;
   2297 		fcp->portdb[i].roles = fcp->tport[loopid].roles;
   2298 		fcp->portdb[i].portid = fcp->tport[loopid].portid;
   2299 		fcp->portdb[i].valid = 1;
   2300 
   2301 		/*
   2302 		 * Tell the outside world we've arrived.
   2303 		 */
   2304 		(void) isp_async(isp, ISPASYNC_PROMENADE, &i);
   2305 	}
   2306 
   2307 	/*
   2308 	 * Now find all previously used targets that are now invalid and
   2309 	 * notify the outer layers that they're gone.
   2310 	 */
   2311 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[hival]; lp++) {
   2312 		if (lp->valid || lp->port_wwn == 0) {
   2313 			continue;
   2314 		}
   2315 
   2316 		/*
   2317 		 * Tell the outside world we've gone
   2318 		 * away and erase our pdb entry.
   2319 		 *
   2320 		 */
   2321 		loopid = lp - fcp->portdb;
   2322 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
   2323 		MEMZERO((void *) lp, sizeof (*lp));
   2324 	}
   2325 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
   2326 	return (0);
   2327 }
   2328 
   2329 
   2330 static int
   2331 isp_fabric_mbox_cmd(struct ispsoftc *isp, mbreg_t *mbp)
   2332 {
   2333 	isp_mboxcmd(isp, mbp, MBLOGNONE);
   2334 	if (mbp->param[0] != MBOX_COMMAND_COMPLETE) {
   2335 		if (FCPARAM(isp)->isp_loopstate == LOOP_SCANNING_FABRIC) {
   2336 			FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   2337 		}
   2338 		if (mbp->param[0] == MBOX_COMMAND_ERROR) {
   2339 			char tbuf[16];
   2340 			char *m;
   2341 			switch (mbp->param[1]) {
   2342 			case 1:
   2343 				m = "No Loop";
   2344 				break;
   2345 			case 2:
   2346 				m = "Failed to allocate IOCB buffer";
   2347 				break;
   2348 			case 3:
   2349 				m = "Failed to allocate XCB buffer";
   2350 				break;
   2351 			case 4:
   2352 				m = "timeout or transmit failed";
   2353 				break;
   2354 			case 5:
   2355 				m = "no fabric loop";
   2356 				break;
   2357 			case 6:
   2358 				m = "remote device not a target";
   2359 				break;
   2360 			default:
   2361 				SNPRINTF(tbuf, sizeof tbuf, "%x",
   2362 				    mbp->param[1]);
   2363 				m = tbuf;
   2364 				break;
   2365 			}
   2366 			isp_prt(isp, ISP_LOGERR, "SNS Failed- %s", m);
   2367 		}
   2368 		return (-1);
   2369 	}
   2370 
   2371 	if (FCPARAM(isp)->isp_fwstate != FW_READY ||
   2372 	    FCPARAM(isp)->isp_loopstate < LOOP_SCANNING_FABRIC) {
   2373 		return (-1);
   2374 	}
   2375 	return(0);
   2376 }
   2377 
   2378 #ifdef	ISP_USE_GA_NXT
   2379 static int
   2380 isp_scan_fabric(struct ispsoftc *isp, int ftype)
   2381 {
   2382 	fcparam *fcp = isp->isp_param;
   2383 	u_int32_t portid, first_portid, last_portid;
   2384 	int hicap, last_port_same;
   2385 
   2386 	if (fcp->isp_onfabric == 0) {
   2387 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2388 		return (0);
   2389 	}
   2390 
   2391 	FC_SCRATCH_ACQUIRE(isp);
   2392 
   2393 	/*
   2394 	 * Since Port IDs are 24 bits, we can check against having seen
   2395 	 * anything yet with this value.
   2396 	 */
   2397 	last_port_same = 0;
   2398 	last_portid = 0xffffffff;	/* not a port */
   2399 	first_portid = portid = fcp->isp_portid;
   2400 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
   2401 
   2402 	for (hicap = 0; hicap < GA_NXT_MAX; hicap++) {
   2403 		mbreg_t mbs;
   2404 		sns_screq_t *rq;
   2405 		sns_ga_nxt_rsp_t *rs0, *rs1;
   2406 		struct lportdb lcl;
   2407 		u_int8_t sc[SNS_GA_NXT_RESP_SIZE];
   2408 
   2409 		rq = (sns_screq_t *)sc;
   2410 		MEMZERO((void *) rq, SNS_GA_NXT_REQ_SIZE);
   2411 		rq->snscb_rblen = SNS_GA_NXT_RESP_SIZE >> 1;
   2412 		rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+0x100);
   2413 		rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+0x100);
   2414 		rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+0x100);
   2415 		rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+0x100);
   2416 		rq->snscb_sblen = 6;
   2417 		rq->snscb_data[0] = SNS_GA_NXT;
   2418 		rq->snscb_data[4] = portid & 0xffff;
   2419 		rq->snscb_data[5] = (portid >> 16) & 0xff;
   2420 		isp_put_sns_request(isp, rq, (sns_screq_t *) fcp->isp_scratch);
   2421 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GA_NXT_REQ_SIZE);
   2422 		mbs.param[0] = MBOX_SEND_SNS;
   2423 		mbs.param[1] = SNS_GA_NXT_REQ_SIZE >> 1;
   2424 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2425 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2426 		/*
   2427 		 * Leave 4 and 5 alone
   2428 		 */
   2429 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2430 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2431 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
   2432 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
   2433 				fcp->isp_loopstate = LOOP_PDB_RCVD;
   2434 			}
   2435 			FC_SCRATCH_RELEASE(isp);
   2436 			return (-1);
   2437 		}
   2438 		MEMORYBARRIER(isp, SYNC_SFORCPU, 0x100, SNS_GA_NXT_RESP_SIZE);
   2439 		rs1 = (sns_ga_nxt_rsp_t *) sc;
   2440 		rs0 = (sns_ga_nxt_rsp_t *) ((u_int8_t *)fcp->isp_scratch+0x100);
   2441 		isp_get_ga_nxt_response(isp, rs0, rs1);
   2442 		if (rs1->snscb_cthdr.ct_response != FS_ACC) {
   2443 			isp_prt(isp, ISP_LOGWARN, swrej, "GA_NXT",
   2444 			    rs1->snscb_cthdr.ct_reason,
   2445 			    rs1->snscb_cthdr.ct_explanation, portid);
   2446 			FC_SCRATCH_RELEASE(isp);
   2447 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2448 			return (0);
   2449 		}
   2450 		portid =
   2451 		    (((u_int32_t) rs1->snscb_port_id[0]) << 16) |
   2452 		    (((u_int32_t) rs1->snscb_port_id[1]) << 8) |
   2453 		    (((u_int32_t) rs1->snscb_port_id[2]));
   2454 
   2455 		/*
   2456 		 * Okay, we now have information about a fabric object.
   2457 		 * If it is the type we're interested in, tell the outer layers
   2458 		 * about it. The outer layer needs to  know: Port ID, WWNN,
   2459 		 * WWPN, FC4 type, and port type.
   2460 		 *
   2461 		 * The lportdb structure is adequate for this.
   2462 		 */
   2463 		MEMZERO(&lcl, sizeof (lcl));
   2464 		lcl.port_type = rs1->snscb_port_type;
   2465 		lcl.fc4_type = ftype;
   2466 		lcl.portid = portid;
   2467 		lcl.node_wwn =
   2468 		    (((u_int64_t)rs1->snscb_nodename[0]) << 56) |
   2469 		    (((u_int64_t)rs1->snscb_nodename[1]) << 48) |
   2470 		    (((u_int64_t)rs1->snscb_nodename[2]) << 40) |
   2471 		    (((u_int64_t)rs1->snscb_nodename[3]) << 32) |
   2472 		    (((u_int64_t)rs1->snscb_nodename[4]) << 24) |
   2473 		    (((u_int64_t)rs1->snscb_nodename[5]) << 16) |
   2474 		    (((u_int64_t)rs1->snscb_nodename[6]) <<  8) |
   2475 		    (((u_int64_t)rs1->snscb_nodename[7]));
   2476 		lcl.port_wwn =
   2477 		    (((u_int64_t)rs1->snscb_portname[0]) << 56) |
   2478 		    (((u_int64_t)rs1->snscb_portname[1]) << 48) |
   2479 		    (((u_int64_t)rs1->snscb_portname[2]) << 40) |
   2480 		    (((u_int64_t)rs1->snscb_portname[3]) << 32) |
   2481 		    (((u_int64_t)rs1->snscb_portname[4]) << 24) |
   2482 		    (((u_int64_t)rs1->snscb_portname[5]) << 16) |
   2483 		    (((u_int64_t)rs1->snscb_portname[6]) <<  8) |
   2484 		    (((u_int64_t)rs1->snscb_portname[7]));
   2485 
   2486 		/*
   2487 		 * Does this fabric object support the type we want?
   2488 		 * If not, skip it.
   2489 		 */
   2490 		if (rs1->snscb_fc4_types[ftype >> 5] & (1 << (ftype & 0x1f))) {
   2491 			if (first_portid == portid) {
   2492 				lcl.last_fabric_dev = 1;
   2493 			} else {
   2494 				lcl.last_fabric_dev = 0;
   2495 			}
   2496 			(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
   2497 		} else {
   2498 			isp_prt(isp, ISP_LOGDEBUG0,
   2499 			    "PortID 0x%x doesn't support FC4 type 0x%x",
   2500 			    portid, ftype);
   2501 		}
   2502 		if (first_portid == portid) {
   2503 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2504 			FC_SCRATCH_RELEASE(isp);
   2505 			return (0);
   2506 		}
   2507 		if (portid == last_portid) {
   2508 			if (last_port_same++ > 20) {
   2509 				isp_prt(isp, ISP_LOGWARN,
   2510 				    "tangled fabric database detected");
   2511 				break;
   2512 			}
   2513 		} else {
   2514 			last_port_same = 0 ;
   2515 			last_portid = portid;
   2516 		}
   2517 	}
   2518 	FC_SCRATCH_RELEASE(isp);
   2519 	if (hicap >= GA_NXT_MAX) {
   2520 		isp_prt(isp, ISP_LOGWARN, "fabric too big (> %d)", GA_NXT_MAX);
   2521 	}
   2522 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2523 	return (0);
   2524 }
   2525 #else
   2526 #define	GIDLEN	((ISP2100_SCRLEN >> 1) + 16)
   2527 #define	NGENT	((GIDLEN - 16) >> 2)
   2528 
   2529 #define	IGPOFF	(ISP2100_SCRLEN - GIDLEN)
   2530 #define	GXOFF	(256)
   2531 
   2532 static int
   2533 isp_scan_fabric(struct ispsoftc *isp, int ftype)
   2534 {
   2535 	fcparam *fcp = FCPARAM(isp);
   2536 	mbreg_t mbs;
   2537 	int i;
   2538 	sns_gid_ft_req_t *rq;
   2539 	sns_gid_ft_rsp_t *rs0, *rs1;
   2540 
   2541 	if (fcp->isp_onfabric == 0) {
   2542 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2543 		return (0);
   2544 	}
   2545 
   2546 	FC_SCRATCH_ACQUIRE(isp);
   2547 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
   2548 
   2549 	rq = (sns_gid_ft_req_t *)fcp->tport;
   2550 	MEMZERO((void *) rq, SNS_GID_FT_REQ_SIZE);
   2551 	rq->snscb_rblen = GIDLEN >> 1;
   2552 	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+IGPOFF);
   2553 	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+IGPOFF);
   2554 	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+IGPOFF);
   2555 	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+IGPOFF);
   2556 	rq->snscb_sblen = 6;
   2557 	rq->snscb_cmd = SNS_GID_FT;
   2558 	rq->snscb_mword_div_2 = NGENT;
   2559 	rq->snscb_fc4_type = ftype;
   2560 	isp_put_gid_ft_request(isp, rq, (sns_gid_ft_req_t *) fcp->isp_scratch);
   2561 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE);
   2562 	mbs.param[0] = MBOX_SEND_SNS;
   2563 	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
   2564 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2565 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2566 
   2567 	/*
   2568 	 * Leave 4 and 5 alone
   2569 	 */
   2570 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2571 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2572 	if (isp_fabric_mbox_cmd(isp, &mbs)) {
   2573 		if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
   2574 			fcp->isp_loopstate = LOOP_PDB_RCVD;
   2575 		}
   2576 		FC_SCRATCH_RELEASE(isp);
   2577 		return (-1);
   2578 	}
   2579 	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   2580 		FC_SCRATCH_RELEASE(isp);
   2581 		return (-1);
   2582 	}
   2583 	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN);
   2584 	rs1 = (sns_gid_ft_rsp_t *) fcp->tport;
   2585 	rs0 = (sns_gid_ft_rsp_t *) ((u_int8_t *)fcp->isp_scratch+IGPOFF);
   2586 	isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
   2587 	if (rs1->snscb_cthdr.ct_response != FS_ACC) {
   2588 		isp_prt(isp, ISP_LOGWARN, swrej, "GID_FT",
   2589 		    rs1->snscb_cthdr.ct_reason,
   2590 		    rs1->snscb_cthdr.ct_explanation, 0);
   2591 		FC_SCRATCH_RELEASE(isp);
   2592 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2593 		return (0);
   2594 	}
   2595 
   2596 	/*
   2597 	 * Okay, we now have a list of Port IDs for this class of device.
   2598 	 * Go through the list and for each one get the WWPN/WWNN for it
   2599 	 * and tell the outer layers about it. The outer layer needs to
   2600 	 * know: Port ID, WWNN, WWPN, FC4 type, and (possibly) port type.
   2601 	 *
   2602 	 * The lportdb structure is adequate for this.
   2603 	 */
   2604 	i = -1;
   2605 	do {
   2606 		sns_gxn_id_req_t grqbuf, *gq = &grqbuf;
   2607 		sns_gxn_id_rsp_t *gs0, grsbuf, *gs1 = &grsbuf;
   2608 		struct lportdb lcl;
   2609 #if	0
   2610 		sns_gff_id_rsp_t *fs0, ffsbuf, *fs1 = &ffsbuf;
   2611 #endif
   2612 
   2613 		i++;
   2614 		MEMZERO(&lcl, sizeof (lcl));
   2615 		lcl.fc4_type = ftype;
   2616 		lcl.portid =
   2617 		    (((u_int32_t) rs1->snscb_ports[i].portid[0]) << 16) |
   2618 		    (((u_int32_t) rs1->snscb_ports[i].portid[1]) << 8) |
   2619 		    (((u_int32_t) rs1->snscb_ports[i].portid[2]));
   2620 
   2621 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
   2622 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
   2623 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
   2624 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
   2625 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
   2626 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
   2627 		gq->snscb_sblen = 6;
   2628 		gq->snscb_cmd = SNS_GPN_ID;
   2629 		gq->snscb_portid = lcl.portid;
   2630 		isp_put_gxn_id_request(isp, gq,
   2631 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
   2632 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
   2633 		mbs.param[0] = MBOX_SEND_SNS;
   2634 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
   2635 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2636 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2637 		/*
   2638 		 * Leave 4 and 5 alone
   2639 		 */
   2640 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2641 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2642 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
   2643 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
   2644 				fcp->isp_loopstate = LOOP_PDB_RCVD;
   2645 			}
   2646 			FC_SCRATCH_RELEASE(isp);
   2647 			return (-1);
   2648 		}
   2649 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   2650 			FC_SCRATCH_RELEASE(isp);
   2651 			return (-1);
   2652 		}
   2653 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
   2654 		gs0 = (sns_gxn_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
   2655 		isp_get_gxn_id_response(isp, gs0, gs1);
   2656 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
   2657 			isp_prt(isp, ISP_LOGWARN, swrej, "GPN_ID",
   2658 			    gs1->snscb_cthdr.ct_reason,
   2659 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
   2660 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   2661 				FC_SCRATCH_RELEASE(isp);
   2662 				return (-1);
   2663 			}
   2664 			continue;
   2665 		}
   2666 		lcl.port_wwn =
   2667 		    (((u_int64_t)gs1->snscb_wwn[0]) << 56) |
   2668 		    (((u_int64_t)gs1->snscb_wwn[1]) << 48) |
   2669 		    (((u_int64_t)gs1->snscb_wwn[2]) << 40) |
   2670 		    (((u_int64_t)gs1->snscb_wwn[3]) << 32) |
   2671 		    (((u_int64_t)gs1->snscb_wwn[4]) << 24) |
   2672 		    (((u_int64_t)gs1->snscb_wwn[5]) << 16) |
   2673 		    (((u_int64_t)gs1->snscb_wwn[6]) <<  8) |
   2674 		    (((u_int64_t)gs1->snscb_wwn[7]));
   2675 
   2676 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
   2677 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
   2678 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
   2679 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
   2680 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
   2681 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
   2682 		gq->snscb_sblen = 6;
   2683 		gq->snscb_cmd = SNS_GNN_ID;
   2684 		gq->snscb_portid = lcl.portid;
   2685 		isp_put_gxn_id_request(isp, gq,
   2686 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
   2687 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
   2688 		mbs.param[0] = MBOX_SEND_SNS;
   2689 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
   2690 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2691 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2692 		/*
   2693 		 * Leave 4 and 5 alone
   2694 		 */
   2695 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2696 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2697 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
   2698 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
   2699 				fcp->isp_loopstate = LOOP_PDB_RCVD;
   2700 			}
   2701 			FC_SCRATCH_RELEASE(isp);
   2702 			return (-1);
   2703 		}
   2704 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   2705 			FC_SCRATCH_RELEASE(isp);
   2706 			return (-1);
   2707 		}
   2708 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
   2709 		gs0 = (sns_gxn_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
   2710 		isp_get_gxn_id_response(isp, gs0, gs1);
   2711 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
   2712 			isp_prt(isp, ISP_LOGWARN, swrej, "GNN_ID",
   2713 			    gs1->snscb_cthdr.ct_reason,
   2714 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
   2715 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   2716 				FC_SCRATCH_RELEASE(isp);
   2717 				return (-1);
   2718 			}
   2719 			continue;
   2720 		}
   2721 		lcl.node_wwn =
   2722 		    (((u_int64_t)gs1->snscb_wwn[0]) << 56) |
   2723 		    (((u_int64_t)gs1->snscb_wwn[1]) << 48) |
   2724 		    (((u_int64_t)gs1->snscb_wwn[2]) << 40) |
   2725 		    (((u_int64_t)gs1->snscb_wwn[3]) << 32) |
   2726 		    (((u_int64_t)gs1->snscb_wwn[4]) << 24) |
   2727 		    (((u_int64_t)gs1->snscb_wwn[5]) << 16) |
   2728 		    (((u_int64_t)gs1->snscb_wwn[6]) <<  8) |
   2729 		    (((u_int64_t)gs1->snscb_wwn[7]));
   2730 
   2731 		/*
   2732 		 * The QLogic f/w is bouncing this with a parameter error.
   2733 		 */
   2734 #if	0
   2735 		/*
   2736 		 * Try and get FC4 Features (FC-GS-3 only).
   2737 		 * We can use the sns_gxn_id_req_t for this request.
   2738 		 */
   2739 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
   2740 		gq->snscb_rblen = SNS_GFF_ID_RESP_SIZE >> 1;
   2741 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
   2742 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
   2743 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
   2744 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
   2745 		gq->snscb_sblen = 6;
   2746 		gq->snscb_cmd = SNS_GFF_ID;
   2747 		gq->snscb_portid = lcl.portid;
   2748 		isp_put_gxn_id_request(isp, gq,
   2749 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
   2750 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
   2751 		mbs.param[0] = MBOX_SEND_SNS;
   2752 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
   2753 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2754 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2755 		/*
   2756 		 * Leave 4 and 5 alone
   2757 		 */
   2758 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2759 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2760 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
   2761 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
   2762 				fcp->isp_loopstate = LOOP_PDB_RCVD;
   2763 			}
   2764 			FC_SCRATCH_RELEASE(isp);
   2765 			return (-1);
   2766 		}
   2767 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   2768 			FC_SCRATCH_RELEASE(isp);
   2769 			return (-1);
   2770 		}
   2771 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GFF_ID_RESP_SIZE);
   2772 		fs0 = (sns_gff_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
   2773 		isp_get_gff_id_response(isp, fs0, fs1);
   2774 		if (fs1->snscb_cthdr.ct_response != FS_ACC) {
   2775 			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGWARN,
   2776 			    swrej, "GFF_ID",
   2777 			    fs1->snscb_cthdr.ct_reason,
   2778 			    fs1->snscb_cthdr.ct_explanation, lcl.portid);
   2779 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   2780 				FC_SCRATCH_RELEASE(isp);
   2781 				return (-1);
   2782 			}
   2783 		} else {
   2784 			int index = (ftype >> 3);
   2785 			int bshft = (ftype & 0x7) * 4;
   2786 			int fc4_fval =
   2787 			    (fs1->snscb_fc4_features[index] >> bshft) & 0xf;
   2788 			if (fc4_fval & 0x1) {
   2789 				lcl.roles |=
   2790 				    (SVC3_INI_ROLE >> SVC3_ROLE_SHIFT);
   2791 			}
   2792 			if (fc4_fval & 0x2) {
   2793 				lcl.roles |=
   2794 				    (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
   2795 			}
   2796 		}
   2797 #endif
   2798 
   2799 		/*
   2800 		 * If we really want to know what kind of port type this is,
   2801 		 * we have to run another CT command. Otherwise, we'll leave
   2802 		 * it as undefined.
   2803 		 *
   2804 		lcl.port_type = 0;
   2805 		 */
   2806 		if (rs1->snscb_ports[i].control & 0x80) {
   2807 			lcl.last_fabric_dev = 1;
   2808 		} else {
   2809 			lcl.last_fabric_dev = 0;
   2810 		}
   2811 		(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
   2812 
   2813 	} while ((rs1->snscb_ports[i].control & 0x80) == 0 && i < NGENT-1);
   2814 
   2815 	/*
   2816 	 * If we're not at the last entry, our list isn't big enough.
   2817 	 */
   2818 	if ((rs1->snscb_ports[i].control & 0x80) == 0) {
   2819 		isp_prt(isp, ISP_LOGWARN, "fabric too big for scratch area");
   2820 	}
   2821 
   2822 	FC_SCRATCH_RELEASE(isp);
   2823 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2824 	return (0);
   2825 }
   2826 #endif
   2827 
   2828 static void
   2829 isp_register_fc4_type(struct ispsoftc *isp)
   2830 {
   2831 	fcparam *fcp = isp->isp_param;
   2832 	u_int8_t local[SNS_RFT_ID_REQ_SIZE];
   2833 	sns_screq_t *reqp = (sns_screq_t *) local;
   2834 	mbreg_t mbs;
   2835 
   2836 	MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
   2837 	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
   2838 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
   2839 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
   2840 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
   2841 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
   2842 	reqp->snscb_sblen = 22;
   2843 	reqp->snscb_data[0] = SNS_RFT_ID;
   2844 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
   2845 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
   2846 	reqp->snscb_data[6] = (1 << FC4_SCSI);
   2847 #if	0
   2848 	reqp->snscb_data[6] |= (1 << FC4_IP);	/* ISO/IEC 8802-2 LLC/SNAP */
   2849 #endif
   2850 	FC_SCRATCH_ACQUIRE(isp);
   2851 	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
   2852 	mbs.param[0] = MBOX_SEND_SNS;
   2853 	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
   2854 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2855 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2856 	/*
   2857 	 * Leave 4 and 5 alone
   2858 	 */
   2859 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2860 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2861 	isp_mboxcmd(isp, &mbs, MBLOGALL);
   2862 	FC_SCRATCH_RELEASE(isp);
   2863 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   2864 		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
   2865 	}
   2866 }
   2867 
   2868 /*
   2869  * Start a command. Locking is assumed done in the caller.
   2870  */
   2871 
   2872 int
   2873 isp_start(XS_T *xs)
   2874 {
   2875 	struct ispsoftc *isp;
   2876 	u_int16_t nxti, optr, handle;
   2877 	u_int8_t local[QENTRY_LEN];
   2878 	ispreq_t *reqp, *qep;
   2879 	int target, i;
   2880 
   2881 	XS_INITERR(xs);
   2882 	isp = XS_ISP(xs);
   2883 
   2884 	/*
   2885 	 * Check to make sure we're supporting initiator role.
   2886 	 */
   2887 	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
   2888 		XS_SETERR(xs, HBA_SELTIMEOUT);
   2889 		return (CMD_COMPLETE);
   2890 	}
   2891 
   2892 	/*
   2893 	 * Now make sure we're running.
   2894 	 */
   2895 
   2896 	if (isp->isp_state != ISP_RUNSTATE) {
   2897 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
   2898 		XS_SETERR(xs, HBA_BOTCH);
   2899 		return (CMD_COMPLETE);
   2900 	}
   2901 
   2902 	/*
   2903 	 * Check command CDB length, etc.. We really are limited to 16 bytes
   2904 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
   2905 	 * but probably only if we're running fairly new firmware (we'll
   2906 	 * let the old f/w choke on an extended command queue entry).
   2907 	 */
   2908 
   2909 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
   2910 		isp_prt(isp, ISP_LOGERR,
   2911 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
   2912 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
   2913 		XS_SETERR(xs, HBA_BOTCH);
   2914 		return (CMD_COMPLETE);
   2915 	}
   2916 
   2917 	/*
   2918 	 * Check to see whether we have good firmware state still or
   2919 	 * need to refresh our port database for this target.
   2920 	 */
   2921 	target = XS_TGT(xs);
   2922 	if (IS_FC(isp)) {
   2923 		fcparam *fcp = isp->isp_param;
   2924 		struct lportdb *lp;
   2925 #ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
   2926 		if (fcp->isp_fwstate != FW_READY ||
   2927 		    fcp->isp_loopstate != LOOP_READY) {
   2928 			return (CMD_RQLATER);
   2929 		}
   2930 
   2931 		/*
   2932 		 * If we're not on a Fabric, we can't have a target
   2933 		 * above FL_PORT_ID-1.
   2934 		 *
   2935 		 * If we're on a fabric and *not* connected as an F-port,
   2936 		 * we can't have a target less than FC_SNS_ID+1. This
   2937 		 * keeps us from having to sort out the difference between
   2938 		 * local public loop devices and those which we might get
   2939 		 * from a switch's database.
   2940 		 */
   2941 		if (fcp->isp_onfabric == 0) {
   2942 			if (target >= FL_PORT_ID) {
   2943 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2944 				return (CMD_COMPLETE);
   2945 			}
   2946 		} else {
   2947 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
   2948 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2949 				return (CMD_COMPLETE);
   2950 			}
   2951 			/*
   2952 			 * We used to exclude having local loop ports
   2953 			 * at the same time that we have fabric ports.
   2954 			 * That is, we used to exclude having ports
   2955 			 * at < FL_PORT_ID if we're FL-port.
   2956 			 *
   2957 			 * That's wrong. The only thing that could be
   2958 			 * dicey is if the switch you're connected to
   2959 			 * has these local loop ports appear on the
   2960 			 * fabric and we somehow attach them twice.
   2961 			 */
   2962 		}
   2963 #else
   2964 		/*
   2965 		 * Check for f/w being in ready state. If the f/w
   2966 		 * isn't in ready state, then we don't know our
   2967 		 * loop ID and the f/w hasn't completed logging
   2968 		 * into all targets on the loop. If this is the
   2969 		 * case, then bounce the command. We pretend this is
   2970 		 * a SELECTION TIMEOUT error if we've never gone to
   2971 		 * FW_READY state at all- in this case we may not
   2972 		 * be hooked to a loop at all and we shouldn't hang
   2973 		 * the machine for this. Otherwise, defer this command
   2974 		 * until later.
   2975 		 */
   2976 		if (fcp->isp_fwstate != FW_READY) {
   2977 			/*
   2978 			 * Give ourselves at most a 250ms delay.
   2979 			 */
   2980 			if (isp_fclink_test(isp, 250000)) {
   2981 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2982 				if (fcp->loop_seen_once) {
   2983 					return (CMD_RQLATER);
   2984 				} else {
   2985 					return (CMD_COMPLETE);
   2986 				}
   2987 			}
   2988 		}
   2989 
   2990 		/*
   2991 		 * If we're not on a Fabric, we can't have a target
   2992 		 * above FL_PORT_ID-1.
   2993 		 *
   2994 		 * If we're on a fabric and *not* connected as an F-port,
   2995 		 * we can't have a target less than FC_SNS_ID+1. This
   2996 		 * keeps us from having to sort out the difference between
   2997 		 * local public loop devices and those which we might get
   2998 		 * from a switch's database.
   2999 		 */
   3000 		if (fcp->isp_onfabric == 0) {
   3001 			if (target >= FL_PORT_ID) {
   3002 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3003 				return (CMD_COMPLETE);
   3004 			}
   3005 		} else {
   3006 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
   3007 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3008 				return (CMD_COMPLETE);
   3009 			}
   3010 			if (fcp->isp_topo != TOPO_F_PORT &&
   3011 			    target < FL_PORT_ID) {
   3012 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3013 				return (CMD_COMPLETE);
   3014 			}
   3015 		}
   3016 
   3017 		/*
   3018 		 * If our loop state is such that we haven't yet received
   3019 		 * a "Port Database Changed" notification (after a LIP or
   3020 		 * a Loop Reset or firmware initialization), then defer
   3021 		 * sending commands for a little while, but only if we've
   3022 		 * seen a valid loop at one point (otherwise we can get
   3023 		 * stuck at initialization time).
   3024 		 */
   3025 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
   3026 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3027 			if (fcp->loop_seen_once) {
   3028 				return (CMD_RQLATER);
   3029 			} else {
   3030 				return (CMD_COMPLETE);
   3031 			}
   3032 		}
   3033 
   3034 		/*
   3035 		 * If we're in the middle of loop or fabric scanning
   3036 		 * or merging the port databases, retry this command later.
   3037 		 */
   3038 		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
   3039 		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
   3040 		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
   3041 			return (CMD_RQLATER);
   3042 		}
   3043 
   3044 		/*
   3045 		 * If our loop state is now such that we've just now
   3046 		 * received a Port Database Change notification, then
   3047 		 * we have to go off and (re)scan the fabric. We back
   3048 		 * out and try again later if this doesn't work.
   3049 		 */
   3050 		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
   3051 			if (isp_scan_fabric(isp, FC4_SCSI)) {
   3052 				return (CMD_RQLATER);
   3053 			}
   3054 			if (fcp->isp_fwstate != FW_READY ||
   3055 			    fcp->isp_loopstate < LOOP_PDB_RCVD) {
   3056 				return (CMD_RQLATER);
   3057 			}
   3058 		}
   3059 
   3060 		/*
   3061 		 * If our loop state is now such that we've just now
   3062 		 * received a Port Database Change notification, then
   3063 		 * we have to go off and (re)synchronize our port
   3064 		 * database.
   3065 		 */
   3066 		if (fcp->isp_loopstate < LOOP_READY) {
   3067 			if (isp_pdb_sync(isp)) {
   3068 				return (CMD_RQLATER);
   3069 			}
   3070 			if (fcp->isp_fwstate != FW_READY ||
   3071 			    fcp->isp_loopstate != LOOP_READY) {
   3072 				return (CMD_RQLATER);
   3073 			}
   3074 		}
   3075 
   3076 		/*
   3077 		 * XXX: Here's were we would cancel any loop_dead flag
   3078 		 * XXX: also cancel in dead_loop timeout that's running
   3079 		 */
   3080 #endif
   3081 
   3082 		/*
   3083 		 * Now check whether we should even think about pursuing this.
   3084 		 */
   3085 		lp = &fcp->portdb[target];
   3086 		if (lp->valid == 0) {
   3087 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3088 			return (CMD_COMPLETE);
   3089 		}
   3090 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
   3091 			isp_prt(isp, ISP_LOGDEBUG2,
   3092 			    "Target %d does not have target service", target);
   3093 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3094 			return (CMD_COMPLETE);
   3095 		}
   3096 		/*
   3097 		 * Now turn target into what the actual Loop ID is.
   3098 		 */
   3099 		target = lp->loopid;
   3100 	}
   3101 
   3102 	/*
   3103 	 * Next check to see if any HBA or Device
   3104 	 * parameters need to be updated.
   3105 	 */
   3106 	if (isp->isp_update != 0) {
   3107 		isp_update(isp);
   3108 	}
   3109 
   3110 	if (isp_getrqentry(isp, &nxti, &optr, (void **)&qep)) {
   3111 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
   3112 		XS_SETERR(xs, HBA_BOTCH);
   3113 		return (CMD_EAGAIN);
   3114 	}
   3115 
   3116 	/*
   3117 	 * Now see if we need to synchronize the ISP with respect to anything.
   3118 	 * We do dual duty here (cough) for synchronizing for busses other
   3119 	 * than which we got here to send a command to.
   3120 	 */
   3121 	reqp = (ispreq_t *) local;
   3122 	if (isp->isp_sendmarker) {
   3123 		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
   3124 		/*
   3125 		 * Check ports to send markers for...
   3126 		 */
   3127 		for (i = 0; i < n; i++) {
   3128 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
   3129 				continue;
   3130 			}
   3131 			MEMZERO((void *) reqp, QENTRY_LEN);
   3132 			reqp->req_header.rqs_entry_count = 1;
   3133 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
   3134 			reqp->req_modifier = SYNC_ALL;
   3135 			reqp->req_target = i << 7;	/* insert bus number */
   3136 			isp_put_request(isp, reqp, qep);
   3137 			ISP_ADD_REQUEST(isp, nxti);
   3138 			isp->isp_sendmarker &= ~(1 << i);
   3139 			if (isp_getrqentry(isp, &nxti, &optr, (void **) &qep)) {
   3140 				isp_prt(isp, ISP_LOGDEBUG0,
   3141 				    "Request Queue Overflow+");
   3142 				XS_SETERR(xs, HBA_BOTCH);
   3143 				return (CMD_EAGAIN);
   3144 			}
   3145 		}
   3146 	}
   3147 
   3148 	MEMZERO((void *)reqp, QENTRY_LEN);
   3149 	reqp->req_header.rqs_entry_count = 1;
   3150 	if (IS_FC(isp)) {
   3151 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
   3152 	} else {
   3153 		if (XS_CDBLEN(xs) > 12)
   3154 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
   3155 		else
   3156 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
   3157 	}
   3158 	/* reqp->req_header.rqs_flags = 0; */
   3159 	/* reqp->req_header.rqs_seqno = 0; */
   3160 	if (IS_FC(isp)) {
   3161 		/*
   3162 		 * See comment in isp_intr
   3163 		 */
   3164 		/* XS_RESID(xs) = 0; */
   3165 
   3166 		/*
   3167 		 * Fibre Channel always requires some kind of tag.
   3168 		 * The Qlogic drivers seem be happy not to use a tag,
   3169 		 * but this breaks for some devices (IBM drives).
   3170 		 */
   3171 		if (XS_TAG_P(xs)) {
   3172 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
   3173 		} else {
   3174 			/*
   3175 			 * If we don't know what tag to use, use HEAD OF QUEUE
   3176 			 * for Request Sense or Simple.
   3177 			 */
   3178 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
   3179 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
   3180 			else
   3181 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
   3182 		}
   3183 	} else {
   3184 		sdparam *sdp = (sdparam *)isp->isp_param;
   3185 		sdp += XS_CHANNEL(xs);
   3186 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
   3187 		    XS_TAG_P(xs)) {
   3188 			reqp->req_flags = XS_TAG_TYPE(xs);
   3189 		}
   3190 	}
   3191 	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
   3192 	if (IS_SCSI(isp)) {
   3193 		reqp->req_lun_trn = XS_LUN(xs);
   3194 		reqp->req_cdblen = XS_CDBLEN(xs);
   3195 	} else {
   3196 		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)
   3197 			((ispreqt2_t *)reqp)->req_scclun = XS_LUN(xs);
   3198 		else
   3199 			((ispreqt2_t *)reqp)->req_lun_trn = XS_LUN(xs);
   3200 	}
   3201 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
   3202 
   3203 	reqp->req_time = XS_TIME(xs) / 1000;
   3204 	if (reqp->req_time == 0 && XS_TIME(xs)) {
   3205 		reqp->req_time = 1;
   3206 	}
   3207 
   3208 	if (isp_save_xs(isp, xs, &handle)) {
   3209 		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
   3210 		XS_SETERR(xs, HBA_BOTCH);
   3211 		return (CMD_EAGAIN);
   3212 	}
   3213 	reqp->req_handle = handle;
   3214 
   3215 	/*
   3216 	 * Set up DMA and/or do any bus swizzling of the request entry
   3217 	 * so that the Qlogic F/W understands what is being asked of it.
   3218 	 */
   3219 	i = ISP_DMASETUP(isp, xs, reqp, &nxti, optr);
   3220 	if (i != CMD_QUEUED) {
   3221 		isp_destroy_handle(isp, handle);
   3222 		/*
   3223 		 * dmasetup sets actual error in packet, and
   3224 		 * return what we were given to return.
   3225 		 */
   3226 		return (i);
   3227 	}
   3228 	XS_SETERR(xs, HBA_NOERROR);
   3229 	isp_prt(isp, ISP_LOGDEBUG2,
   3230 	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
   3231 	    XS_CHANNEL(xs), target, XS_LUN(xs), XS_CDBP(xs)[0],
   3232 	    (long) XS_XFRLEN(xs));
   3233 	ISP_ADD_REQUEST(isp, nxti);
   3234 	isp->isp_nactive++;
   3235 	return (CMD_QUEUED);
   3236 }
   3237 
   3238 /*
   3239  * isp control
   3240  * Locks (ints blocked) assumed held.
   3241  */
   3242 
   3243 int
   3244 isp_control(struct ispsoftc *isp, ispctl_t ctl, void *arg)
   3245 {
   3246 	XS_T *xs;
   3247 	mbreg_t mbs;
   3248 	int bus, tgt;
   3249 	u_int16_t handle;
   3250 
   3251 	switch (ctl) {
   3252 	default:
   3253 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
   3254 		break;
   3255 
   3256 	case ISPCTL_RESET_BUS:
   3257 		/*
   3258 		 * Issue a bus reset.
   3259 		 */
   3260 		mbs.param[0] = MBOX_BUS_RESET;
   3261 		mbs.param[2] = 0;
   3262 		if (IS_SCSI(isp)) {
   3263 			mbs.param[1] =
   3264 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
   3265 			if (mbs.param[1] < 2)
   3266 				mbs.param[1] = 2;
   3267 			bus = *((int *) arg);
   3268 			if (IS_DUALBUS(isp))
   3269 				mbs.param[2] = bus;
   3270 		} else {
   3271 			mbs.param[1] = 10;
   3272 			bus = 0;
   3273 		}
   3274 		isp->isp_sendmarker |= (1 << bus);
   3275 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   3276 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3277 			break;
   3278 		}
   3279 		isp_prt(isp, ISP_LOGINFO,
   3280 		    "driver initiated bus reset of bus %d", bus);
   3281 		return (0);
   3282 
   3283 	case ISPCTL_RESET_DEV:
   3284 		tgt = (*((int *) arg)) & 0xffff;
   3285 		bus = (*((int *) arg)) >> 16;
   3286 		mbs.param[0] = MBOX_ABORT_TARGET;
   3287 		mbs.param[1] = (tgt << 8) | (bus << 15);
   3288 		mbs.param[2] = 3;	/* 'delay', in seconds */
   3289 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   3290 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3291 			break;
   3292 		}
   3293 		isp_prt(isp, ISP_LOGINFO,
   3294 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
   3295 		isp->isp_sendmarker |= (1 << bus);
   3296 		return (0);
   3297 
   3298 	case ISPCTL_ABORT_CMD:
   3299 		xs = (XS_T *) arg;
   3300 		tgt = XS_TGT(xs);
   3301 		handle = isp_find_handle(isp, xs);
   3302 		if (handle == 0) {
   3303 			isp_prt(isp, ISP_LOGWARN,
   3304 			    "cannot find handle for command to abort");
   3305 			break;
   3306 		}
   3307 		bus = XS_CHANNEL(xs);
   3308 		mbs.param[0] = MBOX_ABORT;
   3309 		if (IS_FC(isp)) {
   3310 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)  {
   3311 				mbs.param[1] = tgt << 8;
   3312 				mbs.param[4] = 0;
   3313 				mbs.param[5] = 0;
   3314 				mbs.param[6] = XS_LUN(xs);
   3315 			} else {
   3316 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
   3317 			}
   3318 		} else {
   3319 			mbs.param[1] =
   3320 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
   3321 		}
   3322 		mbs.param[3] = 0;
   3323 		mbs.param[2] = handle;
   3324 		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
   3325 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   3326 			return (0);
   3327 		}
   3328 		/*
   3329 		 * XXX: Look for command in the REQUEST QUEUE. That is,
   3330 		 * XXX: It hasen't been picked up by firmware yet.
   3331 		 */
   3332 		break;
   3333 
   3334 	case ISPCTL_UPDATE_PARAMS:
   3335 
   3336 		isp_update(isp);
   3337 		return (0);
   3338 
   3339 	case ISPCTL_FCLINK_TEST:
   3340 
   3341 		if (IS_FC(isp)) {
   3342 			int usdelay = (arg)? *((int *) arg) : 250000;
   3343 			return (isp_fclink_test(isp, usdelay));
   3344 		}
   3345 		break;
   3346 
   3347 	case ISPCTL_SCAN_FABRIC:
   3348 
   3349 		if (IS_FC(isp)) {
   3350 			int ftype = (arg)? *((int *) arg) : FC4_SCSI;
   3351 			return (isp_scan_fabric(isp, ftype));
   3352 		}
   3353 		break;
   3354 
   3355 	case ISPCTL_SCAN_LOOP:
   3356 
   3357 		if (IS_FC(isp)) {
   3358 			return (isp_scan_loop(isp));
   3359 		}
   3360 		break;
   3361 
   3362 	case ISPCTL_PDB_SYNC:
   3363 
   3364 		if (IS_FC(isp)) {
   3365 			return (isp_pdb_sync(isp));
   3366 		}
   3367 		break;
   3368 
   3369 	case ISPCTL_SEND_LIP:
   3370 
   3371 		if (IS_FC(isp)) {
   3372 			mbs.param[0] = MBOX_INIT_LIP;
   3373 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   3374 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   3375 				return (0);
   3376 			}
   3377 		}
   3378 		break;
   3379 
   3380 	case ISPCTL_GET_POSMAP:
   3381 
   3382 		if (IS_FC(isp) && arg) {
   3383 			return (isp_getmap(isp, arg));
   3384 		}
   3385 		break;
   3386 
   3387 	case ISPCTL_RUN_MBOXCMD:
   3388 
   3389 		isp_mboxcmd(isp, arg, MBLOGALL);
   3390 		return(0);
   3391 
   3392 #ifdef	ISP_TARGET_MODE
   3393 	case ISPCTL_TOGGLE_TMODE:
   3394 	{
   3395 
   3396 		/*
   3397 		 * We don't check/set against role here- that's the
   3398 		 * responsibility for the outer layer to coordinate.
   3399 		 */
   3400 		if (IS_SCSI(isp)) {
   3401 			int param = *(int *)arg;
   3402 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
   3403 			mbs.param[1] = param & 0xffff;
   3404 			mbs.param[2] = param >> 16;
   3405 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   3406 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3407 				break;
   3408 			}
   3409 		}
   3410 		return (0);
   3411 	}
   3412 #endif
   3413 	}
   3414 	return (-1);
   3415 }
   3416 
   3417 /*
   3418  * Interrupt Service Routine(s).
   3419  *
   3420  * External (OS) framework has done the appropriate locking,
   3421  * and the locking will be held throughout this function.
   3422  */
   3423 
   3424 /*
   3425  * Limit our stack depth by sticking with the max likely number
   3426  * of completions on a request queue at any one time.
   3427  */
   3428 #ifndef	MAX_REQUESTQ_COMPLETIONS
   3429 #define	MAX_REQUESTQ_COMPLETIONS	64
   3430 #endif
   3431 
   3432 void
   3433 isp_intr(struct ispsoftc *isp, u_int16_t isr, u_int16_t sema, u_int16_t mbox)
   3434 {
   3435 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
   3436 	u_int16_t iptr, optr, junk;
   3437 	int i, nlooked = 0, ndone = 0;
   3438 
   3439 again:
   3440 	/*
   3441 	 * Is this a mailbox related interrupt?
   3442 	 * The mailbox semaphore will be nonzero if so.
   3443 	 */
   3444 	if (sema) {
   3445 		if (mbox & 0x4000) {
   3446 			isp->isp_intmboxc++;
   3447 			if (isp->isp_mboxbsy) {
   3448 				int i = 0, obits = isp->isp_obits;
   3449 				isp->isp_mboxtmp[i++] = mbox;
   3450 				for (i = 1; i < MAX_MAILBOX; i++) {
   3451 					if ((obits & (1 << i)) == 0) {
   3452 						continue;
   3453 					}
   3454 					isp->isp_mboxtmp[i] =
   3455 					    ISP_READ(isp, MBOX_OFF(i));
   3456 				}
   3457 				if (isp->isp_mbxwrk0) {
   3458 					if (isp_mbox_continue(isp) == 0) {
   3459 						return;
   3460 					}
   3461 				}
   3462 				MBOX_NOTIFY_COMPLETE(isp);
   3463 			} else {
   3464 				isp_prt(isp, ISP_LOGWARN,
   3465 				    "Mbox Command Async (0x%x) with no waiters",
   3466 				    mbox);
   3467 			}
   3468 		} else if (isp_parse_async(isp, mbox) < 0) {
   3469 			return;
   3470 		}
   3471 		if (IS_FC(isp) || isp->isp_state != ISP_RUNSTATE) {
   3472 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3473 			ISP_WRITE(isp, BIU_SEMA, 0);
   3474 			return;
   3475 		}
   3476 	}
   3477 
   3478 	/*
   3479 	 * We can't be getting this now.
   3480 	 */
   3481 	if (isp->isp_state != ISP_RUNSTATE) {
   3482 		isp_prt(isp, ISP_LOGWARN,
   3483 		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
   3484 		/*
   3485 		 * Thank you very much!  *Burrrp*!
   3486 		 */
   3487 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
   3488 		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
   3489 
   3490 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3491 		ISP_WRITE(isp, BIU_SEMA, 0);
   3492 		return;
   3493 	}
   3494 
   3495 	/*
   3496 	 * Get the current Response Queue Out Pointer.
   3497 	 *
   3498 	 * If we're a 2300, we can ask what hardware what it thinks.
   3499 	 */
   3500 	if (IS_23XX(isp)) {
   3501 		optr = ISP_READ(isp, isp->isp_respoutrp);
   3502 		/*
   3503 		 * Debug: to be taken out eventually
   3504 		 */
   3505 		if (isp->isp_residx != optr) {
   3506 			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
   3507 			    optr, isp->isp_residx);
   3508 		}
   3509 	} else {
   3510 		optr = isp->isp_residx;
   3511 	}
   3512 
   3513 	/*
   3514 	 * You *must* read the Response Queue In Pointer
   3515 	 * prior to clearing the RISC interrupt.
   3516 	 *
   3517 	 * Debounce the 2300 if revision less than 2.
   3518 	 */
   3519 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
   3520 		i = 0;
   3521 		do {
   3522 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3523 			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3524 		} while (junk != iptr && ++i < 1000);
   3525 
   3526 		if (iptr != junk) {
   3527 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3528 			isp_prt(isp, ISP_LOGWARN,
   3529 			    "Response Queue Out Pointer Unstable (%x, %x)",
   3530 			    iptr, junk);
   3531 			return;
   3532 		}
   3533 	} else {
   3534 		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3535 	}
   3536 	isp->isp_resodx = iptr;
   3537 
   3538 
   3539 	if (optr == iptr && sema == 0) {
   3540 		/*
   3541 		 * There are a lot of these- reasons unknown- mostly on
   3542 		 * faster Alpha machines.
   3543 		 *
   3544 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
   3545 		 * make sure the old interrupt went away (to avoid 'ringing'
   3546 		 * effects), but that didn't stop this from occurring.
   3547 		 */
   3548 		if (IS_23XX(isp)) {
   3549 			USEC_DELAY(100);
   3550 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
   3551 			junk = ISP_READ(isp, BIU_R2HSTSLO);
   3552 		} else {
   3553 			junk = ISP_READ(isp, BIU_ISR);
   3554 		}
   3555 		if (optr == iptr) {
   3556 			if (IS_23XX(isp)) {
   3557 				;
   3558 			} else {
   3559 				sema = ISP_READ(isp, BIU_SEMA);
   3560 				mbox = ISP_READ(isp, OUTMAILBOX0);
   3561 				if ((sema & 0x3) && (mbox & 0x8000)) {
   3562 					goto again;
   3563 				}
   3564 			}
   3565 			isp->isp_intbogus++;
   3566 			isp_prt(isp, ISP_LOGDEBUG1,
   3567 			    "bogus intr- isr %x (%x) iptr %x optr %x",
   3568 			    isr, junk, iptr, optr);
   3569 		}
   3570 	}
   3571 	isp->isp_resodx = iptr;
   3572 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   3573 	ISP_WRITE(isp, BIU_SEMA, 0);
   3574 
   3575 	if (isp->isp_rspbsy) {
   3576 		return;
   3577 	}
   3578 	isp->isp_rspbsy = 1;
   3579 
   3580 	while (optr != iptr) {
   3581 		ispstatusreq_t local, *sp = &local;
   3582 		isphdr_t *hp;
   3583 		int type;
   3584 		u_int16_t oop;
   3585 		int buddaboom = 0;
   3586 
   3587 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   3588 		oop = optr;
   3589 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
   3590 		nlooked++;
   3591 		/*
   3592 		 * Synchronize our view of this response queue entry.
   3593 		 */
   3594 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
   3595 
   3596 		type = isp_get_response_type(isp, hp);
   3597 
   3598 		if (type == RQSTYPE_RESPONSE) {
   3599 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
   3600 		} else if (type == RQSTYPE_RIO2) {
   3601 			isp_rio2_t rio;
   3602 			isp_get_rio2(isp, (isp_rio2_t *) hp, &rio);
   3603 			for (i = 0; i < rio.req_header.rqs_seqno; i++) {
   3604 				isp_fastpost_complete(isp, rio.req_handles[i]);
   3605 			}
   3606 			if (isp->isp_fpcchiwater < rio.req_header.rqs_seqno)
   3607 				isp->isp_fpcchiwater = rio.req_header.rqs_seqno;
   3608 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3609 			continue;
   3610 		} else {
   3611 			/*
   3612 			 * Somebody reachable via isp_handle_other_response
   3613 			 * may have updated the response queue pointers for
   3614 			 * us, so we reload our goal index.
   3615 			 */
   3616 			if (isp_handle_other_response(isp, type, hp, &optr)) {
   3617 				iptr = isp->isp_resodx;
   3618 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3619 				continue;
   3620 			}
   3621 
   3622 			/*
   3623 			 * After this point, we'll just look at the header as
   3624 			 * we don't know how to deal with the rest of the
   3625 			 * response.
   3626 			 */
   3627 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
   3628 
   3629 			/*
   3630 			 * It really has to be a bounced request just copied
   3631 			 * from the request queue to the response queue. If
   3632 			 * not, something bad has happened.
   3633 			 */
   3634 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
   3635 				isp_prt(isp, ISP_LOGERR, notresp,
   3636 				    sp->req_header.rqs_entry_type, oop, optr,
   3637 				    nlooked);
   3638 				if (isp->isp_dblev & ISP_LOGDEBUG0) {
   3639 					isp_print_bytes(isp, "Queue Entry",
   3640 					    QENTRY_LEN, sp);
   3641 				}
   3642 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3643 				continue;
   3644 			}
   3645 			buddaboom = 1;
   3646 		}
   3647 
   3648 		if (sp->req_header.rqs_flags & 0xf) {
   3649 #define	_RQS_OFLAGS	\
   3650 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
   3651 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   3652 				isp_prt(isp, ISP_LOGWARN,
   3653 				    "continuation segment");
   3654 				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3655 				continue;
   3656 			}
   3657 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   3658 				isp_prt(isp, ISP_LOGDEBUG1,
   3659 				    "internal queues full");
   3660 				/*
   3661 				 * We'll synthesize a QUEUE FULL message below.
   3662 				 */
   3663 			}
   3664 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   3665 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
   3666 				buddaboom++;
   3667 			}
   3668 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   3669 				isp_prt(isp, ISP_LOGERR, "bad request packet");
   3670 				buddaboom++;
   3671 			}
   3672 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
   3673 				isp_prt(isp, ISP_LOGERR,
   3674 				    "unknown flags (0x%x) in response",
   3675 				    sp->req_header.rqs_flags);
   3676 				buddaboom++;
   3677 			}
   3678 #undef	_RQS_OFLAGS
   3679 		}
   3680 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
   3681 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3682 			isp_prt(isp, ISP_LOGERR,
   3683 			    "bad request handle %d (type 0x%x, flags 0x%x)",
   3684 			    sp->req_handle, sp->req_header.rqs_entry_type,
   3685 			    sp->req_header.rqs_flags);
   3686 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3687 			continue;
   3688 		}
   3689 		xs = isp_find_xs(isp, sp->req_handle);
   3690 		if (xs == NULL) {
   3691 			u_int8_t ts = sp->req_completion_status & 0xff;
   3692 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3693 			/*
   3694 			 * Only whine if this isn't the expected fallout of
   3695 			 * aborting the command.
   3696 			 */
   3697 			if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE ||
   3698 			    ts != RQCS_ABORTED) {
   3699 				isp_prt(isp, ISP_LOGERR,
   3700 				    "cannot find handle 0x%x in xflist",
   3701 				    sp->req_handle);
   3702 			}
   3703 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3704 			continue;
   3705 		}
   3706 		isp_destroy_handle(isp, sp->req_handle);
   3707 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   3708 			XS_SETERR(xs, HBA_BUSRESET);
   3709 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3710 		}
   3711 		if (buddaboom) {
   3712 			XS_SETERR(xs, HBA_BOTCH);
   3713 		}
   3714 
   3715 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
   3716 			/*
   3717 			 * Fibre Channel F/W doesn't say we got status
   3718 			 * if there's Sense Data instead. I guess they
   3719 			 * think it goes w/o saying.
   3720 			 */
   3721 			sp->req_state_flags |= RQSF_GOT_STATUS;
   3722 		}
   3723 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   3724 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
   3725 		}
   3726 
   3727 		switch (sp->req_header.rqs_entry_type) {
   3728 		case RQSTYPE_RESPONSE:
   3729 			XS_SET_STATE_STAT(isp, xs, sp);
   3730 			isp_parse_status(isp, sp, xs);
   3731 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
   3732 			    (*XS_STSP(xs) == SCSI_BUSY)) {
   3733 				XS_SETERR(xs, HBA_TGTBSY);
   3734 			}
   3735 			if (IS_SCSI(isp)) {
   3736 				XS_RESID(xs) = sp->req_resid;
   3737 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   3738 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   3739 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
   3740 					XS_SAVE_SENSE(xs, sp);
   3741 				}
   3742 				/*
   3743 				 * A new synchronous rate was negotiated for
   3744 				 * this target. Mark state such that we'll go
   3745 				 * look up that which has changed later.
   3746 				 */
   3747 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   3748 					int t = XS_TGT(xs);
   3749 					sdparam *sdp = isp->isp_param;
   3750 					sdp += XS_CHANNEL(xs);
   3751 					sdp->isp_devparam[t].dev_refresh = 1;
   3752 					isp->isp_update |=
   3753 					    (1 << XS_CHANNEL(xs));
   3754 				}
   3755 			} else {
   3756 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
   3757 					XS_RESID(xs) = 0;
   3758 				} else if (sp->req_scsi_status & RQCS_RESID) {
   3759 					XS_RESID(xs) = sp->req_resid;
   3760 				} else {
   3761 					XS_RESID(xs) = 0;
   3762 				}
   3763 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   3764 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   3765 				    (sp->req_scsi_status & RQCS_SV)) {
   3766 					XS_SAVE_SENSE(xs, sp);
   3767 					/* solely for the benefit of debug */
   3768 					sp->req_state_flags |= RQSF_GOT_SENSE;
   3769 				}
   3770 			}
   3771 			isp_prt(isp, ISP_LOGDEBUG2,
   3772 			   "asked for %ld got resid %ld", (long) XS_XFRLEN(xs),
   3773 			   (long) sp->req_resid);
   3774 			break;
   3775 		case RQSTYPE_REQUEST:
   3776 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   3777 				/*
   3778 				 * Force Queue Full status.
   3779 				 */
   3780 				*XS_STSP(xs) = SCSI_QFULL;
   3781 				XS_SETERR(xs, HBA_NOERROR);
   3782 			} else if (XS_NOERR(xs)) {
   3783 				/*
   3784 				 * ????
   3785 				 */
   3786 				isp_prt(isp, ISP_LOGDEBUG0,
   3787 				    "Request Queue Entry bounced back");
   3788 				XS_SETERR(xs, HBA_BOTCH);
   3789 			}
   3790 			XS_RESID(xs) = XS_XFRLEN(xs);
   3791 			break;
   3792 		default:
   3793 			isp_prt(isp, ISP_LOGWARN,
   3794 			    "unhandled response queue type 0x%x",
   3795 			    sp->req_header.rqs_entry_type);
   3796 			if (XS_NOERR(xs)) {
   3797 				XS_SETERR(xs, HBA_BOTCH);
   3798 			}
   3799 			break;
   3800 		}
   3801 
   3802 		/*
   3803 		 * Free any dma resources. As a side effect, this may
   3804 		 * also do any cache flushing necessary for data coherence.			 */
   3805 		if (XS_XFRLEN(xs)) {
   3806 			ISP_DMAFREE(isp, xs, sp->req_handle);
   3807 		}
   3808 
   3809 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
   3810 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
   3811 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
   3812 			char skey;
   3813 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   3814 				skey = XS_SNSKEY(xs) & 0xf;
   3815 				if (skey < 10)
   3816 					skey += '0';
   3817 				else
   3818 					skey += 'a' - 10;
   3819 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
   3820 				skey = '?';
   3821 			} else {
   3822 				skey = '.';
   3823 			}
   3824 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
   3825 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
   3826 			    *XS_STSP(xs), skey, XS_ERR(xs));
   3827 		}
   3828 
   3829 		if (isp->isp_nactive > 0)
   3830 		    isp->isp_nactive--;
   3831 		complist[ndone++] = xs;	/* defer completion call until later */
   3832 		MEMZERO(hp, QENTRY_LEN);	/* PERF */
   3833 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
   3834 			break;
   3835 		}
   3836 	}
   3837 
   3838 	/*
   3839 	 * If we looked at any commands, then it's valid to find out
   3840 	 * what the outpointer is. It also is a trigger to update the
   3841 	 * ISP's notion of what we've seen so far.
   3842 	 */
   3843 	if (nlooked) {
   3844 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
   3845 		/*
   3846 		 * While we're at it, read the requst queue out pointer.
   3847 		 */
   3848 		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
   3849 		if (isp->isp_rscchiwater < ndone)
   3850 			isp->isp_rscchiwater = ndone;
   3851 	}
   3852 
   3853 	isp->isp_residx = optr;
   3854 	isp->isp_rspbsy = 0;
   3855 	for (i = 0; i < ndone; i++) {
   3856 		xs = complist[i];
   3857 		if (xs) {
   3858 			isp->isp_rsltccmplt++;
   3859 			isp_done(xs);
   3860 		}
   3861 	}
   3862 }
   3863 
   3864 /*
   3865  * Support routines.
   3866  */
   3867 
   3868 static int
   3869 isp_parse_async(struct ispsoftc *isp, u_int16_t mbox)
   3870 {
   3871 	int bus;
   3872 
   3873 	if (IS_DUALBUS(isp)) {
   3874 		bus = ISP_READ(isp, OUTMAILBOX6);
   3875 	} else {
   3876 		bus = 0;
   3877 	}
   3878 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
   3879 
   3880 	switch (mbox) {
   3881 	case ASYNC_BUS_RESET:
   3882 		isp->isp_sendmarker |= (1 << bus);
   3883 #ifdef	ISP_TARGET_MODE
   3884 		isp_target_async(isp, bus, mbox);
   3885 #endif
   3886 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
   3887 		break;
   3888 	case ASYNC_SYSTEM_ERROR:
   3889 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
   3890 		/* no point continuing after this */
   3891 		return (-1);
   3892 
   3893 	case ASYNC_RQS_XFER_ERR:
   3894 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
   3895 		break;
   3896 
   3897 	case ASYNC_RSP_XFER_ERR:
   3898 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
   3899 		break;
   3900 
   3901 	case ASYNC_QWAKEUP:
   3902 		/*
   3903 		 * We've just been notified that the Queue has woken up.
   3904 		 * We don't need to be chatty about this- just unlatch things
   3905 		 * and move on.
   3906 		 */
   3907 		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
   3908 		break;
   3909 
   3910 	case ASYNC_TIMEOUT_RESET:
   3911 		isp_prt(isp, ISP_LOGWARN,
   3912 		    "timeout initiated SCSI bus reset of bus %d", bus);
   3913 		isp->isp_sendmarker |= (1 << bus);
   3914 #ifdef	ISP_TARGET_MODE
   3915 		isp_target_async(isp, bus, mbox);
   3916 #endif
   3917 		break;
   3918 
   3919 	case ASYNC_DEVICE_RESET:
   3920 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
   3921 		isp->isp_sendmarker |= (1 << bus);
   3922 #ifdef	ISP_TARGET_MODE
   3923 		isp_target_async(isp, bus, mbox);
   3924 #endif
   3925 		break;
   3926 
   3927 	case ASYNC_EXTMSG_UNDERRUN:
   3928 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
   3929 		break;
   3930 
   3931 	case ASYNC_SCAM_INT:
   3932 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
   3933 		break;
   3934 
   3935 	case ASYNC_HUNG_SCSI:
   3936 		isp_prt(isp, ISP_LOGERR,
   3937 		    "stalled SCSI Bus after DATA Overrun");
   3938 		/* XXX: Need to issue SCSI reset at this point */
   3939 		break;
   3940 
   3941 	case ASYNC_KILLED_BUS:
   3942 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
   3943 		break;
   3944 
   3945 	case ASYNC_BUS_TRANSIT:
   3946 		mbox = ISP_READ(isp, OUTMAILBOX2);
   3947 		switch (mbox & 0x1c00) {
   3948 		case SXP_PINS_LVD_MODE:
   3949 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
   3950 			SDPARAM(isp)->isp_diffmode = 0;
   3951 			SDPARAM(isp)->isp_ultramode = 0;
   3952 			SDPARAM(isp)->isp_lvdmode = 1;
   3953 			break;
   3954 		case SXP_PINS_HVD_MODE:
   3955 			isp_prt(isp, ISP_LOGINFO,
   3956 			    "Transition to Differential mode");
   3957 			SDPARAM(isp)->isp_diffmode = 1;
   3958 			SDPARAM(isp)->isp_ultramode = 0;
   3959 			SDPARAM(isp)->isp_lvdmode = 0;
   3960 			break;
   3961 		case SXP_PINS_SE_MODE:
   3962 			isp_prt(isp, ISP_LOGINFO,
   3963 			    "Transition to Single Ended mode");
   3964 			SDPARAM(isp)->isp_diffmode = 0;
   3965 			SDPARAM(isp)->isp_ultramode = 1;
   3966 			SDPARAM(isp)->isp_lvdmode = 0;
   3967 			break;
   3968 		default:
   3969 			isp_prt(isp, ISP_LOGWARN,
   3970 			    "Transition to Unknown Mode 0x%x", mbox);
   3971 			break;
   3972 		}
   3973 		/*
   3974 		 * XXX: Set up to renegotiate again!
   3975 		 */
   3976 		/* Can only be for a 1080... */
   3977 		isp->isp_sendmarker |= (1 << bus);
   3978 		break;
   3979 
   3980 	/*
   3981 	 * We can use bus, which will always be zero for FC cards,
   3982 	 * as a mailbox pattern accumulator to be checked below.
   3983 	 */
   3984 	case ASYNC_RIO5:
   3985 		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
   3986 		break;
   3987 
   3988 	case ASYNC_RIO4:
   3989 		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
   3990 		break;
   3991 
   3992 	case ASYNC_RIO3:
   3993 		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
   3994 		break;
   3995 
   3996 	case ASYNC_RIO2:
   3997 		bus = 0x106;	/* outgoing mailbox regs 1-2 */
   3998 		break;
   3999 
   4000 	case ASYNC_RIO1:
   4001 	case ASYNC_CMD_CMPLT:
   4002 		bus = 0x102;	/* outgoing mailbox regs 1 */
   4003 		break;
   4004 
   4005 	case ASYNC_RIO_RESP:
   4006 		break;
   4007 
   4008 	case ASYNC_CTIO_DONE:
   4009 #ifdef	ISP_TARGET_MODE
   4010 		/*
   4011 		 * Bus gets overloaded with the handle. Dual bus
   4012 		 * cards don't put bus# into the handle.
   4013 		 */
   4014 		isp_target_async(isp, ((ISP_READ(isp, OUTMAILBOX2) << 16) |
   4015 		    ISP_READ(isp, OUTMAILBOX1)), mbox);
   4016 #else
   4017 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
   4018 #endif
   4019 		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
   4020 		return (0);
   4021 
   4022 	case ASYNC_LIP_F8:
   4023 	case ASYNC_LIP_OCCURRED:
   4024 		FCPARAM(isp)->isp_lipseq =
   4025 		    ISP_READ(isp, OUTMAILBOX1);
   4026 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4027 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   4028 		isp->isp_sendmarker = 1;
   4029 		isp_mark_getpdb_all(isp);
   4030 		isp_async(isp, ISPASYNC_LIP, NULL);
   4031 #ifdef	ISP_TARGET_MODE
   4032 		isp_target_async(isp, bus, mbox);
   4033 #endif
   4034 		/*
   4035 		 * We've had problems with data corruption occuring on
   4036 		 * commands that complete (with no apparent error) after
   4037 		 * we receive a LIP. This has been observed mostly on
   4038 		 * Local Loop topologies. To be safe, let's just mark
   4039 		 * all active commands as dead.
   4040 		 */
   4041 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
   4042 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
   4043 			int i, j;
   4044 			for (i = j = 0; i < isp->isp_maxcmds; i++) {
   4045 				XS_T *xs;
   4046 				xs = isp->isp_xflist[i];
   4047 				if (xs != NULL) {
   4048 					j++;
   4049 					XS_SETERR(xs, HBA_BUSRESET);
   4050 				}
   4051 			}
   4052 			if (j) {
   4053 				isp_prt(isp, ISP_LOGERR,
   4054 				    "LIP destroyed %d active commands", j);
   4055 			}
   4056 		}
   4057 		break;
   4058 
   4059 	case ASYNC_LOOP_UP:
   4060 		isp->isp_sendmarker = 1;
   4061 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4062 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   4063 		isp_mark_getpdb_all(isp);
   4064 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
   4065 #ifdef	ISP_TARGET_MODE
   4066 		isp_target_async(isp, bus, mbox);
   4067 #endif
   4068 		break;
   4069 
   4070 	case ASYNC_LOOP_DOWN:
   4071 		isp->isp_sendmarker = 1;
   4072 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4073 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   4074 		isp_mark_getpdb_all(isp);
   4075 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
   4076 #ifdef	ISP_TARGET_MODE
   4077 		isp_target_async(isp, bus, mbox);
   4078 #endif
   4079 		break;
   4080 
   4081 	case ASYNC_LOOP_RESET:
   4082 		isp->isp_sendmarker = 1;
   4083 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4084 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   4085 		isp_mark_getpdb_all(isp);
   4086 		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
   4087 #ifdef	ISP_TARGET_MODE
   4088 		isp_target_async(isp, bus, mbox);
   4089 #endif
   4090 		break;
   4091 
   4092 	case ASYNC_PDB_CHANGED:
   4093 		isp->isp_sendmarker = 1;
   4094 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   4095 		isp_mark_getpdb_all(isp);
   4096 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
   4097 		break;
   4098 
   4099 	case ASYNC_CHANGE_NOTIFY:
   4100 		/*
   4101 		 * Not correct, but it will force us to rescan the loop.
   4102 		 */
   4103 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   4104 		isp_mark_getpdb_all(isp);
   4105 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
   4106 		break;
   4107 
   4108 	case ASYNC_PTPMODE:
   4109 		if (FCPARAM(isp)->isp_onfabric)
   4110 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
   4111 		else
   4112 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
   4113 		isp_mark_getpdb_all(isp);
   4114 		isp->isp_sendmarker = 1;
   4115 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4116 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   4117 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   4118 #ifdef	ISP_TARGET_MODE
   4119 		isp_target_async(isp, bus, mbox);
   4120 #endif
   4121 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
   4122 		break;
   4123 
   4124 	case ASYNC_CONNMODE:
   4125 		mbox = ISP_READ(isp, OUTMAILBOX1);
   4126 		isp_mark_getpdb_all(isp);
   4127 		switch (mbox) {
   4128 		case ISP_CONN_LOOP:
   4129 			isp_prt(isp, ISP_LOGINFO,
   4130 			    "Point-to-Point -> Loop mode");
   4131 			break;
   4132 		case ISP_CONN_PTP:
   4133 			isp_prt(isp, ISP_LOGINFO,
   4134 			    "Loop -> Point-to-Point mode");
   4135 			break;
   4136 		case ISP_CONN_BADLIP:
   4137 			isp_prt(isp, ISP_LOGWARN,
   4138 			    "Point-to-Point -> Loop mode (BAD LIP)");
   4139 			break;
   4140 		case ISP_CONN_FATAL:
   4141 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
   4142 			isp_reinit(isp);
   4143 #ifdef	ISP_TARGET_MODE
   4144 			isp_target_async(isp, bus, ASYNC_SYSTEM_ERROR);
   4145 #endif
   4146 			/* no point continuing after this */
   4147 			return (-1);
   4148 		case ISP_CONN_LOOPBACK:
   4149 			isp_prt(isp, ISP_LOGWARN,
   4150 			    "Looped Back in Point-to-Point mode");
   4151 			break;
   4152 		default:
   4153 			isp_prt(isp, ISP_LOGWARN,
   4154 			    "Unknown connection mode (0x%x)", mbox);
   4155 			break;
   4156 		}
   4157 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   4158 		isp->isp_sendmarker = 1;
   4159 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   4160 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   4161 		break;
   4162 
   4163 	default:
   4164 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
   4165 		break;
   4166 	}
   4167 
   4168 	if (bus & 0x100) {
   4169 		int i, nh;
   4170 		u_int16_t handles[5];
   4171 
   4172 		for (nh = 0, i = 1; i < MAX_MAILBOX; i++) {
   4173 			if ((bus & (1 << i)) == 0) {
   4174 				continue;
   4175 			}
   4176 			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
   4177 		}
   4178 		for (i = 0; i < nh; i++) {
   4179 			isp_fastpost_complete(isp, handles[i]);
   4180 			isp_prt(isp,  ISP_LOGDEBUG3,
   4181 			    "fast post completion of %u", handles[i]);
   4182 		}
   4183 		if (isp->isp_fpcchiwater < nh)
   4184 			isp->isp_fpcchiwater = nh;
   4185 	} else {
   4186 		isp->isp_intoasync++;
   4187 	}
   4188 	return (0);
   4189 }
   4190 
   4191 /*
   4192  * Handle other response entries. A pointer to the request queue output
   4193  * index is here in case we want to eat several entries at once, although
   4194  * this is not used currently.
   4195  */
   4196 
   4197 static int
   4198 isp_handle_other_response(struct ispsoftc *isp, int type,
   4199     isphdr_t *hp, u_int16_t *optrp)
   4200 {
   4201 	switch (type) {
   4202 	case RQSTYPE_STATUS_CONT:
   4203 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
   4204 		return (1);
   4205 	case RQSTYPE_ATIO:
   4206 	case RQSTYPE_CTIO:
   4207 	case RQSTYPE_ENABLE_LUN:
   4208 	case RQSTYPE_MODIFY_LUN:
   4209 	case RQSTYPE_NOTIFY:
   4210 	case RQSTYPE_NOTIFY_ACK:
   4211 	case RQSTYPE_CTIO1:
   4212 	case RQSTYPE_ATIO2:
   4213 	case RQSTYPE_CTIO2:
   4214 	case RQSTYPE_CTIO3:
   4215 		isp->isp_rsltccmplt++;	/* count as a response completion */
   4216 #ifdef	ISP_TARGET_MODE
   4217 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
   4218 			return (1);
   4219 		}
   4220 #else
   4221 		optrp = optrp;
   4222 		/* FALLTHROUGH */
   4223 #endif
   4224 	case RQSTYPE_REQUEST:
   4225 	default:
   4226 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
   4227 			return (1);
   4228 		}
   4229 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
   4230 		    isp_get_response_type(isp, hp));
   4231 		return (0);
   4232 	}
   4233 }
   4234 
   4235 static void
   4236 isp_parse_status(struct ispsoftc *isp, ispstatusreq_t *sp, XS_T *xs)
   4237 {
   4238 	switch (sp->req_completion_status & 0xff) {
   4239 	case RQCS_COMPLETE:
   4240 		if (XS_NOERR(xs)) {
   4241 			XS_SETERR(xs, HBA_NOERROR);
   4242 		}
   4243 		return;
   4244 
   4245 	case RQCS_INCOMPLETE:
   4246 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   4247 			isp_prt(isp, ISP_LOGDEBUG1,
   4248 			    "Selection Timeout for %d.%d.%d",
   4249 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4250 			if (XS_NOERR(xs)) {
   4251 				XS_SETERR(xs, HBA_SELTIMEOUT);
   4252 			}
   4253 			return;
   4254 		}
   4255 		isp_prt(isp, ISP_LOGERR,
   4256 		    "command incomplete for %d.%d.%d, state 0x%x",
   4257 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
   4258 		    sp->req_state_flags);
   4259 		break;
   4260 
   4261 	case RQCS_DMA_ERROR:
   4262 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
   4263 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4264 		break;
   4265 
   4266 	case RQCS_TRANSPORT_ERROR:
   4267 	{
   4268 		char buf[172];
   4269 		buf[0] = 0;
   4270 		STRNCAT(buf, "states=>", sizeof buf);
   4271 		if (sp->req_state_flags & RQSF_GOT_BUS) {
   4272 			STRNCAT(buf, " GOT_BUS", sizeof buf);
   4273 		}
   4274 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
   4275 			STRNCAT(buf, " GOT_TGT", sizeof buf);
   4276 		}
   4277 		if (sp->req_state_flags & RQSF_SENT_CDB) {
   4278 			STRNCAT(buf, " SENT_CDB", sizeof buf);
   4279 		}
   4280 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
   4281 			STRNCAT(buf, " XFRD_DATA", sizeof buf);
   4282 		}
   4283 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   4284 			STRNCAT(buf, " GOT_STS", sizeof buf);
   4285 		}
   4286 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
   4287 			STRNCAT(buf, " GOT_SNS", sizeof buf);
   4288 		}
   4289 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
   4290 			STRNCAT(buf, " XFR_CMPLT", sizeof buf);
   4291 		}
   4292 		STRNCAT(buf, "\nstatus=>", sizeof buf);
   4293 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
   4294 			STRNCAT(buf, " Disconnect", sizeof buf);
   4295 		}
   4296 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
   4297 			STRNCAT(buf, " Sync_xfr", sizeof buf);
   4298 		}
   4299 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
   4300 			STRNCAT(buf, " Parity", sizeof buf);
   4301 		}
   4302 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   4303 			STRNCAT(buf, " Bus_Reset", sizeof buf);
   4304 		}
   4305 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
   4306 			STRNCAT(buf, " Device_Reset", sizeof buf);
   4307 		}
   4308 		if (sp->req_status_flags & RQSTF_ABORTED) {
   4309 			STRNCAT(buf, " Aborted", sizeof buf);
   4310 		}
   4311 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
   4312 			STRNCAT(buf, " Timeout", sizeof buf);
   4313 		}
   4314 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   4315 			STRNCAT(buf, " Negotiation", sizeof buf);
   4316 		}
   4317 		isp_prt(isp, ISP_LOGERR, "%s", buf);
   4318 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
   4319 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
   4320 		break;
   4321 	}
   4322 	case RQCS_RESET_OCCURRED:
   4323 		isp_prt(isp, ISP_LOGWARN,
   4324 		    "bus reset destroyed command for %d.%d.%d",
   4325 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4326 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   4327 		if (XS_NOERR(xs)) {
   4328 			XS_SETERR(xs, HBA_BUSRESET);
   4329 		}
   4330 		return;
   4331 
   4332 	case RQCS_ABORTED:
   4333 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
   4334 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4335 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   4336 		if (XS_NOERR(xs)) {
   4337 			XS_SETERR(xs, HBA_ABORTED);
   4338 		}
   4339 		return;
   4340 
   4341 	case RQCS_TIMEOUT:
   4342 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
   4343 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4344 		if (XS_NOERR(xs)) {
   4345 			XS_SETERR(xs, HBA_CMDTIMEOUT);
   4346 		}
   4347 		return;
   4348 
   4349 	case RQCS_DATA_OVERRUN:
   4350 		XS_RESID(xs) = sp->req_resid;
   4351 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
   4352 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4353 		if (XS_NOERR(xs)) {
   4354 			XS_SETERR(xs, HBA_DATAOVR);
   4355 		}
   4356 		return;
   4357 
   4358 	case RQCS_COMMAND_OVERRUN:
   4359 		isp_prt(isp, ISP_LOGERR,
   4360 		    "command overrun for command on %d.%d.%d",
   4361 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4362 		break;
   4363 
   4364 	case RQCS_STATUS_OVERRUN:
   4365 		isp_prt(isp, ISP_LOGERR,
   4366 		    "status overrun for command on %d.%d.%d",
   4367 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4368 		break;
   4369 
   4370 	case RQCS_BAD_MESSAGE:
   4371 		isp_prt(isp, ISP_LOGERR,
   4372 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
   4373 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4374 		break;
   4375 
   4376 	case RQCS_NO_MESSAGE_OUT:
   4377 		isp_prt(isp, ISP_LOGERR,
   4378 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
   4379 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4380 		break;
   4381 
   4382 	case RQCS_EXT_ID_FAILED:
   4383 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
   4384 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4385 		break;
   4386 
   4387 	case RQCS_IDE_MSG_FAILED:
   4388 		isp_prt(isp, ISP_LOGERR,
   4389 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
   4390 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4391 		break;
   4392 
   4393 	case RQCS_ABORT_MSG_FAILED:
   4394 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
   4395 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4396 		break;
   4397 
   4398 	case RQCS_REJECT_MSG_FAILED:
   4399 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
   4400 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4401 		break;
   4402 
   4403 	case RQCS_NOP_MSG_FAILED:
   4404 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
   4405 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4406 		break;
   4407 
   4408 	case RQCS_PARITY_ERROR_MSG_FAILED:
   4409 		isp_prt(isp, ISP_LOGERR,
   4410 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
   4411 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4412 		break;
   4413 
   4414 	case RQCS_DEVICE_RESET_MSG_FAILED:
   4415 		isp_prt(isp, ISP_LOGWARN,
   4416 		    "BUS DEVICE RESET rejected by %d.%d.%d",
   4417 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4418 		break;
   4419 
   4420 	case RQCS_ID_MSG_FAILED:
   4421 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
   4422 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4423 		break;
   4424 
   4425 	case RQCS_UNEXP_BUS_FREE:
   4426 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
   4427 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4428 		break;
   4429 
   4430 	case RQCS_DATA_UNDERRUN:
   4431 		XS_RESID(xs) = sp->req_resid;
   4432 		if (XS_NOERR(xs)) {
   4433 			XS_SETERR(xs, HBA_NOERROR);
   4434 		}
   4435 		return;
   4436 
   4437 	case RQCS_XACT_ERR1:
   4438 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
   4439 		    XS_TGT(xs), XS_LUN(xs));
   4440 		break;
   4441 
   4442 	case RQCS_XACT_ERR2:
   4443 		isp_prt(isp, ISP_LOGERR, xact2,
   4444 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
   4445 		break;
   4446 
   4447 	case RQCS_XACT_ERR3:
   4448 		isp_prt(isp, ISP_LOGERR, xact3,
   4449 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4450 		break;
   4451 
   4452 	case RQCS_BAD_ENTRY:
   4453 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
   4454 		break;
   4455 
   4456 	case RQCS_QUEUE_FULL:
   4457 		isp_prt(isp, ISP_LOGDEBUG0,
   4458 		    "internal queues full for %d.%d.%d status 0x%x", XS_TGT(xs),
   4459 		    XS_LUN(xs), XS_CHANNEL(xs), *XS_STSP(xs));
   4460 
   4461 		/*
   4462 		 * If QFULL or some other status byte is set, then this
   4463 		 * isn't an error, per se.
   4464 		 *
   4465 		 * Unfortunately, some QLogic f/w writers have, in
   4466 		 * some cases, ommitted to *set* status to QFULL.
   4467 		 *
   4468 
   4469 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
   4470 			XS_SETERR(xs, HBA_NOERROR);
   4471 			return;
   4472 		}
   4473 
   4474 		 *
   4475 		 *
   4476 		 */
   4477 
   4478 		*XS_STSP(xs) = SCSI_QFULL;
   4479 		XS_SETERR(xs, HBA_NOERROR);
   4480 		return;
   4481 
   4482 	case RQCS_PHASE_SKIPPED:
   4483 		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
   4484 		    XS_TGT(xs), XS_LUN(xs));
   4485 		break;
   4486 
   4487 	case RQCS_ARQS_FAILED:
   4488 		isp_prt(isp, ISP_LOGERR,
   4489 		    "Auto Request Sense failed for %d.%d.%d",
   4490 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   4491 		if (XS_NOERR(xs)) {
   4492 			XS_SETERR(xs, HBA_ARQFAIL);
   4493 		}
   4494 		return;
   4495 
   4496 	case RQCS_WIDE_FAILED:
   4497 		isp_prt(isp, ISP_LOGERR,
   4498 		    "Wide Negotiation failed for %d.%d.%d",
   4499 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   4500 		if (IS_SCSI(isp)) {
   4501 			sdparam *sdp = isp->isp_param;
   4502 			sdp += XS_CHANNEL(xs);
   4503 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
   4504 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   4505 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   4506 		}
   4507 		if (XS_NOERR(xs)) {
   4508 			XS_SETERR(xs, HBA_NOERROR);
   4509 		}
   4510 		return;
   4511 
   4512 	case RQCS_SYNCXFER_FAILED:
   4513 		isp_prt(isp, ISP_LOGERR,
   4514 		    "SDTR Message failed for target %d.%d.%d",
   4515 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   4516 		if (IS_SCSI(isp)) {
   4517 			sdparam *sdp = isp->isp_param;
   4518 			sdp += XS_CHANNEL(xs);
   4519 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
   4520 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   4521 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   4522 		}
   4523 		break;
   4524 
   4525 	case RQCS_LVD_BUSERR:
   4526 		isp_prt(isp, ISP_LOGERR,
   4527 		    "Bad LVD condition while talking to %d.%d.%d",
   4528 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   4529 		break;
   4530 
   4531 	case RQCS_PORT_UNAVAILABLE:
   4532 		/*
   4533 		 * No such port on the loop. Moral equivalent of SELTIMEO
   4534 		 */
   4535 	case RQCS_PORT_LOGGED_OUT:
   4536 		/*
   4537 		 * It was there (maybe)- treat as a selection timeout.
   4538 		 */
   4539 		if ((sp->req_completion_status & 0xff) == RQCS_PORT_UNAVAILABLE)
   4540 			isp_prt(isp, ISP_LOGINFO,
   4541 			    "Port Unavailable for target %d", XS_TGT(xs));
   4542 		else
   4543 			isp_prt(isp, ISP_LOGINFO,
   4544 			    "port logout for target %d", XS_TGT(xs));
   4545 		/*
   4546 		 * If we're on a local loop, force a LIP (which is overkill)
   4547 		 * to force a re-login of this unit.
   4548 		 */
   4549 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
   4550 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
   4551 			mbreg_t mbs;
   4552 			mbs.param[0] = MBOX_INIT_LIP;
   4553 			isp_mboxcmd_qnw(isp, &mbs, 1);
   4554 		}
   4555 
   4556 		/*
   4557 		 * Probably overkill.
   4558 		 */
   4559 		isp->isp_sendmarker = 1;
   4560 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   4561 		isp_mark_getpdb_all(isp);
   4562 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   4563 		if (XS_NOERR(xs)) {
   4564 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4565 		}
   4566 		return;
   4567 
   4568 	case RQCS_PORT_CHANGED:
   4569 		isp_prt(isp, ISP_LOGWARN,
   4570 		    "port changed for target %d", XS_TGT(xs));
   4571 		if (XS_NOERR(xs)) {
   4572 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4573 		}
   4574 		return;
   4575 
   4576 	case RQCS_PORT_BUSY:
   4577 		isp_prt(isp, ISP_LOGWARN,
   4578 		    "port busy for target %d", XS_TGT(xs));
   4579 		if (XS_NOERR(xs)) {
   4580 			XS_SETERR(xs, HBA_TGTBSY);
   4581 		}
   4582 		return;
   4583 
   4584 	default:
   4585 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
   4586 		    sp->req_completion_status);
   4587 		break;
   4588 	}
   4589 	if (XS_NOERR(xs)) {
   4590 		XS_SETERR(xs, HBA_BOTCH);
   4591 	}
   4592 }
   4593 
   4594 static void
   4595 isp_fastpost_complete(struct ispsoftc *isp, u_int16_t fph)
   4596 {
   4597 	XS_T *xs;
   4598 
   4599 	if (fph == 0) {
   4600 		return;
   4601 	}
   4602 	xs = isp_find_xs(isp, fph);
   4603 	if (xs == NULL) {
   4604 		isp_prt(isp, ISP_LOGWARN,
   4605 		    "Command for fast post handle 0x%x not found", fph);
   4606 		return;
   4607 	}
   4608 	isp_destroy_handle(isp, fph);
   4609 
   4610 	/*
   4611 	 * Since we don't have a result queue entry item,
   4612 	 * we must believe that SCSI status is zero and
   4613 	 * that all data transferred.
   4614 	 */
   4615 	XS_SET_STATE_STAT(isp, xs, NULL);
   4616 	XS_RESID(xs) = 0;
   4617 	*XS_STSP(xs) = SCSI_GOOD;
   4618 	if (XS_XFRLEN(xs)) {
   4619 		ISP_DMAFREE(isp, xs, fph);
   4620 	}
   4621 	if (isp->isp_nactive)
   4622 		isp->isp_nactive--;
   4623 	isp->isp_fphccmplt++;
   4624 	isp_done(xs);
   4625 }
   4626 
   4627 static int
   4628 isp_mbox_continue(struct ispsoftc *isp)
   4629 {
   4630 	mbreg_t mbs;
   4631 	u_int16_t *ptr;
   4632 
   4633 	switch (isp->isp_lastmbxcmd) {
   4634 	case MBOX_WRITE_RAM_WORD:
   4635 	case MBOX_READ_RAM_WORD:
   4636 	case MBOX_READ_RAM_WORD_EXTENDED:
   4637 		break;
   4638 	default:
   4639 		return (1);
   4640 	}
   4641 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
   4642 		isp->isp_mbxwrk0 = 0;
   4643 		return (-1);
   4644 	}
   4645 
   4646 
   4647 	/*
   4648 	 * Clear the previous interrupt.
   4649 	 */
   4650 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   4651 	ISP_WRITE(isp, BIU_SEMA, 0);
   4652 
   4653 	/*
   4654 	 * Continue with next word.
   4655 	 */
   4656 	ptr = isp->isp_mbxworkp;
   4657 	switch (isp->isp_lastmbxcmd) {
   4658 	case MBOX_WRITE_RAM_WORD:
   4659 		mbs.param[2] = *ptr++;
   4660 		mbs.param[1] = isp->isp_mbxwrk1++;
   4661 		break;
   4662 	case MBOX_READ_RAM_WORD:
   4663 	case MBOX_READ_RAM_WORD_EXTENDED:
   4664 		*ptr++ = isp->isp_mboxtmp[2];
   4665 		mbs.param[1] = isp->isp_mbxwrk1++;
   4666 		break;
   4667 	}
   4668 	isp->isp_mbxworkp = ptr;
   4669 	mbs.param[0] = isp->isp_lastmbxcmd;
   4670 	isp->isp_mbxwrk0 -= 1;
   4671 	isp_mboxcmd_qnw(isp, &mbs, 0);
   4672 	return (0);
   4673 }
   4674 
   4675 
   4676 #define	HIBYT(x)			((x) >> 0x8)
   4677 #define	LOBYT(x)			((x)  & 0xff)
   4678 #define	ISPOPMAP(a, b)			(((a) << 8) | (b))
   4679 static u_int16_t mbpscsi[] = {
   4680 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   4681 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   4682 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   4683 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   4684 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   4685 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   4686 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   4687 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   4688 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   4689 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
   4690 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
   4691 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   4692 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   4693 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   4694 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   4695 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   4696 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   4697 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
   4698 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
   4699 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   4700 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
   4701 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
   4702 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
   4703 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
   4704 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
   4705 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
   4706 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
   4707 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   4708 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
   4709 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   4710 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   4711 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   4712 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
   4713 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   4714 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   4715 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   4716 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
   4717 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   4718 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   4719 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
   4720 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   4721 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   4722 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
   4723 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   4724 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   4725 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   4726 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   4727 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   4728 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   4729 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   4730 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   4731 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   4732 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
   4733 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
   4734 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   4735 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   4736 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   4737 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   4738 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
   4739 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   4740 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   4741 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   4742 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   4743 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   4744 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   4745 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   4746 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   4747 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
   4748 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   4749 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
   4750 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
   4751 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
   4752 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
   4753 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
   4754 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
   4755 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
   4756 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   4757 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   4758 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   4759 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   4760 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
   4761 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
   4762 	ISPOPMAP(0xdf, 0xdf),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
   4763 	ISPOPMAP(0xff, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
   4764 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   4765 	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
   4766 	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
   4767 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   4768 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   4769 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   4770 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
   4771 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
   4772 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
   4773 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
   4774 };
   4775 
   4776 #ifndef	ISP_STRIPPED
   4777 static char *scsi_mbcmd_names[] = {
   4778 	"NO-OP",
   4779 	"LOAD RAM",
   4780 	"EXEC FIRMWARE",
   4781 	"DUMP RAM",
   4782 	"WRITE RAM WORD",
   4783 	"READ RAM WORD",
   4784 	"MAILBOX REG TEST",
   4785 	"VERIFY CHECKSUM",
   4786 	"ABOUT FIRMWARE",
   4787 	NULL,
   4788 	NULL,
   4789 	NULL,
   4790 	NULL,
   4791 	NULL,
   4792 	"CHECK FIRMWARE",
   4793 	NULL,
   4794 	"INIT REQUEST QUEUE",
   4795 	"INIT RESULT QUEUE",
   4796 	"EXECUTE IOCB",
   4797 	"WAKE UP",
   4798 	"STOP FIRMWARE",
   4799 	"ABORT",
   4800 	"ABORT DEVICE",
   4801 	"ABORT TARGET",
   4802 	"BUS RESET",
   4803 	"STOP QUEUE",
   4804 	"START QUEUE",
   4805 	"SINGLE STEP QUEUE",
   4806 	"ABORT QUEUE",
   4807 	"GET DEV QUEUE STATUS",
   4808 	NULL,
   4809 	"GET FIRMWARE STATUS",
   4810 	"GET INIT SCSI ID",
   4811 	"GET SELECT TIMEOUT",
   4812 	"GET RETRY COUNT",
   4813 	"GET TAG AGE LIMIT",
   4814 	"GET CLOCK RATE",
   4815 	"GET ACT NEG STATE",
   4816 	"GET ASYNC DATA SETUP TIME",
   4817 	"GET PCI PARAMS",
   4818 	"GET TARGET PARAMS",
   4819 	"GET DEV QUEUE PARAMS",
   4820 	"GET RESET DELAY PARAMS",
   4821 	NULL,
   4822 	NULL,
   4823 	NULL,
   4824 	NULL,
   4825 	NULL,
   4826 	"SET INIT SCSI ID",
   4827 	"SET SELECT TIMEOUT",
   4828 	"SET RETRY COUNT",
   4829 	"SET TAG AGE LIMIT",
   4830 	"SET CLOCK RATE",
   4831 	"SET ACT NEG STATE",
   4832 	"SET ASYNC DATA SETUP TIME",
   4833 	"SET PCI CONTROL PARAMS",
   4834 	"SET TARGET PARAMS",
   4835 	"SET DEV QUEUE PARAMS",
   4836 	"SET RESET DELAY PARAMS",
   4837 	NULL,
   4838 	NULL,
   4839 	NULL,
   4840 	NULL,
   4841 	NULL,
   4842 	"RETURN BIOS BLOCK ADDR",
   4843 	"WRITE FOUR RAM WORDS",
   4844 	"EXEC BIOS IOCB",
   4845 	NULL,
   4846 	NULL,
   4847 	"SET SYSTEM PARAMETER",
   4848 	"GET SYSTEM PARAMETER",
   4849 	NULL,
   4850 	"GET SCAM CONFIGURATION",
   4851 	"SET SCAM CONFIGURATION",
   4852 	"SET FIRMWARE FEATURES",
   4853 	"GET FIRMWARE FEATURES",
   4854 	NULL,
   4855 	NULL,
   4856 	NULL,
   4857 	NULL,
   4858 	"LOAD RAM A64",
   4859 	"DUMP RAM A64",
   4860 	"INITIALIZE REQUEST QUEUE A64",
   4861 	"INITIALIZE RESPONSE QUEUE A64",
   4862 	"EXECUTE IOCB A64",
   4863 	"ENABLE TARGET MODE",
   4864 	"GET TARGET MODE STATE",
   4865 	NULL,
   4866 	NULL,
   4867 	NULL,
   4868 	"SET DATA OVERRUN RECOVERY MODE",
   4869 	"GET DATA OVERRUN RECOVERY MODE",
   4870 	"SET HOST DATA",
   4871 	"GET NOST DATA",
   4872 };
   4873 #endif
   4874 
   4875 static u_int16_t mbpfc[] = {
   4876 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   4877 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   4878 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   4879 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   4880 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   4881 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   4882 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   4883 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   4884 	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   4885 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
   4886 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
   4887 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   4888 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   4889 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   4890 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   4891 	ISPOPMAP(0x03, 0x07),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED(1) */
   4892 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   4893 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
   4894 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
   4895 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   4896 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
   4897 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
   4898 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
   4899 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
   4900 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
   4901 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
   4902 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
   4903 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   4904 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
   4905 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   4906 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   4907 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   4908 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
   4909 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
   4910 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   4911 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
   4912 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
   4913 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
   4914 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
   4915 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
   4916 	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
   4917 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
   4918 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
   4919 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   4920 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   4921 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   4922 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   4923 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   4924 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
   4925 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
   4926 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   4927 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
   4928 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
   4929 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
   4930 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
   4931 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
   4932 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
   4933 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
   4934 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
   4935 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   4936 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   4937 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   4938 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   4939 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   4940 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
   4941 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
   4942 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
   4943 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
   4944 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   4945 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
   4946 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
   4947 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
   4948 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
   4949 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
   4950 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
   4951 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
   4952 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   4953 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   4954 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   4955 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   4956 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
   4957 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
   4958 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
   4959 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
   4960 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   4961 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
   4962 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   4963 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   4964 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   4965 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   4966 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
   4967 	ISPOPMAP(0x00, 0x00),	/* 0x5b: */
   4968 	ISPOPMAP(0x00, 0x00),	/* 0x5c: */
   4969 	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
   4970 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
   4971 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
   4972 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
   4973 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
   4974 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
   4975 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   4976 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
   4977 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
   4978 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
   4979 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
   4980 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
   4981 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
   4982 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
   4983 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
   4984 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
   4985 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
   4986 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
   4987 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
   4988 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
   4989 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
   4990 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
   4991 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
   4992 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
   4993 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
   4994 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
   4995 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
   4996 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
   4997 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
   4998 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
   4999 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
   5000 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
   5001 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
   5002 	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
   5003 };
   5004 /*
   5005  * Footnotes
   5006  *
   5007  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
   5008  *	do not access at this time in the core driver. The caller is
   5009  *	responsible for setting this register first (Gross!).
   5010  */
   5011 
   5012 #ifndef	ISP_STRIPPED
   5013 static char *fc_mbcmd_names[] = {
   5014 	"NO-OP",
   5015 	"LOAD RAM",
   5016 	"EXEC FIRMWARE",
   5017 	"DUMP RAM",
   5018 	"WRITE RAM WORD",
   5019 	"READ RAM WORD",
   5020 	"MAILBOX REG TEST",
   5021 	"VERIFY CHECKSUM",
   5022 	"ABOUT FIRMWARE",
   5023 	"LOAD RAM",
   5024 	"DUMP RAM",
   5025 	NULL,
   5026 	NULL,
   5027 	"READ RAM WORD EXTENDED",
   5028 	"CHECK FIRMWARE",
   5029 	NULL,
   5030 	"INIT REQUEST QUEUE",
   5031 	"INIT RESULT QUEUE",
   5032 	"EXECUTE IOCB",
   5033 	"WAKE UP",
   5034 	"STOP FIRMWARE",
   5035 	"ABORT",
   5036 	"ABORT DEVICE",
   5037 	"ABORT TARGET",
   5038 	"BUS RESET",
   5039 	"STOP QUEUE",
   5040 	"START QUEUE",
   5041 	"SINGLE STEP QUEUE",
   5042 	"ABORT QUEUE",
   5043 	"GET DEV QUEUE STATUS",
   5044 	NULL,
   5045 	"GET FIRMWARE STATUS",
   5046 	"GET LOOP ID",
   5047 	NULL,
   5048 	"GET RETRY COUNT",
   5049 	NULL,
   5050 	NULL,
   5051 	NULL,
   5052 	NULL,
   5053 	NULL,
   5054 	"GET FIRMWARE OPTIONS",
   5055 	"GET PORT QUEUE PARAMS",
   5056 	NULL,
   5057 	NULL,
   5058 	NULL,
   5059 	NULL,
   5060 	NULL,
   5061 	NULL,
   5062 	NULL,
   5063 	NULL,
   5064 	"SET RETRY COUNT",
   5065 	NULL,
   5066 	NULL,
   5067 	NULL,
   5068 	NULL,
   5069 	NULL,
   5070 	"SET FIRMWARE OPTIONS",
   5071 	"SET PORT QUEUE PARAMS",
   5072 	NULL,
   5073 	NULL,
   5074 	NULL,
   5075 	NULL,
   5076 	NULL,
   5077 	NULL,
   5078 	"LOOP PORT BYPASS",
   5079 	"LOOP PORT ENABLE",
   5080 	"GET RESOURCE COUNTS",
   5081 	"REQUEST NON PARTICIPATING MODE",
   5082 	NULL,
   5083 	NULL,
   5084 	NULL,
   5085 	"GET PORT DATABASE,, ENHANCED",
   5086 	NULL,
   5087 	NULL,
   5088 	NULL,
   5089 	NULL,
   5090 	NULL,
   5091 	NULL,
   5092 	NULL,
   5093 	NULL,
   5094 	NULL,
   5095 	NULL,
   5096 	NULL,
   5097 	NULL,
   5098 	"EXECUTE IOCB A64",
   5099 	NULL,
   5100 	NULL,
   5101 	NULL,
   5102 	NULL,
   5103 	NULL,
   5104 	NULL,
   5105 	NULL,
   5106 	NULL,
   5107 	"GET/SET DATA RATE",
   5108 	NULL,
   5109 	NULL,
   5110 	"INIT FIRMWARE",
   5111 	NULL,
   5112 	"INIT LIP",
   5113 	"GET FC-AL POSITION MAP",
   5114 	"GET PORT DATABASE",
   5115 	"CLEAR ACA",
   5116 	"TARGET RESET",
   5117 	"CLEAR TASK SET",
   5118 	"ABORT TASK SET",
   5119 	"GET FW STATE",
   5120 	"GET PORT NAME",
   5121 	"GET LINK STATUS",
   5122 	"INIT LIP RESET",
   5123 	NULL,
   5124 	"SEND SNS",
   5125 	"FABRIC LOGIN",
   5126 	"SEND CHANGE REQUEST",
   5127 	"FABRIC LOGOUT",
   5128 	"INIT LIP LOGIN",
   5129 	NULL,
   5130 	"LOGIN LOOP PORT",
   5131 	"GET PORT/NODE NAME LIST",
   5132 	"SET VENDOR ID",
   5133 	"INITIALIZE IP MAILBOX",
   5134 	NULL,
   5135 	NULL,
   5136 	NULL,
   5137 	NULL,
   5138 	"Get ID List",
   5139 	"SEND LFA",
   5140 	"Lun RESET"
   5141 };
   5142 #endif
   5143 
   5144 static void
   5145 isp_mboxcmd_qnw(struct ispsoftc *isp, mbreg_t *mbp, int nodelay)
   5146 {
   5147 	unsigned int lim, ibits, obits, box, opcode;
   5148 	u_int16_t *mcp;
   5149 
   5150 	if (IS_FC(isp)) {
   5151 		mcp = mbpfc;
   5152 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   5153 	} else {
   5154 		mcp = mbpscsi;
   5155 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   5156 	}
   5157 	opcode = mbp->param[0];
   5158 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   5159 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   5160 	for (box = 0; box < MAX_MAILBOX; box++) {
   5161 		if (ibits & (1 << box)) {
   5162 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   5163 		}
   5164 		if (nodelay == 0) {
   5165 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   5166 		}
   5167 	}
   5168 	if (nodelay == 0) {
   5169 		isp->isp_lastmbxcmd = opcode;
   5170 		isp->isp_obits = obits;
   5171 		isp->isp_mboxbsy = 1;
   5172 	}
   5173 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   5174 	/*
   5175 	 * Oddly enough, if we're not delaying for an answer,
   5176 	 * delay a bit to give the f/w a chance to pick up the
   5177 	 * command.
   5178 	 */
   5179 	if (nodelay) {
   5180 		USEC_DELAY(1000);
   5181 	}
   5182 }
   5183 
   5184 static void
   5185 isp_mboxcmd(struct ispsoftc *isp, mbreg_t *mbp, int logmask)
   5186 {
   5187 	char *cname, *xname, tname[16], mname[16];
   5188 	unsigned int lim, ibits, obits, box, opcode;
   5189 	u_int16_t *mcp;
   5190 
   5191 	if (IS_FC(isp)) {
   5192 		mcp = mbpfc;
   5193 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   5194 	} else {
   5195 		mcp = mbpscsi;
   5196 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   5197 	}
   5198 
   5199 	if ((opcode = mbp->param[0]) >= lim) {
   5200 		mbp->param[0] = MBOX_INVALID_COMMAND;
   5201 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
   5202 		return;
   5203 	}
   5204 
   5205 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   5206 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   5207 
   5208 	if (ibits == 0 && obits == 0) {
   5209 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
   5210 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
   5211 		return;
   5212 	}
   5213 
   5214 	/*
   5215 	 * Get exclusive usage of mailbox registers.
   5216 	 */
   5217 	MBOX_ACQUIRE(isp);
   5218 
   5219 	for (box = 0; box < MAX_MAILBOX; box++) {
   5220 		if (ibits & (1 << box)) {
   5221 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   5222 		}
   5223 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   5224 	}
   5225 
   5226 	isp->isp_lastmbxcmd = opcode;
   5227 
   5228 	/*
   5229 	 * We assume that we can't overwrite a previous command.
   5230 	 */
   5231 	isp->isp_obits = obits;
   5232 	isp->isp_mboxbsy = 1;
   5233 
   5234 	/*
   5235 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   5236 	 */
   5237 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   5238 
   5239 	/*
   5240 	 * While we haven't finished the command, spin our wheels here.
   5241 	 */
   5242 	MBOX_WAIT_COMPLETE(isp);
   5243 
   5244 	if (isp->isp_mboxbsy) {
   5245 		/*
   5246 		 * Command timed out.
   5247 		 */
   5248 		isp->isp_mboxbsy = 0;
   5249 		MBOX_RELEASE(isp);
   5250 		return;
   5251 	}
   5252 
   5253 	/*
   5254 	 * Copy back output registers.
   5255 	 */
   5256 	for (box = 0; box < MAX_MAILBOX; box++) {
   5257 		if (obits & (1 << box)) {
   5258 			mbp->param[box] = isp->isp_mboxtmp[box];
   5259 		}
   5260 	}
   5261 
   5262 	MBOX_RELEASE(isp);
   5263 
   5264 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
   5265 		return;
   5266 	}
   5267 #ifdef	ISP_STRIPPED
   5268 	cname = NULL;
   5269 #else
   5270 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
   5271 #endif
   5272 	if (cname == NULL) {
   5273 		cname = tname;
   5274 		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
   5275 	}
   5276 
   5277 	/*
   5278 	 * Just to be chatty here...
   5279 	 */
   5280 	xname = NULL;
   5281 	switch (mbp->param[0]) {
   5282 	case MBOX_COMMAND_COMPLETE:
   5283 		break;
   5284 	case MBOX_INVALID_COMMAND:
   5285 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
   5286 			xname = "INVALID COMMAND";
   5287 		break;
   5288 	case MBOX_HOST_INTERFACE_ERROR:
   5289 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
   5290 			xname = "HOST INTERFACE ERROR";
   5291 		break;
   5292 	case MBOX_TEST_FAILED:
   5293 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
   5294 			xname = "TEST FAILED";
   5295 		break;
   5296 	case MBOX_COMMAND_ERROR:
   5297 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
   5298 			xname = "COMMAND ERROR";
   5299 		break;
   5300 	case MBOX_COMMAND_PARAM_ERROR:
   5301 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
   5302 			xname = "COMMAND PARAMETER ERROR";
   5303 		break;
   5304 	case MBOX_LOOP_ID_USED:
   5305 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
   5306 			xname = "LOOP ID ALREADY IN USE";
   5307 		break;
   5308 	case MBOX_PORT_ID_USED:
   5309 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
   5310 			xname = "PORT ID ALREADY IN USE";
   5311 		break;
   5312 	case MBOX_ALL_IDS_USED:
   5313 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
   5314 			xname = "ALL LOOP IDS IN USE";
   5315 		break;
   5316 	case 0:		/* special case */
   5317 		xname = "TIMEOUT";
   5318 		break;
   5319 	default:
   5320 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
   5321 		xname = mname;
   5322 		break;
   5323 	}
   5324 	if (xname)
   5325 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
   5326 		    cname, xname);
   5327 }
   5328 
   5329 static void
   5330 isp_fw_state(struct ispsoftc *isp)
   5331 {
   5332 	if (IS_FC(isp)) {
   5333 		mbreg_t mbs;
   5334 		fcparam *fcp = isp->isp_param;
   5335 
   5336 		mbs.param[0] = MBOX_GET_FW_STATE;
   5337 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   5338 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   5339 			fcp->isp_fwstate = mbs.param[1];
   5340 		}
   5341 	}
   5342 }
   5343 
   5344 static void
   5345 isp_update(struct ispsoftc *isp)
   5346 {
   5347 	int bus, upmask;
   5348 
   5349 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
   5350 		if (upmask & (1 << bus)) {
   5351 			isp_update_bus(isp, bus);
   5352 		}
   5353 		upmask &= ~(1 << bus);
   5354 	}
   5355 }
   5356 
   5357 static void
   5358 isp_update_bus(struct ispsoftc *isp, int bus)
   5359 {
   5360 	int tgt;
   5361 	mbreg_t mbs;
   5362 	sdparam *sdp;
   5363 
   5364 	isp->isp_update &= ~(1 << bus);
   5365 	if (IS_FC(isp)) {
   5366 		/*
   5367 		 * There are no 'per-bus' settings for Fibre Channel.
   5368 		 */
   5369 		return;
   5370 	}
   5371 	sdp = isp->isp_param;
   5372 	sdp += bus;
   5373 
   5374 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5375 		u_int16_t flags, period, offset;
   5376 		int get;
   5377 
   5378 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   5379 			sdp->isp_devparam[tgt].dev_update = 0;
   5380 			sdp->isp_devparam[tgt].dev_refresh = 0;
   5381 			isp_prt(isp, ISP_LOGDEBUG0,
   5382 	 		    "skipping target %d bus %d update", tgt, bus);
   5383 			continue;
   5384 		}
   5385 		/*
   5386 		 * If the goal is to update the status of the device,
   5387 		 * take what's in goal_flags and try and set the device
   5388 		 * toward that. Otherwise, if we're just refreshing the
   5389 		 * current device state, get the current parameters.
   5390 		 */
   5391 
   5392 		/*
   5393 		 * Refresh overrides set
   5394 		 */
   5395 		if (sdp->isp_devparam[tgt].dev_refresh) {
   5396 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   5397 			sdp->isp_devparam[tgt].dev_refresh = 0;
   5398 			get = 1;
   5399 		} else if (sdp->isp_devparam[tgt].dev_update) {
   5400 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   5401 			/*
   5402 			 * Make sure goal_flags has "Renegotiate on Error"
   5403 			 * on and "Freeze Queue on Error" off.
   5404 			 */
   5405 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
   5406 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
   5407 
   5408 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
   5409 
   5410 			/*
   5411 			 * Insist that PARITY must be enabled
   5412 			 * if SYNC or WIDE is enabled.
   5413 			 */
   5414 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
   5415 				mbs.param[2] |= DPARM_PARITY;
   5416 			}
   5417 
   5418 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
   5419 				mbs.param[3] = 0;
   5420 			} else {
   5421 				mbs.param[3] =
   5422 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
   5423 				    (sdp->isp_devparam[tgt].goal_period);
   5424 			}
   5425 			/*
   5426 			 * A command completion later that has
   5427 			 * RQSTF_NEGOTIATION set can cause
   5428 			 * the dev_refresh/announce cycle also.
   5429 			 *
   5430 			 * Note: It is really important to update our current
   5431 			 * flags with at least the state of TAG capabilities-
   5432 			 * otherwise we might try and send a tagged command
   5433 			 * when we have it all turned off. So change it here
   5434 			 * to say that current already matches goal.
   5435 			 */
   5436 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
   5437 			sdp->isp_devparam[tgt].actv_flags |=
   5438 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
   5439 			isp_prt(isp, ISP_LOGDEBUG0,
   5440 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
   5441 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
   5442 			    mbs.param[3] & 0xff);
   5443 			sdp->isp_devparam[tgt].dev_update = 0;
   5444 			sdp->isp_devparam[tgt].dev_refresh = 1;
   5445 			get = 0;
   5446 		} else {
   5447 			continue;
   5448 		}
   5449 		mbs.param[1] = (bus << 15) | (tgt << 8);
   5450 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   5451 		if (get == 0) {
   5452 			isp->isp_sendmarker |= (1 << bus);
   5453 			continue;
   5454 		}
   5455 		flags = mbs.param[2];
   5456 		period = mbs.param[3] & 0xff;
   5457 		offset = mbs.param[3] >> 8;
   5458 		sdp->isp_devparam[tgt].actv_flags = flags;
   5459 		sdp->isp_devparam[tgt].actv_period = period;
   5460 		sdp->isp_devparam[tgt].actv_offset = offset;
   5461 		get = (bus << 16) | tgt;
   5462 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
   5463 	}
   5464 
   5465 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5466 		if (sdp->isp_devparam[tgt].dev_update ||
   5467 		    sdp->isp_devparam[tgt].dev_refresh) {
   5468 			isp->isp_update |= (1 << bus);
   5469 			break;
   5470 		}
   5471 	}
   5472 }
   5473 
   5474 static void
   5475 isp_setdfltparm(struct ispsoftc *isp, int channel)
   5476 {
   5477 	int tgt;
   5478 	mbreg_t mbs;
   5479 	sdparam *sdp;
   5480 
   5481 	if (IS_FC(isp)) {
   5482 		fcparam *fcp = (fcparam *) isp->isp_param;
   5483 		int nvfail;
   5484 
   5485 		fcp += channel;
   5486 		if (fcp->isp_gotdparms) {
   5487 			return;
   5488 		}
   5489 		fcp->isp_gotdparms = 1;
   5490 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
   5491 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
   5492 		fcp->isp_execthrottle = ISP_EXEC_THROTTLE;
   5493 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
   5494 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
   5495 		/* Platform specific.... */
   5496 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
   5497 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
   5498 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
   5499 		fcp->isp_fwoptions = 0;
   5500 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
   5501 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
   5502 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
   5503 #ifndef	ISP_NO_FASTPOST_FC
   5504 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
   5505 #endif
   5506 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
   5507 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
   5508 
   5509 		/*
   5510 		 * Make sure this is turned off now until we get
   5511 		 * extended options from NVRAM
   5512 		 */
   5513 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
   5514 
   5515 		/*
   5516 		 * Now try and read NVRAM unless told to not do so.
   5517 		 * This will set fcparam's isp_nodewwn && isp_portwwn.
   5518 		 */
   5519 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   5520 		    	nvfail = isp_read_nvram(isp);
   5521 			if (nvfail)
   5522 				isp->isp_confopts |= ISP_CFG_NONVRAM;
   5523 		} else {
   5524 			nvfail = 1;
   5525 		}
   5526 		/*
   5527 		 * Set node && port to override platform set defaults
   5528 		 * unless the nvram read failed (or none was done),
   5529 		 * or the platform code wants to use what had been
   5530 		 * set in the defaults.
   5531 		 */
   5532 		if (nvfail) {
   5533 			isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
   5534 		}
   5535 		if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
   5536 			isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
   5537 			    (u_int32_t) (DEFAULT_NODEWWN(isp) >> 32),
   5538 			    (u_int32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
   5539 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
   5540 		} else {
   5541 			/*
   5542 			 * We always start out with values derived
   5543 			 * from NVRAM or our platform default.
   5544 			 */
   5545 			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
   5546 		}
   5547 		if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
   5548 			isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
   5549 			    (u_int32_t) (DEFAULT_PORTWWN(isp) >> 32),
   5550 			    (u_int32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
   5551 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
   5552 		} else {
   5553 			/*
   5554 			 * We always start out with values derived
   5555 			 * from NVRAM or our platform default.
   5556 			 */
   5557 			ISP_PORTWWN(isp) = fcp->isp_portwwn;
   5558 		}
   5559 		return;
   5560 	}
   5561 
   5562 	sdp = (sdparam *) isp->isp_param;
   5563 	sdp += channel;
   5564 
   5565 	/*
   5566 	 * Been there, done that, got the T-shirt...
   5567 	 */
   5568 	if (sdp->isp_gotdparms) {
   5569 		return;
   5570 	}
   5571 	sdp->isp_gotdparms = 1;
   5572 
   5573 	/*
   5574 	 * Establish some default parameters.
   5575 	 */
   5576 	sdp->isp_cmd_dma_burst_enable = 0;
   5577 	sdp->isp_data_dma_burst_enabl = 1;
   5578 	sdp->isp_fifo_threshold = 0;
   5579 	sdp->isp_initiator_id = DEFAULT_IID(isp);
   5580 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   5581 		sdp->isp_async_data_setup = 9;
   5582 	} else {
   5583 		sdp->isp_async_data_setup = 6;
   5584 	}
   5585 	sdp->isp_selection_timeout = 250;
   5586 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
   5587 	sdp->isp_tag_aging = 8;
   5588 	sdp->isp_bus_reset_delay = 5;
   5589 	/*
   5590 	 * Don't retry selection, busy or queue full automatically- reflect
   5591 	 * these back to us.
   5592 	 */
   5593 	sdp->isp_retry_count = 0;
   5594 	sdp->isp_retry_delay = 0;
   5595 
   5596 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5597 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
   5598 		sdp->isp_devparam[tgt].dev_enable = 1;
   5599 	}
   5600 
   5601 	/*
   5602 	 * If we've not been told to avoid reading NVRAM, try and read it.
   5603 	 * If we're successful reading it, we can then return because NVRAM
   5604 	 * will tell us what the desired settings are. Otherwise, we establish
   5605 	 * some reasonable 'fake' nvram and goal defaults.
   5606 	 */
   5607 
   5608 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   5609 		if (isp_read_nvram(isp) == 0) {
   5610 			return;
   5611 		}
   5612 	}
   5613 
   5614 	/*
   5615 	 * Now try and see whether we have specific values for them.
   5616 	 */
   5617 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   5618 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   5619 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
   5620 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   5621 			sdp->isp_req_ack_active_neg = 1;
   5622 			sdp->isp_data_line_active_neg = 1;
   5623 		} else {
   5624 			sdp->isp_req_ack_active_neg =
   5625 			    (mbs.param[1+channel] >> 4) & 0x1;
   5626 			sdp->isp_data_line_active_neg =
   5627 			    (mbs.param[1+channel] >> 5) & 0x1;
   5628 		}
   5629 	}
   5630 
   5631 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
   5632 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   5633 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   5634 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   5635 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
   5636 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   5637 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   5638 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   5639 
   5640 	/*
   5641 	 * The trick here is to establish a default for the default (honk!)
   5642 	 * state (goal_flags). Then try and get the current status from
   5643 	 * the card to fill in the current state. We don't, in fact, set
   5644 	 * the default to the SAFE default state- that's not the goal state.
   5645 	 */
   5646 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5647 		u_int8_t off, per;
   5648 		sdp->isp_devparam[tgt].actv_offset = 0;
   5649 		sdp->isp_devparam[tgt].actv_period = 0;
   5650 		sdp->isp_devparam[tgt].actv_flags = 0;
   5651 
   5652 		sdp->isp_devparam[tgt].goal_flags =
   5653 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
   5654 
   5655 		/*
   5656 		 * We default to Wide/Fast for versions less than a 1040
   5657 		 * (unless it's SBus).
   5658 		 */
   5659 		if (IS_ULTRA3(isp)) {
   5660 			off = ISP_80M_SYNCPARMS >> 8;
   5661 			per = ISP_80M_SYNCPARMS & 0xff;
   5662 		} else if (IS_ULTRA2(isp)) {
   5663 			off = ISP_40M_SYNCPARMS >> 8;
   5664 			per = ISP_40M_SYNCPARMS & 0xff;
   5665 		} else if (IS_1240(isp)) {
   5666 			off = ISP_20M_SYNCPARMS >> 8;
   5667 			per = ISP_20M_SYNCPARMS & 0xff;
   5668 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
   5669 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
   5670 		    (isp->isp_bustype == ISP_BT_PCI &&
   5671 		    isp->isp_type < ISP_HA_SCSI_1040) ||
   5672 		    (isp->isp_clock && isp->isp_clock < 60) ||
   5673 		    (sdp->isp_ultramode == 0)) {
   5674 			off = ISP_10M_SYNCPARMS >> 8;
   5675 			per = ISP_10M_SYNCPARMS & 0xff;
   5676 		} else {
   5677 			off = ISP_20M_SYNCPARMS_1040 >> 8;
   5678 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
   5679 		}
   5680 		sdp->isp_devparam[tgt].goal_offset =
   5681 		    sdp->isp_devparam[tgt].nvrm_offset = off;
   5682 		sdp->isp_devparam[tgt].goal_period =
   5683 		    sdp->isp_devparam[tgt].nvrm_period = per;
   5684 
   5685 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
   5686 		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5687 		    sdp->isp_devparam[tgt].nvrm_offset,
   5688 		    sdp->isp_devparam[tgt].nvrm_period);
   5689 	}
   5690 }
   5691 
   5692 /*
   5693  * Re-initialize the ISP and complete all orphaned commands
   5694  * with a 'botched' notice. The reset/init routines should
   5695  * not disturb an already active list of commands.
   5696  *
   5697  * Locks held prior to coming here.
   5698  */
   5699 
   5700 void
   5701 isp_reinit(struct ispsoftc *isp)
   5702 {
   5703 	XS_T *xs;
   5704 	u_int16_t handle;
   5705 
   5706 	isp_reset(isp);
   5707 	if (isp->isp_state != ISP_RESETSTATE) {
   5708 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
   5709 		goto skip;
   5710 	}
   5711 	isp_init(isp);
   5712 	if (isp->isp_role == ISP_ROLE_NONE) {
   5713 		goto skip;
   5714 	}
   5715 	if (isp->isp_state == ISP_INITSTATE) {
   5716 		isp->isp_state = ISP_RUNSTATE;
   5717 	}
   5718 	if (isp->isp_state != ISP_RUNSTATE) {
   5719 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot restart card");
   5720 	}
   5721 skip:
   5722 	isp->isp_nactive = 0;
   5723 
   5724 	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
   5725 		xs = isp_find_xs(isp, handle);
   5726 		if (xs == NULL) {
   5727 			continue;
   5728 		}
   5729 		isp_destroy_handle(isp, handle);
   5730 		if (XS_XFRLEN(xs)) {
   5731 			ISP_DMAFREE(isp, xs, handle);
   5732 			XS_RESID(xs) = XS_XFRLEN(xs);
   5733 		} else {
   5734 			XS_RESID(xs) = 0;
   5735 		}
   5736 		XS_SETERR(xs, HBA_BUSRESET);
   5737 		isp_done(xs);
   5738 	}
   5739 }
   5740 
   5741 /*
   5742  * NVRAM Routines
   5743  */
   5744 static int
   5745 isp_read_nvram(struct ispsoftc *isp)
   5746 {
   5747 	int i, amt;
   5748 	u_int8_t csum, minversion;
   5749 	union {
   5750 		u_int8_t _x[ISP2100_NVRAM_SIZE];
   5751 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
   5752 	} _n;
   5753 #define	nvram_data	_n._x
   5754 #define	nvram_words	_n._s
   5755 
   5756 	if (IS_FC(isp)) {
   5757 		amt = ISP2100_NVRAM_SIZE;
   5758 		minversion = 1;
   5759 	} else if (IS_ULTRA2(isp)) {
   5760 		amt = ISP1080_NVRAM_SIZE;
   5761 		minversion = 0;
   5762 	} else {
   5763 		amt = ISP_NVRAM_SIZE;
   5764 		minversion = 2;
   5765 	}
   5766 
   5767 	/*
   5768 	 * Just read the first two words first to see if we have a valid
   5769 	 * NVRAM to continue reading the rest with.
   5770 	 */
   5771 	for (i = 0; i < 2; i++) {
   5772 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   5773 	}
   5774 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   5775 	    nvram_data[2] != 'P') {
   5776 		if (isp->isp_bustype != ISP_BT_SBUS) {
   5777 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
   5778 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
   5779 			    nvram_data[0], nvram_data[1], nvram_data[2]);
   5780 		}
   5781 		return (-1);
   5782 	}
   5783 	for (i = 2; i < amt>>1; i++) {
   5784 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   5785 	}
   5786 	for (csum = 0, i = 0; i < amt; i++) {
   5787 		csum += nvram_data[i];
   5788 	}
   5789 	if (csum != 0) {
   5790 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
   5791 		return (-1);
   5792 	}
   5793 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   5794 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
   5795 		    ISP_NVRAM_VERSION(nvram_data));
   5796 		return (-1);
   5797 	}
   5798 
   5799 	if (IS_ULTRA3(isp)) {
   5800 		isp_parse_nvram_12160(isp, 0, nvram_data);
   5801 		isp_parse_nvram_12160(isp, 1, nvram_data);
   5802 	} else if (IS_1080(isp)) {
   5803 		isp_parse_nvram_1080(isp, 0, nvram_data);
   5804 	} else if (IS_1280(isp) || IS_1240(isp)) {
   5805 		isp_parse_nvram_1080(isp, 0, nvram_data);
   5806 		isp_parse_nvram_1080(isp, 1, nvram_data);
   5807 	} else if (IS_SCSI(isp)) {
   5808 		isp_parse_nvram_1020(isp, nvram_data);
   5809 	} else {
   5810 		isp_parse_nvram_2100(isp, nvram_data);
   5811 	}
   5812 	return (0);
   5813 #undef	nvram_data
   5814 #undef	nvram_words
   5815 }
   5816 
   5817 static void
   5818 isp_rdnvram_word(struct ispsoftc *isp, int wo, u_int16_t *rp)
   5819 {
   5820 	int i, cbits;
   5821 	u_int16_t bit, rqst;
   5822 
   5823 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   5824 	USEC_DELAY(2);
   5825 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   5826 	USEC_DELAY(2);
   5827 
   5828 	if (IS_FC(isp)) {
   5829 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   5830 		if (IS_2312(isp) && isp->isp_port) {
   5831 			wo += 128;
   5832 		}
   5833 		rqst = (ISP_NVRAM_READ << 8) | wo;
   5834 		cbits = 10;
   5835 	} else if (IS_ULTRA2(isp)) {
   5836 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
   5837 		rqst = (ISP_NVRAM_READ << 8) | wo;
   5838 		cbits = 10;
   5839 	} else {
   5840 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   5841 		rqst = (ISP_NVRAM_READ << 6) | wo;
   5842 		cbits = 8;
   5843 	}
   5844 
   5845 	/*
   5846 	 * Clock the word select request out...
   5847 	 */
   5848 	for (i = cbits; i >= 0; i--) {
   5849 		if ((rqst >> i) & 1) {
   5850 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   5851 		} else {
   5852 			bit = BIU_NVRAM_SELECT;
   5853 		}
   5854 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5855 		USEC_DELAY(2);
   5856 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   5857 		USEC_DELAY(2);
   5858 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5859 		USEC_DELAY(2);
   5860 	}
   5861 	/*
   5862 	 * Now read the result back in (bits come back in MSB format).
   5863 	 */
   5864 	*rp = 0;
   5865 	for (i = 0; i < 16; i++) {
   5866 		u_int16_t rv;
   5867 		*rp <<= 1;
   5868 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   5869 		USEC_DELAY(2);
   5870 		rv = ISP_READ(isp, BIU_NVRAM);
   5871 		if (rv & BIU_NVRAM_DATAIN) {
   5872 			*rp |= 1;
   5873 		}
   5874 		USEC_DELAY(2);
   5875 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   5876 		USEC_DELAY(2);
   5877 	}
   5878 	ISP_WRITE(isp, BIU_NVRAM, 0);
   5879 	USEC_DELAY(2);
   5880 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
   5881 }
   5882 
   5883 static void
   5884 isp_parse_nvram_1020(struct ispsoftc *isp, u_int8_t *nvram_data)
   5885 {
   5886 	sdparam *sdp = (sdparam *) isp->isp_param;
   5887 	int tgt;
   5888 
   5889 	sdp->isp_fifo_threshold =
   5890 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
   5891 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
   5892 
   5893 	sdp->isp_initiator_id =
   5894 		ISP_NVRAM_INITIATOR_ID(nvram_data);
   5895 
   5896 	sdp->isp_bus_reset_delay =
   5897 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   5898 
   5899 	sdp->isp_retry_count =
   5900 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   5901 
   5902 	sdp->isp_retry_delay =
   5903 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   5904 
   5905 	sdp->isp_async_data_setup =
   5906 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   5907 
   5908 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   5909 		if (sdp->isp_async_data_setup < 9) {
   5910 			sdp->isp_async_data_setup = 9;
   5911 		}
   5912 	} else {
   5913 		if (sdp->isp_async_data_setup != 6) {
   5914 			sdp->isp_async_data_setup = 6;
   5915 		}
   5916 	}
   5917 
   5918 	sdp->isp_req_ack_active_neg =
   5919 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   5920 
   5921 	sdp->isp_data_line_active_neg =
   5922 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   5923 
   5924 	sdp->isp_data_dma_burst_enabl =
   5925 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   5926 
   5927 	sdp->isp_cmd_dma_burst_enable =
   5928 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   5929 
   5930 	sdp->isp_tag_aging =
   5931 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   5932 
   5933 	sdp->isp_selection_timeout =
   5934 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   5935 
   5936 	sdp->isp_max_queue_depth =
   5937 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   5938 
   5939 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   5940 
   5941 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   5942 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   5943 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   5944 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   5945 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   5946 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   5947 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   5948 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   5949 
   5950 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   5951 		sdp->isp_devparam[tgt].dev_enable =
   5952 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
   5953 		sdp->isp_devparam[tgt].exc_throttle =
   5954 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
   5955 		sdp->isp_devparam[tgt].nvrm_offset =
   5956 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
   5957 		sdp->isp_devparam[tgt].nvrm_period =
   5958 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
   5959 		/*
   5960 		 * We probably shouldn't lie about this, but it
   5961 		 * it makes it much safer if we limit NVRAM values
   5962 		 * to sanity.
   5963 		 */
   5964 		if (isp->isp_type < ISP_HA_SCSI_1040) {
   5965 			/*
   5966 			 * If we're not ultra, we can't possibly
   5967 			 * be a shorter period than this.
   5968 			 */
   5969 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
   5970 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
   5971 			}
   5972 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
   5973 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
   5974 			}
   5975 		} else {
   5976 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
   5977 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
   5978 			}
   5979 		}
   5980 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   5981 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
   5982 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   5983 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   5984 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
   5985 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   5986 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
   5987 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   5988 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
   5989 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   5990 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
   5991 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   5992 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
   5993 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   5994 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
   5995 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   5996 		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   5997 		    sdp->isp_devparam[tgt].nvrm_offset,
   5998 		    sdp->isp_devparam[tgt].nvrm_period);
   5999 		sdp->isp_devparam[tgt].goal_offset =
   6000 		    sdp->isp_devparam[tgt].nvrm_offset;
   6001 		sdp->isp_devparam[tgt].goal_period =
   6002 		    sdp->isp_devparam[tgt].nvrm_period;
   6003 		sdp->isp_devparam[tgt].goal_flags =
   6004 		    sdp->isp_devparam[tgt].nvrm_flags;
   6005 	}
   6006 }
   6007 
   6008 static void
   6009 isp_parse_nvram_1080(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   6010 {
   6011 	sdparam *sdp = (sdparam *) isp->isp_param;
   6012 	int tgt;
   6013 
   6014 	sdp += bus;
   6015 
   6016 	sdp->isp_fifo_threshold =
   6017 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
   6018 
   6019 	sdp->isp_initiator_id =
   6020 	    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
   6021 
   6022 	sdp->isp_bus_reset_delay =
   6023 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   6024 
   6025 	sdp->isp_retry_count =
   6026 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   6027 
   6028 	sdp->isp_retry_delay =
   6029 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   6030 
   6031 	sdp->isp_async_data_setup =
   6032 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   6033 
   6034 	sdp->isp_req_ack_active_neg =
   6035 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   6036 
   6037 	sdp->isp_data_line_active_neg =
   6038 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   6039 
   6040 	sdp->isp_data_dma_burst_enabl =
   6041 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   6042 
   6043 	sdp->isp_cmd_dma_burst_enable =
   6044 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   6045 
   6046 	sdp->isp_selection_timeout =
   6047 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   6048 
   6049 	sdp->isp_max_queue_depth =
   6050 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   6051 
   6052 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   6053 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   6054 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   6055 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   6056 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   6057 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   6058 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   6059 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   6060 
   6061 
   6062 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   6063 		sdp->isp_devparam[tgt].dev_enable =
   6064 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   6065 		sdp->isp_devparam[tgt].exc_throttle =
   6066 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   6067 		sdp->isp_devparam[tgt].nvrm_offset =
   6068 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   6069 		sdp->isp_devparam[tgt].nvrm_period =
   6070 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   6071 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   6072 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   6073 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   6074 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   6075 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   6076 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   6077 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   6078 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   6079 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   6080 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   6081 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   6082 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   6083 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   6084 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   6085 		sdp->isp_devparam[tgt].actv_flags = 0;
   6086 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   6087 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   6088 		    sdp->isp_devparam[tgt].nvrm_offset,
   6089 		    sdp->isp_devparam[tgt].nvrm_period);
   6090 		sdp->isp_devparam[tgt].goal_offset =
   6091 		    sdp->isp_devparam[tgt].nvrm_offset;
   6092 		sdp->isp_devparam[tgt].goal_period =
   6093 		    sdp->isp_devparam[tgt].nvrm_period;
   6094 		sdp->isp_devparam[tgt].goal_flags =
   6095 		    sdp->isp_devparam[tgt].nvrm_flags;
   6096 	}
   6097 }
   6098 
   6099 static void
   6100 isp_parse_nvram_12160(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   6101 {
   6102 	sdparam *sdp = (sdparam *) isp->isp_param;
   6103 	int tgt;
   6104 
   6105 	sdp += bus;
   6106 
   6107 	sdp->isp_fifo_threshold =
   6108 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
   6109 
   6110 	sdp->isp_initiator_id =
   6111 	    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
   6112 
   6113 	sdp->isp_bus_reset_delay =
   6114 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   6115 
   6116 	sdp->isp_retry_count =
   6117 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   6118 
   6119 	sdp->isp_retry_delay =
   6120 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   6121 
   6122 	sdp->isp_async_data_setup =
   6123 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   6124 
   6125 	sdp->isp_req_ack_active_neg =
   6126 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   6127 
   6128 	sdp->isp_data_line_active_neg =
   6129 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   6130 
   6131 	sdp->isp_data_dma_burst_enabl =
   6132 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   6133 
   6134 	sdp->isp_cmd_dma_burst_enable =
   6135 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   6136 
   6137 	sdp->isp_selection_timeout =
   6138 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   6139 
   6140 	sdp->isp_max_queue_depth =
   6141 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   6142 
   6143 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   6144 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   6145 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   6146 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   6147 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   6148 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   6149 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   6150 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   6151 
   6152 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   6153 		sdp->isp_devparam[tgt].dev_enable =
   6154 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   6155 		sdp->isp_devparam[tgt].exc_throttle =
   6156 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   6157 		sdp->isp_devparam[tgt].nvrm_offset =
   6158 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   6159 		sdp->isp_devparam[tgt].nvrm_period =
   6160 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   6161 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   6162 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   6163 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   6164 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   6165 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   6166 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   6167 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   6168 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   6169 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   6170 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   6171 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   6172 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   6173 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   6174 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   6175 		sdp->isp_devparam[tgt].actv_flags = 0;
   6176 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   6177 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   6178 		    sdp->isp_devparam[tgt].nvrm_offset,
   6179 		    sdp->isp_devparam[tgt].nvrm_period);
   6180 		sdp->isp_devparam[tgt].goal_offset =
   6181 		    sdp->isp_devparam[tgt].nvrm_offset;
   6182 		sdp->isp_devparam[tgt].goal_period =
   6183 		    sdp->isp_devparam[tgt].nvrm_period;
   6184 		sdp->isp_devparam[tgt].goal_flags =
   6185 		    sdp->isp_devparam[tgt].nvrm_flags;
   6186 	}
   6187 }
   6188 
   6189 static void
   6190 isp_parse_nvram_2100(struct ispsoftc *isp, u_int8_t *nvram_data)
   6191 {
   6192 	fcparam *fcp = (fcparam *) isp->isp_param;
   6193 	u_int64_t wwn;
   6194 
   6195 	/*
   6196 	 * There is NVRAM storage for both Port and Node entities-
   6197 	 * but the Node entity appears to be unused on all the cards
   6198 	 * I can find. However, we should account for this being set
   6199 	 * at some point in the future.
   6200 	 *
   6201 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
   6202 	 * bits 48..60. In the case of the 2202, it appears that they do
   6203 	 * use bit 48 to distinguish between the two instances on the card.
   6204 	 * The 2204, which I've never seen, *probably* extends this method.
   6205 	 */
   6206 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
   6207 	if (wwn) {
   6208 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
   6209 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
   6210 		if ((wwn >> 60) == 0) {
   6211 			wwn |= (((u_int64_t) 2)<< 60);
   6212 		}
   6213 	}
   6214 	fcp->isp_portwwn = wwn;
   6215 	if (IS_2200(isp) || IS_23XX(isp)) {
   6216 		wwn = ISP2200_NVRAM_NODE_NAME(nvram_data);
   6217 		if (wwn) {
   6218 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
   6219 			    (u_int32_t) (wwn >> 32),
   6220 			    (u_int32_t) (wwn & 0xffffffff));
   6221 			if ((wwn >> 60) == 0) {
   6222 				wwn |= (((u_int64_t) 2)<< 60);
   6223 			}
   6224 		}
   6225 	} else {
   6226 		wwn &= ~((u_int64_t) 0xfff << 48);
   6227 	}
   6228 	fcp->isp_nodewwn = wwn;
   6229 
   6230 	/*
   6231 	 * Make sure we have both Node and Port as non-zero values.
   6232 	 */
   6233 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
   6234 		fcp->isp_portwwn = fcp->isp_nodewwn;
   6235 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
   6236 		fcp->isp_nodewwn = fcp->isp_portwwn;
   6237 	}
   6238 
   6239 	/*
   6240 	 * Make the Node and Port values sane if they're NAA == 2.
   6241 	 * This means to clear bits 48..56 for the Node WWN and
   6242 	 * make sure that there's some non-zero value in 48..56
   6243 	 * for the Port WWN.
   6244 	 */
   6245 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
   6246 		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
   6247 		    (fcp->isp_nodewwn >> 60) == 2) {
   6248 			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
   6249 		}
   6250 		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
   6251 		    (fcp->isp_portwwn >> 60) == 2) {
   6252 			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
   6253 		}
   6254 	}
   6255 
   6256 	fcp->isp_maxalloc =
   6257 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   6258 	fcp->isp_maxfrmlen =
   6259 		ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   6260 	fcp->isp_retry_delay =
   6261 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   6262 	fcp->isp_retry_count =
   6263 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   6264 	fcp->isp_loopid =
   6265 		ISP2100_NVRAM_HARDLOOPID(nvram_data);
   6266 	fcp->isp_execthrottle =
   6267 		ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   6268 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
   6269 	isp_prt(isp, ISP_LOGDEBUG0,
   6270 	    "NVRAM: maxfrmlen %d execthrottle %d fwoptions 0x%x",
   6271 	    fcp->isp_maxfrmlen, fcp->isp_execthrottle, fcp->isp_fwoptions);
   6272 }
   6273 
   6274 #ifdef	ISP_FW_CRASH_DUMP
   6275 static void isp2200_fw_dump(struct ispsoftc *);
   6276 static void isp2300_fw_dump(struct ispsoftc *);
   6277 
   6278 static void
   6279 isp2200_fw_dump(struct ispsoftc *isp)
   6280 {
   6281 	int i, j;
   6282 	mbreg_t mbs;
   6283 	u_int16_t *ptr;
   6284 
   6285 	ptr = FCPARAM(isp)->isp_dump_data;
   6286 	if (ptr == NULL) {
   6287 		isp_prt(isp, ISP_LOGERR,
   6288 		   "No place to dump RISC registers and SRAM");
   6289 		return;
   6290 	}
   6291 	if (*ptr++) {
   6292 		isp_prt(isp, ISP_LOGERR,
   6293 		   "dump area for RISC registers and SRAM already used");
   6294 		return;
   6295 	}
   6296 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   6297 	for (i = 0; i < 100; i++) {
   6298 		USEC_DELAY(100);
   6299 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6300 			break;
   6301 		}
   6302 	}
   6303 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6304 		/*
   6305 		 * PBIU Registers
   6306 		 */
   6307 		for (i = 0; i < 8; i++) {
   6308 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
   6309 		}
   6310 
   6311 		/*
   6312 		 * Mailbox Registers
   6313 		 */
   6314 		for (i = 0; i < 8; i++) {
   6315 			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
   6316 		}
   6317 
   6318 		/*
   6319 		 * DMA Registers
   6320 		 */
   6321 		for (i = 0; i < 48; i++) {
   6322 			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
   6323 		}
   6324 
   6325 		/*
   6326 		 * RISC H/W Registers
   6327 		 */
   6328 		ISP_WRITE(isp, BIU2100_CSR, 0);
   6329 		for (i = 0; i < 16; i++) {
   6330 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
   6331 		}
   6332 
   6333 		/*
   6334 		 * RISC GP Registers
   6335 		 */
   6336 		for (j = 0; j < 8; j++) {
   6337 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
   6338 			for (i = 0; i < 16; i++) {
   6339 				*ptr++ =
   6340 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6341 			}
   6342 		}
   6343 
   6344 		/*
   6345 		 * Frame Buffer Hardware Registers
   6346 		 */
   6347 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
   6348 		for (i = 0; i < 16; i++) {
   6349 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6350 		}
   6351 
   6352 		/*
   6353 		 * Fibre Protocol Module 0 Hardware Registers
   6354 		 */
   6355 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
   6356 		for (i = 0; i < 64; i++) {
   6357 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6358 		}
   6359 
   6360 		/*
   6361 		 * Fibre Protocol Module 1 Hardware Registers
   6362 		 */
   6363 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
   6364 		for (i = 0; i < 64; i++) {
   6365 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6366 		}
   6367 	} else {
   6368 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
   6369 		return;
   6370 	}
   6371 	isp_prt(isp, ISP_LOGALL,
   6372 	   "isp_fw_dump: RISC registers dumped successfully");
   6373 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
   6374 	for (i = 0; i < 100; i++) {
   6375 		USEC_DELAY(100);
   6376 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
   6377 			break;
   6378 		}
   6379 	}
   6380 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
   6381 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
   6382 		return;
   6383 	}
   6384 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   6385 	for (i = 0; i < 100; i++) {
   6386 		USEC_DELAY(100);
   6387 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6388 			break;
   6389 		}
   6390 	}
   6391 	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
   6392 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
   6393 		return;
   6394 	}
   6395 	ISP_WRITE(isp, RISC_EMB, 0xf2);
   6396 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
   6397 	for (i = 0; i < 100; i++) {
   6398 		USEC_DELAY(100);
   6399 		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
   6400 			break;
   6401 		}
   6402 	}
   6403 	ENABLE_INTS(isp);
   6404 	mbs.param[0] = MBOX_READ_RAM_WORD;
   6405 	mbs.param[1] = 0x1000;
   6406 	isp->isp_mbxworkp = (void *) ptr;
   6407 	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
   6408 	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
   6409 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
   6410 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   6411 		isp_prt(isp, ISP_LOGWARN,
   6412 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
   6413 		return;
   6414 	}
   6415 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
   6416 	*ptr++ = isp->isp_mboxtmp[2];
   6417 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped succesfully");
   6418 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
   6419 }
   6420 
   6421 static void
   6422 isp2300_fw_dump(struct ispsoftc *isp)
   6423 {
   6424 	int i, j;
   6425 	mbreg_t mbs;
   6426 	u_int16_t *ptr;
   6427 
   6428 	ptr = FCPARAM(isp)->isp_dump_data;
   6429 	if (ptr == NULL) {
   6430 		isp_prt(isp, ISP_LOGERR,
   6431 		   "No place to dump RISC registers and SRAM");
   6432 		return;
   6433 	}
   6434 	if (*ptr++) {
   6435 		isp_prt(isp, ISP_LOGERR,
   6436 		   "dump area for RISC registers and SRAM already used");
   6437 		return;
   6438 	}
   6439 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   6440 	for (i = 0; i < 100; i++) {
   6441 		USEC_DELAY(100);
   6442 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6443 			break;
   6444 		}
   6445 	}
   6446 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   6447 		/*
   6448 		 * PBIU registers
   6449 		 */
   6450 		for (i = 0; i < 8; i++) {
   6451 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
   6452 		}
   6453 
   6454 		/*
   6455 		 * ReqQ-RspQ-Risc2Host Status registers
   6456 		 */
   6457 		for (i = 0; i < 8; i++) {
   6458 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
   6459 		}
   6460 
   6461 		/*
   6462 		 * Mailbox Registers
   6463 		 */
   6464 		for (i = 0; i < 32; i++) {
   6465 			*ptr++ =
   6466 			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
   6467 		}
   6468 
   6469 		/*
   6470 		 * Auto Request Response DMA registers
   6471 		 */
   6472 		ISP_WRITE(isp, BIU2100_CSR, 0x40);
   6473 		for (i = 0; i < 32; i++) {
   6474 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6475 		}
   6476 
   6477 		/*
   6478 		 * DMA registers
   6479 		 */
   6480 		ISP_WRITE(isp, BIU2100_CSR, 0x50);
   6481 		for (i = 0; i < 48; i++) {
   6482 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6483 		}
   6484 
   6485 		/*
   6486 		 * RISC hardware registers
   6487 		 */
   6488 		ISP_WRITE(isp, BIU2100_CSR, 0);
   6489 		for (i = 0; i < 16; i++) {
   6490 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
   6491 		}
   6492 
   6493 		/*
   6494 		 * RISC GP? registers
   6495 		 */
   6496 		for (j = 0; j < 8; j++) {
   6497 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
   6498 			for (i = 0; i < 16; i++) {
   6499 				*ptr++ =
   6500 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6501 			}
   6502 		}
   6503 
   6504 		/*
   6505 		 * frame buffer hardware registers
   6506 		 */
   6507 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
   6508 		for (i = 0; i < 64; i++) {
   6509 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6510 		}
   6511 
   6512 		/*
   6513 		 * FPM B0 hardware registers
   6514 		 */
   6515 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
   6516 		for (i = 0; i < 64; i++) {
   6517 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6518 		}
   6519 
   6520 		/*
   6521 		 * FPM B1 hardware registers
   6522 		 */
   6523 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
   6524 		for (i = 0; i < 64; i++) {
   6525 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   6526 		}
   6527 	} else {
   6528 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
   6529 		return;
   6530 	}
   6531 	isp_prt(isp, ISP_LOGALL,
   6532 	   "isp_fw_dump: RISC registers dumped successfully");
   6533 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
   6534 	for (i = 0; i < 100; i++) {
   6535 		USEC_DELAY(100);
   6536 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
   6537 			break;
   6538 		}
   6539 	}
   6540 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
   6541 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
   6542 		return;
   6543 	}
   6544 	ENABLE_INTS(isp);
   6545 	mbs.param[0] = MBOX_READ_RAM_WORD;
   6546 	mbs.param[1] = 0x800;
   6547 	isp->isp_mbxworkp = (void *) ptr;
   6548 	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
   6549 	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
   6550 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
   6551 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   6552 		isp_prt(isp, ISP_LOGWARN,
   6553 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
   6554 		return;
   6555 	}
   6556 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
   6557 	*ptr++ = isp->isp_mboxtmp[2];
   6558 
   6559 	/*
   6560 	 * We don't have access to mailbox registers 8.. onward
   6561 	 * in our 'common' device model- so we have to set it
   6562 	 * here and hope it stays the same!
   6563 	 */
   6564 	ISP_WRITE(isp, PCI_MBOX_REGS2300_OFF + (8 << 1), 0x1);
   6565 
   6566 	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
   6567 	mbs.param[1] = 0;
   6568 	isp->isp_mbxworkp = (void *) ptr;
   6569 	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
   6570 	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
   6571 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
   6572 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   6573 		isp_prt(isp, ISP_LOGWARN,
   6574 		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
   6575 		return;
   6576 	}
   6577 	ptr = isp->isp_mbxworkp;	/* finish final word */
   6578 	*ptr++ = mbs.param[2];
   6579 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped succesfully");
   6580 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
   6581 }
   6582 
   6583 void
   6584 isp_fw_dump(struct ispsoftc *isp)
   6585 {
   6586 	if (IS_2200(isp))
   6587 		isp2200_fw_dump(isp);
   6588 	else if (IS_23XX(isp))
   6589 		isp2300_fw_dump(isp);
   6590 }
   6591 #endif
   6592