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