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