1/*
2 * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses.  You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 *     Redistribution and use in source and binary forms, with or
13 *     without modification, are permitted provided that the following
14 *     conditions are met:
15 *
16 *      - Redistributions of source code must retain the above
17 *        copyright notice, this list of conditions and the following
18 *        disclaimer.
19 *
20 *      - Redistributions in binary form must reproduce the above
21 *        copyright notice, this list of conditions and the following
22 *        disclaimer in the documentation and/or other materials
23 *        provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 *
34 */
35
36/*
37 * Abstract:
38 *	provides byteswapping utilities. Basic functions are obtained from
39 *  platform specific implementations from byteswap_osd.h.
40 */
41
42#ifndef _CL_BYTESWAP_H_
43#define _CL_BYTESWAP_H_
44
45#include <string.h>
46#include <complib/cl_byteswap_osd.h>
47
48#ifdef __cplusplus
49#  define BEGIN_C_DECLS extern "C" {
50#  define END_C_DECLS   }
51#else				/* !__cplusplus */
52#  define BEGIN_C_DECLS
53#  define END_C_DECLS
54#endif				/* __cplusplus */
55
56BEGIN_C_DECLS
57/****h* Component Library/Byte Swapping
58* NAME
59*	Byte Swapping
60*
61* DESCRIPTION
62*	The byte swapping functions and macros allow swapping bytes from network
63*	byte order to host byte order.
64*
65*	All data transmitted between systems should be in network byte order.
66*	In order to utilize such data, it must be converted to host byte order
67*	before use.
68*
69* SEE ALSO
70*	Functions:
71*		cl_ntoh16, cl_hton16, cl_ntoh32, cl_hton32, cl_ntoh64, cl_hton64,
72*		cl_ntoh
73*
74*	Macros:
75*		CL_NTOH16, CL_HTON16, CL_NTOH32, CL_HTON32, CL_NTOH64, CL_HTON64
76*********/
77/*
78 * The byteswap_osd.h provides the following macros.
79 *		__LITTLE_ENDIAN
80 *		__BIG_ENDIAN
81 *		__BYTE_ORDER
82 *
83 * If the platform provides byte swapping functions, byteswap_osd.h also
84 * provides the following macros.
85 *		ntoh16, hton16
86 *		ntoh32, hton32
87 *		ntoh64, hton64
88 */
89#ifndef __BYTE_ORDER
90#error "__BYTE_ORDER macro undefined. Missing in endian.h?"
91#endif
92#if __BYTE_ORDER == __LITTLE_ENDIAN
93#define CPU_LE		1
94#define CPU_BE		0
95#else
96#define CPU_LE		0
97#define CPU_BE		1
98#endif
99/****d* Component Library: Byte Swapping/CL_NTOH16
100* NAME
101*	CL_NTOH16
102*
103* DESCRIPTION
104*	The CL_NTOH16 macro converts a 16-bit value from network byte order to
105*	host byte order.  The CL_NTOH16 macro will cause constant values to be
106*	swapped by the pre-processor.  For variables, CL_NTOH16 is less efficient
107*	than the cl_ntoh16 function.
108*
109* SYNOPSIS
110*	CL_NTOH16( val );
111*
112* PARAMETERS
113*	val
114*		[in] 16-bit value to swap from network byte order to host byte order.
115*
116* RESULT
117*	Value of val converted to host byte order.
118*
119* NOTES
120*	This macro is analogous to CL_HTON16.
121*
122* SEE ALSO
123*	Byte Swapping, CL_HTON16, CL_NTOH32, CL_NTOH64,
124*	cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh
125*********/
126/****d* Component Library: Byte Swapping/CL_HTON16
127* NAME
128*	CL_HTON16
129*
130* DESCRIPTION
131*	The CL_HTON16 macro converts a 16-bit value from host byte order to
132*	network byte order.  The CL_HTON16 macro will cause constant values to be
133*	swapped by the pre-processor.  For variables, CL_HTON16 is less efficient
134*	than the cl_hton16 function.
135*
136* SYNOPSIS
137*	CL_HTON16( val );
138*
139* PARAMETERS
140*	val
141*		[in] 16-bit value to swap from host byte order to network byte order.
142*
143* RESULT
144*	Value of val converted to network byte order.
145*
146* NOTES
147*	This macro is analogous to CL_NTOH16.
148*
149* SEE ALSO
150*	Byte Swapping, CL_NTOH16, CL_HTON32, CL_HTON64,
151*	cl_hton16, cl_hton32, cl_hton64, cl_ntoh
152*********/
153#if CPU_LE
154#define CL_NTOH16( x )		(uint16_t)(		\
155			(((uint16_t)(x) & 0x00FF) << 8) |		\
156			(((uint16_t)(x) & 0xFF00) >> 8) )
157#else
158#define CL_NTOH16( x )	(x)
159#endif
160#define CL_HTON16				CL_NTOH16
161/****f* Component Library: Byte Swapping/cl_ntoh16
162* NAME
163*	cl_ntoh16
164*
165* DESCRIPTION
166*	The cl_ntoh16 function converts a 16-bit value from network byte order to
167*	host byte order.
168*
169* SYNOPSIS
170*	uint16_t
171*	cl_ntoh16(
172*		IN	const uint16_t	val );
173*
174* PARAMETERS
175*	val
176*		[in] Value to swap from network byte order to host byte order.
177*
178* RETURN VALUE
179*	Value of val converted to host byte order.
180*
181* NOTES
182*	This function is analogous to cl_hton16.
183*
184* SEE ALSO
185*	Byte Swapping, cl_hton16, cl_ntoh32, cl_ntoh64, cl_ntoh
186*********/
187/****f* Component Library: Byte Swapping/cl_hton16
188* NAME
189*	cl_hton16
190*
191* DESCRIPTION
192*	The cl_hton16 function converts a 16-bit value from host byte order to
193*	network byte order.
194*
195* SYNOPSIS
196*	uint16_t
197*	cl_hton16(
198*		IN	const uint16_t	val );
199*
200* PARAMETERS
201*	val
202*		[in] Value to swap from host byte order to network byte order .
203*
204* RETURN VALUE
205*	Value of val converted to network byte order.
206*
207* NOTES
208*	This function is analogous to cl_ntoh16.
209*
210* SEE ALSO
211*	Byte Swapping, cl_ntoh16, cl_hton32, cl_hton64, cl_ntoh
212*********/
213#ifndef cl_ntoh16
214#define cl_ntoh16	CL_NTOH16
215#define cl_hton16	CL_HTON16
216#endif
217/****d* Component Library: Byte Swapping/CL_NTOH32
218* NAME
219*	CL_NTOH32
220*
221* DESCRIPTION
222*	The CL_NTOH32 macro converts a 32-bit value from network byte order to
223*	host byte order.  The CL_NTOH32 macro will cause constant values to be
224*	swapped by the pre-processor.  For variables, CL_NTOH32 is less efficient
225*	than the cl_ntoh32 function.
226*
227* SYNOPSIS
228*	CL_NTOH32( val );
229*
230* PARAMETERS
231*	val
232*		[in] 32-bit value to swap from network byte order to host byte order.
233*
234* RESULT
235*	Value of val converted to host byte order.
236*
237* NOTES
238*	This macro is analogous to CL_HTON32.
239*
240* SEE ALSO
241*	Byte Swapping, CL_HTON32, CL_NTOH16, CL_NTOH64,
242*	cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh
243*********/
244/****d* Component Library: Byte Swapping/CL_HTON32
245* NAME
246*	CL_HTON32
247*
248* DESCRIPTION
249*	The CL_HTON32 macro converts a 32-bit value from host byte order to
250*	network byte order.  The CL_HTON32 macro will cause constant values to be
251*	swapped by the pre-processor.  For variables, CL_HTON32 is less efficient
252*	than the cl_hton32 function.
253*
254* SYNOPSIS
255*	CL_HTON32( val );
256*
257* PARAMETERS
258*	val
259*		[in] 32-bit value to swap from host byte order to network byte order.
260*
261* RESULT
262*	Value of val converted to network byte order.
263*
264* NOTES
265*	This macro is analogous to CL_NTOH32.
266*
267* SEE ALSO
268*	Byte Swapping, CL_NTOH32, CL_HTON16, CL_HTON64,
269*	cl_hton16, cl_hton32, cl_hton64, cl_ntoh
270*********/
271#if CPU_LE
272#define CL_NTOH32( x )		(uint32_t)(			\
273			(((uint32_t)(x) & 0x000000FF) << 24) |	\
274			(((uint32_t)(x) & 0x0000FF00) << 8) |	\
275			(((uint32_t)(x) & 0x00FF0000) >> 8) |	\
276			(((uint32_t)(x) & 0xFF000000) >> 24) )
277#else
278#define CL_NTOH32( x )		(x)
279#endif
280#define CL_HTON32	CL_NTOH32
281/****f* Component Library: Byte Swapping/cl_ntoh32
282* NAME
283*	cl_ntoh32
284*
285* DESCRIPTION
286*	The cl_ntoh32 function converts a 32-bit value from network byte order to
287*	host byte order.
288*
289* SYNOPSIS
290*	uint32_t
291*	cl_ntoh32(
292*		IN	const uint32_t	val );
293*
294* PARAMETERS
295*	val
296*		[in] Value to swap from network byte order to host byte order.
297*
298* RETURN VALUE
299*	Value of val converted in host byte order.
300*
301* NOTES
302*	This function is analogous to cl_hton32.
303*
304* SEE ALSO
305*	Byte Swapping, cl_hton32, cl_ntoh16, cl_ntoh64, cl_ntoh
306*********/
307/****f* Component Library: Byte Swapping/cl_hton32
308* NAME
309*	cl_hton32
310*
311* DESCRIPTION
312*	The cl_hton32 function converts a 32-bit value from host byte order to
313*	network byte order.
314*
315* SYNOPSIS
316*	uint32_t
317*	cl_hton32(
318*		IN	const uint32_t	val );
319*
320* PARAMETERS
321*	val
322*		[in] Value to swap from host byte order to network byte order .
323*
324* RETURN VALUE
325*	Value of val converted to network byte order.
326*
327* NOTES
328*	This function is analogous to cl_ntoh32.
329*
330* SEE ALSO
331*	Byte Swapping, cl_ntoh32, cl_hton16, cl_hton64, cl_ntoh
332*********/
333#ifndef cl_ntoh32
334#define cl_ntoh32	CL_NTOH32
335#define cl_hton32	CL_HTON32
336#endif
337/****d* Component Library: Byte Swapping/CL_NTOH64
338* NAME
339*	CL_NTOH64
340*
341* DESCRIPTION
342*	The CL_NTOH64 macro converts a 64-bit value from network byte order to
343*	host byte order.  The CL_NTOH64 macro will cause constant values to be
344*	swapped by the pre-processor.  For variables, CL_NTOH64 is less efficient
345*	than the cl_ntoh64 function.
346*
347* SYNOPSIS
348*	CL_NTOH64( val );
349*
350* PARAMETERS
351*	val
352*		[in] 64-bit value to swap from network byte order to host byte order.
353*
354* RESULT
355*	Value of val converted to host byte order.
356*
357* NOTES
358*	This macro is analogous to CL_HTON64.
359*
360* SEE ALSO
361*	Byte Swapping, CL_HTON64, CL_NTOH16, CL_NTOH32,
362*	cl_ntoh16, cl_ntoh32, cl_ntoh64, cl_ntoh
363*********/
364/****d* Component Library: Byte Swapping/CL_HTON64
365* NAME
366*	CL_HTON64
367*
368* DESCRIPTION
369*	The CL_HTON64 macro converts a 64-bit value from host byte order to
370*	network byte order.  The CL_HTON64 macro will cause constant values to be
371*	swapped by the pre-processor.  For variables, CL_HTON64 is less efficient
372*	than the cl_hton64 function.
373*
374* SYNOPSIS
375*	CL_HTON64( val );
376*
377* PARAMETERS
378*	val
379*		[in] 64-bit value to swap from host byte order to network byte order.
380*
381* RESULT
382*	Value of val converted to network byte order.
383*
384* NOTES
385*	This macro is analogous to CL_NTOH64.
386*
387* SEE ALSO
388*	Byte Swapping, CL_NTOH64, CL_HTON16, CL_HTON32,
389*	cl_hton16, cl_hton32, cl_hton64, cl_ntoh
390*********/
391#if CPU_LE
392#define CL_NTOH64( x )		(uint64_t)(					\
393			(((uint64_t)(x) & 0x00000000000000FFULL) << 56) |	\
394			(((uint64_t)(x) & 0x000000000000FF00ULL) << 40) |	\
395			(((uint64_t)(x) & 0x0000000000FF0000ULL) << 24) |	\
396			(((uint64_t)(x) & 0x00000000FF000000ULL) << 8 ) |	\
397			(((uint64_t)(x) & 0x000000FF00000000ULL) >> 8 ) |	\
398			(((uint64_t)(x) & 0x0000FF0000000000ULL) >> 24) |	\
399			(((uint64_t)(x) & 0x00FF000000000000ULL) >> 40) |	\
400			(((uint64_t)(x) & 0xFF00000000000000ULL) >> 56) )
401#else
402#define CL_NTOH64( x )		(x)
403#endif
404#define CL_HTON64				CL_NTOH64
405/****f* Component Library: Byte Swapping/cl_ntoh64
406* NAME
407*	cl_ntoh64
408*
409* DESCRIPTION
410*	The cl_ntoh64 function converts a 64-bit value from network byte order to
411*	host byte order.
412*
413* SYNOPSIS
414*	uint64_t
415*	cl_ntoh64(
416*		IN	const uint64_t	val );
417*
418* PARAMETERS
419*	val
420*		[in] Value to swap from network byte order to host byte order.
421*
422* RETURN VALUE
423*	Value of val converted in host byte order.
424*
425* NOTES
426*	This function is analogous to cl_hton64.
427*
428* SEE ALSO
429*	Byte Swapping, cl_hton64, cl_ntoh16, cl_ntoh32, cl_ntoh
430*********/
431/****f* Component Library: Byte Swapping/cl_hton64
432* NAME
433*	cl_hton64
434*
435* DESCRIPTION
436*	The cl_hton64 function converts a 64-bit value from host byte order to
437*	network byte order.
438*
439* SYNOPSIS
440*	uint64_t
441*	cl_hton64(
442*		IN	const uint64_t	val );
443*
444* PARAMETERS
445*	val
446*		[in] Value to swap from host byte order to network byte order .
447*
448* RETURN VALUE
449*	Value of val converted to network byte order.
450*
451* NOTES
452*	This function is analogous to cl_ntoh64.
453*
454* SEE ALSO
455*	Byte Swapping, cl_ntoh64, cl_hton16, cl_hton32, cl_ntoh
456*********/
457#ifndef cl_ntoh64
458#define cl_ntoh64	CL_NTOH64
459#define cl_hton64	CL_HTON64
460#endif
461/****f* Component Library: Byte Swapping/cl_ntoh
462* NAME
463*	cl_ntoh
464*
465* DESCRIPTION
466*	The cl_ntoh function converts a value from network byte order to
467*	host byte order.
468*
469* SYNOPSIS
470*/
471static inline void
472cl_ntoh(OUT char *const p_dest,
473	IN const char *const p_src, IN const uint8_t size)
474{
475#if CPU_LE
476	uint8_t i;
477	char temp;
478
479	if (p_src == p_dest) {
480		/* Swap in place if source and destination are the same. */
481		for (i = 0; i < size / 2; i++) {
482			temp = p_dest[i];
483			p_dest[i] = p_src[size - 1 - i];
484			p_dest[size - 1 - i] = temp;
485		}
486	} else {
487		for (i = 0; i < size; i++)
488			p_dest[i] = p_src[size - 1 - i];
489	}
490#else
491	/*
492	 * If the source and destination are not the same, copy the source to
493	 * the destination.
494	 */
495	if (p_src != p_dest)
496		memcpy(p_dest, p_src, size);
497#endif
498}
499
500/*
501* PARAMETERS
502*	p_dest
503*		[in] Pointer to a byte array to contain the converted value of p_src.
504*
505*	p_src
506*		[in] Pointer to a byte array to be converted from network byte
507*		ordering.
508*
509*	size
510*		[in] Number of bytes to swap.p_dest
511*
512* RETURN VALUE
513*	This function does not return a value.
514*
515* NOTES
516*	cl_ntoh can perform in place swapping if both p_src and p_dest point to
517*	the same buffer.
518*
519* SEE ALSO
520*	Byte Swapping, cl_ntoh16, cl_ntoh32, cl_ntoh64
521*********/
522
523END_C_DECLS
524#endif				/* _CL_BYTESWAP_H_ */
525