1 /*
2 * Byte order utilities
3 *
4 * Copyright (C) 2020, Broadcom.
5 *
6 * Unless you and Broadcom execute a separate written software license
7 * agreement governing use of this software, this software is licensed to you
8 * under the terms of the GNU General Public License version 2 (the "GPL"),
9 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10 * following added to such license:
11 *
12 * As a special exception, the copyright holders of this software give you
13 * permission to link this software with independent modules, and to copy and
14 * distribute the resulting executable under terms of your choice, provided that
15 * you also meet, for each linked independent module, the terms and conditions of
16 * the license of that module. An independent module is a module which is not
17 * derived from this software. The special exception does not apply to any
18 * modifications of the software.
19 *
20 *
21 * <<Broadcom-WL-IPTag/Dual:>>
22 *
23 * This file by default provides proper behavior on little-endian architectures.
24 * On big-endian architectures, IL_BIGENDIAN should be defined.
25 */
26
27 #ifndef _BCMENDIAN_H_
28 #define _BCMENDIAN_H_
29
30 #include <typedefs.h>
31
32 /* Reverse the bytes in a 16-bit value */
33 #define BCMSWAP16(val) \
34 ((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
35 (((uint16)(val) & (uint16)0xff00U) >> 8)))
36
37 /* Reverse the bytes in a 32-bit value */
38 #define BCMSWAP32(val) \
39 ((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \
40 (((uint32)(val) & (uint32)0x0000ff00U) << 8) | \
41 (((uint32)(val) & (uint32)0x00ff0000U) >> 8) | \
42 (((uint32)(val) & (uint32)0xff000000U) >> 24)))
43
44 /* Reverse the two 16-bit halves of a 32-bit value */
45 #define BCMSWAP32BY16(val) \
46 ((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
47 (((uint32)(val) & (uint32)0xffff0000U) >> 16)))
48
49 /* Reverse the bytes in a 64-bit value */
50 #define BCMSWAP64(val) \
51 ((uint64)((((uint64)(val) & 0x00000000000000ffULL) << 56) | \
52 (((uint64)(val) & 0x000000000000ff00ULL) << 40) | \
53 (((uint64)(val) & 0x0000000000ff0000ULL) << 24) | \
54 (((uint64)(val) & 0x00000000ff000000ULL) << 8) | \
55 (((uint64)(val) & 0x000000ff00000000ULL) >> 8) | \
56 (((uint64)(val) & 0x0000ff0000000000ULL) >> 24) | \
57 (((uint64)(val) & 0x00ff000000000000ULL) >> 40) | \
58 (((uint64)(val) & 0xff00000000000000ULL) >> 56)))
59
60 /* Reverse the two 32-bit halves of a 64-bit value */
61 #define BCMSWAP64BY32(val) \
62 ((uint64)((((uint64)(val) & 0x00000000ffffffffULL) << 32) | \
63 (((uint64)(val) & 0xffffffff00000000ULL) >> 32)))
64
65 /* Byte swapping macros
66 * Host <=> Network (Big Endian) for 16- and 32-bit values
67 * Host <=> Little-Endian for 16- and 32-bit values
68 */
69 #ifndef hton16
70 #ifndef IL_BIGENDIAN
71 #define HTON16(i) BCMSWAP16(i)
72 #define hton16(i) bcmswap16(i)
73 #define HTON32(i) BCMSWAP32(i)
74 #define hton32(i) bcmswap32(i)
75 #define NTOH16(i) BCMSWAP16(i)
76 #define ntoh16(i) bcmswap16(i)
77 #define NTOH32(i) BCMSWAP32(i)
78 #define ntoh32(i) bcmswap32(i)
79 #define LTOH16(i) (i)
80 #define ltoh16(i) (i)
81 #define LTOH32(i) (i)
82 #define ltoh32(i) (i)
83 #define HTOL16(i) (i)
84 #define htol16(i) (i)
85 #define HTOL32(i) (i)
86 #define htol32(i) (i)
87 #define HTOL64(i) (i)
88 #define htol64(i) (i)
89 #else /* IL_BIGENDIAN */
90 #define HTON16(i) (i)
91 #define hton16(i) (i)
92 #define HTON32(i) (i)
93 #define hton32(i) (i)
94 #define NTOH16(i) (i)
95 #define ntoh16(i) (i)
96 #define NTOH32(i) (i)
97 #define ntoh32(i) (i)
98 #define LTOH16(i) BCMSWAP16(i)
99 #define ltoh16(i) bcmswap16(i)
100 #define LTOH32(i) BCMSWAP32(i)
101 #define ltoh32(i) bcmswap32(i)
102 #define HTOL16(i) BCMSWAP16(i)
103 #define htol16(i) bcmswap16(i)
104 #define HTOL32(i) BCMSWAP32(i)
105 #define htol32(i) bcmswap32(i)
106 #define HTOL64(i) BCMSWAP64(i)
107 #define htol64(i) bcmswap64(i)
108 #endif /* IL_BIGENDIAN */
109 #endif /* hton16 */
110
111 #ifndef IL_BIGENDIAN
112 #define ltoh16_buf(buf, i)
113 #define htol16_buf(buf, i)
114 #define ltoh32_buf(buf, i)
115 #define htol32_buf(buf, i)
116 #define ltoh64_buf(buf, i)
117 #define htol64_buf(buf, i)
118 #else
119 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16 *)(buf), (i))
120 #define htol16_buf(buf, i) bcmswap16_buf((uint16 *)(buf), (i))
121 #define ltoh32_buf(buf, i) bcmswap32_buf((uint16 *)(buf), (i))
122 #define htol32_buf(buf, i) bcmswap32_buf((uint16 *)(buf), (i))
123 #define ltoh64_buf(buf, i) bcmswap64_buf((uint16 *)(buf), (i))
124 #define htol64_buf(buf, i) bcmswap64_buf((uint16 *)(buf), (i))
125 #endif /* IL_BIGENDIAN */
126
127 /* Unaligned loads and stores in host byte order */
128 #ifndef IL_BIGENDIAN
129 #define load32_ua(a) ltoh32_ua(a)
130 #define store32_ua(a, v) htol32_ua_store(v, a)
131 #define load16_ua(a) ltoh16_ua(a)
132 #define store16_ua(a, v) htol16_ua_store(v, a)
133 #define load64_ua(a) ltoh64_ua(a)
134 #define store64_ua(a, v) htol64_ua_store(v, a)
135 #else
136 #define load32_ua(a) ntoh32_ua(a)
137 #define store32_ua(a, v) hton32_ua_store(v, a)
138 #define load16_ua(a) ntoh16_ua(a)
139 #define store16_ua(a, v) hton16_ua_store(v, a)
140 #define load64_ua(a) ntoh64_ua(a)
141 #define store64_ua(a, v) hton64_ua_store(v, a)
142 #endif /* IL_BIGENDIAN */
143
144 #define _LTOH16_UA(cp) ((uint16)(cp)[0] | ((uint16)(cp)[1] << 8))
145 #define _LTOH32_UA(cp) ((uint32)(cp)[0] | ((uint32)(cp)[1] << 8) | \
146 ((uint32)(cp)[2] << 16) | ((uint32)(cp)[3] << 24))
147 #define _NTOH16_UA(cp) (((uint16)(cp)[0] << 8) | (uint16)(cp)[1])
148 #define _NTOH32_UA(cp) (((uint32)(cp)[0] << 24) | ((uint32)(cp)[1] << 16) | \
149 ((uint32)(cp)[2] << 8) | (uint32)(cp)[3])
150
151 #define _LTOH64_UA(cp) ((uint64)(cp)[0] | ((uint64)(cp)[1] << 8) | \
152 ((uint64)(cp)[2] << 16) | ((uint64)(cp)[3] << 24) | \
153 ((uint64)(cp)[4] << 32) | ((uint64)(cp)[5] << 40) | \
154 ((uint64)(cp)[6] << 48) | ((uint64)(cp)[7] << 56))
155
156 #define _NTOH64_UA(cp) ((uint64)(cp)[7] | ((uint64)(cp)[6] << 8) | \
157 ((uint64)(cp)[5] << 16) | ((uint64)(cp)[4] << 24) | \
158 ((uint64)(cp)[3] << 32) | ((uint64)(cp)[2] << 40) | \
159 ((uint64)(cp)[1] << 48) | ((uint64)(cp)[0] << 56))
160
161 #define ltoh_ua(ptr) \
162 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
163 sizeof(*(ptr)) == sizeof(uint16) ? (uint16)_LTOH16_UA((const uint8 *)(ptr)) : \
164 sizeof(*(ptr)) == sizeof(uint32) ? (uint32)_LTOH32_UA((const uint8 *)(ptr)) : \
165 *(uint8 *)0)
166
167 #define ntoh_ua(ptr) \
168 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
169 sizeof(*(ptr)) == sizeof(uint16) ? (uint16)_NTOH16_UA((const uint8 *)(ptr)) : \
170 sizeof(*(ptr)) == sizeof(uint32) ? (uint32)_NTOH32_UA((const uint8 *)(ptr)) : \
171 *(uint8 *)0)
172
173 #ifdef __GNUC__
174
175 /* GNU macro versions avoid referencing the argument multiple times, while also
176 * avoiding the -fno-inline used in ROM builds.
177 */
178
179 #define bcmswap16(val) ({ \
180 uint16 _val = (val); \
181 BCMSWAP16(_val); \
182 })
183
184 #define bcmswap32(val) ({ \
185 uint32 _val = (val); \
186 BCMSWAP32(_val); \
187 })
188
189 #define bcmswap64(val) ({ \
190 uint64 _val = (val); \
191 BCMSWAP64(_val); \
192 })
193
194 #define bcmswap32by16(val) ({ \
195 uint32 _val = (val); \
196 BCMSWAP32BY16(_val); \
197 })
198
199 #define bcmswap16_buf(buf, len) ({ \
200 uint16 *_buf = (uint16 *)(buf); \
201 uint _wds = (len) / 2; \
202 while (_wds--) { \
203 *_buf = bcmswap16(*_buf); \
204 _buf++; \
205 } \
206 })
207
208 #define bcmswap32_buf(buf, len) ({ \
209 uint32 *_buf = (uint32 *)(buf); \
210 uint _wds = (len) / 4; \
211 while (_wds--) { \
212 *_buf = bcmswap32(*_buf); \
213 _buf++; \
214 } \
215 })
216
217 #define bcmswap64_buf(buf, len) ({ \
218 uint64 *_buf = (uint64 *)(buf); \
219 uint _wds = (len) / 8; \
220 while (_wds--) { \
221 *_buf = bcmswap64(*_buf); \
222 _buf++; \
223 } \
224 })
225
226 #define htol16_ua_store(val, bytes) ({ \
227 uint16 _val = (val); \
228 uint8 *_bytes = (uint8 *)(bytes); \
229 _bytes[0] = _val & 0xff; \
230 _bytes[1] = _val >> 8; \
231 })
232
233 #define htol32_ua_store(val, bytes) ({ \
234 uint32 _val = (val); \
235 uint8 *_bytes = (uint8 *)(bytes); \
236 _bytes[0] = _val & 0xff; \
237 _bytes[1] = (_val >> 8) & 0xff; \
238 _bytes[2] = (_val >> 16) & 0xff; \
239 _bytes[3] = _val >> 24; \
240 })
241
242 #define htol64_ua_store(val, bytes) ({ \
243 uint64 _val = (val); \
244 uint8 *_bytes = (uint8 *)(bytes); \
245 int _ii; \
246 for (_ii = 0; _ii < (int)sizeof(_val); ++_ii) { \
247 *_bytes++ = _val & 0xff; \
248 _val >>= 8; \
249 } \
250 })
251
252 #define hton16_ua_store(val, bytes) ({ \
253 uint16 _val = (val); \
254 uint8 *_bytes = (uint8 *)(bytes); \
255 _bytes[0] = _val >> 8; \
256 _bytes[1] = _val & 0xff; \
257 })
258
259 #define hton32_ua_store(val, bytes) ({ \
260 uint32 _val = (val); \
261 uint8 *_bytes = (uint8 *)(bytes); \
262 _bytes[0] = _val >> 24; \
263 _bytes[1] = (_val >> 16) & 0xff; \
264 _bytes[2] = (_val >> 8) & 0xff; \
265 _bytes[3] = _val & 0xff; \
266 })
267
268 #define ltoh16_ua(bytes) ({ \
269 const uint8 *_bytes = (const uint8 *)(bytes); \
270 _LTOH16_UA(_bytes); \
271 })
272
273 #define ltoh32_ua(bytes) ({ \
274 const uint8 *_bytes = (const uint8 *)(bytes); \
275 _LTOH32_UA(_bytes); \
276 })
277
278 #define ltoh64_ua(bytes) ({ \
279 const uint8 *_bytes = (const uint8 *)(bytes); \
280 _LTOH64_UA(_bytes); \
281 })
282
283 #define ntoh16_ua(bytes) ({ \
284 const uint8 *_bytes = (const uint8 *)(bytes); \
285 _NTOH16_UA(_bytes); \
286 })
287
288 #define ntoh32_ua(bytes) ({ \
289 const uint8 *_bytes = (const uint8 *)(bytes); \
290 _NTOH32_UA(_bytes); \
291 })
292
293 #define ntoh64_ua(bytes) ({ \
294 const uint8 *_bytes = (const uint8 *)(bytes); \
295 _NTOH64_UA(_bytes); \
296 })
297
298 #else /* !__GNUC__ */
299
300 /* Inline versions avoid referencing the argument multiple times */
301 static INLINE uint16
bcmswap16(uint16 val)302 bcmswap16(uint16 val)
303 {
304 return BCMSWAP16(val);
305 }
306
307 static INLINE uint32
bcmswap32(uint32 val)308 bcmswap32(uint32 val)
309 {
310 return BCMSWAP32(val);
311 }
312
313 static INLINE uint64
bcmswap64(uint64 val)314 bcmswap64(uint64 val)
315 {
316 return BCMSWAP64(val);
317 }
318
319 static INLINE uint32
bcmswap32by16(uint32 val)320 bcmswap32by16(uint32 val)
321 {
322 return BCMSWAP32BY16(val);
323 }
324
325 /* Reverse pairs of bytes in a buffer (not for high-performance use) */
326 /* buf - start of buffer of shorts to swap */
327 /* len - byte length of buffer */
328 static INLINE void
bcmswap16_buf(uint16 * buf,uint len)329 bcmswap16_buf(uint16 *buf, uint len)
330 {
331 len = len / 2;
332
333 while (len--) {
334 *buf = bcmswap16(*buf);
335 buf++;
336 }
337 }
338
339 /*
340 * Store 16-bit value to unaligned little-endian byte array.
341 */
342 static INLINE void
htol16_ua_store(uint16 val,uint8 * bytes)343 htol16_ua_store(uint16 val, uint8 *bytes)
344 {
345 bytes[0] = val & 0xff;
346 bytes[1] = val >> 8;
347 }
348
349 /*
350 * Store 32-bit value to unaligned little-endian byte array.
351 */
352 static INLINE void
htol32_ua_store(uint32 val,uint8 * bytes)353 htol32_ua_store(uint32 val, uint8 *bytes)
354 {
355 bytes[0] = val & 0xff;
356 bytes[1] = (val >> 8) & 0xff;
357 bytes[2] = (val >> 16) & 0xff;
358 bytes[3] = val >> 24;
359 }
360
361 /*
362 * Store 64-bit value to unaligned little-endian byte array.
363 */
364 static INLINE void
htol64_ua_store(uint64 val,uint8 * bytes)365 htol64_ua_store(uint64 val, uint8 *bytes)
366 {
367 int i;
368 for (i = 0; i < sizeof(val); ++i) {
369 *bytes++ = (uint8)(val & 0xff);
370 val >>= 8;
371 }
372 }
373
374 /*
375 * Store 16-bit value to unaligned network-(big-)endian byte array.
376 */
377 static INLINE void
hton16_ua_store(uint16 val,uint8 * bytes)378 hton16_ua_store(uint16 val, uint8 *bytes)
379 {
380 bytes[0] = val >> 8;
381 bytes[1] = val & 0xff;
382 }
383
384 /*
385 * Store 32-bit value to unaligned network-(big-)endian byte array.
386 */
387 static INLINE void
hton32_ua_store(uint32 val,uint8 * bytes)388 hton32_ua_store(uint32 val, uint8 *bytes)
389 {
390 bytes[0] = val >> 24;
391 bytes[1] = (val >> 16) & 0xff;
392 bytes[2] = (val >> 8) & 0xff;
393 bytes[3] = val & 0xff;
394 }
395
396 /*
397 * Load 16-bit value from unaligned little-endian byte array.
398 */
399 static INLINE uint16
ltoh16_ua(const void * bytes)400 ltoh16_ua(const void *bytes)
401 {
402 return _LTOH16_UA((const uint8 *)bytes);
403 }
404
405 /*
406 * Load 32-bit value from unaligned little-endian byte array.
407 */
408 static INLINE uint32
ltoh32_ua(const void * bytes)409 ltoh32_ua(const void *bytes)
410 {
411 return _LTOH32_UA((const uint8 *)bytes);
412 }
413
414 /*
415 * Load 64-bit value from unaligned little-endian byte array.
416 */
417 static INLINE uint64
ltoh64_ua(const void * bytes)418 ltoh64_ua(const void *bytes)
419 {
420 return _LTOH64_UA((const uint8 *)bytes);
421 }
422
423 /*
424 * Load 16-bit value from unaligned big-(network-)endian byte array.
425 */
426 static INLINE uint16
ntoh16_ua(const void * bytes)427 ntoh16_ua(const void *bytes)
428 {
429 return _NTOH16_UA((const uint8 *)bytes);
430 }
431
432 /*
433 * Load 32-bit value from unaligned big-(network-)endian byte array.
434 */
435 static INLINE uint32
ntoh32_ua(const void * bytes)436 ntoh32_ua(const void *bytes)
437 {
438 return _NTOH32_UA((const uint8 *)bytes);
439 }
440
441 /*
442 * Load 64-bit value from unaligned big-(network-)endian byte array.
443 */
444 static INLINE uint64
ntoh64_ua(const void * bytes)445 ntoh64_ua(const void *bytes)
446 {
447 return _NTOH64_UA((const uint8 *)bytes);
448 }
449
450 #endif /* !__GNUC__ */
451 #endif /* !_BCMENDIAN_H_ */
452