Home | History | Annotate | Line # | Download | only in ic
isp.c revision 1.39.2.7
      1  1.39.2.7   bouyer /* $NetBSD: isp.c,v 1.39.2.7 2001/03/27 15:31:55 bouyer Exp $ */
      2       1.1      cgd /*
      3  1.39.2.2   bouyer  * This driver, which is contained in NetBSD in the files:
      4  1.39.2.2   bouyer  *
      5  1.39.2.2   bouyer  *	sys/dev/ic/isp.c
      6  1.39.2.4   bouyer  *	sys/dev/ic/isp_inline.h
      7  1.39.2.4   bouyer  *	sys/dev/ic/isp_netbsd.c
      8  1.39.2.4   bouyer  *	sys/dev/ic/isp_netbsd.h
      9  1.39.2.4   bouyer  *	sys/dev/ic/isp_target.c
     10  1.39.2.4   bouyer  *	sys/dev/ic/isp_target.h
     11  1.39.2.4   bouyer  *	sys/dev/ic/isp_tpublic.h
     12  1.39.2.4   bouyer  *	sys/dev/ic/ispmbox.h
     13  1.39.2.4   bouyer  *	sys/dev/ic/ispreg.h
     14  1.39.2.4   bouyer  *	sys/dev/ic/ispvar.h
     15  1.39.2.2   bouyer  *	sys/microcode/isp/asm_sbus.h
     16  1.39.2.2   bouyer  *	sys/microcode/isp/asm_1040.h
     17  1.39.2.2   bouyer  *	sys/microcode/isp/asm_1080.h
     18  1.39.2.2   bouyer  *	sys/microcode/isp/asm_12160.h
     19  1.39.2.2   bouyer  *	sys/microcode/isp/asm_2100.h
     20  1.39.2.2   bouyer  *	sys/microcode/isp/asm_2200.h
     21  1.39.2.2   bouyer  *	sys/pci/isp_pci.c
     22  1.39.2.2   bouyer  *	sys/sbus/isp_sbus.c
     23  1.39.2.2   bouyer  *
     24  1.39.2.2   bouyer  * Is being actively maintained by Matthew Jacob (mjacob (at) netbsd.org).
     25  1.39.2.2   bouyer  * This driver also is shared source with FreeBSD, OpenBSD, Linux, Solaris,
     26  1.39.2.2   bouyer  * Linux versions. This tends to be an interesting maintenance problem.
     27  1.39.2.2   bouyer  *
     28  1.39.2.2   bouyer  * Please coordinate with Matthew Jacob on changes you wish to make here.
     29  1.39.2.2   bouyer  */
     30  1.39.2.2   bouyer /*
     31  1.39.2.2   bouyer  * Machine and OS Independent (well, as best as possible)
     32  1.39.2.2   bouyer  * code for the Qlogic ISP SCSI adapters.
     33  1.39.2.2   bouyer  *
     34  1.39.2.7   bouyer  * Copyright (c) 1997, 1998, 1999, 2000, 2001 by Matthew Jacob
     35  1.39.2.2   bouyer  * NASA/Ames Research Center
     36       1.1      cgd  * All rights reserved.
     37       1.1      cgd  *
     38       1.1      cgd  * Redistribution and use in source and binary forms, with or without
     39       1.1      cgd  * modification, are permitted provided that the following conditions
     40       1.1      cgd  * are met:
     41       1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     42  1.39.2.2   bouyer  *    notice immediately at the beginning of the file, without modification,
     43  1.39.2.2   bouyer  *    this list of conditions, and the following disclaimer.
     44  1.39.2.2   bouyer  * 2. The name of the author may not be used to endorse or promote products
     45  1.39.2.2   bouyer  *    derived from this software without specific prior written permission.
     46       1.1      cgd  *
     47  1.39.2.2   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     48  1.39.2.2   bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49  1.39.2.2   bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50  1.39.2.2   bouyer  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     51  1.39.2.2   bouyer  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52  1.39.2.2   bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53  1.39.2.2   bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  1.39.2.2   bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55  1.39.2.2   bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56  1.39.2.2   bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  1.39.2.2   bouyer  * SUCH DAMAGE.
     58       1.1      cgd  */
     59  1.39.2.6   bouyer 
     60       1.1      cgd /*
     61       1.1      cgd  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
     62      1.23   mjacob  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
     63      1.23   mjacob  * ideas dredged from the Solaris driver.
     64       1.1      cgd  */
     65       1.1      cgd 
     66      1.23   mjacob /*
     67      1.23   mjacob  * Include header file appropriate for platform we're building on.
     68      1.23   mjacob  */
     69       1.1      cgd 
     70      1.23   mjacob #ifdef	__NetBSD__
     71      1.23   mjacob #include <dev/ic/isp_netbsd.h>
     72      1.23   mjacob #endif
     73      1.23   mjacob #ifdef	__FreeBSD__
     74      1.23   mjacob #include <dev/isp/isp_freebsd.h>
     75      1.23   mjacob #endif
     76      1.33   mjacob #ifdef	__OpenBSD__
     77      1.33   mjacob #include <dev/ic/isp_openbsd.h>
     78      1.33   mjacob #endif
     79      1.23   mjacob #ifdef	__linux__
     80      1.29   mjacob #include "isp_linux.h"
     81      1.23   mjacob #endif
     82  1.39.2.2   bouyer #ifdef	__svr4__
     83  1.39.2.2   bouyer #include "isp_solaris.h"
     84  1.39.2.2   bouyer #endif
     85       1.1      cgd 
     86      1.23   mjacob /*
     87      1.23   mjacob  * General defines
     88      1.23   mjacob  */
     89       1.1      cgd 
     90      1.23   mjacob #define	MBOX_DELAY_COUNT	1000000 / 100
     91       1.1      cgd 
     92      1.23   mjacob /*
     93      1.27   mjacob  * Local static data
     94      1.27   mjacob  */
     95  1.39.2.4   bouyer static const char warnlun[] =
     96  1.39.2.2   bouyer     "WARNING- cannot determine Expanded LUN capability- limiting to one LUN";
     97  1.39.2.4   bouyer static const char portshift[] =
     98  1.39.2.2   bouyer     "Target %d Loop ID 0x%x (Port 0x%x) => Loop 0x%x (Port 0x%x)";
     99  1.39.2.4   bouyer static const char portdup[] =
    100  1.39.2.2   bouyer     "Target %d duplicates Target %d- killing off both";
    101  1.39.2.4   bouyer static const char retained[] =
    102  1.39.2.2   bouyer     "Retaining Loop ID 0x%x for Target %d (Port 0x%x)";
    103  1.39.2.4   bouyer static const char lretained[] =
    104  1.39.2.2   bouyer     "Retained login of Target %d (Loop ID 0x%x) Port 0x%x";
    105  1.39.2.4   bouyer static const char plogout[] =
    106  1.39.2.2   bouyer     "Logging out Target %d at Loop ID 0x%x (Port 0x%x)";
    107  1.39.2.4   bouyer static const char plogierr[] =
    108  1.39.2.2   bouyer     "Command Error in PLOGI for Port 0x%x (0x%x)";
    109  1.39.2.4   bouyer static const char nopdb[] =
    110  1.39.2.2   bouyer     "Could not get PDB for Device @ Port 0x%x";
    111  1.39.2.4   bouyer static const char pdbmfail1[] =
    112  1.39.2.2   bouyer     "PDB Loop ID info for Device @ Port 0x%x does not match up (0x%x)";
    113  1.39.2.4   bouyer static const char pdbmfail2[] =
    114  1.39.2.2   bouyer     "PDB Port info for Device @ Port 0x%x does not match up (0x%x)";
    115  1.39.2.4   bouyer static const char ldumped[] =
    116  1.39.2.2   bouyer     "Target %d (Loop ID 0x%x) Port 0x%x dumped after login info mismatch";
    117  1.39.2.4   bouyer static const char notresp[] =
    118  1.39.2.3   bouyer   "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
    119  1.39.2.4   bouyer static const char xact1[] =
    120  1.39.2.2   bouyer     "HBA attempted queued transaction with disconnect not set for %d.%d.%d";
    121  1.39.2.4   bouyer static const char xact2[] =
    122  1.39.2.2   bouyer     "HBA attempted queued transaction to target routine %d on target %d bus %d";
    123  1.39.2.4   bouyer static const char xact3[] =
    124  1.39.2.2   bouyer     "HBA attempted queued cmd for %d.%d.%d when queueing disabled";
    125  1.39.2.4   bouyer static const char pskip[] =
    126  1.39.2.2   bouyer     "SCSI phase skipped for target %d.%d.%d";
    127  1.39.2.4   bouyer static const char topology[] =
    128  1.39.2.2   bouyer     "Loop ID %d, AL_PA 0x%x, Port ID 0x%x, Loop State 0x%x, Topology '%s'";
    129  1.39.2.4   bouyer static const char finmsg[] =
    130  1.39.2.6   bouyer     "(%d.%d.%d): FIN dl%d resid %d STS 0x%x SKEY %c XS_ERR=0x%x";
    131      1.27   mjacob /*
    132      1.25   mjacob  * Local function prototypes.
    133      1.23   mjacob  */
    134      1.28   mjacob static int isp_parse_async __P((struct ispsoftc *, int));
    135      1.25   mjacob static int isp_handle_other_response
    136  1.39.2.2   bouyer __P((struct ispsoftc *, ispstatusreq_t *, u_int16_t *));
    137      1.23   mjacob static void isp_parse_status
    138  1.39.2.2   bouyer __P((struct ispsoftc *, ispstatusreq_t *, XS_T *));
    139  1.39.2.7   bouyer static void isp_fastpost_complete __P((struct ispsoftc *, u_int16_t));
    140      1.36   mjacob static void isp_scsi_init __P((struct ispsoftc *));
    141      1.36   mjacob static void isp_scsi_channel_init __P((struct ispsoftc *, int));
    142      1.10   mjacob static void isp_fibre_init __P((struct ispsoftc *));
    143      1.33   mjacob static void isp_mark_getpdb_all __P((struct ispsoftc *));
    144  1.39.2.6   bouyer static int isp_getmap __P((struct ispsoftc *, fcpos_map_t *));
    145      1.33   mjacob static int isp_getpdb __P((struct ispsoftc *, int, isp_pdb_t *));
    146      1.37   mjacob static u_int64_t isp_get_portname __P((struct ispsoftc *, int, int));
    147      1.34   mjacob static int isp_fclink_test __P((struct ispsoftc *, int));
    148  1.39.2.2   bouyer static char *isp2100_fw_statename __P((int));
    149  1.39.2.6   bouyer static int isp_pdb_sync __P((struct ispsoftc *));
    150  1.39.2.6   bouyer static int isp_scan_loop __P((struct ispsoftc *));
    151      1.37   mjacob static int isp_scan_fabric __P((struct ispsoftc *));
    152  1.39.2.5   bouyer static void isp_register_fc4_type __P((struct ispsoftc *));
    153      1.10   mjacob static void isp_fw_state __P((struct ispsoftc *));
    154  1.39.2.2   bouyer static void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *, int));
    155       1.1      cgd 
    156      1.36   mjacob static void isp_update __P((struct ispsoftc *));
    157      1.36   mjacob static void isp_update_bus __P((struct ispsoftc *, int));
    158      1.36   mjacob static void isp_setdfltparm __P((struct ispsoftc *, int));
    159      1.25   mjacob static int isp_read_nvram __P((struct ispsoftc *));
    160      1.25   mjacob static void isp_rdnvram_word __P((struct ispsoftc *, int, u_int16_t *));
    161  1.39.2.2   bouyer static void isp_parse_nvram_1020 __P((struct ispsoftc *, u_int8_t *));
    162  1.39.2.2   bouyer static void isp_parse_nvram_1080 __P((struct ispsoftc *, int, u_int8_t *));
    163  1.39.2.2   bouyer static void isp_parse_nvram_12160 __P((struct ispsoftc *, int, u_int8_t *));
    164  1.39.2.2   bouyer static void isp_parse_nvram_2100 __P((struct ispsoftc *, u_int8_t *));
    165      1.25   mjacob 
    166       1.1      cgd /*
    167       1.1      cgd  * Reset Hardware.
    168      1.25   mjacob  *
    169  1.39.2.2   bouyer  * Hit the chip over the head, download new f/w if available and set it running.
    170      1.25   mjacob  *
    171      1.24   mjacob  * Locking done elsewhere.
    172       1.1      cgd  */
    173       1.1      cgd void
    174  1.39.2.7   bouyer isp_reset(struct ispsoftc *isp)
    175       1.1      cgd {
    176       1.1      cgd 	mbreg_t mbs;
    177  1.39.2.2   bouyer 	int loops, i, touched, dodnld = 1;
    178  1.39.2.5   bouyer 	char *revname = "????";
    179       1.1      cgd 
    180       1.1      cgd 	isp->isp_state = ISP_NILSTATE;
    181       1.4   mjacob 
    182  1.39.2.2   bouyer 
    183       1.4   mjacob 	/*
    184      1.25   mjacob 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
    185      1.25   mjacob 	 * have been set in the MD code. We figure out more
    186      1.25   mjacob 	 * here.
    187  1.39.2.2   bouyer 	 *
    188      1.32   mjacob 	 * After we've fired this chip up, zero out the conf1 register
    189  1.39.2.2   bouyer 	 * for SCSI adapters and do other settings for the 2100.
    190      1.32   mjacob 	 */
    191      1.32   mjacob 
    192      1.32   mjacob 	/*
    193      1.31   mjacob 	 * Get the current running firmware revision out of the
    194      1.31   mjacob 	 * chip before we hit it over the head (if this is our
    195      1.31   mjacob 	 * first time through). Note that we store this as the
    196      1.31   mjacob 	 * 'ROM' firmware revision- which it may not be. In any
    197      1.31   mjacob 	 * case, we don't really use this yet, but we may in
    198      1.31   mjacob 	 * the future.
    199      1.31   mjacob 	 */
    200  1.39.2.2   bouyer 	if ((touched = isp->isp_touched) == 0) {
    201      1.35   mjacob 		/*
    202  1.39.2.2   bouyer 		 * First see whether or not we're sitting in the ISP PROM.
    203  1.39.2.2   bouyer 		 * If we've just been reset, we'll have the string "ISP   "
    204  1.39.2.2   bouyer 		 * spread through outgoing mailbox registers 1-3.
    205  1.39.2.2   bouyer 		 */
    206  1.39.2.2   bouyer 		if (ISP_READ(isp, OUTMAILBOX1) != 0x4953 ||
    207  1.39.2.2   bouyer 		    ISP_READ(isp, OUTMAILBOX2) != 0x5020 ||
    208  1.39.2.2   bouyer 		    ISP_READ(isp, OUTMAILBOX3) != 0x2020) {
    209  1.39.2.2   bouyer 			/*
    210  1.39.2.2   bouyer 			 * Just in case it was paused...
    211  1.39.2.2   bouyer 			 */
    212  1.39.2.2   bouyer 			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
    213  1.39.2.2   bouyer 			mbs.param[0] = MBOX_ABOUT_FIRMWARE;
    214  1.39.2.4   bouyer 			isp_mboxcmd(isp, &mbs, MBOX_COMMAND_ERROR);
    215  1.39.2.2   bouyer 			/*
    216  1.39.2.2   bouyer 			 * This *shouldn't* fail.....
    217  1.39.2.2   bouyer 			 */
    218  1.39.2.2   bouyer 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
    219  1.39.2.2   bouyer 				isp->isp_romfw_rev[0] = mbs.param[1];
    220  1.39.2.2   bouyer 				isp->isp_romfw_rev[1] = mbs.param[2];
    221  1.39.2.2   bouyer 				isp->isp_romfw_rev[2] = mbs.param[3];
    222  1.39.2.2   bouyer 			}
    223      1.30   mjacob 		}
    224  1.39.2.2   bouyer 		isp->isp_touched = 1;
    225      1.30   mjacob 	}
    226      1.30   mjacob 
    227      1.35   mjacob 	DISABLE_INTS(isp);
    228      1.35   mjacob 
    229      1.31   mjacob 	/*
    230  1.39.2.6   bouyer 	 * Put the board into PAUSE mode (so we can read the SXP registers
    231  1.39.2.6   bouyer 	 * or write FPM/FBM registers).
    232      1.31   mjacob 	 */
    233      1.31   mjacob 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
    234      1.31   mjacob 
    235      1.37   mjacob 	if (IS_FC(isp)) {
    236      1.37   mjacob 		switch (isp->isp_type) {
    237      1.37   mjacob 		case ISP_HA_FC_2100:
    238  1.39.2.4   bouyer 			revname = "2100";
    239      1.37   mjacob 			break;
    240      1.37   mjacob 		case ISP_HA_FC_2200:
    241  1.39.2.4   bouyer 			revname = "2200";
    242      1.37   mjacob 			break;
    243      1.37   mjacob 		default:
    244      1.37   mjacob 			break;
    245      1.37   mjacob 		}
    246  1.39.2.6   bouyer 		/*
    247  1.39.2.6   bouyer 		 * While we're paused, reset the FPM module and FBM fifos.
    248  1.39.2.6   bouyer 		 */
    249  1.39.2.6   bouyer 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
    250  1.39.2.6   bouyer 		ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
    251  1.39.2.6   bouyer 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
    252  1.39.2.6   bouyer 		ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
    253  1.39.2.6   bouyer 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
    254  1.39.2.2   bouyer 	} else if (IS_1240(isp)) {
    255  1.39.2.2   bouyer 		sdparam *sdp = isp->isp_param;
    256  1.39.2.2   bouyer 		revname = "1240";
    257      1.36   mjacob 		isp->isp_clock = 60;
    258  1.39.2.2   bouyer 		sdp->isp_ultramode = 1;
    259  1.39.2.2   bouyer 		sdp++;
    260  1.39.2.2   bouyer 		sdp->isp_ultramode = 1;
    261  1.39.2.2   bouyer 		/*
    262  1.39.2.2   bouyer 		 * XXX: Should probably do some bus sensing.
    263  1.39.2.2   bouyer 		 */
    264  1.39.2.2   bouyer 	} else if (IS_ULTRA2(isp)) {
    265  1.39.2.4   bouyer 		static const char m[] = "bus %d is in %s Mode";
    266      1.34   mjacob 		u_int16_t l;
    267      1.33   mjacob 		sdparam *sdp = isp->isp_param;
    268  1.39.2.2   bouyer 
    269      1.36   mjacob 		isp->isp_clock = 100;
    270  1.39.2.2   bouyer 
    271  1.39.2.2   bouyer 		if (IS_1280(isp))
    272  1.39.2.2   bouyer 			revname = "1280";
    273  1.39.2.2   bouyer 		else if (IS_1080(isp))
    274  1.39.2.2   bouyer 			revname = "1080";
    275  1.39.2.2   bouyer 		else if (IS_12160(isp))
    276  1.39.2.2   bouyer 			revname = "12160";
    277  1.39.2.2   bouyer 		else
    278  1.39.2.2   bouyer 			revname = "<UNKLVD>";
    279  1.39.2.2   bouyer 
    280      1.34   mjacob 		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
    281      1.34   mjacob 		switch (l) {
    282      1.34   mjacob 		case ISP1080_LVD_MODE:
    283      1.34   mjacob 			sdp->isp_lvdmode = 1;
    284  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
    285      1.34   mjacob 			break;
    286      1.34   mjacob 		case ISP1080_HVD_MODE:
    287      1.34   mjacob 			sdp->isp_diffmode = 1;
    288  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
    289      1.34   mjacob 			break;
    290      1.34   mjacob 		case ISP1080_SE_MODE:
    291      1.34   mjacob 			sdp->isp_ultramode = 1;
    292  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
    293      1.34   mjacob 			break;
    294      1.34   mjacob 		default:
    295  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGERR,
    296  1.39.2.2   bouyer 			    "unknown mode on bus %d (0x%x)", 0, l);
    297      1.34   mjacob 			break;
    298      1.34   mjacob 		}
    299  1.39.2.2   bouyer 
    300  1.39.2.2   bouyer 		if (IS_DUALBUS(isp)) {
    301  1.39.2.2   bouyer 			sdp++;
    302  1.39.2.2   bouyer 			l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
    303  1.39.2.2   bouyer 			l &= ISP1080_MODE_MASK;
    304  1.39.2.2   bouyer 			switch(l) {
    305  1.39.2.2   bouyer 			case ISP1080_LVD_MODE:
    306  1.39.2.2   bouyer 				sdp->isp_lvdmode = 1;
    307  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
    308  1.39.2.2   bouyer 				break;
    309  1.39.2.2   bouyer 			case ISP1080_HVD_MODE:
    310  1.39.2.2   bouyer 				sdp->isp_diffmode = 1;
    311  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGCONFIG,
    312  1.39.2.2   bouyer 				    m, 1, "Differential");
    313  1.39.2.2   bouyer 				break;
    314  1.39.2.2   bouyer 			case ISP1080_SE_MODE:
    315  1.39.2.2   bouyer 				sdp->isp_ultramode = 1;
    316  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGCONFIG,
    317  1.39.2.2   bouyer 				    m, 1, "Single-Ended");
    318  1.39.2.2   bouyer 				break;
    319  1.39.2.2   bouyer 			default:
    320  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGERR,
    321  1.39.2.2   bouyer 				    "unknown mode on bus %d (0x%x)", 1, l);
    322  1.39.2.2   bouyer 				break;
    323  1.39.2.2   bouyer 			}
    324  1.39.2.2   bouyer 		}
    325      1.10   mjacob 	} else {
    326      1.25   mjacob 		sdparam *sdp = isp->isp_param;
    327      1.30   mjacob 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
    328      1.30   mjacob 		switch (i) {
    329      1.10   mjacob 		default:
    330  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
    331      1.25   mjacob 			/* FALLTHROUGH */
    332      1.10   mjacob 		case 1:
    333      1.34   mjacob 			revname = "1020";
    334      1.10   mjacob 			isp->isp_type = ISP_HA_SCSI_1020;
    335      1.36   mjacob 			isp->isp_clock = 40;
    336      1.22   mjacob 			break;
    337      1.22   mjacob 		case 2:
    338      1.25   mjacob 			/*
    339      1.25   mjacob 			 * Some 1020A chips are Ultra Capable, but don't
    340      1.25   mjacob 			 * run the clock rate up for that unless told to
    341      1.25   mjacob 			 * do so by the Ultra Capable bits being set.
    342      1.25   mjacob 			 */
    343      1.34   mjacob 			revname = "1020A";
    344      1.22   mjacob 			isp->isp_type = ISP_HA_SCSI_1020A;
    345      1.36   mjacob 			isp->isp_clock = 40;
    346      1.10   mjacob 			break;
    347      1.10   mjacob 		case 3:
    348      1.25   mjacob 			revname = "1040";
    349      1.25   mjacob 			isp->isp_type = ISP_HA_SCSI_1040;
    350      1.36   mjacob 			isp->isp_clock = 60;
    351      1.25   mjacob 			break;
    352      1.25   mjacob 		case 4:
    353      1.10   mjacob 			revname = "1040A";
    354      1.10   mjacob 			isp->isp_type = ISP_HA_SCSI_1040A;
    355      1.36   mjacob 			isp->isp_clock = 60;
    356      1.10   mjacob 			break;
    357      1.10   mjacob 		case 5:
    358      1.10   mjacob 			revname = "1040B";
    359      1.10   mjacob 			isp->isp_type = ISP_HA_SCSI_1040B;
    360      1.36   mjacob 			isp->isp_clock = 60;
    361      1.10   mjacob 			break;
    362      1.35   mjacob 		case 6:
    363  1.39.2.2   bouyer 			revname = "1040C";
    364      1.35   mjacob 			isp->isp_type = ISP_HA_SCSI_1040C;
    365      1.36   mjacob 			isp->isp_clock = 60;
    366      1.35   mjacob                         break;
    367       1.8   mjacob 		}
    368      1.25   mjacob 		/*
    369      1.31   mjacob 		 * Now, while we're at it, gather info about ultra
    370      1.31   mjacob 		 * and/or differential mode.
    371      1.25   mjacob 		 */
    372      1.31   mjacob 		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
    373  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
    374      1.31   mjacob 			sdp->isp_diffmode = 1;
    375      1.31   mjacob 		} else {
    376      1.31   mjacob 			sdp->isp_diffmode = 0;
    377      1.31   mjacob 		}
    378      1.31   mjacob 		i = ISP_READ(isp, RISC_PSR);
    379      1.31   mjacob 		if (isp->isp_bustype == ISP_BT_SBUS) {
    380      1.31   mjacob 			i &= RISC_PSR_SBUS_ULTRA;
    381      1.30   mjacob 		} else {
    382      1.31   mjacob 			i &= RISC_PSR_PCI_ULTRA;
    383      1.31   mjacob 		}
    384      1.31   mjacob 		if (i != 0) {
    385  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
    386      1.31   mjacob 			sdp->isp_ultramode = 1;
    387      1.33   mjacob 			/*
    388      1.33   mjacob 			 * If we're in Ultra Mode, we have to be 60Mhz clock-
    389      1.33   mjacob 			 * even for the SBus version.
    390      1.33   mjacob 			 */
    391      1.36   mjacob 			isp->isp_clock = 60;
    392      1.34   mjacob 		} else {
    393      1.31   mjacob 			sdp->isp_ultramode = 0;
    394      1.33   mjacob 			/*
    395      1.33   mjacob 			 * Clock is known. Gronk.
    396      1.33   mjacob 			 */
    397      1.25   mjacob 		}
    398      1.25   mjacob 
    399      1.25   mjacob 		/*
    400      1.25   mjacob 		 * Machine dependent clock (if set) overrides
    401      1.25   mjacob 		 * our generic determinations.
    402      1.25   mjacob 		 */
    403      1.25   mjacob 		if (isp->isp_mdvec->dv_clock) {
    404      1.36   mjacob 			if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
    405      1.36   mjacob 				isp->isp_clock = isp->isp_mdvec->dv_clock;
    406      1.25   mjacob 			}
    407      1.25   mjacob 		}
    408      1.31   mjacob 
    409       1.4   mjacob 	}
    410       1.8   mjacob 
    411       1.1      cgd 	/*
    412  1.39.2.3   bouyer 	 * Clear instrumentation
    413  1.39.2.3   bouyer 	 */
    414  1.39.2.3   bouyer 	isp->isp_intcnt = isp->isp_intbogus = 0;
    415  1.39.2.3   bouyer 
    416  1.39.2.3   bouyer 	/*
    417      1.10   mjacob 	 * Do MD specific pre initialization
    418       1.1      cgd 	 */
    419      1.10   mjacob 	ISP_RESET0(isp);
    420      1.25   mjacob 
    421      1.31   mjacob again:
    422      1.30   mjacob 
    423       1.1      cgd 	/*
    424      1.10   mjacob 	 * Hit the chip over the head with hammer,
    425      1.10   mjacob 	 * and give the ISP a chance to recover.
    426       1.1      cgd 	 */
    427       1.1      cgd 
    428      1.33   mjacob 	if (IS_SCSI(isp)) {
    429      1.10   mjacob 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
    430      1.10   mjacob 		/*
    431      1.10   mjacob 		 * A slight delay...
    432      1.10   mjacob 		 */
    433  1.39.2.2   bouyer 		USEC_DELAY(100);
    434      1.33   mjacob 
    435      1.10   mjacob 		/*
    436      1.10   mjacob 		 * Clear data && control DMA engines.
    437      1.10   mjacob 		 */
    438      1.10   mjacob 		ISP_WRITE(isp, CDMA_CONTROL,
    439      1.34   mjacob 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    440      1.10   mjacob 		ISP_WRITE(isp, DDMA_CONTROL,
    441      1.34   mjacob 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
    442      1.33   mjacob 
    443      1.33   mjacob 
    444      1.10   mjacob 	} else {
    445      1.10   mjacob 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
    446      1.10   mjacob 		/*
    447      1.10   mjacob 		 * A slight delay...
    448      1.10   mjacob 		 */
    449  1.39.2.2   bouyer 		USEC_DELAY(100);
    450      1.31   mjacob 
    451      1.31   mjacob 		/*
    452      1.31   mjacob 		 * Clear data && control DMA engines.
    453      1.31   mjacob 		 */
    454      1.10   mjacob 		ISP_WRITE(isp, CDMA2100_CONTROL,
    455      1.10   mjacob 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    456      1.10   mjacob 		ISP_WRITE(isp, TDMA2100_CONTROL,
    457      1.10   mjacob 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    458      1.10   mjacob 		ISP_WRITE(isp, RDMA2100_CONTROL,
    459      1.10   mjacob 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
    460      1.10   mjacob 	}
    461      1.10   mjacob 
    462       1.1      cgd 	/*
    463       1.1      cgd 	 * Wait for ISP to be ready to go...
    464       1.1      cgd 	 */
    465       1.1      cgd 	loops = MBOX_DELAY_COUNT;
    466      1.10   mjacob 	for (;;) {
    467      1.38   mjacob 		if (IS_SCSI(isp)) {
    468      1.10   mjacob 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
    469      1.10   mjacob 				break;
    470      1.10   mjacob 		} else {
    471      1.10   mjacob 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
    472      1.10   mjacob 				break;
    473      1.10   mjacob 		}
    474  1.39.2.2   bouyer 		USEC_DELAY(100);
    475       1.1      cgd 		if (--loops < 0) {
    476  1.39.2.2   bouyer 			ISP_DUMPREGS(isp, "chip reset timed out");
    477       1.1      cgd 			return;
    478       1.1      cgd 		}
    479       1.1      cgd 	}
    480      1.31   mjacob 
    481       1.1      cgd 	/*
    482      1.31   mjacob 	 * After we've fired this chip up, zero out the conf1 register
    483      1.31   mjacob 	 * for SCSI adapters and other settings for the 2100.
    484       1.1      cgd 	 */
    485      1.31   mjacob 
    486      1.33   mjacob 	if (IS_SCSI(isp)) {
    487      1.10   mjacob 		ISP_WRITE(isp, BIU_CONF1, 0);
    488      1.10   mjacob 	} else {
    489      1.10   mjacob 		ISP_WRITE(isp, BIU2100_CSR, 0);
    490      1.10   mjacob 	}
    491       1.1      cgd 
    492      1.31   mjacob 	/*
    493      1.31   mjacob 	 * Reset RISC Processor
    494      1.31   mjacob 	 */
    495       1.1      cgd 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
    496  1.39.2.2   bouyer 	USEC_DELAY(100);
    497  1.39.2.6   bouyer 	/* Clear semaphore register (just to be sure) */
    498  1.39.2.6   bouyer 	ISP_WRITE(isp, BIU_SEMA, 0);
    499       1.1      cgd 
    500      1.30   mjacob 	/*
    501      1.31   mjacob 	 * Establish some initial burst rate stuff.
    502      1.30   mjacob 	 * (only for the 1XX0 boards). This really should
    503      1.30   mjacob 	 * be done later after fetching from NVRAM.
    504      1.30   mjacob 	 */
    505      1.33   mjacob 	if (IS_SCSI(isp)) {
    506      1.31   mjacob 		u_int16_t tmp = isp->isp_mdvec->dv_conf1;
    507      1.30   mjacob 		/*
    508      1.30   mjacob 		 * Busted FIFO. Turn off all but burst enables.
    509      1.30   mjacob 		 */
    510      1.30   mjacob 		if (isp->isp_type == ISP_HA_SCSI_1040A) {
    511      1.31   mjacob 			tmp &= BIU_BURST_ENABLE;
    512      1.30   mjacob 		}
    513      1.31   mjacob 		ISP_SETBITS(isp, BIU_CONF1, tmp);
    514      1.31   mjacob 		if (tmp & BIU_BURST_ENABLE) {
    515       1.1      cgd 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
    516       1.1      cgd 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
    517       1.1      cgd 		}
    518      1.31   mjacob #ifdef	PTI_CARDS
    519      1.31   mjacob 		if (((sdparam *) isp->isp_param)->isp_ultramode) {
    520      1.34   mjacob 			while (ISP_READ(isp, RISC_MTR) != 0x1313) {
    521      1.31   mjacob 				ISP_WRITE(isp, RISC_MTR, 0x1313);
    522      1.31   mjacob 				ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
    523      1.31   mjacob 			}
    524      1.34   mjacob 		} else {
    525      1.31   mjacob 			ISP_WRITE(isp, RISC_MTR, 0x1212);
    526      1.31   mjacob 		}
    527      1.31   mjacob 		/*
    528      1.31   mjacob 		 * PTI specific register
    529      1.31   mjacob 		 */
    530      1.31   mjacob 		ISP_WRITE(isp, RISC_EMB, DUAL_BANK)
    531      1.31   mjacob #else
    532      1.31   mjacob 		ISP_WRITE(isp, RISC_MTR, 0x1212);
    533      1.31   mjacob #endif
    534      1.31   mjacob 	} else {
    535      1.31   mjacob 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
    536  1.39.2.6   bouyer 		if (IS_2200(isp)) {
    537  1.39.2.6   bouyer 			ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
    538  1.39.2.6   bouyer 		}
    539       1.1      cgd 	}
    540      1.31   mjacob 
    541       1.1      cgd 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
    542       1.1      cgd 
    543       1.1      cgd 	/*
    544       1.1      cgd 	 * Do MD specific post initialization
    545       1.1      cgd 	 */
    546       1.1      cgd 	ISP_RESET1(isp);
    547       1.1      cgd 
    548       1.1      cgd 	/*
    549      1.31   mjacob 	 * Wait for everything to finish firing up...
    550      1.31   mjacob 	 */
    551      1.31   mjacob 	loops = MBOX_DELAY_COUNT;
    552      1.31   mjacob 	while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
    553  1.39.2.2   bouyer 		USEC_DELAY(100);
    554      1.31   mjacob 		if (--loops < 0) {
    555  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGERR,
    556  1.39.2.2   bouyer 			    "MBOX_BUSY never cleared on reset");
    557      1.31   mjacob 			return;
    558      1.31   mjacob 		}
    559      1.31   mjacob 	}
    560      1.31   mjacob 
    561      1.31   mjacob 	/*
    562      1.31   mjacob 	 * Up until this point we've done everything by just reading or
    563      1.31   mjacob 	 * setting registers. From this point on we rely on at least *some*
    564      1.31   mjacob 	 * kind of firmware running in the card.
    565      1.31   mjacob 	 */
    566      1.31   mjacob 
    567      1.31   mjacob 	/*
    568       1.1      cgd 	 * Do some sanity checking.
    569       1.1      cgd 	 */
    570       1.1      cgd 	mbs.param[0] = MBOX_NO_OP;
    571  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    572       1.1      cgd 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    573       1.1      cgd 		return;
    574       1.1      cgd 	}
    575       1.1      cgd 
    576      1.37   mjacob 	if (IS_SCSI(isp)) {
    577      1.10   mjacob 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
    578      1.10   mjacob 		mbs.param[1] = 0xdead;
    579      1.10   mjacob 		mbs.param[2] = 0xbeef;
    580      1.10   mjacob 		mbs.param[3] = 0xffff;
    581      1.10   mjacob 		mbs.param[4] = 0x1111;
    582      1.10   mjacob 		mbs.param[5] = 0xa5a5;
    583  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL);
    584       1.1      cgd 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    585      1.10   mjacob 			return;
    586      1.10   mjacob 		}
    587      1.10   mjacob 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
    588      1.10   mjacob 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
    589      1.10   mjacob 		    mbs.param[5] != 0xa5a5) {
    590  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGERR,
    591  1.39.2.2   bouyer 			    "Register Test Failed (0x%x 0x%x 0x%x 0x%x 0x%x)",
    592  1.39.2.2   bouyer 			    mbs.param[1], mbs.param[2], mbs.param[3],
    593  1.39.2.2   bouyer 			    mbs.param[4], mbs.param[5]);
    594       1.1      cgd 			return;
    595       1.1      cgd 		}
    596      1.10   mjacob 
    597       1.1      cgd 	}
    598       1.1      cgd 
    599       1.1      cgd 	/*
    600      1.18   mjacob 	 * Download new Firmware, unless requested not to do so.
    601      1.18   mjacob 	 * This is made slightly trickier in some cases where the
    602      1.18   mjacob 	 * firmware of the ROM revision is newer than the revision
    603      1.18   mjacob 	 * compiled into the driver. So, where we used to compare
    604      1.18   mjacob 	 * versions of our f/w and the ROM f/w, now we just see
    605      1.18   mjacob 	 * whether we have f/w at all and whether a config flag
    606      1.18   mjacob 	 * has disabled our download.
    607       1.1      cgd 	 */
    608  1.39.2.2   bouyer 	if ((isp->isp_mdvec->dv_ispfw == NULL) ||
    609      1.23   mjacob 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
    610      1.10   mjacob 		dodnld = 0;
    611      1.10   mjacob 	}
    612      1.10   mjacob 
    613  1.39.2.2   bouyer 	if (dodnld) {
    614  1.39.2.2   bouyer 		u_int16_t fwlen  = isp->isp_mdvec->dv_ispfw[3];
    615  1.39.2.2   bouyer 		for (i = 0; i < fwlen; i++) {
    616      1.10   mjacob 			mbs.param[0] = MBOX_WRITE_RAM_WORD;
    617  1.39.2.2   bouyer 			mbs.param[1] = ISP_CODE_ORG + i;
    618      1.10   mjacob 			mbs.param[2] = isp->isp_mdvec->dv_ispfw[i];
    619  1.39.2.2   bouyer 			isp_mboxcmd(isp, &mbs, MBLOGNONE);
    620      1.10   mjacob 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    621  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGERR,
    622  1.39.2.2   bouyer 				    "F/W download failed at word %d", i);
    623      1.31   mjacob 				dodnld = 0;
    624      1.31   mjacob 				goto again;
    625      1.10   mjacob 			}
    626      1.10   mjacob 		}
    627      1.10   mjacob 
    628      1.31   mjacob 		/*
    629      1.31   mjacob 		 * Verify that it downloaded correctly.
    630      1.31   mjacob 		 */
    631      1.31   mjacob 		mbs.param[0] = MBOX_VERIFY_CHECKSUM;
    632  1.39.2.2   bouyer 		mbs.param[1] = ISP_CODE_ORG;
    633  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
    634      1.31   mjacob 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    635  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGERR, "Ram Checksum Failure");
    636      1.31   mjacob 			return;
    637      1.10   mjacob 		}
    638  1.39.2.2   bouyer 		isp->isp_loaded_fw = 1;
    639      1.10   mjacob 	} else {
    640  1.39.2.2   bouyer 		isp->isp_loaded_fw = 0;
    641  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
    642       1.1      cgd 	}
    643       1.1      cgd 
    644       1.1      cgd 	/*
    645      1.10   mjacob 	 * Now start it rolling.
    646      1.10   mjacob 	 *
    647      1.10   mjacob 	 * If we didn't actually download f/w,
    648      1.10   mjacob 	 * we still need to (re)start it.
    649       1.1      cgd 	 */
    650       1.1      cgd 
    651       1.1      cgd 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
    652  1.39.2.2   bouyer 	mbs.param[1] = ISP_CODE_ORG;
    653  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGNONE);
    654  1.39.2.2   bouyer 	/* give it a chance to start */
    655  1.39.2.3   bouyer 	USEC_SLEEP(isp, 500);
    656       1.4   mjacob 
    657      1.38   mjacob 	if (IS_SCSI(isp)) {
    658      1.10   mjacob 		/*
    659      1.25   mjacob 		 * Set CLOCK RATE, but only if asked to.
    660      1.10   mjacob 		 */
    661      1.36   mjacob 		if (isp->isp_clock) {
    662      1.10   mjacob 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
    663      1.36   mjacob 			mbs.param[1] = isp->isp_clock;
    664  1.39.2.2   bouyer 			isp_mboxcmd(isp, &mbs, MBLOGALL);
    665  1.39.2.2   bouyer 			/* we will try not to care if this fails */
    666       1.4   mjacob 		}
    667       1.4   mjacob 	}
    668  1.39.2.2   bouyer 
    669       1.1      cgd 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
    670  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    671       1.1      cgd 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    672       1.1      cgd 		return;
    673       1.1      cgd 	}
    674  1.39.2.2   bouyer 	isp_prt(isp, ISP_LOGCONFIG,
    675  1.39.2.2   bouyer 	    "Board Revision %s, %s F/W Revision %d.%d.%d", revname,
    676  1.39.2.2   bouyer 	    dodnld? "loaded" : "resident", mbs.param[1], mbs.param[2],
    677  1.39.2.2   bouyer 	    mbs.param[3]);
    678      1.34   mjacob 	if (IS_FC(isp)) {
    679      1.32   mjacob 		if (ISP_READ(isp, BIU2100_CSR) & BIU2100_PCI64) {
    680  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGCONFIG,
    681  1.39.2.2   bouyer 			    "Installed in 64-Bit PCI slot");
    682      1.32   mjacob 		}
    683      1.32   mjacob 	}
    684      1.38   mjacob 
    685      1.35   mjacob 	isp->isp_fwrev[0] = mbs.param[1];
    686      1.35   mjacob 	isp->isp_fwrev[1] = mbs.param[2];
    687      1.35   mjacob 	isp->isp_fwrev[2] = mbs.param[3];
    688      1.35   mjacob 	if (isp->isp_romfw_rev[0] || isp->isp_romfw_rev[1] ||
    689      1.35   mjacob 	    isp->isp_romfw_rev[2]) {
    690  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGCONFIG, "Last F/W revision was %d.%d.%d",
    691      1.35   mjacob 		    isp->isp_romfw_rev[0], isp->isp_romfw_rev[1],
    692      1.35   mjacob 		    isp->isp_romfw_rev[2]);
    693      1.25   mjacob 	}
    694      1.38   mjacob 
    695      1.38   mjacob 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
    696  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    697      1.38   mjacob 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    698      1.38   mjacob 		return;
    699      1.38   mjacob 	}
    700      1.38   mjacob 	isp->isp_maxcmds = mbs.param[2];
    701  1.39.2.2   bouyer 	isp_prt(isp, ISP_LOGINFO,
    702  1.39.2.2   bouyer 	    "%d max I/O commands supported", mbs.param[2]);
    703      1.10   mjacob 	isp_fw_state(isp);
    704      1.38   mjacob 
    705      1.36   mjacob 	/*
    706      1.36   mjacob 	 * Set up DMA for the request and result mailboxes.
    707      1.36   mjacob 	 */
    708      1.36   mjacob 	if (ISP_MBOXDMASETUP(isp) != 0) {
    709  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
    710      1.36   mjacob 		return;
    711      1.36   mjacob 	}
    712       1.1      cgd 	isp->isp_state = ISP_RESETSTATE;
    713  1.39.2.2   bouyer 
    714  1.39.2.2   bouyer 	/*
    715  1.39.2.2   bouyer 	 * Okay- now that we have new firmware running, we now (re)set our
    716  1.39.2.2   bouyer 	 * notion of how many luns we support. This is somewhat tricky because
    717  1.39.2.2   bouyer 	 * if we haven't loaded firmware, we don't have an easy way of telling
    718  1.39.2.2   bouyer 	 * how many luns we support.
    719  1.39.2.2   bouyer 	 *
    720  1.39.2.2   bouyer 	 * We'll make a simplifying assumption- if we loaded firmware, we
    721  1.39.2.2   bouyer 	 * are running with expanded lun firmware, otherwise not.
    722  1.39.2.2   bouyer 	 *
    723  1.39.2.2   bouyer 	 * Expanded lun firmware gives you 32 luns for SCSI cards and
    724  1.39.2.2   bouyer 	 * 65536 luns for Fibre Channel cards.
    725  1.39.2.2   bouyer 	 *
    726  1.39.2.2   bouyer 	 * Because the lun is in a a different position in the Request Queue
    727  1.39.2.2   bouyer 	 * Entry structure for Fibre Channel with expanded lun firmware, we
    728  1.39.2.2   bouyer 	 * can only support one lun (lun zero) when we don't know what kind
    729  1.39.2.2   bouyer 	 * of firmware we're running.
    730  1.39.2.2   bouyer 	 *
    731  1.39.2.2   bouyer 	 * Note that we only do this once (the first time thru isp_reset)
    732  1.39.2.2   bouyer 	 * because we may be called again after firmware has been loaded once
    733  1.39.2.2   bouyer 	 * and released.
    734  1.39.2.2   bouyer 	 */
    735  1.39.2.2   bouyer 	if (touched == 0) {
    736  1.39.2.2   bouyer 		if (dodnld) {
    737  1.39.2.2   bouyer 			if (IS_SCSI(isp)) {
    738  1.39.2.2   bouyer 				isp->isp_maxluns = 32;
    739  1.39.2.2   bouyer 			} else {
    740  1.39.2.2   bouyer 				isp->isp_maxluns = 65536;
    741  1.39.2.2   bouyer 			}
    742  1.39.2.2   bouyer 		} else {
    743  1.39.2.2   bouyer 			if (IS_SCSI(isp)) {
    744  1.39.2.2   bouyer 				isp->isp_maxluns = 8;
    745  1.39.2.2   bouyer 			} else {
    746  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGALL, warnlun);
    747  1.39.2.2   bouyer 				isp->isp_maxluns = 1;
    748  1.39.2.2   bouyer 			}
    749  1.39.2.2   bouyer 		}
    750  1.39.2.2   bouyer 	}
    751       1.1      cgd }
    752       1.1      cgd 
    753       1.1      cgd /*
    754      1.31   mjacob  * Initialize Parameters of Hardware to a known state.
    755      1.24   mjacob  *
    756      1.24   mjacob  * Locks are held before coming here.
    757       1.1      cgd  */
    758      1.24   mjacob 
    759       1.1      cgd void
    760  1.39.2.7   bouyer isp_init(struct ispsoftc *isp)
    761       1.1      cgd {
    762      1.36   mjacob 	/*
    763      1.36   mjacob 	 * Must do this first to get defaults established.
    764      1.36   mjacob 	 */
    765      1.36   mjacob 	isp_setdfltparm(isp, 0);
    766  1.39.2.2   bouyer 	if (IS_DUALBUS(isp)) {
    767      1.36   mjacob 		isp_setdfltparm(isp, 1);
    768      1.36   mjacob 	}
    769  1.39.2.6   bouyer 	if (IS_FC(isp)) {
    770  1.39.2.6   bouyer 		isp_fibre_init(isp);
    771  1.39.2.6   bouyer 	} else {
    772  1.39.2.6   bouyer 		isp_scsi_init(isp);
    773      1.36   mjacob 	}
    774      1.36   mjacob }
    775      1.36   mjacob 
    776      1.36   mjacob static void
    777  1.39.2.7   bouyer isp_scsi_init(struct ispsoftc *isp)
    778      1.36   mjacob {
    779      1.36   mjacob 	sdparam *sdp_chan0, *sdp_chan1;
    780       1.1      cgd 	mbreg_t mbs;
    781      1.36   mjacob 
    782      1.36   mjacob 	sdp_chan0 = isp->isp_param;
    783      1.36   mjacob 	sdp_chan1 = sdp_chan0;
    784  1.39.2.2   bouyer 	if (IS_DUALBUS(isp)) {
    785      1.36   mjacob 		sdp_chan1++;
    786      1.36   mjacob 	}
    787      1.36   mjacob 
    788  1.39.2.6   bouyer 	/*
    789  1.39.2.6   bouyer 	 * If we have no role (neither target nor initiator), return.
    790  1.39.2.6   bouyer 	 */
    791  1.39.2.6   bouyer 	if (isp->isp_role == ISP_ROLE_NONE) {
    792  1.39.2.6   bouyer 		return;
    793  1.39.2.6   bouyer 	}
    794  1.39.2.6   bouyer 
    795      1.36   mjacob 	/* First do overall per-card settings. */
    796      1.25   mjacob 
    797      1.25   mjacob 	/*
    798      1.36   mjacob 	 * If we have fast memory timing enabled, turn it on.
    799      1.25   mjacob 	 */
    800  1.39.2.2   bouyer 	if (sdp_chan0->isp_fast_mttr) {
    801      1.36   mjacob 		ISP_WRITE(isp, RISC_MTR, 0x1313);
    802      1.36   mjacob 	}
    803      1.25   mjacob 
    804      1.25   mjacob 	/*
    805      1.36   mjacob 	 * Set Retry Delay and Count.
    806      1.36   mjacob 	 * You set both channels at the same time.
    807      1.33   mjacob 	 */
    808      1.36   mjacob 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
    809      1.36   mjacob 	mbs.param[1] = sdp_chan0->isp_retry_count;
    810      1.36   mjacob 	mbs.param[2] = sdp_chan0->isp_retry_delay;
    811      1.36   mjacob 	mbs.param[6] = sdp_chan1->isp_retry_count;
    812      1.36   mjacob 	mbs.param[7] = sdp_chan1->isp_retry_delay;
    813      1.36   mjacob 
    814  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    815      1.36   mjacob 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    816      1.33   mjacob 		return;
    817      1.33   mjacob 	}
    818      1.34   mjacob 
    819      1.33   mjacob 	/*
    820      1.36   mjacob 	 * Set ASYNC DATA SETUP time. This is very important.
    821      1.25   mjacob 	 */
    822      1.36   mjacob 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
    823      1.36   mjacob 	mbs.param[1] = sdp_chan0->isp_async_data_setup;
    824      1.36   mjacob 	mbs.param[2] = sdp_chan1->isp_async_data_setup;
    825  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    826      1.36   mjacob 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    827      1.10   mjacob 		return;
    828       1.1      cgd 	}
    829       1.1      cgd 
    830      1.10   mjacob 	/*
    831      1.36   mjacob 	 * Set ACTIVE Negation State.
    832      1.32   mjacob 	 */
    833      1.36   mjacob 	mbs.param[0] = MBOX_SET_ACT_NEG_STATE;
    834      1.36   mjacob 	mbs.param[1] =
    835      1.36   mjacob 	    (sdp_chan0->isp_req_ack_active_neg << 4) |
    836      1.36   mjacob 	    (sdp_chan0->isp_data_line_active_neg << 5);
    837      1.36   mjacob 	mbs.param[2] =
    838      1.36   mjacob 	    (sdp_chan1->isp_req_ack_active_neg << 4) |
    839      1.36   mjacob 	    (sdp_chan1->isp_data_line_active_neg << 5);
    840      1.36   mjacob 
    841  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGNONE);
    842      1.36   mjacob 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    843  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
    844  1.39.2.2   bouyer 		    "failed to set active negation state (%d,%d), (%d,%d)",
    845      1.36   mjacob 		    sdp_chan0->isp_req_ack_active_neg,
    846      1.36   mjacob 		    sdp_chan0->isp_data_line_active_neg,
    847      1.36   mjacob 		    sdp_chan1->isp_req_ack_active_neg,
    848      1.36   mjacob 		    sdp_chan1->isp_data_line_active_neg);
    849      1.36   mjacob 		/*
    850      1.36   mjacob 		 * But don't return.
    851      1.36   mjacob 		 */
    852      1.32   mjacob 	}
    853      1.32   mjacob 
    854      1.32   mjacob 	/*
    855      1.36   mjacob 	 * Set the Tag Aging limit
    856      1.10   mjacob 	 */
    857      1.36   mjacob 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
    858      1.36   mjacob 	mbs.param[1] = sdp_chan0->isp_tag_aging;
    859      1.36   mjacob 	mbs.param[2] = sdp_chan1->isp_tag_aging;
    860  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    861       1.1      cgd 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    862  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
    863  1.39.2.2   bouyer 		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
    864       1.1      cgd 		return;
    865       1.1      cgd 	}
    866       1.1      cgd 
    867      1.25   mjacob 	/*
    868      1.36   mjacob 	 * Set selection timeout.
    869      1.25   mjacob 	 */
    870      1.36   mjacob 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
    871      1.36   mjacob 	mbs.param[1] = sdp_chan0->isp_selection_timeout;
    872      1.36   mjacob 	mbs.param[2] = sdp_chan1->isp_selection_timeout;
    873  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    874       1.1      cgd 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    875       1.1      cgd 		return;
    876       1.1      cgd 	}
    877       1.1      cgd 
    878      1.36   mjacob 	/* now do per-channel settings */
    879      1.36   mjacob 	isp_scsi_channel_init(isp, 0);
    880  1.39.2.2   bouyer 	if (IS_DUALBUS(isp))
    881      1.36   mjacob 		isp_scsi_channel_init(isp, 1);
    882      1.36   mjacob 
    883      1.25   mjacob 	/*
    884      1.36   mjacob 	 * Now enable request/response queues
    885      1.25   mjacob 	 */
    886      1.36   mjacob 
    887      1.36   mjacob 	mbs.param[0] = MBOX_INIT_RES_QUEUE;
    888  1.39.2.2   bouyer 	mbs.param[1] = RESULT_QUEUE_LEN(isp);
    889      1.36   mjacob 	mbs.param[2] = DMA_MSW(isp->isp_result_dma);
    890      1.36   mjacob 	mbs.param[3] = DMA_LSW(isp->isp_result_dma);
    891      1.36   mjacob 	mbs.param[4] = 0;
    892      1.36   mjacob 	mbs.param[5] = 0;
    893  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    894       1.1      cgd 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    895       1.1      cgd 		return;
    896       1.1      cgd 	}
    897  1.39.2.2   bouyer 	isp->isp_residx = mbs.param[5];
    898       1.1      cgd 
    899      1.36   mjacob 	mbs.param[0] = MBOX_INIT_REQ_QUEUE;
    900  1.39.2.2   bouyer 	mbs.param[1] = RQUEST_QUEUE_LEN(isp);
    901      1.36   mjacob 	mbs.param[2] = DMA_MSW(isp->isp_rquest_dma);
    902      1.36   mjacob 	mbs.param[3] = DMA_LSW(isp->isp_rquest_dma);
    903      1.36   mjacob 	mbs.param[4] = 0;
    904  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    905       1.1      cgd 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    906       1.1      cgd 		return;
    907       1.1      cgd 	}
    908  1.39.2.2   bouyer 	isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
    909       1.1      cgd 
    910      1.25   mjacob 	/*
    911  1.39.2.2   bouyer 	 * Turn on Fast Posting, LVD transitions
    912  1.39.2.2   bouyer 	 *
    913  1.39.2.2   bouyer 	 * Ultra2 F/W always has had fast posting (and LVD transitions)
    914  1.39.2.2   bouyer 	 *
    915  1.39.2.2   bouyer 	 * Ultra and older (i.e., SBus) cards may not. It's just safer
    916  1.39.2.2   bouyer 	 * to assume not for them.
    917      1.25   mjacob 	 */
    918      1.25   mjacob 
    919  1.39.2.2   bouyer 	mbs.param[0] = MBOX_SET_FW_FEATURES;
    920  1.39.2.2   bouyer 	mbs.param[1] = 0;
    921  1.39.2.2   bouyer 	if (IS_ULTRA2(isp))
    922  1.39.2.2   bouyer 		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
    923  1.39.2.2   bouyer 	if (IS_ULTRA2(isp) || IS_1240(isp))
    924      1.36   mjacob 		mbs.param[1] |= FW_FEATURE_FAST_POST;
    925  1.39.2.2   bouyer 	if (mbs.param[1] != 0) {
    926  1.39.2.2   bouyer 		u_int16_t sfeat = mbs.param[1];
    927  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL);
    928  1.39.2.2   bouyer 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
    929  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGINFO,
    930  1.39.2.2   bouyer 			    "Enabled FW features (0x%x)", sfeat);
    931      1.36   mjacob 		}
    932       1.1      cgd 	}
    933       1.1      cgd 
    934      1.25   mjacob 	/*
    935      1.36   mjacob 	 * Let the outer layers decide whether to issue a SCSI bus reset.
    936      1.25   mjacob 	 */
    937      1.36   mjacob 	isp->isp_state = ISP_INITSTATE;
    938      1.36   mjacob }
    939      1.25   mjacob 
    940      1.36   mjacob static void
    941  1.39.2.7   bouyer isp_scsi_channel_init(struct ispsoftc *isp, int channel)
    942      1.36   mjacob {
    943      1.36   mjacob 	sdparam *sdp;
    944      1.36   mjacob 	mbreg_t mbs;
    945      1.36   mjacob 	int tgt;
    946      1.36   mjacob 
    947      1.36   mjacob 	sdp = isp->isp_param;
    948      1.36   mjacob 	sdp += channel;
    949      1.36   mjacob 
    950      1.36   mjacob 	/*
    951      1.36   mjacob 	 * Set (possibly new) Initiator ID.
    952      1.36   mjacob 	 */
    953      1.36   mjacob 	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
    954      1.36   mjacob 	mbs.param[1] = (channel << 7) | sdp->isp_initiator_id;
    955  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
    956       1.1      cgd 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    957       1.1      cgd 		return;
    958       1.1      cgd 	}
    959  1.39.2.7   bouyer 	isp_prt(isp, ISP_LOGINFO, "Initiator ID is %d on Channel %d",
    960  1.39.2.7   bouyer 	    sdp->isp_initiator_id, channel);
    961  1.39.2.2   bouyer 
    962       1.1      cgd 
    963      1.25   mjacob 	/*
    964      1.32   mjacob 	 * Set current per-target parameters to a safe minimum.
    965      1.25   mjacob 	 */
    966      1.25   mjacob 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
    967  1.39.2.2   bouyer 		int lun;
    968      1.32   mjacob 		u_int16_t sdf;
    969      1.10   mjacob 
    970      1.36   mjacob 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
    971       1.1      cgd 			continue;
    972      1.36   mjacob 		}
    973  1.39.2.2   bouyer 		sdf = DPARM_SAFE_DFLT;
    974      1.36   mjacob 		/*
    975  1.39.2.2   bouyer 		 * It is not quite clear when this changed over so that
    976  1.39.2.2   bouyer 		 * we could force narrow and async for 1000/1020 cards,
    977  1.39.2.2   bouyer 		 * but assume that this is only the case for loaded
    978  1.39.2.2   bouyer 		 * firmware.
    979      1.36   mjacob 		 */
    980  1.39.2.2   bouyer 		if (isp->isp_loaded_fw) {
    981  1.39.2.2   bouyer 			sdf |= DPARM_NARROW | DPARM_ASYNC;
    982      1.20   mjacob 		}
    983      1.32   mjacob 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
    984      1.36   mjacob 		mbs.param[1] = (tgt << 8) | (channel << 15);
    985      1.32   mjacob 		mbs.param[2] = sdf;
    986  1.39.2.2   bouyer 		if ((sdf & DPARM_SYNC) == 0) {
    987  1.39.2.2   bouyer 			mbs.param[3] = 0;
    988  1.39.2.2   bouyer 		} else {
    989  1.39.2.2   bouyer 			mbs.param[3] =
    990  1.39.2.2   bouyer 			    (sdp->isp_devparam[tgt].sync_offset << 8) |
    991  1.39.2.2   bouyer 			    (sdp->isp_devparam[tgt].sync_period);
    992  1.39.2.2   bouyer 		}
    993  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL);
    994       1.1      cgd 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
    995      1.32   mjacob 			sdf = DPARM_SAFE_DFLT;
    996      1.10   mjacob 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
    997      1.36   mjacob 			mbs.param[1] = (tgt << 8) | (channel << 15);
    998      1.32   mjacob 			mbs.param[2] = sdf;
    999  1.39.2.2   bouyer 			mbs.param[3] = 0;
   1000  1.39.2.2   bouyer 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   1001      1.10   mjacob 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1002      1.25   mjacob 				continue;
   1003      1.10   mjacob 			}
   1004       1.1      cgd 		}
   1005      1.36   mjacob 
   1006      1.34   mjacob 		/*
   1007  1.39.2.2   bouyer 		 * We don't update any information directly from the f/w
   1008  1.39.2.2   bouyer 		 * because we need to run at least one command to cause a
   1009  1.39.2.2   bouyer 		 * new state to be latched up. So, we just assume that we
   1010  1.39.2.2   bouyer 		 * converge to the values we just had set.
   1011  1.39.2.2   bouyer 		 *
   1012      1.34   mjacob 		 * Ensure that we don't believe tagged queuing is enabled yet.
   1013      1.34   mjacob 		 * It turns out that sometimes the ISP just ignores our
   1014      1.34   mjacob 		 * attempts to set parameters for devices that it hasn't
   1015      1.34   mjacob 		 * seen yet.
   1016      1.34   mjacob 		 */
   1017  1.39.2.2   bouyer 		sdp->isp_devparam[tgt].cur_dflags = sdf & ~DPARM_TQING;
   1018  1.39.2.2   bouyer 		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
   1019       1.1      cgd 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
   1020      1.36   mjacob 			mbs.param[1] = (channel << 15) | (tgt << 8) | lun;
   1021      1.10   mjacob 			mbs.param[2] = sdp->isp_max_queue_depth;
   1022      1.25   mjacob 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
   1023  1.39.2.2   bouyer 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   1024       1.1      cgd 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1025      1.25   mjacob 				break;
   1026       1.1      cgd 			}
   1027       1.1      cgd 		}
   1028      1.31   mjacob 	}
   1029  1.39.2.2   bouyer 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   1030  1.39.2.2   bouyer 		if (sdp->isp_devparam[tgt].dev_refresh) {
   1031  1.39.2.2   bouyer 			isp->isp_sendmarker |= (1 << channel);
   1032  1.39.2.2   bouyer 			isp->isp_update |= (1 << channel);
   1033  1.39.2.2   bouyer 			break;
   1034  1.39.2.2   bouyer 		}
   1035  1.39.2.2   bouyer 	}
   1036       1.1      cgd }
   1037       1.1      cgd 
   1038      1.24   mjacob /*
   1039      1.24   mjacob  * Fibre Channel specific initialization.
   1040      1.24   mjacob  *
   1041      1.24   mjacob  * Locks are held before coming here.
   1042      1.24   mjacob  */
   1043      1.10   mjacob static void
   1044  1.39.2.7   bouyer isp_fibre_init(struct ispsoftc *isp)
   1045      1.10   mjacob {
   1046      1.10   mjacob 	fcparam *fcp;
   1047      1.10   mjacob 	isp_icb_t *icbp;
   1048      1.10   mjacob 	mbreg_t mbs;
   1049      1.37   mjacob 	int loopid;
   1050  1.39.2.2   bouyer 	u_int64_t nwwn, pwwn;
   1051      1.10   mjacob 
   1052      1.10   mjacob 	fcp = isp->isp_param;
   1053      1.10   mjacob 
   1054  1.39.2.6   bouyer 	/*
   1055  1.39.2.6   bouyer 	 * Do this *before* initializing the firmware.
   1056  1.39.2.6   bouyer 	 */
   1057  1.39.2.6   bouyer 	isp_mark_getpdb_all(isp);
   1058  1.39.2.6   bouyer 	fcp->isp_fwstate = FW_CONFIG_WAIT;
   1059  1.39.2.6   bouyer 	fcp->isp_loopstate = LOOP_NIL;
   1060  1.39.2.6   bouyer 
   1061  1.39.2.6   bouyer 	/*
   1062  1.39.2.6   bouyer 	 * If we have no role (neither target nor initiator), return.
   1063  1.39.2.6   bouyer 	 */
   1064  1.39.2.6   bouyer 	if (isp->isp_role == ISP_ROLE_NONE) {
   1065  1.39.2.6   bouyer 		return;
   1066  1.39.2.6   bouyer 	}
   1067  1.39.2.6   bouyer 
   1068      1.37   mjacob 	loopid = DEFAULT_LOOPID(isp);
   1069      1.10   mjacob 	icbp = (isp_icb_t *) fcp->isp_scratch;
   1070      1.30   mjacob 	MEMZERO(icbp, sizeof (*icbp));
   1071      1.25   mjacob 
   1072      1.25   mjacob 	icbp->icb_version = ICB_VERSION1;
   1073  1.39.2.2   bouyer 
   1074  1.39.2.2   bouyer 	/*
   1075  1.39.2.2   bouyer 	 * Firmware Options are either retrieved from NVRAM or
   1076  1.39.2.2   bouyer 	 * are patched elsewhere. We check them for sanity here
   1077  1.39.2.2   bouyer 	 * and make changes based on board revision, but otherwise
   1078  1.39.2.2   bouyer 	 * let others decide policy.
   1079  1.39.2.2   bouyer 	 */
   1080  1.39.2.2   bouyer 
   1081  1.39.2.2   bouyer 	/*
   1082  1.39.2.2   bouyer 	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
   1083  1.39.2.2   bouyer 	 */
   1084  1.39.2.2   bouyer 	if ((isp->isp_type == ISP_HA_FC_2100) && isp->isp_revision < 5) {
   1085  1.39.2.2   bouyer 		fcp->isp_fwoptions &= ~ICBOPT_FAIRNESS;
   1086  1.39.2.2   bouyer 	}
   1087  1.39.2.2   bouyer 
   1088      1.37   mjacob 	/*
   1089      1.38   mjacob 	 * We have to use FULL LOGIN even though it resets the loop too much
   1090      1.38   mjacob 	 * because otherwise port database entries don't get updated after
   1091  1.39.2.2   bouyer 	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
   1092      1.37   mjacob 	 */
   1093      1.38   mjacob 	if (ISP_FW_REVX(isp->isp_fwrev) < ISP_FW_REV(1, 17, 0)) {
   1094      1.38   mjacob 		fcp->isp_fwoptions |= ICBOPT_FULL_LOGIN;
   1095      1.38   mjacob 	}
   1096  1.39.2.2   bouyer 
   1097  1.39.2.2   bouyer 	/*
   1098  1.39.2.2   bouyer 	 * Insist on Port Database Update Async notifications
   1099  1.39.2.2   bouyer 	 */
   1100  1.39.2.2   bouyer 	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
   1101      1.37   mjacob 
   1102  1.39.2.6   bouyer 
   1103      1.37   mjacob 	/*
   1104  1.39.2.6   bouyer 	 * Make sure that target role reflects into fwoptions.
   1105      1.37   mjacob 	 */
   1106  1.39.2.6   bouyer 	if (isp->isp_role & ISP_ROLE_TARGET) {
   1107  1.39.2.6   bouyer 		fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE;
   1108  1.39.2.6   bouyer 	} else {
   1109  1.39.2.6   bouyer 		fcp->isp_fwoptions &= ~ICBOPT_TGT_ENABLE;
   1110  1.39.2.6   bouyer 	}
   1111      1.31   mjacob 
   1112  1.39.2.6   bouyer 	/*
   1113  1.39.2.6   bouyer 	 * Propagate all of this into the ICB structure.
   1114  1.39.2.6   bouyer 	 */
   1115      1.27   mjacob 	icbp->icb_fwoptions = fcp->isp_fwoptions;
   1116      1.25   mjacob 	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
   1117      1.25   mjacob 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
   1118      1.25   mjacob 	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
   1119  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   1120  1.39.2.2   bouyer 		    "bad frame length (%d) from NVRAM- using %d",
   1121  1.39.2.2   bouyer 		    fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
   1122      1.35   mjacob 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
   1123      1.25   mjacob 	}
   1124      1.25   mjacob 	icbp->icb_maxalloc = fcp->isp_maxalloc;
   1125      1.37   mjacob 	if (icbp->icb_maxalloc < 1) {
   1126  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   1127  1.39.2.2   bouyer 		    "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
   1128      1.35   mjacob 		icbp->icb_maxalloc = 16;
   1129      1.35   mjacob 	}
   1130      1.25   mjacob 	icbp->icb_execthrottle = fcp->isp_execthrottle;
   1131      1.35   mjacob 	if (icbp->icb_execthrottle < 1) {
   1132  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   1133  1.39.2.2   bouyer 		    "bad execution throttle of %d- using 16",
   1134  1.39.2.2   bouyer 		    fcp->isp_execthrottle);
   1135      1.37   mjacob 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
   1136      1.35   mjacob 	}
   1137      1.25   mjacob 	icbp->icb_retry_delay = fcp->isp_retry_delay;
   1138      1.25   mjacob 	icbp->icb_retry_count = fcp->isp_retry_count;
   1139      1.28   mjacob 	icbp->icb_hardaddr = loopid;
   1140  1.39.2.2   bouyer 	/*
   1141  1.39.2.2   bouyer 	 * Right now we just set extended options to prefer point-to-point
   1142  1.39.2.2   bouyer 	 * over loop based upon some soft config options.
   1143  1.39.2.2   bouyer 	 */
   1144  1.39.2.2   bouyer 	if (IS_2200(isp)) {
   1145  1.39.2.2   bouyer 		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
   1146      1.37   mjacob 		/*
   1147  1.39.2.2   bouyer 		 * Prefer or force Point-To-Point instead Loop?
   1148      1.37   mjacob 		 */
   1149  1.39.2.3   bouyer 		switch(isp->isp_confopts & ISP_CFG_PORT_PREF) {
   1150  1.39.2.3   bouyer 		case ISP_CFG_NPORT:
   1151  1.39.2.2   bouyer 			icbp->icb_xfwoptions = ICBXOPT_PTP_2_LOOP;
   1152  1.39.2.3   bouyer 			break;
   1153  1.39.2.3   bouyer 		case ISP_CFG_NPORT_ONLY:
   1154  1.39.2.3   bouyer 			icbp->icb_xfwoptions = ICBXOPT_PTP_ONLY;
   1155  1.39.2.3   bouyer 			break;
   1156  1.39.2.3   bouyer 		case ISP_CFG_LPORT_ONLY:
   1157  1.39.2.3   bouyer 			icbp->icb_xfwoptions = ICBXOPT_LOOP_ONLY;
   1158  1.39.2.3   bouyer 			break;
   1159  1.39.2.3   bouyer 		default:
   1160  1.39.2.2   bouyer 			icbp->icb_xfwoptions = ICBXOPT_LOOP_2_PTP;
   1161  1.39.2.3   bouyer 			break;
   1162  1.39.2.3   bouyer 		}
   1163  1.39.2.2   bouyer 	}
   1164  1.39.2.2   bouyer 	icbp->icb_logintime = 60;	/* 60 second login timeout */
   1165  1.39.2.2   bouyer 
   1166  1.39.2.2   bouyer 	nwwn = ISP_NODEWWN(isp);
   1167  1.39.2.2   bouyer 	pwwn = ISP_PORTWWN(isp);
   1168  1.39.2.2   bouyer 	if (nwwn && pwwn) {
   1169  1.39.2.6   bouyer 		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
   1170  1.39.2.2   bouyer 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, nwwn);
   1171  1.39.2.2   bouyer 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
   1172  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGDEBUG1,
   1173  1.39.2.2   bouyer 		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
   1174  1.39.2.2   bouyer 		    ((u_int32_t) (nwwn >> 32)),
   1175  1.39.2.2   bouyer 		    ((u_int32_t) (nwwn & 0xffffffff)),
   1176  1.39.2.2   bouyer 		    ((u_int32_t) (pwwn >> 32)),
   1177  1.39.2.2   bouyer 		    ((u_int32_t) (pwwn & 0xffffffff)));
   1178      1.35   mjacob 	} else {
   1179  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGDEBUG1, "Not using any WWNs");
   1180  1.39.2.6   bouyer 		icbp->icb_fwoptions &= ~(ICBOPT_BOTH_WWNS|ICBOPT_FULL_LOGIN);
   1181      1.28   mjacob 	}
   1182  1.39.2.2   bouyer 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
   1183  1.39.2.2   bouyer 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
   1184      1.35   mjacob 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_LSW(isp->isp_rquest_dma);
   1185      1.35   mjacob 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_MSW(isp->isp_rquest_dma);
   1186      1.35   mjacob 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_LSW(isp->isp_result_dma);
   1187      1.35   mjacob 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_MSW(isp->isp_result_dma);
   1188  1.39.2.2   bouyer 	isp_prt(isp, ISP_LOGDEBUG1,
   1189  1.39.2.2   bouyer 	    "isp_fibre_init: fwoptions 0x%x", fcp->isp_fwoptions);
   1190      1.38   mjacob 	ISP_SWIZZLE_ICB(isp, icbp);
   1191      1.38   mjacob 
   1192  1.39.2.6   bouyer 
   1193      1.38   mjacob 	/*
   1194  1.39.2.6   bouyer 	 * Init the firmware
   1195      1.38   mjacob 	 */
   1196  1.39.2.2   bouyer 	mbs.param[0] = MBOX_INIT_FIRMWARE;
   1197  1.39.2.2   bouyer 	mbs.param[1] = 0;
   1198  1.39.2.2   bouyer 	mbs.param[2] = DMA_MSW(fcp->isp_scdma);
   1199  1.39.2.2   bouyer 	mbs.param[3] = DMA_LSW(fcp->isp_scdma);
   1200  1.39.2.2   bouyer 	mbs.param[4] = 0;
   1201  1.39.2.2   bouyer 	mbs.param[5] = 0;
   1202  1.39.2.2   bouyer 	mbs.param[6] = 0;
   1203  1.39.2.2   bouyer 	mbs.param[7] = 0;
   1204  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
   1205  1.39.2.2   bouyer 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1206  1.39.2.2   bouyer 		return;
   1207      1.10   mjacob 	}
   1208      1.25   mjacob 	isp->isp_reqidx = isp->isp_reqodx = 0;
   1209      1.10   mjacob 	isp->isp_residx = 0;
   1210      1.33   mjacob 	isp->isp_sendmarker = 1;
   1211      1.33   mjacob 
   1212      1.33   mjacob 	/*
   1213      1.33   mjacob 	 * Whatever happens, we're now committed to being here.
   1214      1.33   mjacob 	 */
   1215      1.33   mjacob 	isp->isp_state = ISP_INITSTATE;
   1216      1.33   mjacob }
   1217      1.33   mjacob 
   1218      1.33   mjacob /*
   1219      1.33   mjacob  * Fibre Channel Support- get the port database for the id.
   1220      1.33   mjacob  *
   1221      1.33   mjacob  * Locks are held before coming here. Return 0 if success,
   1222      1.33   mjacob  * else failure.
   1223      1.33   mjacob  */
   1224      1.33   mjacob 
   1225  1.39.2.6   bouyer static int
   1226  1.39.2.7   bouyer isp_getmap(struct ispsoftc *isp, fcpos_map_t *map)
   1227  1.39.2.6   bouyer {
   1228  1.39.2.6   bouyer 	fcparam *fcp = (fcparam *) isp->isp_param;
   1229  1.39.2.6   bouyer 	mbreg_t mbs;
   1230  1.39.2.6   bouyer 
   1231  1.39.2.6   bouyer 	mbs.param[0] = MBOX_GET_FC_AL_POSITION_MAP;
   1232  1.39.2.6   bouyer 	mbs.param[1] = 0;
   1233  1.39.2.6   bouyer 	mbs.param[2] = DMA_MSW(fcp->isp_scdma);
   1234  1.39.2.6   bouyer 	mbs.param[3] = DMA_LSW(fcp->isp_scdma);
   1235  1.39.2.6   bouyer 	/*
   1236  1.39.2.6   bouyer 	 * Unneeded. For the 2100, except for initializing f/w, registers
   1237  1.39.2.6   bouyer 	 * 4/5 have to not be written to.
   1238  1.39.2.6   bouyer 	 *	mbs.param[4] = 0;
   1239  1.39.2.6   bouyer 	 *	mbs.param[5] = 0;
   1240  1.39.2.6   bouyer 	 *
   1241  1.39.2.6   bouyer 	 */
   1242  1.39.2.6   bouyer 	mbs.param[6] = 0;
   1243  1.39.2.6   bouyer 	mbs.param[7] = 0;
   1244  1.39.2.6   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
   1245  1.39.2.6   bouyer 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   1246  1.39.2.6   bouyer 		MEMCPY(map, fcp->isp_scratch, sizeof (fcpos_map_t));
   1247  1.39.2.6   bouyer 		map->fwmap = mbs.param[1] != 0;
   1248  1.39.2.6   bouyer 		return (0);
   1249  1.39.2.6   bouyer 	}
   1250  1.39.2.6   bouyer 	return (-1);
   1251  1.39.2.6   bouyer }
   1252  1.39.2.6   bouyer 
   1253      1.33   mjacob static void
   1254  1.39.2.7   bouyer isp_mark_getpdb_all(struct ispsoftc *isp)
   1255      1.33   mjacob {
   1256      1.33   mjacob 	fcparam *fcp = (fcparam *) isp->isp_param;
   1257      1.37   mjacob 	int i;
   1258      1.37   mjacob 	for (i = 0; i < MAX_FC_TARG; i++) {
   1259  1.39.2.6   bouyer 		fcp->portdb[i].valid = fcp->portdb[i].fabric_dev = 0;
   1260      1.33   mjacob 	}
   1261      1.33   mjacob }
   1262      1.33   mjacob 
   1263      1.33   mjacob static int
   1264  1.39.2.7   bouyer isp_getpdb(struct ispsoftc *isp, int id, isp_pdb_t *pdbp)
   1265      1.33   mjacob {
   1266      1.33   mjacob 	fcparam *fcp = (fcparam *) isp->isp_param;
   1267      1.33   mjacob 	mbreg_t mbs;
   1268      1.37   mjacob 
   1269      1.33   mjacob 	mbs.param[0] = MBOX_GET_PORT_DB;
   1270      1.33   mjacob 	mbs.param[1] = id << 8;
   1271      1.35   mjacob 	mbs.param[2] = DMA_MSW(fcp->isp_scdma);
   1272      1.35   mjacob 	mbs.param[3] = DMA_LSW(fcp->isp_scdma);
   1273      1.34   mjacob 	/*
   1274      1.34   mjacob 	 * Unneeded. For the 2100, except for initializing f/w, registers
   1275      1.34   mjacob 	 * 4/5 have to not be written to.
   1276      1.34   mjacob 	 *	mbs.param[4] = 0;
   1277      1.34   mjacob 	 *	mbs.param[5] = 0;
   1278      1.34   mjacob 	 *
   1279      1.34   mjacob 	 */
   1280      1.33   mjacob 	mbs.param[6] = 0;
   1281      1.33   mjacob 	mbs.param[7] = 0;
   1282  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
   1283  1.39.2.2   bouyer 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   1284      1.38   mjacob 		ISP_UNSWIZZLE_AND_COPY_PDBP(isp, pdbp, fcp->isp_scratch);
   1285  1.39.2.2   bouyer 		return (0);
   1286      1.33   mjacob 	}
   1287  1.39.2.2   bouyer 	return (-1);
   1288      1.33   mjacob }
   1289      1.33   mjacob 
   1290      1.37   mjacob static u_int64_t
   1291  1.39.2.7   bouyer isp_get_portname(struct ispsoftc *isp, int loopid, int nodename)
   1292      1.37   mjacob {
   1293      1.37   mjacob 	u_int64_t wwn = 0;
   1294      1.37   mjacob 	mbreg_t mbs;
   1295      1.37   mjacob 
   1296      1.37   mjacob 	mbs.param[0] = MBOX_GET_PORT_NAME;
   1297      1.37   mjacob 	mbs.param[1] = loopid << 8;
   1298      1.37   mjacob 	if (nodename)
   1299      1.37   mjacob 		mbs.param[1] |= 1;
   1300  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
   1301      1.37   mjacob 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   1302      1.37   mjacob 		wwn =
   1303      1.37   mjacob 		    (((u_int64_t)(mbs.param[2] & 0xff)) << 56) |
   1304      1.37   mjacob 		    (((u_int64_t)(mbs.param[2] >> 8))	<< 48) |
   1305      1.37   mjacob 		    (((u_int64_t)(mbs.param[3] & 0xff))	<< 40) |
   1306      1.37   mjacob 		    (((u_int64_t)(mbs.param[3] >> 8))	<< 32) |
   1307      1.37   mjacob 		    (((u_int64_t)(mbs.param[6] & 0xff))	<< 24) |
   1308      1.37   mjacob 		    (((u_int64_t)(mbs.param[6] >> 8))	<< 16) |
   1309      1.37   mjacob 		    (((u_int64_t)(mbs.param[7] & 0xff))	<<  8) |
   1310      1.37   mjacob 		    (((u_int64_t)(mbs.param[7] >> 8)));
   1311      1.37   mjacob 	}
   1312      1.37   mjacob 	return (wwn);
   1313      1.37   mjacob }
   1314      1.37   mjacob 
   1315      1.33   mjacob /*
   1316      1.33   mjacob  * Make sure we have good FC link and know our Loop ID.
   1317      1.33   mjacob  */
   1318      1.33   mjacob 
   1319      1.33   mjacob static int
   1320  1.39.2.7   bouyer isp_fclink_test(struct ispsoftc *isp, int usdelay)
   1321      1.33   mjacob {
   1322      1.38   mjacob 	static char *toponames[] = {
   1323      1.38   mjacob 		"Private Loop",
   1324      1.38   mjacob 		"FL Port",
   1325      1.38   mjacob 		"N-Port to N-Port",
   1326  1.39.2.2   bouyer 		"F Port",
   1327  1.39.2.2   bouyer 		"F Port (no FLOGI_ACC response)"
   1328      1.38   mjacob 	};
   1329      1.33   mjacob 	mbreg_t mbs;
   1330  1.39.2.6   bouyer 	int count, check_for_fabric;
   1331      1.33   mjacob 	u_int8_t lwfs;
   1332      1.33   mjacob 	fcparam *fcp;
   1333  1.39.2.6   bouyer 	struct lportdb *lp;
   1334      1.37   mjacob 	isp_pdb_t pdb;
   1335  1.39.2.6   bouyer 
   1336      1.33   mjacob 	fcp = isp->isp_param;
   1337      1.10   mjacob 
   1338      1.10   mjacob 	/*
   1339  1.39.2.2   bouyer 	 * XXX: Here is where we would start a 'loop dead' timeout
   1340  1.39.2.2   bouyer 	 */
   1341  1.39.2.2   bouyer 
   1342  1.39.2.2   bouyer 	/*
   1343      1.33   mjacob 	 * Wait up to N microseconds for F/W to go to a ready state.
   1344      1.10   mjacob 	 */
   1345      1.23   mjacob 	lwfs = FW_CONFIG_WAIT;
   1346  1.39.2.2   bouyer 	count = 0;
   1347  1.39.2.2   bouyer 	while (count < usdelay) {
   1348  1.39.2.2   bouyer 		u_int64_t enano;
   1349  1.39.2.2   bouyer 		u_int32_t wrk;
   1350  1.39.2.2   bouyer 		NANOTIME_T hra, hrb;
   1351  1.39.2.2   bouyer 
   1352  1.39.2.2   bouyer 		GET_NANOTIME(&hra);
   1353      1.10   mjacob 		isp_fw_state(isp);
   1354      1.23   mjacob 		if (lwfs != fcp->isp_fwstate) {
   1355  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGINFO, "Firmware State <%s->%s>",
   1356  1.39.2.2   bouyer 			    isp2100_fw_statename((int)lwfs),
   1357      1.28   mjacob 			    isp2100_fw_statename((int)fcp->isp_fwstate));
   1358      1.23   mjacob 			lwfs = fcp->isp_fwstate;
   1359      1.23   mjacob 		}
   1360      1.23   mjacob 		if (fcp->isp_fwstate == FW_READY) {
   1361      1.10   mjacob 			break;
   1362      1.23   mjacob 		}
   1363  1.39.2.2   bouyer 		GET_NANOTIME(&hrb);
   1364  1.39.2.2   bouyer 
   1365  1.39.2.2   bouyer 		/*
   1366  1.39.2.2   bouyer 		 * Get the elapsed time in nanoseconds.
   1367  1.39.2.2   bouyer 		 * Always guaranteed to be non-zero.
   1368  1.39.2.2   bouyer 		 */
   1369  1.39.2.2   bouyer 		enano = NANOTIME_SUB(&hrb, &hra);
   1370  1.39.2.2   bouyer 
   1371  1.39.2.7   bouyer 		isp_prt(isp, ISP_LOGDEBUG1,
   1372  1.39.2.7   bouyer 		    "usec%d: 0x%lx->0x%lx enano 0x%x%08x",
   1373  1.39.2.4   bouyer 		    count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb),
   1374  1.39.2.7   bouyer 		    (u_int32_t)(enano >> 32), (u_int32_t)(enano & 0xffffffff));
   1375  1.39.2.4   bouyer 
   1376  1.39.2.2   bouyer 		/*
   1377  1.39.2.2   bouyer 		 * If the elapsed time is less than 1 millisecond,
   1378  1.39.2.2   bouyer 		 * delay a period of time up to that millisecond of
   1379  1.39.2.2   bouyer 		 * waiting.
   1380  1.39.2.4   bouyer 		 *
   1381  1.39.2.2   bouyer 		 * This peculiar code is an attempt to try and avoid
   1382  1.39.2.2   bouyer 		 * invoking u_int64_t math support functions for some
   1383  1.39.2.2   bouyer 		 * platforms where linkage is a problem.
   1384  1.39.2.2   bouyer 		 */
   1385  1.39.2.2   bouyer 		if (enano < (1000 * 1000)) {
   1386  1.39.2.2   bouyer 			count += 1000;
   1387  1.39.2.2   bouyer 			enano = (1000 * 1000) - enano;
   1388  1.39.2.2   bouyer 			while (enano > (u_int64_t) 4000000000U) {
   1389  1.39.2.3   bouyer 				USEC_SLEEP(isp, 4000000);
   1390  1.39.2.2   bouyer 				enano -= (u_int64_t) 4000000000U;
   1391  1.39.2.2   bouyer 			}
   1392  1.39.2.2   bouyer 			wrk = enano;
   1393  1.39.2.7   bouyer 			wrk /= 1000;
   1394  1.39.2.7   bouyer 			USEC_SLEEP(isp, wrk);
   1395  1.39.2.2   bouyer 		} else {
   1396  1.39.2.2   bouyer 			while (enano > (u_int64_t) 4000000000U) {
   1397  1.39.2.2   bouyer 				count += 4000000;
   1398  1.39.2.2   bouyer 				enano -= (u_int64_t) 4000000000U;
   1399  1.39.2.2   bouyer 			}
   1400  1.39.2.2   bouyer 			wrk = enano;
   1401  1.39.2.2   bouyer 			count += (wrk / 1000);
   1402  1.39.2.2   bouyer 		}
   1403      1.10   mjacob 	}
   1404      1.10   mjacob 
   1405      1.10   mjacob 	/*
   1406      1.33   mjacob 	 * If we haven't gone to 'ready' state, return.
   1407      1.33   mjacob 	 */
   1408      1.33   mjacob 	if (fcp->isp_fwstate != FW_READY) {
   1409      1.33   mjacob 		return (-1);
   1410      1.33   mjacob 	}
   1411      1.34   mjacob 
   1412      1.33   mjacob 	/*
   1413      1.33   mjacob 	 * Get our Loop ID (if possible). We really need to have it.
   1414      1.10   mjacob 	 */
   1415      1.33   mjacob 	mbs.param[0] = MBOX_GET_LOOP_ID;
   1416  1.39.2.2   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
   1417      1.33   mjacob 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   1418      1.33   mjacob 		return (-1);
   1419      1.23   mjacob 	}
   1420      1.33   mjacob 	fcp->isp_loopid = mbs.param[1];
   1421  1.39.2.2   bouyer 	if (IS_2200(isp)) {
   1422  1.39.2.2   bouyer 		int topo = (int) mbs.param[6];
   1423  1.39.2.2   bouyer 		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB)
   1424  1.39.2.2   bouyer 			topo = TOPO_PTP_STUB;
   1425  1.39.2.2   bouyer 		fcp->isp_topo = topo;
   1426  1.39.2.2   bouyer 	} else {
   1427  1.39.2.2   bouyer 		fcp->isp_topo = TOPO_NL_PORT;
   1428      1.38   mjacob 	}
   1429  1.39.2.6   bouyer 	fcp->isp_portid = fcp->isp_alpa = mbs.param[2] & 0xff;
   1430  1.39.2.2   bouyer 
   1431  1.39.2.6   bouyer 	/*
   1432  1.39.2.6   bouyer 	 * Check to see if we're on a fabric by trying to see if we
   1433  1.39.2.6   bouyer 	 * can talk to the fabric name server. This can be a bit
   1434  1.39.2.6   bouyer 	 * tricky because if we're a 2100, we should check always
   1435  1.39.2.6   bouyer 	 * (in case we're connected to an server doing aliasing).
   1436  1.39.2.6   bouyer 	 */
   1437      1.38   mjacob 	fcp->isp_onfabric = 0;
   1438  1.39.2.6   bouyer 
   1439  1.39.2.6   bouyer 	if (IS_2100(isp))
   1440  1.39.2.6   bouyer 		check_for_fabric = 1;
   1441  1.39.2.6   bouyer 	else if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_F_PORT)
   1442  1.39.2.6   bouyer 		check_for_fabric = 1;
   1443  1.39.2.6   bouyer 	else
   1444  1.39.2.6   bouyer 		check_for_fabric = 0;
   1445  1.39.2.6   bouyer 
   1446  1.39.2.6   bouyer 	if (check_for_fabric && isp_getpdb(isp, FL_PORT_ID, &pdb) == 0) {
   1447  1.39.2.6   bouyer 		int loopid = FL_PORT_ID;
   1448  1.39.2.2   bouyer 		if (IS_2100(isp)) {
   1449  1.39.2.2   bouyer 			fcp->isp_topo = TOPO_FL_PORT;
   1450  1.39.2.2   bouyer 		}
   1451  1.39.2.6   bouyer 
   1452  1.39.2.6   bouyer 		if (BITS2WORD(pdb.pdb_portid_bits) == 0) {
   1453  1.39.2.6   bouyer 			/*
   1454  1.39.2.6   bouyer 			 * Crock.
   1455  1.39.2.6   bouyer 			 */
   1456  1.39.2.6   bouyer 			fcp->isp_topo = TOPO_NL_PORT;
   1457  1.39.2.6   bouyer 			goto not_on_fabric;
   1458  1.39.2.6   bouyer 		}
   1459  1.39.2.6   bouyer 		fcp->isp_portid = mbs.param[2] | ((int) mbs.param[3] << 16);
   1460      1.37   mjacob 
   1461      1.37   mjacob 		/*
   1462  1.39.2.2   bouyer 		 * Save the Fabric controller's port database entry.
   1463      1.37   mjacob 		 */
   1464  1.39.2.6   bouyer 		lp = &fcp->portdb[loopid];
   1465  1.39.2.2   bouyer 		lp->node_wwn =
   1466  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
   1467  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
   1468  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
   1469  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
   1470  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
   1471  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
   1472  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
   1473  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_nodename[7]));
   1474  1.39.2.2   bouyer 		lp->port_wwn =
   1475  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
   1476  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
   1477  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
   1478  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
   1479  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
   1480  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
   1481  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
   1482  1.39.2.2   bouyer 		    (((u_int64_t)pdb.pdb_portname[7]));
   1483  1.39.2.2   bouyer 		lp->roles =
   1484  1.39.2.2   bouyer 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   1485  1.39.2.2   bouyer 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
   1486  1.39.2.2   bouyer 		lp->loopid = pdb.pdb_loopid;
   1487  1.39.2.2   bouyer 		lp->loggedin = lp->valid = 1;
   1488  1.39.2.6   bouyer 		fcp->isp_onfabric = 1;
   1489  1.39.2.6   bouyer 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
   1490  1.39.2.5   bouyer 		isp_register_fc4_type(isp);
   1491  1.39.2.6   bouyer 	} else {
   1492  1.39.2.6   bouyer not_on_fabric:
   1493  1.39.2.2   bouyer 		fcp->isp_onfabric = 0;
   1494  1.39.2.2   bouyer 		fcp->portdb[FL_PORT_ID].valid = 0;
   1495  1.39.2.2   bouyer 	}
   1496  1.39.2.2   bouyer 
   1497  1.39.2.2   bouyer 	isp_prt(isp, ISP_LOGINFO, topology, fcp->isp_loopid, fcp->isp_alpa,
   1498  1.39.2.2   bouyer 	    fcp->isp_portid, fcp->isp_loopstate, toponames[fcp->isp_topo]);
   1499  1.39.2.2   bouyer 
   1500  1.39.2.6   bouyer 	/*
   1501  1.39.2.6   bouyer 	 * Announce ourselves, too. This involves synthesizing an entry.
   1502  1.39.2.6   bouyer 	 */
   1503  1.39.2.6   bouyer 	if (fcp->isp_iid_set == 0) {
   1504  1.39.2.6   bouyer 		fcp->isp_iid_set = 1;
   1505  1.39.2.6   bouyer 		fcp->isp_iid = fcp->isp_loopid;
   1506  1.39.2.6   bouyer 		lp = &fcp->portdb[fcp->isp_iid];
   1507  1.39.2.6   bouyer 	} else {
   1508  1.39.2.6   bouyer 		lp = &fcp->portdb[fcp->isp_iid];
   1509  1.39.2.6   bouyer 		if (fcp->isp_portid != lp->portid ||
   1510  1.39.2.6   bouyer 		    fcp->isp_loopid != lp->loopid ||
   1511  1.39.2.6   bouyer 		    fcp->isp_nodewwn != ISP_NODEWWN(isp) ||
   1512  1.39.2.6   bouyer 		    fcp->isp_portwwn != ISP_PORTWWN(isp)) {
   1513  1.39.2.6   bouyer 			lp->valid = 0;
   1514  1.39.2.6   bouyer 			count = fcp->isp_iid;
   1515  1.39.2.6   bouyer 			(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
   1516  1.39.2.6   bouyer 		}
   1517  1.39.2.6   bouyer 	}
   1518  1.39.2.6   bouyer 	lp->loopid = fcp->isp_loopid;
   1519  1.39.2.6   bouyer 	lp->portid = fcp->isp_portid;
   1520  1.39.2.6   bouyer 	lp->node_wwn = ISP_NODEWWN(isp);
   1521  1.39.2.6   bouyer 	lp->port_wwn = ISP_PORTWWN(isp);
   1522  1.39.2.6   bouyer 	switch (isp->isp_role) {
   1523  1.39.2.6   bouyer 	case ISP_ROLE_NONE:
   1524  1.39.2.6   bouyer 		lp->roles = 0;
   1525  1.39.2.6   bouyer 		break;
   1526  1.39.2.6   bouyer 	case ISP_ROLE_TARGET:
   1527  1.39.2.6   bouyer 		lp->roles = SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT;
   1528  1.39.2.6   bouyer 		break;
   1529  1.39.2.6   bouyer 	case ISP_ROLE_INITIATOR:
   1530  1.39.2.6   bouyer 		lp->roles = SVC3_INI_ROLE >> SVC3_ROLE_SHIFT;
   1531  1.39.2.6   bouyer 		break;
   1532  1.39.2.6   bouyer 	case ISP_ROLE_BOTH:
   1533  1.39.2.6   bouyer 		lp->roles = (SVC3_INI_ROLE|SVC3_TGT_ROLE) >> SVC3_ROLE_SHIFT;
   1534  1.39.2.6   bouyer 		break;
   1535  1.39.2.6   bouyer 	}
   1536  1.39.2.6   bouyer 	lp->loggedin = lp->valid = 1;
   1537  1.39.2.6   bouyer 	count = fcp->isp_iid;
   1538  1.39.2.6   bouyer 	(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
   1539      1.33   mjacob 	return (0);
   1540      1.37   mjacob }
   1541      1.37   mjacob 
   1542  1.39.2.2   bouyer static char *
   1543  1.39.2.7   bouyer isp2100_fw_statename(int state)
   1544  1.39.2.2   bouyer {
   1545  1.39.2.2   bouyer 	switch(state) {
   1546  1.39.2.2   bouyer 	case FW_CONFIG_WAIT:	return "Config Wait";
   1547  1.39.2.2   bouyer 	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
   1548  1.39.2.2   bouyer 	case FW_WAIT_LOGIN:	return "Wait Login";
   1549  1.39.2.2   bouyer 	case FW_READY:		return "Ready";
   1550  1.39.2.2   bouyer 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
   1551  1.39.2.2   bouyer 	case FW_ERROR:		return "Error";
   1552  1.39.2.2   bouyer 	case FW_REINIT:		return "Re-Init";
   1553  1.39.2.2   bouyer 	case FW_NON_PART:	return "Nonparticipating";
   1554  1.39.2.2   bouyer 	default:		return "?????";
   1555  1.39.2.2   bouyer 	}
   1556  1.39.2.2   bouyer }
   1557  1.39.2.2   bouyer 
   1558       1.1      cgd /*
   1559      1.37   mjacob  * Synchronize our soft copy of the port database with what the f/w thinks
   1560      1.37   mjacob  * (with a view toward possibly for a specific target....)
   1561      1.37   mjacob  */
   1562      1.37   mjacob 
   1563      1.37   mjacob static int
   1564  1.39.2.7   bouyer isp_pdb_sync(struct ispsoftc *isp)
   1565      1.37   mjacob {
   1566  1.39.2.6   bouyer 	struct lportdb *lp;
   1567      1.37   mjacob 	fcparam *fcp = isp->isp_param;
   1568      1.37   mjacob 	isp_pdb_t pdb;
   1569  1.39.2.6   bouyer 	int loopid, base, lim;
   1570      1.37   mjacob 
   1571      1.37   mjacob 	/*
   1572  1.39.2.6   bouyer 	 * Make sure we're okay for doing this right now.
   1573      1.37   mjacob 	 */
   1574  1.39.2.6   bouyer 	if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
   1575  1.39.2.6   bouyer 	    fcp->isp_loopstate != LOOP_FSCAN_DONE &&
   1576  1.39.2.6   bouyer 	    fcp->isp_loopstate != LOOP_LSCAN_DONE) {
   1577  1.39.2.6   bouyer 		return (-1);
   1578  1.39.2.6   bouyer 	}
   1579  1.39.2.6   bouyer 
   1580  1.39.2.6   bouyer 	if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT ||
   1581  1.39.2.6   bouyer 	    fcp->isp_topo == TOPO_N_PORT) {
   1582  1.39.2.6   bouyer 		if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
   1583  1.39.2.6   bouyer 			if (isp_scan_loop(isp) != 0) {
   1584  1.39.2.6   bouyer 				return (-1);
   1585  1.39.2.6   bouyer 			}
   1586  1.39.2.6   bouyer 		}
   1587  1.39.2.6   bouyer 	}
   1588  1.39.2.6   bouyer 	fcp->isp_loopstate = LOOP_SYNCING_PDB;
   1589  1.39.2.6   bouyer 
   1590      1.37   mjacob 	/*
   1591  1.39.2.6   bouyer 	 * If we get this far, we've settled our differences with the f/w
   1592  1.39.2.6   bouyer 	 * (for local loop device) and we can say that the loop state is ready.
   1593      1.37   mjacob 	 */
   1594      1.38   mjacob 
   1595  1.39.2.6   bouyer 	if (fcp->isp_topo == TOPO_NL_PORT) {
   1596  1.39.2.6   bouyer 		fcp->loop_seen_once = 1;
   1597  1.39.2.6   bouyer 		fcp->isp_loopstate = LOOP_READY;
   1598  1.39.2.6   bouyer 		return (0);
   1599  1.39.2.2   bouyer 	}
   1600  1.39.2.2   bouyer 
   1601      1.37   mjacob 	/*
   1602  1.39.2.6   bouyer 	 * Find all Fabric Entities that didn't make it from one scan to the
   1603  1.39.2.6   bouyer 	 * next and let the world know they went away. Scan the whole database.
   1604      1.37   mjacob 	 */
   1605  1.39.2.6   bouyer 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
   1606  1.39.2.6   bouyer 		if (lp->was_fabric_dev && lp->fabric_dev == 0) {
   1607  1.39.2.6   bouyer 			loopid = lp - fcp->portdb;
   1608  1.39.2.6   bouyer 			lp->valid = 0;	/* should already be set */
   1609  1.39.2.6   bouyer 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
   1610  1.39.2.6   bouyer 			MEMZERO((void *) lp, sizeof (*lp));
   1611  1.39.2.6   bouyer 			continue;
   1612  1.39.2.6   bouyer 		}
   1613  1.39.2.6   bouyer 		lp->was_fabric_dev = lp->fabric_dev;
   1614  1.39.2.6   bouyer 	}
   1615  1.39.2.6   bouyer 
   1616  1.39.2.6   bouyer 	if (fcp->isp_topo == TOPO_FL_PORT)
   1617  1.39.2.6   bouyer 		base = FC_SNS_ID+1;
   1618  1.39.2.6   bouyer 	else
   1619  1.39.2.6   bouyer 		base = 0;
   1620  1.39.2.6   bouyer 
   1621  1.39.2.6   bouyer 	if (fcp->isp_topo == TOPO_N_PORT)
   1622  1.39.2.6   bouyer 		lim = 1;
   1623  1.39.2.6   bouyer 	else
   1624  1.39.2.6   bouyer 		lim = MAX_FC_TARG;
   1625  1.39.2.2   bouyer 
   1626  1.39.2.2   bouyer 	/*
   1627  1.39.2.6   bouyer 	 * Now log in any fabric devices that the outer layer has
   1628  1.39.2.6   bouyer 	 * left for us to see. This seems the most sane policy
   1629  1.39.2.6   bouyer 	 * for the moment.
   1630  1.39.2.2   bouyer 	 */
   1631  1.39.2.6   bouyer 	for (lp = &fcp->portdb[base]; lp < &fcp->portdb[lim]; lp++) {
   1632  1.39.2.6   bouyer 		u_int32_t portid;
   1633  1.39.2.6   bouyer 		mbreg_t mbs;
   1634  1.39.2.2   bouyer 
   1635  1.39.2.6   bouyer 		loopid = lp - fcp->portdb;
   1636  1.39.2.6   bouyer 		if (loopid >= FL_PORT_ID && loopid <= FC_SNS_ID) {
   1637      1.37   mjacob 			continue;
   1638  1.39.2.6   bouyer 		}
   1639  1.39.2.6   bouyer 
   1640  1.39.2.6   bouyer 		/*
   1641  1.39.2.6   bouyer 		 * Anything here?
   1642  1.39.2.6   bouyer 		 */
   1643      1.37   mjacob 		if (lp->port_wwn == 0) {
   1644      1.37   mjacob 			continue;
   1645      1.37   mjacob 		}
   1646      1.38   mjacob 
   1647      1.37   mjacob 		/*
   1648  1.39.2.6   bouyer 		 * Don't try to log into yourself.
   1649      1.37   mjacob 		 */
   1650  1.39.2.6   bouyer 		if ((portid = lp->portid) == fcp->isp_portid) {
   1651      1.37   mjacob 			continue;
   1652      1.37   mjacob 		}
   1653      1.38   mjacob 
   1654  1.39.2.2   bouyer 
   1655      1.37   mjacob 		/*
   1656  1.39.2.6   bouyer 		 * If we'd been logged in- see if we still are and we haven't
   1657  1.39.2.6   bouyer 		 * changed. If so, no need to log ourselves out, etc..
   1658  1.39.2.6   bouyer 		 *
   1659  1.39.2.6   bouyer 		 * Unfortunately, our charming Qlogic f/w has decided to
   1660  1.39.2.6   bouyer 		 * return a valid port database entry for a fabric device
   1661  1.39.2.6   bouyer 		 * that has, in fact, gone away. And it hangs trying to
   1662  1.39.2.6   bouyer 		 * log it out.
   1663      1.37   mjacob 		 */
   1664  1.39.2.6   bouyer 		if (lp->loggedin &&
   1665  1.39.2.6   bouyer 		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
   1666  1.39.2.6   bouyer 			int nrole;
   1667  1.39.2.6   bouyer 			u_int64_t nwwnn, nwwpn;
   1668  1.39.2.6   bouyer 			nwwnn =
   1669  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
   1670  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
   1671  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
   1672  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
   1673  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
   1674  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
   1675  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
   1676  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_nodename[7]));
   1677  1.39.2.6   bouyer 			nwwpn =
   1678  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
   1679  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
   1680  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
   1681  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
   1682  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
   1683  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
   1684  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
   1685  1.39.2.6   bouyer 			    (((u_int64_t)pdb.pdb_portname[7]));
   1686  1.39.2.6   bouyer 			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
   1687  1.39.2.6   bouyer 			    SVC3_ROLE_SHIFT;
   1688  1.39.2.6   bouyer 			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
   1689  1.39.2.6   bouyer 			    (u_int32_t) BITS2WORD(pdb.pdb_portid_bits) &&
   1690  1.39.2.6   bouyer 			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
   1691  1.39.2.6   bouyer 			    lp->roles == nrole) {
   1692  1.39.2.6   bouyer 				lp->loggedin = lp->valid = 1;
   1693  1.39.2.6   bouyer 				isp_prt(isp, ISP_LOGINFO, lretained,
   1694  1.39.2.6   bouyer 				    (int) (lp - fcp->portdb),
   1695  1.39.2.6   bouyer 				    (int) lp->loopid, lp->portid);
   1696      1.37   mjacob 				continue;
   1697      1.37   mjacob 			}
   1698      1.37   mjacob 		}
   1699      1.38   mjacob 
   1700  1.39.2.6   bouyer 		if (fcp->isp_fwstate != FW_READY ||
   1701  1.39.2.6   bouyer 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
   1702  1.39.2.6   bouyer 			return (-1);
   1703      1.37   mjacob 		}
   1704      1.37   mjacob 
   1705      1.37   mjacob 		/*
   1706  1.39.2.6   bouyer 		 * Force a logout if we were logged in.
   1707      1.37   mjacob 		 */
   1708  1.39.2.6   bouyer 		if (lp->loggedin) {
   1709  1.39.2.6   bouyer 			if (isp_getpdb(isp, lp->loopid, &pdb) == 0) {
   1710  1.39.2.6   bouyer 				mbs.param[0] = MBOX_FABRIC_LOGOUT;
   1711  1.39.2.6   bouyer 				mbs.param[1] = lp->loopid << 8;
   1712  1.39.2.6   bouyer 				mbs.param[2] = 0;
   1713  1.39.2.6   bouyer 				mbs.param[3] = 0;
   1714  1.39.2.6   bouyer 				isp_mboxcmd(isp, &mbs, MBLOGNONE);
   1715  1.39.2.6   bouyer 				lp->loggedin = 0;
   1716  1.39.2.6   bouyer 				isp_prt(isp, ISP_LOGINFO, plogout,
   1717  1.39.2.6   bouyer 				    (int) (lp - fcp->portdb), lp->loopid,
   1718  1.39.2.6   bouyer 				    lp->portid);
   1719  1.39.2.6   bouyer 			}
   1720  1.39.2.6   bouyer 			lp->loggedin = 0;
   1721  1.39.2.6   bouyer 			if (fcp->isp_fwstate != FW_READY ||
   1722  1.39.2.6   bouyer 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
   1723  1.39.2.6   bouyer 				return (-1);
   1724  1.39.2.6   bouyer 			}
   1725      1.37   mjacob 		}
   1726      1.37   mjacob 
   1727      1.37   mjacob 		/*
   1728  1.39.2.6   bouyer 		 * And log in....
   1729      1.37   mjacob 		 */
   1730  1.39.2.6   bouyer 		loopid = lp - fcp->portdb;
   1731  1.39.2.6   bouyer 		lp->loopid = FL_PORT_ID;
   1732  1.39.2.6   bouyer 		do {
   1733  1.39.2.6   bouyer 			mbs.param[0] = MBOX_FABRIC_LOGIN;
   1734  1.39.2.6   bouyer 			mbs.param[1] = loopid << 8;
   1735  1.39.2.6   bouyer 			mbs.param[2] = portid >> 16;
   1736  1.39.2.6   bouyer 			mbs.param[3] = portid & 0xffff;
   1737  1.39.2.6   bouyer 			if (IS_2200(isp)) {
   1738  1.39.2.6   bouyer 				/* only issue a PLOGI if not logged in */
   1739  1.39.2.6   bouyer 				mbs.param[1] |= 0x1;
   1740      1.37   mjacob 			}
   1741  1.39.2.6   bouyer 			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
   1742  1.39.2.6   bouyer 			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
   1743  1.39.2.6   bouyer 			if (fcp->isp_fwstate != FW_READY ||
   1744  1.39.2.6   bouyer 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
   1745  1.39.2.6   bouyer 				return (-1);
   1746  1.39.2.6   bouyer 			}
   1747  1.39.2.6   bouyer 			switch (mbs.param[0]) {
   1748  1.39.2.6   bouyer 			case MBOX_LOOP_ID_USED:
   1749      1.37   mjacob 				/*
   1750  1.39.2.6   bouyer 				 * Try the next available loop id.
   1751      1.37   mjacob 				 */
   1752  1.39.2.6   bouyer 				loopid++;
   1753  1.39.2.6   bouyer 				break;
   1754  1.39.2.6   bouyer 			case MBOX_PORT_ID_USED:
   1755  1.39.2.6   bouyer 				/*
   1756  1.39.2.6   bouyer 				 * This port is already logged in.
   1757  1.39.2.6   bouyer 				 * Snaffle the loop id it's using if it's
   1758  1.39.2.6   bouyer 				 * nonzero, otherwise we're hosed.
   1759  1.39.2.6   bouyer 				 */
   1760  1.39.2.6   bouyer 				if (mbs.param[1] != 0) {
   1761  1.39.2.6   bouyer 					loopid = mbs.param[1];
   1762  1.39.2.6   bouyer 					isp_prt(isp, ISP_LOGINFO, retained,
   1763  1.39.2.6   bouyer 					    loopid, (int) (lp - fcp->portdb),
   1764  1.39.2.6   bouyer 					    lp->portid);
   1765  1.39.2.6   bouyer 				} else {
   1766  1.39.2.6   bouyer 					loopid = MAX_FC_TARG;
   1767  1.39.2.6   bouyer 					break;
   1768  1.39.2.6   bouyer 				}
   1769  1.39.2.6   bouyer 				/* FALLTHROUGH */
   1770  1.39.2.6   bouyer 			case MBOX_COMMAND_COMPLETE:
   1771  1.39.2.6   bouyer 				lp->loggedin = 1;
   1772  1.39.2.6   bouyer 				lp->loopid = loopid;
   1773  1.39.2.6   bouyer 				break;
   1774  1.39.2.6   bouyer 			case MBOX_COMMAND_ERROR:
   1775  1.39.2.6   bouyer 				isp_prt(isp, ISP_LOGINFO, plogierr,
   1776  1.39.2.6   bouyer 				    portid, mbs.param[1]);
   1777  1.39.2.6   bouyer 				/* FALLTHROUGH */
   1778  1.39.2.6   bouyer 			case MBOX_ALL_IDS_USED: /* We're outta IDs */
   1779  1.39.2.6   bouyer 			default:
   1780  1.39.2.6   bouyer 				loopid = MAX_FC_TARG;
   1781  1.39.2.6   bouyer 				break;
   1782      1.37   mjacob 			}
   1783  1.39.2.6   bouyer 		} while (lp->loopid == FL_PORT_ID && loopid < MAX_FC_TARG);
   1784      1.37   mjacob 
   1785      1.37   mjacob 		/*
   1786  1.39.2.6   bouyer 		 * If we get here and we haven't set a Loop ID,
   1787  1.39.2.6   bouyer 		 * we failed to log into this device.
   1788      1.37   mjacob 		 */
   1789  1.39.2.6   bouyer 
   1790  1.39.2.6   bouyer 		if (lp->loopid == FL_PORT_ID) {
   1791  1.39.2.6   bouyer 			lp->loopid = 0;
   1792      1.37   mjacob 			continue;
   1793      1.37   mjacob 		}
   1794      1.37   mjacob 
   1795      1.37   mjacob 		/*
   1796  1.39.2.6   bouyer 		 * Make sure we can get the approriate port information.
   1797      1.37   mjacob 		 */
   1798  1.39.2.6   bouyer 		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
   1799  1.39.2.6   bouyer 			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
   1800  1.39.2.6   bouyer 			goto dump_em;
   1801      1.37   mjacob 		}
   1802      1.37   mjacob 
   1803  1.39.2.6   bouyer 		if (fcp->isp_fwstate != FW_READY ||
   1804  1.39.2.6   bouyer 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
   1805  1.39.2.6   bouyer 			return (-1);
   1806  1.39.2.6   bouyer 		}
   1807      1.37   mjacob 
   1808  1.39.2.6   bouyer 		if (pdb.pdb_loopid != lp->loopid) {
   1809  1.39.2.6   bouyer 			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
   1810  1.39.2.6   bouyer 			    lp->portid, pdb.pdb_loopid);
   1811  1.39.2.6   bouyer 			goto dump_em;
   1812  1.39.2.6   bouyer 		}
   1813  1.39.2.6   bouyer 
   1814  1.39.2.6   bouyer 		if (lp->portid != (u_int32_t) BITS2WORD(pdb.pdb_portid_bits)) {
   1815  1.39.2.6   bouyer 			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
   1816  1.39.2.6   bouyer 			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
   1817  1.39.2.6   bouyer 			goto dump_em;
   1818  1.39.2.6   bouyer 		}
   1819  1.39.2.6   bouyer 
   1820  1.39.2.6   bouyer 		lp->roles =
   1821  1.39.2.6   bouyer 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   1822  1.39.2.6   bouyer 		lp->node_wwn =
   1823  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
   1824  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
   1825  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
   1826  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
   1827  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
   1828  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
   1829  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
   1830  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[7]));
   1831  1.39.2.6   bouyer 		lp->port_wwn =
   1832  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
   1833  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
   1834  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
   1835  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
   1836  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
   1837  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
   1838  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
   1839  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[7]));
   1840      1.37   mjacob 		/*
   1841  1.39.2.6   bouyer 		 * Check to make sure this all makes sense.
   1842      1.37   mjacob 		 */
   1843  1.39.2.6   bouyer 		if (lp->node_wwn && lp->port_wwn) {
   1844  1.39.2.6   bouyer 			lp->valid = 1;
   1845  1.39.2.6   bouyer 			loopid = lp - fcp->portdb;
   1846  1.39.2.6   bouyer 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
   1847  1.39.2.6   bouyer 			continue;
   1848  1.39.2.6   bouyer 		}
   1849  1.39.2.6   bouyer dump_em:
   1850  1.39.2.6   bouyer 		lp->valid = 0;
   1851  1.39.2.6   bouyer 		isp_prt(isp, ISP_LOGINFO,
   1852  1.39.2.6   bouyer 		    ldumped, loopid, lp->loopid, lp->portid);
   1853  1.39.2.6   bouyer 		mbs.param[0] = MBOX_FABRIC_LOGOUT;
   1854  1.39.2.6   bouyer 		mbs.param[1] = lp->loopid << 8;
   1855  1.39.2.6   bouyer 		mbs.param[2] = 0;
   1856  1.39.2.6   bouyer 		mbs.param[3] = 0;
   1857  1.39.2.6   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
   1858  1.39.2.6   bouyer 		if (fcp->isp_fwstate != FW_READY ||
   1859  1.39.2.6   bouyer 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
   1860  1.39.2.6   bouyer 			return (-1);
   1861  1.39.2.6   bouyer 		}
   1862      1.37   mjacob 	}
   1863      1.37   mjacob 	/*
   1864  1.39.2.6   bouyer 	 * If we get here, we've for sure seen not only a valid loop
   1865  1.39.2.6   bouyer 	 * but know what is or isn't on it, so mark this for usage
   1866  1.39.2.6   bouyer 	 * in isp_start.
   1867      1.37   mjacob 	 */
   1868  1.39.2.6   bouyer 	fcp->loop_seen_once = 1;
   1869  1.39.2.6   bouyer 	fcp->isp_loopstate = LOOP_READY;
   1870  1.39.2.6   bouyer 	return (0);
   1871  1.39.2.6   bouyer }
   1872      1.37   mjacob 
   1873  1.39.2.6   bouyer static int
   1874  1.39.2.7   bouyer isp_scan_loop(struct ispsoftc *isp)
   1875  1.39.2.6   bouyer {
   1876  1.39.2.6   bouyer 	struct lportdb *lp;
   1877  1.39.2.6   bouyer 	fcparam *fcp = isp->isp_param;
   1878  1.39.2.6   bouyer 	isp_pdb_t pdb;
   1879  1.39.2.6   bouyer 	int loopid, lim, hival;
   1880  1.39.2.6   bouyer 
   1881  1.39.2.6   bouyer 	switch (fcp->isp_topo) {
   1882  1.39.2.6   bouyer 	case TOPO_NL_PORT:
   1883  1.39.2.6   bouyer 		hival = FL_PORT_ID;
   1884  1.39.2.6   bouyer 		break;
   1885  1.39.2.6   bouyer 	case TOPO_N_PORT:
   1886  1.39.2.6   bouyer 		hival = 2;
   1887  1.39.2.6   bouyer 		break;
   1888  1.39.2.6   bouyer 	case TOPO_FL_PORT:
   1889  1.39.2.6   bouyer 		hival = FC_PORT_ID;
   1890  1.39.2.6   bouyer 		break;
   1891  1.39.2.6   bouyer 	default:
   1892  1.39.2.6   bouyer 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
   1893  1.39.2.6   bouyer 		return (0);
   1894      1.37   mjacob 	}
   1895  1.39.2.6   bouyer 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
   1896      1.37   mjacob 
   1897      1.37   mjacob 	/*
   1898  1.39.2.6   bouyer 	 * make sure the temp port database is clean...
   1899      1.37   mjacob 	 */
   1900  1.39.2.6   bouyer 	MEMZERO((void *)fcp->tport, sizeof (fcp->tport));
   1901      1.37   mjacob 
   1902  1.39.2.6   bouyer 	/*
   1903  1.39.2.6   bouyer 	 * Run through the local loop ports and get port database info
   1904  1.39.2.6   bouyer 	 * for each loop ID.
   1905  1.39.2.6   bouyer 	 *
   1906  1.39.2.6   bouyer 	 * There's a somewhat unexplained situation where the f/w passes back
   1907  1.39.2.6   bouyer 	 * the wrong database entity- if that happens, just restart (up to
   1908  1.39.2.6   bouyer 	 * FL_PORT_ID times).
   1909  1.39.2.6   bouyer 	 */
   1910  1.39.2.6   bouyer 	for (lim = loopid = 0; loopid < hival; loopid++) {
   1911  1.39.2.6   bouyer 		lp = &fcp->tport[loopid];
   1912  1.39.2.6   bouyer 
   1913  1.39.2.6   bouyer 		/*
   1914  1.39.2.6   bouyer 		 * Don't even try for ourselves...
   1915  1.39.2.6   bouyer 	 	 */
   1916  1.39.2.6   bouyer 		if (loopid == fcp->isp_loopid)
   1917  1.39.2.6   bouyer 			continue;
   1918  1.39.2.6   bouyer 
   1919  1.39.2.6   bouyer 		lp->node_wwn = isp_get_portname(isp, loopid, 1);
   1920  1.39.2.6   bouyer 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
   1921  1.39.2.6   bouyer 			return (-1);
   1922  1.39.2.6   bouyer 		if (lp->node_wwn == 0)
   1923  1.39.2.6   bouyer 			continue;
   1924  1.39.2.6   bouyer 		lp->port_wwn = isp_get_portname(isp, loopid, 0);
   1925  1.39.2.6   bouyer 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
   1926  1.39.2.6   bouyer 			return (-1);
   1927  1.39.2.6   bouyer 		if (lp->port_wwn == 0) {
   1928  1.39.2.6   bouyer 			lp->node_wwn = 0;
   1929  1.39.2.5   bouyer 			continue;
   1930  1.39.2.5   bouyer 		}
   1931  1.39.2.5   bouyer 
   1932      1.37   mjacob 		/*
   1933  1.39.2.6   bouyer 		 * Get an entry....
   1934      1.37   mjacob 		 */
   1935  1.39.2.6   bouyer 		if (isp_getpdb(isp, loopid, &pdb) != 0) {
   1936  1.39.2.6   bouyer 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
   1937  1.39.2.6   bouyer 				return (-1);
   1938      1.37   mjacob 			continue;
   1939  1.39.2.5   bouyer 		}
   1940  1.39.2.6   bouyer 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
   1941  1.39.2.6   bouyer 			return (-1);
   1942  1.39.2.6   bouyer 		}
   1943  1.39.2.2   bouyer 
   1944      1.37   mjacob 		/*
   1945  1.39.2.6   bouyer 		 * If the returned database element doesn't match what we
   1946  1.39.2.6   bouyer 		 * asked for, restart the process entirely (up to a point...).
   1947      1.37   mjacob 		 */
   1948  1.39.2.6   bouyer 		if (pdb.pdb_loopid != loopid) {
   1949  1.39.2.6   bouyer 			loopid = 0;
   1950  1.39.2.6   bouyer 			if (lim++ < hival) {
   1951  1.39.2.6   bouyer 				continue;
   1952  1.39.2.6   bouyer 			}
   1953  1.39.2.6   bouyer 			isp_prt(isp, ISP_LOGWARN,
   1954  1.39.2.6   bouyer 			    "giving up on synchronizing the port database");
   1955  1.39.2.6   bouyer 			return (-1);
   1956  1.39.2.6   bouyer 		}
   1957  1.39.2.6   bouyer 
   1958  1.39.2.6   bouyer 		/*
   1959  1.39.2.6   bouyer 		 * Save the pertinent info locally.
   1960  1.39.2.6   bouyer 		 */
   1961  1.39.2.6   bouyer 		lp->node_wwn =
   1962  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
   1963  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
   1964  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
   1965  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
   1966  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
   1967  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
   1968  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
   1969  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_nodename[7]));
   1970  1.39.2.6   bouyer 		lp->port_wwn =
   1971  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
   1972  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
   1973  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
   1974  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
   1975  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
   1976  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
   1977  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
   1978  1.39.2.6   bouyer 		    (((u_int64_t)pdb.pdb_portname[7]));
   1979  1.39.2.6   bouyer 		lp->roles =
   1980  1.39.2.6   bouyer 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
   1981  1.39.2.6   bouyer 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
   1982  1.39.2.6   bouyer 		lp->loopid = pdb.pdb_loopid;
   1983  1.39.2.6   bouyer 	}
   1984  1.39.2.6   bouyer 
   1985  1.39.2.6   bouyer 	/*
   1986  1.39.2.6   bouyer 	 * Mark all of the permanent local loop database entries as invalid
   1987  1.39.2.6   bouyer 	 * (except our own entry).
   1988  1.39.2.6   bouyer 	 */
   1989  1.39.2.6   bouyer 	for (loopid = 0; loopid < hival; loopid++) {
   1990  1.39.2.6   bouyer 		if (loopid == fcp->isp_iid) {
   1991  1.39.2.6   bouyer 			fcp->portdb[loopid].valid = 1;
   1992  1.39.2.6   bouyer 			fcp->portdb[loopid].loopid = fcp->isp_loopid;
   1993      1.37   mjacob 			continue;
   1994  1.39.2.5   bouyer 		}
   1995  1.39.2.6   bouyer 		fcp->portdb[loopid].valid = 0;
   1996  1.39.2.6   bouyer 	}
   1997      1.37   mjacob 
   1998  1.39.2.6   bouyer 	/*
   1999  1.39.2.6   bouyer 	 * Now merge our local copy of the port database into our saved copy.
   2000  1.39.2.6   bouyer 	 * Notify the outer layers of new devices arriving.
   2001  1.39.2.6   bouyer 	 */
   2002  1.39.2.6   bouyer 	for (loopid = 0; loopid < hival; loopid++) {
   2003  1.39.2.6   bouyer 		int i;
   2004  1.39.2.2   bouyer 
   2005      1.37   mjacob 		/*
   2006  1.39.2.6   bouyer 		 * If we don't have a non-zero Port WWN, we're not here.
   2007      1.37   mjacob 		 */
   2008  1.39.2.6   bouyer 		if (fcp->tport[loopid].port_wwn == 0) {
   2009  1.39.2.6   bouyer 			continue;
   2010  1.39.2.2   bouyer 		}
   2011  1.39.2.2   bouyer 
   2012  1.39.2.2   bouyer 		/*
   2013  1.39.2.6   bouyer 		 * Skip ourselves.
   2014  1.39.2.2   bouyer 		 */
   2015  1.39.2.6   bouyer 		if (loopid == fcp->isp_iid) {
   2016  1.39.2.6   bouyer 			continue;
   2017  1.39.2.2   bouyer 		}
   2018      1.37   mjacob 
   2019      1.37   mjacob 		/*
   2020  1.39.2.6   bouyer 		 * For the purposes of deciding whether this is the
   2021  1.39.2.6   bouyer 		 * 'same' device or not, we only search for an identical
   2022  1.39.2.6   bouyer 		 * Port WWN. Node WWNs may or may not be the same as
   2023  1.39.2.6   bouyer 		 * the Port WWN, and there may be multiple different
   2024  1.39.2.6   bouyer 		 * Port WWNs with the same Node WWN. It would be chaos
   2025  1.39.2.6   bouyer 		 * to have multiple identical Port WWNs, so we don't
   2026  1.39.2.6   bouyer 		 * allow that.
   2027      1.37   mjacob 		 */
   2028  1.39.2.6   bouyer 
   2029  1.39.2.6   bouyer 		for (i = 0; i < hival; i++) {
   2030  1.39.2.6   bouyer 			int j;
   2031  1.39.2.6   bouyer 			if (fcp->portdb[i].port_wwn == 0)
   2032  1.39.2.6   bouyer 				continue;
   2033  1.39.2.6   bouyer 			if (fcp->portdb[i].port_wwn !=
   2034  1.39.2.6   bouyer 			    fcp->tport[loopid].port_wwn)
   2035  1.39.2.6   bouyer 				continue;
   2036  1.39.2.6   bouyer 			/*
   2037  1.39.2.6   bouyer 			 * We found this WWN elsewhere- it's changed
   2038  1.39.2.6   bouyer 			 * loopids then. We don't change it's actual
   2039  1.39.2.6   bouyer 			 * position in our cached port database- we
   2040  1.39.2.6   bouyer 			 * just change the actual loop ID we'd use.
   2041  1.39.2.6   bouyer 			 */
   2042  1.39.2.6   bouyer 			if (fcp->portdb[i].loopid != loopid) {
   2043  1.39.2.6   bouyer 				isp_prt(isp, ISP_LOGINFO, portshift, i,
   2044  1.39.2.6   bouyer 				    fcp->portdb[i].loopid,
   2045  1.39.2.6   bouyer 				    fcp->portdb[i].portid, loopid,
   2046  1.39.2.6   bouyer 				    fcp->tport[loopid].portid);
   2047  1.39.2.2   bouyer 			}
   2048  1.39.2.6   bouyer 			fcp->portdb[i].portid = fcp->tport[loopid].portid;
   2049  1.39.2.6   bouyer 			fcp->portdb[i].loopid = loopid;
   2050  1.39.2.6   bouyer 			fcp->portdb[i].valid = 1;
   2051  1.39.2.6   bouyer 			fcp->portdb[i].roles = fcp->tport[loopid].roles;
   2052  1.39.2.6   bouyer 
   2053  1.39.2.6   bouyer 			/*
   2054  1.39.2.6   bouyer 			 * Now make sure this Port WWN doesn't exist elsewhere
   2055  1.39.2.6   bouyer 			 * in the port database.
   2056  1.39.2.6   bouyer 			 */
   2057  1.39.2.6   bouyer 			for (j = i+1; j < hival; j++) {
   2058  1.39.2.6   bouyer 				if (fcp->portdb[i].port_wwn !=
   2059  1.39.2.6   bouyer 				    fcp->portdb[j].port_wwn) {
   2060  1.39.2.6   bouyer 					continue;
   2061  1.39.2.6   bouyer 				}
   2062  1.39.2.6   bouyer 				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
   2063  1.39.2.2   bouyer 				/*
   2064  1.39.2.6   bouyer 				 * Invalidate the 'old' *and* 'new' ones.
   2065  1.39.2.6   bouyer 				 * This is really harsh and not quite right,
   2066  1.39.2.6   bouyer 				 * but if this happens, we really don't know
   2067  1.39.2.6   bouyer 				 * who is what at this point.
   2068  1.39.2.2   bouyer 				 */
   2069  1.39.2.6   bouyer 				fcp->portdb[i].valid = 0;
   2070  1.39.2.6   bouyer 				fcp->portdb[j].valid = 0;
   2071      1.38   mjacob 			}
   2072  1.39.2.6   bouyer 			break;
   2073  1.39.2.6   bouyer 		}
   2074  1.39.2.2   bouyer 
   2075  1.39.2.2   bouyer 		/*
   2076  1.39.2.6   bouyer 		 * If we didn't traverse the entire port database,
   2077  1.39.2.6   bouyer 		 * then we found (and remapped) an existing entry.
   2078  1.39.2.6   bouyer 		 * No need to notify anyone- go for the next one.
   2079  1.39.2.2   bouyer 		 */
   2080  1.39.2.6   bouyer 		if (i < hival) {
   2081  1.39.2.2   bouyer 			continue;
   2082  1.39.2.2   bouyer 		}
   2083  1.39.2.2   bouyer 
   2084  1.39.2.2   bouyer 		/*
   2085  1.39.2.6   bouyer 		 * We've not found this Port WWN anywhere. It's a new entry.
   2086  1.39.2.6   bouyer 		 * See if we can leave it where it is (with target == loopid).
   2087  1.39.2.2   bouyer 		 */
   2088  1.39.2.6   bouyer 		if (fcp->portdb[loopid].port_wwn != 0) {
   2089  1.39.2.6   bouyer 			for (lim = 0; lim < hival; lim++) {
   2090  1.39.2.6   bouyer 				if (fcp->portdb[lim].port_wwn == 0)
   2091  1.39.2.6   bouyer 					break;
   2092  1.39.2.6   bouyer 			}
   2093  1.39.2.6   bouyer 			/* "Cannot Happen" */
   2094  1.39.2.6   bouyer 			if (lim == hival) {
   2095  1.39.2.6   bouyer 				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
   2096  1.39.2.6   bouyer 				continue;
   2097  1.39.2.6   bouyer 			}
   2098  1.39.2.6   bouyer 			i = lim;
   2099  1.39.2.6   bouyer 		} else {
   2100  1.39.2.6   bouyer 			i = loopid;
   2101  1.39.2.2   bouyer 		}
   2102  1.39.2.2   bouyer 
   2103  1.39.2.6   bouyer 		/*
   2104  1.39.2.6   bouyer 		 * NB:	The actual loopid we use here is loopid- we may
   2105  1.39.2.6   bouyer 		 *	in fact be at a completely different index (target).
   2106  1.39.2.6   bouyer 		 */
   2107  1.39.2.6   bouyer 		fcp->portdb[i].loopid = loopid;
   2108  1.39.2.6   bouyer 		fcp->portdb[i].port_wwn = fcp->tport[loopid].port_wwn;
   2109  1.39.2.6   bouyer 		fcp->portdb[i].node_wwn = fcp->tport[loopid].node_wwn;
   2110  1.39.2.6   bouyer 		fcp->portdb[i].roles = fcp->tport[loopid].roles;
   2111  1.39.2.6   bouyer 		fcp->portdb[i].portid = fcp->tport[loopid].portid;
   2112  1.39.2.6   bouyer 		fcp->portdb[i].valid = 1;
   2113  1.39.2.2   bouyer 
   2114  1.39.2.2   bouyer 		/*
   2115  1.39.2.6   bouyer 		 * Tell the outside world we've arrived.
   2116  1.39.2.2   bouyer 		 */
   2117  1.39.2.6   bouyer 		(void) isp_async(isp, ISPASYNC_PROMENADE, &i);
   2118      1.37   mjacob 	}
   2119  1.39.2.6   bouyer 
   2120  1.39.2.2   bouyer 	/*
   2121  1.39.2.6   bouyer 	 * Now find all previously used targets that are now invalid and
   2122  1.39.2.6   bouyer 	 * notify the outer layers that they're gone.
   2123  1.39.2.2   bouyer 	 */
   2124  1.39.2.6   bouyer 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[hival]; lp++) {
   2125  1.39.2.6   bouyer 		if (lp->valid || lp->port_wwn == 0) {
   2126  1.39.2.6   bouyer 			continue;
   2127  1.39.2.6   bouyer 		}
   2128  1.39.2.6   bouyer 
   2129  1.39.2.6   bouyer 		/*
   2130  1.39.2.6   bouyer 		 * Tell the outside world we've gone
   2131  1.39.2.6   bouyer 		 * away and erase our pdb entry.
   2132  1.39.2.6   bouyer 		 *
   2133  1.39.2.6   bouyer 		 */
   2134  1.39.2.6   bouyer 		loopid = lp - fcp->portdb;
   2135  1.39.2.6   bouyer 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
   2136  1.39.2.6   bouyer 		MEMZERO((void *) lp, sizeof (*lp));
   2137  1.39.2.6   bouyer 	}
   2138  1.39.2.6   bouyer 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
   2139      1.37   mjacob 	return (0);
   2140      1.37   mjacob }
   2141      1.37   mjacob 
   2142      1.37   mjacob static int
   2143  1.39.2.7   bouyer isp_scan_fabric(struct ispsoftc *isp)
   2144      1.37   mjacob {
   2145      1.37   mjacob 	fcparam *fcp = isp->isp_param;
   2146  1.39.2.6   bouyer 	u_int32_t portid, first_portid;
   2147      1.37   mjacob 	sns_screq_t *reqp;
   2148      1.37   mjacob 	sns_scrsp_t *resp;
   2149      1.37   mjacob 	mbreg_t mbs;
   2150  1.39.2.6   bouyer 	int hicap, first_portid_seen;
   2151  1.39.2.6   bouyer 
   2152  1.39.2.6   bouyer 	if (fcp->isp_onfabric == 0) {
   2153  1.39.2.6   bouyer 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2154  1.39.2.6   bouyer 		return (0);
   2155  1.39.2.6   bouyer 	}
   2156      1.37   mjacob 
   2157      1.37   mjacob 	reqp = (sns_screq_t *) fcp->isp_scratch;
   2158      1.37   mjacob 	resp = (sns_scrsp_t *) (&((char *)fcp->isp_scratch)[0x100]);
   2159  1.39.2.6   bouyer 	/*
   2160  1.39.2.6   bouyer 	 * Since Port IDs are 24 bits, we can check against having seen
   2161  1.39.2.6   bouyer 	 * anything yet with this value.
   2162  1.39.2.6   bouyer 	 */
   2163  1.39.2.6   bouyer 	first_portid = portid = fcp->isp_portid;
   2164  1.39.2.6   bouyer 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
   2165      1.37   mjacob 
   2166  1.39.2.6   bouyer 	for (first_portid_seen = hicap = 0; hicap < 65535; hicap++) {
   2167      1.37   mjacob 		MEMZERO((void *) reqp, SNS_GAN_REQ_SIZE);
   2168      1.37   mjacob 		reqp->snscb_rblen = SNS_GAN_RESP_SIZE >> 1;
   2169      1.37   mjacob 		reqp->snscb_addr[RQRSP_ADDR0015] =
   2170      1.37   mjacob 			DMA_LSW(fcp->isp_scdma + 0x100);
   2171      1.37   mjacob 		reqp->snscb_addr[RQRSP_ADDR1631] =
   2172      1.37   mjacob 			DMA_MSW(fcp->isp_scdma + 0x100);
   2173      1.37   mjacob 		reqp->snscb_sblen = 6;
   2174      1.37   mjacob 		reqp->snscb_data[0] = SNS_GAN;
   2175      1.37   mjacob 		reqp->snscb_data[4] = portid & 0xffff;
   2176      1.37   mjacob 		reqp->snscb_data[5] = (portid >> 16) & 0xff;
   2177      1.38   mjacob 		ISP_SWIZZLE_SNS_REQ(isp, reqp);
   2178      1.37   mjacob 		mbs.param[0] = MBOX_SEND_SNS;
   2179      1.37   mjacob 		mbs.param[1] = SNS_GAN_REQ_SIZE >> 1;
   2180      1.37   mjacob 		mbs.param[2] = DMA_MSW(fcp->isp_scdma);
   2181      1.37   mjacob 		mbs.param[3] = DMA_LSW(fcp->isp_scdma);
   2182      1.37   mjacob 		mbs.param[6] = 0;
   2183      1.37   mjacob 		mbs.param[7] = 0;
   2184  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   2185      1.37   mjacob 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2186  1.39.2.6   bouyer 			if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC) {
   2187  1.39.2.6   bouyer 				fcp->isp_loopstate = LOOP_PDB_RCVD;
   2188  1.39.2.6   bouyer 			}
   2189  1.39.2.6   bouyer 			return (-1);
   2190  1.39.2.6   bouyer 		}
   2191  1.39.2.6   bouyer 		if (fcp->isp_fwstate != FW_READY ||
   2192  1.39.2.6   bouyer 		    fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
   2193      1.37   mjacob 			return (-1);
   2194      1.37   mjacob 		}
   2195      1.38   mjacob 		ISP_UNSWIZZLE_SNS_RSP(isp, resp, SNS_GAN_RESP_SIZE >> 1);
   2196      1.37   mjacob 		portid = (((u_int32_t) resp->snscb_port_id[0]) << 16) |
   2197      1.37   mjacob 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
   2198      1.37   mjacob 		    (((u_int32_t) resp->snscb_port_id[2]));
   2199  1.39.2.6   bouyer 		(void) isp_async(isp, ISPASYNC_FABRIC_DEV, resp);
   2200  1.39.2.6   bouyer 		if (first_portid == portid) {
   2201  1.39.2.6   bouyer 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2202      1.37   mjacob 			return (0);
   2203      1.37   mjacob 		}
   2204      1.37   mjacob 	}
   2205  1.39.2.6   bouyer 
   2206  1.39.2.6   bouyer 	isp_prt(isp, ISP_LOGWARN, "broken fabric nameserver...*wheeze*...");
   2207  1.39.2.6   bouyer 
   2208      1.37   mjacob 	/*
   2209      1.37   mjacob 	 * We either have a broken name server or a huge fabric if we get here.
   2210      1.37   mjacob 	 */
   2211  1.39.2.6   bouyer 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
   2212      1.37   mjacob 	return (0);
   2213      1.37   mjacob }
   2214  1.39.2.5   bouyer 
   2215  1.39.2.5   bouyer static void
   2216  1.39.2.5   bouyer isp_register_fc4_type(struct ispsoftc *isp)
   2217  1.39.2.5   bouyer {
   2218  1.39.2.5   bouyer 	fcparam *fcp = isp->isp_param;
   2219  1.39.2.5   bouyer 	sns_screq_t *reqp;
   2220  1.39.2.5   bouyer 	mbreg_t mbs;
   2221  1.39.2.5   bouyer 
   2222  1.39.2.5   bouyer 	reqp = (sns_screq_t *) fcp->isp_scratch;
   2223  1.39.2.5   bouyer 	MEMZERO((void *) reqp, SNS_RFT_REQ_SIZE);
   2224  1.39.2.5   bouyer 	reqp->snscb_rblen = SNS_RFT_RESP_SIZE >> 1;
   2225  1.39.2.5   bouyer 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_LSW(fcp->isp_scdma + 0x100);
   2226  1.39.2.5   bouyer 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_MSW(fcp->isp_scdma + 0x100);
   2227  1.39.2.5   bouyer 	reqp->snscb_sblen = 22;
   2228  1.39.2.5   bouyer 	reqp->snscb_data[0] = SNS_RFT;
   2229  1.39.2.5   bouyer 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
   2230  1.39.2.5   bouyer 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
   2231  1.39.2.5   bouyer 	reqp->snscb_data[6] = 0x100;	/* SCS - FCP */
   2232  1.39.2.5   bouyer #if	0
   2233  1.39.2.5   bouyer 	reqp->snscb_data[6] |= 20;	/* ISO/IEC 8802-2 LLC/SNAP */
   2234  1.39.2.5   bouyer #endif
   2235  1.39.2.5   bouyer 	ISP_SWIZZLE_SNS_REQ(isp, reqp);
   2236  1.39.2.5   bouyer 	mbs.param[0] = MBOX_SEND_SNS;
   2237  1.39.2.5   bouyer 	mbs.param[1] = SNS_RFT_REQ_SIZE >> 1;
   2238  1.39.2.5   bouyer 	mbs.param[2] = DMA_MSW(fcp->isp_scdma);
   2239  1.39.2.5   bouyer 	mbs.param[3] = DMA_LSW(fcp->isp_scdma);
   2240  1.39.2.5   bouyer 	mbs.param[6] = 0;
   2241  1.39.2.5   bouyer 	mbs.param[7] = 0;
   2242  1.39.2.5   bouyer 	isp_mboxcmd(isp, &mbs, MBLOGALL);
   2243  1.39.2.5   bouyer 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   2244  1.39.2.6   bouyer 		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
   2245  1.39.2.5   bouyer 	}
   2246  1.39.2.5   bouyer }
   2247  1.39.2.6   bouyer 
   2248      1.37   mjacob /*
   2249      1.23   mjacob  * Start a command. Locking is assumed done in the caller.
   2250       1.1      cgd  */
   2251      1.23   mjacob 
   2252  1.39.2.2   bouyer int
   2253  1.39.2.7   bouyer isp_start(XS_T *xs)
   2254       1.1      cgd {
   2255       1.1      cgd 	struct ispsoftc *isp;
   2256  1.39.2.7   bouyer 	u_int16_t iptr, optr, handle;
   2257      1.10   mjacob 	union {
   2258      1.10   mjacob 		ispreq_t *_reqp;
   2259      1.10   mjacob 		ispreqt2_t *_t2reqp;
   2260      1.10   mjacob 	} _u;
   2261      1.10   mjacob #define	reqp	_u._reqp
   2262      1.10   mjacob #define	t2reqp	_u._t2reqp
   2263      1.16   mjacob #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
   2264      1.38   mjacob 	int target, i;
   2265       1.1      cgd 
   2266      1.23   mjacob 	XS_INITERR(xs);
   2267      1.23   mjacob 	isp = XS_ISP(xs);
   2268       1.1      cgd 
   2269  1.39.2.6   bouyer 
   2270  1.39.2.6   bouyer 	/*
   2271  1.39.2.6   bouyer 	 * Check to make sure we're supporting initiator role.
   2272  1.39.2.6   bouyer 	 */
   2273  1.39.2.6   bouyer 	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
   2274  1.39.2.6   bouyer 		XS_SETERR(xs, HBA_SELTIMEOUT);
   2275  1.39.2.6   bouyer 		return (CMD_COMPLETE);
   2276  1.39.2.6   bouyer 	}
   2277  1.39.2.6   bouyer 
   2278  1.39.2.6   bouyer 	/*
   2279  1.39.2.6   bouyer 	 * Now make sure we're running.
   2280  1.39.2.6   bouyer 	 */
   2281  1.39.2.6   bouyer 
   2282      1.24   mjacob 	if (isp->isp_state != ISP_RUNSTATE) {
   2283  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
   2284      1.24   mjacob 		XS_SETERR(xs, HBA_BOTCH);
   2285      1.24   mjacob 		return (CMD_COMPLETE);
   2286      1.24   mjacob 	}
   2287      1.24   mjacob 
   2288      1.25   mjacob 	/*
   2289  1.39.2.2   bouyer 	 * Check command CDB length, etc.. We really are limited to 16 bytes
   2290  1.39.2.2   bouyer 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
   2291  1.39.2.2   bouyer 	 * but probably only if we're running fairly new firmware (we'll
   2292  1.39.2.2   bouyer 	 * let the old f/w choke on an extended command queue entry).
   2293      1.25   mjacob 	 */
   2294      1.33   mjacob 
   2295  1.39.2.2   bouyer 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
   2296  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   2297  1.39.2.2   bouyer 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
   2298  1.39.2.2   bouyer 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
   2299      1.25   mjacob 		XS_SETERR(xs, HBA_BOTCH);
   2300      1.25   mjacob 		return (CMD_COMPLETE);
   2301      1.25   mjacob 	}
   2302      1.25   mjacob 
   2303      1.25   mjacob 	/*
   2304      1.33   mjacob 	 * Check to see whether we have good firmware state still or
   2305      1.33   mjacob 	 * need to refresh our port database for this target.
   2306      1.33   mjacob 	 */
   2307      1.37   mjacob 	target = XS_TGT(xs);
   2308      1.33   mjacob 	if (IS_FC(isp)) {
   2309      1.33   mjacob 		fcparam *fcp = isp->isp_param;
   2310      1.37   mjacob 		struct lportdb *lp;
   2311  1.39.2.7   bouyer #ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
   2312  1.39.2.7   bouyer 		if (fcp->isp_fwstate != FW_READY ||
   2313  1.39.2.7   bouyer 		    fcp->isp_loopstate != LOOP_READY) {
   2314  1.39.2.7   bouyer 			return (CMD_RQLATER);
   2315  1.39.2.7   bouyer 		}
   2316  1.39.2.7   bouyer 
   2317  1.39.2.7   bouyer 		/*
   2318  1.39.2.7   bouyer 		 * If we're not on a Fabric, we can't have a target
   2319  1.39.2.7   bouyer 		 * above FL_PORT_ID-1.
   2320  1.39.2.7   bouyer 		 *
   2321  1.39.2.7   bouyer 		 * If we're on a fabric and *not* connected as an F-port,
   2322  1.39.2.7   bouyer 		 * we can't have a target less than FC_SNS_ID+1. This
   2323  1.39.2.7   bouyer 		 * keeps us from having to sort out the difference between
   2324  1.39.2.7   bouyer 		 * local public loop devices and those which we might get
   2325  1.39.2.7   bouyer 		 * from a switch's database.
   2326  1.39.2.7   bouyer 		 */
   2327  1.39.2.7   bouyer 		if (fcp->isp_onfabric == 0) {
   2328  1.39.2.7   bouyer 			if (target >= FL_PORT_ID) {
   2329  1.39.2.7   bouyer 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2330  1.39.2.7   bouyer 				return (CMD_COMPLETE);
   2331  1.39.2.7   bouyer 			}
   2332  1.39.2.7   bouyer 		} else {
   2333  1.39.2.7   bouyer 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
   2334  1.39.2.7   bouyer 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2335  1.39.2.7   bouyer 				return (CMD_COMPLETE);
   2336  1.39.2.7   bouyer 			}
   2337  1.39.2.7   bouyer 			if (fcp->isp_topo != TOPO_F_PORT &&
   2338  1.39.2.7   bouyer 			    target < FL_PORT_ID) {
   2339  1.39.2.7   bouyer 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2340  1.39.2.7   bouyer 				return (CMD_COMPLETE);
   2341  1.39.2.7   bouyer 			}
   2342  1.39.2.7   bouyer 		}
   2343  1.39.2.7   bouyer #else
   2344  1.39.2.6   bouyer 		/*
   2345  1.39.2.6   bouyer 		 * Check for f/w being in ready state. If the f/w
   2346  1.39.2.6   bouyer 		 * isn't in ready state, then we don't know our
   2347  1.39.2.6   bouyer 		 * loop ID and the f/w hasn't completed logging
   2348  1.39.2.6   bouyer 		 * into all targets on the loop. If this is the
   2349  1.39.2.6   bouyer 		 * case, then bounce the command. We pretend this is
   2350  1.39.2.6   bouyer 		 * a SELECTION TIMEOUT error if we've never gone to
   2351  1.39.2.6   bouyer 		 * FW_READY state at all- in this case we may not
   2352  1.39.2.6   bouyer 		 * be hooked to a loop at all and we shouldn't hang
   2353  1.39.2.6   bouyer 		 * the machine for this. Otherwise, defer this command
   2354  1.39.2.6   bouyer 		 * until later.
   2355  1.39.2.6   bouyer 		 */
   2356  1.39.2.6   bouyer 		if (fcp->isp_fwstate != FW_READY) {
   2357  1.39.2.6   bouyer 			/*
   2358  1.39.2.6   bouyer 			 * Give ourselves at most a 250ms delay.
   2359  1.39.2.6   bouyer 			 */
   2360  1.39.2.6   bouyer 			if (isp_fclink_test(isp, 250000)) {
   2361  1.39.2.6   bouyer 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2362  1.39.2.6   bouyer 				if (fcp->loop_seen_once) {
   2363  1.39.2.6   bouyer 					return (CMD_RQLATER);
   2364  1.39.2.6   bouyer 				} else {
   2365  1.39.2.6   bouyer 					return (CMD_COMPLETE);
   2366  1.39.2.6   bouyer 				}
   2367  1.39.2.6   bouyer 			}
   2368  1.39.2.6   bouyer 		}
   2369  1.39.2.6   bouyer 
   2370  1.39.2.2   bouyer 		/*
   2371  1.39.2.2   bouyer 		 * If we're not on a Fabric, we can't have a target
   2372  1.39.2.5   bouyer 		 * above FL_PORT_ID-1.
   2373  1.39.2.5   bouyer 		 *
   2374  1.39.2.5   bouyer 		 * If we're on a fabric and *not* connected as an F-port,
   2375  1.39.2.5   bouyer 		 * we can't have a target less than FC_SNS_ID+1. This
   2376  1.39.2.5   bouyer 		 * keeps us from having to sort out the difference between
   2377  1.39.2.5   bouyer 		 * local public loop devices and those which we might get
   2378  1.39.2.5   bouyer 		 * from a switch's database.
   2379  1.39.2.2   bouyer 		 */
   2380  1.39.2.2   bouyer 		if (fcp->isp_onfabric == 0) {
   2381  1.39.2.2   bouyer 			if (target >= FL_PORT_ID) {
   2382  1.39.2.2   bouyer 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2383  1.39.2.2   bouyer 				return (CMD_COMPLETE);
   2384  1.39.2.2   bouyer 			}
   2385  1.39.2.2   bouyer 		} else {
   2386  1.39.2.2   bouyer 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
   2387  1.39.2.2   bouyer 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2388  1.39.2.2   bouyer 				return (CMD_COMPLETE);
   2389  1.39.2.2   bouyer 			}
   2390  1.39.2.5   bouyer 			if (fcp->isp_topo != TOPO_F_PORT &&
   2391  1.39.2.2   bouyer 			    target < FL_PORT_ID) {
   2392      1.37   mjacob 				XS_SETERR(xs, HBA_SELTIMEOUT);
   2393      1.37   mjacob 				return (CMD_COMPLETE);
   2394      1.37   mjacob 			}
   2395      1.37   mjacob 		}
   2396      1.37   mjacob 
   2397      1.37   mjacob 		/*
   2398      1.37   mjacob 		 * If our loop state is such that we haven't yet received
   2399      1.37   mjacob 		 * a "Port Database Changed" notification (after a LIP or
   2400      1.37   mjacob 		 * a Loop Reset or firmware initialization), then defer
   2401  1.39.2.2   bouyer 		 * sending commands for a little while, but only if we've
   2402  1.39.2.2   bouyer 		 * seen a valid loop at one point (otherwise we can get
   2403  1.39.2.2   bouyer 		 * stuck at initialization time).
   2404      1.37   mjacob 		 */
   2405      1.37   mjacob 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
   2406      1.37   mjacob 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2407  1.39.2.2   bouyer 			if (fcp->loop_seen_once) {
   2408  1.39.2.2   bouyer 				return (CMD_RQLATER);
   2409  1.39.2.2   bouyer 			} else {
   2410  1.39.2.2   bouyer 				return (CMD_COMPLETE);
   2411  1.39.2.2   bouyer 			}
   2412      1.37   mjacob 		}
   2413      1.37   mjacob 
   2414      1.37   mjacob 		/*
   2415  1.39.2.6   bouyer 		 * If we're in the middle of loop or fabric scanning
   2416  1.39.2.6   bouyer 		 * or merging the port databases, retry this command later.
   2417  1.39.2.6   bouyer 		 */
   2418  1.39.2.6   bouyer 		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
   2419  1.39.2.6   bouyer 		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
   2420  1.39.2.6   bouyer 		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
   2421  1.39.2.6   bouyer 			return (CMD_RQLATER);
   2422  1.39.2.6   bouyer 		}
   2423  1.39.2.6   bouyer 
   2424  1.39.2.6   bouyer 		/*
   2425  1.39.2.6   bouyer 		 * If our loop state is now such that we've just now
   2426  1.39.2.6   bouyer 		 * received a Port Database Change notification, then
   2427  1.39.2.6   bouyer 		 * we have to go off and (re)scan the fabric. We back
   2428  1.39.2.6   bouyer 		 * out and try again later if this doesn't work.
   2429  1.39.2.6   bouyer 		 */
   2430  1.39.2.6   bouyer 		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
   2431  1.39.2.6   bouyer 			if (isp_scan_fabric(isp)) {
   2432  1.39.2.6   bouyer 				return (CMD_RQLATER);
   2433  1.39.2.6   bouyer 			}
   2434  1.39.2.6   bouyer 			if (fcp->isp_fwstate != FW_READY ||
   2435  1.39.2.6   bouyer 			    fcp->isp_loopstate < LOOP_PDB_RCVD) {
   2436  1.39.2.6   bouyer 				return (CMD_RQLATER);
   2437  1.39.2.6   bouyer 			}
   2438  1.39.2.6   bouyer 		}
   2439  1.39.2.6   bouyer 
   2440  1.39.2.6   bouyer 		/*
   2441      1.37   mjacob 		 * If our loop state is now such that we've just now
   2442      1.37   mjacob 		 * received a Port Database Change notification, then
   2443  1.39.2.2   bouyer 		 * we have to go off and (re)synchronize our port
   2444  1.39.2.2   bouyer 		 * database.
   2445      1.37   mjacob 		 */
   2446  1.39.2.6   bouyer 		if (fcp->isp_loopstate < LOOP_READY) {
   2447  1.39.2.6   bouyer 			if (isp_pdb_sync(isp)) {
   2448  1.39.2.6   bouyer 				return (CMD_RQLATER);
   2449  1.39.2.6   bouyer 			}
   2450  1.39.2.6   bouyer 			if (fcp->isp_fwstate != FW_READY ||
   2451  1.39.2.6   bouyer 			    fcp->isp_loopstate != LOOP_READY) {
   2452  1.39.2.6   bouyer 				return (CMD_RQLATER);
   2453      1.33   mjacob 			}
   2454      1.33   mjacob 		}
   2455      1.37   mjacob 
   2456      1.33   mjacob 		/*
   2457  1.39.2.2   bouyer 		 * XXX: Here's were we would cancel any loop_dead flag
   2458  1.39.2.2   bouyer 		 * XXX: also cancel in dead_loop timeout that's running
   2459  1.39.2.2   bouyer 		 */
   2460  1.39.2.7   bouyer #endif
   2461  1.39.2.2   bouyer 
   2462  1.39.2.2   bouyer 		/*
   2463      1.37   mjacob 		 * Now check whether we should even think about pursuing this.
   2464      1.33   mjacob 		 */
   2465      1.37   mjacob 		lp = &fcp->portdb[target];
   2466      1.37   mjacob 		if (lp->valid == 0) {
   2467      1.37   mjacob 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2468      1.37   mjacob 			return (CMD_COMPLETE);
   2469      1.37   mjacob 		}
   2470      1.37   mjacob 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
   2471  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGDEBUG2,
   2472  1.39.2.2   bouyer 			    "Target %d does not have target service", target);
   2473      1.37   mjacob 			XS_SETERR(xs, HBA_SELTIMEOUT);
   2474      1.37   mjacob 			return (CMD_COMPLETE);
   2475      1.33   mjacob 		}
   2476      1.37   mjacob 		/*
   2477  1.39.2.6   bouyer 		 * Now turn target into what the actual Loop ID is.
   2478      1.37   mjacob 		 */
   2479      1.37   mjacob 		target = lp->loopid;
   2480      1.33   mjacob 	}
   2481      1.33   mjacob 
   2482      1.33   mjacob 	/*
   2483      1.33   mjacob 	 * Next check to see if any HBA or Device
   2484      1.25   mjacob 	 * parameters need to be updated.
   2485      1.25   mjacob 	 */
   2486      1.36   mjacob 	if (isp->isp_update != 0) {
   2487      1.25   mjacob 		isp_update(isp);
   2488      1.10   mjacob 	}
   2489      1.25   mjacob 
   2490  1.39.2.2   bouyer 	if (isp_getrqentry(isp, &iptr, &optr, (void **) &reqp)) {
   2491  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
   2492      1.23   mjacob 		XS_SETERR(xs, HBA_BOTCH);
   2493      1.23   mjacob 		return (CMD_EAGAIN);
   2494       1.1      cgd 	}
   2495      1.13   mjacob 
   2496      1.36   mjacob 	/*
   2497      1.36   mjacob 	 * Now see if we need to synchronize the ISP with respect to anything.
   2498      1.36   mjacob 	 * We do dual duty here (cough) for synchronizing for busses other
   2499      1.36   mjacob 	 * than which we got here to send a command to.
   2500      1.36   mjacob 	 */
   2501       1.1      cgd 	if (isp->isp_sendmarker) {
   2502  1.39.2.2   bouyer 		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
   2503      1.27   mjacob 		/*
   2504      1.36   mjacob 		 * Check ports to send markers for...
   2505      1.27   mjacob 		 */
   2506      1.36   mjacob 		for (i = 0; i < n; i++) {
   2507      1.36   mjacob 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
   2508      1.36   mjacob 				continue;
   2509      1.36   mjacob 			}
   2510      1.36   mjacob 			MEMZERO((void *) reqp, sizeof (*reqp));
   2511      1.36   mjacob 			reqp->req_header.rqs_entry_count = 1;
   2512      1.36   mjacob 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
   2513      1.36   mjacob 			reqp->req_modifier = SYNC_ALL;
   2514      1.38   mjacob 			reqp->req_target = i << 7;	/* insert bus number */
   2515      1.38   mjacob 			ISP_SWIZZLE_REQUEST(isp, reqp);
   2516  1.39.2.2   bouyer 			ISP_ADD_REQUEST(isp, iptr);
   2517      1.36   mjacob 
   2518  1.39.2.2   bouyer 			if (isp_getrqentry(isp, &iptr, &optr, (void **)&reqp)) {
   2519  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGDEBUG0,
   2520  1.39.2.2   bouyer 				    "Request Queue Overflow+");
   2521      1.36   mjacob 				XS_SETERR(xs, HBA_BOTCH);
   2522      1.36   mjacob 				return (CMD_EAGAIN);
   2523      1.36   mjacob 			}
   2524       1.1      cgd 		}
   2525       1.1      cgd 	}
   2526       1.1      cgd 
   2527      1.30   mjacob 	MEMZERO((void *) reqp, UZSIZE);
   2528      1.10   mjacob 	reqp->req_header.rqs_entry_count = 1;
   2529      1.38   mjacob 	if (IS_FC(isp)) {
   2530      1.10   mjacob 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
   2531      1.10   mjacob 	} else {
   2532  1.39.2.2   bouyer 		if (XS_CDBLEN(xs) > 12)
   2533  1.39.2.2   bouyer 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
   2534  1.39.2.2   bouyer 		else
   2535  1.39.2.2   bouyer 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
   2536      1.10   mjacob 	}
   2537      1.10   mjacob 	reqp->req_header.rqs_flags = 0;
   2538      1.38   mjacob 	reqp->req_header.rqs_seqno = 0;
   2539      1.38   mjacob 	if (IS_FC(isp)) {
   2540      1.10   mjacob 		/*
   2541      1.10   mjacob 		 * See comment in isp_intr
   2542      1.10   mjacob 		 */
   2543      1.23   mjacob 		XS_RESID(xs) = 0;
   2544      1.34   mjacob 
   2545      1.10   mjacob 		/*
   2546  1.39.2.2   bouyer 		 * Fibre Channel always requires some kind of tag.
   2547  1.39.2.2   bouyer 		 * The Qlogic drivers seem be happy not to use a tag,
   2548  1.39.2.2   bouyer 		 * but this breaks for some devices (IBM drives).
   2549      1.10   mjacob 		 */
   2550  1.39.2.2   bouyer 		if (XS_TAG_P(xs)) {
   2551  1.39.2.2   bouyer 			t2reqp->req_flags = XS_TAG_TYPE(xs);
   2552  1.39.2.2   bouyer 		} else {
   2553  1.39.2.2   bouyer 			/*
   2554  1.39.2.2   bouyer 			 * If we don't know what tag to use, use HEAD OF QUEUE
   2555  1.39.2.2   bouyer 			 * for Request Sense or Ordered (for safety's sake).
   2556  1.39.2.2   bouyer 			 */
   2557  1.39.2.2   bouyer 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
   2558  1.39.2.2   bouyer 				t2reqp->req_flags = REQFLAG_HTAG;
   2559  1.39.2.2   bouyer 			else
   2560  1.39.2.2   bouyer 				t2reqp->req_flags = REQFLAG_OTAG;
   2561      1.10   mjacob 		}
   2562      1.10   mjacob 	} else {
   2563      1.23   mjacob 		sdparam *sdp = (sdparam *)isp->isp_param;
   2564      1.37   mjacob 		if ((sdp->isp_devparam[target].cur_dflags & DPARM_TQING) &&
   2565  1.39.2.2   bouyer 		    XS_TAG_P(xs)) {
   2566  1.39.2.2   bouyer 			reqp->req_flags = XS_TAG_TYPE(xs);
   2567      1.10   mjacob 		}
   2568      1.10   mjacob 	}
   2569      1.37   mjacob 	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
   2570      1.38   mjacob 	if (IS_SCSI(isp)) {
   2571      1.28   mjacob 		reqp->req_lun_trn = XS_LUN(xs);
   2572      1.23   mjacob 		reqp->req_cdblen = XS_CDBLEN(xs);
   2573      1.28   mjacob 	} else {
   2574  1.39.2.2   bouyer 		if (isp->isp_maxluns > 16)
   2575  1.39.2.2   bouyer 			t2reqp->req_scclun = XS_LUN(xs);
   2576  1.39.2.2   bouyer 		else
   2577  1.39.2.2   bouyer 			t2reqp->req_lun_trn = XS_LUN(xs);
   2578      1.10   mjacob 	}
   2579      1.31   mjacob 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
   2580       1.1      cgd 
   2581      1.23   mjacob 	reqp->req_time = XS_TIME(xs) / 1000;
   2582      1.23   mjacob 	if (reqp->req_time == 0 && XS_TIME(xs))
   2583      1.10   mjacob 		reqp->req_time = 1;
   2584      1.33   mjacob 
   2585      1.33   mjacob 	/*
   2586      1.33   mjacob 	 * Always give a bit more leeway to commands after a bus reset.
   2587      1.36   mjacob 	 * XXX: DOES NOT DISTINGUISH WHICH PORT MAY HAVE BEEN SYNCED
   2588      1.33   mjacob 	 */
   2589      1.38   mjacob 	if (isp->isp_sendmarker && reqp->req_time < 5) {
   2590      1.33   mjacob 		reqp->req_time = 5;
   2591      1.38   mjacob 	}
   2592  1.39.2.7   bouyer 	if (isp_save_xs(isp, xs, &handle)) {
   2593  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGDEBUG1, "out of xflist pointers");
   2594      1.38   mjacob 		XS_SETERR(xs, HBA_BOTCH);
   2595      1.38   mjacob 		return (CMD_EAGAIN);
   2596      1.38   mjacob 	}
   2597  1.39.2.7   bouyer 	reqp->req_handle = handle;
   2598      1.38   mjacob 	/*
   2599      1.38   mjacob 	 * Set up DMA and/or do any bus swizzling of the request entry
   2600      1.38   mjacob 	 * so that the Qlogic F/W understands what is being asked of it.
   2601      1.38   mjacob  	*/
   2602      1.27   mjacob 	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
   2603      1.27   mjacob 	if (i != CMD_QUEUED) {
   2604  1.39.2.7   bouyer 		isp_destroy_handle(isp, handle);
   2605      1.27   mjacob 		/*
   2606      1.27   mjacob 		 * dmasetup sets actual error in packet, and
   2607      1.27   mjacob 		 * return what we were given to return.
   2608      1.27   mjacob 		 */
   2609      1.27   mjacob 		return (i);
   2610       1.1      cgd 	}
   2611      1.23   mjacob 	XS_SETERR(xs, HBA_NOERROR);
   2612  1.39.2.2   bouyer 	isp_prt(isp, ISP_LOGDEBUG2,
   2613  1.39.2.6   bouyer 	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
   2614  1.39.2.6   bouyer 	    XS_CHANNEL(xs), target, XS_LUN(xs), XS_CDBP(xs)[0],
   2615  1.39.2.6   bouyer 	    (long) XS_XFRLEN(xs));
   2616  1.39.2.2   bouyer 	ISP_ADD_REQUEST(isp, iptr);
   2617      1.23   mjacob 	isp->isp_nactive++;
   2618      1.33   mjacob 	if (isp->isp_sendmarker)
   2619      1.33   mjacob 		isp->isp_sendmarker = 0;
   2620      1.23   mjacob 	return (CMD_QUEUED);
   2621      1.10   mjacob #undef	reqp
   2622      1.10   mjacob #undef	t2reqp
   2623       1.1      cgd }
   2624       1.1      cgd 
   2625       1.1      cgd /*
   2626      1.23   mjacob  * isp control
   2627      1.23   mjacob  * Locks (ints blocked) assumed held.
   2628       1.1      cgd  */
   2629       1.1      cgd 
   2630       1.1      cgd int
   2631  1.39.2.7   bouyer isp_control(struct ispsoftc *isp, ispctl_t ctl, void *arg)
   2632       1.1      cgd {
   2633  1.39.2.2   bouyer 	XS_T *xs;
   2634      1.23   mjacob 	mbreg_t mbs;
   2635      1.38   mjacob 	int bus, tgt;
   2636  1.39.2.7   bouyer 	u_int16_t handle;
   2637       1.3      cgd 
   2638      1.23   mjacob 	switch (ctl) {
   2639      1.23   mjacob 	default:
   2640  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
   2641      1.23   mjacob 		break;
   2642       1.3      cgd 
   2643      1.23   mjacob 	case ISPCTL_RESET_BUS:
   2644      1.33   mjacob 		/*
   2645      1.33   mjacob 		 * Issue a bus reset.
   2646      1.33   mjacob 		 */
   2647      1.23   mjacob 		mbs.param[0] = MBOX_BUS_RESET;
   2648  1.39.2.2   bouyer 		mbs.param[2] = 0;
   2649      1.38   mjacob 		if (IS_SCSI(isp)) {
   2650      1.32   mjacob 			mbs.param[1] =
   2651      1.32   mjacob 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
   2652      1.33   mjacob 			if (mbs.param[1] < 2)
   2653      1.33   mjacob 				mbs.param[1] = 2;
   2654      1.38   mjacob 			bus = *((int *) arg);
   2655  1.39.2.2   bouyer 			if (IS_DUALBUS(isp))
   2656  1.39.2.2   bouyer 				mbs.param[2] = bus;
   2657      1.32   mjacob 		} else {
   2658      1.38   mjacob 			mbs.param[1] = 10;
   2659      1.38   mjacob 			bus = 0;
   2660      1.32   mjacob 		}
   2661  1.39.2.2   bouyer 		isp->isp_sendmarker |= (1 << bus);
   2662  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   2663      1.23   mjacob 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2664      1.23   mjacob 			break;
   2665      1.23   mjacob 		}
   2666  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO,
   2667  1.39.2.2   bouyer 		    "driver initiated bus reset of bus %d", bus);
   2668      1.23   mjacob 		return (0);
   2669      1.23   mjacob 
   2670      1.34   mjacob 	case ISPCTL_RESET_DEV:
   2671      1.36   mjacob 		tgt = (*((int *) arg)) & 0xffff;
   2672      1.36   mjacob 		bus = (*((int *) arg)) >> 16;
   2673      1.23   mjacob 		mbs.param[0] = MBOX_ABORT_TARGET;
   2674      1.36   mjacob 		mbs.param[1] = (tgt << 8) | (bus << 15);
   2675      1.32   mjacob 		mbs.param[2] = 3;	/* 'delay', in seconds */
   2676  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   2677      1.23   mjacob 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2678      1.23   mjacob 			break;
   2679      1.23   mjacob 		}
   2680  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO,
   2681  1.39.2.2   bouyer 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
   2682  1.39.2.2   bouyer 		isp->isp_sendmarker |= (1 << bus);
   2683      1.23   mjacob 		return (0);
   2684      1.23   mjacob 
   2685      1.34   mjacob 	case ISPCTL_ABORT_CMD:
   2686  1.39.2.2   bouyer 		xs = (XS_T *) arg;
   2687  1.39.2.2   bouyer 		tgt = XS_TGT(xs);
   2688      1.38   mjacob 		handle = isp_find_handle(isp, xs);
   2689      1.38   mjacob 		if (handle == 0) {
   2690  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGWARN,
   2691  1.39.2.2   bouyer 			    "cannot find handle for command to abort");
   2692      1.23   mjacob 			break;
   2693      1.23   mjacob 		}
   2694      1.38   mjacob 		bus = XS_CHANNEL(xs);
   2695      1.23   mjacob 		mbs.param[0] = MBOX_ABORT;
   2696      1.38   mjacob 		if (IS_FC(isp)) {
   2697  1.39.2.2   bouyer 			if (isp->isp_maxluns > 16) {
   2698  1.39.2.2   bouyer 				mbs.param[1] = tgt << 8;
   2699  1.39.2.2   bouyer 				mbs.param[4] = 0;
   2700  1.39.2.2   bouyer 				mbs.param[5] = 0;
   2701  1.39.2.2   bouyer 				mbs.param[6] = XS_LUN(xs);
   2702  1.39.2.2   bouyer 			} else {
   2703  1.39.2.2   bouyer 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
   2704  1.39.2.2   bouyer 			}
   2705      1.28   mjacob 		} else {
   2706      1.38   mjacob 			mbs.param[1] =
   2707      1.38   mjacob 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
   2708      1.28   mjacob 		}
   2709  1.39.2.7   bouyer 		mbs.param[3] = 0;
   2710  1.39.2.7   bouyer 		mbs.param[2] = handle;
   2711  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
   2712  1.39.2.2   bouyer 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   2713  1.39.2.2   bouyer 			return (0);
   2714      1.23   mjacob 		}
   2715  1.39.2.2   bouyer 		/*
   2716  1.39.2.2   bouyer 		 * XXX: Look for command in the REQUEST QUEUE. That is,
   2717  1.39.2.2   bouyer 		 * XXX: It hasen't been picked up by firmware yet.
   2718  1.39.2.2   bouyer 		 */
   2719  1.39.2.2   bouyer 		break;
   2720      1.26   mjacob 
   2721      1.26   mjacob 	case ISPCTL_UPDATE_PARAMS:
   2722  1.39.2.6   bouyer 
   2723      1.26   mjacob 		isp_update(isp);
   2724      1.34   mjacob 		return (0);
   2725      1.33   mjacob 
   2726      1.33   mjacob 	case ISPCTL_FCLINK_TEST:
   2727  1.39.2.6   bouyer 
   2728  1.39.2.2   bouyer 		if (IS_FC(isp)) {
   2729  1.39.2.2   bouyer 			int usdelay = (arg)? *((int *) arg) : 250000;
   2730  1.39.2.2   bouyer 			return (isp_fclink_test(isp, usdelay));
   2731  1.39.2.2   bouyer 		}
   2732  1.39.2.2   bouyer 		break;
   2733  1.39.2.2   bouyer 
   2734  1.39.2.6   bouyer 	case ISPCTL_SCAN_FABRIC:
   2735  1.39.2.6   bouyer 
   2736  1.39.2.6   bouyer 		if (IS_FC(isp)) {
   2737  1.39.2.6   bouyer 			return (isp_scan_fabric(isp));
   2738  1.39.2.6   bouyer 		}
   2739  1.39.2.6   bouyer 		break;
   2740  1.39.2.6   bouyer 
   2741  1.39.2.6   bouyer 	case ISPCTL_SCAN_LOOP:
   2742  1.39.2.6   bouyer 
   2743  1.39.2.6   bouyer 		if (IS_FC(isp)) {
   2744  1.39.2.6   bouyer 			return (isp_scan_loop(isp));
   2745  1.39.2.6   bouyer 		}
   2746  1.39.2.6   bouyer 		break;
   2747  1.39.2.6   bouyer 
   2748  1.39.2.2   bouyer 	case ISPCTL_PDB_SYNC:
   2749  1.39.2.6   bouyer 
   2750  1.39.2.6   bouyer 		if (IS_FC(isp)) {
   2751  1.39.2.6   bouyer 			return (isp_pdb_sync(isp));
   2752  1.39.2.6   bouyer 		}
   2753  1.39.2.6   bouyer 		break;
   2754  1.39.2.6   bouyer 
   2755  1.39.2.6   bouyer 	case ISPCTL_SEND_LIP:
   2756  1.39.2.6   bouyer 
   2757  1.39.2.2   bouyer 		if (IS_FC(isp)) {
   2758  1.39.2.6   bouyer 			mbs.param[0] = MBOX_INIT_LIP;
   2759  1.39.2.6   bouyer 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   2760  1.39.2.6   bouyer 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   2761  1.39.2.6   bouyer 				return (0);
   2762  1.39.2.6   bouyer 			}
   2763  1.39.2.6   bouyer 		}
   2764  1.39.2.6   bouyer 		break;
   2765  1.39.2.6   bouyer 
   2766  1.39.2.6   bouyer 	case ISPCTL_GET_POSMAP:
   2767  1.39.2.6   bouyer 
   2768  1.39.2.6   bouyer 		if (IS_FC(isp) && arg) {
   2769  1.39.2.6   bouyer 			return (isp_getmap(isp, arg));
   2770  1.39.2.2   bouyer 		}
   2771  1.39.2.2   bouyer 		break;
   2772  1.39.2.6   bouyer 
   2773  1.39.2.6   bouyer 	case ISPCTL_RUN_MBOXCMD:
   2774  1.39.2.6   bouyer 
   2775  1.39.2.6   bouyer 		isp_mboxcmd(isp, arg, MBLOGALL);
   2776  1.39.2.6   bouyer 		return(0);
   2777  1.39.2.6   bouyer 
   2778  1.39.2.2   bouyer #ifdef	ISP_TARGET_MODE
   2779  1.39.2.2   bouyer 	case ISPCTL_TOGGLE_TMODE:
   2780  1.39.2.2   bouyer 	{
   2781  1.39.2.2   bouyer 		int ena = *(int *)arg;
   2782  1.39.2.6   bouyer 
   2783  1.39.2.6   bouyer 		/*
   2784  1.39.2.6   bouyer 		 * We don't check/set against role here- that's the
   2785  1.39.2.6   bouyer 		 * responsibility for the outer layer to coordinate.
   2786  1.39.2.6   bouyer 		 */
   2787  1.39.2.2   bouyer 		if (IS_SCSI(isp)) {
   2788  1.39.2.2   bouyer 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
   2789  1.39.2.2   bouyer 			mbs.param[1] = (ena)? ENABLE_TARGET_FLAG : 0;
   2790  1.39.2.2   bouyer 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   2791  1.39.2.2   bouyer 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   2792  1.39.2.2   bouyer 				break;
   2793  1.39.2.2   bouyer 			}
   2794  1.39.2.2   bouyer 		}
   2795  1.39.2.2   bouyer 		return (0);
   2796  1.39.2.2   bouyer 	}
   2797  1.39.2.2   bouyer #endif
   2798       1.1      cgd 	}
   2799      1.23   mjacob 	return (-1);
   2800       1.1      cgd }
   2801       1.1      cgd 
   2802      1.23   mjacob /*
   2803      1.23   mjacob  * Interrupt Service Routine(s).
   2804      1.23   mjacob  *
   2805      1.23   mjacob  * External (OS) framework has done the appropriate locking,
   2806      1.23   mjacob  * and the locking will be held throughout this function.
   2807      1.23   mjacob  */
   2808      1.23   mjacob 
   2809  1.39.2.2   bouyer /*
   2810  1.39.2.2   bouyer  * Limit our stack depth by sticking with the max likely number
   2811  1.39.2.2   bouyer  * of completions on a request queue at any one time.
   2812  1.39.2.2   bouyer  */
   2813  1.39.2.2   bouyer #define	MAX_REQUESTQ_COMPLETIONS	32
   2814  1.39.2.2   bouyer 
   2815       1.1      cgd int
   2816  1.39.2.7   bouyer isp_intr(void *arg)
   2817       1.1      cgd {
   2818       1.1      cgd 	struct ispsoftc *isp = arg;
   2819  1.39.2.2   bouyer 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
   2820  1.39.2.2   bouyer 	u_int16_t iptr, optr, isr, sema, junk;
   2821      1.32   mjacob 	int i, nlooked = 0, ndone = 0;
   2822       1.1      cgd 
   2823  1.39.2.2   bouyer 	if (IS_2100(isp)) {
   2824  1.39.2.2   bouyer 		i = 0;
   2825  1.39.2.2   bouyer 		do {
   2826  1.39.2.2   bouyer 			isr = ISP_READ(isp, BIU_ISR);
   2827  1.39.2.2   bouyer 			junk = ISP_READ(isp, BIU_ISR);
   2828  1.39.2.2   bouyer 		} while (isr != junk && ++i < 1000);
   2829  1.39.2.2   bouyer 		if (isr != junk) {
   2830  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGWARN,
   2831  1.39.2.2   bouyer 			    "isr unsteady (%x, %x)", isr, junk);
   2832  1.39.2.2   bouyer 		}
   2833  1.39.2.2   bouyer 		i = 0;
   2834  1.39.2.2   bouyer 		do {
   2835  1.39.2.2   bouyer 			sema = ISP_READ(isp, BIU_SEMA);
   2836  1.39.2.2   bouyer 			junk = ISP_READ(isp, BIU_SEMA);
   2837  1.39.2.2   bouyer 		} while (sema != junk && ++i < 1000);
   2838  1.39.2.2   bouyer 		if (sema != junk) {
   2839  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGWARN,
   2840  1.39.2.2   bouyer 			    "sema unsteady (%x, %x)", sema, junk);
   2841  1.39.2.2   bouyer 		}
   2842  1.39.2.2   bouyer 	} else {
   2843      1.38   mjacob 		isr = ISP_READ(isp, BIU_ISR);
   2844  1.39.2.2   bouyer 		sema = ISP_READ(isp, BIU_SEMA);
   2845      1.39   mjacob 	}
   2846  1.39.2.2   bouyer 	isp_prt(isp, ISP_LOGDEBUG3, "isp_intr isr %x sem %x", isr, sema);
   2847  1.39.2.2   bouyer 	isr &= INT_PENDING_MASK(isp);
   2848  1.39.2.2   bouyer 	sema &= BIU_SEMA_LOCK;
   2849  1.39.2.3   bouyer 	isp->isp_intcnt++;
   2850  1.39.2.2   bouyer 	if (isr == 0 && sema == 0) {
   2851  1.39.2.3   bouyer 		isp->isp_intbogus++;
   2852      1.39   mjacob 		return (0);
   2853       1.1      cgd 	}
   2854       1.1      cgd 
   2855      1.36   mjacob 	if (sema) {
   2856  1.39.2.2   bouyer 		u_int16_t mbox;
   2857  1.39.2.2   bouyer 
   2858  1.39.2.2   bouyer 		if (IS_2100(isp)) {
   2859  1.39.2.2   bouyer 			i = 0;
   2860  1.39.2.2   bouyer 			do {
   2861  1.39.2.2   bouyer 				mbox = ISP_READ(isp, OUTMAILBOX0);
   2862  1.39.2.2   bouyer 				junk = ISP_READ(isp, OUTMAILBOX0);;
   2863  1.39.2.2   bouyer 			} while (junk != mbox && ++i < 1000);
   2864  1.39.2.2   bouyer 			if (mbox != junk) {
   2865  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGWARN,
   2866  1.39.2.2   bouyer 				    "mailbox0 unsteady (%x, %x)", mbox, junk);
   2867  1.39.2.2   bouyer 				ISP_WRITE(isp, BIU_SEMA, 0);
   2868  1.39.2.2   bouyer 				ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2869  1.39.2.2   bouyer 				return (1);
   2870  1.39.2.2   bouyer 			}
   2871  1.39.2.2   bouyer 		} else {
   2872  1.39.2.2   bouyer 			mbox = ISP_READ(isp, OUTMAILBOX0);
   2873  1.39.2.2   bouyer 		}
   2874      1.33   mjacob 		if (mbox & 0x4000) {
   2875  1.39.2.2   bouyer 			int obits, i = 0;
   2876  1.39.2.2   bouyer 			if ((obits = isp->isp_mboxbsy) != 0) {
   2877  1.39.2.2   bouyer 				isp->isp_mboxtmp[i++] = mbox;
   2878  1.39.2.4   bouyer 				for (i = 1; i < MAX_MAILBOX; i++) {
   2879  1.39.2.2   bouyer 					if ((obits & (1 << i)) == 0) {
   2880  1.39.2.2   bouyer 						continue;
   2881  1.39.2.2   bouyer 					}
   2882  1.39.2.2   bouyer 					isp->isp_mboxtmp[i] =
   2883  1.39.2.2   bouyer 					    ISP_READ(isp, MBOX_OFF(i));
   2884  1.39.2.2   bouyer 				}
   2885  1.39.2.2   bouyer 				MBOX_NOTIFY_COMPLETE(isp);
   2886  1.39.2.2   bouyer 			} else {
   2887  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGWARN,
   2888  1.39.2.2   bouyer 				    "Mbox Command Async (0x%x) with no waiters",
   2889  1.39.2.2   bouyer 				    mbox);
   2890  1.39.2.2   bouyer 			}
   2891      1.33   mjacob 		} else {
   2892  1.39.2.7   bouyer 			int fhandle = isp_parse_async(isp, (int) mbox);
   2893  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
   2894      1.35   mjacob 			if (fhandle > 0) {
   2895  1.39.2.7   bouyer 				isp_fastpost_complete(isp, (u_int16_t) fhandle);
   2896      1.33   mjacob 			}
   2897      1.31   mjacob 		}
   2898  1.39.2.2   bouyer 		if (IS_FC(isp) || isp->isp_state != ISP_RUNSTATE) {
   2899  1.39.2.2   bouyer 			ISP_WRITE(isp, BIU_SEMA, 0);
   2900  1.39.2.2   bouyer 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2901  1.39.2.2   bouyer 			return (1);
   2902  1.39.2.2   bouyer 		}
   2903  1.39.2.2   bouyer 	}
   2904  1.39.2.2   bouyer 
   2905  1.39.2.2   bouyer 	/*
   2906  1.39.2.2   bouyer 	 * We can't be getting this now.
   2907  1.39.2.2   bouyer 	 */
   2908  1.39.2.2   bouyer 	if (isp->isp_state != ISP_RUNSTATE) {
   2909  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN,
   2910  1.39.2.2   bouyer 		    "interrupt (isr=%x, sema=%x) when not ready", isr, sema);
   2911  1.39.2.2   bouyer 		ISP_WRITE(isp, INMAILBOX5, ISP_READ(isp, OUTMAILBOX5));
   2912      1.35   mjacob 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2913  1.39.2.2   bouyer 		ISP_WRITE(isp, BIU_SEMA, 0);
   2914      1.35   mjacob 		return (1);
   2915       1.1      cgd 	}
   2916      1.25   mjacob 
   2917      1.35   mjacob 	/*
   2918      1.35   mjacob 	 * You *must* read OUTMAILBOX5 prior to clearing the RISC interrupt.
   2919      1.35   mjacob 	 */
   2920      1.24   mjacob 	optr = isp->isp_residx;
   2921  1.39.2.2   bouyer 
   2922  1.39.2.2   bouyer 	if (IS_2100(isp)) {
   2923  1.39.2.2   bouyer 		i = 0;
   2924  1.39.2.2   bouyer 		do {
   2925  1.39.2.2   bouyer 			iptr = ISP_READ(isp, OUTMAILBOX5);
   2926  1.39.2.2   bouyer 			junk = ISP_READ(isp, OUTMAILBOX5);
   2927  1.39.2.2   bouyer 		} while (junk != iptr && ++i < 1000);
   2928  1.39.2.2   bouyer 
   2929  1.39.2.2   bouyer 		if (iptr != junk) {
   2930  1.39.2.2   bouyer 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2931  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGWARN,
   2932  1.39.2.2   bouyer 			    "mailbox5 unsteady (%x, %x)", iptr, junk);
   2933  1.39.2.2   bouyer 			return (1);
   2934  1.39.2.2   bouyer 		}
   2935  1.39.2.2   bouyer 	} else {
   2936  1.39.2.2   bouyer 		iptr = ISP_READ(isp, OUTMAILBOX5);
   2937  1.39.2.2   bouyer 	}
   2938  1.39.2.2   bouyer 
   2939  1.39.2.2   bouyer 	if (sema) {
   2940  1.39.2.2   bouyer 		ISP_WRITE(isp, BIU_SEMA, 0);
   2941  1.39.2.2   bouyer 	}
   2942      1.35   mjacob 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
   2943  1.39.2.2   bouyer 
   2944  1.39.2.2   bouyer 	if (optr == iptr && sema == 0) {
   2945  1.39.2.2   bouyer 		/*
   2946  1.39.2.2   bouyer 		 * There are a lot of these- reasons unknown- mostly on
   2947  1.39.2.2   bouyer 		 * faster Alpha machines.
   2948  1.39.2.2   bouyer 		 *
   2949  1.39.2.2   bouyer 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
   2950  1.39.2.2   bouyer 		 * make sure the old interrupt went away (to avoid 'ringing'
   2951  1.39.2.2   bouyer 		 * effects), but that didn't stop this from occurring.
   2952  1.39.2.2   bouyer 		 */
   2953  1.39.2.2   bouyer 		junk = ISP_READ(isp, BIU_ISR);
   2954  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGDEBUG2,
   2955  1.39.2.2   bouyer 		    "bogus intr- isr %x (%x) iptr %x optr %x",
   2956  1.39.2.2   bouyer 		    isr, junk, iptr, optr);
   2957      1.10   mjacob 	}
   2958      1.10   mjacob 
   2959       1.1      cgd 	while (optr != iptr) {
   2960       1.1      cgd 		ispstatusreq_t *sp;
   2961  1.39.2.2   bouyer 		u_int16_t oop;
   2962       1.1      cgd 		int buddaboom = 0;
   2963       1.1      cgd 
   2964       1.7  thorpej 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
   2965      1.25   mjacob 		oop = optr;
   2966  1.39.2.2   bouyer 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
   2967      1.32   mjacob 		nlooked++;
   2968      1.38   mjacob 		/*
   2969      1.38   mjacob 		 * Do any appropriate unswizzling of what the Qlogic f/w has
   2970  1.39.2.2   bouyer 		 * written into memory so it makes sense to us. This is a
   2971  1.39.2.2   bouyer 		 * per-platform thing. Also includes any memory barriers.
   2972      1.38   mjacob 		 */
   2973  1.39.2.2   bouyer 		ISP_UNSWIZZLE_RESPONSE(isp, sp, oop);
   2974       1.1      cgd 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
   2975      1.25   mjacob 			if (isp_handle_other_response(isp, sp, &optr) == 0) {
   2976  1.39.2.2   bouyer 				MEMZERO(sp, sizeof (isphdr_t));
   2977      1.25   mjacob 				continue;
   2978      1.25   mjacob 			}
   2979      1.25   mjacob 			/*
   2980      1.25   mjacob 			 * It really has to be a bounced request just copied
   2981      1.36   mjacob 			 * from the request queue to the response queue. If
   2982      1.36   mjacob 			 * not, something bad has happened.
   2983      1.25   mjacob 			 */
   2984       1.1      cgd 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
   2985  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGERR, notresp,
   2986  1.39.2.3   bouyer 				    sp->req_header.rqs_entry_type, oop, optr,
   2987  1.39.2.3   bouyer 				    nlooked);
   2988  1.39.2.3   bouyer 				if (isp->isp_dblev & ISP_LOGDEBUG0) {
   2989  1.39.2.3   bouyer 					isp_print_bytes(isp, "Queue Entry",
   2990  1.39.2.3   bouyer 					    QENTRY_LEN, sp);
   2991  1.39.2.3   bouyer 				}
   2992  1.39.2.2   bouyer 				MEMZERO(sp, sizeof (isphdr_t));
   2993       1.1      cgd 				continue;
   2994       1.1      cgd 			}
   2995       1.1      cgd 			buddaboom = 1;
   2996       1.1      cgd 		}
   2997       1.1      cgd 
   2998       1.1      cgd 		if (sp->req_header.rqs_flags & 0xf) {
   2999      1.36   mjacob #define	_RQS_OFLAGS	\
   3000      1.36   mjacob 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
   3001       1.1      cgd 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
   3002  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGWARN,
   3003  1.39.2.2   bouyer 				    "continuation segment");
   3004       1.1      cgd 				ISP_WRITE(isp, INMAILBOX5, optr);
   3005       1.1      cgd 				continue;
   3006       1.1      cgd 			}
   3007      1.23   mjacob 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   3008  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGDEBUG1,
   3009  1.39.2.2   bouyer 				    "internal queues full");
   3010      1.36   mjacob 				/*
   3011  1.39.2.2   bouyer 				 * We'll synthesize a QUEUE FULL message below.
   3012      1.36   mjacob 				 */
   3013      1.23   mjacob 			}
   3014      1.23   mjacob 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
   3015  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
   3016      1.23   mjacob 				buddaboom++;
   3017      1.23   mjacob 			}
   3018      1.23   mjacob 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
   3019  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGERR, "bad request packet");
   3020      1.23   mjacob 				buddaboom++;
   3021      1.23   mjacob 			}
   3022      1.36   mjacob 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
   3023  1.39.2.2   bouyer 				isp_prt(isp, ISP_LOGERR,
   3024  1.39.2.2   bouyer 				    "unknown flags (0x%x) in response",
   3025  1.39.2.2   bouyer 				    sp->req_header.rqs_flags);
   3026      1.36   mjacob 				buddaboom++;
   3027      1.36   mjacob 			}
   3028      1.36   mjacob #undef	_RQS_OFLAGS
   3029       1.1      cgd 		}
   3030      1.38   mjacob 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
   3031  1.39.2.2   bouyer 			MEMZERO(sp, sizeof (isphdr_t));
   3032  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGERR,
   3033  1.39.2.6   bouyer 			    "bad request handle %d (type 0x%x, flags 0x%x)",
   3034  1.39.2.6   bouyer 			    sp->req_handle, sp->req_header.rqs_entry_type,
   3035  1.39.2.6   bouyer 			    sp->req_header.rqs_flags);
   3036       1.1      cgd 			ISP_WRITE(isp, INMAILBOX5, optr);
   3037       1.1      cgd 			continue;
   3038       1.1      cgd 		}
   3039      1.38   mjacob 		xs = isp_find_xs(isp, sp->req_handle);
   3040       1.1      cgd 		if (xs == NULL) {
   3041  1.39.2.2   bouyer 			MEMZERO(sp, sizeof (isphdr_t));
   3042  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGERR,
   3043  1.39.2.2   bouyer 			    "cannot find handle 0x%x in xflist",
   3044  1.39.2.2   bouyer 			    sp->req_handle);
   3045       1.1      cgd 			ISP_WRITE(isp, INMAILBOX5, optr);
   3046       1.1      cgd 			continue;
   3047       1.1      cgd 		}
   3048      1.38   mjacob 		isp_destroy_handle(isp, sp->req_handle);
   3049       1.1      cgd 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   3050      1.36   mjacob 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3051       1.1      cgd 		}
   3052       1.1      cgd 		if (buddaboom) {
   3053      1.23   mjacob 			XS_SETERR(xs, HBA_BOTCH);
   3054       1.1      cgd 		}
   3055  1.39.2.2   bouyer 
   3056  1.39.2.2   bouyer 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
   3057      1.31   mjacob 			/*
   3058  1.39.2.2   bouyer 			 * Fibre Channel F/W doesn't say we got status
   3059  1.39.2.2   bouyer 			 * if there's Sense Data instead. I guess they
   3060  1.39.2.2   bouyer 			 * think it goes w/o saying.
   3061      1.31   mjacob 			 */
   3062  1.39.2.2   bouyer 			sp->req_state_flags |= RQSF_GOT_STATUS;
   3063  1.39.2.2   bouyer 		}
   3064  1.39.2.2   bouyer 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   3065  1.39.2.2   bouyer 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
   3066       1.1      cgd 		}
   3067  1.39.2.1  thorpej 
   3068  1.39.2.2   bouyer 		switch (sp->req_header.rqs_entry_type) {
   3069  1.39.2.2   bouyer 		case RQSTYPE_RESPONSE:
   3070  1.39.2.2   bouyer 			XS_SET_STATE_STAT(isp, xs, sp);
   3071  1.39.2.2   bouyer 			isp_parse_status(isp, sp, xs);
   3072  1.39.2.2   bouyer 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
   3073  1.39.2.2   bouyer 			    (*XS_STSP(xs) == SCSI_BUSY)) {
   3074  1.39.2.1  thorpej 				XS_SETERR(xs, HBA_TGTBSY);
   3075  1.39.2.1  thorpej 			}
   3076  1.39.2.2   bouyer 			if (IS_SCSI(isp)) {
   3077  1.39.2.2   bouyer 				XS_RESID(xs) = sp->req_resid;
   3078  1.39.2.2   bouyer 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   3079  1.39.2.2   bouyer 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   3080  1.39.2.2   bouyer 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
   3081  1.39.2.2   bouyer 					XS_SAVE_SENSE(xs, sp);
   3082  1.39.2.2   bouyer 				}
   3083  1.39.2.2   bouyer 				/*
   3084  1.39.2.2   bouyer 				 * A new synchronous rate was negotiated for
   3085  1.39.2.2   bouyer 				 * this target. Mark state such that we'll go
   3086  1.39.2.2   bouyer 				 * look up that which has changed later.
   3087  1.39.2.2   bouyer 				 */
   3088  1.39.2.2   bouyer 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   3089  1.39.2.2   bouyer 					int t = XS_TGT(xs);
   3090  1.39.2.2   bouyer 					sdparam *sdp = isp->isp_param;
   3091  1.39.2.2   bouyer 					sdp += XS_CHANNEL(xs);
   3092  1.39.2.2   bouyer 					sdp->isp_devparam[t].dev_refresh = 1;
   3093  1.39.2.2   bouyer 					isp->isp_update |=
   3094  1.39.2.2   bouyer 					    (1 << XS_CHANNEL(xs));
   3095  1.39.2.2   bouyer 				}
   3096  1.39.2.2   bouyer 			} else {
   3097  1.39.2.2   bouyer 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
   3098  1.39.2.2   bouyer 					XS_RESID(xs) = 0;
   3099  1.39.2.2   bouyer 				} else if (sp->req_scsi_status & RQCS_RESID) {
   3100  1.39.2.2   bouyer 					XS_RESID(xs) = sp->req_resid;
   3101  1.39.2.2   bouyer 				} else {
   3102  1.39.2.2   bouyer 					XS_RESID(xs) = 0;
   3103  1.39.2.2   bouyer 				}
   3104  1.39.2.2   bouyer 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
   3105  1.39.2.2   bouyer 				    (*XS_STSP(xs) == SCSI_CHECK) &&
   3106  1.39.2.2   bouyer 				    (sp->req_scsi_status & RQCS_SV)) {
   3107  1.39.2.2   bouyer 					XS_SAVE_SENSE(xs, sp);
   3108  1.39.2.6   bouyer 					/* solely for the benefit of debug */
   3109  1.39.2.6   bouyer 					sp->req_state_flags |= RQSF_GOT_SENSE;
   3110  1.39.2.2   bouyer 				}
   3111      1.25   mjacob 			}
   3112  1.39.2.6   bouyer 			isp_prt(isp, ISP_LOGDEBUG2,
   3113  1.39.2.6   bouyer 			   "asked for %ld got resid %ld", (long) XS_XFRLEN(xs),
   3114  1.39.2.6   bouyer 			   (long) sp->req_resid);
   3115  1.39.2.2   bouyer 			break;
   3116  1.39.2.2   bouyer 		case RQSTYPE_REQUEST:
   3117      1.36   mjacob 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
   3118      1.36   mjacob 				/*
   3119  1.39.2.2   bouyer 				 * Force Queue Full status.
   3120      1.36   mjacob 				 */
   3121  1.39.2.2   bouyer 				*XS_STSP(xs) = SCSI_QFULL;
   3122  1.39.2.2   bouyer 				XS_SETERR(xs, HBA_NOERROR);
   3123      1.36   mjacob 			} else if (XS_NOERR(xs)) {
   3124      1.36   mjacob 				XS_SETERR(xs, HBA_BOTCH);
   3125      1.36   mjacob 			}
   3126  1.39.2.2   bouyer 			XS_RESID(xs) = XS_XFRLEN(xs);
   3127  1.39.2.2   bouyer 			break;
   3128  1.39.2.2   bouyer 		default:
   3129  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGWARN,
   3130  1.39.2.6   bouyer 			    "unhandled response queue type 0x%x",
   3131  1.39.2.2   bouyer 			    sp->req_header.rqs_entry_type);
   3132      1.30   mjacob 			if (XS_NOERR(xs)) {
   3133      1.23   mjacob 				XS_SETERR(xs, HBA_BOTCH);
   3134      1.30   mjacob 			}
   3135  1.39.2.2   bouyer 			break;
   3136       1.1      cgd 		}
   3137  1.39.2.2   bouyer 
   3138  1.39.2.2   bouyer 		/*
   3139  1.39.2.2   bouyer 		 * Free any dma resources. As a side effect, this may
   3140  1.39.2.2   bouyer 		 * also do any cache flushing necessary for data coherence.			 */
   3141      1.23   mjacob 		if (XS_XFRLEN(xs)) {
   3142      1.38   mjacob 			ISP_DMAFREE(isp, xs, sp->req_handle);
   3143       1.1      cgd 		}
   3144  1.39.2.2   bouyer 
   3145  1.39.2.2   bouyer 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
   3146  1.39.2.6   bouyer 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
   3147  1.39.2.6   bouyer 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
   3148  1.39.2.2   bouyer 			char skey;
   3149      1.10   mjacob 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
   3150  1.39.2.2   bouyer 				skey = XS_SNSKEY(xs) & 0xf;
   3151  1.39.2.2   bouyer 				if (skey < 10)
   3152  1.39.2.2   bouyer 					skey += '0';
   3153  1.39.2.2   bouyer 				else
   3154  1.39.2.6   bouyer 					skey += 'a' - 10;
   3155  1.39.2.2   bouyer 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
   3156  1.39.2.2   bouyer 				skey = '?';
   3157  1.39.2.2   bouyer 			} else {
   3158  1.39.2.2   bouyer 				skey = '.';
   3159       1.1      cgd 			}
   3160  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
   3161  1.39.2.2   bouyer 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
   3162  1.39.2.2   bouyer 			    *XS_STSP(xs), skey, XS_ERR(xs));
   3163       1.1      cgd 		}
   3164      1.23   mjacob 
   3165      1.28   mjacob 		if (isp->isp_nactive > 0)
   3166      1.28   mjacob 		    isp->isp_nactive--;
   3167      1.25   mjacob 		complist[ndone++] = xs;	/* defer completion call until later */
   3168  1.39.2.2   bouyer 		MEMZERO(sp, sizeof (isphdr_t));
   3169  1.39.2.2   bouyer 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
   3170  1.39.2.2   bouyer 			break;
   3171  1.39.2.2   bouyer 		}
   3172       1.1      cgd 	}
   3173      1.32   mjacob 
   3174      1.27   mjacob 	/*
   3175      1.32   mjacob 	 * If we looked at any commands, then it's valid to find out
   3176      1.32   mjacob 	 * what the outpointer is. It also is a trigger to update the
   3177      1.32   mjacob 	 * ISP's notion of what we've seen so far.
   3178      1.27   mjacob 	 */
   3179      1.33   mjacob 	if (nlooked) {
   3180      1.32   mjacob 		ISP_WRITE(isp, INMAILBOX5, optr);
   3181      1.34   mjacob 		isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
   3182      1.27   mjacob 	}
   3183  1.39.2.2   bouyer 
   3184       1.1      cgd 	isp->isp_residx = optr;
   3185      1.25   mjacob 	for (i = 0; i < ndone; i++) {
   3186      1.25   mjacob 		xs = complist[i];
   3187      1.25   mjacob 		if (xs) {
   3188  1.39.2.2   bouyer 			isp_done(xs);
   3189      1.25   mjacob 		}
   3190      1.25   mjacob 	}
   3191       1.1      cgd 	return (1);
   3192       1.1      cgd }
   3193       1.1      cgd 
   3194       1.1      cgd /*
   3195       1.1      cgd  * Support routines.
   3196       1.1      cgd  */
   3197       1.1      cgd 
   3198      1.25   mjacob static int
   3199  1.39.2.7   bouyer isp_parse_async(struct ispsoftc *isp, int mbox)
   3200      1.25   mjacob {
   3201  1.39.2.2   bouyer 	int bus;
   3202  1.39.2.7   bouyer 	u_int16_t fast_post_handle = 0;
   3203      1.31   mjacob 
   3204  1.39.2.2   bouyer 	if (IS_DUALBUS(isp)) {
   3205  1.39.2.2   bouyer 		bus = ISP_READ(isp, OUTMAILBOX6);
   3206  1.39.2.2   bouyer 	} else {
   3207  1.39.2.2   bouyer 		bus = 0;
   3208  1.39.2.2   bouyer 	}
   3209  1.39.2.2   bouyer 
   3210      1.25   mjacob 	switch (mbox) {
   3211      1.25   mjacob 	case ASYNC_BUS_RESET:
   3212  1.39.2.2   bouyer 		isp->isp_sendmarker |= (1 << bus);
   3213      1.29   mjacob #ifdef	ISP_TARGET_MODE
   3214  1.39.2.2   bouyer 		isp_target_async(isp, bus, mbox);
   3215      1.29   mjacob #endif
   3216  1.39.2.2   bouyer 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
   3217      1.25   mjacob 		break;
   3218      1.25   mjacob 	case ASYNC_SYSTEM_ERROR:
   3219      1.25   mjacob 		mbox = ISP_READ(isp, OUTMAILBOX1);
   3220  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3221  1.39.2.6   bouyer 		    "Internal Firmware Error @ RISC Addr 0x%x", mbox);
   3222  1.39.2.6   bouyer 		ISP_DUMPREGS(isp, "Firmware Error");
   3223  1.39.2.2   bouyer 		isp_reinit(isp);
   3224  1.39.2.3   bouyer #ifdef	ISP_TARGET_MODE
   3225  1.39.2.6   bouyer 		isp_target_async(isp, bus, ASYNC_SYSTEM_ERROR);
   3226  1.39.2.3   bouyer #endif
   3227      1.25   mjacob 		/* no point continuing after this */
   3228      1.31   mjacob 		return (-1);
   3229      1.25   mjacob 
   3230      1.25   mjacob 	case ASYNC_RQS_XFER_ERR:
   3231  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
   3232      1.25   mjacob 		break;
   3233      1.25   mjacob 
   3234      1.25   mjacob 	case ASYNC_RSP_XFER_ERR:
   3235  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
   3236      1.25   mjacob 		break;
   3237      1.25   mjacob 
   3238      1.25   mjacob 	case ASYNC_QWAKEUP:
   3239  1.39.2.2   bouyer 		/*
   3240  1.39.2.2   bouyer 		 * We've just been notified that the Queue has woken up.
   3241  1.39.2.2   bouyer 		 * We don't need to be chatty about this- just unlatch things
   3242  1.39.2.2   bouyer 		 * and move on.
   3243  1.39.2.2   bouyer 		 */
   3244      1.25   mjacob 		mbox = ISP_READ(isp, OUTMAILBOX4);
   3245      1.25   mjacob 		break;
   3246      1.25   mjacob 
   3247      1.25   mjacob 	case ASYNC_TIMEOUT_RESET:
   3248  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN,
   3249  1.39.2.7   bouyer 		    "timeout initiated SCSI bus reset of bus %d", bus);
   3250  1.39.2.2   bouyer 		isp->isp_sendmarker |= (1 << bus);
   3251      1.29   mjacob #ifdef	ISP_TARGET_MODE
   3252  1.39.2.2   bouyer 		isp_target_async(isp, bus, mbox);
   3253      1.29   mjacob #endif
   3254      1.25   mjacob 		break;
   3255      1.25   mjacob 
   3256      1.28   mjacob 	case ASYNC_DEVICE_RESET:
   3257  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
   3258  1.39.2.2   bouyer 		isp->isp_sendmarker |= (1 << bus);
   3259      1.29   mjacob #ifdef	ISP_TARGET_MODE
   3260  1.39.2.2   bouyer 		isp_target_async(isp, bus, mbox);
   3261      1.29   mjacob #endif
   3262      1.25   mjacob 		break;
   3263      1.25   mjacob 
   3264      1.25   mjacob 	case ASYNC_EXTMSG_UNDERRUN:
   3265  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
   3266      1.25   mjacob 		break;
   3267      1.25   mjacob 
   3268      1.25   mjacob 	case ASYNC_SCAM_INT:
   3269  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
   3270      1.25   mjacob 		break;
   3271      1.25   mjacob 
   3272      1.25   mjacob 	case ASYNC_HUNG_SCSI:
   3273  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3274  1.39.2.2   bouyer 		    "stalled SCSI Bus after DATA Overrun");
   3275      1.25   mjacob 		/* XXX: Need to issue SCSI reset at this point */
   3276      1.25   mjacob 		break;
   3277      1.25   mjacob 
   3278      1.25   mjacob 	case ASYNC_KILLED_BUS:
   3279  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
   3280      1.25   mjacob 		break;
   3281      1.25   mjacob 
   3282      1.25   mjacob 	case ASYNC_BUS_TRANSIT:
   3283      1.34   mjacob 		mbox = ISP_READ(isp, OUTMAILBOX2);
   3284      1.34   mjacob 		switch (mbox & 0x1c00) {
   3285      1.34   mjacob 		case SXP_PINS_LVD_MODE:
   3286  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
   3287  1.39.2.2   bouyer 			SDPARAM(isp)->isp_diffmode = 0;
   3288  1.39.2.2   bouyer 			SDPARAM(isp)->isp_ultramode = 0;
   3289  1.39.2.2   bouyer 			SDPARAM(isp)->isp_lvdmode = 1;
   3290      1.34   mjacob 			break;
   3291      1.34   mjacob 		case SXP_PINS_HVD_MODE:
   3292  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGINFO,
   3293  1.39.2.2   bouyer 			    "Transition to Differential mode");
   3294  1.39.2.2   bouyer 			SDPARAM(isp)->isp_diffmode = 1;
   3295  1.39.2.2   bouyer 			SDPARAM(isp)->isp_ultramode = 0;
   3296  1.39.2.2   bouyer 			SDPARAM(isp)->isp_lvdmode = 0;
   3297      1.34   mjacob 			break;
   3298      1.34   mjacob 		case SXP_PINS_SE_MODE:
   3299  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGINFO,
   3300  1.39.2.2   bouyer 			    "Transition to Single Ended mode");
   3301  1.39.2.2   bouyer 			SDPARAM(isp)->isp_diffmode = 0;
   3302  1.39.2.2   bouyer 			SDPARAM(isp)->isp_ultramode = 1;
   3303  1.39.2.2   bouyer 			SDPARAM(isp)->isp_lvdmode = 0;
   3304      1.34   mjacob 			break;
   3305      1.34   mjacob 		default:
   3306  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGWARN,
   3307  1.39.2.2   bouyer 			    "Transition to Unknown Mode 0x%x", mbox);
   3308      1.34   mjacob 			break;
   3309      1.34   mjacob 		}
   3310      1.34   mjacob 		/*
   3311      1.34   mjacob 		 * XXX: Set up to renegotiate again!
   3312      1.34   mjacob 		 */
   3313      1.36   mjacob 		/* Can only be for a 1080... */
   3314  1.39.2.2   bouyer 		isp->isp_sendmarker |= (1 << bus);
   3315      1.25   mjacob 		break;
   3316      1.25   mjacob 
   3317      1.25   mjacob 	case ASYNC_CMD_CMPLT:
   3318  1.39.2.7   bouyer 		fast_post_handle = ISP_READ(isp, OUTMAILBOX1);
   3319  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGDEBUG3, "fast post completion of %u",
   3320  1.39.2.2   bouyer 		    fast_post_handle);
   3321      1.25   mjacob 		break;
   3322      1.25   mjacob 
   3323      1.25   mjacob 	case ASYNC_CTIO_DONE:
   3324  1.39.2.2   bouyer #ifdef	ISP_TARGET_MODE
   3325  1.39.2.2   bouyer 		/*
   3326  1.39.2.2   bouyer 		 * Bus gets overloaded with the handle. Dual bus
   3327  1.39.2.2   bouyer 		 * cards don't put bus# into the handle.
   3328  1.39.2.2   bouyer 		 */
   3329  1.39.2.2   bouyer 		bus = (ISP_READ(isp, OUTMAILBOX2) << 16) |
   3330  1.39.2.2   bouyer 		    ISP_READ(isp, OUTMAILBOX1);
   3331  1.39.2.2   bouyer 		isp_target_async(isp, bus, mbox);
   3332  1.39.2.2   bouyer #else
   3333  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
   3334  1.39.2.2   bouyer #endif
   3335      1.25   mjacob 		break;
   3336      1.25   mjacob 
   3337      1.25   mjacob 	case ASYNC_LIP_OCCURRED:
   3338  1.39.2.2   bouyer 		FCPARAM(isp)->isp_lipseq =
   3339      1.39   mjacob 		    ISP_READ(isp, OUTMAILBOX1);
   3340  1.39.2.2   bouyer 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3341  1.39.2.2   bouyer 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3342      1.33   mjacob 		isp->isp_sendmarker = 1;
   3343      1.35   mjacob 		isp_mark_getpdb_all(isp);
   3344  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO, "LIP occurred");
   3345  1.39.2.2   bouyer #ifdef	ISP_TARGET_MODE
   3346  1.39.2.2   bouyer 		isp_target_async(isp, bus, mbox);
   3347  1.39.2.2   bouyer #endif
   3348      1.25   mjacob 		break;
   3349      1.25   mjacob 
   3350      1.25   mjacob 	case ASYNC_LOOP_UP:
   3351      1.37   mjacob 		isp->isp_sendmarker = 1;
   3352  1.39.2.2   bouyer 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3353  1.39.2.2   bouyer 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3354      1.35   mjacob 		isp_mark_getpdb_all(isp);
   3355      1.32   mjacob 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
   3356  1.39.2.2   bouyer #ifdef	ISP_TARGET_MODE
   3357  1.39.2.2   bouyer 		isp_target_async(isp, bus, mbox);
   3358  1.39.2.2   bouyer #endif
   3359      1.25   mjacob 		break;
   3360      1.25   mjacob 
   3361      1.25   mjacob 	case ASYNC_LOOP_DOWN:
   3362      1.37   mjacob 		isp->isp_sendmarker = 1;
   3363  1.39.2.2   bouyer 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3364  1.39.2.2   bouyer 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   3365      1.35   mjacob 		isp_mark_getpdb_all(isp);
   3366      1.32   mjacob 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
   3367  1.39.2.2   bouyer #ifdef	ISP_TARGET_MODE
   3368  1.39.2.2   bouyer 		isp_target_async(isp, bus, mbox);
   3369  1.39.2.2   bouyer #endif
   3370      1.25   mjacob 		break;
   3371      1.25   mjacob 
   3372      1.25   mjacob 	case ASYNC_LOOP_RESET:
   3373      1.37   mjacob 		isp->isp_sendmarker = 1;
   3374  1.39.2.2   bouyer 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3375  1.39.2.2   bouyer 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
   3376      1.35   mjacob 		isp_mark_getpdb_all(isp);
   3377  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO, "Loop RESET");
   3378      1.29   mjacob #ifdef	ISP_TARGET_MODE
   3379  1.39.2.2   bouyer 		isp_target_async(isp, bus, mbox);
   3380      1.29   mjacob #endif
   3381      1.25   mjacob 		break;
   3382      1.25   mjacob 
   3383      1.25   mjacob 	case ASYNC_PDB_CHANGED:
   3384      1.33   mjacob 		isp->isp_sendmarker = 1;
   3385  1.39.2.2   bouyer 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   3386      1.33   mjacob 		isp_mark_getpdb_all(isp);
   3387  1.39.2.6   bouyer 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
   3388      1.25   mjacob 		break;
   3389      1.25   mjacob 
   3390      1.25   mjacob 	case ASYNC_CHANGE_NOTIFY:
   3391      1.38   mjacob 		/*
   3392      1.38   mjacob 		 * Not correct, but it will force us to rescan the loop.
   3393      1.38   mjacob 		 */
   3394  1.39.2.2   bouyer 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
   3395  1.39.2.5   bouyer 		isp_mark_getpdb_all(isp);
   3396  1.39.2.6   bouyer 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
   3397      1.25   mjacob 		break;
   3398      1.25   mjacob 
   3399  1.39.2.2   bouyer 	case ASYNC_PTPMODE:
   3400  1.39.2.2   bouyer 		if (FCPARAM(isp)->isp_onfabric)
   3401  1.39.2.2   bouyer 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
   3402  1.39.2.6   bouyer 		else
   3403  1.39.2.6   bouyer 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
   3404  1.39.2.2   bouyer 		isp_mark_getpdb_all(isp);
   3405  1.39.2.2   bouyer 		isp->isp_sendmarker = 1;
   3406  1.39.2.2   bouyer 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3407  1.39.2.2   bouyer 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3408  1.39.2.6   bouyer 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   3409  1.39.2.2   bouyer #ifdef	ISP_TARGET_MODE
   3410  1.39.2.2   bouyer 		isp_target_async(isp, bus, mbox);
   3411  1.39.2.2   bouyer #endif
   3412  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
   3413  1.39.2.2   bouyer 		break;
   3414  1.39.2.2   bouyer 
   3415  1.39.2.2   bouyer 	case ASYNC_CONNMODE:
   3416  1.39.2.2   bouyer 		mbox = ISP_READ(isp, OUTMAILBOX1);
   3417  1.39.2.6   bouyer 		isp_mark_getpdb_all(isp);
   3418  1.39.2.2   bouyer 		switch (mbox) {
   3419  1.39.2.2   bouyer 		case ISP_CONN_LOOP:
   3420  1.39.2.6   bouyer 			isp_prt(isp, ISP_LOGINFO,
   3421  1.39.2.6   bouyer 			    "Point-to-Point -> Loop mode");
   3422  1.39.2.2   bouyer 			break;
   3423  1.39.2.2   bouyer 		case ISP_CONN_PTP:
   3424  1.39.2.6   bouyer 			isp_prt(isp, ISP_LOGINFO,
   3425  1.39.2.6   bouyer 			    "Loop -> Point-to-Point mode");
   3426  1.39.2.2   bouyer 			break;
   3427  1.39.2.2   bouyer 		case ISP_CONN_BADLIP:
   3428  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGWARN,
   3429  1.39.2.6   bouyer 			    "Point-to-Point -> Loop mode (BAD LIP)");
   3430  1.39.2.2   bouyer 			break;
   3431  1.39.2.2   bouyer 		case ISP_CONN_FATAL:
   3432  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
   3433  1.39.2.2   bouyer 			isp_reinit(isp);
   3434  1.39.2.3   bouyer #ifdef	ISP_TARGET_MODE
   3435  1.39.2.3   bouyer 			isp_target_async(isp, bus, ASYNC_SYSTEM_ERROR);
   3436  1.39.2.3   bouyer #endif
   3437  1.39.2.2   bouyer 			/* no point continuing after this */
   3438  1.39.2.2   bouyer 			return (-1);
   3439  1.39.2.2   bouyer 		case ISP_CONN_LOOPBACK:
   3440  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGWARN,
   3441  1.39.2.2   bouyer 			    "Looped Back in Point-to-Point mode");
   3442  1.39.2.6   bouyer 			break;
   3443  1.39.2.6   bouyer 		default:
   3444  1.39.2.6   bouyer 			isp_prt(isp, ISP_LOGWARN,
   3445  1.39.2.6   bouyer 			    "Unknown connection mode (0x%x)", mbox);
   3446  1.39.2.6   bouyer 			break;
   3447  1.39.2.2   bouyer 		}
   3448  1.39.2.6   bouyer 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
   3449  1.39.2.6   bouyer 		isp->isp_sendmarker = 1;
   3450  1.39.2.6   bouyer 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
   3451  1.39.2.6   bouyer 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
   3452  1.39.2.2   bouyer 		break;
   3453  1.39.2.2   bouyer 
   3454      1.25   mjacob 	default:
   3455  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
   3456      1.25   mjacob 		break;
   3457      1.25   mjacob 	}
   3458      1.31   mjacob 	return (fast_post_handle);
   3459      1.25   mjacob }
   3460      1.25   mjacob 
   3461  1.39.2.2   bouyer /*
   3462  1.39.2.2   bouyer  * Handle other response entries. A pointer to the request queue output
   3463  1.39.2.2   bouyer  * index is here in case we want to eat several entries at once, although
   3464  1.39.2.2   bouyer  * this is not used currently.
   3465  1.39.2.2   bouyer  */
   3466  1.39.2.2   bouyer 
   3467      1.25   mjacob static int
   3468  1.39.2.7   bouyer isp_handle_other_response(struct ispsoftc *isp,
   3469  1.39.2.7   bouyer     ispstatusreq_t *sp, u_int16_t *optrp)
   3470      1.25   mjacob {
   3471      1.25   mjacob 	switch (sp->req_header.rqs_entry_type) {
   3472  1.39.2.2   bouyer 	case RQSTYPE_STATUS_CONT:
   3473  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
   3474  1.39.2.2   bouyer 		return (0);
   3475      1.38   mjacob 	case RQSTYPE_ATIO:
   3476  1.39.2.2   bouyer 	case RQSTYPE_CTIO:
   3477      1.29   mjacob 	case RQSTYPE_ENABLE_LUN:
   3478      1.29   mjacob 	case RQSTYPE_MODIFY_LUN:
   3479      1.38   mjacob 	case RQSTYPE_NOTIFY:
   3480      1.38   mjacob 	case RQSTYPE_NOTIFY_ACK:
   3481      1.38   mjacob 	case RQSTYPE_CTIO1:
   3482      1.29   mjacob 	case RQSTYPE_ATIO2:
   3483      1.38   mjacob 	case RQSTYPE_CTIO2:
   3484      1.38   mjacob 	case RQSTYPE_CTIO3:
   3485      1.38   mjacob #ifdef	ISP_TARGET_MODE
   3486  1.39.2.2   bouyer 		return (isp_target_notify(isp, sp, optrp));
   3487      1.29   mjacob #else
   3488  1.39.2.2   bouyer 		optrp = optrp;
   3489      1.38   mjacob 		/* FALLTHROUGH */
   3490      1.29   mjacob #endif
   3491      1.38   mjacob 	case RQSTYPE_REQUEST:
   3492      1.25   mjacob 	default:
   3493  1.39.2.6   bouyer 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, sp)) {
   3494  1.39.2.6   bouyer 			return (0);
   3495  1.39.2.6   bouyer 		}
   3496  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
   3497  1.39.2.2   bouyer 		    sp->req_header.rqs_entry_type);
   3498      1.29   mjacob 		return (-1);
   3499      1.29   mjacob 	}
   3500      1.29   mjacob }
   3501      1.29   mjacob 
   3502      1.23   mjacob static void
   3503  1.39.2.7   bouyer isp_parse_status(struct ispsoftc *isp, ispstatusreq_t *sp, XS_T *xs)
   3504       1.1      cgd {
   3505  1.39.2.2   bouyer 	switch (sp->req_completion_status & 0xff) {
   3506       1.1      cgd 	case RQCS_COMPLETE:
   3507  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3508  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_NOERROR);
   3509  1.39.2.2   bouyer 		}
   3510      1.23   mjacob 		return;
   3511      1.10   mjacob 
   3512       1.1      cgd 	case RQCS_INCOMPLETE:
   3513       1.1      cgd 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
   3514  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGDEBUG1,
   3515  1.39.2.2   bouyer 			    "Selection Timeout for %d.%d.%d",
   3516  1.39.2.3   bouyer 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3517  1.39.2.2   bouyer 			if (XS_NOERR(xs)) {
   3518  1.39.2.2   bouyer 				XS_SETERR(xs, HBA_SELTIMEOUT);
   3519  1.39.2.2   bouyer 			}
   3520      1.23   mjacob 			return;
   3521       1.1      cgd 		}
   3522  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3523  1.39.2.2   bouyer 		    "command incomplete for %d.%d.%d, state 0x%x",
   3524  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
   3525      1.25   mjacob 		    sp->req_state_flags);
   3526      1.25   mjacob 		break;
   3527      1.25   mjacob 
   3528      1.25   mjacob 	case RQCS_DMA_ERROR:
   3529  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
   3530  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3531       1.1      cgd 		break;
   3532      1.15   mjacob 
   3533      1.15   mjacob 	case RQCS_TRANSPORT_ERROR:
   3534  1.39.2.2   bouyer 	{
   3535  1.39.2.2   bouyer 		char buf[172];
   3536  1.39.2.2   bouyer 		buf[0] = 0;
   3537  1.39.2.2   bouyer 		STRNCAT(buf, "states=>", sizeof buf);
   3538  1.39.2.2   bouyer 		if (sp->req_state_flags & RQSF_GOT_BUS) {
   3539  1.39.2.2   bouyer 			STRNCAT(buf, " GOT_BUS", sizeof buf);
   3540  1.39.2.2   bouyer 		}
   3541  1.39.2.2   bouyer 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
   3542  1.39.2.2   bouyer 			STRNCAT(buf, " GOT_TGT", sizeof buf);
   3543  1.39.2.2   bouyer 		}
   3544  1.39.2.2   bouyer 		if (sp->req_state_flags & RQSF_SENT_CDB) {
   3545  1.39.2.2   bouyer 			STRNCAT(buf, " SENT_CDB", sizeof buf);
   3546  1.39.2.2   bouyer 		}
   3547  1.39.2.2   bouyer 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
   3548  1.39.2.2   bouyer 			STRNCAT(buf, " XFRD_DATA", sizeof buf);
   3549  1.39.2.2   bouyer 		}
   3550  1.39.2.2   bouyer 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
   3551  1.39.2.2   bouyer 			STRNCAT(buf, " GOT_STS", sizeof buf);
   3552  1.39.2.2   bouyer 		}
   3553  1.39.2.2   bouyer 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
   3554  1.39.2.2   bouyer 			STRNCAT(buf, " GOT_SNS", sizeof buf);
   3555  1.39.2.2   bouyer 		}
   3556  1.39.2.2   bouyer 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
   3557  1.39.2.2   bouyer 			STRNCAT(buf, " XFR_CMPLT", sizeof buf);
   3558  1.39.2.2   bouyer 		}
   3559  1.39.2.2   bouyer 		STRNCAT(buf, "\nstatus=>", sizeof buf);
   3560  1.39.2.2   bouyer 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
   3561  1.39.2.2   bouyer 			STRNCAT(buf, " Disconnect", sizeof buf);
   3562  1.39.2.2   bouyer 		}
   3563  1.39.2.2   bouyer 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
   3564  1.39.2.2   bouyer 			STRNCAT(buf, " Sync_xfr", sizeof buf);
   3565  1.39.2.2   bouyer 		}
   3566  1.39.2.2   bouyer 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
   3567  1.39.2.2   bouyer 			STRNCAT(buf, " Parity", sizeof buf);
   3568  1.39.2.2   bouyer 		}
   3569  1.39.2.2   bouyer 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
   3570  1.39.2.2   bouyer 			STRNCAT(buf, " Bus_Reset", sizeof buf);
   3571  1.39.2.2   bouyer 		}
   3572  1.39.2.2   bouyer 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
   3573  1.39.2.2   bouyer 			STRNCAT(buf, " Device_Reset", sizeof buf);
   3574  1.39.2.2   bouyer 		}
   3575  1.39.2.2   bouyer 		if (sp->req_status_flags & RQSTF_ABORTED) {
   3576  1.39.2.2   bouyer 			STRNCAT(buf, " Aborted", sizeof buf);
   3577  1.39.2.2   bouyer 		}
   3578  1.39.2.2   bouyer 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
   3579  1.39.2.2   bouyer 			STRNCAT(buf, " Timeout", sizeof buf);
   3580  1.39.2.2   bouyer 		}
   3581  1.39.2.2   bouyer 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
   3582  1.39.2.2   bouyer 			STRNCAT(buf, " Negotiation", sizeof buf);
   3583  1.39.2.2   bouyer 		}
   3584  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "%s", buf);
   3585  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
   3586  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
   3587      1.15   mjacob 		break;
   3588  1.39.2.2   bouyer 	}
   3589      1.25   mjacob 	case RQCS_RESET_OCCURRED:
   3590  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN,
   3591  1.39.2.2   bouyer 		    "bus reset destroyed command for %d.%d.%d",
   3592  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3593  1.39.2.2   bouyer 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3594  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3595  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_BUSRESET);
   3596  1.39.2.2   bouyer 		}
   3597      1.25   mjacob 		return;
   3598      1.25   mjacob 
   3599      1.25   mjacob 	case RQCS_ABORTED:
   3600  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
   3601  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3602  1.39.2.2   bouyer 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
   3603  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3604  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_ABORTED);
   3605  1.39.2.2   bouyer 		}
   3606      1.25   mjacob 		return;
   3607      1.25   mjacob 
   3608      1.25   mjacob 	case RQCS_TIMEOUT:
   3609  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
   3610  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3611  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3612  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_CMDTIMEOUT);
   3613  1.39.2.2   bouyer 		}
   3614      1.25   mjacob 		return;
   3615      1.25   mjacob 
   3616      1.10   mjacob 	case RQCS_DATA_OVERRUN:
   3617  1.39.2.2   bouyer 		XS_RESID(xs) = sp->req_resid;
   3618  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
   3619  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3620  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3621  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_DATAOVR);
   3622      1.10   mjacob 		}
   3623      1.23   mjacob 		return;
   3624      1.10   mjacob 
   3625      1.25   mjacob 	case RQCS_COMMAND_OVERRUN:
   3626  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3627  1.39.2.2   bouyer 		    "command overrun for command on %d.%d.%d",
   3628  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3629      1.25   mjacob 		break;
   3630      1.25   mjacob 
   3631      1.25   mjacob 	case RQCS_STATUS_OVERRUN:
   3632  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3633  1.39.2.2   bouyer 		    "status overrun for command on %d.%d.%d",
   3634  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3635      1.25   mjacob 		break;
   3636      1.25   mjacob 
   3637      1.25   mjacob 	case RQCS_BAD_MESSAGE:
   3638  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3639  1.39.2.2   bouyer 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
   3640  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3641      1.25   mjacob 		break;
   3642      1.25   mjacob 
   3643      1.25   mjacob 	case RQCS_NO_MESSAGE_OUT:
   3644  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3645  1.39.2.2   bouyer 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
   3646  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3647      1.25   mjacob 		break;
   3648      1.25   mjacob 
   3649      1.25   mjacob 	case RQCS_EXT_ID_FAILED:
   3650  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
   3651  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3652      1.25   mjacob 		break;
   3653      1.25   mjacob 
   3654      1.25   mjacob 	case RQCS_IDE_MSG_FAILED:
   3655  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3656  1.39.2.2   bouyer 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
   3657  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3658      1.25   mjacob 		break;
   3659      1.25   mjacob 
   3660      1.25   mjacob 	case RQCS_ABORT_MSG_FAILED:
   3661  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
   3662  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3663      1.25   mjacob 		break;
   3664      1.25   mjacob 
   3665      1.25   mjacob 	case RQCS_REJECT_MSG_FAILED:
   3666  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
   3667  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3668      1.25   mjacob 		break;
   3669      1.25   mjacob 
   3670      1.25   mjacob 	case RQCS_NOP_MSG_FAILED:
   3671  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
   3672  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3673      1.25   mjacob 		break;
   3674      1.25   mjacob 
   3675      1.25   mjacob 	case RQCS_PARITY_ERROR_MSG_FAILED:
   3676  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3677  1.39.2.2   bouyer 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
   3678  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3679      1.25   mjacob 		break;
   3680      1.25   mjacob 
   3681      1.25   mjacob 	case RQCS_DEVICE_RESET_MSG_FAILED:
   3682  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN,
   3683  1.39.2.2   bouyer 		    "BUS DEVICE RESET rejected by %d.%d.%d",
   3684  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3685      1.25   mjacob 		break;
   3686      1.25   mjacob 
   3687      1.25   mjacob 	case RQCS_ID_MSG_FAILED:
   3688  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
   3689  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3690      1.25   mjacob 		break;
   3691      1.25   mjacob 
   3692      1.25   mjacob 	case RQCS_UNEXP_BUS_FREE:
   3693  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
   3694  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3695      1.25   mjacob 		break;
   3696      1.25   mjacob 
   3697       1.1      cgd 	case RQCS_DATA_UNDERRUN:
   3698  1.39.2.2   bouyer 		XS_RESID(xs) = sp->req_resid;
   3699  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3700  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_NOERROR);
   3701      1.10   mjacob 		}
   3702      1.23   mjacob 		return;
   3703      1.10   mjacob 
   3704      1.25   mjacob 	case RQCS_XACT_ERR1:
   3705  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
   3706  1.39.2.2   bouyer 		    XS_TGT(xs), XS_LUN(xs));
   3707      1.25   mjacob 		break;
   3708      1.25   mjacob 
   3709      1.25   mjacob 	case RQCS_XACT_ERR2:
   3710  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, xact2,
   3711  1.39.2.2   bouyer 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
   3712      1.25   mjacob 		break;
   3713      1.25   mjacob 
   3714      1.25   mjacob 	case RQCS_XACT_ERR3:
   3715  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, xact3, XS_TGT(xs),
   3716  1.39.2.2   bouyer 		    XS_LUN(xs), XS_CHANNEL(xs));
   3717      1.25   mjacob 		break;
   3718      1.25   mjacob 
   3719      1.25   mjacob 	case RQCS_BAD_ENTRY:
   3720  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
   3721      1.25   mjacob 		break;
   3722      1.25   mjacob 
   3723      1.25   mjacob 	case RQCS_QUEUE_FULL:
   3724  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGDEBUG1,
   3725  1.39.2.2   bouyer 		    "internal queues full for %d.%d.%d status 0x%x", XS_TGT(xs),
   3726  1.39.2.2   bouyer 		    XS_LUN(xs), XS_CHANNEL(xs), *XS_STSP(xs));
   3727  1.39.2.2   bouyer 		/*
   3728  1.39.2.2   bouyer 		 * If QFULL or some other status byte is set, then this
   3729  1.39.2.2   bouyer 		 * isn't an error, per se.
   3730  1.39.2.2   bouyer 		 */
   3731  1.39.2.2   bouyer 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
   3732  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_NOERROR);
   3733  1.39.2.2   bouyer 			return;
   3734  1.39.2.2   bouyer 		}
   3735  1.39.2.2   bouyer 		break;
   3736      1.25   mjacob 
   3737      1.25   mjacob 	case RQCS_PHASE_SKIPPED:
   3738  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, pskip,
   3739  1.39.2.2   bouyer 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3740      1.25   mjacob 		break;
   3741      1.25   mjacob 
   3742      1.25   mjacob 	case RQCS_ARQS_FAILED:
   3743  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3744  1.39.2.2   bouyer 		    "Auto Request Sense failed for %d.%d.%d",
   3745  1.39.2.2   bouyer 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
   3746  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3747  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_ARQFAIL);
   3748  1.39.2.2   bouyer 		}
   3749      1.23   mjacob 		return;
   3750      1.10   mjacob 
   3751      1.25   mjacob 	case RQCS_WIDE_FAILED:
   3752  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3753  1.39.2.2   bouyer 		    "Wide Negotiation failed for %d.%d.%d",
   3754  1.39.2.2   bouyer 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3755      1.36   mjacob 		if (IS_SCSI(isp)) {
   3756      1.25   mjacob 			sdparam *sdp = isp->isp_param;
   3757      1.36   mjacob 			sdp += XS_CHANNEL(xs);
   3758      1.31   mjacob 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
   3759      1.25   mjacob 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3760  1.39.2.2   bouyer 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3761  1.39.2.2   bouyer 		}
   3762  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3763  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_NOERROR);
   3764      1.25   mjacob 		}
   3765      1.23   mjacob 		return;
   3766      1.10   mjacob 
   3767      1.25   mjacob 	case RQCS_SYNCXFER_FAILED:
   3768  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3769  1.39.2.2   bouyer 		    "SDTR Message failed for target %d.%d.%d",
   3770  1.39.2.2   bouyer 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3771      1.36   mjacob 		if (IS_SCSI(isp)) {
   3772      1.25   mjacob 			sdparam *sdp = isp->isp_param;
   3773      1.36   mjacob 			sdp += XS_CHANNEL(xs);
   3774      1.31   mjacob 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
   3775      1.25   mjacob 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
   3776  1.39.2.2   bouyer 			isp->isp_update |= (1 << XS_CHANNEL(xs));
   3777      1.25   mjacob 		}
   3778      1.25   mjacob 		break;
   3779      1.25   mjacob 
   3780      1.25   mjacob 	case RQCS_LVD_BUSERR:
   3781  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR,
   3782  1.39.2.2   bouyer 		    "Bad LVD condition while talking to %d.%d.%d",
   3783  1.39.2.2   bouyer 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
   3784      1.25   mjacob 		break;
   3785      1.10   mjacob 
   3786      1.10   mjacob 	case RQCS_PORT_UNAVAILABLE:
   3787      1.10   mjacob 		/*
   3788      1.10   mjacob 		 * No such port on the loop. Moral equivalent of SELTIMEO
   3789      1.10   mjacob 		 */
   3790  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO,
   3791  1.39.2.2   bouyer 		    "Port Unavailable for target %d", XS_TGT(xs));
   3792  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3793  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3794  1.39.2.2   bouyer 		}
   3795      1.23   mjacob 		return;
   3796      1.10   mjacob 
   3797      1.10   mjacob 	case RQCS_PORT_LOGGED_OUT:
   3798      1.27   mjacob 		/*
   3799      1.27   mjacob 		 * It was there (maybe)- treat as a selection timeout.
   3800      1.27   mjacob 		 */
   3801  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGINFO,
   3802  1.39.2.2   bouyer 		    "port logout for target %d", XS_TGT(xs));
   3803  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3804  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3805  1.39.2.2   bouyer 		}
   3806      1.27   mjacob 		return;
   3807      1.10   mjacob 
   3808      1.10   mjacob 	case RQCS_PORT_CHANGED:
   3809  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN,
   3810  1.39.2.2   bouyer 		    "port changed for target %d", XS_TGT(xs));
   3811  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3812  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_SELTIMEOUT);
   3813  1.39.2.2   bouyer 		}
   3814  1.39.2.2   bouyer 		return;
   3815      1.10   mjacob 
   3816      1.10   mjacob 	case RQCS_PORT_BUSY:
   3817  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN,
   3818  1.39.2.2   bouyer 		    "port busy for target %d", XS_TGT(xs));
   3819  1.39.2.2   bouyer 		if (XS_NOERR(xs)) {
   3820  1.39.2.2   bouyer 			XS_SETERR(xs, HBA_TGTBSY);
   3821  1.39.2.2   bouyer 		}
   3822      1.23   mjacob 		return;
   3823      1.10   mjacob 
   3824       1.1      cgd 	default:
   3825  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
   3826      1.34   mjacob 		    sp->req_completion_status);
   3827       1.1      cgd 		break;
   3828       1.1      cgd 	}
   3829  1.39.2.2   bouyer 	if (XS_NOERR(xs)) {
   3830  1.39.2.2   bouyer 		XS_SETERR(xs, HBA_BOTCH);
   3831  1.39.2.2   bouyer 	}
   3832       1.1      cgd }
   3833       1.1      cgd 
   3834      1.33   mjacob static void
   3835  1.39.2.7   bouyer isp_fastpost_complete(struct ispsoftc *isp, u_int16_t fph)
   3836      1.33   mjacob {
   3837  1.39.2.2   bouyer 	XS_T *xs;
   3838      1.33   mjacob 
   3839  1.39.2.7   bouyer 	if (fph == 0) {
   3840      1.33   mjacob 		return;
   3841      1.38   mjacob 	}
   3842      1.38   mjacob 	xs = isp_find_xs(isp, fph);
   3843      1.33   mjacob 	if (xs == NULL) {
   3844  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN,
   3845  1.39.2.2   bouyer 		    "Command for fast post handle 0x%x not found", fph);
   3846      1.33   mjacob 		return;
   3847      1.33   mjacob 	}
   3848      1.38   mjacob 	isp_destroy_handle(isp, fph);
   3849      1.38   mjacob 
   3850      1.33   mjacob 	/*
   3851      1.33   mjacob 	 * Since we don't have a result queue entry item,
   3852      1.33   mjacob 	 * we must believe that SCSI status is zero and
   3853      1.33   mjacob 	 * that all data transferred.
   3854      1.33   mjacob 	 */
   3855  1.39.2.2   bouyer 	XS_SET_STATE_STAT(isp, xs, NULL);
   3856      1.33   mjacob 	XS_RESID(xs) = 0;
   3857  1.39.2.2   bouyer 	*XS_STSP(xs) = SCSI_GOOD;
   3858      1.33   mjacob 	if (XS_XFRLEN(xs)) {
   3859      1.38   mjacob 		ISP_DMAFREE(isp, xs, fph);
   3860      1.33   mjacob 	}
   3861      1.38   mjacob 	if (isp->isp_nactive)
   3862      1.38   mjacob 		isp->isp_nactive--;
   3863  1.39.2.2   bouyer 	isp_done(xs);
   3864      1.33   mjacob }
   3865      1.33   mjacob 
   3866  1.39.2.2   bouyer #define	HIBYT(x)			((x) >> 0x8)
   3867  1.39.2.2   bouyer #define	LOBYT(x)			((x)  & 0xff)
   3868  1.39.2.2   bouyer #define	ISPOPMAP(a, b)			(((a) << 8) | (b))
   3869  1.39.2.2   bouyer static u_int16_t mbpscsi[] = {
   3870  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   3871  1.39.2.2   bouyer 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   3872  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   3873  1.39.2.2   bouyer 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   3874  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   3875  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   3876  1.39.2.2   bouyer 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   3877  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   3878  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   3879  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
   3880  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
   3881  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   3882  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   3883  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   3884  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   3885  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   3886  1.39.2.2   bouyer 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   3887  1.39.2.2   bouyer 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
   3888  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
   3889  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   3890  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
   3891  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
   3892  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
   3893  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
   3894  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
   3895  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
   3896  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
   3897  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   3898  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
   3899  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   3900  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   3901  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   3902  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
   3903  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
   3904  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   3905  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
   3906  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
   3907  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
   3908  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
   3909  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
   3910  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
   3911  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
   3912  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
   3913  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   3914  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   3915  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   3916  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   3917  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   3918  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
   3919  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
   3920  1.39.2.2   bouyer 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   3921  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
   3922  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
   3923  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
   3924  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
   3925  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
   3926  1.39.2.2   bouyer 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
   3927  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
   3928  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
   3929  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   3930  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   3931  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   3932  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   3933  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   3934  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
   3935  1.39.2.2   bouyer 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
   3936  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
   3937  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
   3938  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   3939  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
   3940  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
   3941  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
   3942  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
   3943  1.39.2.2   bouyer 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
   3944  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
   3945  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
   3946  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   3947  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   3948  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   3949  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   3950  1.39.2.2   bouyer 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
   3951  1.39.2.2   bouyer 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
   3952  1.39.2.2   bouyer 	ISPOPMAP(0xdf, 0xdf),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
   3953  1.39.2.2   bouyer 	ISPOPMAP(0xff, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
   3954  1.39.2.2   bouyer 	ISPOPMAP(0xcf, 0xff),	/* 0x54: EXECUTE IOCB A64 */
   3955  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x01),	/* 0x55: ENABLE TARGET MODE */
   3956  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   3957  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   3958  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   3959  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   3960  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
   3961  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
   3962  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
   3963  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
   3964       1.1      cgd };
   3965  1.39.2.2   bouyer 
   3966  1.39.2.7   bouyer #ifndef	ISP_STRIPPED
   3967  1.39.2.2   bouyer static char *scsi_mbcmd_names[] = {
   3968  1.39.2.2   bouyer 	"NO-OP",
   3969  1.39.2.2   bouyer 	"LOAD RAM",
   3970  1.39.2.2   bouyer 	"EXEC FIRMWARE",
   3971  1.39.2.2   bouyer 	"DUMP RAM",
   3972  1.39.2.2   bouyer 	"WRITE RAM WORD",
   3973  1.39.2.2   bouyer 	"READ RAM WORD",
   3974  1.39.2.2   bouyer 	"MAILBOX REG TEST",
   3975  1.39.2.2   bouyer 	"VERIFY CHECKSUM",
   3976  1.39.2.2   bouyer 	"ABOUT FIRMWARE",
   3977  1.39.2.2   bouyer 	NULL,
   3978  1.39.2.2   bouyer 	NULL,
   3979  1.39.2.2   bouyer 	NULL,
   3980  1.39.2.2   bouyer 	NULL,
   3981  1.39.2.2   bouyer 	NULL,
   3982  1.39.2.2   bouyer 	"CHECK FIRMWARE",
   3983  1.39.2.2   bouyer 	NULL,
   3984  1.39.2.2   bouyer 	"INIT REQUEST QUEUE",
   3985  1.39.2.2   bouyer 	"INIT RESULT QUEUE",
   3986  1.39.2.2   bouyer 	"EXECUTE IOCB",
   3987  1.39.2.2   bouyer 	"WAKE UP",
   3988  1.39.2.2   bouyer 	"STOP FIRMWARE",
   3989  1.39.2.2   bouyer 	"ABORT",
   3990  1.39.2.2   bouyer 	"ABORT DEVICE",
   3991  1.39.2.2   bouyer 	"ABORT TARGET",
   3992  1.39.2.2   bouyer 	"BUS RESET",
   3993  1.39.2.2   bouyer 	"STOP QUEUE",
   3994  1.39.2.2   bouyer 	"START QUEUE",
   3995  1.39.2.2   bouyer 	"SINGLE STEP QUEUE",
   3996  1.39.2.2   bouyer 	"ABORT QUEUE",
   3997  1.39.2.2   bouyer 	"GET DEV QUEUE STATUS",
   3998  1.39.2.2   bouyer 	NULL,
   3999  1.39.2.2   bouyer 	"GET FIRMWARE STATUS",
   4000  1.39.2.2   bouyer 	"GET INIT SCSI ID",
   4001  1.39.2.2   bouyer 	"GET SELECT TIMEOUT",
   4002  1.39.2.2   bouyer 	"GET RETRY COUNT",
   4003  1.39.2.2   bouyer 	"GET TAG AGE LIMIT",
   4004  1.39.2.2   bouyer 	"GET CLOCK RATE",
   4005  1.39.2.2   bouyer 	"GET ACT NEG STATE",
   4006  1.39.2.2   bouyer 	"GET ASYNC DATA SETUP TIME",
   4007  1.39.2.2   bouyer 	"GET PCI PARAMS",
   4008  1.39.2.2   bouyer 	"GET TARGET PARAMS",
   4009  1.39.2.2   bouyer 	"GET DEV QUEUE PARAMS",
   4010  1.39.2.2   bouyer 	"GET RESET DELAY PARAMS",
   4011  1.39.2.2   bouyer 	NULL,
   4012  1.39.2.2   bouyer 	NULL,
   4013  1.39.2.2   bouyer 	NULL,
   4014  1.39.2.2   bouyer 	NULL,
   4015  1.39.2.2   bouyer 	NULL,
   4016  1.39.2.2   bouyer 	"SET INIT SCSI ID",
   4017  1.39.2.2   bouyer 	"SET SELECT TIMEOUT",
   4018  1.39.2.2   bouyer 	"SET RETRY COUNT",
   4019  1.39.2.2   bouyer 	"SET TAG AGE LIMIT",
   4020  1.39.2.2   bouyer 	"SET CLOCK RATE",
   4021  1.39.2.2   bouyer 	"SET ACT NEG STATE",
   4022  1.39.2.2   bouyer 	"SET ASYNC DATA SETUP TIME",
   4023  1.39.2.2   bouyer 	"SET PCI CONTROL PARAMS",
   4024  1.39.2.2   bouyer 	"SET TARGET PARAMS",
   4025  1.39.2.2   bouyer 	"SET DEV QUEUE PARAMS",
   4026  1.39.2.2   bouyer 	"SET RESET DELAY PARAMS",
   4027  1.39.2.2   bouyer 	NULL,
   4028  1.39.2.2   bouyer 	NULL,
   4029  1.39.2.2   bouyer 	NULL,
   4030  1.39.2.2   bouyer 	NULL,
   4031  1.39.2.2   bouyer 	NULL,
   4032  1.39.2.2   bouyer 	"RETURN BIOS BLOCK ADDR",
   4033  1.39.2.2   bouyer 	"WRITE FOUR RAM WORDS",
   4034  1.39.2.2   bouyer 	"EXEC BIOS IOCB",
   4035  1.39.2.2   bouyer 	NULL,
   4036  1.39.2.2   bouyer 	NULL,
   4037  1.39.2.2   bouyer 	"SET SYSTEM PARAMETER",
   4038  1.39.2.2   bouyer 	"GET SYSTEM PARAMETER",
   4039  1.39.2.2   bouyer 	NULL,
   4040  1.39.2.2   bouyer 	"GET SCAM CONFIGURATION",
   4041  1.39.2.2   bouyer 	"SET SCAM CONFIGURATION",
   4042  1.39.2.2   bouyer 	"SET FIRMWARE FEATURES",
   4043  1.39.2.2   bouyer 	"GET FIRMWARE FEATURES",
   4044  1.39.2.2   bouyer 	NULL,
   4045  1.39.2.2   bouyer 	NULL,
   4046  1.39.2.2   bouyer 	NULL,
   4047  1.39.2.2   bouyer 	NULL,
   4048  1.39.2.2   bouyer 	"LOAD RAM A64",
   4049  1.39.2.2   bouyer 	"DUMP RAM A64",
   4050  1.39.2.2   bouyer 	"INITIALIZE REQUEST QUEUE A64",
   4051  1.39.2.2   bouyer 	"INITIALIZE RESPONSE QUEUE A64",
   4052  1.39.2.2   bouyer 	"EXECUTE IOCB A64",
   4053  1.39.2.2   bouyer 	"ENABLE TARGET MODE",
   4054  1.39.2.2   bouyer 	NULL,
   4055  1.39.2.2   bouyer 	NULL,
   4056  1.39.2.2   bouyer 	NULL,
   4057  1.39.2.2   bouyer 	NULL,
   4058  1.39.2.2   bouyer 	"SET DATA OVERRUN RECOVERY MODE",
   4059  1.39.2.2   bouyer 	"GET DATA OVERRUN RECOVERY MODE",
   4060  1.39.2.2   bouyer 	"SET HOST DATA",
   4061  1.39.2.2   bouyer 	"GET NOST DATA",
   4062  1.39.2.2   bouyer };
   4063  1.39.2.2   bouyer #endif
   4064  1.39.2.2   bouyer 
   4065  1.39.2.2   bouyer static u_int16_t mbpfc[] = {
   4066  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
   4067  1.39.2.2   bouyer 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
   4068  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
   4069  1.39.2.6   bouyer 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
   4070  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
   4071  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
   4072  1.39.2.2   bouyer 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
   4073  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
   4074  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
   4075  1.39.2.2   bouyer 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
   4076  1.39.2.2   bouyer 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
   4077  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
   4078  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
   4079  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
   4080  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
   4081  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
   4082  1.39.2.2   bouyer 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
   4083  1.39.2.2   bouyer 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
   4084  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
   4085  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
   4086  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
   4087  1.39.2.2   bouyer 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
   4088  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
   4089  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
   4090  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
   4091  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
   4092  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
   4093  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
   4094  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
   4095  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
   4096  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
   4097  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
   4098  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
   4099  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
   4100  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
   4101  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
   4102  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
   4103  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
   4104  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
   4105  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
   4106  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x1),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
   4107  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
   4108  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
   4109  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
   4110  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
   4111  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
   4112  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
   4113  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
   4114  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
   4115  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
   4116  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
   4117  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
   4118  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
   4119  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
   4120  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
   4121  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
   4122  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
   4123  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
   4124  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
   4125  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
   4126  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
   4127  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
   4128  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
   4129  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
   4130  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
   4131  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
   4132  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
   4133  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
   4134  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
   4135  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
   4136  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
   4137  1.39.2.2   bouyer 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
   4138  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
   4139  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
   4140  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
   4141  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
   4142  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
   4143  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
   4144  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
   4145  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
   4146  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
   4147  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
   4148  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
   4149  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
   4150  1.39.2.2   bouyer 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
   4151  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
   4152  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
   4153  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
   4154  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
   4155  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
   4156  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
   4157  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x5b: */
   4158  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x5c: */
   4159  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x5d: */
   4160  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
   4161  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
   4162  1.39.2.2   bouyer 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
   4163  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
   4164  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
   4165  1.39.2.2   bouyer 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
   4166  1.39.2.2   bouyer 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
   4167  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
   4168  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
   4169  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
   4170  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
   4171  1.39.2.2   bouyer 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
   4172  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
   4173  1.39.2.2   bouyer 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
   4174  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
   4175  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
   4176  1.39.2.2   bouyer 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
   4177  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
   4178  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
   4179  1.39.2.2   bouyer 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
   4180  1.39.2.2   bouyer 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
   4181  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
   4182  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
   4183  1.39.2.2   bouyer 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
   4184  1.39.2.2   bouyer 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
   4185  1.39.2.2   bouyer 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
   4186  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
   4187  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
   4188  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
   4189  1.39.2.2   bouyer 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
   4190  1.39.2.2   bouyer 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
   4191  1.39.2.2   bouyer 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
   4192  1.39.2.2   bouyer 	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
   4193  1.39.2.2   bouyer };
   4194  1.39.2.2   bouyer 
   4195  1.39.2.2   bouyer #ifndef	ISP_STRIPPED
   4196  1.39.2.2   bouyer static char *fc_mbcmd_names[] = {
   4197  1.39.2.2   bouyer 	"NO-OP",
   4198  1.39.2.2   bouyer 	"LOAD RAM",
   4199  1.39.2.2   bouyer 	"EXEC FIRMWARE",
   4200  1.39.2.2   bouyer 	"DUMP RAM",
   4201  1.39.2.2   bouyer 	"WRITE RAM WORD",
   4202  1.39.2.2   bouyer 	"READ RAM WORD",
   4203  1.39.2.2   bouyer 	"MAILBOX REG TEST",
   4204  1.39.2.2   bouyer 	"VERIFY CHECKSUM",
   4205  1.39.2.2   bouyer 	"ABOUT FIRMWARE",
   4206  1.39.2.2   bouyer 	"LOAD RAM",
   4207  1.39.2.2   bouyer 	"DUMP RAM",
   4208  1.39.2.2   bouyer 	NULL,
   4209  1.39.2.2   bouyer 	NULL,
   4210  1.39.2.2   bouyer 	NULL,
   4211  1.39.2.2   bouyer 	"CHECK FIRMWARE",
   4212  1.39.2.2   bouyer 	NULL,
   4213  1.39.2.2   bouyer 	"INIT REQUEST QUEUE",
   4214  1.39.2.2   bouyer 	"INIT RESULT QUEUE",
   4215  1.39.2.2   bouyer 	"EXECUTE IOCB",
   4216  1.39.2.2   bouyer 	"WAKE UP",
   4217  1.39.2.2   bouyer 	"STOP FIRMWARE",
   4218  1.39.2.2   bouyer 	"ABORT",
   4219  1.39.2.2   bouyer 	"ABORT DEVICE",
   4220  1.39.2.2   bouyer 	"ABORT TARGET",
   4221  1.39.2.2   bouyer 	"BUS RESET",
   4222  1.39.2.2   bouyer 	"STOP QUEUE",
   4223  1.39.2.2   bouyer 	"START QUEUE",
   4224  1.39.2.2   bouyer 	"SINGLE STEP QUEUE",
   4225  1.39.2.2   bouyer 	"ABORT QUEUE",
   4226  1.39.2.2   bouyer 	"GET DEV QUEUE STATUS",
   4227  1.39.2.2   bouyer 	NULL,
   4228  1.39.2.2   bouyer 	"GET FIRMWARE STATUS",
   4229  1.39.2.2   bouyer 	"GET LOOP ID",
   4230  1.39.2.2   bouyer 	NULL,
   4231  1.39.2.2   bouyer 	"GET RETRY COUNT",
   4232  1.39.2.2   bouyer 	NULL,
   4233  1.39.2.2   bouyer 	NULL,
   4234  1.39.2.2   bouyer 	NULL,
   4235  1.39.2.2   bouyer 	NULL,
   4236  1.39.2.2   bouyer 	NULL,
   4237  1.39.2.2   bouyer 	"GET FIRMWARE OPTIONS",
   4238  1.39.2.2   bouyer 	"GET PORT QUEUE PARAMS",
   4239  1.39.2.2   bouyer 	NULL,
   4240  1.39.2.2   bouyer 	NULL,
   4241  1.39.2.2   bouyer 	NULL,
   4242  1.39.2.2   bouyer 	NULL,
   4243  1.39.2.2   bouyer 	NULL,
   4244  1.39.2.2   bouyer 	NULL,
   4245  1.39.2.2   bouyer 	NULL,
   4246  1.39.2.2   bouyer 	NULL,
   4247  1.39.2.2   bouyer 	"SET RETRY COUNT",
   4248  1.39.2.2   bouyer 	NULL,
   4249  1.39.2.2   bouyer 	NULL,
   4250  1.39.2.2   bouyer 	NULL,
   4251  1.39.2.2   bouyer 	NULL,
   4252  1.39.2.2   bouyer 	NULL,
   4253  1.39.2.2   bouyer 	"SET FIRMWARE OPTIONS",
   4254  1.39.2.2   bouyer 	"SET PORT QUEUE PARAMS",
   4255  1.39.2.2   bouyer 	NULL,
   4256  1.39.2.2   bouyer 	NULL,
   4257  1.39.2.2   bouyer 	NULL,
   4258  1.39.2.2   bouyer 	NULL,
   4259  1.39.2.2   bouyer 	NULL,
   4260  1.39.2.2   bouyer 	NULL,
   4261  1.39.2.2   bouyer 	"LOOP PORT BYPASS",
   4262  1.39.2.2   bouyer 	"LOOP PORT ENABLE",
   4263  1.39.2.2   bouyer 	"GET RESOURCE COUNTS",
   4264  1.39.2.2   bouyer 	"REQUEST NON PARTICIPATING MODE",
   4265  1.39.2.2   bouyer 	NULL,
   4266  1.39.2.2   bouyer 	NULL,
   4267  1.39.2.2   bouyer 	NULL,
   4268  1.39.2.2   bouyer 	"GET PORT DATABASE,, ENHANCED",
   4269  1.39.2.2   bouyer 	NULL,
   4270  1.39.2.2   bouyer 	NULL,
   4271  1.39.2.2   bouyer 	NULL,
   4272  1.39.2.2   bouyer 	NULL,
   4273  1.39.2.2   bouyer 	NULL,
   4274  1.39.2.2   bouyer 	NULL,
   4275  1.39.2.2   bouyer 	NULL,
   4276  1.39.2.2   bouyer 	NULL,
   4277  1.39.2.2   bouyer 	NULL,
   4278  1.39.2.2   bouyer 	NULL,
   4279  1.39.2.2   bouyer 	NULL,
   4280  1.39.2.2   bouyer 	NULL,
   4281  1.39.2.2   bouyer 	"EXECUTE IOCB A64",
   4282  1.39.2.2   bouyer 	NULL,
   4283  1.39.2.2   bouyer 	NULL,
   4284  1.39.2.2   bouyer 	NULL,
   4285  1.39.2.2   bouyer 	NULL,
   4286  1.39.2.2   bouyer 	NULL,
   4287  1.39.2.2   bouyer 	NULL,
   4288  1.39.2.2   bouyer 	NULL,
   4289  1.39.2.2   bouyer 	NULL,
   4290  1.39.2.2   bouyer 	NULL,
   4291  1.39.2.2   bouyer 	NULL,
   4292  1.39.2.2   bouyer 	NULL,
   4293  1.39.2.2   bouyer 	"INIT FIRMWARE",
   4294  1.39.2.2   bouyer 	NULL,
   4295  1.39.2.2   bouyer 	"INIT LIP",
   4296  1.39.2.2   bouyer 	"GET FC-AL POSITION MAP",
   4297  1.39.2.2   bouyer 	"GET PORT DATABASE",
   4298  1.39.2.2   bouyer 	"CLEAR ACA",
   4299  1.39.2.2   bouyer 	"TARGET RESET",
   4300  1.39.2.2   bouyer 	"CLEAR TASK SET",
   4301  1.39.2.2   bouyer 	"ABORT TASK SET",
   4302  1.39.2.2   bouyer 	"GET FW STATE",
   4303  1.39.2.2   bouyer 	"GET PORT NAME",
   4304  1.39.2.2   bouyer 	"GET LINK STATUS",
   4305  1.39.2.2   bouyer 	"INIT LIP RESET",
   4306  1.39.2.2   bouyer 	NULL,
   4307  1.39.2.2   bouyer 	"SEND SNS",
   4308  1.39.2.2   bouyer 	"FABRIC LOGIN",
   4309  1.39.2.2   bouyer 	"SEND CHANGE REQUEST",
   4310  1.39.2.2   bouyer 	"FABRIC LOGOUT",
   4311  1.39.2.2   bouyer 	"INIT LIP LOGIN",
   4312  1.39.2.2   bouyer 	NULL,
   4313  1.39.2.2   bouyer 	"LOGIN LOOP PORT",
   4314  1.39.2.2   bouyer 	"GET PORT/NODE NAME LIST",
   4315  1.39.2.2   bouyer 	"SET VENDOR ID",
   4316  1.39.2.2   bouyer 	"INITIALIZE IP MAILBOX",
   4317  1.39.2.2   bouyer 	NULL,
   4318  1.39.2.2   bouyer 	NULL,
   4319  1.39.2.2   bouyer 	NULL,
   4320  1.39.2.2   bouyer 	NULL,
   4321  1.39.2.2   bouyer 	"Get ID List",
   4322  1.39.2.2   bouyer 	"SEND LFA",
   4323  1.39.2.2   bouyer 	"Lun RESET"
   4324  1.39.2.2   bouyer };
   4325  1.39.2.2   bouyer #endif
   4326       1.1      cgd 
   4327      1.10   mjacob static void
   4328  1.39.2.7   bouyer isp_mboxcmd(struct ispsoftc *isp, mbreg_t *mbp, int logmask)
   4329       1.1      cgd {
   4330  1.39.2.2   bouyer 	char *cname, *xname, tname[16], mname[16];
   4331  1.39.2.2   bouyer 	unsigned int lim, ibits, obits, box, opcode;
   4332  1.39.2.2   bouyer 	u_int16_t *mcp;
   4333  1.39.2.2   bouyer 
   4334  1.39.2.2   bouyer 	if (IS_FC(isp)) {
   4335  1.39.2.2   bouyer 		mcp = mbpfc;
   4336  1.39.2.2   bouyer 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
   4337  1.39.2.2   bouyer 	} else {
   4338  1.39.2.2   bouyer 		mcp = mbpscsi;
   4339  1.39.2.2   bouyer 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
   4340       1.1      cgd 	}
   4341       1.1      cgd 
   4342  1.39.2.2   bouyer 	if ((opcode = mbp->param[0]) >= lim) {
   4343  1.39.2.2   bouyer 		mbp->param[0] = MBOX_INVALID_COMMAND;
   4344  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
   4345      1.10   mjacob 		return;
   4346       1.1      cgd 	}
   4347       1.1      cgd 
   4348  1.39.2.2   bouyer 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   4349  1.39.2.2   bouyer 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
   4350      1.10   mjacob 
   4351  1.39.2.2   bouyer 	if (ibits == 0 && obits == 0) {
   4352  1.39.2.2   bouyer 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
   4353  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
   4354  1.39.2.2   bouyer 		return;
   4355      1.28   mjacob 	}
   4356      1.24   mjacob 
   4357      1.33   mjacob 	/*
   4358  1.39.2.2   bouyer 	 * Get exclusive usage of mailbox registers.
   4359      1.33   mjacob 	 */
   4360  1.39.2.2   bouyer 	MBOX_ACQUIRE(isp);
   4361      1.33   mjacob 
   4362  1.39.2.2   bouyer 	for (box = 0; box < MAX_MAILBOX; box++) {
   4363  1.39.2.2   bouyer 		if (ibits & (1 << box)) {
   4364  1.39.2.2   bouyer 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
   4365      1.34   mjacob 		}
   4366  1.39.2.2   bouyer 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
   4367       1.1      cgd 	}
   4368       1.1      cgd 
   4369  1.39.2.6   bouyer 	isp->isp_lastmbxcmd = opcode;
   4370  1.39.2.6   bouyer 
   4371       1.1      cgd 	/*
   4372  1.39.2.2   bouyer 	 * We assume that we can't overwrite a previous command.
   4373       1.1      cgd 	 */
   4374  1.39.2.2   bouyer 	isp->isp_mboxbsy = obits;
   4375       1.1      cgd 
   4376       1.1      cgd 	/*
   4377       1.1      cgd 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
   4378       1.1      cgd 	 */
   4379       1.1      cgd 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
   4380       1.1      cgd 
   4381      1.33   mjacob 	/*
   4382  1.39.2.2   bouyer 	 * While we haven't finished the command, spin our wheels here.
   4383      1.33   mjacob 	 */
   4384  1.39.2.2   bouyer 	MBOX_WAIT_COMPLETE(isp);
   4385       1.1      cgd 
   4386       1.1      cgd 	/*
   4387  1.39.2.2   bouyer 	 * Copy back output registers.
   4388       1.1      cgd 	 */
   4389  1.39.2.2   bouyer 	for (box = 0; box < MAX_MAILBOX; box++) {
   4390  1.39.2.2   bouyer 		if (obits & (1 << box)) {
   4391  1.39.2.2   bouyer 			mbp->param[box] = isp->isp_mboxtmp[box];
   4392      1.36   mjacob 		}
   4393      1.36   mjacob 	}
   4394      1.36   mjacob 
   4395  1.39.2.2   bouyer 	MBOX_RELEASE(isp);
   4396       1.1      cgd 
   4397  1.39.2.2   bouyer 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
   4398  1.39.2.2   bouyer 		return;
   4399  1.39.2.2   bouyer 	}
   4400  1.39.2.2   bouyer #ifdef	ISP_STRIPPED
   4401  1.39.2.2   bouyer 	cname = NULL;
   4402  1.39.2.2   bouyer #else
   4403  1.39.2.2   bouyer 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
   4404  1.39.2.2   bouyer #endif
   4405  1.39.2.2   bouyer 	if (cname == NULL) {
   4406  1.39.2.2   bouyer 		SNPRINTF(cname, sizeof tname, "opcode %x", opcode);
   4407  1.39.2.2   bouyer 	}
   4408      1.10   mjacob 
   4409      1.10   mjacob 	/*
   4410      1.10   mjacob 	 * Just to be chatty here...
   4411      1.10   mjacob 	 */
   4412  1.39.2.2   bouyer 	xname = NULL;
   4413      1.34   mjacob 	switch (mbp->param[0]) {
   4414      1.10   mjacob 	case MBOX_COMMAND_COMPLETE:
   4415      1.10   mjacob 		break;
   4416      1.10   mjacob 	case MBOX_INVALID_COMMAND:
   4417  1.39.2.2   bouyer 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
   4418  1.39.2.2   bouyer 			xname = "INVALID COMMAND";
   4419      1.10   mjacob 		break;
   4420      1.10   mjacob 	case MBOX_HOST_INTERFACE_ERROR:
   4421  1.39.2.2   bouyer 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
   4422  1.39.2.2   bouyer 			xname = "HOST INTERFACE ERROR";
   4423      1.10   mjacob 		break;
   4424      1.10   mjacob 	case MBOX_TEST_FAILED:
   4425  1.39.2.2   bouyer 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
   4426  1.39.2.2   bouyer 			xname = "TEST FAILED";
   4427      1.10   mjacob 		break;
   4428      1.10   mjacob 	case MBOX_COMMAND_ERROR:
   4429  1.39.2.2   bouyer 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
   4430  1.39.2.2   bouyer 			xname = "COMMAND ERROR";
   4431      1.10   mjacob 		break;
   4432      1.10   mjacob 	case MBOX_COMMAND_PARAM_ERROR:
   4433  1.39.2.2   bouyer 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
   4434  1.39.2.2   bouyer 			xname = "COMMAND PARAMETER ERROR";
   4435      1.10   mjacob 		break;
   4436  1.39.2.2   bouyer 	case MBOX_LOOP_ID_USED:
   4437  1.39.2.2   bouyer 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
   4438  1.39.2.2   bouyer 			xname = "LOOP ID ALREADY IN USE";
   4439  1.39.2.2   bouyer 		break;
   4440  1.39.2.2   bouyer 	case MBOX_PORT_ID_USED:
   4441  1.39.2.2   bouyer 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
   4442  1.39.2.2   bouyer 			xname = "PORT ID ALREADY IN USE";
   4443  1.39.2.2   bouyer 		break;
   4444  1.39.2.2   bouyer 	case MBOX_ALL_IDS_USED:
   4445  1.39.2.2   bouyer 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
   4446  1.39.2.2   bouyer 			xname = "ALL LOOP IDS IN USE";
   4447      1.37   mjacob 		break;
   4448  1.39.2.2   bouyer 	case 0:		/* special case */
   4449  1.39.2.2   bouyer 		xname = "TIMEOUT";
   4450      1.37   mjacob 		break;
   4451  1.39.2.2   bouyer 	default:
   4452  1.39.2.2   bouyer 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
   4453  1.39.2.2   bouyer 		xname = mname;
   4454      1.33   mjacob 		break;
   4455  1.39.2.2   bouyer 	}
   4456  1.39.2.2   bouyer 	if (xname)
   4457  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
   4458  1.39.2.2   bouyer 		    cname, xname);
   4459  1.39.2.2   bouyer }
   4460      1.10   mjacob 
   4461      1.10   mjacob static void
   4462  1.39.2.7   bouyer isp_fw_state(struct ispsoftc *isp)
   4463      1.10   mjacob {
   4464      1.38   mjacob 	if (IS_FC(isp)) {
   4465  1.39.2.2   bouyer 		mbreg_t mbs;
   4466      1.10   mjacob 		fcparam *fcp = isp->isp_param;
   4467  1.39.2.2   bouyer 
   4468      1.10   mjacob 		mbs.param[0] = MBOX_GET_FW_STATE;
   4469  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4470  1.39.2.6   bouyer 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
   4471  1.39.2.2   bouyer 			fcp->isp_fwstate = mbs.param[1];
   4472  1.39.2.6   bouyer 		}
   4473      1.10   mjacob 	}
   4474      1.10   mjacob }
   4475      1.10   mjacob 
   4476      1.10   mjacob static void
   4477  1.39.2.7   bouyer isp_update(struct ispsoftc *isp)
   4478      1.10   mjacob {
   4479  1.39.2.2   bouyer 	int bus, upmask;
   4480      1.36   mjacob 
   4481  1.39.2.2   bouyer 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
   4482  1.39.2.2   bouyer 		if (upmask & (1 << bus)) {
   4483      1.36   mjacob 			isp_update_bus(isp, bus);
   4484      1.36   mjacob 		}
   4485  1.39.2.2   bouyer 		upmask &= ~(1 << bus);
   4486      1.36   mjacob 	}
   4487      1.36   mjacob }
   4488      1.36   mjacob 
   4489      1.36   mjacob static void
   4490  1.39.2.7   bouyer isp_update_bus(struct ispsoftc *isp, int bus)
   4491      1.36   mjacob {
   4492      1.25   mjacob 	int tgt;
   4493      1.10   mjacob 	mbreg_t mbs;
   4494      1.10   mjacob 	sdparam *sdp;
   4495      1.10   mjacob 
   4496  1.39.2.2   bouyer 	isp->isp_update &= ~(1 << bus);
   4497      1.38   mjacob 	if (IS_FC(isp)) {
   4498  1.39.2.2   bouyer 		/*
   4499  1.39.2.2   bouyer 		 * There are no 'per-bus' settings for Fibre Channel.
   4500  1.39.2.2   bouyer 		 */
   4501       1.4   mjacob 		return;
   4502       1.4   mjacob 	}
   4503      1.25   mjacob 	sdp = isp->isp_param;
   4504      1.36   mjacob 	sdp += bus;
   4505      1.36   mjacob 
   4506      1.25   mjacob 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4507      1.36   mjacob 		u_int16_t flags, period, offset;
   4508      1.31   mjacob 		int get;
   4509      1.31   mjacob 
   4510      1.25   mjacob 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
   4511  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].dev_update = 0;
   4512  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].dev_refresh = 0;
   4513  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGDEBUG1,
   4514  1.39.2.2   bouyer 	 		    "skipping target %d bus %d update", tgt, bus);
   4515      1.25   mjacob 			continue;
   4516      1.25   mjacob 		}
   4517      1.34   mjacob 		/*
   4518      1.34   mjacob 		 * If the goal is to update the status of the device,
   4519      1.34   mjacob 		 * take what's in dev_flags and try and set the device
   4520      1.34   mjacob 		 * toward that. Otherwise, if we're just refreshing the
   4521      1.34   mjacob 		 * current device state, get the current parameters.
   4522      1.34   mjacob 		 */
   4523  1.39.2.2   bouyer 
   4524  1.39.2.2   bouyer 		/*
   4525  1.39.2.2   bouyer 		 * Refresh overrides set
   4526  1.39.2.2   bouyer 		 */
   4527  1.39.2.2   bouyer 		if (sdp->isp_devparam[tgt].dev_refresh) {
   4528  1.39.2.2   bouyer 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   4529  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].dev_refresh = 0;
   4530  1.39.2.2   bouyer 			get = 1;
   4531  1.39.2.2   bouyer 		} else if (sdp->isp_devparam[tgt].dev_update) {
   4532      1.31   mjacob 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
   4533  1.39.2.2   bouyer 			/*
   4534  1.39.2.2   bouyer 			 * Make sure dev_flags has "Renegotiate on Error"
   4535  1.39.2.2   bouyer 			 * on and "Freeze Queue on Error" off.
   4536  1.39.2.2   bouyer 			 */
   4537  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].dev_flags |= DPARM_RENEG;
   4538  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].dev_flags &= ~DPARM_QFRZ;
   4539  1.39.2.2   bouyer 
   4540      1.31   mjacob 			mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
   4541  1.39.2.2   bouyer 
   4542      1.36   mjacob 			/*
   4543  1.39.2.2   bouyer 			 * Insist that PARITY must be enabled
   4544  1.39.2.2   bouyer 			 * if SYNC or WIDE is enabled.
   4545      1.36   mjacob 			 */
   4546  1.39.2.2   bouyer 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
   4547      1.36   mjacob 				mbs.param[2] |= DPARM_PARITY;
   4548      1.36   mjacob 			}
   4549  1.39.2.2   bouyer 
   4550  1.39.2.2   bouyer 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
   4551  1.39.2.2   bouyer 				mbs.param[3] = 0;
   4552  1.39.2.2   bouyer 			} else {
   4553  1.39.2.2   bouyer 				mbs.param[3] =
   4554  1.39.2.2   bouyer 				    (sdp->isp_devparam[tgt].sync_offset << 8) |
   4555  1.39.2.2   bouyer 				    (sdp->isp_devparam[tgt].sync_period);
   4556  1.39.2.2   bouyer 			}
   4557      1.34   mjacob 			/*
   4558      1.34   mjacob 			 * A command completion later that has
   4559  1.39.2.2   bouyer 			 * RQSTF_NEGOTIATION set canl cause
   4560  1.39.2.2   bouyer 			 * the dev_refresh/announce cycle also.
   4561  1.39.2.2   bouyer 			 &
   4562      1.34   mjacob 			 *
   4563      1.34   mjacob 			 * Note: It is really important to update our current
   4564      1.34   mjacob 			 * flags with at least the state of TAG capabilities-
   4565      1.34   mjacob 			 * otherwise we might try and send a tagged command
   4566      1.34   mjacob 			 * when we have it all turned off. So change it here
   4567      1.34   mjacob 			 * to say that current already matches goal.
   4568      1.34   mjacob 			 */
   4569      1.34   mjacob 			sdp->isp_devparam[tgt].cur_dflags &= ~DPARM_TQING;
   4570      1.34   mjacob 			sdp->isp_devparam[tgt].cur_dflags |=
   4571      1.34   mjacob 			    (sdp->isp_devparam[tgt].dev_flags & DPARM_TQING);
   4572  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGDEBUG2,
   4573  1.39.2.2   bouyer 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
   4574  1.39.2.2   bouyer 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
   4575  1.39.2.2   bouyer 			    mbs.param[3] & 0xff);
   4576  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].dev_update = 0;
   4577      1.36   mjacob 			sdp->isp_devparam[tgt].dev_refresh = 1;
   4578      1.31   mjacob 			get = 0;
   4579      1.31   mjacob 		} else {
   4580      1.25   mjacob 			continue;
   4581      1.25   mjacob 		}
   4582      1.36   mjacob 		mbs.param[1] = (bus << 15) | (tgt << 8) ;
   4583  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4584      1.31   mjacob 		if (get == 0) {
   4585      1.36   mjacob 			isp->isp_sendmarker |= (1 << bus);
   4586      1.31   mjacob 			continue;
   4587      1.31   mjacob 		}
   4588      1.31   mjacob 		flags = mbs.param[2];
   4589      1.31   mjacob 		period = mbs.param[3] & 0xff;
   4590      1.31   mjacob 		offset = mbs.param[3] >> 8;
   4591      1.31   mjacob 		sdp->isp_devparam[tgt].cur_dflags = flags;
   4592      1.34   mjacob 		sdp->isp_devparam[tgt].cur_period = period;
   4593      1.34   mjacob 		sdp->isp_devparam[tgt].cur_offset = offset;
   4594      1.36   mjacob 		get = (bus << 16) | tgt;
   4595      1.36   mjacob 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
   4596      1.10   mjacob 	}
   4597  1.39.2.2   bouyer 
   4598  1.39.2.2   bouyer 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4599  1.39.2.2   bouyer 		if (sdp->isp_devparam[tgt].dev_update ||
   4600  1.39.2.2   bouyer 		    sdp->isp_devparam[tgt].dev_refresh) {
   4601  1.39.2.2   bouyer 			isp->isp_update |= (1 << bus);
   4602  1.39.2.2   bouyer 			break;
   4603  1.39.2.2   bouyer 		}
   4604  1.39.2.2   bouyer 	}
   4605      1.25   mjacob }
   4606      1.10   mjacob 
   4607      1.25   mjacob static void
   4608  1.39.2.7   bouyer isp_setdfltparm(struct ispsoftc *isp, int channel)
   4609      1.25   mjacob {
   4610      1.34   mjacob 	int tgt;
   4611      1.25   mjacob 	mbreg_t mbs;
   4612  1.39.2.2   bouyer 	sdparam *sdp;
   4613      1.36   mjacob 
   4614      1.36   mjacob 	if (IS_FC(isp)) {
   4615      1.36   mjacob 		fcparam *fcp = (fcparam *) isp->isp_param;
   4616  1.39.2.5   bouyer 		int nvfail;
   4617  1.39.2.5   bouyer 
   4618      1.36   mjacob 		fcp += channel;
   4619      1.36   mjacob 		if (fcp->isp_gotdparms) {
   4620      1.36   mjacob 			return;
   4621      1.36   mjacob 		}
   4622      1.36   mjacob 		fcp->isp_gotdparms = 1;
   4623      1.37   mjacob 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
   4624      1.37   mjacob 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
   4625  1.39.2.2   bouyer 		fcp->isp_execthrottle = ISP_EXEC_THROTTLE;
   4626      1.37   mjacob 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
   4627      1.37   mjacob 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
   4628      1.37   mjacob 		/* Platform specific.... */
   4629      1.37   mjacob 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
   4630  1.39.2.2   bouyer 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
   4631  1.39.2.2   bouyer 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
   4632  1.39.2.2   bouyer 		fcp->isp_fwoptions = 0;
   4633  1.39.2.2   bouyer 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
   4634  1.39.2.2   bouyer 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
   4635  1.39.2.2   bouyer 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
   4636  1.39.2.2   bouyer #ifndef	ISP_NO_FASTPOST_FC
   4637  1.39.2.2   bouyer 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
   4638  1.39.2.2   bouyer #endif
   4639  1.39.2.2   bouyer 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
   4640  1.39.2.2   bouyer 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
   4641  1.39.2.2   bouyer 
   4642  1.39.2.2   bouyer 		/*
   4643  1.39.2.2   bouyer 		 * Make sure this is turned off now until we get
   4644  1.39.2.2   bouyer 		 * extended options from NVRAM
   4645  1.39.2.2   bouyer 		 */
   4646  1.39.2.2   bouyer 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
   4647  1.39.2.2   bouyer 
   4648      1.37   mjacob 		/*
   4649  1.39.2.5   bouyer 		 * Now try and read NVRAM unless told to not do so.
   4650  1.39.2.5   bouyer 		 * This will set fcparam's isp_nodewwn && isp_portwwn.
   4651      1.37   mjacob 		 */
   4652  1.39.2.5   bouyer 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4653  1.39.2.5   bouyer 		    	nvfail = isp_read_nvram(isp);
   4654  1.39.2.5   bouyer 			if (nvfail)
   4655  1.39.2.5   bouyer 				isp->isp_confopts |= ISP_CFG_NONVRAM;
   4656  1.39.2.5   bouyer 		} else {
   4657  1.39.2.5   bouyer 			nvfail = 1;
   4658  1.39.2.5   bouyer 		}
   4659  1.39.2.5   bouyer 		/*
   4660  1.39.2.5   bouyer 		 * Set node && port to override platform set defaults
   4661  1.39.2.5   bouyer 		 * unless the nvram read failed (or none was done),
   4662  1.39.2.5   bouyer 		 * or the platform code wants to use what had been
   4663  1.39.2.5   bouyer 		 * set in the defaults.
   4664  1.39.2.5   bouyer 		 */
   4665  1.39.2.5   bouyer 		if (nvfail || (isp->isp_confopts & ISP_CFG_OWNWWN)) {
   4666  1.39.2.5   bouyer 			isp_prt(isp, ISP_LOGCONFIG,
   4667  1.39.2.5   bouyer 			    "Using Node WWN 0x%08x%08x, Port WWN 0x%08x%08x",
   4668  1.39.2.5   bouyer 			    (u_int32_t) (DEFAULT_NODEWWN(isp) >> 32),
   4669  1.39.2.5   bouyer 			    (u_int32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff),
   4670  1.39.2.5   bouyer 			    (u_int32_t) (DEFAULT_PORTWWN(isp) >> 32),
   4671  1.39.2.5   bouyer 			    (u_int32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
   4672  1.39.2.5   bouyer 			isp->isp_confopts |= ISP_CFG_OWNWWN;
   4673  1.39.2.5   bouyer 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
   4674  1.39.2.5   bouyer 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
   4675  1.39.2.5   bouyer 		} else {
   4676  1.39.2.5   bouyer 			/*
   4677  1.39.2.5   bouyer 			 * We always start out with values derived
   4678  1.39.2.5   bouyer 			 * from NVRAM or our platform default.
   4679  1.39.2.5   bouyer 			 */
   4680  1.39.2.5   bouyer 			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
   4681  1.39.2.5   bouyer 			ISP_PORTWWN(isp) = fcp->isp_portwwn;
   4682      1.36   mjacob 		}
   4683      1.36   mjacob 		return;
   4684      1.36   mjacob 	}
   4685      1.36   mjacob 
   4686  1.39.2.2   bouyer 	sdp = (sdparam *) isp->isp_param;
   4687  1.39.2.2   bouyer 	sdp += channel;
   4688      1.10   mjacob 
   4689      1.10   mjacob 	/*
   4690      1.25   mjacob 	 * Been there, done that, got the T-shirt...
   4691      1.10   mjacob 	 */
   4692      1.36   mjacob 	if (sdp->isp_gotdparms) {
   4693      1.25   mjacob 		return;
   4694      1.25   mjacob 	}
   4695      1.36   mjacob 	sdp->isp_gotdparms = 1;
   4696      1.25   mjacob 
   4697      1.34   mjacob 	/*
   4698      1.34   mjacob 	 * If we've not been told to avoid reading NVRAM, try and read it.
   4699      1.34   mjacob 	 * If we're successful reading it, we can return since NVRAM will
   4700      1.34   mjacob 	 * tell us the right thing to do. Otherwise, establish some reasonable
   4701      1.34   mjacob 	 * defaults.
   4702      1.34   mjacob 	 */
   4703      1.34   mjacob 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4704      1.34   mjacob 		if (isp_read_nvram(isp) == 0) {
   4705      1.34   mjacob 			return;
   4706      1.34   mjacob 		}
   4707      1.25   mjacob 	}
   4708      1.10   mjacob 
   4709      1.36   mjacob 	/*
   4710      1.36   mjacob 	 * Now try and see whether we have specific values for them.
   4711      1.36   mjacob 	 */
   4712  1.39.2.2   bouyer 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4713  1.39.2.2   bouyer 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
   4714  1.39.2.2   bouyer 		isp_mboxcmd(isp, &mbs, MBLOGALL);
   4715  1.39.2.2   bouyer 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4716  1.39.2.2   bouyer 			sdp->isp_req_ack_active_neg = 1;
   4717  1.39.2.2   bouyer 			sdp->isp_data_line_active_neg = 1;
   4718  1.39.2.2   bouyer 		} else {
   4719  1.39.2.2   bouyer 			sdp->isp_req_ack_active_neg =
   4720  1.39.2.2   bouyer 			    (mbs.param[1+channel] >> 4) & 0x1;
   4721  1.39.2.2   bouyer 			sdp->isp_data_line_active_neg =
   4722  1.39.2.2   bouyer 			    (mbs.param[1+channel] >> 5) & 0x1;
   4723      1.36   mjacob 		}
   4724      1.10   mjacob 	} else {
   4725  1.39.2.2   bouyer 		sdp->isp_req_ack_active_neg = 1;
   4726  1.39.2.2   bouyer 		sdp->isp_data_line_active_neg = 1;
   4727      1.10   mjacob 	}
   4728      1.32   mjacob 
   4729  1.39.2.2   bouyer 	isp_prt(isp, ISP_LOGDEBUG1,
   4730  1.39.2.2   bouyer 	    "defaulting bus %d REQ/ACK Active Negation is %d",
   4731  1.39.2.2   bouyer 	    channel, sdp->isp_req_ack_active_neg);
   4732  1.39.2.2   bouyer 	isp_prt(isp, ISP_LOGDEBUG1,
   4733  1.39.2.2   bouyer 	    "defaulting bus %d DATA Active Negation is %d",
   4734  1.39.2.2   bouyer 	    channel, sdp->isp_data_line_active_neg);
   4735  1.39.2.2   bouyer 
   4736      1.34   mjacob 	/*
   4737      1.34   mjacob 	 * The trick here is to establish a default for the default (honk!)
   4738      1.34   mjacob 	 * state (dev_flags). Then try and get the current status from
   4739      1.34   mjacob 	 * the card to fill in the current state. We don't, in fact, set
   4740      1.34   mjacob 	 * the default to the SAFE default state- that's not the goal state.
   4741      1.34   mjacob 	 */
   4742      1.34   mjacob 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4743      1.34   mjacob 		sdp->isp_devparam[tgt].cur_offset = 0;
   4744      1.34   mjacob 		sdp->isp_devparam[tgt].cur_period = 0;
   4745      1.34   mjacob 		sdp->isp_devparam[tgt].dev_flags = DPARM_DEFAULT;
   4746      1.34   mjacob 		sdp->isp_devparam[tgt].cur_dflags = 0;
   4747  1.39.2.2   bouyer 		/*
   4748  1.39.2.2   bouyer 		 * We default to Wide/Fast for versions less than a 1040
   4749  1.39.2.2   bouyer 		 * (unless it's SBus).
   4750  1.39.2.2   bouyer 		 */
   4751  1.39.2.2   bouyer 		if ((isp->isp_bustype == ISP_BT_SBUS &&
   4752  1.39.2.2   bouyer 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
   4753  1.39.2.2   bouyer 		    (isp->isp_bustype == ISP_BT_PCI &&
   4754  1.39.2.2   bouyer 		    isp->isp_type < ISP_HA_SCSI_1040) ||
   4755  1.39.2.2   bouyer 		    (isp->isp_clock && isp->isp_clock < 60) ||
   4756  1.39.2.2   bouyer 		    (sdp->isp_ultramode == 0)) {
   4757      1.34   mjacob 			sdp->isp_devparam[tgt].sync_offset =
   4758      1.32   mjacob 			    ISP_10M_SYNCPARMS >> 8;
   4759      1.34   mjacob 			sdp->isp_devparam[tgt].sync_period =
   4760      1.32   mjacob 			    ISP_10M_SYNCPARMS & 0xff;
   4761  1.39.2.2   bouyer 		} else if (IS_ULTRA3(isp)) {
   4762  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].sync_offset =
   4763  1.39.2.2   bouyer 			    ISP_80M_SYNCPARMS >> 8;
   4764  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].sync_period =
   4765  1.39.2.2   bouyer 			    ISP_80M_SYNCPARMS & 0xff;
   4766  1.39.2.2   bouyer 		} else if (IS_ULTRA2(isp)) {
   4767      1.34   mjacob 			sdp->isp_devparam[tgt].sync_offset =
   4768      1.34   mjacob 			    ISP_40M_SYNCPARMS >> 8;
   4769      1.34   mjacob 			sdp->isp_devparam[tgt].sync_period =
   4770      1.34   mjacob 			    ISP_40M_SYNCPARMS & 0xff;
   4771  1.39.2.2   bouyer 		} else if (IS_1240(isp)) {
   4772      1.34   mjacob 			sdp->isp_devparam[tgt].sync_offset =
   4773      1.32   mjacob 			    ISP_20M_SYNCPARMS >> 8;
   4774      1.34   mjacob 			sdp->isp_devparam[tgt].sync_period =
   4775      1.32   mjacob 			    ISP_20M_SYNCPARMS & 0xff;
   4776  1.39.2.2   bouyer 		} else {
   4777  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].sync_offset =
   4778  1.39.2.2   bouyer 			    ISP_20M_SYNCPARMS_1040 >> 8;
   4779  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].sync_period =
   4780  1.39.2.2   bouyer 			    ISP_20M_SYNCPARMS_1040 & 0xff;
   4781      1.32   mjacob 		}
   4782      1.32   mjacob 
   4783      1.32   mjacob 		/*
   4784      1.32   mjacob 		 * Don't get current target parameters if we've been
   4785      1.32   mjacob 		 * told not to use NVRAM- it's really the same thing.
   4786      1.32   mjacob 		 */
   4787  1.39.2.2   bouyer 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
   4788      1.25   mjacob 
   4789  1.39.2.2   bouyer 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
   4790  1.39.2.2   bouyer 			mbs.param[1] = tgt << 8;
   4791  1.39.2.2   bouyer 			isp_mboxcmd(isp, &mbs, MBLOGALL);
   4792  1.39.2.2   bouyer 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
   4793  1.39.2.2   bouyer 				continue;
   4794  1.39.2.2   bouyer 			}
   4795  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].cur_dflags = mbs.param[2];
   4796  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].dev_flags = mbs.param[2];
   4797  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].cur_period = mbs.param[3] & 0xff;
   4798  1.39.2.2   bouyer 			sdp->isp_devparam[tgt].cur_offset = mbs.param[3] >> 8;
   4799      1.34   mjacob 
   4800  1.39.2.2   bouyer 			/*
   4801  1.39.2.2   bouyer 			 * The maximum period we can really see
   4802  1.39.2.2   bouyer 			 * here is 100 (decimal), or 400 ns.
   4803  1.39.2.2   bouyer 			 * For some unknown reason we sometimes
   4804  1.39.2.2   bouyer 			 * get back wildass numbers from the
   4805  1.39.2.2   bouyer 			 * boot device's parameters (alpha only).
   4806  1.39.2.2   bouyer 			 */
   4807  1.39.2.2   bouyer 			if ((mbs.param[3] & 0xff) <= 0x64) {
   4808  1.39.2.2   bouyer 				sdp->isp_devparam[tgt].sync_period =
   4809  1.39.2.2   bouyer 				    mbs.param[3] & 0xff;
   4810  1.39.2.2   bouyer 				sdp->isp_devparam[tgt].sync_offset =
   4811  1.39.2.2   bouyer 				    mbs.param[3] >> 8;
   4812  1.39.2.2   bouyer 			}
   4813      1.25   mjacob 
   4814  1.39.2.2   bouyer 			/*
   4815  1.39.2.2   bouyer 			 * It is not safe to run Ultra Mode with a clock < 60.
   4816  1.39.2.2   bouyer 			 */
   4817  1.39.2.2   bouyer 			if (((isp->isp_clock && isp->isp_clock < 60) ||
   4818  1.39.2.2   bouyer 			    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
   4819  1.39.2.2   bouyer 			    (sdp->isp_devparam[tgt].sync_period <=
   4820  1.39.2.2   bouyer 			    (ISP_20M_SYNCPARMS & 0xff))) {
   4821  1.39.2.2   bouyer 				sdp->isp_devparam[tgt].sync_offset =
   4822  1.39.2.2   bouyer 				    ISP_10M_SYNCPARMS >> 8;
   4823  1.39.2.2   bouyer 				sdp->isp_devparam[tgt].sync_period =
   4824  1.39.2.2   bouyer 				    ISP_10M_SYNCPARMS & 0xff;
   4825  1.39.2.2   bouyer 			}
   4826      1.10   mjacob 		}
   4827  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGDEBUG1,
   4828  1.39.2.2   bouyer 		    "Initial bus %d tgt %d flags %x offset %x period %x",
   4829  1.39.2.2   bouyer 		    channel, tgt, sdp->isp_devparam[tgt].dev_flags,
   4830  1.39.2.2   bouyer 		    sdp->isp_devparam[tgt].sync_offset,
   4831  1.39.2.2   bouyer 		    sdp->isp_devparam[tgt].sync_period);
   4832      1.10   mjacob 	}
   4833      1.10   mjacob 
   4834      1.10   mjacob 	/*
   4835      1.36   mjacob 	 * Establish default some more default parameters.
   4836      1.10   mjacob 	 */
   4837      1.10   mjacob 	sdp->isp_cmd_dma_burst_enable = 1;
   4838      1.10   mjacob 	sdp->isp_data_dma_burst_enabl = 1;
   4839      1.25   mjacob 	sdp->isp_fifo_threshold = 0;
   4840  1.39.2.2   bouyer 	sdp->isp_initiator_id = DEFAULT_IID(isp);
   4841      1.25   mjacob 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   4842      1.25   mjacob 		sdp->isp_async_data_setup = 9;
   4843      1.25   mjacob 	} else {
   4844      1.25   mjacob 		sdp->isp_async_data_setup = 6;
   4845      1.25   mjacob 	}
   4846      1.10   mjacob 	sdp->isp_selection_timeout = 250;
   4847  1.39.2.2   bouyer 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
   4848      1.10   mjacob 	sdp->isp_tag_aging = 8;
   4849      1.10   mjacob 	sdp->isp_bus_reset_delay = 3;
   4850      1.36   mjacob 	sdp->isp_retry_count = 2;
   4851      1.36   mjacob 	sdp->isp_retry_delay = 2;
   4852      1.10   mjacob 
   4853      1.34   mjacob 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
   4854  1.39.2.2   bouyer 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
   4855      1.34   mjacob 		sdp->isp_devparam[tgt].dev_enable = 1;
   4856      1.10   mjacob 	}
   4857      1.10   mjacob }
   4858      1.10   mjacob 
   4859      1.34   mjacob /*
   4860      1.24   mjacob  * Re-initialize the ISP and complete all orphaned commands
   4861      1.38   mjacob  * with a 'botched' notice. The reset/init routines should
   4862      1.38   mjacob  * not disturb an already active list of commands.
   4863      1.24   mjacob  *
   4864      1.24   mjacob  * Locks held prior to coming here.
   4865      1.24   mjacob  */
   4866      1.24   mjacob 
   4867      1.24   mjacob void
   4868  1.39.2.7   bouyer isp_reinit(struct ispsoftc *isp)
   4869      1.10   mjacob {
   4870  1.39.2.2   bouyer 	XS_T *xs;
   4871  1.39.2.7   bouyer 	u_int16_t handle;
   4872      1.10   mjacob 
   4873      1.10   mjacob 	isp_reset(isp);
   4874  1.39.2.6   bouyer 	if (isp->isp_state != ISP_RESETSTATE) {
   4875  1.39.2.6   bouyer 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
   4876  1.39.2.6   bouyer 		goto skip;
   4877  1.39.2.6   bouyer 	}
   4878  1.39.2.6   bouyer 	isp_init(isp);
   4879  1.39.2.6   bouyer 	if (isp->isp_role == ISP_ROLE_NONE) {
   4880  1.39.2.6   bouyer 		goto skip;
   4881  1.39.2.6   bouyer 	}
   4882  1.39.2.6   bouyer 	if (isp->isp_state == ISP_INITSTATE) {
   4883  1.39.2.6   bouyer 		isp->isp_state = ISP_RUNSTATE;
   4884      1.24   mjacob 	}
   4885      1.24   mjacob 	if (isp->isp_state != ISP_RUNSTATE) {
   4886  1.39.2.6   bouyer 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot restart card");
   4887      1.24   mjacob 	}
   4888  1.39.2.6   bouyer skip:
   4889      1.38   mjacob 	isp->isp_nactive = 0;
   4890      1.10   mjacob 
   4891  1.39.2.2   bouyer 	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
   4892      1.38   mjacob 		xs = isp_find_xs(isp, handle);
   4893      1.38   mjacob 		if (xs == NULL) {
   4894      1.10   mjacob 			continue;
   4895      1.27   mjacob 		}
   4896      1.38   mjacob 		isp_destroy_handle(isp, handle);
   4897      1.38   mjacob 		if (XS_XFRLEN(xs)) {
   4898      1.38   mjacob 			ISP_DMAFREE(isp, xs, handle);
   4899      1.38   mjacob 			XS_RESID(xs) = XS_XFRLEN(xs);
   4900      1.38   mjacob 		} else {
   4901      1.38   mjacob 			XS_RESID(xs) = 0;
   4902      1.38   mjacob 		}
   4903      1.27   mjacob 		XS_SETERR(xs, HBA_BUSRESET);
   4904  1.39.2.2   bouyer 		isp_done(xs);
   4905      1.10   mjacob 	}
   4906      1.15   mjacob }
   4907      1.15   mjacob 
   4908      1.28   mjacob /*
   4909      1.25   mjacob  * NVRAM Routines
   4910      1.25   mjacob  */
   4911      1.25   mjacob static int
   4912  1.39.2.7   bouyer isp_read_nvram(struct ispsoftc *isp)
   4913      1.25   mjacob {
   4914      1.25   mjacob 	int i, amt;
   4915      1.25   mjacob 	u_int8_t csum, minversion;
   4916      1.25   mjacob 	union {
   4917      1.25   mjacob 		u_int8_t _x[ISP2100_NVRAM_SIZE];
   4918      1.25   mjacob 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
   4919      1.25   mjacob 	} _n;
   4920      1.25   mjacob #define	nvram_data	_n._x
   4921      1.25   mjacob #define	nvram_words	_n._s
   4922      1.25   mjacob 
   4923      1.33   mjacob 	if (IS_FC(isp)) {
   4924      1.25   mjacob 		amt = ISP2100_NVRAM_SIZE;
   4925      1.25   mjacob 		minversion = 1;
   4926  1.39.2.2   bouyer 	} else if (IS_ULTRA2(isp)) {
   4927      1.36   mjacob 		amt = ISP1080_NVRAM_SIZE;
   4928      1.36   mjacob 		minversion = 0;
   4929      1.25   mjacob 	} else {
   4930      1.25   mjacob 		amt = ISP_NVRAM_SIZE;
   4931      1.25   mjacob 		minversion = 2;
   4932      1.25   mjacob 	}
   4933      1.25   mjacob 
   4934      1.25   mjacob 	/*
   4935      1.25   mjacob 	 * Just read the first two words first to see if we have a valid
   4936      1.25   mjacob 	 * NVRAM to continue reading the rest with.
   4937      1.25   mjacob 	 */
   4938      1.25   mjacob 	for (i = 0; i < 2; i++) {
   4939      1.25   mjacob 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   4940      1.25   mjacob 	}
   4941      1.25   mjacob 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
   4942      1.25   mjacob 	    nvram_data[2] != 'P') {
   4943      1.25   mjacob 		if (isp->isp_bustype != ISP_BT_SBUS) {
   4944  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
   4945  1.39.2.2   bouyer 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
   4946  1.39.2.2   bouyer 			    nvram_data[0], nvram_data[1], nvram_data[2]);
   4947      1.25   mjacob 		}
   4948      1.25   mjacob 		return (-1);
   4949      1.25   mjacob 	}
   4950      1.25   mjacob 	for (i = 2; i < amt>>1; i++) {
   4951      1.25   mjacob 		isp_rdnvram_word(isp, i, &nvram_words[i]);
   4952      1.25   mjacob 	}
   4953      1.25   mjacob 	for (csum = 0, i = 0; i < amt; i++) {
   4954      1.25   mjacob 		csum += nvram_data[i];
   4955      1.25   mjacob 	}
   4956      1.25   mjacob 	if (csum != 0) {
   4957  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
   4958      1.25   mjacob 		return (-1);
   4959      1.25   mjacob 	}
   4960      1.25   mjacob 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
   4961  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
   4962      1.25   mjacob 		    ISP_NVRAM_VERSION(nvram_data));
   4963      1.25   mjacob 		return (-1);
   4964      1.25   mjacob 	}
   4965      1.25   mjacob 
   4966  1.39.2.2   bouyer 	if (IS_ULTRA3(isp)) {
   4967  1.39.2.2   bouyer 		isp_parse_nvram_12160(isp, 0, nvram_data);
   4968  1.39.2.2   bouyer 		isp_parse_nvram_12160(isp, 1, nvram_data);
   4969  1.39.2.2   bouyer 	} else if (IS_1080(isp)) {
   4970  1.39.2.2   bouyer 		isp_parse_nvram_1080(isp, 0, nvram_data);
   4971  1.39.2.2   bouyer 	} else if (IS_1280(isp) || IS_1240(isp)) {
   4972  1.39.2.2   bouyer 		isp_parse_nvram_1080(isp, 0, nvram_data);
   4973  1.39.2.2   bouyer 		isp_parse_nvram_1080(isp, 1, nvram_data);
   4974      1.36   mjacob 	} else if (IS_SCSI(isp)) {
   4975  1.39.2.2   bouyer 		isp_parse_nvram_1020(isp, nvram_data);
   4976      1.25   mjacob 	} else {
   4977  1.39.2.2   bouyer 		isp_parse_nvram_2100(isp, nvram_data);
   4978      1.25   mjacob 	}
   4979      1.25   mjacob 	return (0);
   4980  1.39.2.2   bouyer #undef	nvram_data
   4981  1.39.2.2   bouyer #undef	nvram_words
   4982      1.25   mjacob }
   4983      1.25   mjacob 
   4984      1.25   mjacob static void
   4985  1.39.2.7   bouyer isp_rdnvram_word(struct ispsoftc *isp, int wo, u_int16_t *rp)
   4986      1.25   mjacob {
   4987      1.25   mjacob 	int i, cbits;
   4988      1.25   mjacob 	u_int16_t bit, rqst;
   4989      1.25   mjacob 
   4990      1.25   mjacob 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   4991  1.39.2.2   bouyer 	USEC_DELAY(2);
   4992      1.25   mjacob 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   4993  1.39.2.2   bouyer 	USEC_DELAY(2);
   4994      1.25   mjacob 
   4995      1.36   mjacob 	if (IS_FC(isp)) {
   4996      1.25   mjacob 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
   4997      1.36   mjacob 		rqst = (ISP_NVRAM_READ << 8) | wo;
   4998      1.36   mjacob 		cbits = 10;
   4999  1.39.2.2   bouyer 	} else if (IS_ULTRA2(isp)) {
   5000      1.36   mjacob 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
   5001      1.25   mjacob 		rqst = (ISP_NVRAM_READ << 8) | wo;
   5002      1.25   mjacob 		cbits = 10;
   5003      1.25   mjacob 	} else {
   5004      1.25   mjacob 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
   5005      1.25   mjacob 		rqst = (ISP_NVRAM_READ << 6) | wo;
   5006      1.25   mjacob 		cbits = 8;
   5007      1.25   mjacob 	}
   5008      1.25   mjacob 
   5009      1.25   mjacob 	/*
   5010      1.25   mjacob 	 * Clock the word select request out...
   5011      1.25   mjacob 	 */
   5012      1.25   mjacob 	for (i = cbits; i >= 0; i--) {
   5013      1.25   mjacob 		if ((rqst >> i) & 1) {
   5014      1.25   mjacob 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
   5015      1.25   mjacob 		} else {
   5016      1.25   mjacob 			bit = BIU_NVRAM_SELECT;
   5017      1.25   mjacob 		}
   5018      1.25   mjacob 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5019  1.39.2.2   bouyer 		USEC_DELAY(2);
   5020      1.25   mjacob 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
   5021  1.39.2.2   bouyer 		USEC_DELAY(2);
   5022      1.25   mjacob 		ISP_WRITE(isp, BIU_NVRAM, bit);
   5023  1.39.2.2   bouyer 		USEC_DELAY(2);
   5024      1.25   mjacob 	}
   5025      1.25   mjacob 	/*
   5026      1.25   mjacob 	 * Now read the result back in (bits come back in MSB format).
   5027      1.25   mjacob 	 */
   5028      1.25   mjacob 	*rp = 0;
   5029      1.25   mjacob 	for (i = 0; i < 16; i++) {
   5030      1.25   mjacob 		u_int16_t rv;
   5031      1.25   mjacob 		*rp <<= 1;
   5032      1.25   mjacob 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
   5033  1.39.2.2   bouyer 		USEC_DELAY(2);
   5034      1.25   mjacob 		rv = ISP_READ(isp, BIU_NVRAM);
   5035      1.25   mjacob 		if (rv & BIU_NVRAM_DATAIN) {
   5036      1.25   mjacob 			*rp |= 1;
   5037      1.25   mjacob 		}
   5038  1.39.2.2   bouyer 		USEC_DELAY(2);
   5039      1.25   mjacob 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
   5040  1.39.2.2   bouyer 		USEC_DELAY(2);
   5041      1.25   mjacob 	}
   5042      1.25   mjacob 	ISP_WRITE(isp, BIU_NVRAM, 0);
   5043  1.39.2.2   bouyer 	USEC_DELAY(2);
   5044  1.39.2.2   bouyer 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
   5045  1.39.2.2   bouyer }
   5046  1.39.2.2   bouyer 
   5047  1.39.2.2   bouyer static void
   5048  1.39.2.7   bouyer isp_parse_nvram_1020(struct ispsoftc *isp, u_int8_t *nvram_data)
   5049  1.39.2.2   bouyer {
   5050  1.39.2.2   bouyer 	int i;
   5051  1.39.2.2   bouyer 	sdparam *sdp = (sdparam *) isp->isp_param;
   5052  1.39.2.2   bouyer 
   5053  1.39.2.2   bouyer 	sdp->isp_fifo_threshold =
   5054  1.39.2.2   bouyer 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
   5055  1.39.2.2   bouyer 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
   5056  1.39.2.2   bouyer 
   5057  1.39.2.2   bouyer 	sdp->isp_initiator_id =
   5058  1.39.2.2   bouyer 		ISP_NVRAM_INITIATOR_ID(nvram_data);
   5059  1.39.2.2   bouyer 
   5060  1.39.2.2   bouyer 	sdp->isp_bus_reset_delay =
   5061  1.39.2.2   bouyer 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
   5062  1.39.2.2   bouyer 
   5063  1.39.2.2   bouyer 	sdp->isp_retry_count =
   5064  1.39.2.2   bouyer 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
   5065  1.39.2.2   bouyer 
   5066  1.39.2.2   bouyer 	sdp->isp_retry_delay =
   5067  1.39.2.2   bouyer 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
   5068  1.39.2.2   bouyer 
   5069  1.39.2.2   bouyer 	sdp->isp_async_data_setup =
   5070  1.39.2.2   bouyer 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
   5071  1.39.2.2   bouyer 
   5072  1.39.2.2   bouyer 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
   5073  1.39.2.2   bouyer 		if (sdp->isp_async_data_setup < 9) {
   5074  1.39.2.2   bouyer 			sdp->isp_async_data_setup = 9;
   5075  1.39.2.2   bouyer 		}
   5076  1.39.2.2   bouyer 	} else {
   5077  1.39.2.2   bouyer 		if (sdp->isp_async_data_setup != 6) {
   5078  1.39.2.2   bouyer 			sdp->isp_async_data_setup = 6;
   5079  1.39.2.2   bouyer 		}
   5080  1.39.2.2   bouyer 	}
   5081  1.39.2.2   bouyer 
   5082  1.39.2.2   bouyer 	sdp->isp_req_ack_active_neg =
   5083  1.39.2.2   bouyer 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
   5084  1.39.2.2   bouyer 
   5085  1.39.2.2   bouyer 	sdp->isp_data_line_active_neg =
   5086  1.39.2.2   bouyer 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
   5087  1.39.2.2   bouyer 
   5088  1.39.2.2   bouyer 	sdp->isp_data_dma_burst_enabl =
   5089  1.39.2.2   bouyer 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
   5090  1.39.2.2   bouyer 
   5091  1.39.2.2   bouyer 	sdp->isp_cmd_dma_burst_enable =
   5092  1.39.2.2   bouyer 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
   5093  1.39.2.2   bouyer 
   5094  1.39.2.2   bouyer 	sdp->isp_tag_aging =
   5095  1.39.2.2   bouyer 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
   5096  1.39.2.2   bouyer 
   5097  1.39.2.2   bouyer 	sdp->isp_selection_timeout =
   5098  1.39.2.2   bouyer 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
   5099  1.39.2.2   bouyer 
   5100  1.39.2.2   bouyer 	sdp->isp_max_queue_depth =
   5101  1.39.2.2   bouyer 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
   5102  1.39.2.2   bouyer 
   5103  1.39.2.2   bouyer 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
   5104  1.39.2.2   bouyer 	for (i = 0; i < MAX_TARGETS; i++) {
   5105  1.39.2.2   bouyer 		sdp->isp_devparam[i].dev_enable =
   5106  1.39.2.2   bouyer 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
   5107  1.39.2.2   bouyer 		sdp->isp_devparam[i].exc_throttle =
   5108  1.39.2.2   bouyer 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
   5109  1.39.2.2   bouyer 		sdp->isp_devparam[i].sync_offset =
   5110  1.39.2.2   bouyer 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
   5111  1.39.2.2   bouyer 		sdp->isp_devparam[i].sync_period =
   5112  1.39.2.2   bouyer 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
   5113  1.39.2.2   bouyer 
   5114  1.39.2.2   bouyer 		if (isp->isp_type < ISP_HA_SCSI_1040) {
   5115  1.39.2.2   bouyer 			/*
   5116  1.39.2.2   bouyer 			 * If we're not ultra, we can't possibly
   5117  1.39.2.2   bouyer 			 * be a shorter period than this.
   5118  1.39.2.2   bouyer 			 */
   5119  1.39.2.2   bouyer 			if (sdp->isp_devparam[i].sync_period < 0x19) {
   5120  1.39.2.2   bouyer 				sdp->isp_devparam[i].sync_period = 0x19;
   5121  1.39.2.2   bouyer 			}
   5122  1.39.2.2   bouyer 			if (sdp->isp_devparam[i].sync_offset > 0xc) {
   5123  1.39.2.2   bouyer 				sdp->isp_devparam[i].sync_offset = 0x0c;
   5124  1.39.2.2   bouyer 			}
   5125  1.39.2.2   bouyer 		} else {
   5126  1.39.2.2   bouyer 			if (sdp->isp_devparam[i].sync_offset > 0x8) {
   5127  1.39.2.2   bouyer 				sdp->isp_devparam[i].sync_offset = 0x8;
   5128  1.39.2.2   bouyer 			}
   5129  1.39.2.2   bouyer 		}
   5130  1.39.2.2   bouyer 		sdp->isp_devparam[i].dev_flags = 0;
   5131  1.39.2.2   bouyer 		if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
   5132  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   5133  1.39.2.2   bouyer 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   5134  1.39.2.2   bouyer 		if (ISP_NVRAM_TGT_TQING(nvram_data, i))
   5135  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   5136  1.39.2.2   bouyer 		if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
   5137  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   5138  1.39.2.2   bouyer 		if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
   5139  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   5140  1.39.2.2   bouyer 		if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
   5141  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   5142  1.39.2.2   bouyer 		if (ISP_NVRAM_TGT_DISC(nvram_data, i))
   5143  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   5144  1.39.2.2   bouyer 		sdp->isp_devparam[i].cur_dflags = 0; /* we don't know */
   5145  1.39.2.2   bouyer 	}
   5146  1.39.2.2   bouyer }
   5147  1.39.2.2   bouyer 
   5148  1.39.2.2   bouyer static void
   5149  1.39.2.7   bouyer isp_parse_nvram_1080(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   5150  1.39.2.2   bouyer {
   5151  1.39.2.2   bouyer 	int i;
   5152  1.39.2.2   bouyer 	sdparam *sdp = (sdparam *) isp->isp_param;
   5153  1.39.2.2   bouyer 	sdp += bus;
   5154  1.39.2.2   bouyer 
   5155  1.39.2.2   bouyer 	sdp->isp_fifo_threshold =
   5156  1.39.2.2   bouyer 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
   5157  1.39.2.2   bouyer 
   5158  1.39.2.2   bouyer 	sdp->isp_initiator_id =
   5159  1.39.2.2   bouyer 	    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
   5160  1.39.2.2   bouyer 
   5161  1.39.2.2   bouyer 	sdp->isp_bus_reset_delay =
   5162  1.39.2.2   bouyer 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   5163  1.39.2.2   bouyer 
   5164  1.39.2.2   bouyer 	sdp->isp_retry_count =
   5165  1.39.2.2   bouyer 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   5166  1.39.2.2   bouyer 
   5167  1.39.2.2   bouyer 	sdp->isp_retry_delay =
   5168  1.39.2.2   bouyer 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   5169  1.39.2.2   bouyer 
   5170  1.39.2.2   bouyer 	sdp->isp_async_data_setup =
   5171  1.39.2.2   bouyer 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
   5172  1.39.2.2   bouyer 	    bus);
   5173  1.39.2.2   bouyer 
   5174  1.39.2.2   bouyer 	sdp->isp_req_ack_active_neg =
   5175  1.39.2.2   bouyer 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
   5176  1.39.2.2   bouyer 	    bus);
   5177  1.39.2.2   bouyer 
   5178  1.39.2.2   bouyer 	sdp->isp_data_line_active_neg =
   5179  1.39.2.2   bouyer 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
   5180  1.39.2.2   bouyer 	    bus);
   5181  1.39.2.2   bouyer 
   5182  1.39.2.2   bouyer 	sdp->isp_data_dma_burst_enabl =
   5183  1.39.2.2   bouyer 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   5184  1.39.2.2   bouyer 
   5185  1.39.2.2   bouyer 	sdp->isp_cmd_dma_burst_enable =
   5186  1.39.2.2   bouyer 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
   5187  1.39.2.2   bouyer 
   5188  1.39.2.2   bouyer 	sdp->isp_selection_timeout =
   5189  1.39.2.2   bouyer 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   5190  1.39.2.2   bouyer 
   5191  1.39.2.2   bouyer 	sdp->isp_max_queue_depth =
   5192  1.39.2.2   bouyer 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   5193  1.39.2.2   bouyer 
   5194  1.39.2.2   bouyer 	for (i = 0; i < MAX_TARGETS; i++) {
   5195  1.39.2.2   bouyer 		sdp->isp_devparam[i].dev_enable =
   5196  1.39.2.2   bouyer 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
   5197  1.39.2.2   bouyer 		sdp->isp_devparam[i].exc_throttle =
   5198  1.39.2.2   bouyer 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
   5199  1.39.2.2   bouyer 		sdp->isp_devparam[i].sync_offset =
   5200  1.39.2.2   bouyer 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
   5201  1.39.2.2   bouyer 		sdp->isp_devparam[i].sync_period =
   5202  1.39.2.2   bouyer 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
   5203  1.39.2.2   bouyer 		sdp->isp_devparam[i].dev_flags = 0;
   5204  1.39.2.2   bouyer 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, i, bus))
   5205  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   5206  1.39.2.2   bouyer 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   5207  1.39.2.2   bouyer 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, i, bus))
   5208  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   5209  1.39.2.2   bouyer 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, i, bus))
   5210  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   5211  1.39.2.2   bouyer 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, i, bus))
   5212  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   5213  1.39.2.2   bouyer 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, i, bus))
   5214  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   5215  1.39.2.2   bouyer 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, i, bus))
   5216  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   5217  1.39.2.2   bouyer 		sdp->isp_devparam[i].cur_dflags = 0;
   5218  1.39.2.2   bouyer 	}
   5219  1.39.2.2   bouyer }
   5220  1.39.2.2   bouyer 
   5221  1.39.2.2   bouyer static void
   5222  1.39.2.7   bouyer isp_parse_nvram_12160(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
   5223  1.39.2.2   bouyer {
   5224  1.39.2.2   bouyer 	sdparam *sdp = (sdparam *) isp->isp_param;
   5225  1.39.2.2   bouyer 	int i;
   5226  1.39.2.2   bouyer 
   5227  1.39.2.2   bouyer 	sdp += bus;
   5228  1.39.2.2   bouyer 
   5229  1.39.2.2   bouyer 	sdp->isp_fifo_threshold =
   5230  1.39.2.2   bouyer 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
   5231  1.39.2.2   bouyer 
   5232  1.39.2.2   bouyer 	sdp->isp_initiator_id =
   5233  1.39.2.2   bouyer 	    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
   5234  1.39.2.2   bouyer 
   5235  1.39.2.2   bouyer 	sdp->isp_bus_reset_delay =
   5236  1.39.2.2   bouyer 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
   5237  1.39.2.2   bouyer 
   5238  1.39.2.2   bouyer 	sdp->isp_retry_count =
   5239  1.39.2.2   bouyer 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
   5240  1.39.2.2   bouyer 
   5241  1.39.2.2   bouyer 	sdp->isp_retry_delay =
   5242  1.39.2.2   bouyer 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
   5243  1.39.2.2   bouyer 
   5244  1.39.2.2   bouyer 	sdp->isp_async_data_setup =
   5245  1.39.2.2   bouyer 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
   5246  1.39.2.2   bouyer 	    bus);
   5247  1.39.2.2   bouyer 
   5248  1.39.2.2   bouyer 	sdp->isp_req_ack_active_neg =
   5249  1.39.2.2   bouyer 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
   5250  1.39.2.2   bouyer 	    bus);
   5251  1.39.2.2   bouyer 
   5252  1.39.2.2   bouyer 	sdp->isp_data_line_active_neg =
   5253  1.39.2.2   bouyer 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
   5254  1.39.2.2   bouyer 	    bus);
   5255  1.39.2.2   bouyer 
   5256  1.39.2.2   bouyer 	sdp->isp_data_dma_burst_enabl =
   5257  1.39.2.2   bouyer 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   5258  1.39.2.2   bouyer 
   5259  1.39.2.2   bouyer 	sdp->isp_cmd_dma_burst_enable =
   5260  1.39.2.2   bouyer 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
   5261  1.39.2.2   bouyer 
   5262  1.39.2.2   bouyer 	sdp->isp_selection_timeout =
   5263  1.39.2.2   bouyer 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
   5264  1.39.2.2   bouyer 
   5265  1.39.2.2   bouyer 	sdp->isp_max_queue_depth =
   5266  1.39.2.2   bouyer 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
   5267  1.39.2.2   bouyer 
   5268  1.39.2.2   bouyer 	for (i = 0; i < MAX_TARGETS; i++) {
   5269  1.39.2.2   bouyer 		sdp->isp_devparam[i].dev_enable =
   5270  1.39.2.2   bouyer 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
   5271  1.39.2.2   bouyer 		sdp->isp_devparam[i].exc_throttle =
   5272  1.39.2.2   bouyer 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
   5273  1.39.2.2   bouyer 		sdp->isp_devparam[i].sync_offset =
   5274  1.39.2.2   bouyer 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
   5275  1.39.2.2   bouyer 		sdp->isp_devparam[i].sync_period =
   5276  1.39.2.2   bouyer 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
   5277  1.39.2.2   bouyer 		sdp->isp_devparam[i].dev_flags = 0;
   5278  1.39.2.2   bouyer 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, i, bus))
   5279  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
   5280  1.39.2.2   bouyer 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
   5281  1.39.2.2   bouyer 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, i, bus))
   5282  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
   5283  1.39.2.2   bouyer 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, i, bus))
   5284  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
   5285  1.39.2.2   bouyer 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, i, bus))
   5286  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
   5287  1.39.2.2   bouyer 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, i, bus))
   5288  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
   5289  1.39.2.2   bouyer 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, i, bus))
   5290  1.39.2.2   bouyer 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
   5291  1.39.2.2   bouyer 		sdp->isp_devparam[i].cur_dflags = 0;
   5292  1.39.2.2   bouyer 	}
   5293  1.39.2.2   bouyer }
   5294  1.39.2.2   bouyer 
   5295  1.39.2.2   bouyer static void
   5296  1.39.2.7   bouyer isp_parse_nvram_2100(struct ispsoftc *isp, u_int8_t *nvram_data)
   5297  1.39.2.2   bouyer {
   5298  1.39.2.2   bouyer 	fcparam *fcp = (fcparam *) isp->isp_param;
   5299  1.39.2.2   bouyer 	u_int64_t wwn;
   5300  1.39.2.2   bouyer 
   5301  1.39.2.2   bouyer 	/*
   5302  1.39.2.2   bouyer 	 * There is NVRAM storage for both Port and Node entities-
   5303  1.39.2.2   bouyer 	 * but the Node entity appears to be unused on all the cards
   5304  1.39.2.2   bouyer 	 * I can find. However, we should account for this being set
   5305  1.39.2.2   bouyer 	 * at some point in the future.
   5306  1.39.2.2   bouyer 	 *
   5307  1.39.2.2   bouyer 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
   5308  1.39.2.2   bouyer 	 * bits 48..60. In the case of the 2202, it appears that they do
   5309  1.39.2.2   bouyer 	 * use bit 48 to distinguish between the two instances on the card.
   5310  1.39.2.2   bouyer 	 * The 2204, which I've never seen, *probably* extends this method.
   5311  1.39.2.2   bouyer 	 */
   5312  1.39.2.2   bouyer 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
   5313  1.39.2.2   bouyer 	if (wwn) {
   5314  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
   5315  1.39.2.2   bouyer 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
   5316  1.39.2.2   bouyer 		if ((wwn >> 60) == 0) {
   5317  1.39.2.2   bouyer 			wwn |= (((u_int64_t) 2)<< 60);
   5318  1.39.2.2   bouyer 		}
   5319  1.39.2.2   bouyer 	}
   5320  1.39.2.2   bouyer 	fcp->isp_portwwn = wwn;
   5321  1.39.2.2   bouyer 	wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
   5322  1.39.2.2   bouyer 	if (wwn) {
   5323  1.39.2.2   bouyer 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
   5324  1.39.2.2   bouyer 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
   5325  1.39.2.2   bouyer 		if ((wwn >> 60) == 0) {
   5326  1.39.2.2   bouyer 			wwn |= (((u_int64_t) 2)<< 60);
   5327  1.39.2.2   bouyer 		}
   5328  1.39.2.2   bouyer 	}
   5329  1.39.2.2   bouyer 	fcp->isp_nodewwn = wwn;
   5330  1.39.2.2   bouyer 
   5331  1.39.2.2   bouyer 	/*
   5332  1.39.2.2   bouyer 	 * Make sure we have both Node and Port as non-zero values.
   5333  1.39.2.2   bouyer 	 */
   5334  1.39.2.2   bouyer 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
   5335  1.39.2.2   bouyer 		fcp->isp_portwwn = fcp->isp_nodewwn;
   5336  1.39.2.2   bouyer 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
   5337  1.39.2.2   bouyer 		fcp->isp_nodewwn = fcp->isp_portwwn;
   5338  1.39.2.2   bouyer 	}
   5339  1.39.2.2   bouyer 
   5340  1.39.2.2   bouyer 	/*
   5341  1.39.2.2   bouyer 	 * Make the Node and Port values sane if they're NAA == 2.
   5342  1.39.2.2   bouyer 	 * This means to clear bits 48..56 for the Node WWN and
   5343  1.39.2.2   bouyer 	 * make sure that there's some non-zero value in 48..56
   5344  1.39.2.2   bouyer 	 * for the Port WWN.
   5345  1.39.2.2   bouyer 	 */
   5346  1.39.2.2   bouyer 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
   5347  1.39.2.2   bouyer 		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
   5348  1.39.2.2   bouyer 		    (fcp->isp_nodewwn >> 60) == 2) {
   5349  1.39.2.2   bouyer 			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
   5350  1.39.2.2   bouyer 		}
   5351  1.39.2.2   bouyer 		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
   5352  1.39.2.2   bouyer 		    (fcp->isp_portwwn >> 60) == 2) {
   5353  1.39.2.2   bouyer 			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
   5354  1.39.2.2   bouyer 		}
   5355  1.39.2.2   bouyer 	}
   5356  1.39.2.2   bouyer 
   5357  1.39.2.2   bouyer 	fcp->isp_maxalloc =
   5358  1.39.2.2   bouyer 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
   5359  1.39.2.2   bouyer 	fcp->isp_maxfrmlen =
   5360  1.39.2.2   bouyer 		ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
   5361  1.39.2.2   bouyer 	fcp->isp_retry_delay =
   5362  1.39.2.2   bouyer 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
   5363  1.39.2.2   bouyer 	fcp->isp_retry_count =
   5364  1.39.2.2   bouyer 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
   5365  1.39.2.2   bouyer 	fcp->isp_loopid =
   5366  1.39.2.2   bouyer 		ISP2100_NVRAM_HARDLOOPID(nvram_data);
   5367  1.39.2.2   bouyer 	fcp->isp_execthrottle =
   5368  1.39.2.2   bouyer 		ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
   5369  1.39.2.2   bouyer 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
   5370  1.39.2.2   bouyer 	isp_prt(isp, ISP_LOGDEBUG0,
   5371  1.39.2.2   bouyer 	    "fwoptions from nvram are 0x%x", fcp->isp_fwoptions);
   5372       1.1      cgd }
   5373