/*****************************************************************************
 * Copyright (c) 1988-95 Digi International Inc., All Rights Reserved
 * 
 * This  software  contains  proprietary  and  confidential   information   of
 * Digi International  Inc.   By  accepting  transfer  of this copy, Recipient
 * agrees to retain this software in  confidence,  to  prevent  disclosure  to
 * others,  and  to make no  use of this software other than that for which it
 * was delivered.  This is an unpublished copyright work  Digi   International
 * Inc.    Except   as   permitted   by  federal  law,  17 USC 117, copying is
 * strictly prohibited.
 * 
 * Digi  International  Inc.  CONFIDENTIAL  -   (Digi    International    Inc.
 * Confidential  when   combined   with   the   aggregated  modules  for  this
 * product) OBJECT CODE ONLY SOURCE MATERIALS
 * 
 * (C) COPYRIGHT Digi International Inc. 1995
 * 
 * All Rights Reserved
 * 
 * US   Government   Users   Restricted   Rights   -   Use,   duplication   or
 * disclosure  restricted by GSA ADP Schedule Contract with Digi International
 * Inc.
 * 
 *****************************************************************************/

/*******************************************************************
 *
 *  SpiderX25 X25 PLP Driver
 *
 *  Copyright 1991 Spider Systems Limited
 *
 *  X25DB.H
 *
 *
 *******************************************************************/

/*
 *	x25db.h
 *	 /projects/common/PBRAIN/SCCS/pbrainF/dev/sys/x25/0/s.x25db.h
 *	@(#)x25db.h	1.34
 *
 *	Last delta created	13:33:41 2/27/92
 *	This file extracted	18:32:53 11/19/92
 *
 */

/* ----------------------------------------------------------------------
   Structure and constant definition file for the X25 code.
   ---------------------------------------------------------------------- */

/* ----------------------------------------------------------------------
	The internal Q structure used everywhere
   ---------------------------------------------------------------------- */

struct Q {
	mblk_t *head;
	mblk_t *tail;
};

/* ----------------------------------------------------------------------
          defines for the X.25 code domain field in LSAPs
   ---------------------------------------------------------------------- */

#define		LAN84		0
#define		WAN84		1
#define		WAN80		2
#define		WAN88		3
#define		LAN88		4

/* ----------------------------------------------------------------------
	Two useful defines to differentiate 'in' and 'out' modes
   ---------------------------------------------------------------------- */

#define		X_INWARD	0
#define		X_UTWARD	1

/* ----------------------------------------------------------------------
	defines for range checking in NUI Override.
   ---------------------------------------------------------------------- */
#define		MIN_WND		1
#define		MIN_PKT		4

/* ----------------------------------------------------------------------
	Virtual circuit and Link VC slot references.
   ---------------------------------------------------------------------- */

#define		HASHLEN		100	/* Length vc hash table		*/
#define		SLOTFREE	0       /* Value marks chnl available	*/

typedef		unsigned short	VCREF;

#define		VC_SLOT_SIZE	sizeof(VCREF)
#define		VC_SLOT_LEN	(HASHLEN * VC_SLOT_SIZE)


struct x25vcformat {

unsigned char	xstate;			/* VC state */
unsigned char	xtag;			/* Finger record as X25 vc */

struct wlcfg	*xbase;			/* Config base pointer */
mblk_t		*x25txqhead;		/* Data out q */
mblk_t		*x25txqtail;		/*   ditto    */
mblk_t		*xEDbuff;		/* Expedited out data */
mblk_t		*reassemble;		/* Reassembly area, incoming */
mblk_t		*datatogo;		/* Fragmentation, outgoing */
unsigned char	x25txqsize;		/* X25txqsize */
unsigned char	xdomain;		/* LAN84 etc. marker */

/* NUI facilities */
/* N.B. If NUI subscribed to, these will ALWAYS
   take preference over same fields in xbase.      */
struct {
        unsigned int  LOCDEFPKTSIZE;	/* Local default pkt p */
        unsigned int  REMDEFPKTSIZE;	/* Local default pkt p */
        unsigned int  LOCDEFWSIZE;	/* Local default window size */
        unsigned int  REMDEFWSIZE;	/* Local default window size */
		unsigned int  locdefthclass;   /* Local default value         */
		unsigned int  remdefthclass;   /* Remote default value        */
		unsigned int  SUB_MODES;	/* Mode tuning bits for net */
		unsigned int  CUG_CONTROL;	/* CUG facilities */
} nuibase;
unsigned char   NUI_override;		/* NUI Override subscription */

int		xconnid;		/* A unique id for connection */
unsigned short	xusrevent;		/* Event caused set */

mblk_t		*calldata;		/* User Call/CAA data */
mblk_t		*Udiscdata;		/* User disconnect data */
struct x25vcformat FAR *xvclink;	/* Used to queue vcs    */
struct lsformat    FAR *lsaplink;	/* Pointer to LSAP record */
int		acksoutstanding;	/* Frames sent not ackd */
int		remcredit;		/* Ack count from user */
int		nsdulen;		/* Reassembly length */
int		nsref;			/* Reference employed by user */
int		userref;		/* Reference supplied by user */
int		qposition;		/* Listener request queue position */
unsigned short	nslim;			/* Concatenation limit */
unsigned short	lci;			/* Logical channel identifier */
unsigned short	xflag;			/* VC flags */
unsigned short	ISOflag;		/* ISO facility flags */
unsigned short	CCITTflag;		/* CCITT facility flags */
unsigned short	xtock[4];		/* Timer array */
unsigned short	rpkt;			/* Remote max packet size */
unsigned short	lpkt;			/* Local max packet size */
unsigned short	ltrans;			/* Transit delay selection */
unsigned short	etoecumulative;		/* Cumulative end to end transit */
unsigned short	etoetarget;		/* Target - ditto - */
unsigned short	etoemaxaccept;		/* Maximum accepatable -ditto- */
unsigned char   cug_index[MAX_CUG_LEN]; /* Supplied Closed User Group   */
unsigned char   nui_len;                /* Supplied NUI length          */
unsigned char   nui_field[MAX_NUI_LEN]; /* Supplied Network User ID     */
unsigned char   rpoa_len;               /* Supplied RPOA length         */
unsigned char   rpoa_field[MAX_RPOA_LEN]; /* Supplied RPOA              */
unsigned char   x_fac_len;              /* Length Supplied X.25 Facs    */
unsigned char   cg_fac_len;             /* Length Supplied Calling Facs */
unsigned char   cd_fac_len;             /* Length Supplied Called Facs  */
unsigned char   fac_field[MAX_FAC_LEN]; /* Supplied Facility Field      */
unsigned char	ch_mu_len;		/* Length Monetary Unit		*/
unsigned char	ch_mu_field[MAX_MU_LEN];/* Indicated Monetary Unit	*/
unsigned char	ch_sc_len;		/* Length Segment Count		*/
unsigned char	ch_sc_field[MAX_SC_LEN];/* Indicated Segment Count      */
unsigned char	ch_cd_len;		/* Length Call Duration		*/
unsigned char	ch_cd_field[MAX_CD_LEN];/* Indicated Call Duration	*/
unsigned char	called_add_mod;         /* Reason why address modified  */
unsigned char	call_redirect;          /* Reason why call redirected   */
struct xaddrf   orig_add;		/* Original DTE address if above*/
unsigned char   prty_len;               /* Length of priority field     */
unsigned char   prty[MAX_PRTY_LEN];     /* Indicated Priority           */
unsigned char   protection_type;        /* Type of protection required  */
unsigned char   prot_len;               /* Length of protection field   */
unsigned char   lowprot_len;            /* Len. of low protection field */
unsigned char   protection[MAX_PROT];   /* Indicated protection         */
unsigned char   lowprotection[MAX_PROT];/* Indicated low protection     */
unsigned char	lgngfi, lcn;		/* Logical group/gfi and channel id */
struct xaddrf	called;			/* Called NSAP address */
struct xaddrf	caller;			/* Calling NSAP address */
struct xaddrf	rcalled;		/* Responding called NSAP */
unsigned long	xu_ident;		/* Subnetwork soft identifier */
unsigned char	ampvc;			/* Marks PVC db when non-zero */

unsigned char	rpktp,  lpktp;		/* Rem/Loc psize parameter */
unsigned char	rwsize, lwsize;		/* Rem/Loc window sizes */
unsigned char	rthru , lthru;		/* Rem/Loc throughput class */
unsigned char	rminthru,lminthru;	/* Rem/Loc minimum throughput */

unsigned char	lastnr;			/* Last P(R) received */
unsigned char	psexpected;		/* Next P(S) in sequence to rx */
unsigned char	nrtogo;			/* Next P(R) to be sent */
unsigned char	nrsent;			/* Last P(R) sent */
unsigned char	hold_nr;		/* Upper bound on P(R) to be sent */
unsigned char	nstogo;			/* Next P(S) to be sent */
unsigned char	xmodulo;		/* Modulus -1 for extended etc. */
unsigned char	lastQin;		/* Qbit status last in */
unsigned char	lastMin;		/* Mbit status last in */
unsigned char	ftoack;			/* Remote frames still to ack */
unsigned char	xretx;			/* Retransmission count */
unsigned char	discause;		/* Disconnect causes */
unsigned char	disdiag;		/* Disconnect diagnostics */
unsigned char	rstcause;		/* Reset causes */
unsigned char	rstdiag;		/* Reset diagnostics */
unsigned char	nsucause;		/* User set cause */
unsigned char	nsudiag;		/* User set reason */

unsigned char	loc_Dsupport;		/* Support for D-bit */
unsigned char	rem_Dsupport;		/* Support for D-bit */
unsigned char	Dctl;			/* Action when clash on Dbit data  */
unsigned char	Dreq_rcvd_cnt;		/* Rconf req received from network */
unsigned char	Dreq_rcvd_seq[128];	/* Sequence numbers of the above   */
unsigned char	Dreq_sent_cnt;		/* Rconf req sent to network       */
unsigned char	Dreq_sent_seq[128];	/* Sequence numbers of the above   */
unsigned char	ack_immediate;		/* Dont delay Dbit acknowledgement */

unsigned char	NSUattached;		/* Set when user attached */

unsigned char	Udatawaiting;		/* Set on data pressure */
unsigned char	Uexdatawaiting;		/* Set on expedited pressure */
unsigned char	DTEbusy;		/* Set when RNR or busy */
unsigned char	CONS_call;		/* Set when CONS procedures are used */
unsigned char	bureason;		/* Record why buffer needed */
unsigned char	prtyvalue;		/* Priority hook */
unsigned short	hashval;		/* Hashed lci value */

mblk_t		*mb_gbuf;		/* Fragment being 'GET'at    */
mblk_t		*mb_gind;               /* Retained pointer for gset */
mblk_t		*mb_pbuf;               /* Fragment being 'PUT' at   */
int		mb_gl;                  /* Length left in fragment   */

/* SNMP statistics fields */
unsigned long	est_time;		/* Time of Circuit Establishment */
unsigned int	direction;		/* Incoming/Outgoing/PVC         */
unsigned int	in_octets;		/* Octets sent to upper layer    */
unsigned int	in_pdu;			/* PDUs sent to upper layer      */
unsigned int	remote_resets;		/* DTE-initiated resets          */
unsigned int	provider_resets;	/* Provider-initiated resets     */
unsigned int	rx_int_pkts;		/* Interrupt packets received    */
unsigned int	out_octets;		/* Octets sent to upper layer    */
unsigned int	out_pdu;		/* PDUs sent to upper layer      */
unsigned int	tx_int_pkts;		/* Expedited data packets sent   */
unsigned int	T22_expire;		/* Count of T22 expirations      */
unsigned int	T25_expire;		/* Count of T25 expirations      */
unsigned int	T26_expire;		/* Count of T26 expirations      */
};



struct lsformat {

unsigned char	lstate;			/* PLP/LSAP state */
unsigned char	ltag;			/* Finger record as PLP/LSAP */

struct wlcfg	*lbase;			/* Config base pointer */
unsigned char	ldomain;		/* LAN84 etc. marker */
int		lconnid;		/* A unique id for PLP */
unsigned short	lusrevent;		/* Event caused set */

uint3264	lowhandle;		/* The link level handle supplied   */
int		lref;			/* Reference given by link level    */
int		myref;			/* Reference to self                */
int		concount;		/* Count of established connections */
int		prgcnt;			/* PRGBRA counter */
unsigned short	ltock[4];		/* Timer array                      */

struct lsapformat lladdr;		/* The DTE LSAP address */
unsigned long	lu_ident;		/* Subnetwork soft identifier */
unsigned char	lbureason;		/* Reason for needing a buffer */
unsigned char	lretx;			/* RES retry count */
unsigned char	DTEmode;		/* DTE or DCE    */
unsigned char	rescause;		/* Restart cause */
unsigned char	resdiag;		/* Restart diags */
int		cfg_index;		/* Index into xcfg for subnetwork */

struct x25vcformat FAR *CRqueue;	/* Queue of vc awaiting CR          */
VCREF		slot[HASHLEN];		/* VC hash table                    */
};


struct x25cfgformat {
	unsigned char	net_ok;
	unsigned char   trace_active;
	queue_t        *trace_queue;
	struct   wlcfg  net_cfg;

};

/* ---- X25 VIRTUAL CIRCUIT STATES ----

   NB:  The ordering of this set is important.
*/

#define		Idle		0	/* Record is not in use         */
#define		AskingNRS	1	/* CR is being validated by NRS */
#define		P1		2	/* VC state is READY            */
#define		P2		3	/* VC in DTE CALL REQUEST       */
#define		P3		4	/* VC in DXE INCOMING CALL      */
#define		P5		5	/* VC in CALL COLLISION         */
#define		DataTransfer	6	/* VC in P4 (see xflags)        */
#define		DXEbusy		7	/* VC in P4, DXE sent RNR	*/
#define		D2		8	/* VC in DTE RESET REQUEST      */
#define		D2pending	9	/* Wanting buffer for RESET     */
#define		WtgRCU		10	/* Waiting U RSC to int.err.    */
#define		WtgRCN		11	/* Waiting X.25 RSC for user    */
#define		WtgRCNpending	12	/* Buffer reqd to enter state   */
#define		P4pending	13	/* Buffer reqd for X.25 RSC     */
#define		pRESUonly	14	/* Buffer for user rst only	*/
#define		RESUonly	15	/* User only being reset	*/
#define		pDTransfer	16	/* Buffer for RSC to user	*/
#define		WRCUpending	17	/* Buffer reqd internal RST     */
#define		DXErpending	18	/* Buffer reqd RST indication   */
#define		DXEresetting	19	/* Waiting U RSC to X.25 RI     */
#define		P6		20	/* VC in DTE CLEAR REQUEST      */
#define		P6pending	21	/* Wanting buffer for CLEAR     */
#define		WUcpending	22	/* Buffer reqd DI no netconn    */
#define		WUNcpending	23	/* Buffer reqd internal DI      */
#define		DXEcpending	24	/* Buffer reqd CLR REQ->User    */
#define		DXEcfpending    25	/* Buffer reqd CLC to User      */
#define		DXEclearing	26	/* Wanting buffer for CLC       */

/* ---- PACKET LEVEL AND LSAP/LINK STATES ---- */

#define		L_connecting	1	/* Connecting to DXE            */
#define		WtgRES		2	/* Connected resolving DXE      */
#define		WtgDXE		3	/* Random wait started          */
#define		L_connected	4	/* Connected and resolved DXE   */
#define		L3restarting	5	/* DTE RESTART REQUEST          */
#define		L_disconnecting	6	/* Waiting link disc reply      */
#define		WtgRpending	7	/* Buffer to enter WtgRES       */
#define		L3rpending	8	/* Buffer to enter L3restarting */
#define		L_dpending	9	/* Buffer to enter L_disconnect */

/* ---- VIRTUAL CIRCUIT FLAG SETTINGS (xflag) ---- */

#define		INTOUTSTANDING	1	/* Await INTERRUPT CONFIRMATION */
#define		caareq		2	/* Marks CAA seen (diag map)    */
#define		NRSidentified	4	/* NRS has supplied LSAP addr   */
#define		vc_counted	8	/* Marks vc as established -P4  */
#define		INCwaited	16	/* Waiting INC response usr     */
#define		LIM_ASSERTED	32	/* User asserted nsdu limit	*/
#define		PK_OK		64	/* User can neg PSIZE in CAA	*/
#define		WS_OK		128	/* User can neg WSIZE in CAA	*/
#define		Prty_flag	256	/* Priority circuit flag	*/
#define		Intl_flag	512	/* International call flag	*/
#define		thruforced	1024	/* Tclass forced packet sizes	*/
#define		Prty_slug	2048	/* Prty value forced pkt size   */

/* ---- VIRTUAL CIRCUIT FACILITY FLAG SETTINGS ---- */

/* Definitions for ISO defined facility flags */

#define		REVCHARGE	1
#define		FSELECT		2
#define		FSRRESP		4
#define		TCLASS		8
#define		PSIZENEG	16
#define		WSIZENEG	32
#define		TRDELAY		64
#define		CUGREQ		128
#define     	BCUGREQ    	256
#define     	NUIREQ      	512
#define     	RPOAREQ     	1024
#define 	CHGREQ		2048

/* Definitions for CCITT defined facility flags */

#define		EXPDATANEG	1
#define		MTCLASS		2
#define		ENDTOEND	4
#define		ENDTARGET	8
#define		ENDMAX		16
#define		EXPSELECTED	32		/* Set when exp in call */
#define		EXPINUSE	64		/* When negotiated      */
#define		EXTDADDR	128

/* Definition of minimum value of Throughput */

#define		ISO_THRU_MIN	3

/* ---- VIRTUAL CIRCUIT USER/SET EVENTS ----

   NB the priority ordering here is important.  The values go in
   pairs, the event itself and the switch variable value.
*/

#define		labort		1	/* LSAP gone, or restart */
#define		LSAPgone	1

#define		xDRout		2	/* User disconnect       */
#define		Udisconnect	2

#define		xUSRerr		4	/* User error detected   */
#define		Uerror		3

#define		xNRSreply	8	/* NRS validation        */
#define		NRSvalidation	4

#define		xCRout		16	/* User CR               */
#define		Uconnect	5

#define		xCAAout		32	/* User connect response */
#define		UCconfirm	6

#define		xRSCout		64	/* User reset response   */
#define		URSTresponse	7

#define		xRSTout		128	/* User reset            */
#define		URSTrequest	8

#define		xINCout		256	/* Interrupt confirm     */
#define		AKEDdata	9

#define		xINTout		512	/* Interrupt to send     */
#define		UEDrequest	10

#define		xDTout		1024	/* User data to send     */
#define		UDTrequest	11

#define		xAKout		2048	/* User acks data        */
#define		UAKs		12

#define		xBUFreq		4096	/* Needing buffer        */
#define		BUFrequest	13

#define		ALLXUSREVENTS	( xDRout | xUSRerr | xNRSreply | \
				  xCRout | xCAAout | xRSCout   | \
				  xRSTout| xINCout | xINTout   | \
				  xDTout | xAKout                )

/* ---- PLP/LSAP EVENT SET ---- */

#define		lclosure	1	/* Disconnect or discnf received  */
#define		lbye		1

#define		lopkills	2	/* Request to kill LLC connection */
#define		lzap		2

#define		lneedbuf	4	/* Buffer request event           */
#define		lBUFrequest	3

/* ---- SYSTEM ERROR/MONITOR INDICES ---- */

#define		BadL2func	0
#define		Cantlzap	1
#define		L2badcc		2
#define		L2baddcnf	3
#define		L2badref	4
#define		L2report	5
#define		L2reset		6
#define		L3T25timeouts	7
#define		L3timeouts	8
#define		L3badAE		9
#define		L3badT20	10
#define		L3badT24	11
#define		L3badT25	12
#define		L3badevent	13
#define		L3badgfi	14
#define		L3badlstate	15
#define		L3badltock2	16
#define		L3badrandom	17
#define		L3badxtock0	18
#define		L3clrbadstate	19
#define		L3conlt0	20
#define		L3deqfailed	21

#define		L3indnodata	23
#define		L3matrixcall	24
#define		L3nodb		25
#define		L3qoscheck	26
#define		L3outbad	27
#define		L3shortframe	28
#define		L3tabfault	29
#define		L3usererror	30
#define		L3usergone	31
#define		LNeednotneeded	32
#define		NSUbadref	33
#define		NSUdtnull	34
#define		NSUednull	35
#define		NSUrefrange	36
#define		NeednotNeeded	37
#define		NoNRSrequest	38
#define		UDRbad		39
#define		Ubadint		40
#define		Unoint		41
#define		L2badtag	42
#define		L3baddiag	43

/* ----------------------------------------------------------------------
   Definitions of internal structures which are not visible to the upper
			level user.
   ---------------------------------------------------------------------- */

/* ------ The Upper Glue Database Structure ------ */

struct xgldbf {
	queue_t	 *wq;		/* Write queue pointer */
	queue_t	 *rq;		/* Read queue pointer */
	struct Q gdbq;		/* Queue of listener CI/DIs */
	struct Q g_ear;		/* Listen address queue */
	int	 gstate;	/* State of the database */
	int	 gref;		/* Reference to the database */
	int	 xnetref;	/* Reference to X.25 db (attached) */
	int	 g_earmax;	/* Max no simult. CIs handled */
	int	 g_earuse;	/* Number CIs sent up */
	ushort	 xuid;		/* Effective user ID opener */
	unsigned char pvcflg;	/* Marks PVC - 2 states only */
};

/* ------ The Upper Glue Database States ------ */

#define		G_IDLE		0	/* DB is free for use */
#define		G_OPEN		1	/* DB has been opened */
#define		G_LISTENING	2	/* DB is listening for CIs */
#define		G_ATTACHED	3	/* DB is handling X.25 connection */
#define		G_FROZEN	4	/* DB M_ERROR sent */
#define		G_FREEZING	5	/* DB going to G_FROZEN (buf reqd) */
#define		G_PVCWAITING	6	/* User waiting PVC attach rsp */

/* ------ Internal connect and disconnect formats ------ */

struct i_xdiscf {
   struct xdiscf xdisc;            /* As defined in x25_proto.h */
   int           holdns;           /* To hold the netref on listener */
};

struct i_xcallf {
   struct xcallf xcall;            /* As defined in x25_proto.h */
   int           holdns;           /* To hold the netref on listener */
};


/* ------ Internal Management Structures ------ */

#define PRIORITY_LISTEN 512
#define ROOT_LISTEN     256

struct i_xlistenf {
   unsigned char  l_cumode;		/* Match mode for CUDF */
   unsigned char  l_culength;		/* Length of CUDF match field */
   unsigned char  l_cubytes[16];	/* CUDF match field */
   unsigned char  l_mode;		/* Match mode for address */
   unsigned char  l_type;		/* Address type DTE or NSAP */
   unsigned char  l_length;		/* Semi-octet length of match field */
   unsigned char  l_add[20];		/* Address match field */
   int		  l_total;		/* Total length to match on         */
   int		  l_gref;		/* Glue module listen is for        */
   int		  l_snid;       /* Subnet work ID */

};

/* ----------------------------------------------------------------------
                X.25 MUX LOWER SIDE DEFINITIONS
   ---------------------------------------------------------------------- */

struct xsndbf   {
	queue_t       *wq;	/* Pointer to l write q */
	queue_t       *rq;	/* Pointer to l read q  */
	int           linkref;	/* Reference to this element */
	int	      lindex;	/* STREAMS unique link index */
	int           lregref;	/* Stored upper reg stream id */
	unsigned char regok;	/* Registration complete */
	unsigned char linked;	/* I_LINK has appeared */
	unsigned long u_snid;   /* Upper level identifier */
	int           *statptr;	/* Pointer to L3monarray */
};

/* --- X25 Direction Constants --- */
#define	X25_DRCTN_IN	1
#define	X25_DRCTN_OUT	2
#define	X25_DRCTN_PVC	3

