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