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