Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.30
      1 /* $NetBSD: isp.c,v 1.30 1999/01/10 05:07:53 mjacob Exp $ */
      2 /* release_12_28_98_A+ */
      3 /*
      4  * Machine and OS Independent (well, as best as possible)
      5  * code for the Qlogic ISP SCSI adapters.
      6  *
      7  *---------------------------------------
      8  * Copyright (c) 1997, 1998 by Matthew Jacob
      9  * NASA/Ames Research Center
     10  * All rights reserved.
     11  *---------------------------------------
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice immediately at the beginning of the file, without modification,
     18  *    this list of conditions, and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. The name of the author may not be used to endorse or promote products
     23  *    derived from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     29  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
     40  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
     41  * ideas dredged from the Solaris driver.
     42  */
     43 
     44 /*
     45  * Include header file appropriate for platform we're building on.
     46  */
     47 
     48 #ifdef	__NetBSD__
     49 #include <dev/ic/isp_netbsd.h>
     50 #endif
     51 #ifdef	__FreeBSD__
     52 #include <dev/isp/isp_freebsd.h>
     53 #endif
     54 #ifdef	__linux__
     55 #include "isp_linux.h"
     56 #endif
     57 
     58 /*
     59  * General defines
     60  */
     61 
     62 #define	MBOX_DELAY_COUNT	1000000 / 100
     63 
     64 /*
     65  * Local static data
     66  */
     67 #ifdef	ISP_TARGET_MODE
     68 static const char tgtiqd[36] = {
     69 	0x03, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
     70 	0x51, 0x4C, 0x4F, 0x47, 0x49, 0x43, 0x20, 0x20,
     71 #ifdef	__NetBSD__
     72 	0x4E, 0x45, 0x54, 0x42, 0x53, 0x44, 0x20, 0x20,
     73 #else
     74 # ifdef	__FreeBSD__
     75 	0x46, 0x52, 0x45, 0x45, 0x42, 0x52, 0x44, 0x20,
     76 # else
     77 #  ifdef linux
     78 	0x4C, 0x49, 0x4E, 0x55, 0x58, 0x20, 0x20, 0x20,
     79 #  else
     80 #  endif
     81 # endif
     82 #endif
     83 	0x54, 0x41, 0x52, 0x47, 0x45, 0x54, 0x20, 0x20,
     84 	0x20, 0x20, 0x20, 0x31
     85 };
     86 #endif
     87 
     88 
     89 /*
     90  * Local function prototypes.
     91  */
     92 static int isp_parse_async __P((struct ispsoftc *, int));
     93 static int isp_handle_other_response
     94 __P((struct ispsoftc *, ispstatusreq_t *, u_int8_t *));
     95 #ifdef	ISP_TARGET_MODE
     96 static int isp_modify_lun __P((struct ispsoftc *, int, int, int));
     97 static void isp_notify_ack __P((struct ispsoftc *, void *));
     98 static void isp_handle_atio __P((struct ispsoftc *, void *));
     99 static void isp_handle_atio2 __P((struct ispsoftc *, void *));
    100 static void isp_handle_ctio __P((struct ispsoftc *, void *));
    101 static void isp_handle_ctio2 __P((struct ispsoftc *, void *));
    102 #endif
    103 static void isp_parse_status
    104 __P((struct ispsoftc *, ispstatusreq_t *, ISP_SCSI_XFER_T *));
    105 static void isp_fibre_init __P((struct ispsoftc *));
    106 static void isp_fw_state __P((struct ispsoftc *));
    107 static void isp_dumpregs __P((struct ispsoftc *, const char *));
    108 static void isp_dumpxflist __P((struct ispsoftc *));
    109 static void isp_prtstst __P((ispstatusreq_t *));
    110 static char *isp2100_fw_statename __P((int));
    111 static void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *));
    112 
    113 static void isp_update  __P((struct ispsoftc *));
    114 static void isp_setdfltparm __P((struct ispsoftc *));
    115 static int isp_read_nvram __P((struct ispsoftc *));
    116 static void isp_rdnvram_word __P((struct ispsoftc *, int, u_int16_t *));
    117 
    118 /*
    119  * Reset Hardware.
    120  *
    121  * Hit the chip over the head, download new f/w.
    122  *
    123  * Locking done elsewhere.
    124  */
    125 void
    126 isp_reset(isp)
    127 	struct ispsoftc *isp;
    128 {
    129 	static char once = 1;
    130 	mbreg_t mbs;
    131 	int loops, i, dodnld = 1, deadchip;
    132 	char *revname;
    133 
    134 	isp->isp_state = ISP_NILSTATE;
    135 
    136 	/*
    137 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
    138 	 * have been set in the MD code. We figure out more
    139 	 * here.
    140 	 */
    141 	isp->isp_dblev = DFLT_DBLEVEL;
    142 	deadchip = ISP_READ(isp, HCCR) & HCCR_RESET;
    143 	if (deadchip) {
    144 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
    145 		if (ISP_READ(isp, HCCR) & HCCR_RESET) {
    146 			isp_dumpregs(isp, "still reset after release");
    147 			SYS_DELAY(1000);
    148 		} else {
    149 			deadchip = 1;
    150 		}
    151 	}
    152 
    153 	if (isp->isp_type & ISP_HA_FC) {
    154 		revname = "2100";
    155 	} else {
    156 		sdparam *sdp = isp->isp_param;
    157 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
    158 		switch (i) {
    159 		default:
    160 			PRINTF("%s: unknown chip rev. 0x%x- assuming a 1020\n",
    161 			    isp->isp_name, i);
    162 			/* FALLTHROUGH */
    163 		case 1:
    164 			revname = "1020";
    165 			isp->isp_type = ISP_HA_SCSI_1020;
    166 			sdp->isp_clock = 40;
    167 			break;
    168 		case 2:
    169 			/*
    170 			 * Some 1020A chips are Ultra Capable, but don't
    171 			 * run the clock rate up for that unless told to
    172 			 * do so by the Ultra Capable bits being set.
    173 			 */
    174 			revname = "1020A";
    175 			isp->isp_type = ISP_HA_SCSI_1020A;
    176 			sdp->isp_clock = 40;
    177 			break;
    178 		case 3:
    179 			revname = "1040";
    180 			isp->isp_type = ISP_HA_SCSI_1040;
    181 			sdp->isp_clock = 60;
    182 			break;
    183 		case 4:
    184 			revname = "1040A";
    185 			isp->isp_type = ISP_HA_SCSI_1040A;
    186 			sdp->isp_clock = 60;
    187 			break;
    188 		case 5:
    189 			revname = "1040B";
    190 			isp->isp_type = ISP_HA_SCSI_1040B;
    191 			sdp->isp_clock = 60;
    192 			break;
    193 		}
    194 		/*
    195 		 * Try and figure out if we're connected to a differential bus.
    196 		 * You have to pause the RISC processor to read SXP registers.
    197 		 */
    198 		if (deadchip == 0) {
    199 			ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
    200 			i = 100;
    201 			while ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
    202 				SYS_DELAY(20);
    203 				if (--i == 0) {
    204 					isp_dumpregs(isp,
    205 					    "cannot stop RISC processor");
    206 					i = -1;
    207 					break;
    208 				}
    209 			}
    210 		} else {
    211 			i = 0;
    212 		}
    213 		if (i > 0) {
    214 			if (isp->isp_bustype != ISP_BT_SBUS) {
    215 				ISP_SETBITS(isp, BIU_CONF1, BIU_PCI_CONF1_SXP);
    216 			}
    217 			if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
    218 				IDPRINTF(2, ("%s: Differential Mode Set\n",
    219 				    isp->isp_name));
    220 				sdp->isp_diffmode = 1;
    221 			} else {
    222 				sdp->isp_diffmode = 0;
    223 			}
    224 
    225 			if (isp->isp_bustype != ISP_BT_SBUS) {
    226 				ISP_CLRBITS(isp, BIU_CONF1, BIU_PCI_CONF1_SXP);
    227 			}
    228 
    229 			/*
    230 			 * Figure out whether we're ultra capable.
    231 			 */
    232 			i = ISP_READ(isp, RISC_PSR);
    233 			if (isp->isp_bustype != ISP_BT_SBUS) {
    234 				i &= RISC_PSR_PCI_ULTRA;
    235 			} else {
    236 				i &= RISC_PSR_SBUS_ULTRA;
    237 			}
    238 			if (i) {
    239 				IDPRINTF(2, ("%s: Ultra Mode Capable\n",
    240 				    isp->isp_name));
    241 				sdp->isp_clock = 60;
    242 			} else {
    243 				sdp->isp_clock = 40;
    244 			}
    245 			/*
    246 			 * Restart processor, if necessary.
    247 			 */
    248 			if (deadchip == 0)
    249 				ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
    250 		}
    251 		/*
    252 		 * Machine dependent clock (if set) overrides
    253 		 * our generic determinations.
    254 		 */
    255 		if (isp->isp_mdvec->dv_clock) {
    256 			if (isp->isp_mdvec->dv_clock < sdp->isp_clock) {
    257 				sdp->isp_clock = isp->isp_mdvec->dv_clock;
    258 			}
    259 		}
    260 	}
    261 
    262 	/*
    263 	 * Do MD specific pre initialization
    264 	 */
    265 	ISP_RESET0(isp);
    266 
    267 	if (once == 1 && deadchip == 0) {
    268 		once = 0;
    269 		/*
    270 		 * Get the current running firmware revision out of the
    271 		 * chip before we hit it over the head (if this is our
    272 		 * first time through). Note that we store this as the
    273 		 * 'ROM' firmware revision- which it may not be. In any
    274 		 * case, we don't really use this yet, but we may in
    275 		 * the future.
    276 		 */
    277 		mbs.param[0] = MBOX_ABOUT_FIRMWARE;
    278 		isp_mboxcmd(isp, &mbs);
    279 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    280 			IDPRINTF(3, ("%s: initial ABOUT FIRMWARE command "
    281 			    "failed\n", isp->isp_name));
    282 		} else {
    283 			isp->isp_romfw_rev =
    284 			    (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
    285 		}
    286 	}
    287 
    288 
    289 	/*
    290 	 * Hit the chip over the head with hammer,
    291 	 * and give the ISP a chance to recover.
    292 	 */
    293 
    294 	if (isp->isp_type & ISP_HA_SCSI) {
    295 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
    296 		/*
    297 		 * A slight delay...
    298 		 */
    299 		SYS_DELAY(100);
    300 
    301 		/*
    302 		 * Clear data && control DMA engines.
    303 		 */
    304 		ISP_WRITE(isp, CDMA_CONTROL,
    305 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    306 		ISP_WRITE(isp, DDMA_CONTROL,
    307 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    308 	} else {
    309 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
    310 		/*
    311 		 * A slight delay...
    312 		 */
    313 		SYS_DELAY(100);
    314 		ISP_WRITE(isp, CDMA2100_CONTROL,
    315 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    316 		ISP_WRITE(isp, TDMA2100_CONTROL,
    317 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    318 		ISP_WRITE(isp, RDMA2100_CONTROL,
    319 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    320 	}
    321 
    322 	/*
    323 	 * Wait for ISP to be ready to go...
    324 	 */
    325 	loops = MBOX_DELAY_COUNT;
    326 	for (;;) {
    327 		if (isp->isp_type & ISP_HA_SCSI) {
    328 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
    329 				break;
    330 		} else {
    331 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
    332 				break;
    333 		}
    334 		SYS_DELAY(100);
    335 		if (--loops < 0) {
    336 			isp_dumpregs(isp, "chip reset timed out");
    337 			return;
    338 		}
    339 	}
    340 	/*
    341 	 * More initialization
    342 	 */
    343 	if (isp->isp_type & ISP_HA_SCSI) {
    344 		ISP_WRITE(isp, BIU_CONF1, 0);
    345 	} else {
    346 		ISP_WRITE(isp, BIU2100_CSR, 0);
    347 		/*
    348 		 * All 2100's are 60Mhz with fast rams onboard.
    349 		 */
    350 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
    351 	}
    352 
    353 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
    354 	SYS_DELAY(100);
    355 
    356 	/*
    357 	 * Establish some initial burst rate thingies
    358 	 * (only for the 1XX0 boards). This really should
    359 	 * be done later after fetching from NVRAM.
    360 	 */
    361 	if (isp->isp_type & ISP_HA_SCSI) {
    362 		u_int16_t conf1 = isp->isp_mdvec->dv_conf1;
    363 		/*
    364 		 * Busted FIFO. Turn off all but burst enables.
    365 		 */
    366 		if (isp->isp_type == ISP_HA_SCSI_1040A) {
    367 			conf1 &= BIU_BURST_ENABLE;
    368 		}
    369 		ISP_SETBITS(isp, BIU_CONF1, conf1);
    370 		if (conf1 & BIU_BURST_ENABLE) {
    371 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
    372 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
    373 		}
    374 	}
    375 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
    376 
    377 	/*
    378 	 * Do MD specific post initialization
    379 	 */
    380 	ISP_RESET1(isp);
    381 
    382 	/*
    383 	 * Enable interrupts
    384 	 */
    385 	ENABLE_INTS(isp);
    386 
    387 	/*
    388 	 * Do some sanity checking.
    389 	 */
    390 	mbs.param[0] = MBOX_NO_OP;
    391 	isp_mboxcmd(isp, &mbs);
    392 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    393 		isp_dumpregs(isp, "NOP test failed");
    394 		return;
    395 	}
    396 
    397 	if (isp->isp_type & ISP_HA_SCSI) {
    398 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
    399 		mbs.param[1] = 0xdead;
    400 		mbs.param[2] = 0xbeef;
    401 		mbs.param[3] = 0xffff;
    402 		mbs.param[4] = 0x1111;
    403 		mbs.param[5] = 0xa5a5;
    404 		isp_mboxcmd(isp, &mbs);
    405 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    406 			isp_dumpregs(isp,
    407 				"Mailbox Register test didn't complete");
    408 			return;
    409 		}
    410 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
    411 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
    412 		    mbs.param[5] != 0xa5a5) {
    413 			isp_dumpregs(isp, "Register Test Failed");
    414 			return;
    415 		}
    416 
    417 	}
    418 
    419 	/*
    420 	 * Download new Firmware, unless requested not to do so.
    421 	 * This is made slightly trickier in some cases where the
    422 	 * firmware of the ROM revision is newer than the revision
    423 	 * compiled into the driver. So, where we used to compare
    424 	 * versions of our f/w and the ROM f/w, now we just see
    425 	 * whether we have f/w at all and whether a config flag
    426 	 * has disabled our download.
    427 	 */
    428 	if ((isp->isp_mdvec->dv_fwlen == 0) ||
    429 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
    430 		dodnld = 0;
    431 	}
    432 
    433 	if (dodnld) {
    434 		for (i = 0; i < isp->isp_mdvec->dv_fwlen; i++) {
    435 			mbs.param[0] = MBOX_WRITE_RAM_WORD;
    436 			mbs.param[1] = isp->isp_mdvec->dv_codeorg + i;
    437 			mbs.param[2] = isp->isp_mdvec->dv_ispfw[i];
    438 			isp_mboxcmd(isp, &mbs);
    439 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    440 				isp_dumpregs(isp, "f/w download failed");
    441 				return;
    442 			}
    443 		}
    444 
    445 		if (isp->isp_mdvec->dv_fwlen) {
    446 			/*
    447 			 * Verify that it downloaded correctly.
    448 			 */
    449 			mbs.param[0] = MBOX_VERIFY_CHECKSUM;
    450 			mbs.param[1] = isp->isp_mdvec->dv_codeorg;
    451 			isp_mboxcmd(isp, &mbs);
    452 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    453 				isp_dumpregs(isp, "ram checksum failure");
    454 				return;
    455 			}
    456 		}
    457 	} else {
    458 		IDPRINTF(3, ("%s: skipping f/w download\n", isp->isp_name));
    459 	}
    460 
    461 	/*
    462 	 * Now start it rolling.
    463 	 *
    464 	 * If we didn't actually download f/w,
    465 	 * we still need to (re)start it.
    466 	 */
    467 
    468 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
    469 	mbs.param[1] = isp->isp_mdvec->dv_codeorg;
    470 	isp_mboxcmd(isp, &mbs);
    471 
    472 	if (isp->isp_type & ISP_HA_SCSI) {
    473 		sdparam *sdp = isp->isp_param;
    474 		/*
    475 		 * Set CLOCK RATE, but only if asked to.
    476 		 */
    477 		if (sdp->isp_clock) {
    478 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
    479 			mbs.param[1] = sdp->isp_clock;
    480 			isp_mboxcmd(isp, &mbs);
    481 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    482 				isp_dumpregs(isp, "failed to set CLOCKRATE");
    483 				/* but continue */
    484 			} else {
    485 				IDPRINTF(3, ("%s: setting input clock to %d\n",
    486 				    isp->isp_name, sdp->isp_clock));
    487 			}
    488 		}
    489 	}
    490 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
    491 	isp_mboxcmd(isp, &mbs);
    492 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    493 		isp_dumpregs(isp, "ABOUT FIRMWARE command failed");
    494 		return;
    495 	}
    496 	PRINTF("%s: Board Revision %s, %s F/W Revision %d.%d\n",
    497 		isp->isp_name, revname, dodnld? "loaded" : "resident",
    498 		mbs.param[1], mbs.param[2]);
    499 	isp->isp_fwrev = (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
    500 	if (isp->isp_romfw_rev && dodnld) {
    501 		PRINTF("%s: Last F/W revision was %d.%d\n", isp->isp_name,
    502 		    isp->isp_romfw_rev >> 10, isp->isp_romfw_rev & 0x3ff);
    503 	}
    504 	isp_fw_state(isp);
    505 	isp->isp_state = ISP_RESETSTATE;
    506 }
    507 
    508 /*
    509  * Initialize Hardware to known state
    510  *
    511  * Locks are held before coming here.
    512  */
    513 
    514 void
    515 isp_init(isp)
    516 	struct ispsoftc *isp;
    517 {
    518 	sdparam *sdp;
    519 	mbreg_t mbs;
    520 	int tgt;
    521 
    522 	/*
    523 	 * Must do first.
    524 	 */
    525 	isp_setdfltparm(isp);
    526 
    527 	/*
    528 	 * If we're fibre, we have a completely different
    529 	 * initialization method.
    530 	 */
    531 
    532 	if (isp->isp_type & ISP_HA_FC) {
    533 		isp_fibre_init(isp);
    534 		return;
    535 	}
    536 	sdp = isp->isp_param;
    537 
    538 	/*
    539 	 * Set (possibly new) Initiator ID.
    540 	 */
    541 	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
    542 	mbs.param[1] = sdp->isp_initiator_id;
    543 	isp_mboxcmd(isp, &mbs);
    544 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    545 		isp_dumpregs(isp, "failed to set initiator id");
    546 		return;
    547 	}
    548 
    549 	/*
    550 	 * Set Retry Delay and Count
    551 	 */
    552 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
    553 	mbs.param[1] = sdp->isp_retry_count;
    554 	mbs.param[2] = sdp->isp_retry_delay;
    555 	isp_mboxcmd(isp, &mbs);
    556 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    557 		isp_dumpregs(isp, "failed to set retry count and delay");
    558 		return;
    559 	}
    560 
    561 	/*
    562 	 * Set ASYNC DATA SETUP time. This is very important.
    563 	 */
    564 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
    565 	mbs.param[1] = sdp->isp_async_data_setup;
    566 	isp_mboxcmd(isp, &mbs);
    567 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    568 		isp_dumpregs(isp, "failed to set async data setup time");
    569 		return;
    570 	}
    571 
    572 	/*
    573 	 * Set ACTIVE Negation State.
    574 	 */
    575 	mbs.param[0] = MBOX_SET_ACTIVE_NEG_STATE;
    576 	mbs.param[1] =
    577 	    (sdp->isp_req_ack_active_neg << 4) |
    578 	    (sdp->isp_data_line_active_neg << 5);
    579 	isp_mboxcmd(isp, &mbs);
    580 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    581 		isp_dumpregs(isp, "failed to set active neg state");
    582 		return;
    583 	}
    584 
    585 	/*
    586 	 * Set the Tag Aging limit
    587 	 */
    588 
    589 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
    590 	mbs.param[1] = sdp->isp_tag_aging;
    591 	isp_mboxcmd(isp, &mbs);
    592 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    593 		isp_dumpregs(isp, "failed to set tag age limit");
    594 		return;
    595 	}
    596 
    597 	/*
    598 	 * Set selection timeout.
    599 	 */
    600 
    601 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
    602 	mbs.param[1] = sdp->isp_selection_timeout;
    603 	isp_mboxcmd(isp, &mbs);
    604 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    605 		isp_dumpregs(isp, "failed to set selection timeout");
    606 		return;
    607 	}
    608 
    609 	/*
    610 	 * Set per-target parameters to a safe minimum.
    611 	 */
    612 
    613 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
    614 		int maxlun, lun;
    615 
    616 		if (sdp->isp_devparam[tgt].dev_enable == 0)
    617 			continue;
    618 
    619 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
    620 		mbs.param[1] = tgt << 8;
    621 		mbs.param[2] = DPARM_SAFE_DFLT;
    622 		mbs.param[3] = 0;
    623 		/*
    624 		 * It is not quite clear when this changed over so that
    625 		 * we could force narrow and async, so assume >= 7.55.
    626 		 *
    627 		 * Otherwise, a SCSI bus reset issued below will force
    628 		 * the back to the narrow, async state (but see note
    629 		 * below also). Technically we should also do without
    630 		 * Parity.
    631 		 */
    632 		if (isp->isp_fwrev >= ISP_FW_REV(7, 55)) {
    633 			mbs.param[2] |= DPARM_NARROW | DPARM_ASYNC;
    634 		}
    635 		sdp->isp_devparam[tgt].cur_dflags = mbs.param[2] >> 8;
    636 
    637 		IDPRINTF(3, ("\n%s: tgt %d cflags %x offset %x period %x\n",
    638 		    isp->isp_name, tgt, mbs.param[2], mbs.param[3] >> 8,
    639 		    mbs.param[3] & 0xff));
    640 		isp_mboxcmd(isp, &mbs);
    641 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    642 
    643 			PRINTF("%s: failed to set parameters for tgt %d\n",
    644 				isp->isp_name, tgt);
    645 
    646 			PRINTF("%s: flags %x offset %x period %x\n",
    647 				isp->isp_name, sdp->isp_devparam[tgt].dev_flags,
    648 				sdp->isp_devparam[tgt].sync_offset,
    649 				sdp->isp_devparam[tgt].sync_period);
    650 
    651 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
    652 			mbs.param[1] = tgt << 8;
    653 			mbs.param[2] = DPARM_SAFE_DFLT;
    654 			mbs.param[3] = 0;
    655 			isp_mboxcmd(isp, &mbs);
    656 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    657 				PRINTF("%s: failed even to set defaults for "
    658 				    "target %d\n", isp->isp_name, tgt);
    659 				continue;
    660 			}
    661 		}
    662 
    663 		maxlun = (isp->isp_fwrev >= ISP_FW_REV(7, 55))? 32 : 8;
    664 		for (lun = 0; lun < maxlun; lun++) {
    665 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
    666 			mbs.param[1] = (tgt << 8) | lun;
    667 			mbs.param[2] = sdp->isp_max_queue_depth;
    668 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
    669 			isp_mboxcmd(isp, &mbs);
    670 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    671 				PRINTF("%s: failed to set device queue "
    672 				    "parameters for target %d, lun %d\n",
    673 				    isp->isp_name, tgt, lun);
    674 				break;
    675 			}
    676 		}
    677 	}
    678 
    679 	/*
    680 	 * Set up DMA for the request and result mailboxes.
    681 	 */
    682 	if (ISP_MBOXDMASETUP(isp) != 0) {
    683 		PRINTF("%s: can't setup dma mailboxes\n", isp->isp_name);
    684 		return;
    685 	}
    686 
    687 	mbs.param[0] = MBOX_INIT_RES_QUEUE;
    688 	mbs.param[1] = RESULT_QUEUE_LEN;
    689 	mbs.param[2] = (u_int16_t) (isp->isp_result_dma >> 16);
    690 	mbs.param[3] = (u_int16_t) (isp->isp_result_dma & 0xffff);
    691 	mbs.param[4] = 0;
    692 	mbs.param[5] = 0;
    693 	isp_mboxcmd(isp, &mbs);
    694 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    695 		isp_dumpregs(isp, "set of response queue failed");
    696 		return;
    697 	}
    698 	isp->isp_residx = 0;
    699 
    700 	mbs.param[0] = MBOX_INIT_REQ_QUEUE;
    701 	mbs.param[1] = RQUEST_QUEUE_LEN;
    702 	mbs.param[2] = (u_int16_t) (isp->isp_rquest_dma >> 16);
    703 	mbs.param[3] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
    704 	mbs.param[4] = 0;
    705 	mbs.param[5] = 0;
    706 	isp_mboxcmd(isp, &mbs);
    707 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    708 		isp_dumpregs(isp, "set of request queue failed");
    709 		return;
    710 	}
    711 	isp->isp_reqidx = isp->isp_reqodx = 0;
    712 
    713 	/*
    714 	 * XXX: See whether or not for 7.55 F/W or later we
    715 	 * XXX: can do without this, and see whether we should
    716 	 * XXX: honor the NVRAM SCSI_RESET_DISABLE token.
    717 	 */
    718 	mbs.param[0] = MBOX_BUS_RESET;
    719 	mbs.param[1] = 3;
    720 	isp_mboxcmd(isp, &mbs);
    721 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    722 		isp_dumpregs(isp, "SCSI bus reset failed");
    723 	}
    724 	/*
    725 	 * This is really important to have set after a bus reset.
    726 	 */
    727 	isp->isp_sendmarker = 1;
    728 	isp->isp_state = ISP_INITSTATE;
    729 }
    730 
    731 /*
    732  * Fibre Channel specific initialization.
    733  *
    734  * Locks are held before coming here.
    735  */
    736 static void
    737 isp_fibre_init(isp)
    738 	struct ispsoftc *isp;
    739 {
    740 	fcparam *fcp;
    741 	isp_icb_t *icbp;
    742 	mbreg_t mbs;
    743 	int count, loopid;
    744 	u_int8_t lwfs;
    745 
    746 	fcp = isp->isp_param;
    747 
    748 	if (ISP_MBOXDMASETUP(isp) != 0) {
    749 		PRINTF("%s: can't setup DMA for mailboxes\n", isp->isp_name);
    750 		return;
    751 	}
    752 	/*
    753 	 * For systems that don't have BIOS methods for which
    754 	 * we can easily change the NVRAM based loopid, we'll
    755 	 * override that here. Note that when we initialize
    756 	 * the firmware we may get back a different loopid than
    757 	 * we asked for anyway. XXX This is probably not the
    758 	 * best way to figure this out XXX
    759 	 */
    760 #ifndef	__i386__
    761 	loopid = DEFAULT_LOOPID;
    762 #else
    763 	loopid = fcp->isp_loopid;
    764 #endif
    765 
    766 
    767 	icbp = (isp_icb_t *) fcp->isp_scratch;
    768 	MEMZERO(icbp, sizeof (*icbp));
    769 
    770 	icbp->icb_version = ICB_VERSION1;
    771 #ifdef	ISP_TARGET_MODE
    772 	fcp->isp_fwoptions = ICBOPT_TGT_ENABLE|ICBOPT_INI_TGTTYPE;
    773 #else
    774 	fcp->isp_fwoptions = 0;
    775 #endif
    776 	fcp->isp_fwoptions |= ICBOPT_INI_ADISC|ICBOPT_FAIRNESS;
    777 	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
    778 	fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
    779 #ifdef	CHECKME
    780 	fcp->isp_fwoptions |= ICBOPT_USE_PORTNAME;
    781 #endif
    782 #ifdef	THIS_WORKED
    783 	/*
    784 	 * This has unhappiness in target mode
    785 	 */
    786 	fcp->isp_fwoptions |= ICBOPT_FULL_LOGIN;
    787 #endif
    788 	icbp->icb_fwoptions = fcp->isp_fwoptions;
    789 	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
    790 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
    791 	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
    792 		PRINTF("%s: bad frame length (%d) from NVRAM- using %d\n",
    793 		    isp->isp_name, fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
    794 	}
    795 	icbp->icb_maxalloc = fcp->isp_maxalloc;
    796 	icbp->icb_execthrottle = fcp->isp_execthrottle;
    797 	icbp->icb_retry_delay = fcp->isp_retry_delay;
    798 	icbp->icb_retry_count = fcp->isp_retry_count;
    799 	icbp->icb_hardaddr = loopid;
    800 
    801 	MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwn);
    802 	if (icbp->icb_fwoptions & ICBOPT_USE_PORTNAME) {
    803 		u_int64_t portname = fcp->isp_wwn | (2LL << 56);
    804 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, portname);
    805 	}
    806 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN;
    807 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN;
    808 	icbp->icb_rqstaddr[RQRSP_ADDR0015] =
    809 	    (u_int16_t) (isp->isp_rquest_dma & 0xffff);
    810 	icbp->icb_rqstaddr[RQRSP_ADDR1631] =
    811 	    (u_int16_t) (isp->isp_rquest_dma >> 16);
    812 	icbp->icb_respaddr[RQRSP_ADDR0015] =
    813 	    (u_int16_t) (isp->isp_result_dma & 0xffff);
    814 	icbp->icb_respaddr[RQRSP_ADDR1631] =
    815 	    (u_int16_t) (isp->isp_result_dma >> 16);
    816 
    817 	for (count = 0; count < 10; count++) {
    818 		mbs.param[0] = MBOX_INIT_FIRMWARE;
    819 		mbs.param[1] = 0;
    820 		mbs.param[2] = (u_int16_t) (fcp->isp_scdma >> 16);
    821 		mbs.param[3] = (u_int16_t) (fcp->isp_scdma & 0xffff);
    822 		mbs.param[4] = 0;
    823 		mbs.param[5] = 0;
    824 		mbs.param[6] = 0;
    825 		mbs.param[7] = 0;
    826 
    827 		isp_mboxcmd(isp, &mbs);
    828 
    829 		switch (mbs.param[0]) {
    830 		case MBOX_COMMAND_COMPLETE:
    831 			count = 10;
    832 			break;
    833 		case ASYNC_LIP_OCCURRED:
    834 		case ASYNC_LOOP_UP:
    835 		case ASYNC_LOOP_DOWN:
    836 		case ASYNC_LOOP_RESET:
    837 		case ASYNC_PDB_CHANGED:
    838 		case ASYNC_CHANGE_NOTIFY:
    839 			if (count > 9) {
    840 				PRINTF("%s: too many retries to get going- "
    841 				    "giving up\n", isp->isp_name);
    842 				return;
    843 			}
    844 			break;
    845 		default:
    846 			isp_dumpregs(isp, "INIT FIRMWARE failed");
    847 			return;
    848 		}
    849 	}
    850 	isp->isp_reqidx = isp->isp_reqodx = 0;
    851 	isp->isp_residx = 0;
    852 
    853 	/*
    854 	 * Wait up to 5 seconds for FW to go to READY state.
    855 	 */
    856 	lwfs = FW_CONFIG_WAIT;
    857 	for (count = 0; count < 12000; count++) {
    858 		isp_fw_state(isp);
    859 		if (lwfs != fcp->isp_fwstate) {
    860 			PRINTF("%s: Firmware State %s -> %s\n",
    861 			    isp->isp_name, isp2100_fw_statename((int)lwfs),
    862 			    isp2100_fw_statename((int)fcp->isp_fwstate));
    863 			lwfs = fcp->isp_fwstate;
    864 		}
    865 		if (fcp->isp_fwstate == FW_READY) {
    866 			break;
    867 		}
    868 		SYS_DELAY(1000);	/* wait one millisecond */
    869 	}
    870 	isp->isp_sendmarker = 1;
    871 
    872 	/*
    873 	 * Get our Loop ID
    874 	 * (if possible)
    875 	 */
    876 	if (fcp->isp_fwstate == FW_READY) {
    877 		mbs.param[0] = MBOX_GET_LOOP_ID;
    878 		isp_mboxcmd(isp, &mbs);
    879 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    880 			isp_dumpregs(isp, "GET LOOP ID failed");
    881 			return;
    882 		}
    883 		fcp->isp_loopid = mbs.param[1];
    884 		fcp->isp_alpa = mbs.param[2];
    885 		PRINTF("%s: Loop ID %d, ALPA 0x%x\n", isp->isp_name,
    886 		    fcp->isp_loopid, fcp->isp_alpa);
    887 		isp->isp_state = ISP_INITSTATE;
    888 #ifdef	ISP_TARGET_MODE
    889 		DISABLE_INTS(isp);
    890 		if (isp_modify_lun(isp, 0, 1, 1)) {
    891 			PRINTF("%s: failed to enable target mode\n",
    892 			    isp->isp_name);
    893 		}
    894 		ENABLE_INTS(isp);
    895 #endif
    896 	} else {
    897 		PRINTF("%s: failed to go to FW READY state- will not attach\n",
    898 		    isp->isp_name);
    899 	}
    900 }
    901 
    902 /*
    903  * Start a command. Locking is assumed done in the caller.
    904  */
    905 
    906 int32_t
    907 ispscsicmd(xs)
    908 	ISP_SCSI_XFER_T *xs;
    909 {
    910 	struct ispsoftc *isp;
    911 	u_int8_t iptr, optr;
    912 	union {
    913 		ispreq_t *_reqp;
    914 		ispreqt2_t *_t2reqp;
    915 	} _u;
    916 #define	reqp	_u._reqp
    917 #define	t2reqp	_u._t2reqp
    918 #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
    919 	int i;
    920 
    921 	XS_INITERR(xs);
    922 	isp = XS_ISP(xs);
    923 
    924 	if (isp->isp_state != ISP_RUNSTATE) {
    925 		PRINTF("%s: adapter not ready\n", isp->isp_name);
    926 		XS_SETERR(xs, HBA_BOTCH);
    927 		return (CMD_COMPLETE);
    928 	}
    929 
    930 	/*
    931 	 * We *could* do the different sequence type that has clos
    932 	 * to the whole Queue Entry for the command,.
    933 	 */
    934 	if (XS_CDBLEN(xs) > ((isp->isp_type & ISP_HA_FC)? 16 : 12)) {
    935 		PRINTF("%s: unsupported cdb length (%d)\n",
    936 		    isp->isp_name, XS_CDBLEN(xs));
    937 		XS_SETERR(xs, HBA_BOTCH);
    938 		return (CMD_COMPLETE);
    939 	}
    940 
    941 	/*
    942 	 * First check to see if any HBA or Device
    943 	 * parameters need to be updated.
    944 	 */
    945 	if (isp->isp_update) {
    946 		isp_update(isp);
    947 	}
    948 
    949 	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
    950 	iptr = isp->isp_reqidx;
    951 
    952 	reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
    953 	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
    954 	if (iptr == optr) {
    955 		IDPRINTF(2, ("%s: Request Queue Overflow\n", isp->isp_name));
    956 		XS_SETERR(xs, HBA_BOTCH);
    957 		return (CMD_EAGAIN);
    958 	}
    959 	if (isp->isp_type & ISP_HA_FC) {
    960 		DISABLE_INTS(isp);
    961 	}
    962 
    963 	if (isp->isp_sendmarker) {
    964 		u_int8_t niptr;
    965 		ispmarkreq_t *marker = (ispmarkreq_t *) reqp;
    966 
    967 		MEMZERO((void *) marker, sizeof (*marker));
    968 		marker->req_header.rqs_entry_count = 1;
    969 		marker->req_header.rqs_entry_type = RQSTYPE_MARKER;
    970 		marker->req_modifier = SYNC_ALL;
    971 
    972 		isp->isp_sendmarker = 0;
    973 
    974 		/*
    975 		 * Unconditionally update the input pointer anyway.
    976 		 */
    977 		ISP_WRITE(isp, INMAILBOX4, iptr);
    978 		isp->isp_reqidx = iptr;
    979 
    980 		niptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
    981 		if (niptr == optr) {
    982 			if (isp->isp_type & ISP_HA_FC) {
    983 				ENABLE_INTS(isp);
    984 			}
    985 			IDPRINTF(2, ("%s: Request Queue Overflow+\n",
    986 			    isp->isp_name));
    987 			XS_SETERR(xs, HBA_BOTCH);
    988 			return (CMD_EAGAIN);
    989 		}
    990 		reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
    991 		iptr = niptr;
    992 	}
    993 
    994 	MEMZERO((void *) reqp, UZSIZE);
    995 	reqp->req_header.rqs_entry_count = 1;
    996 	if (isp->isp_type & ISP_HA_FC) {
    997 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
    998 	} else {
    999 		reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
   1000 	}
   1001 	reqp->req_header.rqs_flags = 0;
   1002 	reqp->req_header.rqs_seqno = isp->isp_seqno++;
   1003 
   1004 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   1005 		if (isp->isp_xflist[i] == NULL)
   1006 			break;
   1007 	}
   1008 	if (i == RQUEST_QUEUE_LEN) {
   1009 		if (isp->isp_type & ISP_HA_FC)
   1010 			ENABLE_INTS(isp);
   1011 		IDPRINTF(2, ("%s: out of xflist pointers\n", isp->isp_name));
   1012 		XS_SETERR(xs, HBA_BOTCH);
   1013 		return (CMD_EAGAIN);
   1014 	} else {
   1015 		/*
   1016 		 * Never have a handle that is zero, so
   1017 		 * set req_handle off by one.
   1018 		 */
   1019 		isp->isp_xflist[i] = xs;
   1020 		reqp->req_handle = i+1;
   1021 	}
   1022 
   1023 	if (isp->isp_type & ISP_HA_FC) {
   1024 		/*
   1025 		 * See comment in isp_intr
   1026 		 */
   1027 		XS_RESID(xs) = 0;
   1028 		/*
   1029 		 * Fibre Channel always requires some kind of tag.
   1030 		 * If we're marked as "Can't Tag", just do simple
   1031 		 * instead of ordered tags. It's pretty clear to me
   1032 		 * that we shouldn't do head of queue tagging in
   1033 		 * this case.
   1034 		 */
   1035 		if (XS_CANTAG(xs)) {
   1036 			t2reqp->req_flags = XS_KINDOF_TAG(xs);
   1037 		} else {
   1038  			t2reqp->req_flags = REQFLAG_STAG;
   1039 		}
   1040 	} else {
   1041 		sdparam *sdp = (sdparam *)isp->isp_param;
   1042 		if ((sdp->isp_devparam[XS_TGT(xs)].cur_dflags & DPARM_TQING) &&
   1043 		    XS_CANTAG(xs)) {
   1044 			reqp->req_flags = XS_KINDOF_TAG(xs);
   1045 		} else {
   1046 			reqp->req_flags = 0;
   1047 		}
   1048 	}
   1049 	reqp->req_target = XS_TGT(xs);
   1050 	if (isp->isp_type & ISP_HA_SCSI) {
   1051 		reqp->req_lun_trn = XS_LUN(xs);
   1052 		reqp->req_cdblen = XS_CDBLEN(xs);
   1053 	} else {
   1054 #ifdef	SCCLUN
   1055 		reqp->req_scclun = XS_LUN(xs);
   1056 #else
   1057 		reqp->req_lun_trn = XS_LUN(xs);
   1058 #endif
   1059 
   1060 	}
   1061 	memcpy(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
   1062 
   1063 	IDPRINTF(5, ("%s(%d.%d): START%d cmd 0x%x datalen %d\n", isp->isp_name,
   1064 	    XS_TGT(xs), XS_LUN(xs), reqp->req_header.rqs_seqno,
   1065 	    reqp->req_cdb[0], XS_XFRLEN(xs)));
   1066 
   1067 	reqp->req_time = XS_TIME(xs) / 1000;
   1068 	if (reqp->req_time == 0 && XS_TIME(xs))
   1069 		reqp->req_time = 1;
   1070 	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
   1071 	if (i != CMD_QUEUED) {
   1072 		if (isp->isp_type & ISP_HA_FC)
   1073 			ENABLE_INTS(isp);
   1074 		/*
   1075 		 * dmasetup sets actual error in packet, and
   1076 		 * return what we were given to return.
   1077 		 */
   1078 		return (i);
   1079 	}
   1080 	XS_SETERR(xs, HBA_NOERROR);
   1081 	ISP_WRITE(isp, INMAILBOX4, iptr);
   1082 	isp->isp_reqidx = iptr;
   1083 	if (isp->isp_type & ISP_HA_FC) {
   1084 		ENABLE_INTS(isp);
   1085 	}
   1086 	isp->isp_nactive++;
   1087 	return (CMD_QUEUED);
   1088 #undef	reqp
   1089 #undef	t2reqp
   1090 }
   1091 
   1092 /*
   1093  * isp control
   1094  * Locks (ints blocked) assumed held.
   1095  */
   1096 
   1097 int
   1098 isp_control(isp, ctl, arg)
   1099 	struct ispsoftc *isp;
   1100 	ispctl_t ctl;
   1101 	void *arg;
   1102 {
   1103 	ISP_SCSI_XFER_T *xs;
   1104 	mbreg_t mbs;
   1105 	int i;
   1106 
   1107 	switch (ctl) {
   1108 	default:
   1109 		PRINTF("%s: isp_control unknown control op %x\n",
   1110 		    isp->isp_name, ctl);
   1111 		break;
   1112 
   1113 	case ISPCTL_RESET_BUS:
   1114 		mbs.param[0] = MBOX_BUS_RESET;
   1115 		mbs.param[1] = (isp->isp_type & ISP_HA_FC)? 5: 2;
   1116 		isp_mboxcmd(isp, &mbs);
   1117 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1118 			isp_dumpregs(isp, "isp_control SCSI bus reset failed");
   1119 			break;
   1120 		}
   1121 		/*
   1122 		 * This is really important to have set after a bus reset.
   1123 		 */
   1124 		isp->isp_sendmarker = 1;
   1125 		PRINTF("%s: driver initiated bus reset\n", isp->isp_name);
   1126 		return (0);
   1127 
   1128         case ISPCTL_RESET_DEV:
   1129 		/*
   1130 		 * Note that under parallel SCSI, this issues a BDR message.
   1131 		 * Under FC, we could probably be using ABORT TASK SET
   1132 		 * command.
   1133 		 */
   1134 
   1135 		mbs.param[0] = MBOX_ABORT_TARGET;
   1136 		mbs.param[1] = ((long)arg) << 8;
   1137 		mbs.param[2] = 2;	/* 'delay', in seconds */
   1138 		isp_mboxcmd(isp, &mbs);
   1139 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1140 			isp_dumpregs(isp, "SCSI Target  reset failed");
   1141 			break;
   1142 		}
   1143 		PRINTF("%s: Target %d Reset Succeeded\n", isp->isp_name,
   1144 		    (int) ((long) arg));
   1145 		isp->isp_sendmarker = 1;
   1146 		return (0);
   1147 
   1148         case ISPCTL_ABORT_CMD:
   1149 		xs = (ISP_SCSI_XFER_T *) arg;
   1150 		for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   1151 			if (xs == isp->isp_xflist[i]) {
   1152 				break;
   1153 			}
   1154 		}
   1155 		if (i == RQUEST_QUEUE_LEN) {
   1156 			PRINTF("%s: isp_control- cannot find command to abort "
   1157 			    "in active list\n", isp->isp_name);
   1158 			break;
   1159 		}
   1160 		mbs.param[0] = MBOX_ABORT;
   1161 #ifdef	SCCLUN
   1162 		if (isp->isp_type & ISP_HA_FC) {
   1163 			mbs.param[1] = XS_TGT(xs) << 8;
   1164 			mbs.param[4] = 0;
   1165 			mbs.param[5] = 0;
   1166 			mbs.param[6] = XS_LUN(xs);
   1167 		} else {
   1168 			mbs.param[1] = XS_TGT(xs) << 8 | XS_LUN(xs);
   1169 		}
   1170 #else
   1171 		mbs.param[1] = XS_TGT(xs) << 8 | XS_LUN(xs);
   1172 #endif
   1173 		mbs.param[2] = (i+1) >> 16;
   1174 		mbs.param[3] = (i+1) & 0xffff;
   1175 		isp_mboxcmd(isp, &mbs);
   1176 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1177 			PRINTF("%s: isp_control MBOX_ABORT failure (code %x)\n",
   1178 			    isp->isp_name, mbs.param[0]);
   1179 			break;
   1180 		}
   1181 		PRINTF("%s: command for target %d lun %d was aborted\n",
   1182 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1183 		return (0);
   1184 
   1185 	case ISPCTL_UPDATE_PARAMS:
   1186 		isp_update(isp);
   1187 		return(0);
   1188 	}
   1189 	return (-1);
   1190 }
   1191 
   1192 /*
   1193  * Interrupt Service Routine(s).
   1194  *
   1195  * External (OS) framework has done the appropriate locking,
   1196  * and the locking will be held throughout this function.
   1197  */
   1198 
   1199 int
   1200 isp_intr(arg)
   1201 	void *arg;
   1202 {
   1203 	ISP_SCSI_XFER_T *complist[RESULT_QUEUE_LEN], *xs;
   1204 	struct ispsoftc *isp = arg;
   1205 	u_int8_t iptr, optr;
   1206 	u_int16_t isr;
   1207 	int i, ndone = 0;
   1208 
   1209 	isr = ISP_READ(isp, BIU_ISR);
   1210 	if (isp->isp_type & ISP_HA_FC) {
   1211 		if (isr == 0 || (isr & BIU2100_ISR_RISC_INT) == 0) {
   1212 			if (isr) {
   1213 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
   1214 					     isp->isp_name, isr));
   1215 			}
   1216 			return (0);
   1217 		}
   1218 	} else {
   1219 		if (isr == 0 || (isr & BIU_ISR_RISC_INT) == 0) {
   1220 			if (isr) {
   1221 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
   1222 					     isp->isp_name, isr));
   1223 			}
   1224 			return (0);
   1225 		}
   1226 	}
   1227 
   1228 	if (ISP_READ(isp, BIU_SEMA) & 1) {
   1229 		u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
   1230 		if (isp_parse_async(isp, (int) mbox))
   1231 			return (1);
   1232 		ISP_WRITE(isp, BIU_SEMA, 0);
   1233 	}
   1234 
   1235 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   1236 
   1237 	optr = isp->isp_residx;
   1238 	iptr = ISP_READ(isp, OUTMAILBOX5);
   1239 
   1240 	if (optr == iptr) {
   1241 		IDPRINTF(4, ("why intr? isr %x iptr %x optr %x\n",
   1242 		    isr, optr, iptr));
   1243 	}
   1244 	ENABLE_INTS(isp);
   1245 
   1246 	while (optr != iptr) {
   1247 		ispstatusreq_t *sp;
   1248 		u_int8_t oop;
   1249 		int buddaboom = 0;
   1250 
   1251 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   1252 		oop = optr;
   1253 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN);
   1254 
   1255 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
   1256 			if (isp_handle_other_response(isp, sp, &optr) == 0) {
   1257 				ISP_WRITE(isp, INMAILBOX5, optr);
   1258 				continue;
   1259 			}
   1260 			/*
   1261 			 * It really has to be a bounced request just copied
   1262 			 * from the request queue to the response queue.
   1263 			 */
   1264 
   1265 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
   1266 				ISP_WRITE(isp, INMAILBOX5, optr);
   1267 				continue;
   1268 			}
   1269 			PRINTF("%s: not RESPONSE in RESPONSE Queue "
   1270 			    "(type 0x%x) @ idx %d (next %d)\n", isp->isp_name,
   1271 			    sp->req_header.rqs_entry_type, oop, optr);
   1272 			buddaboom = 1;
   1273 		}
   1274 
   1275 		if (sp->req_header.rqs_flags & 0xf) {
   1276 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   1277 				ISP_WRITE(isp, INMAILBOX5, optr);
   1278 				continue;
   1279 			}
   1280 			PRINTF("%s: rqs_flags=%x", isp->isp_name,
   1281 				sp->req_header.rqs_flags & 0xf);
   1282 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   1283 				PRINTF("%s: internal queues full\n",
   1284 				    isp->isp_name);
   1285 				/* XXXX: this command *could* get restarted */
   1286 				buddaboom++;
   1287 			}
   1288 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   1289 				PRINTF("%s: bad header\n", isp->isp_name);
   1290 				buddaboom++;
   1291 			}
   1292 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   1293 				PRINTF("%s: bad request packet\n",
   1294 				    isp->isp_name);
   1295 				buddaboom++;
   1296 			}
   1297 		}
   1298 		if (sp->req_handle > RQUEST_QUEUE_LEN || sp->req_handle < 1) {
   1299 			PRINTF("%s: bad request handle %d\n", isp->isp_name,
   1300 				sp->req_handle);
   1301 			ISP_WRITE(isp, INMAILBOX5, optr);
   1302 			continue;
   1303 		}
   1304 		xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[sp->req_handle - 1];
   1305 		if (xs == NULL) {
   1306 			PRINTF("%s: NULL xs in xflist (handle %x)\n",
   1307 			    isp->isp_name, sp->req_handle);
   1308 			isp_dumpxflist(isp);
   1309 			ISP_WRITE(isp, INMAILBOX5, optr);
   1310 			continue;
   1311 		}
   1312 		isp->isp_xflist[sp->req_handle - 1] = NULL;
   1313 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   1314 			isp->isp_sendmarker = 1;
   1315 		}
   1316 		if (buddaboom) {
   1317 			XS_SETERR(xs, HBA_BOTCH);
   1318 		}
   1319 		XS_STS(xs) = sp->req_scsi_status & 0xff;
   1320 		if (isp->isp_type & ISP_HA_SCSI) {
   1321 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   1322 				memcpy(XS_SNSP(xs), sp->req_sense_data,
   1323 					XS_SNSLEN(xs));
   1324 				XS_SNS_IS_VALID(xs);
   1325 			}
   1326 		} else {
   1327 			if (XS_STS(xs) == SCSI_CHECK) {
   1328 				XS_SNS_IS_VALID(xs);
   1329 				memcpy(XS_SNSP(xs), sp->req_sense_data,
   1330 					XS_SNSLEN(xs));
   1331 				sp->req_state_flags |= RQSF_GOT_SENSE;
   1332 			}
   1333 		}
   1334 		if (XS_NOERR(xs) && XS_STS(xs) == SCSI_BUSY) {
   1335 			XS_SETERR(xs, HBA_TGTBSY);
   1336 		}
   1337 
   1338 		if (sp->req_header.rqs_entry_type == RQSTYPE_RESPONSE) {
   1339 			if (XS_NOERR(xs)) {
   1340 			    if (sp->req_completion_status != RQCS_COMPLETE) {
   1341 				isp_parse_status(isp, sp, xs);
   1342 			    } else {
   1343 				XS_SETERR(xs, HBA_NOERROR);
   1344 			    }
   1345 			}
   1346 		} else {
   1347 			PRINTF("%s: unknown return %x\n", isp->isp_name,
   1348 				sp->req_header.rqs_entry_type);
   1349 			if (XS_NOERR(xs)) {
   1350 				XS_SETERR(xs, HBA_BOTCH);
   1351 			}
   1352 		}
   1353 		if (isp->isp_type & ISP_HA_SCSI) {
   1354 			XS_RESID(xs) = sp->req_resid;
   1355 		} else if (sp->req_scsi_status & RQCS_RU) {
   1356 			XS_RESID(xs) = sp->req_resid;
   1357 			IDPRINTF(4, ("%s: cnt %d rsd %d\n", isp->isp_name,
   1358 				XS_XFRLEN(xs), sp->req_resid));
   1359 		}
   1360 		if (XS_XFRLEN(xs)) {
   1361 			ISP_DMAFREE(isp, xs, sp->req_handle - 1);
   1362 		}
   1363 		/*
   1364 		 * XXX: If we have a check condition, but no Sense Data,
   1365 		 * XXX: mark it as an error (ARQ failed). We need to
   1366 		 * XXX: to do a more distinct job because there may
   1367 		 * XXX: cases where ARQ is disabled.
   1368 		 */
   1369 		if (XS_STS(xs) == SCSI_CHECK && !(XS_IS_SNS_VALID(xs))) {
   1370 			if (XS_NOERR(xs)) {
   1371 				PRINTF("%s: ARQ failure for target %d lun %d\n",
   1372 				    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   1373 				XS_SETERR(xs, HBA_ARQFAIL);
   1374 			}
   1375 		}
   1376 		if ((isp->isp_dblev >= 5) ||
   1377 		    (isp->isp_dblev > 2 && !XS_NOERR(xs))) {
   1378 			PRINTF("%s(%d.%d): FIN%d dl%d resid%d STS %x",
   1379 			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
   1380 			    sp->req_header.rqs_seqno, XS_XFRLEN(xs),
   1381 			    XS_RESID(xs), XS_STS(xs));
   1382 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   1383 				PRINTF(" Skey: %x", XS_SNSKEY(xs));
   1384 				if (!(XS_IS_SNS_VALID(xs))) {
   1385 					PRINTF(" BUT NOT SET");
   1386 				}
   1387 			}
   1388 			PRINTF(" XS_ERR=0x%x\n", (unsigned int) XS_ERR(xs));
   1389 		}
   1390 
   1391 		ISP_WRITE(isp, INMAILBOX5, optr);
   1392 		if (isp->isp_nactive > 0)
   1393 		    isp->isp_nactive--;
   1394 		complist[ndone++] = xs;	/* defer completion call until later */
   1395 	}
   1396 	/*
   1397 	 * If we completed any commands, then it's valid to find out
   1398 	 * what the outpointer is.
   1399 	 */
   1400 	if (ndone) {
   1401 	 	isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
   1402 	}
   1403 	isp->isp_residx = optr;
   1404 	for (i = 0; i < ndone; i++) {
   1405 		xs = complist[i];
   1406 		if (xs) {
   1407 			XS_CMD_DONE(xs);
   1408 		}
   1409 	}
   1410 	return (1);
   1411 }
   1412 
   1413 /*
   1414  * Support routines.
   1415  */
   1416 
   1417 static int
   1418 isp_parse_async(isp, mbox)
   1419 	struct ispsoftc *isp;
   1420 	int mbox;
   1421 {
   1422 	switch (mbox) {
   1423 	case ASYNC_BUS_RESET:
   1424 		PRINTF("%s: SCSI bus reset detected\n", isp->isp_name);
   1425 		isp->isp_sendmarker = 1;
   1426 #ifdef	ISP_TARGET_MODE
   1427 		isp_notify_ack(isp, NULL);
   1428 #endif
   1429 		break;
   1430 
   1431 	case ASYNC_SYSTEM_ERROR:
   1432 		mbox = ISP_READ(isp, OUTMAILBOX1);
   1433 		PRINTF("%s: Internal FW Error @ RISC Addr 0x%x\n",
   1434 		    isp->isp_name, mbox);
   1435 		isp_restart(isp);
   1436 		/* no point continuing after this */
   1437 		return (1);
   1438 
   1439 	case ASYNC_RQS_XFER_ERR:
   1440 		PRINTF("%s: Request Queue Transfer Error\n", isp->isp_name);
   1441 		break;
   1442 
   1443 	case ASYNC_RSP_XFER_ERR:
   1444 		PRINTF("%s: Response Queue Transfer Error\n", isp->isp_name);
   1445 		break;
   1446 
   1447 	case ASYNC_QWAKEUP:
   1448 		/* don't need to be chatty */
   1449 		mbox = ISP_READ(isp, OUTMAILBOX4);
   1450 		break;
   1451 
   1452 	case ASYNC_TIMEOUT_RESET:
   1453 		PRINTF("%s: timeout initiated SCSI bus reset\n", isp->isp_name);
   1454 		isp->isp_sendmarker = 1;
   1455 #ifdef	ISP_TARGET_MODE
   1456 		isp_notify_ack(isp, NULL);
   1457 #endif
   1458 		break;
   1459 
   1460 	case ASYNC_DEVICE_RESET:
   1461 		PRINTF("%s: device reset\n", isp->isp_name);
   1462 #ifdef	ISP_TARGET_MODE
   1463 		isp_notify_ack(isp, NULL);
   1464 #endif
   1465 		break;
   1466 
   1467 	case ASYNC_EXTMSG_UNDERRUN:
   1468 		PRINTF("%s: extended message underrun\n", isp->isp_name);
   1469 		break;
   1470 
   1471 	case ASYNC_SCAM_INT:
   1472 		PRINTF("%s: SCAM interrupt\n", isp->isp_name);
   1473 		break;
   1474 
   1475 	case ASYNC_HUNG_SCSI:
   1476 		PRINTF("%s: stalled SCSI Bus after DATA Overrun\n",
   1477 		    isp->isp_name);
   1478 		/* XXX: Need to issue SCSI reset at this point */
   1479 		break;
   1480 
   1481 	case ASYNC_KILLED_BUS:
   1482 		PRINTF("%s: SCSI Bus reset after DATA Overrun\n",
   1483 		    isp->isp_name);
   1484 		break;
   1485 
   1486 	case ASYNC_BUS_TRANSIT:
   1487 		PRINTF("%s: LBD->HVD Transition 0x%x\n",
   1488 		    isp->isp_name, ISP_READ(isp, OUTMAILBOX1));
   1489 		break;
   1490 
   1491 	case ASYNC_CMD_CMPLT:
   1492 		PRINTF("%s: fast post completion\n", isp->isp_name);
   1493 #if	0
   1494 		fast_post_handle = (ISP_READ(isp, OUTMAILBOX1) << 16) |
   1495 		    ISP_READ(isp, OUTMAILBOX2);
   1496 #endif
   1497 		break;
   1498 
   1499 	case ASYNC_CTIO_DONE:
   1500 		/* Should only occur when Fast Posting Set for 2100s */
   1501 		PRINTF("%s: CTIO done\n", isp->isp_name);
   1502 		break;
   1503 
   1504 	case ASYNC_LIP_OCCURRED:
   1505 		PRINTF("%s: LIP occurred\n", isp->isp_name);
   1506 		break;
   1507 
   1508 	case ASYNC_LOOP_UP:
   1509 		PRINTF("%s: Loop UP\n", isp->isp_name);
   1510 		break;
   1511 
   1512 	case ASYNC_LOOP_DOWN:
   1513 		PRINTF("%s: Loop DOWN\n", isp->isp_name);
   1514 		break;
   1515 
   1516 	case ASYNC_LOOP_RESET:
   1517 		PRINTF("%s: Loop RESET\n", isp->isp_name);
   1518 #ifdef	ISP_TARGET_MODE
   1519 		isp_notify_ack(isp, NULL);
   1520 #endif
   1521 		break;
   1522 
   1523 	case ASYNC_PDB_CHANGED:
   1524 		PRINTF("%s: Port Database Changed\n", isp->isp_name);
   1525 		break;
   1526 
   1527 	case ASYNC_CHANGE_NOTIFY:
   1528 		PRINTF("%s: Name Server Database Changed\n", isp->isp_name);
   1529 		break;
   1530 
   1531 	default:
   1532 		PRINTF("%s: async %x\n", isp->isp_name, mbox);
   1533 		break;
   1534 	}
   1535 	return (0);
   1536 }
   1537 
   1538 static int
   1539 isp_handle_other_response(isp, sp, optrp)
   1540 	struct ispsoftc *isp;
   1541 	ispstatusreq_t *sp;
   1542 	u_int8_t *optrp;
   1543 {
   1544 	u_int8_t iptr, optr;
   1545 	int reqsize = 0;
   1546 	void *ireqp = NULL;
   1547 #ifdef	ISP_TARGET_MODE
   1548 	union {
   1549 		at_entry_t	*atio;
   1550 		at2_entry_t	*at2io;
   1551 		ct_entry_t	*ctio;
   1552 		ct2_entry_t	*ct2io;
   1553 		lun_entry_t	*lunen;
   1554 		in_entry_t	*inot;
   1555 		in_fcentry_t	*inot_fc;
   1556 		na_entry_t	*nack;
   1557 		na_fcentry_t	*nack_fc;
   1558 		void            *voidp;
   1559 #define	atio	un.atio
   1560 #define	at2io	un.at2io
   1561 #define	ctio	un.ctio
   1562 #define	ct2io	un.ct2io
   1563 #define	lunen	un.lunen
   1564 #define	inot	un.inot
   1565 #define	inot_fc	un.inot_fc
   1566 #define	nack	un.nack
   1567 #define	nack_fc	un.nack_fc
   1568 	} un;
   1569 
   1570 	un.voidp = sp;
   1571 #endif
   1572 
   1573 
   1574 	switch (sp->req_header.rqs_entry_type) {
   1575 	case RQSTYPE_REQUEST:
   1576 		return (-1);
   1577 #ifdef	ISP_TARGET_MODE
   1578 	case RQSTYPE_NOTIFY_ACK:
   1579 	{
   1580 		static const char *f =
   1581 			"%s: Notify Ack Status 0x%x Sequence Id 0x%x\n"
   1582 		/*
   1583 		 * The ISP is acknowleding our ack of an Immediate Notify.
   1584 		 */
   1585 		if (isp->isp_type & ISP_HA_FC) {
   1586 			PRINTF(f, isp->isp_name,
   1587 			    nack_fc->na-status, nack_fc->na_seqid);
   1588 		} else {
   1589 			PRINTF(f, isp->isp_name,
   1590 			    nack->na_status, nack->na_seqid);
   1591 		}
   1592 		break;
   1593 	}
   1594 	case RQSTYPE_NOTIFY:
   1595 	{
   1596 		u_int16_t seqid, status;
   1597 
   1598 		/*
   1599 		 * Either the ISP received a SCSI message it cannot handle
   1600 		 * or some other out of band condition (e.g., Port Logout)
   1601 		 * or it is returning an Immediate Notify entry we sent.
   1602 		 */
   1603 		if (isp->isp_type & ISP_HA_FC) {
   1604 			status = inot_fc->status;
   1605 			seqid = inot_fc->in_seqid;
   1606 		} else {
   1607 			status = inot->status;
   1608 			seqid = inot->seqid & 0xff;
   1609 		}
   1610 		PRINTF("%s: Immediate Notify Status 0x%x Sequence Id 0x%x\n",
   1611 		    isp->isp_name, status, seqid);
   1612 
   1613 		switch (status) {
   1614 		case IN_MSG_RECEIVED:
   1615 		case IN_IDE_RECEIVED:
   1616 			ptisp_got_msg(ptp, &inot);
   1617 			break;
   1618 		case IN_RSRC_UNAVAIL:
   1619                         PRINTF("%s: Firmware out of ATIOs\n", isp->isp_name);
   1620                         break;
   1621                 case IN_ABORT_TASK:
   1622 			PRINTF("%s: Abort Task iid %d rx_id 0x%x\n",
   1623 			    inot_fc->in_iid, seqid);
   1624                         break;
   1625                 case IN_PORT_LOGOUT:
   1626 			PRINTF("%s: Port Logout for Initiator %d\n",
   1627 			    isp->isp_name, inot_fc->in_iid);
   1628                         break;
   1629                 default:
   1630 			PRINTF("%s: bad status (0x%x) in Immediate Notify\n",
   1631                             isp->isp_name, status);
   1632                         break;
   1633 
   1634 		}
   1635 		isp_notify_ack(isp, un.voidp);
   1636 		reqsize = 0;
   1637 		break;
   1638 	}
   1639 	case RQSTYPE_ENABLE_LUN:
   1640 	case RQSTYPE_MODIFY_LUN:
   1641 		if (lunen->req_status != 1) {
   1642 		    PRINTF("%s: ENABLE/MODIFY LUN returned status 0x%x\n",
   1643 			isp->isp_name, lunen->req_status);
   1644 		}
   1645 		break;
   1646 	case RQSTYPE_ATIO2:
   1647 	{
   1648 		fcparam *fcp = isp->isp_param;
   1649 		ispctiot2_t local, *ct2 = NULL;
   1650 		ispatiot2_t *at2 = (ispatiot2_t *) sp;
   1651 		int s, lun;
   1652 
   1653 #ifdef	SCCLUN
   1654 		lun = at2->req_scclun;
   1655 #else
   1656 		lun = at2->req_lun;
   1657 #endif
   1658 		PRINTF("%s: atio2 loopid %d for lun %d rxid 0x%x flags0x%x "
   1659 		    "tflags0x%x ecodes0x%x rqstatus0x%x\n", isp->isp_name,
   1660 		    at2->req_initiator, lun, at2->req_rxid,
   1661 		    at2->req_flags, at2->req_taskflags, at2->req_execodes,
   1662 		    at2->req_status);
   1663 
   1664 		switch (at2->req_status & ~ATIO_SENSEVALID) {
   1665 		case ATIO_PATH_INVALID:
   1666 			PRINTF("%s: ATIO2 Path Invalid\n", isp->isp_name);
   1667 			break;
   1668 		case ATIO_NOCAP:
   1669 			PRINTF("%s: ATIO2 No Cap\n", isp->isp_name);
   1670 			break;
   1671 		case ATIO_BDR_MSG:
   1672 			PRINTF("%s: ATIO2 BDR Received\n", isp->isp_name);
   1673 			break;
   1674 		case ATIO_CDB_RECEIVED:
   1675 			ct2 = &local;
   1676 			break;
   1677 		default:
   1678 			PRINTF("%s: unknown req_status 0x%x\n", isp->isp_name,
   1679 			    at2->req_status);
   1680 			break;
   1681 		}
   1682 		if (ct2 == NULL) {
   1683 			/*
   1684 			 * Just do an ACCEPT on this fellow.
   1685 			 */
   1686 			at2->req_header.rqs_entry_type = RQSTYPE_ATIO2;
   1687 			at2->req_header.rqs_flags = 0;
   1688 			at2->req_flags = 1;
   1689 			ireqp = at2;
   1690 			reqsize = sizeof (*at2);
   1691 			break;
   1692 		}
   1693 		PRINTF("%s: datalen %d cdb0=0x%x\n", isp->isp_name,
   1694 		    at2->req_datalen, at2->req_cdb[0]);
   1695 		MEMZERO((void *) ct2, sizeof (*ct2));
   1696 		ct2->req_header.rqs_entry_type = RQSTYPE_CTIO2;
   1697 		ct2->req_header.rqs_entry_count = 1;
   1698 		ct2->req_header.rqs_flags = 0;
   1699 		ct2->req_header.rqs_seqno = isp->isp_seqno++;
   1700 		ct2->req_handle = (at2->req_initiator << 16) | lun;
   1701 #ifndef	SCCLUN
   1702 		ct2->req_lun = lun;
   1703 #endif
   1704 		ct2->req_initiator = at2->req_initiator;
   1705 		ct2->req_rxid = at2->req_rxid;
   1706 
   1707 		ct2->req_flags = CTIO_SEND_STATUS;
   1708 		switch (at2->req_cdb[0]) {
   1709 		case 0x0:		/* TUR */
   1710 			ct2->req_flags |= CTIO_NODATA | CTIO2_SMODE0;
   1711 			ct2->req_m.mode0.req_scsi_status = CTIO2_STATUS_VALID;
   1712 			break;
   1713 
   1714 		case 0x3:		/* REQUEST SENSE */
   1715 		case 0x12:		/* INQUIRE */
   1716 			ct2->req_flags |= CTIO_SEND_DATA | CTIO2_SMODE0;
   1717 			ct2->req_m.mode0.req_scsi_status = CTIO2_STATUS_VALID;
   1718 			ct2->req_seg_count = 1;
   1719 			if (at2->req_cdb[0] == 0x12) {
   1720 				s = sizeof(tgtiqd);
   1721 				memcpy(fcp->isp_scratch, tgtiqd, s);
   1722 			} else {
   1723 				s = at2->req_datalen;
   1724 				MEMZERO(fcp->isp_scratch, s);
   1725 			}
   1726 			ct2->req_m.mode0.req_dataseg[0].ds_base =
   1727 			    fcp->isp_scdma;
   1728 			ct2->req_m.mode0.req_dataseg[0].ds_count = s;
   1729 			ct2->req_m.mode0.req_datalen = s;
   1730 #if	1
   1731 			if (at2->req_datalen < s) {
   1732 				ct2->req_m.mode1.req_scsi_status |=
   1733 				    CTIO2_RESP_VALID|CTIO2_RSPOVERUN;
   1734 			} else if (at2->req_datalen > s) {
   1735 				ct2->req_m.mode1.req_scsi_status |=
   1736 				    CTIO2_RESP_VALID|CTIO2_RSPUNDERUN;
   1737 			}
   1738 #endif
   1739 			break;
   1740 
   1741 		default:		/* ALL OTHERS */
   1742 			ct2->req_flags |= CTIO_NODATA | CTIO2_SMODE1;
   1743 			ct2->req_m.mode1.req_scsi_status = 0;
   1744 #if	1
   1745 			if (at2->req_datalen) {
   1746 				ct2->req_m.mode1.req_scsi_status |=
   1747 				    CTIO2_RSPUNDERUN;
   1748 #if	BYTE_ORDER == BIG_ENDIAN
   1749 				ct2->req_resid[1] = at2->req_datalen & 0xff;
   1750 				ct2->req_resid[0] =
   1751 					(at2->req_datalen >> 8) & 0xff;
   1752 				ct2->req_resid[3] =
   1753 					(at2->req_datalen >> 16) & 0xff;
   1754 				ct2->req_resid[2] =
   1755 					(at2->req_datalen >> 24) & 0xff;
   1756 #else
   1757 				ct2->req_resid[0] = at2->req_datalen & 0xff;
   1758 				ct2->req_resid[1] =
   1759 					(at2->req_datalen >> 8) & 0xff;
   1760 				ct2->req_resid[2] =
   1761 					(at2->req_datalen >> 16) & 0xff;
   1762 				ct2->req_resid[3] =
   1763 					(at2->req_datalen >> 24) & 0xff;
   1764 #endif
   1765 			}
   1766 #endif
   1767 			if ((at2->req_status & ATIO_SENSEVALID) == 0) {
   1768 				ct2->req_m.mode1.req_sense_len = 18;
   1769 				ct2->req_m.mode1.req_scsi_status |= 2;
   1770 				ct2->req_m.mode1.req_response[0] = 0x70;
   1771 				ct2->req_m.mode1.req_response[2] = 0x2;
   1772 			} else {
   1773 				ct2->req_m.mode1.req_sense_len = 18;
   1774 				ct2->req_m.mode1.req_scsi_status |=
   1775 				    at2->req_scsi_status;
   1776 				memcpy(ct2->req_m.mode1.req_response,
   1777 				    at2->req_sense, sizeof (at2->req_sense));
   1778 			}
   1779 			break;
   1780 		}
   1781 		reqsize = sizeof (*ct2);
   1782 		ireqp = ct2;
   1783 		break;
   1784 	}
   1785 	case RQSTYPE_CTIO2:
   1786 	{
   1787 		ispatiot2_t *at2;
   1788 		ispctiot2_t *ct2 = (ispctiot2_t *) sp;
   1789 		PRINTF("%s: CTIO2 returned status 0x%x\n", isp->isp_name,
   1790 		    ct2->req_status);
   1791 		/*
   1792 	 	 * Return the ATIO to the board.
   1793 		 */
   1794 		at2 = (ispatiot2_t *) sp;
   1795 		at2->req_header.rqs_entry_type = RQSTYPE_ATIO2;
   1796 		at2->req_header.rqs_entry_count = 1;
   1797 		at2->req_header.rqs_flags = 0;
   1798 		at2->req_header.rqs_seqno = isp->isp_seqno++;
   1799 		at2->req_status = 1;
   1800 		reqsize = sizeof (*at2);
   1801 		ireqp = at2;
   1802 		break;
   1803 	}
   1804 #undef	atio
   1805 #undef	at2io
   1806 #undef	ctio
   1807 #undef	ct2io
   1808 #undef	lunen
   1809 #undef	inot
   1810 #undef	inot_fc
   1811 #undef	nack
   1812 #undef	nack_fc
   1813 #endif
   1814 	default:
   1815 		PRINTF("%s: other response type %x\n", isp->isp_name,
   1816 		    sp->req_header.rqs_entry_type);
   1817 		break;
   1818 	}
   1819 	if (reqsize) {
   1820 		void *reqp;
   1821 		optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
   1822 		iptr = isp->isp_reqidx;
   1823 		reqp = (void *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
   1824 		iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
   1825 		if (iptr == optr) {
   1826 			PRINTF("%s: Request Queue Overflow other response\n",
   1827 			    isp->isp_name);
   1828 		} else {
   1829 			memcpy(reqp, ireqp, reqsize);
   1830 			ISP_WRITE(isp, INMAILBOX4, iptr);
   1831 			isp->isp_reqidx = iptr;
   1832 		}
   1833 	}
   1834 	return (0);
   1835 }
   1836 
   1837 #ifdef	ISP_TARGET_MODE
   1838 
   1839 static void isp_tmd_newcmd_dflt __P((void *, tmd_cmd_t *));
   1840 static void isp_tmd_event_dflt __P((void *, int));
   1841 static void isp_tmd_notify_dflt __P((void *, tmd_notify_t *));
   1842 
   1843 static void isp_tgt_data_xfer __P ((tmd_cmd_t *));
   1844 static void isp_tgt_endcmd __P ((tmd_cmd_t *, u_int8_t));
   1845 static void isp_tgt_done __P ((tmd_cmd_t *));
   1846 
   1847 static void
   1848 isp_tmd_newcmd_dflt(arg0, cmdp)
   1849 	void *arg0;
   1850 	tmd_cmd_t *cmdp;
   1851 {
   1852 }
   1853 
   1854 static void
   1855 isp_tmd_event_dflt(arg0, event)
   1856 	void *arg0;
   1857 	int event;
   1858 {
   1859 }
   1860 
   1861 static void
   1862 isp_tmd_notify_dflt(arg0, npt)
   1863 	void *arg0;
   1864 	tmd_notify_t *npt;
   1865 {
   1866 }
   1867 
   1868 /*
   1869  * Locks held, and ints disabled (if FC).
   1870  *
   1871  * XXX: SETUP ONLY FOR INITIAL ENABLING RIGHT NOW
   1872  */
   1873 static int
   1874 isp_modify_lun(isp, lun, icnt, ccnt)
   1875 	struct ispsoftc *isp;
   1876 	int lun;	/* logical unit to enable, modify, or disable */
   1877 	int icnt;	/* immediate notify count */
   1878 	int ccnt;	/* command count */
   1879 {
   1880 	isplun_t *ip = NULL;
   1881 	u_int8_t iptr, optr;
   1882 
   1883 	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
   1884 	iptr = isp->isp_reqidx;
   1885 	ip = (isplun_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
   1886 	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
   1887 	if (iptr == optr) {
   1888 		PRINTF("%s: Request Queue Overflow in isp_modify_lun\n",
   1889 		    isp->isp_name);
   1890 		return (-1);
   1891 	}
   1892 
   1893 	MEMZERO((void *) ip, sizeof (*ip));
   1894 	ip->req_header.rqs_entry_type = RQSTYPE_ENABLE_LUN;
   1895 	ip->req_header.rqs_entry_count = 1;
   1896 	ip->req_header.rqs_seqno = isp->isp_seqno++;
   1897 	ip->req_handle = RQSTYPE_ENABLE_LUN;
   1898 	if (isp->isp_type & ISP_HA_SCSI) {
   1899 		ip->req_lun = lun;
   1900 	}
   1901 	ip->req_cmdcount = ccnt;
   1902 	ip->req_imcount = icnt;
   1903 	ip->req_timeout = 0;	/* default 30 seconds */
   1904 	ISP_WRITE(isp, INMAILBOX4, iptr);
   1905 	isp->isp_reqidx = iptr;
   1906 	return (0);
   1907 }
   1908 
   1909 static void
   1910 isp_notify_ack(isp, ptrp)
   1911 	struct ispsoftc *isp;
   1912 	void *ptrp;
   1913 {
   1914 	void *reqp;
   1915 	u_int8_t iptr, optr;
   1916 	union {
   1917 		na_fcentry_t _naf;
   1918 		na_entry_t _nas;
   1919 	} un;
   1920 
   1921 	MEMZERO((caddr_t)&un, sizeof (un));
   1922 	un._nas.na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
   1923 	un._nas.na_header.rqs_entry_count = 1;
   1924 
   1925 	if (isp->isp_type & ISP_HA_FC) {
   1926 		na_fcentry_t *na = &un._nas;
   1927 		if (ptrp) {
   1928 			in_fcentry_t *inp = ptrp;
   1929 			na->na_iid = inp->in_iid;
   1930 			na->na_lun = inp->in_lun;
   1931 			na->na_task_flags = inp->in_task_flags;
   1932 			na->na_seqid = inp->in_seqid;
   1933 			na->na_status = inp->in_status;
   1934 		} else {
   1935 			na->na_flags = NAFC_RST_CLRD;
   1936 		}
   1937 	} else {
   1938 		na_entry_t *na = &un._nas;
   1939 		if (ptrp) {
   1940 			in_entry_t *inp = ptrp;
   1941 			na->na_iid = inp->in_iid;
   1942 			na->na_lun = inp->in_lun;
   1943 			na->na_tgt = inp->in_tgt;
   1944 			na->na_seqid = inp->in_seqid;
   1945 		} else {
   1946 			na->na_flags = NA_RST_CLRD;
   1947 		}
   1948 	}
   1949 	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
   1950 	iptr = isp->isp_reqidx;
   1951 	reqp = (void *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
   1952 	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
   1953 	if (iptr == optr) {
   1954 		PRINTF("%s: Request Queue Overflow For isp_notify_ack\n",
   1955 		    isp->isp_name);
   1956 	} else {
   1957 		memcpy(reqp, ireqp, sizeof (un));
   1958 		ISP_WRITE(isp, INMAILBOX4, iptr);
   1959 		isp->isp_reqidx = iptr;
   1960 	}
   1961 }
   1962 
   1963 /*
   1964  * These are dummy stubs for now until the outside framework is plugged in.
   1965  */
   1966 
   1967 static void
   1968 isp_handle_atio (isp, aep)
   1969 	struct ispsoftc *isp;
   1970 	at_entry_t *aep;
   1971 {
   1972 	int status, connected;
   1973 	tmd_cmd_t local, *cdp = &local;
   1974 
   1975 	/*
   1976 	 * Get the ATIO status and see if we're still connected.
   1977 	 */
   1978 	status = aep->at_status;
   1979 	connected = ((aep->at_flags & AT_NODISC) != 0);
   1980 
   1981 	PRINTF("%s: ATIO status=0x%x, connected=%d\n", isp->isp_name,
   1982 	    status, connected);
   1983 
   1984 	/*
   1985 	 * The firmware status (except for the SenseValid bit) indicates
   1986 	 * why this ATIO was sent to us.
   1987 	 * If SenseValid is set, the firware has recommended Sense Data.
   1988 	 * If the Disconnects Disabled bit is set in the flags field,
   1989 	 * we're still connected on the SCSI bus - i.e. the initiator
   1990 	 * did not set DiscPriv in the identify message. We don't care
   1991 	 * about this so it's ignored.
   1992 	 */
   1993 	switch(status & ~TGTSVALID) {
   1994 	case AT_PATH_INVALID:
   1995 		/*
   1996 		 * ATIO rejected by the firmware due to disabled lun.
   1997 		 */
   1998 		PRINTF("%s: Firmware rejected ATIO for disabled lun %d\n",
   1999 		    isp->isp_name, aep->at_lun);
   2000 		break;
   2001 
   2002 	case AT_PHASE_ERROR:
   2003 		/*
   2004 		 * Bus Pase Sequence error.
   2005 		 *
   2006 		 * The firmware should have filled in the correct
   2007 		 * sense data.
   2008 		 */
   2009 
   2010 
   2011 		if (status & TGTSVALID) {
   2012 			memcpy(&cdp->cd_sensedata, aep->at_sense,
   2013 			    sizeof (cdp->cd_sensedata));
   2014 			PRINTF("%s: Bus Phase Sequence error key 0x%x\n",
   2015 			    isp->isp_name, cdp->cd_sensedata[2] & 0xf);
   2016 		} else {
   2017 			PRINTF("%s: Bus Phase Sequence With No Sense\n",
   2018 			    isp->isp_name);
   2019 		}
   2020 		(*isp->isp_tmd_newcmd)(isp, cdp);
   2021 		break;
   2022 
   2023 	case AT_NOCAP:
   2024 		/*
   2025 		 * Requested Capability not available
   2026 		 * We sent an ATIO that overflowed the firmware's
   2027 		 * command resource count.
   2028 		 */
   2029 		PRINTF("%s: Firmware rejected ATIO, command count overflow\n",
   2030 		    isp->isp_name);
   2031 		break;
   2032 
   2033 	case AT_BDR_MSG:
   2034 		/*
   2035 		 * If we send an ATIO to the firmware to increment
   2036 		 * its command resource count, and the firmware is
   2037 		 * recovering from a Bus Device Reset, it returns
   2038 		 * the ATIO with this status.
   2039 		 */
   2040 		PRINTF("%s: ATIO returned with BDR received\n", isp->isp_name);
   2041 		break;
   2042 
   2043 	case AT_CDB:
   2044 		/*
   2045 		 * New CDB
   2046 		 */
   2047 		cdp->cd_hba = isp;
   2048 		cdp->cd_iid = aep->at_iid;
   2049 		cdp->cd_tgt = aep->at_tgt;
   2050 		cdp->cd_lun = aep->at_lun;
   2051 		cdp->cd_tagtype = aep->at_tag_type;
   2052 		cdp->cd_tagval = aep->at_tag_val;
   2053 		memcpy(cdp->cd_cdb, aep->at_cdb, 16);
   2054 		PRINTF("%s: CDB 0x%x itl %d/%d/%d\n", isp->isp_name,
   2055 		    cdp->cd_cdb[0], cdp->cd_iid, cdp->cd_tgt, cdp->cd_lun);
   2056 		(*isp->isp_tmd_newcmd)(isp, cdp);
   2057 		break;
   2058 
   2059 	default:
   2060 		PRINTF("%s: Unknown status (0x%x) in ATIO\n",
   2061 		    isp->isp_name, status);
   2062 		cdp->cd_hba = isp;
   2063 		cdp->cd_iid = aep->at_iid;
   2064 		cdp->cd_tgt = aep->at_tgt;
   2065 		cdp->cd_lun = aep->at_lun;
   2066 		cdp->cd_tagtype = aep->at_tag_type;
   2067 		cdp->cd_tagval = aep->at_tag_val;
   2068 		isp_tgtcmd_done(cdp);
   2069 		break;
   2070 	}
   2071 }
   2072 
   2073 static void
   2074 isp_handle_atio2(isp, aep)
   2075 	struct ispsoftc *isp;
   2076 	at2_entry_t *aep;
   2077 {
   2078 	int status;
   2079 	tmd_cmd_t local, *cdp = &local;
   2080 
   2081 	/*
   2082 	 * Get the ATIO2 status.
   2083 	 */
   2084 	status = aep->at_status;
   2085 	PRINTD("%s: ATIO2 status=0x%x\n", status);
   2086 
   2087 	/*
   2088 	 * The firmware status (except for the SenseValid bit) indicates
   2089 	 * why this ATIO was sent to us.
   2090 	 * If SenseValid is set, the firware has recommended Sense Data.
   2091 	 */
   2092 	switch(status & ~TGTSVALID) {
   2093 	case AT_PATH_INVALID:
   2094 		/*
   2095 		 * ATIO rejected by the firmware due to disabled lun.
   2096 		 */
   2097 		PRINTF("%s: Firmware rejected ATIO2 for disabled lun %d\n",
   2098 		    isp->isp_name, aep->at_lun);
   2099 		break;
   2100 
   2101 	case AT_NOCAP:
   2102 		/*
   2103 		 * Requested Capability not available
   2104 		 * We sent an ATIO that overflowed the firmware's
   2105 		 * command resource count.
   2106 		 */
   2107 		PRINTF("%s: Firmware rejected ATIO2, command count overflow\n",
   2108 		    isp->isp_name);
   2109 		break;
   2110 
   2111 	case AT_BDR_MSG:
   2112 		/*
   2113 		 * If we send an ATIO to the firmware to increment
   2114 		 * its command resource count, and the firmware is
   2115 		 * recovering from a Bus Device Reset, it returns
   2116 		 * the ATIO with this status.
   2117 		 */
   2118 		PRINTF("%s: ATIO2 returned with BDR rcvd\n", isp->isp_name);
   2119 		break;
   2120 
   2121 	case AT_CDB:
   2122 		/*
   2123 		 * New CDB
   2124 		 */
   2125 		cdp->cd_hba = isp;
   2126 		cdp->cd_iid = aep->at_iid;
   2127 		cdp->cd_tgt = 0;
   2128 		cdp->cd_lun = aep->at_lun;
   2129 		memcpy(cdp->cd_cdb, aep->at_cdb, 16);
   2130 		cdp->cd_rxid = aep->at_rxid;
   2131 		cdp->cp_origdlen = aep->at_datalen;
   2132 		cdp->cp_totbytes = 0;
   2133 		PRINTF("%s: CDB 0x%x rx_id 0x%x itl %d/%d/%d dlen %d\n",
   2134 		    isp->isp_name, cdp->cd_cdb[0], cdp->cd_tagval, cdp->cd_iid,
   2135 		    cdp->cd_tgt, cdp->cd_lun, aep->at_datalen);
   2136 		(*isp->isp_tmd_newcmd)(isp, cdp);
   2137 		break;
   2138 
   2139 	default:
   2140 		PRINTF("%s: Unknown status (0x%x) in ATIO2\n",
   2141 		    isp->isp_name, status);
   2142 		cdp->cd_hba = isp;
   2143 		cdp->cd_iid = aep->at_iid;
   2144 		cdp->cd_tgt = aep->at_tgt;
   2145 		cdp->cd_lun = aep->at_lun;
   2146 		cdp->cp_rxid = aep->at_rxid;
   2147 		isp_tgtcmd_done(cdp);
   2148 		break;
   2149 	}
   2150 }
   2151 
   2152 static void
   2153 isp_handle_ctio(isp, cep)
   2154 	struct ispsoftc *isp;
   2155 	ct_entry_t *aep;
   2156 {
   2157 }
   2158 
   2159 static void
   2160 isp_handle_ctio2(isp, cep)
   2161 	struct ispsoftc *isp;
   2162 	at2_entry_t *aep;
   2163 {
   2164 }
   2165 #endif
   2166 
   2167 static void
   2168 isp_parse_status(isp, sp, xs)
   2169 	struct ispsoftc *isp;
   2170 	ispstatusreq_t *sp;
   2171 	ISP_SCSI_XFER_T *xs;
   2172 {
   2173 	switch (sp->req_completion_status) {
   2174 	case RQCS_COMPLETE:
   2175 		XS_SETERR(xs, HBA_NOERROR);
   2176 		return;
   2177 
   2178 	case RQCS_INCOMPLETE:
   2179 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   2180 			IDPRINTF(3, ("%s: Selection Timeout for target %d\n",
   2181 			    isp->isp_name, XS_TGT(xs)));
   2182 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2183 			return;
   2184 		}
   2185 		PRINTF("%s: command incomplete for target %d lun %d, state "
   2186 		    "0x%x\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs),
   2187 		    sp->req_state_flags);
   2188 		break;
   2189 
   2190 	case RQCS_DMA_ERROR:
   2191 		PRINTF("%s: DMA error for command on target %d, lun %d\n",
   2192 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2193 		break;
   2194 
   2195 	case RQCS_TRANSPORT_ERROR:
   2196 		PRINTF("%s: transport error\n", isp->isp_name);
   2197 		isp_prtstst(sp);
   2198 		break;
   2199 
   2200 	case RQCS_RESET_OCCURRED:
   2201 		IDPRINTF(2, ("%s: bus reset destroyed command for target %d "
   2202 		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
   2203 		isp->isp_sendmarker = 1;
   2204 		XS_SETERR(xs, HBA_BUSRESET);
   2205 		return;
   2206 
   2207 	case RQCS_ABORTED:
   2208 		PRINTF("%s: command aborted for target %d lun %d\n",
   2209 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2210 		isp->isp_sendmarker = 1;
   2211 		XS_SETERR(xs, HBA_ABORTED);
   2212 		return;
   2213 
   2214 	case RQCS_TIMEOUT:
   2215 		IDPRINTF(2, ("%s: command timed out for target %d lun %d\n",
   2216 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
   2217 		XS_SETERR(xs, HBA_CMDTIMEOUT);
   2218 		return;
   2219 
   2220 	case RQCS_DATA_OVERRUN:
   2221 		if (isp->isp_type & ISP_HA_FC) {
   2222 			XS_RESID(xs) = sp->req_resid;
   2223 			break;
   2224 		}
   2225 		XS_SETERR(xs, HBA_DATAOVR);
   2226 		return;
   2227 
   2228 	case RQCS_COMMAND_OVERRUN:
   2229 		PRINTF("%s: command overrun for command on target %d, lun %d\n",
   2230 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2231 		break;
   2232 
   2233 	case RQCS_STATUS_OVERRUN:
   2234 		PRINTF("%s: status overrun for command on target %d, lun %d\n",
   2235 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2236 		break;
   2237 
   2238 	case RQCS_BAD_MESSAGE:
   2239 		PRINTF("%s: message not COMMAND COMPLETE after status on "
   2240 		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
   2241 		    XS_LUN(xs));
   2242 		break;
   2243 
   2244 	case RQCS_NO_MESSAGE_OUT:
   2245 		PRINTF("%s: No MESSAGE OUT phase after selection on "
   2246 		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
   2247 		    XS_LUN(xs));
   2248 		break;
   2249 
   2250 	case RQCS_EXT_ID_FAILED:
   2251 		PRINTF("%s: EXTENDED IDENTIFY failed on target %d, lun %d\n",
   2252 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2253 		break;
   2254 
   2255 	case RQCS_IDE_MSG_FAILED:
   2256 		PRINTF("%s: target %d lun %d rejected INITIATOR DETECTED "
   2257 		    "ERROR message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2258 		break;
   2259 
   2260 	case RQCS_ABORT_MSG_FAILED:
   2261 		PRINTF("%s: target %d lun %d rejected ABORT message\n",
   2262 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2263 		break;
   2264 
   2265 	case RQCS_REJECT_MSG_FAILED:
   2266 		PRINTF("%s: target %d lun %d rejected MESSAGE REJECT message\n",
   2267 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2268 		break;
   2269 
   2270 	case RQCS_NOP_MSG_FAILED:
   2271 		PRINTF("%s: target %d lun %d rejected NOP message\n",
   2272 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2273 		break;
   2274 
   2275 	case RQCS_PARITY_ERROR_MSG_FAILED:
   2276 		PRINTF("%s: target %d lun %d rejected MESSAGE PARITY ERROR "
   2277 		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2278 		break;
   2279 
   2280 	case RQCS_DEVICE_RESET_MSG_FAILED:
   2281 		PRINTF("%s: target %d lun %d rejected BUS DEVICE RESET "
   2282 		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2283 		break;
   2284 
   2285 	case RQCS_ID_MSG_FAILED:
   2286 		PRINTF("%s: target %d lun %d rejected IDENTIFY "
   2287 		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2288 		break;
   2289 
   2290 	case RQCS_UNEXP_BUS_FREE:
   2291 		PRINTF("%s: target %d lun %d had an unexpected bus free\n",
   2292 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2293 		break;
   2294 
   2295 	case RQCS_DATA_UNDERRUN:
   2296 		if (isp->isp_type & ISP_HA_FC) {
   2297 			XS_RESID(xs) = sp->req_resid;
   2298 			/* an UNDERRUN is not a botch ??? */
   2299 		}
   2300 		XS_SETERR(xs, HBA_NOERROR);
   2301 		return;
   2302 
   2303 	case RQCS_XACT_ERR1:
   2304 		PRINTF("%s: HBA attempted queued transaction with disconnect "
   2305 		    "not set for target %d lun %d\n", isp->isp_name, XS_TGT(xs),
   2306 		    XS_LUN(xs));
   2307 		break;
   2308 
   2309 	case RQCS_XACT_ERR2:
   2310 		PRINTF("%s: HBA attempted queued transaction to target "
   2311 		    "routine %d on target %d\n", isp->isp_name, XS_LUN(xs),
   2312 		    XS_TGT(xs));
   2313 		break;
   2314 
   2315 	case RQCS_XACT_ERR3:
   2316 		PRINTF("%s: HBA attempted queued transaction for target %d lun "
   2317 		    "%d when queueing disabled\n", isp->isp_name, XS_TGT(xs),
   2318 		    XS_LUN(xs));
   2319 		break;
   2320 
   2321 	case RQCS_BAD_ENTRY:
   2322 		PRINTF("%s: invalid IOCB entry type detected\n", isp->isp_name);
   2323 		break;
   2324 
   2325 	case RQCS_QUEUE_FULL:
   2326 		PRINTF("%s: internal queues full for target %d lun %d "
   2327 		    "status 0x%x\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs),
   2328 		    XS_STS(xs));
   2329 		/*
   2330 		 * If QFULL or some other status byte is set, then this
   2331 		 * isn't an error, per se.
   2332 		 */
   2333 		if (XS_STS(xs) != 0) {
   2334 			XS_SETERR(xs, HBA_NOERROR);
   2335 			return;
   2336 		}
   2337 		break;
   2338 
   2339 	case RQCS_PHASE_SKIPPED:
   2340 		PRINTF("%s: SCSI phase skipped (e.g., COMMAND COMPLETE w/o "
   2341 		    "STATUS phase) for target %d lun %d\n", isp->isp_name,
   2342 		    XS_TGT(xs), XS_LUN(xs));
   2343 		break;
   2344 
   2345 	case RQCS_ARQS_FAILED:
   2346 		PRINTF("%s: Auto Request Sense failed for target %d lun %d\n",
   2347 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2348 		XS_SETERR(xs, HBA_ARQFAIL);
   2349 		return;
   2350 
   2351 	case RQCS_WIDE_FAILED:
   2352 		PRINTF("%s: Wide Negotiation failed for target %d lun %d\n",
   2353 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2354 		if (isp->isp_type & ISP_HA_SCSI) {
   2355 			sdparam *sdp = isp->isp_param;
   2356 			isp->isp_update = 1;
   2357 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   2358 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
   2359 		}
   2360 		XS_SETERR(xs, HBA_NOERROR);
   2361 		return;
   2362 
   2363 	case RQCS_SYNCXFER_FAILED:
   2364 		PRINTF("%s: SDTR Message failed for target %d lun %d\n",
   2365 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2366 		if (isp->isp_type & ISP_HA_SCSI) {
   2367 			sdparam *sdp = isp->isp_param;
   2368 			isp->isp_update = 1;
   2369 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   2370 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
   2371 		}
   2372 		break;
   2373 
   2374 	case RQCS_LVD_BUSERR:
   2375 		PRINTF("%s: Bad LVD Bus condition while talking to target %d "
   2376 		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
   2377 		break;
   2378 
   2379 	case RQCS_PORT_UNAVAILABLE:
   2380 		/*
   2381 		 * No such port on the loop. Moral equivalent of SELTIMEO
   2382 		 */
   2383 		IDPRINTF(3, ("%s: Port Unavailable for target %d\n",
   2384 		    isp->isp_name, XS_TGT(xs)));
   2385 		XS_SETERR(xs, HBA_SELTIMEOUT);
   2386 		return;
   2387 
   2388 	case RQCS_PORT_LOGGED_OUT:
   2389 		/*
   2390 		 * It was there (maybe)- treat as a selection timeout.
   2391 		 */
   2392 		PRINTF("%s: port logout for target %d\n",
   2393 			isp->isp_name, XS_TGT(xs));
   2394 		XS_SETERR(xs, HBA_SELTIMEOUT);
   2395 		return;
   2396 
   2397 	case RQCS_PORT_CHANGED:
   2398 		PRINTF("%s: port changed for target %d\n",
   2399 			isp->isp_name, XS_TGT(xs));
   2400 		break;
   2401 
   2402 	case RQCS_PORT_BUSY:
   2403 		PRINTF("%s: port busy for target %d\n",
   2404 			isp->isp_name, XS_TGT(xs));
   2405 		XS_SETERR(xs, HBA_TGTBSY);
   2406 		return;
   2407 
   2408 	default:
   2409 		PRINTF("%s: comp status %x\n", isp->isp_name,
   2410 		       sp->req_completion_status);
   2411 		break;
   2412 	}
   2413 	XS_SETERR(xs, HBA_BOTCH);
   2414 }
   2415 
   2416 #define	HINIB(x)			((x) >> 0x4)
   2417 #define	LONIB(x)			((x)  & 0xf)
   2418 #define MAKNIB(a, b)			(((a) << 4) | (b))
   2419 static u_int8_t mbpcnt[] = {
   2420 	MAKNIB(1, 1),	/* 0x00: MBOX_NO_OP */
   2421 	MAKNIB(5, 5),	/* 0x01: MBOX_LOAD_RAM */
   2422 	MAKNIB(2, 0),	/* 0x02: MBOX_EXEC_FIRMWARE */
   2423 	MAKNIB(5, 5),	/* 0x03: MBOX_DUMP_RAM */
   2424 	MAKNIB(3, 3),	/* 0x04: MBOX_WRITE_RAM_WORD */
   2425 	MAKNIB(2, 3),	/* 0x05: MBOX_READ_RAM_WORD */
   2426 	MAKNIB(6, 6),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   2427 	MAKNIB(2, 3),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   2428 	MAKNIB(1, 3),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   2429 	MAKNIB(0, 0),	/* 0x09: */
   2430 	MAKNIB(0, 0),	/* 0x0a: */
   2431 	MAKNIB(0, 0),	/* 0x0b: */
   2432 	MAKNIB(0, 0),	/* 0x0c: */
   2433 	MAKNIB(0, 0),	/* 0x0d: */
   2434 	MAKNIB(1, 2),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   2435 	MAKNIB(0, 0),	/* 0x0f: */
   2436 	MAKNIB(5, 5),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   2437 	MAKNIB(6, 6),	/* 0x11: MBOX_INIT_RES_QUEUE */
   2438 	MAKNIB(4, 4),	/* 0x12: MBOX_EXECUTE_IOCB */
   2439 	MAKNIB(2, 2),	/* 0x13: MBOX_WAKE_UP	*/
   2440 	MAKNIB(1, 6),	/* 0x14: MBOX_STOP_FIRMWARE */
   2441 	MAKNIB(4, 4),	/* 0x15: MBOX_ABORT */
   2442 	MAKNIB(2, 2),	/* 0x16: MBOX_ABORT_DEVICE */
   2443 	MAKNIB(3, 3),	/* 0x17: MBOX_ABORT_TARGET */
   2444 	MAKNIB(2, 2),	/* 0x18: MBOX_BUS_RESET */
   2445 	MAKNIB(2, 3),	/* 0x19: MBOX_STOP_QUEUE */
   2446 	MAKNIB(2, 3),	/* 0x1a: MBOX_START_QUEUE */
   2447 	MAKNIB(2, 3),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   2448 	MAKNIB(2, 3),	/* 0x1c: MBOX_ABORT_QUEUE */
   2449 	MAKNIB(2, 4),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   2450 	MAKNIB(0, 0),	/* 0x1e: */
   2451 	MAKNIB(1, 3),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   2452 	MAKNIB(1, 3),	/* 0x20: MBOX_GET_INIT_SCSI_ID, MBOX_GET_LOOP_ID */
   2453 	MAKNIB(1, 2),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   2454 	MAKNIB(1, 3),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   2455 	MAKNIB(1, 2),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   2456 	MAKNIB(1, 2),	/* 0x24: MBOX_GET_CLOCK_RATE */
   2457 	MAKNIB(1, 2),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   2458 	MAKNIB(1, 2),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   2459 	MAKNIB(1, 3),	/* 0x27: MBOX_GET_PCI_PARAMS */
   2460 	MAKNIB(2, 4),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   2461 	MAKNIB(2, 4),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   2462 	MAKNIB(0, 0),	/* 0x2a: */
   2463 	MAKNIB(0, 0),	/* 0x2b: */
   2464 	MAKNIB(0, 0),	/* 0x2c: */
   2465 	MAKNIB(0, 0),	/* 0x2d: */
   2466 	MAKNIB(0, 0),	/* 0x2e: */
   2467 	MAKNIB(0, 0),	/* 0x2f: */
   2468 	MAKNIB(2, 2),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   2469 	MAKNIB(2, 2),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   2470 	MAKNIB(3, 3),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   2471 	MAKNIB(2, 2),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   2472 	MAKNIB(2, 2),	/* 0x34: MBOX_SET_CLOCK_RATE */
   2473 	MAKNIB(2, 2),	/* 0x35: MBOX_SET_ACTIVE_NEG_STATE */
   2474 	MAKNIB(2, 2),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   2475 	MAKNIB(3, 3),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   2476 	MAKNIB(4, 4),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   2477 	MAKNIB(4, 4),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   2478 	MAKNIB(0, 0),	/* 0x3a: */
   2479 	MAKNIB(0, 0),	/* 0x3b: */
   2480 	MAKNIB(0, 0),	/* 0x3c: */
   2481 	MAKNIB(0, 0),	/* 0x3d: */
   2482 	MAKNIB(0, 0),	/* 0x3e: */
   2483 	MAKNIB(0, 0),	/* 0x3f: */
   2484 	MAKNIB(1, 2),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   2485 	MAKNIB(6, 1),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   2486 	MAKNIB(2, 3),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   2487 	MAKNIB(0, 0),	/* 0x43: */
   2488 	MAKNIB(0, 0),	/* 0x44: */
   2489 	MAKNIB(0, 0),	/* 0x45: */
   2490 	MAKNIB(0, 0),	/* 0x46: */
   2491 	MAKNIB(0, 0),	/* 0x47: */
   2492 	MAKNIB(0, 0),	/* 0x48: */
   2493 	MAKNIB(0, 0),	/* 0x49: */
   2494 	MAKNIB(0, 0),	/* 0x4a: */
   2495 	MAKNIB(0, 0),	/* 0x4b: */
   2496 	MAKNIB(0, 0),	/* 0x4c: */
   2497 	MAKNIB(0, 0),	/* 0x4d: */
   2498 	MAKNIB(0, 0),	/* 0x4e: */
   2499 	MAKNIB(0, 0),	/* 0x4f: */
   2500 	MAKNIB(0, 0),	/* 0x50: */
   2501 	MAKNIB(0, 0),	/* 0x51: */
   2502 	MAKNIB(0, 0),	/* 0x52: */
   2503 	MAKNIB(0, 0),	/* 0x53: */
   2504 	MAKNIB(8, 0),	/* 0x54: MBOX_EXEC_COMMAND_IOCB_A64 */
   2505 	MAKNIB(0, 0),	/* 0x55: */
   2506 	MAKNIB(0, 0),	/* 0x56: */
   2507 	MAKNIB(0, 0),	/* 0x57: */
   2508 	MAKNIB(0, 0),	/* 0x58: */
   2509 	MAKNIB(0, 0),	/* 0x59: */
   2510 	MAKNIB(0, 0),	/* 0x5a: */
   2511 	MAKNIB(0, 0),	/* 0x5b: */
   2512 	MAKNIB(0, 0),	/* 0x5c: */
   2513 	MAKNIB(0, 0),	/* 0x5d: */
   2514 	MAKNIB(0, 0),	/* 0x5e: */
   2515 	MAKNIB(0, 0),	/* 0x5f: */
   2516 	MAKNIB(8, 6),	/* 0x60: MBOX_INIT_FIRMWARE */
   2517 	MAKNIB(0, 0),	/* 0x60: MBOX_GET_INIT_CONTROL_BLOCK  (FORMAT?) */
   2518 	MAKNIB(2, 1),	/* 0x62: MBOX_INIT_LIP */
   2519 	MAKNIB(8, 1),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   2520 	MAKNIB(8, 1),	/* 0x64: MBOX_GET_PORT_DB */
   2521 	MAKNIB(3, 1),	/* 0x65: MBOX_CLEAR_ACA */
   2522 	MAKNIB(3, 1),	/* 0x66: MBOX_TARGET_RESET */
   2523 	MAKNIB(3, 1),	/* 0x67: MBOX_CLEAR_TASK_SET */
   2524 	MAKNIB(3, 1),	/* 0x69: MBOX_ABORT_TASK_SET */
   2525 	MAKNIB(1, 2)	/* 0x69: MBOX_GET_FW_STATE */
   2526 };
   2527 #define	NMBCOM	(sizeof (mbpcnt) / sizeof (mbpcnt[0]))
   2528 
   2529 static void
   2530 isp_mboxcmd(isp, mbp)
   2531 	struct ispsoftc *isp;
   2532 	mbreg_t *mbp;
   2533 {
   2534 	int outparam, inparam;
   2535 	int loops, dld = 0;
   2536 	u_int8_t opcode;
   2537 
   2538 	if (mbp->param[0] == ISP2100_SET_PCI_PARAM) {
   2539 		opcode = mbp->param[0] = MBOX_SET_PCI_PARAMETERS;
   2540 		inparam = 4;
   2541 		outparam = 4;
   2542 		goto command_known;
   2543 	} else if (mbp->param[0] > NMBCOM) {
   2544 		PRINTF("%s: bad command %x\n", isp->isp_name, mbp->param[0]);
   2545 		return;
   2546 	}
   2547 
   2548 	opcode = mbp->param[0];
   2549 	inparam = HINIB(mbpcnt[mbp->param[0]]);
   2550 	outparam =  LONIB(mbpcnt[mbp->param[0]]);
   2551 
   2552 	if (inparam == 0 && outparam == 0) {
   2553 		PRINTF("%s: no parameters for %x\n", isp->isp_name,
   2554 			mbp->param[0]);
   2555 		return;
   2556 	}
   2557 
   2558 
   2559 	/*
   2560 	 * Check for variants
   2561 	 */
   2562 #ifdef	SCCLUN
   2563 	if (isp->isp_type & ISP_HA_FC) {
   2564 		switch (mbp->param[0]) {
   2565 		case MBOX_ABORT:
   2566 			inparam = 7;
   2567 			break;
   2568 		case MBOX_ABORT_DEVICE:
   2569 		case MBOX_START_QUEUE:
   2570 		case MBOX_STOP_QUEUE:
   2571 		case MBOX_SINGLE_STEP_QUEUE:
   2572 		case MBOX_ABORT_QUEUE:
   2573 		case MBOX_GET_DEV_QUEUE_STATUS:
   2574 			inparam = 3;
   2575 			break;
   2576 		default:
   2577 			break;
   2578 		}
   2579 	}
   2580 #endif
   2581 
   2582 command_known:
   2583 
   2584 	/*
   2585 	 * Make sure we can send some words..
   2586 	 */
   2587 
   2588 	loops = MBOX_DELAY_COUNT;
   2589 	while ((ISP_READ(isp, HCCR) & HCCR_HOST_INT) != 0) {
   2590 		SYS_DELAY(100);
   2591 		if (--loops < 0) {
   2592 			PRINTF("%s: isp_mboxcmd timeout #1\n", isp->isp_name);
   2593 			if (dld++) {
   2594 				return;
   2595 			}
   2596 			PRINTF("%s: but we'll try again, isr=%x\n",
   2597 			    isp->isp_name, ISP_READ(isp, BIU_ISR));
   2598 			if (ISP_READ(isp, BIU_SEMA) & 1) {
   2599 				u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
   2600 				if (isp_parse_async(isp, (int) mbox))
   2601 					return;
   2602 				ISP_WRITE(isp, BIU_SEMA, 0);
   2603 			}
   2604 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2605 			goto command_known;
   2606 		}
   2607 	}
   2608 
   2609 	/*
   2610 	 * Write input parameters
   2611 	 */
   2612 	switch (inparam) {
   2613 	case 8: ISP_WRITE(isp, INMAILBOX7, mbp->param[7]); mbp->param[7] = 0;
   2614 	case 7: ISP_WRITE(isp, INMAILBOX6, mbp->param[6]); mbp->param[6] = 0;
   2615 	case 6: ISP_WRITE(isp, INMAILBOX5, mbp->param[5]); mbp->param[5] = 0;
   2616 	case 5: ISP_WRITE(isp, INMAILBOX4, mbp->param[4]); mbp->param[4] = 0;
   2617 	case 4: ISP_WRITE(isp, INMAILBOX3, mbp->param[3]); mbp->param[3] = 0;
   2618 	case 3: ISP_WRITE(isp, INMAILBOX2, mbp->param[2]); mbp->param[2] = 0;
   2619 	case 2: ISP_WRITE(isp, INMAILBOX1, mbp->param[1]); mbp->param[1] = 0;
   2620 	case 1: ISP_WRITE(isp, INMAILBOX0, mbp->param[0]); mbp->param[0] = 0;
   2621 	}
   2622 
   2623 	/*
   2624 	 * Clear semaphore on mailbox registers
   2625 	 */
   2626 	ISP_WRITE(isp, BIU_SEMA, 0);
   2627 
   2628 	/*
   2629 	 * Clear RISC int condition.
   2630 	 */
   2631 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2632 
   2633 	/*
   2634 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   2635 	 */
   2636 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   2637 
   2638 	/*
   2639 	 * Wait until RISC int is set, except 2100
   2640 	 */
   2641 	if ((isp->isp_type & ISP_HA_FC) == 0) {
   2642 		loops = MBOX_DELAY_COUNT;
   2643 		while ((ISP_READ(isp, BIU_ISR) & BIU_ISR_RISC_INT) == 0) {
   2644 			SYS_DELAY(100);
   2645 			if (--loops < 0) {
   2646 				PRINTF("%s: isp_mboxcmd timeout #2\n",
   2647 				    isp->isp_name);
   2648 				return;
   2649 			}
   2650 		}
   2651 	}
   2652 
   2653 	/*
   2654 	 * Check to make sure that the semaphore has been set.
   2655 	 */
   2656 	loops = MBOX_DELAY_COUNT;
   2657 	while ((ISP_READ(isp, BIU_SEMA) & 1) == 0) {
   2658 		SYS_DELAY(100);
   2659 		if (--loops < 0) {
   2660 			PRINTF("%s: isp_mboxcmd timeout #3\n", isp->isp_name);
   2661 			return;
   2662 		}
   2663 	}
   2664 
   2665 	/*
   2666 	 * Make sure that the MBOX_BUSY has gone away
   2667 	 */
   2668 	loops = MBOX_DELAY_COUNT;
   2669 	while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
   2670 		SYS_DELAY(100);
   2671 		if (--loops < 0) {
   2672 			PRINTF("%s: isp_mboxcmd timeout #4\n", isp->isp_name);
   2673 			return;
   2674 		}
   2675 	}
   2676 
   2677 
   2678 	/*
   2679 	 * Pick up output parameters.
   2680 	 */
   2681 	switch (outparam) {
   2682 	case 8: mbp->param[7] = ISP_READ(isp, OUTMAILBOX7);
   2683 	case 7: mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
   2684 	case 6: mbp->param[5] = ISP_READ(isp, OUTMAILBOX5);
   2685 	case 5: mbp->param[4] = ISP_READ(isp, OUTMAILBOX4);
   2686 	case 4: mbp->param[3] = ISP_READ(isp, OUTMAILBOX3);
   2687 	case 3: mbp->param[2] = ISP_READ(isp, OUTMAILBOX2);
   2688 	case 2: mbp->param[1] = ISP_READ(isp, OUTMAILBOX1);
   2689 	case 1: mbp->param[0] = ISP_READ(isp, OUTMAILBOX0);
   2690 	}
   2691 
   2692 	/*
   2693 	 * Clear RISC int.
   2694 	 */
   2695 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2696 
   2697 	/*
   2698 	 * Release semaphore on mailbox registers
   2699 	 */
   2700 	ISP_WRITE(isp, BIU_SEMA, 0);
   2701 
   2702 	/*
   2703 	 * Just to be chatty here...
   2704 	 */
   2705 	switch(mbp->param[0]) {
   2706 	case MBOX_COMMAND_COMPLETE:
   2707 		break;
   2708 	case MBOX_INVALID_COMMAND:
   2709 		IDPRINTF(2, ("%s: mbox cmd %x failed with INVALID_COMMAND\n",
   2710 		    isp->isp_name, opcode));
   2711 		break;
   2712 	case MBOX_HOST_INTERFACE_ERROR:
   2713 		PRINTF("%s: mbox cmd %x failed with HOST_INTERFACE_ERROR\n",
   2714 		    isp->isp_name, opcode);
   2715 		break;
   2716 	case MBOX_TEST_FAILED:
   2717 		PRINTF("%s: mbox cmd %x failed with TEST_FAILED\n",
   2718 		    isp->isp_name, opcode);
   2719 		break;
   2720 	case MBOX_COMMAND_ERROR:
   2721 		PRINTF("%s: mbox cmd %x failed with COMMAND_ERROR\n",
   2722 		    isp->isp_name, opcode);
   2723 		break;
   2724 	case MBOX_COMMAND_PARAM_ERROR:
   2725 		PRINTF("%s: mbox cmd %x failed with COMMAND_PARAM_ERROR\n",
   2726 		    isp->isp_name, opcode);
   2727 		break;
   2728 
   2729 	/*
   2730 	 * Be silent about these...
   2731 	 */
   2732 
   2733 	case ASYNC_LOOP_UP:
   2734 	case ASYNC_LIP_OCCURRED:
   2735 	case ASYNC_PDB_CHANGED:
   2736 		break;
   2737 
   2738 	default:
   2739 		/*
   2740 		 * The expected return of EXEC_FIRMWARE is zero.
   2741 		 */
   2742 		if ((opcode == MBOX_EXEC_FIRMWARE && mbp->param[0] != 0) ||
   2743 		    (opcode != MBOX_EXEC_FIRMWARE)) {
   2744 			PRINTF("%s: mbox cmd %x failed with error %x\n",
   2745 				isp->isp_name, opcode, mbp->param[0]);
   2746 		}
   2747 		break;
   2748 	}
   2749 }
   2750 
   2751 void
   2752 isp_lostcmd(isp, xs)
   2753 	struct ispsoftc *isp;
   2754 	ISP_SCSI_XFER_T *xs;
   2755 {
   2756 	mbreg_t mbs;
   2757 
   2758 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
   2759 	isp_mboxcmd(isp, &mbs);
   2760 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2761 		isp_dumpregs(isp, "couldn't GET FIRMWARE STATUS");
   2762 		return;
   2763 	}
   2764 	if (mbs.param[1]) {
   2765 		PRINTF("%s: %d commands on completion queue\n",
   2766 		       isp->isp_name, mbs.param[1]);
   2767 	}
   2768 	if (XS_NULL(xs))
   2769 		return;
   2770 
   2771 	mbs.param[0] = MBOX_GET_DEV_QUEUE_STATUS;
   2772 	mbs.param[1] = (XS_TGT(xs) << 8) | XS_LUN(xs);
   2773 	isp_mboxcmd(isp, &mbs);
   2774 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2775 		isp_dumpregs(isp, "couldn't GET DEVICE QUEUE STATUS");
   2776 		return;
   2777 	}
   2778 	PRINTF("%s: lost command for target %d lun %d, %d active of %d, "
   2779 		"Queue State: %x\n", isp->isp_name, XS_TGT(xs),
   2780 		XS_LUN(xs), mbs.param[2], mbs.param[3], mbs.param[1]);
   2781 
   2782 	isp_dumpregs(isp, "lost command");
   2783 	/*
   2784 	 * XXX: Need to try and do something to recover.
   2785 	 */
   2786 }
   2787 
   2788 static void
   2789 isp_dumpregs(isp, msg)
   2790 	struct ispsoftc *isp;
   2791 	const char *msg;
   2792 {
   2793 	PRINTF("%s: %s\n", isp->isp_name, msg);
   2794 	if (isp->isp_type & ISP_HA_SCSI)
   2795 		PRINTF("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
   2796 	else
   2797 		PRINTF("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
   2798 	PRINTF(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
   2799 	       ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
   2800 	PRINTF("risc_hccr=%x\n", ISP_READ(isp, HCCR));
   2801 
   2802 	if (isp->isp_type & ISP_HA_SCSI) {
   2803 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
   2804 		PRINTF("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
   2805 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
   2806 			ISP_READ(isp, CDMA_FIFO_STS));
   2807 		PRINTF("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
   2808 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
   2809 			ISP_READ(isp, DDMA_FIFO_STS));
   2810 		PRINTF("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
   2811 			ISP_READ(isp, SXP_INTERRUPT),
   2812 			ISP_READ(isp, SXP_GROSS_ERR),
   2813 			ISP_READ(isp, SXP_PINS_CONTROL));
   2814 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
   2815 	}
   2816 	ISP_DUMPREGS(isp);
   2817 }
   2818 
   2819 static void
   2820 isp_dumpxflist(isp)
   2821 	struct ispsoftc *isp;
   2822 {
   2823 	volatile ISP_SCSI_XFER_T *xs;
   2824 	int i, hdp;
   2825 
   2826 	for (hdp = i = 0; i < RQUEST_QUEUE_LEN; i++) {
   2827 		xs = isp->isp_xflist[i];
   2828 		if (xs == NULL) {
   2829 			continue;
   2830 		}
   2831 		if (hdp == 0) {
   2832 			PRINTF("%s: active requests\n", isp->isp_name);
   2833 			hdp++;
   2834 		}
   2835 		PRINTF(" Active Handle %d: tgt %d lun %d dlen %d\n",
   2836 		    i+1, XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs));
   2837 	}
   2838 }
   2839 
   2840 static void
   2841 isp_fw_state(isp)
   2842 	struct ispsoftc *isp;
   2843 {
   2844 	mbreg_t mbs;
   2845 	if (isp->isp_type & ISP_HA_FC) {
   2846 		int once = 0;
   2847 		fcparam *fcp = isp->isp_param;
   2848 again:
   2849 		mbs.param[0] = MBOX_GET_FW_STATE;
   2850 		isp_mboxcmd(isp, &mbs);
   2851 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2852 			if (mbs.param[0] == ASYNC_LIP_OCCURRED ||
   2853 			    mbs.param[0] == ASYNC_PDB_CHANGED ||
   2854 			    mbs.param[0] == ASYNC_LOOP_UP) {
   2855 				if (once++ < 2) {
   2856 					goto again;
   2857 				}
   2858 			}
   2859 			isp_dumpregs(isp, "GET FIRMWARE STATE failed");
   2860 			return;
   2861 		}
   2862 		fcp->isp_fwstate = mbs.param[1];
   2863 	}
   2864 }
   2865 
   2866 static void
   2867 isp_update(isp)
   2868 	struct ispsoftc *isp;
   2869 {
   2870 	int tgt;
   2871 	mbreg_t mbs;
   2872 	sdparam *sdp;
   2873 
   2874 	isp->isp_update = 0;
   2875 
   2876 	if (isp->isp_type & ISP_HA_FC) {
   2877 		return;
   2878 	}
   2879 
   2880 	sdp = isp->isp_param;
   2881 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   2882 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   2883 			continue;
   2884 		}
   2885 		if (sdp->isp_devparam[tgt].dev_update == 0) {
   2886 			continue;
   2887 		}
   2888 
   2889 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   2890 		mbs.param[1] = tgt << 8;
   2891 		mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
   2892 		mbs.param[3] =
   2893 			(sdp->isp_devparam[tgt].sync_offset << 8) |
   2894 			(sdp->isp_devparam[tgt].sync_period);
   2895 
   2896 		IDPRINTF(3, ("\n%s: tgt %d cflags %x offset %x period %x\n",
   2897 		    isp->isp_name, tgt, mbs.param[2], mbs.param[3] >> 8,
   2898 		    mbs.param[3] & 0xff));
   2899 
   2900 		isp_mboxcmd(isp, &mbs);
   2901 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2902 			PRINTF("%s: failed to change SCSI parameters for "
   2903 			    "target %d\n", isp->isp_name, tgt);
   2904 		} else {
   2905 			char *wt;
   2906 			int x, flags;
   2907 
   2908 			flags = sdp->isp_devparam[tgt].cur_dflags =
   2909 			    sdp->isp_devparam[tgt].dev_flags;
   2910 
   2911 			x = sdp->isp_devparam[tgt].sync_period & 0xff;
   2912 			if (flags & DPARM_SYNC) {
   2913 				if (x == (ISP_20M_SYNCPARMS & 0xff)) {
   2914 					x = 20;
   2915 				} else if (x == (ISP_10M_SYNCPARMS & 0xff)) {
   2916 					x = 10;
   2917 				} else if (x == (ISP_08M_SYNCPARMS & 0xff)) {
   2918 					x = 8;
   2919 				} else if (x == (ISP_05M_SYNCPARMS & 0xff)) {
   2920 					x = 5;
   2921 				} else if (x == (ISP_04M_SYNCPARMS & 0xff)) {
   2922 					x = 4;
   2923 				} else {
   2924 					x = 0;
   2925 				}
   2926 			} else {
   2927 				x = 0;
   2928 			}
   2929 			switch (flags & (DPARM_WIDE|DPARM_TQING)) {
   2930 			case DPARM_WIDE:
   2931 				wt = ", 16 bit wide\n";
   2932 				break;
   2933 			case DPARM_TQING:
   2934 				wt = ", Tagged Queueing Enabled\n";
   2935 				break;
   2936 			case DPARM_WIDE|DPARM_TQING:
   2937 				wt = ", 16 bit wide, Tagged Queueing Enabled\n";
   2938 				break;
   2939 
   2940 			default:
   2941 				wt = "\n";
   2942 				break;
   2943 			}
   2944 			if (x) {
   2945 				IDPRINTF(3, ("%s: Target %d maximum Sync Mode "
   2946 				    "at %dMHz%s", isp->isp_name, tgt, x, wt));
   2947 			} else {
   2948 				IDPRINTF(3, ("%s: Target %d Async Mode%s",
   2949 				    isp->isp_name, tgt, wt));
   2950 			}
   2951 		}
   2952 		sdp->isp_devparam[tgt].dev_update = 0;
   2953 	}
   2954 }
   2955 
   2956 static void
   2957 isp_setdfltparm(isp)
   2958 	struct ispsoftc *isp;
   2959 {
   2960 	int i, use_nvram;
   2961 	mbreg_t mbs;
   2962 	sdparam *sdp;
   2963 
   2964 	/*
   2965 	 * Been there, done that, got the T-shirt...
   2966 	 */
   2967 	if (isp->isp_gotdparms) {
   2968 		IDPRINTF(3, ("%s: already have dparms\n", isp->isp_name));
   2969 		return;
   2970 	}
   2971 	isp->isp_gotdparms = 1;
   2972 
   2973 	use_nvram = (isp_read_nvram(isp) == 0);
   2974 	if (use_nvram) {
   2975 		return;
   2976 	}
   2977 	if (isp->isp_type & ISP_HA_FC) {
   2978 		fcparam *fcp = (fcparam *) isp->isp_param;
   2979 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
   2980 		fcp->isp_maxalloc = 256;
   2981 		fcp->isp_execthrottle = 16;
   2982 		fcp->isp_retry_delay = 5;
   2983 		fcp->isp_retry_count = 0;
   2984 		/*
   2985 		 * It would be nice to fake up a WWN in case we don't
   2986 		 * get one out of NVRAM. Solaris does this for SOCAL
   2987 		 * cards that don't have SBus properties- it sets up
   2988 		 * a WWN based upon the system MAC Address.
   2989 		 */
   2990 		fcp->isp_wwn = 0;
   2991 		return;
   2992 	}
   2993 
   2994 	sdp = (sdparam *) isp->isp_param;
   2995 	mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   2996 	isp_mboxcmd(isp, &mbs);
   2997 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2998 		IDPRINTF(2, ("could not GET ACT NEG STATE\n"));
   2999 		sdp->isp_req_ack_active_neg = 1;
   3000 		sdp->isp_data_line_active_neg = 1;
   3001 	} else {
   3002 		sdp->isp_req_ack_active_neg = (mbs.param[1] >> 4) & 0x1;
   3003 		sdp->isp_data_line_active_neg = (mbs.param[1] >> 5) & 0x1;
   3004 	}
   3005 	for (i = 0; i < MAX_TARGETS; i++) {
   3006 
   3007 		mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   3008 		mbs.param[1] = i << 8;
   3009 		isp_mboxcmd(isp, &mbs);
   3010 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   3011 			PRINTF("%s: can't get SCSI parameters for target %d\n",
   3012 			    isp->isp_name, i);
   3013 			sdp->isp_devparam[i].sync_period = 0;
   3014 			sdp->isp_devparam[i].sync_offset = 0;
   3015 			sdp->isp_devparam[i].dev_flags = DPARM_SAFE_DFLT;
   3016 			continue;
   3017 		}
   3018 		sdp->isp_devparam[i].dev_flags = mbs.param[2];
   3019 
   3020 		/*
   3021 		 * The maximum period we can really see
   3022 		 * here is 100 (decimal), or 400 ns.
   3023 		 * For some unknown reason we sometimes
   3024 		 * get back wildass numbers from the
   3025 		 * boot device's parameters.
   3026 		 *
   3027 		 * XXX: Hmm- this may be based on a different
   3028 		 * XXX: clock rate.
   3029 		 */
   3030 		if ((mbs.param[3] & 0xff) <= 0x64) {
   3031 			sdp->isp_devparam[i].sync_period = mbs.param[3] & 0xff;
   3032 			sdp->isp_devparam[i].sync_offset = mbs.param[3] >> 8;
   3033 		}
   3034 
   3035 		/*
   3036 		 * It is not safe to run Ultra Mode with a clock < 60.
   3037 		 */
   3038 		if (((sdp->isp_clock && sdp->isp_clock < 60) ||
   3039 		    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
   3040 		    (sdp->isp_devparam[i].sync_period ==
   3041 		    (ISP_20M_SYNCPARMS & 0xff))) {
   3042 			sdp->isp_devparam[i].sync_offset =
   3043 				ISP_10M_SYNCPARMS >> 8;
   3044 			sdp->isp_devparam[i].sync_period =
   3045 				ISP_10M_SYNCPARMS & 0xff;
   3046 		}
   3047 
   3048 	}
   3049 
   3050 	/*
   3051 	 * Set Default Host Adapter Parameters
   3052 	 */
   3053 	sdp->isp_cmd_dma_burst_enable = 1;
   3054 	sdp->isp_data_dma_burst_enabl = 1;
   3055 	sdp->isp_fifo_threshold = 0;
   3056 	sdp->isp_initiator_id = 7;
   3057 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   3058 		sdp->isp_async_data_setup = 9;
   3059 	} else {
   3060 		sdp->isp_async_data_setup = 6;
   3061 	}
   3062 	sdp->isp_selection_timeout = 250;
   3063 	sdp->isp_max_queue_depth = 128;
   3064 	sdp->isp_tag_aging = 8;
   3065 	sdp->isp_bus_reset_delay = 3;
   3066 	sdp->isp_retry_count = 0;
   3067 	sdp->isp_retry_delay = 1;
   3068 
   3069 	for (i = 0; i < MAX_TARGETS; i++) {
   3070 		sdp->isp_devparam[i].exc_throttle = 16;
   3071 		sdp->isp_devparam[i].dev_enable = 1;
   3072 	}
   3073 }
   3074 
   3075 /*
   3076  * Re-initialize the ISP and complete all orphaned commands
   3077  * with a 'botched' notice.
   3078  *
   3079  * Locks held prior to coming here.
   3080  */
   3081 
   3082 void
   3083 isp_restart(isp)
   3084 	struct ispsoftc *isp;
   3085 {
   3086 	ISP_SCSI_XFER_T *tlist[RQUEST_QUEUE_LEN], *xs;
   3087 	int i;
   3088 
   3089 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   3090 		tlist[i] = (ISP_SCSI_XFER_T *) isp->isp_xflist[i];
   3091 		isp->isp_xflist[i] = NULL;
   3092 	}
   3093 	isp_reset(isp);
   3094 	if (isp->isp_state == ISP_RESETSTATE) {
   3095 		isp_init(isp);
   3096 		if (isp->isp_state == ISP_INITSTATE) {
   3097 			isp->isp_state = ISP_RUNSTATE;
   3098 		}
   3099 	}
   3100 	if (isp->isp_state != ISP_RUNSTATE) {
   3101 		PRINTF("%s: isp_restart cannot restart ISP\n", isp->isp_name);
   3102 	}
   3103 
   3104 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
   3105 		xs = tlist[i];
   3106 		if (XS_NULL(xs)) {
   3107 			continue;
   3108 		}
   3109 		if (isp->isp_nactive > 0)
   3110 		    isp->isp_nactive--;
   3111 		XS_RESID(xs) = XS_XFRLEN(xs);
   3112 		XS_SETERR(xs, HBA_BUSRESET);
   3113 		XS_CMD_DONE(xs);
   3114 	}
   3115 }
   3116 
   3117 /*
   3118  * Miscellaneous debug statements.
   3119  */
   3120 static void
   3121 isp_prtstst(sp)
   3122 	ispstatusreq_t *sp;
   3123 {
   3124 	PRINTF("states->");
   3125 	if (sp->req_state_flags & RQSF_GOT_BUS)
   3126 		PRINTF("GOT_BUS ");
   3127 	if (sp->req_state_flags & RQSF_GOT_TARGET)
   3128 		PRINTF("GOT_TGT ");
   3129 	if (sp->req_state_flags & RQSF_SENT_CDB)
   3130 		PRINTF("SENT_CDB ");
   3131 	if (sp->req_state_flags & RQSF_XFRD_DATA)
   3132 		PRINTF("XFRD_DATA ");
   3133 	if (sp->req_state_flags & RQSF_GOT_STATUS)
   3134 		PRINTF("GOT_STS ");
   3135 	if (sp->req_state_flags & RQSF_GOT_SENSE)
   3136 		PRINTF("GOT_SNS ");
   3137 	if (sp->req_state_flags & RQSF_XFER_COMPLETE)
   3138 		PRINTF("XFR_CMPLT ");
   3139 	PRINTF("\n");
   3140 	PRINTF("status->");
   3141 	if (sp->req_status_flags & RQSTF_DISCONNECT)
   3142 		PRINTF("Disconnect ");
   3143 	if (sp->req_status_flags & RQSTF_SYNCHRONOUS)
   3144 		PRINTF("Sync_xfr ");
   3145 	if (sp->req_status_flags & RQSTF_PARITY_ERROR)
   3146 		PRINTF("Parity ");
   3147 	if (sp->req_status_flags & RQSTF_BUS_RESET)
   3148 		PRINTF("Bus_Reset ");
   3149 	if (sp->req_status_flags & RQSTF_DEVICE_RESET)
   3150 		PRINTF("Device_Reset ");
   3151 	if (sp->req_status_flags & RQSTF_ABORTED)
   3152 		PRINTF("Aborted ");
   3153 	if (sp->req_status_flags & RQSTF_TIMEOUT)
   3154 		PRINTF("Timeout ");
   3155 	if (sp->req_status_flags & RQSTF_NEGOTIATION)
   3156 		PRINTF("Negotiation ");
   3157 	PRINTF("\n");
   3158 }
   3159 
   3160 static char *
   3161 isp2100_fw_statename(state)
   3162 	int state;
   3163 {
   3164 	switch(state) {
   3165 	case FW_CONFIG_WAIT:	return "Config Wait";
   3166 	case FW_WAIT_AL_PA:	return "Waiting for AL/PA";
   3167 	case FW_WAIT_LOGIN:	return "Wait Login";
   3168 	case FW_READY:		return "Ready";
   3169 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
   3170 	case FW_ERROR:		return "Error";
   3171 	case FW_REINIT:		return "Re-Init";
   3172 	case FW_NON_PART:	return "Nonparticipating";
   3173 	default:		return "eh?";
   3174 	}
   3175 }
   3176 
   3177 /*
   3178  * NVRAM Routines
   3179  */
   3180 
   3181 static int
   3182 isp_read_nvram(isp)
   3183 	struct ispsoftc *isp;
   3184 {
   3185 	int i, amt;
   3186 	u_int8_t csum, minversion;
   3187 	union {
   3188 		u_int8_t _x[ISP2100_NVRAM_SIZE];
   3189 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
   3190 	} _n;
   3191 #define	nvram_data	_n._x
   3192 #define	nvram_words	_n._s
   3193 
   3194 	if (isp->isp_type & ISP_HA_FC) {
   3195 		amt = ISP2100_NVRAM_SIZE;
   3196 		minversion = 1;
   3197 	} else {
   3198 		amt = ISP_NVRAM_SIZE;
   3199 		minversion = 2;
   3200 	}
   3201 
   3202 	/*
   3203 	 * Just read the first two words first to see if we have a valid
   3204 	 * NVRAM to continue reading the rest with.
   3205 	 */
   3206 	for (i = 0; i < 2; i++) {
   3207 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   3208 	}
   3209 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   3210 	    nvram_data[2] != 'P') {
   3211 		if (isp->isp_bustype != ISP_BT_SBUS) {
   3212 			PRINTF("%s: invalid NVRAM header\n", isp->isp_name);
   3213 		}
   3214 		return (-1);
   3215 	}
   3216 	for (i = 2; i < amt>>1; i++) {
   3217 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   3218 	}
   3219 	for (csum = 0, i = 0; i < amt; i++) {
   3220 		csum += nvram_data[i];
   3221 	}
   3222 	if (csum != 0) {
   3223 		PRINTF("%s: invalid NVRAM checksum\n", isp->isp_name);
   3224 		return (-1);
   3225 	}
   3226 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   3227 		PRINTF("%s: version %d NVRAM not understood\n", isp->isp_name,
   3228 		    ISP_NVRAM_VERSION(nvram_data));
   3229 		return (-1);
   3230 	}
   3231 
   3232 	if (isp->isp_type & ISP_HA_SCSI) {
   3233 		sdparam *sdp = (sdparam *) isp->isp_param;
   3234 
   3235 		sdp->isp_fifo_threshold =
   3236 			ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
   3237 			(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
   3238 
   3239 		sdp->isp_initiator_id =
   3240 			ISP_NVRAM_INITIATOR_ID(nvram_data);
   3241 
   3242 		sdp->isp_bus_reset_delay =
   3243 			ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   3244 
   3245 		sdp->isp_retry_count =
   3246 			ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   3247 
   3248 		sdp->isp_retry_delay =
   3249 			ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   3250 
   3251 		sdp->isp_async_data_setup =
   3252 			ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   3253 
   3254 		if (isp->isp_type >= ISP_HA_SCSI_1040) {
   3255 			if (sdp->isp_async_data_setup < 9) {
   3256 				sdp->isp_async_data_setup = 9;
   3257 			}
   3258 		} else {
   3259 			if (sdp->isp_async_data_setup != 6) {
   3260 				sdp->isp_async_data_setup = 6;
   3261 			}
   3262 		}
   3263 
   3264 		sdp->isp_req_ack_active_neg =
   3265 			ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   3266 
   3267 		sdp->isp_data_line_active_neg =
   3268 			ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   3269 
   3270 		sdp->isp_data_dma_burst_enabl =
   3271 			ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   3272 
   3273 		sdp->isp_cmd_dma_burst_enable =
   3274 			ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   3275 
   3276 		sdp->isp_tag_aging =
   3277 			ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   3278 
   3279 		sdp->isp_selection_timeout =
   3280 			ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   3281 
   3282 		sdp->isp_max_queue_depth =
   3283 			ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   3284 
   3285 		sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   3286 
   3287 #if	0
   3288 		PRINTF("%s: fifo_threshold = 0x%x cbena%d dbena%d\n",
   3289 		    isp->isp_name, sdp->isp_fifo_threshold,
   3290 		    sdp->isp_cmd_dma_burst_enable,
   3291 		    sdp->isp_data_dma_burst_enabl);
   3292 #endif
   3293 		for (i = 0; i < 16; i++) {
   3294 			sdp->isp_devparam[i].dev_enable =
   3295 				ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
   3296 			sdp->isp_devparam[i].exc_throttle =
   3297 				ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
   3298 			sdp->isp_devparam[i].sync_offset =
   3299 				ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
   3300 			sdp->isp_devparam[i].sync_period =
   3301 				ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
   3302 
   3303 			if (isp->isp_type < ISP_HA_SCSI_1040) {
   3304 				/*
   3305 				 * If we're not ultra, we can't possibly
   3306 				 * be a shorter period than this.
   3307 				 */
   3308 				if (sdp->isp_devparam[i].sync_period < 0x19) {
   3309 					sdp->isp_devparam[i].sync_period =
   3310 					    0x19;
   3311 				}
   3312 				if (sdp->isp_devparam[i].sync_offset > 0xc) {
   3313 					sdp->isp_devparam[i].sync_offset =
   3314 					    0x0c;
   3315 				}
   3316 			} else {
   3317 				if (sdp->isp_devparam[i].sync_offset > 0x8) {
   3318 					sdp->isp_devparam[i].sync_offset = 0x8;
   3319 				}
   3320 			}
   3321 
   3322 			sdp->isp_devparam[i].dev_flags = 0;
   3323 
   3324 			if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
   3325 				sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   3326 			if (ISP_NVRAM_TGT_QFRZ(nvram_data, i)) {
   3327 				PRINTF("%s: not supporting QFRZ option for "
   3328 				    "target %d\n", isp->isp_name, i);
   3329 			}
   3330 			sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   3331 			if (ISP_NVRAM_TGT_ARQ(nvram_data, i) == 0) {
   3332 				PRINTF("%s: not disabling ARQ option for "
   3333 				    "target %d\n", isp->isp_name, i);
   3334 			}
   3335 			if (ISP_NVRAM_TGT_TQING(nvram_data, i))
   3336 				sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   3337 			if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
   3338 				sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   3339 			if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
   3340 				sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   3341 			if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
   3342 				sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   3343 			if (ISP_NVRAM_TGT_DISC(nvram_data, i))
   3344 				sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   3345 		}
   3346 	} else {
   3347 		fcparam *fcp = (fcparam *) isp->isp_param;
   3348 		union {
   3349 			struct {
   3350 #if	BYTE_ORDER == BIG_ENDIAN
   3351 				u_int32_t hi32;
   3352 				u_int32_t lo32;
   3353 #else
   3354 				u_int32_t lo32;
   3355 				u_int32_t hi32;
   3356 #endif
   3357 			} wds;
   3358 			u_int64_t full64;
   3359 		} wwnstore;
   3360 
   3361 		wwnstore.full64 = ISP2100_NVRAM_NODE_NAME(nvram_data);
   3362 		PRINTF("%s: Adapter WWN 0x%08x%08x\n", isp->isp_name,
   3363 		    wwnstore.wds.hi32, wwnstore.wds.lo32);
   3364 		fcp->isp_wwn = wwnstore.full64;
   3365 		wwnstore.full64 = ISP2100_NVRAM_BOOT_NODE_NAME(nvram_data);
   3366 		if (wwnstore.full64 != 0) {
   3367 			PRINTF("%s: BOOT DEVICE WWN 0x%08x%08x\n",
   3368 			    isp->isp_name, wwnstore.wds.hi32,
   3369 			    wwnstore.wds.lo32);
   3370 		}
   3371 		fcp->isp_maxalloc =
   3372 			ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   3373 		fcp->isp_maxfrmlen =
   3374 			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   3375 		fcp->isp_retry_delay =
   3376 			ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   3377 		fcp->isp_retry_count =
   3378 			ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   3379 		fcp->isp_loopid =
   3380 			ISP2100_NVRAM_HARDLOOPID(nvram_data);
   3381 		fcp->isp_execthrottle =
   3382 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   3383 	}
   3384 	return (0);
   3385 }
   3386 
   3387 static void
   3388 isp_rdnvram_word(isp, wo, rp)
   3389 	struct ispsoftc *isp;
   3390 	int wo;
   3391 	u_int16_t *rp;
   3392 {
   3393 	int i, cbits;
   3394 	u_int16_t bit, rqst;
   3395 
   3396 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   3397 	SYS_DELAY(2);
   3398 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   3399 	SYS_DELAY(2);
   3400 
   3401 	if (isp->isp_type & ISP_HA_FC) {
   3402 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   3403 		rqst = (ISP_NVRAM_READ << 8) | wo;
   3404 		cbits = 10;
   3405 	} else {
   3406 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   3407 		rqst = (ISP_NVRAM_READ << 6) | wo;
   3408 		cbits = 8;
   3409 	}
   3410 
   3411 	/*
   3412 	 * Clock the word select request out...
   3413 	 */
   3414 	for (i = cbits; i >= 0; i--) {
   3415 		if ((rqst >> i) & 1) {
   3416 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   3417 		} else {
   3418 			bit = BIU_NVRAM_SELECT;
   3419 		}
   3420 		ISP_WRITE(isp, BIU_NVRAM, bit);
   3421 		SYS_DELAY(2);
   3422 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   3423 		SYS_DELAY(2);
   3424 		ISP_WRITE(isp, BIU_NVRAM, bit);
   3425 		SYS_DELAY(2);
   3426 	}
   3427 	/*
   3428 	 * Now read the result back in (bits come back in MSB format).
   3429 	 */
   3430 	*rp = 0;
   3431 	for (i = 0; i < 16; i++) {
   3432 		u_int16_t rv;
   3433 		*rp <<= 1;
   3434 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   3435 		SYS_DELAY(2);
   3436 		rv = ISP_READ(isp, BIU_NVRAM);
   3437 		if (rv & BIU_NVRAM_DATAIN) {
   3438 			*rp |= 1;
   3439 		}
   3440 		SYS_DELAY(2);
   3441 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   3442 		SYS_DELAY(2);
   3443 	}
   3444 	ISP_WRITE(isp, BIU_NVRAM, 0);
   3445 	SYS_DELAY(2);
   3446 #if	BYTE_ORDER == BIG_ENDIAN
   3447 	*rp = ((*rp >> 8) | ((*rp & 0xff) << 8));
   3448 #endif
   3449 }
   3450