Home | History | Annotate | Line # | Download | only in ic
isp_netbsd.h revision 1.43
      1  1.43  mjacob /* $NetBSD: isp_netbsd.h,v 1.43 2001/05/25 21:45:55 mjacob Exp $ */
      2  1.29  mjacob /*
      3  1.29  mjacob  * This driver, which is contained in NetBSD in the files:
      4  1.29  mjacob  *
      5  1.29  mjacob  *	sys/dev/ic/isp.c
      6  1.33     wiz  *	sys/dev/ic/isp_inline.h
      7  1.33     wiz  *	sys/dev/ic/isp_netbsd.c
      8  1.33     wiz  *	sys/dev/ic/isp_netbsd.h
      9  1.33     wiz  *	sys/dev/ic/isp_target.c
     10  1.33     wiz  *	sys/dev/ic/isp_target.h
     11  1.33     wiz  *	sys/dev/ic/isp_tpublic.h
     12  1.33     wiz  *	sys/dev/ic/ispmbox.h
     13  1.33     wiz  *	sys/dev/ic/ispreg.h
     14  1.33     wiz  *	sys/dev/ic/ispvar.h
     15  1.29  mjacob  *	sys/microcode/isp/asm_sbus.h
     16  1.29  mjacob  *	sys/microcode/isp/asm_1040.h
     17  1.29  mjacob  *	sys/microcode/isp/asm_1080.h
     18  1.29  mjacob  *	sys/microcode/isp/asm_12160.h
     19  1.29  mjacob  *	sys/microcode/isp/asm_2100.h
     20  1.29  mjacob  *	sys/microcode/isp/asm_2200.h
     21  1.29  mjacob  *	sys/pci/isp_pci.c
     22  1.29  mjacob  *	sys/sbus/isp_sbus.c
     23  1.29  mjacob  *
     24  1.29  mjacob  * Is being actively maintained by Matthew Jacob (mjacob (at) netbsd.org).
     25  1.29  mjacob  * This driver also is shared source with FreeBSD, OpenBSD, Linux, Solaris,
     26  1.29  mjacob  * Linux versions. This tends to be an interesting maintenance problem.
     27  1.29  mjacob  *
     28  1.29  mjacob  * Please coordinate with Matthew Jacob on changes you wish to make here.
     29  1.29  mjacob  */
     30   1.1  mjacob /*
     31   1.1  mjacob  * NetBSD Specific definitions for the Qlogic ISP Host Adapter
     32  1.15  mjacob  * Matthew Jacob <mjacob (at) nas.nasa.gov>
     33  1.15  mjacob  */
     34  1.15  mjacob /*
     35  1.15  mjacob  * Copyright (C) 1997, 1998, 1999 National Aeronautics & Space Administration
     36   1.1  mjacob  * All rights reserved.
     37   1.1  mjacob  *
     38   1.1  mjacob  * Redistribution and use in source and binary forms, with or without
     39   1.1  mjacob  * modification, are permitted provided that the following conditions
     40   1.1  mjacob  * are met:
     41   1.1  mjacob  * 1. Redistributions of source code must retain the above copyright
     42  1.15  mjacob  *    notice, this list of conditions and the following disclaimer.
     43   1.1  mjacob  * 2. Redistributions in binary form must reproduce the above copyright
     44   1.1  mjacob  *    notice, this list of conditions and the following disclaimer in the
     45   1.1  mjacob  *    documentation and/or other materials provided with the distribution.
     46   1.1  mjacob  * 3. The name of the author may not be used to endorse or promote products
     47  1.15  mjacob  *    derived from this software without specific prior written permission
     48   1.1  mjacob  *
     49  1.15  mjacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     50  1.15  mjacob  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     51  1.15  mjacob  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     52  1.15  mjacob  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     53  1.15  mjacob  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54  1.15  mjacob  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55  1.15  mjacob  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56  1.15  mjacob  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57  1.15  mjacob  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58  1.15  mjacob  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59   1.1  mjacob  */
     60   1.1  mjacob #ifndef	_ISP_NETBSD_H
     61   1.1  mjacob #define	_ISP_NETBSD_H
     62   1.1  mjacob 
     63   1.1  mjacob #include <sys/types.h>
     64   1.1  mjacob #include <sys/param.h>
     65   1.1  mjacob #include <sys/systm.h>
     66   1.1  mjacob #include <sys/kernel.h>
     67   1.1  mjacob #include <sys/errno.h>
     68   1.1  mjacob #include <sys/ioctl.h>
     69   1.1  mjacob #include <sys/device.h>
     70   1.1  mjacob #include <sys/malloc.h>
     71   1.1  mjacob #include <sys/buf.h>
     72   1.1  mjacob #include <sys/proc.h>
     73   1.1  mjacob #include <sys/user.h>
     74  1.43  mjacob #include <sys/kthread.h>
     75   1.1  mjacob 
     76   1.1  mjacob 
     77   1.1  mjacob #include <dev/scsipi/scsi_all.h>
     78   1.1  mjacob #include <dev/scsipi/scsipi_all.h>
     79   1.1  mjacob #include <dev/scsipi/scsiconf.h>
     80   1.1  mjacob 
     81   1.1  mjacob #include <dev/scsipi/scsi_message.h>
     82   1.1  mjacob #include <dev/scsipi/scsipi_debug.h>
     83   1.1  mjacob 
     84  1.15  mjacob #include "opt_isp.h"
     85  1.26  mjacob 
     86  1.15  mjacob 
     87  1.43  mjacob #define	ISP_PLATFORM_VERSION_MAJOR	2
     88  1.43  mjacob #define	ISP_PLATFORM_VERSION_MINOR	0
     89   1.1  mjacob 
     90   1.1  mjacob struct isposinfo {
     91   1.1  mjacob 	struct device		_dev;
     92  1.40  bouyer 	struct scsipi_channel	_chan;
     93  1.40  bouyer 	struct scsipi_channel	_chan_b;
     94   1.6  mjacob 	struct scsipi_adapter   _adapter;
     95  1.27  mjacob 	int	splsaved;
     96  1.27  mjacob 	int	mboxwaiting;
     97  1.29  mjacob 	u_int32_t	islocked;
     98  1.29  mjacob 	u_int32_t	onintstack;
     99  1.43  mjacob 	unsigned int		: 26,
    100  1.43  mjacob 		loop_checked	: 1,
    101  1.41  mjacob 		mbox_wanted	: 1,
    102  1.41  mjacob 		mbox_locked	: 1,
    103  1.41  mjacob 		no_mbox_ints	: 1,
    104  1.43  mjacob 		paused		: 1,
    105  1.41  mjacob 		blocked		: 1;
    106  1.18  mjacob 	union {
    107  1.27  mjacob 		u_int64_t	_wwn;
    108  1.18  mjacob 		u_int16_t	_discovered[2];
    109  1.27  mjacob #define	wwn_seed	un._wwn
    110  1.27  mjacob #define	discovered	un._discovered
    111  1.18  mjacob 	} un;
    112  1.43  mjacob 	u_int32_t threadwork;
    113  1.43  mjacob 	struct proc *thread;
    114   1.1  mjacob };
    115  1.32  mjacob #define	ISP_MUSTPOLL(isp)	\
    116  1.32  mjacob 	(isp->isp_osinfo.onintstack || isp->isp_osinfo.no_mbox_ints)
    117  1.18  mjacob 
    118  1.43  mjacob #define	HANDLE_LOOPSTATE_IN_OUTER_LAYERS	1
    119  1.43  mjacob 
    120  1.27  mjacob /*
    121  1.27  mjacob  * Required Macros/Defines
    122  1.27  mjacob  */
    123  1.27  mjacob 
    124  1.42  mjacob #define	INLINE			__inline
    125  1.27  mjacob 
    126  1.15  mjacob #define	ISP2100_SCRLEN		0x400
    127   1.1  mjacob 
    128  1.27  mjacob #define	MEMZERO			bzero
    129  1.27  mjacob #define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
    130  1.27  mjacob #define	SNPRINTF		snprintf
    131  1.27  mjacob #define	STRNCAT			strncat
    132  1.27  mjacob #define	USEC_DELAY		DELAY
    133  1.32  mjacob #define	USEC_SLEEP(isp, x)		\
    134  1.32  mjacob 	if (!ISP_MUSTPOLL(isp))		\
    135  1.32  mjacob 		ISP_UNLOCK(isp);	\
    136  1.32  mjacob 	DELAY(x);			\
    137  1.32  mjacob 	if (!ISP_MUSTPOLL(isp))		\
    138  1.32  mjacob 		ISP_LOCK(isp)
    139  1.27  mjacob 
    140  1.27  mjacob #define	NANOTIME_T		struct timeval
    141  1.27  mjacob #define	GET_NANOTIME		microtime
    142  1.27  mjacob #define	GET_NANOSEC(x)		(((x)->tv_sec * 1000000 + (x)->tv_usec) * 1000)
    143  1.27  mjacob #define	NANOTIME_SUB		isp_microtime_sub
    144   1.1  mjacob 
    145  1.27  mjacob #define	MAXISPREQUEST(isp)	256
    146   1.8  mjacob 
    147  1.11  mjacob #ifdef	__alpha__
    148  1.27  mjacob #define	MEMORYBARRIER(isp, type, offset, size)	alpha_mb()
    149  1.11  mjacob #else
    150  1.27  mjacob #define	MEMORYBARRIER(isp, type, offset, size)
    151  1.11  mjacob #endif
    152   1.3  mjacob 
    153  1.41  mjacob #define	MBOX_ACQUIRE(isp)						\
    154  1.41  mjacob 	if (isp->isp_osinfo.onintstack) {				\
    155  1.41  mjacob 		if (isp->isp_osinfo.mbox_locked) {			\
    156  1.41  mjacob 			mbp->param[0] = MBOX_HOST_INTERFACE_ERROR;	\
    157  1.41  mjacob 			isp_prt(isp, ISP_LOGERR, "cannot acquire MBOX lock"); \
    158  1.41  mjacob 			return;						\
    159  1.41  mjacob 		}							\
    160  1.41  mjacob 	} else {							\
    161  1.41  mjacob 		while (isp->isp_osinfo.mbox_locked) {			\
    162  1.41  mjacob 			isp->isp_osinfo.mbox_wanted = 1;		\
    163  1.41  mjacob 			(void) tsleep(&isp->isp_osinfo.mboxwaiting+1,	\
    164  1.41  mjacob 			    PRIBIO, "mbox_acquire", 0);			\
    165  1.41  mjacob 		}							\
    166  1.41  mjacob 	}								\
    167  1.41  mjacob 	isp->isp_osinfo.mbox_locked = 1
    168  1.41  mjacob 
    169  1.27  mjacob #define	MBOX_WAIT_COMPLETE	isp_wait_complete
    170  1.13  mjacob 
    171  1.27  mjacob #define	MBOX_NOTIFY_COMPLETE(isp)					\
    172  1.27  mjacob 	if (isp->isp_osinfo.mboxwaiting) {				\
    173  1.27  mjacob                 isp->isp_osinfo.mboxwaiting = 0;			\
    174  1.27  mjacob                 wakeup(&isp->isp_osinfo.mboxwaiting);			\
    175  1.27  mjacob         }								\
    176  1.27  mjacob 	isp->isp_mboxbsy = 0
    177   1.1  mjacob 
    178  1.41  mjacob #define	MBOX_RELEASE(isp)						\
    179  1.41  mjacob 	if (isp->isp_osinfo.mbox_wanted) {				\
    180  1.41  mjacob 		isp->isp_osinfo.mbox_wanted = 0;			\
    181  1.41  mjacob 		wakeup(&isp->isp_osinfo.mboxwaiting+1);			\
    182  1.41  mjacob 	}								\
    183  1.41  mjacob 	isp->isp_osinfo.mbox_locked = 0
    184  1.26  mjacob 
    185  1.27  mjacob #ifndef	SCSI_GOOD
    186  1.27  mjacob #define	SCSI_GOOD	0x0
    187  1.27  mjacob #endif
    188  1.27  mjacob #ifndef	SCSI_CHECK
    189  1.27  mjacob #define	SCSI_CHECK	0x2
    190  1.27  mjacob #endif
    191  1.27  mjacob #ifndef	SCSI_BUSY
    192  1.27  mjacob #define	SCSI_BUSY	0x8
    193  1.27  mjacob #endif
    194  1.27  mjacob #ifndef	SCSI_QFULL
    195  1.27  mjacob #define	SCSI_QFULL	0x28
    196  1.27  mjacob #endif
    197  1.26  mjacob 
    198  1.27  mjacob #define	XS_T			struct scsipi_xfer
    199  1.40  bouyer #define	XS_CHANNEL(xs)		((int) (xs)->xs_periph->periph_channel->chan_channel)
    200  1.40  bouyer #define	XS_ISP(xs)		\
    201  1.40  bouyer 	((void *)(xs)->xs_periph->periph_channel->chan_adapter->adapt_dev)
    202  1.40  bouyer #define	XS_LUN(xs)		((int) (xs)->xs_periph->periph_lun)
    203  1.40  bouyer #define	XS_TGT(xs)		((int) (xs)->xs_periph->periph_target)
    204  1.27  mjacob #define	XS_CDBP(xs)		((caddr_t) (xs)->cmd)
    205  1.27  mjacob #define	XS_CDBLEN(xs)		(xs)->cmdlen
    206   1.1  mjacob #define	XS_XFRLEN(xs)		(xs)->datalen
    207   1.1  mjacob #define	XS_TIME(xs)		(xs)->timeout
    208  1.27  mjacob #define	XS_RESID(xs)		(xs)->resid
    209  1.27  mjacob #define	XS_STSP(xs)		(&(xs)->status)
    210   1.1  mjacob #define	XS_SNSP(xs)		(&(xs)->sense.scsi_sense)
    211  1.27  mjacob #define	XS_SNSLEN(xs)		(sizeof (xs)->sense)
    212   1.1  mjacob #define	XS_SNSKEY(xs)		((xs)->sense.scsi_sense.flags)
    213  1.27  mjacob #define	XS_TAG_P(ccb)		(((xs)->xs_control & XS_CTL_POLL) != 0)
    214  1.27  mjacob #define	XS_TAG_TYPE(xs)	\
    215  1.28     tls 	(((xs)->xs_control & XS_CTL_URGENT) ? REQFLAG_HTAG : \
    216  1.28     tls 	((xs)->bp && xs->bp->b_flags & B_ASYNC) ? REQFLAG_STAG : REQFLAG_OTAG)
    217  1.27  mjacob 
    218  1.27  mjacob #define	XS_SETERR(xs, v)	(xs)->error = v
    219  1.27  mjacob 
    220  1.27  mjacob #	define	HBA_NOERROR		XS_NOERROR
    221  1.27  mjacob #	define	HBA_BOTCH		XS_DRIVER_STUFFUP
    222  1.27  mjacob #	define	HBA_CMDTIMEOUT		XS_TIMEOUT
    223  1.27  mjacob #	define	HBA_SELTIMEOUT		XS_SELTIMEOUT
    224  1.27  mjacob #	define	HBA_TGTBSY		XS_BUSY
    225  1.27  mjacob #	define	HBA_BUSRESET		XS_RESET
    226  1.27  mjacob #	define	HBA_ABORTED		XS_DRIVER_STUFFUP
    227  1.27  mjacob #	define	HBA_DATAOVR		XS_DRIVER_STUFFUP
    228  1.27  mjacob #	define	HBA_ARQFAIL		XS_DRIVER_STUFFUP
    229   1.1  mjacob 
    230  1.27  mjacob #define	XS_ERR(xs)		(xs)->error
    231  1.27  mjacob 
    232  1.27  mjacob #define	XS_NOERR(xs)		(xs)->error == XS_NOERROR
    233  1.27  mjacob 
    234  1.27  mjacob #define	XS_INITERR(xs)		(xs)->error = 0, XS_CMD_S_CLEAR(xs)
    235  1.27  mjacob 
    236  1.27  mjacob #define	XS_SAVE_SENSE(xs, sp)				\
    237  1.34  mjacob 	if (xs->error == XS_NOERROR) {			\
    238  1.34  mjacob 		xs->error = XS_SENSE;			\
    239  1.34  mjacob 	}						\
    240  1.27  mjacob 	bcopy(sp->req_sense_data, &(xs)->sense,		\
    241  1.27  mjacob 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
    242  1.27  mjacob 
    243  1.27  mjacob #define	XS_SET_STATE_STAT(a, b, c)
    244  1.27  mjacob 
    245  1.27  mjacob #define	DEFAULT_IID(x)		7
    246  1.27  mjacob #define	DEFAULT_LOOPID(x)	108
    247  1.27  mjacob #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.un._wwn
    248  1.30  mjacob #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.un._wwn
    249  1.30  mjacob #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
    250  1.30  mjacob #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
    251  1.27  mjacob 
    252  1.32  mjacob #if	_BYTE_ORDER == _BIG_ENDIAN
    253  1.32  mjacob #define	ISP_SWIZZLE_REQUEST		isp_swizzle_request
    254  1.32  mjacob #define	ISP_UNSWIZZLE_RESPONSE		isp_unswizzle_response
    255  1.32  mjacob #define	ISP_SWIZZLE_ICB			isp_swizzle_icb
    256  1.32  mjacob #define	ISP_UNSWIZZLE_AND_COPY_PDBP	isp_unswizzle_and_copy_pdbp
    257  1.32  mjacob #define	ISP_SWIZZLE_SNS_REQ		isp_swizzle_sns_req
    258  1.32  mjacob #define	ISP_UNSWIZZLE_SNS_RSP		isp_unswizzle_sns_rsp
    259  1.32  mjacob #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
    260   1.3  mjacob #else
    261  1.27  mjacob #define	ISP_SWIZZLE_REQUEST(a, b)
    262  1.27  mjacob #define	ISP_UNSWIZZLE_RESPONSE(a, b, c)
    263  1.32  mjacob #define	ISP_SWIZZLE_ICB(a, b)
    264  1.32  mjacob #define	ISP_UNSWIZZLE_AND_COPY_PDBP(isp, dest, src)	\
    265  1.32  mjacob 	if((void *)src != (void *)dest) bcopy(src, dest, sizeof (isp_pdb_t))
    266  1.27  mjacob #define	ISP_SWIZZLE_SNS_REQ(a, b)
    267  1.27  mjacob #define	ISP_UNSWIZZLE_SNS_RSP(a, b, c)
    268  1.27  mjacob #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
    269  1.27  mjacob #endif
    270  1.27  mjacob 
    271  1.27  mjacob /*
    272  1.27  mjacob  * Includes of common header files
    273  1.27  mjacob  */
    274   1.1  mjacob 
    275  1.27  mjacob #include <dev/ic/ispreg.h>
    276  1.27  mjacob #include <dev/ic/ispvar.h>
    277  1.27  mjacob #include <dev/ic/ispmbox.h>
    278  1.39  mjacob #include <dev/ic/isp_ioctl.h>
    279  1.27  mjacob 
    280  1.27  mjacob /*
    281  1.27  mjacob  * isp_osinfo definitions, extensions and shorthand.
    282  1.27  mjacob  */
    283  1.27  mjacob #define	isp_name	isp_osinfo._dev.dv_xname
    284  1.27  mjacob #define	isp_unit	isp_osinfo._dev.dv_unit
    285  1.43  mjacob #define	isp_chanA	isp_osinfo._chan
    286  1.43  mjacob #define	isp_chanB	isp_osinfo._chan_b
    287  1.43  mjacob 
    288  1.27  mjacob 
    289  1.27  mjacob /*
    290  1.27  mjacob  * Driver prototypes..
    291  1.27  mjacob  */
    292  1.38  mjacob void isp_attach(struct ispsoftc *);
    293  1.38  mjacob void isp_uninit(struct ispsoftc *);
    294  1.27  mjacob 
    295  1.42  mjacob static INLINE void isp_lock(struct ispsoftc *);
    296  1.42  mjacob static INLINE void isp_unlock(struct ispsoftc *);
    297  1.42  mjacob static INLINE char *strncat(char *, const char *, size_t);
    298  1.42  mjacob static INLINE u_int64_t
    299  1.38  mjacob isp_microtime_sub(struct timeval *, struct timeval *);
    300  1.38  mjacob static void isp_wait_complete(struct ispsoftc *);
    301  1.32  mjacob #if	_BYTE_ORDER == _BIG_ENDIAN
    302  1.42  mjacob static INLINE void isp_swizzle_request(struct ispsoftc *, ispreq_t *);
    303  1.42  mjacob static INLINE void isp_unswizzle_response(struct ispsoftc *, void *, u_int16_t);
    304  1.42  mjacob static INLINE void isp_swizzle_icb(struct ispsoftc *, isp_icb_t *);
    305  1.42  mjacob static INLINE void
    306  1.32  mjacob isp_unswizzle_and_copy_pdbp(struct ispsoftc *, isp_pdb_t *, void *);
    307  1.42  mjacob static INLINE void isp_swizzle_sns_req(struct ispsoftc *, sns_screq_t *);
    308  1.42  mjacob static INLINE void isp_unswizzle_sns_rsp(struct ispsoftc *, sns_scrsp_t *, int);
    309  1.32  mjacob #endif
    310  1.27  mjacob 
    311  1.27  mjacob /*
    312  1.27  mjacob  * Driver wide data...
    313  1.27  mjacob  */
    314  1.27  mjacob 
    315  1.27  mjacob /*
    316  1.27  mjacob  * Locking macros...
    317  1.27  mjacob  */
    318  1.27  mjacob #define	ISP_LOCK		isp_lock
    319  1.27  mjacob #define	ISP_UNLOCK		isp_unlock
    320  1.29  mjacob #define	ISP_ILOCK(x)		isp_lock(x); isp->isp_osinfo.onintstack++
    321  1.29  mjacob #define	ISP_IUNLOCK(x)		isp->isp_osinfo.onintstack--; isp_unlock(x)
    322  1.27  mjacob 
    323  1.27  mjacob /*
    324  1.27  mjacob  * Platform private flags
    325  1.27  mjacob  */
    326   1.1  mjacob 
    327  1.26  mjacob #define	XS_PSTS_INWDOG		0x10000000
    328  1.26  mjacob #define	XS_PSTS_GRACE		0x20000000
    329  1.26  mjacob #define	XS_PSTS_ALL		0x30000000
    330  1.26  mjacob 
    331  1.26  mjacob #define	XS_CMD_S_WDOG(xs)	(xs)->xs_status |= XS_PSTS_INWDOG
    332  1.26  mjacob #define	XS_CMD_C_WDOG(xs)	(xs)->xs_status &= ~XS_PSTS_INWDOG
    333  1.26  mjacob #define	XS_CMD_WDOG_P(xs)	(((xs)->xs_status & XS_PSTS_INWDOG) != 0)
    334  1.26  mjacob 
    335  1.26  mjacob #define	XS_CMD_S_GRACE(xs)	(xs)->xs_status |= XS_PSTS_GRACE
    336  1.26  mjacob #define	XS_CMD_C_GRACE(xs)	(xs)->xs_status &= ~XS_PSTS_GRACE
    337  1.26  mjacob #define	XS_CMD_GRACE_P(xs)	(((xs)->xs_status & XS_PSTS_GRACE) != 0)
    338  1.26  mjacob 
    339  1.26  mjacob #define	XS_CMD_S_DONE(xs)	(xs)->xs_status |= XS_STS_DONE
    340  1.26  mjacob #define	XS_CMD_C_DONE(xs)	(xs)->xs_status &= ~XS_STS_DONE
    341  1.26  mjacob #define	XS_CMD_DONE_P(xs)	(((xs)->xs_status & XS_STS_DONE) != 0)
    342  1.26  mjacob 
    343  1.26  mjacob #define	XS_CMD_S_CLEAR(xs)	(xs)->xs_status &= ~XS_PSTS_ALL
    344  1.26  mjacob 
    345   1.1  mjacob /*
    346  1.27  mjacob  * Platform specific 'inline' or support functions
    347   1.1  mjacob  */
    348  1.42  mjacob static INLINE void
    349  1.38  mjacob isp_lock(struct ispsoftc *isp)
    350  1.27  mjacob {
    351  1.27  mjacob 	int s = splbio();
    352  1.29  mjacob 	if (isp->isp_osinfo.islocked++ == 0) {
    353  1.27  mjacob 		isp->isp_osinfo.splsaved = s;
    354  1.27  mjacob 	} else {
    355  1.27  mjacob 		splx(s);
    356  1.27  mjacob 	}
    357  1.27  mjacob }
    358   1.1  mjacob 
    359  1.42  mjacob static INLINE void
    360  1.38  mjacob isp_unlock(struct ispsoftc *isp)
    361  1.27  mjacob {
    362  1.30  mjacob 	if (isp->isp_osinfo.islocked-- <= 1) {
    363  1.27  mjacob 		isp->isp_osinfo.islocked = 0;
    364  1.27  mjacob 		splx(isp->isp_osinfo.splsaved);
    365  1.27  mjacob 	}
    366  1.27  mjacob }
    367   1.1  mjacob 
    368  1.42  mjacob static INLINE char *
    369  1.38  mjacob strncat(char *d, const char *s, size_t c)
    370  1.26  mjacob {
    371  1.26  mjacob         char *t = d;
    372  1.26  mjacob 
    373  1.26  mjacob         if (c) {
    374  1.26  mjacob                 while (*d)
    375  1.26  mjacob                         d++;
    376  1.26  mjacob                 while ((*d++ = *s++)) {
    377  1.26  mjacob                         if (--c == 0) {
    378  1.26  mjacob                                 *d = '\0';
    379  1.26  mjacob                                 break;
    380  1.26  mjacob                         }
    381  1.26  mjacob                 }
    382  1.26  mjacob         }
    383  1.26  mjacob         return (t);
    384  1.26  mjacob }
    385  1.26  mjacob 
    386  1.42  mjacob static INLINE u_int64_t
    387  1.38  mjacob isp_microtime_sub(struct timeval *b, struct timeval *a)
    388  1.27  mjacob {
    389  1.27  mjacob 	struct timeval x;
    390  1.27  mjacob 	u_int64_t elapsed;
    391  1.27  mjacob 	timersub(b, a, &x);
    392  1.27  mjacob 	elapsed = GET_NANOSEC(&x);
    393  1.27  mjacob 	if (elapsed == 0)
    394  1.27  mjacob 		elapsed++;
    395  1.27  mjacob 	return (elapsed);
    396  1.27  mjacob }
    397  1.18  mjacob 
    398  1.42  mjacob static INLINE void
    399  1.38  mjacob isp_wait_complete(struct ispsoftc *isp)
    400  1.27  mjacob {
    401  1.27  mjacob 	if (isp->isp_osinfo.onintstack || isp->isp_osinfo.no_mbox_ints) {
    402  1.27  mjacob 		int usecs = 0;
    403  1.34  mjacob 		/*
    404  1.34  mjacob 		 * For sanity's sake, we don't delay longer
    405  1.34  mjacob 		 * than 5 seconds for polled commands.
    406  1.34  mjacob 		 */
    407  1.34  mjacob 		while (usecs < 5 * 1000000) {
    408  1.27  mjacob 			(void) isp_intr(isp);
    409  1.27  mjacob 			if (isp->isp_mboxbsy == 0) {
    410  1.27  mjacob 				break;
    411  1.27  mjacob 			}
    412  1.27  mjacob 			USEC_DELAY(500);
    413  1.27  mjacob 			usecs += 500;
    414  1.27  mjacob 		}
    415  1.27  mjacob 		if (isp->isp_mboxbsy != 0) {
    416  1.34  mjacob 			isp_prt(isp, ISP_LOGWARN,
    417  1.34  mjacob 			    "Polled Mailbox Command (0x%x) Timeout",
    418  1.36  mjacob 			    isp->isp_lastmbxcmd);
    419  1.27  mjacob 		}
    420  1.27  mjacob 	} else {
    421  1.27  mjacob 		int rv = 0;
    422  1.27  mjacob                 isp->isp_osinfo.mboxwaiting = 1;
    423  1.27  mjacob                 while (isp->isp_osinfo.mboxwaiting && rv == 0) {
    424  1.35  mjacob 			static const struct timeval dtime = { 5, 0 };
    425  1.27  mjacob 			int timo;
    426  1.27  mjacob 			struct timeval tv;
    427  1.27  mjacob 			microtime(&tv);
    428  1.34  mjacob 			timeradd(&tv, &dtime, &tv);
    429  1.27  mjacob 			if ((timo = hzto(&tv)) == 0) {
    430  1.27  mjacob 				timo = 1;
    431  1.27  mjacob 			}
    432  1.27  mjacob 			rv = tsleep(&isp->isp_osinfo.mboxwaiting,
    433  1.27  mjacob 			    PRIBIO, "isp_mboxcmd", timo);
    434  1.27  mjacob 		}
    435  1.27  mjacob 		if (rv == EWOULDBLOCK) {
    436  1.27  mjacob 			isp->isp_mboxbsy = 0;
    437  1.27  mjacob 			isp->isp_osinfo.mboxwaiting = 0;
    438  1.34  mjacob 			isp_prt(isp, ISP_LOGWARN,
    439  1.34  mjacob 			    "Interrupting Mailbox Command (0x%x) Timeout",
    440  1.36  mjacob 			    isp->isp_lastmbxcmd);
    441  1.27  mjacob 		}
    442  1.27  mjacob 	}
    443  1.27  mjacob }
    444  1.32  mjacob 
    445  1.32  mjacob #if	_BYTE_ORDER == _BIG_ENDIAN
    446  1.42  mjacob static INLINE void
    447  1.32  mjacob isp_swizzle_request(struct ispsoftc *isp, ispreq_t *rq)
    448  1.32  mjacob {
    449  1.32  mjacob 	if (IS_FC(isp)) {
    450  1.32  mjacob 		ispreqt2_t *tq = (ispreqt2_t *) rq;
    451  1.32  mjacob 		tq->req_handle = bswap32(tq->req_handle);
    452  1.32  mjacob 		tq->req_scclun = bswap16(tq->req_scclun);
    453  1.32  mjacob 		tq->req_flags = bswap16(tq->req_flags);
    454  1.32  mjacob 		tq->req_time = bswap16(tq->req_time);
    455  1.32  mjacob 		tq->req_totalcnt = bswap32(tq->req_totalcnt);
    456  1.32  mjacob 	} else if (isp->isp_bustype == ISP_BT_SBUS) {
    457  1.32  mjacob 		_ISP_SWAP8(rq->req_header.rqs_entry_count,
    458  1.32  mjacob 		    rq->req_header.rqs_entry_type);
    459  1.32  mjacob 		_ISP_SWAP8(rq->req_header.rqs_flags, rq->req_header.rqs_seqno);
    460  1.32  mjacob 		_ISP_SWAP8(rq->req_target, rq->req_lun_trn);
    461  1.32  mjacob 	} else {
    462  1.32  mjacob 		rq->req_handle = bswap32(rq->req_handle);
    463  1.32  mjacob 		rq->req_cdblen = bswap16(rq->req_cdblen);
    464  1.32  mjacob 		rq->req_flags = bswap16(rq->req_flags);
    465  1.32  mjacob 		rq->req_time = bswap16(rq->req_time);
    466  1.32  mjacob 	}
    467  1.32  mjacob }
    468  1.32  mjacob 
    469  1.42  mjacob static INLINE void
    470  1.32  mjacob isp_unswizzle_response(struct ispsoftc *isp, void *rp, u_int16_t optr)
    471  1.32  mjacob {
    472  1.32  mjacob 	ispstatusreq_t *sp = rp;
    473  1.32  mjacob 	MEMORYBARRIER(isp, SYNC_RESPONSE, optr * QENTRY_LEN, QENTRY_LEN);
    474  1.32  mjacob 	if (isp->isp_bustype == ISP_BT_SBUS) {
    475  1.32  mjacob 		_ISP_SWAP8(sp->req_header.rqs_entry_count,
    476  1.32  mjacob 		    sp->req_header.rqs_entry_type);
    477  1.32  mjacob 		_ISP_SWAP8(sp->req_header.rqs_flags, sp->req_header.rqs_seqno);
    478  1.32  mjacob 	} else switch (sp->req_header.rqs_entry_type) {
    479  1.32  mjacob 	case RQSTYPE_RESPONSE:
    480  1.32  mjacob 		sp->req_handle = bswap32(sp->req_handle);
    481  1.32  mjacob 		sp->req_scsi_status = bswap16(sp->req_scsi_status);
    482  1.32  mjacob 		sp->req_completion_status = bswap16(sp->req_completion_status);
    483  1.32  mjacob 		sp->req_state_flags = bswap16(sp->req_state_flags);
    484  1.32  mjacob 		sp->req_status_flags = bswap16(sp->req_status_flags);
    485  1.32  mjacob 		sp->req_time = bswap16(sp->req_time);
    486  1.32  mjacob 		sp->req_sense_len = bswap16(sp->req_sense_len);
    487  1.32  mjacob 		sp->req_resid = bswap32(sp->req_resid);
    488  1.32  mjacob 		break;
    489  1.32  mjacob 	default:
    490  1.32  mjacob 		break;
    491  1.32  mjacob 	}
    492  1.32  mjacob }
    493  1.32  mjacob 
    494  1.42  mjacob static INLINE void
    495  1.32  mjacob isp_swizzle_icb(struct ispsoftc *isp, isp_icb_t *icbp)
    496  1.32  mjacob {
    497  1.32  mjacob 	_ISP_SWAP8(icbp->icb_version, icbp->_reserved0);
    498  1.32  mjacob 	_ISP_SWAP8(icbp->icb_retry_count, icbp->icb_retry_delay);
    499  1.32  mjacob 	_ISP_SWAP8(icbp->icb_iqdevtype, icbp->icb_logintime);
    500  1.32  mjacob 	_ISP_SWAP8(icbp->icb_ccnt, icbp->icb_icnt);
    501  1.32  mjacob 	_ISP_SWAP8(icbp->icb_racctimer, icbp->icb_idelaytimer);
    502  1.32  mjacob 	icbp->icb_fwoptions = bswap16(icbp->icb_fwoptions);
    503  1.32  mjacob 	icbp->icb_maxfrmlen = bswap16(icbp->icb_maxfrmlen);
    504  1.32  mjacob 	icbp->icb_maxalloc = bswap16(icbp->icb_maxalloc);
    505  1.32  mjacob 	icbp->icb_execthrottle = bswap16(icbp->icb_execthrottle);
    506  1.32  mjacob 	icbp->icb_hardaddr = bswap16(icbp->icb_hardaddr);
    507  1.32  mjacob 	icbp->icb_rqstout = bswap16(icbp->icb_rqstout);
    508  1.32  mjacob 	icbp->icb_rspnsin = bswap16(icbp->icb_rspnsin);
    509  1.32  mjacob 	icbp->icb_rqstqlen = bswap16(icbp->icb_rqstqlen);
    510  1.32  mjacob 	icbp->icb_rsltqlen = bswap16(icbp->icb_rsltqlen);
    511  1.32  mjacob 	icbp->icb_lunenables = bswap16(icbp->icb_lunenables);
    512  1.32  mjacob 	icbp->icb_lunetimeout = bswap16(icbp->icb_lunetimeout);
    513  1.32  mjacob 	icbp->icb_xfwoptions = bswap16(icbp->icb_xfwoptions);
    514  1.32  mjacob 	icbp->icb_zfwoptions = bswap16(icbp->icb_zfwoptions);
    515  1.32  mjacob 	icbp->icb_rqstaddr[0] = bswap16(icbp->icb_rqstaddr[0]);
    516  1.32  mjacob 	icbp->icb_rqstaddr[1] = bswap16(icbp->icb_rqstaddr[1]);
    517  1.32  mjacob 	icbp->icb_rqstaddr[2] = bswap16(icbp->icb_rqstaddr[2]);
    518  1.32  mjacob 	icbp->icb_rqstaddr[3] = bswap16(icbp->icb_rqstaddr[3]);
    519  1.32  mjacob 	icbp->icb_respaddr[0] = bswap16(icbp->icb_respaddr[0]);
    520  1.32  mjacob 	icbp->icb_respaddr[1] = bswap16(icbp->icb_respaddr[1]);
    521  1.32  mjacob 	icbp->icb_respaddr[2] = bswap16(icbp->icb_respaddr[2]);
    522  1.32  mjacob 	icbp->icb_respaddr[3] = bswap16(icbp->icb_respaddr[3]);
    523  1.32  mjacob }
    524  1.32  mjacob 
    525  1.42  mjacob static INLINE void
    526  1.32  mjacob isp_unswizzle_and_copy_pdbp(struct ispsoftc *isp, isp_pdb_t *dst, void *src)
    527  1.32  mjacob {
    528  1.32  mjacob 	isp_pdb_t *pdbp = src;
    529  1.32  mjacob 	dst->pdb_options = bswap16(pdbp->pdb_options);
    530  1.32  mjacob 	dst->pdb_mstate = pdbp->pdb_sstate;
    531  1.32  mjacob 	dst->pdb_sstate = pdbp->pdb_mstate;
    532  1.32  mjacob 	dst->pdb_hardaddr_bits[0] = pdbp->pdb_hardaddr_bits[0];
    533  1.32  mjacob 	dst->pdb_hardaddr_bits[1] = pdbp->pdb_hardaddr_bits[1];
    534  1.32  mjacob 	dst->pdb_hardaddr_bits[2] = pdbp->pdb_hardaddr_bits[2];
    535  1.32  mjacob 	dst->pdb_hardaddr_bits[3] = pdbp->pdb_hardaddr_bits[3];
    536  1.32  mjacob 	dst->pdb_portid_bits[0] = pdbp->pdb_portid_bits[0];
    537  1.32  mjacob 	dst->pdb_portid_bits[1] = pdbp->pdb_portid_bits[1];
    538  1.32  mjacob 	dst->pdb_portid_bits[2] = pdbp->pdb_portid_bits[2];
    539  1.32  mjacob 	dst->pdb_portid_bits[3] = pdbp->pdb_portid_bits[3];
    540  1.32  mjacob 	dst->pdb_nodename[0] = pdbp->pdb_nodename[0];
    541  1.32  mjacob 	dst->pdb_nodename[1] = pdbp->pdb_nodename[1];
    542  1.32  mjacob 	dst->pdb_nodename[2] = pdbp->pdb_nodename[2];
    543  1.32  mjacob 	dst->pdb_nodename[3] = pdbp->pdb_nodename[3];
    544  1.32  mjacob 	dst->pdb_nodename[4] = pdbp->pdb_nodename[4];
    545  1.32  mjacob 	dst->pdb_nodename[5] = pdbp->pdb_nodename[5];
    546  1.32  mjacob 	dst->pdb_nodename[6] = pdbp->pdb_nodename[6];
    547  1.32  mjacob 	dst->pdb_nodename[7] = pdbp->pdb_nodename[7];
    548  1.32  mjacob 	dst->pdb_portname[0] = pdbp->pdb_portname[0];
    549  1.32  mjacob 	dst->pdb_portname[1] = pdbp->pdb_portname[1];
    550  1.32  mjacob 	dst->pdb_portname[2] = pdbp->pdb_portname[2];
    551  1.32  mjacob 	dst->pdb_portname[3] = pdbp->pdb_portname[3];
    552  1.32  mjacob 	dst->pdb_portname[4] = pdbp->pdb_portname[4];
    553  1.32  mjacob 	dst->pdb_portname[5] = pdbp->pdb_portname[5];
    554  1.32  mjacob 	dst->pdb_portname[6] = pdbp->pdb_portname[6];
    555  1.32  mjacob 	dst->pdb_portname[7] = pdbp->pdb_portname[7];
    556  1.32  mjacob 	dst->pdb_execthrottle = bswap16(pdbp->pdb_execthrottle);
    557  1.32  mjacob 	dst->pdb_exec_count = bswap16(pdbp->pdb_exec_count);
    558  1.32  mjacob 	dst->pdb_resalloc = bswap16(pdbp->pdb_resalloc);
    559  1.32  mjacob 	dst->pdb_curalloc = bswap16(pdbp->pdb_curalloc);
    560  1.32  mjacob 	dst->pdb_qhead = bswap16(pdbp->pdb_qhead);
    561  1.32  mjacob 	dst->pdb_qtail = bswap16(pdbp->pdb_qtail);
    562  1.32  mjacob 	dst->pdb_tl_next = bswap16(pdbp->pdb_tl_next);
    563  1.32  mjacob 	dst->pdb_tl_last = bswap16(pdbp->pdb_tl_last);
    564  1.32  mjacob 	dst->pdb_features = bswap16(pdbp->pdb_features);
    565  1.32  mjacob 	dst->pdb_pconcurrnt = bswap16(pdbp->pdb_pconcurrnt);
    566  1.32  mjacob 	dst->pdb_roi = bswap16(pdbp->pdb_roi);
    567  1.32  mjacob 	dst->pdb_rdsiz = bswap16(pdbp->pdb_rdsiz);
    568  1.32  mjacob 	dst->pdb_ncseq = bswap16(pdbp->pdb_ncseq);
    569  1.32  mjacob 	dst->pdb_noseq = bswap16(pdbp->pdb_noseq);
    570  1.32  mjacob 	dst->pdb_labrtflg = bswap16(pdbp->pdb_labrtflg);
    571  1.32  mjacob 	dst->pdb_lstopflg = bswap16(pdbp->pdb_lstopflg);
    572  1.32  mjacob 	dst->pdb_sqhead = bswap16(pdbp->pdb_sqhead);
    573  1.32  mjacob 	dst->pdb_sqtail = bswap16(pdbp->pdb_sqtail);
    574  1.32  mjacob 	dst->pdb_ptimer = bswap16(pdbp->pdb_ptimer);
    575  1.32  mjacob 	dst->pdb_nxt_seqid = bswap16(pdbp->pdb_nxt_seqid);
    576  1.32  mjacob 	dst->pdb_fcount = bswap16(pdbp->pdb_fcount);
    577  1.32  mjacob 	dst->pdb_prli_len = bswap16(pdbp->pdb_prli_len);
    578  1.32  mjacob 	dst->pdb_prli_svc0 = bswap16(pdbp->pdb_prli_svc0);
    579  1.32  mjacob 	dst->pdb_prli_svc3 = bswap16(pdbp->pdb_prli_svc3);
    580  1.32  mjacob 	dst->pdb_loopid = bswap16(pdbp->pdb_loopid);
    581  1.32  mjacob 	dst->pdb_il_ptr = bswap16(pdbp->pdb_il_ptr);
    582  1.32  mjacob 	dst->pdb_sl_ptr = bswap16(pdbp->pdb_sl_ptr);
    583  1.32  mjacob 	dst->pdb_retry_count = pdbp->pdb_retry_delay;
    584  1.32  mjacob 	dst->pdb_retry_delay = pdbp->pdb_retry_count;
    585  1.32  mjacob 	dst->pdb_target = pdbp->pdb_initiator;
    586  1.32  mjacob 	dst->pdb_initiator = pdbp->pdb_target;
    587  1.32  mjacob }
    588  1.32  mjacob 
    589  1.42  mjacob static INLINE void
    590  1.32  mjacob isp_swizzle_sns_req(struct ispsoftc *isp, sns_screq_t *reqp)
    591  1.32  mjacob {
    592  1.32  mjacob 	u_int16_t index, nwords = reqp->snscb_sblen;
    593  1.32  mjacob 	reqp->snscb_rblen = bswap16(reqp->snscb_rblen);
    594  1.32  mjacob 	reqp->snscb_addr[0] = bswap16(reqp->snscb_addr[0]);
    595  1.32  mjacob 	reqp->snscb_addr[1] = bswap16(reqp->snscb_addr[1]);
    596  1.32  mjacob 	reqp->snscb_addr[2] = bswap16(reqp->snscb_addr[2]);
    597  1.32  mjacob 	reqp->snscb_addr[3] = bswap16(reqp->snscb_addr[3]);
    598  1.32  mjacob 	reqp->snscb_sblen = bswap16(reqp->snscb_sblen);
    599  1.32  mjacob 	for (index = 0; index < nwords; index++) {
    600  1.32  mjacob 		reqp->snscb_data[index] = bswap16(reqp->snscb_data[index]);
    601  1.32  mjacob 	}
    602  1.32  mjacob }
    603  1.32  mjacob 
    604  1.42  mjacob static INLINE void
    605  1.32  mjacob isp_unswizzle_sns_rsp(struct ispsoftc *isp, sns_scrsp_t *resp, int nwords)
    606  1.32  mjacob {
    607  1.32  mjacob 	int index;
    608  1.36  mjacob 	/*
    609  1.36  mjacob 	 * Don't even think about asking. This. Is. So. Lame.
    610  1.36  mjacob 	 */
    611  1.36  mjacob 	if (IS_2200(isp) &&
    612  1.36  mjacob 	    ISP_FW_REVX(isp->isp_fwrev) == ISP_FW_REV(2, 1, 26)) {
    613  1.36  mjacob 		nwords = 128;
    614  1.36  mjacob 	}
    615  1.32  mjacob 	for (index = 0; index < nwords; index++) {
    616  1.32  mjacob 		resp->snscb_data[index] = bswap16(resp->snscb_data[index]);
    617  1.32  mjacob 	}
    618  1.32  mjacob }
    619  1.32  mjacob #endif
    620  1.27  mjacob 
    621  1.27  mjacob /*
    622  1.27  mjacob  * Common inline functions
    623  1.27  mjacob  */
    624  1.18  mjacob #include <dev/ic/isp_inline.h>
    625  1.18  mjacob 
    626  1.19  mjacob #if	!defined(ISP_DISABLE_FW) && !defined(ISP_COMPILE_FW)
    627  1.19  mjacob #define	ISP_COMPILE_FW	1
    628  1.19  mjacob #endif
    629   1.1  mjacob #endif	/* _ISP_NETBSD_H */
    630