Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.23
      1 /* $NetBSD: isp.c,v 1.23 1998/07/15 19:50:16 mjacob Exp $ */
      2 /* $Id: isp.c,v 1.23 1998/07/15 19:50:16 mjacob Exp $ */
      3 /*
      4  * Machine 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  * Function prototypes.
     66  */
     67 static void isp_parse_status
     68 __P((struct ispsoftc *, ispstatusreq_t *, ISP_SCSI_XFER_T *));
     69 static void isp_fibre_init __P((struct ispsoftc *));
     70 static void isp_fw_state __P((struct ispsoftc *));
     71 static void isp_dumpregs __P((struct ispsoftc *, const char *));
     72 static void isp_dumpxflist __P((struct ispsoftc *));
     73 static void isp_setdparm __P((struct ispsoftc *));
     74 static void isp_prtstst __P((ispstatusreq_t *));
     75 static void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *));
     76 
     77 /*
     78  * Reset Hardware.
     79  */
     80 void
     81 isp_reset(isp)
     82 	struct ispsoftc *isp;
     83 {
     84 	mbreg_t mbs;
     85 	int loops, i, dodnld = 1;
     86 	char *revname;
     87 	ISP_LOCKVAL_DECL;
     88 
     89 	isp->isp_state = ISP_NILSTATE;
     90 
     91 	/*
     92 	 * Basic types have been set in the MD code.
     93 	 * See if we can't figure out more here.
     94 	 */
     95 	isp->isp_dblev = DFLT_DBLEVEL;
     96 	if (isp->isp_type & ISP_HA_FC) {
     97 		revname = "2100";
     98 	} else {
     99 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
    100 		switch (i) {
    101 		default:
    102 			PRINTF("%s: unknown ISP type %x- assuming 1020\n",
    103 			    isp->isp_name, i);
    104 			isp->isp_type = ISP_HA_SCSI_1020;
    105 			revname = "10X0";
    106 			break;
    107 		case 1:
    108 			revname = "1020";
    109 			isp->isp_type = ISP_HA_SCSI_1020;
    110 			break;
    111 		case 2:
    112 			revname = "1020A";
    113 			isp->isp_type = ISP_HA_SCSI_1020A;
    114 			break;
    115 		case 3:
    116 			revname = "1040A";
    117 			isp->isp_type = ISP_HA_SCSI_1040A;
    118 			break;
    119 		case 5:
    120 			revname = "1040B";
    121 			isp->isp_type = ISP_HA_SCSI_1040B;
    122 			break;
    123 		}
    124 	}
    125 
    126 	/*
    127 	 * Do MD specific pre initialization
    128 	 */
    129 	ISP_LOCK(isp);
    130 	ISP_RESET0(isp);
    131 	isp_setdparm(isp);	/*
    132 				 * XXX- need to get rid of thie call
    133 				 * XXX- prior to a reset. We need
    134 				 * XXX- to get this now only to try
    135 				 * XXX- and read the clock settings.
    136 				 * XXX- typically for SBus only.
    137 				 */
    138 	/*
    139 	 * Hit the chip over the head with hammer,
    140 	 * and give the ISP a chance to recover.
    141 	 */
    142 
    143 	if (isp->isp_type & ISP_HA_SCSI) {
    144 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
    145 		/*
    146 		 * A slight delay...
    147 		 */
    148 		SYS_DELAY(100);
    149 
    150 		/*
    151 		 * Clear data && control DMA engines.
    152 		 */
    153 		ISP_WRITE(isp, CDMA_CONTROL,
    154 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    155 		ISP_WRITE(isp, DDMA_CONTROL,
    156 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    157 	} else {
    158 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
    159 		/*
    160 		 * A slight delay...
    161 		 */
    162 		SYS_DELAY(100);
    163 		ISP_WRITE(isp, CDMA2100_CONTROL,
    164 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    165 		ISP_WRITE(isp, TDMA2100_CONTROL,
    166 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    167 		ISP_WRITE(isp, RDMA2100_CONTROL,
    168 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    169 	}
    170 
    171 	/*
    172 	 * Wait for ISP to be ready to go...
    173 	 */
    174 	loops = MBOX_DELAY_COUNT;
    175 	for (;;) {
    176 		if (isp->isp_type & ISP_HA_SCSI) {
    177 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
    178 				break;
    179 		} else {
    180 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
    181 				break;
    182 		}
    183 		SYS_DELAY(100);
    184 		if (--loops < 0) {
    185 			isp_dumpregs(isp, "chip reset timed out");
    186 			ISP_UNLOCK(isp);
    187 			return;
    188 		}
    189 	}
    190 	/*
    191 	 * More initialization
    192 	 */
    193 	if (isp->isp_type & ISP_HA_SCSI) {
    194 		ISP_WRITE(isp, BIU_CONF1, 0);
    195 	} else {
    196 		ISP_WRITE(isp, BIU2100_CSR, 0);
    197 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);	/* FM */
    198 	}
    199 
    200 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
    201 	SYS_DELAY(100);
    202 
    203 	if (isp->isp_type & ISP_HA_SCSI) {
    204 		ISP_SETBITS(isp, BIU_CONF1, isp->isp_mdvec->dv_conf1);
    205 		if (isp->isp_mdvec->dv_conf1 & BIU_BURST_ENABLE) {
    206 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
    207 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
    208 		}
    209 	}
    210 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
    211 
    212 	/*
    213 	 * Do MD specific post initialization
    214 	 */
    215 	ISP_RESET1(isp);
    216 
    217 	/*
    218 	 * Enable interrupts
    219 	 */
    220 	ENABLE_INTS(isp);
    221 
    222 	/*
    223 	 * Do some sanity checking.
    224 	 */
    225 	mbs.param[0] = MBOX_NO_OP;
    226 	isp_mboxcmd(isp, &mbs);
    227 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    228 		isp_dumpregs(isp, "NOP test failed");
    229 		ISP_UNLOCK(isp);
    230 		return;
    231 	}
    232 
    233 	if (isp->isp_type & ISP_HA_SCSI) {
    234 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
    235 		mbs.param[1] = 0xdead;
    236 		mbs.param[2] = 0xbeef;
    237 		mbs.param[3] = 0xffff;
    238 		mbs.param[4] = 0x1111;
    239 		mbs.param[5] = 0xa5a5;
    240 		isp_mboxcmd(isp, &mbs);
    241 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    242 			isp_dumpregs(isp,
    243 				"Mailbox Register test didn't complete");
    244 			ISP_UNLOCK(isp);
    245 			return;
    246 		}
    247 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
    248 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
    249 		    mbs.param[5] != 0xa5a5) {
    250 			isp_dumpregs(isp, "Register Test Failed");
    251 			ISP_UNLOCK(isp);
    252 			return;
    253 		}
    254 
    255 	}
    256 
    257 	/*
    258 	 * Download new Firmware, unless requested not to do so.
    259 	 * This is made slightly trickier in some cases where the
    260 	 * firmware of the ROM revision is newer than the revision
    261 	 * compiled into the driver. So, where we used to compare
    262 	 * versions of our f/w and the ROM f/w, now we just see
    263 	 * whether we have f/w at all and whether a config flag
    264 	 * has disabled our download.
    265 	 */
    266 	if ((isp->isp_mdvec->dv_fwlen == 0) ||
    267 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
    268 		dodnld = 0;
    269 	}
    270 
    271 	if (dodnld) {
    272 		for (i = 0; i < isp->isp_mdvec->dv_fwlen; i++) {
    273 			mbs.param[0] = MBOX_WRITE_RAM_WORD;
    274 			mbs.param[1] = isp->isp_mdvec->dv_codeorg + i;
    275 			mbs.param[2] = isp->isp_mdvec->dv_ispfw[i];
    276 			isp_mboxcmd(isp, &mbs);
    277 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    278 				isp_dumpregs(isp, "f/w download failed");
    279 				ISP_UNLOCK(isp);
    280 				return;
    281 			}
    282 		}
    283 
    284 		if (isp->isp_mdvec->dv_fwlen) {
    285 			/*
    286 			 * Verify that it downloaded correctly.
    287 			 */
    288 			mbs.param[0] = MBOX_VERIFY_CHECKSUM;
    289 			mbs.param[1] = isp->isp_mdvec->dv_codeorg;
    290 			isp_mboxcmd(isp, &mbs);
    291 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    292 				isp_dumpregs(isp, "ram checksum failure");
    293 				ISP_UNLOCK(isp);
    294 				return;
    295 			}
    296 		}
    297 	} else {
    298 		IDPRINTF(3, ("%s: skipping f/w download\n", isp->isp_name));
    299 	}
    300 
    301 	/*
    302 	 * Now start it rolling.
    303 	 *
    304 	 * If we didn't actually download f/w,
    305 	 * we still need to (re)start it.
    306 	 */
    307 
    308 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
    309 	mbs.param[1] = isp->isp_mdvec->dv_codeorg;
    310 	isp_mboxcmd(isp, &mbs);
    311 
    312 	if (isp->isp_type & ISP_HA_SCSI) {
    313 		/*
    314 		 * Set CLOCK RATE
    315 		 */
    316 		if (((sdparam *)isp->isp_param)->isp_clock) {
    317 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
    318 			mbs.param[1] = ((sdparam *)isp->isp_param)->isp_clock;
    319 			isp_mboxcmd(isp, &mbs);
    320 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    321 				isp_dumpregs(isp, "failed to set CLOCKRATE");
    322 				ISP_UNLOCK(isp);
    323 				return;
    324 			}
    325 		}
    326 	}
    327 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
    328 	isp_mboxcmd(isp, &mbs);
    329 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    330 		isp_dumpregs(isp, "ABOUT FIRMWARE command failed");
    331 		ISP_UNLOCK(isp);
    332 		return;
    333 	}
    334 	PRINTF("%s: Board Revision %s, %s F/W Revision %d.%d\n",
    335 		isp->isp_name, revname, dodnld? "loaded" : "ROM",
    336 		mbs.param[1], mbs.param[2]);
    337 	isp_fw_state(isp);
    338 	isp->isp_state = ISP_RESETSTATE;
    339 	ISP_UNLOCK(isp);
    340 }
    341 
    342 /*
    343  * Initialize Hardware to known state
    344  */
    345 void
    346 isp_init(isp)
    347 	struct ispsoftc *isp;
    348 {
    349 	sdparam *sdp;
    350 	mbreg_t mbs;
    351 	int i, l;
    352 	ISP_LOCKVAL_DECL;
    353 
    354 	if (isp->isp_type & ISP_HA_FC) {
    355 		isp_fibre_init(isp);
    356 		return;
    357 	}
    358 
    359 	sdp = isp->isp_param;
    360 
    361 	/*
    362 	 * Try and figure out if we're connected to a differential bus.
    363 	 * You have to pause the RISC processor to read SXP registers.
    364 	 *
    365 	 * This, by the way, is likely broken in that it should be
    366 	 * getting this info from NVRAM settings too.
    367 	 */
    368 	ISP_LOCK(isp);
    369 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
    370 	if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_SENSE) {
    371 		sdp->isp_diffmode = 1;
    372 		PRINTF("%s: Differential Mode\n", isp->isp_name);
    373 	} else {
    374 		/*
    375 		 * Force pullups on.
    376 		 */
    377 		sdp->isp_req_ack_active_neg = 1;
    378 		sdp->isp_data_line_active_neg = 1;
    379 		sdp->isp_diffmode = 0;
    380 	}
    381 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
    382 
    383 	mbs.param[0] = MBOX_GET_INIT_SCSI_ID;
    384 	isp_mboxcmd(isp, &mbs);
    385 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    386 		ISP_UNLOCK(isp);
    387 		isp_dumpregs(isp, "failed to get initiator id");
    388 		return;
    389 	}
    390 	if (mbs.param[1] != sdp->isp_initiator_id) {
    391 		PRINTF("%s: setting Initiator ID to %d\n", isp->isp_name,
    392 			sdp->isp_initiator_id);
    393 		mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
    394 		mbs.param[1] = sdp->isp_initiator_id;
    395 		isp_mboxcmd(isp, &mbs);
    396 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    397 			ISP_UNLOCK(isp);
    398 			isp_dumpregs(isp, "failed to set initiator id");
    399 			return;
    400 		}
    401 	} else {
    402 		IDPRINTF(3, ("%s: leaving Initiator ID at %d\n", isp->isp_name,
    403 			sdp->isp_initiator_id));
    404 	}
    405 
    406 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
    407 	mbs.param[1] = sdp->isp_retry_count;
    408 	mbs.param[2] = sdp->isp_retry_delay;
    409 	isp_mboxcmd(isp, &mbs);
    410 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    411 		ISP_UNLOCK(isp);
    412 		isp_dumpregs(isp, "failed to set retry count and delay");
    413 		return;
    414 	}
    415 
    416 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
    417 	mbs.param[1] = sdp->isp_async_data_setup;
    418 	isp_mboxcmd(isp, &mbs);
    419 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    420 		ISP_UNLOCK(isp);
    421 		isp_dumpregs(isp, "failed to set async data setup time");
    422 		return;
    423 	}
    424 
    425 	mbs.param[0] = MBOX_SET_ACTIVE_NEG_STATE;
    426 	mbs.param[1] =	(sdp->isp_req_ack_active_neg << 4) |
    427 			(sdp->isp_data_line_active_neg << 5);
    428 	isp_mboxcmd(isp, &mbs);
    429 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    430 		ISP_UNLOCK(isp);
    431 		isp_dumpregs(isp, "failed to set active neg state");
    432 		return;
    433 	}
    434 
    435 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
    436 	mbs.param[1] = sdp->isp_tag_aging;
    437 	isp_mboxcmd(isp, &mbs);
    438 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    439 		ISP_UNLOCK(isp);
    440 		isp_dumpregs(isp, "failed to set tag age limit");
    441 		return;
    442 	}
    443 
    444 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
    445 	mbs.param[1] = sdp->isp_selection_timeout;
    446 	isp_mboxcmd(isp, &mbs);
    447 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    448 		ISP_UNLOCK(isp);
    449 		isp_dumpregs(isp, "failed to set selection timeout");
    450 		return;
    451 	}
    452 
    453 	IDPRINTF(2, ("%s: devparm, W=wide, S=sync, T=Tag\n", isp->isp_name));
    454 	for (i = 0; i < MAX_TARGETS; i++) {
    455 		char bz[9];
    456 		u_int16_t cj = sdp->isp_devparam[i].sync_period;
    457 
    458 		if (sdp->isp_devparam[i].dev_flags & DPARM_SYNC) {
    459 			u_int16_t x;
    460 			if (cj == (ISP_20M_SYNCPARMS & 0xff)) {
    461 				x = 20;
    462 			} else if (cj == (ISP_10M_SYNCPARMS & 0xff)) {
    463 				x = 10;
    464 			} else if (cj == (ISP_08M_SYNCPARMS & 0xff)) {
    465 				x = 8;
    466 			} else if (cj == (ISP_05M_SYNCPARMS & 0xff)) {
    467 				x = 5;
    468 			} else if (cj == (ISP_04M_SYNCPARMS & 0xff)) {
    469 				x = 4;
    470 			} else {
    471 				x = 0;
    472 			}
    473 			if (x)
    474 				sprintf(bz, "%02dMHz:", x);
    475 			else
    476 				sprintf(bz, "?%04x:", cj);
    477 		} else {
    478 			sprintf(bz, "Async:");
    479 		}
    480 		if (sdp->isp_devparam[i].dev_flags & DPARM_WIDE)
    481 			bz[6] = 'W';
    482 		else
    483 			bz[6] = ' ';
    484 		if (sdp->isp_devparam[i].dev_flags & DPARM_TQING)
    485 			bz[7] = 'T';
    486 		else
    487 			bz[7] = ' ';
    488 		bz[8] = 0;
    489 		IDPRINTF(2, (" id%x:%s", i, bz));
    490 		if (((i+1) & 0x3) == 0)
    491 			IDPRINTF(2, ("\n"));
    492 		if (sdp->isp_devparam[i].dev_enable == 0)
    493 			continue;
    494 
    495 		/*
    496 		 * It is not safe to run the 1020 in ultra mode.
    497 		 */
    498 		if (isp->isp_type == ISP_HA_SCSI_1020 &&
    499 		    cj == (ISP_20M_SYNCPARMS & 0xff)) {
    500 			PRINTF("%s: an ISP1020 set to Ultra Speed- derating.\n",
    501 				isp->isp_name);
    502 			sdp->isp_devparam[i].sync_offset =
    503 				ISP_10M_SYNCPARMS >> 8;
    504 			sdp->isp_devparam[i].sync_period =
    505 				ISP_10M_SYNCPARMS & 0xff;
    506 		}
    507 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
    508 		mbs.param[1] = i << 8;
    509 		mbs.param[2] = sdp->isp_devparam[i].dev_flags << 8;
    510 		mbs.param[3] =
    511 			(sdp->isp_devparam[i].sync_offset << 8) |
    512 			(sdp->isp_devparam[i].sync_period);
    513 
    514 		IDPRINTF(3, ("\n%s: target %d flags %x offset %x period %x\n",
    515 			     isp->isp_name, i, sdp->isp_devparam[i].dev_flags,
    516 			     sdp->isp_devparam[i].sync_offset,
    517 			     sdp->isp_devparam[i].sync_period));
    518 		isp_mboxcmd(isp, &mbs);
    519 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    520 			PRINTF("%s: failed to set parameters for target %d\n",
    521 				isp->isp_name, i);
    522 			PRINTF("%s: flags %x offset %x period %x\n",
    523 				isp->isp_name, sdp->isp_devparam[i].dev_flags,
    524 				sdp->isp_devparam[i].sync_offset,
    525 				sdp->isp_devparam[i].sync_period);
    526 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
    527 			mbs.param[1] = i << 8;
    528 			mbs.param[2] = DPARM_DEFAULT << 8;
    529 			mbs.param[3] = ISP_10M_SYNCPARMS;
    530 			isp_mboxcmd(isp, &mbs);
    531 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    532 				ISP_UNLOCK(isp);
    533 				PRINTF("%s: failed even to set defaults\n",
    534 					isp->isp_name);
    535 				return;
    536 			}
    537 		}
    538 		for (l = 0; l < MAX_LUNS; l++) {
    539 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
    540 			mbs.param[1] = (i << 8) | l;
    541 			mbs.param[2] = sdp->isp_max_queue_depth;
    542 			mbs.param[3] = sdp->isp_devparam[i].exc_throttle;
    543 			isp_mboxcmd(isp, &mbs);
    544 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    545 				ISP_UNLOCK(isp);
    546 				isp_dumpregs(isp, "failed to set device queue "
    547 				       "parameters");
    548 				return;
    549 			}
    550 		}
    551 	}
    552 
    553 	/*
    554 	 * Set up DMA for the request and result mailboxes.
    555 	 */
    556 	if (ISP_MBOXDMASETUP(isp)) {
    557 		ISP_UNLOCK(isp);
    558 		PRINTF("%s: can't setup dma mailboxes\n", isp->isp_name);
    559 		return;
    560 	}
    561 
    562 	mbs.param[0] = MBOX_INIT_RES_QUEUE;
    563 	mbs.param[1] = RESULT_QUEUE_LEN;
    564 	mbs.param[2] = (u_int16_t) (isp->isp_result_dma >> 16);
    565 	mbs.param[3] = (u_int16_t) (isp->isp_result_dma & 0xffff);
    566 	mbs.param[4] = 0;
    567 	mbs.param[5] = 0;
    568 	isp_mboxcmd(isp, &mbs);
    569 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    570 		ISP_UNLOCK(isp);
    571 		isp_dumpregs(isp, "set of response queue failed");
    572 		return;
    573 	}
    574 	isp->isp_residx = 0;
    575 
    576 	mbs.param[0] = MBOX_INIT_REQ_QUEUE;
    577 	mbs.param[1] = RQUEST_QUEUE_LEN;
    578 	mbs.param[2] = (u_int16_t) (isp->isp_rquest_dma >> 16);
    579 	mbs.param[3] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
    580 	mbs.param[4] = 0;
    581 	mbs.param[5] = 0;
    582 	isp_mboxcmd(isp, &mbs);
    583 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    584 		ISP_UNLOCK(isp);
    585 		isp_dumpregs(isp, "set of request queue failed");
    586 		return;
    587 	}
    588 	isp->isp_reqidx = 0;
    589 
    590 	/*
    591 	 * Unfortunately, this is the only way right now for
    592 	 * forcing a sync renegotiation. If we boot off of
    593 	 * an Alpha, it's put the chip in SYNC mode, but we
    594 	 * haven't necessarily set up the parameters the
    595 	 * same, so we'll have to yank the reset line to
    596 	 * get everyone to renegotiate.
    597 	 */
    598 
    599 	mbs.param[0] = MBOX_BUS_RESET;
    600 	mbs.param[1] = 2;
    601 	isp_mboxcmd(isp, &mbs);
    602 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    603 		ISP_UNLOCK(isp);
    604 		isp_dumpregs(isp, "SCSI bus reset failed");
    605 	}
    606 	/*
    607 	 * This is really important to have set after a bus reset.
    608 	 */
    609 	isp->isp_sendmarker = 1;
    610 	ISP_UNLOCK(isp);
    611 	isp->isp_state = ISP_INITSTATE;
    612 }
    613 
    614 static void
    615 isp_fibre_init(isp)
    616 	struct ispsoftc *isp;
    617 {
    618 	fcparam *fcp;
    619 	isp_icb_t *icbp;
    620 	mbreg_t mbs;
    621 	int count;
    622 	u_int8_t lwfs;
    623 	ISP_LOCKVAL_DECL;
    624 
    625 	fcp = isp->isp_param;
    626 
    627 	fcp->isp_retry_count = 0;
    628 	fcp->isp_retry_delay = 1;
    629 
    630 	ISP_LOCK(isp);
    631 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
    632 	mbs.param[1] = fcp->isp_retry_count;
    633 	mbs.param[2] = fcp->isp_retry_delay;
    634 	isp_mboxcmd(isp, &mbs);
    635 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    636 		ISP_UNLOCK(isp);
    637 		isp_dumpregs(isp, "failed to set retry count and delay");
    638 		return;
    639 	}
    640 
    641 	if (ISP_MBOXDMASETUP(isp)) {
    642 		ISP_UNLOCK(isp);
    643 		PRINTF("%s: can't setup DMA for mailboxes\n", isp->isp_name);
    644 		return;
    645 	}
    646 
    647 	icbp = (isp_icb_t *) fcp->isp_scratch;
    648 	bzero(icbp, sizeof (*icbp));
    649 #if 0
    650 	icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
    651 	MAKE_NODE_NAME(isp, icbp);
    652 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN;
    653 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN;
    654 	icbp->icb_rqstaddr[0] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
    655 	icbp->icb_rqstaddr[1] = (u_int16_t) (isp->isp_rquest_dma >> 16);
    656 	icbp->icb_respaddr[0] = (u_int16_t) (isp->isp_result_dma & 0xffff);
    657 	icbp->icb_respaddr[1] = (u_int16_t) (isp->isp_result_dma >> 16);
    658 #endif
    659 	icbp->icb_version = 1;
    660 	icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
    661 	icbp->icb_maxalloc = 256;
    662 	icbp->icb_execthrottle = 16;
    663 	icbp->icb_retry_delay = 5;
    664 	icbp->icb_retry_count = 0;
    665 	MAKE_NODE_NAME(isp, icbp);
    666 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN;
    667 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN;
    668 	icbp->icb_rqstaddr[0] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
    669 	icbp->icb_rqstaddr[1] = (u_int16_t) (isp->isp_rquest_dma >> 16);
    670 	icbp->icb_respaddr[0] = (u_int16_t) (isp->isp_result_dma & 0xffff);
    671 	icbp->icb_respaddr[1] = (u_int16_t) (isp->isp_result_dma >> 16);
    672 
    673 	mbs.param[0] = MBOX_INIT_FIRMWARE;
    674 	mbs.param[1] = 0;
    675 	mbs.param[2] = (u_int16_t) (fcp->isp_scdma >> 16);
    676 	mbs.param[3] = (u_int16_t) (fcp->isp_scdma & 0xffff);
    677 	mbs.param[4] = 0;
    678 	mbs.param[5] = 0;
    679 	mbs.param[6] = 0;
    680 	mbs.param[7] = 0;
    681 	isp_mboxcmd(isp, &mbs);
    682 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    683 		ISP_UNLOCK(isp);
    684 		isp_dumpregs(isp, "INIT FIRMWARE failed");
    685 		return;
    686 	}
    687 	isp->isp_reqidx = 0;
    688 	isp->isp_residx = 0;
    689 
    690 	/*
    691 	 * Wait up to 12 seconds for FW to go to READY state.
    692 	 * This used to be 3 seconds, but that lost.
    693 	 *
    694 	 * This is all very much not right. The problem here
    695 	 * is that the cable may not be plugged in, or there
    696 	 * may be many many members of the loop that haven't
    697 	 * been logged into.
    698 	 *
    699 	 * This model of doing things doesn't support dynamic
    700 	 * attachment, so we just plain lose (for now).
    701 	 */
    702 	lwfs = FW_CONFIG_WAIT;
    703 	for (count = 0; count < 12000; count++) {
    704 		isp_fw_state(isp);
    705 		if (lwfs != fcp->isp_fwstate) {
    706 			PRINTF("%s: Firmware State %s -> %s\n", isp->isp_name,
    707 			    fw_statename(lwfs), fw_statename(fcp->isp_fwstate));
    708 			lwfs = fcp->isp_fwstate;
    709 		}
    710 		if (fcp->isp_fwstate == FW_READY) {
    711 			break;
    712 		}
    713 		SYS_DELAY(1000);	/* wait one millisecond */
    714 	}
    715 	isp->isp_sendmarker = 1;
    716 
    717 	/*
    718 	 * Get our Loop ID
    719 	 * (if possible)
    720 	 */
    721 	if (fcp->isp_fwstate == FW_READY) {
    722 		mbs.param[0] = MBOX_GET_LOOP_ID;
    723 		isp_mboxcmd(isp, &mbs);
    724 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    725 			isp_dumpregs(isp, "GET LOOP ID failed");
    726 			ISP_UNLOCK(isp);
    727 			return;
    728 		}
    729 		fcp->isp_loopid = mbs.param[1];
    730 		if (fcp->isp_loopid) {
    731 			PRINTF("%s: Loop ID 0x%x\n", isp->isp_name,
    732 				fcp->isp_loopid);
    733 		}
    734 		isp->isp_state = ISP_INITSTATE;
    735 	} else {
    736 		PRINTF("%s: failed to go to FW READY state- will not attach\n",
    737 		    isp->isp_name);
    738 	}
    739 	ISP_UNLOCK(isp);
    740 }
    741 
    742 /*
    743  * Free any associated resources prior to decommissioning and
    744  * set the card to a known state (so it doesn't wake up and kick
    745  * us when we aren't expecting it to).
    746  *
    747  * Locks are held before coming here.
    748  */
    749 void
    750 isp_uninit(isp)
    751 	struct ispsoftc *isp;
    752 {
    753 	/*
    754 	 * Leave with interrupts disabled.
    755 	 */
    756 	DISABLE_INTS(isp);
    757 
    758 	/*
    759 	 * Stop the watchdog timer (if started).
    760 	 */
    761 	STOP_WATCHDOG(isp_watch, isp);
    762 }
    763 
    764 
    765 /*
    766  * Start a command. Locking is assumed done in the caller.
    767  */
    768 
    769 int32_t
    770 ispscsicmd(xs)
    771 	ISP_SCSI_XFER_T *xs;
    772 {
    773 	struct ispsoftc *isp;
    774 	u_int8_t iptr, optr;
    775 	union {
    776 		ispreq_t *_reqp;
    777 		ispreqt2_t *_t2reqp;
    778 	} _u;
    779 #define	reqp	_u._reqp
    780 #define	t2reqp	_u._t2reqp
    781 #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
    782 	int i;
    783 
    784 	XS_INITERR(xs);
    785 	isp = XS_ISP(xs);
    786 
    787 	if (isp->isp_type & ISP_HA_FC) {
    788 		if (XS_CDBLEN(xs) > 12) {
    789 			PRINTF("%s: unsupported cdb length for fibre (%d)\n",
    790 				isp->isp_name, XS_CDBLEN(xs));
    791 			XS_SETERR(xs, HBA_BOTCH);
    792 			return (CMD_COMPLETE);
    793 		}
    794 	}
    795 	optr = ISP_READ(isp, OUTMAILBOX4);
    796 	iptr = isp->isp_reqidx;
    797 
    798 	reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
    799 	iptr = (iptr + 1) & (RQUEST_QUEUE_LEN - 1);
    800 	if (iptr == optr) {
    801 		PRINTF("%s: Request Queue Overflow\n", isp->isp_name);
    802 		XS_SETERR(xs, HBA_BOTCH);
    803 		return (CMD_EAGAIN);
    804 	}
    805 
    806 	if (isp->isp_type & ISP_HA_FC) {
    807 		DISABLE_INTS(isp);
    808 	}
    809 
    810 	if (isp->isp_sendmarker) {
    811 		ispmarkreq_t *marker = (ispmarkreq_t *) reqp;
    812 
    813 		bzero((void *) marker, sizeof (*marker));
    814 		marker->req_header.rqs_entry_count = 1;
    815 		marker->req_header.rqs_entry_type = RQSTYPE_MARKER;
    816 		marker->req_modifier = SYNC_ALL;
    817 
    818 		isp->isp_sendmarker = 0;
    819 
    820 		if (((iptr + 1) & (RQUEST_QUEUE_LEN - 1)) == optr) {
    821 			ISP_WRITE(isp, INMAILBOX4, iptr);
    822 			isp->isp_reqidx = iptr;
    823 
    824 			if (isp->isp_type & ISP_HA_FC) {
    825 				ENABLE_INTS(isp);
    826 			}
    827 			PRINTF("%s: Request Queue Overflow+\n", isp->isp_name);
    828 			XS_SETERR(xs, HBA_BOTCH);
    829 			return (CMD_EAGAIN);
    830 		}
    831 		reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
    832 		iptr = (iptr + 1) & (RQUEST_QUEUE_LEN - 1);
    833 	}
    834 
    835 	bzero((void *) reqp, UZSIZE);
    836 	reqp->req_header.rqs_entry_count = 1;
    837 	if (isp->isp_type & ISP_HA_FC) {
    838 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
    839 	} else {
    840 		reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
    841 	}
    842 	reqp->req_header.rqs_flags = 0;
    843 	reqp->req_header.rqs_seqno = isp->isp_seqno++;
    844 
    845 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
    846 		if (isp->isp_xflist[i] == NULL)
    847 			break;
    848 	}
    849 	if (i == RQUEST_QUEUE_LEN) {
    850 		if (isp->isp_type & ISP_HA_FC)
    851 			ENABLE_INTS(isp);
    852 		PRINTF("%s: ran out of xflist pointers?????\n", isp->isp_name);
    853 		XS_SETERR(xs, HBA_BOTCH);
    854 		return (CMD_EAGAIN);
    855 	} else {
    856 		/*
    857 		 * Never have a handle that is zero, so
    858 		 * set req_handle off by one.
    859 		 */
    860 		isp->isp_xflist[i] = xs;
    861 		reqp->req_handle = i+1;
    862 	}
    863 
    864 	if (isp->isp_type & ISP_HA_FC) {
    865 		/*
    866 		 * See comment in isp_intr
    867 		 */
    868 		XS_RESID(xs) = 0;
    869 		/*
    870 		 * Fibre Channel always requires some kind of tag.
    871 		 * If we're marked as "Can't Tag", just do simple
    872 		 * instead of ordered tags. It's pretty clear to me
    873 		 * that we shouldn't do head of queue tagging in
    874 		 * this case.
    875 		 */
    876 		if (XS_CANTAG(xs)) {
    877 			t2reqp->req_flags = XS_KINDOF_TAG(xs);
    878 		} else {
    879  			t2reqp->req_flags = REQFLAG_STAG;
    880 		}
    881 	} else {
    882 		sdparam *sdp = (sdparam *)isp->isp_param;
    883 		if ((sdp->isp_devparam[XS_TGT(xs)].dev_flags & DPARM_TQING) &&
    884 		    XS_CANTAG(xs)) {
    885 			reqp->req_flags = XS_KINDOF_TAG(xs);
    886 		} else {
    887 			reqp->req_flags = 0;
    888 		}
    889 	}
    890 	reqp->req_lun_trn = XS_LUN(xs);
    891 	reqp->req_target = XS_TGT(xs);
    892 	if (isp->isp_type & ISP_HA_SCSI) {
    893 		reqp->req_cdblen = XS_CDBLEN(xs);
    894 	}
    895 	bcopy((void *)XS_CDBP(xs), reqp->req_cdb, XS_CDBLEN(xs));
    896 
    897 	IDPRINTF(5, ("%s(%d.%d): START%d cmd 0x%x datalen %d\n", isp->isp_name,
    898 	    XS_TGT(xs), XS_LUN(xs), reqp->req_header.rqs_seqno,
    899 	    reqp->req_cdb[0], XS_XFRLEN(xs)));
    900 
    901 	reqp->req_time = XS_TIME(xs) / 1000;
    902 	if (reqp->req_time == 0 && XS_TIME(xs))
    903 		reqp->req_time = 1;
    904 	if (ISP_DMASETUP(isp, xs, reqp, &iptr, optr)) {
    905 		if (isp->isp_type & ISP_HA_FC)
    906 			ENABLE_INTS(isp);
    907 		/* dmasetup sets actual error */
    908 		return (CMD_COMPLETE);
    909 	}
    910 	XS_SETERR(xs, HBA_NOERROR);
    911 	ISP_WRITE(isp, INMAILBOX4, iptr);
    912 	isp->isp_reqidx = iptr;
    913 	if (isp->isp_type & ISP_HA_FC) {
    914 		ENABLE_INTS(isp);
    915 	}
    916 	isp->isp_nactive++;
    917 	return (CMD_QUEUED);
    918 #undef	reqp
    919 #undef	t2reqp
    920 }
    921 
    922 /*
    923  * isp control
    924  * Locks (ints blocked) assumed held.
    925  */
    926 
    927 int
    928 isp_control(isp, ctl, arg)
    929 	struct ispsoftc *isp;
    930 	ispctl_t ctl;
    931 	void *arg;
    932 {
    933 	ISP_SCSI_XFER_T *xs;
    934 	mbreg_t mbs;
    935 	int i;
    936 
    937 	switch (ctl) {
    938 	default:
    939 		PRINTF("%s: isp_control unknown control op %x\n",
    940 		    isp->isp_name, ctl);
    941 		break;
    942 
    943 	case ISPCTL_RESET_BUS:
    944 		/*
    945 		 * Right now, for Fibre, we'll punt on loop reset.
    946 		 * The reason is that it takes a really long time
    947 		 * to go through the renegotiation after a LIP,
    948 		 * and we really have to hang out until it's done
    949 		 * to see what's there after a LIP- until the
    950 		 * LIP is done and the loop comes back up,
    951 		 * commands just fail (and, yes, we could handle
    952 		 * that a little better).
    953 		 */
    954 		if (isp->isp_type & ISP_HA_FC) {
    955 			PRINTF("%s: Skipping FC resets\n", isp->isp_name);
    956 			return (0);
    957 		}
    958 		mbs.param[0] = MBOX_BUS_RESET;
    959 		mbs.param[1] = 2;	/* 'delay', in seconds */
    960 		isp_mboxcmd(isp, &mbs);
    961 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    962 			isp_dumpregs(isp, "isp_control SCSI bus reset failed");
    963 			break;
    964 		}
    965 		/*
    966 		 * This is really important to have set after a bus reset.
    967 		 */
    968 		isp->isp_sendmarker = 1;
    969 		PRINTF("%s: Bus Reset\n", isp->isp_name);
    970 		return (0);
    971 
    972         case ISPCTL_RESET_DEV:
    973 		/*
    974 		 * Note that under parallel SCSI, this issues a BDR message.
    975 		 * Under FC, we could probably be using ABORT TASK SET
    976 		 * command.
    977 		 */
    978 
    979 		mbs.param[0] = MBOX_ABORT_TARGET;
    980 		mbs.param[1] = ((long)arg) << 8;
    981 		mbs.param[2] = 2;	/* 'delay', in seconds */
    982 		isp_mboxcmd(isp, &mbs);
    983 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    984 			isp_dumpregs(isp, "SCSI Target  reset failed");
    985 			break;
    986 		}
    987 		PRINTF("%s: Target %d Reset Succeeded\n", isp->isp_name,
    988 		    (int) ((long) arg));
    989 		isp->isp_sendmarker = 1;
    990 		return (0);
    991 
    992         case ISPCTL_ABORT_CMD:
    993 		xs = (ISP_SCSI_XFER_T *) arg;
    994 		for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
    995 			if (xs == isp->isp_xflist[i]) {
    996 				break;
    997 			}
    998 		}
    999 		if (i == RQUEST_QUEUE_LEN) {
   1000 			PRINTF("%s: isp_control- cannot find command to abort "
   1001 			    "in active list\n", isp->isp_name);
   1002 			break;
   1003 		}
   1004 		mbs.param[0] = MBOX_ABORT;
   1005 		mbs.param[1] = XS_TGT(xs) | XS_LUN(xs);
   1006 		mbs.param[2] = (i+1) >> 16;
   1007 		mbs.param[3] = (i+1) & 0xffff;
   1008 		isp_mboxcmd(isp, &mbs);
   1009 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1010 			PRINTF("%s: isp_control MBOX_ABORT failure (code %x)\n",
   1011 			    isp->isp_name, mbs.param[0]);
   1012 			break;
   1013 		}
   1014 		PRINTF("%s: command for target %d lun %d was aborted\n",
   1015 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1016 		return (0);
   1017 	}
   1018 	return (-1);
   1019 }
   1020 
   1021 /*
   1022  * Interrupt Service Routine(s).
   1023  *
   1024  * External (OS) framework has done the appropriate locking,
   1025  * and the locking will be held throughout this function.
   1026  */
   1027 
   1028 int
   1029 isp_intr(arg)
   1030 	void *arg;
   1031 {
   1032 	ISP_SCSI_XFER_T *clist[RQUEST_QUEUE_LEN], *xs;
   1033 	struct ispsoftc *isp = arg;
   1034 	u_int16_t iptr, optr, isr;
   1035 	int i, ndone = 0;
   1036 
   1037 	isr = ISP_READ(isp, BIU_ISR);
   1038 	if (isp->isp_type & ISP_HA_FC) {
   1039 		if (isr == 0 || (isr & BIU2100_ISR_RISC_INT) == 0) {
   1040 			if (isr) {
   1041 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
   1042 					     isp->isp_name, isr));
   1043 			}
   1044 			return (0);
   1045 		}
   1046 	} else {
   1047 		if (isr == 0 || (isr & BIU_ISR_RISC_INT) == 0) {
   1048 			if (isr) {
   1049 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
   1050 					     isp->isp_name, isr));
   1051 			}
   1052 			return (0);
   1053 		}
   1054 	}
   1055 
   1056 	optr = isp->isp_residx;
   1057 	if (ISP_READ(isp, BIU_SEMA) & 1) {
   1058 		u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
   1059 		switch (mbox) {
   1060 		case ASYNC_BUS_RESET:
   1061 			PRINTF("%s: SCSI bus reset detected\n", isp->isp_name);
   1062 			isp->isp_sendmarker = 1;
   1063 			break;
   1064 		case ASYNC_SYSTEM_ERROR:
   1065 			mbox = ISP_READ(isp, OUTMAILBOX1);
   1066 			PRINTF("%s: Internal System Error RISC Addr %x\n",
   1067 			    isp->isp_name, mbox);
   1068 			break;
   1069 		case ASYNC_RQS_XFER_ERR:
   1070 			PRINTF("%s: Request Queue Transfer Error\n",
   1071 			    isp->isp_name);
   1072 			break;
   1073 		case ASYNC_RSP_XFER_ERR:
   1074 			PRINTF("%s: Response Queue Transfer Error\n",
   1075 			    isp->isp_name);
   1076 			break;
   1077 		case ASYNC_QWAKEUP:
   1078 			/* don't need to be chatty */
   1079 			mbox = ISP_READ(isp, OUTMAILBOX4);
   1080 			break;
   1081 		case ASYNC_TIMEOUT_RESET:
   1082 			PRINTF("%s: timeout initiated SCSI bus reset\n",
   1083 			    isp->isp_name);
   1084 			isp->isp_sendmarker = 1;
   1085 			break;
   1086 		case ASYNC_LIP_OCCURRED:
   1087 			PRINTF("%s: LIP occurred\n", isp->isp_name);
   1088 			break;
   1089 		case ASYNC_LOOP_UP:
   1090 			PRINTF("%s: Loop UP\n", isp->isp_name);
   1091 			break;
   1092 		case ASYNC_LOOP_DOWN:
   1093 			PRINTF("%s: Loop DOWN\n", isp->isp_name);
   1094 			break;
   1095 		case ASYNC_LOOP_RESET:
   1096 			PRINTF("%s: Loop RESET\n", isp->isp_name);
   1097 			break;
   1098 		default:
   1099 			PRINTF("%s: async %x\n", isp->isp_name, mbox);
   1100 			break;
   1101 		}
   1102 		ISP_WRITE(isp, BIU_SEMA, 0);
   1103 	}
   1104 
   1105 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   1106 	iptr = ISP_READ(isp, OUTMAILBOX5);
   1107 	if (optr == iptr) {
   1108 		IDPRINTF(4, ("why intr? isr %x iptr %x optr %x\n",
   1109 			isr, optr, iptr));
   1110 	}
   1111 	ENABLE_INTS(isp);
   1112 
   1113 	while (optr != iptr) {
   1114 		ispstatusreq_t *sp;
   1115 		int buddaboom = 0;
   1116 
   1117 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   1118 
   1119 		optr = (optr + 1) & (RESULT_QUEUE_LEN - 1);
   1120 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
   1121 			PRINTF("%s: not RESPONSE in RESPONSE Queue (0x%x)\n",
   1122 				isp->isp_name, sp->req_header.rqs_entry_type);
   1123 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
   1124 				ISP_WRITE(isp, INMAILBOX5, optr);
   1125 				continue;
   1126 			}
   1127 			buddaboom = 1;
   1128 		}
   1129 
   1130 		if (sp->req_header.rqs_flags & 0xf) {
   1131 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   1132 				ISP_WRITE(isp, INMAILBOX5, optr);
   1133 				continue;
   1134 			}
   1135 			PRINTF("%s: rqs_flags=%x", isp->isp_name,
   1136 				sp->req_header.rqs_flags & 0xf);
   1137 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   1138 				PRINTF("%s: internal queues full\n",
   1139 				    isp->isp_name);
   1140 				/* XXXX: this command *could* get restarted */
   1141 				buddaboom++;
   1142 			}
   1143 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   1144 				PRINTF("%s: bad header\n", isp->isp_name);
   1145 				buddaboom++;
   1146 			}
   1147 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   1148 				PRINTF("%s: bad request packet\n",
   1149 				    isp->isp_name);
   1150 				buddaboom++;
   1151 			}
   1152 		}
   1153 		if (sp->req_handle > RQUEST_QUEUE_LEN || sp->req_handle < 1) {
   1154 			PRINTF("%s: bad request handle %d\n", isp->isp_name,
   1155 				sp->req_handle);
   1156 			ISP_WRITE(isp, INMAILBOX5, optr);
   1157 			continue;
   1158 		}
   1159 		xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[sp->req_handle - 1];
   1160 		if (xs == NULL) {
   1161 			PRINTF("%s: NULL xs in xflist (handle %x)\n",
   1162 			    isp->isp_name, sp->req_handle);
   1163 			isp_dumpxflist(isp);
   1164 			ISP_WRITE(isp, INMAILBOX5, optr);
   1165 			continue;
   1166 		}
   1167 		isp->isp_xflist[sp->req_handle - 1] = NULL;
   1168 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   1169 			isp->isp_sendmarker = 1;
   1170 		}
   1171 		if (buddaboom) {
   1172 			XS_SETERR(xs, HBA_BOTCH);
   1173 		}
   1174 		XS_STS(xs) = sp->req_scsi_status & 0xff;
   1175 		if (isp->isp_type & ISP_HA_SCSI) {
   1176 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   1177 				bcopy(sp->req_sense_data, XS_SNSP(xs),
   1178 					XS_SNSLEN(xs));
   1179 				XS_SNS_IS_VALID(xs);
   1180 			}
   1181 		} else {
   1182 			if (XS_STS(xs) == SCSI_CHECK) {
   1183 				XS_SNS_IS_VALID(xs);
   1184 				bcopy(sp->req_sense_data, XS_SNSP(xs),
   1185 					XS_SNSLEN(xs));
   1186 				sp->req_state_flags |= RQSF_GOT_SENSE;
   1187 			}
   1188 		}
   1189 		if (XS_NOERR(xs) && XS_STS(xs) == SCSI_BUSY) {
   1190 			XS_SETERR(xs, HBA_TGTBSY);
   1191 		}
   1192 
   1193 		if (sp->req_header.rqs_entry_type == RQSTYPE_RESPONSE) {
   1194 			if (XS_NOERR(xs) && sp->req_completion_status)
   1195 				isp_parse_status(isp, sp, xs);
   1196 		} else {
   1197 			PRINTF("%s: unknown return %x\n", isp->isp_name,
   1198 				sp->req_header.rqs_entry_type);
   1199 			if (XS_NOERR(xs))
   1200 				XS_SETERR(xs, HBA_BOTCH);
   1201 		}
   1202 		if (isp->isp_type & ISP_HA_SCSI) {
   1203 			XS_RESID(xs) = sp->req_resid;
   1204 		} else if (sp->req_scsi_status & RQCS_RU) {
   1205 			XS_RESID(xs) = sp->req_resid;
   1206 			IDPRINTF(4, ("%s: cnt %d rsd %d\n", isp->isp_name,
   1207 				XS_XFRLEN(xs), sp->req_resid));
   1208 		}
   1209 		if (XS_XFRLEN(xs)) {
   1210 			ISP_DMAFREE(isp, xs, sp->req_handle - 1);
   1211 		}
   1212 		/*
   1213 		 * XXX: If we have a check condition, but no Sense Data,
   1214 		 * XXX: mark it as an error (ARQ failed). We need to
   1215 		 * XXX: to do a more distinct job because there may
   1216 		 * XXX: cases where ARQ is disabled.
   1217 		 */
   1218 		if (XS_STS(xs) == SCSI_CHECK && !(XS_IS_SNS_VALID(xs))) {
   1219 			if (XS_NOERR(xs)) {
   1220 				PRINTF("%s: ARQ Failure\n", isp->isp_name);
   1221 				XS_SETERR(xs, HBA_ARQFAIL);
   1222 			}
   1223 		}
   1224 		if ((isp->isp_dblev >= 5) ||
   1225 		    (isp->isp_dblev > 2 && !XS_NOERR(xs))) {
   1226 			PRINTF("%s(%d.%d): FIN%d dl%d resid%d STS %x",
   1227 			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
   1228 			    sp->req_header.rqs_seqno, XS_XFRLEN(xs),
   1229 			    XS_RESID(xs), XS_STS(xs));
   1230 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   1231 				PRINTF(" Skey: %x", XS_SNSKEY(xs));
   1232 				if (!(XS_IS_SNS_VALID(xs))) {
   1233 					PRINTF(" BUT NOT SET");
   1234 				}
   1235 			}
   1236 			PRINTF(" XS_ERR=0x%x\n", XS_ERR(xs));
   1237 		}
   1238 
   1239 		ISP_WRITE(isp, INMAILBOX5, optr);
   1240 		isp->isp_nactive--;
   1241 		if (isp->isp_nactive < 0)
   1242 			isp->isp_nactive = 0;
   1243 		clist[ndone++] = xs;	/* defer completion call until later */
   1244 	}
   1245 	isp->isp_residx = optr;
   1246 	for (i = 0; i < ndone; i++)
   1247 		XS_CMD_DONE(clist[i]);
   1248 	return (1);
   1249 }
   1250 
   1251 /*
   1252  * Support routines.
   1253  */
   1254 
   1255 static void
   1256 isp_parse_status(isp, sp, xs)
   1257 	struct ispsoftc *isp;
   1258 	ispstatusreq_t *sp;
   1259 	ISP_SCSI_XFER_T *xs;
   1260 {
   1261 	switch (sp->req_completion_status) {
   1262 	case RQCS_COMPLETE:
   1263 		XS_SETERR(xs, HBA_NOERROR);
   1264 		return;
   1265 
   1266 	case RQCS_INCOMPLETE:
   1267 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   1268 			IDPRINTF(2, ("Selection Timeout\n"));
   1269 			XS_SETERR(xs, HBA_SELTIMEOUT);
   1270 			return;
   1271 		}
   1272 		PRINTF("%s: incomplete, state %x\n",
   1273 			isp->isp_name, sp->req_state_flags);
   1274 		break;
   1275 
   1276 	case RQCS_TRANSPORT_ERROR:
   1277 		PRINTF("%s: transport error\n", isp->isp_name);
   1278 		isp_prtstst(sp);
   1279 		break;
   1280 
   1281 	case RQCS_DATA_OVERRUN:
   1282 		if (isp->isp_type & ISP_HA_FC) {
   1283 			XS_RESID(xs) = sp->req_resid;
   1284 			break;
   1285 		}
   1286 		XS_SETERR(xs, HBA_DATAOVR);
   1287 		return;
   1288 
   1289 	case RQCS_DATA_UNDERRUN:
   1290 		if (isp->isp_type & ISP_HA_FC) {
   1291 			XS_RESID(xs) = sp->req_resid;
   1292 			/* an UNDERRUN is not a botch ??? */
   1293 		}
   1294 		XS_SETERR(xs, HBA_NOERROR);
   1295 		return;
   1296 
   1297 	case RQCS_TIMEOUT:
   1298 		XS_SETERR(xs, HBA_CMDTIMEOUT);
   1299 		return;
   1300 
   1301 	case RQCS_RESET_OCCURRED:
   1302 		PRINTF("%s: reset occurred, %d active\n", isp->isp_name,
   1303 			isp->isp_nactive);
   1304 		isp->isp_sendmarker = 1;
   1305 		XS_SETERR(xs, HBA_BUSRESET);
   1306 		return;
   1307 
   1308 	case RQCS_ABORTED:
   1309 		PRINTF("%s: command aborted\n", isp->isp_name);
   1310 		isp->isp_sendmarker = 1;
   1311 		XS_SETERR(xs, HBA_ABORTED);
   1312 		return;
   1313 
   1314 	case RQCS_PORT_UNAVAILABLE:
   1315 		/*
   1316 		 * No such port on the loop. Moral equivalent of SELTIMEO
   1317 		 */
   1318 		XS_SETERR(xs, HBA_SELTIMEOUT);
   1319 		return;
   1320 
   1321 	case RQCS_PORT_LOGGED_OUT:
   1322 		PRINTF("%s: port logout for target %d\n",
   1323 			isp->isp_name, XS_TGT(xs));
   1324 		break;
   1325 
   1326 	case RQCS_PORT_CHANGED:
   1327 		PRINTF("%s: port changed for target %d\n",
   1328 			isp->isp_name, XS_TGT(xs));
   1329 		break;
   1330 
   1331 	case RQCS_PORT_BUSY:
   1332 		PRINTF("%s: port busy for target %d\n",
   1333 			isp->isp_name, XS_TGT(xs));
   1334 		XS_SETERR(xs, HBA_TGTBSY);
   1335 		return;
   1336 
   1337 	default:
   1338 		PRINTF("%s: comp status %x\n", isp->isp_name,
   1339 		       sp->req_completion_status);
   1340 		break;
   1341 	}
   1342 	XS_SETERR(xs, HBA_BOTCH);
   1343 }
   1344 
   1345 #define	HINIB(x)			((x) >> 0x4)
   1346 #define	LONIB(x)			((x)  & 0xf)
   1347 #define MAKNIB(a, b)			(((a) << 4) | (b))
   1348 static u_int8_t mbpcnt[] = {
   1349 	MAKNIB(1, 1),	/* 0x00: MBOX_NO_OP */
   1350 	MAKNIB(5, 5),	/* 0x01: MBOX_LOAD_RAM */
   1351 	MAKNIB(2, 0),	/* 0x02: MBOX_EXEC_FIRMWARE */
   1352 	MAKNIB(5, 5),	/* 0x03: MBOX_DUMP_RAM */
   1353 	MAKNIB(3, 3),	/* 0x04: MBOX_WRITE_RAM_WORD */
   1354 	MAKNIB(2, 3),	/* 0x05: MBOX_READ_RAM_WORD */
   1355 	MAKNIB(6, 6),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   1356 	MAKNIB(2, 3),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   1357 	MAKNIB(1, 3),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   1358 	MAKNIB(0, 0),	/* 0x09: */
   1359 	MAKNIB(0, 0),	/* 0x0a: */
   1360 	MAKNIB(0, 0),	/* 0x0b: */
   1361 	MAKNIB(0, 0),	/* 0x0c: */
   1362 	MAKNIB(0, 0),	/* 0x0d: */
   1363 	MAKNIB(1, 2),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   1364 	MAKNIB(0, 0),	/* 0x0f: */
   1365 	MAKNIB(5, 5),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   1366 	MAKNIB(6, 6),	/* 0x11: MBOX_INIT_RES_QUEUE */
   1367 	MAKNIB(4, 4),	/* 0x12: MBOX_EXECUTE_IOCB */
   1368 	MAKNIB(2, 2),	/* 0x13: MBOX_WAKE_UP	*/
   1369 	MAKNIB(1, 6),	/* 0x14: MBOX_STOP_FIRMWARE */
   1370 	MAKNIB(4, 4),	/* 0x15: MBOX_ABORT */
   1371 	MAKNIB(2, 2),	/* 0x16: MBOX_ABORT_DEVICE */
   1372 	MAKNIB(3, 3),	/* 0x17: MBOX_ABORT_TARGET */
   1373 	MAKNIB(2, 2),	/* 0x18: MBOX_BUS_RESET */
   1374 	MAKNIB(2, 3),	/* 0x19: MBOX_STOP_QUEUE */
   1375 	MAKNIB(2, 3),	/* 0x1a: MBOX_START_QUEUE */
   1376 	MAKNIB(2, 3),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   1377 	MAKNIB(2, 3),	/* 0x1c: MBOX_ABORT_QUEUE */
   1378 	MAKNIB(2, 4),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   1379 	MAKNIB(0, 0),	/* 0x1e: */
   1380 	MAKNIB(1, 3),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   1381 	MAKNIB(1, 2),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
   1382 	MAKNIB(1, 2),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   1383 	MAKNIB(1, 3),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   1384 	MAKNIB(1, 2),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   1385 	MAKNIB(1, 2),	/* 0x24: MBOX_GET_CLOCK_RATE */
   1386 	MAKNIB(1, 2),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   1387 	MAKNIB(1, 2),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   1388 	MAKNIB(1, 3),	/* 0x27: MBOX_GET_PCI_PARAMS */
   1389 	MAKNIB(2, 4),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   1390 	MAKNIB(2, 4),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   1391 	MAKNIB(0, 0),	/* 0x2a: */
   1392 	MAKNIB(0, 0),	/* 0x2b: */
   1393 	MAKNIB(0, 0),	/* 0x2c: */
   1394 	MAKNIB(0, 0),	/* 0x2d: */
   1395 	MAKNIB(0, 0),	/* 0x2e: */
   1396 	MAKNIB(0, 0),	/* 0x2f: */
   1397 	MAKNIB(2, 2),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   1398 	MAKNIB(2, 2),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   1399 	MAKNIB(3, 3),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   1400 	MAKNIB(2, 2),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   1401 	MAKNIB(2, 2),	/* 0x34: MBOX_SET_CLOCK_RATE */
   1402 	MAKNIB(2, 2),	/* 0x35: MBOX_SET_ACTIVE_NEG_STATE */
   1403 	MAKNIB(2, 2),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   1404 	MAKNIB(3, 3),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   1405 	MAKNIB(4, 4),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   1406 	MAKNIB(4, 4),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   1407 	MAKNIB(0, 0),	/* 0x3a: */
   1408 	MAKNIB(0, 0),	/* 0x3b: */
   1409 	MAKNIB(0, 0),	/* 0x3c: */
   1410 	MAKNIB(0, 0),	/* 0x3d: */
   1411 	MAKNIB(0, 0),	/* 0x3e: */
   1412 	MAKNIB(0, 0),	/* 0x3f: */
   1413 	MAKNIB(1, 2),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   1414 	MAKNIB(6, 1),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   1415 	MAKNIB(2, 3),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   1416 	MAKNIB(0, 0),	/* 0x43: */
   1417 	MAKNIB(0, 0),	/* 0x44: */
   1418 	MAKNIB(0, 0),	/* 0x45: */
   1419 	MAKNIB(0, 0),	/* 0x46: */
   1420 	MAKNIB(0, 0),	/* 0x47: */
   1421 	MAKNIB(0, 0),	/* 0x48: */
   1422 	MAKNIB(0, 0),	/* 0x49: */
   1423 	MAKNIB(0, 0),	/* 0x4a: */
   1424 	MAKNIB(0, 0),	/* 0x4b: */
   1425 	MAKNIB(0, 0),	/* 0x4c: */
   1426 	MAKNIB(0, 0),	/* 0x4d: */
   1427 	MAKNIB(0, 0),	/* 0x4e: */
   1428 	MAKNIB(0, 0),	/* 0x4f: */
   1429 	MAKNIB(0, 0),	/* 0x50: */
   1430 	MAKNIB(0, 0),	/* 0x51: */
   1431 	MAKNIB(0, 0),	/* 0x52: */
   1432 	MAKNIB(0, 0),	/* 0x53: */
   1433 	MAKNIB(8, 0),	/* 0x54: MBOX_EXEC_COMMAND_IOCB_A64 */
   1434 	MAKNIB(0, 0),	/* 0x55: */
   1435 	MAKNIB(0, 0),	/* 0x56: */
   1436 	MAKNIB(0, 0),	/* 0x57: */
   1437 	MAKNIB(0, 0),	/* 0x58: */
   1438 	MAKNIB(0, 0),	/* 0x59: */
   1439 	MAKNIB(0, 0),	/* 0x5a: */
   1440 	MAKNIB(0, 0),	/* 0x5b: */
   1441 	MAKNIB(0, 0),	/* 0x5c: */
   1442 	MAKNIB(0, 0),	/* 0x5d: */
   1443 	MAKNIB(0, 0),	/* 0x5e: */
   1444 	MAKNIB(0, 0),	/* 0x5f: */
   1445 	MAKNIB(8, 6),	/* 0x60: MBOX_INIT_FIRMWARE */
   1446 	MAKNIB(0, 0),	/* 0x60: MBOX_GET_INIT_CONTROL_BLOCK  (FORMAT?) */
   1447 	MAKNIB(2, 1),	/* 0x62: MBOX_INIT_LIP */
   1448 	MAKNIB(8, 1),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   1449 	MAKNIB(8, 1),	/* 0x64: MBOX_GET_PORT_DB */
   1450 	MAKNIB(3, 1),	/* 0x65: MBOX_CLEAR_ACA */
   1451 	MAKNIB(3, 1),	/* 0x66: MBOX_TARGET_RESET */
   1452 	MAKNIB(3, 1),	/* 0x67: MBOX_CLEAR_TASK_SET */
   1453 	MAKNIB(3, 1),	/* 0x69: MBOX_ABORT_TASK_SET */
   1454 	MAKNIB(1, 2)	/* 0x69: MBOX_GET_FW_STATE */
   1455 };
   1456 #define	NMBCOM	(sizeof (mbpcnt) / sizeof (mbpcnt[0]))
   1457 
   1458 static void
   1459 isp_mboxcmd(isp, mbp)
   1460 	struct ispsoftc *isp;
   1461 	mbreg_t *mbp;
   1462 {
   1463 	int outparam, inparam;
   1464 	int loops;
   1465 	u_int8_t opcode;
   1466 
   1467 	if (mbp->param[0] == ISP2100_SET_PCI_PARAM) {
   1468 		opcode = mbp->param[0] = MBOX_SET_PCI_PARAMETERS;
   1469 		inparam = 4;
   1470 		outparam = 4;
   1471 		goto command_known;
   1472 	} else if (mbp->param[0] > NMBCOM) {
   1473 		PRINTF("%s: bad command %x\n", isp->isp_name, mbp->param[0]);
   1474 		return;
   1475 	}
   1476 
   1477 	opcode = mbp->param[0];
   1478 	inparam = HINIB(mbpcnt[mbp->param[0]]);
   1479 	outparam =  LONIB(mbpcnt[mbp->param[0]]);
   1480 
   1481 	if (inparam == 0 && outparam == 0) {
   1482 		PRINTF("%s: no parameters for %x\n", isp->isp_name,
   1483 			mbp->param[0]);
   1484 		return;
   1485 	}
   1486 
   1487 
   1488 command_known:
   1489 	/*
   1490 	 * Make sure we can send some words..
   1491 	 */
   1492 
   1493 	loops = MBOX_DELAY_COUNT;
   1494 	while ((ISP_READ(isp, HCCR) & HCCR_HOST_INT) != 0) {
   1495 		SYS_DELAY(100);
   1496 		if (--loops < 0) {
   1497 			PRINTF("%s: isp_mboxcmd timeout #1\n", isp->isp_name);
   1498 			return;
   1499 		}
   1500 	}
   1501 
   1502 	/*
   1503 	 * Write input parameters
   1504 	 */
   1505 	switch (inparam) {
   1506 	case 8: ISP_WRITE(isp, INMAILBOX7, mbp->param[7]); mbp->param[7] = 0;
   1507 	case 7: ISP_WRITE(isp, INMAILBOX6, mbp->param[6]); mbp->param[6] = 0;
   1508 	case 6: ISP_WRITE(isp, INMAILBOX5, mbp->param[5]); mbp->param[5] = 0;
   1509 	case 5: ISP_WRITE(isp, INMAILBOX4, mbp->param[4]); mbp->param[4] = 0;
   1510 	case 4: ISP_WRITE(isp, INMAILBOX3, mbp->param[3]); mbp->param[3] = 0;
   1511 	case 3: ISP_WRITE(isp, INMAILBOX2, mbp->param[2]); mbp->param[2] = 0;
   1512 	case 2: ISP_WRITE(isp, INMAILBOX1, mbp->param[1]); mbp->param[1] = 0;
   1513 	case 1: ISP_WRITE(isp, INMAILBOX0, mbp->param[0]); mbp->param[0] = 0;
   1514 	}
   1515 
   1516 	/*
   1517 	 * Clear semaphore on mailbox registers
   1518 	 */
   1519 	ISP_WRITE(isp, BIU_SEMA, 0);
   1520 
   1521 	/*
   1522 	 * Clear RISC int condition.
   1523 	 */
   1524 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   1525 
   1526 	/*
   1527 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   1528 	 */
   1529 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   1530 
   1531 	/*
   1532 	 * Wait until RISC int is set, except 2100
   1533 	 */
   1534 	if ((isp->isp_type & ISP_HA_FC) == 0) {
   1535 		loops = MBOX_DELAY_COUNT;
   1536 		while ((ISP_READ(isp, BIU_ISR) & BIU_ISR_RISC_INT) == 0) {
   1537 			SYS_DELAY(100);
   1538 			if (--loops < 0) {
   1539 				PRINTF("%s: isp_mboxcmd timeout #2\n",
   1540 				    isp->isp_name);
   1541 				return;
   1542 			}
   1543 		}
   1544 	}
   1545 
   1546 	/*
   1547 	 * Check to make sure that the semaphore has been set.
   1548 	 */
   1549 	loops = MBOX_DELAY_COUNT;
   1550 	while ((ISP_READ(isp, BIU_SEMA) & 1) == 0) {
   1551 		SYS_DELAY(100);
   1552 		if (--loops < 0) {
   1553 			PRINTF("%s: isp_mboxcmd timeout #3\n", isp->isp_name);
   1554 			return;
   1555 		}
   1556 	}
   1557 
   1558 	/*
   1559 	 * Make sure that the MBOX_BUSY has gone away
   1560 	 */
   1561 	loops = MBOX_DELAY_COUNT;
   1562 	while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
   1563 		SYS_DELAY(100);
   1564 		if (--loops < 0) {
   1565 			PRINTF("%s: isp_mboxcmd timeout #4\n", isp->isp_name);
   1566 			return;
   1567 		}
   1568 	}
   1569 
   1570 
   1571 	/*
   1572 	 * Pick up output parameters.
   1573 	 */
   1574 	switch (outparam) {
   1575 	case 8: mbp->param[7] = ISP_READ(isp, OUTMAILBOX7);
   1576 	case 7: mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
   1577 	case 6: mbp->param[5] = ISP_READ(isp, OUTMAILBOX5);
   1578 	case 5: mbp->param[4] = ISP_READ(isp, OUTMAILBOX4);
   1579 	case 4: mbp->param[3] = ISP_READ(isp, OUTMAILBOX3);
   1580 	case 3: mbp->param[2] = ISP_READ(isp, OUTMAILBOX2);
   1581 	case 2: mbp->param[1] = ISP_READ(isp, OUTMAILBOX1);
   1582 	case 1: mbp->param[0] = ISP_READ(isp, OUTMAILBOX0);
   1583 	}
   1584 
   1585 	/*
   1586 	 * Clear RISC int.
   1587 	 */
   1588 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   1589 
   1590 	/*
   1591 	 * Release semaphore on mailbox registers
   1592 	 */
   1593 	ISP_WRITE(isp, BIU_SEMA, 0);
   1594 
   1595 	/*
   1596 	 * Just to be chatty here...
   1597 	 */
   1598 	switch(mbp->param[0]) {
   1599 	case MBOX_COMMAND_COMPLETE:
   1600 		break;
   1601 	case MBOX_INVALID_COMMAND:
   1602 		/*
   1603 		 * GET_CLOCK_RATE can fail a lot
   1604 		 * So can a couple of other commands.
   1605 		 */
   1606 		if (isp->isp_dblev > 2  && opcode != MBOX_GET_CLOCK_RATE) {
   1607 			PRINTF("%s: mbox cmd %x failed with INVALID_COMMAND\n",
   1608 				isp->isp_name, opcode);
   1609 		}
   1610 		break;
   1611 	case MBOX_HOST_INTERFACE_ERROR:
   1612 		PRINTF("%s: mbox cmd %x failed with HOST_INTERFACE_ERROR\n",
   1613 			isp->isp_name, opcode);
   1614 		break;
   1615 	case MBOX_TEST_FAILED:
   1616 		PRINTF("%s: mbox cmd %x failed with TEST_FAILED\n",
   1617 			isp->isp_name, opcode);
   1618 		break;
   1619 	case MBOX_COMMAND_ERROR:
   1620 		PRINTF("%s: mbox cmd %x failed with COMMAND_ERROR\n",
   1621 			isp->isp_name, opcode);
   1622 		break;
   1623 	case MBOX_COMMAND_PARAM_ERROR:
   1624 		PRINTF("%s: mbox cmd %x failed with COMMAND_PARAM_ERROR\n",
   1625 			isp->isp_name, opcode);
   1626 		break;
   1627 
   1628 	case ASYNC_LIP_OCCURRED:
   1629 		break;
   1630 
   1631 	default:
   1632 		/*
   1633 		 * The expected return of EXEC_FIRMWARE is zero.
   1634 		 */
   1635 		if ((opcode == MBOX_EXEC_FIRMWARE && mbp->param[0] != 0) ||
   1636 		    (opcode != MBOX_EXEC_FIRMWARE)) {
   1637 			PRINTF("%s: mbox cmd %x failed with error %x\n",
   1638 				isp->isp_name, opcode, mbp->param[0]);
   1639 		}
   1640 		break;
   1641 	}
   1642 }
   1643 
   1644 void
   1645 isp_lostcmd(struct ispsoftc *isp, ISP_SCSI_XFER_T *xs)
   1646 {
   1647 	mbreg_t mbs;
   1648 
   1649 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
   1650 	isp_mboxcmd(isp, &mbs);
   1651 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1652 		isp_dumpregs(isp, "couldn't GET FIRMWARE STATUS");
   1653 		return;
   1654 	}
   1655 	if (mbs.param[1]) {
   1656 		PRINTF("%s: %d commands on completion queue\n",
   1657 		       isp->isp_name, mbs.param[1]);
   1658 	}
   1659 	if (XS_NULL(xs))
   1660 		return;
   1661 
   1662 	mbs.param[0] = MBOX_GET_DEV_QUEUE_STATUS;
   1663 	mbs.param[1] = XS_TGT(xs) << 8 | XS_LUN(xs);
   1664 	isp_mboxcmd(isp, &mbs);
   1665 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1666 		isp_dumpregs(isp, "couldn't GET DEVICE QUEUE STATUS");
   1667 		return;
   1668 	}
   1669 	PRINTF("%s: lost command for target %d lun %d, %d active of %d, "
   1670 		"Queue State: %x\n", isp->isp_name, XS_TGT(xs),
   1671 		XS_LUN(xs), mbs.param[2], mbs.param[3], mbs.param[1]);
   1672 
   1673 	isp_dumpregs(isp, "lost command");
   1674 	/*
   1675 	 * XXX: Need to try and do something to recover.
   1676 	 */
   1677 }
   1678 
   1679 static void
   1680 isp_dumpregs(struct ispsoftc *isp, const char *msg)
   1681 {
   1682 	PRINTF("%s: %s\n", isp->isp_name, msg);
   1683 	if (isp->isp_type & ISP_HA_SCSI)
   1684 		PRINTF("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
   1685 	else
   1686 		PRINTF("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
   1687 	PRINTF(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
   1688 	       ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
   1689 	PRINTF("risc_hccr=%x\n", ISP_READ(isp, HCCR));
   1690 
   1691 	if (isp->isp_type & ISP_HA_SCSI) {
   1692 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   1693 		PRINTF("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
   1694 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
   1695 			ISP_READ(isp, CDMA_FIFO_STS));
   1696 		PRINTF("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
   1697 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
   1698 			ISP_READ(isp, DDMA_FIFO_STS));
   1699 		PRINTF("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
   1700 			ISP_READ(isp, SXP_INTERRUPT),
   1701 			ISP_READ(isp, SXP_GROSS_ERR),
   1702 			ISP_READ(isp, SXP_PINS_CONTROL));
   1703 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
   1704 	}
   1705 	ISP_DUMPREGS(isp);
   1706 }
   1707 
   1708 static void
   1709 isp_dumpxflist(struct ispsoftc *isp)
   1710 {
   1711 	volatile ISP_SCSI_XFER_T *xs;
   1712 	int i, hdp;
   1713 
   1714 	for (hdp = i = 0; i < RQUEST_QUEUE_LEN; i++) {
   1715 		xs = isp->isp_xflist[i];
   1716 		if (xs == NULL) {
   1717 			continue;
   1718 		}
   1719 		if (hdp == 0) {
   1720 			PRINTF("%s: active requests\n", isp->isp_name);
   1721 			hdp++;
   1722 		}
   1723 		PRINTF(" Active Handle %d: tgt %d lun %d dlen %d\n",
   1724 		    i+1, XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs));
   1725 	}
   1726 }
   1727 
   1728 static void
   1729 isp_fw_state(struct ispsoftc *isp)
   1730 {
   1731 	mbreg_t mbs;
   1732 	if (isp->isp_type & ISP_HA_FC) {
   1733 		int once = 0;
   1734 		fcparam *fcp = isp->isp_param;
   1735 again:
   1736 		mbs.param[0] = MBOX_GET_FW_STATE;
   1737 		isp_mboxcmd(isp, &mbs);
   1738 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1739 			if (mbs.param[0] == ASYNC_LIP_OCCURRED) {
   1740 				if (!once++) {
   1741 					goto again;
   1742 				}
   1743 			}
   1744 			isp_dumpregs(isp, "GET FIRMWARE STATE failed");
   1745 			return;
   1746 		}
   1747 		fcp->isp_fwstate = mbs.param[1];
   1748 	}
   1749 }
   1750 
   1751 static void
   1752 isp_setdparm(struct ispsoftc *isp)
   1753 {
   1754 	int i;
   1755 	mbreg_t mbs;
   1756 	sdparam *sdp;
   1757 
   1758 	isp->isp_fwrev = 0;
   1759 	if (isp->isp_type & ISP_HA_FC) {
   1760 		/*
   1761 		 * ROM in 2100 doesn't appear to support ABOUT_FIRMWARE
   1762 		 */
   1763 		return;
   1764 	}
   1765 
   1766 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
   1767 	isp_mboxcmd(isp, &mbs);
   1768 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1769 		IDPRINTF(3, ("1st ABOUT FIRMWARE command failed"));
   1770 	} else {
   1771 		isp->isp_fwrev =
   1772 			(((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
   1773 	}
   1774 
   1775 
   1776 	sdp = (sdparam *) isp->isp_param;
   1777 	/*
   1778 	 * Try and get old clock rate out before we hit the
   1779 	 * chip over the head- but if and only if we don't
   1780 	 * know our desired clock rate.
   1781 	 */
   1782 	if (isp->isp_mdvec->dv_clock == 0) {
   1783 		mbs.param[0] = MBOX_GET_CLOCK_RATE;
   1784 		isp_mboxcmd(isp, &mbs);
   1785 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   1786 			sdp->isp_clock = mbs.param[1];
   1787 			PRINTF("%s: using board clock 0x%x\n",
   1788 				isp->isp_name, sdp->isp_clock);
   1789 		}
   1790 	} else {
   1791 		sdp->isp_clock = isp->isp_mdvec->dv_clock;
   1792 	}
   1793 
   1794 	mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   1795 	isp_mboxcmd(isp, &mbs);
   1796 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1797 		IDPRINTF(2, ("could not GET ACT NEG STATE\n"));
   1798 		sdp->isp_req_ack_active_neg = 1;
   1799 		sdp->isp_data_line_active_neg = 1;
   1800 	} else {
   1801 		sdp->isp_req_ack_active_neg = (mbs.param[1] >> 4) & 0x1;
   1802 		sdp->isp_data_line_active_neg = (mbs.param[1] >> 5) & 0x1;
   1803 	}
   1804 	for (i = 0; i < MAX_TARGETS; i++) {
   1805 		mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   1806 		mbs.param[1] = i << 8;
   1807 		isp_mboxcmd(isp, &mbs);
   1808 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1809 			IDPRINTF(2, ("cannot get params for target %d\n", i));
   1810 			sdp->isp_devparam[i].sync_period =
   1811 				ISP_10M_SYNCPARMS & 0xff;
   1812 			sdp->isp_devparam[i].sync_offset =
   1813 				ISP_10M_SYNCPARMS >> 8;
   1814 			sdp->isp_devparam[i].dev_flags = DPARM_DEFAULT;
   1815 		} else {
   1816 			IDPRINTF(3, ("\%s: target %d - flags 0x%x, sync %x\n",
   1817 			       isp->isp_name, i, mbs.param[2], mbs.param[3]));
   1818 			sdp->isp_devparam[i].dev_flags = mbs.param[2] >> 8;
   1819 			/*
   1820 			 * The maximum period we can really see
   1821 			 * here is 100 (decimal), or 400 ns.
   1822 			 * For some unknown reason we sometimes
   1823 			 * get back wildass numbers from the
   1824 			 * boot device's paramaters.
   1825 			 */
   1826 			if ((mbs.param[3] & 0xff) <= 0x64) {
   1827 				sdp->isp_devparam[i].sync_period =
   1828 					mbs.param[3] & 0xff;
   1829 				sdp->isp_devparam[i].sync_offset =
   1830 					mbs.param[3] >> 8;
   1831 			}
   1832 		}
   1833 	}
   1834 
   1835 	/*
   1836 	 * Set Default Host Adapter Parameters
   1837 	 * XXX: Should try and get them out of NVRAM
   1838 	 */
   1839 	sdp->isp_adapter_enabled = 1;
   1840 	sdp->isp_cmd_dma_burst_enable = 1;
   1841 	sdp->isp_data_dma_burst_enabl = 1;
   1842 	sdp->isp_fifo_threshold = 2;
   1843 	sdp->isp_initiator_id = 7;
   1844 	sdp->isp_async_data_setup = 6;
   1845 	sdp->isp_selection_timeout = 250;
   1846 	sdp->isp_max_queue_depth = 128;
   1847 	sdp->isp_tag_aging = 8;
   1848 	sdp->isp_bus_reset_delay = 3;
   1849 	sdp->isp_retry_count = 0;
   1850 	sdp->isp_retry_delay = 1;
   1851 
   1852 	for (i = 0; i < MAX_TARGETS; i++) {
   1853 		sdp->isp_devparam[i].exc_throttle = 16;
   1854 		sdp->isp_devparam[i].dev_enable = 1;
   1855 	}
   1856 }
   1857 
   1858 static void
   1859 isp_phoenix(struct ispsoftc *isp)
   1860 {
   1861 	ISP_SCSI_XFER_T *tlist[RQUEST_QUEUE_LEN], *xs;
   1862 	int i;
   1863 
   1864 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   1865 		tlist[i] = (ISP_SCSI_XFER_T *) isp->isp_xflist[i];
   1866 	}
   1867 	isp_reset(isp);
   1868 	isp_init(isp);
   1869 	isp->isp_state = ISP_RUNSTATE;
   1870 
   1871 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   1872 		xs = tlist[i];
   1873 		if (XS_NULL(xs))
   1874 			continue;
   1875 		isp->isp_nactive--;
   1876 		if (isp->isp_nactive < 0)
   1877 			isp->isp_nactive = 0;
   1878 		XS_RESID(xs) = XS_XFRLEN(xs);
   1879 		XS_SETERR(xs, HBA_BOTCH);
   1880 		XS_CMD_DONE(xs);
   1881 	}
   1882 }
   1883 
   1884 void
   1885 isp_watch(void *arg)
   1886 {
   1887 	int i;
   1888 	struct ispsoftc *isp = arg;
   1889 	ISP_SCSI_XFER_T *xs;
   1890 	ISP_LOCKVAL_DECL;
   1891 
   1892 	/*
   1893 	 * Look for completely dead commands (but not polled ones).
   1894 	 */
   1895 	ISP_ILOCK(isp);
   1896 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   1897 		if ((xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[i]) == NULL) {
   1898 			continue;
   1899 		}
   1900 		if (XS_TIME(xs) == 0) {
   1901 			continue;
   1902 		}
   1903 		XS_TIME(xs) -= (WATCH_INTERVAL * 1000);
   1904 		/*
   1905 		 * Avoid later thinking that this
   1906 		 * transaction is not being timed.
   1907 		 * Then give ourselves to watchdog
   1908 		 * periods of grace.
   1909 		 */
   1910 		if (XS_TIME(xs) == 0)
   1911 			XS_TIME(xs) = 1;
   1912 		else if (XS_TIME(xs) > -(2 * WATCH_INTERVAL * 1000)) {
   1913 			continue;
   1914 		}
   1915 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
   1916 			PRINTF("%s: isp_watch failed to abort command\n",
   1917 			    isp->isp_name);
   1918 			isp_phoenix(isp);
   1919 			break;
   1920 		}
   1921 	}
   1922 	ISP_IUNLOCK(isp);
   1923 	RESTART_WATCHDOG(isp_watch, isp);
   1924 }
   1925 
   1926 static void
   1927 isp_prtstst(ispstatusreq_t *sp)
   1928 {
   1929 	PRINTF("states->");
   1930 	if (sp->req_state_flags & RQSF_GOT_BUS)
   1931 		PRINTF("GOT_BUS ");
   1932 	if (sp->req_state_flags & RQSF_GOT_TARGET)
   1933 		PRINTF("GOT_TGT ");
   1934 	if (sp->req_state_flags & RQSF_SENT_CDB)
   1935 		PRINTF("SENT_CDB ");
   1936 	if (sp->req_state_flags & RQSF_XFRD_DATA)
   1937 		PRINTF("XFRD_DATA ");
   1938 	if (sp->req_state_flags & RQSF_GOT_STATUS)
   1939 		PRINTF("GOT_STS ");
   1940 	if (sp->req_state_flags & RQSF_GOT_SENSE)
   1941 		PRINTF("GOT_SNS ");
   1942 	if (sp->req_state_flags & RQSF_XFER_COMPLETE)
   1943 		PRINTF("XFR_CMPLT ");
   1944 	PRINTF("\n");
   1945 	PRINTF("status->");
   1946 	if (sp->req_status_flags & RQSTF_DISCONNECT)
   1947 		PRINTF("Disconnect ");
   1948 	if (sp->req_status_flags & RQSTF_SYNCHRONOUS)
   1949 		PRINTF("Sync_xfr ");
   1950 	if (sp->req_status_flags & RQSTF_PARITY_ERROR)
   1951 		PRINTF("Parity ");
   1952 	if (sp->req_status_flags & RQSTF_BUS_RESET)
   1953 		PRINTF("Bus_Reset ");
   1954 	if (sp->req_status_flags & RQSTF_DEVICE_RESET)
   1955 		PRINTF("Device_Reset ");
   1956 	if (sp->req_status_flags & RQSTF_ABORTED)
   1957 		PRINTF("Aborted ");
   1958 	if (sp->req_status_flags & RQSTF_TIMEOUT)
   1959 		PRINTF("Timeout ");
   1960 	if (sp->req_status_flags & RQSTF_NEGOTIATION)
   1961 		PRINTF("Negotiation ");
   1962 	PRINTF("\n");
   1963 }
   1964