Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.28
      1 /* $NetBSD: isp.c,v 1.28 1998/12/05 19:43:52 mjacob Exp $ */
      2 /* isp.c 1.28 */
      3 /*
      4  * Machine and OS Independent (well, as best as possible)
      5  * code for the Qlogic ISP SCSI adapters.
      6  *
      7  *---------------------------------------
      8  * Copyright (c) 1997, 1998 by Matthew Jacob
      9  * NASA/Ames Research Center
     10  * All rights reserved.
     11  *---------------------------------------
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice immediately at the beginning of the file, without modification,
     18  *    this list of conditions, and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. The name of the author may not be used to endorse or promote products
     23  *    derived from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     29  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
     40  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
     41  * ideas dredged from the Solaris driver.
     42  */
     43 
     44 /*
     45  * Include header file appropriate for platform we're building on.
     46  */
     47 
     48 #ifdef	__NetBSD__
     49 #include <dev/ic/isp_netbsd.h>
     50 #endif
     51 #ifdef	__FreeBSD__
     52 #include <dev/isp/isp_freebsd.h>
     53 #endif
     54 #ifdef	__linux__
     55 #include <isp_linux.h>
     56 #endif
     57 
     58 /*
     59  * General defines
     60  */
     61 
     62 #define	MBOX_DELAY_COUNT	1000000 / 100
     63 
     64 /*
     65  * Local static data
     66  */
     67 
     68 
     69 /*
     70  * Local function prototypes.
     71  */
     72 static int isp_parse_async __P((struct ispsoftc *, int));
     73 static int isp_handle_other_response
     74 __P((struct ispsoftc *, ispstatusreq_t *, u_int8_t *));
     75 static void isp_parse_status
     76 __P((struct ispsoftc *, ispstatusreq_t *, ISP_SCSI_XFER_T *));
     77 static void isp_fibre_init __P((struct ispsoftc *));
     78 static void isp_fw_state __P((struct ispsoftc *));
     79 static void isp_dumpregs __P((struct ispsoftc *, const char *));
     80 static void isp_dumpxflist __P((struct ispsoftc *));
     81 static void isp_prtstst __P((ispstatusreq_t *));
     82 static char *isp2100_fw_statename __P((int));
     83 static void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *));
     84 
     85 static void isp_update  __P((struct ispsoftc *));
     86 static void isp_setdfltparm __P((struct ispsoftc *));
     87 static int isp_read_nvram __P((struct ispsoftc *));
     88 static void isp_rdnvram_word __P((struct ispsoftc *, int, u_int16_t *));
     89 
     90 /*
     91  * Reset Hardware.
     92  *
     93  * Hit the chip over the head, download new f/w.
     94  *
     95  * Locking done elsewhere.
     96  */
     97 void
     98 isp_reset(isp)
     99 	struct ispsoftc *isp;
    100 {
    101 	static char once = 1;
    102 	mbreg_t mbs;
    103 	int loops, i, dodnld = 1;
    104 	char *revname;
    105 
    106 	isp->isp_state = ISP_NILSTATE;
    107 
    108 	/*
    109 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
    110 	 * have been set in the MD code. We figure out more
    111 	 * here.
    112 	 */
    113 	isp->isp_dblev = DFLT_DBLEVEL;
    114 	if (isp->isp_type & ISP_HA_FC) {
    115 		revname = "2100";
    116 	} else {
    117 		sdparam *sdp = isp->isp_param;
    118 
    119 		int rev = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
    120 		switch (rev) {
    121 		default:
    122 			PRINTF("%s: unknown chip rev. 0x%x- assuming a 1020\n",
    123 			    isp->isp_name, rev);
    124 			/* FALLTHROUGH */
    125 		case 1:
    126 			revname = "1020";
    127 			isp->isp_type = ISP_HA_SCSI_1020;
    128 			sdp->isp_clock = 40;
    129 			break;
    130 		case 2:
    131 			/*
    132 			 * Some 1020A chips are Ultra Capable, but don't
    133 			 * run the clock rate up for that unless told to
    134 			 * do so by the Ultra Capable bits being set.
    135 			 */
    136 			revname = "1020A";
    137 			isp->isp_type = ISP_HA_SCSI_1020A;
    138 			sdp->isp_clock = 40;
    139 			break;
    140 		case 3:
    141 			revname = "1040";
    142 			isp->isp_type = ISP_HA_SCSI_1040;
    143 			sdp->isp_clock = 60;
    144 			break;
    145 		case 4:
    146 			revname = "1040A";
    147 			isp->isp_type = ISP_HA_SCSI_1040A;
    148 			sdp->isp_clock = 60;
    149 			break;
    150 		case 5:
    151 			revname = "1040B";
    152 			isp->isp_type = ISP_HA_SCSI_1040B;
    153 			sdp->isp_clock = 60;
    154 			break;
    155 		}
    156 		/*
    157 		 * Try and figure out if we're connected to a differential bus.
    158 		 * You have to pause the RISC processor to read SXP registers.
    159 		 */
    160 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
    161 		i = 100;
    162 		while ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
    163 			SYS_DELAY(20);
    164 			if (--i == 0) {
    165 				PRINTF("%s: unable to pause RISC processor\n",
    166 				    isp->isp_name);
    167 				i = -1;
    168 				break;
    169 			}
    170 		}
    171 		if (i > 0) {
    172 			if (isp->isp_bustype != ISP_BT_SBUS) {
    173 				ISP_SETBITS(isp, BIU_CONF1, BIU_PCI_CONF1_SXP);
    174 			}
    175 			if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
    176 				IDPRINTF(2, ("%s: Differential Mode Set\n",
    177 				    isp->isp_name));
    178 				sdp->isp_diffmode = 1;
    179 			} else {
    180 				sdp->isp_diffmode = 0;
    181 			}
    182 
    183 			if (isp->isp_bustype != ISP_BT_SBUS) {
    184 				ISP_CLRBITS(isp, BIU_CONF1, BIU_PCI_CONF1_SXP);
    185 			}
    186 
    187 			/*
    188 			 * Figure out whether we're ultra capable.
    189 			 */
    190 			i = ISP_READ(isp, RISC_PSR);
    191 			if (isp->isp_bustype != ISP_BT_SBUS) {
    192 				i &= RISC_PSR_PCI_ULTRA;
    193 			} else {
    194 				i &= RISC_PSR_SBUS_ULTRA;
    195 			}
    196 			if (i) {
    197 				IDPRINTF(2, ("%s: Ultra Mode Capable\n",
    198 				    isp->isp_name));
    199 				sdp->isp_clock = 60;
    200 			} else {
    201 				sdp->isp_clock = 40;
    202 			}
    203 			/*
    204 			 * Restart processor
    205 			 */
    206 			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
    207 		}
    208 		/*
    209 		 * Machine dependent clock (if set) overrides
    210 		 * our generic determinations.
    211 		 */
    212 		if (isp->isp_mdvec->dv_clock) {
    213 			if (isp->isp_mdvec->dv_clock < sdp->isp_clock) {
    214 				sdp->isp_clock = isp->isp_mdvec->dv_clock;
    215 			}
    216 		}
    217 	}
    218 
    219 	/*
    220 	 * Do MD specific pre initialization
    221 	 */
    222 	ISP_RESET0(isp);
    223 
    224 	if (once == 1) {
    225 		once = 0;
    226 		/*
    227 		 * Get the current running firmware revision out of the
    228 		 * chip before we hit it over the head (if this is our
    229 		 * first time through). Note that we store this as the
    230 		 * 'ROM' firmware revision- which it may not be. In any
    231 		 * case, we don't really use this yet, but we may in
    232 		 * the future.
    233 		 */
    234 		mbs.param[0] = MBOX_ABOUT_FIRMWARE;
    235 		isp_mboxcmd(isp, &mbs);
    236 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    237 			IDPRINTF(3, ("%s: initial ABOUT FIRMWARE command "
    238 			    "failed\n", isp->isp_name));
    239 		} else {
    240 			isp->isp_romfw_rev =
    241 			    (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
    242 		}
    243 	}
    244 
    245 	/*
    246 	 * Hit the chip over the head with hammer,
    247 	 * and give the ISP a chance to recover.
    248 	 */
    249 
    250 	if (isp->isp_type & ISP_HA_SCSI) {
    251 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
    252 		/*
    253 		 * A slight delay...
    254 		 */
    255 		SYS_DELAY(100);
    256 
    257 		/*
    258 		 * Clear data && control DMA engines.
    259 		 */
    260 		ISP_WRITE(isp, CDMA_CONTROL,
    261 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    262 		ISP_WRITE(isp, DDMA_CONTROL,
    263 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    264 	} else {
    265 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
    266 		/*
    267 		 * A slight delay...
    268 		 */
    269 		SYS_DELAY(100);
    270 		ISP_WRITE(isp, CDMA2100_CONTROL,
    271 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    272 		ISP_WRITE(isp, TDMA2100_CONTROL,
    273 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    274 		ISP_WRITE(isp, RDMA2100_CONTROL,
    275 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    276 	}
    277 
    278 	/*
    279 	 * Wait for ISP to be ready to go...
    280 	 */
    281 	loops = MBOX_DELAY_COUNT;
    282 	for (;;) {
    283 		if (isp->isp_type & ISP_HA_SCSI) {
    284 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
    285 				break;
    286 		} else {
    287 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
    288 				break;
    289 		}
    290 		SYS_DELAY(100);
    291 		if (--loops < 0) {
    292 			isp_dumpregs(isp, "chip reset timed out");
    293 			return;
    294 		}
    295 	}
    296 	/*
    297 	 * More initialization
    298 	 */
    299 	if (isp->isp_type & ISP_HA_SCSI) {
    300 		ISP_WRITE(isp, BIU_CONF1, 0);
    301 	} else {
    302 		ISP_WRITE(isp, BIU2100_CSR, 0);
    303 		/*
    304 		 * All 2100's are 60Mhz with fast rams onboard.
    305 		 */
    306 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
    307 	}
    308 
    309 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
    310 	SYS_DELAY(100);
    311 
    312 	if (isp->isp_type & ISP_HA_SCSI) {
    313 		ISP_SETBITS(isp, BIU_CONF1, isp->isp_mdvec->dv_conf1);
    314 		if (isp->isp_mdvec->dv_conf1 & BIU_BURST_ENABLE) {
    315 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
    316 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
    317 		}
    318 	}
    319 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
    320 
    321 	/*
    322 	 * Do MD specific post initialization
    323 	 */
    324 	ISP_RESET1(isp);
    325 
    326 	/*
    327 	 * Enable interrupts
    328 	 */
    329 	ENABLE_INTS(isp);
    330 
    331 	/*
    332 	 * Do some sanity checking.
    333 	 */
    334 	mbs.param[0] = MBOX_NO_OP;
    335 	isp_mboxcmd(isp, &mbs);
    336 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    337 		isp_dumpregs(isp, "NOP test failed");
    338 		return;
    339 	}
    340 
    341 	if (isp->isp_type & ISP_HA_SCSI) {
    342 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
    343 		mbs.param[1] = 0xdead;
    344 		mbs.param[2] = 0xbeef;
    345 		mbs.param[3] = 0xffff;
    346 		mbs.param[4] = 0x1111;
    347 		mbs.param[5] = 0xa5a5;
    348 		isp_mboxcmd(isp, &mbs);
    349 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    350 			isp_dumpregs(isp,
    351 				"Mailbox Register test didn't complete");
    352 			return;
    353 		}
    354 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
    355 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
    356 		    mbs.param[5] != 0xa5a5) {
    357 			isp_dumpregs(isp, "Register Test Failed");
    358 			return;
    359 		}
    360 
    361 	}
    362 
    363 	/*
    364 	 * Download new Firmware, unless requested not to do so.
    365 	 * This is made slightly trickier in some cases where the
    366 	 * firmware of the ROM revision is newer than the revision
    367 	 * compiled into the driver. So, where we used to compare
    368 	 * versions of our f/w and the ROM f/w, now we just see
    369 	 * whether we have f/w at all and whether a config flag
    370 	 * has disabled our download.
    371 	 */
    372 	if ((isp->isp_mdvec->dv_fwlen == 0) ||
    373 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
    374 		dodnld = 0;
    375 	}
    376 
    377 	if (dodnld) {
    378 		for (i = 0; i < isp->isp_mdvec->dv_fwlen; i++) {
    379 			mbs.param[0] = MBOX_WRITE_RAM_WORD;
    380 			mbs.param[1] = isp->isp_mdvec->dv_codeorg + i;
    381 			mbs.param[2] = isp->isp_mdvec->dv_ispfw[i];
    382 			isp_mboxcmd(isp, &mbs);
    383 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    384 				isp_dumpregs(isp, "f/w download failed");
    385 				return;
    386 			}
    387 		}
    388 
    389 		if (isp->isp_mdvec->dv_fwlen) {
    390 			/*
    391 			 * Verify that it downloaded correctly.
    392 			 */
    393 			mbs.param[0] = MBOX_VERIFY_CHECKSUM;
    394 			mbs.param[1] = isp->isp_mdvec->dv_codeorg;
    395 			isp_mboxcmd(isp, &mbs);
    396 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    397 				isp_dumpregs(isp, "ram checksum failure");
    398 				return;
    399 			}
    400 		}
    401 	} else {
    402 		IDPRINTF(3, ("%s: skipping f/w download\n", isp->isp_name));
    403 	}
    404 
    405 	/*
    406 	 * Now start it rolling.
    407 	 *
    408 	 * If we didn't actually download f/w,
    409 	 * we still need to (re)start it.
    410 	 */
    411 
    412 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
    413 	mbs.param[1] = isp->isp_mdvec->dv_codeorg;
    414 	isp_mboxcmd(isp, &mbs);
    415 
    416 	if (isp->isp_type & ISP_HA_SCSI) {
    417 		sdparam *sdp = isp->isp_param;
    418 		/*
    419 		 * Set CLOCK RATE, but only if asked to.
    420 		 */
    421 		if (sdp->isp_clock) {
    422 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
    423 			mbs.param[1] = sdp->isp_clock;
    424 			isp_mboxcmd(isp, &mbs);
    425 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    426 				isp_dumpregs(isp, "failed to set CLOCKRATE");
    427 				/* but continue */
    428 			} else {
    429 				IDPRINTF(3, ("%s: setting input clock to %d\n",
    430 				    isp->isp_name, sdp->isp_clock));
    431 			}
    432 		}
    433 	}
    434 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
    435 	isp_mboxcmd(isp, &mbs);
    436 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    437 		isp_dumpregs(isp, "ABOUT FIRMWARE command failed");
    438 		return;
    439 	}
    440 	PRINTF("%s: Board Revision %s, %s F/W Revision %d.%d\n",
    441 		isp->isp_name, revname, dodnld? "loaded" : "resident",
    442 		mbs.param[1], mbs.param[2]);
    443 	isp->isp_fwrev = (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
    444 	if (isp->isp_romfw_rev && dodnld) {
    445 		PRINTF("%s: Last F/W revision was %d.%d\n", isp->isp_name,
    446 		    isp->isp_romfw_rev >> 10, isp->isp_romfw_rev & 0x3ff);
    447 	}
    448 	isp_fw_state(isp);
    449 	isp->isp_state = ISP_RESETSTATE;
    450 }
    451 
    452 /*
    453  * Initialize Hardware to known state
    454  *
    455  * Locks are held before coming here.
    456  */
    457 
    458 void
    459 isp_init(isp)
    460 	struct ispsoftc *isp;
    461 {
    462 	sdparam *sdp;
    463 	mbreg_t mbs;
    464 	int tgt;
    465 
    466 	/*
    467 	 * Must do first.
    468 	 */
    469 	isp_setdfltparm(isp);
    470 
    471 	/*
    472 	 * If we're fibre, we have a completely different
    473 	 * initialization method.
    474 	 */
    475 
    476 	if (isp->isp_type & ISP_HA_FC) {
    477 		isp_fibre_init(isp);
    478 		return;
    479 	}
    480 	sdp = isp->isp_param;
    481 
    482 	/*
    483 	 * Set (possibly new) Initiator ID.
    484 	 */
    485 	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
    486 	mbs.param[1] = sdp->isp_initiator_id;
    487 	isp_mboxcmd(isp, &mbs);
    488 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    489 		isp_dumpregs(isp, "failed to set initiator id");
    490 		return;
    491 	}
    492 
    493 	/*
    494 	 * Set Retry Delay and Count
    495 	 */
    496 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
    497 	mbs.param[1] = sdp->isp_retry_count;
    498 	mbs.param[2] = sdp->isp_retry_delay;
    499 	isp_mboxcmd(isp, &mbs);
    500 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    501 		isp_dumpregs(isp, "failed to set retry count and delay");
    502 		return;
    503 	}
    504 
    505 	/*
    506 	 * Set ASYNC DATA SETUP time. This is very important.
    507 	 */
    508 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
    509 	mbs.param[1] = sdp->isp_async_data_setup;
    510 	isp_mboxcmd(isp, &mbs);
    511 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    512 		isp_dumpregs(isp, "failed to set async data setup time");
    513 		return;
    514 	}
    515 
    516 	/*
    517 	 * Set ACTIVE Negation State.
    518 	 */
    519 	mbs.param[0] = MBOX_SET_ACTIVE_NEG_STATE;
    520 	mbs.param[1] =
    521 	    (sdp->isp_req_ack_active_neg << 4) |
    522 	    (sdp->isp_data_line_active_neg << 5);
    523 	isp_mboxcmd(isp, &mbs);
    524 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    525 		isp_dumpregs(isp, "failed to set active neg state");
    526 		return;
    527 	}
    528 
    529 	/*
    530 	 * Set the Tag Aging limit
    531 	 */
    532 
    533 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
    534 	mbs.param[1] = sdp->isp_tag_aging;
    535 	isp_mboxcmd(isp, &mbs);
    536 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    537 		isp_dumpregs(isp, "failed to set tag age limit");
    538 		return;
    539 	}
    540 
    541 	/*
    542 	 * Set selection timeout.
    543 	 */
    544 
    545 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
    546 	mbs.param[1] = sdp->isp_selection_timeout;
    547 	isp_mboxcmd(isp, &mbs);
    548 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    549 		isp_dumpregs(isp, "failed to set selection timeout");
    550 		return;
    551 	}
    552 
    553 	/*
    554 	 * Set per-target parameters to a safe minimum.
    555 	 */
    556 
    557 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
    558 		int maxlun, lun;
    559 
    560 		if (sdp->isp_devparam[tgt].dev_enable == 0)
    561 			continue;
    562 
    563 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
    564 		mbs.param[1] = tgt << 8;
    565 		mbs.param[2] = DPARM_SAFE_DFLT;
    566 		mbs.param[3] = 0;
    567 		/*
    568 		 * It is not quite clear when this changed over so that
    569 		 * we could force narrow and async, so assume >= 7.55.
    570 		 *
    571 		 * Otherwise, a SCSI bus reset issued below will force
    572 		 * the back to the narrow, async state (but see note
    573 		 * below also). Technically we should also do without
    574 		 * Parity.
    575 		 */
    576 		if (isp->isp_fwrev >= ISP_FW_REV(7, 55)) {
    577 			mbs.param[2] |= DPARM_NARROW | DPARM_ASYNC;
    578 		}
    579 		sdp->isp_devparam[tgt].cur_dflags = mbs.param[2] >> 8;
    580 
    581 		IDPRINTF(3, ("\n%s: tgt %d cflags %x offset %x period %x\n",
    582 		    isp->isp_name, tgt, mbs.param[2], mbs.param[3] >> 8,
    583 		    mbs.param[3] & 0xff));
    584 		isp_mboxcmd(isp, &mbs);
    585 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    586 
    587 			PRINTF("%s: failed to set parameters for tgt %d\n",
    588 				isp->isp_name, tgt);
    589 
    590 			PRINTF("%s: flags %x offset %x period %x\n",
    591 				isp->isp_name, sdp->isp_devparam[tgt].dev_flags,
    592 				sdp->isp_devparam[tgt].sync_offset,
    593 				sdp->isp_devparam[tgt].sync_period);
    594 
    595 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
    596 			mbs.param[1] = tgt << 8;
    597 			mbs.param[2] = DPARM_SAFE_DFLT;
    598 			mbs.param[3] = 0;
    599 			isp_mboxcmd(isp, &mbs);
    600 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    601 				PRINTF("%s: failed even to set defaults for "
    602 				    "target %d\n", isp->isp_name, tgt);
    603 				continue;
    604 			}
    605 		}
    606 
    607 		maxlun = (isp->isp_fwrev >= ISP_FW_REV(7, 55))? 32 : 8;
    608 		for (lun = 0; lun < maxlun; lun++) {
    609 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
    610 			mbs.param[1] = (tgt << 8) | lun;
    611 			mbs.param[2] = sdp->isp_max_queue_depth;
    612 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
    613 			isp_mboxcmd(isp, &mbs);
    614 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    615 				PRINTF("%s: failed to set device queue "
    616 				    "parameters for target %d, lun %d\n",
    617 				    isp->isp_name, tgt, lun);
    618 				break;
    619 			}
    620 		}
    621 	}
    622 
    623 	/*
    624 	 * Set up DMA for the request and result mailboxes.
    625 	 */
    626 	if (ISP_MBOXDMASETUP(isp) != 0) {
    627 		PRINTF("%s: can't setup dma mailboxes\n", isp->isp_name);
    628 		return;
    629 	}
    630 
    631 	mbs.param[0] = MBOX_INIT_RES_QUEUE;
    632 	mbs.param[1] = RESULT_QUEUE_LEN;
    633 	mbs.param[2] = (u_int16_t) (isp->isp_result_dma >> 16);
    634 	mbs.param[3] = (u_int16_t) (isp->isp_result_dma & 0xffff);
    635 	mbs.param[4] = 0;
    636 	mbs.param[5] = 0;
    637 	isp_mboxcmd(isp, &mbs);
    638 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    639 		isp_dumpregs(isp, "set of response queue failed");
    640 		return;
    641 	}
    642 	isp->isp_residx = 0;
    643 
    644 	mbs.param[0] = MBOX_INIT_REQ_QUEUE;
    645 	mbs.param[1] = RQUEST_QUEUE_LEN;
    646 	mbs.param[2] = (u_int16_t) (isp->isp_rquest_dma >> 16);
    647 	mbs.param[3] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
    648 	mbs.param[4] = 0;
    649 	mbs.param[5] = 0;
    650 	isp_mboxcmd(isp, &mbs);
    651 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    652 		isp_dumpregs(isp, "set of request queue failed");
    653 		return;
    654 	}
    655 	isp->isp_reqidx = isp->isp_reqodx = 0;
    656 
    657 	/*
    658 	 * XXX: See whether or not for 7.55 F/W or later we
    659 	 * XXX: can do without this, and see whether we should
    660 	 * XXX: honor the NVRAM SCSI_RESET_DISABLE token.
    661 	 */
    662 	mbs.param[0] = MBOX_BUS_RESET;
    663 	mbs.param[1] = 3;
    664 	isp_mboxcmd(isp, &mbs);
    665 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    666 		isp_dumpregs(isp, "SCSI bus reset failed");
    667 	}
    668 	/*
    669 	 * This is really important to have set after a bus reset.
    670 	 */
    671 	isp->isp_sendmarker = 1;
    672 	isp->isp_state = ISP_INITSTATE;
    673 }
    674 
    675 /*
    676  * Fibre Channel specific initialization.
    677  *
    678  * Locks are held before coming here.
    679  */
    680 static void
    681 isp_fibre_init(isp)
    682 	struct ispsoftc *isp;
    683 {
    684 	fcparam *fcp;
    685 	isp_icb_t *icbp;
    686 	mbreg_t mbs;
    687 	int count, loopid;
    688 	u_int8_t lwfs;
    689 
    690 	fcp = isp->isp_param;
    691 
    692 	if (ISP_MBOXDMASETUP(isp) != 0) {
    693 		PRINTF("%s: can't setup DMA for mailboxes\n", isp->isp_name);
    694 		return;
    695 	}
    696 	/*
    697 	 * For systems that don't have BIOS methods for which
    698 	 * we can easily change the NVRAM based loopid, we'll
    699 	 * override that here. Note that when we initialize
    700 	 * the firmware we may get back a different loopid than
    701 	 * we asked for anyway. XXX This is probably not the
    702 	 * best way to figure this out XXX
    703 	 */
    704 #ifndef	__i386__
    705 	loopid = DEFAULT_LOOPID;
    706 #else
    707 	loopid = fcp->isp_loopid;
    708 #endif
    709 
    710 
    711 	icbp = (isp_icb_t *) fcp->isp_scratch;
    712 	bzero(icbp, sizeof (*icbp));
    713 
    714 	icbp->icb_version = ICB_VERSION1;
    715 	fcp->isp_fwoptions = 0;
    716 	fcp->isp_fwoptions |= ICBOPT_INI_ADISC|ICBOPT_FAIRNESS;
    717 	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
    718 	fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
    719 #ifdef	CHECKME
    720 	fcp->isp_fwoptions |= ICBOPT_USE_PORTNAME;
    721 #endif
    722 #ifdef	THIS_WORKED
    723 	/*
    724 	 * This has unhappiness in target mode
    725 	 */
    726 	fcp->isp_fwoptions |= ICBOPT_FULL_LOGIN;
    727 #endif
    728 	icbp->icb_fwoptions = fcp->isp_fwoptions;
    729 	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
    730 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
    731 	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
    732 		PRINTF("%s: bad frame length (%d) from NVRAM- using %d\n",
    733 		    isp->isp_name, fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
    734 	}
    735 	icbp->icb_maxalloc = fcp->isp_maxalloc;
    736 	icbp->icb_execthrottle = fcp->isp_execthrottle;
    737 	icbp->icb_retry_delay = fcp->isp_retry_delay;
    738 	icbp->icb_retry_count = fcp->isp_retry_count;
    739 	icbp->icb_hardaddr = loopid;
    740 
    741 	MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwn);
    742 	if (icbp->icb_fwoptions & ICBOPT_USE_PORTNAME) {
    743 		u_int64_t portname = fcp->isp_wwn | (2LL << 56);
    744 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, portname);
    745 	}
    746 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN;
    747 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN;
    748 	icbp->icb_rqstaddr[RQRSP_ADDR0015] =
    749 	    (u_int16_t) (isp->isp_rquest_dma & 0xffff);
    750 	icbp->icb_rqstaddr[RQRSP_ADDR1631] =
    751 	    (u_int16_t) (isp->isp_rquest_dma >> 16);
    752 	icbp->icb_respaddr[RQRSP_ADDR0015] =
    753 	    (u_int16_t) (isp->isp_result_dma & 0xffff);
    754 	icbp->icb_respaddr[RQRSP_ADDR1631] =
    755 	    (u_int16_t) (isp->isp_result_dma >> 16);
    756 
    757 	for (count = 0; count < 10; count++) {
    758 		mbs.param[0] = MBOX_INIT_FIRMWARE;
    759 		mbs.param[1] = 0;
    760 		mbs.param[2] = (u_int16_t) (fcp->isp_scdma >> 16);
    761 		mbs.param[3] = (u_int16_t) (fcp->isp_scdma & 0xffff);
    762 		mbs.param[4] = 0;
    763 		mbs.param[5] = 0;
    764 		mbs.param[6] = 0;
    765 		mbs.param[7] = 0;
    766 
    767 		isp_mboxcmd(isp, &mbs);
    768 
    769 		switch (mbs.param[0]) {
    770 		case MBOX_COMMAND_COMPLETE:
    771 			count = 10;
    772 			break;
    773 		case ASYNC_LIP_OCCURRED:
    774 		case ASYNC_LOOP_UP:
    775 		case ASYNC_LOOP_DOWN:
    776 		case ASYNC_LOOP_RESET:
    777 		case ASYNC_PDB_CHANGED:
    778 		case ASYNC_CHANGE_NOTIFY:
    779 			if (count > 9) {
    780 				PRINTF("%s: too many retries to get going- "
    781 				    "giving up\n", isp->isp_name);
    782 				return;
    783 			}
    784 			break;
    785 		default:
    786 			isp_dumpregs(isp, "INIT FIRMWARE failed");
    787 			return;
    788 		}
    789 	}
    790 	isp->isp_reqidx = isp->isp_reqodx = 0;
    791 	isp->isp_residx = 0;
    792 
    793 	/*
    794 	 * Wait up to 5 seconds for FW to go to READY state.
    795 	 */
    796 	lwfs = FW_CONFIG_WAIT;
    797 	for (count = 0; count < 12000; count++) {
    798 		isp_fw_state(isp);
    799 		if (lwfs != fcp->isp_fwstate) {
    800 			PRINTF("%s: Firmware State %s -> %s\n",
    801 			    isp->isp_name, isp2100_fw_statename((int)lwfs),
    802 			    isp2100_fw_statename((int)fcp->isp_fwstate));
    803 			lwfs = fcp->isp_fwstate;
    804 		}
    805 		if (fcp->isp_fwstate == FW_READY) {
    806 			break;
    807 		}
    808 		SYS_DELAY(1000);	/* wait one millisecond */
    809 	}
    810 	isp->isp_sendmarker = 1;
    811 
    812 	/*
    813 	 * Get our Loop ID
    814 	 * (if possible)
    815 	 */
    816 	if (fcp->isp_fwstate == FW_READY) {
    817 		mbs.param[0] = MBOX_GET_LOOP_ID;
    818 		isp_mboxcmd(isp, &mbs);
    819 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    820 			isp_dumpregs(isp, "GET LOOP ID failed");
    821 			return;
    822 		}
    823 		fcp->isp_loopid = mbs.param[1];
    824 		fcp->isp_alpa = mbs.param[2];
    825 		PRINTF("%s: Loop ID %d, ALPA 0x%x\n", isp->isp_name,
    826 		    fcp->isp_loopid, fcp->isp_alpa);
    827 		isp->isp_state = ISP_INITSTATE;
    828 	} else {
    829 		PRINTF("%s: failed to go to FW READY state- will not attach\n",
    830 		    isp->isp_name);
    831 	}
    832 }
    833 
    834 /*
    835  * Free any associated resources prior to decommissioning and
    836  * set the card to a known state (so it doesn't wake up and kick
    837  * us when we aren't expecting it to).
    838  *
    839  * Locks are held before coming here.
    840  */
    841 void
    842 isp_uninit(isp)
    843 	struct ispsoftc *isp;
    844 {
    845 	/*
    846 	 * Leave with interrupts disabled.
    847 	 */
    848 	DISABLE_INTS(isp);
    849 
    850 	/*
    851 	 * Stop the watchdog timer (if started).
    852 	 */
    853 	STOP_WATCHDOG(isp_watch, isp);
    854 }
    855 
    856 
    857 /*
    858  * Start a command. Locking is assumed done in the caller.
    859  */
    860 
    861 int32_t
    862 ispscsicmd(xs)
    863 	ISP_SCSI_XFER_T *xs;
    864 {
    865 	struct ispsoftc *isp;
    866 	u_int8_t iptr, optr;
    867 	union {
    868 		ispreq_t *_reqp;
    869 		ispreqt2_t *_t2reqp;
    870 	} _u;
    871 #define	reqp	_u._reqp
    872 #define	t2reqp	_u._t2reqp
    873 #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
    874 	int i;
    875 
    876 	XS_INITERR(xs);
    877 	isp = XS_ISP(xs);
    878 
    879 	if (isp->isp_state != ISP_RUNSTATE) {
    880 		PRINTF("%s: adapter not ready\n", isp->isp_name);
    881 		XS_SETERR(xs, HBA_BOTCH);
    882 		return (CMD_COMPLETE);
    883 	}
    884 
    885 	/*
    886 	 * We *could* do the different sequence type that has clos
    887 	 * to the whole Queue Entry for the command,.
    888 	 */
    889 	if (XS_CDBLEN(xs) > ((isp->isp_type & ISP_HA_FC)? 16 : 12)) {
    890 		PRINTF("%s: unsupported cdb length (%d)\n",
    891 		    isp->isp_name, XS_CDBLEN(xs));
    892 		XS_SETERR(xs, HBA_BOTCH);
    893 		return (CMD_COMPLETE);
    894 	}
    895 
    896 	/*
    897 	 * First check to see if any HBA or Device
    898 	 * parameters need to be updated.
    899 	 */
    900 	if (isp->isp_update) {
    901 		isp_update(isp);
    902 	}
    903 
    904 	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
    905 	iptr = isp->isp_reqidx;
    906 
    907 	reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
    908 	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
    909 	if (iptr == optr) {
    910 		IDPRINTF(2, ("%s: Request Queue Overflow\n", isp->isp_name));
    911 		XS_SETERR(xs, HBA_BOTCH);
    912 		return (CMD_EAGAIN);
    913 	}
    914 	if (isp->isp_type & ISP_HA_FC) {
    915 		DISABLE_INTS(isp);
    916 	}
    917 
    918 	if (isp->isp_sendmarker) {
    919 		u_int8_t niptr;
    920 		ispmarkreq_t *marker = (ispmarkreq_t *) reqp;
    921 
    922 		bzero((void *) marker, sizeof (*marker));
    923 		marker->req_header.rqs_entry_count = 1;
    924 		marker->req_header.rqs_entry_type = RQSTYPE_MARKER;
    925 		marker->req_modifier = SYNC_ALL;
    926 
    927 		isp->isp_sendmarker = 0;
    928 
    929 		/*
    930 		 * Unconditionally update the input pointer anyway.
    931 		 */
    932 		ISP_WRITE(isp, INMAILBOX4, iptr);
    933 		isp->isp_reqidx = iptr;
    934 
    935 		niptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
    936 		if (niptr == optr) {
    937 			if (isp->isp_type & ISP_HA_FC) {
    938 				ENABLE_INTS(isp);
    939 			}
    940 			IDPRINTF(2, ("%s: Request Queue Overflow+\n",
    941 			    isp->isp_name));
    942 			XS_SETERR(xs, HBA_BOTCH);
    943 			return (CMD_EAGAIN);
    944 		}
    945 		reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
    946 		iptr = niptr;
    947 	}
    948 
    949 	bzero((void *) reqp, UZSIZE);
    950 	reqp->req_header.rqs_entry_count = 1;
    951 	if (isp->isp_type & ISP_HA_FC) {
    952 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
    953 	} else {
    954 		reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
    955 	}
    956 	reqp->req_header.rqs_flags = 0;
    957 	reqp->req_header.rqs_seqno = isp->isp_seqno++;
    958 
    959 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
    960 		if (isp->isp_xflist[i] == NULL)
    961 			break;
    962 	}
    963 	if (i == RQUEST_QUEUE_LEN) {
    964 		if (isp->isp_type & ISP_HA_FC)
    965 			ENABLE_INTS(isp);
    966 		IDPRINTF(2, ("%s: out of xflist pointers\n", isp->isp_name));
    967 		XS_SETERR(xs, HBA_BOTCH);
    968 		return (CMD_EAGAIN);
    969 	} else {
    970 		/*
    971 		 * Never have a handle that is zero, so
    972 		 * set req_handle off by one.
    973 		 */
    974 		isp->isp_xflist[i] = xs;
    975 		reqp->req_handle = i+1;
    976 	}
    977 
    978 	if (isp->isp_type & ISP_HA_FC) {
    979 		/*
    980 		 * See comment in isp_intr
    981 		 */
    982 		XS_RESID(xs) = 0;
    983 		/*
    984 		 * Fibre Channel always requires some kind of tag.
    985 		 * If we're marked as "Can't Tag", just do simple
    986 		 * instead of ordered tags. It's pretty clear to me
    987 		 * that we shouldn't do head of queue tagging in
    988 		 * this case.
    989 		 */
    990 		if (XS_CANTAG(xs)) {
    991 			t2reqp->req_flags = XS_KINDOF_TAG(xs);
    992 		} else {
    993  			t2reqp->req_flags = REQFLAG_STAG;
    994 		}
    995 	} else {
    996 		sdparam *sdp = (sdparam *)isp->isp_param;
    997 		if ((sdp->isp_devparam[XS_TGT(xs)].cur_dflags & DPARM_TQING) &&
    998 		    XS_CANTAG(xs)) {
    999 			reqp->req_flags = XS_KINDOF_TAG(xs);
   1000 		} else {
   1001 			reqp->req_flags = 0;
   1002 		}
   1003 	}
   1004 	reqp->req_target = XS_TGT(xs);
   1005 	if (isp->isp_type & ISP_HA_SCSI) {
   1006 		reqp->req_lun_trn = XS_LUN(xs);
   1007 		reqp->req_cdblen = XS_CDBLEN(xs);
   1008 	} else {
   1009 #ifdef	SCCLUN
   1010 		reqp->req_scclun = XS_LUN(xs);
   1011 #else
   1012 		reqp->req_lun_trn = XS_LUN(xs);
   1013 #endif
   1014 
   1015 	}
   1016 	bcopy((void *)XS_CDBP(xs), reqp->req_cdb, XS_CDBLEN(xs));
   1017 
   1018 	IDPRINTF(5, ("%s(%d.%d): START%d cmd 0x%x datalen %d\n", isp->isp_name,
   1019 	    XS_TGT(xs), XS_LUN(xs), reqp->req_header.rqs_seqno,
   1020 	    reqp->req_cdb[0], XS_XFRLEN(xs)));
   1021 
   1022 	reqp->req_time = XS_TIME(xs) / 1000;
   1023 	if (reqp->req_time == 0 && XS_TIME(xs))
   1024 		reqp->req_time = 1;
   1025 	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
   1026 	if (i != CMD_QUEUED) {
   1027 		if (isp->isp_type & ISP_HA_FC)
   1028 			ENABLE_INTS(isp);
   1029 		/*
   1030 		 * dmasetup sets actual error in packet, and
   1031 		 * return what we were given to return.
   1032 		 */
   1033 		return (i);
   1034 	}
   1035 	XS_SETERR(xs, HBA_NOERROR);
   1036 	ISP_WRITE(isp, INMAILBOX4, iptr);
   1037 	isp->isp_reqidx = iptr;
   1038 	if (isp->isp_type & ISP_HA_FC) {
   1039 		ENABLE_INTS(isp);
   1040 	}
   1041 	isp->isp_nactive++;
   1042 	return (CMD_QUEUED);
   1043 #undef	reqp
   1044 #undef	t2reqp
   1045 }
   1046 
   1047 /*
   1048  * isp control
   1049  * Locks (ints blocked) assumed held.
   1050  */
   1051 
   1052 int
   1053 isp_control(isp, ctl, arg)
   1054 	struct ispsoftc *isp;
   1055 	ispctl_t ctl;
   1056 	void *arg;
   1057 {
   1058 	ISP_SCSI_XFER_T *xs;
   1059 	mbreg_t mbs;
   1060 	int i;
   1061 
   1062 	switch (ctl) {
   1063 	default:
   1064 		PRINTF("%s: isp_control unknown control op %x\n",
   1065 		    isp->isp_name, ctl);
   1066 		break;
   1067 
   1068 	case ISPCTL_RESET_BUS:
   1069 		mbs.param[0] = MBOX_BUS_RESET;
   1070 		mbs.param[1] = (isp->isp_type & ISP_HA_FC)? 5: 2;
   1071 		isp_mboxcmd(isp, &mbs);
   1072 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1073 			isp_dumpregs(isp, "isp_control SCSI bus reset failed");
   1074 			break;
   1075 		}
   1076 		/*
   1077 		 * This is really important to have set after a bus reset.
   1078 		 */
   1079 		isp->isp_sendmarker = 1;
   1080 		PRINTF("%s: driver initiated bus reset\n", isp->isp_name);
   1081 		return (0);
   1082 
   1083         case ISPCTL_RESET_DEV:
   1084 		/*
   1085 		 * Note that under parallel SCSI, this issues a BDR message.
   1086 		 * Under FC, we could probably be using ABORT TASK SET
   1087 		 * command.
   1088 		 */
   1089 
   1090 		mbs.param[0] = MBOX_ABORT_TARGET;
   1091 		mbs.param[1] = ((long)arg) << 8;
   1092 		mbs.param[2] = 2;	/* 'delay', in seconds */
   1093 		isp_mboxcmd(isp, &mbs);
   1094 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1095 			isp_dumpregs(isp, "SCSI Target  reset failed");
   1096 			break;
   1097 		}
   1098 		PRINTF("%s: Target %d Reset Succeeded\n", isp->isp_name,
   1099 		    (int) ((long) arg));
   1100 		isp->isp_sendmarker = 1;
   1101 		return (0);
   1102 
   1103         case ISPCTL_ABORT_CMD:
   1104 		xs = (ISP_SCSI_XFER_T *) arg;
   1105 		for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   1106 			if (xs == isp->isp_xflist[i]) {
   1107 				break;
   1108 			}
   1109 		}
   1110 		if (i == RQUEST_QUEUE_LEN) {
   1111 			PRINTF("%s: isp_control- cannot find command to abort "
   1112 			    "in active list\n", isp->isp_name);
   1113 			break;
   1114 		}
   1115 		mbs.param[0] = MBOX_ABORT;
   1116 #ifdef	SCCLUN
   1117 		if (isp->isp_type & ISP_HA_FC) {
   1118 			mbs.param[1] = XS_TGT(xs) << 8;
   1119 			mbs.param[4] = 0;
   1120 			mbs.param[5] = 0;
   1121 			mbs.param[6] = XS_LUN(xs);
   1122 		} else {
   1123 			mbs.param[1] = XS_TGT(xs) << 8 | XS_LUN(xs);
   1124 		}
   1125 #else
   1126 		mbs.param[1] = XS_TGT(xs) << 8 | XS_LUN(xs);
   1127 #endif
   1128 		mbs.param[2] = (i+1) >> 16;
   1129 		mbs.param[3] = (i+1) & 0xffff;
   1130 		isp_mboxcmd(isp, &mbs);
   1131 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1132 			PRINTF("%s: isp_control MBOX_ABORT failure (code %x)\n",
   1133 			    isp->isp_name, mbs.param[0]);
   1134 			break;
   1135 		}
   1136 		PRINTF("%s: command for target %d lun %d was aborted\n",
   1137 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1138 		return (0);
   1139 
   1140 	case ISPCTL_UPDATE_PARAMS:
   1141 		isp_update(isp);
   1142 		return(0);
   1143 	}
   1144 	return (-1);
   1145 }
   1146 
   1147 /*
   1148  * Interrupt Service Routine(s).
   1149  *
   1150  * External (OS) framework has done the appropriate locking,
   1151  * and the locking will be held throughout this function.
   1152  */
   1153 
   1154 int
   1155 isp_intr(arg)
   1156 	void *arg;
   1157 {
   1158 	ISP_SCSI_XFER_T *complist[RESULT_QUEUE_LEN], *xs;
   1159 	struct ispsoftc *isp = arg;
   1160 	u_int8_t iptr, optr;
   1161 	u_int16_t isr;
   1162 	int i, ndone = 0;
   1163 
   1164 	isr = ISP_READ(isp, BIU_ISR);
   1165 	if (isp->isp_type & ISP_HA_FC) {
   1166 		if (isr == 0 || (isr & BIU2100_ISR_RISC_INT) == 0) {
   1167 			if (isr) {
   1168 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
   1169 					     isp->isp_name, isr));
   1170 			}
   1171 			return (0);
   1172 		}
   1173 	} else {
   1174 		if (isr == 0 || (isr & BIU_ISR_RISC_INT) == 0) {
   1175 			if (isr) {
   1176 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
   1177 					     isp->isp_name, isr));
   1178 			}
   1179 			return (0);
   1180 		}
   1181 	}
   1182 
   1183 	if (ISP_READ(isp, BIU_SEMA) & 1) {
   1184 		u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
   1185 		if (isp_parse_async(isp, (int) mbox))
   1186 			return (1);
   1187 		ISP_WRITE(isp, BIU_SEMA, 0);
   1188 	}
   1189 
   1190 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   1191 
   1192 	optr = isp->isp_residx;
   1193 	iptr = ISP_READ(isp, OUTMAILBOX5);
   1194 
   1195 	if (optr == iptr) {
   1196 		IDPRINTF(4, ("why intr? isr %x iptr %x optr %x\n",
   1197 		    isr, optr, iptr));
   1198 	}
   1199 	ENABLE_INTS(isp);
   1200 
   1201 	while (optr != iptr) {
   1202 		ispstatusreq_t *sp;
   1203 		u_int8_t oop;
   1204 		int buddaboom = 0;
   1205 
   1206 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   1207 		oop = optr;
   1208 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN);
   1209 
   1210 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
   1211 			if (isp_handle_other_response(isp, sp, &optr) == 0) {
   1212 				ISP_WRITE(isp, INMAILBOX5, optr);
   1213 				continue;
   1214 			}
   1215 			/*
   1216 			 * It really has to be a bounced request just copied
   1217 			 * from the request queue to the response queue.
   1218 			 */
   1219 
   1220 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
   1221 				ISP_WRITE(isp, INMAILBOX5, optr);
   1222 				continue;
   1223 			}
   1224 			PRINTF("%s: not RESPONSE in RESPONSE Queue "
   1225 			    "(type 0x%x) @ idx %d (next %d)\n", isp->isp_name,
   1226 			    sp->req_header.rqs_entry_type, oop, optr);
   1227 			buddaboom = 1;
   1228 		}
   1229 
   1230 		if (sp->req_header.rqs_flags & 0xf) {
   1231 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   1232 				ISP_WRITE(isp, INMAILBOX5, optr);
   1233 				continue;
   1234 			}
   1235 			PRINTF("%s: rqs_flags=%x", isp->isp_name,
   1236 				sp->req_header.rqs_flags & 0xf);
   1237 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   1238 				PRINTF("%s: internal queues full\n",
   1239 				    isp->isp_name);
   1240 				/* XXXX: this command *could* get restarted */
   1241 				buddaboom++;
   1242 			}
   1243 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   1244 				PRINTF("%s: bad header\n", isp->isp_name);
   1245 				buddaboom++;
   1246 			}
   1247 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   1248 				PRINTF("%s: bad request packet\n",
   1249 				    isp->isp_name);
   1250 				buddaboom++;
   1251 			}
   1252 		}
   1253 		if (sp->req_handle > RQUEST_QUEUE_LEN || sp->req_handle < 1) {
   1254 			PRINTF("%s: bad request handle %d\n", isp->isp_name,
   1255 				sp->req_handle);
   1256 			ISP_WRITE(isp, INMAILBOX5, optr);
   1257 			continue;
   1258 		}
   1259 		xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[sp->req_handle - 1];
   1260 		if (xs == NULL) {
   1261 			PRINTF("%s: NULL xs in xflist (handle %x)\n",
   1262 			    isp->isp_name, sp->req_handle);
   1263 			isp_dumpxflist(isp);
   1264 			ISP_WRITE(isp, INMAILBOX5, optr);
   1265 			continue;
   1266 		}
   1267 		isp->isp_xflist[sp->req_handle - 1] = NULL;
   1268 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   1269 			isp->isp_sendmarker = 1;
   1270 		}
   1271 		if (buddaboom) {
   1272 			XS_SETERR(xs, HBA_BOTCH);
   1273 		}
   1274 		XS_STS(xs) = sp->req_scsi_status & 0xff;
   1275 		if (isp->isp_type & ISP_HA_SCSI) {
   1276 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   1277 				bcopy(sp->req_sense_data, XS_SNSP(xs),
   1278 					XS_SNSLEN(xs));
   1279 				XS_SNS_IS_VALID(xs);
   1280 			}
   1281 		} else {
   1282 			if (XS_STS(xs) == SCSI_CHECK) {
   1283 				XS_SNS_IS_VALID(xs);
   1284 				bcopy(sp->req_sense_data, XS_SNSP(xs),
   1285 					XS_SNSLEN(xs));
   1286 				sp->req_state_flags |= RQSF_GOT_SENSE;
   1287 			}
   1288 		}
   1289 		if (XS_NOERR(xs) && XS_STS(xs) == SCSI_BUSY) {
   1290 			XS_SETERR(xs, HBA_TGTBSY);
   1291 		}
   1292 
   1293 		if (sp->req_header.rqs_entry_type == RQSTYPE_RESPONSE) {
   1294 			if (XS_NOERR(xs)) {
   1295 			    if (sp->req_completion_status != RQCS_COMPLETE) {
   1296 				isp_parse_status(isp, sp, xs);
   1297 			    } else {
   1298 				XS_SETERR(xs, HBA_NOERROR);
   1299 			    }
   1300 			}
   1301 		} else {
   1302 			PRINTF("%s: unknown return %x\n", isp->isp_name,
   1303 				sp->req_header.rqs_entry_type);
   1304 			if (XS_NOERR(xs))
   1305 				XS_SETERR(xs, HBA_BOTCH);
   1306 		}
   1307 		if (isp->isp_type & ISP_HA_SCSI) {
   1308 			XS_RESID(xs) = sp->req_resid;
   1309 		} else if (sp->req_scsi_status & RQCS_RU) {
   1310 			XS_RESID(xs) = sp->req_resid;
   1311 			IDPRINTF(4, ("%s: cnt %d rsd %d\n", isp->isp_name,
   1312 				XS_XFRLEN(xs), sp->req_resid));
   1313 		}
   1314 		if (XS_XFRLEN(xs)) {
   1315 			ISP_DMAFREE(isp, xs, sp->req_handle - 1);
   1316 		}
   1317 		/*
   1318 		 * XXX: If we have a check condition, but no Sense Data,
   1319 		 * XXX: mark it as an error (ARQ failed). We need to
   1320 		 * XXX: to do a more distinct job because there may
   1321 		 * XXX: cases where ARQ is disabled.
   1322 		 */
   1323 		if (XS_STS(xs) == SCSI_CHECK && !(XS_IS_SNS_VALID(xs))) {
   1324 			if (XS_NOERR(xs)) {
   1325 				PRINTF("%s: ARQ failure for target %d lun %d\n",
   1326 				    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1327 				XS_SETERR(xs, HBA_ARQFAIL);
   1328 			}
   1329 		}
   1330 		if ((isp->isp_dblev >= 5) ||
   1331 		    (isp->isp_dblev > 2 && !XS_NOERR(xs))) {
   1332 			PRINTF("%s(%d.%d): FIN%d dl%d resid%d STS %x",
   1333 			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
   1334 			    sp->req_header.rqs_seqno, XS_XFRLEN(xs),
   1335 			    XS_RESID(xs), XS_STS(xs));
   1336 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   1337 				PRINTF(" Skey: %x", XS_SNSKEY(xs));
   1338 				if (!(XS_IS_SNS_VALID(xs))) {
   1339 					PRINTF(" BUT NOT SET");
   1340 				}
   1341 			}
   1342 			PRINTF(" XS_ERR=0x%x\n", (unsigned int) XS_ERR(xs));
   1343 		}
   1344 
   1345 		ISP_WRITE(isp, INMAILBOX5, optr);
   1346 		if (isp->isp_nactive > 0)
   1347 		    isp->isp_nactive--;
   1348 		complist[ndone++] = xs;	/* defer completion call until later */
   1349 	}
   1350 	/*
   1351 	 * If we completed any commands, then it's valid to find out
   1352 	 * what the outpointer is.
   1353 	 */
   1354 	if (ndone) {
   1355 	 	isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
   1356 	}
   1357 	isp->isp_residx = optr;
   1358 	for (i = 0; i < ndone; i++) {
   1359 		xs = complist[i];
   1360 		if (xs) {
   1361 			XS_CMD_DONE(xs);
   1362 		}
   1363 	}
   1364 	return (1);
   1365 }
   1366 
   1367 /*
   1368  * Support routines.
   1369  */
   1370 
   1371 static int
   1372 isp_parse_async(isp, mbox)
   1373 	struct ispsoftc *isp;
   1374 	int mbox;
   1375 {
   1376 	switch (mbox) {
   1377 	case ASYNC_BUS_RESET:
   1378 		PRINTF("%s: SCSI bus reset detected\n", isp->isp_name);
   1379 		isp->isp_sendmarker = 1;
   1380 		break;
   1381 
   1382 	case ASYNC_SYSTEM_ERROR:
   1383 		mbox = ISP_READ(isp, OUTMAILBOX1);
   1384 		PRINTF("%s: Internal FW Error @ RISC Addr 0x%x\n",
   1385 		    isp->isp_name, mbox);
   1386 		isp_restart(isp);
   1387 		/* no point continuing after this */
   1388 		return (1);
   1389 
   1390 	case ASYNC_RQS_XFER_ERR:
   1391 		PRINTF("%s: Request Queue Transfer Error\n", isp->isp_name);
   1392 		break;
   1393 
   1394 	case ASYNC_RSP_XFER_ERR:
   1395 		PRINTF("%s: Response Queue Transfer Error\n", isp->isp_name);
   1396 		break;
   1397 
   1398 	case ASYNC_QWAKEUP:
   1399 		/* don't need to be chatty */
   1400 		mbox = ISP_READ(isp, OUTMAILBOX4);
   1401 		break;
   1402 
   1403 	case ASYNC_TIMEOUT_RESET:
   1404 		PRINTF("%s: timeout initiated SCSI bus reset\n", isp->isp_name);
   1405 		isp->isp_sendmarker = 1;
   1406 		break;
   1407 
   1408 	case ASYNC_DEVICE_RESET:
   1409 		PRINTF("%s: device reset\n", isp->isp_name);
   1410 		break;
   1411 
   1412 	case ASYNC_EXTMSG_UNDERRUN:
   1413 		PRINTF("%s: extended message underrun\n", isp->isp_name);
   1414 		break;
   1415 
   1416 	case ASYNC_SCAM_INT:
   1417 		PRINTF("%s: SCAM interrupt\n", isp->isp_name);
   1418 		break;
   1419 
   1420 	case ASYNC_HUNG_SCSI:
   1421 		PRINTF("%s: stalled SCSI Bus after DATA Overrun\n",
   1422 		    isp->isp_name);
   1423 		/* XXX: Need to issue SCSI reset at this point */
   1424 		break;
   1425 
   1426 	case ASYNC_KILLED_BUS:
   1427 		PRINTF("%s: SCSI Bus reset after DATA Overrun\n",
   1428 		    isp->isp_name);
   1429 		break;
   1430 
   1431 	case ASYNC_BUS_TRANSIT:
   1432 		PRINTF("%s: LBD->HVD Transition 0x%x\n",
   1433 		    isp->isp_name, ISP_READ(isp, OUTMAILBOX1));
   1434 		break;
   1435 
   1436 	case ASYNC_CMD_CMPLT:
   1437 		PRINTF("%s: fast post completion\n", isp->isp_name);
   1438 #if	0
   1439 		fast_post_handle = (ISP_READ(isp, OUTMAILBOX1) << 16) |
   1440 		    ISP_READ(isp, OUTMAILBOX2);
   1441 #endif
   1442 		break;
   1443 
   1444 	case ASYNC_CTIO_DONE:
   1445 		/* Should only occur when Fast Posting Set for 2100s */
   1446 		PRINTF("%s: CTIO done\n", isp->isp_name);
   1447 		break;
   1448 
   1449 	case ASYNC_LIP_OCCURRED:
   1450 		PRINTF("%s: LIP occurred\n", isp->isp_name);
   1451 		break;
   1452 
   1453 	case ASYNC_LOOP_UP:
   1454 		PRINTF("%s: Loop UP\n", isp->isp_name);
   1455 		break;
   1456 
   1457 	case ASYNC_LOOP_DOWN:
   1458 		PRINTF("%s: Loop DOWN\n", isp->isp_name);
   1459 		break;
   1460 
   1461 	case ASYNC_LOOP_RESET:
   1462 		PRINTF("%s: Loop RESET\n", isp->isp_name);
   1463 		break;
   1464 
   1465 	case ASYNC_PDB_CHANGED:
   1466 		PRINTF("%s: Port Database Changed\n", isp->isp_name);
   1467 		break;
   1468 
   1469 	case ASYNC_CHANGE_NOTIFY:
   1470 		PRINTF("%s: Name Server Database Changed\n", isp->isp_name);
   1471 		break;
   1472 
   1473 	default:
   1474 		PRINTF("%s: async %x\n", isp->isp_name, mbox);
   1475 		break;
   1476 	}
   1477 	return (0);
   1478 }
   1479 
   1480 static int
   1481 isp_handle_other_response(isp, sp, optrp)
   1482 	struct ispsoftc *isp;
   1483 	ispstatusreq_t *sp;
   1484 	u_int8_t *optrp;
   1485 {
   1486 	u_int8_t iptr, optr;
   1487 	int reqsize = 0;
   1488 	void *ireqp = NULL;
   1489 
   1490 
   1491 	switch (sp->req_header.rqs_entry_type) {
   1492 	case RQSTYPE_REQUEST:
   1493 		return (-1);
   1494 	default:
   1495 		PRINTF("%s: other response type %x\n", isp->isp_name,
   1496 		    sp->req_header.rqs_entry_type);
   1497 		break;
   1498 	}
   1499 	if (reqsize) {
   1500 		void *reqp;
   1501 		optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
   1502 		iptr = isp->isp_reqidx;
   1503 		reqp = (void *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
   1504 		iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
   1505 		if (iptr == optr) {
   1506 			PRINTF("%s: Request Queue Overflow other response\n",
   1507 			    isp->isp_name);
   1508 		} else {
   1509 			bcopy(ireqp, reqp, reqsize);
   1510 			ISP_WRITE(isp, INMAILBOX4, iptr);
   1511 			isp->isp_reqidx = iptr;
   1512 		}
   1513 	}
   1514 	return (0);
   1515 }
   1516 
   1517 
   1518 static void
   1519 isp_parse_status(isp, sp, xs)
   1520 	struct ispsoftc *isp;
   1521 	ispstatusreq_t *sp;
   1522 	ISP_SCSI_XFER_T *xs;
   1523 {
   1524 	switch (sp->req_completion_status) {
   1525 	case RQCS_COMPLETE:
   1526 		XS_SETERR(xs, HBA_NOERROR);
   1527 		return;
   1528 
   1529 	case RQCS_INCOMPLETE:
   1530 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   1531 			IDPRINTF(3, ("%s: Selection Timeout for target %d\n",
   1532 			    isp->isp_name, XS_TGT(xs)));
   1533 			XS_SETERR(xs, HBA_SELTIMEOUT);
   1534 			return;
   1535 		}
   1536 		PRINTF("%s: command incomplete for target %d lun %d, state "
   1537 		    "0x%x\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs),
   1538 		    sp->req_state_flags);
   1539 		break;
   1540 
   1541 	case RQCS_DMA_ERROR:
   1542 		PRINTF("%s: DMA error for command on target %d, lun %d\n",
   1543 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1544 		break;
   1545 
   1546 	case RQCS_TRANSPORT_ERROR:
   1547 		PRINTF("%s: transport error\n", isp->isp_name);
   1548 		isp_prtstst(sp);
   1549 		break;
   1550 
   1551 	case RQCS_RESET_OCCURRED:
   1552 		IDPRINTF(2, ("%s: bus reset destroyed command for target %d "
   1553 		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
   1554 		isp->isp_sendmarker = 1;
   1555 		XS_SETERR(xs, HBA_BUSRESET);
   1556 		return;
   1557 
   1558 	case RQCS_ABORTED:
   1559 		PRINTF("%s: command aborted for target %d lun %d\n",
   1560 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1561 		isp->isp_sendmarker = 1;
   1562 		XS_SETERR(xs, HBA_ABORTED);
   1563 		return;
   1564 
   1565 	case RQCS_TIMEOUT:
   1566 		IDPRINTF(2, ("%s: command timed out for target %d lun %d\n",
   1567 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
   1568 		XS_SETERR(xs, HBA_CMDTIMEOUT);
   1569 		return;
   1570 
   1571 	case RQCS_DATA_OVERRUN:
   1572 		if (isp->isp_type & ISP_HA_FC) {
   1573 			XS_RESID(xs) = sp->req_resid;
   1574 			break;
   1575 		}
   1576 		XS_SETERR(xs, HBA_DATAOVR);
   1577 		return;
   1578 
   1579 	case RQCS_COMMAND_OVERRUN:
   1580 		PRINTF("%s: command overrun for command on target %d, lun %d\n",
   1581 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1582 		break;
   1583 
   1584 	case RQCS_STATUS_OVERRUN:
   1585 		PRINTF("%s: status overrun for command on target %d, lun %d\n",
   1586 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1587 		break;
   1588 
   1589 	case RQCS_BAD_MESSAGE:
   1590 		PRINTF("%s: message not COMMAND COMPLETE after status on "
   1591 		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
   1592 		    XS_LUN(xs));
   1593 		break;
   1594 
   1595 	case RQCS_NO_MESSAGE_OUT:
   1596 		PRINTF("%s: No MESSAGE OUT phase after selection on "
   1597 		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
   1598 		    XS_LUN(xs));
   1599 		break;
   1600 
   1601 	case RQCS_EXT_ID_FAILED:
   1602 		PRINTF("%s: EXTENDED IDENTIFY failed on target %d, lun %d\n",
   1603 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1604 		break;
   1605 
   1606 	case RQCS_IDE_MSG_FAILED:
   1607 		PRINTF("%s: target %d lun %d rejected INITIATOR DETECTED "
   1608 		    "ERROR message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1609 		break;
   1610 
   1611 	case RQCS_ABORT_MSG_FAILED:
   1612 		PRINTF("%s: target %d lun %d rejected ABORT message\n",
   1613 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1614 		break;
   1615 
   1616 	case RQCS_REJECT_MSG_FAILED:
   1617 		PRINTF("%s: target %d lun %d rejected MESSAGE REJECT message\n",
   1618 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1619 		break;
   1620 
   1621 	case RQCS_NOP_MSG_FAILED:
   1622 		PRINTF("%s: target %d lun %d rejected NOP message\n",
   1623 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1624 		break;
   1625 
   1626 	case RQCS_PARITY_ERROR_MSG_FAILED:
   1627 		PRINTF("%s: target %d lun %d rejected MESSAGE PARITY ERROR "
   1628 		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1629 		break;
   1630 
   1631 	case RQCS_DEVICE_RESET_MSG_FAILED:
   1632 		PRINTF("%s: target %d lun %d rejected BUS DEVICE RESET "
   1633 		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1634 		break;
   1635 
   1636 	case RQCS_ID_MSG_FAILED:
   1637 		PRINTF("%s: target %d lun %d rejected IDENTIFY "
   1638 		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1639 		break;
   1640 
   1641 	case RQCS_UNEXP_BUS_FREE:
   1642 		PRINTF("%s: target %d lun %d had an unexpected bus free\n",
   1643 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1644 		break;
   1645 
   1646 	case RQCS_DATA_UNDERRUN:
   1647 		if (isp->isp_type & ISP_HA_FC) {
   1648 			XS_RESID(xs) = sp->req_resid;
   1649 			/* an UNDERRUN is not a botch ??? */
   1650 		}
   1651 		XS_SETERR(xs, HBA_NOERROR);
   1652 		return;
   1653 
   1654 	case RQCS_XACT_ERR1:
   1655 		PRINTF("%s: HBA attempted queued transaction with disconnect "
   1656 		    "not set for target %d lun %d\n", isp->isp_name, XS_TGT(xs),
   1657 		    XS_LUN(xs));
   1658 		break;
   1659 
   1660 	case RQCS_XACT_ERR2:
   1661 		PRINTF("%s: HBA attempted queued transaction to target "
   1662 		    "routine %d on target %d\n", isp->isp_name, XS_LUN(xs),
   1663 		    XS_TGT(xs));
   1664 		break;
   1665 
   1666 	case RQCS_XACT_ERR3:
   1667 		PRINTF("%s: HBA attempted queued transaction for target %d lun "
   1668 		    "%d when queueing disabled\n", isp->isp_name, XS_TGT(xs),
   1669 		    XS_LUN(xs));
   1670 		break;
   1671 
   1672 	case RQCS_BAD_ENTRY:
   1673 		PRINTF("%s: invalid IOCB entry type detected\n", isp->isp_name);
   1674 		break;
   1675 
   1676 	case RQCS_QUEUE_FULL:
   1677 		PRINTF("%s: internal queues full for target %d lun %d\n",
   1678 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1679 		break;
   1680 
   1681 	case RQCS_PHASE_SKIPPED:
   1682 		PRINTF("%s: SCSI phase skipped (e.g., COMMAND COMPLETE w/o "
   1683 		    "STATUS phase) for target %d lun %d\n", isp->isp_name,
   1684 		    XS_TGT(xs), XS_LUN(xs));
   1685 		break;
   1686 
   1687 	case RQCS_ARQS_FAILED:
   1688 		PRINTF("%s: Auto Request Sense failed for target %d lun %d\n",
   1689 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1690 		XS_SETERR(xs, HBA_ARQFAIL);
   1691 		return;
   1692 
   1693 	case RQCS_WIDE_FAILED:
   1694 		PRINTF("%s: Wide Negotiation failed for target %d lun %d\n",
   1695 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1696 		if (isp->isp_type & ISP_HA_SCSI) {
   1697 			sdparam *sdp = isp->isp_param;
   1698 			isp->isp_update = 1;
   1699 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   1700 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
   1701 		}
   1702 		XS_SETERR(xs, HBA_NOERROR);
   1703 		return;
   1704 
   1705 	case RQCS_SYNCXFER_FAILED:
   1706 		PRINTF("%s: SDTR Message failed for target %d lun %d\n",
   1707 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1708 		if (isp->isp_type & ISP_HA_SCSI) {
   1709 			sdparam *sdp = isp->isp_param;
   1710 			isp->isp_update = 1;
   1711 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   1712 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
   1713 		}
   1714 		break;
   1715 
   1716 	case RQCS_LVD_BUSERR:
   1717 		PRINTF("%s: Bad LVD Bus condition while talking to target %d "
   1718 		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1719 		break;
   1720 
   1721 	case RQCS_PORT_UNAVAILABLE:
   1722 		/*
   1723 		 * No such port on the loop. Moral equivalent of SELTIMEO
   1724 		 */
   1725 		IDPRINTF(3, ("%s: Port Unavailable for target %d\n",
   1726 		    isp->isp_name, XS_TGT(xs)));
   1727 		XS_SETERR(xs, HBA_SELTIMEOUT);
   1728 		return;
   1729 
   1730 	case RQCS_PORT_LOGGED_OUT:
   1731 		/*
   1732 		 * It was there (maybe)- treat as a selection timeout.
   1733 		 */
   1734 		PRINTF("%s: port logout for target %d\n",
   1735 			isp->isp_name, XS_TGT(xs));
   1736 		XS_SETERR(xs, HBA_SELTIMEOUT);
   1737 		return;
   1738 
   1739 	case RQCS_PORT_CHANGED:
   1740 		PRINTF("%s: port changed for target %d\n",
   1741 			isp->isp_name, XS_TGT(xs));
   1742 		break;
   1743 
   1744 	case RQCS_PORT_BUSY:
   1745 		PRINTF("%s: port busy for target %d\n",
   1746 			isp->isp_name, XS_TGT(xs));
   1747 		XS_SETERR(xs, HBA_TGTBSY);
   1748 		return;
   1749 
   1750 	default:
   1751 		PRINTF("%s: comp status %x\n", isp->isp_name,
   1752 		       sp->req_completion_status);
   1753 		break;
   1754 	}
   1755 	XS_SETERR(xs, HBA_BOTCH);
   1756 }
   1757 
   1758 #define	HINIB(x)			((x) >> 0x4)
   1759 #define	LONIB(x)			((x)  & 0xf)
   1760 #define MAKNIB(a, b)			(((a) << 4) | (b))
   1761 static u_int8_t mbpcnt[] = {
   1762 	MAKNIB(1, 1),	/* 0x00: MBOX_NO_OP */
   1763 	MAKNIB(5, 5),	/* 0x01: MBOX_LOAD_RAM */
   1764 	MAKNIB(2, 0),	/* 0x02: MBOX_EXEC_FIRMWARE */
   1765 	MAKNIB(5, 5),	/* 0x03: MBOX_DUMP_RAM */
   1766 	MAKNIB(3, 3),	/* 0x04: MBOX_WRITE_RAM_WORD */
   1767 	MAKNIB(2, 3),	/* 0x05: MBOX_READ_RAM_WORD */
   1768 	MAKNIB(6, 6),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   1769 	MAKNIB(2, 3),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   1770 	MAKNIB(1, 3),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   1771 	MAKNIB(0, 0),	/* 0x09: */
   1772 	MAKNIB(0, 0),	/* 0x0a: */
   1773 	MAKNIB(0, 0),	/* 0x0b: */
   1774 	MAKNIB(0, 0),	/* 0x0c: */
   1775 	MAKNIB(0, 0),	/* 0x0d: */
   1776 	MAKNIB(1, 2),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   1777 	MAKNIB(0, 0),	/* 0x0f: */
   1778 	MAKNIB(5, 5),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   1779 	MAKNIB(6, 6),	/* 0x11: MBOX_INIT_RES_QUEUE */
   1780 	MAKNIB(4, 4),	/* 0x12: MBOX_EXECUTE_IOCB */
   1781 	MAKNIB(2, 2),	/* 0x13: MBOX_WAKE_UP	*/
   1782 	MAKNIB(1, 6),	/* 0x14: MBOX_STOP_FIRMWARE */
   1783 	MAKNIB(4, 4),	/* 0x15: MBOX_ABORT */
   1784 	MAKNIB(2, 2),	/* 0x16: MBOX_ABORT_DEVICE */
   1785 	MAKNIB(3, 3),	/* 0x17: MBOX_ABORT_TARGET */
   1786 	MAKNIB(2, 2),	/* 0x18: MBOX_BUS_RESET */
   1787 	MAKNIB(2, 3),	/* 0x19: MBOX_STOP_QUEUE */
   1788 	MAKNIB(2, 3),	/* 0x1a: MBOX_START_QUEUE */
   1789 	MAKNIB(2, 3),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   1790 	MAKNIB(2, 3),	/* 0x1c: MBOX_ABORT_QUEUE */
   1791 	MAKNIB(2, 4),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   1792 	MAKNIB(0, 0),	/* 0x1e: */
   1793 	MAKNIB(1, 3),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   1794 	MAKNIB(1, 3),	/* 0x20: MBOX_GET_INIT_SCSI_ID, MBOX_GET_LOOP_ID */
   1795 	MAKNIB(1, 2),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   1796 	MAKNIB(1, 3),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   1797 	MAKNIB(1, 2),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   1798 	MAKNIB(1, 2),	/* 0x24: MBOX_GET_CLOCK_RATE */
   1799 	MAKNIB(1, 2),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   1800 	MAKNIB(1, 2),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   1801 	MAKNIB(1, 3),	/* 0x27: MBOX_GET_PCI_PARAMS */
   1802 	MAKNIB(2, 4),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   1803 	MAKNIB(2, 4),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   1804 	MAKNIB(0, 0),	/* 0x2a: */
   1805 	MAKNIB(0, 0),	/* 0x2b: */
   1806 	MAKNIB(0, 0),	/* 0x2c: */
   1807 	MAKNIB(0, 0),	/* 0x2d: */
   1808 	MAKNIB(0, 0),	/* 0x2e: */
   1809 	MAKNIB(0, 0),	/* 0x2f: */
   1810 	MAKNIB(2, 2),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   1811 	MAKNIB(2, 2),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   1812 	MAKNIB(3, 3),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   1813 	MAKNIB(2, 2),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   1814 	MAKNIB(2, 2),	/* 0x34: MBOX_SET_CLOCK_RATE */
   1815 	MAKNIB(2, 2),	/* 0x35: MBOX_SET_ACTIVE_NEG_STATE */
   1816 	MAKNIB(2, 2),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   1817 	MAKNIB(3, 3),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   1818 	MAKNIB(4, 4),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   1819 	MAKNIB(4, 4),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   1820 	MAKNIB(0, 0),	/* 0x3a: */
   1821 	MAKNIB(0, 0),	/* 0x3b: */
   1822 	MAKNIB(0, 0),	/* 0x3c: */
   1823 	MAKNIB(0, 0),	/* 0x3d: */
   1824 	MAKNIB(0, 0),	/* 0x3e: */
   1825 	MAKNIB(0, 0),	/* 0x3f: */
   1826 	MAKNIB(1, 2),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   1827 	MAKNIB(6, 1),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   1828 	MAKNIB(2, 3),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   1829 	MAKNIB(0, 0),	/* 0x43: */
   1830 	MAKNIB(0, 0),	/* 0x44: */
   1831 	MAKNIB(0, 0),	/* 0x45: */
   1832 	MAKNIB(0, 0),	/* 0x46: */
   1833 	MAKNIB(0, 0),	/* 0x47: */
   1834 	MAKNIB(0, 0),	/* 0x48: */
   1835 	MAKNIB(0, 0),	/* 0x49: */
   1836 	MAKNIB(0, 0),	/* 0x4a: */
   1837 	MAKNIB(0, 0),	/* 0x4b: */
   1838 	MAKNIB(0, 0),	/* 0x4c: */
   1839 	MAKNIB(0, 0),	/* 0x4d: */
   1840 	MAKNIB(0, 0),	/* 0x4e: */
   1841 	MAKNIB(0, 0),	/* 0x4f: */
   1842 	MAKNIB(0, 0),	/* 0x50: */
   1843 	MAKNIB(0, 0),	/* 0x51: */
   1844 	MAKNIB(0, 0),	/* 0x52: */
   1845 	MAKNIB(0, 0),	/* 0x53: */
   1846 	MAKNIB(8, 0),	/* 0x54: MBOX_EXEC_COMMAND_IOCB_A64 */
   1847 	MAKNIB(0, 0),	/* 0x55: */
   1848 	MAKNIB(0, 0),	/* 0x56: */
   1849 	MAKNIB(0, 0),	/* 0x57: */
   1850 	MAKNIB(0, 0),	/* 0x58: */
   1851 	MAKNIB(0, 0),	/* 0x59: */
   1852 	MAKNIB(0, 0),	/* 0x5a: */
   1853 	MAKNIB(0, 0),	/* 0x5b: */
   1854 	MAKNIB(0, 0),	/* 0x5c: */
   1855 	MAKNIB(0, 0),	/* 0x5d: */
   1856 	MAKNIB(0, 0),	/* 0x5e: */
   1857 	MAKNIB(0, 0),	/* 0x5f: */
   1858 	MAKNIB(8, 6),	/* 0x60: MBOX_INIT_FIRMWARE */
   1859 	MAKNIB(0, 0),	/* 0x60: MBOX_GET_INIT_CONTROL_BLOCK  (FORMAT?) */
   1860 	MAKNIB(2, 1),	/* 0x62: MBOX_INIT_LIP */
   1861 	MAKNIB(8, 1),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   1862 	MAKNIB(8, 1),	/* 0x64: MBOX_GET_PORT_DB */
   1863 	MAKNIB(3, 1),	/* 0x65: MBOX_CLEAR_ACA */
   1864 	MAKNIB(3, 1),	/* 0x66: MBOX_TARGET_RESET */
   1865 	MAKNIB(3, 1),	/* 0x67: MBOX_CLEAR_TASK_SET */
   1866 	MAKNIB(3, 1),	/* 0x69: MBOX_ABORT_TASK_SET */
   1867 	MAKNIB(1, 2)	/* 0x69: MBOX_GET_FW_STATE */
   1868 };
   1869 #define	NMBCOM	(sizeof (mbpcnt) / sizeof (mbpcnt[0]))
   1870 
   1871 static void
   1872 isp_mboxcmd(isp, mbp)
   1873 	struct ispsoftc *isp;
   1874 	mbreg_t *mbp;
   1875 {
   1876 	int outparam, inparam;
   1877 	int loops, dld = 0;
   1878 	u_int8_t opcode;
   1879 
   1880 	if (mbp->param[0] == ISP2100_SET_PCI_PARAM) {
   1881 		opcode = mbp->param[0] = MBOX_SET_PCI_PARAMETERS;
   1882 		inparam = 4;
   1883 		outparam = 4;
   1884 		goto command_known;
   1885 	} else if (mbp->param[0] > NMBCOM) {
   1886 		PRINTF("%s: bad command %x\n", isp->isp_name, mbp->param[0]);
   1887 		return;
   1888 	}
   1889 
   1890 	opcode = mbp->param[0];
   1891 	inparam = HINIB(mbpcnt[mbp->param[0]]);
   1892 	outparam =  LONIB(mbpcnt[mbp->param[0]]);
   1893 
   1894 	if (inparam == 0 && outparam == 0) {
   1895 		PRINTF("%s: no parameters for %x\n", isp->isp_name,
   1896 			mbp->param[0]);
   1897 		return;
   1898 	}
   1899 
   1900 
   1901 	/*
   1902 	 * Check for variants
   1903 	 */
   1904 #ifdef	SCCLUN
   1905 	if (isp->isp_type & ISP_HA_FC) {
   1906 		switch (mbp->param[0]) {
   1907 		case MBOX_ABORT:
   1908 			inparam = 7;
   1909 			break;
   1910 		case MBOX_ABORT_DEVICE:
   1911 		case MBOX_START_QUEUE:
   1912 		case MBOX_STOP_QUEUE:
   1913 		case MBOX_SINGLE_STEP_QUEUE:
   1914 		case MBOX_ABORT_QUEUE:
   1915 		case MBOX_GET_DEV_QUEUE_STATUS:
   1916 			inparam = 3;
   1917 			break;
   1918 		default:
   1919 			break;
   1920 		}
   1921 	}
   1922 #endif
   1923 
   1924 command_known:
   1925 
   1926 	/*
   1927 	 * Make sure we can send some words..
   1928 	 */
   1929 
   1930 	loops = MBOX_DELAY_COUNT;
   1931 	while ((ISP_READ(isp, HCCR) & HCCR_HOST_INT) != 0) {
   1932 		SYS_DELAY(100);
   1933 		if (--loops < 0) {
   1934 			PRINTF("%s: isp_mboxcmd timeout #1\n", isp->isp_name);
   1935 			if (dld++) {
   1936 				return;
   1937 			}
   1938 			PRINTF("%s: but we'll try again, isr=%x\n",
   1939 			    isp->isp_name, ISP_READ(isp, BIU_ISR));
   1940 			if (ISP_READ(isp, BIU_SEMA) & 1) {
   1941 				u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
   1942 				if (isp_parse_async(isp, (int) mbox))
   1943 					return;
   1944 				ISP_WRITE(isp, BIU_SEMA, 0);
   1945 			}
   1946 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   1947 			goto command_known;
   1948 		}
   1949 	}
   1950 
   1951 	/*
   1952 	 * Write input parameters
   1953 	 */
   1954 	switch (inparam) {
   1955 	case 8: ISP_WRITE(isp, INMAILBOX7, mbp->param[7]); mbp->param[7] = 0;
   1956 	case 7: ISP_WRITE(isp, INMAILBOX6, mbp->param[6]); mbp->param[6] = 0;
   1957 	case 6: ISP_WRITE(isp, INMAILBOX5, mbp->param[5]); mbp->param[5] = 0;
   1958 	case 5: ISP_WRITE(isp, INMAILBOX4, mbp->param[4]); mbp->param[4] = 0;
   1959 	case 4: ISP_WRITE(isp, INMAILBOX3, mbp->param[3]); mbp->param[3] = 0;
   1960 	case 3: ISP_WRITE(isp, INMAILBOX2, mbp->param[2]); mbp->param[2] = 0;
   1961 	case 2: ISP_WRITE(isp, INMAILBOX1, mbp->param[1]); mbp->param[1] = 0;
   1962 	case 1: ISP_WRITE(isp, INMAILBOX0, mbp->param[0]); mbp->param[0] = 0;
   1963 	}
   1964 
   1965 	/*
   1966 	 * Clear semaphore on mailbox registers
   1967 	 */
   1968 	ISP_WRITE(isp, BIU_SEMA, 0);
   1969 
   1970 	/*
   1971 	 * Clear RISC int condition.
   1972 	 */
   1973 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   1974 
   1975 	/*
   1976 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   1977 	 */
   1978 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   1979 
   1980 	/*
   1981 	 * Wait until RISC int is set, except 2100
   1982 	 */
   1983 	if ((isp->isp_type & ISP_HA_FC) == 0) {
   1984 		loops = MBOX_DELAY_COUNT;
   1985 		while ((ISP_READ(isp, BIU_ISR) & BIU_ISR_RISC_INT) == 0) {
   1986 			SYS_DELAY(100);
   1987 			if (--loops < 0) {
   1988 				PRINTF("%s: isp_mboxcmd timeout #2\n",
   1989 				    isp->isp_name);
   1990 				return;
   1991 			}
   1992 		}
   1993 	}
   1994 
   1995 	/*
   1996 	 * Check to make sure that the semaphore has been set.
   1997 	 */
   1998 	loops = MBOX_DELAY_COUNT;
   1999 	while ((ISP_READ(isp, BIU_SEMA) & 1) == 0) {
   2000 		SYS_DELAY(100);
   2001 		if (--loops < 0) {
   2002 			PRINTF("%s: isp_mboxcmd timeout #3\n", isp->isp_name);
   2003 			return;
   2004 		}
   2005 	}
   2006 
   2007 	/*
   2008 	 * Make sure that the MBOX_BUSY has gone away
   2009 	 */
   2010 	loops = MBOX_DELAY_COUNT;
   2011 	while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
   2012 		SYS_DELAY(100);
   2013 		if (--loops < 0) {
   2014 			PRINTF("%s: isp_mboxcmd timeout #4\n", isp->isp_name);
   2015 			return;
   2016 		}
   2017 	}
   2018 
   2019 
   2020 	/*
   2021 	 * Pick up output parameters.
   2022 	 */
   2023 	switch (outparam) {
   2024 	case 8: mbp->param[7] = ISP_READ(isp, OUTMAILBOX7);
   2025 	case 7: mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
   2026 	case 6: mbp->param[5] = ISP_READ(isp, OUTMAILBOX5);
   2027 	case 5: mbp->param[4] = ISP_READ(isp, OUTMAILBOX4);
   2028 	case 4: mbp->param[3] = ISP_READ(isp, OUTMAILBOX3);
   2029 	case 3: mbp->param[2] = ISP_READ(isp, OUTMAILBOX2);
   2030 	case 2: mbp->param[1] = ISP_READ(isp, OUTMAILBOX1);
   2031 	case 1: mbp->param[0] = ISP_READ(isp, OUTMAILBOX0);
   2032 	}
   2033 
   2034 	/*
   2035 	 * Clear RISC int.
   2036 	 */
   2037 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2038 
   2039 	/*
   2040 	 * Release semaphore on mailbox registers
   2041 	 */
   2042 	ISP_WRITE(isp, BIU_SEMA, 0);
   2043 
   2044 	/*
   2045 	 * Just to be chatty here...
   2046 	 */
   2047 	switch(mbp->param[0]) {
   2048 	case MBOX_COMMAND_COMPLETE:
   2049 		break;
   2050 	case MBOX_INVALID_COMMAND:
   2051 		IDPRINTF(2, ("%s: mbox cmd %x failed with INVALID_COMMAND\n",
   2052 		    isp->isp_name, opcode));
   2053 		break;
   2054 	case MBOX_HOST_INTERFACE_ERROR:
   2055 		PRINTF("%s: mbox cmd %x failed with HOST_INTERFACE_ERROR\n",
   2056 		    isp->isp_name, opcode);
   2057 		break;
   2058 	case MBOX_TEST_FAILED:
   2059 		PRINTF("%s: mbox cmd %x failed with TEST_FAILED\n",
   2060 		    isp->isp_name, opcode);
   2061 		break;
   2062 	case MBOX_COMMAND_ERROR:
   2063 		PRINTF("%s: mbox cmd %x failed with COMMAND_ERROR\n",
   2064 		    isp->isp_name, opcode);
   2065 		break;
   2066 	case MBOX_COMMAND_PARAM_ERROR:
   2067 		PRINTF("%s: mbox cmd %x failed with COMMAND_PARAM_ERROR\n",
   2068 		    isp->isp_name, opcode);
   2069 		break;
   2070 
   2071 	/*
   2072 	 * Be silent about these...
   2073 	 */
   2074 
   2075 	case ASYNC_LOOP_UP:
   2076 	case ASYNC_LIP_OCCURRED:
   2077 	case ASYNC_PDB_CHANGED:
   2078 		break;
   2079 
   2080 	default:
   2081 		/*
   2082 		 * The expected return of EXEC_FIRMWARE is zero.
   2083 		 */
   2084 		if ((opcode == MBOX_EXEC_FIRMWARE && mbp->param[0] != 0) ||
   2085 		    (opcode != MBOX_EXEC_FIRMWARE)) {
   2086 			PRINTF("%s: mbox cmd %x failed with error %x\n",
   2087 				isp->isp_name, opcode, mbp->param[0]);
   2088 		}
   2089 		break;
   2090 	}
   2091 }
   2092 
   2093 void
   2094 isp_lostcmd(isp, xs)
   2095 	struct ispsoftc *isp;
   2096 	ISP_SCSI_XFER_T *xs;
   2097 {
   2098 	mbreg_t mbs;
   2099 
   2100 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
   2101 	isp_mboxcmd(isp, &mbs);
   2102 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2103 		isp_dumpregs(isp, "couldn't GET FIRMWARE STATUS");
   2104 		return;
   2105 	}
   2106 	if (mbs.param[1]) {
   2107 		PRINTF("%s: %d commands on completion queue\n",
   2108 		       isp->isp_name, mbs.param[1]);
   2109 	}
   2110 	if (XS_NULL(xs))
   2111 		return;
   2112 
   2113 	mbs.param[0] = MBOX_GET_DEV_QUEUE_STATUS;
   2114 	mbs.param[1] = (XS_TGT(xs) << 8) | XS_LUN(xs);
   2115 	isp_mboxcmd(isp, &mbs);
   2116 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2117 		isp_dumpregs(isp, "couldn't GET DEVICE QUEUE STATUS");
   2118 		return;
   2119 	}
   2120 	PRINTF("%s: lost command for target %d lun %d, %d active of %d, "
   2121 		"Queue State: %x\n", isp->isp_name, XS_TGT(xs),
   2122 		XS_LUN(xs), mbs.param[2], mbs.param[3], mbs.param[1]);
   2123 
   2124 	isp_dumpregs(isp, "lost command");
   2125 	/*
   2126 	 * XXX: Need to try and do something to recover.
   2127 	 */
   2128 }
   2129 
   2130 static void
   2131 isp_dumpregs(isp, msg)
   2132 	struct ispsoftc *isp;
   2133 	const char *msg;
   2134 {
   2135 	PRINTF("%s: %s\n", isp->isp_name, msg);
   2136 	if (isp->isp_type & ISP_HA_SCSI)
   2137 		PRINTF("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
   2138 	else
   2139 		PRINTF("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
   2140 	PRINTF(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
   2141 	       ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
   2142 	PRINTF("risc_hccr=%x\n", ISP_READ(isp, HCCR));
   2143 
   2144 	if (isp->isp_type & ISP_HA_SCSI) {
   2145 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   2146 		PRINTF("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
   2147 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
   2148 			ISP_READ(isp, CDMA_FIFO_STS));
   2149 		PRINTF("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
   2150 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
   2151 			ISP_READ(isp, DDMA_FIFO_STS));
   2152 		PRINTF("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
   2153 			ISP_READ(isp, SXP_INTERRUPT),
   2154 			ISP_READ(isp, SXP_GROSS_ERR),
   2155 			ISP_READ(isp, SXP_PINS_CONTROL));
   2156 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
   2157 	}
   2158 	ISP_DUMPREGS(isp);
   2159 }
   2160 
   2161 static void
   2162 isp_dumpxflist(isp)
   2163 	struct ispsoftc *isp;
   2164 {
   2165 	volatile ISP_SCSI_XFER_T *xs;
   2166 	int i, hdp;
   2167 
   2168 	for (hdp = i = 0; i < RQUEST_QUEUE_LEN; i++) {
   2169 		xs = isp->isp_xflist[i];
   2170 		if (xs == NULL) {
   2171 			continue;
   2172 		}
   2173 		if (hdp == 0) {
   2174 			PRINTF("%s: active requests\n", isp->isp_name);
   2175 			hdp++;
   2176 		}
   2177 		PRINTF(" Active Handle %d: tgt %d lun %d dlen %d\n",
   2178 		    i+1, XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs));
   2179 	}
   2180 }
   2181 
   2182 static void
   2183 isp_fw_state(isp)
   2184 	struct ispsoftc *isp;
   2185 {
   2186 	mbreg_t mbs;
   2187 	if (isp->isp_type & ISP_HA_FC) {
   2188 		int once = 0;
   2189 		fcparam *fcp = isp->isp_param;
   2190 again:
   2191 		mbs.param[0] = MBOX_GET_FW_STATE;
   2192 		isp_mboxcmd(isp, &mbs);
   2193 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2194 			if (mbs.param[0] == ASYNC_LIP_OCCURRED ||
   2195 			    mbs.param[0] == ASYNC_PDB_CHANGED ||
   2196 			    mbs.param[0] == ASYNC_LOOP_UP) {
   2197 				if (once++ < 2) {
   2198 					goto again;
   2199 				}
   2200 			}
   2201 			isp_dumpregs(isp, "GET FIRMWARE STATE failed");
   2202 			return;
   2203 		}
   2204 		fcp->isp_fwstate = mbs.param[1];
   2205 	}
   2206 }
   2207 
   2208 static void
   2209 isp_update(isp)
   2210 	struct ispsoftc *isp;
   2211 {
   2212 	int tgt;
   2213 	mbreg_t mbs;
   2214 	sdparam *sdp;
   2215 
   2216 	isp->isp_update = 0;
   2217 
   2218 	if (isp->isp_type & ISP_HA_FC) {
   2219 		return;
   2220 	}
   2221 
   2222 	sdp = isp->isp_param;
   2223 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   2224 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   2225 			continue;
   2226 		}
   2227 		if (sdp->isp_devparam[tgt].dev_update == 0) {
   2228 			continue;
   2229 		}
   2230 
   2231 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   2232 		mbs.param[1] = tgt << 8;
   2233 		mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
   2234 		mbs.param[3] =
   2235 			(sdp->isp_devparam[tgt].sync_offset << 8) |
   2236 			(sdp->isp_devparam[tgt].sync_period);
   2237 
   2238 		IDPRINTF(3, ("\n%s: tgt %d cflags %x offset %x period %x\n",
   2239 		    isp->isp_name, tgt, mbs.param[2], mbs.param[3] >> 8,
   2240 		    mbs.param[3] & 0xff));
   2241 
   2242 		isp_mboxcmd(isp, &mbs);
   2243 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2244 			PRINTF("%s: failed to change SCSI parameters for "
   2245 			    "target %d\n", isp->isp_name, tgt);
   2246 		} else {
   2247 			char *wt;
   2248 			int x, flags;
   2249 
   2250 			flags = sdp->isp_devparam[tgt].cur_dflags =
   2251 			    sdp->isp_devparam[tgt].dev_flags;
   2252 
   2253 			x = sdp->isp_devparam[tgt].sync_period & 0xff;
   2254 			if (flags & DPARM_SYNC) {
   2255 				if (x == (ISP_20M_SYNCPARMS & 0xff)) {
   2256 					x = 20;
   2257 				} else if (x == (ISP_10M_SYNCPARMS & 0xff)) {
   2258 					x = 10;
   2259 				} else if (x == (ISP_08M_SYNCPARMS & 0xff)) {
   2260 					x = 8;
   2261 				} else if (x == (ISP_05M_SYNCPARMS & 0xff)) {
   2262 					x = 5;
   2263 				} else if (x == (ISP_04M_SYNCPARMS & 0xff)) {
   2264 					x = 4;
   2265 				} else {
   2266 					x = 0;
   2267 				}
   2268 			} else {
   2269 				x = 0;
   2270 			}
   2271 			switch (flags & (DPARM_WIDE|DPARM_TQING)) {
   2272 			case DPARM_WIDE:
   2273 				wt = ", 16 bit wide\n";
   2274 				break;
   2275 			case DPARM_TQING:
   2276 				wt = ", Tagged Queueing Enabled\n";
   2277 				break;
   2278 			case DPARM_WIDE|DPARM_TQING:
   2279 				wt = ", 16 bit wide, Tagged Queueing Enabled\n";
   2280 				break;
   2281 
   2282 			default:
   2283 				wt = "\n";
   2284 				break;
   2285 			}
   2286 			if (x) {
   2287 				IDPRINTF(3, ("%s: Target %d maximum Sync Mode "
   2288 				    "at %dMHz%s", isp->isp_name, tgt, x, wt));
   2289 			} else {
   2290 				IDPRINTF(3, ("%s: Target %d Async Mode%s",
   2291 				    isp->isp_name, tgt, wt));
   2292 			}
   2293 		}
   2294 		sdp->isp_devparam[tgt].dev_update = 0;
   2295 	}
   2296 }
   2297 
   2298 static void
   2299 isp_setdfltparm(isp)
   2300 	struct ispsoftc *isp;
   2301 {
   2302 	int i, use_nvram;
   2303 	mbreg_t mbs;
   2304 	sdparam *sdp;
   2305 
   2306 	/*
   2307 	 * Been there, done that, got the T-shirt...
   2308 	 */
   2309 	if (isp->isp_gotdparms) {
   2310 		IDPRINTF(3, ("%s: already have dparms\n", isp->isp_name));
   2311 		return;
   2312 	}
   2313 	isp->isp_gotdparms = 1;
   2314 
   2315 	use_nvram = (isp_read_nvram(isp) == 0);
   2316 	if (use_nvram) {
   2317 		return;
   2318 	}
   2319 	if (isp->isp_type & ISP_HA_FC) {
   2320 		fcparam *fcp = (fcparam *) isp->isp_param;
   2321 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
   2322 		fcp->isp_maxalloc = 256;
   2323 		fcp->isp_execthrottle = 16;
   2324 		fcp->isp_retry_delay = 5;
   2325 		fcp->isp_retry_count = 0;
   2326 		/*
   2327 		 * It would be nice to fake up a WWN in case we don't
   2328 		 * get one out of NVRAM. Solaris does this for SOCAL
   2329 		 * cards that don't have SBus properties- it sets up
   2330 		 * a WWN based upon the system MAC Address.
   2331 		 */
   2332 		fcp->isp_wwn = 0;
   2333 		return;
   2334 	}
   2335 
   2336 	sdp = (sdparam *) isp->isp_param;
   2337 	mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   2338 	isp_mboxcmd(isp, &mbs);
   2339 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2340 		IDPRINTF(2, ("could not GET ACT NEG STATE\n"));
   2341 		sdp->isp_req_ack_active_neg = 1;
   2342 		sdp->isp_data_line_active_neg = 1;
   2343 	} else {
   2344 		sdp->isp_req_ack_active_neg = (mbs.param[1] >> 4) & 0x1;
   2345 		sdp->isp_data_line_active_neg = (mbs.param[1] >> 5) & 0x1;
   2346 	}
   2347 	for (i = 0; i < MAX_TARGETS; i++) {
   2348 
   2349 		mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   2350 		mbs.param[1] = i << 8;
   2351 		isp_mboxcmd(isp, &mbs);
   2352 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2353 			PRINTF("%s: can't get SCSI parameters for target %d\n",
   2354 			    isp->isp_name, i);
   2355 			sdp->isp_devparam[i].sync_period = 0;
   2356 			sdp->isp_devparam[i].sync_offset = 0;
   2357 			sdp->isp_devparam[i].dev_flags = DPARM_SAFE_DFLT;
   2358 			continue;
   2359 		}
   2360 		sdp->isp_devparam[i].dev_flags = mbs.param[2];
   2361 
   2362 		/*
   2363 		 * The maximum period we can really see
   2364 		 * here is 100 (decimal), or 400 ns.
   2365 		 * For some unknown reason we sometimes
   2366 		 * get back wildass numbers from the
   2367 		 * boot device's parameters.
   2368 		 *
   2369 		 * XXX: Hmm- this may be based on a different
   2370 		 * XXX: clock rate.
   2371 		 */
   2372 		if ((mbs.param[3] & 0xff) <= 0x64) {
   2373 			sdp->isp_devparam[i].sync_period = mbs.param[3] & 0xff;
   2374 			sdp->isp_devparam[i].sync_offset = mbs.param[3] >> 8;
   2375 		}
   2376 
   2377 		/*
   2378 		 * It is not safe to run Ultra Mode with a clock < 60.
   2379 		 */
   2380 		if (((sdp->isp_clock && sdp->isp_clock < 60) ||
   2381 		    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
   2382 		    (sdp->isp_devparam[i].sync_period ==
   2383 		    (ISP_20M_SYNCPARMS & 0xff))) {
   2384 			sdp->isp_devparam[i].sync_offset =
   2385 				ISP_10M_SYNCPARMS >> 8;
   2386 			sdp->isp_devparam[i].sync_period =
   2387 				ISP_10M_SYNCPARMS & 0xff;
   2388 		}
   2389 
   2390 	}
   2391 
   2392 	/*
   2393 	 * Set Default Host Adapter Parameters
   2394 	 */
   2395 	sdp->isp_cmd_dma_burst_enable = 1;
   2396 	sdp->isp_data_dma_burst_enabl = 1;
   2397 	sdp->isp_fifo_threshold = 0;
   2398 	sdp->isp_initiator_id = 7;
   2399 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   2400 		sdp->isp_async_data_setup = 9;
   2401 	} else {
   2402 		sdp->isp_async_data_setup = 6;
   2403 	}
   2404 	sdp->isp_selection_timeout = 250;
   2405 	sdp->isp_max_queue_depth = 128;
   2406 	sdp->isp_tag_aging = 8;
   2407 	sdp->isp_bus_reset_delay = 3;
   2408 	sdp->isp_retry_count = 0;
   2409 	sdp->isp_retry_delay = 1;
   2410 
   2411 	for (i = 0; i < MAX_TARGETS; i++) {
   2412 		sdp->isp_devparam[i].exc_throttle = 16;
   2413 		sdp->isp_devparam[i].dev_enable = 1;
   2414 	}
   2415 }
   2416 
   2417 /*
   2418  * Re-initialize the ISP and complete all orphaned commands
   2419  * with a 'botched' notice.
   2420  *
   2421  * Locks held prior to coming here.
   2422  */
   2423 
   2424 void
   2425 isp_restart(isp)
   2426 	struct ispsoftc *isp;
   2427 {
   2428 	ISP_SCSI_XFER_T *tlist[RQUEST_QUEUE_LEN], *xs;
   2429 	int i;
   2430 
   2431 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   2432 		tlist[i] = (ISP_SCSI_XFER_T *) isp->isp_xflist[i];
   2433 		isp->isp_xflist[i] = NULL;
   2434 	}
   2435 	isp_reset(isp);
   2436 	if (isp->isp_state == ISP_RESETSTATE) {
   2437 		isp_init(isp);
   2438 		if (isp->isp_state == ISP_INITSTATE) {
   2439 			isp->isp_state = ISP_RUNSTATE;
   2440 		}
   2441 	}
   2442 	if (isp->isp_state != ISP_RUNSTATE) {
   2443 		PRINTF("%s: isp_restart cannot restart ISP\n", isp->isp_name);
   2444 	}
   2445 
   2446 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   2447 		xs = tlist[i];
   2448 		if (XS_NULL(xs)) {
   2449 			continue;
   2450 		}
   2451 		if (isp->isp_nactive > 0)
   2452 		    isp->isp_nactive--;
   2453 		XS_RESID(xs) = XS_XFRLEN(xs);
   2454 		XS_SETERR(xs, HBA_BUSRESET);
   2455 		XS_CMD_DONE(xs);
   2456 	}
   2457 }
   2458 
   2459 void
   2460 isp_watch(arg)
   2461 	void *arg;
   2462 {
   2463 	int i;
   2464 	struct ispsoftc *isp = arg;
   2465 	ISP_SCSI_XFER_T *xs;
   2466 	ISP_LOCKVAL_DECL;
   2467 
   2468 	/*
   2469 	 * Look for completely dead commands (but not polled ones).
   2470 	 */
   2471 	ISP_ILOCK(isp);
   2472 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   2473 		if ((xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[i]) == NULL) {
   2474 			continue;
   2475 		}
   2476 		if (XS_TIME(xs) == 0) {
   2477 			continue;
   2478 		}
   2479 		XS_TIME(xs) -= (WATCH_INTERVAL * 1000);
   2480 		/*
   2481 		 * Avoid later thinking that this
   2482 		 * transaction is not being timed.
   2483 		 * Then give ourselves to watchdog
   2484 		 * periods of grace.
   2485 		 */
   2486 		if (XS_TIME(xs) == 0)
   2487 			XS_TIME(xs) = 1;
   2488 		else if (XS_TIME(xs) > -(2 * WATCH_INTERVAL * 1000)) {
   2489 			continue;
   2490 		}
   2491 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
   2492 			PRINTF("%s: isp_watch failed to abort command\n",
   2493 			    isp->isp_name);
   2494 			isp_restart(isp);
   2495 			break;
   2496 		}
   2497 	}
   2498 	ISP_IUNLOCK(isp);
   2499 	RESTART_WATCHDOG(isp_watch, isp);
   2500 }
   2501 
   2502 /*
   2503  * Miscellaneous debug statements.
   2504  */
   2505 static void
   2506 isp_prtstst(sp)
   2507 	ispstatusreq_t *sp;
   2508 {
   2509 	PRINTF("states->");
   2510 	if (sp->req_state_flags & RQSF_GOT_BUS)
   2511 		PRINTF("GOT_BUS ");
   2512 	if (sp->req_state_flags & RQSF_GOT_TARGET)
   2513 		PRINTF("GOT_TGT ");
   2514 	if (sp->req_state_flags & RQSF_SENT_CDB)
   2515 		PRINTF("SENT_CDB ");
   2516 	if (sp->req_state_flags & RQSF_XFRD_DATA)
   2517 		PRINTF("XFRD_DATA ");
   2518 	if (sp->req_state_flags & RQSF_GOT_STATUS)
   2519 		PRINTF("GOT_STS ");
   2520 	if (sp->req_state_flags & RQSF_GOT_SENSE)
   2521 		PRINTF("GOT_SNS ");
   2522 	if (sp->req_state_flags & RQSF_XFER_COMPLETE)
   2523 		PRINTF("XFR_CMPLT ");
   2524 	PRINTF("\n");
   2525 	PRINTF("status->");
   2526 	if (sp->req_status_flags & RQSTF_DISCONNECT)
   2527 		PRINTF("Disconnect ");
   2528 	if (sp->req_status_flags & RQSTF_SYNCHRONOUS)
   2529 		PRINTF("Sync_xfr ");
   2530 	if (sp->req_status_flags & RQSTF_PARITY_ERROR)
   2531 		PRINTF("Parity ");
   2532 	if (sp->req_status_flags & RQSTF_BUS_RESET)
   2533 		PRINTF("Bus_Reset ");
   2534 	if (sp->req_status_flags & RQSTF_DEVICE_RESET)
   2535 		PRINTF("Device_Reset ");
   2536 	if (sp->req_status_flags & RQSTF_ABORTED)
   2537 		PRINTF("Aborted ");
   2538 	if (sp->req_status_flags & RQSTF_TIMEOUT)
   2539 		PRINTF("Timeout ");
   2540 	if (sp->req_status_flags & RQSTF_NEGOTIATION)
   2541 		PRINTF("Negotiation ");
   2542 	PRINTF("\n");
   2543 }
   2544 
   2545 static char *
   2546 isp2100_fw_statename(state)
   2547 	int state;
   2548 {
   2549 	switch(state) {
   2550 	case FW_CONFIG_WAIT:	return "Config Wait";
   2551 	case FW_WAIT_AL_PA:	return "Waiting for AL/PA";
   2552 	case FW_WAIT_LOGIN:	return "Wait Login";
   2553 	case FW_READY:		return "Ready";
   2554 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
   2555 	case FW_ERROR:		return "Error";
   2556 	case FW_REINIT:		return "Re-Init";
   2557 	case FW_NON_PART:	return "Nonparticipating";
   2558 	default:		return "eh?";
   2559 	}
   2560 }
   2561 
   2562 /*
   2563  * NVRAM Routines
   2564  */
   2565 
   2566 static int
   2567 isp_read_nvram(isp)
   2568 	struct ispsoftc *isp;
   2569 {
   2570 	int i, amt;
   2571 	u_int8_t csum, minversion;
   2572 	union {
   2573 		u_int8_t _x[ISP2100_NVRAM_SIZE];
   2574 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
   2575 	} _n;
   2576 #define	nvram_data	_n._x
   2577 #define	nvram_words	_n._s
   2578 
   2579 	if (isp->isp_type & ISP_HA_FC) {
   2580 		amt = ISP2100_NVRAM_SIZE;
   2581 		minversion = 1;
   2582 	} else {
   2583 		amt = ISP_NVRAM_SIZE;
   2584 		minversion = 2;
   2585 	}
   2586 
   2587 	/*
   2588 	 * Just read the first two words first to see if we have a valid
   2589 	 * NVRAM to continue reading the rest with.
   2590 	 */
   2591 	for (i = 0; i < 2; i++) {
   2592 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   2593 	}
   2594 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   2595 	    nvram_data[2] != 'P') {
   2596 		if (isp->isp_bustype != ISP_BT_SBUS) {
   2597 			PRINTF("%s: invalid NVRAM header\n", isp->isp_name);
   2598 		}
   2599 		return (-1);
   2600 	}
   2601 	for (i = 2; i < amt>>1; i++) {
   2602 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   2603 	}
   2604 	for (csum = 0, i = 0; i < amt; i++) {
   2605 		csum += nvram_data[i];
   2606 	}
   2607 	if (csum != 0) {
   2608 		PRINTF("%s: invalid NVRAM checksum\n", isp->isp_name);
   2609 		return (-1);
   2610 	}
   2611 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   2612 		PRINTF("%s: version %d NVRAM not understood\n", isp->isp_name,
   2613 		    ISP_NVRAM_VERSION(nvram_data));
   2614 		return (-1);
   2615 	}
   2616 
   2617 	if (isp->isp_type & ISP_HA_SCSI) {
   2618 		sdparam *sdp = (sdparam *) isp->isp_param;
   2619 
   2620 		/* XXX CHECK THIS FOR SANITY XXX */
   2621 		sdp->isp_fifo_threshold =
   2622 			ISP_NVRAM_FIFO_THRESHOLD(nvram_data);
   2623 
   2624 		sdp->isp_initiator_id =
   2625 			ISP_NVRAM_INITIATOR_ID(nvram_data);
   2626 
   2627 		sdp->isp_bus_reset_delay =
   2628 			ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   2629 
   2630 		sdp->isp_retry_count =
   2631 			ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   2632 
   2633 		sdp->isp_retry_delay =
   2634 			ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   2635 
   2636 		sdp->isp_async_data_setup =
   2637 			ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   2638 
   2639 		if (isp->isp_type >= ISP_HA_SCSI_1040) {
   2640 			if (sdp->isp_async_data_setup < 9) {
   2641 				sdp->isp_async_data_setup = 9;
   2642 			}
   2643 		} else {
   2644 			if (sdp->isp_async_data_setup != 6) {
   2645 				sdp->isp_async_data_setup = 6;
   2646 			}
   2647 		}
   2648 
   2649 		sdp->isp_req_ack_active_neg =
   2650 			ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   2651 
   2652 		sdp->isp_data_line_active_neg =
   2653 			ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   2654 
   2655 		sdp->isp_data_dma_burst_enabl =
   2656 			ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   2657 
   2658 		sdp->isp_cmd_dma_burst_enable =
   2659 			ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   2660 
   2661 		sdp->isp_tag_aging =
   2662 			ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   2663 
   2664 		/* XXX ISP_NVRAM_FIFO_THRESHOLD_128 XXX */
   2665 
   2666 		sdp->isp_selection_timeout =
   2667 			ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   2668 
   2669 		sdp->isp_max_queue_depth =
   2670 			ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   2671 
   2672 		sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   2673 
   2674 		for (i = 0; i < 16; i++) {
   2675 			sdp->isp_devparam[i].dev_enable =
   2676 				ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
   2677 			sdp->isp_devparam[i].exc_throttle =
   2678 				ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
   2679 			sdp->isp_devparam[i].sync_offset =
   2680 				ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
   2681 			sdp->isp_devparam[i].sync_period =
   2682 				ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
   2683 
   2684 			if (isp->isp_type < ISP_HA_SCSI_1040) {
   2685 				/*
   2686 				 * If we're not ultra, we can't possibly
   2687 				 * be a shorter period than this.
   2688 				 */
   2689 				if (sdp->isp_devparam[i].sync_period < 0x19) {
   2690 					sdp->isp_devparam[i].sync_period =
   2691 					    0x19;
   2692 				}
   2693 				if (sdp->isp_devparam[i].sync_offset > 0xc) {
   2694 					sdp->isp_devparam[i].sync_offset =
   2695 					    0x0c;
   2696 				}
   2697 			} else {
   2698 				if (sdp->isp_devparam[i].sync_offset > 0x8) {
   2699 					sdp->isp_devparam[i].sync_offset = 0x8;
   2700 				}
   2701 			}
   2702 
   2703 			sdp->isp_devparam[i].dev_flags = 0;
   2704 
   2705 			if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
   2706 				sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   2707 			if (ISP_NVRAM_TGT_QFRZ(nvram_data, i)) {
   2708 				PRINTF("%s: not supporting QFRZ option for "
   2709 				    "target %d\n", isp->isp_name, i);
   2710 			}
   2711 			sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   2712 			if (ISP_NVRAM_TGT_ARQ(nvram_data, i) == 0) {
   2713 				PRINTF("%s: not disabling ARQ option for "
   2714 				    "target %d\n", isp->isp_name, i);
   2715 			}
   2716 			if (ISP_NVRAM_TGT_TQING(nvram_data, i))
   2717 				sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   2718 			if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
   2719 				sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   2720 			if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
   2721 				sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   2722 			if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
   2723 				sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   2724 			if (ISP_NVRAM_TGT_DISC(nvram_data, i))
   2725 				sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   2726 		}
   2727 	} else {
   2728 		fcparam *fcp = (fcparam *) isp->isp_param;
   2729 		union {
   2730 			struct {
   2731 #if	BYTE_ORDER == BIG_ENDIAN
   2732 				u_int32_t hi32;
   2733 				u_int32_t lo32;
   2734 #else
   2735 				u_int32_t lo32;
   2736 				u_int32_t hi32;
   2737 #endif
   2738 			} wds;
   2739 			u_int64_t full64;
   2740 		} wwnstore;
   2741 
   2742 		wwnstore.full64 = ISP2100_NVRAM_NODE_NAME(nvram_data);
   2743 		PRINTF("%s: Adapter WWN 0x%08x%08x\n", isp->isp_name,
   2744 		    wwnstore.wds.hi32, wwnstore.wds.lo32);
   2745 		fcp->isp_wwn = wwnstore.full64;
   2746 		wwnstore.full64 = ISP2100_NVRAM_BOOT_NODE_NAME(nvram_data);
   2747 		if (wwnstore.full64 != 0) {
   2748 			PRINTF("%s: BOOT DEVICE WWN 0x%08x%08x\n",
   2749 			    isp->isp_name, wwnstore.wds.hi32,
   2750 			    wwnstore.wds.lo32);
   2751 		}
   2752 		fcp->isp_maxalloc =
   2753 			ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   2754 		fcp->isp_maxfrmlen =
   2755 			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   2756 		fcp->isp_retry_delay =
   2757 			ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   2758 		fcp->isp_retry_count =
   2759 			ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   2760 		fcp->isp_loopid =
   2761 			ISP2100_NVRAM_HARDLOOPID(nvram_data);
   2762 		fcp->isp_execthrottle =
   2763 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   2764 	}
   2765 	return (0);
   2766 }
   2767 
   2768 static void
   2769 isp_rdnvram_word(isp, wo, rp)
   2770 	struct ispsoftc *isp;
   2771 	int wo;
   2772 	u_int16_t *rp;
   2773 {
   2774 	int i, cbits;
   2775 	u_int16_t bit, rqst;
   2776 
   2777 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   2778 	SYS_DELAY(2);
   2779 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   2780 	SYS_DELAY(2);
   2781 
   2782 	if (isp->isp_type & ISP_HA_FC) {
   2783 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   2784 		rqst = (ISP_NVRAM_READ << 8) | wo;
   2785 		cbits = 10;
   2786 	} else {
   2787 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   2788 		rqst = (ISP_NVRAM_READ << 6) | wo;
   2789 		cbits = 8;
   2790 	}
   2791 
   2792 	/*
   2793 	 * Clock the word select request out...
   2794 	 */
   2795 	for (i = cbits; i >= 0; i--) {
   2796 		if ((rqst >> i) & 1) {
   2797 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   2798 		} else {
   2799 			bit = BIU_NVRAM_SELECT;
   2800 		}
   2801 		ISP_WRITE(isp, BIU_NVRAM, bit);
   2802 		SYS_DELAY(2);
   2803 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   2804 		SYS_DELAY(2);
   2805 		ISP_WRITE(isp, BIU_NVRAM, bit);
   2806 		SYS_DELAY(2);
   2807 	}
   2808 	/*
   2809 	 * Now read the result back in (bits come back in MSB format).
   2810 	 */
   2811 	*rp = 0;
   2812 	for (i = 0; i < 16; i++) {
   2813 		u_int16_t rv;
   2814 		*rp <<= 1;
   2815 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   2816 		SYS_DELAY(2);
   2817 		rv = ISP_READ(isp, BIU_NVRAM);
   2818 		if (rv & BIU_NVRAM_DATAIN) {
   2819 			*rp |= 1;
   2820 		}
   2821 		SYS_DELAY(2);
   2822 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   2823 		SYS_DELAY(2);
   2824 	}
   2825 	ISP_WRITE(isp, BIU_NVRAM, 0);
   2826 	SYS_DELAY(2);
   2827 #if	BYTE_ORDER == BIG_ENDIAN
   2828 	*rp = ((*rp >> 8) | ((*rp & 0xff) << 8));
   2829 #endif
   2830 }
   2831