/*
 *****************************************************************
 *                                                               *
 *    Copyright (c) Essential Communications Corp, 1997          *
 *                                                               *
 *   All Rights Reserved.  Unpublished rights  reserved  under   *
 *   the copyright laws of the United States.                    *
 *                                                               *
 *   RESTRICTED RIGHTS LEGEND   Use, duplication, or disclosure  *
 *   by the U.S. Government is subject to restrictions  as  set  *
 *   forth in Subparagraph (c)(1)(ii)  of  DFARS  252.227-7013,  *
 *   or  in  FAR 52.227-19, as applicable.                       *
 *                                                               *
 *****************************************************************
 *
 * 	essioctl.h
 *
 * $Source: /usr/users/releng/repos/src/driver/essioctl.h,v $
 * $Revision: 1.30 $ $Locker:  $
 * $Date: 1999/01/05 23:44:55 $ $Author: droux $ $State: Exp $
 */

#ifndef _ESSIOCTL_H_
#define _ESSIOCTL_H_

/*
 * Ioctls that support the character API. User applications.
 */

/* ioctl structures  -  Character device driver */

#if defined(__sgi)
struct ess_times {		/* a tick is 160 nsec or 120 nsec */
    int preemptTime;		/* number of ticks before pre-empt */
    int quietTime;		/* number of ticks we stay quiet */
};
#endif /* defined(__sgi) */

#if defined(sun)
#include <sys/ioccom.h>
#endif /* defined(sun) */

/*
 * Character API Ioctls
 *  In the following ioctls the ioctl number should not be changed as
 *  long as binary compatability with 2.0 is retained.
 */
#define HIPIOC_GET_DEVICE_STATE _IOR('e', 34, int)  /* NIC operating mode */
#define HIP_ON		0x01
#define HIP_LONG	0x02
#define HIP_PH		0x04
#define HIP_LOOPBACK	0x08
#define HIP_DIRECT	0x10
#define HIP_IPI3M	0x20
#define HIP_IPI3M_ACT	0x40
#define HIPIOC_BIND_ULP  _IO('e', 20)	     /* bind the ULP to the device */
#define HIPIOC_UNBIND_ULP  _IO('e', 35)	      /* unbind the ULP to the device */
/* Special bind values for HIPPI-PH */
#define HIPPI_ULP_PH        1	/* Exclusive access to HIPPI-PH channel */
#define HIPPI_ULP_PH_EXCL   1	/* Exclusive access to HIPPI-PH channel */
#define HIPPI_ULP_PH_SHR   -1	/* Shared access to HIPPI-PH channel */

#define HIPIOCW_I        _IO('e', 22)	     /* set the Ifield up */
#define HIPIOCW_CONNECT  _IO('e', 23)	     /* connect to specified Ifield */
#define HIPIOCW_DISCONN  _IO('e', 24)        /* disconnect previous connect */
#define HIPIOCW_D1_AREA  _IO('e', 45)  /* Set  D1 */
#define HIPIOCW_D1_AREA_PTR _IO('e', 46) /* Set  D1 ptr */
#define HIPIOCW_SEP_HDR  _IO('e', 42)	      /* Separate data and headers */
#define HIPIOCW_PERM     _IO('e', 43)	      /* Permanent connection */
#define HIPIOCW_SET_ULP  _IO('e', 44)	      /* Set send ULP */

#define HIPIOCW_START_PKT _IO('e', 26)	     /* begin a multi-write packet */
/* special value for the HIPIOCW_START_PKT ioctl */
#define HIPIOCW_SHBURST  _IO('e', 27)	     /* set a short burst, possible? */
#define HIPIOCW_END_PKT  _IO('e', 28)        /* end the current packet */
#define HIPIOCW_D1_SIZE  _IO('e', 29)	     /* set D1 size */

#define HIPIOCW_ERR      _IOR('e', 31, int)  /* write error matrix */
/* the following come from the SGI implementation.  We can't tell the
 * difference between a timeout and a rejection, so we return HIP_SRCERR_TIMEO
 * for both of these cases, we've also added HIP_SRCERR_SW for unexpected
 * driver failures */
#define HIP_SRCERR_NONE		0
#define HIP_SRCERR_SEQ		1	     /* HIPPI source sequence err */
#define HIP_SRCERR_DSIC		2	     /* Source lost DSIC */
#define HIP_SRCERR_TIMEO	3	     /* Source timed out xmit */
#define HIP_SRCERR_CONNLS	4	     /* Source lost CONNECT signal*/
#define HIP_SRCERR_REJ		5	     /* Connect req was rejected*/
#define HIP_SRCERR_SHUT		6	     /* Interface shut down */
#define HIP_SRCERR_SW           7	     /* Unexpected driver failure */

/* return bytes read so far */
#define HIPIOCR_PKT_OFFSET _IOR('e', 25, unsigned int)
#define HIPIOCR_EIO        _IO('e', 33)       /* Set EIO processing mode */
#define HIPIOCR_SEP_HDR    _IO('e', 36)	      /* Separate data and headers */
#define HIPIOCR_GET_D1     _IOR('e', 37, int) /* Get D1 header */
#define HIPIOCR_GET_FP     _IOR('e', 38, int) /* Get  FP header */
#define HIPIOCR_TRUNCATE_PKT _IO('e', 39)     /* Truncate cur pkt */

#define HIPIOCR_ERRS       _IOR('e', 30, int)  /* read error matrix */
#define HIP_DSTERR_PARITY	0x01	/* Destination parity error */
#define HIP_DSTERR_LLRC		0x02	/* Destination LLRC error */
#define HIP_DSTERR_FRAME	0x04	/* Glink- Frame error */
#define HIP_DSTERR_SYNC		0x08	/* Glink- Sync error */
#define HIP_DSTERR_ILBURST	0x10	/* Destination illegal burst error */
#define HIP_DSTERR_SDIC		0x20	/* Destination SDIC lost error */
#define HIP_DSTERR_SHBST	0x40	/* Unexpected short burst */
#define HIP_DSTERR_RDY		0x80	/* Glink- Lost Link ready */
#define HIP_DSTERR_RXTO		0x100	/* Receive Timeout */
#define HIP_DSTERR_PKTLEN	0x200	/* Packet length error */

#define HIPIOC_GET_NICS    _IOR('e', 49, int) /* Return number of NICs in sys*/

/* This is not called directly by application */
#define HIPIOC_GET_DEV     _IOR('e', 15, int) /* return free device number */

#if defined(__sgi)
#define ESS_SET_PREEMPT _IOW('e', 16, struct ess_times)	/* set SGI preempt */
#define ESS_GET_PREEMPT _IOR('e', 17, struct ess_times)	/* get SGI preempt */
#endif /* defined(__sgi) */

/* Infinite packet length */
#define D2SIZE_UNKNOWN	 0xffffffff


#ifndef _KERNEL			/* user only macro */

/* basedev values for usual NICs */
#if !defined(ESS_GIGE)
#define NIC0 "/dev/hippi/h0"
#define NIC1 "/dev/hippi/h1"
#define NIC2 "/dev/hippi/h2"
#define NIC3 "/dev/hippi/h3"
#define NIC4 "/dev/hippi/h4"
#define NIC5 "/dev/hippi/h5"
#define NIC6 "/dev/hippi/h6"
#define NIC7 "/dev/hippi/h7"
#define NIC8 "/dev/hippi/h8"
#define NIC9 "/dev/hippi/h9"
#define NIC10 "/dev/hippi/h10"
#define NIC11 "/dev/hippi/h11"
#define NIC12 "/dev/hippi/h12"
#define NIC13 "/dev/hippi/h13"
#define NIC14 "/dev/hippi/h14"
#define NIC15 "/dev/hippi/h15"
#else
#define NIC0 "/dev/gige/g0"
#define NIC1 "/dev/gige/g1"
#define NIC2 "/dev/gige/g2"
#define NIC3 "/dev/gige/g3"
#define NIC4 "/dev/gige/g4"
#define NIC5 "/dev/gige/g5"
#define NIC6 "/dev/gige/g6"
#define NIC7 "/dev/gige/g7"
#define NIC8 "/dev/gige/g8"
#define NIC9 "/dev/gige/g9"
#define NIC10 "/dev/gige/g10"
#define NIC11 "/dev/gige/g11"
#define NIC12 "/dev/gige/g12"
#define NIC13 "/dev/gige/g13"
#define NIC14 "/dev/gige/g14"
#define NIC15 "/dev/gige/g15"
#endif	/* defined(ESS_GIGE) */

#define DEV_NAME_TEMPLATE "%s.%d"
#define DEV_LENGTH 24		/* up to device 999 plus the '\0' */

/* macro to handle single line open application device */
#define HIP_APP_OPEN(basedev, flags, rc) { \
    int _hip_app_open_fd1, _hip_app_open_fd2; \
    char _hip_app_open_s[DEV_LENGTH]; \
\
    _hip_app_open_fd1 = open(basedev, 0); \
    if (_hip_app_open_fd1 < 0) \
        rc = _hip_app_open_fd1; \
    else { \
    extern int errno; \
    int _hip_app_open_trys=0;	\
	for(;;) { \
	    _hip_app_open_trys++;	\
            rc = ioctl(_hip_app_open_fd1, HIPIOC_GET_DEV, &_hip_app_open_fd2); \
            if (rc >= 0 && _hip_app_open_fd2 >= 0) { \
                (void) sprintf(_hip_app_open_s, DEV_NAME_TEMPLATE, basedev, \
			_hip_app_open_fd2); \
                rc = open(_hip_app_open_s, flags); \
	        if (rc < 0) { \
		    if (errno == 16/*EBUSY*/ && _hip_app_open_trys < 4 ) \
			continue; \
		    else { \
            	        (void) close(_hip_app_open_fd1); \
		        break; \
		    } \
		} else { \
            	    (void) close(_hip_app_open_fd1); \
		    break; \
		} \
            } else { \
		(void) close(_hip_app_open_fd1); \
		break; \
	    } \
        } \
    } \
}
#endif /* _KERNEL */

/*
 * Obsolete/compatability  CDI Ioctls
 *  The ioctls exist and behave as in the 2.0 release with some
 *  extensions. Enetually, these will be moved to essdioctl.h or deleted.
 */
#define ESS_ALLOW_LARGE_XFERS _IO('e', 10) /* disallow large xfers */
#define ESS_DISALLOW_LARGE_XFERS _IO('e', 11) /* disallow large xfers */

#define HIPPI_SETONOFF   _IO('e', 13) /* turn on/off HIPPI */
#define	HIPPI_OFF	    0	/* Turn off HIPPI */
#define	HIPPI_OFF_DUMP	    1	/* Turn off HIPPI, dump NIC */
#define HIPPI_ON	    2	/* Turn on HIPPI */
#define HIPPI_RESTART	    3	/* Restart HIPPI */
#define HIPPI_RESTART_DUMP  4	/* Restart HIPPI, dump NIC */
#define	HIPPI_PH	    5	/* Set HIPPI on in HIPPI-PH mode */
#define	HIPPI_FP	    6	/* Set HIPPI on in HIPPI-FP mode */
#define	HIPPI_LOOPBACK	    7	/* Set HIPPI on in HW loopback mode */
#define	HIPPI_NETWORK       8	/* Set HIPPI on in HW network mode */
#define	HIPPI_DIRECT        9	/* Two NICs directly connected */
#define	HIPPI_SWITCHED     10	/* NIC connected to switch */
#define HIPIOC_ACCEPT_FLAG _IO('e', 14) /* accept or reject conns */

#define ESS_GETSTRACE    _IOWR('e', 18, int *)    /* read snap trace buffer */

#define HIPIOC_GET_STATS _IOWR('e', 12, nic_stats_t) /* get stats, SGI style*/
typedef struct nic_stats {
    unsigned int hst_flags;		/* status flags */

#define HST_FLAG_DSIC       0x0001 /* SRC sees interconnect */
#define HST_FLAG_SDIC       0x0002 /* DST sees interconnect */
#define HST_FLAG_DST_ACCEPT 0x0010 /* DST is accepting connections */
#define HST_FLAG_DST_PKTIN  0x0020 /* DST: PACKET is high */
#define HST_FLAG_DST_REQIN  0x0040 /* DST REQUEST input is high */
#define HST_FLAG_SRC_REQOUT 0x0100 /* SRC: REQUEST is asserted */
#define HST_FLAG_SRC_CONN   0x0200 /* SRC: CONNECT input is high */
#define HST_FLAG_SW_ACTIVE  0x0400 /* software: administratively up */
#define HST_FLAG_SW_LONG    0x0800 /* software: legal to send long packets */
#define HST_FLAG_PH	    0x1000 /* software: NIC is in HIPPI-PH mode */
#define HST_FLAG_LOOPBACK   0x2000 /* software: NIC is cfg Loopback mode */
#define HST_FLAG_DIRECT     0x4000 /* software: NIC is cfg direct mode */
#define HST_FLAG_IS_LOOPBACK 0x8000 /* software: NIC is in Loopback mode */
#define HST_FLAG_IS_DIRECT 0x10000 /* software: NIC is in direct mode */
#define HST_FLAG_LINK_ON   0x20000 /* software: Optical link is on */
#define HST_FLAG_FW_UP     0x40000 /* software: Runcode is on */
#define HST_FLAG_IPI3M     0x80000 /* software: IPI3 Master configured */
#define HST_FLAG_IPI3MA   0x100000 /* software: IPI3 Master active */

    /* Source statistics */
    unsigned int hst_s_conns;		/* total connections attemptd */
    unsigned int hst_s_packets;	/* total packets set */
    unsigned int hst_s_rejects;	/* connection attempts rejected */
    unsigned int hst_s_dm_seqerrs;	/* data sm sequence error */
    unsigned int hst_s_cd_seqerrs;	/* conn sm sequence error, dst */
    unsigned int hst_s_cs_seqerrs;	/* conn sm sequence error, src */
    unsigned int hst_s_dsic_lost;
    unsigned int hst_s_timeo;		/* timed out connections attempts */
    unsigned int hst_s_connls;	/* connections dropped by other side */
    unsigned int hst_s_par_err;	/* source parity error */
    unsigned int hst_s_resvd[6];	/* reserved for future statistics */

    /* destination statistics */
    unsigned int hst_d_conns;		/* total connections accepted */
    unsigned int hst_d_packets;		/* total packets received */
    unsigned int hst_d_badulps;		/* pkts dropped due to unknown ULP */
    unsigned int hst_d_ledrop;		/* HIPPI-LE packets dropped */
    unsigned int hst_d_llrc;		/* conns dropped due to llrc error */
    unsigned int hst_d_par_err;		/* conns dropped due to parity err */
    unsigned int hst_d_seq_err;		/* conns dropped due to sequence err */
    unsigned int hst_d_sync;		/* sync errors */
    unsigned int hst_d_illbrst;		/* packet with illegal burst sizes */
    unsigned int hst_d_sdic_lost;	/* conns dropped due to sdic lost */
    unsigned int hst_d_nullconn;	/* connections with zero packets */
    unsigned int hst_d_resvd[5];	/* reserved for future statistics */

} nic_stats_t;

#define HIPIOC_STIMEO    _IO('e', 21)	     /* set the timeout value */
#define HIPIOCR_ONE_READ _IO('e', 32)	     /* for D1/D2 together */

#endif  /* _ESSIOCTL_H_ */
