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