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