blob: b8a5a2dc5874c0be48345cfb608a08ecb6ab648f [file] [log] [blame]
Shad Ansari2f7f9be2017-06-07 13:34:53 -07001/*
2<:copyright-BRCM:2016:DUAL/GPL:standard
3
4 Broadcom Proprietary and Confidential.(c) 2016 Broadcom
5 All Rights Reserved
6
7Unless you and Broadcom execute a separate written software license
8agreement governing use of this software, this software is licensed
9to you under the terms of the GNU General Public License version 2
10(the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
11with the following added to such license:
12
13 As a special exception, the copyright holders of this software give
14 you permission to link this software with independent modules, and
15 to copy and distribute the resulting executable under terms of your
16 choice, provided that you also meet, for each linked independent
17 module, the terms and conditions of the license of that module.
18 An independent module is a module which is not derived from this
19 software. The special exception does not apply to any modifications
20 of the software.
21
22Not withstanding the above, under no circumstances may you combine
23this software in any way with any other Broadcom software provided
24under a license other than the GPL, without Broadcom's express prior
25written consent.
26
27:>
28 */
29
30#ifndef _BCMOLT_UTILS_H_
31#define _BCMOLT_UTILS_H_
32
33#include "bcmos_system.h"
34
35#define BCMOS_MACADDR_FMT_STR "%02X:%02X:%02X:%02X:%02X:%02X"
36#define BCMOS_MACADDR_PARAMS(mac) (mac)->u8[0],(mac)->u8[1],(mac)->u8[2],(mac)->u8[3],(mac)->u8[4],(mac)->u8[5]
37
38#define MAC_STR_LEN 18
39
40#define BYTES_IN_MEGABYTE(bytes) ((bytes) / (1024 * 1024))
41
42static inline char *bcmos_mac_2_str(const bcmos_mac_address *mac, char *buf)
43{
44 snprintf(buf, MAC_STR_LEN, BCMOS_MACADDR_FMT_STR, BCMOS_MACADDR_PARAMS(mac));
45 return buf;
46}
47
48/** Swap a byte string of any length.
49 *
50 * \param[in] ptr pointer to a memory space.
51 * \param[in] len length
52 * \note
53 * {0, 1, 2, 3} becomes {3, 2, 1, 0}
54 */
55static inline void bcmos_swap_bytes_in_place(uint8_t *ptr, uint32_t len)
56{
57 int ii;
58 char tmp;
59
60 for (ii = 0; ii < (len / 2); ii++)
61 {
62 tmp = ptr[len - ii - 1];
63 ptr[len - ii - 1] = ptr[ii];
64 ptr[ii] = tmp;
65 }
66}
67
68/** Swap a byte string of any length.
69 *
70 * \param[out] dst pointer to a memory space for the swapped bytes.
71 * \param[in] src pointer to a memory space for bytes to be swapped.
72 * \param[in] len length in bytes
73 * \note
74 * {0, 1, 2, 3} becomes {3, 2, 1, 0}
75 */
76static inline void bcmos_swap_bytes(uint8_t *dst, const uint8_t *src, const uint32_t len)
77{
78 int ii;
79
80 for (ii = 0; ii < len; ii++)
81 {
82 dst[ii] = src[len - ii - 1];
83 }
84}
85
86/** Copy bits from a given range of the source to a different range of the
87 * destination
88 *
89 * \param[in] dst destination value to be merged to
90 * \param[in] dst_hi high bit position
91 * \param[in] dst_lo low bit position
92 * \param[in] src source value to copy the bits from
93 * \param[in] src_hi high bit position
94 * \param[in] src_lo low bit position
95 *
96 * \return destination value
97 */
98static inline uint32_t bcmos_bits_copy_u32(uint32_t dst, uint8_t dst_hi, uint8_t dst_lo,
99 uint32_t src, uint8_t src_hi, uint8_t src_lo)
100{
101 dst &= (~(((1 << ((dst_hi - dst_lo) + 1)) - 1) << dst_lo));
102 src &= (((1 << ((src_hi - src_lo) + 1)) - 1) << src_lo);
103 dst |= ((dst_lo >= src_lo)? (src << (dst_lo - src_lo)): (src >> (src_lo - dst_lo)));
104 return dst;
105}
106
107/* network to host on unaligned array of uint32_t elements. Treat pointer p as a pointer to an array of uint32_t elements. Get element i. */
108#define N2H32(p, i) ((((const uint8_t *)(p))[(i) * 4]<<24) | (((const uint8_t *)(p))[(i) * 4+1]<<16) | (((const uint8_t *)(p))[(i) * 4+2]<<8) | (((const uint8_t *)(p))[(i) * 4+3]))
109/* network to host on unaligned array of uint16_t elements. Treat pointer p as a pointer to an array of uint16_t elements. Get element i. */
110#define N2H16(p, i) ((((const uint8_t *)(p))[(i) * 2]<<8) | (((const uint8_t *)(p))[(i) * 2+1]))
111/* network to host on unaligned array of uint8_t elements. Treat pointer p as a pointer to an array of uint8_t elements. Get element i. */
112#define N2H8(p, i) (((const uint8_t *)(p))[(i)])
113
114/* For internal use by the following H2N* macros */
115#define H2N8(p, v) *((uint8_t *)p) = (uint8_t)(v)
116/* host to network conversion of a uint16_t with support for non 16 bit aligned destination address. p is a (uint8_t *) destination pointer. v is a uint16_t value. v can be written to p even if p is unaligned to 16 bits. */
117#define H2N16(p, v) (H2N8((p), (v) >> 8), H2N8(((uint8_t *)(p) + 1), (v)))
118/* host to network conversion of a uint32_t with support for non 32 bit aligned destination address. p is a (uint8_t *) destination pointer. v is a uint32_t value. v can be written to p even if p is unaligned to 32 bits. */
119#define H2N32(p, v) (H2N16((p), (v) >> 16), H2N16(((uint8_t *)(p) + 2), (v)))
120
121#define NUM_ELEM(array) (sizeof(array) / sizeof((array)[0]))
122
123typedef void (*bcmos_msg_print_cb)(void *context, const char *fmt, ...);
124
125void bcmos_hexdump(bcmos_msg_print_cb print_cb, void *context, const void *buffer, uint32_t offset, uint32_t count, const char *indent);
126void bcmos_hexdump_one_line(const char *funcname, uint32_t lineno, bcmos_msg_print_cb print_cb, void *context, const void *buffer, uint32_t offset, uint32_t count, const char *prefix,
127 const char *suffix);
128
129uint32_t eth_calc_crc32(uint32_t crc, const void *buf, size_t size);
130
131#define BCMOLT_TIME_STR_MAX_LEN 80
132
133#endif /* BCMOLT_UTILS_H */
134