Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.61
      1 /* $NetBSD: isp.c,v 1.61 2000/08/16 18:10:21 mjacob Exp $ */
      2 /*
      3  * This driver, which is contained in NetBSD in the files:
      4  *
      5  *	sys/dev/ic/isp.c
      6  *	sys/dev/ic/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 		/*
   2397 		 * XXX: Look for command in the REQUEST QUEUE. That is,
   2398 		 * XXX: It hasen't been picked up by firmware yet.
   2399 		 */
   2400 		break;
   2401 
   2402 	case ISPCTL_UPDATE_PARAMS:
   2403 		isp_update(isp);
   2404 		return (0);
   2405 
   2406 	case ISPCTL_FCLINK_TEST:
   2407 		if (IS_FC(isp)) {
   2408 			int usdelay = (arg)? *((int *) arg) : 250000;
   2409 			return (isp_fclink_test(isp, usdelay));
   2410 		}
   2411 		break;
   2412 
   2413 	case ISPCTL_PDB_SYNC:
   2414 		if (IS_FC(isp)) {
   2415 			return (isp_pdb_sync(isp, -1));
   2416 		}
   2417 		break;
   2418 #ifdef	ISP_TARGET_MODE
   2419 	case ISPCTL_TOGGLE_TMODE:
   2420 	{
   2421 		int ena = *(int *)arg;
   2422 		if (IS_SCSI(isp)) {
   2423 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
   2424 			mbs.param[1] = (ena)? ENABLE_TARGET_FLAG : 0;
   2425 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   2426 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2427 				break;
   2428 			}
   2429 		} else {
   2430 			fcparam *fcp = isp->isp_param;
   2431 			/*
   2432 			 * We assume somebody has quiesced this bus.
   2433 			 */
   2434 			if (ena) {
   2435 				if (fcp->isp_fwoptions & ICBOPT_TGT_ENABLE) {
   2436 					return (0);
   2437 				}
   2438 				fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE;
   2439 			} else {
   2440 				if (!(fcp->isp_fwoptions & ICBOPT_TGT_ENABLE)) {
   2441 					return (0);
   2442 				}
   2443 				fcp->isp_fwoptions &= ~ICBOPT_TGT_ENABLE;
   2444 			}
   2445 			isp->isp_state = ISP_NILSTATE;
   2446 			isp_reset(isp);
   2447 			if (isp->isp_state != ISP_RESETSTATE) {
   2448 				break;
   2449 			}
   2450 			isp_init(isp);
   2451 			if (isp->isp_state != ISP_INITSTATE) {
   2452 				break;
   2453 			}
   2454 			isp->isp_state = ISP_RUNSTATE;
   2455 		}
   2456 		return (0);
   2457 	}
   2458 #endif
   2459 	}
   2460 	return (-1);
   2461 }
   2462 
   2463 /*
   2464  * Interrupt Service Routine(s).
   2465  *
   2466  * External (OS) framework has done the appropriate locking,
   2467  * and the locking will be held throughout this function.
   2468  */
   2469 
   2470 /*
   2471  * Limit our stack depth by sticking with the max likely number
   2472  * of completions on a request queue at any one time.
   2473  */
   2474 #define	MAX_REQUESTQ_COMPLETIONS	32
   2475 
   2476 int
   2477 isp_intr(arg)
   2478 	void *arg;
   2479 {
   2480 	struct ispsoftc *isp = arg;
   2481 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
   2482 	u_int16_t iptr, optr, isr, sema, junk;
   2483 	int i, nlooked = 0, ndone = 0;
   2484 
   2485 	if (IS_2100(isp)) {
   2486 		i = 0;
   2487 		do {
   2488 			isr = ISP_READ(isp, BIU_ISR);
   2489 			junk = ISP_READ(isp, BIU_ISR);
   2490 		} while (isr != junk && ++i < 1000);
   2491 		if (isr != junk) {
   2492 			isp_prt(isp, ISP_LOGWARN,
   2493 			    "isr unsteady (%x, %x)", isr, junk);
   2494 		}
   2495 		i = 0;
   2496 		do {
   2497 			sema = ISP_READ(isp, BIU_SEMA);
   2498 			junk = ISP_READ(isp, BIU_SEMA);
   2499 		} while (sema != junk && ++i < 1000);
   2500 		if (sema != junk) {
   2501 			isp_prt(isp, ISP_LOGWARN,
   2502 			    "sema unsteady (%x, %x)", sema, junk);
   2503 		}
   2504 	} else {
   2505 		isr = ISP_READ(isp, BIU_ISR);
   2506 		sema = ISP_READ(isp, BIU_SEMA);
   2507 	}
   2508 	isp_prt(isp, ISP_LOGDEBUG3, "isp_intr isr %x sem %x", isr, sema);
   2509 	isr &= INT_PENDING_MASK(isp);
   2510 	sema &= BIU_SEMA_LOCK;
   2511 	if (isr == 0 && sema == 0) {
   2512 		return (0);
   2513 	}
   2514 
   2515 	if (sema) {
   2516 		u_int16_t mbox;
   2517 
   2518 		if (IS_2100(isp)) {
   2519 			i = 0;
   2520 			do {
   2521 				mbox = ISP_READ(isp, OUTMAILBOX0);
   2522 				junk = ISP_READ(isp, OUTMAILBOX0);;
   2523 			} while (junk != mbox && ++i < 1000);
   2524 			if (mbox != junk) {
   2525 				isp_prt(isp, ISP_LOGWARN,
   2526 				    "mailbox0 unsteady (%x, %x)", mbox, junk);
   2527 				ISP_WRITE(isp, BIU_SEMA, 0);
   2528 				ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2529 				return (1);
   2530 			}
   2531 		} else {
   2532 			mbox = ISP_READ(isp, OUTMAILBOX0);
   2533 		}
   2534 		if (mbox & 0x4000) {
   2535 			int obits, i = 0;
   2536 			if ((obits = isp->isp_mboxbsy) != 0) {
   2537 				isp->isp_mboxtmp[i++] = mbox;
   2538 				for (i = 1; i < 8; i++) {
   2539 					if ((obits & (1 << i)) == 0) {
   2540 						continue;
   2541 					}
   2542 					isp->isp_mboxtmp[i] =
   2543 					    ISP_READ(isp, MBOX_OFF(i));
   2544 				}
   2545 				MBOX_NOTIFY_COMPLETE(isp);
   2546 			} else {
   2547 				isp_prt(isp, ISP_LOGWARN,
   2548 				    "Mbox Command Async (0x%x) with no waiters",
   2549 				    mbox);
   2550 			}
   2551 		} else {
   2552 			u_int32_t fhandle = isp_parse_async(isp, (int) mbox);
   2553 			isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
   2554 			if (fhandle > 0) {
   2555 				isp_fastpost_complete(isp, fhandle);
   2556 			}
   2557 		}
   2558 		if (IS_FC(isp) || isp->isp_state != ISP_RUNSTATE) {
   2559 			ISP_WRITE(isp, BIU_SEMA, 0);
   2560 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2561 			return (1);
   2562 		}
   2563 	}
   2564 
   2565 	/*
   2566 	 * We can't be getting this now.
   2567 	 */
   2568 	if (isp->isp_state != ISP_RUNSTATE) {
   2569 		isp_prt(isp, ISP_LOGWARN,
   2570 		    "interrupt (isr=%x, sema=%x) when not ready", isr, sema);
   2571 		ISP_WRITE(isp, INMAILBOX5, ISP_READ(isp, OUTMAILBOX5));
   2572 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2573 		ISP_WRITE(isp, BIU_SEMA, 0);
   2574 		return (1);
   2575 	}
   2576 
   2577 	/*
   2578 	 * You *must* read OUTMAILBOX5 prior to clearing the RISC interrupt.
   2579 	 */
   2580 	optr = isp->isp_residx;
   2581 
   2582 	if (IS_2100(isp)) {
   2583 		i = 0;
   2584 		do {
   2585 			iptr = ISP_READ(isp, OUTMAILBOX5);
   2586 			junk = ISP_READ(isp, OUTMAILBOX5);
   2587 		} while (junk != iptr && ++i < 1000);
   2588 
   2589 		if (iptr != junk) {
   2590 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2591 			isp_prt(isp, ISP_LOGWARN,
   2592 			    "mailbox5 unsteady (%x, %x)", iptr, junk);
   2593 			return (1);
   2594 		}
   2595 	} else {
   2596 		iptr = ISP_READ(isp, OUTMAILBOX5);
   2597 	}
   2598 
   2599 	if (sema) {
   2600 		ISP_WRITE(isp, BIU_SEMA, 0);
   2601 	}
   2602 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2603 
   2604 	if (optr == iptr && sema == 0) {
   2605 		/*
   2606 		 * There are a lot of these- reasons unknown- mostly on
   2607 		 * faster Alpha machines.
   2608 		 *
   2609 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
   2610 		 * make sure the old interrupt went away (to avoid 'ringing'
   2611 		 * effects), but that didn't stop this from occurring.
   2612 		 */
   2613 		junk = ISP_READ(isp, BIU_ISR);
   2614 		isp_prt(isp, ISP_LOGDEBUG2,
   2615 		    "bogus intr- isr %x (%x) iptr %x optr %x",
   2616 		    isr, junk, iptr, optr);
   2617 	}
   2618 
   2619 	while (optr != iptr) {
   2620 		ispstatusreq_t *sp;
   2621 		u_int16_t oop;
   2622 		int buddaboom = 0;
   2623 
   2624 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   2625 		oop = optr;
   2626 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
   2627 		nlooked++;
   2628 		/*
   2629 		 * Do any appropriate unswizzling of what the Qlogic f/w has
   2630 		 * written into memory so it makes sense to us. This is a
   2631 		 * per-platform thing. Also includes any memory barriers.
   2632 		 */
   2633 		ISP_UNSWIZZLE_RESPONSE(isp, sp, oop);
   2634 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
   2635 			if (isp_handle_other_response(isp, sp, &optr) == 0) {
   2636 				MEMZERO(sp, sizeof (isphdr_t));
   2637 				continue;
   2638 			}
   2639 			/*
   2640 			 * It really has to be a bounced request just copied
   2641 			 * from the request queue to the response queue. If
   2642 			 * not, something bad has happened.
   2643 			 */
   2644 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
   2645 				isp_prt(isp, ISP_LOGERR, notresp,
   2646 				    sp->req_header.rqs_entry_type, oop, optr);
   2647 				MEMZERO(sp, sizeof (isphdr_t));
   2648 				continue;
   2649 			}
   2650 			buddaboom = 1;
   2651 		}
   2652 
   2653 		if (sp->req_header.rqs_flags & 0xf) {
   2654 #define	_RQS_OFLAGS	\
   2655 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
   2656 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   2657 				isp_prt(isp, ISP_LOGWARN,
   2658 				    "continuation segment");
   2659 				ISP_WRITE(isp, INMAILBOX5, optr);
   2660 				continue;
   2661 			}
   2662 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   2663 				isp_prt(isp, ISP_LOGDEBUG1,
   2664 				    "internal queues full");
   2665 				/*
   2666 				 * We'll synthesize a QUEUE FULL message below.
   2667 				 */
   2668 			}
   2669 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   2670 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
   2671 				buddaboom++;
   2672 			}
   2673 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   2674 				isp_prt(isp, ISP_LOGERR, "bad request packet");
   2675 				buddaboom++;
   2676 			}
   2677 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
   2678 				isp_prt(isp, ISP_LOGERR,
   2679 				    "unknown flags (0x%x) in response",
   2680 				    sp->req_header.rqs_flags);
   2681 				buddaboom++;
   2682 			}
   2683 #undef	_RQS_OFLAGS
   2684 		}
   2685 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
   2686 			MEMZERO(sp, sizeof (isphdr_t));
   2687 			isp_prt(isp, ISP_LOGERR,
   2688 			    "bad request handle %d", sp->req_handle);
   2689 			ISP_WRITE(isp, INMAILBOX5, optr);
   2690 			continue;
   2691 		}
   2692 		xs = isp_find_xs(isp, sp->req_handle);
   2693 		if (xs == NULL) {
   2694 			MEMZERO(sp, sizeof (isphdr_t));
   2695 			isp_prt(isp, ISP_LOGERR,
   2696 			    "cannot find handle 0x%x in xflist",
   2697 			    sp->req_handle);
   2698 			ISP_WRITE(isp, INMAILBOX5, optr);
   2699 			continue;
   2700 		}
   2701 		isp_destroy_handle(isp, sp->req_handle);
   2702 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   2703 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   2704 		}
   2705 		if (buddaboom) {
   2706 			XS_SETERR(xs, HBA_BOTCH);
   2707 		}
   2708 
   2709 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
   2710 			/*
   2711 			 * Fibre Channel F/W doesn't say we got status
   2712 			 * if there's Sense Data instead. I guess they
   2713 			 * think it goes w/o saying.
   2714 			 */
   2715 			sp->req_state_flags |= RQSF_GOT_STATUS;
   2716 		}
   2717 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   2718 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
   2719 		}
   2720 
   2721 		switch (sp->req_header.rqs_entry_type) {
   2722 		case RQSTYPE_RESPONSE:
   2723 			XS_SET_STATE_STAT(isp, xs, sp);
   2724 			isp_parse_status(isp, sp, xs);
   2725 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
   2726 			    (*XS_STSP(xs) == SCSI_BUSY)) {
   2727 				XS_SETERR(xs, HBA_TGTBSY);
   2728 			}
   2729 			if (IS_SCSI(isp)) {
   2730 				XS_RESID(xs) = sp->req_resid;
   2731 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   2732 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   2733 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
   2734 					XS_SAVE_SENSE(xs, sp);
   2735 				}
   2736 				/*
   2737 				 * A new synchronous rate was negotiated for
   2738 				 * this target. Mark state such that we'll go
   2739 				 * look up that which has changed later.
   2740 				 */
   2741 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   2742 					int t = XS_TGT(xs);
   2743 					sdparam *sdp = isp->isp_param;
   2744 					sdp += XS_CHANNEL(xs);
   2745 					sdp->isp_devparam[t].dev_refresh = 1;
   2746 					isp->isp_update |=
   2747 					    (1 << XS_CHANNEL(xs));
   2748 				}
   2749 			} else {
   2750 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
   2751 					XS_RESID(xs) = 0;
   2752 				} else if (sp->req_scsi_status & RQCS_RESID) {
   2753 					XS_RESID(xs) = sp->req_resid;
   2754 				} else {
   2755 					XS_RESID(xs) = 0;
   2756 				}
   2757 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   2758 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   2759 				    (sp->req_scsi_status & RQCS_SV)) {
   2760 					XS_SAVE_SENSE(xs, sp);
   2761 				}
   2762 			}
   2763 			isp_prt(isp, ISP_LOGDEBUG2, "asked for %d got resid %d",
   2764 				XS_XFRLEN(xs), sp->req_resid);
   2765 			break;
   2766 		case RQSTYPE_REQUEST:
   2767 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   2768 				/*
   2769 				 * Force Queue Full status.
   2770 				 */
   2771 				*XS_STSP(xs) = SCSI_QFULL;
   2772 				XS_SETERR(xs, HBA_NOERROR);
   2773 			} else if (XS_NOERR(xs)) {
   2774 				XS_SETERR(xs, HBA_BOTCH);
   2775 			}
   2776 			XS_RESID(xs) = XS_XFRLEN(xs);
   2777 			break;
   2778 		default:
   2779 			isp_prt(isp, ISP_LOGWARN,
   2780 			    "unhandled respose queue type 0x%x",
   2781 			    sp->req_header.rqs_entry_type);
   2782 			if (XS_NOERR(xs)) {
   2783 				XS_SETERR(xs, HBA_BOTCH);
   2784 			}
   2785 			break;
   2786 		}
   2787 
   2788 		/*
   2789 		 * Free any dma resources. As a side effect, this may
   2790 		 * also do any cache flushing necessary for data coherence.			 */
   2791 		if (XS_XFRLEN(xs)) {
   2792 			ISP_DMAFREE(isp, xs, sp->req_handle);
   2793 		}
   2794 
   2795 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
   2796 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && !XS_NOERR(xs))) {
   2797 			char skey;
   2798 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   2799 				skey = XS_SNSKEY(xs) & 0xf;
   2800 				if (skey < 10)
   2801 					skey += '0';
   2802 				else
   2803 					skey += 'a';
   2804 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
   2805 				skey = '?';
   2806 			} else {
   2807 				skey = '.';
   2808 			}
   2809 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
   2810 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
   2811 			    *XS_STSP(xs), skey, XS_ERR(xs));
   2812 		}
   2813 
   2814 		if (isp->isp_nactive > 0)
   2815 		    isp->isp_nactive--;
   2816 		complist[ndone++] = xs;	/* defer completion call until later */
   2817 		MEMZERO(sp, sizeof (isphdr_t));
   2818 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
   2819 			break;
   2820 		}
   2821 	}
   2822 
   2823 	/*
   2824 	 * If we looked at any commands, then it's valid to find out
   2825 	 * what the outpointer is. It also is a trigger to update the
   2826 	 * ISP's notion of what we've seen so far.
   2827 	 */
   2828 	if (nlooked) {
   2829 		ISP_WRITE(isp, INMAILBOX5, optr);
   2830 		isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
   2831 	}
   2832 
   2833 	isp->isp_residx = optr;
   2834 	for (i = 0; i < ndone; i++) {
   2835 		xs = complist[i];
   2836 		if (xs) {
   2837 			isp_done(xs);
   2838 		}
   2839 	}
   2840 	return (1);
   2841 }
   2842 
   2843 /*
   2844  * Support routines.
   2845  */
   2846 
   2847 static int
   2848 isp_parse_async(isp, mbox)
   2849 	struct ispsoftc *isp;
   2850 	int mbox;
   2851 {
   2852 	int bus;
   2853 	u_int32_t fast_post_handle = 0;
   2854 
   2855 	if (IS_DUALBUS(isp)) {
   2856 		bus = ISP_READ(isp, OUTMAILBOX6);
   2857 	} else {
   2858 		bus = 0;
   2859 	}
   2860 
   2861 	switch (mbox) {
   2862 	case ASYNC_BUS_RESET:
   2863 		isp->isp_sendmarker |= (1 << bus);
   2864 #ifdef	ISP_TARGET_MODE
   2865 		isp_target_async(isp, bus, mbox);
   2866 #endif
   2867 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
   2868 		break;
   2869 	case ASYNC_SYSTEM_ERROR:
   2870 		mbox = ISP_READ(isp, OUTMAILBOX1);
   2871 		isp_prt(isp, ISP_LOGERR,
   2872 		    "Internal FW Error @ RISC Addr 0x%x", mbox);
   2873 		isp_reinit(isp);
   2874 		/* no point continuing after this */
   2875 		return (-1);
   2876 
   2877 	case ASYNC_RQS_XFER_ERR:
   2878 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
   2879 		break;
   2880 
   2881 	case ASYNC_RSP_XFER_ERR:
   2882 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
   2883 		break;
   2884 
   2885 	case ASYNC_QWAKEUP:
   2886 		/*
   2887 		 * We've just been notified that the Queue has woken up.
   2888 		 * We don't need to be chatty about this- just unlatch things
   2889 		 * and move on.
   2890 		 */
   2891 		mbox = ISP_READ(isp, OUTMAILBOX4);
   2892 		break;
   2893 
   2894 	case ASYNC_TIMEOUT_RESET:
   2895 		isp_prt(isp, ISP_LOGWARN,
   2896 		    "timeout initiated SCSI bus reset of bus %d\n", bus);
   2897 		isp->isp_sendmarker |= (1 << bus);
   2898 #ifdef	ISP_TARGET_MODE
   2899 		isp_target_async(isp, bus, mbox);
   2900 #endif
   2901 		break;
   2902 
   2903 	case ASYNC_DEVICE_RESET:
   2904 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
   2905 		isp->isp_sendmarker |= (1 << bus);
   2906 #ifdef	ISP_TARGET_MODE
   2907 		isp_target_async(isp, bus, mbox);
   2908 #endif
   2909 		break;
   2910 
   2911 	case ASYNC_EXTMSG_UNDERRUN:
   2912 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
   2913 		break;
   2914 
   2915 	case ASYNC_SCAM_INT:
   2916 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
   2917 		break;
   2918 
   2919 	case ASYNC_HUNG_SCSI:
   2920 		isp_prt(isp, ISP_LOGERR,
   2921 		    "stalled SCSI Bus after DATA Overrun");
   2922 		/* XXX: Need to issue SCSI reset at this point */
   2923 		break;
   2924 
   2925 	case ASYNC_KILLED_BUS:
   2926 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
   2927 		break;
   2928 
   2929 	case ASYNC_BUS_TRANSIT:
   2930 		mbox = ISP_READ(isp, OUTMAILBOX2);
   2931 		switch (mbox & 0x1c00) {
   2932 		case SXP_PINS_LVD_MODE:
   2933 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
   2934 			SDPARAM(isp)->isp_diffmode = 0;
   2935 			SDPARAM(isp)->isp_ultramode = 0;
   2936 			SDPARAM(isp)->isp_lvdmode = 1;
   2937 			break;
   2938 		case SXP_PINS_HVD_MODE:
   2939 			isp_prt(isp, ISP_LOGINFO,
   2940 			    "Transition to Differential mode");
   2941 			SDPARAM(isp)->isp_diffmode = 1;
   2942 			SDPARAM(isp)->isp_ultramode = 0;
   2943 			SDPARAM(isp)->isp_lvdmode = 0;
   2944 			break;
   2945 		case SXP_PINS_SE_MODE:
   2946 			isp_prt(isp, ISP_LOGINFO,
   2947 			    "Transition to Single Ended mode");
   2948 			SDPARAM(isp)->isp_diffmode = 0;
   2949 			SDPARAM(isp)->isp_ultramode = 1;
   2950 			SDPARAM(isp)->isp_lvdmode = 0;
   2951 			break;
   2952 		default:
   2953 			isp_prt(isp, ISP_LOGWARN,
   2954 			    "Transition to Unknown Mode 0x%x", mbox);
   2955 			break;
   2956 		}
   2957 		/*
   2958 		 * XXX: Set up to renegotiate again!
   2959 		 */
   2960 		/* Can only be for a 1080... */
   2961 		isp->isp_sendmarker |= (1 << bus);
   2962 		break;
   2963 
   2964 	case ASYNC_CMD_CMPLT:
   2965 		fast_post_handle = (ISP_READ(isp, OUTMAILBOX2) << 16) |
   2966 		    ISP_READ(isp, OUTMAILBOX1);
   2967 		isp_prt(isp, ISP_LOGDEBUG3, "fast post completion of %u",
   2968 		    fast_post_handle);
   2969 		break;
   2970 
   2971 	case ASYNC_CTIO_DONE:
   2972 #ifdef	ISP_TARGET_MODE
   2973 		/*
   2974 		 * Bus gets overloaded with the handle. Dual bus
   2975 		 * cards don't put bus# into the handle.
   2976 		 */
   2977 		bus = (ISP_READ(isp, OUTMAILBOX2) << 16) |
   2978 		    ISP_READ(isp, OUTMAILBOX1);
   2979 		isp_target_async(isp, bus, mbox);
   2980 #else
   2981 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
   2982 #endif
   2983 		break;
   2984 
   2985 	case ASYNC_LIP_OCCURRED:
   2986 		FCPARAM(isp)->isp_lipseq =
   2987 		    ISP_READ(isp, OUTMAILBOX1);
   2988 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   2989 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   2990 		isp->isp_sendmarker = 1;
   2991 		isp_mark_getpdb_all(isp);
   2992 		isp_prt(isp, ISP_LOGINFO, "LIP occurred");
   2993 #ifdef	ISP_TARGET_MODE
   2994 		isp_target_async(isp, bus, mbox);
   2995 #endif
   2996 		break;
   2997 
   2998 	case ASYNC_LOOP_UP:
   2999 		isp->isp_sendmarker = 1;
   3000 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3001 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3002 		isp_mark_getpdb_all(isp);
   3003 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
   3004 #ifdef	ISP_TARGET_MODE
   3005 		isp_target_async(isp, bus, mbox);
   3006 #endif
   3007 		break;
   3008 
   3009 	case ASYNC_LOOP_DOWN:
   3010 		isp->isp_sendmarker = 1;
   3011 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3012 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   3013 		isp_mark_getpdb_all(isp);
   3014 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
   3015 #ifdef	ISP_TARGET_MODE
   3016 		isp_target_async(isp, bus, mbox);
   3017 #endif
   3018 		break;
   3019 
   3020 	case ASYNC_LOOP_RESET:
   3021 		isp->isp_sendmarker = 1;
   3022 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3023 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   3024 		isp_mark_getpdb_all(isp);
   3025 		isp_prt(isp, ISP_LOGINFO, "Loop RESET");
   3026 #ifdef	ISP_TARGET_MODE
   3027 		isp_target_async(isp, bus, mbox);
   3028 #endif
   3029 		break;
   3030 
   3031 	case ASYNC_PDB_CHANGED:
   3032 		isp->isp_sendmarker = 1;
   3033 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   3034 		isp_mark_getpdb_all(isp);
   3035 		isp_prt(isp, ISP_LOGINFO, "Port Database Changed");
   3036 		break;
   3037 
   3038 	case ASYNC_CHANGE_NOTIFY:
   3039 		isp_mark_getpdb_all(isp);
   3040 		/*
   3041 		 * Not correct, but it will force us to rescan the loop.
   3042 		 */
   3043 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   3044 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, NULL);
   3045 		break;
   3046 
   3047 	case ASYNC_PTPMODE:
   3048 		if (FCPARAM(isp)->isp_onfabric)
   3049 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
   3050 		else
   3051 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
   3052 		isp_mark_getpdb_all(isp);
   3053 		isp->isp_sendmarker = 1;
   3054 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3055 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3056 #ifdef	ISP_TARGET_MODE
   3057 		isp_target_async(isp, bus, mbox);
   3058 #endif
   3059 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
   3060 		break;
   3061 
   3062 	case ASYNC_CONNMODE:
   3063 		mbox = ISP_READ(isp, OUTMAILBOX1);
   3064 		switch (mbox) {
   3065 		case ISP_CONN_LOOP:
   3066 			isp_prt(isp, ISP_LOGINFO, "Point-to-Point->Loop mode");
   3067 			break;
   3068 		case ISP_CONN_PTP:
   3069 			isp_prt(isp, ISP_LOGINFO, "Loop->Point-to-Point mode");
   3070 			break;
   3071 		case ISP_CONN_BADLIP:
   3072 			isp_prt(isp, ISP_LOGWARN,
   3073 			    "Point-to-Point->Loop mode (1)");
   3074 			break;
   3075 		case ISP_CONN_FATAL:
   3076 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
   3077 			isp_reinit(isp);
   3078 			/* no point continuing after this */
   3079 			return (-1);
   3080 
   3081 		case ISP_CONN_LOOPBACK:
   3082 			isp_prt(isp, ISP_LOGWARN,
   3083 			    "Looped Back in Point-to-Point mode");
   3084 		}
   3085 		break;
   3086 
   3087 	default:
   3088 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
   3089 		break;
   3090 	}
   3091 	return (fast_post_handle);
   3092 }
   3093 
   3094 /*
   3095  * Handle other response entries. A pointer to the request queue output
   3096  * index is here in case we want to eat several entries at once, although
   3097  * this is not used currently.
   3098  */
   3099 
   3100 static int
   3101 isp_handle_other_response(isp, sp, optrp)
   3102 	struct ispsoftc *isp;
   3103 	ispstatusreq_t *sp;
   3104 	u_int16_t *optrp;
   3105 {
   3106 	switch (sp->req_header.rqs_entry_type) {
   3107 	case RQSTYPE_STATUS_CONT:
   3108 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
   3109 		return (0);
   3110 	case RQSTYPE_ATIO:
   3111 	case RQSTYPE_CTIO:
   3112 	case RQSTYPE_ENABLE_LUN:
   3113 	case RQSTYPE_MODIFY_LUN:
   3114 	case RQSTYPE_NOTIFY:
   3115 	case RQSTYPE_NOTIFY_ACK:
   3116 	case RQSTYPE_CTIO1:
   3117 	case RQSTYPE_ATIO2:
   3118 	case RQSTYPE_CTIO2:
   3119 	case RQSTYPE_CTIO3:
   3120 #ifdef	ISP_TARGET_MODE
   3121 		return (isp_target_notify(isp, sp, optrp));
   3122 #else
   3123 		optrp = optrp;
   3124 		/* FALLTHROUGH */
   3125 #endif
   3126 	case RQSTYPE_REQUEST:
   3127 	default:
   3128 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
   3129 		    sp->req_header.rqs_entry_type);
   3130 		return (-1);
   3131 	}
   3132 }
   3133 
   3134 static void
   3135 isp_parse_status(isp, sp, xs)
   3136 	struct ispsoftc *isp;
   3137 	ispstatusreq_t *sp;
   3138 	XS_T *xs;
   3139 {
   3140 	switch (sp->req_completion_status & 0xff) {
   3141 	case RQCS_COMPLETE:
   3142 		if (XS_NOERR(xs)) {
   3143 			XS_SETERR(xs, HBA_NOERROR);
   3144 		}
   3145 		return;
   3146 
   3147 	case RQCS_INCOMPLETE:
   3148 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   3149 			isp_prt(isp, ISP_LOGDEBUG1,
   3150 			    "Selection Timeout for %d.%d.%d",
   3151 			    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3152 			if (XS_NOERR(xs)) {
   3153 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3154 			}
   3155 			return;
   3156 		}
   3157 		isp_prt(isp, ISP_LOGERR,
   3158 		    "command incomplete for %d.%d.%d, state 0x%x",
   3159 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
   3160 		    sp->req_state_flags);
   3161 		break;
   3162 
   3163 	case RQCS_DMA_ERROR:
   3164 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
   3165 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3166 		break;
   3167 
   3168 	case RQCS_TRANSPORT_ERROR:
   3169 	{
   3170 		char buf[172];
   3171 		buf[0] = 0;
   3172 		STRNCAT(buf, "states=>", sizeof buf);
   3173 		if (sp->req_state_flags & RQSF_GOT_BUS) {
   3174 			STRNCAT(buf, " GOT_BUS", sizeof buf);
   3175 		}
   3176 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
   3177 			STRNCAT(buf, " GOT_TGT", sizeof buf);
   3178 		}
   3179 		if (sp->req_state_flags & RQSF_SENT_CDB) {
   3180 			STRNCAT(buf, " SENT_CDB", sizeof buf);
   3181 		}
   3182 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
   3183 			STRNCAT(buf, " XFRD_DATA", sizeof buf);
   3184 		}
   3185 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   3186 			STRNCAT(buf, " GOT_STS", sizeof buf);
   3187 		}
   3188 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
   3189 			STRNCAT(buf, " GOT_SNS", sizeof buf);
   3190 		}
   3191 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
   3192 			STRNCAT(buf, " XFR_CMPLT", sizeof buf);
   3193 		}
   3194 		STRNCAT(buf, "\nstatus=>", sizeof buf);
   3195 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
   3196 			STRNCAT(buf, " Disconnect", sizeof buf);
   3197 		}
   3198 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
   3199 			STRNCAT(buf, " Sync_xfr", sizeof buf);
   3200 		}
   3201 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
   3202 			STRNCAT(buf, " Parity", sizeof buf);
   3203 		}
   3204 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   3205 			STRNCAT(buf, " Bus_Reset", sizeof buf);
   3206 		}
   3207 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
   3208 			STRNCAT(buf, " Device_Reset", sizeof buf);
   3209 		}
   3210 		if (sp->req_status_flags & RQSTF_ABORTED) {
   3211 			STRNCAT(buf, " Aborted", sizeof buf);
   3212 		}
   3213 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
   3214 			STRNCAT(buf, " Timeout", sizeof buf);
   3215 		}
   3216 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   3217 			STRNCAT(buf, " Negotiation", sizeof buf);
   3218 		}
   3219 		isp_prt(isp, ISP_LOGERR, "%s", buf);
   3220 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
   3221 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
   3222 		break;
   3223 	}
   3224 	case RQCS_RESET_OCCURRED:
   3225 		isp_prt(isp, ISP_LOGWARN,
   3226 		    "bus reset destroyed command for %d.%d.%d",
   3227 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3228 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3229 		if (XS_NOERR(xs)) {
   3230 			XS_SETERR(xs, HBA_BUSRESET);
   3231 		}
   3232 		return;
   3233 
   3234 	case RQCS_ABORTED:
   3235 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
   3236 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3237 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3238 		if (XS_NOERR(xs)) {
   3239 			XS_SETERR(xs, HBA_ABORTED);
   3240 		}
   3241 		return;
   3242 
   3243 	case RQCS_TIMEOUT:
   3244 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
   3245 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3246 		if (XS_NOERR(xs)) {
   3247 			XS_SETERR(xs, HBA_CMDTIMEOUT);
   3248 		}
   3249 		return;
   3250 
   3251 	case RQCS_DATA_OVERRUN:
   3252 		XS_RESID(xs) = sp->req_resid;
   3253 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
   3254 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3255 		if (XS_NOERR(xs)) {
   3256 			XS_SETERR(xs, HBA_DATAOVR);
   3257 		}
   3258 		return;
   3259 
   3260 	case RQCS_COMMAND_OVERRUN:
   3261 		isp_prt(isp, ISP_LOGERR,
   3262 		    "command overrun for command on %d.%d.%d",
   3263 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3264 		break;
   3265 
   3266 	case RQCS_STATUS_OVERRUN:
   3267 		isp_prt(isp, ISP_LOGERR,
   3268 		    "status overrun for command on %d.%d.%d",
   3269 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3270 		break;
   3271 
   3272 	case RQCS_BAD_MESSAGE:
   3273 		isp_prt(isp, ISP_LOGERR,
   3274 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
   3275 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3276 		break;
   3277 
   3278 	case RQCS_NO_MESSAGE_OUT:
   3279 		isp_prt(isp, ISP_LOGERR,
   3280 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
   3281 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3282 		break;
   3283 
   3284 	case RQCS_EXT_ID_FAILED:
   3285 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
   3286 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3287 		break;
   3288 
   3289 	case RQCS_IDE_MSG_FAILED:
   3290 		isp_prt(isp, ISP_LOGERR,
   3291 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
   3292 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3293 		break;
   3294 
   3295 	case RQCS_ABORT_MSG_FAILED:
   3296 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
   3297 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3298 		break;
   3299 
   3300 	case RQCS_REJECT_MSG_FAILED:
   3301 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
   3302 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3303 		break;
   3304 
   3305 	case RQCS_NOP_MSG_FAILED:
   3306 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
   3307 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3308 		break;
   3309 
   3310 	case RQCS_PARITY_ERROR_MSG_FAILED:
   3311 		isp_prt(isp, ISP_LOGERR,
   3312 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
   3313 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3314 		break;
   3315 
   3316 	case RQCS_DEVICE_RESET_MSG_FAILED:
   3317 		isp_prt(isp, ISP_LOGWARN,
   3318 		    "BUS DEVICE RESET rejected by %d.%d.%d",
   3319 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3320 		break;
   3321 
   3322 	case RQCS_ID_MSG_FAILED:
   3323 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
   3324 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3325 		break;
   3326 
   3327 	case RQCS_UNEXP_BUS_FREE:
   3328 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
   3329 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3330 		break;
   3331 
   3332 	case RQCS_DATA_UNDERRUN:
   3333 		XS_RESID(xs) = sp->req_resid;
   3334 		if (XS_NOERR(xs)) {
   3335 			XS_SETERR(xs, HBA_NOERROR);
   3336 		}
   3337 		return;
   3338 
   3339 	case RQCS_XACT_ERR1:
   3340 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
   3341 		    XS_TGT(xs), XS_LUN(xs));
   3342 		break;
   3343 
   3344 	case RQCS_XACT_ERR2:
   3345 		isp_prt(isp, ISP_LOGERR, xact2,
   3346 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
   3347 		break;
   3348 
   3349 	case RQCS_XACT_ERR3:
   3350 		isp_prt(isp, ISP_LOGERR, xact3, XS_TGT(xs),
   3351 		    XS_LUN(xs), XS_CHANNEL(xs));
   3352 		break;
   3353 
   3354 	case RQCS_BAD_ENTRY:
   3355 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
   3356 		break;
   3357 
   3358 	case RQCS_QUEUE_FULL:
   3359 		isp_prt(isp, ISP_LOGDEBUG1,
   3360 		    "internal queues full for %d.%d.%d status 0x%x", XS_TGT(xs),
   3361 		    XS_LUN(xs), XS_CHANNEL(xs), *XS_STSP(xs));
   3362 		/*
   3363 		 * If QFULL or some other status byte is set, then this
   3364 		 * isn't an error, per se.
   3365 		 */
   3366 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
   3367 			XS_SETERR(xs, HBA_NOERROR);
   3368 			return;
   3369 		}
   3370 		break;
   3371 
   3372 	case RQCS_PHASE_SKIPPED:
   3373 		isp_prt(isp, ISP_LOGERR, pskip,
   3374 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3375 		break;
   3376 
   3377 	case RQCS_ARQS_FAILED:
   3378 		isp_prt(isp, ISP_LOGERR,
   3379 		    "Auto Request Sense failed for %d.%d.%d",
   3380 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3381 		if (XS_NOERR(xs)) {
   3382 			XS_SETERR(xs, HBA_ARQFAIL);
   3383 		}
   3384 		return;
   3385 
   3386 	case RQCS_WIDE_FAILED:
   3387 		isp_prt(isp, ISP_LOGERR,
   3388 		    "Wide Negotiation failed for %d.%d.%d",
   3389 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3390 		if (IS_SCSI(isp)) {
   3391 			sdparam *sdp = isp->isp_param;
   3392 			sdp += XS_CHANNEL(xs);
   3393 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
   3394 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3395 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3396 		}
   3397 		if (XS_NOERR(xs)) {
   3398 			XS_SETERR(xs, HBA_NOERROR);
   3399 		}
   3400 		return;
   3401 
   3402 	case RQCS_SYNCXFER_FAILED:
   3403 		isp_prt(isp, ISP_LOGERR,
   3404 		    "SDTR Message failed for target %d.%d.%d",
   3405 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3406 		if (IS_SCSI(isp)) {
   3407 			sdparam *sdp = isp->isp_param;
   3408 			sdp += XS_CHANNEL(xs);
   3409 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
   3410 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3411 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3412 		}
   3413 		break;
   3414 
   3415 	case RQCS_LVD_BUSERR:
   3416 		isp_prt(isp, ISP_LOGERR,
   3417 		    "Bad LVD condition while talking to %d.%d.%d",
   3418 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3419 		break;
   3420 
   3421 	case RQCS_PORT_UNAVAILABLE:
   3422 		/*
   3423 		 * No such port on the loop. Moral equivalent of SELTIMEO
   3424 		 */
   3425 		isp_prt(isp, ISP_LOGINFO,
   3426 		    "Port Unavailable for target %d", XS_TGT(xs));
   3427 		if (XS_NOERR(xs)) {
   3428 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3429 		}
   3430 		return;
   3431 
   3432 	case RQCS_PORT_LOGGED_OUT:
   3433 		/*
   3434 		 * It was there (maybe)- treat as a selection timeout.
   3435 		 */
   3436 		isp_prt(isp, ISP_LOGINFO,
   3437 		    "port logout for target %d", XS_TGT(xs));
   3438 		if (XS_NOERR(xs)) {
   3439 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3440 		}
   3441 		return;
   3442 
   3443 	case RQCS_PORT_CHANGED:
   3444 		isp_prt(isp, ISP_LOGWARN,
   3445 		    "port changed for target %d", XS_TGT(xs));
   3446 		if (XS_NOERR(xs)) {
   3447 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3448 		}
   3449 		return;
   3450 
   3451 	case RQCS_PORT_BUSY:
   3452 		isp_prt(isp, ISP_LOGWARN,
   3453 		    "port busy for target %d", XS_TGT(xs));
   3454 		if (XS_NOERR(xs)) {
   3455 			XS_SETERR(xs, HBA_TGTBSY);
   3456 		}
   3457 		return;
   3458 
   3459 	default:
   3460 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
   3461 		    sp->req_completion_status);
   3462 		break;
   3463 	}
   3464 	if (XS_NOERR(xs)) {
   3465 		XS_SETERR(xs, HBA_BOTCH);
   3466 	}
   3467 }
   3468 
   3469 static void
   3470 isp_fastpost_complete(isp, fph)
   3471 	struct ispsoftc *isp;
   3472 	u_int32_t fph;
   3473 {
   3474 	XS_T *xs;
   3475 
   3476 	if (fph < 1) {
   3477 		return;
   3478 	}
   3479 	xs = isp_find_xs(isp, fph);
   3480 	if (xs == NULL) {
   3481 		isp_prt(isp, ISP_LOGWARN,
   3482 		    "Command for fast post handle 0x%x not found", fph);
   3483 		return;
   3484 	}
   3485 	isp_destroy_handle(isp, fph);
   3486 
   3487 	/*
   3488 	 * Since we don't have a result queue entry item,
   3489 	 * we must believe that SCSI status is zero and
   3490 	 * that all data transferred.
   3491 	 */
   3492 	XS_SET_STATE_STAT(isp, xs, NULL);
   3493 	XS_RESID(xs) = 0;
   3494 	*XS_STSP(xs) = SCSI_GOOD;
   3495 	if (XS_XFRLEN(xs)) {
   3496 		ISP_DMAFREE(isp, xs, fph);
   3497 	}
   3498 	if (isp->isp_nactive)
   3499 		isp->isp_nactive--;
   3500 	isp_done(xs);
   3501 }
   3502 
   3503 #define	HIBYT(x)			((x) >> 0x8)
   3504 #define	LOBYT(x)			((x)  & 0xff)
   3505 #define	ISPOPMAP(a, b)			(((a) << 8) | (b))
   3506 static u_int16_t mbpscsi[] = {
   3507 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   3508 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   3509 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   3510 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   3511 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   3512 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   3513 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   3514 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   3515 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   3516 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
   3517 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
   3518 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   3519 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   3520 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   3521 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   3522 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   3523 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   3524 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
   3525 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
   3526 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   3527 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
   3528 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
   3529 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
   3530 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
   3531 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
   3532 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
   3533 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
   3534 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   3535 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
   3536 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   3537 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   3538 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   3539 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
   3540 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   3541 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   3542 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   3543 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
   3544 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   3545 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   3546 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
   3547 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   3548 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   3549 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
   3550 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   3551 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   3552 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   3553 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   3554 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   3555 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   3556 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   3557 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   3558 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   3559 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
   3560 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
   3561 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   3562 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   3563 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   3564 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   3565 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
   3566 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   3567 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   3568 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   3569 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   3570 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   3571 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   3572 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   3573 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   3574 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
   3575 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   3576 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
   3577 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
   3578 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
   3579 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
   3580 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
   3581 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
   3582 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
   3583 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   3584 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   3585 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   3586 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   3587 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
   3588 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
   3589 	ISPOPMAP(0xdf, 0xdf),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
   3590 	ISPOPMAP(0xff, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
   3591 	ISPOPMAP(0xcf, 0xff),	/* 0x54: EXECUTE IOCB A64 */
   3592 	ISPOPMAP(0x03, 0x01),	/* 0x55: ENABLE TARGET MODE */
   3593 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   3594 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   3595 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   3596 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   3597 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
   3598 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
   3599 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
   3600 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
   3601 };
   3602 
   3603 #ifndef	ISP_STRIPEED
   3604 static char *scsi_mbcmd_names[] = {
   3605 	"NO-OP",
   3606 	"LOAD RAM",
   3607 	"EXEC FIRMWARE",
   3608 	"DUMP RAM",
   3609 	"WRITE RAM WORD",
   3610 	"READ RAM WORD",
   3611 	"MAILBOX REG TEST",
   3612 	"VERIFY CHECKSUM",
   3613 	"ABOUT FIRMWARE",
   3614 	NULL,
   3615 	NULL,
   3616 	NULL,
   3617 	NULL,
   3618 	NULL,
   3619 	"CHECK FIRMWARE",
   3620 	NULL,
   3621 	"INIT REQUEST QUEUE",
   3622 	"INIT RESULT QUEUE",
   3623 	"EXECUTE IOCB",
   3624 	"WAKE UP",
   3625 	"STOP FIRMWARE",
   3626 	"ABORT",
   3627 	"ABORT DEVICE",
   3628 	"ABORT TARGET",
   3629 	"BUS RESET",
   3630 	"STOP QUEUE",
   3631 	"START QUEUE",
   3632 	"SINGLE STEP QUEUE",
   3633 	"ABORT QUEUE",
   3634 	"GET DEV QUEUE STATUS",
   3635 	NULL,
   3636 	"GET FIRMWARE STATUS",
   3637 	"GET INIT SCSI ID",
   3638 	"GET SELECT TIMEOUT",
   3639 	"GET RETRY COUNT",
   3640 	"GET TAG AGE LIMIT",
   3641 	"GET CLOCK RATE",
   3642 	"GET ACT NEG STATE",
   3643 	"GET ASYNC DATA SETUP TIME",
   3644 	"GET PCI PARAMS",
   3645 	"GET TARGET PARAMS",
   3646 	"GET DEV QUEUE PARAMS",
   3647 	"GET RESET DELAY PARAMS",
   3648 	NULL,
   3649 	NULL,
   3650 	NULL,
   3651 	NULL,
   3652 	NULL,
   3653 	"SET INIT SCSI ID",
   3654 	"SET SELECT TIMEOUT",
   3655 	"SET RETRY COUNT",
   3656 	"SET TAG AGE LIMIT",
   3657 	"SET CLOCK RATE",
   3658 	"SET ACT NEG STATE",
   3659 	"SET ASYNC DATA SETUP TIME",
   3660 	"SET PCI CONTROL PARAMS",
   3661 	"SET TARGET PARAMS",
   3662 	"SET DEV QUEUE PARAMS",
   3663 	"SET RESET DELAY PARAMS",
   3664 	NULL,
   3665 	NULL,
   3666 	NULL,
   3667 	NULL,
   3668 	NULL,
   3669 	"RETURN BIOS BLOCK ADDR",
   3670 	"WRITE FOUR RAM WORDS",
   3671 	"EXEC BIOS IOCB",
   3672 	NULL,
   3673 	NULL,
   3674 	"SET SYSTEM PARAMETER",
   3675 	"GET SYSTEM PARAMETER",
   3676 	NULL,
   3677 	"GET SCAM CONFIGURATION",
   3678 	"SET SCAM CONFIGURATION",
   3679 	"SET FIRMWARE FEATURES",
   3680 	"GET FIRMWARE FEATURES",
   3681 	NULL,
   3682 	NULL,
   3683 	NULL,
   3684 	NULL,
   3685 	"LOAD RAM A64",
   3686 	"DUMP RAM A64",
   3687 	"INITIALIZE REQUEST QUEUE A64",
   3688 	"INITIALIZE RESPONSE QUEUE A64",
   3689 	"EXECUTE IOCB A64",
   3690 	"ENABLE TARGET MODE",
   3691 	NULL,
   3692 	NULL,
   3693 	NULL,
   3694 	NULL,
   3695 	"SET DATA OVERRUN RECOVERY MODE",
   3696 	"GET DATA OVERRUN RECOVERY MODE",
   3697 	"SET HOST DATA",
   3698 	"GET NOST DATA",
   3699 };
   3700 #endif
   3701 
   3702 static u_int16_t mbpfc[] = {
   3703 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   3704 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   3705 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   3706 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   3707 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   3708 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   3709 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   3710 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   3711 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   3712 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
   3713 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
   3714 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   3715 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   3716 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   3717 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   3718 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   3719 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   3720 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
   3721 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
   3722 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   3723 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
   3724 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
   3725 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
   3726 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
   3727 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
   3728 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
   3729 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
   3730 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   3731 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
   3732 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   3733 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   3734 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   3735 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
   3736 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
   3737 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   3738 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
   3739 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
   3740 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
   3741 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
   3742 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
   3743 	ISPOPMAP(0x0f, 0x1),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
   3744 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
   3745 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
   3746 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   3747 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   3748 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   3749 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   3750 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   3751 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
   3752 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
   3753 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   3754 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
   3755 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
   3756 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
   3757 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
   3758 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
   3759 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
   3760 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
   3761 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
   3762 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   3763 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   3764 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   3765 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   3766 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   3767 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
   3768 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
   3769 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
   3770 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
   3771 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   3772 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
   3773 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
   3774 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
   3775 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
   3776 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
   3777 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
   3778 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
   3779 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   3780 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   3781 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   3782 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   3783 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
   3784 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
   3785 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
   3786 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
   3787 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   3788 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
   3789 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   3790 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   3791 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   3792 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   3793 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
   3794 	ISPOPMAP(0x00, 0x00),	/* 0x5b: */
   3795 	ISPOPMAP(0x00, 0x00),	/* 0x5c: */
   3796 	ISPOPMAP(0x00, 0x00),	/* 0x5d: */
   3797 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
   3798 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
   3799 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
   3800 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
   3801 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
   3802 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   3803 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
   3804 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
   3805 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
   3806 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
   3807 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
   3808 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
   3809 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
   3810 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
   3811 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
   3812 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
   3813 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
   3814 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
   3815 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
   3816 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
   3817 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
   3818 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
   3819 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
   3820 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
   3821 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
   3822 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
   3823 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
   3824 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
   3825 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
   3826 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
   3827 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
   3828 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
   3829 	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
   3830 };
   3831 
   3832 #ifndef	ISP_STRIPPED
   3833 static char *fc_mbcmd_names[] = {
   3834 	"NO-OP",
   3835 	"LOAD RAM",
   3836 	"EXEC FIRMWARE",
   3837 	"DUMP RAM",
   3838 	"WRITE RAM WORD",
   3839 	"READ RAM WORD",
   3840 	"MAILBOX REG TEST",
   3841 	"VERIFY CHECKSUM",
   3842 	"ABOUT FIRMWARE",
   3843 	"LOAD RAM",
   3844 	"DUMP RAM",
   3845 	NULL,
   3846 	NULL,
   3847 	NULL,
   3848 	"CHECK FIRMWARE",
   3849 	NULL,
   3850 	"INIT REQUEST QUEUE",
   3851 	"INIT RESULT QUEUE",
   3852 	"EXECUTE IOCB",
   3853 	"WAKE UP",
   3854 	"STOP FIRMWARE",
   3855 	"ABORT",
   3856 	"ABORT DEVICE",
   3857 	"ABORT TARGET",
   3858 	"BUS RESET",
   3859 	"STOP QUEUE",
   3860 	"START QUEUE",
   3861 	"SINGLE STEP QUEUE",
   3862 	"ABORT QUEUE",
   3863 	"GET DEV QUEUE STATUS",
   3864 	NULL,
   3865 	"GET FIRMWARE STATUS",
   3866 	"GET LOOP ID",
   3867 	NULL,
   3868 	"GET RETRY COUNT",
   3869 	NULL,
   3870 	NULL,
   3871 	NULL,
   3872 	NULL,
   3873 	NULL,
   3874 	"GET FIRMWARE OPTIONS",
   3875 	"GET PORT QUEUE PARAMS",
   3876 	NULL,
   3877 	NULL,
   3878 	NULL,
   3879 	NULL,
   3880 	NULL,
   3881 	NULL,
   3882 	NULL,
   3883 	NULL,
   3884 	"SET RETRY COUNT",
   3885 	NULL,
   3886 	NULL,
   3887 	NULL,
   3888 	NULL,
   3889 	NULL,
   3890 	"SET FIRMWARE OPTIONS",
   3891 	"SET PORT QUEUE PARAMS",
   3892 	NULL,
   3893 	NULL,
   3894 	NULL,
   3895 	NULL,
   3896 	NULL,
   3897 	NULL,
   3898 	"LOOP PORT BYPASS",
   3899 	"LOOP PORT ENABLE",
   3900 	"GET RESOURCE COUNTS",
   3901 	"REQUEST NON PARTICIPATING MODE",
   3902 	NULL,
   3903 	NULL,
   3904 	NULL,
   3905 	"GET PORT DATABASE,, ENHANCED",
   3906 	NULL,
   3907 	NULL,
   3908 	NULL,
   3909 	NULL,
   3910 	NULL,
   3911 	NULL,
   3912 	NULL,
   3913 	NULL,
   3914 	NULL,
   3915 	NULL,
   3916 	NULL,
   3917 	NULL,
   3918 	"EXECUTE IOCB A64",
   3919 	NULL,
   3920 	NULL,
   3921 	NULL,
   3922 	NULL,
   3923 	NULL,
   3924 	NULL,
   3925 	NULL,
   3926 	NULL,
   3927 	NULL,
   3928 	NULL,
   3929 	NULL,
   3930 	"INIT FIRMWARE",
   3931 	NULL,
   3932 	"INIT LIP",
   3933 	"GET FC-AL POSITION MAP",
   3934 	"GET PORT DATABASE",
   3935 	"CLEAR ACA",
   3936 	"TARGET RESET",
   3937 	"CLEAR TASK SET",
   3938 	"ABORT TASK SET",
   3939 	"GET FW STATE",
   3940 	"GET PORT NAME",
   3941 	"GET LINK STATUS",
   3942 	"INIT LIP RESET",
   3943 	NULL,
   3944 	"SEND SNS",
   3945 	"FABRIC LOGIN",
   3946 	"SEND CHANGE REQUEST",
   3947 	"FABRIC LOGOUT",
   3948 	"INIT LIP LOGIN",
   3949 	NULL,
   3950 	"LOGIN LOOP PORT",
   3951 	"GET PORT/NODE NAME LIST",
   3952 	"SET VENDOR ID",
   3953 	"INITIALIZE IP MAILBOX",
   3954 	NULL,
   3955 	NULL,
   3956 	NULL,
   3957 	NULL,
   3958 	"Get ID List",
   3959 	"SEND LFA",
   3960 	"Lun RESET"
   3961 };
   3962 #endif
   3963 
   3964 static void
   3965 isp_mboxcmd(isp, mbp, logmask)
   3966 	struct ispsoftc *isp;
   3967 	mbreg_t *mbp;
   3968 	int logmask;
   3969 {
   3970 	char *cname, *xname, tname[16], mname[16];
   3971 	unsigned int lim, ibits, obits, box, opcode;
   3972 	u_int16_t *mcp;
   3973 
   3974 	if (IS_FC(isp)) {
   3975 		mcp = mbpfc;
   3976 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   3977 	} else {
   3978 		mcp = mbpscsi;
   3979 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   3980 	}
   3981 
   3982 	if ((opcode = mbp->param[0]) >= lim) {
   3983 		mbp->param[0] = MBOX_INVALID_COMMAND;
   3984 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
   3985 		return;
   3986 	}
   3987 
   3988 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   3989 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   3990 
   3991 	if (ibits == 0 && obits == 0) {
   3992 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
   3993 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
   3994 		return;
   3995 	}
   3996 
   3997 	/*
   3998 	 * Get exclusive usage of mailbox registers.
   3999 	 */
   4000 	MBOX_ACQUIRE(isp);
   4001 
   4002 	for (box = 0; box < MAX_MAILBOX; box++) {
   4003 		if (ibits & (1 << box)) {
   4004 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   4005 		}
   4006 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   4007 	}
   4008 
   4009 	/*
   4010 	 * We assume that we can't overwrite a previous command.
   4011 	 */
   4012 	isp->isp_mboxbsy = obits;
   4013 
   4014 	/*
   4015 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   4016 	 */
   4017 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   4018 
   4019 	/*
   4020 	 * Give the f/w a chance to pick this up.
   4021 	 */
   4022 	USEC_DELAY(250);
   4023 
   4024 
   4025 	/*
   4026 	 * While we haven't finished the command, spin our wheels here.
   4027 	 */
   4028 	MBOX_WAIT_COMPLETE(isp);
   4029 
   4030 	/*
   4031 	 * Copy back output registers.
   4032 	 */
   4033 	for (box = 0; box < MAX_MAILBOX; box++) {
   4034 		if (obits & (1 << box)) {
   4035 			mbp->param[box] = isp->isp_mboxtmp[box];
   4036 		}
   4037 	}
   4038 
   4039 	MBOX_RELEASE(isp);
   4040 
   4041 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
   4042 		return;
   4043 	}
   4044 #ifdef	ISP_STRIPPED
   4045 	cname = NULL;
   4046 #else
   4047 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
   4048 #endif
   4049 	if (cname == NULL) {
   4050 		SNPRINTF(cname, sizeof tname, "opcode %x", opcode);
   4051 	}
   4052 
   4053 	/*
   4054 	 * Just to be chatty here...
   4055 	 */
   4056 	xname = NULL;
   4057 	switch (mbp->param[0]) {
   4058 	case MBOX_COMMAND_COMPLETE:
   4059 		break;
   4060 	case MBOX_INVALID_COMMAND:
   4061 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
   4062 			xname = "INVALID COMMAND";
   4063 		break;
   4064 	case MBOX_HOST_INTERFACE_ERROR:
   4065 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
   4066 			xname = "HOST INTERFACE ERROR";
   4067 		break;
   4068 	case MBOX_TEST_FAILED:
   4069 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
   4070 			xname = "TEST FAILED";
   4071 		break;
   4072 	case MBOX_COMMAND_ERROR:
   4073 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
   4074 			xname = "COMMAND ERROR";
   4075 		break;
   4076 	case MBOX_COMMAND_PARAM_ERROR:
   4077 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
   4078 			xname = "COMMAND PARAMETER ERROR";
   4079 		break;
   4080 	case MBOX_LOOP_ID_USED:
   4081 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
   4082 			xname = "LOOP ID ALREADY IN USE";
   4083 		break;
   4084 	case MBOX_PORT_ID_USED:
   4085 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
   4086 			xname = "PORT ID ALREADY IN USE";
   4087 		break;
   4088 	case MBOX_ALL_IDS_USED:
   4089 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
   4090 			xname = "ALL LOOP IDS IN USE";
   4091 		break;
   4092 	case 0:		/* special case */
   4093 		xname = "TIMEOUT";
   4094 		break;
   4095 	default:
   4096 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
   4097 		xname = mname;
   4098 		break;
   4099 	}
   4100 	if (xname)
   4101 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
   4102 		    cname, xname);
   4103 }
   4104 
   4105 static void
   4106 isp_fw_state(isp)
   4107 	struct ispsoftc *isp;
   4108 {
   4109 	if (IS_FC(isp)) {
   4110 		mbreg_t mbs;
   4111 		fcparam *fcp = isp->isp_param;
   4112 
   4113 		mbs.param[0] = MBOX_GET_FW_STATE;
   4114 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4115 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE)
   4116 			fcp->isp_fwstate = mbs.param[1];
   4117 	}
   4118 }
   4119 
   4120 static void
   4121 isp_update(isp)
   4122 	struct ispsoftc *isp;
   4123 {
   4124 	int bus, upmask;
   4125 
   4126 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
   4127 		if (upmask & (1 << bus)) {
   4128 			isp_update_bus(isp, bus);
   4129 		}
   4130 		upmask &= ~(1 << bus);
   4131 	}
   4132 }
   4133 
   4134 static void
   4135 isp_update_bus(isp, bus)
   4136 	struct ispsoftc *isp;
   4137 	int bus;
   4138 {
   4139 	int tgt;
   4140 	mbreg_t mbs;
   4141 	sdparam *sdp;
   4142 
   4143 	isp->isp_update &= ~(1 << bus);
   4144 	if (IS_FC(isp)) {
   4145 		/*
   4146 		 * There are no 'per-bus' settings for Fibre Channel.
   4147 		 */
   4148 		return;
   4149 	}
   4150 	sdp = isp->isp_param;
   4151 	sdp += bus;
   4152 
   4153 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4154 		u_int16_t flags, period, offset;
   4155 		int get;
   4156 
   4157 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   4158 			sdp->isp_devparam[tgt].dev_update = 0;
   4159 			sdp->isp_devparam[tgt].dev_refresh = 0;
   4160 			isp_prt(isp, ISP_LOGDEBUG1,
   4161 	 		    "skipping target %d bus %d update", tgt, bus);
   4162 			continue;
   4163 		}
   4164 		/*
   4165 		 * If the goal is to update the status of the device,
   4166 		 * take what's in dev_flags and try and set the device
   4167 		 * toward that. Otherwise, if we're just refreshing the
   4168 		 * current device state, get the current parameters.
   4169 		 */
   4170 
   4171 		/*
   4172 		 * Refresh overrides set
   4173 		 */
   4174 		if (sdp->isp_devparam[tgt].dev_refresh) {
   4175 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   4176 			sdp->isp_devparam[tgt].dev_refresh = 0;
   4177 			get = 1;
   4178 		} else if (sdp->isp_devparam[tgt].dev_update) {
   4179 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   4180 			/*
   4181 			 * Make sure dev_flags has "Renegotiate on Error"
   4182 			 * on and "Freeze Queue on Error" off.
   4183 			 */
   4184 			sdp->isp_devparam[tgt].dev_flags |= DPARM_RENEG;
   4185 			sdp->isp_devparam[tgt].dev_flags &= ~DPARM_QFRZ;
   4186 
   4187 			mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
   4188 
   4189 			/*
   4190 			 * Insist that PARITY must be enabled
   4191 			 * if SYNC or WIDE is enabled.
   4192 			 */
   4193 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
   4194 				mbs.param[2] |= DPARM_PARITY;
   4195 			}
   4196 
   4197 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
   4198 				mbs.param[3] = 0;
   4199 			} else {
   4200 				mbs.param[3] =
   4201 				    (sdp->isp_devparam[tgt].sync_offset << 8) |
   4202 				    (sdp->isp_devparam[tgt].sync_period);
   4203 			}
   4204 			/*
   4205 			 * A command completion later that has
   4206 			 * RQSTF_NEGOTIATION set canl cause
   4207 			 * the dev_refresh/announce cycle also.
   4208 			 &
   4209 			 *
   4210 			 * Note: It is really important to update our current
   4211 			 * flags with at least the state of TAG capabilities-
   4212 			 * otherwise we might try and send a tagged command
   4213 			 * when we have it all turned off. So change it here
   4214 			 * to say that current already matches goal.
   4215 			 */
   4216 			sdp->isp_devparam[tgt].cur_dflags &= ~DPARM_TQING;
   4217 			sdp->isp_devparam[tgt].cur_dflags |=
   4218 			    (sdp->isp_devparam[tgt].dev_flags & DPARM_TQING);
   4219 			isp_prt(isp, ISP_LOGDEBUG2,
   4220 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
   4221 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
   4222 			    mbs.param[3] & 0xff);
   4223 			sdp->isp_devparam[tgt].dev_update = 0;
   4224 			sdp->isp_devparam[tgt].dev_refresh = 1;
   4225 			get = 0;
   4226 		} else {
   4227 			continue;
   4228 		}
   4229 		mbs.param[1] = (bus << 15) | (tgt << 8) ;
   4230 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4231 		if (get == 0) {
   4232 			isp->isp_sendmarker |= (1 << bus);
   4233 			continue;
   4234 		}
   4235 		flags = mbs.param[2];
   4236 		period = mbs.param[3] & 0xff;
   4237 		offset = mbs.param[3] >> 8;
   4238 		sdp->isp_devparam[tgt].cur_dflags = flags;
   4239 		sdp->isp_devparam[tgt].cur_period = period;
   4240 		sdp->isp_devparam[tgt].cur_offset = offset;
   4241 		get = (bus << 16) | tgt;
   4242 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
   4243 	}
   4244 
   4245 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4246 		if (sdp->isp_devparam[tgt].dev_update ||
   4247 		    sdp->isp_devparam[tgt].dev_refresh) {
   4248 			isp->isp_update |= (1 << bus);
   4249 			break;
   4250 		}
   4251 	}
   4252 }
   4253 
   4254 static void
   4255 isp_setdfltparm(isp, channel)
   4256 	struct ispsoftc *isp;
   4257 	int channel;
   4258 {
   4259 	int tgt;
   4260 	mbreg_t mbs;
   4261 	sdparam *sdp;
   4262 
   4263 	if (IS_FC(isp)) {
   4264 		fcparam *fcp = (fcparam *) isp->isp_param;
   4265 		fcp += channel;
   4266 		if (fcp->isp_gotdparms) {
   4267 			return;
   4268 		}
   4269 		fcp->isp_gotdparms = 1;
   4270 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
   4271 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
   4272 		fcp->isp_execthrottle = ISP_EXEC_THROTTLE;
   4273 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
   4274 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
   4275 		/* Platform specific.... */
   4276 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
   4277 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
   4278 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
   4279 		fcp->isp_fwoptions = 0;
   4280 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
   4281 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
   4282 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
   4283 #ifndef	ISP_NO_FASTPOST_FC
   4284 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
   4285 #endif
   4286 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
   4287 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
   4288 
   4289 		/*
   4290 		 * Make sure this is turned off now until we get
   4291 		 * extended options from NVRAM
   4292 		 */
   4293 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
   4294 
   4295 		/*
   4296 		 * Now try and read NVRAM
   4297 		 */
   4298 		if ((isp->isp_confopts & (ISP_CFG_NONVRAM|ISP_CFG_OWNWWN)) ||
   4299 		    (isp_read_nvram(isp))) {
   4300 			isp_prt(isp, ISP_LOGINFO,
   4301 			    "Node WWN 0x%08x%08x, Port WWN 0x%08x%08x",
   4302 			    (u_int32_t) (fcp->isp_nodewwn >> 32),
   4303 			    (u_int32_t) (fcp->isp_nodewwn & 0xffffffff),
   4304 			    (u_int32_t) (fcp->isp_portwwn >> 32),
   4305 			    (u_int32_t) (fcp->isp_portwwn & 0xffffffff));
   4306 		}
   4307 		return;
   4308 	}
   4309 
   4310 	sdp = (sdparam *) isp->isp_param;
   4311 	sdp += channel;
   4312 
   4313 	/*
   4314 	 * Been there, done that, got the T-shirt...
   4315 	 */
   4316 	if (sdp->isp_gotdparms) {
   4317 		return;
   4318 	}
   4319 	sdp->isp_gotdparms = 1;
   4320 
   4321 	/*
   4322 	 * If we've not been told to avoid reading NVRAM, try and read it.
   4323 	 * If we're successful reading it, we can return since NVRAM will
   4324 	 * tell us the right thing to do. Otherwise, establish some reasonable
   4325 	 * defaults.
   4326 	 */
   4327 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4328 		if (isp_read_nvram(isp) == 0) {
   4329 			return;
   4330 		}
   4331 	}
   4332 
   4333 	/*
   4334 	 * Now try and see whether we have specific values for them.
   4335 	 */
   4336 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4337 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   4338 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4339 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4340 			sdp->isp_req_ack_active_neg = 1;
   4341 			sdp->isp_data_line_active_neg = 1;
   4342 		} else {
   4343 			sdp->isp_req_ack_active_neg =
   4344 			    (mbs.param[1+channel] >> 4) & 0x1;
   4345 			sdp->isp_data_line_active_neg =
   4346 			    (mbs.param[1+channel] >> 5) & 0x1;
   4347 		}
   4348 	} else {
   4349 		sdp->isp_req_ack_active_neg = 1;
   4350 		sdp->isp_data_line_active_neg = 1;
   4351 	}
   4352 
   4353 	isp_prt(isp, ISP_LOGDEBUG1,
   4354 	    "defaulting bus %d REQ/ACK Active Negation is %d",
   4355 	    channel, sdp->isp_req_ack_active_neg);
   4356 	isp_prt(isp, ISP_LOGDEBUG1,
   4357 	    "defaulting bus %d DATA Active Negation is %d",
   4358 	    channel, sdp->isp_data_line_active_neg);
   4359 
   4360 	/*
   4361 	 * The trick here is to establish a default for the default (honk!)
   4362 	 * state (dev_flags). Then try and get the current status from
   4363 	 * the card to fill in the current state. We don't, in fact, set
   4364 	 * the default to the SAFE default state- that's not the goal state.
   4365 	 */
   4366 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4367 		sdp->isp_devparam[tgt].cur_offset = 0;
   4368 		sdp->isp_devparam[tgt].cur_period = 0;
   4369 		sdp->isp_devparam[tgt].dev_flags = DPARM_DEFAULT;
   4370 		sdp->isp_devparam[tgt].cur_dflags = 0;
   4371 		/*
   4372 		 * We default to Wide/Fast for versions less than a 1040
   4373 		 * (unless it's SBus).
   4374 		 */
   4375 		if ((isp->isp_bustype == ISP_BT_SBUS &&
   4376 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
   4377 		    (isp->isp_bustype == ISP_BT_PCI &&
   4378 		    isp->isp_type < ISP_HA_SCSI_1040) ||
   4379 		    (isp->isp_clock && isp->isp_clock < 60) ||
   4380 		    (sdp->isp_ultramode == 0)) {
   4381 			sdp->isp_devparam[tgt].sync_offset =
   4382 			    ISP_10M_SYNCPARMS >> 8;
   4383 			sdp->isp_devparam[tgt].sync_period =
   4384 			    ISP_10M_SYNCPARMS & 0xff;
   4385 		} else if (IS_ULTRA3(isp)) {
   4386 			sdp->isp_devparam[tgt].sync_offset =
   4387 			    ISP_80M_SYNCPARMS >> 8;
   4388 			sdp->isp_devparam[tgt].sync_period =
   4389 			    ISP_80M_SYNCPARMS & 0xff;
   4390 		} else if (IS_ULTRA2(isp)) {
   4391 			sdp->isp_devparam[tgt].sync_offset =
   4392 			    ISP_40M_SYNCPARMS >> 8;
   4393 			sdp->isp_devparam[tgt].sync_period =
   4394 			    ISP_40M_SYNCPARMS & 0xff;
   4395 		} else if (IS_1240(isp)) {
   4396 			sdp->isp_devparam[tgt].sync_offset =
   4397 			    ISP_20M_SYNCPARMS >> 8;
   4398 			sdp->isp_devparam[tgt].sync_period =
   4399 			    ISP_20M_SYNCPARMS & 0xff;
   4400 		} else {
   4401 			sdp->isp_devparam[tgt].sync_offset =
   4402 			    ISP_20M_SYNCPARMS_1040 >> 8;
   4403 			sdp->isp_devparam[tgt].sync_period =
   4404 			    ISP_20M_SYNCPARMS_1040 & 0xff;
   4405 		}
   4406 
   4407 		/*
   4408 		 * Don't get current target parameters if we've been
   4409 		 * told not to use NVRAM- it's really the same thing.
   4410 		 */
   4411 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4412 
   4413 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   4414 			mbs.param[1] = tgt << 8;
   4415 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   4416 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4417 				continue;
   4418 			}
   4419 			sdp->isp_devparam[tgt].cur_dflags = mbs.param[2];
   4420 			sdp->isp_devparam[tgt].dev_flags = mbs.param[2];
   4421 			sdp->isp_devparam[tgt].cur_period = mbs.param[3] & 0xff;
   4422 			sdp->isp_devparam[tgt].cur_offset = mbs.param[3] >> 8;
   4423 
   4424 			/*
   4425 			 * The maximum period we can really see
   4426 			 * here is 100 (decimal), or 400 ns.
   4427 			 * For some unknown reason we sometimes
   4428 			 * get back wildass numbers from the
   4429 			 * boot device's parameters (alpha only).
   4430 			 */
   4431 			if ((mbs.param[3] & 0xff) <= 0x64) {
   4432 				sdp->isp_devparam[tgt].sync_period =
   4433 				    mbs.param[3] & 0xff;
   4434 				sdp->isp_devparam[tgt].sync_offset =
   4435 				    mbs.param[3] >> 8;
   4436 			}
   4437 
   4438 			/*
   4439 			 * It is not safe to run Ultra Mode with a clock < 60.
   4440 			 */
   4441 			if (((isp->isp_clock && isp->isp_clock < 60) ||
   4442 			    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
   4443 			    (sdp->isp_devparam[tgt].sync_period <=
   4444 			    (ISP_20M_SYNCPARMS & 0xff))) {
   4445 				sdp->isp_devparam[tgt].sync_offset =
   4446 				    ISP_10M_SYNCPARMS >> 8;
   4447 				sdp->isp_devparam[tgt].sync_period =
   4448 				    ISP_10M_SYNCPARMS & 0xff;
   4449 			}
   4450 		}
   4451 		isp_prt(isp, ISP_LOGDEBUG1,
   4452 		    "Initial bus %d tgt %d flags %x offset %x period %x",
   4453 		    channel, tgt, sdp->isp_devparam[tgt].dev_flags,
   4454 		    sdp->isp_devparam[tgt].sync_offset,
   4455 		    sdp->isp_devparam[tgt].sync_period);
   4456 	}
   4457 
   4458 	/*
   4459 	 * Establish default some more default parameters.
   4460 	 */
   4461 	sdp->isp_cmd_dma_burst_enable = 1;
   4462 	sdp->isp_data_dma_burst_enabl = 1;
   4463 	sdp->isp_fifo_threshold = 0;
   4464 	sdp->isp_initiator_id = DEFAULT_IID(isp);
   4465 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   4466 		sdp->isp_async_data_setup = 9;
   4467 	} else {
   4468 		sdp->isp_async_data_setup = 6;
   4469 	}
   4470 	sdp->isp_selection_timeout = 250;
   4471 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
   4472 	sdp->isp_tag_aging = 8;
   4473 	sdp->isp_bus_reset_delay = 3;
   4474 	sdp->isp_retry_count = 2;
   4475 	sdp->isp_retry_delay = 2;
   4476 
   4477 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4478 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
   4479 		sdp->isp_devparam[tgt].dev_enable = 1;
   4480 	}
   4481 }
   4482 
   4483 /*
   4484  * Re-initialize the ISP and complete all orphaned commands
   4485  * with a 'botched' notice. The reset/init routines should
   4486  * not disturb an already active list of commands.
   4487  *
   4488  * Locks held prior to coming here.
   4489  */
   4490 
   4491 void
   4492 isp_reinit(isp)
   4493 	struct ispsoftc *isp;
   4494 {
   4495 	XS_T *xs;
   4496 	u_int32_t handle;
   4497 
   4498 	isp_reset(isp);
   4499 	if (isp->isp_state == ISP_RESETSTATE) {
   4500 		isp_init(isp);
   4501 		if (isp->isp_state == ISP_INITSTATE) {
   4502 			isp->isp_state = ISP_RUNSTATE;
   4503 		}
   4504 	}
   4505 	if (isp->isp_state != ISP_RUNSTATE) {
   4506 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot restart ISP");
   4507 	}
   4508 	isp->isp_nactive = 0;
   4509 
   4510 	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
   4511 		xs = isp_find_xs(isp, handle);
   4512 		if (xs == NULL) {
   4513 			continue;
   4514 		}
   4515 		isp_destroy_handle(isp, handle);
   4516 		if (XS_XFRLEN(xs)) {
   4517 			ISP_DMAFREE(isp, xs, handle);
   4518 			XS_RESID(xs) = XS_XFRLEN(xs);
   4519 		} else {
   4520 			XS_RESID(xs) = 0;
   4521 		}
   4522 		XS_SETERR(xs, HBA_BUSRESET);
   4523 		isp_done(xs);
   4524 	}
   4525 }
   4526 
   4527 /*
   4528  * NVRAM Routines
   4529  */
   4530 static int
   4531 isp_read_nvram(isp)
   4532 	struct ispsoftc *isp;
   4533 {
   4534 	int i, amt;
   4535 	u_int8_t csum, minversion;
   4536 	union {
   4537 		u_int8_t _x[ISP2100_NVRAM_SIZE];
   4538 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
   4539 	} _n;
   4540 #define	nvram_data	_n._x
   4541 #define	nvram_words	_n._s
   4542 
   4543 	if (IS_FC(isp)) {
   4544 		amt = ISP2100_NVRAM_SIZE;
   4545 		minversion = 1;
   4546 	} else if (IS_ULTRA2(isp)) {
   4547 		amt = ISP1080_NVRAM_SIZE;
   4548 		minversion = 0;
   4549 	} else {
   4550 		amt = ISP_NVRAM_SIZE;
   4551 		minversion = 2;
   4552 	}
   4553 
   4554 	/*
   4555 	 * Just read the first two words first to see if we have a valid
   4556 	 * NVRAM to continue reading the rest with.
   4557 	 */
   4558 	for (i = 0; i < 2; i++) {
   4559 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   4560 	}
   4561 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   4562 	    nvram_data[2] != 'P') {
   4563 		if (isp->isp_bustype != ISP_BT_SBUS) {
   4564 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
   4565 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
   4566 			    nvram_data[0], nvram_data[1], nvram_data[2]);
   4567 		}
   4568 		return (-1);
   4569 	}
   4570 	for (i = 2; i < amt>>1; i++) {
   4571 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   4572 	}
   4573 	for (csum = 0, i = 0; i < amt; i++) {
   4574 		csum += nvram_data[i];
   4575 	}
   4576 	if (csum != 0) {
   4577 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
   4578 		return (-1);
   4579 	}
   4580 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   4581 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
   4582 		    ISP_NVRAM_VERSION(nvram_data));
   4583 		return (-1);
   4584 	}
   4585 
   4586 	if (IS_ULTRA3(isp)) {
   4587 		isp_parse_nvram_12160(isp, 0, nvram_data);
   4588 		isp_parse_nvram_12160(isp, 1, nvram_data);
   4589 	} else if (IS_1080(isp)) {
   4590 		isp_parse_nvram_1080(isp, 0, nvram_data);
   4591 	} else if (IS_1280(isp) || IS_1240(isp)) {
   4592 		isp_parse_nvram_1080(isp, 0, nvram_data);
   4593 		isp_parse_nvram_1080(isp, 1, nvram_data);
   4594 	} else if (IS_SCSI(isp)) {
   4595 		isp_parse_nvram_1020(isp, nvram_data);
   4596 	} else {
   4597 		isp_parse_nvram_2100(isp, nvram_data);
   4598 	}
   4599 	return (0);
   4600 #undef	nvram_data
   4601 #undef	nvram_words
   4602 }
   4603 
   4604 static void
   4605 isp_rdnvram_word(isp, wo, rp)
   4606 	struct ispsoftc *isp;
   4607 	int wo;
   4608 	u_int16_t *rp;
   4609 {
   4610 	int i, cbits;
   4611 	u_int16_t bit, rqst;
   4612 
   4613 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   4614 	USEC_DELAY(2);
   4615 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   4616 	USEC_DELAY(2);
   4617 
   4618 	if (IS_FC(isp)) {
   4619 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   4620 		rqst = (ISP_NVRAM_READ << 8) | wo;
   4621 		cbits = 10;
   4622 	} else if (IS_ULTRA2(isp)) {
   4623 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
   4624 		rqst = (ISP_NVRAM_READ << 8) | wo;
   4625 		cbits = 10;
   4626 	} else {
   4627 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   4628 		rqst = (ISP_NVRAM_READ << 6) | wo;
   4629 		cbits = 8;
   4630 	}
   4631 
   4632 	/*
   4633 	 * Clock the word select request out...
   4634 	 */
   4635 	for (i = cbits; i >= 0; i--) {
   4636 		if ((rqst >> i) & 1) {
   4637 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   4638 		} else {
   4639 			bit = BIU_NVRAM_SELECT;
   4640 		}
   4641 		ISP_WRITE(isp, BIU_NVRAM, bit);
   4642 		USEC_DELAY(2);
   4643 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   4644 		USEC_DELAY(2);
   4645 		ISP_WRITE(isp, BIU_NVRAM, bit);
   4646 		USEC_DELAY(2);
   4647 	}
   4648 	/*
   4649 	 * Now read the result back in (bits come back in MSB format).
   4650 	 */
   4651 	*rp = 0;
   4652 	for (i = 0; i < 16; i++) {
   4653 		u_int16_t rv;
   4654 		*rp <<= 1;
   4655 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   4656 		USEC_DELAY(2);
   4657 		rv = ISP_READ(isp, BIU_NVRAM);
   4658 		if (rv & BIU_NVRAM_DATAIN) {
   4659 			*rp |= 1;
   4660 		}
   4661 		USEC_DELAY(2);
   4662 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   4663 		USEC_DELAY(2);
   4664 	}
   4665 	ISP_WRITE(isp, BIU_NVRAM, 0);
   4666 	USEC_DELAY(2);
   4667 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
   4668 }
   4669 
   4670 static void
   4671 isp_parse_nvram_1020(isp, nvram_data)
   4672 	struct ispsoftc *isp;
   4673 	u_int8_t *nvram_data;
   4674 {
   4675 	int i;
   4676 	sdparam *sdp = (sdparam *) isp->isp_param;
   4677 
   4678 	sdp->isp_fifo_threshold =
   4679 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
   4680 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
   4681 
   4682 	sdp->isp_initiator_id =
   4683 		ISP_NVRAM_INITIATOR_ID(nvram_data);
   4684 
   4685 	sdp->isp_bus_reset_delay =
   4686 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   4687 
   4688 	sdp->isp_retry_count =
   4689 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   4690 
   4691 	sdp->isp_retry_delay =
   4692 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   4693 
   4694 	sdp->isp_async_data_setup =
   4695 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   4696 
   4697 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   4698 		if (sdp->isp_async_data_setup < 9) {
   4699 			sdp->isp_async_data_setup = 9;
   4700 		}
   4701 	} else {
   4702 		if (sdp->isp_async_data_setup != 6) {
   4703 			sdp->isp_async_data_setup = 6;
   4704 		}
   4705 	}
   4706 
   4707 	sdp->isp_req_ack_active_neg =
   4708 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   4709 
   4710 	sdp->isp_data_line_active_neg =
   4711 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   4712 
   4713 	sdp->isp_data_dma_burst_enabl =
   4714 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   4715 
   4716 	sdp->isp_cmd_dma_burst_enable =
   4717 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   4718 
   4719 	sdp->isp_tag_aging =
   4720 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   4721 
   4722 	sdp->isp_selection_timeout =
   4723 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   4724 
   4725 	sdp->isp_max_queue_depth =
   4726 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   4727 
   4728 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   4729 	for (i = 0; i < MAX_TARGETS; i++) {
   4730 		sdp->isp_devparam[i].dev_enable =
   4731 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
   4732 		sdp->isp_devparam[i].exc_throttle =
   4733 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
   4734 		sdp->isp_devparam[i].sync_offset =
   4735 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
   4736 		sdp->isp_devparam[i].sync_period =
   4737 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
   4738 
   4739 		if (isp->isp_type < ISP_HA_SCSI_1040) {
   4740 			/*
   4741 			 * If we're not ultra, we can't possibly
   4742 			 * be a shorter period than this.
   4743 			 */
   4744 			if (sdp->isp_devparam[i].sync_period < 0x19) {
   4745 				sdp->isp_devparam[i].sync_period = 0x19;
   4746 			}
   4747 			if (sdp->isp_devparam[i].sync_offset > 0xc) {
   4748 				sdp->isp_devparam[i].sync_offset = 0x0c;
   4749 			}
   4750 		} else {
   4751 			if (sdp->isp_devparam[i].sync_offset > 0x8) {
   4752 				sdp->isp_devparam[i].sync_offset = 0x8;
   4753 			}
   4754 		}
   4755 		sdp->isp_devparam[i].dev_flags = 0;
   4756 		if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
   4757 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   4758 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   4759 		if (ISP_NVRAM_TGT_TQING(nvram_data, i))
   4760 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   4761 		if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
   4762 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   4763 		if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
   4764 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   4765 		if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
   4766 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   4767 		if (ISP_NVRAM_TGT_DISC(nvram_data, i))
   4768 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   4769 		sdp->isp_devparam[i].cur_dflags = 0; /* we don't know */
   4770 	}
   4771 }
   4772 
   4773 static void
   4774 isp_parse_nvram_1080(isp, bus, nvram_data)
   4775 	struct ispsoftc *isp;
   4776 	int bus;
   4777 	u_int8_t *nvram_data;
   4778 {
   4779 	int i;
   4780 	sdparam *sdp = (sdparam *) isp->isp_param;
   4781 	sdp += bus;
   4782 
   4783 	sdp->isp_fifo_threshold =
   4784 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
   4785 
   4786 	sdp->isp_initiator_id =
   4787 	    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
   4788 
   4789 	sdp->isp_bus_reset_delay =
   4790 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   4791 
   4792 	sdp->isp_retry_count =
   4793 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   4794 
   4795 	sdp->isp_retry_delay =
   4796 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   4797 
   4798 	sdp->isp_async_data_setup =
   4799 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
   4800 	    bus);
   4801 
   4802 	sdp->isp_req_ack_active_neg =
   4803 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
   4804 	    bus);
   4805 
   4806 	sdp->isp_data_line_active_neg =
   4807 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
   4808 	    bus);
   4809 
   4810 	sdp->isp_data_dma_burst_enabl =
   4811 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   4812 
   4813 	sdp->isp_cmd_dma_burst_enable =
   4814 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   4815 
   4816 	sdp->isp_selection_timeout =
   4817 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   4818 
   4819 	sdp->isp_max_queue_depth =
   4820 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   4821 
   4822 	for (i = 0; i < MAX_TARGETS; i++) {
   4823 		sdp->isp_devparam[i].dev_enable =
   4824 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
   4825 		sdp->isp_devparam[i].exc_throttle =
   4826 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
   4827 		sdp->isp_devparam[i].sync_offset =
   4828 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
   4829 		sdp->isp_devparam[i].sync_period =
   4830 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
   4831 		sdp->isp_devparam[i].dev_flags = 0;
   4832 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, i, bus))
   4833 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   4834 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   4835 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, i, bus))
   4836 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   4837 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, i, bus))
   4838 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   4839 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, i, bus))
   4840 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   4841 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, i, bus))
   4842 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   4843 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, i, bus))
   4844 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   4845 		sdp->isp_devparam[i].cur_dflags = 0;
   4846 	}
   4847 }
   4848 
   4849 static void
   4850 isp_parse_nvram_12160(isp, bus, nvram_data)
   4851 	struct ispsoftc *isp;
   4852 	int bus;
   4853 	u_int8_t *nvram_data;
   4854 {
   4855 	sdparam *sdp = (sdparam *) isp->isp_param;
   4856 	int i;
   4857 
   4858 	sdp += bus;
   4859 
   4860 	sdp->isp_fifo_threshold =
   4861 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
   4862 
   4863 	sdp->isp_initiator_id =
   4864 	    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
   4865 
   4866 	sdp->isp_bus_reset_delay =
   4867 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   4868 
   4869 	sdp->isp_retry_count =
   4870 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   4871 
   4872 	sdp->isp_retry_delay =
   4873 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   4874 
   4875 	sdp->isp_async_data_setup =
   4876 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
   4877 	    bus);
   4878 
   4879 	sdp->isp_req_ack_active_neg =
   4880 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
   4881 	    bus);
   4882 
   4883 	sdp->isp_data_line_active_neg =
   4884 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
   4885 	    bus);
   4886 
   4887 	sdp->isp_data_dma_burst_enabl =
   4888 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   4889 
   4890 	sdp->isp_cmd_dma_burst_enable =
   4891 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   4892 
   4893 	sdp->isp_selection_timeout =
   4894 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   4895 
   4896 	sdp->isp_max_queue_depth =
   4897 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   4898 
   4899 	for (i = 0; i < MAX_TARGETS; i++) {
   4900 		sdp->isp_devparam[i].dev_enable =
   4901 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
   4902 		sdp->isp_devparam[i].exc_throttle =
   4903 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
   4904 		sdp->isp_devparam[i].sync_offset =
   4905 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
   4906 		sdp->isp_devparam[i].sync_period =
   4907 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
   4908 		sdp->isp_devparam[i].dev_flags = 0;
   4909 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, i, bus))
   4910 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   4911 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   4912 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, i, bus))
   4913 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   4914 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, i, bus))
   4915 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   4916 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, i, bus))
   4917 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   4918 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, i, bus))
   4919 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   4920 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, i, bus))
   4921 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   4922 		sdp->isp_devparam[i].cur_dflags = 0;
   4923 	}
   4924 }
   4925 
   4926 static void
   4927 isp_parse_nvram_2100(isp, nvram_data)
   4928 	struct ispsoftc *isp;
   4929 	u_int8_t *nvram_data;
   4930 {
   4931 	fcparam *fcp = (fcparam *) isp->isp_param;
   4932 	u_int64_t wwn;
   4933 
   4934 	/*
   4935 	 * There is supposed to be WWNN storage as distinct
   4936 	 * from WWPN storage in NVRAM, but it doesn't appear
   4937 	 * to be used sanely across all cards.
   4938 	 */
   4939 
   4940 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
   4941 	if (wwn != 0LL) {
   4942 		switch ((int) (wwn >> 60)) {
   4943 		case 0:
   4944 			/*
   4945 			 * Broken PTI cards with nothing in the top nibble. Pah.
   4946 			 */
   4947 			wwn |= (2LL << 60);
   4948 			/* FALLTHROUGH */
   4949 		case 2:
   4950 			fcp->isp_nodewwn = wwn;
   4951 			fcp->isp_nodewwn &= ~((0xfffLL) << 48);
   4952 			fcp->isp_portwwn =
   4953 			    PORT_FROM_NODE_WWN(isp, fcp->isp_nodewwn);
   4954 			break;
   4955 		default:
   4956 			fcp->isp_portwwn = fcp->isp_nodewwn = wwn;
   4957 		}
   4958 	}
   4959 	isp_prt(isp, ISP_LOGCONFIG, "NVRAM Derived Node WWN 0x%08x%08x",
   4960 	    (u_int32_t) (fcp->isp_nodewwn >> 32),
   4961 	    (u_int32_t) (fcp->isp_nodewwn & 0xffffffff));
   4962 	isp_prt(isp, ISP_LOGCONFIG, "NVRAM Derived Port WWN 0x%08x%08x",
   4963 	    (u_int32_t) (fcp->isp_portwwn >> 32),
   4964 	    (u_int32_t) (fcp->isp_portwwn & 0xffffffff));
   4965 	fcp->isp_maxalloc =
   4966 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   4967 	fcp->isp_maxfrmlen =
   4968 		ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   4969 	fcp->isp_retry_delay =
   4970 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   4971 	fcp->isp_retry_count =
   4972 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   4973 	fcp->isp_loopid =
   4974 		ISP2100_NVRAM_HARDLOOPID(nvram_data);
   4975 	fcp->isp_execthrottle =
   4976 		ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   4977 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
   4978 }
   4979