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