Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.111
      1 /* $NetBSD: isp.c,v 1.111 2007/05/24 21:30:41 mjacob Exp $ */
      2 /*
      3  * Machine and OS Independent (well, as best as possible)
      4  * code for the Qlogic ISP SCSI adapters.
      5  *
      6  * Copyright (C) 1997, 1998, 1999 National Aeronautics & Space Administration
      7  * All rights reserved.
      8  *
      9  * Additional Copyright (C) 2000-2007 by Matthew Jacob
     10  * All rights reserved.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  *
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
     37  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
     38  * ideas dredged from the Solaris driver.
     39  */
     40 
     41 /*
     42  * Include header file appropriate for platform we're building on.
     43  */
     44 #ifdef	__NetBSD__
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: isp.c,v 1.111 2007/05/24 21:30:41 mjacob Exp $");
     47 #include <dev/ic/isp_netbsd.h>
     48 #endif
     49 #ifdef	__FreeBSD__
     50 #include <sys/cdefs.h>
     51 __FBSDID("$FreeBSD$");
     52 #include <dev/isp/isp_freebsd.h>
     53 #endif
     54 #ifdef	__OpenBSD__
     55 #include <dev/ic/isp_openbsd.h>
     56 #endif
     57 #ifdef	__linux__
     58 #include "isp_linux.h"
     59 #endif
     60 #ifdef	__svr4__
     61 #include "isp_solaris.h"
     62 #endif
     63 
     64 /*
     65  * General defines
     66  */
     67 
     68 #define	MBOX_DELAY_COUNT	1000000 / 100
     69 #define	ISP_MARK_PORTDB(a, b)	\
     70     isp_prt(isp, ISP_LOGSANCFG, "line %d: markportdb", __LINE__); \
     71     isp_mark_portdb(a, b)
     72 
     73 /*
     74  * Local static data
     75  */
     76 static const char fconf[] =
     77     "PortDB[%d] changed:\n current =(0x%x@0x%06x 0x%08x%08x 0x%08x%08x)\n"
     78     " database=(0x%x@0x%06x 0x%08x%08x 0x%08x%08x)";
     79 static const char notresp[] =
     80   "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
     81 static const char xact1[] =
     82     "HBA attempted queued transaction with disconnect not set for %d.%d.%d";
     83 static const char xact2[] =
     84     "HBA attempted queued transaction to target routine %d on target %d bus %d";
     85 static const char xact3[] =
     86     "HBA attempted queued cmd for %d.%d.%d when queueing disabled";
     87 static const char pskip[] =
     88     "SCSI phase skipped for target %d.%d.%d";
     89 static const char topology[] =
     90     "HBA PortID 0x%06x N-Port Handle %d, Connection Topology '%s'";
     91 static const char ourwwn[] =
     92     "HBA WWNN 0x%08x%08x HBA WWPN 0x%08x%08x";
     93 static const char finmsg[] =
     94     "%d.%d.%d: FIN dl%d resid %d STS 0x%x SKEY %c XS_ERR=0x%x";
     95 static const char sc0[] =
     96     "%s CHAN %d FTHRSH %d IID %d RESETD %d RETRYC %d RETRYD %d ASD 0x%x";
     97 static const char sc1[] =
     98     "%s RAAN 0x%x DLAN 0x%x DDMAB 0x%x CDMAB 0x%x SELTIME %d MQD %d";
     99 static const char sc2[] = "%s CHAN %d TGT %d FLAGS 0x%x 0x%x/0x%x";
    100 static const char sc3[] = "Generated";
    101 static const char sc4[] = "NVRAM";
    102 static const char bun[] =
    103     "bad underrun for %d.%d (count %d, resid %d, status %s)";
    104 
    105 /*
    106  * Local function prototypes.
    107  */
    108 static int isp_parse_async(ispsoftc_t *, uint16_t);
    109 static int isp_handle_other_response(ispsoftc_t *, int, isphdr_t *,
    110     uint32_t *);
    111 static void
    112 isp_parse_status(ispsoftc_t *, ispstatusreq_t *, XS_T *, long *);
    113 static void
    114 isp_parse_status_24xx(ispsoftc_t *, isp24xx_statusreq_t *, XS_T *, long *);
    115 static void isp_fastpost_complete(ispsoftc_t *, uint16_t);
    116 static int isp_mbox_continue(ispsoftc_t *);
    117 static void isp_scsi_init(ispsoftc_t *);
    118 static void isp_scsi_channel_init(ispsoftc_t *, int);
    119 static void isp_fibre_init(ispsoftc_t *);
    120 static void isp_fibre_init_2400(ispsoftc_t *);
    121 static void isp_mark_portdb(ispsoftc_t *, int);
    122 static int isp_plogx(ispsoftc_t *, uint16_t, uint32_t, int, int);
    123 static int isp_port_login(ispsoftc_t *, uint16_t, uint32_t);
    124 static int isp_port_logout(ispsoftc_t *, uint16_t, uint32_t);
    125 static int isp_getpdb(ispsoftc_t *, uint16_t, isp_pdb_t *, int);
    126 static uint64_t isp_get_portname(ispsoftc_t *, int, int);
    127 static int isp_fclink_test(ispsoftc_t *, int);
    128 static const char *ispfc_fw_statename(int);
    129 static int isp_pdb_sync(ispsoftc_t *);
    130 static int isp_scan_loop(ispsoftc_t *);
    131 static int isp_gid_ft_sns(ispsoftc_t *);
    132 static int isp_gid_ft_ct_passthru(ispsoftc_t *);
    133 static int isp_scan_fabric(ispsoftc_t *);
    134 static int isp_login_device(ispsoftc_t *, uint32_t, isp_pdb_t *, uint16_t *);
    135 static int isp_register_fc4_type(ispsoftc_t *);
    136 static int isp_register_fc4_type_24xx(ispsoftc_t *);
    137 static uint16_t isp_nxt_handle(ispsoftc_t *, uint16_t);
    138 static void isp_fw_state(ispsoftc_t *);
    139 static void isp_mboxcmd_qnw(ispsoftc_t *, mbreg_t *, int);
    140 static void isp_mboxcmd(ispsoftc_t *, mbreg_t *);
    141 
    142 static void isp_update(ispsoftc_t *);
    143 static void isp_update_bus(ispsoftc_t *, int);
    144 static void isp_setdfltparm(ispsoftc_t *, int);
    145 static void isp_setdfltfcparm(ispsoftc_t *);
    146 static int isp_read_nvram(ispsoftc_t *);
    147 static int isp_read_nvram_2400(ispsoftc_t *);
    148 static void isp_rdnvram_word(ispsoftc_t *, int, uint16_t *);
    149 static void isp_rd_2400_nvram(ispsoftc_t *, uint32_t, uint32_t *);
    150 static void isp_parse_nvram_1020(ispsoftc_t *, uint8_t *);
    151 static void isp_parse_nvram_1080(ispsoftc_t *, int, uint8_t *);
    152 static void isp_parse_nvram_12160(ispsoftc_t *, int, uint8_t *);
    153 static void isp_fix_nvram_wwns(ispsoftc_t *);
    154 static void isp_parse_nvram_2100(ispsoftc_t *, uint8_t *);
    155 static void isp_parse_nvram_2400(ispsoftc_t *, uint8_t *);
    156 
    157 /*
    158  * Reset Hardware.
    159  *
    160  * Hit the chip over the head, download new f/w if available and set it running.
    161  *
    162  * Locking done elsewhere.
    163  */
    164 
    165 void
    166 isp_reset(ispsoftc_t *isp)
    167 {
    168 	mbreg_t mbs;
    169 	uint32_t code_org, val;
    170 	int loops, i, dodnld = 1;
    171 	static const char *btype = "????";
    172 	static const char dcrc[] = "Downloaded RISC Code Checksum Failure";
    173 
    174 	isp->isp_state = ISP_NILSTATE;
    175 
    176 	/*
    177 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
    178 	 * have been set in the MD code. We figure out more
    179 	 * here. Possibly more refined types based upon PCI
    180 	 * identification. Chip revision has been gathered.
    181 	 *
    182 	 * After we've fired this chip up, zero out the conf1 register
    183 	 * for SCSI adapters and do other settings for the 2100.
    184 	 */
    185 
    186 	/*
    187 	 * Get the current running firmware revision out of the
    188 	 * chip before we hit it over the head (if this is our
    189 	 * first time through). Note that we store this as the
    190 	 * 'ROM' firmware revision- which it may not be. In any
    191 	 * case, we don't really use this yet, but we may in
    192 	 * the future.
    193 	 */
    194 	if (isp->isp_touched == 0) {
    195 		/*
    196 		 * First see whether or not we're sitting in the ISP PROM.
    197 		 * If we've just been reset, we'll have the string "ISP   "
    198 		 * spread through outgoing mailbox registers 1-3. We do
    199 		 * this for PCI cards because otherwise we really don't
    200 		 * know what state the card is in and we could hang if
    201 		 * we try this command otherwise.
    202 		 *
    203 		 * For SBus cards, we just do this because they almost
    204 		 * certainly will be running firmware by now.
    205 		 */
    206 		if (ISP_READ(isp, OUTMAILBOX1) != 0x4953 ||
    207 		    ISP_READ(isp, OUTMAILBOX2) != 0x5020 ||
    208 		    ISP_READ(isp, OUTMAILBOX3) != 0x2020) {
    209 			/*
    210 			 * Just in case it was paused...
    211 			 */
    212 			if (IS_24XX(isp)) {
    213 				ISP_WRITE(isp, BIU2400_HCCR,
    214 				    HCCR_2400_CMD_RELEASE);
    215 			} else {
    216 				ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
    217 			}
    218 			MEMZERO(&mbs, sizeof (mbs));
    219 			mbs.param[0] = MBOX_ABOUT_FIRMWARE;
    220 			mbs.logval = MBLOGNONE;
    221 			isp_mboxcmd(isp, &mbs);
    222 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
    223 				isp->isp_romfw_rev[0] = mbs.param[1];
    224 				isp->isp_romfw_rev[1] = mbs.param[2];
    225 				isp->isp_romfw_rev[2] = mbs.param[3];
    226 			}
    227 		}
    228 		isp->isp_touched = 1;
    229 	}
    230 
    231 	ISP_DISABLE_INTS(isp);
    232 
    233 	/*
    234 	 * Pick an initial maxcmds value which will be used
    235 	 * to allocate xflist pointer space. It may be changed
    236 	 * later by the firmware.
    237 	 */
    238 	if (IS_24XX(isp)) {
    239 		isp->isp_maxcmds = 4096;
    240 	} else if (IS_2322(isp)) {
    241 		isp->isp_maxcmds = 2048;
    242 	} else if (IS_23XX(isp) || IS_2200(isp)) {
    243 		isp->isp_maxcmds = 1024;
    244  	} else {
    245 		isp->isp_maxcmds = 512;
    246 	}
    247 
    248 	/*
    249 	 * Set up DMA for the request and result queues.
    250 	 *
    251 	 * We do this now so we can use the request queue
    252 	 * for a dma
    253 	 */
    254 	if (ISP_MBOXDMASETUP(isp) != 0) {
    255 		isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
    256 		return;
    257 	}
    258 
    259 
    260 	/*
    261 	 * Set up default request/response queue in-pointer/out-pointer
    262 	 * register indices.
    263 	 */
    264 	if (IS_24XX(isp)) {
    265 		isp->isp_rqstinrp = BIU2400_REQINP;
    266 		isp->isp_rqstoutrp = BIU2400_REQOUTP;
    267 		isp->isp_respinrp = BIU2400_RSPINP;
    268 		isp->isp_respoutrp = BIU2400_RSPOUTP;
    269 		isp->isp_atioinrp = BIU2400_ATIO_RSPINP;
    270 		isp->isp_atiooutrp = BIU2400_ATIO_REQINP;
    271 	} else if (IS_23XX(isp)) {
    272 		isp->isp_rqstinrp = BIU_REQINP;
    273 		isp->isp_rqstoutrp = BIU_REQOUTP;
    274 		isp->isp_respinrp = BIU_RSPINP;
    275 		isp->isp_respoutrp = BIU_RSPOUTP;
    276 	} else {
    277 		isp->isp_rqstinrp = INMAILBOX4;
    278 		isp->isp_rqstoutrp = OUTMAILBOX4;
    279 		isp->isp_respinrp = OUTMAILBOX5;
    280 		isp->isp_respoutrp = INMAILBOX5;
    281 	}
    282 
    283 	/*
    284 	 * Put the board into PAUSE mode (so we can read the SXP registers
    285 	 * or write FPM/FBM registers).
    286 	 */
    287 	if (IS_24XX(isp)) {
    288 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_HOST_INT);
    289 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
    290 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_PAUSE);
    291 	} else {
    292 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
    293 	}
    294 
    295 	if (IS_FC(isp)) {
    296 		switch (isp->isp_type) {
    297 		case ISP_HA_FC_2100:
    298 			btype = "2100";
    299 			break;
    300 		case ISP_HA_FC_2200:
    301 			btype = "2200";
    302 			break;
    303 		case ISP_HA_FC_2300:
    304 			btype = "2300";
    305 			break;
    306 		case ISP_HA_FC_2312:
    307 			btype = "2312";
    308 			break;
    309 		case ISP_HA_FC_2322:
    310 			btype = "2322";
    311 			break;
    312 		case ISP_HA_FC_2400:
    313 			btype = "2422";
    314 			break;
    315 		default:
    316 			break;
    317 		}
    318 
    319 		if (!IS_24XX(isp)) {
    320 			/*
    321 			 * While we're paused, reset the FPM module and FBM
    322 			 * fifos.
    323 			 */
    324 			ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
    325 			ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
    326 			ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
    327 			ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
    328 			ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
    329 		}
    330 	} else if (IS_1240(isp)) {
    331 		sdparam *sdp = isp->isp_param;
    332 		btype = "1240";
    333 		isp->isp_clock = 60;
    334 		sdp->isp_ultramode = 1;
    335 		sdp++;
    336 		sdp->isp_ultramode = 1;
    337 		/*
    338 		 * XXX: Should probably do some bus sensing.
    339 		 */
    340 	} else if (IS_ULTRA2(isp)) {
    341 		static const char m[] = "bus %d is in %s Mode";
    342 		uint16_t l;
    343 		sdparam *sdp = isp->isp_param;
    344 
    345 		isp->isp_clock = 100;
    346 
    347 		if (IS_1280(isp))
    348 			btype = "1280";
    349 		else if (IS_1080(isp))
    350 			btype = "1080";
    351 		else if (IS_10160(isp))
    352 			btype = "10160";
    353 		else if (IS_12160(isp))
    354 			btype = "12160";
    355 		else
    356 			btype = "<UNKLVD>";
    357 
    358 		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
    359 		switch (l) {
    360 		case ISP1080_LVD_MODE:
    361 			sdp->isp_lvdmode = 1;
    362 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
    363 			break;
    364 		case ISP1080_HVD_MODE:
    365 			sdp->isp_diffmode = 1;
    366 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
    367 			break;
    368 		case ISP1080_SE_MODE:
    369 			sdp->isp_ultramode = 1;
    370 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
    371 			break;
    372 		default:
    373 			isp_prt(isp, ISP_LOGERR,
    374 			    "unknown mode on bus %d (0x%x)", 0, l);
    375 			break;
    376 		}
    377 
    378 		if (IS_DUALBUS(isp)) {
    379 			sdp++;
    380 			l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
    381 			l &= ISP1080_MODE_MASK;
    382 			switch(l) {
    383 			case ISP1080_LVD_MODE:
    384 				sdp->isp_lvdmode = 1;
    385 				isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
    386 				break;
    387 			case ISP1080_HVD_MODE:
    388 				sdp->isp_diffmode = 1;
    389 				isp_prt(isp, ISP_LOGCONFIG,
    390 				    m, 1, "Differential");
    391 				break;
    392 			case ISP1080_SE_MODE:
    393 				sdp->isp_ultramode = 1;
    394 				isp_prt(isp, ISP_LOGCONFIG,
    395 				    m, 1, "Single-Ended");
    396 				break;
    397 			default:
    398 				isp_prt(isp, ISP_LOGERR,
    399 				    "unknown mode on bus %d (0x%x)", 1, l);
    400 				break;
    401 			}
    402 		}
    403 	} else {
    404 		sdparam *sdp = isp->isp_param;
    405 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
    406 		switch (i) {
    407 		default:
    408 			isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
    409 			/* FALLTHROUGH */
    410 		case 1:
    411 			btype = "1020";
    412 			isp->isp_type = ISP_HA_SCSI_1020;
    413 			isp->isp_clock = 40;
    414 			break;
    415 		case 2:
    416 			/*
    417 			 * Some 1020A chips are Ultra Capable, but don't
    418 			 * run the clock rate up for that unless told to
    419 			 * do so by the Ultra Capable bits being set.
    420 			 */
    421 			btype = "1020A";
    422 			isp->isp_type = ISP_HA_SCSI_1020A;
    423 			isp->isp_clock = 40;
    424 			break;
    425 		case 3:
    426 			btype = "1040";
    427 			isp->isp_type = ISP_HA_SCSI_1040;
    428 			isp->isp_clock = 60;
    429 			break;
    430 		case 4:
    431 			btype = "1040A";
    432 			isp->isp_type = ISP_HA_SCSI_1040A;
    433 			isp->isp_clock = 60;
    434 			break;
    435 		case 5:
    436 			btype = "1040B";
    437 			isp->isp_type = ISP_HA_SCSI_1040B;
    438 			isp->isp_clock = 60;
    439 			break;
    440 		case 6:
    441 			btype = "1040C";
    442 			isp->isp_type = ISP_HA_SCSI_1040C;
    443 			isp->isp_clock = 60;
    444                         break;
    445 		}
    446 		/*
    447 		 * Now, while we're at it, gather info about ultra
    448 		 * and/or differential mode.
    449 		 */
    450 		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
    451 			isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
    452 			sdp->isp_diffmode = 1;
    453 		} else {
    454 			sdp->isp_diffmode = 0;
    455 		}
    456 		i = ISP_READ(isp, RISC_PSR);
    457 		if (isp->isp_bustype == ISP_BT_SBUS) {
    458 			i &= RISC_PSR_SBUS_ULTRA;
    459 		} else {
    460 			i &= RISC_PSR_PCI_ULTRA;
    461 		}
    462 		if (i != 0) {
    463 			isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
    464 			sdp->isp_ultramode = 1;
    465 			/*
    466 			 * If we're in Ultra Mode, we have to be 60MHz clock-
    467 			 * even for the SBus version.
    468 			 */
    469 			isp->isp_clock = 60;
    470 		} else {
    471 			sdp->isp_ultramode = 0;
    472 			/*
    473 			 * Clock is known. Gronk.
    474 			 */
    475 		}
    476 
    477 		/*
    478 		 * Machine dependent clock (if set) overrides
    479 		 * our generic determinations.
    480 		 */
    481 		if (isp->isp_mdvec->dv_clock) {
    482 			if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
    483 				isp->isp_clock = isp->isp_mdvec->dv_clock;
    484 			}
    485 		}
    486 
    487 	}
    488 
    489 	/*
    490 	 * Clear instrumentation
    491 	 */
    492 	isp->isp_intcnt = isp->isp_intbogus = 0;
    493 
    494 	/*
    495 	 * Do MD specific pre initialization
    496 	 */
    497 	ISP_RESET0(isp);
    498 
    499 	/*
    500 	 * Hit the chip over the head with hammer,
    501 	 * and give the ISP a chance to recover.
    502 	 */
    503 
    504 	if (IS_SCSI(isp)) {
    505 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
    506 		/*
    507 		 * A slight delay...
    508 		 */
    509 		USEC_DELAY(100);
    510 
    511 		/*
    512 		 * Clear data && control DMA engines.
    513 		 */
    514 		ISP_WRITE(isp, CDMA_CONTROL,
    515 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    516 		ISP_WRITE(isp, DDMA_CONTROL,
    517 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    518 
    519 
    520 	} else if (IS_24XX(isp)) {
    521 		/*
    522 		 * Stop DMA and wait for it to stop.
    523 		 */
    524 		ISP_WRITE(isp, BIU2400_CSR, BIU2400_DMA_STOP|(3 << 4));
    525 		for (val = loops = 0; loops < 30000; loops++) {
    526 			USEC_DELAY(10);
    527 			val = ISP_READ(isp, BIU2400_CSR);
    528 			if ((val & BIU2400_DMA_ACTIVE) == 0) {
    529 				break;
    530 			}
    531 		}
    532 		if (val & BIU2400_DMA_ACTIVE) {
    533 			ISP_RESET0(isp);
    534 			isp_prt(isp, ISP_LOGERR, "DMA Failed to Stop on Reset");
    535 			return;
    536 		}
    537 		/*
    538 		 * Hold it in SOFT_RESET and STOP state for 100us.
    539 		 */
    540 		ISP_WRITE(isp, BIU2400_CSR,
    541 		    BIU2400_SOFT_RESET|BIU2400_DMA_STOP|(3 << 4));
    542 		USEC_DELAY(100);
    543 		for (loops = 0; loops < 10000; loops++) {
    544 			USEC_DELAY(5);
    545 			val = ISP_READ(isp, OUTMAILBOX0);
    546 		}
    547 		for (val = loops = 0; loops < 500000; loops ++) {
    548 			val = ISP_READ(isp, BIU2400_CSR);
    549 			if ((val & BIU2400_SOFT_RESET) == 0) {
    550 				break;
    551 			}
    552 		}
    553 		if (val & BIU2400_SOFT_RESET) {
    554 			ISP_RESET0(isp);
    555 			isp_prt(isp, ISP_LOGERR, "Failed to come out of reset");
    556 			return;
    557 		}
    558 	} else {
    559 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
    560 		/*
    561 		 * A slight delay...
    562 		 */
    563 		USEC_DELAY(100);
    564 
    565 		/*
    566 		 * Clear data && control DMA engines.
    567 		 */
    568 		ISP_WRITE(isp, CDMA2100_CONTROL,
    569 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    570 		ISP_WRITE(isp, TDMA2100_CONTROL,
    571 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    572 		ISP_WRITE(isp, RDMA2100_CONTROL,
    573 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    574 	}
    575 
    576 	/*
    577 	 * Wait for ISP to be ready to go...
    578 	 */
    579 	loops = MBOX_DELAY_COUNT;
    580 	for (;;) {
    581 		if (IS_SCSI(isp)) {
    582 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET)) {
    583 				break;
    584 			}
    585 		} else if (IS_24XX(isp)) {
    586 			if (ISP_READ(isp, OUTMAILBOX0) == 0) {
    587 				break;
    588 			}
    589 		} else {
    590 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
    591 				break;
    592 		}
    593 		USEC_DELAY(100);
    594 		if (--loops < 0) {
    595 			ISP_DUMPREGS(isp, "chip reset timed out");
    596 			ISP_RESET0(isp);
    597 			return;
    598 		}
    599 	}
    600 
    601 	/*
    602 	 * After we've fired this chip up, zero out the conf1 register
    603 	 * for SCSI adapters and other settings for the 2100.
    604 	 */
    605 
    606 	if (IS_SCSI(isp)) {
    607 		ISP_WRITE(isp, BIU_CONF1, 0);
    608 	} else if (!IS_24XX(isp)) {
    609 		ISP_WRITE(isp, BIU2100_CSR, 0);
    610 	}
    611 
    612 	/*
    613 	 * Reset RISC Processor
    614 	 */
    615 	if (IS_24XX(isp)) {
    616 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
    617 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RELEASE);
    618 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RESET);
    619 	} else {
    620 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
    621 		USEC_DELAY(100);
    622 		ISP_WRITE(isp, BIU_SEMA, 0);
    623 	}
    624 
    625 
    626 	/*
    627 	 * Post-RISC Reset stuff.
    628 	 */
    629 	if (IS_24XX(isp)) {
    630 		for (val = loops = 0; loops < 5000000; loops++) {
    631 			USEC_DELAY(5);
    632 			val = ISP_READ(isp, OUTMAILBOX0);
    633 			if (val == 0) {
    634 				break;
    635 			}
    636 		}
    637 		if (val != 0) {
    638 			ISP_RESET0(isp);
    639 			isp_prt(isp, ISP_LOGERR, "reset didn't clear");
    640 			return;
    641 		}
    642 	} else if (IS_SCSI(isp)) {
    643 		uint16_t tmp = isp->isp_mdvec->dv_conf1;
    644 		/*
    645 		 * Busted FIFO. Turn off all but burst enables.
    646 		 */
    647 		if (isp->isp_type == ISP_HA_SCSI_1040A) {
    648 			tmp &= BIU_BURST_ENABLE;
    649 		}
    650 		ISP_SETBITS(isp, BIU_CONF1, tmp);
    651 		if (tmp & BIU_BURST_ENABLE) {
    652 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
    653 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
    654 		}
    655 		if (SDPARAM(isp)->isp_ptisp) {
    656 			if (SDPARAM(isp)->isp_ultramode) {
    657 				while (ISP_READ(isp, RISC_MTR) != 0x1313) {
    658 					ISP_WRITE(isp, RISC_MTR, 0x1313);
    659 					ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
    660 				}
    661 			} else {
    662 				ISP_WRITE(isp, RISC_MTR, 0x1212);
    663 			}
    664 			/*
    665 			 * PTI specific register
    666 			 */
    667 			ISP_WRITE(isp, RISC_EMB, DUAL_BANK);
    668 		} else {
    669 			ISP_WRITE(isp, RISC_MTR, 0x1212);
    670 		}
    671 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
    672 	} else {
    673 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
    674 		if (IS_2200(isp) || IS_23XX(isp)) {
    675 			ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
    676 		}
    677 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
    678 	}
    679 
    680 	ISP_WRITE(isp, isp->isp_rqstinrp, 0);
    681 	ISP_WRITE(isp, isp->isp_rqstoutrp, 0);
    682 	ISP_WRITE(isp, isp->isp_respinrp, 0);
    683 	ISP_WRITE(isp, isp->isp_respoutrp, 0);
    684 
    685 
    686 	/*
    687 	 * Do MD specific post initialization
    688 	 */
    689 	ISP_RESET1(isp);
    690 
    691 	/*
    692 	 * Wait for everything to finish firing up.
    693 	 *
    694 	 * Avoid doing this on the 2312 because you can generate a PCI
    695 	 * parity error (chip breakage).
    696 	 */
    697 	if (IS_2312(isp)) {
    698 		USEC_DELAY(100);
    699 	} else {
    700 		loops = MBOX_DELAY_COUNT;
    701 		while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
    702 			USEC_DELAY(100);
    703 			if (--loops < 0) {
    704 				ISP_RESET0(isp);
    705 				isp_prt(isp, ISP_LOGERR,
    706 				    "MBOX_BUSY never cleared on reset");
    707 				return;
    708 			}
    709 		}
    710 	}
    711 
    712 	/*
    713 	 * Up until this point we've done everything by just reading or
    714 	 * setting registers. From this point on we rely on at least *some*
    715 	 * kind of firmware running in the card.
    716 	 */
    717 
    718 	/*
    719 	 * Do some sanity checking.
    720 	 */
    721 	MEMZERO(&mbs, sizeof (mbs));
    722 	mbs.param[0] = MBOX_NO_OP;
    723 	mbs.logval = MBLOGALL;
    724 	isp_mboxcmd(isp, &mbs);
    725 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    726 		ISP_RESET0(isp);
    727 		return;
    728 	}
    729 
    730 	if (IS_SCSI(isp) || IS_24XX(isp)) {
    731 		MEMZERO(&mbs, sizeof (mbs));
    732 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
    733 		mbs.param[1] = 0xdead;
    734 		mbs.param[2] = 0xbeef;
    735 		mbs.param[3] = 0xffff;
    736 		mbs.param[4] = 0x1111;
    737 		mbs.param[5] = 0xa5a5;
    738 		mbs.param[6] = 0x0000;
    739 		mbs.param[7] = 0x0000;
    740 		mbs.logval = MBLOGALL;
    741 		isp_mboxcmd(isp, &mbs);
    742 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    743 			ISP_RESET0(isp);
    744 			return;
    745 		}
    746 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
    747 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
    748 		    mbs.param[5] != 0xa5a5) {
    749 			ISP_RESET0(isp);
    750 			isp_prt(isp, ISP_LOGERR,
    751 			    "Register Test Failed (0x%x 0x%x 0x%x 0x%x 0x%x)",
    752 			    mbs.param[1], mbs.param[2], mbs.param[3],
    753 			    mbs.param[4], mbs.param[5]);
    754 			return;
    755 		}
    756 
    757 	}
    758 
    759 	/*
    760 	 * Download new Firmware, unless requested not to do so.
    761 	 * This is made slightly trickier in some cases where the
    762 	 * firmware of the ROM revision is newer than the revision
    763 	 * compiled into the driver. So, where we used to compare
    764 	 * versions of our f/w and the ROM f/w, now we just see
    765 	 * whether we have f/w at all and whether a config flag
    766 	 * has disabled our download.
    767 	 */
    768 	if ((isp->isp_mdvec->dv_ispfw == NULL) ||
    769 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
    770 		dodnld = 0;
    771 	}
    772 
    773 	if (IS_24XX(isp)) {
    774 		code_org = ISP_CODE_ORG_2400;
    775 	} else if (IS_23XX(isp)) {
    776 		code_org = ISP_CODE_ORG_2300;
    777 	} else {
    778 		code_org = ISP_CODE_ORG;
    779 	}
    780 
    781 	if (dodnld && IS_24XX(isp)) {
    782 		const uint32_t *ptr = isp->isp_mdvec->dv_ispfw;
    783 
    784 		/*
    785 		 * NB: Whatever you do do, do *not* issue the VERIFY FIRMWARE
    786 		 * NB: command to the 2400 while loading new firmware. This
    787 		 * NB: causes the new f/w to start and immediately crash back
    788 		 * NB: to the ROM.
    789 		 */
    790 
    791 		/*
    792 		 * Keep loading until we run out of f/w.
    793 		 */
    794 		code_org = ptr[2];	/* 1st load address is our start addr */
    795 
    796 		for (;;) {
    797 			uint32_t la, wi, wl;
    798 
    799 			isp_prt(isp, ISP_LOGDEBUG0,
    800 			    "load 0x%x words of code at load address 0x%x",
    801 			    ptr[3], ptr[2]);
    802 
    803 			wi = 0;
    804 			la = ptr[2];
    805 			wl = ptr[3];
    806 
    807 			while (wi < ptr[3]) {
    808 				uint32_t *cp;
    809 				uint32_t nw;
    810 
    811 				nw = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) >> 2;
    812 				if (nw > wl) {
    813 					nw = wl;
    814 				}
    815 				cp = isp->isp_rquest;
    816 				for (i = 0; i < nw; i++) {
    817 					cp[i] = ptr[wi++];
    818 					wl--;
    819 				}
    820 				MEMORYBARRIER(isp, SYNC_REQUEST,
    821 				    0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)));
    822 				MEMZERO(&mbs, sizeof (mbs));
    823 				mbs.param[0] = MBOX_LOAD_RISC_RAM;
    824 				mbs.param[1] = la;
    825 				mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
    826 				mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
    827 				mbs.param[4] = nw >> 16;
    828 				mbs.param[5] = nw;
    829 				mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
    830 				mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
    831 				mbs.param[8] = la >> 16;
    832 				mbs.logval = MBLOGALL;
    833 				isp_mboxcmd(isp, &mbs);
    834 				if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    835 					isp_prt(isp, ISP_LOGERR,
    836 					    "F/W Risc Ram Load Failed");
    837 					ISP_RESET0(isp);
    838 					return;
    839 				}
    840 				la += nw;
    841 			}
    842 
    843 			if (ptr[1] == 0) {
    844 				break;
    845 			}
    846 			ptr += ptr[3];
    847 		}
    848 		isp->isp_loaded_fw = 1;
    849 	} else if (dodnld && IS_23XX(isp)) {
    850 		const uint16_t *ptr = isp->isp_mdvec->dv_ispfw;
    851 		uint16_t wi, wl, segno;
    852 		uint32_t la;
    853 
    854 		la = code_org;
    855 		segno = 0;
    856 
    857 		for (;;) {
    858 			uint32_t nxtaddr;
    859 
    860 			isp_prt(isp, ISP_LOGDEBUG0,
    861 			    "load 0x%x words of code at load address 0x%x",
    862 			    ptr[3], la);
    863 
    864 			wi = 0;
    865 			wl = ptr[3];
    866 
    867 			while (wi < ptr[3]) {
    868 				uint16_t *cp;
    869 				uint32_t nw;
    870 
    871 				nw = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) >> 1;
    872 				if (nw > wl) {
    873 					nw = wl;
    874 				}
    875 				if (nw > (1 << 15)) {
    876 					nw = 1 << 15;
    877 				}
    878 				cp = isp->isp_rquest;
    879 				for (i = 0; i < nw; i++) {
    880 					cp[i] = ptr[wi++];
    881 					wl--;
    882 				}
    883 				MEMORYBARRIER(isp, SYNC_REQUEST,
    884 				    0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)));
    885 				MEMZERO(&mbs, sizeof (mbs));
    886 				mbs.param[0] = MBOX_LOAD_RISC_RAM;
    887 				mbs.param[1] = la;
    888 				mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
    889 				mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
    890 				mbs.param[4] = nw;
    891 				mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
    892 				mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
    893 				mbs.param[8] = la >> 16;
    894 				mbs.logval = MBLOGALL;
    895 				isp_mboxcmd(isp, &mbs);
    896 				if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    897 					isp_prt(isp, ISP_LOGERR,
    898 					    "F/W Risc Ram Load Failed");
    899 					ISP_RESET0(isp);
    900 					return;
    901 				}
    902 				la += nw;
    903 			}
    904 
    905 			if (!IS_2322(isp)) {
    906 				/*
    907 				 * Verify that it downloaded correctly.
    908 				 */
    909 				MEMZERO(&mbs, sizeof (mbs));
    910 				mbs.param[0] = MBOX_VERIFY_CHECKSUM;
    911 				mbs.param[1] = code_org;
    912 				mbs.logval = MBLOGNONE;
    913 				isp_mboxcmd(isp, &mbs);
    914 				if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    915 					isp_prt(isp, ISP_LOGERR, dcrc);
    916 					ISP_RESET0(isp);
    917 					return;
    918 				}
    919 				break;
    920 			}
    921 
    922 			if (++segno == 3) {
    923 				break;
    924 			}
    925 
    926 			/*
    927 			 * If we're a 2322, the firmware actually comes in
    928 			 * three chunks. We loaded the first at the code_org
    929 			 * address. The other two chunks, which follow right
    930 			 * after each other in memory here, get loaded at
    931 			 * addresses specfied at offset 0x9..0xB.
    932 			 */
    933 
    934 			nxtaddr = ptr[3];
    935 			ptr = &ptr[nxtaddr];
    936 			la = ptr[5] | ((ptr[4] & 0x3f) << 16);
    937 		}
    938 		isp->isp_loaded_fw = 1;
    939 	} else if (dodnld) {
    940 		union {
    941 			const uint16_t *cp;
    942 			uint16_t *np;
    943 		} u;
    944 		u.cp = isp->isp_mdvec->dv_ispfw;
    945 		isp->isp_mbxworkp = &u.np[1];
    946 		isp->isp_mbxwrk0 = u.np[3] - 1;
    947 		isp->isp_mbxwrk1 = code_org + 1;
    948 		MEMZERO(&mbs, sizeof (mbs));
    949 		mbs.param[0] = MBOX_WRITE_RAM_WORD;
    950 		mbs.param[1] = code_org;
    951 		mbs.param[2] = u.np[0];
    952 		mbs.logval = MBLOGNONE;
    953 		isp_mboxcmd(isp, &mbs);
    954 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    955 			isp_prt(isp, ISP_LOGERR,
    956 			    "F/W download failed at word %d",
    957 			    isp->isp_mbxwrk1 - code_org);
    958 			ISP_RESET0(isp);
    959 			return;
    960 		}
    961 		/*
    962 		 * Verify that it downloaded correctly.
    963 		 */
    964 		MEMZERO(&mbs, sizeof (mbs));
    965 		mbs.param[0] = MBOX_VERIFY_CHECKSUM;
    966 		mbs.param[1] = code_org;
    967 		mbs.logval = MBLOGNONE;
    968 		isp_mboxcmd(isp, &mbs);
    969 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    970 			isp_prt(isp, ISP_LOGERR, dcrc);
    971 			ISP_RESET0(isp);
    972 			return;
    973 		}
    974 		isp->isp_loaded_fw = 1;
    975 	} else {
    976 		isp->isp_loaded_fw = 0;
    977 		isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
    978 	}
    979 
    980 	/*
    981 	 * Now start it rolling.
    982 	 *
    983 	 * If we didn't actually download f/w,
    984 	 * we still need to (re)start it.
    985 	 */
    986 
    987 
    988 	MEMZERO(&mbs, sizeof (mbs));
    989 	mbs.timeout = 1000000;
    990 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
    991 	if (IS_24XX(isp)) {
    992 		mbs.param[1] = code_org >> 16;
    993 		mbs.param[2] = code_org;
    994 		if (isp->isp_loaded_fw) {
    995 			mbs.param[3] = 0;
    996 		} else {
    997 			mbs.param[3] = 1;
    998 		}
    999 	} else if (IS_2322(isp)) {
   1000 		mbs.param[1] = code_org;
   1001 		if (isp->isp_loaded_fw) {
   1002 			mbs.param[2] = 0;
   1003 		} else {
   1004 			mbs.param[2] = 1;
   1005 		}
   1006 	} else {
   1007 		mbs.param[1] = code_org;
   1008 	}
   1009 
   1010 	mbs.logval = MBLOGALL;
   1011 	isp_mboxcmd(isp, &mbs);
   1012 	if (IS_2322(isp) || IS_24XX(isp)) {
   1013 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1014 			ISP_RESET0(isp);
   1015 			return;
   1016 		}
   1017 	}
   1018 
   1019 	/*
   1020 	 * Give it a chance to finish starting up.
   1021 	 */
   1022 	USEC_DELAY(250000);
   1023 
   1024 	if (IS_SCSI(isp)) {
   1025 		/*
   1026 		 * Set CLOCK RATE, but only if asked to.
   1027 		 */
   1028 		if (isp->isp_clock) {
   1029 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
   1030 			mbs.param[1] = isp->isp_clock;
   1031 			mbs.logval = MBLOGNONE;
   1032 			isp_mboxcmd(isp, &mbs);
   1033 			/* we will try not to care if this fails */
   1034 		}
   1035 	}
   1036 
   1037 	MEMZERO(&mbs, sizeof (mbs));
   1038 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
   1039 	mbs.logval = MBLOGALL;
   1040 	isp_mboxcmd(isp, &mbs);
   1041 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1042 		ISP_RESET0(isp);
   1043 		return;
   1044 	}
   1045 
   1046 	if (IS_24XX(isp) && mbs.param[1] == 0xdead) {
   1047 		isp_prt(isp, ISP_LOGERR, "f/w didn't *really* start");
   1048 		ISP_RESET0(isp);
   1049 		return;
   1050 	}
   1051 
   1052 	/*
   1053 	 * The SBus firmware that we are using apparently does not return
   1054 	 * major, minor, micro revisions in the mailbox registers, which
   1055 	 * is really, really, annoying.
   1056 	 */
   1057 	if (ISP_SBUS_SUPPORTED && isp->isp_bustype == ISP_BT_SBUS) {
   1058 		if (dodnld) {
   1059 #ifdef	ISP_TARGET_MODE
   1060 			isp->isp_fwrev[0] = 7;
   1061 			isp->isp_fwrev[1] = 55;
   1062 #else
   1063 			isp->isp_fwrev[0] = 1;
   1064 			isp->isp_fwrev[1] = 37;
   1065 #endif
   1066 			isp->isp_fwrev[2] = 0;
   1067 		}
   1068 	} else {
   1069 		isp->isp_fwrev[0] = mbs.param[1];
   1070 		isp->isp_fwrev[1] = mbs.param[2];
   1071 		isp->isp_fwrev[2] = mbs.param[3];
   1072 	}
   1073 
   1074 	isp_prt(isp, ISP_LOGALL,
   1075 	    "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
   1076 	    btype, isp->isp_revision, dodnld? "loaded" : "resident",
   1077 	    isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
   1078 
   1079 	if (IS_FC(isp)) {
   1080 		/*
   1081 		 * We do not believe firmware attributes for 2100 code less
   1082 		 * than 1.17.0, unless it's the firmware we specifically
   1083 		 * are loading.
   1084 		 *
   1085 		 * Note that all 22XX and later f/w is greater than 1.X.0.
   1086 		 */
   1087 		if ((ISP_FW_OLDER_THAN(isp, 1, 17, 1))) {
   1088 #ifdef	USE_SMALLER_2100_FIRMWARE
   1089 			FCPARAM(isp)->isp_fwattr = ISP_FW_ATTR_SCCLUN;
   1090 #else
   1091 			FCPARAM(isp)->isp_fwattr = 0;
   1092 #endif
   1093 		} else {
   1094 			FCPARAM(isp)->isp_fwattr = mbs.param[6];
   1095 			isp_prt(isp, ISP_LOGDEBUG0,
   1096 			    "Firmware Attributes = 0x%x", mbs.param[6]);
   1097 		}
   1098 		FCPARAM(isp)->isp_2klogin = 0;
   1099 		FCPARAM(isp)->isp_sccfw = 0;
   1100 		FCPARAM(isp)->isp_tmode = 0;
   1101 		if (IS_24XX(isp)) {
   1102 			FCPARAM(isp)->isp_2klogin = 1;
   1103 			FCPARAM(isp)->isp_sccfw = 1;
   1104 			FCPARAM(isp)->isp_tmode = 1;
   1105 		} else {
   1106 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
   1107 				FCPARAM(isp)->isp_sccfw = 1;
   1108 			}
   1109 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_2KLOGINS) {
   1110 				FCPARAM(isp)->isp_2klogin = 1;
   1111 				FCPARAM(isp)->isp_sccfw = 1;
   1112 			}
   1113 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_TMODE) {
   1114 				FCPARAM(isp)->isp_tmode = 1;
   1115 			}
   1116 		}
   1117 		if (FCPARAM(isp)->isp_2klogin) {
   1118 			isp_prt(isp, ISP_LOGCONFIG, "2K Logins Supported");
   1119 		}
   1120 	}
   1121 
   1122 	if (isp->isp_romfw_rev[0] || isp->isp_romfw_rev[1] ||
   1123 	    isp->isp_romfw_rev[2]) {
   1124 		isp_prt(isp, ISP_LOGCONFIG, "Last F/W revision was %d.%d.%d",
   1125 		    isp->isp_romfw_rev[0], isp->isp_romfw_rev[1],
   1126 		    isp->isp_romfw_rev[2]);
   1127 	}
   1128 
   1129 	if (!IS_24XX(isp)) {
   1130 		MEMZERO(&mbs, sizeof (mbs));
   1131 		mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
   1132 		mbs.logval = MBLOGALL;
   1133 		isp_mboxcmd(isp, &mbs);
   1134 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1135 			ISP_RESET0(isp);
   1136 			return;
   1137 		}
   1138 		if (isp->isp_maxcmds >= mbs.param[2]) {
   1139 			isp->isp_maxcmds = mbs.param[2];
   1140 		}
   1141 	}
   1142 	isp_prt(isp, ISP_LOGCONFIG,
   1143 	    "%d max I/O command limit set", isp->isp_maxcmds);
   1144 	isp_fw_state(isp);
   1145 
   1146 	isp->isp_state = ISP_RESETSTATE;
   1147 
   1148 	/*
   1149 	 * Okay- now that we have new firmware running, we now (re)set our
   1150 	 * notion of how many luns we support. This is somewhat tricky because
   1151 	 * if we haven't loaded firmware, we sometimes do not have an easy way
   1152 	 * of knowing how many luns we support.
   1153 	 *
   1154 	 * Expanded lun firmware gives you 32 luns for SCSI cards and
   1155 	 * 16384 luns for Fibre Channel cards.
   1156 	 *
   1157 	 * It turns out that even for QLogic 2100s with ROM 1.10 and above
   1158 	 * we do get a firmware attributes word returned in mailbox register 6.
   1159 	 *
   1160 	 * Because the lun is in a different position in the Request Queue
   1161 	 * Entry structure for Fibre Channel with expanded lun firmware, we
   1162 	 * can only support one lun (lun zero) when we don't know what kind
   1163 	 * of firmware we're running.
   1164 	 */
   1165 	if (IS_SCSI(isp)) {
   1166 		if (dodnld) {
   1167 			if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
   1168 				isp->isp_maxluns = 32;
   1169 			} else {
   1170 				isp->isp_maxluns = 8;
   1171 			}
   1172 		} else {
   1173 			isp->isp_maxluns = 8;
   1174 		}
   1175 	} else {
   1176 		if (FCPARAM(isp)->isp_sccfw) {
   1177 			isp->isp_maxluns = 16384;
   1178 		} else {
   1179 			isp->isp_maxluns = 16;
   1180 		}
   1181 	}
   1182 	/*
   1183 	 * Must do this first to get defaults established.
   1184 	 */
   1185 	if (IS_SCSI(isp)) {
   1186 		isp_setdfltparm(isp, 0);
   1187 		if (IS_DUALBUS(isp)) {
   1188 			isp_setdfltparm(isp, 1);
   1189 		}
   1190 	} else {
   1191 		isp_setdfltfcparm(isp);
   1192 	}
   1193 
   1194 }
   1195 
   1196 /*
   1197  * Initialize Parameters of Hardware to a known state.
   1198  *
   1199  * Locks are held before coming here.
   1200  */
   1201 
   1202 void
   1203 isp_init(ispsoftc_t *isp)
   1204 {
   1205 	if (IS_FC(isp)) {
   1206 		/*
   1207 		 * Do this *before* initializing the firmware.
   1208 		 */
   1209 		ISP_MARK_PORTDB(isp, 0);
   1210 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   1211 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   1212 
   1213 		if (isp->isp_role != ISP_ROLE_NONE) {
   1214 			if (IS_24XX(isp)) {
   1215 				isp_fibre_init_2400(isp);
   1216 			} else {
   1217 				isp_fibre_init(isp);
   1218 			}
   1219 		}
   1220 	} else {
   1221 		isp_scsi_init(isp);
   1222 	}
   1223 }
   1224 
   1225 static void
   1226 isp_scsi_init(ispsoftc_t *isp)
   1227 {
   1228 	sdparam *sdp_chan0, *sdp_chan1;
   1229 	mbreg_t mbs;
   1230 
   1231 	sdp_chan0 = isp->isp_param;
   1232 	sdp_chan1 = sdp_chan0;
   1233 	if (IS_DUALBUS(isp)) {
   1234 		sdp_chan1++;
   1235 	}
   1236 
   1237 	/*
   1238 	 * If we have no role (neither target nor initiator), return.
   1239 	 */
   1240 	if (isp->isp_role == ISP_ROLE_NONE) {
   1241 		return;
   1242 	}
   1243 
   1244 	/* First do overall per-card settings. */
   1245 
   1246 	/*
   1247 	 * If we have fast memory timing enabled, turn it on.
   1248 	 */
   1249 	if (sdp_chan0->isp_fast_mttr) {
   1250 		ISP_WRITE(isp, RISC_MTR, 0x1313);
   1251 	}
   1252 
   1253 	/*
   1254 	 * Set Retry Delay and Count.
   1255 	 * You set both channels at the same time.
   1256 	 */
   1257 	MEMZERO(&mbs, sizeof (mbs));
   1258 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
   1259 	mbs.param[1] = sdp_chan0->isp_retry_count;
   1260 	mbs.param[2] = sdp_chan0->isp_retry_delay;
   1261 	mbs.param[6] = sdp_chan1->isp_retry_count;
   1262 	mbs.param[7] = sdp_chan1->isp_retry_delay;
   1263 	mbs.logval = MBLOGALL;
   1264 	isp_mboxcmd(isp, &mbs);
   1265 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1266 		return;
   1267 	}
   1268 
   1269 	/*
   1270 	 * Set ASYNC DATA SETUP time. This is very important.
   1271 	 */
   1272 	MEMZERO(&mbs, sizeof (mbs));
   1273 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
   1274 	mbs.param[1] = sdp_chan0->isp_async_data_setup;
   1275 	mbs.param[2] = sdp_chan1->isp_async_data_setup;
   1276 	mbs.logval = MBLOGALL;
   1277 	isp_mboxcmd(isp, &mbs);
   1278 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1279 		return;
   1280 	}
   1281 
   1282 	/*
   1283 	 * Set ACTIVE Negation State.
   1284 	 */
   1285 	MEMZERO(&mbs, sizeof (mbs));
   1286 	mbs.param[0] = MBOX_SET_ACT_NEG_STATE;
   1287 	mbs.param[1] =
   1288 	    (sdp_chan0->isp_req_ack_active_neg << 4) |
   1289 	    (sdp_chan0->isp_data_line_active_neg << 5);
   1290 	mbs.param[2] =
   1291 	    (sdp_chan1->isp_req_ack_active_neg << 4) |
   1292 	    (sdp_chan1->isp_data_line_active_neg << 5);
   1293 	mbs.logval = MBLOGNONE;
   1294 	isp_mboxcmd(isp, &mbs);
   1295 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1296 		isp_prt(isp, ISP_LOGERR,
   1297 		    "failed to set active negation state (%d,%d), (%d,%d)",
   1298 		    sdp_chan0->isp_req_ack_active_neg,
   1299 		    sdp_chan0->isp_data_line_active_neg,
   1300 		    sdp_chan1->isp_req_ack_active_neg,
   1301 		    sdp_chan1->isp_data_line_active_neg);
   1302 		/*
   1303 		 * But don't return.
   1304 		 */
   1305 	}
   1306 
   1307 	/*
   1308 	 * Set the Tag Aging limit
   1309 	 */
   1310 	MEMZERO(&mbs, sizeof (mbs));
   1311 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
   1312 	mbs.param[1] = sdp_chan0->isp_tag_aging;
   1313 	mbs.param[2] = sdp_chan1->isp_tag_aging;
   1314 	mbs.logval = MBLOGALL;
   1315 	isp_mboxcmd(isp, &mbs);
   1316 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1317 		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
   1318 		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
   1319 		return;
   1320 	}
   1321 
   1322 	/*
   1323 	 * Set selection timeout.
   1324 	 */
   1325 	MEMZERO(&mbs, sizeof (mbs));
   1326 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
   1327 	mbs.param[1] = sdp_chan0->isp_selection_timeout;
   1328 	mbs.param[2] = sdp_chan1->isp_selection_timeout;
   1329 	mbs.logval = MBLOGALL;
   1330 	isp_mboxcmd(isp, &mbs);
   1331 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1332 		return;
   1333 	}
   1334 
   1335 	/* now do per-channel settings */
   1336 	isp_scsi_channel_init(isp, 0);
   1337 	if (IS_DUALBUS(isp))
   1338 		isp_scsi_channel_init(isp, 1);
   1339 
   1340 	/*
   1341 	 * Now enable request/response queues
   1342 	 */
   1343 
   1344 	if (IS_ULTRA2(isp) || IS_1240(isp)) {
   1345 		MEMZERO(&mbs, sizeof (mbs));
   1346 		mbs.param[0] = MBOX_INIT_RES_QUEUE_A64;
   1347 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
   1348 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
   1349 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
   1350 		mbs.param[4] = 0;
   1351 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
   1352 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
   1353 		mbs.logval = MBLOGALL;
   1354 		isp_mboxcmd(isp, &mbs);
   1355 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1356 			return;
   1357 		}
   1358 		isp->isp_residx = mbs.param[5];
   1359 
   1360 		MEMZERO(&mbs, sizeof (mbs));
   1361 		mbs.param[0] = MBOX_INIT_REQ_QUEUE_A64;
   1362 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
   1363 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
   1364 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
   1365 		mbs.param[5] = 0;
   1366 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
   1367 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
   1368 		mbs.logval = MBLOGALL;
   1369 		isp_mboxcmd(isp, &mbs);
   1370 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1371 			return;
   1372 		}
   1373 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
   1374 	} else {
   1375 		MEMZERO(&mbs, sizeof (mbs));
   1376 		mbs.param[0] = MBOX_INIT_RES_QUEUE;
   1377 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
   1378 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
   1379 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
   1380 		mbs.param[4] = 0;
   1381 		mbs.logval = MBLOGALL;
   1382 		isp_mboxcmd(isp, &mbs);
   1383 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1384 			return;
   1385 		}
   1386 		isp->isp_residx = mbs.param[5];
   1387 
   1388 		MEMZERO(&mbs, sizeof (mbs));
   1389 		mbs.param[0] = MBOX_INIT_REQ_QUEUE;
   1390 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
   1391 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
   1392 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
   1393 		mbs.param[5] = 0;
   1394 		mbs.logval = MBLOGALL;
   1395 		isp_mboxcmd(isp, &mbs);
   1396 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1397 			return;
   1398 		}
   1399 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
   1400 	}
   1401 
   1402 	/*
   1403 	 * Turn on Fast Posting, LVD transitions
   1404 	 *
   1405 	 * Ultra2 F/W always has had fast posting (and LVD transitions)
   1406 	 *
   1407 	 * Ultra and older (i.e., SBus) cards may not. It's just safer
   1408 	 * to assume not for them.
   1409 	 */
   1410 
   1411 	MEMZERO(&mbs, sizeof (mbs));
   1412 	mbs.param[0] = MBOX_SET_FW_FEATURES;
   1413 	mbs.param[1] = 0;
   1414 	if (IS_ULTRA2(isp))
   1415 		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
   1416 #ifndef	ISP_NO_RIO
   1417 	if (IS_ULTRA2(isp) || IS_1240(isp))
   1418 		mbs.param[1] |= FW_FEATURE_RIO_16BIT;
   1419 #else
   1420 	if (IS_ULTRA2(isp) || IS_1240(isp))
   1421 		mbs.param[1] |= FW_FEATURE_FAST_POST;
   1422 #endif
   1423 	if (mbs.param[1] != 0) {
   1424 		uint16_t sfeat = mbs.param[1];
   1425 		mbs.logval = MBLOGALL;
   1426 		isp_mboxcmd(isp, &mbs);
   1427 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   1428 			isp_prt(isp, ISP_LOGINFO,
   1429 			    "Enabled FW features (0x%x)", sfeat);
   1430 		}
   1431 	}
   1432 
   1433 	/*
   1434 	 * Let the outer layers decide whether to issue a SCSI bus reset.
   1435 	 */
   1436 	isp->isp_state = ISP_INITSTATE;
   1437 }
   1438 
   1439 static void
   1440 isp_scsi_channel_init(ispsoftc_t *isp, int channel)
   1441 {
   1442 	sdparam *sdp;
   1443 	mbreg_t mbs;
   1444 	int tgt;
   1445 
   1446 	sdp = isp->isp_param;
   1447 	sdp += channel;
   1448 
   1449 	/*
   1450 	 * Set (possibly new) Initiator ID.
   1451 	 */
   1452 	MEMZERO(&mbs, sizeof (mbs));
   1453 	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
   1454 	mbs.param[1] = (channel << 7) | sdp->isp_initiator_id;
   1455 	mbs.logval = MBLOGALL;
   1456 	isp_mboxcmd(isp, &mbs);
   1457 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1458 		return;
   1459 	}
   1460 	isp_prt(isp, ISP_LOGINFO, "Initiator ID is %d on Channel %d",
   1461 	    sdp->isp_initiator_id, channel);
   1462 
   1463 
   1464 	/*
   1465 	 * Set current per-target parameters to an initial safe minimum.
   1466 	 */
   1467 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   1468 		int lun;
   1469 		uint16_t sdf;
   1470 
   1471 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   1472 			continue;
   1473 		}
   1474 #ifndef	ISP_TARGET_MODE
   1475 		sdf = sdp->isp_devparam[tgt].goal_flags;
   1476 		sdf &= DPARM_SAFE_DFLT;
   1477 		/*
   1478 		 * It is not quite clear when this changed over so that
   1479 		 * we could force narrow and async for 1000/1020 cards,
   1480 		 * but assume that this is only the case for loaded
   1481 		 * firmware.
   1482 		 */
   1483 		if (isp->isp_loaded_fw) {
   1484 			sdf |= DPARM_NARROW | DPARM_ASYNC;
   1485 		}
   1486 #else
   1487 		/*
   1488 		 * The !$*!)$!$)* f/w uses the same index into some
   1489 		 * internal table to decide how to respond to negotiations,
   1490 		 * so if we've said "let's be safe" for ID X, and ID X
   1491 		 * selects *us*, the negotiations will back to 'safe'
   1492 		 * (as in narrow/async). What the f/w *should* do is
   1493 		 * use the initiator id settings to decide how to respond.
   1494 		 */
   1495 		sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
   1496 #endif
   1497 		MEMZERO(&mbs, sizeof (mbs));
   1498 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   1499 		mbs.param[1] = (channel << 15) | (tgt << 8);
   1500 		mbs.param[2] = sdf;
   1501 		if ((sdf & DPARM_SYNC) == 0) {
   1502 			mbs.param[3] = 0;
   1503 		} else {
   1504 			mbs.param[3] =
   1505 			    (sdp->isp_devparam[tgt].goal_offset << 8) |
   1506 			    (sdp->isp_devparam[tgt].goal_period);
   1507 		}
   1508 		isp_prt(isp, ISP_LOGDEBUG0,
   1509 		    "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
   1510 		    channel, tgt, mbs.param[2], mbs.param[3] >> 8,
   1511 		    mbs.param[3] & 0xff);
   1512 		mbs.logval = MBLOGNONE;
   1513 		isp_mboxcmd(isp, &mbs);
   1514 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1515 			sdf = DPARM_SAFE_DFLT;
   1516 			MEMZERO(&mbs, sizeof (mbs));
   1517 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   1518 			mbs.param[1] = (tgt << 8) | (channel << 15);
   1519 			mbs.param[2] = sdf;
   1520 			mbs.param[3] = 0;
   1521 			mbs.logval = MBLOGALL;
   1522 			isp_mboxcmd(isp, &mbs);
   1523 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1524 				continue;
   1525 			}
   1526 		}
   1527 
   1528 		/*
   1529 		 * We don't update any information directly from the f/w
   1530 		 * because we need to run at least one command to cause a
   1531 		 * new state to be latched up. So, we just assume that we
   1532 		 * converge to the values we just had set.
   1533 		 *
   1534 		 * Ensure that we don't believe tagged queuing is enabled yet.
   1535 		 * It turns out that sometimes the ISP just ignores our
   1536 		 * attempts to set parameters for devices that it hasn't
   1537 		 * seen yet.
   1538 		 */
   1539 		sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
   1540 		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
   1541 			MEMZERO(&mbs, sizeof (mbs));
   1542 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
   1543 			mbs.param[1] = (channel << 15) | (tgt << 8) | lun;
   1544 			mbs.param[2] = sdp->isp_max_queue_depth;
   1545 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
   1546 			mbs.logval = MBLOGALL;
   1547 			isp_mboxcmd(isp, &mbs);
   1548 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1549 				break;
   1550 			}
   1551 		}
   1552 	}
   1553 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   1554 		if (sdp->isp_devparam[tgt].dev_refresh) {
   1555 			isp->isp_sendmarker |= (1 << channel);
   1556 			isp->isp_update |= (1 << channel);
   1557 			break;
   1558 		}
   1559 	}
   1560 }
   1561 
   1562 /*
   1563  * Fibre Channel specific initialization.
   1564  */
   1565 static void
   1566 isp_fibre_init(ispsoftc_t *isp)
   1567 {
   1568 	fcparam *fcp;
   1569 	isp_icb_t local, *icbp = &local;
   1570 	mbreg_t mbs;
   1571 	int ownloopid;
   1572 	uint64_t nwwn, pwwn;
   1573 
   1574 	fcp = isp->isp_param;
   1575 
   1576 	MEMZERO(icbp, sizeof (*icbp));
   1577 	icbp->icb_version = ICB_VERSION1;
   1578 	icbp->icb_fwoptions = fcp->isp_fwoptions;
   1579 
   1580 	/*
   1581 	 * Firmware Options are either retrieved from NVRAM or
   1582 	 * are patched elsewhere. We check them for sanity here
   1583 	 * and make changes based on board revision, but otherwise
   1584 	 * let others decide policy.
   1585 	 */
   1586 
   1587 	/*
   1588 	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
   1589 	 */
   1590 	if (IS_2100(isp) && isp->isp_revision < 5) {
   1591 		icbp->icb_fwoptions &= ~ICBOPT_FAIRNESS;
   1592 	}
   1593 
   1594 	/*
   1595 	 * We have to use FULL LOGIN even though it resets the loop too much
   1596 	 * because otherwise port database entries don't get updated after
   1597 	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
   1598 	 */
   1599 	if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
   1600 		icbp->icb_fwoptions |= ICBOPT_FULL_LOGIN;
   1601 	}
   1602 
   1603 	/*
   1604 	 * Insist on Port Database Update Async notifications
   1605 	 */
   1606 	icbp->icb_fwoptions |= ICBOPT_PDBCHANGE_AE;
   1607 
   1608 	/*
   1609 	 * Make sure that target role reflects into fwoptions.
   1610 	 */
   1611 	if (isp->isp_role & ISP_ROLE_TARGET) {
   1612 		icbp->icb_fwoptions |= ICBOPT_TGT_ENABLE;
   1613 	} else {
   1614 		icbp->icb_fwoptions &= ~ICBOPT_TGT_ENABLE;
   1615 	}
   1616 
   1617 	if (isp->isp_role & ISP_ROLE_INITIATOR) {
   1618 		icbp->icb_fwoptions &= ~ICBOPT_INI_DISABLE;
   1619 	} else {
   1620 		icbp->icb_fwoptions |= ICBOPT_INI_DISABLE;
   1621 	}
   1622 
   1623 	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
   1624 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
   1625 	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
   1626 		isp_prt(isp, ISP_LOGERR,
   1627 		    "bad frame length (%d) from NVRAM- using %d",
   1628 		    fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
   1629 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
   1630 	}
   1631 	icbp->icb_maxalloc = fcp->isp_maxalloc;
   1632 	if (icbp->icb_maxalloc < 1) {
   1633 		isp_prt(isp, ISP_LOGERR,
   1634 		    "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
   1635 		icbp->icb_maxalloc = 16;
   1636 	}
   1637 	icbp->icb_execthrottle = fcp->isp_execthrottle;
   1638 	if (icbp->icb_execthrottle < 1) {
   1639 		isp_prt(isp, ISP_LOGERR,
   1640 		    "bad execution throttle of %d- using 16",
   1641 		    fcp->isp_execthrottle);
   1642 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
   1643 	}
   1644 	icbp->icb_retry_delay = fcp->isp_retry_delay;
   1645 	icbp->icb_retry_count = fcp->isp_retry_count;
   1646 	icbp->icb_hardaddr = fcp->isp_loopid;
   1647 	ownloopid = (isp->isp_confopts & ISP_CFG_OWNLOOPID) != 0;
   1648 	if (icbp->icb_hardaddr > 125) {
   1649 		icbp->icb_hardaddr = 0;
   1650 		ownloopid = 0;
   1651 	}
   1652 
   1653 	/*
   1654 	 * Our life seems so much better with 2200s and later with
   1655 	 * the latest f/w if we set Hard Address.
   1656 	 */
   1657 	if (ownloopid || ISP_FW_NEWER_THAN(isp, 2, 2, 5)) {
   1658 		icbp->icb_fwoptions |= ICBOPT_HARD_ADDRESS;
   1659 	}
   1660 
   1661 	/*
   1662 	 * Right now we just set extended options to prefer point-to-point
   1663 	 * over loop based upon some soft config options.
   1664 	 *
   1665 	 * NB: for the 2300, ICBOPT_EXTENDED is required.
   1666 	 */
   1667 	if (IS_2200(isp) || IS_23XX(isp)) {
   1668 		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
   1669 		/*
   1670 		 * Prefer or force Point-To-Point instead Loop?
   1671 		 */
   1672 		switch(isp->isp_confopts & ISP_CFG_PORT_PREF) {
   1673 		case ISP_CFG_NPORT:
   1674 			icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
   1675 			break;
   1676 		case ISP_CFG_NPORT_ONLY:
   1677 			icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
   1678 			break;
   1679 		case ISP_CFG_LPORT_ONLY:
   1680 			icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
   1681 			break;
   1682 		default:
   1683 			icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
   1684 			break;
   1685 		}
   1686 		if (IS_2200(isp)) {
   1687 			if (ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
   1688 				icbp->icb_xfwoptions |= ICBXOPT_RIO_16BIT;
   1689 				icbp->icb_racctimer = 4;
   1690 				icbp->icb_idelaytimer = 8;
   1691 			}
   1692 			icbp->icb_fwoptions |= ICBOPT_FAST_POST;
   1693 		} else {
   1694 			/*
   1695 			 * QLogic recommends that FAST Posting be turned
   1696 			 * off for 23XX cards and instead allow the HBA
   1697 			 * to write response queue entries and interrupt
   1698 			 * after a delay (ZIO).
   1699 			 */
   1700 			icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
   1701 			if ((fcp->isp_xfwoptions & ICBXOPT_TIMER_MASK) ==
   1702 			    ICBXOPT_ZIO) {
   1703 				icbp->icb_xfwoptions |= ICBXOPT_ZIO;
   1704 				icbp->icb_idelaytimer = 10;
   1705 			}
   1706 			if (isp->isp_confopts & ISP_CFG_ONEGB) {
   1707 				icbp->icb_zfwoptions |= ICBZOPT_RATE_ONEGB;
   1708 			} else if (isp->isp_confopts & ISP_CFG_TWOGB) {
   1709 				icbp->icb_zfwoptions |= ICBZOPT_RATE_TWOGB;
   1710 			} else {
   1711 				icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
   1712 			}
   1713 			if (fcp->isp_zfwoptions & ICBZOPT_50_OHM) {
   1714 				icbp->icb_zfwoptions |= ICBZOPT_50_OHM;
   1715 			}
   1716 		}
   1717 	}
   1718 
   1719 
   1720 	/*
   1721 	 * For 22XX > 2.1.26 && 23XX, set some options.
   1722 	 * XXX: Probably okay for newer 2100 f/w too.
   1723 	 */
   1724 	if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
   1725 		/*
   1726 		 * Turn on LIP F8 async event (1)
   1727 		 * Turn on generate AE 8013 on all LIP Resets (2)
   1728 		 * Disable LIP F7 switching (8)
   1729 		 */
   1730 		MEMZERO(&mbs, sizeof (mbs));
   1731 		mbs.param[0] = MBOX_SET_FIRMWARE_OPTIONS;
   1732 		mbs.param[1] = 0xb;
   1733 		mbs.param[2] = 0;
   1734 		mbs.param[3] = 0;
   1735 		mbs.logval = MBLOGALL;
   1736 		isp_mboxcmd(isp, &mbs);
   1737 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1738 			return;
   1739 		}
   1740 	}
   1741 	icbp->icb_logintime = ICB_LOGIN_TOV;
   1742 	icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
   1743 
   1744 	nwwn = ISP_NODEWWN(isp);
   1745 	pwwn = ISP_PORTWWN(isp);
   1746 	if (nwwn && pwwn) {
   1747 		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
   1748 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, nwwn);
   1749 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
   1750 		isp_prt(isp, ISP_LOGDEBUG1,
   1751 		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
   1752 		    ((uint32_t) (nwwn >> 32)),
   1753 		    ((uint32_t) (nwwn & 0xffffffff)),
   1754 		    ((uint32_t) (pwwn >> 32)),
   1755 		    ((uint32_t) (pwwn & 0xffffffff)));
   1756 	} else if (pwwn) {
   1757 		icbp->icb_fwoptions &= ~ICBOPT_BOTH_WWNS;
   1758 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
   1759 		isp_prt(isp, ISP_LOGDEBUG1,
   1760 		    "Setting ICB Port 0x%08x%08x",
   1761 		    ((uint32_t) (pwwn >> 32)),
   1762 		    ((uint32_t) (pwwn & 0xffffffff)));
   1763 	} else {
   1764 		isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
   1765 		return;
   1766 	}
   1767 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
   1768 	if (icbp->icb_rqstqlen < 1) {
   1769 		isp_prt(isp, ISP_LOGERR, "bad request queue length");
   1770 	}
   1771 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
   1772 	if (icbp->icb_rsltqlen < 1) {
   1773 		isp_prt(isp, ISP_LOGERR, "bad result queue length");
   1774 	}
   1775 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
   1776 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
   1777 	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
   1778 	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
   1779 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
   1780 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
   1781 	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
   1782 	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
   1783 
   1784 	isp_prt(isp, ISP_LOGDEBUG0,
   1785 	    "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
   1786 	    icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
   1787 
   1788 	FC_SCRATCH_ACQUIRE(isp);
   1789 	isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
   1790 
   1791 	/*
   1792 	 * Init the firmware
   1793 	 */
   1794 	MEMZERO(&mbs, sizeof (mbs));
   1795 	mbs.param[0] = MBOX_INIT_FIRMWARE;
   1796 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   1797 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   1798 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   1799 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   1800 	mbs.logval = MBLOGALL;
   1801 	mbs.timeout = 30 * 1000000;
   1802 	isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %p (%08x%08x)",
   1803 	    fcp->isp_scratch, (uint32_t) ((uint64_t)fcp->isp_scdma >> 32),
   1804 	    (uint32_t) fcp->isp_scdma);
   1805 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp));
   1806 	isp_mboxcmd(isp, &mbs);
   1807 	FC_SCRATCH_RELEASE(isp);
   1808 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1809 		isp_print_bytes(isp, "isp_fibre_init", sizeof (*icbp), icbp);
   1810 		return;
   1811 	}
   1812 	isp->isp_reqidx = 0;
   1813 	isp->isp_reqodx = 0;
   1814 	isp->isp_residx = 0;
   1815 
   1816 	/*
   1817 	 * Whatever happens, we're now committed to being here.
   1818 	 */
   1819 	isp->isp_state = ISP_INITSTATE;
   1820 }
   1821 
   1822 static void
   1823 isp_fibre_init_2400(ispsoftc_t *isp)
   1824 {
   1825 	fcparam *fcp;
   1826 	isp_icb_2400_t local, *icbp = &local;
   1827 	mbreg_t mbs;
   1828 	int ownloopid;
   1829 	uint64_t nwwn, pwwn;
   1830 
   1831 	fcp = isp->isp_param;
   1832 
   1833 	/*
   1834 	 * Turn on LIP F8 async event (1)
   1835 	 */
   1836 	MEMZERO(&mbs, sizeof (mbs));
   1837 	mbs.param[0] = MBOX_SET_FIRMWARE_OPTIONS;
   1838 	mbs.param[1] = 1;
   1839 	mbs.logval = MBLOGALL;
   1840 	isp_mboxcmd(isp, &mbs);
   1841 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1842 		return;
   1843 	}
   1844 
   1845 	/*
   1846 	 * XXX: This should be applied to icb- not fwoptions
   1847 	 */
   1848 	if (isp->isp_role & ISP_ROLE_TARGET) {
   1849 		fcp->isp_fwoptions |= ICB2400_OPT1_TGT_ENABLE;
   1850 	} else {
   1851 		fcp->isp_fwoptions &= ~ICB2400_OPT1_TGT_ENABLE;
   1852 	}
   1853 
   1854 	if (isp->isp_role & ISP_ROLE_INITIATOR) {
   1855 		fcp->isp_fwoptions &= ~ICB2400_OPT1_INI_DISABLE;
   1856 	} else {
   1857 		fcp->isp_fwoptions |= ICB2400_OPT1_INI_DISABLE;
   1858 	}
   1859 
   1860 	MEMZERO(icbp, sizeof (*icbp));
   1861 	icbp->icb_version = ICB_VERSION1;
   1862 	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
   1863 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
   1864 	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
   1865 		isp_prt(isp, ISP_LOGERR,
   1866 		    "bad frame length (%d) from NVRAM- using %d",
   1867 		    fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
   1868 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
   1869 	}
   1870 
   1871 	icbp->icb_execthrottle = fcp->isp_execthrottle;
   1872 	if (icbp->icb_execthrottle < 1) {
   1873 		isp_prt(isp, ISP_LOGERR,
   1874 		    "bad execution throttle of %d- using 16",
   1875 		    fcp->isp_execthrottle);
   1876 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
   1877 	}
   1878 
   1879 	if (isp->isp_role & ISP_ROLE_TARGET) {
   1880 		/*
   1881 		 * Get current resource count
   1882 		 */
   1883 		MEMZERO(&mbs, sizeof (mbs));
   1884 		mbs.param[0] = MBOX_GET_RESOURCE_COUNT;
   1885 		mbs.obits = 0x4cf;
   1886 		mbs.logval = MBLOGALL;
   1887 		isp_mboxcmd(isp, &mbs);
   1888 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1889 			return;
   1890 		}
   1891 		icbp->icb_xchgcnt = mbs.param[3];
   1892 	}
   1893 
   1894 	icbp->icb_fwoptions1 = fcp->isp_fwoptions;
   1895 
   1896 	icbp->icb_hardaddr = fcp->isp_loopid;
   1897 	ownloopid = (isp->isp_confopts & ISP_CFG_OWNLOOPID) != 0;
   1898 	if (icbp->icb_hardaddr > 125) {
   1899 		icbp->icb_hardaddr = 0;
   1900 		ownloopid = 0;
   1901 	}
   1902 	if (ownloopid) {
   1903 		icbp->icb_fwoptions1 |= ICB2400_OPT1_HARD_ADDRESS;
   1904 	}
   1905 
   1906 	icbp->icb_fwoptions2 = fcp->isp_xfwoptions;
   1907 	switch(isp->isp_confopts & ISP_CFG_PORT_PREF) {
   1908 	case ISP_CFG_NPORT:
   1909 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
   1910 		icbp->icb_fwoptions2 |= ICB2400_OPT2_PTP_2_LOOP;
   1911 		break;
   1912 	case ISP_CFG_NPORT_ONLY:
   1913 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
   1914 		icbp->icb_fwoptions2 |= ICB2400_OPT2_PTP_ONLY;
   1915 		break;
   1916 	case ISP_CFG_LPORT_ONLY:
   1917 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
   1918 		icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_ONLY;
   1919 		break;
   1920 	default:
   1921 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
   1922 		icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_2_PTP;
   1923 		break;
   1924 	}
   1925 
   1926 	switch (icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK) {
   1927 	case ICB2400_OPT2_ZIO:
   1928 	case ICB2400_OPT2_ZIO1:
   1929 		icbp->icb_idelaytimer = 0;
   1930 		break;
   1931 	case 0:
   1932 		break;
   1933 	default:
   1934 		isp_prt(isp, ISP_LOGWARN, "bad value %x in fwopt2 timer field",
   1935 		    icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK);
   1936 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TIMER_MASK;
   1937 		break;
   1938 	}
   1939 
   1940 	icbp->icb_fwoptions3 = fcp->isp_zfwoptions;
   1941 	icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_AUTO;
   1942 	if (isp->isp_confopts & ISP_CFG_ONEGB) {
   1943 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_ONEGB;
   1944 	} else if (isp->isp_confopts & ISP_CFG_TWOGB) {
   1945 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_TWOGB;
   1946 	} else if (isp->isp_confopts & ISP_CFG_FOURGB) {
   1947 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_FOURGB;
   1948 	} else {
   1949 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_AUTO;
   1950 	}
   1951 
   1952 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
   1953 		icbp->icb_fwoptions3 |= ICB2400_OPT3_SOFTID;
   1954 	}
   1955 	icbp->icb_logintime = ICB_LOGIN_TOV;
   1956 
   1957 	nwwn = ISP_NODEWWN(isp);
   1958 	pwwn = ISP_PORTWWN(isp);
   1959 
   1960 	if (nwwn && pwwn) {
   1961 		icbp->icb_fwoptions1 |= ICB2400_OPT1_BOTH_WWNS;
   1962 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, nwwn);
   1963 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
   1964 		isp_prt(isp, ISP_LOGDEBUG1,
   1965 		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
   1966 		    ((uint32_t) (nwwn >> 32)),
   1967 		    ((uint32_t) (nwwn & 0xffffffff)),
   1968 		    ((uint32_t) (pwwn >> 32)),
   1969 		    ((uint32_t) (pwwn & 0xffffffff)));
   1970 	} else if (pwwn) {
   1971 		icbp->icb_fwoptions1 &= ~ICB2400_OPT1_BOTH_WWNS;
   1972 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
   1973 		isp_prt(isp, ISP_LOGDEBUG1,
   1974 		    "Setting ICB Port 0x%08x%08x",
   1975 		    ((uint32_t) (pwwn >> 32)),
   1976 		    ((uint32_t) (pwwn & 0xffffffff)));
   1977 	} else {
   1978 		isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
   1979 		return;
   1980 	}
   1981 	icbp->icb_retry_count = fcp->isp_retry_count;
   1982 
   1983 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
   1984 	if (icbp->icb_rqstqlen < 8) {
   1985 		isp_prt(isp, ISP_LOGERR, "bad request queue length %d",
   1986 		    icbp->icb_rqstqlen);
   1987 		return;
   1988 	}
   1989 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
   1990 	if (icbp->icb_rsltqlen < 8) {
   1991 		isp_prt(isp, ISP_LOGERR, "bad result queue length %d",
   1992 		    icbp->icb_rsltqlen);
   1993 		return;
   1994 	}
   1995 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
   1996 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
   1997 	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
   1998 	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
   1999 
   2000 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
   2001 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
   2002 	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
   2003 	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
   2004 
   2005 #ifdef	ISP_TARGET_MODE
   2006 	if (isp->isp_role & ISP_ROLE_TARGET) {
   2007 		icbp->icb_atioqlen = RESULT_QUEUE_LEN(isp);
   2008 		if (icbp->icb_atioqlen < 8) {
   2009 			isp_prt(isp, ISP_LOGERR, "bad ATIO queue length %d",
   2010 			    icbp->icb_atioqlen);
   2011 			return;
   2012 		}
   2013 		icbp->icb_atioqaddr[RQRSP_ADDR0015] =
   2014 		    DMA_WD0(isp->isp_atioq_dma);
   2015 		icbp->icb_atioqaddr[RQRSP_ADDR1631] =
   2016 		    DMA_WD1(isp->isp_atioq_dma);
   2017 		icbp->icb_atioqaddr[RQRSP_ADDR3247] =
   2018 		    DMA_WD2(isp->isp_atioq_dma);
   2019 		icbp->icb_atioqaddr[RQRSP_ADDR4863] =
   2020 		    DMA_WD3(isp->isp_atioq_dma);
   2021 		isp_prt(isp, ISP_LOGDEBUG0,
   2022 		    "isp_fibre_init_2400: atioq %04x%04x%04x%04x",
   2023 		    DMA_WD3(isp->isp_atioq_dma), DMA_WD2(isp->isp_atioq_dma),
   2024 		    DMA_WD1(isp->isp_atioq_dma), DMA_WD0(isp->isp_atioq_dma));
   2025 	}
   2026 #endif
   2027 
   2028 	isp_prt(isp, ISP_LOGDEBUG0,
   2029 	    "isp_fibre_init_2400: fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x",
   2030 	    icbp->icb_fwoptions1, icbp->icb_fwoptions2, icbp->icb_fwoptions3);
   2031 
   2032 	isp_prt(isp, ISP_LOGDEBUG0,
   2033 	    "isp_fibre_init_2400: rqst %04x%04x%04x%04x rsp %04x%04x%04x%04x",
   2034 	    DMA_WD3(isp->isp_rquest_dma), DMA_WD2(isp->isp_rquest_dma),
   2035 	    DMA_WD1(isp->isp_rquest_dma), DMA_WD0(isp->isp_rquest_dma),
   2036 	    DMA_WD3(isp->isp_result_dma), DMA_WD2(isp->isp_result_dma),
   2037 	    DMA_WD1(isp->isp_result_dma), DMA_WD0(isp->isp_result_dma));
   2038 
   2039 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
   2040 		isp_print_bytes(isp, "isp_fibre_init_2400", sizeof (*icbp),
   2041 		    icbp);
   2042 	}
   2043 	FC_SCRATCH_ACQUIRE(isp);
   2044 	isp_put_icb_2400(isp, icbp, fcp->isp_scratch);
   2045 
   2046 
   2047 	/*
   2048 	 * Init the firmware
   2049 	 */
   2050 	MEMZERO(&mbs, sizeof (mbs));
   2051 	mbs.param[0] = MBOX_INIT_FIRMWARE;
   2052 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2053 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2054 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2055 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2056 	mbs.logval = MBLOGALL;
   2057 	mbs.timeout = 30 * 1000000;
   2058 	isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %04x%04x%04x%04x",
   2059 	    DMA_WD3(fcp->isp_scdma), DMA_WD2(fcp->isp_scdma),
   2060 	    DMA_WD1(fcp->isp_scdma), DMA_WD0(fcp->isp_scdma));
   2061 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp));
   2062 	isp_mboxcmd(isp, &mbs);
   2063 	FC_SCRATCH_RELEASE(isp);
   2064 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2065 		return;
   2066 	}
   2067 	isp->isp_reqidx = 0;
   2068 	isp->isp_reqodx = 0;
   2069 	isp->isp_residx = 0;
   2070 
   2071 	/*
   2072 	 * Whatever happens, we're now committed to being here.
   2073 	 */
   2074 	isp->isp_state = ISP_INITSTATE;
   2075 }
   2076 
   2077 static void
   2078 isp_mark_portdb(ispsoftc_t *isp, int onprobation)
   2079 {
   2080 	fcparam *fcp = (fcparam *) isp->isp_param;
   2081 	int i;
   2082 
   2083 	for (i = 0; i < MAX_FC_TARG; i++) {
   2084 		if (onprobation == 0) {
   2085 			MEMZERO(&fcp->portdb[i], sizeof (fcportdb_t));
   2086 		} else {
   2087 			switch (fcp->portdb[i].state) {
   2088 			case FC_PORTDB_STATE_CHANGED:
   2089 			case FC_PORTDB_STATE_PENDING_VALID:
   2090 			case FC_PORTDB_STATE_VALID:
   2091 			case FC_PORTDB_STATE_PROBATIONAL:
   2092 				fcp->portdb[i].state =
   2093 					FC_PORTDB_STATE_PROBATIONAL;
   2094 				break;
   2095 			case FC_PORTDB_STATE_ZOMBIE:
   2096 				break;
   2097 			case FC_PORTDB_STATE_NIL:
   2098 			default:
   2099 				MEMZERO(&fcp->portdb[i], sizeof (fcportdb_t));
   2100 				fcp->portdb[i].state =
   2101 					FC_PORTDB_STATE_NIL;
   2102 				break;
   2103 			}
   2104 		}
   2105 	}
   2106 }
   2107 
   2108 /*
   2109  * Perform an IOCB PLOGI or LOGO via EXECUTE IOCB A64 for 24XX cards
   2110  * or via FABRIC LOGIN/FABRIC LOGOUT for other cards.
   2111  */
   2112 static int
   2113 isp_plogx(ispsoftc_t *isp, uint16_t handle, uint32_t portid, int flags, int gs)
   2114 {
   2115 	mbreg_t mbs;
   2116 	uint8_t q[QENTRY_LEN];
   2117 	isp_plogx_t *plp;
   2118 	uint8_t *scp;
   2119 	uint32_t sst, parm1;
   2120 	int rval;
   2121 
   2122 	if (!IS_24XX(isp)) {
   2123 		int action = flags & PLOGX_FLG_CMD_MASK;
   2124 		if (action == PLOGX_FLG_CMD_PLOGI) {
   2125 			return (isp_port_login(isp, handle, portid));
   2126 		} else if (action == PLOGX_FLG_CMD_LOGO) {
   2127 			return (isp_port_logout(isp, handle, portid));
   2128 		} else {
   2129 			return (MBOX_INVALID_COMMAND);
   2130 		}
   2131 	}
   2132 
   2133 	MEMZERO(q, QENTRY_LEN);
   2134 	plp = (isp_plogx_t *) q;
   2135 	plp->plogx_header.rqs_entry_count = 1;
   2136 	plp->plogx_header.rqs_entry_type = RQSTYPE_LOGIN;
   2137 	plp->plogx_handle = 0xffffffff;
   2138 	plp->plogx_nphdl = handle;
   2139 	plp->plogx_portlo = portid;
   2140 	plp->plogx_rspsz_porthi = (portid >> 16) & 0xff;
   2141 	plp->plogx_flags = flags;
   2142 
   2143 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
   2144 		isp_print_bytes(isp, "IOCB LOGX", QENTRY_LEN, plp);
   2145 	}
   2146 
   2147 	if (gs == 0) {
   2148 		FC_SCRATCH_ACQUIRE(isp);
   2149 	}
   2150 	scp = FCPARAM(isp)->isp_scratch;
   2151 	isp_put_plogx(isp, plp, (isp_plogx_t *) scp);
   2152 
   2153 
   2154 	MEMZERO(&mbs, sizeof (mbs));
   2155 	mbs.param[0] = MBOX_EXEC_COMMAND_IOCB_A64;
   2156 	mbs.param[1] = QENTRY_LEN;
   2157 	mbs.param[2] = DMA_WD1(FCPARAM(isp)->isp_scdma);
   2158 	mbs.param[3] = DMA_WD0(FCPARAM(isp)->isp_scdma);
   2159 	mbs.param[6] = DMA_WD3(FCPARAM(isp)->isp_scdma);
   2160 	mbs.param[7] = DMA_WD2(FCPARAM(isp)->isp_scdma);
   2161 	mbs.timeout = 500000;
   2162 	mbs.logval = MBLOGALL;
   2163 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN);
   2164 	isp_mboxcmd(isp, &mbs);
   2165 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2166 		rval = mbs.param[0];
   2167 		goto out;
   2168 	}
   2169 	MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN);
   2170 	scp += QENTRY_LEN;
   2171 	isp_get_plogx(isp, (isp_plogx_t *) scp, plp);
   2172 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
   2173 		isp_print_bytes(isp, "IOCB LOGX response", QENTRY_LEN, plp);
   2174 	}
   2175 
   2176 	if (plp->plogx_status == PLOGX_STATUS_OK) {
   2177 		rval = 0;
   2178 		goto out;
   2179 	} else if (plp->plogx_status != PLOGX_STATUS_IOCBERR) {
   2180 		isp_prt(isp, ISP_LOGWARN, "status 0x%x on port login IOCB",
   2181 		    plp->plogx_status);
   2182 		rval = -1;
   2183 		goto out;
   2184 	}
   2185 
   2186 	sst = plp->plogx_ioparm[0].lo16 | (plp->plogx_ioparm[0].hi16 << 16);
   2187 	parm1 = plp->plogx_ioparm[1].lo16 | (plp->plogx_ioparm[1].hi16 << 16);
   2188 
   2189 	rval = -1;
   2190 
   2191 	switch (sst) {
   2192 	case PLOGX_IOCBERR_NOLINK:
   2193 		isp_prt(isp, ISP_LOGERR, "PLOGX failed- no link");
   2194 		break;
   2195 	case PLOGX_IOCBERR_NOIOCB:
   2196 		isp_prt(isp, ISP_LOGERR, "PLOGX failed- no IOCB buffer");
   2197 		break;
   2198 	case PLOGX_IOCBERR_NOXGHG:
   2199 		isp_prt(isp, ISP_LOGERR,
   2200 		    "PLOGX failed- no Exchange Control Block");
   2201 		break;
   2202 	case PLOGX_IOCBERR_FAILED:
   2203 		isp_prt(isp, ISP_LOGERR,
   2204 		    "PLOGX(0x%x) of Port 0x%06x failed: reason 0x%x (last LOGIN"
   2205 		    " state 0x%x)", flags, portid, parm1 & 0xff,
   2206 		    (parm1 >> 8) & 0xff);
   2207 		break;
   2208 	case PLOGX_IOCBERR_NOFABRIC:
   2209 		isp_prt(isp, ISP_LOGERR, "PLOGX failed- no fabric");
   2210 		break;
   2211 	case PLOGX_IOCBERR_NOTREADY:
   2212 		isp_prt(isp, ISP_LOGERR, "PLOGX failed- f/w not ready");
   2213 		break;
   2214 	case PLOGX_IOCBERR_NOLOGIN:
   2215 		isp_prt(isp, ISP_LOGERR,
   2216 		    "PLOGX failed- not logged in (last LOGIN state 0x%x)",
   2217 		    parm1);
   2218 		rval = MBOX_NOT_LOGGED_IN;
   2219 		break;
   2220 	case PLOGX_IOCBERR_REJECT:
   2221 		isp_prt(isp, ISP_LOGERR, "PLOGX failed: LS_RJT = 0x%x", parm1);
   2222 		break;
   2223 	case PLOGX_IOCBERR_NOPCB:
   2224 		isp_prt(isp, ISP_LOGERR, "PLOGX failed- no PCB allocated");
   2225 		break;
   2226 	case PLOGX_IOCBERR_EINVAL:
   2227 		isp_prt(isp, ISP_LOGERR,
   2228 		    "PLOGX failed: invalid parameter at offset 0x%x", parm1);
   2229 		break;
   2230 	case PLOGX_IOCBERR_PORTUSED:
   2231 		isp_prt(isp, ISP_LOGDEBUG0,
   2232 		    "portid 0x%x already logged in with N-port handle 0x%x",
   2233 		    portid, parm1);
   2234 		rval = MBOX_PORT_ID_USED | (handle << 16);
   2235 		break;
   2236 	case PLOGX_IOCBERR_HNDLUSED:
   2237 		isp_prt(isp, ISP_LOGDEBUG0,
   2238 		    "N-port handle 0x%x already used for portid 0x%x",
   2239 		    handle, parm1);
   2240 		rval = MBOX_LOOP_ID_USED;
   2241 		break;
   2242 	case PLOGX_IOCBERR_NOHANDLE:
   2243 		isp_prt(isp, ISP_LOGERR, "PLOGX failed- no handle allocated");
   2244 		break;
   2245 	case PLOGX_IOCBERR_NOFLOGI:
   2246 		isp_prt(isp, ISP_LOGERR, "PLOGX failed- no FLOGI_ACC");
   2247 		break;
   2248 	default:
   2249 		isp_prt(isp, ISP_LOGERR, "status %x from %x", plp->plogx_status,
   2250 		    flags);
   2251 		rval = -1;
   2252 		break;
   2253 	}
   2254 out:
   2255 	if (gs == 0) {
   2256 		FC_SCRATCH_RELEASE(isp);
   2257 	}
   2258 	return (rval);
   2259 }
   2260 
   2261 static int
   2262 isp_port_login(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
   2263 {
   2264 	mbreg_t mbs;
   2265 
   2266 	MEMZERO(&mbs, sizeof (mbs));
   2267 	mbs.param[0] = MBOX_FABRIC_LOGIN;
   2268 	if (FCPARAM(isp)->isp_2klogin) {
   2269 		mbs.param[1] = handle;
   2270 		mbs.ibits = (1 << 10);
   2271 	} else {
   2272 		mbs.param[1] = handle << 8;
   2273 	}
   2274 	mbs.param[2] = portid >> 16;
   2275 	mbs.param[3] = portid;
   2276 	mbs.logval = MBLOGNONE;
   2277 	mbs.timeout = 500000;
   2278 	isp_mboxcmd(isp, &mbs);
   2279 
   2280 	switch (mbs.param[0]) {
   2281 	case MBOX_PORT_ID_USED:
   2282 		isp_prt(isp, ISP_LOGDEBUG0,
   2283 		    "isp_plogi_old: portid 0x%06x already logged in as %u",
   2284 		    portid, mbs.param[1]);
   2285 		return (MBOX_PORT_ID_USED | (mbs.param[1] << 16));
   2286 
   2287 	case MBOX_LOOP_ID_USED:
   2288 		isp_prt(isp, ISP_LOGDEBUG0,
   2289 		    "isp_plogi_old: handle %u in use for port id 0x%02xXXXX",
   2290 		    handle, mbs.param[1] & 0xff);
   2291 		return (MBOX_LOOP_ID_USED);
   2292 
   2293 	case MBOX_COMMAND_COMPLETE:
   2294 		return (0);
   2295 
   2296 	case MBOX_COMMAND_ERROR:
   2297 		isp_prt(isp, ISP_LOGINFO,
   2298 		    "isp_plogi_old: error 0x%x in PLOGI to port 0x%06x",
   2299 		    mbs.param[1], portid);
   2300 		return (MBOX_COMMAND_ERROR);
   2301 
   2302 	case MBOX_ALL_IDS_USED:
   2303 		isp_prt(isp, ISP_LOGINFO,
   2304 		    "isp_plogi_old: all IDs used for fabric login");
   2305 		return (MBOX_ALL_IDS_USED);
   2306 
   2307 	default:
   2308 		isp_prt(isp, ISP_LOGINFO,
   2309 		    "isp_plogi_old: error 0x%x on port login of 0x%06x@0x%0x",
   2310 		    mbs.param[0], portid, handle);
   2311 		return (mbs.param[0]);
   2312 	}
   2313 }
   2314 
   2315 static int
   2316 isp_port_logout(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
   2317 {
   2318 	mbreg_t mbs;
   2319 
   2320 	MEMZERO(&mbs, sizeof (mbs));
   2321 	mbs.param[0] = MBOX_FABRIC_LOGOUT;
   2322 	if (FCPARAM(isp)->isp_2klogin) {
   2323 		mbs.param[1] = handle;
   2324 		mbs.ibits = (1 << 10);
   2325 	} else {
   2326 		mbs.param[1] = handle << 8;
   2327 	}
   2328 	mbs.logval = MBLOGNONE;
   2329 	mbs.timeout = 100000;
   2330 	isp_mboxcmd(isp, &mbs);
   2331 	return (mbs.param[0] == MBOX_COMMAND_COMPLETE? 0 : mbs.param[0]);
   2332 }
   2333 
   2334 static int
   2335 isp_getpdb(ispsoftc_t *isp, uint16_t id, isp_pdb_t *pdb, int dolock)
   2336 {
   2337 	fcparam *fcp = (fcparam *) isp->isp_param;
   2338 	mbreg_t mbs;
   2339 	union {
   2340 		isp_pdb_21xx_t fred;
   2341 		isp_pdb_24xx_t bill;
   2342 	} un;
   2343 
   2344 	MEMZERO(&mbs, sizeof (mbs));
   2345 	mbs.param[0] = MBOX_GET_PORT_DB;
   2346 	if (IS_24XX(isp)) {
   2347 		mbs.ibits = 0x3ff;
   2348 		mbs.param[1] = id;
   2349 	} else if (FCPARAM(isp)->isp_2klogin) {
   2350 		mbs.param[1] = id;
   2351 		mbs.ibits = (1 << 10);
   2352 	} else {
   2353 		mbs.param[1] = id << 8;
   2354 	}
   2355 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   2356 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   2357 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   2358 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   2359 	mbs.timeout = 250000;
   2360 	mbs.logval = MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR;
   2361 	if (dolock) {
   2362 		FC_SCRATCH_ACQUIRE(isp);
   2363 	}
   2364 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (un));
   2365 	isp_mboxcmd(isp, &mbs);
   2366 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2367 		if (dolock) {
   2368 			FC_SCRATCH_RELEASE(isp);
   2369 		}
   2370 		return (-1);
   2371 	}
   2372 	if (IS_24XX(isp)) {
   2373 		isp_get_pdb_24xx(isp, fcp->isp_scratch, &un.bill);
   2374 		pdb->handle = un.bill.pdb_handle;
   2375 		pdb->s3_role = un.bill.pdb_prli_svc3;
   2376 		pdb->portid = BITS2WORD_24XX(un.bill.pdb_portid_bits);
   2377 		MEMCPY(pdb->portname, un.bill.pdb_portname, 8);
   2378 		MEMCPY(pdb->nodename, un.bill.pdb_nodename, 8);
   2379 	} else {
   2380 		isp_get_pdb_21xx(isp, fcp->isp_scratch, &un.fred);
   2381 		pdb->handle = un.fred.pdb_loopid;
   2382 		pdb->s3_role = un.fred.pdb_prli_svc3;
   2383 		pdb->portid = BITS2WORD(un.fred.pdb_portid_bits);
   2384 		MEMCPY(pdb->portname, un.fred.pdb_portname, 8);
   2385 		MEMCPY(pdb->nodename, un.fred.pdb_nodename, 8);
   2386 	}
   2387 	if (dolock) {
   2388 		FC_SCRATCH_RELEASE(isp);
   2389 	}
   2390 	return (0);
   2391 }
   2392 
   2393 static uint64_t
   2394 isp_get_portname(ispsoftc_t *isp, int loopid, int nodename)
   2395 {
   2396 	uint64_t wwn = (uint64_t) -1;
   2397 	mbreg_t mbs;
   2398 
   2399 	MEMZERO(&mbs, sizeof (mbs));
   2400 	mbs.param[0] = MBOX_GET_PORT_NAME;
   2401 	if (FCPARAM(isp)->isp_2klogin || IS_24XX(isp)) {
   2402 		mbs.param[1] = loopid;
   2403 		mbs.ibits = (1 << 10);
   2404 		if (nodename) {
   2405 			mbs.param[10] = 1;
   2406 		}
   2407 	} else {
   2408 		mbs.param[1] = loopid << 8;
   2409 		if (nodename) {
   2410 			mbs.param[1] |= 1;
   2411 		}
   2412 	}
   2413 	mbs.logval = MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR;
   2414 	isp_mboxcmd(isp, &mbs);
   2415 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2416 		return (wwn);
   2417 	}
   2418 	if (IS_24XX(isp)) {
   2419 		wwn =
   2420 		    (((uint64_t)(mbs.param[2] >> 8))  << 56) |
   2421 		    (((uint64_t)(mbs.param[2] & 0xff))	<< 48) |
   2422 		    (((uint64_t)(mbs.param[3] >> 8))	<< 40) |
   2423 		    (((uint64_t)(mbs.param[3] & 0xff))	<< 32) |
   2424 		    (((uint64_t)(mbs.param[6] >> 8))	<< 24) |
   2425 		    (((uint64_t)(mbs.param[6] & 0xff))	<< 16) |
   2426 		    (((uint64_t)(mbs.param[7] >> 8))	<<  8) |
   2427 		    (((uint64_t)(mbs.param[7] & 0xff)));
   2428 	} else {
   2429 		wwn =
   2430 		    (((uint64_t)(mbs.param[2] & 0xff))  << 56) |
   2431 		    (((uint64_t)(mbs.param[2] >> 8))	<< 48) |
   2432 		    (((uint64_t)(mbs.param[3] & 0xff))	<< 40) |
   2433 		    (((uint64_t)(mbs.param[3] >> 8))	<< 32) |
   2434 		    (((uint64_t)(mbs.param[6] & 0xff))	<< 24) |
   2435 		    (((uint64_t)(mbs.param[6] >> 8))	<< 16) |
   2436 		    (((uint64_t)(mbs.param[7] & 0xff))	<<  8) |
   2437 		    (((uint64_t)(mbs.param[7] >> 8)));
   2438 	}
   2439 	return (wwn);
   2440 }
   2441 
   2442 /*
   2443  * Make sure we have good FC link.
   2444  */
   2445 
   2446 static int
   2447 isp_fclink_test(ispsoftc_t *isp, int usdelay)
   2448 {
   2449 	static const char *toponames[] = {
   2450 		"Private Loop",
   2451 		"FL Port",
   2452 		"N-Port to N-Port",
   2453 		"F Port",
   2454 		"F Port (no FLOGI_ACC response)"
   2455 	};
   2456 	mbreg_t mbs;
   2457 	int count, check_for_fabric;
   2458 	uint8_t lwfs;
   2459 	int loopid;
   2460 	fcparam *fcp;
   2461 	fcportdb_t *lp;
   2462 	isp_pdb_t pdb;
   2463 
   2464 	fcp = isp->isp_param;
   2465 
   2466 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "FC Link Test Entry");
   2467 	ISP_MARK_PORTDB(isp, 1);
   2468 
   2469 	/*
   2470 	 * Wait up to N microseconds for F/W to go to a ready state.
   2471 	 */
   2472 	lwfs = FW_CONFIG_WAIT;
   2473 	count = 0;
   2474 	while (count < usdelay) {
   2475 		uint64_t enano;
   2476 		uint32_t wrk;
   2477 		NANOTIME_T hra, hrb;
   2478 
   2479 		GET_NANOTIME(&hra);
   2480 		isp_fw_state(isp);
   2481 		if (lwfs != fcp->isp_fwstate) {
   2482 			isp_prt(isp, ISP_LOGCONFIG|ISP_LOGSANCFG,
   2483 			    "Firmware State <%s->%s>",
   2484 			    ispfc_fw_statename((int)lwfs),
   2485 			    ispfc_fw_statename((int)fcp->isp_fwstate));
   2486 			lwfs = fcp->isp_fwstate;
   2487 		}
   2488 		if (fcp->isp_fwstate == FW_READY) {
   2489 			break;
   2490 		}
   2491 		GET_NANOTIME(&hrb);
   2492 
   2493 		/*
   2494 		 * Get the elapsed time in nanoseconds.
   2495 		 * Always guaranteed to be non-zero.
   2496 		 */
   2497 		enano = NANOTIME_SUB(&hrb, &hra);
   2498 
   2499 		isp_prt(isp, ISP_LOGDEBUG1,
   2500 		    "usec%d: 0x%lx->0x%lx enano 0x%x%08x",
   2501 		    count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb),
   2502 		    (uint32_t)(enano >> 32), (uint32_t)(enano & 0xffffffff));
   2503 
   2504 		/*
   2505 		 * If the elapsed time is less than 1 millisecond,
   2506 		 * delay a period of time up to that millisecond of
   2507 		 * waiting.
   2508 		 *
   2509 		 * This peculiar code is an attempt to try and avoid
   2510 		 * invoking uint64_t math support functions for some
   2511 		 * platforms where linkage is a problem.
   2512 		 */
   2513 		if (enano < (1000 * 1000)) {
   2514 			count += 1000;
   2515 			enano = (1000 * 1000) - enano;
   2516 			while (enano > (uint64_t) 4000000000U) {
   2517 				USEC_SLEEP(isp, 4000000);
   2518 				enano -= (uint64_t) 4000000000U;
   2519 			}
   2520 			wrk = enano;
   2521 			wrk /= 1000;
   2522 			USEC_SLEEP(isp, wrk);
   2523 		} else {
   2524 			while (enano > (uint64_t) 4000000000U) {
   2525 				count += 4000000;
   2526 				enano -= (uint64_t) 4000000000U;
   2527 			}
   2528 			wrk = enano;
   2529 			count += (wrk / 1000);
   2530 		}
   2531 	}
   2532 
   2533 	/*
   2534 	 * If we haven't gone to 'ready' state, return.
   2535 	 */
   2536 	if (fcp->isp_fwstate != FW_READY) {
   2537 		isp_prt(isp, ISP_LOGSANCFG,
   2538 		    "isp_fclink_test: not at FW_READY state");
   2539 		return (-1);
   2540 	}
   2541 
   2542 	/*
   2543 	 * Get our Loop ID and Port ID.
   2544 	 */
   2545 	MEMZERO(&mbs, sizeof (mbs));
   2546 	mbs.param[0] = MBOX_GET_LOOP_ID;
   2547 	mbs.logval = MBLOGALL;
   2548 	isp_mboxcmd(isp, &mbs);
   2549 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2550 		return (-1);
   2551 	}
   2552 
   2553 	if (FCPARAM(isp)->isp_2klogin) {
   2554 		fcp->isp_loopid = mbs.param[1];
   2555 	} else {
   2556 		fcp->isp_loopid = mbs.param[1] & 0xff;
   2557 	}
   2558 
   2559 	if (IS_2100(isp)) {
   2560 		fcp->isp_topo = TOPO_NL_PORT;
   2561 	} else {
   2562 		int topo = (int) mbs.param[6];
   2563 		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB) {
   2564 			topo = TOPO_PTP_STUB;
   2565 		}
   2566 		fcp->isp_topo = topo;
   2567 	}
   2568 	fcp->isp_portid = mbs.param[2] | (mbs.param[3] << 16);
   2569 
   2570 	if (IS_2100(isp)) {
   2571 		/*
   2572 		 * Don't bother with fabric if we are using really old
   2573 		 * 2100 firmware. It's just not worth it.
   2574 		 */
   2575 		if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
   2576 			check_for_fabric = 1;
   2577 		} else {
   2578 			check_for_fabric = 0;
   2579 		}
   2580 	} else if (fcp->isp_topo == TOPO_FL_PORT ||
   2581 	    fcp->isp_topo == TOPO_F_PORT) {
   2582 		check_for_fabric = 1;
   2583 	} else {
   2584 		check_for_fabric = 0;
   2585 	}
   2586 
   2587 	if (IS_24XX(isp)) {
   2588 		loopid = NPH_FL_ID;
   2589 	} else {
   2590 		loopid = FL_ID;
   2591 	}
   2592 
   2593 	if (check_for_fabric && isp_getpdb(isp, loopid, &pdb, 1) == 0) {
   2594 		int r;
   2595 		if (IS_2100(isp)) {
   2596 			fcp->isp_topo = TOPO_FL_PORT;
   2597 		}
   2598 		if (pdb.portid == 0) {
   2599 			/*
   2600 			 * Crock.
   2601 			 */
   2602 			fcp->isp_topo = TOPO_NL_PORT;
   2603 			goto not_on_fabric;
   2604 		}
   2605 
   2606 		/*
   2607 		 * Save the Fabric controller's port database entry.
   2608 		 */
   2609 		lp = &fcp->portdb[FL_ID];
   2610 		lp->state = FC_PORTDB_STATE_PENDING_VALID;
   2611 		MAKE_WWN_FROM_NODE_NAME(lp->node_wwn, pdb.nodename);
   2612 		MAKE_WWN_FROM_NODE_NAME(lp->port_wwn, pdb.portname);
   2613 		lp->roles = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   2614 		lp->portid = pdb.portid;
   2615 		lp->handle = pdb.handle;
   2616 		lp->new_portid = lp->portid;
   2617 		lp->new_roles = lp->roles;
   2618 		if (IS_24XX(isp)) {
   2619 			r = isp_register_fc4_type_24xx(isp);
   2620 		} else {
   2621 			r = isp_register_fc4_type(isp);
   2622 		}
   2623 		if (r) {
   2624 			isp_prt(isp, ISP_LOGSANCFG,
   2625 			    "isp_fclink_test: register fc4 type failed");
   2626 			return (-1);
   2627 		}
   2628 	} else {
   2629 not_on_fabric:
   2630 		fcp->portdb[FL_ID].state = FC_PORTDB_STATE_NIL;
   2631 	}
   2632 
   2633 	fcp->isp_gbspeed = 1;
   2634 	if (IS_23XX(isp) || IS_24XX(isp)) {
   2635 		MEMZERO(&mbs, sizeof (mbs));
   2636 		mbs.param[0] = MBOX_GET_SET_DATA_RATE;
   2637 		mbs.param[1] = MBGSD_GET_RATE;
   2638 		/* mbs.param[2] undefined if we're just getting rate */
   2639 		mbs.logval = MBLOGALL;
   2640 		mbs.timeout = 3000000;
   2641 		isp_mboxcmd(isp, &mbs);
   2642 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   2643 			if (mbs.param[1] == MBGSD_FOURGB) {
   2644 				isp_prt(isp, ISP_LOGINFO, "4Gb link speed/s");
   2645 				fcp->isp_gbspeed = 4;
   2646 			} if (mbs.param[1] == MBGSD_TWOGB) {
   2647 				isp_prt(isp, ISP_LOGINFO, "2Gb link speed/s");
   2648 				fcp->isp_gbspeed = 2;
   2649 			}
   2650 		}
   2651 	}
   2652 
   2653 	/*
   2654 	 * Announce ourselves, too.
   2655 	 */
   2656 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGCONFIG, topology, fcp->isp_portid,
   2657 	    fcp->isp_loopid, toponames[fcp->isp_topo]);
   2658 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGCONFIG, ourwwn,
   2659 	    (uint32_t) (ISP_NODEWWN(isp) >> 32),
   2660 	    (uint32_t) ISP_NODEWWN(isp),
   2661 	    (uint32_t) (ISP_PORTWWN(isp) >> 32),
   2662 	    (uint32_t) ISP_PORTWWN(isp));
   2663 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "FC Link Test Complete");
   2664 	return (0);
   2665 }
   2666 
   2667 static const char *
   2668 ispfc_fw_statename(int state)
   2669 {
   2670 	switch(state) {
   2671 	case FW_CONFIG_WAIT:	return "Config Wait";
   2672 	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
   2673 	case FW_WAIT_LOGIN:	return "Wait Login";
   2674 	case FW_READY:		return "Ready";
   2675 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
   2676 	case FW_ERROR:		return "Error";
   2677 	case FW_REINIT:		return "Re-Init";
   2678 	case FW_NON_PART:	return "Nonparticipating";
   2679 	default:		return "?????";
   2680 	}
   2681 }
   2682 
   2683 /*
   2684  * Complete the synchronization of our Port Database.
   2685  *
   2686  * At this point, we've scanned the local loop (if any) and the fabric
   2687  * and performed fabric logins on all new devices.
   2688  *
   2689  * Our task here is to go through our port database and remove any entities
   2690  * that are still marked probational (issuing PLOGO for ones which we had
   2691  * PLOGI'd into) or are dead.
   2692  *
   2693  * Our task here is to also check policy to decide whether devices which
   2694  * have *changed* in some way should still be kept active. For example,
   2695  * if a device has just changed PortID, we can either elect to treat it
   2696  * as an old device or as a newly arrived device (and notify the outer
   2697  * layer appropriately).
   2698  *
   2699  * We also do initiator map target id assignment here for new initiator
   2700  * devices and refresh old ones ot make sure that they point to the corret
   2701  * entities.
   2702  */
   2703 static int
   2704 isp_pdb_sync(ispsoftc_t *isp)
   2705 {
   2706 	fcparam *fcp = isp->isp_param;
   2707 	fcportdb_t *lp;
   2708 	uint16_t dbidx;
   2709 
   2710 	if (fcp->isp_loopstate == LOOP_READY) {
   2711 		return (0);
   2712 	}
   2713 
   2714 	/*
   2715 	 * Make sure we're okay for doing this right now.
   2716 	 */
   2717 	if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
   2718 	    fcp->isp_loopstate != LOOP_FSCAN_DONE &&
   2719 	    fcp->isp_loopstate != LOOP_LSCAN_DONE) {
   2720 		isp_prt(isp, ISP_LOGWARN, "isp_pdb_sync: bad loopstate %d",
   2721 		    fcp->isp_loopstate);
   2722 		return (-1);
   2723 	}
   2724 
   2725 	if (fcp->isp_topo == TOPO_FL_PORT ||
   2726 	    fcp->isp_topo == TOPO_NL_PORT ||
   2727 	    fcp->isp_topo == TOPO_N_PORT) {
   2728 		if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
   2729 			if (isp_scan_loop(isp) != 0) {
   2730 				isp_prt(isp, ISP_LOGWARN,
   2731 				    "isp_pdb_sync: isp_scan_loop failed");
   2732 				return (-1);
   2733 			}
   2734 		}
   2735 	}
   2736 
   2737 	if (fcp->isp_topo == TOPO_F_PORT || fcp->isp_topo == TOPO_FL_PORT) {
   2738 		if (fcp->isp_loopstate < LOOP_FSCAN_DONE) {
   2739 			if (isp_scan_fabric(isp) != 0) {
   2740 				isp_prt(isp, ISP_LOGWARN,
   2741 				    "isp_pdb_sync: isp_scan_fabric failed");
   2742 				return (-1);
   2743 			}
   2744 		}
   2745 	}
   2746 
   2747 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Synchronizing PDBs");
   2748 
   2749 	fcp->isp_loopstate = LOOP_SYNCING_PDB;
   2750 
   2751 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
   2752 		lp = &fcp->portdb[dbidx];
   2753 
   2754 		if (lp->state == FC_PORTDB_STATE_NIL) {
   2755 			continue;
   2756 		}
   2757 
   2758 		if (lp->state == FC_PORTDB_STATE_VALID) {
   2759 			if (dbidx != FL_ID) {
   2760 				isp_prt(isp,
   2761 				    ISP_LOGERR, "portdb idx %d already valid",
   2762 			    	    dbidx);
   2763 			}
   2764 			continue;
   2765 		}
   2766 
   2767 		switch (lp->state) {
   2768 		case FC_PORTDB_STATE_PROBATIONAL:
   2769 		case FC_PORTDB_STATE_DEAD:
   2770 			/*
   2771 			 * It's up to the outer layers to clear isp_ini_map.
   2772 			 */
   2773 			lp->state = FC_PORTDB_STATE_NIL;
   2774 			isp_async(isp, ISPASYNC_DEV_GONE, lp);
   2775 			if (lp->autologin == 0) {
   2776 				(void) isp_plogx(isp, lp->handle, lp->portid,
   2777 				    PLOGX_FLG_CMD_LOGO |
   2778 				    PLOGX_FLG_IMPLICIT |
   2779 				    PLOGX_FLG_FREE_NPHDL, 0);
   2780 			} else {
   2781 				lp->autologin = 0;
   2782 			}
   2783 			lp->new_roles = 0;
   2784 			lp->new_portid = 0;
   2785 			/*
   2786 			 * Note that we might come out of this with our state
   2787 			 * set to FC_PORTDB_STATE_ZOMBIE.
   2788 			 */
   2789 			break;
   2790 		case FC_PORTDB_STATE_NEW:
   2791 			/*
   2792 			 * It's up to the outer layers to assign a virtual
   2793 			 * target id in isp_ini_map (if any).
   2794 			 */
   2795 			lp->portid = lp->new_portid;
   2796 			lp->roles = lp->new_roles;
   2797 			lp->state = FC_PORTDB_STATE_VALID;
   2798 			isp_async(isp, ISPASYNC_DEV_ARRIVED, lp);
   2799 			lp->new_roles = 0;
   2800 			lp->new_portid = 0;
   2801 			lp->reserved = 0;
   2802 			lp->new_reserved = 0;
   2803 			break;
   2804 		case FC_PORTDB_STATE_CHANGED:
   2805 /*
   2806  * XXXX FIX THIS
   2807  */
   2808 			lp->state = FC_PORTDB_STATE_VALID;
   2809 			isp_async(isp, ISPASYNC_DEV_CHANGED, lp);
   2810 			lp->new_roles = 0;
   2811 			lp->new_portid = 0;
   2812 			lp->reserved = 0;
   2813 			lp->new_reserved = 0;
   2814 			break;
   2815 		case FC_PORTDB_STATE_PENDING_VALID:
   2816 			lp->portid = lp->new_portid;
   2817 			lp->roles = lp->new_roles;
   2818 			if (lp->ini_map_idx) {
   2819 				int t = lp->ini_map_idx - 1;
   2820 				fcp->isp_ini_map[t] = dbidx + 1;
   2821 			}
   2822 			lp->state = FC_PORTDB_STATE_VALID;
   2823 			isp_async(isp, ISPASYNC_DEV_STAYED, lp);
   2824 			if (dbidx != FL_ID) {
   2825 				lp->new_roles = 0;
   2826 				lp->new_portid = 0;
   2827 			}
   2828 			lp->reserved = 0;
   2829 			lp->new_reserved = 0;
   2830 			break;
   2831 		case FC_PORTDB_STATE_ZOMBIE:
   2832 			break;
   2833 		default:
   2834 			isp_prt(isp, ISP_LOGWARN,
   2835 			    "isp_scan_loop: state %d for idx %d",
   2836 			    lp->state, dbidx);
   2837 			isp_dump_portdb(isp);
   2838 		}
   2839 	}
   2840 
   2841 	/*
   2842 	 * If we get here, we've for sure seen not only a valid loop
   2843 	 * but know what is or isn't on it, so mark this for usage
   2844 	 * in isp_start.
   2845 	 */
   2846 	fcp->loop_seen_once = 1;
   2847 	fcp->isp_loopstate = LOOP_READY;
   2848 	return (0);
   2849 }
   2850 
   2851 /*
   2852  * Scan local loop for devices.
   2853  */
   2854 static int
   2855 isp_scan_loop(ispsoftc_t *isp)
   2856 {
   2857 	fcportdb_t *lp, tmp;
   2858 	fcparam *fcp = isp->isp_param;
   2859 	int i;
   2860 	isp_pdb_t pdb;
   2861 	uint16_t handle, lim = 0;
   2862 
   2863 	if (fcp->isp_fwstate < FW_READY ||
   2864 	    fcp->isp_loopstate < LOOP_PDB_RCVD) {
   2865 		return (-1);
   2866 	}
   2867 
   2868 	if (fcp->isp_loopstate > LOOP_SCANNING_LOOP) {
   2869 		return (0);
   2870 	}
   2871 
   2872 	/*
   2873 	 * Check our connection topology.
   2874 	 *
   2875 	 * If we're a public or private loop, we scan 0..125 as handle values.
   2876 	 * The firmware has (typically) peformed a PLOGI for us.
   2877 	 *
   2878 	 * If we're a N-port connection, we treat this is a short loop (0..1).
   2879 	 *
   2880 	 * If we're in target mode, we can all possible handles to see who
   2881 	 * might have logged into us.
   2882 	 */
   2883 	switch (fcp->isp_topo) {
   2884 	case TOPO_NL_PORT:
   2885 	case TOPO_FL_PORT:
   2886 		lim = LOCAL_LOOP_LIM;
   2887 		break;
   2888 	case TOPO_N_PORT:
   2889 		lim = 2;
   2890 		break;
   2891 	default:
   2892 		isp_prt(isp, ISP_LOGDEBUG0, "no loop topology to scan");
   2893 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
   2894 		return (0);
   2895 	}
   2896 
   2897 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
   2898 
   2899 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "FC scan loop 0..%d", lim-1);
   2900 
   2901 
   2902 	/*
   2903 	 * Run through the list and get the port database info for each one.
   2904 	 */
   2905 	for (handle = 0; handle < lim; handle++) {
   2906 		/*
   2907 		 * But don't even try for ourselves...
   2908 	 	 */
   2909 		if (handle == fcp->isp_loopid) {
   2910 			continue;
   2911 		}
   2912 
   2913 		/*
   2914 		 * In older cards with older f/w GET_PORT_DATABASE has been
   2915 		 * known to hang. This trick gets around that problem.
   2916 		 */
   2917 		if (IS_2100(isp) || IS_2200(isp)) {
   2918 			uint64_t node_wwn = isp_get_portname(isp, handle, 1);
   2919 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
   2920 				return (-1);
   2921 			}
   2922 			if (node_wwn == 0) {
   2923 				continue;
   2924 			}
   2925 		}
   2926 
   2927 		/*
   2928 		 * Get the port database entity for this index.
   2929 		 */
   2930 		if (isp_getpdb(isp, handle, &pdb, 1) != 0) {
   2931 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
   2932 				ISP_MARK_PORTDB(isp, 1);
   2933 				return (-1);
   2934 			}
   2935 			continue;
   2936 		}
   2937 
   2938 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
   2939 			ISP_MARK_PORTDB(isp, 1);
   2940 			return (-1);
   2941 		}
   2942 
   2943 		/*
   2944 		 * On *very* old 2100 firmware we would end up sometimes
   2945 		 * with the firmware returning the port database entry
   2946 		 * for something else. We used to restart this, but
   2947 		 * now we just punt.
   2948 		 */
   2949 		if (IS_2100(isp) && pdb.handle != handle) {
   2950 			isp_prt(isp, ISP_LOGWARN,
   2951 			    "giving up on synchronizing the port database");
   2952 			ISP_MARK_PORTDB(isp, 1);
   2953 			return (-1);
   2954 		}
   2955 
   2956 		/*
   2957 		 * Save the pertinent info locally.
   2958 		 */
   2959 		MAKE_WWN_FROM_NODE_NAME(tmp.node_wwn, pdb.nodename);
   2960 		MAKE_WWN_FROM_NODE_NAME(tmp.port_wwn, pdb.portname);
   2961 		tmp.roles = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   2962 		tmp.portid = pdb.portid;
   2963 		tmp.handle = pdb.handle;
   2964 
   2965 		/*
   2966 		 * Check to make sure it's still a valid entry. The 24XX seems
   2967 		 * to return a portid but not a WWPN/WWNN or role for devices
   2968 		 * which shift on a loop.
   2969 		 */
   2970 		if (tmp.node_wwn == 0 || tmp.port_wwn == 0 || tmp.portid == 0) {
   2971 			int a, b, c;
   2972 			a = (tmp.node_wwn == 0);
   2973 			b = (tmp.port_wwn == 0);
   2974 			c = (tmp.portid == 0);
   2975 			isp_prt(isp, ISP_LOGWARN,
   2976 			    "bad pdb (%1d%1d%1d) @ handle 0x%x", a, b, c,
   2977 			    handle);
   2978 			isp_dump_portdb(isp);
   2979 			continue;
   2980 		}
   2981 
   2982 		/*
   2983 		 * Now search the entire port database
   2984 		 * for the same Port and Node WWN.
   2985 		 */
   2986 		for (i = 0; i < MAX_FC_TARG; i++) {
   2987 			lp = &fcp->portdb[i];
   2988 			if (lp->state == FC_PORTDB_STATE_NIL) {
   2989 				continue;
   2990 			}
   2991 			if (lp->node_wwn != tmp.node_wwn) {
   2992 				continue;
   2993 			}
   2994 			if (lp->port_wwn != tmp.port_wwn) {
   2995 				continue;
   2996 			}
   2997 
   2998 			/*
   2999 			 * Okay- we've found a non-nil entry that matches.
   3000 			 * Check to make sure it's probational or a zombie.
   3001 			 */
   3002 			if (lp->state != FC_PORTDB_STATE_PROBATIONAL &&
   3003 			    lp->state != FC_PORTDB_STATE_ZOMBIE) {
   3004 				isp_prt(isp, ISP_LOGERR,
   3005 				    "[%d] not probational/zombie (0x%x)",
   3006 				    i, lp->state);
   3007 				isp_dump_portdb(isp);
   3008 				ISP_MARK_PORTDB(isp, 1);
   3009 				return (-1);
   3010 			}
   3011 
   3012 			/*
   3013 			 * Mark the device as something the f/w logs into
   3014 			 * automatically.
   3015 			 */
   3016 			lp->autologin = 1;
   3017 
   3018 			/*
   3019 			 * Check to make see if really still the same
   3020 			 * device. If it is, we mark it pending valid.
   3021 			 */
   3022 			if (lp->portid == tmp.portid &&
   3023 			    lp->handle == tmp.handle &&
   3024 			    lp->roles == tmp.roles) {
   3025 				lp->new_portid = tmp.portid;
   3026 				lp->new_roles = tmp.roles;
   3027 				lp->state = FC_PORTDB_STATE_PENDING_VALID;
   3028 				isp_prt(isp, ISP_LOGSANCFG,
   3029 				    "Loop Port 0x%02x@0x%x Pending Valid",
   3030 				    tmp.portid, tmp.handle);
   3031 				break;
   3032 			}
   3033 
   3034 			/*
   3035 			 * We can wipe out the old handle value
   3036 			 * here because it's no longer valid.
   3037 			 */
   3038 			lp->handle = tmp.handle;
   3039 
   3040 			/*
   3041 			 * Claim that this has changed and let somebody else
   3042 			 * decide what to do.
   3043 			 */
   3044 			isp_prt(isp, ISP_LOGSANCFG,
   3045 			    "Loop Port 0x%02x@0x%x changed",
   3046 			    tmp.portid, tmp.handle);
   3047 			lp->state = FC_PORTDB_STATE_CHANGED;
   3048 			lp->new_portid = tmp.portid;
   3049 			lp->new_roles = tmp.roles;
   3050 			break;
   3051 		}
   3052 
   3053 		/*
   3054 		 * Did we find and update an old entry?
   3055 		 */
   3056 		if (i < MAX_FC_TARG) {
   3057 			continue;
   3058 		}
   3059 
   3060 		/*
   3061 		 * Ah. A new device entry. Find an empty slot
   3062 		 * for it and save info for later disposition.
   3063 		 */
   3064 		for (i = 0; i < MAX_FC_TARG; i++) {
   3065 			if (fcp->portdb[i].state == FC_PORTDB_STATE_NIL) {
   3066 				break;
   3067 			}
   3068 		}
   3069 		if (i == MAX_FC_TARG) {
   3070 			isp_prt(isp, ISP_LOGERR, "out of portdb entries");
   3071 			continue;
   3072 		}
   3073 		lp = &fcp->portdb[i];
   3074 
   3075 		MEMZERO(lp, sizeof (fcportdb_t));
   3076 		lp->autologin = 1;
   3077 		lp->state = FC_PORTDB_STATE_NEW;
   3078 		lp->new_portid = tmp.portid;
   3079 		lp->new_roles = tmp.roles;
   3080 		lp->handle = tmp.handle;
   3081 		lp->port_wwn = tmp.port_wwn;
   3082 		lp->node_wwn = tmp.node_wwn;
   3083 		isp_prt(isp, ISP_LOGSANCFG,
   3084 		    "Loop Port 0x%02x@0x%x is New Entry",
   3085 		    tmp.portid, tmp.handle);
   3086 	}
   3087 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
   3088 	return (0);
   3089 }
   3090 
   3091 /*
   3092  * Scan the fabric for devices and add them to our port database.
   3093  *
   3094  * Use the GID_FT command to get all Port IDs for FC4 SCSI devices it knows.
   3095  *
   3096  * For 2100-23XX cards, we can use the SNS mailbox command to pass simple
   3097  * name server commands to the switch management server via the QLogic f/w.
   3098  *
   3099  * For the 24XX card, we have to use CT-Pass through run via the Execute IOCB
   3100  * mailbox command.
   3101  *
   3102  * The net result is to leave the list of Port IDs setting untranslated in
   3103  * offset IGPOFF of the FC scratch area, whereupon we'll canonicalize it to
   3104  * host order at OGPOFF.
   3105  */
   3106 
   3107 /*
   3108  * Take less than half of our scratch area to store Port IDs
   3109  */
   3110 #define	GIDLEN	((ISP2100_SCRLEN >> 1) - 16 - SNS_GID_FT_REQ_SIZE)
   3111 #define	NGENT	((GIDLEN - 16) >> 2)
   3112 
   3113 #define	IGPOFF	(2 * QENTRY_LEN)
   3114 #define	OGPOFF	(ISP2100_SCRLEN >> 1)
   3115 #define	ZTXOFF	(ISP2100_SCRLEN - (1 * QENTRY_LEN))
   3116 #define	CTXOFF	(ISP2100_SCRLEN - (2 * QENTRY_LEN))
   3117 #define	XTXOFF	(ISP2100_SCRLEN - (3 * QENTRY_LEN))
   3118 
   3119 static int
   3120 isp_gid_ft_sns(ispsoftc_t *isp)
   3121 {
   3122 	union {
   3123 		sns_gid_ft_req_t _x;
   3124 		uint8_t _y[SNS_GID_FT_REQ_SIZE];
   3125 	} un;
   3126 	fcparam *fcp = FCPARAM(isp);
   3127 	sns_gid_ft_req_t *rq = &un._x;
   3128 	mbreg_t mbs;
   3129 
   3130 	isp_prt(isp, ISP_LOGDEBUG0, "scanning fabric (GID_FT) via SNS");
   3131 
   3132 	MEMZERO(rq, SNS_GID_FT_REQ_SIZE);
   3133 	rq->snscb_rblen = GIDLEN >> 1;
   3134 	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + IGPOFF);
   3135 	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + IGPOFF);
   3136 	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + IGPOFF);
   3137 	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + IGPOFF);
   3138 	rq->snscb_sblen = 6;
   3139 	rq->snscb_cmd = SNS_GID_FT;
   3140 	rq->snscb_mword_div_2 = NGENT;
   3141 	rq->snscb_fc4_type = FC4_SCSI;
   3142 
   3143 	isp_put_gid_ft_request(isp, rq, fcp->isp_scratch);
   3144 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE);
   3145 
   3146 	MEMZERO(&mbs, sizeof (mbs));
   3147 	mbs.param[0] = MBOX_SEND_SNS;
   3148 	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
   3149 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   3150 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   3151 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   3152 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   3153 	mbs.logval = MBLOGALL;
   3154 	mbs.timeout = 10000000;
   3155 	isp_mboxcmd(isp, &mbs);
   3156 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3157 		if (mbs.param[0] == MBOX_INVALID_COMMAND) {
   3158 			return (1);
   3159 		} else {
   3160 			return (-1);
   3161 		}
   3162 	}
   3163 	return (0);
   3164 }
   3165 
   3166 static int
   3167 isp_gid_ft_ct_passthru(ispsoftc_t *isp)
   3168 {
   3169 	mbreg_t mbs;
   3170 	fcparam *fcp = FCPARAM(isp);
   3171 	union {
   3172 		isp_ct_pt_t plocal;
   3173 		ct_hdr_t clocal;
   3174 		uint8_t q[QENTRY_LEN];
   3175 	} un;
   3176 	isp_ct_pt_t *pt;
   3177 	ct_hdr_t *ct;
   3178 	uint32_t *rp;
   3179 	uint8_t *scp = fcp->isp_scratch;
   3180 
   3181 	isp_prt(isp, ISP_LOGDEBUG0, "scanning fabric (GID_FT) via CT");
   3182 
   3183 	if (!IS_24XX(isp)) {
   3184 		return (1);
   3185 	}
   3186 
   3187 	/*
   3188 	 * Build a Passthrough IOCB in memory.
   3189 	 */
   3190 	pt = &un.plocal;
   3191 	MEMZERO(un.q, QENTRY_LEN);
   3192 	pt->ctp_header.rqs_entry_count = 1;
   3193 	pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
   3194 	pt->ctp_handle = 0xffffffff;
   3195 	pt->ctp_nphdl = NPH_SNS_ID;
   3196 	pt->ctp_cmd_cnt = 1;
   3197 	pt->ctp_time = 30;
   3198 	pt->ctp_rsp_cnt = 1;
   3199 	pt->ctp_rsp_bcnt = GIDLEN;
   3200 	pt->ctp_cmd_bcnt = sizeof (*ct) + sizeof (uint32_t);
   3201 	pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
   3202 	pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
   3203 	pt->ctp_dataseg[0].ds_count = sizeof (*ct) + sizeof (uint32_t);
   3204 	pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
   3205 	pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
   3206 	pt->ctp_dataseg[1].ds_count = GIDLEN;
   3207 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
   3208 		isp_print_bytes(isp, "ct IOCB", QENTRY_LEN, pt);
   3209 	}
   3210 	isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
   3211 
   3212 	/*
   3213 	 * Build the CT header and command in memory.
   3214 	 *
   3215 	 * Note that the CT header has to end up as Big Endian format in memory.
   3216 	 */
   3217 	ct = &un.clocal;
   3218 	MEMZERO(ct, sizeof (*ct));
   3219 	ct->ct_revision = CT_REVISION;
   3220 	ct->ct_fcs_type = CT_FC_TYPE_FC;
   3221 	ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
   3222 	ct->ct_cmd_resp = SNS_GID_FT;
   3223 	ct->ct_bcnt_resid = (GIDLEN - 16) >> 2;
   3224 
   3225 	isp_put_ct_hdr(isp, ct, (ct_hdr_t *) &scp[XTXOFF]);
   3226 	rp = (uint32_t *) &scp[XTXOFF+sizeof (*ct)];
   3227 	ISP_IOZPUT_32(isp, FC4_SCSI, rp);
   3228 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
   3229 		isp_print_bytes(isp, "CT HDR + payload after put",
   3230 		    sizeof (*ct) + sizeof (uint32_t), &scp[XTXOFF]);
   3231 	}
   3232 	MEMZERO(&scp[ZTXOFF], QENTRY_LEN);
   3233 	MEMZERO(&mbs, sizeof (mbs));
   3234 	mbs.param[0] = MBOX_EXEC_COMMAND_IOCB_A64;
   3235 	mbs.param[1] = QENTRY_LEN;
   3236 	mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
   3237 	mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
   3238 	mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
   3239 	mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
   3240 	mbs.timeout = 500000;
   3241 	mbs.logval = MBLOGALL;
   3242 	MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN);
   3243 	isp_mboxcmd(isp, &mbs);
   3244 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3245 		return (-1);
   3246 	}
   3247 	MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN);
   3248 	pt = &un.plocal;
   3249 	isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
   3250 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
   3251 		isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
   3252 	}
   3253 
   3254 	if (pt->ctp_status && pt->ctp_status != RQCS_DATA_UNDERRUN) {
   3255 		isp_prt(isp, ISP_LOGWARN, "CT Passthrough returned 0x%x",
   3256 		    pt->ctp_status);
   3257 		return (-1);
   3258 	}
   3259 	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN + 16);
   3260 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
   3261 		isp_print_bytes(isp, "CT response", GIDLEN+16, &scp[IGPOFF]);
   3262 	}
   3263 	return (0);
   3264 }
   3265 
   3266 static int
   3267 isp_scan_fabric(ispsoftc_t *isp)
   3268 {
   3269 	fcparam *fcp = FCPARAM(isp);
   3270 	uint32_t portid;
   3271 	uint16_t handle, oldhandle;
   3272 	int portidx, portlim, r;
   3273 	sns_gid_ft_rsp_t *rs0, *rs1;
   3274 
   3275 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "FC Scan Fabric");
   3276 	if (fcp->isp_fwstate != FW_READY ||
   3277 	    fcp->isp_loopstate < LOOP_LSCAN_DONE) {
   3278 		return (-1);
   3279 	}
   3280 	if (fcp->isp_loopstate > LOOP_SCANNING_FABRIC) {
   3281 		return (0);
   3282 	}
   3283 	if (fcp->isp_topo != TOPO_FL_PORT && fcp->isp_topo != TOPO_F_PORT) {
   3284 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
   3285 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
   3286 		    "FC Scan Fabric Done (no fabric)");
   3287 		return (0);
   3288 	}
   3289 
   3290 	FC_SCRATCH_ACQUIRE(isp);
   3291 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
   3292 
   3293 	if (IS_24XX(isp)) {
   3294 		r = isp_gid_ft_ct_passthru(isp);
   3295 	} else {
   3296 		r = isp_gid_ft_sns(isp);
   3297 	}
   3298 
   3299 	if (r > 0) {
   3300 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
   3301 		FC_SCRATCH_RELEASE(isp);
   3302 		return (0);
   3303 	} else if (r < 0) {
   3304 		fcp->isp_loopstate = LOOP_PDB_RCVD;	/* try again */
   3305 		FC_SCRATCH_RELEASE(isp);
   3306 		return (0);
   3307 	}
   3308 	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   3309 		FC_SCRATCH_RELEASE(isp);
   3310 		return (-1);
   3311 	}
   3312 
   3313 	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN);
   3314 	rs0 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+IGPOFF);
   3315 	rs1 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+OGPOFF);
   3316 	isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
   3317 	if (rs1->snscb_cthdr.ct_cmd_resp != LS_ACC) {
   3318 		int level;
   3319 		if (rs1->snscb_cthdr.ct_reason == 9 &&
   3320 		    rs1->snscb_cthdr.ct_explanation == 7) {
   3321 			level = ISP_LOGSANCFG|ISP_LOGDEBUG0;
   3322 		} else {
   3323 			level = ISP_LOGWARN;
   3324 		}
   3325 		isp_prt(isp, level, "Fabric Nameserver rejected GID_FT "
   3326 		    "(Reason=0x%x Expl=0x%x)", rs1->snscb_cthdr.ct_reason,
   3327 		    rs1->snscb_cthdr.ct_explanation);
   3328 		FC_SCRATCH_RELEASE(isp);
   3329 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
   3330 		return (0);
   3331 	}
   3332 
   3333 
   3334 	/*
   3335 	 * If we get this far, we certainly still have the fabric controller.
   3336 	 */
   3337 	fcp->portdb[FL_ID].state = FC_PORTDB_STATE_PENDING_VALID;
   3338 
   3339 	/*
   3340 	 * Prime the handle we will start using.
   3341 	 */
   3342 	oldhandle = NIL_HANDLE;
   3343 
   3344 	/*
   3345 	 * Okay, we now have a list of Port IDs for all FC4 SCSI devices
   3346 	 * that the Fabric Name server knows about. Go through the list
   3347 	 * and remove duplicate port ids.
   3348 	 */
   3349 
   3350 	portlim = 0;
   3351 	portidx = 0;
   3352 	for (portidx = 0; portidx < NGENT-1; portidx++) {
   3353 		if (rs1->snscb_ports[portidx].control & 0x80) {
   3354 			break;
   3355 		}
   3356 	}
   3357 
   3358 	/*
   3359 	 * If we're not at the last entry, our list wasn't big enough.
   3360 	 */
   3361 	if ((rs1->snscb_ports[portidx].control & 0x80) == 0) {
   3362 		isp_prt(isp, ISP_LOGWARN,
   3363 		    "fabric too big for scratch area: increase ISP2100_SCRLEN");
   3364 	}
   3365 	portlim = portidx + 1;
   3366 	isp_prt(isp, ISP_LOGSANCFG,
   3367 	    "got %d ports back from name server", portlim);
   3368 
   3369 	for (portidx = 0; portidx < portlim; portidx++) {
   3370 		int npidx;
   3371 
   3372 		portid =
   3373 		    ((rs1->snscb_ports[portidx].portid[0]) << 16) |
   3374 		    ((rs1->snscb_ports[portidx].portid[1]) << 8) |
   3375 		    ((rs1->snscb_ports[portidx].portid[2]));
   3376 
   3377 		for (npidx = portidx + 1; npidx < portlim; npidx++) {
   3378 			uint32_t new_portid =
   3379 			    ((rs1->snscb_ports[npidx].portid[0]) << 16) |
   3380 			    ((rs1->snscb_ports[npidx].portid[1]) << 8) |
   3381 			    ((rs1->snscb_ports[npidx].portid[2]));
   3382 			if (new_portid == portid) {
   3383 				break;
   3384 			}
   3385 		}
   3386 
   3387 		if (npidx < portlim) {
   3388 			rs1->snscb_ports[npidx].portid[0] = 0;
   3389 			rs1->snscb_ports[npidx].portid[1] = 0;
   3390 			rs1->snscb_ports[npidx].portid[2] = 0;
   3391 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
   3392 			    "removing duplicate PortID 0x%x entry from list",
   3393 			    portid);
   3394 		}
   3395 	}
   3396 
   3397 	/*
   3398 	 * Okay, we now have a list of Port IDs for all FC4 SCSI devices
   3399 	 * that the Fabric Name server knows about.
   3400 	 *
   3401 	 * For each entry on this list go through our port database looking
   3402 	 * for probational entries- if we find one, then an old entry is
   3403 	 * is maybe still this one. We get some information to find out.
   3404 	 *
   3405 	 * Otherwise, it's a new fabric device, and we log into it
   3406 	 * (unconditionally). After searching the entire database
   3407 	 * again to make sure that we never ever ever ever have more
   3408 	 * than one entry that has the same PortID or the same
   3409 	 * WWNN/WWPN duple, we enter the device into our database.
   3410 	 */
   3411 
   3412 	for (portidx = 0; portidx < portlim; portidx++) {
   3413 		fcportdb_t *lp;
   3414 		isp_pdb_t pdb;
   3415 		uint64_t wwnn, wwpn;
   3416 		int dbidx, nr;
   3417 
   3418 		portid =
   3419 		    ((rs1->snscb_ports[portidx].portid[0]) << 16) |
   3420 		    ((rs1->snscb_ports[portidx].portid[1]) << 8) |
   3421 		    ((rs1->snscb_ports[portidx].portid[2]));
   3422 
   3423 		if (portid == 0) {
   3424 			isp_prt(isp, ISP_LOGSANCFG,
   3425 			    "skipping null PortID at idx %d", portidx);
   3426 			continue;
   3427 		}
   3428 
   3429 		/*
   3430 		 * Skip ourselves...
   3431 		 */
   3432 		if (portid == fcp->isp_portid) {
   3433 			isp_prt(isp, ISP_LOGSANCFG,
   3434 			    "skip ourselves @ PortID 0x%06x", portid);
   3435 			continue;
   3436 		}
   3437 		isp_prt(isp, ISP_LOGSANCFG,
   3438 		    "Checking Fabric Port 0x%06x", portid);
   3439 
   3440 		/*
   3441 		 * We now search our Port Database for any
   3442 		 * probational entries with this PortID. We don't
   3443 		 * look for zombies here- only probational
   3444 		 * entries (we've already logged out of zombies).
   3445 		 */
   3446 		for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
   3447 			lp = &fcp->portdb[dbidx];
   3448 
   3449 			if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
   3450 				continue;
   3451 			}
   3452 			if (lp->portid == portid) {
   3453 				break;
   3454 			}
   3455 		}
   3456 
   3457 		/*
   3458 		 * We found a probational entry with this Port ID.
   3459 		 */
   3460 		if (dbidx < MAX_FC_TARG) {
   3461 			int handle_changed = 0;
   3462 
   3463 			lp = &fcp->portdb[dbidx];
   3464 
   3465 			/*
   3466 			 * See if we're still logged into it.
   3467 			 *
   3468 			 * If we aren't, mark it as a dead device and
   3469 			 * leave the new portid in the database entry
   3470 			 * for somebody further along to decide what to
   3471 			 * do (policy choice).
   3472 			 *
   3473 			 * If we are, check to see if it's the same
   3474 			 * device still (it should be). If for some
   3475 			 * reason it isn't, mark it as a changed device
   3476 			 * and leave the new portid and role in the
   3477 			 * database entry for somebody further along to
   3478 			 * decide what to do (policy choice).
   3479 			 *
   3480 			 */
   3481 
   3482 			r = isp_getpdb(isp, lp->handle, &pdb, 0);
   3483 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   3484 				FC_SCRATCH_RELEASE(isp);
   3485 				ISP_MARK_PORTDB(isp, 1);
   3486 				return (-1);
   3487 			}
   3488 			if (r != 0) {
   3489 				lp->new_portid = portid;
   3490 				lp->state = FC_PORTDB_STATE_DEAD;
   3491 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
   3492 				    "Fabric Port 0x%06x considered dead",
   3493 				    portid);
   3494 				continue;
   3495 			}
   3496 
   3497 
   3498 			/*
   3499 			 * Check to make sure that handle, portid, WWPN and
   3500 			 * WWNN agree. If they don't, then the association
   3501 			 * between this PortID and the stated handle has been
   3502 			 * broken by the firmware.
   3503 			 */
   3504 			MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
   3505 			MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
   3506 			if (pdb.handle != lp->handle ||
   3507 			    pdb.portid != portid ||
   3508 			    wwpn != lp->port_wwn ||
   3509 			    wwnn != lp->node_wwn) {
   3510 				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
   3511 				    fconf, dbidx, pdb.handle, pdb.portid,
   3512 				    (uint32_t) (wwnn >> 32), (uint32_t) wwnn,
   3513 				    (uint32_t) (wwpn >> 32), (uint32_t) wwpn,
   3514 				    lp->handle, portid,
   3515 				    (uint32_t) (lp->node_wwn >> 32),
   3516 				    (uint32_t) lp->node_wwn,
   3517 				    (uint32_t) (lp->port_wwn >> 32),
   3518 				    (uint32_t) lp->port_wwn);
   3519 				/*
   3520 				 * Try to re-login to this device using a
   3521 				 * new handle. If that fails, mark it dead.
   3522 				 *
   3523 				 * isp_login_device will check for handle and
   3524 				 * portid consistency after re-login.
   3525 				 *
   3526 				 */
   3527 				if (isp_login_device(isp, portid, &pdb,
   3528 				    &oldhandle)) {
   3529 					lp->new_portid = portid;
   3530 					lp->state = FC_PORTDB_STATE_DEAD;
   3531 					if (fcp->isp_loopstate !=
   3532 					    LOOP_SCANNING_FABRIC) {
   3533 						FC_SCRATCH_RELEASE(isp);
   3534 						ISP_MARK_PORTDB(isp, 1);
   3535 						return (-1);
   3536 					}
   3537 					continue;
   3538 				}
   3539 				MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
   3540 				MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
   3541 				if (wwpn != lp->port_wwn ||
   3542 				    wwnn != lp->node_wwn) {
   3543 					isp_prt(isp, ISP_LOGWARN, "changed WWN"
   3544 					    " after relogin");
   3545 					lp->new_portid = portid;
   3546 					lp->state = FC_PORTDB_STATE_DEAD;
   3547 					continue;
   3548 				}
   3549 
   3550 				lp->handle = pdb.handle;
   3551 				handle_changed++;
   3552 			}
   3553 
   3554 			nr = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   3555 
   3556 			/*
   3557 			 * Check to see whether the portid and roles have
   3558 			 * stayed the same. If they have stayed the same,
   3559 			 * we believe that this is the same device and it
   3560 			 * hasn't become disconnected and reconnected, so
   3561 			 * mark it as pending valid.
   3562 			 *
   3563 			 * If they aren't the same, mark the device as a
   3564 			 * changed device and save the new port id and role
   3565 			 * and let somebody else decide.
   3566 			 */
   3567 
   3568 			lp->new_portid = portid;
   3569 			lp->new_roles = nr;
   3570 			if (pdb.portid != lp->portid || nr != lp->roles ||
   3571 			    handle_changed) {
   3572 				isp_prt(isp, ISP_LOGSANCFG,
   3573 				    "Fabric Port 0x%06x changed", portid);
   3574 				lp->state = FC_PORTDB_STATE_CHANGED;
   3575 			} else {
   3576 				isp_prt(isp, ISP_LOGSANCFG,
   3577 				    "Fabric Port 0x%06x Now Pending Valid",
   3578 				    portid);
   3579 				lp->state = FC_PORTDB_STATE_PENDING_VALID;
   3580 			}
   3581 			continue;
   3582 		}
   3583 
   3584 		/*
   3585 		 * Ah- a new entry. Search the database again for all non-NIL
   3586 		 * entries to make sure we never ever make a new database entry
   3587 		 * with the same port id. While we're at it, mark where the
   3588 		 * last free entry was.
   3589 		 */
   3590 
   3591 		dbidx = MAX_FC_TARG;
   3592 		for (lp = fcp->portdb; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
   3593 			if (lp >= &fcp->portdb[FL_ID] &&
   3594 			    lp <= &fcp->portdb[SNS_ID]) {
   3595 				continue;
   3596 			}
   3597 			if (lp->state == FC_PORTDB_STATE_NIL) {
   3598 				if (dbidx == MAX_FC_TARG) {
   3599 					dbidx = lp - fcp->portdb;
   3600 				}
   3601 				continue;
   3602 			}
   3603 			if (lp->state == FC_PORTDB_STATE_ZOMBIE) {
   3604 				continue;
   3605 			}
   3606 			if (lp->portid == portid) {
   3607 				break;
   3608 			}
   3609 		}
   3610 
   3611 		if (lp < &fcp->portdb[MAX_FC_TARG]) {
   3612 			isp_prt(isp, ISP_LOGWARN,
   3613 			    "PortID 0x%06x already at %d handle %d state %d",
   3614 			    portid, dbidx, lp->handle, lp->state);
   3615 			continue;
   3616 		}
   3617 
   3618 		/*
   3619 		 * We should have the index of the first free entry seen.
   3620 		 */
   3621 		if (dbidx == MAX_FC_TARG) {
   3622 			isp_prt(isp, ISP_LOGERR,
   3623 			    "port database too small to login PortID 0x%06x"
   3624 			    "- increase MAX_FC_TARG", portid);
   3625 			continue;
   3626 		}
   3627 
   3628 		/*
   3629 		 * Otherwise, point to our new home.
   3630 		 */
   3631 		lp = &fcp->portdb[dbidx];
   3632 
   3633 		/*
   3634 		 * Try to see if we are logged into this device,
   3635 		 * and maybe log into it.
   3636 		 *
   3637 		 * isp_login_device will check for handle and
   3638 		 * portid consistency after login.
   3639 		 */
   3640 		if (isp_login_device(isp, portid, &pdb, &oldhandle)) {
   3641 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   3642 				FC_SCRATCH_RELEASE(isp);
   3643 				ISP_MARK_PORTDB(isp, 1);
   3644 				return (-1);
   3645 			}
   3646 			continue;
   3647 		}
   3648 
   3649 		handle = pdb.handle;
   3650 		MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
   3651 		MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
   3652 		nr = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   3653 
   3654 		/*
   3655 		 * And go through the database *one* more time to make sure
   3656 		 * that we do not make more than one entry that has the same
   3657 		 * WWNN/WWPN duple
   3658 		 */
   3659 		for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
   3660 			if (dbidx >= FL_ID && dbidx <= SNS_ID) {
   3661 				continue;
   3662 			}
   3663 			if (fcp->portdb[dbidx].state == FC_PORTDB_STATE_NIL) {
   3664 				continue;
   3665 			}
   3666 			if (fcp->portdb[dbidx].node_wwn == wwnn &&
   3667 			    fcp->portdb[dbidx].port_wwn == wwpn) {
   3668 				break;
   3669 			}
   3670 		}
   3671 
   3672 		if (dbidx == MAX_FC_TARG) {
   3673 			MEMZERO(lp, sizeof (fcportdb_t));
   3674 			lp->handle = handle;
   3675 			lp->node_wwn = wwnn;
   3676 			lp->port_wwn = wwpn;
   3677 			lp->new_portid = portid;
   3678 			lp->new_roles = nr;
   3679 			lp->state = FC_PORTDB_STATE_NEW;
   3680 			isp_prt(isp, ISP_LOGSANCFG,
   3681 			    "Fabric Port 0x%06x is New Entry", portid);
   3682 			continue;
   3683 		}
   3684 
   3685     		if (fcp->portdb[dbidx].state != FC_PORTDB_STATE_ZOMBIE) {
   3686 			isp_prt(isp, ISP_LOGWARN,
   3687 			    "PortID 0x%x 0x%08x%08x/0x%08x%08x %ld already at "
   3688 			    "idx %d, state 0x%x", portid,
   3689 			    (uint32_t) (wwnn >> 32), (uint32_t) wwnn,
   3690 			    (uint32_t) (wwpn >> 32), (uint32_t) wwpn,
   3691 			    (long) (lp - fcp->portdb), dbidx,
   3692 			    fcp->portdb[dbidx].state);
   3693 			continue;
   3694 		}
   3695 
   3696 		/*
   3697 		 * We found a zombie entry that matches us.
   3698 		 * Revive it. We know that WWN and WWPN
   3699 		 * are the same. For fabric devices, we
   3700 		 * don't care that handle is different
   3701 		 * as we assign that. If role or portid
   3702 		 * are different, it maybe a changed device.
   3703 		 */
   3704 		lp = &fcp->portdb[dbidx];
   3705 		lp->handle = handle;
   3706 		lp->new_portid = portid;
   3707 		lp->new_roles = nr;
   3708 		if (lp->portid != portid || lp->roles != nr) {
   3709 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
   3710 			    "Zombie Fabric Port 0x%06x Now Changed", portid);
   3711 			lp->state = FC_PORTDB_STATE_CHANGED;
   3712 		} else {
   3713 			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
   3714 			    "Zombie Fabric Port 0x%06x Now Pending Valid",
   3715 			    portid);
   3716 			lp->state = FC_PORTDB_STATE_PENDING_VALID;
   3717 		}
   3718 	}
   3719 
   3720 	FC_SCRATCH_RELEASE(isp);
   3721 	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
   3722 		ISP_MARK_PORTDB(isp, 1);
   3723 		return (-1);
   3724 	}
   3725 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
   3726 	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "FC Scan Fabric Done");
   3727 	return (0);
   3728 }
   3729 
   3730 /*
   3731  * Find an unused handle and try and use to login to a port.
   3732  */
   3733 static int
   3734 isp_login_device(ispsoftc_t *isp, uint32_t portid, isp_pdb_t *p, uint16_t *ohp)
   3735 {
   3736 	int lim, i, r;
   3737 	uint16_t handle;
   3738 
   3739 	if (FCPARAM(isp)->isp_2klogin) {
   3740 		lim = NPH_MAX_2K;
   3741 	} else {
   3742 		lim = NPH_MAX;
   3743 	}
   3744 
   3745 	handle = isp_nxt_handle(isp, *ohp);
   3746 	for (i = 0; i < lim; i++) {
   3747 		/*
   3748 		 * See if we're still logged into something with
   3749 		 * this handle and that something agrees with this
   3750 		 * port id.
   3751 		 */
   3752 		r = isp_getpdb(isp, handle, p, 0);
   3753 		if (r == 0 && p->portid != portid) {
   3754 			(void) isp_plogx(isp, handle, portid,
   3755 			    PLOGX_FLG_CMD_LOGO | PLOGX_FLG_IMPLICIT, 1);
   3756 		} else if (r == 0) {
   3757 			break;
   3758 		}
   3759 		if (FCPARAM(isp)->isp_loopstate != LOOP_SCANNING_FABRIC) {
   3760 			return (-1);
   3761 		}
   3762 		/*
   3763 		 * Now try and log into the device
   3764 		 */
   3765 		r = isp_plogx(isp, handle, portid, PLOGX_FLG_CMD_PLOGI, 1);
   3766 		if (FCPARAM(isp)->isp_loopstate != LOOP_SCANNING_FABRIC) {
   3767 			return (-1);
   3768 		}
   3769 		if (r == 0) {
   3770 			*ohp = handle;
   3771 			break;
   3772 		} else if ((r & 0xffff) == MBOX_PORT_ID_USED) {
   3773 			handle = r >> 16;
   3774 			break;
   3775 		} else if (r != MBOX_LOOP_ID_USED) {
   3776 			i = lim;
   3777 			break;
   3778 		} else {
   3779 			*ohp = handle;
   3780 			handle = isp_nxt_handle(isp, *ohp);
   3781 		}
   3782 	}
   3783 
   3784 	if (i == lim) {
   3785 		isp_prt(isp, ISP_LOGWARN, "PLOGI 0x%06x failed", portid);
   3786 		return (-1);
   3787 	}
   3788 
   3789 	/*
   3790 	 * If we successfully logged into it, get the PDB for it
   3791 	 * so we can crosscheck that it is still what we think it
   3792 	 * is and that we also have the role it plays
   3793 	 */
   3794 	r = isp_getpdb(isp, handle, p, 0);
   3795 	if (FCPARAM(isp)->isp_loopstate != LOOP_SCANNING_FABRIC) {
   3796 		return (-1);
   3797 	}
   3798 	if (r != 0) {
   3799 		isp_prt(isp, ISP_LOGERR, "new device 0x%06x@0x%x disappeared",
   3800 		    portid, handle);
   3801 		return (-1);
   3802 	}
   3803 
   3804 	if (p->handle != handle || p->portid != portid) {
   3805 		isp_prt(isp, ISP_LOGERR,
   3806 		    "new device 0x%06x@0x%x changed (0x%06x@0x%0x)",
   3807 		    portid, handle, p->portid, p->handle);
   3808 		return (-1);
   3809 	}
   3810 	return (0);
   3811 }
   3812 
   3813 static int
   3814 isp_register_fc4_type(ispsoftc_t *isp)
   3815 {
   3816 	fcparam *fcp = isp->isp_param;
   3817 	uint8_t local[SNS_RFT_ID_REQ_SIZE];
   3818 	sns_screq_t *reqp = (sns_screq_t *) local;
   3819 	mbreg_t mbs;
   3820 
   3821 	MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
   3822 	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
   3823 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
   3824 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
   3825 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
   3826 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
   3827 	reqp->snscb_sblen = 22;
   3828 	reqp->snscb_data[0] = SNS_RFT_ID;
   3829 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
   3830 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
   3831 	reqp->snscb_data[6] = (1 << FC4_SCSI);
   3832 	FC_SCRATCH_ACQUIRE(isp);
   3833 	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
   3834 	MEMZERO(&mbs, sizeof (mbs));
   3835 	mbs.param[0] = MBOX_SEND_SNS;
   3836 	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
   3837 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
   3838 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
   3839 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
   3840 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
   3841 	mbs.logval = MBLOGALL;
   3842 	mbs.timeout = 10000000;
   3843 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_RFT_ID_REQ_SIZE);
   3844 	isp_mboxcmd(isp, &mbs);
   3845 	FC_SCRATCH_RELEASE(isp);
   3846 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   3847 		return (0);
   3848 	} else {
   3849 		return (-1);
   3850 	}
   3851 }
   3852 
   3853 static int
   3854 isp_register_fc4_type_24xx(ispsoftc_t *isp)
   3855 {
   3856 	mbreg_t mbs;
   3857 	fcparam *fcp = FCPARAM(isp);
   3858 	union {
   3859 		isp_ct_pt_t plocal;
   3860 		rft_id_t clocal;
   3861 		uint8_t q[QENTRY_LEN];
   3862 	} un;
   3863 	isp_ct_pt_t *pt;
   3864 	ct_hdr_t *ct;
   3865 	rft_id_t *rp;
   3866 	uint8_t *scp = fcp->isp_scratch;
   3867 
   3868 	FC_SCRATCH_ACQUIRE(isp);
   3869 	/*
   3870 	 * Build a Passthrough IOCB in memory.
   3871 	 */
   3872 	MEMZERO(un.q, QENTRY_LEN);
   3873 	pt = &un.plocal;
   3874 	pt->ctp_header.rqs_entry_count = 1;
   3875 	pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
   3876 	pt->ctp_handle = 0xffffffff;
   3877 	pt->ctp_nphdl = NPH_SNS_ID;
   3878 	pt->ctp_cmd_cnt = 1;
   3879 	pt->ctp_time = 1;
   3880 	pt->ctp_rsp_cnt = 1;
   3881 	pt->ctp_rsp_bcnt = sizeof (ct_hdr_t);
   3882 	pt->ctp_cmd_bcnt = sizeof (rft_id_t);
   3883 	pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
   3884 	pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
   3885 	pt->ctp_dataseg[0].ds_count = sizeof (rft_id_t);
   3886 	pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
   3887 	pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
   3888 	pt->ctp_dataseg[1].ds_count = sizeof (ct_hdr_t);
   3889 	isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
   3890 
   3891 	/*
   3892 	 * Build the CT header and command in memory.
   3893 	 *
   3894 	 * Note that the CT header has to end up as Big Endian format in memory.
   3895 	 */
   3896 	MEMZERO(&un.clocal, sizeof (un.clocal));
   3897 	ct = &un.clocal.rftid_hdr;
   3898 	ct->ct_revision = CT_REVISION;
   3899 	ct->ct_fcs_type = CT_FC_TYPE_FC;
   3900 	ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
   3901 	ct->ct_cmd_resp = SNS_RFT_ID;
   3902 	ct->ct_bcnt_resid = (sizeof (rft_id_t) - sizeof (ct_hdr_t)) >> 2;
   3903 	rp = &un.clocal;
   3904 	rp->rftid_portid[0] = fcp->isp_portid >> 16;
   3905 	rp->rftid_portid[1] = fcp->isp_portid >> 8;
   3906 	rp->rftid_portid[2] = fcp->isp_portid;
   3907 	rp->rftid_fc4types[FC4_SCSI >> 5] = 1 << (FC4_SCSI & 0x1f);
   3908 	isp_put_rft_id(isp, rp, (rft_id_t *) &scp[XTXOFF]);
   3909 
   3910 	MEMZERO(&scp[ZTXOFF], sizeof (ct_hdr_t));
   3911 
   3912 	MEMZERO(&mbs, sizeof (mbs));
   3913 	mbs.param[0] = MBOX_EXEC_COMMAND_IOCB_A64;
   3914 	mbs.param[1] = QENTRY_LEN;
   3915 	mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
   3916 	mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
   3917 	mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
   3918 	mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
   3919 	mbs.timeout = 500000;
   3920 	mbs.logval = MBLOGALL;
   3921 	MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN);
   3922 	isp_mboxcmd(isp, &mbs);
   3923 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3924 		FC_SCRATCH_RELEASE(isp);
   3925 		return (-1);
   3926 	}
   3927 	MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN);
   3928 	pt = &un.plocal;
   3929 	isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
   3930 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
   3931 		isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
   3932 	}
   3933 	if (pt->ctp_status) {
   3934 		FC_SCRATCH_RELEASE(isp);
   3935 		isp_prt(isp, ISP_LOGWARN, "CT Passthrough returned 0x%x",
   3936 		    pt->ctp_status);
   3937 		return (-1);
   3938 	}
   3939 
   3940 	isp_get_ct_hdr(isp, (ct_hdr_t *) &scp[IGPOFF], ct);
   3941 	FC_SCRATCH_RELEASE(isp);
   3942 
   3943 	if (ct->ct_cmd_resp == LS_RJT) {
   3944 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
   3945 		    "Register FC4 Type rejected");
   3946 		return (-1);
   3947 	} else if (ct->ct_cmd_resp == LS_ACC) {
   3948 		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
   3949 		    "Register FC4 Type accepted");
   3950 		return(0);
   3951 	} else {
   3952 		isp_prt(isp, ISP_LOGWARN,
   3953 		    "Register FC4 Type: 0x%x", ct->ct_cmd_resp);
   3954 		return (-1);
   3955 	}
   3956 }
   3957 
   3958 static uint16_t
   3959 isp_nxt_handle(ispsoftc_t *isp, uint16_t handle)
   3960 {
   3961 	int i;
   3962 	if (handle == NIL_HANDLE) {
   3963 		if (FCPARAM(isp)->isp_topo == TOPO_F_PORT) {
   3964 			handle = 0;
   3965 		} else {
   3966 			handle = SNS_ID+1;
   3967 		}
   3968 	} else {
   3969 		handle += 1;
   3970 		if (handle >= FL_ID && handle <= SNS_ID) {
   3971 			handle = SNS_ID+1;
   3972 		}
   3973 		if (handle >= NPH_RESERVED && handle <= NPH_FL_ID) {
   3974 			handle = NPH_FL_ID+1;
   3975 		}
   3976 		if (FCPARAM(isp)->isp_2klogin) {
   3977 			if (handle == NPH_MAX_2K) {
   3978 				handle = 0;
   3979 			}
   3980 		} else {
   3981 			if (handle == NPH_MAX) {
   3982 				handle = 0;
   3983 			}
   3984 		}
   3985 	}
   3986 	if (handle == FCPARAM(isp)->isp_loopid) {
   3987 		return (isp_nxt_handle(isp, handle));
   3988 	}
   3989 	for (i = 0; i < MAX_FC_TARG; i++) {
   3990 		if (FCPARAM(isp)->portdb[i].state == FC_PORTDB_STATE_NIL) {
   3991 			continue;
   3992 		}
   3993 		if (FCPARAM(isp)->portdb[i].handle == handle) {
   3994 			return (isp_nxt_handle(isp, handle));
   3995 		}
   3996 	}
   3997 	return (handle);
   3998 }
   3999 
   4000 /*
   4001  * Start a command. Locking is assumed done in the caller.
   4002  */
   4003 
   4004 int
   4005 isp_start(XS_T *xs)
   4006 {
   4007 	ispsoftc_t *isp;
   4008 	uint32_t nxti, optr, handle;
   4009 	uint8_t local[QENTRY_LEN];
   4010 	ispreq_t *reqp, *qep;
   4011 	void *cdbp;
   4012 	uint16_t *tptr;
   4013 	int target, i, hdlidx = 0;
   4014 
   4015 	XS_INITERR(xs);
   4016 	isp = XS_ISP(xs);
   4017 
   4018 	/*
   4019 	 * Check to make sure we're supporting initiator role.
   4020 	 */
   4021 	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
   4022 		XS_SETERR(xs, HBA_SELTIMEOUT);
   4023 		return (CMD_COMPLETE);
   4024 	}
   4025 
   4026 	/*
   4027 	 * Now make sure we're running.
   4028 	 */
   4029 
   4030 	if (isp->isp_state != ISP_RUNSTATE) {
   4031 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
   4032 		XS_SETERR(xs, HBA_BOTCH);
   4033 		return (CMD_COMPLETE);
   4034 	}
   4035 
   4036 	/*
   4037 	 * Check command CDB length, etc.. We really are limited to 16 bytes
   4038 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
   4039 	 * but probably only if we're running fairly new firmware (we'll
   4040 	 * let the old f/w choke on an extended command queue entry).
   4041 	 */
   4042 
   4043 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
   4044 		isp_prt(isp, ISP_LOGERR,
   4045 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
   4046 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
   4047 		XS_SETERR(xs, HBA_BOTCH);
   4048 		return (CMD_COMPLETE);
   4049 	}
   4050 
   4051 	/*
   4052 	 * Translate the target to device handle as appropriate, checking
   4053 	 * for correct device state as well.
   4054 	 */
   4055 	target = XS_TGT(xs);
   4056 	if (IS_FC(isp)) {
   4057 		fcparam *fcp = isp->isp_param;
   4058 
   4059 		/*
   4060 		 * Try again later.
   4061 		 */
   4062 		if (fcp->isp_fwstate != FW_READY ||
   4063 		    fcp->isp_loopstate != LOOP_READY) {
   4064 			return (CMD_RQLATER);
   4065 		}
   4066 
   4067 		if (XS_TGT(xs) >= MAX_FC_TARG) {
   4068 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4069 			return (CMD_COMPLETE);
   4070 		}
   4071 
   4072 		hdlidx = fcp->isp_ini_map[XS_TGT(xs)] - 1;
   4073 		isp_prt(isp, ISP_LOGDEBUG1, "XS_TGT(xs)=%d- hdlidx value %d",
   4074 		    XS_TGT(xs), hdlidx);
   4075 		if (hdlidx < 0 || hdlidx >= MAX_FC_TARG) {
   4076 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4077 			return (CMD_COMPLETE);
   4078 		}
   4079 		if (fcp->portdb[hdlidx].state == FC_PORTDB_STATE_ZOMBIE) {
   4080 			return (CMD_RQLATER);
   4081 		}
   4082 		if (fcp->portdb[hdlidx].state != FC_PORTDB_STATE_VALID) {
   4083 			XS_SETERR(xs, HBA_SELTIMEOUT);
   4084 			return (CMD_COMPLETE);
   4085 		}
   4086 		target = fcp->portdb[hdlidx].handle;
   4087 	}
   4088 
   4089 	/*
   4090 	 * Next check to see if any HBA or Device parameters need to be updated.
   4091 	 */
   4092 	if (isp->isp_update != 0) {
   4093 		isp_update(isp);
   4094 	}
   4095 
   4096  start_again:
   4097 
   4098 	if (isp_getrqentry(isp, &nxti, &optr, (void *)&qep)) {
   4099 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
   4100 		XS_SETERR(xs, HBA_BOTCH);
   4101 		return (CMD_EAGAIN);
   4102 	}
   4103 
   4104 	/*
   4105 	 * Now see if we need to synchronize the ISP with respect to anything.
   4106 	 * We do dual duty here (cough) for synchronizing for busses other
   4107 	 * than which we got here to send a command to.
   4108 	 */
   4109 	reqp = (ispreq_t *) local;
   4110 	if (isp->isp_sendmarker) {
   4111 		if (IS_24XX(isp)) {
   4112 			isp_marker_24xx_t *m = (isp_marker_24xx_t *) qep;
   4113 			MEMZERO(m, QENTRY_LEN);
   4114 			m->mrk_header.rqs_entry_count = 1;
   4115 			m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
   4116 			m->mrk_modifier = SYNC_ALL;
   4117 			isp_put_marker_24xx(isp, m, (isp_marker_24xx_t *)qep);
   4118 			ISP_ADD_REQUEST(isp, nxti);
   4119 			isp->isp_sendmarker = 0;
   4120 			goto start_again;
   4121 		} else {
   4122 			for (i = 0; i < (IS_DUALBUS(isp)? 2: 1); i++) {
   4123 				isp_marker_t *m = (isp_marker_t *) qep;
   4124 				if ((isp->isp_sendmarker & (1 << i)) == 0) {
   4125 					continue;
   4126 				}
   4127 				MEMZERO(m, QENTRY_LEN);
   4128 				m->mrk_header.rqs_entry_count = 1;
   4129 				m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
   4130 				m->mrk_target = (i << 7);	/* bus # */
   4131 				m->mrk_modifier = SYNC_ALL;
   4132 				isp_put_marker(isp, m, (isp_marker_t *) qep);
   4133 				ISP_ADD_REQUEST(isp, nxti);
   4134 				isp->isp_sendmarker &= ~(1 << i);
   4135 				goto start_again;
   4136 			}
   4137 		}
   4138 	}
   4139 
   4140 	MEMZERO((void *)reqp, QENTRY_LEN);
   4141 	reqp->req_header.rqs_entry_count = 1;
   4142 	if (IS_24XX(isp)) {
   4143 		reqp->req_header.rqs_entry_type = RQSTYPE_T7RQS;
   4144 	} else if (IS_FC(isp)) {
   4145 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
   4146 	} else {
   4147 		if (XS_CDBLEN(xs) > 12)
   4148 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
   4149 		else
   4150 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
   4151 	}
   4152 	/* reqp->req_header.rqs_flags = 0; */
   4153 	/* reqp->req_header.rqs_seqno = 0; */
   4154 	if (IS_24XX(isp)) {
   4155 		int ttype;
   4156 		if (XS_TAG_P(xs)) {
   4157 			ttype = XS_TAG_TYPE(xs);
   4158 		} else {
   4159 			if (XS_CDBP(xs)[0] == 0x3) {
   4160 				ttype = REQFLAG_HTAG;
   4161 			} else {
   4162 				ttype = REQFLAG_STAG;
   4163 			}
   4164 		}
   4165 		if (ttype == REQFLAG_OTAG) {
   4166 			ttype = FCP_CMND_TASK_ATTR_ORDERED;
   4167 		} else if (ttype == REQFLAG_HTAG) {
   4168 			ttype = FCP_CMND_TASK_ATTR_HEAD;
   4169 		} else {
   4170 			ttype = FCP_CMND_TASK_ATTR_SIMPLE;
   4171 		}
   4172 		((ispreqt7_t *)reqp)->req_task_attribute = ttype;
   4173 	} else if (IS_FC(isp)) {
   4174 		/*
   4175 		 * See comment in isp_intr
   4176 		 */
   4177 		/* XS_RESID(xs) = 0; */
   4178 
   4179 		/*
   4180 		 * Fibre Channel always requires some kind of tag.
   4181 		 * The Qlogic drivers seem be happy not to use a tag,
   4182 		 * but this breaks for some devices (IBM drives).
   4183 		 */
   4184 		if (XS_TAG_P(xs)) {
   4185 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
   4186 		} else {
   4187 			/*
   4188 			 * If we don't know what tag to use, use HEAD OF QUEUE
   4189 			 * for Request Sense or Simple.
   4190 			 */
   4191 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
   4192 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
   4193 			else
   4194 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
   4195 		}
   4196 	} else {
   4197 		sdparam *sdp = (sdparam *)isp->isp_param;
   4198 		sdp += XS_CHANNEL(xs);
   4199 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
   4200 		    XS_TAG_P(xs)) {
   4201 			reqp->req_flags = XS_TAG_TYPE(xs);
   4202 		}
   4203 	}
   4204 	cdbp = reqp->req_cdb;
   4205 	tptr = &reqp->req_time;
   4206 
   4207 	if (IS_SCSI(isp)) {
   4208 		reqp->req_target = target | (XS_CHANNEL(xs) << 7);
   4209 		reqp->req_lun_trn = XS_LUN(xs);
   4210 		reqp->req_cdblen = XS_CDBLEN(xs);
   4211 	} else if (IS_24XX(isp)) {
   4212 		fcportdb_t *lp;
   4213 
   4214 		lp = &FCPARAM(isp)->portdb[hdlidx];
   4215 		((ispreqt7_t *)reqp)->req_nphdl = target;
   4216 		((ispreqt7_t *)reqp)->req_tidlo = lp->portid;
   4217 		((ispreqt7_t *)reqp)->req_tidhi = lp->portid >> 16;
   4218 		if (XS_LUN(xs) > 256) {
   4219 			((ispreqt7_t *)reqp)->req_lun[0] = XS_LUN(xs) >> 8;
   4220 			((ispreqt7_t *)reqp)->req_lun[0] |= 0x40;
   4221 		}
   4222 		((ispreqt7_t *)reqp)->req_lun[1] = XS_LUN(xs);
   4223 		cdbp = ((ispreqt7_t *)reqp)->req_cdb;
   4224 		tptr = &((ispreqt7_t *)reqp)->req_time;
   4225 	} else if (FCPARAM(isp)->isp_2klogin) {
   4226 		((ispreqt2e_t *)reqp)->req_target = target;
   4227 		((ispreqt2e_t *)reqp)->req_scclun = XS_LUN(xs);
   4228 	} else if (FCPARAM(isp)->isp_sccfw) {
   4229 		((ispreqt2_t *)reqp)->req_target = target;
   4230 		((ispreqt2_t *)reqp)->req_scclun = XS_LUN(xs);
   4231 	} else {
   4232 		((ispreqt2_t *)reqp)->req_target = target;
   4233 		((ispreqt2_t *)reqp)->req_lun_trn = XS_LUN(xs);
   4234 	}
   4235 	MEMCPY(cdbp, XS_CDBP(xs), XS_CDBLEN(xs));
   4236 
   4237 	*tptr = XS_TIME(xs) / 1000;
   4238 	if (*tptr == 0 && XS_TIME(xs)) {
   4239 		*tptr = 1;
   4240 	}
   4241 	if (IS_24XX(isp) && *tptr > 0x1999) {
   4242 		*tptr = 0x1999;
   4243 	}
   4244 
   4245 	if (isp_save_xs(isp, xs, &handle)) {
   4246 		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
   4247 		XS_SETERR(xs, HBA_BOTCH);
   4248 		return (CMD_EAGAIN);
   4249 	}
   4250 	/* Whew. Thankfully the same for type 7 requests */
   4251 	reqp->req_handle = handle;
   4252 
   4253 	/*
   4254 	 * Set up DMA and/or do any bus swizzling of the request entry
   4255 	 * so that the Qlogic F/W understands what is being asked of it.
   4256 	 */
   4257 	i = ISP_DMASETUP(isp, xs, reqp, &nxti, optr);
   4258 	if (i != CMD_QUEUED) {
   4259 		isp_destroy_handle(isp, handle);
   4260 		/*
   4261 		 * dmasetup sets actual error in packet, and
   4262 		 * return what we were given to return.
   4263 		 */
   4264 		return (i);
   4265 	}
   4266 	XS_SETERR(xs, HBA_NOERROR);
   4267 	isp_prt(isp, ISP_LOGDEBUG2,
   4268 	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
   4269 	    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), XS_CDBP(xs)[0],
   4270 	    (long) XS_XFRLEN(xs));
   4271 	ISP_ADD_REQUEST(isp, nxti);
   4272 	isp->isp_nactive++;
   4273 	return (CMD_QUEUED);
   4274 }
   4275 
   4276 /*
   4277  * isp control
   4278  * Locks (ints blocked) assumed held.
   4279  */
   4280 
   4281 int
   4282 isp_control(ispsoftc_t *isp, ispctl_t ctl, void *arg)
   4283 {
   4284 	XS_T *xs;
   4285 	mbreg_t mbs;
   4286 	int bus, tgt;
   4287 	uint32_t handle;
   4288 
   4289 	MEMZERO(&mbs, sizeof (mbs));
   4290 
   4291 	switch (ctl) {
   4292 	default:
   4293 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
   4294 		break;
   4295 
   4296 	case ISPCTL_RESET_BUS:
   4297 		/*
   4298 		 * Issue a bus reset.
   4299 		 */
   4300 		if (IS_24XX(isp)) {
   4301 			isp_prt(isp, ISP_LOGWARN, "RESET BUS NOT IMPLEMENTED");
   4302 			break;
   4303 		} else if (IS_FC(isp)) {
   4304 			mbs.param[1] = 10;
   4305 			bus = 0;
   4306 		} else {
   4307 			mbs.param[1] = SDPARAM(isp)->isp_bus_reset_delay;
   4308 			if (mbs.param[1] < 2) {
   4309 				mbs.param[1] = 2;
   4310 			}
   4311 			bus = *((int *) arg);
   4312 			if (IS_DUALBUS(isp)) {
   4313 				mbs.param[2] = bus;
   4314 			}
   4315 		}
   4316 		mbs.param[0] = MBOX_BUS_RESET;
   4317 		isp->isp_sendmarker |= (1 << bus);
   4318 		mbs.logval = MBLOGALL;
   4319 		isp_mboxcmd(isp, &mbs);
   4320 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4321 			break;
   4322 		}
   4323 		isp_prt(isp, ISP_LOGINFO,
   4324 		    "driver initiated bus reset of bus %d", bus);
   4325 		return (0);
   4326 
   4327 	case ISPCTL_RESET_DEV:
   4328 		tgt = (*((int *) arg)) & 0xffff;
   4329 		if (IS_24XX(isp)) {
   4330 			isp_prt(isp, ISP_LOGWARN, "RESET DEV NOT IMPLEMENTED");
   4331 			break;
   4332 		} else if (IS_FC(isp)) {
   4333 			if (FCPARAM(isp)->isp_2klogin) {
   4334 				mbs.param[1] = tgt;
   4335 				mbs.ibits = (1 << 10);
   4336 			} else {
   4337 				mbs.param[1] = (tgt << 8);
   4338 			}
   4339 			bus = 0;
   4340 		} else {
   4341 			bus = (*((int *) arg)) >> 16;
   4342 			mbs.param[1] = (bus << 15) | (tgt << 8);
   4343 		}
   4344 		mbs.param[0] = MBOX_ABORT_TARGET;
   4345 		mbs.param[2] = 3;	/* 'delay', in seconds */
   4346 		mbs.logval = MBLOGALL;
   4347 		isp_mboxcmd(isp, &mbs);
   4348 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4349 			break;
   4350 		}
   4351 		isp_prt(isp, ISP_LOGINFO,
   4352 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
   4353 		isp->isp_sendmarker |= (1 << bus);
   4354 		return (0);
   4355 
   4356 	case ISPCTL_ABORT_CMD:
   4357 		xs = (XS_T *) arg;
   4358 		tgt = XS_TGT(xs);
   4359 
   4360 		handle = isp_find_handle(isp, xs);
   4361 		if (handle == 0) {
   4362 			isp_prt(isp, ISP_LOGWARN,
   4363 			    "cannot find handle for command to abort");
   4364 			break;
   4365 		}
   4366 		if (IS_24XX(isp)) {
   4367 			isp_prt(isp, ISP_LOGWARN, "ABORT CMD NOT IMPLEMENTED");
   4368 			break;
   4369 		} else if (IS_FC(isp)) {
   4370 			if (FCPARAM(isp)->isp_sccfw) {
   4371 				if (FCPARAM(isp)->isp_2klogin) {
   4372 					mbs.param[1] = tgt;
   4373 				} else {
   4374 					mbs.param[1] = tgt << 8;
   4375 				}
   4376 				mbs.param[6] = XS_LUN(xs);
   4377 			} else {
   4378 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
   4379 			}
   4380 		} else {
   4381 			bus = XS_CHANNEL(xs);
   4382 			mbs.param[1] = (bus << 15) | (tgt << 8) | XS_LUN(xs);
   4383 		}
   4384 		mbs.param[0] = MBOX_ABORT;
   4385 		mbs.param[2] = handle;
   4386 		mbs.logval = MBLOGALL & ~MBOX_COMMAND_ERROR;
   4387 		isp_mboxcmd(isp, &mbs);
   4388 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4389 			break;
   4390 		}
   4391 		return (0);
   4392 
   4393 	case ISPCTL_UPDATE_PARAMS:
   4394 
   4395 		isp_update(isp);
   4396 		return (0);
   4397 
   4398 	case ISPCTL_FCLINK_TEST:
   4399 
   4400 		if (IS_FC(isp)) {
   4401 			int usdelay = *((int *) arg);
   4402 			if (usdelay == 0) {
   4403 				usdelay =  250000;
   4404 			}
   4405 			return (isp_fclink_test(isp, usdelay));
   4406 		}
   4407 		break;
   4408 
   4409 	case ISPCTL_SCAN_FABRIC:
   4410 
   4411 		if (IS_FC(isp)) {
   4412 			return (isp_scan_fabric(isp));
   4413 		}
   4414 		break;
   4415 
   4416 	case ISPCTL_SCAN_LOOP:
   4417 
   4418 		if (IS_FC(isp)) {
   4419 			return (isp_scan_loop(isp));
   4420 		}
   4421 		break;
   4422 
   4423 	case ISPCTL_PDB_SYNC:
   4424 
   4425 		if (IS_FC(isp)) {
   4426 			return (isp_pdb_sync(isp));
   4427 		}
   4428 		break;
   4429 
   4430 	case ISPCTL_SEND_LIP:
   4431 
   4432 		if (IS_FC(isp) && !IS_24XX(isp)) {
   4433 			mbs.param[0] = MBOX_INIT_LIP;
   4434 			if (FCPARAM(isp)->isp_2klogin) {
   4435 				mbs.ibits = (1 << 10);
   4436 			}
   4437 			mbs.logval = MBLOGALL;
   4438 			isp_mboxcmd(isp, &mbs);
   4439 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   4440 				return (0);
   4441 			}
   4442 		}
   4443 		break;
   4444 
   4445 	case ISPCTL_GET_PDB:
   4446 		if (IS_FC(isp) && arg) {
   4447 			int id = *((int *)arg);
   4448 			isp_pdb_t *pdb = arg;
   4449 			return (isp_getpdb(isp, id, pdb, 1));
   4450 		}
   4451 		break;
   4452 
   4453 	case ISPCTL_GET_PORTNAME:
   4454 	{
   4455 		uint64_t *wwnp = arg;
   4456 		int loopid = *wwnp;
   4457 		*wwnp = isp_get_portname(isp, loopid, 0);
   4458 		if (*wwnp == (uint64_t) -1) {
   4459 			break;
   4460 		} else {
   4461 			return (0);
   4462 		}
   4463 	}
   4464 	case ISPCTL_RUN_MBOXCMD:
   4465 
   4466 		isp_mboxcmd(isp, arg);
   4467 		return(0);
   4468 
   4469 	case ISPCTL_PLOGX:
   4470 	{
   4471 		isp_plcmd_t *p = arg;
   4472 		int r;
   4473 
   4474 		if ((p->flags & PLOGX_FLG_CMD_MASK) != PLOGX_FLG_CMD_PLOGI ||
   4475 		    (p->handle != NIL_HANDLE)) {
   4476 			return (isp_plogx(isp, p->handle, p->portid,
   4477 			    p->flags, 0));
   4478 		}
   4479 		do {
   4480 			p->handle = isp_nxt_handle(isp, p->handle);
   4481 			r = isp_plogx(isp, p->handle, p->portid, p->flags, 0);
   4482 			if ((r & 0xffff) == MBOX_PORT_ID_USED) {
   4483 				p->handle = r >> 16;
   4484 				r = 0;
   4485 				break;
   4486 			}
   4487 		} while ((r & 0xffff) == MBOX_LOOP_ID_USED);
   4488 		return (r);
   4489 	}
   4490 #ifdef	ISP_TARGET_MODE
   4491 	case ISPCTL_TOGGLE_TMODE:
   4492 	{
   4493 
   4494 		/*
   4495 		 * We don't check/set against role here- that's the
   4496 		 * responsibility for the outer layer to coordinate.
   4497 		 */
   4498 		if (IS_SCSI(isp)) {
   4499 			int param = *(int *)arg;
   4500 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
   4501 			mbs.param[1] = param & 0xffff;
   4502 			mbs.param[2] = param >> 16;
   4503 			mbs.logval = MBLOGALL;
   4504 			isp_mboxcmd(isp, &mbs);
   4505 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4506 				break;
   4507 			}
   4508 		}
   4509 		return (0);
   4510 	}
   4511 #endif
   4512 	}
   4513 	return (-1);
   4514 }
   4515 
   4516 /*
   4517  * Interrupt Service Routine(s).
   4518  *
   4519  * External (OS) framework has done the appropriate locking,
   4520  * and the locking will be held throughout this function.
   4521  */
   4522 
   4523 /*
   4524  * Limit our stack depth by sticking with the max likely number
   4525  * of completions on a request queue at any one time.
   4526  */
   4527 #ifndef	MAX_REQUESTQ_COMPLETIONS
   4528 #define	MAX_REQUESTQ_COMPLETIONS	32
   4529 #endif
   4530 
   4531 void
   4532 isp_intr(ispsoftc_t *isp, uint32_t isr, uint16_t sema, uint16_t mbox)
   4533 {
   4534 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
   4535 	uint32_t iptr, optr, junk;
   4536 	int i, nlooked = 0, ndone = 0;
   4537 
   4538 again:
   4539 	optr = isp->isp_residx;
   4540 	/*
   4541 	 * Is this a mailbox related interrupt?
   4542 	 * The mailbox semaphore will be nonzero if so.
   4543 	 */
   4544 	if (sema) {
   4545 		if (mbox & 0x4000) {
   4546 			isp->isp_intmboxc++;
   4547 			if (isp->isp_mboxbsy) {
   4548 				int obits = isp->isp_obits;
   4549 				isp->isp_mboxtmp[0] = mbox;
   4550 				for (i = 1; i < MAX_MAILBOX(isp); i++) {
   4551 					if ((obits & (1 << i)) == 0) {
   4552 						continue;
   4553 					}
   4554 					isp->isp_mboxtmp[i] =
   4555 					    ISP_READ(isp, MBOX_OFF(i));
   4556 				}
   4557 				if (isp->isp_mbxwrk0) {
   4558 					if (isp_mbox_continue(isp) == 0) {
   4559 						return;
   4560 					}
   4561 				}
   4562 				MBOX_NOTIFY_COMPLETE(isp);
   4563 			} else {
   4564 				isp_prt(isp, ISP_LOGWARN,
   4565 				    "mailbox cmd (0x%x) with no waiters", mbox);
   4566 			}
   4567 		} else if (isp_parse_async(isp, mbox) < 0) {
   4568 			return;
   4569 		}
   4570 		if ((IS_FC(isp) && mbox != ASYNC_RIO_RESP) ||
   4571 		    isp->isp_state != ISP_RUNSTATE) {
   4572 			goto out;
   4573 		}
   4574 	}
   4575 
   4576 	/*
   4577 	 * We can't be getting this now.
   4578 	 */
   4579 	if (isp->isp_state != ISP_RUNSTATE) {
   4580 		isp_prt(isp, ISP_LOGINFO,
   4581 		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
   4582 		/*
   4583 		 * Thank you very much!  *Burrrp*!
   4584 		 */
   4585 		ISP_WRITE(isp, isp->isp_respoutrp,
   4586 		    ISP_READ(isp, isp->isp_respinrp));
   4587 		if (IS_24XX(isp)) {
   4588 			ISP_DISABLE_INTS(isp);
   4589 		}
   4590 		goto out;
   4591 	}
   4592 
   4593 #ifdef	ISP_TARGET_MODE
   4594 	/*
   4595 	 * Check for ATIO Queue entries.
   4596 	 */
   4597 	if (isp->isp_rspbsy == 0 && (isp->isp_role & ISP_ROLE_TARGET) &&
   4598 	    IS_24XX(isp)) {
   4599 		iptr = ISP_READ(isp, isp->isp_atioinrp);
   4600 		optr = ISP_READ(isp, isp->isp_atiooutrp);
   4601 
   4602 		isp->isp_rspbsy = 1;
   4603 		while (optr != iptr) {
   4604 			uint8_t qe[QENTRY_LEN];
   4605 			isphdr_t *hp;
   4606 			uint32_t oop;
   4607 			void *addr;
   4608 
   4609 			oop = optr;
   4610 			MEMORYBARRIER(isp, SYNC_ATIOQ, oop, QENTRY_LEN);
   4611 			addr = ISP_QUEUE_ENTRY(isp->isp_atioq, oop);
   4612 			isp_get_hdr(isp, addr, (isphdr_t *)qe);
   4613 			hp = (isphdr_t *)qe;
   4614 			switch (hp->rqs_entry_type) {
   4615 			case RQSTYPE_NOTIFY:
   4616 			case RQSTYPE_ATIO:
   4617 				(void) isp_target_notify(isp, addr, &oop);
   4618 				break;
   4619 			default:
   4620 				isp_print_qentry(isp, "?ATIOQ entry?",
   4621 				    oop, addr);
   4622 				break;
   4623 			}
   4624 			optr = ISP_NXT_QENTRY(oop, RESULT_QUEUE_LEN(isp));
   4625 			ISP_WRITE(isp, isp->isp_atiooutrp, optr);
   4626 		}
   4627 		isp->isp_rspbsy = 0;
   4628 		optr = isp->isp_residx;
   4629 	}
   4630 #endif
   4631 
   4632 	/*
   4633 	 * Get the current Response Queue Out Pointer.
   4634 	 *
   4635 	 * If we're a 2300 or 2400, we can ask what hardware what it thinks.
   4636 	 */
   4637 	if (IS_23XX(isp) || IS_24XX(isp)) {
   4638 		optr = ISP_READ(isp, isp->isp_respoutrp);
   4639 		/*
   4640 		 * Debug: to be taken out eventually
   4641 		 */
   4642 		if (isp->isp_residx != optr) {
   4643 			isp_prt(isp, ISP_LOGINFO,
   4644 			    "isp_intr: hard optr=%x, soft optr %x",
   4645 			    optr, isp->isp_residx);
   4646 			isp->isp_residx = optr;
   4647 		}
   4648 	} else {
   4649 		optr = isp->isp_residx;
   4650 	}
   4651 
   4652 	/*
   4653 	 * You *must* read the Response Queue In Pointer
   4654 	 * prior to clearing the RISC interrupt.
   4655 	 *
   4656 	 * Debounce the 2300 if revision less than 2.
   4657 	 */
   4658 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
   4659 		i = 0;
   4660 		do {
   4661 			iptr = ISP_READ(isp, isp->isp_respinrp);
   4662 			junk = ISP_READ(isp, isp->isp_respinrp);
   4663 		} while (junk != iptr && ++i < 1000);
   4664 
   4665 		if (iptr != junk) {
   4666 			isp_prt(isp, ISP_LOGWARN,
   4667 			    "Response Queue Out Pointer Unstable (%x, %x)",
   4668 			    iptr, junk);
   4669 			goto out;
   4670 		}
   4671 	} else {
   4672 		iptr = ISP_READ(isp, isp->isp_respinrp);
   4673 	}
   4674 	isp->isp_resodx = iptr;
   4675 
   4676 
   4677 	if (optr == iptr && sema == 0) {
   4678 		/*
   4679 		 * There are a lot of these- reasons unknown- mostly on
   4680 		 * faster Alpha machines.
   4681 		 *
   4682 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
   4683 		 * make sure the old interrupt went away (to avoid 'ringing'
   4684 		 * effects), but that didn't stop this from occurring.
   4685 		 */
   4686 		if (IS_24XX(isp)) {
   4687 			junk = 0;
   4688 		} else if (IS_23XX(isp)) {
   4689 			USEC_DELAY(100);
   4690 			iptr = ISP_READ(isp, isp->isp_respinrp);
   4691 			junk = ISP_READ(isp, BIU_R2HSTSLO);
   4692 		} else {
   4693 			junk = ISP_READ(isp, BIU_ISR);
   4694 		}
   4695 		if (optr == iptr) {
   4696 			if (IS_23XX(isp) || IS_24XX(isp)) {
   4697 				;
   4698 			} else {
   4699 				sema = ISP_READ(isp, BIU_SEMA);
   4700 				mbox = ISP_READ(isp, OUTMAILBOX0);
   4701 				if ((sema & 0x3) && (mbox & 0x8000)) {
   4702 					goto again;
   4703 				}
   4704 			}
   4705 			isp->isp_intbogus++;
   4706 			isp_prt(isp, ISP_LOGDEBUG1,
   4707 			    "bogus intr- isr %x (%x) iptr %x optr %x",
   4708 			    isr, junk, iptr, optr);
   4709 		}
   4710 	}
   4711 	isp->isp_resodx = iptr;
   4712 
   4713 
   4714 	if (isp->isp_rspbsy) {
   4715 		goto out;
   4716 	}
   4717 	isp->isp_rspbsy = 1;
   4718 	while (optr != iptr) {
   4719 		uint8_t qe[QENTRY_LEN];
   4720 		ispstatusreq_t *sp = (ispstatusreq_t *) qe;
   4721 		isphdr_t *hp;
   4722 		int buddaboom, etype, scsi_status, completion_status;
   4723 		int req_status_flags, req_state_flags;
   4724 		uint8_t *snsp, *resp;
   4725 		uint32_t rlen, slen;
   4726 		long resid;
   4727 		uint16_t oop;
   4728 
   4729 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   4730 		oop = optr;
   4731 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
   4732 		nlooked++;
   4733  read_again:
   4734 		buddaboom = req_status_flags = req_state_flags = 0;
   4735 		resid = 0L;
   4736 
   4737 		/*
   4738 		 * Synchronize our view of this response queue entry.
   4739 		 */
   4740 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
   4741 		isp_get_hdr(isp, hp, &sp->req_header);
   4742 		etype = sp->req_header.rqs_entry_type;
   4743 
   4744 		if (IS_24XX(isp) && etype == RQSTYPE_T7RQS) {
   4745 			isp24xx_statusreq_t *sp2 = (isp24xx_statusreq_t *)qe;
   4746 			isp_get_24xx_response(isp,
   4747 			    (isp24xx_statusreq_t *)hp, sp2);
   4748 			if (isp->isp_dblev & ISP_LOGDEBUG1) {
   4749 				isp_print_bytes(isp,
   4750 				    "Response Queue Entry", QENTRY_LEN, sp2);
   4751 			}
   4752 			scsi_status = sp2->req_scsi_status;
   4753 			completion_status = sp2->req_completion_status;
   4754 			req_state_flags = 0;
   4755 			resid = sp2->req_resid;
   4756 		} else if (etype == RQSTYPE_RESPONSE) {
   4757 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
   4758 			if (isp->isp_dblev & ISP_LOGDEBUG1) {
   4759 				isp_print_bytes(isp,
   4760 				    "Response Queue Entry", QENTRY_LEN, sp);
   4761 			}
   4762 			scsi_status = sp->req_scsi_status;
   4763 			completion_status = sp->req_completion_status;
   4764 			req_status_flags = sp->req_status_flags;
   4765 			req_state_flags = sp->req_state_flags;
   4766 			resid = sp->req_resid;
   4767 		} else if (etype == RQSTYPE_RIO2) {
   4768 			isp_rio2_t *rio = (isp_rio2_t *)qe;
   4769 			isp_get_rio2(isp, (isp_rio2_t *) hp, rio);
   4770 			if (isp->isp_dblev & ISP_LOGDEBUG1) {
   4771 				isp_print_bytes(isp,
   4772 				    "Response Queue Entry", QENTRY_LEN, rio);
   4773 			}
   4774 			for (i = 0; i < rio->req_header.rqs_seqno; i++) {
   4775 				isp_fastpost_complete(isp, rio->req_handles[i]);
   4776 			}
   4777 			if (isp->isp_fpcchiwater < rio->req_header.rqs_seqno) {
   4778 				isp->isp_fpcchiwater =
   4779 				    rio->req_header.rqs_seqno;
   4780 			}
   4781 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
   4782 			continue;
   4783 		} else {
   4784 			/*
   4785 			 * Somebody reachable via isp_handle_other_response
   4786 			 * may have updated the response queue pointers for
   4787 			 * us, so we reload our goal index.
   4788 			 */
   4789 			int r;
   4790 			r = isp_handle_other_response(isp, etype, hp, &optr);
   4791 			if (r < 0) {
   4792 				goto read_again;
   4793 			}
   4794 			if (r > 0) {
   4795 				iptr = isp->isp_resodx;
   4796 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
   4797 				continue;
   4798 			}
   4799 
   4800 			/*
   4801 			 * After this point, we'll just look at the header as
   4802 			 * we don't know how to deal with the rest of the
   4803 			 * response.
   4804 			 */
   4805 
   4806 			/*
   4807 			 * It really has to be a bounced request just copied
   4808 			 * from the request queue to the response queue. If
   4809 			 * not, something bad has happened.
   4810 			 */
   4811 			if (etype != RQSTYPE_REQUEST) {
   4812 				isp_prt(isp, ISP_LOGERR, notresp,
   4813 				    etype, oop, optr, nlooked);
   4814 				isp_print_bytes(isp,
   4815 				    "Request Queue Entry", QENTRY_LEN, sp);
   4816 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
   4817 				continue;
   4818 			}
   4819 			buddaboom = 1;
   4820 			scsi_status = sp->req_scsi_status;
   4821 			completion_status = sp->req_completion_status;
   4822 			req_status_flags = sp->req_status_flags;
   4823 			req_state_flags = sp->req_state_flags;
   4824 			resid = sp->req_resid;
   4825 		}
   4826 
   4827 		if (sp->req_header.rqs_flags & RQSFLAG_MASK) {
   4828 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   4829 				isp_prt(isp, ISP_LOGWARN,
   4830 				    "continuation segment");
   4831 				ISP_WRITE(isp, isp->isp_respoutrp, optr);
   4832 				continue;
   4833 			}
   4834 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   4835 				isp_prt(isp, ISP_LOGDEBUG1,
   4836 				    "internal queues full");
   4837 				/*
   4838 				 * We'll synthesize a QUEUE FULL message below.
   4839 				 */
   4840 			}
   4841 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   4842 				isp_print_bytes(isp, "bad header flag",
   4843 				    QENTRY_LEN, sp);
   4844 				buddaboom++;
   4845 			}
   4846 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   4847 				isp_print_bytes(isp, "bad request packet",
   4848 				    QENTRY_LEN, sp);
   4849 				buddaboom++;
   4850 			}
   4851 		}
   4852 
   4853 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
   4854 			isp_prt(isp, ISP_LOGERR,
   4855 			    "bad request handle %d (type 0x%x)",
   4856 			    sp->req_handle, etype);
   4857 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
   4858 			ISP_WRITE(isp, isp->isp_respoutrp, optr);
   4859 			continue;
   4860 		}
   4861 		xs = isp_find_xs(isp, sp->req_handle);
   4862 		if (xs == NULL) {
   4863 			uint8_t ts = completion_status & 0xff;
   4864 			/*
   4865 			 * Only whine if this isn't the expected fallout of
   4866 			 * aborting the command.
   4867 			 */
   4868 			if (etype != RQSTYPE_RESPONSE) {
   4869 				isp_prt(isp, ISP_LOGERR,
   4870 				    "cannot find handle 0x%x (type 0x%x)",
   4871 				    sp->req_handle, etype);
   4872 			} else if (ts != RQCS_ABORTED) {
   4873 				isp_prt(isp, ISP_LOGERR,
   4874 				    "cannot find handle 0x%x (status 0x%x)",
   4875 				    sp->req_handle, ts);
   4876 			}
   4877 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
   4878 			ISP_WRITE(isp, isp->isp_respoutrp, optr);
   4879 			continue;
   4880 		}
   4881 		isp_destroy_handle(isp, sp->req_handle);
   4882 		if (req_status_flags & RQSTF_BUS_RESET) {
   4883 			XS_SETERR(xs, HBA_BUSRESET);
   4884 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   4885 		}
   4886 		if (buddaboom) {
   4887 			XS_SETERR(xs, HBA_BOTCH);
   4888 		}
   4889 
   4890 		resp = NULL;
   4891 		rlen = 0;
   4892 		snsp = NULL;
   4893 		slen = 0;
   4894 		if (IS_24XX(isp) && (scsi_status & RQCS_RV) != 0) {
   4895 			resp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
   4896 			rlen = ((isp24xx_statusreq_t *)sp)->req_response_len;
   4897 		} else if (IS_FC(isp) && (scsi_status & RQCS_RV) != 0) {
   4898 			resp = sp->req_response;
   4899 			rlen = sp->req_response_len;
   4900 		}
   4901 		if (IS_FC(isp) && (scsi_status & RQCS_SV) != 0) {
   4902 			/*
   4903 			 * Fibre Channel F/W doesn't say we got status
   4904 			 * if there's Sense Data instead. I guess they
   4905 			 * think it goes w/o saying.
   4906 			 */
   4907 			req_state_flags |= RQSF_GOT_STATUS|RQSF_GOT_SENSE;
   4908 			if (IS_24XX(isp)) {
   4909 				snsp =
   4910 				    ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
   4911 				snsp += rlen;
   4912 				slen =
   4913 				    ((isp24xx_statusreq_t *)sp)->req_sense_len;
   4914 			} else {
   4915 				snsp = sp->req_sense_data;
   4916 				slen = sp->req_sense_len;
   4917 			}
   4918 		} else if (IS_SCSI(isp) && (req_state_flags & RQSF_GOT_SENSE)) {
   4919 			snsp = sp->req_sense_data;
   4920 			slen = sp->req_sense_len;
   4921 		}
   4922 		if (req_state_flags & RQSF_GOT_STATUS) {
   4923 			*XS_STSP(xs) = scsi_status & 0xff;
   4924 		}
   4925 
   4926 		switch (etype) {
   4927 		case RQSTYPE_RESPONSE:
   4928 			XS_SET_STATE_STAT(isp, xs, sp);
   4929 			if (resp && rlen >= 4 &&
   4930 			    resp[FCP_RSPNS_CODE_OFFSET] != 0) {
   4931 				isp_prt(isp, ISP_LOGWARN,
   4932 				    "%d.%d FCP RESPONSE: 0x%x",
   4933 				    XS_TGT(xs), XS_LUN(xs),
   4934 				    resp[FCP_RSPNS_CODE_OFFSET]);
   4935 				XS_SETERR(xs, HBA_BOTCH);
   4936 			}
   4937 			if (IS_24XX(isp)) {
   4938 				isp_parse_status_24xx(isp,
   4939 				    (isp24xx_statusreq_t *)sp, xs, &resid);
   4940 			} else {
   4941 				isp_parse_status(isp, (void *)sp, xs, &resid);
   4942 			}
   4943 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
   4944 			    (*XS_STSP(xs) == SCSI_BUSY)) {
   4945 				XS_SETERR(xs, HBA_TGTBSY);
   4946 			}
   4947 			if (IS_SCSI(isp)) {
   4948 				XS_RESID(xs) = resid;
   4949 				/*
   4950 				 * A new synchronous rate was negotiated for
   4951 				 * this target. Mark state such that we'll go
   4952 				 * look up that which has changed later.
   4953 				 */
   4954 				if (req_status_flags & RQSTF_NEGOTIATION) {
   4955 					int t = XS_TGT(xs);
   4956 					sdparam *sdp = isp->isp_param;
   4957 					sdp += XS_CHANNEL(xs);
   4958 					sdp->isp_devparam[t].dev_refresh = 1;
   4959 					isp->isp_update |=
   4960 					    (1 << XS_CHANNEL(xs));
   4961 				}
   4962 			} else {
   4963 				if (req_status_flags & RQSF_XFER_COMPLETE) {
   4964 					XS_RESID(xs) = 0;
   4965 				} else if (scsi_status & RQCS_RESID) {
   4966 					XS_RESID(xs) = resid;
   4967 				} else {
   4968 					XS_RESID(xs) = 0;
   4969 				}
   4970 			}
   4971 			if (snsp && slen) {
   4972 				XS_SAVE_SENSE(xs, snsp, slen);
   4973 			}
   4974 			isp_prt(isp, ISP_LOGDEBUG2,
   4975 			   "asked for %ld got raw resid %ld settled for %ld",
   4976 			    (long) XS_XFRLEN(xs), resid, (long) XS_RESID(xs));
   4977 			break;
   4978 		case RQSTYPE_REQUEST:
   4979 		case RQSTYPE_A64:
   4980 		case RQSTYPE_T2RQS:
   4981 		case RQSTYPE_T3RQS:
   4982 		case RQSTYPE_T7RQS:
   4983 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   4984 				/*
   4985 				 * Force Queue Full status.
   4986 				 */
   4987 				*XS_STSP(xs) = SCSI_QFULL;
   4988 				XS_SETERR(xs, HBA_NOERROR);
   4989 			} else if (XS_NOERR(xs)) {
   4990 				/*
   4991 				 * ????
   4992 				 */
   4993 				XS_SETERR(xs, HBA_BOTCH);
   4994 				isp_prt(isp, ISP_LOGDEBUG0,
   4995 				    "Request Queue Entry bounced back");
   4996 				if ((isp->isp_dblev & ISP_LOGDEBUG1) == 0) {
   4997 					isp_print_bytes(isp, "Bounced Request",
   4998 					    QENTRY_LEN, qe);
   4999 				}
   5000 			}
   5001 			XS_RESID(xs) = XS_XFRLEN(xs);
   5002 			break;
   5003 		default:
   5004 			isp_print_bytes(isp, "Unhandled Response Type",
   5005 			    QENTRY_LEN, qe);
   5006 			if (XS_NOERR(xs)) {
   5007 				XS_SETERR(xs, HBA_BOTCH);
   5008 			}
   5009 			break;
   5010 		}
   5011 
   5012 		/*
   5013 		 * Free any DMA resources. As a side effect, this may
   5014 		 * also do any cache flushing necessary for data coherence.			 */
   5015 		if (XS_XFRLEN(xs)) {
   5016 			ISP_DMAFREE(isp, xs, sp->req_handle);
   5017 		}
   5018 
   5019 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
   5020 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
   5021 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
   5022 			char skey;
   5023 			if (req_state_flags & RQSF_GOT_SENSE) {
   5024 				skey = XS_SNSKEY(xs) & 0xf;
   5025 				if (skey < 10)
   5026 					skey += '0';
   5027 				else
   5028 					skey += 'a' - 10;
   5029 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
   5030 				skey = '?';
   5031 			} else {
   5032 				skey = '.';
   5033 			}
   5034 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
   5035 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
   5036 			    *XS_STSP(xs), skey, XS_ERR(xs));
   5037 		}
   5038 
   5039 		if (isp->isp_nactive > 0)
   5040 		    isp->isp_nactive--;
   5041 		complist[ndone++] = xs;	/* defer completion call until later */
   5042 		MEMZERO(hp, QENTRY_LEN);	/* PERF */
   5043 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
   5044 			break;
   5045 		}
   5046 	}
   5047 
   5048 	/*
   5049 	 * If we looked at any commands, then it's valid to find out
   5050 	 * what the outpointer is. It also is a trigger to update the
   5051 	 * ISP's notion of what we've seen so far.
   5052 	 */
   5053 	if (nlooked) {
   5054 		ISP_WRITE(isp, isp->isp_respoutrp, optr);
   5055 		/*
   5056 		 * While we're at it, read the requst queue out pointer.
   5057 		 */
   5058 		isp->isp_reqodx = ISP_READ(isp, isp->isp_rqstoutrp);
   5059 		if (isp->isp_rscchiwater < ndone) {
   5060 			isp->isp_rscchiwater = ndone;
   5061 		}
   5062 	}
   5063 
   5064 out:
   5065 
   5066 	if (IS_24XX(isp)) {
   5067 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
   5068 	} else {
   5069 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   5070 		ISP_WRITE(isp, BIU_SEMA, 0);
   5071 	}
   5072 
   5073 	isp->isp_residx = optr;
   5074 	isp->isp_rspbsy = 0;
   5075 	for (i = 0; i < ndone; i++) {
   5076 		xs = complist[i];
   5077 		if (xs) {
   5078 			isp->isp_rsltccmplt++;
   5079 			isp_done(xs);
   5080 		}
   5081 	}
   5082 }
   5083 
   5084 /*
   5085  * Support routines.
   5086  */
   5087 
   5088 static int
   5089 isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
   5090 {
   5091 	int rval = 0;
   5092 	int bus;
   5093 
   5094 	if (IS_DUALBUS(isp)) {
   5095 		bus = ISP_READ(isp, OUTMAILBOX6);
   5096 	} else {
   5097 		bus = 0;
   5098 	}
   5099 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
   5100 
   5101 	switch (mbox) {
   5102 	case ASYNC_BUS_RESET:
   5103 		isp->isp_sendmarker |= (1 << bus);
   5104 #ifdef	ISP_TARGET_MODE
   5105 		if (isp_target_async(isp, bus, mbox)) {
   5106 			rval = -1;
   5107 		}
   5108 #endif
   5109 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
   5110 		break;
   5111 	case ASYNC_SYSTEM_ERROR:
   5112 		isp->isp_state = ISP_CRASHED;
   5113 		if (IS_FC(isp)) {
   5114 			FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   5115 			FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   5116 		}
   5117 		/*
   5118 		 * Were we waiting for a mailbox command to complete?
   5119 		 * If so, it's dead, so wake up the waiter.
   5120 		 */
   5121 		if (isp->isp_mboxbsy) {
   5122 			isp->isp_obits = 1;
   5123 			isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
   5124 			MBOX_NOTIFY_COMPLETE(isp);
   5125 		}
   5126 		/*
   5127 		 * It's up to the handler for isp_async to reinit stuff and
   5128 		 * restart the firmware
   5129 		 */
   5130 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
   5131 		rval = -1;
   5132 		break;
   5133 
   5134 	case ASYNC_RQS_XFER_ERR:
   5135 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
   5136 		break;
   5137 
   5138 	case ASYNC_RSP_XFER_ERR:
   5139 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
   5140 		break;
   5141 
   5142 	case ASYNC_QWAKEUP:
   5143 		/*
   5144 		 * We've just been notified that the Queue has woken up.
   5145 		 * We don't need to be chatty about this- just unlatch things
   5146 		 * and move on.
   5147 		 */
   5148 		mbox = ISP_READ(isp, isp->isp_rqstoutrp);
   5149 		break;
   5150 
   5151 	case ASYNC_TIMEOUT_RESET:
   5152 		isp_prt(isp, ISP_LOGWARN,
   5153 		    "timeout initiated SCSI bus reset of bus %d", bus);
   5154 		isp->isp_sendmarker |= (1 << bus);
   5155 #ifdef	ISP_TARGET_MODE
   5156 		if (isp_target_async(isp, bus, mbox)) {
   5157 			rval = -1;
   5158 		}
   5159 #endif
   5160 		break;
   5161 
   5162 	case ASYNC_DEVICE_RESET:
   5163 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
   5164 		isp->isp_sendmarker |= (1 << bus);
   5165 #ifdef	ISP_TARGET_MODE
   5166 		if (isp_target_async(isp, bus, mbox)) {
   5167 			rval = -1;
   5168 		}
   5169 #endif
   5170 		break;
   5171 
   5172 	case ASYNC_EXTMSG_UNDERRUN:
   5173 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
   5174 		break;
   5175 
   5176 	case ASYNC_SCAM_INT:
   5177 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
   5178 		break;
   5179 
   5180 	case ASYNC_HUNG_SCSI:
   5181 		isp_prt(isp, ISP_LOGERR,
   5182 		    "stalled SCSI Bus after DATA Overrun");
   5183 		/* XXX: Need to issue SCSI reset at this point */
   5184 		break;
   5185 
   5186 	case ASYNC_KILLED_BUS:
   5187 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
   5188 		break;
   5189 
   5190 	case ASYNC_BUS_TRANSIT:
   5191 		mbox = ISP_READ(isp, OUTMAILBOX2);
   5192 		switch (mbox & 0x1c00) {
   5193 		case SXP_PINS_LVD_MODE:
   5194 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
   5195 			SDPARAM(isp)->isp_diffmode = 0;
   5196 			SDPARAM(isp)->isp_ultramode = 0;
   5197 			SDPARAM(isp)->isp_lvdmode = 1;
   5198 			break;
   5199 		case SXP_PINS_HVD_MODE:
   5200 			isp_prt(isp, ISP_LOGINFO,
   5201 			    "Transition to Differential mode");
   5202 			SDPARAM(isp)->isp_diffmode = 1;
   5203 			SDPARAM(isp)->isp_ultramode = 0;
   5204 			SDPARAM(isp)->isp_lvdmode = 0;
   5205 			break;
   5206 		case SXP_PINS_SE_MODE:
   5207 			isp_prt(isp, ISP_LOGINFO,
   5208 			    "Transition to Single Ended mode");
   5209 			SDPARAM(isp)->isp_diffmode = 0;
   5210 			SDPARAM(isp)->isp_ultramode = 1;
   5211 			SDPARAM(isp)->isp_lvdmode = 0;
   5212 			break;
   5213 		default:
   5214 			isp_prt(isp, ISP_LOGWARN,
   5215 			    "Transition to Unknown Mode 0x%x", mbox);
   5216 			break;
   5217 		}
   5218 		/*
   5219 		 * XXX: Set up to renegotiate again!
   5220 		 */
   5221 		/* Can only be for a 1080... */
   5222 		isp->isp_sendmarker |= (1 << bus);
   5223 		break;
   5224 
   5225 	/*
   5226 	 * We can use bus, which will always be zero for FC cards,
   5227 	 * as a mailbox pattern accumulator to be checked below.
   5228 	 */
   5229 	case ASYNC_RIO5:
   5230 		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
   5231 		break;
   5232 
   5233 	case ASYNC_RIO4:
   5234 		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
   5235 		break;
   5236 
   5237 	case ASYNC_RIO3:
   5238 		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
   5239 		break;
   5240 
   5241 	case ASYNC_RIO2:
   5242 		bus = 0x106;	/* outgoing mailbox regs 1-2 */
   5243 		break;
   5244 
   5245 	case ASYNC_RIO1:
   5246 	case ASYNC_CMD_CMPLT:
   5247 		bus = 0x102;	/* outgoing mailbox regs 1 */
   5248 		break;
   5249 
   5250 	case ASYNC_RIO_RESP:
   5251 		return (rval);
   5252 
   5253 	case ASYNC_CTIO_DONE:
   5254 	{
   5255 #ifdef	ISP_TARGET_MODE
   5256 		int handle =
   5257 		    (ISP_READ(isp, OUTMAILBOX2) << 16) |
   5258 		    (ISP_READ(isp, OUTMAILBOX1));
   5259 		if (isp_target_async(isp, handle, mbox)) {
   5260 			rval = -1;
   5261 		} else {
   5262 			/* count it as a fast posting intr */
   5263 			isp->isp_fphccmplt++;
   5264 		}
   5265 #else
   5266 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
   5267 		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
   5268 #endif
   5269 		break;
   5270 	}
   5271 	case ASYNC_LIP_ERROR:
   5272 	case ASYNC_LIP_F8:
   5273 	case ASYNC_LIP_OCCURRED:
   5274 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   5275 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   5276 		isp->isp_sendmarker = 1;
   5277 		ISP_MARK_PORTDB(isp, 1);
   5278 		isp_async(isp, ISPASYNC_LIP, NULL);
   5279 #ifdef	ISP_TARGET_MODE
   5280 		if (isp_target_async(isp, bus, mbox)) {
   5281 			rval = -1;
   5282 		}
   5283 #endif
   5284 		/*
   5285 		 * We've had problems with data corruption occuring on
   5286 		 * commands that complete (with no apparent error) after
   5287 		 * we receive a LIP. This has been observed mostly on
   5288 		 * Local Loop topologies. To be safe, let's just mark
   5289 		 * all active commands as dead.
   5290 		 */
   5291 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
   5292 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
   5293 			int i, j;
   5294 			for (i = j = 0; i < isp->isp_maxcmds; i++) {
   5295 				XS_T *xs;
   5296 				xs = isp->isp_xflist[i];
   5297 				if (xs != NULL) {
   5298 					j++;
   5299 					XS_SETERR(xs, HBA_BUSRESET);
   5300 				}
   5301 			}
   5302 			if (j) {
   5303 				isp_prt(isp, ISP_LOGERR,
   5304 				    "LIP destroyed %d active commands", j);
   5305 			}
   5306 		}
   5307 		break;
   5308 
   5309 	case ASYNC_LOOP_UP:
   5310 		isp->isp_sendmarker = 1;
   5311 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   5312 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   5313 		ISP_MARK_PORTDB(isp, 1);
   5314 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
   5315 #ifdef	ISP_TARGET_MODE
   5316 		if (isp_target_async(isp, bus, mbox)) {
   5317 			rval = -1;
   5318 		}
   5319 #endif
   5320 		break;
   5321 
   5322 	case ASYNC_LOOP_DOWN:
   5323 		isp->isp_sendmarker = 1;
   5324 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   5325 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   5326 		ISP_MARK_PORTDB(isp, 1);
   5327 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
   5328 #ifdef	ISP_TARGET_MODE
   5329 		if (isp_target_async(isp, bus, mbox)) {
   5330 			rval = -1;
   5331 		}
   5332 #endif
   5333 		break;
   5334 
   5335 	case ASYNC_LOOP_RESET:
   5336 		isp->isp_sendmarker = 1;
   5337 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   5338 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   5339 		ISP_MARK_PORTDB(isp, 1);
   5340 		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
   5341 #ifdef	ISP_TARGET_MODE
   5342 		if (isp_target_async(isp, bus, mbox)) {
   5343 			rval = -1;
   5344 		}
   5345 #endif
   5346 		break;
   5347 
   5348 	case ASYNC_PDB_CHANGED:
   5349 		isp->isp_sendmarker = 1;
   5350 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   5351 		ISP_MARK_PORTDB(isp, 1);
   5352 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
   5353 		break;
   5354 
   5355 	case ASYNC_CHANGE_NOTIFY:
   5356 	    	if (FCPARAM(isp)->isp_topo == TOPO_F_PORT) {
   5357 			FCPARAM(isp)->isp_loopstate = LOOP_LSCAN_DONE;
   5358 		} else {
   5359 			FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   5360 		}
   5361 		ISP_MARK_PORTDB(isp, 1);
   5362 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
   5363 		break;
   5364 
   5365 	case ASYNC_PTPMODE:
   5366 		ISP_MARK_PORTDB(isp, 1);
   5367 		isp->isp_sendmarker = 1;
   5368 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   5369 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   5370 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   5371 #ifdef	ISP_TARGET_MODE
   5372 		if (isp_target_async(isp, bus, mbox)) {
   5373 			rval = -1;
   5374 		}
   5375 #endif
   5376 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
   5377 		break;
   5378 
   5379 	case ASYNC_CONNMODE:
   5380 		mbox = ISP_READ(isp, OUTMAILBOX1);
   5381 		ISP_MARK_PORTDB(isp, 1);
   5382 		switch (mbox) {
   5383 		case ISP_CONN_LOOP:
   5384 			isp_prt(isp, ISP_LOGINFO,
   5385 			    "Point-to-Point -> Loop mode");
   5386 			break;
   5387 		case ISP_CONN_PTP:
   5388 			isp_prt(isp, ISP_LOGINFO,
   5389 			    "Loop -> Point-to-Point mode");
   5390 			break;
   5391 		case ISP_CONN_BADLIP:
   5392 			isp_prt(isp, ISP_LOGWARN,
   5393 			    "Point-to-Point -> Loop mode (BAD LIP)");
   5394 			break;
   5395 		case ISP_CONN_FATAL:
   5396 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
   5397 			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
   5398 			return (-1);
   5399 		case ISP_CONN_LOOPBACK:
   5400 			isp_prt(isp, ISP_LOGWARN,
   5401 			    "Looped Back in Point-to-Point mode");
   5402 			break;
   5403 		default:
   5404 			isp_prt(isp, ISP_LOGWARN,
   5405 			    "Unknown connection mode (0x%x)", mbox);
   5406 			break;
   5407 		}
   5408 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   5409 		isp->isp_sendmarker = 1;
   5410 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   5411 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   5412 		break;
   5413 
   5414 	case ASYNC_RJT_SENT:	/* same as ASYNC_QFULL_SENT */
   5415 		if (IS_24XX(isp)) {
   5416 			isp_prt(isp, ISP_LOGTDEBUG0, "LS_RJT sent");
   5417 			break;
   5418 		} else if (IS_2200(isp)) {
   5419 			isp_prt(isp, ISP_LOGTDEBUG0, "QFULL sent");
   5420 			break;
   5421 		}
   5422 		/* FALLTHROUGH */
   5423 	default:
   5424 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
   5425 		break;
   5426 	}
   5427 
   5428 	if (bus & 0x100) {
   5429 		int i, nh;
   5430 		uint16_t handles[16];
   5431 
   5432 		for (nh = 0, i = 1; i < MAX_MAILBOX(isp); i++) {
   5433 			if ((bus & (1 << i)) == 0) {
   5434 				continue;
   5435 			}
   5436 			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
   5437 		}
   5438 		for (i = 0; i < nh; i++) {
   5439 			isp_fastpost_complete(isp, handles[i]);
   5440 			isp_prt(isp,  ISP_LOGDEBUG3,
   5441 			    "fast post completion of %u", handles[i]);
   5442 		}
   5443 		if (isp->isp_fpcchiwater < nh) {
   5444 			isp->isp_fpcchiwater = nh;
   5445 		}
   5446 	} else {
   5447 		isp->isp_intoasync++;
   5448 	}
   5449 	return (rval);
   5450 }
   5451 
   5452 /*
   5453  * Handle other response entries. A pointer to the request queue output
   5454  * index is here in case we want to eat several entries at once, although
   5455  * this is not used currently.
   5456  */
   5457 
   5458 static int
   5459 isp_handle_other_response(ispsoftc_t *isp, int type,
   5460     isphdr_t *hp, uint32_t *optrp)
   5461 {
   5462 	switch (type) {
   5463 	case RQSTYPE_STATUS_CONT:
   5464 		isp_prt(isp, ISP_LOGDEBUG0, "Ignored Continuation Response");
   5465 		return (1);
   5466 	case RQSTYPE_MARKER:
   5467 		isp_prt(isp, ISP_LOGDEBUG0, "Marker Response");
   5468 		return (1);
   5469 	case RQSTYPE_ATIO:
   5470 	case RQSTYPE_CTIO:
   5471 	case RQSTYPE_ENABLE_LUN:
   5472 	case RQSTYPE_MODIFY_LUN:
   5473 	case RQSTYPE_NOTIFY:
   5474 	case RQSTYPE_NOTIFY_ACK:
   5475 	case RQSTYPE_CTIO1:
   5476 	case RQSTYPE_ATIO2:
   5477 	case RQSTYPE_CTIO2:
   5478 	case RQSTYPE_CTIO3:
   5479 	case RQSTYPE_CTIO7:
   5480 	case RQSTYPE_ABTS_RCVD:
   5481 	case RQSTYPE_ABTS_RSP:
   5482 		isp->isp_rsltccmplt++;	/* count as a response completion */
   5483 #ifdef	ISP_TARGET_MODE
   5484 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
   5485 			return (1);
   5486 		}
   5487 #endif
   5488 		/* FALLTHROUGH */
   5489 	case RQSTYPE_REQUEST:
   5490 	default:
   5491 		USEC_DELAY(100);
   5492 		if (type != isp_get_response_type(isp, hp)) {
   5493 			/*
   5494 			 * This is questionable- we're just papering over
   5495 			 * something we've seen on SMP linux in target
   5496 			 * mode- we don't really know what's happening
   5497 			 * here that causes us to think we've gotten
   5498 			 * an entry, but that either the entry isn't
   5499 			 * filled out yet or our CPU read data is stale.
   5500 			 */
   5501 			isp_prt(isp, ISP_LOGINFO,
   5502 				"unstable type in response queue");
   5503 			return (-1);
   5504 		}
   5505 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
   5506 		    isp_get_response_type(isp, hp));
   5507 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
   5508 			return (1);
   5509 		}
   5510 		return (0);
   5511 	}
   5512 }
   5513 
   5514 static void
   5515 isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs, long *rp)
   5516 {
   5517 	switch (sp->req_completion_status & 0xff) {
   5518 	case RQCS_COMPLETE:
   5519 		if (XS_NOERR(xs)) {
   5520 			XS_SETERR(xs, HBA_NOERROR);
   5521 		}
   5522 		return;
   5523 
   5524 	case RQCS_INCOMPLETE:
   5525 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   5526 			isp_prt(isp, ISP_LOGDEBUG1,
   5527 			    "Selection Timeout for %d.%d.%d",
   5528 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5529 			if (XS_NOERR(xs)) {
   5530 				XS_SETERR(xs, HBA_SELTIMEOUT);
   5531 				*rp = XS_XFRLEN(xs);
   5532 			}
   5533 			return;
   5534 		}
   5535 		isp_prt(isp, ISP_LOGERR,
   5536 		    "command incomplete for %d.%d.%d, state 0x%x",
   5537 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
   5538 		    sp->req_state_flags);
   5539 		break;
   5540 
   5541 	case RQCS_DMA_ERROR:
   5542 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
   5543 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5544 		*rp = XS_XFRLEN(xs);
   5545 		break;
   5546 
   5547 	case RQCS_TRANSPORT_ERROR:
   5548 	{
   5549 		char buf[172];
   5550 		SNPRINTF(buf, sizeof (buf), "states=>");
   5551 		if (sp->req_state_flags & RQSF_GOT_BUS) {
   5552 			SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
   5553 		}
   5554 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
   5555 			SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
   5556 		}
   5557 		if (sp->req_state_flags & RQSF_SENT_CDB) {
   5558 			SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
   5559 		}
   5560 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
   5561 			SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
   5562 		}
   5563 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   5564 			SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
   5565 		}
   5566 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
   5567 			SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
   5568 		}
   5569 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
   5570 			SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
   5571 		}
   5572 		SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
   5573 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
   5574 			SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
   5575 		}
   5576 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
   5577 			SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
   5578 		}
   5579 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
   5580 			SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
   5581 		}
   5582 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   5583 			SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
   5584 		}
   5585 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
   5586 			SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
   5587 		}
   5588 		if (sp->req_status_flags & RQSTF_ABORTED) {
   5589 			SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
   5590 		}
   5591 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
   5592 			SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
   5593 		}
   5594 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   5595 			SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
   5596 		}
   5597 		isp_prt(isp, ISP_LOGERR, "%s", buf);
   5598 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
   5599 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
   5600 		*rp = XS_XFRLEN(xs);
   5601 		break;
   5602 	}
   5603 	case RQCS_RESET_OCCURRED:
   5604 		isp_prt(isp, ISP_LOGWARN,
   5605 		    "bus reset destroyed command for %d.%d.%d",
   5606 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5607 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   5608 		if (XS_NOERR(xs)) {
   5609 			XS_SETERR(xs, HBA_BUSRESET);
   5610 		}
   5611 		*rp = XS_XFRLEN(xs);
   5612 		return;
   5613 
   5614 	case RQCS_ABORTED:
   5615 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
   5616 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5617 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   5618 		if (XS_NOERR(xs)) {
   5619 			XS_SETERR(xs, HBA_ABORTED);
   5620 		}
   5621 		return;
   5622 
   5623 	case RQCS_TIMEOUT:
   5624 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
   5625 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5626 		/*
   5627 	 	 * XXX: Check to see if we logged out of the device.
   5628 		 */
   5629 		if (XS_NOERR(xs)) {
   5630 			XS_SETERR(xs, HBA_CMDTIMEOUT);
   5631 		}
   5632 		return;
   5633 
   5634 	case RQCS_DATA_OVERRUN:
   5635 		XS_RESID(xs) = sp->req_resid;
   5636 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
   5637 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5638 		if (XS_NOERR(xs)) {
   5639 			XS_SETERR(xs, HBA_DATAOVR);
   5640 		}
   5641 		return;
   5642 
   5643 	case RQCS_COMMAND_OVERRUN:
   5644 		isp_prt(isp, ISP_LOGERR,
   5645 		    "command overrun for command on %d.%d.%d",
   5646 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5647 		break;
   5648 
   5649 	case RQCS_STATUS_OVERRUN:
   5650 		isp_prt(isp, ISP_LOGERR,
   5651 		    "status overrun for command on %d.%d.%d",
   5652 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5653 		break;
   5654 
   5655 	case RQCS_BAD_MESSAGE:
   5656 		isp_prt(isp, ISP_LOGERR,
   5657 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
   5658 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5659 		break;
   5660 
   5661 	case RQCS_NO_MESSAGE_OUT:
   5662 		isp_prt(isp, ISP_LOGERR,
   5663 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
   5664 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5665 		break;
   5666 
   5667 	case RQCS_EXT_ID_FAILED:
   5668 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
   5669 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5670 		break;
   5671 
   5672 	case RQCS_IDE_MSG_FAILED:
   5673 		isp_prt(isp, ISP_LOGERR,
   5674 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
   5675 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5676 		break;
   5677 
   5678 	case RQCS_ABORT_MSG_FAILED:
   5679 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
   5680 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5681 		break;
   5682 
   5683 	case RQCS_REJECT_MSG_FAILED:
   5684 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
   5685 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5686 		break;
   5687 
   5688 	case RQCS_NOP_MSG_FAILED:
   5689 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
   5690 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5691 		break;
   5692 
   5693 	case RQCS_PARITY_ERROR_MSG_FAILED:
   5694 		isp_prt(isp, ISP_LOGERR,
   5695 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
   5696 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5697 		break;
   5698 
   5699 	case RQCS_DEVICE_RESET_MSG_FAILED:
   5700 		isp_prt(isp, ISP_LOGWARN,
   5701 		    "BUS DEVICE RESET rejected by %d.%d.%d",
   5702 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5703 		break;
   5704 
   5705 	case RQCS_ID_MSG_FAILED:
   5706 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
   5707 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5708 		break;
   5709 
   5710 	case RQCS_UNEXP_BUS_FREE:
   5711 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
   5712 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5713 		break;
   5714 
   5715 	case RQCS_DATA_UNDERRUN:
   5716 	{
   5717 		if (IS_FC(isp)) {
   5718 			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
   5719 			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
   5720 				isp_prt(isp, ISP_LOGWARN, bun, XS_TGT(xs),
   5721 				    XS_LUN(xs), XS_XFRLEN(xs), sp->req_resid,
   5722 				    (ru_marked)? "marked" : "not marked");
   5723 				if (XS_NOERR(xs)) {
   5724 					XS_SETERR(xs, HBA_BOTCH);
   5725 				}
   5726 				return;
   5727 			}
   5728 		}
   5729 		XS_RESID(xs) = sp->req_resid;
   5730 		if (XS_NOERR(xs)) {
   5731 			XS_SETERR(xs, HBA_NOERROR);
   5732 		}
   5733 		return;
   5734 	}
   5735 
   5736 	case RQCS_XACT_ERR1:
   5737 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
   5738 		    XS_TGT(xs), XS_LUN(xs));
   5739 		break;
   5740 
   5741 	case RQCS_XACT_ERR2:
   5742 		isp_prt(isp, ISP_LOGERR, xact2,
   5743 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
   5744 		break;
   5745 
   5746 	case RQCS_XACT_ERR3:
   5747 		isp_prt(isp, ISP_LOGERR, xact3,
   5748 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5749 		break;
   5750 
   5751 	case RQCS_BAD_ENTRY:
   5752 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
   5753 		break;
   5754 
   5755 	case RQCS_QUEUE_FULL:
   5756 		isp_prt(isp, ISP_LOGDEBUG0,
   5757 		    "internal queues full for %d.%d.%d status 0x%x",
   5758 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), *XS_STSP(xs));
   5759 
   5760 		/*
   5761 		 * If QFULL or some other status byte is set, then this
   5762 		 * isn't an error, per se.
   5763 		 *
   5764 		 * Unfortunately, some QLogic f/w writers have, in
   5765 		 * some cases, ommitted to *set* status to QFULL.
   5766 		 *
   5767 
   5768 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
   5769 			XS_SETERR(xs, HBA_NOERROR);
   5770 			return;
   5771 		}
   5772 
   5773 		 *
   5774 		 *
   5775 		 */
   5776 
   5777 		*XS_STSP(xs) = SCSI_QFULL;
   5778 		XS_SETERR(xs, HBA_NOERROR);
   5779 		return;
   5780 
   5781 	case RQCS_PHASE_SKIPPED:
   5782 		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
   5783 		    XS_TGT(xs), XS_LUN(xs));
   5784 		break;
   5785 
   5786 	case RQCS_ARQS_FAILED:
   5787 		isp_prt(isp, ISP_LOGERR,
   5788 		    "Auto Request Sense failed for %d.%d.%d",
   5789 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5790 		if (XS_NOERR(xs)) {
   5791 			XS_SETERR(xs, HBA_ARQFAIL);
   5792 		}
   5793 		return;
   5794 
   5795 	case RQCS_WIDE_FAILED:
   5796 		isp_prt(isp, ISP_LOGERR,
   5797 		    "Wide Negotiation failed for %d.%d.%d",
   5798 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   5799 		if (IS_SCSI(isp)) {
   5800 			sdparam *sdp = isp->isp_param;
   5801 			sdp += XS_CHANNEL(xs);
   5802 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
   5803 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   5804 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   5805 		}
   5806 		if (XS_NOERR(xs)) {
   5807 			XS_SETERR(xs, HBA_NOERROR);
   5808 		}
   5809 		return;
   5810 
   5811 	case RQCS_SYNCXFER_FAILED:
   5812 		isp_prt(isp, ISP_LOGERR,
   5813 		    "SDTR Message failed for target %d.%d.%d",
   5814 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   5815 		if (IS_SCSI(isp)) {
   5816 			sdparam *sdp = isp->isp_param;
   5817 			sdp += XS_CHANNEL(xs);
   5818 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
   5819 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   5820 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   5821 		}
   5822 		break;
   5823 
   5824 	case RQCS_LVD_BUSERR:
   5825 		isp_prt(isp, ISP_LOGERR,
   5826 		    "Bad LVD condition while talking to %d.%d.%d",
   5827 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   5828 		break;
   5829 
   5830 	case RQCS_PORT_UNAVAILABLE:
   5831 		/*
   5832 		 * No such port on the loop. Moral equivalent of SELTIMEO
   5833 		 */
   5834 	case RQCS_PORT_LOGGED_OUT:
   5835 	{
   5836 		const char *reason;
   5837 		uint8_t sts = sp->req_completion_status & 0xff;
   5838 
   5839 		/*
   5840 		 * It was there (maybe)- treat as a selection timeout.
   5841 		 */
   5842 		if (sts == RQCS_PORT_UNAVAILABLE) {
   5843 			reason = "unavailable";
   5844 		} else {
   5845 			reason = "logout";
   5846 		}
   5847 
   5848 		isp_prt(isp, ISP_LOGINFO, "port %s for target %d",
   5849 		    reason, XS_TGT(xs));
   5850 
   5851 		/*
   5852 		 * If we're on a local loop, force a LIP (which is overkill)
   5853 		 * to force a re-login of this unit. If we're on fabric,
   5854 		 * then we'll have to log in again as a matter of course.
   5855 		 */
   5856 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
   5857 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
   5858 			mbreg_t mbs;
   5859 			MEMZERO(&mbs, sizeof (mbs));
   5860 			mbs.param[0] = MBOX_INIT_LIP;
   5861 			if (FCPARAM(isp)->isp_2klogin) {
   5862 				mbs.ibits = (1 << 10);
   5863 			}
   5864 			mbs.logval = MBLOGALL;
   5865 			isp_mboxcmd_qnw(isp, &mbs, 1);
   5866 		}
   5867 		if (XS_NOERR(xs)) {
   5868 			XS_SETERR(xs, HBA_SELTIMEOUT);
   5869 		}
   5870 		return;
   5871 	}
   5872 	case RQCS_PORT_CHANGED:
   5873 		isp_prt(isp, ISP_LOGWARN,
   5874 		    "port changed for target %d", XS_TGT(xs));
   5875 		if (XS_NOERR(xs)) {
   5876 			XS_SETERR(xs, HBA_SELTIMEOUT);
   5877 		}
   5878 		return;
   5879 
   5880 	case RQCS_PORT_BUSY:
   5881 		isp_prt(isp, ISP_LOGWARN,
   5882 		    "port busy for target %d", XS_TGT(xs));
   5883 		if (XS_NOERR(xs)) {
   5884 			XS_SETERR(xs, HBA_TGTBSY);
   5885 		}
   5886 		return;
   5887 
   5888 	default:
   5889 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
   5890 		    sp->req_completion_status);
   5891 		break;
   5892 	}
   5893 	if (XS_NOERR(xs)) {
   5894 		XS_SETERR(xs, HBA_BOTCH);
   5895 	}
   5896 }
   5897 
   5898 static void
   5899 isp_parse_status_24xx(ispsoftc_t *isp, isp24xx_statusreq_t *sp,
   5900     XS_T *xs, long *rp)
   5901 {
   5902 	switch (sp->req_completion_status) {
   5903 	case RQCS_COMPLETE:
   5904 		if (XS_NOERR(xs)) {
   5905 			XS_SETERR(xs, HBA_NOERROR);
   5906 		}
   5907 		return;
   5908 
   5909 	case RQCS_DMA_ERROR:
   5910 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
   5911 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5912 		break;
   5913 
   5914 	case RQCS_TRANSPORT_ERROR:
   5915 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d",
   5916 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5917 		break;
   5918 
   5919 	case RQCS_RESET_OCCURRED:
   5920 		isp_prt(isp, ISP_LOGWARN,
   5921 		    "bus reset destroyed command for %d.%d.%d",
   5922 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5923 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   5924 		if (XS_NOERR(xs)) {
   5925 			XS_SETERR(xs, HBA_BUSRESET);
   5926 		}
   5927 		return;
   5928 
   5929 	case RQCS_ABORTED:
   5930 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
   5931 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5932 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   5933 		if (XS_NOERR(xs)) {
   5934 			XS_SETERR(xs, HBA_ABORTED);
   5935 		}
   5936 		return;
   5937 
   5938 	case RQCS_TIMEOUT:
   5939 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
   5940 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5941 		if (XS_NOERR(xs)) {
   5942 			XS_SETERR(xs, HBA_CMDTIMEOUT);
   5943 		}
   5944 		return;
   5945 
   5946 	case RQCS_DATA_OVERRUN:
   5947 		XS_RESID(xs) = sp->req_resid;
   5948 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
   5949 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   5950 		if (XS_NOERR(xs)) {
   5951 			XS_SETERR(xs, HBA_DATAOVR);
   5952 		}
   5953 		return;
   5954 
   5955 	case RQCS_24XX_DRE:	/* data reassembly error */
   5956 		isp_prt(isp, ISP_LOGERR, "data reassembly error for target %d",
   5957 		    XS_TGT(xs));
   5958 		if (XS_NOERR(xs)) {
   5959 			XS_SETERR(xs, HBA_ABORTED);
   5960 		}
   5961 		*rp = XS_XFRLEN(xs);
   5962 		return;
   5963 
   5964 	case RQCS_24XX_TABORT:	/* aborted by target */
   5965 		isp_prt(isp, ISP_LOGERR, "target %d sent ABTS",
   5966 		    XS_TGT(xs));
   5967 		if (XS_NOERR(xs)) {
   5968 			XS_SETERR(xs, HBA_ABORTED);
   5969 		}
   5970 		return;
   5971 
   5972 	case RQCS_DATA_UNDERRUN:
   5973 
   5974 		XS_RESID(xs) = sp->req_resid;
   5975 		if (XS_NOERR(xs)) {
   5976 			XS_SETERR(xs, HBA_NOERROR);
   5977 		}
   5978 		return;
   5979 
   5980 	case RQCS_PORT_UNAVAILABLE:
   5981 		/*
   5982 		 * No such port on the loop. Moral equivalent of SELTIMEO
   5983 		 */
   5984 	case RQCS_PORT_LOGGED_OUT:
   5985 	{
   5986 		const char *reason;
   5987 		uint8_t sts = sp->req_completion_status & 0xff;
   5988 
   5989 		/*
   5990 		 * It was there (maybe)- treat as a selection timeout.
   5991 		 */
   5992 		if (sts == RQCS_PORT_UNAVAILABLE) {
   5993 			reason = "unavailable";
   5994 		} else {
   5995 			reason = "logout";
   5996 		}
   5997 
   5998 		isp_prt(isp, ISP_LOGINFO, "port %s for target %d",
   5999 		    reason, XS_TGT(xs));
   6000 
   6001 		/*
   6002 		 * If we're on a local loop, force a LIP (which is overkill)
   6003 		 * to force a re-login of this unit. If we're on fabric,
   6004 		 * then we'll have to log in again as a matter of course.
   6005 		 */
   6006 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
   6007 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
   6008 			mbreg_t mbs;
   6009 			MEMZERO(&mbs, sizeof (mbs));
   6010 			mbs.param[0] = MBOX_INIT_LIP;
   6011 			if (FCPARAM(isp)->isp_2klogin) {
   6012 				mbs.ibits = (1 << 10);
   6013 			}
   6014 			mbs.logval = MBLOGALL;
   6015 			isp_mboxcmd_qnw(isp, &mbs, 1);
   6016 		}
   6017 		if (XS_NOERR(xs)) {
   6018 			XS_SETERR(xs, HBA_SELTIMEOUT);
   6019 		}
   6020 		return;
   6021 	}
   6022 	case RQCS_PORT_CHANGED:
   6023 		isp_prt(isp, ISP_LOGWARN,
   6024 		    "port changed for target %d", XS_TGT(xs));
   6025 		if (XS_NOERR(xs)) {
   6026 			XS_SETERR(xs, HBA_SELTIMEOUT);
   6027 		}
   6028 		return;
   6029 
   6030 
   6031 	case RQCS_24XX_ENOMEM:	/* f/w resource unavailable */
   6032 		isp_prt(isp, ISP_LOGWARN,
   6033 		    "f/w resource unavailable for target %d", XS_TGT(xs));
   6034 		if (XS_NOERR(xs)) {
   6035 			*XS_STSP(xs) = SCSI_BUSY;
   6036 			XS_SETERR(xs, HBA_TGTBSY);
   6037 		}
   6038 		return;
   6039 
   6040 	case RQCS_24XX_TMO:	/* task management overrun */
   6041 		isp_prt(isp, ISP_LOGWARN,
   6042 		    "command for target %d overlapped task management",
   6043 		    XS_TGT(xs));
   6044 		if (XS_NOERR(xs)) {
   6045 			*XS_STSP(xs) = SCSI_BUSY;
   6046 			XS_SETERR(xs, HBA_TGTBSY);
   6047 		}
   6048 		return;
   6049 
   6050 	default:
   6051 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
   6052 		    sp->req_completion_status);
   6053 		break;
   6054 	}
   6055 	if (XS_NOERR(xs)) {
   6056 		XS_SETERR(xs, HBA_BOTCH);
   6057 	}
   6058 }
   6059 
   6060 static void
   6061 isp_fastpost_complete(ispsoftc_t *isp, uint16_t fph)
   6062 {
   6063 	XS_T *xs;
   6064 
   6065 	if (fph == 0) {
   6066 		return;
   6067 	}
   6068 	xs = isp_find_xs(isp, fph);
   6069 	if (xs == NULL) {
   6070 		isp_prt(isp, ISP_LOGWARN,
   6071 		    "Command for fast post handle 0x%x not found", fph);
   6072 		return;
   6073 	}
   6074 	isp_destroy_handle(isp, fph);
   6075 
   6076 	/*
   6077 	 * Since we don't have a result queue entry item,
   6078 	 * we must believe that SCSI status is zero and
   6079 	 * that all data transferred.
   6080 	 */
   6081 	XS_SET_STATE_STAT(isp, xs, NULL);
   6082 	XS_RESID(xs) = 0;
   6083 	*XS_STSP(xs) = SCSI_GOOD;
   6084 	if (XS_XFRLEN(xs)) {
   6085 		ISP_DMAFREE(isp, xs, fph);
   6086 	}
   6087 	if (isp->isp_nactive)
   6088 		isp->isp_nactive--;
   6089 	isp->isp_fphccmplt++;
   6090 	isp_done(xs);
   6091 }
   6092 
   6093 static int
   6094 isp_mbox_continue(ispsoftc_t *isp)
   6095 {
   6096 	mbreg_t mbs;
   6097 	uint16_t *ptr;
   6098 	uint32_t offset;
   6099 
   6100 	switch (isp->isp_lastmbxcmd) {
   6101 	case MBOX_WRITE_RAM_WORD:
   6102 	case MBOX_READ_RAM_WORD:
   6103 	case MBOX_WRITE_RAM_WORD_EXTENDED:
   6104 	case MBOX_READ_RAM_WORD_EXTENDED:
   6105 		break;
   6106 	default:
   6107 		return (1);
   6108 	}
   6109 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
   6110 		isp->isp_mbxwrk0 = 0;
   6111 		return (-1);
   6112 	}
   6113 
   6114 	/*
   6115 	 * Clear the previous interrupt.
   6116 	 */
   6117 	if (IS_24XX(isp)) {
   6118 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
   6119 	} else {
   6120 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   6121 		ISP_WRITE(isp, BIU_SEMA, 0);
   6122 	}
   6123 
   6124 	/*
   6125 	 * Continue with next word.
   6126 	 */
   6127 	MEMZERO(&mbs, sizeof (mbs));
   6128 	ptr = isp->isp_mbxworkp;
   6129 	switch (isp->isp_lastmbxcmd) {
   6130 	case MBOX_WRITE_RAM_WORD:
   6131 		mbs.param[1] = isp->isp_mbxwrk1++;;
   6132 		mbs.param[2] = *ptr++;;
   6133 		break;
   6134 	case MBOX_READ_RAM_WORD:
   6135 		*ptr++ = isp->isp_mboxtmp[2];
   6136 		mbs.param[1] = isp->isp_mbxwrk1++;
   6137 		break;
   6138 	case MBOX_WRITE_RAM_WORD_EXTENDED:
   6139 		offset = isp->isp_mbxwrk1;
   6140 		offset |= isp->isp_mbxwrk8 << 16;
   6141 
   6142 		mbs.param[2] = *ptr++;;
   6143 		mbs.param[1] = offset;
   6144 		mbs.param[8] = offset >> 16;
   6145 		isp->isp_mbxwrk1 = ++offset;
   6146 		isp->isp_mbxwrk8 = offset >> 16;
   6147 		break;
   6148 	case MBOX_READ_RAM_WORD_EXTENDED:
   6149 		offset = isp->isp_mbxwrk1;
   6150 		offset |= isp->isp_mbxwrk8 << 16;
   6151 
   6152 		*ptr++ = isp->isp_mboxtmp[2];
   6153 		mbs.param[1] = offset;
   6154 		mbs.param[8] = offset >> 16;
   6155 		isp->isp_mbxwrk1 = ++offset;
   6156 		isp->isp_mbxwrk8 = offset >> 16;
   6157 		break;
   6158 	}
   6159 	isp->isp_mbxworkp = ptr;
   6160 	isp->isp_mbxwrk0--;
   6161 	mbs.param[0] = isp->isp_lastmbxcmd;
   6162 	mbs.logval = MBLOGALL;
   6163 	isp_mboxcmd_qnw(isp, &mbs, 0);
   6164 	return (0);
   6165 }
   6166 
   6167 #define	HIWRD(x)			((x) >> 16)
   6168 #define	LOWRD(x)			((x)  & 0xffff)
   6169 #define	ISPOPMAP(a, b)			(((a) << 16) | (b))
   6170 static const uint32_t mbpscsi[] = {
   6171 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   6172 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   6173 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   6174 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   6175 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   6176 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   6177 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   6178 	ISPOPMAP(0x07, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   6179 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   6180 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
   6181 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
   6182 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   6183 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   6184 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   6185 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   6186 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   6187 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   6188 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
   6189 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
   6190 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   6191 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
   6192 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
   6193 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
   6194 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
   6195 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
   6196 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
   6197 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
   6198 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   6199 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
   6200 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   6201 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   6202 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   6203 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
   6204 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   6205 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   6206 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   6207 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
   6208 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   6209 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   6210 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
   6211 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   6212 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   6213 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
   6214 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   6215 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   6216 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   6217 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   6218 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   6219 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   6220 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   6221 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   6222 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   6223 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
   6224 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
   6225 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   6226 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   6227 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   6228 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   6229 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
   6230 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   6231 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   6232 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   6233 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   6234 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   6235 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   6236 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   6237 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   6238 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
   6239 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   6240 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
   6241 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
   6242 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
   6243 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
   6244 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
   6245 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
   6246 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
   6247 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   6248 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   6249 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   6250 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   6251 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
   6252 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
   6253 	ISPOPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
   6254 	ISPOPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
   6255 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUCUTE COMMAND IOCB A64 */
   6256 	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
   6257 	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
   6258 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   6259 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   6260 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   6261 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
   6262 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
   6263 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
   6264 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
   6265 };
   6266 
   6267 static const char *scsi_mbcmd_names[] = {
   6268 	"NO-OP",
   6269 	"LOAD RAM",
   6270 	"EXEC FIRMWARE",
   6271 	"DUMP RAM",
   6272 	"WRITE RAM WORD",
   6273 	"READ RAM WORD",
   6274 	"MAILBOX REG TEST",
   6275 	"VERIFY CHECKSUM",
   6276 	"ABOUT FIRMWARE",
   6277 	NULL,
   6278 	NULL,
   6279 	NULL,
   6280 	NULL,
   6281 	NULL,
   6282 	"CHECK FIRMWARE",
   6283 	NULL,
   6284 	"INIT REQUEST QUEUE",
   6285 	"INIT RESULT QUEUE",
   6286 	"EXECUTE IOCB",
   6287 	"WAKE UP",
   6288 	"STOP FIRMWARE",
   6289 	"ABORT",
   6290 	"ABORT DEVICE",
   6291 	"ABORT TARGET",
   6292 	"BUS RESET",
   6293 	"STOP QUEUE",
   6294 	"START QUEUE",
   6295 	"SINGLE STEP QUEUE",
   6296 	"ABORT QUEUE",
   6297 	"GET DEV QUEUE STATUS",
   6298 	NULL,
   6299 	"GET FIRMWARE STATUS",
   6300 	"GET INIT SCSI ID",
   6301 	"GET SELECT TIMEOUT",
   6302 	"GET RETRY COUNT",
   6303 	"GET TAG AGE LIMIT",
   6304 	"GET CLOCK RATE",
   6305 	"GET ACT NEG STATE",
   6306 	"GET ASYNC DATA SETUP TIME",
   6307 	"GET PCI PARAMS",
   6308 	"GET TARGET PARAMS",
   6309 	"GET DEV QUEUE PARAMS",
   6310 	"GET RESET DELAY PARAMS",
   6311 	NULL,
   6312 	NULL,
   6313 	NULL,
   6314 	NULL,
   6315 	NULL,
   6316 	"SET INIT SCSI ID",
   6317 	"SET SELECT TIMEOUT",
   6318 	"SET RETRY COUNT",
   6319 	"SET TAG AGE LIMIT",
   6320 	"SET CLOCK RATE",
   6321 	"SET ACT NEG STATE",
   6322 	"SET ASYNC DATA SETUP TIME",
   6323 	"SET PCI CONTROL PARAMS",
   6324 	"SET TARGET PARAMS",
   6325 	"SET DEV QUEUE PARAMS",
   6326 	"SET RESET DELAY PARAMS",
   6327 	NULL,
   6328 	NULL,
   6329 	NULL,
   6330 	NULL,
   6331 	NULL,
   6332 	"RETURN BIOS BLOCK ADDR",
   6333 	"WRITE FOUR RAM WORDS",
   6334 	"EXEC BIOS IOCB",
   6335 	NULL,
   6336 	NULL,
   6337 	"SET SYSTEM PARAMETER",
   6338 	"GET SYSTEM PARAMETER",
   6339 	NULL,
   6340 	"GET SCAM CONFIGURATION",
   6341 	"SET SCAM CONFIGURATION",
   6342 	"SET FIRMWARE FEATURES",
   6343 	"GET FIRMWARE FEATURES",
   6344 	NULL,
   6345 	NULL,
   6346 	NULL,
   6347 	NULL,
   6348 	"LOAD RAM A64",
   6349 	"DUMP RAM A64",
   6350 	"INITIALIZE REQUEST QUEUE A64",
   6351 	"INITIALIZE RESPONSE QUEUE A64",
   6352 	"EXECUTE IOCB A64",
   6353 	"ENABLE TARGET MODE",
   6354 	"GET TARGET MODE STATE",
   6355 	NULL,
   6356 	NULL,
   6357 	NULL,
   6358 	"SET DATA OVERRUN RECOVERY MODE",
   6359 	"GET DATA OVERRUN RECOVERY MODE",
   6360 	"SET HOST DATA",
   6361 	"GET NOST DATA",
   6362 };
   6363 
   6364 static const uint32_t mbpfc[] = {
   6365 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   6366 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   6367 	ISPOPMAP(0x0f, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   6368 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   6369 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   6370 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   6371 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   6372 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   6373 	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   6374 	ISPOPMAP(0xdf, 0x01),	/* 0x09: MBOX_LOAD_RISC_RAM_2100 */
   6375 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
   6376 	ISPOPMAP(0x1ff, 0x01),	/* 0x0b: MBOX_LOAD_RISC_RAM */
   6377 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   6378 	ISPOPMAP(0x10f, 0x01),	/* 0x0d: MBOX_WRITE_RAM_WORD_EXTENDED */
   6379 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   6380 	ISPOPMAP(0x10f, 0x05),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED */
   6381 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   6382 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
   6383 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
   6384 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   6385 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
   6386 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
   6387 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
   6388 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
   6389 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
   6390 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
   6391 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
   6392 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   6393 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
   6394 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   6395 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   6396 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   6397 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
   6398 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
   6399 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   6400 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
   6401 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
   6402 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
   6403 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
   6404 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
   6405 	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
   6406 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
   6407 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
   6408 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   6409 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   6410 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   6411 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   6412 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   6413 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
   6414 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
   6415 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   6416 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
   6417 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
   6418 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
   6419 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
   6420 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
   6421 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
   6422 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
   6423 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
   6424 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   6425 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   6426 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   6427 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   6428 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   6429 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
   6430 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
   6431 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNT */
   6432 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_OFFLINE_MODE */
   6433 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   6434 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
   6435 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
   6436 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
   6437 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
   6438 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
   6439 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
   6440 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
   6441 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   6442 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   6443 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   6444 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   6445 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
   6446 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
   6447 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
   6448 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
   6449 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   6450 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
   6451 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   6452 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   6453 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   6454 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   6455 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
   6456 	ISPOPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
   6457 	ISPOPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
   6458 	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
   6459 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
   6460 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
   6461 	ISPOPMAP(0xcd, 0x01),	/* 0x60: MBOX_INIT_FIRMWARE */
   6462 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
   6463 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
   6464 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   6465 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
   6466 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
   6467 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
   6468 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
   6469 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
   6470 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
   6471 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
   6472 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
   6473 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
   6474 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
   6475 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
   6476 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
   6477 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
   6478 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
   6479 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
   6480 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
   6481 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
   6482 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
   6483 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
   6484 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
   6485 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
   6486 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
   6487 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
   6488 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
   6489 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
   6490 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
   6491 	ISPOPMAP(0x0f, 0x01)	/* 0x7e: LUN RESET */
   6492 };
   6493 /*
   6494  * Footnotes
   6495  *
   6496  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
   6497  *	do not access at this time in the core driver. The caller is
   6498  *	responsible for setting this register first (Gross!). The assumption
   6499  *	is that we won't overflow.
   6500  */
   6501 
   6502 static const char *fc_mbcmd_names[] = {
   6503 	"NO-OP",
   6504 	"LOAD RAM",
   6505 	"EXEC FIRMWARE",
   6506 	"DUMP RAM",
   6507 	"WRITE RAM WORD",
   6508 	"READ RAM WORD",
   6509 	"MAILBOX REG TEST",
   6510 	"VERIFY CHECKSUM",
   6511 	"ABOUT FIRMWARE",
   6512 	"LOAD RAM",
   6513 	"DUMP RAM",
   6514 	"WRITE RAM WORD EXTENDED",
   6515 	NULL,
   6516 	"READ RAM WORD EXTENDED",
   6517 	"CHECK FIRMWARE",
   6518 	NULL,
   6519 	"INIT REQUEST QUEUE",
   6520 	"INIT RESULT QUEUE",
   6521 	"EXECUTE IOCB",
   6522 	"WAKE UP",
   6523 	"STOP FIRMWARE",
   6524 	"ABORT",
   6525 	"ABORT DEVICE",
   6526 	"ABORT TARGET",
   6527 	"BUS RESET",
   6528 	"STOP QUEUE",
   6529 	"START QUEUE",
   6530 	"SINGLE STEP QUEUE",
   6531 	"ABORT QUEUE",
   6532 	"GET DEV QUEUE STATUS",
   6533 	NULL,
   6534 	"GET FIRMWARE STATUS",
   6535 	"GET LOOP ID",
   6536 	NULL,
   6537 	"GET RETRY COUNT",
   6538 	NULL,
   6539 	NULL,
   6540 	NULL,
   6541 	NULL,
   6542 	NULL,
   6543 	"GET FIRMWARE OPTIONS",
   6544 	"GET PORT QUEUE PARAMS",
   6545 	NULL,
   6546 	NULL,
   6547 	NULL,
   6548 	NULL,
   6549 	NULL,
   6550 	NULL,
   6551 	NULL,
   6552 	NULL,
   6553 	"SET RETRY COUNT",
   6554 	NULL,
   6555 	NULL,
   6556 	NULL,
   6557 	NULL,
   6558 	NULL,
   6559 	"SET FIRMWARE OPTIONS",
   6560 	"SET PORT QUEUE PARAMS",
   6561 	NULL,
   6562 	NULL,
   6563 	NULL,
   6564 	NULL,
   6565 	NULL,
   6566 	NULL,
   6567 	"LOOP PORT BYPASS",
   6568 	"LOOP PORT ENABLE",
   6569 	"GET RESOURCE COUNT",
   6570 	"REQUEST NON PARTICIPATING MODE",
   6571 	NULL,
   6572 	NULL,
   6573 	NULL,
   6574 	"GET PORT DATABASE ENHANCED",
   6575 	NULL,
   6576 	NULL,
   6577 	NULL,
   6578 	NULL,
   6579 	NULL,
   6580 	NULL,
   6581 	NULL,
   6582 	NULL,
   6583 	NULL,
   6584 	NULL,
   6585 	NULL,
   6586 	NULL,
   6587 	"EXECUTE IOCB A64",
   6588 	NULL,
   6589 	NULL,
   6590 	NULL,
   6591 	NULL,
   6592 	NULL,
   6593 	NULL,
   6594 	"DRIVER HEARTBEAT",
   6595 	NULL,
   6596 	"GET/SET DATA RATE",
   6597 	NULL,
   6598 	NULL,
   6599 	"INIT FIRMWARE",
   6600 	NULL,
   6601 	"INIT LIP",
   6602 	"GET FC-AL POSITION MAP",
   6603 	"GET PORT DATABASE",
   6604 	"CLEAR ACA",
   6605 	"TARGET RESET",
   6606 	"CLEAR TASK SET",
   6607 	"ABORT TASK SET",
   6608 	"GET FW STATE",
   6609 	"GET PORT NAME",
   6610 	"GET LINK STATUS",
   6611 	"INIT LIP RESET",
   6612 	NULL,
   6613 	"SEND SNS",
   6614 	"FABRIC LOGIN",
   6615 	"SEND CHANGE REQUEST",
   6616 	"FABRIC LOGOUT",
   6617 	"INIT LIP LOGIN",
   6618 	NULL,
   6619 	"LOGIN LOOP PORT",
   6620 	"GET PORT/NODE NAME LIST",
   6621 	"SET VENDOR ID",
   6622 	"INITIALIZE IP MAILBOX",
   6623 	NULL,
   6624 	NULL,
   6625 	NULL,
   6626 	NULL,
   6627 	"Get ID List",
   6628 	"SEND LFA",
   6629 	"Lun RESET"
   6630 };
   6631 
   6632 static void
   6633 isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
   6634 {
   6635 	unsigned int ibits, obits, box, opcode;
   6636 	const uint32_t *mcp;
   6637 
   6638 	if (IS_FC(isp)) {
   6639 		mcp = mbpfc;
   6640 	} else {
   6641 		mcp = mbpscsi;
   6642 	}
   6643 	opcode = mbp->param[0];
   6644 	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
   6645 	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
   6646 	ibits |= mbp->ibits;
   6647 	obits |= mbp->obits;
   6648 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
   6649 		if (ibits & (1 << box)) {
   6650 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   6651 		}
   6652 		if (nodelay == 0) {
   6653 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   6654 		}
   6655 	}
   6656 	if (nodelay == 0) {
   6657 		isp->isp_lastmbxcmd = opcode;
   6658 		isp->isp_obits = obits;
   6659 		isp->isp_mboxbsy = 1;
   6660 	}
   6661 	if (IS_24XX(isp)) {
   6662 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
   6663 	} else {
   6664 		ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   6665 	}
   6666 	/*
   6667 	 * Oddly enough, if we're not delaying for an answer,
   6668 	 * delay a bit to give the f/w a chance to pick up the
   6669 	 * command.
   6670 	 */
   6671 	if (nodelay) {
   6672 		USEC_DELAY(1000);
   6673 	}
   6674 }
   6675 
   6676 static void
   6677 isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp)
   6678 {
   6679 	const char *cname, *xname;
   6680 	char tname[16], mname[16];
   6681 	unsigned int lim, ibits, obits, box, opcode;
   6682 	const uint32_t *mcp;
   6683 
   6684 	if (IS_FC(isp)) {
   6685 		mcp = mbpfc;
   6686 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   6687 	} else {
   6688 		mcp = mbpscsi;
   6689 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   6690 	}
   6691 
   6692 	if ((opcode = mbp->param[0]) >= lim) {
   6693 		mbp->param[0] = MBOX_INVALID_COMMAND;
   6694 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
   6695 		return;
   6696 	}
   6697 
   6698 	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
   6699 	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
   6700 
   6701 	/*
   6702 	 * Pick up any additional bits that the caller might have set.
   6703 	 */
   6704 	ibits |= mbp->ibits;
   6705 	obits |= mbp->obits;
   6706 
   6707 	if (ibits == 0 && obits == 0) {
   6708 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
   6709 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
   6710 		return;
   6711 	}
   6712 
   6713 	/*
   6714 	 * Get exclusive usage of mailbox registers.
   6715 	 */
   6716 	if (MBOX_ACQUIRE(isp)) {
   6717 		mbp->param[0] = MBOX_REGS_BUSY;
   6718 		goto out;
   6719 	}
   6720 
   6721 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
   6722 		if (ibits & (1 << box)) {
   6723 			isp_prt(isp, ISP_LOGDEBUG1, "IN mbox %d = 0x%04x", box,
   6724 			    mbp->param[box]);
   6725 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   6726 		}
   6727 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   6728 	}
   6729 
   6730 	isp->isp_lastmbxcmd = opcode;
   6731 
   6732 	/*
   6733 	 * We assume that we can't overwrite a previous command.
   6734 	 */
   6735 	isp->isp_obits = obits;
   6736 	isp->isp_mboxbsy = 1;
   6737 
   6738 	/*
   6739 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   6740 	 */
   6741 	if (IS_24XX(isp)) {
   6742 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
   6743 	} else {
   6744 		ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   6745 	}
   6746 
   6747 	/*
   6748 	 * While we haven't finished the command, spin our wheels here.
   6749 	 */
   6750 	MBOX_WAIT_COMPLETE(isp, mbp);
   6751 
   6752 	/*
   6753 	 * Did the command time out?
   6754 	 */
   6755 	if (mbp->param[0] == MBOX_TIMEOUT) {
   6756 		MBOX_RELEASE(isp);
   6757 		goto out;
   6758 	}
   6759 
   6760 	/*
   6761 	 * Copy back output registers.
   6762 	 */
   6763 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
   6764 		if (obits & (1 << box)) {
   6765 			mbp->param[box] = isp->isp_mboxtmp[box];
   6766 			isp_prt(isp, ISP_LOGDEBUG1, "OUT mbox %d = 0x%04x", box,
   6767 			    mbp->param[box]);
   6768 		}
   6769 	}
   6770 
   6771 	MBOX_RELEASE(isp);
   6772  out:
   6773 	isp->isp_mboxbsy = 0;
   6774 	if (mbp->logval == 0 || opcode == MBOX_EXEC_FIRMWARE) {
   6775 		return;
   6776 	}
   6777 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
   6778 	if (cname == NULL) {
   6779 		cname = tname;
   6780 		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
   6781 	}
   6782 
   6783 	/*
   6784 	 * Just to be chatty here...
   6785 	 */
   6786 	xname = NULL;
   6787 	switch (mbp->param[0]) {
   6788 	case MBOX_COMMAND_COMPLETE:
   6789 		break;
   6790 	case MBOX_INVALID_COMMAND:
   6791 		if (mbp->logval & MBLOGMASK(MBOX_COMMAND_COMPLETE)) {
   6792 			xname = "INVALID COMMAND";
   6793 		}
   6794 		break;
   6795 	case MBOX_HOST_INTERFACE_ERROR:
   6796 		if (mbp->logval & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR)) {
   6797 			xname = "HOST INTERFACE ERROR";
   6798 		}
   6799 		break;
   6800 	case MBOX_TEST_FAILED:
   6801 		if (mbp->logval & MBLOGMASK(MBOX_TEST_FAILED)) {
   6802 			xname = "TEST FAILED";
   6803 		}
   6804 		break;
   6805 	case MBOX_COMMAND_ERROR:
   6806 		if (mbp->logval & MBLOGMASK(MBOX_COMMAND_ERROR)) {
   6807 			xname = "COMMAND ERROR";
   6808 		}
   6809 		break;
   6810 	case MBOX_COMMAND_PARAM_ERROR:
   6811 		if (mbp->logval & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR)) {
   6812 			xname = "COMMAND PARAMETER ERROR";
   6813 		}
   6814 		break;
   6815 	case MBOX_LOOP_ID_USED:
   6816 		if (mbp->logval & MBLOGMASK(MBOX_LOOP_ID_USED)) {
   6817 			xname = "LOOP ID ALREADY IN USE";
   6818 		}
   6819 		break;
   6820 	case MBOX_PORT_ID_USED:
   6821 		if (mbp->logval & MBLOGMASK(MBOX_PORT_ID_USED)) {
   6822 			xname = "PORT ID ALREADY IN USE";
   6823 		}
   6824 		break;
   6825 	case MBOX_ALL_IDS_USED:
   6826 		if (mbp->logval & MBLOGMASK(MBOX_ALL_IDS_USED)) {
   6827 			xname = "ALL LOOP IDS IN USE";
   6828 		}
   6829 		break;
   6830 	case MBOX_REGS_BUSY:
   6831 		xname = "REGISTERS BUSY";
   6832 		break;
   6833 	case MBOX_TIMEOUT:
   6834 		xname = "TIMEOUT";
   6835 		break;
   6836 	default:
   6837 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
   6838 		xname = mname;
   6839 		break;
   6840 	}
   6841 	if (xname) {
   6842 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
   6843 		    cname, xname);
   6844 	}
   6845 }
   6846 
   6847 static void
   6848 isp_fw_state(ispsoftc_t *isp)
   6849 {
   6850 	if (IS_FC(isp)) {
   6851 		mbreg_t mbs;
   6852 		fcparam *fcp = isp->isp_param;
   6853 
   6854 		MEMZERO(&mbs, sizeof (mbs));
   6855 		mbs.param[0] = MBOX_GET_FW_STATE;
   6856 		mbs.logval = MBLOGALL;
   6857 		isp_mboxcmd(isp, &mbs);
   6858 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   6859 			fcp->isp_fwstate = mbs.param[1];
   6860 		}
   6861 	}
   6862 }
   6863 
   6864 static void
   6865 isp_update(ispsoftc_t *isp)
   6866 {
   6867 	int bus, upmask;
   6868 
   6869 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
   6870 		if (upmask & (1 << bus)) {
   6871 			isp_update_bus(isp, bus);
   6872 		}
   6873 		upmask &= ~(1 << bus);
   6874 	}
   6875 }
   6876 
   6877 static void
   6878 isp_update_bus(ispsoftc_t *isp, int bus)
   6879 {
   6880 	int tgt;
   6881 	mbreg_t mbs;
   6882 	sdparam *sdp;
   6883 
   6884 	isp->isp_update &= ~(1 << bus);
   6885 	if (IS_FC(isp)) {
   6886 		/*
   6887 		 * There are no 'per-bus' settings for Fibre Channel.
   6888 		 */
   6889 		return;
   6890 	}
   6891 	sdp = isp->isp_param;
   6892 	sdp += bus;
   6893 
   6894 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   6895 		uint16_t flags, period, offset;
   6896 		int get;
   6897 
   6898 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   6899 			sdp->isp_devparam[tgt].dev_update = 0;
   6900 			sdp->isp_devparam[tgt].dev_refresh = 0;
   6901 			isp_prt(isp, ISP_LOGDEBUG0,
   6902 	 		    "skipping target %d bus %d update", tgt, bus);
   6903 			continue;
   6904 		}
   6905 		/*
   6906 		 * If the goal is to update the status of the device,
   6907 		 * take what's in goal_flags and try and set the device
   6908 		 * toward that. Otherwise, if we're just refreshing the
   6909 		 * current device state, get the current parameters.
   6910 		 */
   6911 
   6912 		MEMZERO(&mbs, sizeof (mbs));
   6913 
   6914 		/*
   6915 		 * Refresh overrides set
   6916 		 */
   6917 		if (sdp->isp_devparam[tgt].dev_refresh) {
   6918 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   6919 			get = 1;
   6920 		} else if (sdp->isp_devparam[tgt].dev_update) {
   6921 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   6922 
   6923 			/*
   6924 			 * Make sure goal_flags has "Renegotiate on Error"
   6925 			 * on and "Freeze Queue on Error" off.
   6926 			 */
   6927 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
   6928 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
   6929 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
   6930 
   6931 			/*
   6932 			 * Insist that PARITY must be enabled
   6933 			 * if SYNC or WIDE is enabled.
   6934 			 */
   6935 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
   6936 				mbs.param[2] |= DPARM_PARITY;
   6937 			}
   6938 
   6939 			if (mbs.param[2] & DPARM_SYNC) {
   6940 				mbs.param[3] =
   6941 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
   6942 				    (sdp->isp_devparam[tgt].goal_period);
   6943 			}
   6944 			/*
   6945 			 * A command completion later that has
   6946 			 * RQSTF_NEGOTIATION set can cause
   6947 			 * the dev_refresh/announce cycle also.
   6948 			 *
   6949 			 * Note: It is really important to update our current
   6950 			 * flags with at least the state of TAG capabilities-
   6951 			 * otherwise we might try and send a tagged command
   6952 			 * when we have it all turned off. So change it here
   6953 			 * to say that current already matches goal.
   6954 			 */
   6955 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
   6956 			sdp->isp_devparam[tgt].actv_flags |=
   6957 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
   6958 			isp_prt(isp, ISP_LOGDEBUG0,
   6959 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
   6960 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
   6961 			    mbs.param[3] & 0xff);
   6962 			get = 0;
   6963 		} else {
   6964 			continue;
   6965 		}
   6966 		mbs.param[1] = (bus << 15) | (tgt << 8);
   6967 		mbs.logval = MBLOGALL;
   6968 		isp_mboxcmd(isp, &mbs);
   6969 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   6970 			continue;
   6971 		}
   6972 		if (get == 0) {
   6973 			isp->isp_sendmarker |= (1 << bus);
   6974 			sdp->isp_devparam[tgt].dev_update = 0;
   6975 			sdp->isp_devparam[tgt].dev_refresh = 1;
   6976 		} else {
   6977 			sdp->isp_devparam[tgt].dev_refresh = 0;
   6978 			flags = mbs.param[2];
   6979 			period = mbs.param[3] & 0xff;
   6980 			offset = mbs.param[3] >> 8;
   6981 			sdp->isp_devparam[tgt].actv_flags = flags;
   6982 			sdp->isp_devparam[tgt].actv_period = period;
   6983 			sdp->isp_devparam[tgt].actv_offset = offset;
   6984 			get = (bus << 16) | tgt;
   6985 			(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
   6986 		}
   6987 	}
   6988 
   6989 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   6990 		if (sdp->isp_devparam[tgt].dev_update ||
   6991 		    sdp->isp_devparam[tgt].dev_refresh) {
   6992 			isp->isp_update |= (1 << bus);
   6993 			break;
   6994 		}
   6995 	}
   6996 }
   6997 
   6998 #ifndef	DEFAULT_EXEC_THROTTLE
   6999 #define	DEFAULT_EXEC_THROTTLE(isp)	ISP_EXEC_THROTTLE
   7000 #endif
   7001 
   7002 static void
   7003 isp_setdfltparm(ispsoftc_t *isp, int channel)
   7004 {
   7005 	int tgt;
   7006 	sdparam *sdp;
   7007 
   7008 	sdp = (sdparam *) isp->isp_param;
   7009 	sdp += channel;
   7010 
   7011 	/*
   7012 	 * Been there, done that, got the T-shirt...
   7013 	 */
   7014 	if (sdp->isp_gotdparms) {
   7015 		return;
   7016 	}
   7017 	sdp->isp_gotdparms = 1;
   7018 	sdp->isp_bad_nvram = 0;
   7019 	/*
   7020 	 * Establish some default parameters.
   7021 	 */
   7022 	sdp->isp_cmd_dma_burst_enable = 0;
   7023 	sdp->isp_data_dma_burst_enabl = 1;
   7024 	sdp->isp_fifo_threshold = 0;
   7025 	sdp->isp_initiator_id = DEFAULT_IID(isp);
   7026 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   7027 		sdp->isp_async_data_setup = 9;
   7028 	} else {
   7029 		sdp->isp_async_data_setup = 6;
   7030 	}
   7031 	sdp->isp_selection_timeout = 250;
   7032 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
   7033 	sdp->isp_tag_aging = 8;
   7034 	sdp->isp_bus_reset_delay = 5;
   7035 	/*
   7036 	 * Don't retry selection, busy or queue full automatically- reflect
   7037 	 * these back to us.
   7038 	 */
   7039 	sdp->isp_retry_count = 0;
   7040 	sdp->isp_retry_delay = 0;
   7041 
   7042 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   7043 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
   7044 		sdp->isp_devparam[tgt].dev_enable = 1;
   7045 	}
   7046 
   7047 	/*
   7048 	 * If we've not been told to avoid reading NVRAM, try and read it.
   7049 	 * If we're successful reading it, we can then return because NVRAM
   7050 	 * will tell us what the desired settings are. Otherwise, we establish
   7051 	 * some reasonable 'fake' nvram and goal defaults.
   7052 	 */
   7053 
   7054 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   7055 		if (isp_read_nvram(isp) == 0) {
   7056 			return;
   7057 		}
   7058 		sdp->isp_bad_nvram = 1;
   7059 	}
   7060 
   7061 	/*
   7062 	 * Now try and see whether we have specific values for them.
   7063 	 */
   7064 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   7065 		mbreg_t mbs;
   7066 
   7067 		MEMZERO(&mbs, sizeof (mbs));
   7068 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   7069 		mbs.logval = MBLOGNONE;
   7070 		isp_mboxcmd(isp, &mbs);
   7071 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   7072 			sdp->isp_req_ack_active_neg = 1;
   7073 			sdp->isp_data_line_active_neg = 1;
   7074 		} else {
   7075 			sdp->isp_req_ack_active_neg =
   7076 			    (mbs.param[1+channel] >> 4) & 0x1;
   7077 			sdp->isp_data_line_active_neg =
   7078 			    (mbs.param[1+channel] >> 5) & 0x1;
   7079 		}
   7080 	}
   7081 
   7082 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
   7083 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   7084 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   7085 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   7086 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
   7087 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   7088 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   7089 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   7090 
   7091 	/*
   7092 	 * The trick here is to establish a default for the default (honk!)
   7093 	 * state (goal_flags). Then try and get the current status from
   7094 	 * the card to fill in the current state. We don't, in fact, set
   7095 	 * the default to the SAFE default state- that's not the goal state.
   7096 	 */
   7097 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   7098 		uint8_t off, per;
   7099 		sdp->isp_devparam[tgt].actv_offset = 0;
   7100 		sdp->isp_devparam[tgt].actv_period = 0;
   7101 		sdp->isp_devparam[tgt].actv_flags = 0;
   7102 
   7103 		sdp->isp_devparam[tgt].goal_flags =
   7104 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
   7105 
   7106 		/*
   7107 		 * We default to Wide/Fast for versions less than a 1040
   7108 		 * (unless it's SBus).
   7109 		 */
   7110 		if (IS_ULTRA3(isp)) {
   7111 			off = ISP_80M_SYNCPARMS >> 8;
   7112 			per = ISP_80M_SYNCPARMS & 0xff;
   7113 		} else if (IS_ULTRA2(isp)) {
   7114 			off = ISP_40M_SYNCPARMS >> 8;
   7115 			per = ISP_40M_SYNCPARMS & 0xff;
   7116 		} else if (IS_1240(isp)) {
   7117 			off = ISP_20M_SYNCPARMS >> 8;
   7118 			per = ISP_20M_SYNCPARMS & 0xff;
   7119 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
   7120 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
   7121 		    (isp->isp_bustype == ISP_BT_PCI &&
   7122 		    isp->isp_type < ISP_HA_SCSI_1040) ||
   7123 		    (isp->isp_clock && isp->isp_clock < 60) ||
   7124 		    (sdp->isp_ultramode == 0)) {
   7125 			off = ISP_10M_SYNCPARMS >> 8;
   7126 			per = ISP_10M_SYNCPARMS & 0xff;
   7127 		} else {
   7128 			off = ISP_20M_SYNCPARMS_1040 >> 8;
   7129 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
   7130 		}
   7131 		sdp->isp_devparam[tgt].goal_offset =
   7132 		    sdp->isp_devparam[tgt].nvrm_offset = off;
   7133 		sdp->isp_devparam[tgt].goal_period =
   7134 		    sdp->isp_devparam[tgt].nvrm_period = per;
   7135 
   7136 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
   7137 		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   7138 		    sdp->isp_devparam[tgt].nvrm_offset,
   7139 		    sdp->isp_devparam[tgt].nvrm_period);
   7140 	}
   7141 }
   7142 
   7143 #ifndef	DEFAULT_FRAMESIZE
   7144 #define	DEFAULT_FRAMESIZE(isp)		ICB_DFLT_FRMLEN
   7145 #endif
   7146 static void
   7147 isp_setdfltfcparm(ispsoftc_t *isp)
   7148 {
   7149 	fcparam *fcp = FCPARAM(isp);
   7150 
   7151 	if (fcp->isp_gotdparms) {
   7152 		return;
   7153 	}
   7154 	fcp->isp_gotdparms = 1;
   7155 	fcp->isp_bad_nvram = 0;
   7156 	fcp->isp_maxfrmlen = DEFAULT_FRAMESIZE(isp);
   7157 	fcp->isp_maxalloc = ICB_DFLT_ALLOC;
   7158 	fcp->isp_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
   7159 	fcp->isp_retry_delay = ICB_DFLT_RDELAY;
   7160 	fcp->isp_retry_count = ICB_DFLT_RCOUNT;
   7161 	/* Platform specific.... */
   7162 	fcp->isp_loopid = DEFAULT_LOOPID(isp);
   7163 	fcp->isp_wwnn_nvram = DEFAULT_NODEWWN(isp);
   7164 	fcp->isp_wwpn_nvram = DEFAULT_PORTWWN(isp);
   7165 	fcp->isp_fwoptions = 0;
   7166 	fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
   7167 	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
   7168 	fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
   7169 	fcp->isp_fwoptions |= ICBOPT_FAST_POST;
   7170 	if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
   7171 		fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
   7172 	}
   7173 
   7174 	/*
   7175 	 * Make sure this is turned off now until we get
   7176 	 * extended options from NVRAM
   7177 	 */
   7178 	fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
   7179 
   7180 	/*
   7181 	 * Now try and read NVRAM unless told to not do so.
   7182 	 * This will set fcparam's isp_wwnn_nvram && isp_wwpn_nvram.
   7183 	 */
   7184 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   7185 		int i, j = 0;
   7186 		/*
   7187 		 * Give a couple of tries at reading NVRAM.
   7188 		 */
   7189 		for (i = 0; i < 2; i++) {
   7190 			j = isp_read_nvram(isp);
   7191 			if (j == 0) {
   7192 				break;
   7193 			}
   7194 		}
   7195 		if (j) {
   7196 			fcp->isp_bad_nvram = 1;
   7197 			isp->isp_confopts |= ISP_CFG_NONVRAM;
   7198 			isp->isp_confopts |= ISP_CFG_OWNWWPN;
   7199 			isp->isp_confopts |= ISP_CFG_OWNWWNN;
   7200 		}
   7201 	} else {
   7202 		isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
   7203 	}
   7204 
   7205 	/*
   7206 	 * Set node && port to override platform set defaults
   7207 	 * unless the nvram read failed (or none was done),
   7208 	 * or the platform code wants to use what had been
   7209 	 * set in the defaults.
   7210 	 */
   7211 	if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
   7212 		isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
   7213 		    (uint32_t) (DEFAULT_NODEWWN(isp) >> 32),
   7214 		    (uint32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
   7215 		ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
   7216 	} else {
   7217 		/*
   7218 		 * We always start out with values derived
   7219 		 * from NVRAM or our platform default.
   7220 		 */
   7221 		ISP_NODEWWN(isp) = fcp->isp_wwnn_nvram;
   7222 		if (fcp->isp_wwnn_nvram == 0) {
   7223 			isp_prt(isp, ISP_LOGCONFIG,
   7224 			    "bad WWNN- using default");
   7225 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
   7226 		}
   7227 	}
   7228 	if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
   7229 		isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
   7230 		    (uint32_t) (DEFAULT_PORTWWN(isp) >> 32),
   7231 		    (uint32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
   7232 		ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
   7233 	} else {
   7234 		/*
   7235 		 * We always start out with values derived
   7236 		 * from NVRAM or our platform default.
   7237 		 */
   7238 		ISP_PORTWWN(isp) = fcp->isp_wwpn_nvram;
   7239 		if (fcp->isp_wwpn_nvram == 0) {
   7240 			isp_prt(isp, ISP_LOGCONFIG,
   7241 			    "bad WWPN- using default");
   7242 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
   7243 		}
   7244 	}
   7245 }
   7246 
   7247 /*
   7248  * Re-initialize the ISP and complete all orphaned commands
   7249  * with a 'botched' notice. The reset/init routines should
   7250  * not disturb an already active list of commands.
   7251  */
   7252 
   7253 void
   7254 isp_reinit(ispsoftc_t *isp)
   7255 {
   7256 	XS_T *xs;
   7257 	uint32_t tmp;
   7258 
   7259 	if (IS_FC(isp)) {
   7260 		ISP_MARK_PORTDB(isp, 0);
   7261 	}
   7262 	isp_reset(isp);
   7263 	if (isp->isp_state != ISP_RESETSTATE) {
   7264 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
   7265 	} else if (isp->isp_role != ISP_ROLE_NONE) {
   7266 		isp_init(isp);
   7267 		if (isp->isp_state == ISP_INITSTATE) {
   7268 			isp->isp_state = ISP_RUNSTATE;
   7269 		}
   7270 		if (isp->isp_state != ISP_RUNSTATE) {
   7271 			isp_prt(isp, ISP_LOGERR,
   7272 			    "isp_reinit cannot restart card");
   7273 			ISP_DISABLE_INTS(isp);
   7274 		}
   7275 	} else {
   7276 		ISP_DISABLE_INTS(isp);
   7277 		if (IS_FC(isp)) {
   7278 			/*
   7279 			 * If we're in ISP_ROLE_NONE, turn off the lasers.
   7280 			 */
   7281 			if (!IS_24XX(isp)) {
   7282 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
   7283 				ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
   7284 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
   7285 				ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
   7286 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
   7287 			}
   7288 		}
   7289  	}
   7290 	isp->isp_nactive = 0;
   7291 
   7292 	for (tmp = 0; tmp < isp->isp_maxcmds; tmp++) {
   7293 		uint32_t handle;
   7294 
   7295 		xs = isp->isp_xflist[tmp];
   7296 		if (xs == NULL) {
   7297 			continue;
   7298 		}
   7299 		handle = isp_find_handle(isp, xs);
   7300 		if (handle == 0) {
   7301 			continue;
   7302 		}
   7303 		isp_destroy_handle(isp, handle);
   7304 		if (XS_XFRLEN(xs)) {
   7305 			ISP_DMAFREE(isp, xs, handle);
   7306 			XS_RESID(xs) = XS_XFRLEN(xs);
   7307 		} else {
   7308 			XS_RESID(xs) = 0;
   7309 		}
   7310 		XS_SETERR(xs, HBA_BUSRESET);
   7311 		isp_done(xs);
   7312 	}
   7313 #ifdef	ISP_TARGET_MODE
   7314 	MEMZERO(isp->isp_tgtlist, isp->isp_maxcmds * sizeof (void **));
   7315 #endif
   7316 }
   7317 
   7318 /*
   7319  * NVRAM Routines
   7320  */
   7321 static int
   7322 isp_read_nvram(ispsoftc_t *isp)
   7323 {
   7324 	int i, amt, retval;
   7325 	uint8_t csum, minversion;
   7326 	union {
   7327 		uint8_t _x[ISP2100_NVRAM_SIZE];
   7328 		uint16_t _s[ISP2100_NVRAM_SIZE>>1];
   7329 	} _n;
   7330 #define	nvram_data	_n._x
   7331 #define	nvram_words	_n._s
   7332 
   7333 	if (IS_24XX(isp)) {
   7334 		return (isp_read_nvram_2400(isp));
   7335 	} else if (IS_FC(isp)) {
   7336 		amt = ISP2100_NVRAM_SIZE;
   7337 		minversion = 1;
   7338 	} else if (IS_ULTRA2(isp)) {
   7339 		amt = ISP1080_NVRAM_SIZE;
   7340 		minversion = 0;
   7341 	} else {
   7342 		amt = ISP_NVRAM_SIZE;
   7343 		minversion = 2;
   7344 	}
   7345 
   7346 	for (i = 0; i < amt>>1; i++) {
   7347 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   7348 	}
   7349 
   7350 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   7351 	    nvram_data[2] != 'P') {
   7352 		if (isp->isp_bustype != ISP_BT_SBUS) {
   7353 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
   7354 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
   7355 			    nvram_data[0], nvram_data[1], nvram_data[2]);
   7356 		}
   7357 		retval = -1;
   7358 		goto out;
   7359 	}
   7360 
   7361 	for (csum = 0, i = 0; i < amt; i++) {
   7362 		csum += nvram_data[i];
   7363 	}
   7364 	if (csum != 0) {
   7365 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
   7366 		retval = -1;
   7367 		goto out;
   7368 	}
   7369 
   7370 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   7371 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
   7372 		    ISP_NVRAM_VERSION(nvram_data));
   7373 		retval = -1;
   7374 		goto out;
   7375 	}
   7376 
   7377 	if (IS_ULTRA3(isp)) {
   7378 		isp_parse_nvram_12160(isp, 0, nvram_data);
   7379 		if (IS_12160(isp))
   7380 			isp_parse_nvram_12160(isp, 1, nvram_data);
   7381 	} else if (IS_1080(isp)) {
   7382 		isp_parse_nvram_1080(isp, 0, nvram_data);
   7383 	} else if (IS_1280(isp) || IS_1240(isp)) {
   7384 		isp_parse_nvram_1080(isp, 0, nvram_data);
   7385 		isp_parse_nvram_1080(isp, 1, nvram_data);
   7386 	} else if (IS_SCSI(isp)) {
   7387 		isp_parse_nvram_1020(isp, nvram_data);
   7388 	} else {
   7389 		isp_parse_nvram_2100(isp, nvram_data);
   7390 	}
   7391 	retval = 0;
   7392 out:
   7393 	return (retval);
   7394 #undef	nvram_data
   7395 #undef	nvram_words
   7396 }
   7397 
   7398 static int
   7399 isp_read_nvram_2400(ispsoftc_t *isp)
   7400 {
   7401 	uint8_t *nvram_data = FCPARAM(isp)->isp_scratch;
   7402 	int retval = 0;
   7403 	uint32_t addr, csum, lwrds, *dptr;
   7404 
   7405 	if (isp->isp_port) {
   7406 		addr = ISP2400_NVRAM_PORT1_ADDR;
   7407 	} else {
   7408 		addr = ISP2400_NVRAM_PORT0_ADDR;
   7409 	}
   7410 
   7411 	dptr = (uint32_t *) nvram_data;
   7412 	for (lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
   7413 		isp_rd_2400_nvram(isp, addr++, dptr++);
   7414 	}
   7415 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   7416 	    nvram_data[2] != 'P') {
   7417 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
   7418 		retval = -1;
   7419 		goto out;
   7420 	}
   7421 	dptr = (uint32_t *) nvram_data;
   7422 	for (csum = 0, lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
   7423 		csum += dptr[lwrds];
   7424 	}
   7425 	if (csum != 0) {
   7426 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
   7427 		retval = -1;
   7428 		goto out;
   7429 	}
   7430 	isp_parse_nvram_2400(isp, nvram_data);
   7431 out:
   7432 	return (retval);
   7433 }
   7434 
   7435 static void
   7436 isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
   7437 {
   7438 	int i, cbits;
   7439 	uint16_t bit, rqst, junk;
   7440 
   7441 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   7442 	USEC_DELAY(10);
   7443 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   7444 	USEC_DELAY(10);
   7445 
   7446 	if (IS_FC(isp)) {
   7447 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   7448 		if (IS_2312(isp) && isp->isp_port) {
   7449 			wo += 128;
   7450 		}
   7451 		rqst = (ISP_NVRAM_READ << 8) | wo;
   7452 		cbits = 10;
   7453 	} else if (IS_ULTRA2(isp)) {
   7454 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
   7455 		rqst = (ISP_NVRAM_READ << 8) | wo;
   7456 		cbits = 10;
   7457 	} else {
   7458 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   7459 		rqst = (ISP_NVRAM_READ << 6) | wo;
   7460 		cbits = 8;
   7461 	}
   7462 
   7463 	/*
   7464 	 * Clock the word select request out...
   7465 	 */
   7466 	for (i = cbits; i >= 0; i--) {
   7467 		if ((rqst >> i) & 1) {
   7468 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   7469 		} else {
   7470 			bit = BIU_NVRAM_SELECT;
   7471 		}
   7472 		ISP_WRITE(isp, BIU_NVRAM, bit);
   7473 		USEC_DELAY(10);
   7474 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
   7475 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   7476 		USEC_DELAY(10);
   7477 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
   7478 		ISP_WRITE(isp, BIU_NVRAM, bit);
   7479 		USEC_DELAY(10);
   7480 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
   7481 	}
   7482 	/*
   7483 	 * Now read the result back in (bits come back in MSB format).
   7484 	 */
   7485 	*rp = 0;
   7486 	for (i = 0; i < 16; i++) {
   7487 		uint16_t rv;
   7488 		*rp <<= 1;
   7489 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   7490 		USEC_DELAY(10);
   7491 		rv = ISP_READ(isp, BIU_NVRAM);
   7492 		if (rv & BIU_NVRAM_DATAIN) {
   7493 			*rp |= 1;
   7494 		}
   7495 		USEC_DELAY(10);
   7496 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   7497 		USEC_DELAY(10);
   7498 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
   7499 	}
   7500 	ISP_WRITE(isp, BIU_NVRAM, 0);
   7501 	USEC_DELAY(10);
   7502 	junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
   7503 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
   7504 }
   7505 
   7506 static void
   7507 isp_rd_2400_nvram(ispsoftc_t *isp, uint32_t addr, uint32_t *rp)
   7508 {
   7509 	int loops = 0;
   7510 	const uint32_t base = 0x7ffe0000;
   7511 	uint32_t tmp = 0;
   7512 
   7513 	ISP_WRITE(isp, BIU2400_FLASH_ADDR, base | addr);
   7514 	for (loops = 0; loops < 5000; loops++) {
   7515 		USEC_DELAY(10);
   7516 		tmp = ISP_READ(isp, BIU2400_FLASH_ADDR);
   7517 		if ((tmp & (1U << 31)) != 0) {
   7518 			break;
   7519 		}
   7520 	}
   7521 	if (tmp & (1U << 31)) {
   7522 		tmp = ISP_READ(isp, BIU2400_FLASH_DATA);
   7523 		*rp = tmp;
   7524 	} else {
   7525 		*rp = 0xffffffff;
   7526 	}
   7527 }
   7528 
   7529 static void
   7530 isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
   7531 {
   7532 	sdparam *sdp = (sdparam *) isp->isp_param;
   7533 	int tgt;
   7534 
   7535 	sdp->isp_fifo_threshold =
   7536 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
   7537 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
   7538 
   7539 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
   7540 		sdp->isp_initiator_id =
   7541 			ISP_NVRAM_INITIATOR_ID(nvram_data);
   7542 
   7543 	sdp->isp_bus_reset_delay =
   7544 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   7545 
   7546 	sdp->isp_retry_count =
   7547 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   7548 
   7549 	sdp->isp_retry_delay =
   7550 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   7551 
   7552 	sdp->isp_async_data_setup =
   7553 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   7554 
   7555 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   7556 		if (sdp->isp_async_data_setup < 9) {
   7557 			sdp->isp_async_data_setup = 9;
   7558 		}
   7559 	} else {
   7560 		if (sdp->isp_async_data_setup != 6) {
   7561 			sdp->isp_async_data_setup = 6;
   7562 		}
   7563 	}
   7564 
   7565 	sdp->isp_req_ack_active_neg =
   7566 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   7567 
   7568 	sdp->isp_data_line_active_neg =
   7569 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   7570 
   7571 	sdp->isp_data_dma_burst_enabl =
   7572 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   7573 
   7574 	sdp->isp_cmd_dma_burst_enable =
   7575 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   7576 
   7577 	sdp->isp_tag_aging =
   7578 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   7579 
   7580 	sdp->isp_selection_timeout =
   7581 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   7582 
   7583 	sdp->isp_max_queue_depth =
   7584 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   7585 
   7586 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   7587 
   7588 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   7589 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   7590 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   7591 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   7592 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   7593 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   7594 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   7595 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   7596 
   7597 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   7598 		sdp->isp_devparam[tgt].dev_enable =
   7599 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
   7600 		sdp->isp_devparam[tgt].exc_throttle =
   7601 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
   7602 		sdp->isp_devparam[tgt].nvrm_offset =
   7603 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
   7604 		sdp->isp_devparam[tgt].nvrm_period =
   7605 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
   7606 		/*
   7607 		 * We probably shouldn't lie about this, but it
   7608 		 * it makes it much safer if we limit NVRAM values
   7609 		 * to sanity.
   7610 		 */
   7611 		if (isp->isp_type < ISP_HA_SCSI_1040) {
   7612 			/*
   7613 			 * If we're not ultra, we can't possibly
   7614 			 * be a shorter period than this.
   7615 			 */
   7616 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
   7617 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
   7618 			}
   7619 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
   7620 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
   7621 			}
   7622 		} else {
   7623 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
   7624 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
   7625 			}
   7626 		}
   7627 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   7628 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
   7629 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   7630 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   7631 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
   7632 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   7633 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
   7634 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   7635 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
   7636 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   7637 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
   7638 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   7639 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
   7640 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   7641 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
   7642 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   7643 		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   7644 		    sdp->isp_devparam[tgt].nvrm_offset,
   7645 		    sdp->isp_devparam[tgt].nvrm_period);
   7646 		sdp->isp_devparam[tgt].goal_offset =
   7647 		    sdp->isp_devparam[tgt].nvrm_offset;
   7648 		sdp->isp_devparam[tgt].goal_period =
   7649 		    sdp->isp_devparam[tgt].nvrm_period;
   7650 		sdp->isp_devparam[tgt].goal_flags =
   7651 		    sdp->isp_devparam[tgt].nvrm_flags;
   7652 	}
   7653 }
   7654 
   7655 static void
   7656 isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
   7657 {
   7658 	sdparam *sdp = (sdparam *) isp->isp_param;
   7659 	int tgt;
   7660 
   7661 	sdp += bus;
   7662 
   7663 	sdp->isp_fifo_threshold =
   7664 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
   7665 
   7666 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
   7667 		sdp->isp_initiator_id =
   7668 		    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
   7669 
   7670 	sdp->isp_bus_reset_delay =
   7671 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   7672 
   7673 	sdp->isp_retry_count =
   7674 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   7675 
   7676 	sdp->isp_retry_delay =
   7677 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   7678 
   7679 	sdp->isp_async_data_setup =
   7680 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   7681 
   7682 	sdp->isp_req_ack_active_neg =
   7683 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   7684 
   7685 	sdp->isp_data_line_active_neg =
   7686 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   7687 
   7688 	sdp->isp_data_dma_burst_enabl =
   7689 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   7690 
   7691 	sdp->isp_cmd_dma_burst_enable =
   7692 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   7693 
   7694 	sdp->isp_selection_timeout =
   7695 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   7696 
   7697 	sdp->isp_max_queue_depth =
   7698 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   7699 
   7700 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   7701 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   7702 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   7703 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   7704 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   7705 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   7706 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   7707 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   7708 
   7709 
   7710 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   7711 		sdp->isp_devparam[tgt].dev_enable =
   7712 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   7713 		sdp->isp_devparam[tgt].exc_throttle =
   7714 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   7715 		sdp->isp_devparam[tgt].nvrm_offset =
   7716 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   7717 		sdp->isp_devparam[tgt].nvrm_period =
   7718 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   7719 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   7720 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   7721 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   7722 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   7723 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   7724 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   7725 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   7726 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   7727 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   7728 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   7729 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   7730 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   7731 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   7732 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   7733 		sdp->isp_devparam[tgt].actv_flags = 0;
   7734 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   7735 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   7736 		    sdp->isp_devparam[tgt].nvrm_offset,
   7737 		    sdp->isp_devparam[tgt].nvrm_period);
   7738 		sdp->isp_devparam[tgt].goal_offset =
   7739 		    sdp->isp_devparam[tgt].nvrm_offset;
   7740 		sdp->isp_devparam[tgt].goal_period =
   7741 		    sdp->isp_devparam[tgt].nvrm_period;
   7742 		sdp->isp_devparam[tgt].goal_flags =
   7743 		    sdp->isp_devparam[tgt].nvrm_flags;
   7744 	}
   7745 }
   7746 
   7747 static void
   7748 isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
   7749 {
   7750 	sdparam *sdp = (sdparam *) isp->isp_param;
   7751 	int tgt;
   7752 
   7753 	sdp += bus;
   7754 
   7755 	sdp->isp_fifo_threshold =
   7756 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
   7757 
   7758 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
   7759 		sdp->isp_initiator_id =
   7760 		    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
   7761 
   7762 	sdp->isp_bus_reset_delay =
   7763 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   7764 
   7765 	sdp->isp_retry_count =
   7766 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   7767 
   7768 	sdp->isp_retry_delay =
   7769 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   7770 
   7771 	sdp->isp_async_data_setup =
   7772 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
   7773 
   7774 	sdp->isp_req_ack_active_neg =
   7775 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
   7776 
   7777 	sdp->isp_data_line_active_neg =
   7778 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
   7779 
   7780 	sdp->isp_data_dma_burst_enabl =
   7781 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   7782 
   7783 	sdp->isp_cmd_dma_burst_enable =
   7784 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   7785 
   7786 	sdp->isp_selection_timeout =
   7787 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   7788 
   7789 	sdp->isp_max_queue_depth =
   7790 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   7791 
   7792 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
   7793 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
   7794 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
   7795 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
   7796 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
   7797 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
   7798 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
   7799 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
   7800 
   7801 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   7802 		sdp->isp_devparam[tgt].dev_enable =
   7803 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
   7804 		sdp->isp_devparam[tgt].exc_throttle =
   7805 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
   7806 		sdp->isp_devparam[tgt].nvrm_offset =
   7807 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
   7808 		sdp->isp_devparam[tgt].nvrm_period =
   7809 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
   7810 		sdp->isp_devparam[tgt].nvrm_flags = 0;
   7811 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
   7812 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
   7813 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
   7814 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
   7815 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
   7816 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
   7817 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
   7818 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
   7819 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
   7820 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
   7821 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
   7822 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
   7823 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
   7824 		sdp->isp_devparam[tgt].actv_flags = 0;
   7825 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
   7826 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
   7827 		    sdp->isp_devparam[tgt].nvrm_offset,
   7828 		    sdp->isp_devparam[tgt].nvrm_period);
   7829 		sdp->isp_devparam[tgt].goal_offset =
   7830 		    sdp->isp_devparam[tgt].nvrm_offset;
   7831 		sdp->isp_devparam[tgt].goal_period =
   7832 		    sdp->isp_devparam[tgt].nvrm_period;
   7833 		sdp->isp_devparam[tgt].goal_flags =
   7834 		    sdp->isp_devparam[tgt].nvrm_flags;
   7835 	}
   7836 }
   7837 
   7838 static void
   7839 isp_fix_nvram_wwns(ispsoftc_t *isp)
   7840 {
   7841 	fcparam *fcp = FCPARAM(isp);
   7842 
   7843 	/*
   7844 	 * Make sure we have both Node and Port as non-zero values.
   7845 	 */
   7846 	if (fcp->isp_wwnn_nvram != 0 && fcp->isp_wwpn_nvram == 0) {
   7847 		fcp->isp_wwpn_nvram = fcp->isp_wwnn_nvram;
   7848 	} else if (fcp->isp_wwnn_nvram == 0 && fcp->isp_wwpn_nvram != 0) {
   7849 		fcp->isp_wwnn_nvram = fcp->isp_wwpn_nvram;
   7850 	}
   7851 
   7852 	/*
   7853 	 * Make the Node and Port values sane if they're NAA == 2.
   7854 	 * This means to clear bits 48..56 for the Node WWN and
   7855 	 * make sure that there's some non-zero value in 48..56
   7856 	 * for the Port WWN.
   7857 	 */
   7858 	if (fcp->isp_wwnn_nvram && fcp->isp_wwpn_nvram) {
   7859 		if ((fcp->isp_wwnn_nvram & (((uint64_t) 0xfff) << 48)) != 0 &&
   7860 		    (fcp->isp_wwnn_nvram >> 60) == 2) {
   7861 			fcp->isp_wwnn_nvram &= ~((uint64_t) 0xfff << 48);
   7862 		}
   7863 		if ((fcp->isp_wwpn_nvram & (((uint64_t) 0xfff) << 48)) == 0 &&
   7864 		    (fcp->isp_wwpn_nvram >> 60) == 2) {
   7865 			fcp->isp_wwpn_nvram |= ((uint64_t) 1 << 56);
   7866 		}
   7867 	}
   7868 }
   7869 
   7870 static void
   7871 isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
   7872 {
   7873 	fcparam *fcp = FCPARAM(isp);
   7874 	uint64_t wwn;
   7875 
   7876 	/*
   7877 	 * There is NVRAM storage for both Port and Node entities-
   7878 	 * but the Node entity appears to be unused on all the cards
   7879 	 * I can find. However, we should account for this being set
   7880 	 * at some point in the future.
   7881 	 *
   7882 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
   7883 	 * bits 48..60. In the case of the 2202, it appears that they do
   7884 	 * use bit 48 to distinguish between the two instances on the card.
   7885 	 * The 2204, which I've never seen, *probably* extends this method.
   7886 	 */
   7887 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
   7888 	if (wwn) {
   7889 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
   7890 		    (uint32_t) (wwn >> 32), (uint32_t) (wwn & 0xffffffff));
   7891 		if ((wwn >> 60) == 0) {
   7892 			wwn |= (((uint64_t) 2)<< 60);
   7893 		}
   7894 	}
   7895 	fcp->isp_wwpn_nvram = wwn;
   7896 	if (IS_2200(isp) || IS_23XX(isp)) {
   7897 		wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
   7898 		if (wwn) {
   7899 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
   7900 			    (uint32_t) (wwn >> 32),
   7901 			    (uint32_t) (wwn & 0xffffffff));
   7902 			if ((wwn >> 60) == 0) {
   7903 				wwn |= (((uint64_t) 2)<< 60);
   7904 			}
   7905 		}
   7906 	} else {
   7907 		wwn &= ~((uint64_t) 0xfff << 48);
   7908 	}
   7909 	fcp->isp_wwnn_nvram = wwn;
   7910 
   7911 	isp_fix_nvram_wwns(isp);
   7912 
   7913 	fcp->isp_maxalloc = ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   7914 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
   7915 		fcp->isp_maxfrmlen = ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   7916 	}
   7917 	fcp->isp_retry_delay = ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   7918 	fcp->isp_retry_count = ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   7919 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
   7920 		fcp->isp_loopid = ISP2100_NVRAM_HARDLOOPID(nvram_data);
   7921 	}
   7922 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
   7923 		fcp->isp_execthrottle =
   7924 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   7925 	}
   7926 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
   7927 	isp_prt(isp, ISP_LOGDEBUG0,
   7928 	    "NVRAM 0x%08x%08x 0x%08x%08x maxalloc %d maxframelen %d",
   7929 	    (uint32_t) (fcp->isp_wwnn_nvram >> 32), (uint32_t) fcp->isp_wwnn_nvram,
   7930 	    (uint32_t) (fcp->isp_wwpn_nvram >> 32), (uint32_t) fcp->isp_wwpn_nvram,
   7931 	    ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data),
   7932 	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data));
   7933 	isp_prt(isp, ISP_LOGDEBUG0,
   7934 	    "execthrottle %d fwoptions 0x%x hardloop %d tov %d",
   7935 	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
   7936 	    ISP2100_NVRAM_OPTIONS(nvram_data),
   7937 	    ISP2100_NVRAM_HARDLOOPID(nvram_data),
   7938 	    ISP2100_NVRAM_TOV(nvram_data));
   7939 	fcp->isp_xfwoptions = ISP2100_XFW_OPTIONS(nvram_data);
   7940 	fcp->isp_zfwoptions = ISP2100_ZFW_OPTIONS(nvram_data);
   7941 	isp_prt(isp, ISP_LOGDEBUG0,
   7942 	    "xfwoptions 0x%x zfw options 0x%x",
   7943 	    ISP2100_XFW_OPTIONS(nvram_data), ISP2100_ZFW_OPTIONS(nvram_data));
   7944 }
   7945 
   7946 static void
   7947 isp_parse_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
   7948 {
   7949 	fcparam *fcp = FCPARAM(isp);
   7950 	uint64_t wwn;
   7951 
   7952 	isp_prt(isp, ISP_LOGDEBUG0,
   7953 	    "NVRAM 0x%08x%08x 0x%08x%08x exchg_cnt %d maxframelen %d",
   7954 	    (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data) >> 32),
   7955 	    (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data)),
   7956 	    (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data) >> 32),
   7957 	    (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data)),
   7958 	    ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data),
   7959 	    ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data));
   7960 	isp_prt(isp, ISP_LOGDEBUG0,
   7961 	    "NVRAM execthr %d loopid %d fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x",
   7962 	    ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data),
   7963 	    ISP2400_NVRAM_HARDLOOPID(nvram_data),
   7964 	    ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data),
   7965 	    ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data),
   7966 	    ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data));
   7967 
   7968 	wwn = ISP2400_NVRAM_PORT_NAME(nvram_data);
   7969 	if (wwn) {
   7970 		if ((wwn >> 60) != 2 && (wwn >> 60) != 5) {
   7971 			wwn = 0;
   7972 		}
   7973 	}
   7974 	fcp->isp_wwpn_nvram = wwn;
   7975 
   7976 	wwn = ISP2400_NVRAM_NODE_NAME(nvram_data);
   7977 	if (wwn) {
   7978 		if ((wwn >> 60) != 2 && (wwn >> 60) != 5) {
   7979 			wwn = 0;
   7980 		}
   7981 	}
   7982 	fcp->isp_wwnn_nvram = wwn;
   7983 
   7984 	isp_fix_nvram_wwns(isp);
   7985 
   7986 	if (ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data)) {
   7987 		fcp->isp_maxalloc = ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data);
   7988 	}
   7989 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
   7990 		fcp->isp_maxfrmlen = ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data);
   7991 	}
   7992 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
   7993 		fcp->isp_loopid = ISP2400_NVRAM_HARDLOOPID(nvram_data);
   7994 	}
   7995 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
   7996 		fcp->isp_execthrottle =
   7997 			ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data);
   7998 	}
   7999 	fcp->isp_fwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data);
   8000 	fcp->isp_xfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data);
   8001 	fcp->isp_zfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data);
   8002 }
   8003 
   8004 #ifdef	ISP_FW_CRASH_DUMP
   8005 static void isp2200_fw_dump(ispsoftc_t *);
   8006 static void isp2300_fw_dump(ispsoftc_t *);
   8007 
   8008 static void
   8009 isp2200_fw_dump(ispsoftc_t *isp)
   8010 {
   8011 	int i, j;
   8012 	mbreg_t mbs;
   8013 	uint16_t *ptr;
   8014 
   8015 	MEMZERO(&mbs, sizeof (mbs));
   8016 	ptr = FCPARAM(isp)->isp_dump_data;
   8017 	if (ptr == NULL) {
   8018 		isp_prt(isp, ISP_LOGERR,
   8019 		   "No place to dump RISC registers and SRAM");
   8020 		return;
   8021 	}
   8022 	if (*ptr++) {
   8023 		isp_prt(isp, ISP_LOGERR,
   8024 		   "dump area for RISC registers and SRAM already used");
   8025 		return;
   8026 	}
   8027 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   8028 	for (i = 0; i < 100; i++) {
   8029 		USEC_DELAY(100);
   8030 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   8031 			break;
   8032 		}
   8033 	}
   8034 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   8035 		/*
   8036 		 * PBIU Registers
   8037 		 */
   8038 		for (i = 0; i < 8; i++) {
   8039 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
   8040 		}
   8041 
   8042 		/*
   8043 		 * Mailbox Registers
   8044 		 */
   8045 		for (i = 0; i < 8; i++) {
   8046 			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
   8047 		}
   8048 
   8049 		/*
   8050 		 * DMA Registers
   8051 		 */
   8052 		for (i = 0; i < 48; i++) {
   8053 			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
   8054 		}
   8055 
   8056 		/*
   8057 		 * RISC H/W Registers
   8058 		 */
   8059 		ISP_WRITE(isp, BIU2100_CSR, 0);
   8060 		for (i = 0; i < 16; i++) {
   8061 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
   8062 		}
   8063 
   8064 		/*
   8065 		 * RISC GP Registers
   8066 		 */
   8067 		for (j = 0; j < 8; j++) {
   8068 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
   8069 			for (i = 0; i < 16; i++) {
   8070 				*ptr++ =
   8071 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8072 			}
   8073 		}
   8074 
   8075 		/*
   8076 		 * Frame Buffer Hardware Registers
   8077 		 */
   8078 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
   8079 		for (i = 0; i < 16; i++) {
   8080 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8081 		}
   8082 
   8083 		/*
   8084 		 * Fibre Protocol Module 0 Hardware Registers
   8085 		 */
   8086 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
   8087 		for (i = 0; i < 64; i++) {
   8088 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8089 		}
   8090 
   8091 		/*
   8092 		 * Fibre Protocol Module 1 Hardware Registers
   8093 		 */
   8094 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
   8095 		for (i = 0; i < 64; i++) {
   8096 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8097 		}
   8098 	} else {
   8099 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
   8100 		return;
   8101 	}
   8102 	isp_prt(isp, ISP_LOGALL,
   8103 	   "isp_fw_dump: RISC registers dumped successfully");
   8104 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
   8105 	for (i = 0; i < 100; i++) {
   8106 		USEC_DELAY(100);
   8107 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
   8108 			break;
   8109 		}
   8110 	}
   8111 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
   8112 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
   8113 		return;
   8114 	}
   8115 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   8116 	for (i = 0; i < 100; i++) {
   8117 		USEC_DELAY(100);
   8118 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   8119 			break;
   8120 		}
   8121 	}
   8122 	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
   8123 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
   8124 		return;
   8125 	}
   8126 	ISP_WRITE(isp, RISC_EMB, 0xf2);
   8127 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
   8128 	for (i = 0; i < 100; i++) {
   8129 		USEC_DELAY(100);
   8130 		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
   8131 			break;
   8132 		}
   8133 	}
   8134 	ISP_ENABLE_INTS(isp);
   8135 	mbs.param[0] = MBOX_READ_RAM_WORD;
   8136 	mbs.param[1] = 0x1000;
   8137 	isp->isp_mbxworkp = (void *) ptr;
   8138 	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
   8139 	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
   8140 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
   8141 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   8142 		isp_prt(isp, ISP_LOGWARN,
   8143 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
   8144 		return;
   8145 	}
   8146 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
   8147 	*ptr++ = isp->isp_mboxtmp[2];
   8148 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
   8149 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
   8150 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
   8151 }
   8152 
   8153 static void
   8154 isp2300_fw_dump(ispsoftc_t *isp)
   8155 {
   8156 	int i, j;
   8157 	mbreg_t mbs;
   8158 	uint16_t *ptr;
   8159 
   8160 	MEMZERO(&mbs, sizeof (mbs));
   8161 	ptr = FCPARAM(isp)->isp_dump_data;
   8162 	if (ptr == NULL) {
   8163 		isp_prt(isp, ISP_LOGERR,
   8164 		   "No place to dump RISC registers and SRAM");
   8165 		return;
   8166 	}
   8167 	if (*ptr++) {
   8168 		isp_prt(isp, ISP_LOGERR,
   8169 		   "dump area for RISC registers and SRAM already used");
   8170 		return;
   8171 	}
   8172 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   8173 	for (i = 0; i < 100; i++) {
   8174 		USEC_DELAY(100);
   8175 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   8176 			break;
   8177 		}
   8178 	}
   8179 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
   8180 		/*
   8181 		 * PBIU registers
   8182 		 */
   8183 		for (i = 0; i < 8; i++) {
   8184 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
   8185 		}
   8186 
   8187 		/*
   8188 		 * ReqQ-RspQ-Risc2Host Status registers
   8189 		 */
   8190 		for (i = 0; i < 8; i++) {
   8191 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
   8192 		}
   8193 
   8194 		/*
   8195 		 * Mailbox Registers
   8196 		 */
   8197 		for (i = 0; i < 32; i++) {
   8198 			*ptr++ =
   8199 			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
   8200 		}
   8201 
   8202 		/*
   8203 		 * Auto Request Response DMA registers
   8204 		 */
   8205 		ISP_WRITE(isp, BIU2100_CSR, 0x40);
   8206 		for (i = 0; i < 32; i++) {
   8207 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8208 		}
   8209 
   8210 		/*
   8211 		 * DMA registers
   8212 		 */
   8213 		ISP_WRITE(isp, BIU2100_CSR, 0x50);
   8214 		for (i = 0; i < 48; i++) {
   8215 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8216 		}
   8217 
   8218 		/*
   8219 		 * RISC hardware registers
   8220 		 */
   8221 		ISP_WRITE(isp, BIU2100_CSR, 0);
   8222 		for (i = 0; i < 16; i++) {
   8223 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
   8224 		}
   8225 
   8226 		/*
   8227 		 * RISC GP? registers
   8228 		 */
   8229 		for (j = 0; j < 8; j++) {
   8230 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
   8231 			for (i = 0; i < 16; i++) {
   8232 				*ptr++ =
   8233 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8234 			}
   8235 		}
   8236 
   8237 		/*
   8238 		 * frame buffer hardware registers
   8239 		 */
   8240 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
   8241 		for (i = 0; i < 64; i++) {
   8242 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8243 		}
   8244 
   8245 		/*
   8246 		 * FPM B0 hardware registers
   8247 		 */
   8248 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
   8249 		for (i = 0; i < 64; i++) {
   8250 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8251 		}
   8252 
   8253 		/*
   8254 		 * FPM B1 hardware registers
   8255 		 */
   8256 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
   8257 		for (i = 0; i < 64; i++) {
   8258 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
   8259 		}
   8260 	} else {
   8261 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
   8262 		return;
   8263 	}
   8264 	isp_prt(isp, ISP_LOGALL,
   8265 	   "isp_fw_dump: RISC registers dumped successfully");
   8266 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
   8267 	for (i = 0; i < 100; i++) {
   8268 		USEC_DELAY(100);
   8269 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
   8270 			break;
   8271 		}
   8272 	}
   8273 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
   8274 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
   8275 		return;
   8276 	}
   8277 	ISP_ENABLE_INTS(isp);
   8278 	MEMZERO(&mbs, sizeof (mbs));
   8279 	mbs.param[0] = MBOX_READ_RAM_WORD;
   8280 	mbs.param[1] = 0x800;
   8281 	isp->isp_mbxworkp = (void *) ptr;
   8282 	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
   8283 	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
   8284 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
   8285 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   8286 		isp_prt(isp, ISP_LOGWARN,
   8287 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
   8288 		return;
   8289 	}
   8290 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
   8291 	*ptr++ = isp->isp_mboxtmp[2];
   8292 	MEMZERO(&mbs, sizeof (mbs));
   8293 	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
   8294 	mbs.param[8] = 1;
   8295 	isp->isp_mbxworkp = (void *) ptr;
   8296 	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
   8297 	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
   8298 	isp->isp_mbxwrk8 = 0x1;
   8299 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
   8300 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   8301 		isp_prt(isp, ISP_LOGWARN,
   8302 		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
   8303 		return;
   8304 	}
   8305 	ptr = isp->isp_mbxworkp;	/* finish final word */
   8306 	*ptr++ = mbs.param[2];
   8307 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
   8308 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
   8309 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
   8310 }
   8311 
   8312 void
   8313 isp_fw_dump(ispsoftc_t *isp)
   8314 {
   8315 	if (IS_2200(isp))
   8316 		isp2200_fw_dump(isp);
   8317 	else if (IS_23XX(isp))
   8318 		isp2300_fw_dump(isp);
   8319 	else if (IS_24XX(isp))
   8320 		isp_prt(isp, ISP_LOGERR, "24XX dump method undefined");
   8321 
   8322 }
   8323 #endif
   8324