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