isp.c revision 39439
118316Swollman/* $FreeBSD: head/sys/dev/isp/isp.c 39439 1998-09-17 22:53:35Z mjacob $ */
218316Swollman/*
318316Swollman * Machine and OS Independent (well, as best as possible)
418316Swollman * code for the Qlogic ISP SCSI adapters.
518316Swollman *
618316Swollman *---------------------------------------
718316Swollman * Copyright (c) 1997, 1998 by Matthew Jacob
818316Swollman * NASA/Ames Research Center
918316Swollman * All rights reserved.
1018316Swollman *---------------------------------------
1118316Swollman *
1218316Swollman * Redistribution and use in source and binary forms, with or without
1318316Swollman * modification, are permitted provided that the following conditions
1418316Swollman * are met:
1518316Swollman * 1. Redistributions of source code must retain the above copyright
1618316Swollman *    notice immediately at the beginning of the file, without modification,
1718316Swollman *    this list of conditions, and the following disclaimer.
1818316Swollman * 2. Redistributions in binary form must reproduce the above copyright
1918316Swollman *    notice, this list of conditions and the following disclaimer in the
2018316Swollman *    documentation and/or other materials provided with the distribution.
2118316Swollman * 3. The name of the author may not be used to endorse or promote products
2218316Swollman *    derived from this software without specific prior written permission.
2318316Swollman *
2418316Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2518316Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2618316Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2718316Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2846303Smarkm * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2950476Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3018316Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3118316Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3246303Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3318316Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3446303Smarkm * SUCH DAMAGE.
3518316Swollman */
3618316Swollman
3718316Swollman/*
3818316Swollman * Inspiration and ideas about this driver are from Erik Moe's Linux driver
3918316Swollman * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
4018316Swollman * ideas dredged from the Solaris driver.
4146303Smarkm */
4232502Scharnier
4346303Smarkm/*
4418316Swollman * Include header file appropriate for platform we're building on.
4518316Swollman */
4618316Swollman
4718316Swollman#ifdef	__NetBSD__
4818316Swollman#include <dev/ic/isp_netbsd.h>
4918316Swollman#endif
5018316Swollman#ifdef	__FreeBSD__
5118316Swollman#include <dev/isp/isp_freebsd.h>
52126250Sbms#endif
53126250Sbms#ifdef	__linux__
54126250Sbms#include <isp_linux.h>
5546303Smarkm#endif
56126250Sbms
57126250Sbms/*
58126250Sbms * General defines
59126250Sbms */
60126250Sbms
61126250Sbms#define	MBOX_DELAY_COUNT	1000000 / 100
62126250Sbms
63126250Sbms/*
64126250Sbms * Local static data
65126250Sbms */
66126250Sbms#if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
67126250Sbmsstatic const char tgtiqd[36] = {
68126250Sbms	0x03, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
69126250Sbms	0x51, 0x4C, 0x4F, 0x47, 0x49, 0x43, 0x20, 0x20,
7046303Smarkm#ifdef	__NetBSD__
7118316Swollman	0x4E, 0x45, 0x54, 0x42, 0x53, 0x44, 0x20, 0x20,
7218316Swollman#else
7318316Swollman# ifdef	__FreeBSD__
7418316Swollman	0x46, 0x52, 0x45, 0x45, 0x42, 0x52, 0x44, 0x20,
75126250Sbms# else
76126250Sbms#  ifdef linux
77126250Sbms	0x4C, 0x49, 0x4E, 0x55, 0x58, 0x20, 0x20, 0x20,
7846303Smarkm#  else
7946303Smarkm#  endif
8046303Smarkm# endif
8146303Smarkm#endif
8246303Smarkm	0x54, 0x41, 0x52, 0x47, 0x45, 0x54, 0x20, 0x20,
8346303Smarkm	0x20, 0x20, 0x20, 0x31
8446303Smarkm};
8546303Smarkm#endif
8646303Smarkm
87126250Sbms
8846303Smarkm/*
8946303Smarkm * Local function prototypes.
9018316Swollman */
9118316Swollmanstatic int isp_parse_async __P((struct ispsoftc *, u_int16_t));
9218316Swollmanstatic int isp_handle_other_response
9346303Smarkm__P((struct ispsoftc *, ispstatusreq_t *, u_int8_t *));
9418316Swollman#if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
9546303Smarkmstatic int isp_modify_lun __P((struct ispsoftc *, int, int, int));
9646303Smarkm#endif
9718316Swollmanstatic void isp_parse_status
9818316Swollman__P((struct ispsoftc *, ispstatusreq_t *, ISP_SCSI_XFER_T *));
9918316Swollmanstatic void isp_fibre_init __P((struct ispsoftc *));
10018316Swollmanstatic void isp_fw_state __P((struct ispsoftc *));
10118316Swollmanstatic void isp_dumpregs __P((struct ispsoftc *, const char *));
10218316Swollmanstatic void isp_dumpxflist __P((struct ispsoftc *));
10318316Swollmanstatic void isp_prtstst __P((ispstatusreq_t *));
10418316Swollmanstatic void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *));
10518316Swollman
10618316Swollmanstatic void isp_update  __P((struct ispsoftc *));
10718316Swollmanstatic void isp_setdfltparm __P((struct ispsoftc *));
10818316Swollmanstatic int isp_read_nvram __P((struct ispsoftc *));
10918316Swollmanstatic void isp_rdnvram_word __P((struct ispsoftc *, int, u_int16_t *));
11018316Swollman
11118316Swollman/*
11218316Swollman * Reset Hardware.
11318316Swollman *
11418316Swollman * Hit the chip over the head, download new f/w.
11519880Swollman *
11619880Swollman * Locking done elsewhere.
11719880Swollman */
11819880Swollmanvoid
11918316Swollmanisp_reset(isp)
12018316Swollman	struct ispsoftc *isp;
12118316Swollman{
12246303Smarkm	static char once = 1;
12346303Smarkm	mbreg_t mbs;
12446303Smarkm	int loops, i, dodnld = 1;
12518316Swollman	char *revname;
12646303Smarkm
12746303Smarkm	isp->isp_state = ISP_NILSTATE;
12846303Smarkm
12918316Swollman	/*
13018316Swollman	 * Basic types (SCSI, FibreChannel and PCI or SBus)
13146303Smarkm	 * have been set in the MD code. We figure out more
13237908Scharnier	 * here.
13318316Swollman	 */
13446303Smarkm	isp->isp_dblev = DFLT_DBLEVEL;
13537908Scharnier	if (isp->isp_type & ISP_HA_FC) {
13618316Swollman		revname = "2100";
13718316Swollman	} else {
13818316Swollman		sdparam *sdp = isp->isp_param;
13918316Swollman
14019880Swollman		int rev = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
14146303Smarkm		switch (rev) {
14218316Swollman		default:
14318316Swollman			PRINTF("%s: unknown chip rev. 0x%x- assuming a 1020\n",
14418316Swollman			    isp->isp_name, rev);
14518316Swollman			/* FALLTHROUGH */
14618316Swollman		case 1:
14746303Smarkm			revname = "1020";
14824359Simp			isp->isp_type = ISP_HA_SCSI_1020;
14918316Swollman			sdp->isp_clock = 40;
15018316Swollman			break;
15118316Swollman		case 2:
15218316Swollman			/*
15318316Swollman			 * Some 1020A chips are Ultra Capable, but don't
15418316Swollman			 * run the clock rate up for that unless told to
15518316Swollman			 * do so by the Ultra Capable bits being set.
15618316Swollman			 */
15718316Swollman			revname = "1020A";
15818316Swollman			isp->isp_type = ISP_HA_SCSI_1020A;
15918316Swollman			sdp->isp_clock = 40;
16018316Swollman			break;
16118316Swollman		case 3:
16218316Swollman			revname = "1040";
16318316Swollman			isp->isp_type = ISP_HA_SCSI_1040;
16418316Swollman			sdp->isp_clock = 60;
16518316Swollman			break;
16618316Swollman		case 4:
16718316Swollman			revname = "1040A";
16818316Swollman			isp->isp_type = ISP_HA_SCSI_1040A;
16937908Scharnier			sdp->isp_clock = 60;
17018316Swollman			break;
17118316Swollman		case 5:
17218316Swollman			revname = "1040B";
17318316Swollman			isp->isp_type = ISP_HA_SCSI_1040B;
17418316Swollman			sdp->isp_clock = 60;
17537908Scharnier			break;
17618316Swollman		}
17718316Swollman		/*
17818316Swollman		 * Try and figure out if we're connected to a differential bus.
179299768Spfg		 * You have to pause the RISC processor to read SXP registers.
18046303Smarkm		 */
18146303Smarkm		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
18246303Smarkm		i = 100;
18346303Smarkm		while ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
18446303Smarkm			SYS_DELAY(20);
18546303Smarkm			if (--i == 0) {
18646303Smarkm				PRINTF("%s: unable to pause RISC processor\n",
18718316Swollman				    isp->isp_name);
18818316Swollman				i = -1;
18918316Swollman				break;
19018316Swollman			}
19118316Swollman		}
19218316Swollman		if (i > 0) {
19318316Swollman			if (isp->isp_bustype != ISP_BT_SBUS) {
19418316Swollman				ISP_SETBITS(isp, BIU_CONF1, BIU_PCI_CONF1_SXP);
19518316Swollman			}
19618316Swollman			if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
19746303Smarkm				IDPRINTF(2, ("%s: Differential Mode Set\n",
19846303Smarkm				    isp->isp_name));
19946303Smarkm				sdp->isp_diffmode = 1;
20046303Smarkm			} else {
20146303Smarkm				sdp->isp_diffmode = 0;
20246303Smarkm			}
20318316Swollman
20446303Smarkm			if (isp->isp_bustype != ISP_BT_SBUS) {
20518316Swollman				ISP_CLRBITS(isp, BIU_CONF1, BIU_PCI_CONF1_SXP);
20646303Smarkm			}
20718316Swollman
20846303Smarkm			/*
20918316Swollman			 * Figure out whether we're ultra capable.
21046303Smarkm			 */
21118316Swollman			i = ISP_READ(isp, RISC_PSR);
21218316Swollman			if (isp->isp_bustype != ISP_BT_SBUS) {
21346303Smarkm				i &= RISC_PSR_PCI_ULTRA;
21418316Swollman			} else {
21518316Swollman				i &= RISC_PSR_SBUS_ULTRA;
21618316Swollman			}
21718316Swollman			if (i) {
21818316Swollman				IDPRINTF(2, ("%s: Ultra Mode Capable\n",
21946303Smarkm				    isp->isp_name));
22046303Smarkm				sdp->isp_clock = 60;
22118316Swollman			} else {
22218316Swollman				sdp->isp_clock = 40;
22318316Swollman			}
22446303Smarkm			/*
22518316Swollman			 * Restart processor
22618316Swollman			 */
22718316Swollman			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
22818316Swollman		}
22946303Smarkm		/*
23018316Swollman		 * Machine dependent clock (if set) overrides
23118316Swollman		 * our generic determinations.
23218316Swollman		 */
23318316Swollman		if (isp->isp_mdvec->dv_clock) {
23446303Smarkm			if (isp->isp_mdvec->dv_clock < sdp->isp_clock) {
23518316Swollman				sdp->isp_clock = isp->isp_mdvec->dv_clock;
23646303Smarkm			}
23718316Swollman		}
23818316Swollman	}
23937908Scharnier
24018316Swollman	/*
24146303Smarkm	 * Do MD specific pre initialization
24246303Smarkm	 */
24318316Swollman	ISP_RESET0(isp);
24418316Swollman
24518316Swollman	if (once == 1) {
24619880Swollman		once = 0;
24719880Swollman		/*
24819880Swollman		 * Get the current running firmware revision out of the
24919880Swollman		 * chip before we hit it over the head (if this is our
25037908Scharnier		 * first time through). Note that we store this as the
25119880Swollman		 * 'ROM' firmware revision- which it may not be. In any
25219880Swollman		 * case, we don't really use this yet, but we may in
25319880Swollman		 * the future.
25419880Swollman		 */
25519880Swollman		mbs.param[0] = MBOX_ABOUT_FIRMWARE;
25619880Swollman		isp_mboxcmd(isp, &mbs);
25737908Scharnier		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
25819880Swollman			IDPRINTF(3, ("%s: initial ABOUT FIRMWARE command "
25946303Smarkm			    "failed\n", isp->isp_name));
26037908Scharnier		} else {
26119880Swollman			isp->isp_romfw_rev =
26219880Swollman			    (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
26319880Swollman		}
26419880Swollman	}
26537908Scharnier
26619880Swollman	/*
26737908Scharnier	 * Hit the chip over the head with hammer,
26819880Swollman	 * and give the ISP a chance to recover.
26919880Swollman	 */
27019880Swollman
27118316Swollman	if (isp->isp_type & ISP_HA_SCSI) {
27237908Scharnier		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
27318316Swollman		/*
27418316Swollman		 * A slight delay...
27518316Swollman		 */
27646303Smarkm		SYS_DELAY(100);
27737908Scharnier
27846303Smarkm		/*
27946303Smarkm		 * Clear data && control DMA engines.
28046303Smarkm		 */
28146303Smarkm		ISP_WRITE(isp, CDMA_CONTROL,
28218316Swollman		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
28346303Smarkm		ISP_WRITE(isp, DDMA_CONTROL,
28446303Smarkm		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
28546524Smarkm	} else {
28646524Smarkm		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
28746303Smarkm		/*
28818316Swollman		 * A slight delay...
28918316Swollman		 */
29018316Swollman		SYS_DELAY(100);
29146303Smarkm		ISP_WRITE(isp, CDMA2100_CONTROL,
29218316Swollman			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
29318316Swollman		ISP_WRITE(isp, TDMA2100_CONTROL,
29418316Swollman			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
29546524Smarkm		ISP_WRITE(isp, RDMA2100_CONTROL,
29618316Swollman			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
29718316Swollman	}
29818316Swollman
29918316Swollman	/*
30018316Swollman	 * Wait for ISP to be ready to go...
30118316Swollman	 */
30218316Swollman	loops = MBOX_DELAY_COUNT;
30318316Swollman	for (;;) {
30418316Swollman		if (isp->isp_type & ISP_HA_SCSI) {
30546303Smarkm			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
30618316Swollman				break;
30718316Swollman		} else {
30846303Smarkm			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
30937908Scharnier				break;
31046303Smarkm		}
31137908Scharnier		SYS_DELAY(100);
31246303Smarkm		if (--loops < 0) {
31346303Smarkm			isp_dumpregs(isp, "chip reset timed out");
31446303Smarkm			return;
31546303Smarkm		}
31646303Smarkm	}
31737908Scharnier	/*
31837908Scharnier	 * More initialization
31918316Swollman	 */
32046303Smarkm	if (isp->isp_type & ISP_HA_SCSI) {
32118316Swollman		ISP_WRITE(isp, BIU_CONF1, 0);
32218316Swollman	} else {
32318316Swollman		ISP_WRITE(isp, BIU2100_CSR, 0);
32418316Swollman		/*
32518316Swollman		 * All 2100's are 60Mhz with fast rams onboard.
32618316Swollman		 */
32718316Swollman		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
32818316Swollman	}
32946303Smarkm
33046303Smarkm	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
33146303Smarkm	SYS_DELAY(100);
33246303Smarkm
33318316Swollman	if (isp->isp_type & ISP_HA_SCSI) {
33418316Swollman		ISP_SETBITS(isp, BIU_CONF1, isp->isp_mdvec->dv_conf1);
33518316Swollman		if (isp->isp_mdvec->dv_conf1 & BIU_BURST_ENABLE) {
33618316Swollman			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
33718316Swollman			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
33818316Swollman		}
33918316Swollman	}
34046303Smarkm	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
34118316Swollman
34218316Swollman	/*
34318316Swollman	 * Do MD specific post initialization
34418316Swollman	 */
34518316Swollman	ISP_RESET1(isp);
34646303Smarkm
34746303Smarkm	/*
34846303Smarkm	 * Enable interrupts
34946524Smarkm	 */
35046524Smarkm	ENABLE_INTS(isp);
35146303Smarkm
35218316Swollman	/*
35318316Swollman	 * Do some sanity checking.
35418316Swollman	 */
35518316Swollman	mbs.param[0] = MBOX_NO_OP;
356299768Spfg	isp_mboxcmd(isp, &mbs);
35718316Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
35818316Swollman		isp_dumpregs(isp, "NOP test failed");
35918316Swollman		return;
36018316Swollman	}
36118316Swollman
36218316Swollman	if (isp->isp_type & ISP_HA_SCSI) {
36318316Swollman		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
36418316Swollman		mbs.param[1] = 0xdead;
36518316Swollman		mbs.param[2] = 0xbeef;
36618316Swollman		mbs.param[3] = 0xffff;
36718316Swollman		mbs.param[4] = 0x1111;
36818316Swollman		mbs.param[5] = 0xa5a5;
36919880Swollman		isp_mboxcmd(isp, &mbs);
37019880Swollman		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
37118316Swollman			isp_dumpregs(isp,
37218316Swollman				"Mailbox Register test didn't complete");
37318316Swollman			return;
37418316Swollman		}
37518316Swollman		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
37618316Swollman		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
37718316Swollman		    mbs.param[5] != 0xa5a5) {
37818316Swollman			isp_dumpregs(isp, "Register Test Failed");
37918316Swollman			return;
38018316Swollman		}
38119880Swollman
38218316Swollman	}
38318316Swollman
38418316Swollman	/*
38518316Swollman	 * Download new Firmware, unless requested not to do so.
38618316Swollman	 * This is made slightly trickier in some cases where the
38719880Swollman	 * firmware of the ROM revision is newer than the revision
38819880Swollman	 * compiled into the driver. So, where we used to compare
38919880Swollman	 * versions of our f/w and the ROM f/w, now we just see
39019880Swollman	 * whether we have f/w at all and whether a config flag
39146303Smarkm	 * has disabled our download.
39219880Swollman	 */
39319880Swollman	if ((isp->isp_mdvec->dv_fwlen == 0) ||
39419880Swollman	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
39519880Swollman		dodnld = 0;
39619880Swollman	}
39719880Swollman
39819880Swollman	if (dodnld) {
399126250Sbms		for (i = 0; i < isp->isp_mdvec->dv_fwlen; i++) {
40019880Swollman			mbs.param[0] = MBOX_WRITE_RAM_WORD;
40146303Smarkm			mbs.param[1] = isp->isp_mdvec->dv_codeorg + i;
40246303Smarkm			mbs.param[2] = isp->isp_mdvec->dv_ispfw[i];
40319880Swollman			isp_mboxcmd(isp, &mbs);
40446303Smarkm			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
40519880Swollman				isp_dumpregs(isp, "f/w download failed");
40646303Smarkm				return;
40746303Smarkm			}
40846303Smarkm		}
409126250Sbms
41019880Swollman		if (isp->isp_mdvec->dv_fwlen) {
41119880Swollman			/*
41219880Swollman			 * Verify that it downloaded correctly.
41319880Swollman			 */
41418316Swollman			mbs.param[0] = MBOX_VERIFY_CHECKSUM;
41518316Swollman			mbs.param[1] = isp->isp_mdvec->dv_codeorg;
41618316Swollman			isp_mboxcmd(isp, &mbs);
41718316Swollman			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
41818316Swollman				isp_dumpregs(isp, "ram checksum failure");
41918316Swollman				return;
420299768Spfg			}
42118316Swollman		}
422299768Spfg	} else {
423126250Sbms		IDPRINTF(3, ("%s: skipping f/w download\n", isp->isp_name));
42418316Swollman	}
42518316Swollman
42618316Swollman	/*
42718316Swollman	 * Now start it rolling.
42818316Swollman	 *
42946303Smarkm	 * If we didn't actually download f/w,
43018316Swollman	 * we still need to (re)start it.
43118316Swollman	 */
43246303Smarkm
43318316Swollman	mbs.param[0] = MBOX_EXEC_FIRMWARE;
43418316Swollman	mbs.param[1] = isp->isp_mdvec->dv_codeorg;
43546303Smarkm	isp_mboxcmd(isp, &mbs);
43618316Swollman
43718316Swollman	if (isp->isp_type & ISP_HA_SCSI) {
43846303Smarkm		sdparam *sdp = isp->isp_param;
43946524Smarkm		/*
44046524Smarkm		 * Set CLOCK RATE, but only if asked to.
44146303Smarkm		 */
44218316Swollman		if (sdp->isp_clock) {
44318316Swollman			mbs.param[0] = MBOX_SET_CLOCK_RATE;
44418316Swollman			mbs.param[1] = sdp->isp_clock;
44518316Swollman			isp_mboxcmd(isp, &mbs);
44618316Swollman			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
44718316Swollman				isp_dumpregs(isp, "failed to set CLOCKRATE");
448299768Spfg				/* but continue */
44918316Swollman			} else {
45018316Swollman				IDPRINTF(3, ("%s: setting input clock to %d\n",
45118316Swollman				    isp->isp_name, sdp->isp_clock));
452299768Spfg			}
45318316Swollman		}
454299768Spfg	}
45546303Smarkm	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
45646303Smarkm	isp_mboxcmd(isp, &mbs);
45746303Smarkm	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
45846303Smarkm		isp_dumpregs(isp, "ABOUT FIRMWARE command failed");
45918316Swollman		return;
46018316Swollman	}
46118316Swollman	PRINTF("%s: Board Revision %s, %s F/W Revision %d.%d\n",
46218316Swollman		isp->isp_name, revname, dodnld? "loaded" : "resident",
46318316Swollman		mbs.param[1], mbs.param[2]);
46418316Swollman	isp->isp_fwrev = (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
46518316Swollman	if (isp->isp_romfw_rev && dodnld) {
46618316Swollman		PRINTF("%s: Last F/W revision was %d.%d\n", isp->isp_name,
46718316Swollman		    isp->isp_romfw_rev >> 10, isp->isp_romfw_rev & 0x3ff);
46818316Swollman	}
46918316Swollman	isp_fw_state(isp);
47032502Scharnier	isp->isp_state = ISP_RESETSTATE;
47118316Swollman}
47246524Smarkm
47346524Smarkm/*
47418316Swollman * Initialize Hardware to known state
47518316Swollman *
47618316Swollman * Locks are held before coming here.
47718316Swollman */
47818316Swollman
479299768Spfgvoid
48018316Swollmanisp_init(isp)
48118316Swollman	struct ispsoftc *isp;
48218316Swollman{
48318316Swollman	sdparam *sdp;
48418316Swollman	mbreg_t mbs;
48518316Swollman	int tgt;
48618316Swollman
48718316Swollman	/*
48818316Swollman	 * Must do first.
48918316Swollman	 */
49046303Smarkm	isp_setdfltparm(isp);
49146524Smarkm
49246524Smarkm	/*
49346303Smarkm	 * If we're fibre, we have a completely different
49418316Swollman	 * initialization method.
49518316Swollman	 */
49618316Swollman
49718316Swollman	if (isp->isp_type & ISP_HA_FC) {
49818316Swollman		isp_fibre_init(isp);
49918316Swollman		return;
50018316Swollman	}
50118316Swollman	sdp = isp->isp_param;
50218316Swollman
50319880Swollman	/*
50418316Swollman	 * Set (possibly new) Initiator ID.
50518316Swollman	 */
50646303Smarkm	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
50718316Swollman	mbs.param[1] = sdp->isp_initiator_id;
50818316Swollman	isp_mboxcmd(isp, &mbs);
50918316Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
51018316Swollman		isp_dumpregs(isp, "failed to set initiator id");
51118316Swollman		return;
51246524Smarkm	}
51346524Smarkm
51418316Swollman	/*
51518316Swollman	 * Set Retry Delay and Count
51646303Smarkm	 */
51718316Swollman	mbs.param[0] = MBOX_SET_RETRY_COUNT;
51818316Swollman	mbs.param[1] = sdp->isp_retry_count;
51918316Swollman	mbs.param[2] = sdp->isp_retry_delay;
52018316Swollman	isp_mboxcmd(isp, &mbs);
52118316Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
52218316Swollman		isp_dumpregs(isp, "failed to set retry count and delay");
523299768Spfg		return;
52418316Swollman	}
52518316Swollman
52618316Swollman	/*
52746303Smarkm	 * Set ASYNC DATA SETUP time. This is very important.
52818316Swollman	 */
52918316Swollman	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
53018316Swollman	mbs.param[1] = sdp->isp_async_data_setup;
53146303Smarkm	isp_mboxcmd(isp, &mbs);
53218316Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
53346524Smarkm		isp_dumpregs(isp, "failed to set async data setup time");
53446524Smarkm		return;
53518316Swollman	}
53618316Swollman
53718316Swollman	/*
53818316Swollman	 * Set ACTIVE Negation State.
53918316Swollman	 */
54018316Swollman	mbs.param[0] = MBOX_SET_ACTIVE_NEG_STATE;
54118316Swollman	mbs.param[1] =
54219880Swollman	    (sdp->isp_req_ack_active_neg << 4) |
54319880Swollman	    (sdp->isp_data_line_active_neg << 5);
54419880Swollman	isp_mboxcmd(isp, &mbs);
54519880Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
54619880Swollman		isp_dumpregs(isp, "failed to set active neg state");
54719880Swollman		return;
54819880Swollman	}
54919880Swollman
55019880Swollman	/*
55119880Swollman	 * Set the Tag Aging limit
55219880Swollman	 */
55319880Swollman
55419880Swollman	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
55519880Swollman	mbs.param[1] = sdp->isp_tag_aging;
55619880Swollman	isp_mboxcmd(isp, &mbs);
55719880Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
55819880Swollman		isp_dumpregs(isp, "failed to set tag age limit");
55919880Swollman		return;
56019880Swollman	}
56119880Swollman
56219880Swollman	/*
56319880Swollman	 * Set selection timeout.
56419880Swollman	 */
56519880Swollman
56619880Swollman	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
56719880Swollman	mbs.param[1] = sdp->isp_selection_timeout;
56819880Swollman	isp_mboxcmd(isp, &mbs);
56919880Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
57019880Swollman		isp_dumpregs(isp, "failed to set selection timeout");
57119880Swollman		return;
57219880Swollman	}
57319880Swollman
57419880Swollman	/*
57519880Swollman	 * Set per-target parameters to a safe minimum.
57619880Swollman	 */
57719880Swollman
57819880Swollman	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
57919880Swollman		int maxlun, lun;
58019880Swollman
58119880Swollman		if (sdp->isp_devparam[tgt].dev_enable == 0)
58219880Swollman			continue;
58319880Swollman
58419880Swollman		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
58519880Swollman		mbs.param[1] = tgt << 8;
58619880Swollman		mbs.param[2] = DPARM_SAFE_DFLT;
58719880Swollman		mbs.param[3] = 0;
58819880Swollman		/*
58919880Swollman		 * It is not quite clear when this changed over so that
59019880Swollman		 * we could force narrow and async, so assume >= 7.55.
59119880Swollman		 *
59219880Swollman		 * Otherwise, a SCSI bus reset issued below will force
59319880Swollman		 * the back to the narrow, async state (but see note
59419880Swollman		 * below also). Technically we should also do without
59519880Swollman		 * Parity.
59618316Swollman		 */
59718316Swollman		if (isp->isp_fwrev >= ISP_FW_REV(7, 55)) {
59818316Swollman			mbs.param[2] |= DPARM_NARROW | DPARM_ASYNC;
59918316Swollman		}
60018316Swollman		sdp->isp_devparam[tgt].cur_dflags = mbs.param[2] >> 8;
60118316Swollman
60218316Swollman		IDPRINTF(3, ("\n%s: tgt %d cflags %x offset %x period %x\n",
60318316Swollman		    isp->isp_name, tgt, mbs.param[2], mbs.param[3] >> 8,
60446303Smarkm		    mbs.param[3] & 0xff));
60518316Swollman		isp_mboxcmd(isp, &mbs);
606126250Sbms		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
60746303Smarkm
60846303Smarkm			PRINTF("%s: failed to set parameters for tgt %d\n",
60918316Swollman				isp->isp_name, tgt);
61018316Swollman
61118316Swollman			PRINTF("%s: flags %x offset %x period %x\n",
61218316Swollman				isp->isp_name, sdp->isp_devparam[tgt].dev_flags,
61318316Swollman				sdp->isp_devparam[tgt].sync_offset,
61419880Swollman				sdp->isp_devparam[tgt].sync_period);
61518316Swollman
61618316Swollman			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
61718316Swollman			mbs.param[1] = tgt << 8;
61818316Swollman			mbs.param[2] = DPARM_SAFE_DFLT;
61918316Swollman			mbs.param[3] = 0;
62018316Swollman			isp_mboxcmd(isp, &mbs);
62118316Swollman			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
622299768Spfg				PRINTF("%s: failed even to set defaults for "
62318316Swollman				    "target %d\n", isp->isp_name, tgt);
62418316Swollman				continue;
62518316Swollman			}
62618316Swollman		}
62718316Swollman
62818316Swollman		maxlun = (isp->isp_fwrev >= ISP_FW_REV(7, 55))? 32 : 8;
62918316Swollman		for (lun = 0; lun < maxlun; lun++) {
63018316Swollman			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
63118316Swollman			mbs.param[1] = (tgt << 8) | lun;
63218316Swollman			mbs.param[2] = sdp->isp_max_queue_depth;
63318316Swollman			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
63418316Swollman			isp_mboxcmd(isp, &mbs);
63518316Swollman			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
63618316Swollman				PRINTF("%s: failed to set device queue "
63718316Swollman				    "parameters for target %d, lun %d\n",
63846303Smarkm				    isp->isp_name, tgt, lun);
63918316Swollman				break;
64018316Swollman			}
64146303Smarkm		}
64218316Swollman	}
64318316Swollman
64418316Swollman	/*
64518316Swollman	 * Set up DMA for the request and result mailboxes.
64618316Swollman	 */
64718316Swollman	if (ISP_MBOXDMASETUP(isp) != 0) {
64818316Swollman		PRINTF("%s: can't setup dma mailboxes\n", isp->isp_name);
64918316Swollman		return;
65018316Swollman	}
65118316Swollman
65218316Swollman	mbs.param[0] = MBOX_INIT_RES_QUEUE;
65318316Swollman	mbs.param[1] = RESULT_QUEUE_LEN;
65418316Swollman	mbs.param[2] = (u_int16_t) (isp->isp_result_dma >> 16);
65518316Swollman	mbs.param[3] = (u_int16_t) (isp->isp_result_dma & 0xffff);
65618316Swollman	mbs.param[4] = 0;
65718316Swollman	mbs.param[5] = 0;
65818316Swollman	isp_mboxcmd(isp, &mbs);
65918316Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
66018316Swollman		isp_dumpregs(isp, "set of response queue failed");
66118316Swollman		return;
66218316Swollman	}
66318316Swollman	isp->isp_residx = 0;
66418316Swollman
66518316Swollman	mbs.param[0] = MBOX_INIT_REQ_QUEUE;
66618316Swollman	mbs.param[1] = RQUEST_QUEUE_LEN;
66718316Swollman	mbs.param[2] = (u_int16_t) (isp->isp_rquest_dma >> 16);
66818316Swollman	mbs.param[3] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
66918316Swollman	mbs.param[4] = 0;
67018316Swollman	mbs.param[5] = 0;
67118316Swollman	isp_mboxcmd(isp, &mbs);
67218316Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
67318316Swollman		isp_dumpregs(isp, "set of request queue failed");
67418316Swollman		return;
67518316Swollman	}
67618316Swollman	isp->isp_reqidx = isp->isp_reqodx = 0;
67718316Swollman
67818316Swollman	/*
67918316Swollman	 * XXX: See whether or not for 7.55 F/W or later we
68018316Swollman	 * XXX: can do without this, and see whether we should
68118316Swollman	 * XXX: honor the NVRAM SCSI_RESET_DISABLE token.
68218316Swollman	 */
68318316Swollman	mbs.param[0] = MBOX_BUS_RESET;
68418316Swollman	mbs.param[1] = 3;
68518316Swollman	isp_mboxcmd(isp, &mbs);
68618316Swollman	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
68718316Swollman		isp_dumpregs(isp, "SCSI bus reset failed");
68818316Swollman	}
68918316Swollman	/*
69018316Swollman	 * This is really important to have set after a bus reset.
69118316Swollman	 */
69218316Swollman	isp->isp_sendmarker = 1;
69318316Swollman	isp->isp_state = ISP_INITSTATE;
694299768Spfg}
69518316Swollman
69618316Swollman/*
69718316Swollman * Fibre Channel specific initialization.
69818316Swollman *
69918316Swollman * Locks are held before coming here.
70018316Swollman */
70118316Swollmanstatic void
70218316Swollmanisp_fibre_init(isp)
70318316Swollman	struct ispsoftc *isp;
70418316Swollman{
705299768Spfg	fcparam *fcp;
70618316Swollman	isp_icb_t *icbp;
70718316Swollman	mbreg_t mbs;
70818316Swollman	int count;
70918316Swollman	u_int8_t lwfs;
71018316Swollman
71119880Swollman	fcp = isp->isp_param;
71219880Swollman
71319880Swollman	if (ISP_MBOXDMASETUP(isp) != 0) {
71419880Swollman		PRINTF("%s: can't setup DMA for mailboxes\n", isp->isp_name);
71519880Swollman		return;
71619880Swollman	}
71719880Swollman
71819880Swollman	icbp = (isp_icb_t *) fcp->isp_scratch;
71919880Swollman	bzero(icbp, sizeof (*icbp));
72019880Swollman
72119880Swollman	icbp->icb_version = ICB_VERSION1;
72219880Swollman
72346303Smarkm	fcp->isp_fwoptions = 0;
72419880Swollman#ifdef	ISP2100_TARGET_MODE
72546303Smarkm	fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE	| ICBOPT_INI_TGTTYPE;
72646303Smarkm	icbp->icb_iqdevtype = 0x23;	/* DPQ_SUPPORTED/PROCESSOR */
72719880Swollman#endif
72846303Smarkm	icbp->icb_fwoptions = fcp->isp_fwoptions;
72919880Swollman	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
73046303Smarkm	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
73146303Smarkm	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
73219880Swollman		PRINTF("%s: bad frame length (%d) from NVRAM- using %d\n",
73319880Swollman		    isp->isp_name, fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
73446303Smarkm	}
73519880Swollman	icbp->icb_maxalloc = fcp->isp_maxalloc;
73619880Swollman	icbp->icb_execthrottle = fcp->isp_execthrottle;
73719880Swollman	icbp->icb_retry_delay = fcp->isp_retry_delay;
73819880Swollman	icbp->icb_retry_count = fcp->isp_retry_count;
73946303Smarkm
74019880Swollman	MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwn);
74118316Swollman
74246303Smarkm	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN;
74346303Smarkm	icbp->icb_rsltqlen = RESULT_QUEUE_LEN;
74446303Smarkm	icbp->icb_rqstaddr[RQRSP_ADDR0015] =
74546303Smarkm	    (u_int16_t) (isp->isp_rquest_dma & 0xffff);
746126250Sbms	icbp->icb_rqstaddr[RQRSP_ADDR1631] =
747126250Sbms	    (u_int16_t) (isp->isp_rquest_dma >> 16);
74846303Smarkm	icbp->icb_respaddr[RQRSP_ADDR0015] =
749126250Sbms	    (u_int16_t) (isp->isp_result_dma & 0xffff);
75046303Smarkm	icbp->icb_respaddr[RQRSP_ADDR1631] =
75146303Smarkm	    (u_int16_t) (isp->isp_result_dma >> 16);
75246303Smarkm
75346303Smarkm	for (count = 0; count < 10; count++) {
75446303Smarkm		mbs.param[0] = MBOX_INIT_FIRMWARE;
75546303Smarkm		mbs.param[1] = 0;
75618316Swollman		mbs.param[2] = (u_int16_t) (fcp->isp_scdma >> 16);
75718316Swollman		mbs.param[3] = (u_int16_t) (fcp->isp_scdma & 0xffff);
75818316Swollman		mbs.param[4] = 0;
75918316Swollman		mbs.param[5] = 0;
76018316Swollman		mbs.param[6] = 0;
761126250Sbms		mbs.param[7] = 0;
762126250Sbms
763126250Sbms		isp_mboxcmd(isp, &mbs);
764126250Sbms
76518316Swollman		switch (mbs.param[0]) {
76618316Swollman		case MBOX_COMMAND_COMPLETE:
76718316Swollman			count = 10;
76846303Smarkm			break;
76918316Swollman		case ASYNC_LIP_OCCURRED:
77018316Swollman		case ASYNC_LOOP_UP:
77118316Swollman		case ASYNC_LOOP_DOWN:
77218316Swollman		case ASYNC_LOOP_RESET:
773299768Spfg		case ASYNC_PDB_CHANGED:
77418316Swollman		case ASYNC_CHANGE_NOTIFY:
77518316Swollman			if (count > 9) {
77618316Swollman				PRINTF("%s: too many retries to get going- "
77718316Swollman				    "giving up\n", isp->isp_name);
778299768Spfg				return;
77918316Swollman			}
78018316Swollman			break;
78118316Swollman		default:
78218316Swollman			isp_dumpregs(isp, "INIT FIRMWARE failed");
78318316Swollman			return;
78418316Swollman		}
78518316Swollman	}
78618316Swollman	isp->isp_reqidx = isp->isp_reqodx = 0;
78718316Swollman	isp->isp_residx = 0;
78818316Swollman
78918316Swollman	/*
79018316Swollman	 * Wait up to 12 seconds for FW to go to READY state.
79118316Swollman	 * This used to be 3 seconds, but that lost.
79218316Swollman	 *
79318316Swollman	 * This is all very much not right. The problem here
79490868Smike	 * is that the cable may not be plugged in, or there
79518316Swollman	 * may be many many members of the loop that haven't
79618316Swollman	 * been logged into.
79718316Swollman	 *
79818316Swollman	 * This model of doing things doesn't support dynamic
79918316Swollman	 * attachment, so we just plain lose (for now).
80018316Swollman	 */
80118316Swollman	lwfs = FW_CONFIG_WAIT;
80218316Swollman	for (count = 0; count < 12000; count++) {
80318316Swollman		isp_fw_state(isp);
80418316Swollman		if (lwfs != fcp->isp_fwstate) {
80518316Swollman			PRINTF("%s: Firmware State %s -> %s\n", isp->isp_name,
80618316Swollman			    fw_statename(lwfs), fw_statename(fcp->isp_fwstate));
80718316Swollman			lwfs = fcp->isp_fwstate;
80818316Swollman		}
80918316Swollman		if (fcp->isp_fwstate == FW_READY) {
81018316Swollman			break;
81118316Swollman		}
81218316Swollman		SYS_DELAY(1000);	/* wait one millisecond */
81318316Swollman	}
81418316Swollman	isp->isp_sendmarker = 1;
81518316Swollman
81618316Swollman	/*
81718316Swollman	 * Get our Loop ID
81818316Swollman	 * (if possible)
81918316Swollman	 */
82018316Swollman	if (fcp->isp_fwstate == FW_READY) {
82118316Swollman		mbs.param[0] = MBOX_GET_LOOP_ID;
82218316Swollman		isp_mboxcmd(isp, &mbs);
823299768Spfg		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
82418316Swollman			isp_dumpregs(isp, "GET LOOP ID failed");
82546303Smarkm			return;
82618316Swollman		}
82746303Smarkm		fcp->isp_loopid = mbs.param[1];
82818316Swollman		fcp->isp_alpa = mbs.param[2];
82918316Swollman		PRINTF("%s: Loop ID 0x%x, ALPA 0x%x\n", isp->isp_name,
83018316Swollman		    fcp->isp_loopid, fcp->isp_alpa);
83118316Swollman		isp->isp_state = ISP_INITSTATE;
83218316Swollman#if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
83318316Swollman		DISABLE_INTS(isp);
834299768Spfg		if (isp->isp_fwrev >= ISP_FW_REV(1, 13)) {
83518316Swollman			if (isp_modify_lun(isp, 0, 1, 1)) {
83618316Swollman				PRINTF("%s: failed to establish target mode\n",
83790868Smike				    isp->isp_name);
83818316Swollman			}
83918316Swollman		}
84018316Swollman		ENABLE_INTS(isp);
84118316Swollman#endif
842299768Spfg	} else {
84318316Swollman		PRINTF("%s: failed to go to FW READY state- will not attach\n",
84418316Swollman		    isp->isp_name);
84518316Swollman	}
84618316Swollman}
84718316Swollman
84818316Swollman/*
84918316Swollman * Free any associated resources prior to decommissioning and
85018316Swollman * set the card to a known state (so it doesn't wake up and kick
85118316Swollman * us when we aren't expecting it to).
85218316Swollman *
85318316Swollman * Locks are held before coming here.
85418316Swollman */
85518316Swollmanvoid
85618316Swollmanisp_uninit(isp)
85718316Swollman	struct ispsoftc *isp;
85819880Swollman{
85919880Swollman	/*
86019880Swollman	 * Leave with interrupts disabled.
86119880Swollman	 */
86219880Swollman	DISABLE_INTS(isp);
86319880Swollman
86446303Smarkm	/*
86519880Swollman	 * Stop the watchdog timer (if started).
86619880Swollman	 */
86719880Swollman	STOP_WATCHDOG(isp_watch, isp);
86819880Swollman}
86946303Smarkm
87046303Smarkm
87119880Swollman/*
87219880Swollman * Start a command. Locking is assumed done in the caller.
87319880Swollman */
87419880Swollman
87519880Swollmanint32_t
87619880Swollmanispscsicmd(xs)
87719880Swollman	ISP_SCSI_XFER_T *xs;
87819880Swollman{
87919880Swollman	struct ispsoftc *isp;
88019880Swollman	u_int8_t iptr, optr;
88119880Swollman	union {
88219880Swollman		ispreq_t *_reqp;
88319880Swollman		ispreqt2_t *_t2reqp;
88437815Sphk	} _u;
88519880Swollman#define	reqp	_u._reqp
88619880Swollman#define	t2reqp	_u._t2reqp
88719880Swollman#define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
88819880Swollman	int i;
88919880Swollman
89019880Swollman	XS_INITERR(xs);
89119880Swollman	isp = XS_ISP(xs);
89219880Swollman
89319880Swollman	if (isp->isp_state != ISP_RUNSTATE) {
89419880Swollman		PRINTF("%s: adapter not ready\n", isp->isp_name);
89519880Swollman		XS_SETERR(xs, HBA_BOTCH);
89619880Swollman		return (CMD_COMPLETE);
89719880Swollman	}
89819880Swollman
89919880Swollman	/*
90019880Swollman	 * We *could* do the different sequence type that has clos
90119880Swollman	 * to the whole Queue Entry for the command,.
90219880Swollman	 */
90319880Swollman	if (XS_CDBLEN(xs) > ((isp->isp_type & ISP_HA_FC)? 16 : 12)) {
90419880Swollman		PRINTF("%s: unsupported cdb length (%d)\n",
90519880Swollman		    isp->isp_name, XS_CDBLEN(xs));
90619880Swollman		XS_SETERR(xs, HBA_BOTCH);
90719880Swollman		return (CMD_COMPLETE);
90819880Swollman	}
90919880Swollman
91019880Swollman	/*
91119880Swollman	 * First check to see if any HBA or Device
91219880Swollman	 * parameters need to be updated.
913299768Spfg	 */
91419880Swollman	if (isp->isp_update) {
91519880Swollman		isp_update(isp);
91619880Swollman	}
91719880Swollman
91819880Swollman	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
91919880Swollman	iptr = isp->isp_reqidx;
920
921	reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
922	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
923	if (iptr == optr) {
924		IDPRINTF(2, ("%s: Request Queue Overflow\n", isp->isp_name));
925		XS_SETERR(xs, HBA_BOTCH);
926		return (CMD_EAGAIN);
927	}
928	if (isp->isp_type & ISP_HA_FC) {
929		DISABLE_INTS(isp);
930	}
931
932	if (isp->isp_sendmarker) {
933		u_int8_t niptr;
934		ispmarkreq_t *marker = (ispmarkreq_t *) reqp;
935
936		bzero((void *) marker, sizeof (*marker));
937		marker->req_header.rqs_entry_count = 1;
938		marker->req_header.rqs_entry_type = RQSTYPE_MARKER;
939		marker->req_modifier = SYNC_ALL;
940
941		isp->isp_sendmarker = 0;
942
943		/*
944		 * Unconditionally update the input pointer anyway.
945		 */
946		ISP_WRITE(isp, INMAILBOX4, iptr);
947		isp->isp_reqidx = iptr;
948
949		niptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
950		if (niptr == optr) {
951			if (isp->isp_type & ISP_HA_FC) {
952				ENABLE_INTS(isp);
953			}
954			IDPRINTF(2, ("%s: Request Queue Overflow+\n",
955			    isp->isp_name));
956			XS_SETERR(xs, HBA_BOTCH);
957			return (CMD_EAGAIN);
958		}
959		reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
960		iptr = niptr;
961	}
962
963	bzero((void *) reqp, UZSIZE);
964	reqp->req_header.rqs_entry_count = 1;
965	if (isp->isp_type & ISP_HA_FC) {
966		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
967	} else {
968		reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
969	}
970	reqp->req_header.rqs_flags = 0;
971	reqp->req_header.rqs_seqno = isp->isp_seqno++;
972
973	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
974		if (isp->isp_xflist[i] == NULL)
975			break;
976	}
977	if (i == RQUEST_QUEUE_LEN) {
978		if (isp->isp_type & ISP_HA_FC)
979			ENABLE_INTS(isp);
980		IDPRINTF(2, ("%s: out of xflist pointers\n", isp->isp_name));
981		XS_SETERR(xs, HBA_BOTCH);
982		return (CMD_EAGAIN);
983	} else {
984		/*
985		 * Never have a handle that is zero, so
986		 * set req_handle off by one.
987		 */
988		isp->isp_xflist[i] = xs;
989		reqp->req_handle = i+1;
990	}
991
992	if (isp->isp_type & ISP_HA_FC) {
993		/*
994		 * See comment in isp_intr
995		 */
996		XS_RESID(xs) = 0;
997		/*
998		 * Fibre Channel always requires some kind of tag.
999		 * If we're marked as "Can't Tag", just do simple
1000		 * instead of ordered tags. It's pretty clear to me
1001		 * that we shouldn't do head of queue tagging in
1002		 * this case.
1003		 */
1004		if (XS_CANTAG(xs)) {
1005			t2reqp->req_flags = XS_KINDOF_TAG(xs);
1006		} else {
1007 			t2reqp->req_flags = REQFLAG_STAG;
1008		}
1009	} else {
1010		sdparam *sdp = (sdparam *)isp->isp_param;
1011		if ((sdp->isp_devparam[XS_TGT(xs)].cur_dflags & DPARM_TQING) &&
1012		    XS_CANTAG(xs)) {
1013			reqp->req_flags = XS_KINDOF_TAG(xs);
1014		} else {
1015			reqp->req_flags = 0;
1016		}
1017	}
1018	reqp->req_lun_trn = XS_LUN(xs);
1019	reqp->req_target = XS_TGT(xs);
1020	if (isp->isp_type & ISP_HA_SCSI) {
1021		reqp->req_cdblen = XS_CDBLEN(xs);
1022	}
1023	bcopy((void *)XS_CDBP(xs), reqp->req_cdb, XS_CDBLEN(xs));
1024
1025	IDPRINTF(5, ("%s(%d.%d): START%d cmd 0x%x datalen %d\n", isp->isp_name,
1026	    XS_TGT(xs), XS_LUN(xs), reqp->req_header.rqs_seqno,
1027	    reqp->req_cdb[0], XS_XFRLEN(xs)));
1028
1029	reqp->req_time = XS_TIME(xs) / 1000;
1030	if (reqp->req_time == 0 && XS_TIME(xs))
1031		reqp->req_time = 1;
1032	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
1033	if (i != CMD_QUEUED) {
1034		if (isp->isp_type & ISP_HA_FC)
1035			ENABLE_INTS(isp);
1036		/*
1037		 * dmasetup sets actual error in packet, and
1038		 * return what we were given to return.
1039		 */
1040		return (i);
1041	}
1042	XS_SETERR(xs, HBA_NOERROR);
1043	ISP_WRITE(isp, INMAILBOX4, iptr);
1044	isp->isp_reqidx = iptr;
1045	if (isp->isp_type & ISP_HA_FC) {
1046		ENABLE_INTS(isp);
1047	}
1048	isp->isp_nactive++;
1049	return (CMD_QUEUED);
1050#undef	reqp
1051#undef	t2reqp
1052}
1053
1054/*
1055 * isp control
1056 * Locks (ints blocked) assumed held.
1057 */
1058
1059int
1060isp_control(isp, ctl, arg)
1061	struct ispsoftc *isp;
1062	ispctl_t ctl;
1063	void *arg;
1064{
1065	ISP_SCSI_XFER_T *xs;
1066	mbreg_t mbs;
1067	int i;
1068
1069	switch (ctl) {
1070	default:
1071		PRINTF("%s: isp_control unknown control op %x\n",
1072		    isp->isp_name, ctl);
1073		break;
1074
1075	case ISPCTL_RESET_BUS:
1076		mbs.param[0] = MBOX_BUS_RESET;
1077		mbs.param[1] = (isp->isp_type & ISP_HA_FC)? 5: 2;
1078		isp_mboxcmd(isp, &mbs);
1079		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1080			isp_dumpregs(isp, "isp_control SCSI bus reset failed");
1081			break;
1082		}
1083		/*
1084		 * This is really important to have set after a bus reset.
1085		 */
1086		isp->isp_sendmarker = 1;
1087		PRINTF("%s: driver initiated bus reset\n", isp->isp_name);
1088		return (0);
1089
1090        case ISPCTL_RESET_DEV:
1091		/*
1092		 * Note that under parallel SCSI, this issues a BDR message.
1093		 * Under FC, we could probably be using ABORT TASK SET
1094		 * command.
1095		 */
1096
1097		mbs.param[0] = MBOX_ABORT_TARGET;
1098		mbs.param[1] = ((long)arg) << 8;
1099		mbs.param[2] = 2;	/* 'delay', in seconds */
1100		isp_mboxcmd(isp, &mbs);
1101		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1102			isp_dumpregs(isp, "SCSI Target  reset failed");
1103			break;
1104		}
1105		PRINTF("%s: Target %d Reset Succeeded\n", isp->isp_name,
1106		    (int) ((long) arg));
1107		isp->isp_sendmarker = 1;
1108		return (0);
1109
1110        case ISPCTL_ABORT_CMD:
1111		xs = (ISP_SCSI_XFER_T *) arg;
1112		for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
1113			if (xs == isp->isp_xflist[i]) {
1114				break;
1115			}
1116		}
1117		if (i == RQUEST_QUEUE_LEN) {
1118			PRINTF("%s: isp_control- cannot find command to abort "
1119			    "in active list\n", isp->isp_name);
1120			break;
1121		}
1122		mbs.param[0] = MBOX_ABORT;
1123		mbs.param[1] = XS_TGT(xs) | XS_LUN(xs);
1124		mbs.param[2] = (i+1) >> 16;
1125		mbs.param[3] = (i+1) & 0xffff;
1126		isp_mboxcmd(isp, &mbs);
1127		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1128			PRINTF("%s: isp_control MBOX_ABORT failure (code %x)\n",
1129			    isp->isp_name, mbs.param[0]);
1130			break;
1131		}
1132		PRINTF("%s: command for target %d lun %d was aborted\n",
1133		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1134		return (0);
1135
1136	case ISPCTL_UPDATE_PARAMS:
1137		isp_update(isp);
1138		return(0);
1139	}
1140	return (-1);
1141}
1142
1143/*
1144 * Interrupt Service Routine(s).
1145 *
1146 * External (OS) framework has done the appropriate locking,
1147 * and the locking will be held throughout this function.
1148 */
1149
1150int
1151isp_intr(arg)
1152	void *arg;
1153{
1154	ISP_SCSI_XFER_T *complist[RESULT_QUEUE_LEN], *xs;
1155	struct ispsoftc *isp = arg;
1156	u_int8_t iptr, optr;
1157	u_int16_t isr;
1158	int i, ndone = 0;
1159
1160	isr = ISP_READ(isp, BIU_ISR);
1161	if (isp->isp_type & ISP_HA_FC) {
1162		if (isr == 0 || (isr & BIU2100_ISR_RISC_INT) == 0) {
1163			if (isr) {
1164				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
1165					     isp->isp_name, isr));
1166			}
1167			return (0);
1168		}
1169	} else {
1170		if (isr == 0 || (isr & BIU_ISR_RISC_INT) == 0) {
1171			if (isr) {
1172				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
1173					     isp->isp_name, isr));
1174			}
1175			return (0);
1176		}
1177	}
1178
1179	if (ISP_READ(isp, BIU_SEMA) & 1) {
1180		u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
1181		if (isp_parse_async(isp, mbox))
1182			return (1);
1183		ISP_WRITE(isp, BIU_SEMA, 0);
1184	}
1185
1186	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
1187
1188	optr = isp->isp_residx;
1189	iptr = ISP_READ(isp, OUTMAILBOX5);
1190
1191	if (optr == iptr) {
1192		IDPRINTF(4, ("why intr? isr %x iptr %x optr %x\n",
1193		    isr, optr, iptr));
1194	}
1195	ENABLE_INTS(isp);
1196
1197	while (optr != iptr) {
1198		ispstatusreq_t *sp;
1199		u_int8_t oop;
1200		int buddaboom = 0;
1201
1202		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
1203		oop = optr;
1204		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN);
1205
1206		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
1207			if (isp_handle_other_response(isp, sp, &optr) == 0) {
1208				ISP_WRITE(isp, INMAILBOX5, optr);
1209				continue;
1210			}
1211			/*
1212			 * It really has to be a bounced request just copied
1213			 * from the request queue to the response queue.
1214			 */
1215
1216			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
1217				ISP_WRITE(isp, INMAILBOX5, optr);
1218				continue;
1219			}
1220			PRINTF("%s: not RESPONSE in RESPONSE Queue "
1221			    "(type 0x%x) @ idx %d (next %d)\n", isp->isp_name,
1222			    sp->req_header.rqs_entry_type, oop, optr);
1223			buddaboom = 1;
1224		}
1225
1226		if (sp->req_header.rqs_flags & 0xf) {
1227			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
1228				ISP_WRITE(isp, INMAILBOX5, optr);
1229				continue;
1230			}
1231			PRINTF("%s: rqs_flags=%x", isp->isp_name,
1232				sp->req_header.rqs_flags & 0xf);
1233			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
1234				PRINTF("%s: internal queues full\n",
1235				    isp->isp_name);
1236				/* XXXX: this command *could* get restarted */
1237				buddaboom++;
1238			}
1239			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
1240				PRINTF("%s: bad header\n", isp->isp_name);
1241				buddaboom++;
1242			}
1243			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
1244				PRINTF("%s: bad request packet\n",
1245				    isp->isp_name);
1246				buddaboom++;
1247			}
1248		}
1249		if (sp->req_handle > RQUEST_QUEUE_LEN || sp->req_handle < 1) {
1250			PRINTF("%s: bad request handle %d\n", isp->isp_name,
1251				sp->req_handle);
1252			ISP_WRITE(isp, INMAILBOX5, optr);
1253			continue;
1254		}
1255		xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[sp->req_handle - 1];
1256		if (xs == NULL) {
1257			PRINTF("%s: NULL xs in xflist (handle %x)\n",
1258			    isp->isp_name, sp->req_handle);
1259			isp_dumpxflist(isp);
1260			ISP_WRITE(isp, INMAILBOX5, optr);
1261			continue;
1262		}
1263		isp->isp_xflist[sp->req_handle - 1] = NULL;
1264		if (sp->req_status_flags & RQSTF_BUS_RESET) {
1265			isp->isp_sendmarker = 1;
1266		}
1267		if (buddaboom) {
1268			XS_SETERR(xs, HBA_BOTCH);
1269		}
1270		XS_STS(xs) = sp->req_scsi_status & 0xff;
1271		if (isp->isp_type & ISP_HA_SCSI) {
1272			if (sp->req_state_flags & RQSF_GOT_SENSE) {
1273				bcopy(sp->req_sense_data, XS_SNSP(xs),
1274					XS_SNSLEN(xs));
1275				XS_SNS_IS_VALID(xs);
1276			}
1277		} else {
1278			if (XS_STS(xs) == SCSI_CHECK) {
1279				XS_SNS_IS_VALID(xs);
1280				bcopy(sp->req_sense_data, XS_SNSP(xs),
1281					XS_SNSLEN(xs));
1282				sp->req_state_flags |= RQSF_GOT_SENSE;
1283			}
1284		}
1285		if (XS_NOERR(xs) && XS_STS(xs) == SCSI_BUSY) {
1286			XS_SETERR(xs, HBA_TGTBSY);
1287		}
1288
1289		if (sp->req_header.rqs_entry_type == RQSTYPE_RESPONSE) {
1290			if (XS_NOERR(xs)) {
1291			    if (sp->req_completion_status != RQCS_COMPLETE) {
1292				isp_parse_status(isp, sp, xs);
1293			    } else {
1294				XS_SETERR(xs, HBA_NOERROR);
1295			    }
1296			}
1297		} else {
1298			PRINTF("%s: unknown return %x\n", isp->isp_name,
1299				sp->req_header.rqs_entry_type);
1300			if (XS_NOERR(xs))
1301				XS_SETERR(xs, HBA_BOTCH);
1302		}
1303		if (isp->isp_type & ISP_HA_SCSI) {
1304			XS_RESID(xs) = sp->req_resid;
1305		} else if (sp->req_scsi_status & RQCS_RU) {
1306			XS_RESID(xs) = sp->req_resid;
1307			IDPRINTF(4, ("%s: cnt %d rsd %d\n", isp->isp_name,
1308				XS_XFRLEN(xs), sp->req_resid));
1309		}
1310		if (XS_XFRLEN(xs)) {
1311			ISP_DMAFREE(isp, xs, sp->req_handle - 1);
1312		}
1313		/*
1314		 * XXX: If we have a check condition, but no Sense Data,
1315		 * XXX: mark it as an error (ARQ failed). We need to
1316		 * XXX: to do a more distinct job because there may
1317		 * XXX: cases where ARQ is disabled.
1318		 */
1319		if (XS_STS(xs) == SCSI_CHECK && !(XS_IS_SNS_VALID(xs))) {
1320			if (XS_NOERR(xs)) {
1321				PRINTF("%s: ARQ Failure\n", isp->isp_name);
1322				XS_SETERR(xs, HBA_ARQFAIL);
1323			}
1324		}
1325		if ((isp->isp_dblev >= 5) ||
1326		    (isp->isp_dblev > 2 && !XS_NOERR(xs))) {
1327			PRINTF("%s(%d.%d): FIN%d dl%d resid%d STS %x",
1328			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
1329			    sp->req_header.rqs_seqno, XS_XFRLEN(xs),
1330			    XS_RESID(xs), XS_STS(xs));
1331			if (sp->req_state_flags & RQSF_GOT_SENSE) {
1332				PRINTF(" Skey: %x", XS_SNSKEY(xs));
1333				if (!(XS_IS_SNS_VALID(xs))) {
1334					PRINTF(" BUT NOT SET");
1335				}
1336			}
1337			PRINTF(" XS_ERR=0x%x\n", (unsigned int) XS_ERR(xs));
1338		}
1339
1340		ISP_WRITE(isp, INMAILBOX5, optr);
1341		isp->isp_nactive--;
1342		if (isp->isp_nactive < 0)
1343			isp->isp_nactive = 0;
1344		complist[ndone++] = xs;	/* defer completion call until later */
1345	}
1346	/*
1347	 * If we completed any commands, then it's valid to find out
1348	 * what the outpointer is.
1349	 */
1350	if (ndone) {
1351	 	isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
1352	}
1353	isp->isp_residx = optr;
1354	for (i = 0; i < ndone; i++) {
1355		xs = complist[i];
1356		if (xs) {
1357			XS_CMD_DONE(xs);
1358		}
1359	}
1360	return (1);
1361}
1362
1363/*
1364 * Support routines.
1365 */
1366
1367static int
1368isp_parse_async(isp, mbox)
1369	struct ispsoftc *isp;
1370	u_int16_t mbox;
1371{
1372	switch (mbox) {
1373	case ASYNC_BUS_RESET:
1374		PRINTF("%s: SCSI bus reset detected\n", isp->isp_name);
1375		isp->isp_sendmarker = 1;
1376		break;
1377
1378	case ASYNC_SYSTEM_ERROR:
1379		mbox = ISP_READ(isp, OUTMAILBOX1);
1380		PRINTF("%s: Internal FW Error @ RISC Addr 0x%x\n",
1381		    isp->isp_name, mbox);
1382		isp_restart(isp);
1383		/* no point continuing after this */
1384		return (1);
1385
1386	case ASYNC_RQS_XFER_ERR:
1387		PRINTF("%s: Request Queue Transfer Error\n", isp->isp_name);
1388		break;
1389
1390	case ASYNC_RSP_XFER_ERR:
1391		PRINTF("%s: Response Queue Transfer Error\n", isp->isp_name);
1392		break;
1393
1394	case ASYNC_QWAKEUP:
1395		/* don't need to be chatty */
1396		mbox = ISP_READ(isp, OUTMAILBOX4);
1397		break;
1398
1399	case ASYNC_TIMEOUT_RESET:
1400		PRINTF("%s: timeout initiated SCSI bus reset\n", isp->isp_name);
1401		isp->isp_sendmarker = 1;
1402		break;
1403
1404	case ASYNC_UNSPEC_TMODE:
1405		PRINTF("%s: mystery async target completion\n", isp->isp_name);
1406		break;
1407
1408	case ASYNC_EXTMSG_UNDERRUN:
1409		PRINTF("%s: extended message underrun\n", isp->isp_name);
1410		break;
1411
1412	case ASYNC_SCAM_INT:
1413		PRINTF("%s: SCAM interrupt\n", isp->isp_name);
1414		break;
1415
1416	case ASYNC_HUNG_SCSI:
1417		PRINTF("%s: stalled SCSI Bus after DATA Overrun\n",
1418		    isp->isp_name);
1419		/* XXX: Need to issue SCSI reset at this point */
1420		break;
1421
1422	case ASYNC_KILLED_BUS:
1423		PRINTF("%s: SCSI Bus reset after DATA Overrun\n",
1424		    isp->isp_name);
1425		break;
1426
1427	case ASYNC_BUS_TRANSIT:
1428		PRINTF("%s: LBD->HVD Transition 0x%x\n",
1429		    isp->isp_name, ISP_READ(isp, OUTMAILBOX1));
1430		break;
1431
1432	case ASYNC_CMD_CMPLT:
1433		PRINTF("%s: fast post completion\n", isp->isp_name);
1434#if	0
1435		fast_post_handle = (ISP_READ(isp, OUTMAILBOX1) << 16) |
1436		    ISP_READ(isp, OUTMAILBOX2);
1437#endif
1438		break;
1439
1440	case ASYNC_CTIO_DONE:
1441		PRINTF("%s: CTIO done\n", isp->isp_name);
1442		break;
1443
1444	case ASYNC_LIP_OCCURRED:
1445		PRINTF("%s: LIP occurred\n", isp->isp_name);
1446		break;
1447
1448	case ASYNC_LOOP_UP:
1449		PRINTF("%s: Loop UP\n", isp->isp_name);
1450		break;
1451
1452	case ASYNC_LOOP_DOWN:
1453		PRINTF("%s: Loop DOWN\n", isp->isp_name);
1454		break;
1455
1456	case ASYNC_LOOP_RESET:
1457		PRINTF("%s: Loop RESET\n", isp->isp_name);
1458		break;
1459
1460	case ASYNC_PDB_CHANGED:
1461		PRINTF("%s: Port Database Changed\n", isp->isp_name);
1462		break;
1463
1464	case ASYNC_CHANGE_NOTIFY:
1465		PRINTF("%s: Name Server Database Changed\n", isp->isp_name);
1466		break;
1467
1468	default:
1469		PRINTF("%s: async %x\n", isp->isp_name, mbox);
1470		break;
1471	}
1472	return (0);
1473}
1474
1475static int
1476isp_handle_other_response(isp, sp, optrp)
1477	struct ispsoftc *isp;
1478	ispstatusreq_t *sp;
1479	u_int8_t *optrp;
1480{
1481	u_int8_t iptr, optr;
1482	int reqsize = 0;
1483	void *ireqp = NULL;
1484
1485	switch (sp->req_header.rqs_entry_type) {
1486	case RQSTYPE_REQUEST:
1487		return (-1);
1488#if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
1489	case RQSTYPE_NOTIFY_ACK:
1490	{
1491		ispnotify_t *spx = (ispnotify_t *) sp;
1492		PRINTF("%s: Immediate Notify Ack %d.%d Status 0x%x Sequence "
1493		    "0x%x\n", isp->isp_name, spx->req_initiator, spx->req_lun,
1494		    spx->req_status, spx->req_sequence);
1495		break;
1496	}
1497	case RQSTYPE_NOTIFY:
1498	{
1499		ispnotify_t *spx = (ispnotify_t *) sp;
1500
1501		PRINTF("%s: Notify loopid %d to lun %d req_status 0x%x "
1502		    "req_task_flags 0x%x seq 0x%x\n", isp->isp_name, 				    spx->req_initiator, spx->req_lun, spx->req_status,
1503		    spx->req_task_flags, spx->req_sequence);
1504		reqsize = sizeof (*spx);
1505		spx->req_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
1506		spx->req_header.rqs_entry_count = 1;
1507		spx->req_header.rqs_flags = 0;
1508		spx->req_header.rqs_seqno = isp->isp_seqno++;
1509		spx->req_handle = (spx->req_initiator<<16) | RQSTYPE_NOTIFY_ACK;
1510		if (spx->req_status == IN_RSRC_UNAVAIL)
1511			spx->req_flags = LUN_INCR_CMD;
1512		else if (spx->req_status == IN_NOCAP)
1513			spx->req_flags = LUN_INCR_IMMED;
1514		else {
1515			reqsize = 0;
1516		}
1517		ireqp = spx;
1518		break;
1519	}
1520	case RQSTYPE_ENABLE_LUN:
1521	{
1522		isplun_t *ip = (isplun_t *) sp;
1523		if (ip->req_status != 1) {
1524		    PRINTF("%s: ENABLE LUN returned status 0x%x\n",
1525			isp->isp_name, ip->req_status);
1526		}
1527		break;
1528	}
1529	case RQSTYPE_ATIO2:
1530	{
1531		fcparam *fcp = isp->isp_param;
1532		ispctiot2_t local, *ct2 = NULL;
1533		ispatiot2_t *at2 = (ispatiot2_t *) sp;
1534		int s;
1535
1536		PRINTF("%s: atio2 loopid %d for lun %d rxid 0x%x flags 0x%x "
1537		    "task flags 0x%x exec codes 0x%x\n", isp->isp_name,
1538		    at2->req_initiator, at2->req_lun, at2->req_rxid,
1539		    at2->req_flags, at2->req_taskflags, at2->req_execodes);
1540
1541		switch (at2->req_status & ~ATIO_SENSEVALID) {
1542		case ATIO_PATH_INVALID:
1543			PRINTF("%s: ATIO2 Path Invalid\n", isp->isp_name);
1544			break;
1545		case ATIO_NOCAP:
1546			PRINTF("%s: ATIO2 No Cap\n", isp->isp_name);
1547			break;
1548		case ATIO_BDR_MSG:
1549			PRINTF("%s: ATIO2 BDR Received\n", isp->isp_name);
1550			break;
1551		case ATIO_CDB_RECEIVED:
1552			ct2 = &local;
1553			break;
1554		default:
1555			PRINTF("%s: unknown req_status 0x%x\n", isp->isp_name,
1556			    at2->req_status);
1557			break;
1558		}
1559		if (ct2 == NULL) {
1560			/*
1561			 * Just do an ACCEPT on this fellow.
1562			 */
1563			at2->req_header.rqs_entry_type = RQSTYPE_ATIO2;
1564			at2->req_header.rqs_flags = 0;
1565			at2->req_flags = 1;
1566			ireqp = at2;
1567			reqsize = sizeof (*at2);
1568			break;
1569		}
1570		PRINTF("%s: datalen %d cdb0=0x%x\n", isp->isp_name,
1571		    at2->req_datalen, at2->req_cdb[0]);
1572		bzero ((void *) ct2, sizeof (*ct2));
1573		ct2->req_header.rqs_entry_type = RQSTYPE_CTIO2;
1574		ct2->req_header.rqs_entry_count = 1;
1575		ct2->req_header.rqs_flags = 0;
1576		ct2->req_header.rqs_seqno = isp->isp_seqno++;
1577		ct2->req_handle = (at2->req_initiator << 16) | at2->req_lun;
1578		ct2->req_lun = at2->req_lun;
1579		ct2->req_initiator = at2->req_initiator;
1580		ct2->req_rxid = at2->req_rxid;
1581
1582		ct2->req_flags = CTIO_SEND_STATUS;
1583		switch (at2->req_cdb[0]) {
1584		case 0x0:		/* TUR */
1585			ct2->req_flags |= CTIO_NODATA | CTIO2_SMODE0;
1586			ct2->req_m.mode0.req_scsi_status = CTIO2_STATUS_VALID;
1587			break;
1588
1589		case 0x3:		/* REQUEST SENSE */
1590		case 0x12:		/* INQUIRE */
1591			ct2->req_flags |= CTIO_SEND_DATA | CTIO2_SMODE0;
1592			ct2->req_m.mode0.req_scsi_status = CTIO2_STATUS_VALID;
1593			ct2->req_seg_count = 1;
1594			if (at2->req_cdb[0] == 0x12) {
1595				s = sizeof(tgtiqd);
1596				bcopy((void *)tgtiqd, fcp->isp_scratch, s);
1597			} else {
1598				s = at2->req_datalen;
1599				bzero(fcp->isp_scratch, s);
1600			}
1601			ct2->req_m.mode0.req_dataseg[0].ds_base =
1602			    fcp->isp_scdma;
1603			ct2->req_m.mode0.req_dataseg[0].ds_count = s;
1604			ct2->req_m.mode0.req_datalen = s;
1605#if	0
1606			if (at2->req_datalen < s) {
1607				ct2->req_m.mode1.req_scsi_status |=
1608				    CTIO2_RESP_VALID|CTIO2_RSPOVERUN;
1609			} else if (at2->req_datalen > s) {
1610				ct2->req_m.mode1.req_scsi_status |=
1611				    CTIO2_RESP_VALID|CTIO2_RSPUNDERUN;
1612			}
1613#endif
1614			break;
1615
1616		default:		/* ALL OTHERS */
1617			ct2->req_flags |= CTIO_NODATA | CTIO2_SMODE1;
1618			ct2->req_m.mode1.req_scsi_status = 0;
1619#if	0
1620			if (at2->req_datalen) {
1621				ct2->req_m.mode1.req_scsi_status |=
1622				    CTIO2_RSPUNDERUN;
1623#if	BYTE_ORDER == BIG_ENDIAN
1624				ct2->req_resid[1] = at2->req_datalen & 0xff;
1625				ct2->req_resid[0] =
1626					(at2->req_datalen >> 8) & 0xff;
1627				ct2->req_resid[3] =
1628					(at2->req_datalen >> 16) & 0xff;
1629				ct2->req_resid[2] =
1630					(at2->req_datalen >> 24) & 0xff;
1631#else
1632				ct2->req_resid[0] = at2->req_datalen & 0xff;
1633				ct2->req_resid[1] =
1634					(at2->req_datalen >> 8) & 0xff;
1635				ct2->req_resid[2] =
1636					(at2->req_datalen >> 16) & 0xff;
1637				ct2->req_resid[3] =
1638					(at2->req_datalen >> 24) & 0xff;
1639#endif
1640			}
1641#endif
1642			if ((at2->req_status & ATIO_SENSEVALID) == 0) {
1643				ct2->req_m.mode1.req_sense_len = 18;
1644				ct2->req_m.mode1.req_scsi_status |= 2;
1645				ct2->req_m.mode1.req_response[0] = 0x70;
1646				ct2->req_m.mode1.req_response[2] = 0x2;
1647			} else {
1648				ct2->req_m.mode1.req_sense_len = 18;
1649				ct2->req_m.mode1.req_scsi_status |=
1650				    at2->req_scsi_status;
1651				bcopy((void *)at2->req_sense,
1652				    (void *)ct2->req_m.mode1.req_response,
1653				    sizeof (at2->req_sense));
1654			}
1655			break;
1656		}
1657		reqsize = sizeof (*ct2);
1658		ireqp = ct2;
1659		break;
1660	}
1661	case RQSTYPE_CTIO2:
1662	{
1663		ispatiot2_t *at2;
1664		ispctiot2_t *ct2 = (ispctiot2_t *) sp;
1665		PRINTF("%s: CTIO2 returned status 0x%x\n", isp->isp_name,
1666		    ct2->req_status);
1667		/*
1668	 	 * Return the ATIO to the board.
1669		 */
1670		at2 = (ispatiot2_t *) sp;
1671		at2->req_header.rqs_entry_type = RQSTYPE_ATIO2;
1672		at2->req_header.rqs_entry_count = 1;
1673		at2->req_header.rqs_flags = 0;
1674		at2->req_header.rqs_seqno = isp->isp_seqno++;
1675		at2->req_status = 1;
1676		reqsize = sizeof (*at2);
1677		ireqp = at2;
1678		break;
1679	}
1680#endif
1681	default:
1682		PRINTF("%s: other response type %x\n", isp->isp_name,
1683		    sp->req_header.rqs_entry_type);
1684		break;
1685	}
1686	if (reqsize) {
1687		void *reqp;
1688		optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
1689		iptr = isp->isp_reqidx;
1690		reqp = (void *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
1691		iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
1692		if (iptr == optr) {
1693			PRINTF("%s: Request Queue Overflow other response\n",
1694			    isp->isp_name);
1695		} else {
1696			bcopy(ireqp, reqp, reqsize);
1697			ISP_WRITE(isp, INMAILBOX4, iptr);
1698			isp->isp_reqidx = iptr;
1699		}
1700	}
1701	return (0);
1702}
1703
1704#if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
1705/*
1706 * Locks held, and ints disabled (if FC).
1707 *
1708 * XXX: SETUP ONLY FOR INITIAL ENABLING RIGHT NOW
1709 */
1710static int
1711isp_modify_lun(isp, lun, icnt, ccnt)
1712	struct ispsoftc *isp;
1713	int lun;	/* logical unit to enable, modify, or disable */
1714	int icnt;	/* immediate notify count */
1715	int ccnt;	/* command count */
1716{
1717	isplun_t *ip = NULL;
1718	u_int8_t iptr, optr;
1719
1720	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
1721	iptr = isp->isp_reqidx;
1722	ip = (isplun_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
1723	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
1724	if (iptr == optr) {
1725		PRINTF("%s: Request Queue Overflow in isp_modify_lun\n",
1726		    isp->isp_name);
1727		return (-1);
1728	}
1729
1730	bzero((void *) ip, sizeof (*ip));
1731	ip->req_header.rqs_entry_type = RQSTYPE_ENABLE_LUN;
1732	ip->req_header.rqs_entry_count = 1;
1733	ip->req_header.rqs_flags = 0;
1734	ip->req_header.rqs_seqno = isp->isp_seqno++;
1735	ip->req_handle = RQSTYPE_ENABLE_LUN;
1736	ip->req_lun = lun;
1737	ip->req_cmdcount = ccnt;
1738	ip->req_imcount = icnt;
1739	ip->req_timeout = 0;	/* default 30 seconds */
1740	ISP_WRITE(isp, INMAILBOX4, iptr);
1741	isp->isp_reqidx = iptr;
1742	return (0);
1743}
1744#endif
1745
1746static void
1747isp_parse_status(isp, sp, xs)
1748	struct ispsoftc *isp;
1749	ispstatusreq_t *sp;
1750	ISP_SCSI_XFER_T *xs;
1751{
1752	switch (sp->req_completion_status) {
1753	case RQCS_COMPLETE:
1754		XS_SETERR(xs, HBA_NOERROR);
1755		return;
1756
1757	case RQCS_INCOMPLETE:
1758		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
1759			IDPRINTF(3, ("%s: Selection Timeout for target %d\n",
1760			    isp->isp_name, XS_TGT(xs)));
1761			XS_SETERR(xs, HBA_SELTIMEOUT);
1762			return;
1763		}
1764		PRINTF("%s: command incomplete for target %d lun %d, state "
1765		    "0x%x\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs),
1766		    sp->req_state_flags);
1767		break;
1768
1769	case RQCS_DMA_ERROR:
1770		PRINTF("%s: DMA error for command on target %d, lun %d\n",
1771		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1772		break;
1773
1774	case RQCS_TRANSPORT_ERROR:
1775		PRINTF("%s: transport error\n", isp->isp_name);
1776		isp_prtstst(sp);
1777		break;
1778
1779	case RQCS_RESET_OCCURRED:
1780		IDPRINTF(2, ("%s: bus reset destroyed command for target %d "
1781		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
1782		isp->isp_sendmarker = 1;
1783		XS_SETERR(xs, HBA_BUSRESET);
1784		return;
1785
1786	case RQCS_ABORTED:
1787		PRINTF("%s: command aborted for target %d lun %d\n",
1788		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1789		isp->isp_sendmarker = 1;
1790		XS_SETERR(xs, HBA_ABORTED);
1791		return;
1792
1793	case RQCS_TIMEOUT:
1794		IDPRINTF(2, ("%s: command timed out for target %d lun %d\n",
1795		    isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
1796		XS_SETERR(xs, HBA_CMDTIMEOUT);
1797		return;
1798
1799	case RQCS_DATA_OVERRUN:
1800		if (isp->isp_type & ISP_HA_FC) {
1801			XS_RESID(xs) = sp->req_resid;
1802			break;
1803		}
1804		XS_SETERR(xs, HBA_DATAOVR);
1805		return;
1806
1807	case RQCS_COMMAND_OVERRUN:
1808		PRINTF("%s: command overrun for command on target %d, lun %d\n",
1809		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1810		break;
1811
1812	case RQCS_STATUS_OVERRUN:
1813		PRINTF("%s: status overrun for command on target %d, lun %d\n",
1814		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1815		break;
1816
1817	case RQCS_BAD_MESSAGE:
1818		PRINTF("%s: message not COMMAND COMPLETE after status on "
1819		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
1820		    XS_LUN(xs));
1821		break;
1822
1823	case RQCS_NO_MESSAGE_OUT:
1824		PRINTF("%s: No MESSAGE OUT phase after selection on "
1825		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
1826		    XS_LUN(xs));
1827		break;
1828
1829	case RQCS_EXT_ID_FAILED:
1830		PRINTF("%s: EXTENDED IDENTIFY failed on target %d, lun %d\n",
1831		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1832		break;
1833
1834	case RQCS_IDE_MSG_FAILED:
1835		PRINTF("%s: target %d lun %d rejected INITIATOR DETECTED "
1836		    "ERROR message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1837		break;
1838
1839	case RQCS_ABORT_MSG_FAILED:
1840		PRINTF("%s: target %d lun %d rejected ABORT message\n",
1841		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1842		break;
1843
1844	case RQCS_REJECT_MSG_FAILED:
1845		PRINTF("%s: target %d lun %d rejected MESSAGE REJECT message\n",
1846		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1847		break;
1848
1849	case RQCS_NOP_MSG_FAILED:
1850		PRINTF("%s: target %d lun %d rejected NOP message\n",
1851		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1852		break;
1853
1854	case RQCS_PARITY_ERROR_MSG_FAILED:
1855		PRINTF("%s: target %d lun %d rejected MESSAGE PARITY ERROR "
1856		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1857		break;
1858
1859	case RQCS_DEVICE_RESET_MSG_FAILED:
1860		PRINTF("%s: target %d lun %d rejected BUS DEVICE RESET "
1861		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1862		break;
1863
1864	case RQCS_ID_MSG_FAILED:
1865		PRINTF("%s: target %d lun %d rejected IDENTIFY "
1866		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1867		break;
1868
1869	case RQCS_UNEXP_BUS_FREE:
1870		PRINTF("%s: target %d lun %d had unexeptected bus free\n",
1871		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1872		break;
1873
1874	case RQCS_DATA_UNDERRUN:
1875		if (isp->isp_type & ISP_HA_FC) {
1876			XS_RESID(xs) = sp->req_resid;
1877			/* an UNDERRUN is not a botch ??? */
1878		}
1879		XS_SETERR(xs, HBA_NOERROR);
1880		return;
1881
1882	case RQCS_XACT_ERR1:
1883		PRINTF("%s: HBA attempted queued transaction with disconnect "
1884		    "not set for target %d lun %d\n", isp->isp_name, XS_TGT(xs),
1885		    XS_LUN(xs));
1886		break;
1887
1888	case RQCS_XACT_ERR2:
1889		PRINTF("%s: HBA attempted queued transaction to target "
1890		    "routine %d on target %d\n", isp->isp_name, XS_LUN(xs),
1891		    XS_TGT(xs));
1892		break;
1893
1894	case RQCS_XACT_ERR3:
1895		PRINTF("%s: HBA attempted queued transaction for target %d lun "
1896		    "%d when queueing disabled\n", isp->isp_name, XS_TGT(xs),
1897		    XS_LUN(xs));
1898		break;
1899
1900	case RQCS_BAD_ENTRY:
1901		PRINTF("%s: invalid IOCB entry type detected\n", isp->isp_name);
1902		break;
1903
1904	case RQCS_QUEUE_FULL:
1905		PRINTF("%s: internal queues full for target %d lun %d\n",
1906		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1907		break;
1908
1909	case RQCS_PHASE_SKIPPED:
1910		PRINTF("%s: SCSI phase skipped (e.g., COMMAND COMPLETE w/o "
1911		    "STATUS phase) for target %d lun %d\n", isp->isp_name,
1912		    XS_TGT(xs), XS_LUN(xs));
1913		break;
1914
1915	case RQCS_ARQS_FAILED:
1916		PRINTF("%s: Auto Request Sense failed for target %d lun %d\n",
1917		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1918		XS_SETERR(xs, HBA_ARQFAIL);
1919		return;
1920
1921	case RQCS_WIDE_FAILED:
1922		PRINTF("%s: Wide Negotiation failed for target %d lun %d\n",
1923		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1924		if (isp->isp_type & ISP_HA_SCSI) {
1925			sdparam *sdp = isp->isp_param;
1926			isp->isp_update = 1;
1927			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
1928			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
1929		}
1930		XS_SETERR(xs, HBA_NOERROR);
1931		return;
1932
1933	case RQCS_SYNCXFER_FAILED:
1934		PRINTF("%s: SDTR Message failed for target %d lun %d\n",
1935		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1936		if (isp->isp_type & ISP_HA_SCSI) {
1937			sdparam *sdp = isp->isp_param;
1938			isp->isp_update = 1;
1939			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
1940			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
1941		}
1942		break;
1943
1944	case RQCS_LVD_BUSERR:
1945		PRINTF("%s: Bad LVD Bus condition while talking to target %d "
1946		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1947		break;
1948
1949	case RQCS_PORT_UNAVAILABLE:
1950		/*
1951		 * No such port on the loop. Moral equivalent of SELTIMEO
1952		 */
1953		IDPRINTF(3, ("%s: Port Unavailable for target %d\n",
1954		    isp->isp_name, XS_TGT(xs)));
1955		XS_SETERR(xs, HBA_SELTIMEOUT);
1956		return;
1957
1958	case RQCS_PORT_LOGGED_OUT:
1959		/*
1960		 * It was there (maybe)- treat as a selection timeout.
1961		 */
1962		PRINTF("%s: port logout for target %d\n",
1963			isp->isp_name, XS_TGT(xs));
1964		XS_SETERR(xs, HBA_SELTIMEOUT);
1965		return;
1966
1967	case RQCS_PORT_CHANGED:
1968		PRINTF("%s: port changed for target %d\n",
1969			isp->isp_name, XS_TGT(xs));
1970		break;
1971
1972	case RQCS_PORT_BUSY:
1973		PRINTF("%s: port busy for target %d\n",
1974			isp->isp_name, XS_TGT(xs));
1975		XS_SETERR(xs, HBA_TGTBSY);
1976		return;
1977
1978	default:
1979		PRINTF("%s: comp status %x\n", isp->isp_name,
1980		       sp->req_completion_status);
1981		break;
1982	}
1983	XS_SETERR(xs, HBA_BOTCH);
1984}
1985
1986#define	HINIB(x)			((x) >> 0x4)
1987#define	LONIB(x)			((x)  & 0xf)
1988#define MAKNIB(a, b)			(((a) << 4) | (b))
1989static u_int8_t mbpcnt[] = {
1990	MAKNIB(1, 1),	/* 0x00: MBOX_NO_OP */
1991	MAKNIB(5, 5),	/* 0x01: MBOX_LOAD_RAM */
1992	MAKNIB(2, 0),	/* 0x02: MBOX_EXEC_FIRMWARE */
1993	MAKNIB(5, 5),	/* 0x03: MBOX_DUMP_RAM */
1994	MAKNIB(3, 3),	/* 0x04: MBOX_WRITE_RAM_WORD */
1995	MAKNIB(2, 3),	/* 0x05: MBOX_READ_RAM_WORD */
1996	MAKNIB(6, 6),	/* 0x06: MBOX_MAILBOX_REG_TEST */
1997	MAKNIB(2, 3),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
1998	MAKNIB(1, 3),	/* 0x08: MBOX_ABOUT_FIRMWARE */
1999	MAKNIB(0, 0),	/* 0x09: */
2000	MAKNIB(0, 0),	/* 0x0a: */
2001	MAKNIB(0, 0),	/* 0x0b: */
2002	MAKNIB(0, 0),	/* 0x0c: */
2003	MAKNIB(0, 0),	/* 0x0d: */
2004	MAKNIB(1, 2),	/* 0x0e: MBOX_CHECK_FIRMWARE */
2005	MAKNIB(0, 0),	/* 0x0f: */
2006	MAKNIB(5, 5),	/* 0x10: MBOX_INIT_REQ_QUEUE */
2007	MAKNIB(6, 6),	/* 0x11: MBOX_INIT_RES_QUEUE */
2008	MAKNIB(4, 4),	/* 0x12: MBOX_EXECUTE_IOCB */
2009	MAKNIB(2, 2),	/* 0x13: MBOX_WAKE_UP	*/
2010	MAKNIB(1, 6),	/* 0x14: MBOX_STOP_FIRMWARE */
2011	MAKNIB(4, 4),	/* 0x15: MBOX_ABORT */
2012	MAKNIB(2, 2),	/* 0x16: MBOX_ABORT_DEVICE */
2013	MAKNIB(3, 3),	/* 0x17: MBOX_ABORT_TARGET */
2014	MAKNIB(2, 2),	/* 0x18: MBOX_BUS_RESET */
2015	MAKNIB(2, 3),	/* 0x19: MBOX_STOP_QUEUE */
2016	MAKNIB(2, 3),	/* 0x1a: MBOX_START_QUEUE */
2017	MAKNIB(2, 3),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
2018	MAKNIB(2, 3),	/* 0x1c: MBOX_ABORT_QUEUE */
2019	MAKNIB(2, 4),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
2020	MAKNIB(0, 0),	/* 0x1e: */
2021	MAKNIB(1, 3),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
2022	MAKNIB(1, 3),	/* 0x20: MBOX_GET_INIT_SCSI_ID, MBOX_GET_LOOP_ID */
2023	MAKNIB(1, 2),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
2024	MAKNIB(1, 3),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
2025	MAKNIB(1, 2),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
2026	MAKNIB(1, 2),	/* 0x24: MBOX_GET_CLOCK_RATE */
2027	MAKNIB(1, 2),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
2028	MAKNIB(1, 2),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
2029	MAKNIB(1, 3),	/* 0x27: MBOX_GET_PCI_PARAMS */
2030	MAKNIB(2, 4),	/* 0x28: MBOX_GET_TARGET_PARAMS */
2031	MAKNIB(2, 4),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
2032	MAKNIB(0, 0),	/* 0x2a: */
2033	MAKNIB(0, 0),	/* 0x2b: */
2034	MAKNIB(0, 0),	/* 0x2c: */
2035	MAKNIB(0, 0),	/* 0x2d: */
2036	MAKNIB(0, 0),	/* 0x2e: */
2037	MAKNIB(0, 0),	/* 0x2f: */
2038	MAKNIB(2, 2),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
2039	MAKNIB(2, 2),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
2040	MAKNIB(3, 3),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
2041	MAKNIB(2, 2),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
2042	MAKNIB(2, 2),	/* 0x34: MBOX_SET_CLOCK_RATE */
2043	MAKNIB(2, 2),	/* 0x35: MBOX_SET_ACTIVE_NEG_STATE */
2044	MAKNIB(2, 2),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
2045	MAKNIB(3, 3),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
2046	MAKNIB(4, 4),	/* 0x38: MBOX_SET_TARGET_PARAMS */
2047	MAKNIB(4, 4),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
2048	MAKNIB(0, 0),	/* 0x3a: */
2049	MAKNIB(0, 0),	/* 0x3b: */
2050	MAKNIB(0, 0),	/* 0x3c: */
2051	MAKNIB(0, 0),	/* 0x3d: */
2052	MAKNIB(0, 0),	/* 0x3e: */
2053	MAKNIB(0, 0),	/* 0x3f: */
2054	MAKNIB(1, 2),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
2055	MAKNIB(6, 1),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
2056	MAKNIB(2, 3),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
2057	MAKNIB(0, 0),	/* 0x43: */
2058	MAKNIB(0, 0),	/* 0x44: */
2059	MAKNIB(0, 0),	/* 0x45: */
2060	MAKNIB(0, 0),	/* 0x46: */
2061	MAKNIB(0, 0),	/* 0x47: */
2062	MAKNIB(0, 0),	/* 0x48: */
2063	MAKNIB(0, 0),	/* 0x49: */
2064	MAKNIB(0, 0),	/* 0x4a: */
2065	MAKNIB(0, 0),	/* 0x4b: */
2066	MAKNIB(0, 0),	/* 0x4c: */
2067	MAKNIB(0, 0),	/* 0x4d: */
2068	MAKNIB(0, 0),	/* 0x4e: */
2069	MAKNIB(0, 0),	/* 0x4f: */
2070	MAKNIB(0, 0),	/* 0x50: */
2071	MAKNIB(0, 0),	/* 0x51: */
2072	MAKNIB(0, 0),	/* 0x52: */
2073	MAKNIB(0, 0),	/* 0x53: */
2074	MAKNIB(8, 0),	/* 0x54: MBOX_EXEC_COMMAND_IOCB_A64 */
2075	MAKNIB(0, 0),	/* 0x55: */
2076	MAKNIB(0, 0),	/* 0x56: */
2077	MAKNIB(0, 0),	/* 0x57: */
2078	MAKNIB(0, 0),	/* 0x58: */
2079	MAKNIB(0, 0),	/* 0x59: */
2080	MAKNIB(0, 0),	/* 0x5a: */
2081	MAKNIB(0, 0),	/* 0x5b: */
2082	MAKNIB(0, 0),	/* 0x5c: */
2083	MAKNIB(0, 0),	/* 0x5d: */
2084	MAKNIB(0, 0),	/* 0x5e: */
2085	MAKNIB(0, 0),	/* 0x5f: */
2086	MAKNIB(8, 6),	/* 0x60: MBOX_INIT_FIRMWARE */
2087	MAKNIB(0, 0),	/* 0x60: MBOX_GET_INIT_CONTROL_BLOCK  (FORMAT?) */
2088	MAKNIB(2, 1),	/* 0x62: MBOX_INIT_LIP */
2089	MAKNIB(8, 1),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
2090	MAKNIB(8, 1),	/* 0x64: MBOX_GET_PORT_DB */
2091	MAKNIB(3, 1),	/* 0x65: MBOX_CLEAR_ACA */
2092	MAKNIB(3, 1),	/* 0x66: MBOX_TARGET_RESET */
2093	MAKNIB(3, 1),	/* 0x67: MBOX_CLEAR_TASK_SET */
2094	MAKNIB(3, 1),	/* 0x69: MBOX_ABORT_TASK_SET */
2095	MAKNIB(1, 2)	/* 0x69: MBOX_GET_FW_STATE */
2096};
2097#define	NMBCOM	(sizeof (mbpcnt) / sizeof (mbpcnt[0]))
2098
2099static void
2100isp_mboxcmd(isp, mbp)
2101	struct ispsoftc *isp;
2102	mbreg_t *mbp;
2103{
2104	int outparam, inparam;
2105	int loops, dld = 0;
2106	u_int8_t opcode;
2107
2108	if (mbp->param[0] == ISP2100_SET_PCI_PARAM) {
2109		opcode = mbp->param[0] = MBOX_SET_PCI_PARAMETERS;
2110		inparam = 4;
2111		outparam = 4;
2112		goto command_known;
2113	} else if (mbp->param[0] > NMBCOM) {
2114		PRINTF("%s: bad command %x\n", isp->isp_name, mbp->param[0]);
2115		return;
2116	}
2117
2118	opcode = mbp->param[0];
2119	inparam = HINIB(mbpcnt[mbp->param[0]]);
2120	outparam =  LONIB(mbpcnt[mbp->param[0]]);
2121
2122	if (inparam == 0 && outparam == 0) {
2123		PRINTF("%s: no parameters for %x\n", isp->isp_name,
2124			mbp->param[0]);
2125		return;
2126	}
2127
2128
2129command_known:
2130
2131	/*
2132	 * Make sure we can send some words..
2133	 */
2134
2135	loops = MBOX_DELAY_COUNT;
2136	while ((ISP_READ(isp, HCCR) & HCCR_HOST_INT) != 0) {
2137		SYS_DELAY(100);
2138		if (--loops < 0) {
2139			PRINTF("%s: isp_mboxcmd timeout #1\n", isp->isp_name);
2140			if (dld++) {
2141				return;
2142			}
2143			PRINTF("%s: but we'll try again, isr=%x\n",
2144			    isp->isp_name, ISP_READ(isp, BIU_ISR));
2145			if (ISP_READ(isp, BIU_SEMA) & 1) {
2146				u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
2147				if (isp_parse_async(isp, mbox))
2148					return;
2149				ISP_WRITE(isp, BIU_SEMA, 0);
2150			}
2151			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2152			goto command_known;
2153		}
2154	}
2155
2156	/*
2157	 * Write input parameters
2158	 */
2159	switch (inparam) {
2160	case 8: ISP_WRITE(isp, INMAILBOX7, mbp->param[7]); mbp->param[7] = 0;
2161	case 7: ISP_WRITE(isp, INMAILBOX6, mbp->param[6]); mbp->param[6] = 0;
2162	case 6: ISP_WRITE(isp, INMAILBOX5, mbp->param[5]); mbp->param[5] = 0;
2163	case 5: ISP_WRITE(isp, INMAILBOX4, mbp->param[4]); mbp->param[4] = 0;
2164	case 4: ISP_WRITE(isp, INMAILBOX3, mbp->param[3]); mbp->param[3] = 0;
2165	case 3: ISP_WRITE(isp, INMAILBOX2, mbp->param[2]); mbp->param[2] = 0;
2166	case 2: ISP_WRITE(isp, INMAILBOX1, mbp->param[1]); mbp->param[1] = 0;
2167	case 1: ISP_WRITE(isp, INMAILBOX0, mbp->param[0]); mbp->param[0] = 0;
2168	}
2169
2170	/*
2171	 * Clear semaphore on mailbox registers
2172	 */
2173	ISP_WRITE(isp, BIU_SEMA, 0);
2174
2175	/*
2176	 * Clear RISC int condition.
2177	 */
2178	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2179
2180	/*
2181	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
2182	 */
2183	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
2184
2185	/*
2186	 * Wait until RISC int is set, except 2100
2187	 */
2188	if ((isp->isp_type & ISP_HA_FC) == 0) {
2189		loops = MBOX_DELAY_COUNT;
2190		while ((ISP_READ(isp, BIU_ISR) & BIU_ISR_RISC_INT) == 0) {
2191			SYS_DELAY(100);
2192			if (--loops < 0) {
2193				PRINTF("%s: isp_mboxcmd timeout #2\n",
2194				    isp->isp_name);
2195				return;
2196			}
2197		}
2198	}
2199
2200	/*
2201	 * Check to make sure that the semaphore has been set.
2202	 */
2203	loops = MBOX_DELAY_COUNT;
2204	while ((ISP_READ(isp, BIU_SEMA) & 1) == 0) {
2205		SYS_DELAY(100);
2206		if (--loops < 0) {
2207			PRINTF("%s: isp_mboxcmd timeout #3\n", isp->isp_name);
2208			return;
2209		}
2210	}
2211
2212	/*
2213	 * Make sure that the MBOX_BUSY has gone away
2214	 */
2215	loops = MBOX_DELAY_COUNT;
2216	while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
2217		SYS_DELAY(100);
2218		if (--loops < 0) {
2219			PRINTF("%s: isp_mboxcmd timeout #4\n", isp->isp_name);
2220			return;
2221		}
2222	}
2223
2224
2225	/*
2226	 * Pick up output parameters.
2227	 */
2228	switch (outparam) {
2229	case 8: mbp->param[7] = ISP_READ(isp, OUTMAILBOX7);
2230	case 7: mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
2231	case 6: mbp->param[5] = ISP_READ(isp, OUTMAILBOX5);
2232	case 5: mbp->param[4] = ISP_READ(isp, OUTMAILBOX4);
2233	case 4: mbp->param[3] = ISP_READ(isp, OUTMAILBOX3);
2234	case 3: mbp->param[2] = ISP_READ(isp, OUTMAILBOX2);
2235	case 2: mbp->param[1] = ISP_READ(isp, OUTMAILBOX1);
2236	case 1: mbp->param[0] = ISP_READ(isp, OUTMAILBOX0);
2237	}
2238
2239	/*
2240	 * Clear RISC int.
2241	 */
2242	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2243
2244	/*
2245	 * Release semaphore on mailbox registers
2246	 */
2247	ISP_WRITE(isp, BIU_SEMA, 0);
2248
2249	/*
2250	 * Just to be chatty here...
2251	 */
2252	switch(mbp->param[0]) {
2253	case MBOX_COMMAND_COMPLETE:
2254		break;
2255	case MBOX_INVALID_COMMAND:
2256		IDPRINTF(2, ("%s: mbox cmd %x failed with INVALID_COMMAND\n",
2257		    isp->isp_name, opcode));
2258		break;
2259	case MBOX_HOST_INTERFACE_ERROR:
2260		PRINTF("%s: mbox cmd %x failed with HOST_INTERFACE_ERROR\n",
2261		    isp->isp_name, opcode);
2262		break;
2263	case MBOX_TEST_FAILED:
2264		PRINTF("%s: mbox cmd %x failed with TEST_FAILED\n",
2265		    isp->isp_name, opcode);
2266		break;
2267	case MBOX_COMMAND_ERROR:
2268		PRINTF("%s: mbox cmd %x failed with COMMAND_ERROR\n",
2269		    isp->isp_name, opcode);
2270		break;
2271	case MBOX_COMMAND_PARAM_ERROR:
2272		PRINTF("%s: mbox cmd %x failed with COMMAND_PARAM_ERROR\n",
2273		    isp->isp_name, opcode);
2274		break;
2275
2276	case ASYNC_LOOP_UP:
2277	case ASYNC_LIP_OCCURRED:
2278		break;
2279
2280	default:
2281		/*
2282		 * The expected return of EXEC_FIRMWARE is zero.
2283		 */
2284		if ((opcode == MBOX_EXEC_FIRMWARE && mbp->param[0] != 0) ||
2285		    (opcode != MBOX_EXEC_FIRMWARE)) {
2286			PRINTF("%s: mbox cmd %x failed with error %x\n",
2287				isp->isp_name, opcode, mbp->param[0]);
2288		}
2289		break;
2290	}
2291}
2292
2293void
2294isp_lostcmd(isp, xs)
2295	struct ispsoftc *isp;
2296	ISP_SCSI_XFER_T *xs;
2297{
2298	mbreg_t mbs;
2299
2300	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
2301	isp_mboxcmd(isp, &mbs);
2302	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2303		isp_dumpregs(isp, "couldn't GET FIRMWARE STATUS");
2304		return;
2305	}
2306	if (mbs.param[1]) {
2307		PRINTF("%s: %d commands on completion queue\n",
2308		       isp->isp_name, mbs.param[1]);
2309	}
2310	if (XS_NULL(xs))
2311		return;
2312
2313	mbs.param[0] = MBOX_GET_DEV_QUEUE_STATUS;
2314	mbs.param[1] = (XS_TGT(xs) << 8) | XS_LUN(xs);
2315	isp_mboxcmd(isp, &mbs);
2316	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2317		isp_dumpregs(isp, "couldn't GET DEVICE QUEUE STATUS");
2318		return;
2319	}
2320	PRINTF("%s: lost command for target %d lun %d, %d active of %d, "
2321		"Queue State: %x\n", isp->isp_name, XS_TGT(xs),
2322		XS_LUN(xs), mbs.param[2], mbs.param[3], mbs.param[1]);
2323
2324	isp_dumpregs(isp, "lost command");
2325	/*
2326	 * XXX: Need to try and do something to recover.
2327	 */
2328}
2329
2330static void
2331isp_dumpregs(isp, msg)
2332	struct ispsoftc *isp;
2333	const char *msg;
2334{
2335	PRINTF("%s: %s\n", isp->isp_name, msg);
2336	if (isp->isp_type & ISP_HA_SCSI)
2337		PRINTF("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
2338	else
2339		PRINTF("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
2340	PRINTF(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
2341	       ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
2342	PRINTF("risc_hccr=%x\n", ISP_READ(isp, HCCR));
2343
2344	if (isp->isp_type & ISP_HA_SCSI) {
2345		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
2346		PRINTF("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
2347			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
2348			ISP_READ(isp, CDMA_FIFO_STS));
2349		PRINTF("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
2350			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
2351			ISP_READ(isp, DDMA_FIFO_STS));
2352		PRINTF("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
2353			ISP_READ(isp, SXP_INTERRUPT),
2354			ISP_READ(isp, SXP_GROSS_ERR),
2355			ISP_READ(isp, SXP_PINS_CONTROL));
2356		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
2357	}
2358	ISP_DUMPREGS(isp);
2359}
2360
2361static void
2362isp_dumpxflist(isp)
2363	struct ispsoftc *isp;
2364{
2365	volatile ISP_SCSI_XFER_T *xs;
2366	int i, hdp;
2367
2368	for (hdp = i = 0; i < RQUEST_QUEUE_LEN; i++) {
2369		xs = isp->isp_xflist[i];
2370		if (xs == NULL) {
2371			continue;
2372		}
2373		if (hdp == 0) {
2374			PRINTF("%s: active requests\n", isp->isp_name);
2375			hdp++;
2376		}
2377		PRINTF(" Active Handle %d: tgt %d lun %d dlen %d\n",
2378		    i+1, XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs));
2379	}
2380}
2381
2382static void
2383isp_fw_state(isp)
2384	struct ispsoftc *isp;
2385{
2386	mbreg_t mbs;
2387	if (isp->isp_type & ISP_HA_FC) {
2388		int once = 0;
2389		fcparam *fcp = isp->isp_param;
2390again:
2391		mbs.param[0] = MBOX_GET_FW_STATE;
2392		isp_mboxcmd(isp, &mbs);
2393		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2394			if (mbs.param[0] == ASYNC_LIP_OCCURRED ||
2395			    mbs.param[0] == ASYNC_LOOP_UP) {
2396				if (once++ < 2) {
2397					goto again;
2398				}
2399			}
2400			isp_dumpregs(isp, "GET FIRMWARE STATE failed");
2401			return;
2402		}
2403		fcp->isp_fwstate = mbs.param[1];
2404	}
2405}
2406
2407static void
2408isp_update(isp)
2409	struct ispsoftc *isp;
2410{
2411	int tgt;
2412	mbreg_t mbs;
2413	sdparam *sdp;
2414
2415	isp->isp_update = 0;
2416
2417	if (isp->isp_type & ISP_HA_FC) {
2418		return;
2419	}
2420
2421	sdp = isp->isp_param;
2422	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
2423		if (sdp->isp_devparam[tgt].dev_enable == 0) {
2424			continue;
2425		}
2426		if (sdp->isp_devparam[tgt].dev_update == 0) {
2427			continue;
2428		}
2429
2430		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
2431		mbs.param[1] = tgt << 8;
2432		mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
2433		mbs.param[3] =
2434			(sdp->isp_devparam[tgt].sync_offset << 8) |
2435			(sdp->isp_devparam[tgt].sync_period);
2436
2437		IDPRINTF(3, ("\n%s: tgt %d cflags %x offset %x period %x\n",
2438		    isp->isp_name, tgt, mbs.param[2], mbs.param[3] >> 8,
2439		    mbs.param[3] & 0xff));
2440
2441		isp_mboxcmd(isp, &mbs);
2442		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2443			PRINTF("%s: failed to change SCSI parameters for "
2444			    "target %d\n", isp->isp_name, tgt);
2445		} else {
2446			char *wt;
2447			int x, flags;
2448
2449			flags = sdp->isp_devparam[tgt].cur_dflags =
2450			    sdp->isp_devparam[tgt].dev_flags;
2451
2452			x = sdp->isp_devparam[tgt].sync_period & 0xff;
2453			if (flags & DPARM_SYNC) {
2454				if (x == (ISP_20M_SYNCPARMS & 0xff)) {
2455					x = 20;
2456				} else if (x == (ISP_10M_SYNCPARMS & 0xff)) {
2457					x = 10;
2458				} else if (x == (ISP_08M_SYNCPARMS & 0xff)) {
2459					x = 8;
2460				} else if (x == (ISP_05M_SYNCPARMS & 0xff)) {
2461					x = 5;
2462				} else if (x == (ISP_04M_SYNCPARMS & 0xff)) {
2463					x = 4;
2464				} else {
2465					x = 0;
2466				}
2467			} else {
2468				x = 0;
2469			}
2470			switch (flags & (DPARM_WIDE|DPARM_TQING)) {
2471			case DPARM_WIDE:
2472				wt = ", 16 bit wide\n";
2473				break;
2474			case DPARM_TQING:
2475				wt = ", Tagged Queueing Enabled\n";
2476				break;
2477			case DPARM_WIDE|DPARM_TQING:
2478				wt = ", 16 bit wide, Tagged Queueing Enabled\n";
2479				break;
2480
2481			default:
2482				wt = "\n";
2483				break;
2484			}
2485			if (x) {
2486				IDPRINTF(3, ("%s: Target %d maximum Sync Mode "
2487				    "at %dMHz%s", isp->isp_name, tgt, x, wt));
2488			} else {
2489				IDPRINTF(3, ("%s: Target %d Async Mode%s",
2490				    isp->isp_name, tgt, wt));
2491			}
2492		}
2493		sdp->isp_devparam[tgt].dev_update = 0;
2494	}
2495}
2496
2497static void
2498isp_setdfltparm(isp)
2499	struct ispsoftc *isp;
2500{
2501	int i, use_nvram;
2502	mbreg_t mbs;
2503	sdparam *sdp;
2504
2505	/*
2506	 * Been there, done that, got the T-shirt...
2507	 */
2508	if (isp->isp_gotdparms) {
2509		IDPRINTF(3, ("%s: already have dparms\n", isp->isp_name));
2510		return;
2511	}
2512	isp->isp_gotdparms = 1;
2513
2514	use_nvram = (isp_read_nvram(isp) == 0);
2515	if (use_nvram) {
2516		return;
2517	}
2518	if (isp->isp_type & ISP_HA_FC) {
2519		fcparam *fcp = (fcparam *) isp->isp_param;
2520		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
2521		fcp->isp_maxalloc = 256;
2522		fcp->isp_execthrottle = 16;
2523		fcp->isp_retry_delay = 5;
2524		fcp->isp_retry_count = 0;
2525		/*
2526		 * It would be nice to fake up a WWN in case we don't
2527		 * get one out of NVRAM. Solaris does this for SOCAL
2528		 * cards that don't have SBus properties- it sets up
2529		 * a WWN based upon the system MAC Address.
2530		 */
2531		fcp->isp_wwn = 0;
2532		return;
2533	}
2534
2535	sdp = (sdparam *) isp->isp_param;
2536	mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
2537	isp_mboxcmd(isp, &mbs);
2538	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2539		IDPRINTF(2, ("could not GET ACT NEG STATE\n"));
2540		sdp->isp_req_ack_active_neg = 1;
2541		sdp->isp_data_line_active_neg = 1;
2542	} else {
2543		sdp->isp_req_ack_active_neg = (mbs.param[1] >> 4) & 0x1;
2544		sdp->isp_data_line_active_neg = (mbs.param[1] >> 5) & 0x1;
2545	}
2546	for (i = 0; i < MAX_TARGETS; i++) {
2547
2548		mbs.param[0] = MBOX_GET_TARGET_PARAMS;
2549		mbs.param[1] = i << 8;
2550		isp_mboxcmd(isp, &mbs);
2551		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2552			PRINTF("%s: can't get SCSI parameters for target %d\n",
2553			    isp->isp_name, i);
2554			sdp->isp_devparam[i].sync_period = 0;
2555			sdp->isp_devparam[i].sync_offset = 0;
2556			sdp->isp_devparam[i].dev_flags = DPARM_SAFE_DFLT;
2557			continue;
2558		}
2559		sdp->isp_devparam[i].dev_flags = mbs.param[2];
2560
2561		/*
2562		 * The maximum period we can really see
2563		 * here is 100 (decimal), or 400 ns.
2564		 * For some unknown reason we sometimes
2565		 * get back wildass numbers from the
2566		 * boot device's parameters.
2567		 *
2568		 * XXX: Hmm- this may be based on a different
2569		 * XXX: clock rate.
2570		 */
2571		if ((mbs.param[3] & 0xff) <= 0x64) {
2572			sdp->isp_devparam[i].sync_period = mbs.param[3] & 0xff;
2573			sdp->isp_devparam[i].sync_offset = mbs.param[3] >> 8;
2574		}
2575
2576		/*
2577		 * It is not safe to run Ultra Mode with a clock < 60.
2578		 */
2579		if (((sdp->isp_clock && sdp->isp_clock < 60) ||
2580		    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
2581		    (sdp->isp_devparam[i].sync_period ==
2582		    (ISP_20M_SYNCPARMS & 0xff))) {
2583			sdp->isp_devparam[i].sync_offset =
2584				ISP_10M_SYNCPARMS >> 8;
2585			sdp->isp_devparam[i].sync_period =
2586				ISP_10M_SYNCPARMS & 0xff;
2587		}
2588
2589	}
2590
2591	/*
2592	 * Set Default Host Adapter Parameters
2593	 */
2594	sdp->isp_cmd_dma_burst_enable = 1;
2595	sdp->isp_data_dma_burst_enabl = 1;
2596	sdp->isp_fifo_threshold = 0;
2597	sdp->isp_initiator_id = 7;
2598	if (isp->isp_type >= ISP_HA_SCSI_1040) {
2599		sdp->isp_async_data_setup = 9;
2600	} else {
2601		sdp->isp_async_data_setup = 6;
2602	}
2603	sdp->isp_selection_timeout = 250;
2604	sdp->isp_max_queue_depth = 128;
2605	sdp->isp_tag_aging = 8;
2606	sdp->isp_bus_reset_delay = 3;
2607	sdp->isp_retry_count = 0;
2608	sdp->isp_retry_delay = 1;
2609
2610	for (i = 0; i < MAX_TARGETS; i++) {
2611		sdp->isp_devparam[i].exc_throttle = 16;
2612		sdp->isp_devparam[i].dev_enable = 1;
2613	}
2614}
2615
2616/*
2617 * Re-initialize the ISP and complete all orphaned commands
2618 * with a 'botched' notice.
2619 *
2620 * Locks held prior to coming here.
2621 */
2622
2623void
2624isp_restart(isp)
2625	struct ispsoftc *isp;
2626{
2627	ISP_SCSI_XFER_T *tlist[RQUEST_QUEUE_LEN], *xs;
2628	int i;
2629
2630	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
2631		tlist[i] = (ISP_SCSI_XFER_T *) isp->isp_xflist[i];
2632		isp->isp_xflist[i] = NULL;
2633	}
2634	isp_reset(isp);
2635	if (isp->isp_state == ISP_RESETSTATE) {
2636		isp_init(isp);
2637		if (isp->isp_state == ISP_INITSTATE) {
2638			isp->isp_state = ISP_RUNSTATE;
2639		}
2640	}
2641	if (isp->isp_state != ISP_RUNSTATE) {
2642		PRINTF("%s: isp_restart cannot restart ISP\n", isp->isp_name);
2643	}
2644
2645	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
2646		xs = tlist[i];
2647		if (XS_NULL(xs)) {
2648			continue;
2649		}
2650		isp->isp_nactive--;
2651		if (isp->isp_nactive < 0) {
2652			isp->isp_nactive = 0;
2653		}
2654		XS_RESID(xs) = XS_XFRLEN(xs);
2655		XS_SETERR(xs, HBA_BUSRESET);
2656		XS_CMD_DONE(xs);
2657	}
2658}
2659
2660void
2661isp_watch(arg)
2662	void *arg;
2663{
2664	int i;
2665	struct ispsoftc *isp = arg;
2666	ISP_SCSI_XFER_T *xs;
2667	ISP_LOCKVAL_DECL;
2668
2669	/*
2670	 * Look for completely dead commands (but not polled ones).
2671	 */
2672	ISP_ILOCK(isp);
2673	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
2674		if ((xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[i]) == NULL) {
2675			continue;
2676		}
2677		if (XS_TIME(xs) == 0) {
2678			continue;
2679		}
2680		XS_TIME(xs) -= (WATCH_INTERVAL * 1000);
2681		/*
2682		 * Avoid later thinking that this
2683		 * transaction is not being timed.
2684		 * Then give ourselves to watchdog
2685		 * periods of grace.
2686		 */
2687		if (XS_TIME(xs) == 0)
2688			XS_TIME(xs) = 1;
2689		else if (XS_TIME(xs) > -(2 * WATCH_INTERVAL * 1000)) {
2690			continue;
2691		}
2692		if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
2693			PRINTF("%s: isp_watch failed to abort command\n",
2694			    isp->isp_name);
2695			isp_restart(isp);
2696			break;
2697		}
2698	}
2699	ISP_IUNLOCK(isp);
2700	RESTART_WATCHDOG(isp_watch, isp);
2701}
2702
2703static void
2704isp_prtstst(sp)
2705	ispstatusreq_t *sp;
2706{
2707	PRINTF("states->");
2708	if (sp->req_state_flags & RQSF_GOT_BUS)
2709		PRINTF("GOT_BUS ");
2710	if (sp->req_state_flags & RQSF_GOT_TARGET)
2711		PRINTF("GOT_TGT ");
2712	if (sp->req_state_flags & RQSF_SENT_CDB)
2713		PRINTF("SENT_CDB ");
2714	if (sp->req_state_flags & RQSF_XFRD_DATA)
2715		PRINTF("XFRD_DATA ");
2716	if (sp->req_state_flags & RQSF_GOT_STATUS)
2717		PRINTF("GOT_STS ");
2718	if (sp->req_state_flags & RQSF_GOT_SENSE)
2719		PRINTF("GOT_SNS ");
2720	if (sp->req_state_flags & RQSF_XFER_COMPLETE)
2721		PRINTF("XFR_CMPLT ");
2722	PRINTF("\n");
2723	PRINTF("status->");
2724	if (sp->req_status_flags & RQSTF_DISCONNECT)
2725		PRINTF("Disconnect ");
2726	if (sp->req_status_flags & RQSTF_SYNCHRONOUS)
2727		PRINTF("Sync_xfr ");
2728	if (sp->req_status_flags & RQSTF_PARITY_ERROR)
2729		PRINTF("Parity ");
2730	if (sp->req_status_flags & RQSTF_BUS_RESET)
2731		PRINTF("Bus_Reset ");
2732	if (sp->req_status_flags & RQSTF_DEVICE_RESET)
2733		PRINTF("Device_Reset ");
2734	if (sp->req_status_flags & RQSTF_ABORTED)
2735		PRINTF("Aborted ");
2736	if (sp->req_status_flags & RQSTF_TIMEOUT)
2737		PRINTF("Timeout ");
2738	if (sp->req_status_flags & RQSTF_NEGOTIATION)
2739		PRINTF("Negotiation ");
2740	PRINTF("\n");
2741}
2742
2743/*
2744 * NVRAM Routines
2745 */
2746
2747static int
2748isp_read_nvram(isp)
2749	struct ispsoftc *isp;
2750{
2751	int i, amt;
2752	u_int8_t csum, minversion;
2753	union {
2754		u_int8_t _x[ISP2100_NVRAM_SIZE];
2755		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
2756	} _n;
2757#define	nvram_data	_n._x
2758#define	nvram_words	_n._s
2759
2760	if (isp->isp_type & ISP_HA_FC) {
2761		amt = ISP2100_NVRAM_SIZE;
2762		minversion = 1;
2763	} else {
2764		amt = ISP_NVRAM_SIZE;
2765		minversion = 2;
2766	}
2767
2768	/*
2769	 * Just read the first two words first to see if we have a valid
2770	 * NVRAM to continue reading the rest with.
2771	 */
2772	for (i = 0; i < 2; i++) {
2773		isp_rdnvram_word(isp, i, &nvram_words[i]);
2774	}
2775	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
2776	    nvram_data[2] != 'P') {
2777		if (isp->isp_bustype != ISP_BT_SBUS) {
2778			PRINTF("%s: invalid NVRAM header\n", isp->isp_name);
2779		}
2780		return (-1);
2781	}
2782	for (i = 2; i < amt>>1; i++) {
2783		isp_rdnvram_word(isp, i, &nvram_words[i]);
2784	}
2785	for (csum = 0, i = 0; i < amt; i++) {
2786		csum += nvram_data[i];
2787	}
2788	if (csum != 0) {
2789		PRINTF("%s: invalid NVRAM checksum\n", isp->isp_name);
2790		return (-1);
2791	}
2792	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
2793		PRINTF("%s: version %d NVRAM not understood\n", isp->isp_name,
2794		    ISP_NVRAM_VERSION(nvram_data));
2795		return (-1);
2796	}
2797
2798	if (isp->isp_type & ISP_HA_SCSI) {
2799		sdparam *sdp = (sdparam *) isp->isp_param;
2800
2801		/* XXX CHECK THIS FOR SANITY XXX */
2802		sdp->isp_fifo_threshold =
2803			ISP_NVRAM_FIFO_THRESHOLD(nvram_data);
2804
2805		sdp->isp_initiator_id =
2806			ISP_NVRAM_INITIATOR_ID(nvram_data);
2807
2808		sdp->isp_bus_reset_delay =
2809			ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
2810
2811		sdp->isp_retry_count =
2812			ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
2813
2814		sdp->isp_retry_delay =
2815			ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
2816
2817		sdp->isp_async_data_setup =
2818			ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
2819
2820		if (isp->isp_type >= ISP_HA_SCSI_1040) {
2821			if (sdp->isp_async_data_setup < 9) {
2822				sdp->isp_async_data_setup = 9;
2823			}
2824		} else {
2825			if (sdp->isp_async_data_setup != 6) {
2826				sdp->isp_async_data_setup = 6;
2827			}
2828		}
2829
2830		sdp->isp_req_ack_active_neg =
2831			ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
2832
2833		sdp->isp_data_line_active_neg =
2834			ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
2835
2836		sdp->isp_data_dma_burst_enabl =
2837			ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
2838
2839		sdp->isp_cmd_dma_burst_enable =
2840			ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
2841
2842		sdp->isp_tag_aging =
2843			ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
2844
2845		/* XXX ISP_NVRAM_FIFO_THRESHOLD_128 XXX */
2846
2847		sdp->isp_selection_timeout =
2848			ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
2849
2850		sdp->isp_max_queue_depth =
2851			ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
2852
2853		sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
2854
2855		for (i = 0; i < 16; i++) {
2856			sdp->isp_devparam[i].dev_enable =
2857				ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
2858			sdp->isp_devparam[i].exc_throttle =
2859				ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
2860			sdp->isp_devparam[i].sync_offset =
2861				ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
2862			sdp->isp_devparam[i].sync_period =
2863				ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
2864
2865			if (isp->isp_type < ISP_HA_SCSI_1040) {
2866				/*
2867				 * If we're not ultra, we can't possibly
2868				 * be a shorter period than this.
2869				 */
2870				if (sdp->isp_devparam[i].sync_period < 0x19) {
2871					sdp->isp_devparam[i].sync_period =
2872					    0x19;
2873				}
2874				if (sdp->isp_devparam[i].sync_offset > 0xc) {
2875					sdp->isp_devparam[i].sync_offset =
2876					    0x0c;
2877				}
2878			} else {
2879				if (sdp->isp_devparam[i].sync_offset > 0x8) {
2880					sdp->isp_devparam[i].sync_offset = 0x8;
2881				}
2882			}
2883
2884			sdp->isp_devparam[i].dev_flags = 0;
2885
2886			if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
2887				sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
2888			if (ISP_NVRAM_TGT_QFRZ(nvram_data, i)) {
2889				PRINTF("%s: not supporting QFRZ option for "
2890				    "target %d\n", isp->isp_name, i);
2891			}
2892			sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
2893			if (ISP_NVRAM_TGT_ARQ(nvram_data, i) == 0) {
2894				PRINTF("%s: not disabling ARQ option for "
2895				    "target %d\n", isp->isp_name, i);
2896			}
2897			if (ISP_NVRAM_TGT_TQING(nvram_data, i))
2898				sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
2899			if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
2900				sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
2901			if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
2902				sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
2903			if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
2904				sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
2905			if (ISP_NVRAM_TGT_DISC(nvram_data, i))
2906				sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
2907		}
2908	} else {
2909		fcparam *fcp = (fcparam *) isp->isp_param;
2910		union {
2911			struct {
2912#if	BYTE_ORDER == BIG_ENDIAN
2913				u_int32_t hi32;
2914				u_int32_t lo32;
2915#else
2916				u_int32_t lo32;
2917				u_int32_t hi32;
2918#endif
2919			} wds;
2920			u_int64_t full64;
2921		} wwnstore;
2922
2923		wwnstore.full64 = ISP2100_NVRAM_NODE_NAME(nvram_data);
2924		PRINTF("%s: Adapter WWN 0x%08x%08x\n", isp->isp_name,
2925		    wwnstore.wds.hi32, wwnstore.wds.lo32);
2926		fcp->isp_wwn = wwnstore.full64;
2927		wwnstore.full64 = ISP2100_NVRAM_BOOT_NODE_NAME(nvram_data);
2928		if (wwnstore.full64 != 0) {
2929			PRINTF("%s: BOOT DEVICE WWN 0x%08x%08x\n", isp->isp_name,
2930			    wwnstore.wds.hi32, wwnstore.wds.lo32);
2931		}
2932		fcp->isp_maxalloc =
2933			ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
2934		fcp->isp_maxfrmlen =
2935			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
2936		fcp->isp_retry_delay =
2937			ISP2100_NVRAM_RETRY_DELAY(nvram_data);
2938		fcp->isp_retry_count =
2939			ISP2100_NVRAM_RETRY_COUNT(nvram_data);
2940		fcp->isp_loopid =
2941			ISP2100_NVRAM_HARDLOOPID(nvram_data);
2942		fcp->isp_execthrottle =
2943			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
2944	}
2945	return (0);
2946}
2947
2948static void
2949isp_rdnvram_word(isp, wo, rp)
2950	struct ispsoftc *isp;
2951	int wo;
2952	u_int16_t *rp;
2953{
2954	int i, cbits;
2955	u_int16_t bit, rqst;
2956
2957	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
2958	SYS_DELAY(2);
2959	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
2960	SYS_DELAY(2);
2961
2962	if (isp->isp_type & ISP_HA_FC) {
2963		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
2964		rqst = (ISP_NVRAM_READ << 8) | wo;
2965		cbits = 10;
2966	} else {
2967		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
2968		rqst = (ISP_NVRAM_READ << 6) | wo;
2969		cbits = 8;
2970	}
2971
2972	/*
2973	 * Clock the word select request out...
2974	 */
2975	for (i = cbits; i >= 0; i--) {
2976		if ((rqst >> i) & 1) {
2977			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
2978		} else {
2979			bit = BIU_NVRAM_SELECT;
2980		}
2981		ISP_WRITE(isp, BIU_NVRAM, bit);
2982		SYS_DELAY(2);
2983		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
2984		SYS_DELAY(2);
2985		ISP_WRITE(isp, BIU_NVRAM, bit);
2986		SYS_DELAY(2);
2987	}
2988	/*
2989	 * Now read the result back in (bits come back in MSB format).
2990	 */
2991	*rp = 0;
2992	for (i = 0; i < 16; i++) {
2993		u_int16_t rv;
2994		*rp <<= 1;
2995		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
2996		SYS_DELAY(2);
2997		rv = ISP_READ(isp, BIU_NVRAM);
2998		if (rv & BIU_NVRAM_DATAIN) {
2999			*rp |= 1;
3000		}
3001		SYS_DELAY(2);
3002		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
3003		SYS_DELAY(2);
3004	}
3005	ISP_WRITE(isp, BIU_NVRAM, 0);
3006	SYS_DELAY(2);
3007#if	BYTE_ORDER == BIG_ENDIAN
3008	*rp = ((*rp >> 8) | ((*rp & 0xff) << 8));
3009#endif
3010}
3011