1 /******************************************************************************
2  *
3  * Name:	skcsum.h
4  * Project:	GEnesis - SysKonnect SK-NET Gigabit Ethernet (SK-98xx)
5  * Purpose:	Store/verify Internet checksum in send/receive packets.
6  *
7  ******************************************************************************/
8 
9 /******************************************************************************
10  *
11  *	(C)Copyright 1998-2001 SysKonnect GmbH.
12  *
13  *	This program is free software; you can redistribute it and/or modify
14  *	it under the terms of the GNU General Public License as published by
15  *	the Free Software Foundation; either version 2 of the License, or
16  *	(at your option) any later version.
17  *
18  *	The information in this file is provided "AS IS" without warranty.
19  *
20  ******************************************************************************/
21 
22 /******************************************************************************
23  *
24  * Description:
25  *
26  * Public header file for the "GEnesis" common module "CSUM".
27  *
28  * "GEnesis" is an abbreviation of "Gigabit Ethernet Network System in Silicon"
29  * and is the code name of this SysKonnect project.
30  *
31  * Compilation Options:
32  *
33  *	SK_USE_CSUM - Define if CSUM is to be used. Otherwise, CSUM will be an
34  *	empty module.
35  *
36  *	SKCS_OVERWRITE_PROTO - Define to overwrite the default protocol id
37  *	definitions. In this case, all SKCS_PROTO_xxx definitions must be made
38  *	external.
39  *
40  *	SKCS_OVERWRITE_STATUS - Define to overwrite the default return status
41  *	definitions. In this case, all SKCS_STATUS_xxx definitions must be made
42  *	external.
43  *
44  * Include File Hierarchy:
45  *
46  *	"h/skcsum.h"
47  *	 "h/sktypes.h"
48  *	 "h/skqueue.h"
49  *
50  ******************************************************************************/
51 
52 #ifndef __INC_SKCSUM_H
53 #define __INC_SKCSUM_H
54 
55 #include "h/sktypes.h"
56 #include "h/skqueue.h"
57 
58 /* defines ********************************************************************/
59 
60 /*
61  * Define the default bit flags for 'SKCS_PACKET_INFO.ProtocolFlags'  if no user
62  * overwrite.
63  */
64 #ifndef SKCS_OVERWRITE_PROTO	/* User overwrite? */
65 #define SKCS_PROTO_IP	0x1	/* IP (Internet Protocol version 4) */
66 #define SKCS_PROTO_TCP	0x2	/* TCP (Transmission Control Protocol) */
67 #define SKCS_PROTO_UDP	0x4	/* UDP (User Datagram Protocol) */
68 
69 /* Indices for protocol statistics. */
70 #define SKCS_PROTO_STATS_IP	0
71 #define SKCS_PROTO_STATS_UDP	1
72 #define SKCS_PROTO_STATS_TCP	2
73 #define SKCS_NUM_PROTOCOLS	3	/* Number of supported protocols. */
74 #endif	/* !SKCS_OVERWRITE_PROTO */
75 
76 /*
77  * Define the default SKCS_STATUS type and values if no user overwrite.
78  *
79  *	SKCS_STATUS_UNKNOWN_IP_VERSION - Not an IP v4 frame.
80  *	SKCS_STATUS_IP_CSUM_ERROR - IP checksum error.
81  *	SKCS_STATUS_IP_CSUM_ERROR_TCP - IP checksum error in TCP frame.
82  *	SKCS_STATUS_IP_CSUM_ERROR_UDP - IP checksum error in UDP frame
83  *	SKCS_STATUS_IP_FRAGMENT - IP fragment (IP checksum ok).
84  *	SKCS_STATUS_IP_CSUM_OK - IP checksum ok (not a TCP or UDP frame).
85  *	SKCS_STATUS_TCP_CSUM_ERROR - TCP checksum error (IP checksum ok).
86  *	SKCS_STATUS_UDP_CSUM_ERROR - UDP checksum error (IP checksum ok).
87  *	SKCS_STATUS_TCP_CSUM_OK - IP and TCP checksum ok.
88  *	SKCS_STATUS_UDP_CSUM_OK - IP and UDP checksum ok.
89  *	SKCS_STATUS_IP_CSUM_OK_NO_UDP - IP checksum OK and no UDP checksum.
90  */
91 #ifndef SKCS_OVERWRITE_STATUS	/* User overwrite? */
92 #define SKCS_STATUS	int	/* Define status type. */
93 
94 #define SKCS_STATUS_UNKNOWN_IP_VERSION	1
95 #define SKCS_STATUS_IP_CSUM_ERROR		2
96 #define SKCS_STATUS_IP_FRAGMENT			3
97 #define SKCS_STATUS_IP_CSUM_OK			4
98 #define SKCS_STATUS_TCP_CSUM_ERROR		5
99 #define SKCS_STATUS_UDP_CSUM_ERROR		6
100 #define SKCS_STATUS_TCP_CSUM_OK			7
101 #define SKCS_STATUS_UDP_CSUM_OK			8
102 /* needed for Microsoft */
103 #define SKCS_STATUS_IP_CSUM_ERROR_UDP	9
104 #define SKCS_STATUS_IP_CSUM_ERROR_TCP	10
105 /* UDP checksum may be omitted */
106 #define SKCS_STATUS_IP_CSUM_OK_NO_UDP	11
107 #endif	/* !SKCS_OVERWRITE_STATUS */
108 
109 /* Clear protocol statistics event. */
110 #define SK_CSUM_EVENT_CLEAR_PROTO_STATS	1
111 
112 /*
113  * Add two values in one's complement.
114  *
115  * Note: One of the two input values may be "longer" than 16-bit, but then the
116  * resulting sum may be 17 bits long. In this case, add zero to the result using
117  * SKCS_OC_ADD() again.
118  *
119  *	Result = Value1 + Value2
120  */
121 #define SKCS_OC_ADD(Result, Value1, Value2) {				\
122 	unsigned long Sum;						\
123 									\
124 	Sum = (unsigned long) (Value1) + (unsigned long) (Value2);	\
125 	/* Add-in any carry. */						\
126 	(Result) = (Sum & 0xffff) + (Sum >> 16);			\
127 }
128 
129 /*
130  * Subtract two values in one's complement.
131  *
132  *	Result = Value1 - Value2
133  */
134 #define SKCS_OC_SUB(Result, Value1, Value2)	\
135 	SKCS_OC_ADD((Result), (Value1), ~(Value2) & 0xffff)
136 
137 /* typedefs *******************************************************************/
138 
139 /*
140  * SKCS_PROTO_STATS - The CSUM protocol statistics structure.
141  *
142  * There is one instance of this structure for each protocol supported.
143  */
144 typedef struct s_CsProtocolStatistics {
145 	SK_U64 RxOkCts;		/* Receive checksum ok. */
146 	SK_U64 RxUnableCts;	/* Unable to verify receive checksum. */
147 	SK_U64 RxErrCts;	/* Receive checksum error. */
148 	SK_U64 TxOkCts;		/* Transmit checksum ok. */
149 	SK_U64 TxUnableCts;	/* Unable to calculate checksum in hw. */
150 } SKCS_PROTO_STATS;
151 
152 /*
153  * s_Csum - The CSUM module context structure.
154  */
155 typedef struct s_Csum {
156 	/* Enabled receive SK_PROTO_XXX bit flags. */
157 	unsigned ReceiveFlags[SK_MAX_NETS];
158 #ifdef TX_CSUM
159 	unsigned TransmitFlags[SK_MAX_NETS];
160 #endif /* TX_CSUM */
161 
162 	/* The protocol statistics structure; one per supported protocol. */
163 	SKCS_PROTO_STATS ProtoStats[SK_MAX_NETS][SKCS_NUM_PROTOCOLS];
164 } SK_CSUM;
165 
166 /*
167  * SKCS_PACKET_INFO - The packet information structure.
168  */
169 typedef struct s_CsPacketInfo {
170 	/* Bit field specifiying the desired/found protocols. */
171 	unsigned ProtocolFlags;
172 
173 	/* Length of complete IP header, including any option fields. */
174 	unsigned IpHeaderLength;
175 
176 	/* IP header checksum. */
177 	unsigned IpHeaderChecksum;
178 
179 	/* TCP/UDP pseudo header checksum. */
180 	unsigned PseudoHeaderChecksum;
181 } SKCS_PACKET_INFO;
182 
183 /* function prototypes ********************************************************/
184 
185 #ifndef SK_CS_CALCULATE_CHECKSUM
186 extern unsigned SkCsCalculateChecksum(
187 	void		*pData,
188 	unsigned	Length);
189 #endif /* SK_CS_CALCULATE_CHECKSUM */
190 
191 extern int SkCsEvent(
192 	SK_AC		*pAc,
193 	SK_IOC		Ioc,
194 	SK_U32		Event,
195 	SK_EVPARA	Param);
196 
197 extern SKCS_STATUS SkCsGetReceiveInfo(
198 	SK_AC		*pAc,
199 	void		*pIpHeader,
200 	unsigned	Checksum1,
201 	unsigned	Checksum2,
202 	int			NetNumber);
203 
204 extern void SkCsGetSendInfo(
205 	SK_AC				*pAc,
206 	void				*pIpHeader,
207 	SKCS_PACKET_INFO	*pPacketInfo,
208 	int					NetNumber);
209 
210 extern void SkCsSetReceiveFlags(
211 	SK_AC		*pAc,
212 	unsigned	ReceiveFlags,
213 	unsigned	*pChecksum1Offset,
214 	unsigned	*pChecksum2Offset,
215 	int			NetNumber);
216 
217 #endif	/* __INC_SKCSUM_H */
218