Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with 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 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | |
| 30 | #ifndef BCMOS_TYPES_H_ |
| 31 | #define BCMOS_TYPES_H_ |
| 32 | |
| 33 | #ifndef BCMOS_SYSTEM_H_ |
| 34 | #error Please do not include bcmos_types.h directly. Include bcmos_system.h |
| 35 | #endif |
| 36 | |
| 37 | #include "bcmos_pack.h" |
| 38 | |
| 39 | /** \defgroup system_types Generic types |
| 40 | * @{ |
| 41 | */ |
| 42 | |
| 43 | /* |
| 44 | * Limits of integer types. |
| 45 | */ |
| 46 | |
| 47 | /* Minimum of signed integer types. */ |
| 48 | #ifndef INT8_MIN |
| 49 | #define INT8_MIN (0x80) |
| 50 | #endif |
| 51 | #ifndef INT16_MIN |
| 52 | #define INT16_MIN (0x8000) |
| 53 | #endif |
| 54 | #ifndef INT32_MIN |
| 55 | #define INT32_MIN (0x80000000) |
| 56 | #endif |
| 57 | #ifndef INT64_MIN |
| 58 | #define INT64_MIN (0x8000000000000000) |
| 59 | #endif |
| 60 | |
| 61 | /* Maximum of signed integer types. */ |
| 62 | #ifndef INT8_MAX |
| 63 | #define INT8_MAX (0x7F) |
| 64 | #endif |
| 65 | #ifndef INT16_MAX |
| 66 | #define INT16_MAX (0x7FFF) |
| 67 | #endif |
| 68 | #ifndef INT32_MAX |
| 69 | #define INT32_MAX (0x7FFFFFFF) |
| 70 | #endif |
| 71 | #ifndef INT64_MAX |
| 72 | #define INT64_MAX (0x7FFFFFFFFFFFFFFF) |
| 73 | #endif |
| 74 | |
| 75 | /* Maximum of unsigned integer types. */ |
| 76 | #ifndef UINT8_MAX |
| 77 | #define UINT8_MAX (0xFF) |
| 78 | #endif |
| 79 | #ifndef UINT16_MAX |
| 80 | #define UINT16_MAX (0xFFFF) |
| 81 | #endif |
| 82 | #ifndef UINT32_MAX |
| 83 | #define UINT32_MAX (0xFFFFFFFF) |
| 84 | #endif |
| 85 | #ifndef UINT64_MAX |
| 86 | #define UINT64_MAX (0xFFFFFFFFFFFFFFFF) |
| 87 | #endif |
| 88 | |
| 89 | |
| 90 | /** Endianness */ |
| 91 | #define BCMOS_ENDIAN_BIG 0 |
| 92 | #define BCMOS_ENDIAN_LITTLE 1 |
| 93 | |
| 94 | typedef enum |
| 95 | { |
| 96 | BCMOS_ENDIAN_BIG_E = BCMOS_ENDIAN_BIG, |
| 97 | BCMOS_ENDIAN_LITTLE_E = BCMOS_ENDIAN_LITTLE, |
| 98 | } bcmos_endian; |
| 99 | |
| 100 | /* If endianness is not set explicitly, try to autodetect it. |
| 101 | * Modern gcc versions (over 4.8) define __BYTE_ORDER__ |
| 102 | * see "gcc -E -dM - < /dev/null | grep ENDIAN" |
| 103 | */ |
| 104 | #ifndef BCM_CPU_ENDIAN |
| 105 | |
| 106 | #ifdef __BYTE_ORDER__ |
| 107 | |
| 108 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 109 | #define BCM_CPU_ENDIAN BCMOS_ENDIAN_LITTLE |
| 110 | #else |
| 111 | #define BCM_CPU_ENDIAN BCMOS_ENDIAN_BIG |
| 112 | #endif |
| 113 | |
| 114 | #else |
| 115 | |
| 116 | /* We are dealing with compiler that doesn't set __BYTE_ORDER__. |
| 117 | * If it is simulation build than it must be LE x86. |
| 118 | * Otherwise, no way to tell. |
| 119 | */ |
| 120 | #ifdef SIMULATION_BUILD |
| 121 | #define BCM_CPU_ENDIAN BCMOS_ENDIAN_LITTLE |
| 122 | #endif |
| 123 | |
| 124 | #endif /* #ifdef __BYTE_ORDER */ |
| 125 | |
| 126 | #endif /* #ifndef BCM_CPU_ENDIAN */ |
| 127 | |
| 128 | #include <bcm_config.h> |
| 129 | |
| 130 | /** 24-bit unsigned integer */ |
| 131 | typedef union |
| 132 | { |
| 133 | uint8_t u8[3]; |
| 134 | struct __PACKED_ATTR_START__ |
| 135 | { |
| 136 | #if (BCM_CPU_ENDIAN == BCMOS_ENDIAN_BIG) |
| 137 | uint8_t hi; |
| 138 | uint8_t mid; |
| 139 | uint8_t low; |
| 140 | #elif (BCM_CPU_ENDIAN == BCMOS_ENDIAN_LITTLE) |
| 141 | uint8_t low; |
| 142 | uint8_t mid; |
| 143 | uint8_t hi; |
| 144 | #else |
| 145 | #error BCM_CPU_ENDIAN must be BCMOS_ENDIAN_BIG or _LITTLE |
| 146 | #endif |
| 147 | } __PACKED_ATTR_END__ low_hi; |
| 148 | } uint24_t; |
| 149 | |
| 150 | static inline uint32_t uint24_to_32(uint24_t u24) |
| 151 | { |
| 152 | return (u24.low_hi.hi << 16) | (u24.low_hi.mid << 8) | u24.low_hi.low; |
| 153 | } |
| 154 | |
| 155 | static inline uint24_t uint32_to_24(uint32_t u32) |
| 156 | { |
| 157 | return (uint24_t){ .low_hi= { .hi = (u32 >> 16) & 0xff, .mid = (u32 >> 8) & 0xff, .low = u32 & 0xff } }; |
| 158 | } |
| 159 | |
| 160 | /** VLAN tag (CoS/CFI/VID) */ |
| 161 | typedef uint16_t bcmos_vlan_tag; |
| 162 | |
| 163 | #define BCMOS_ETH_ALEN 6 |
| 164 | |
| 165 | /** MAC address */ |
| 166 | typedef struct |
| 167 | { |
| 168 | uint8_t u8[BCMOS_ETH_ALEN]; |
| 169 | } bcmos_mac_address; |
| 170 | |
| 171 | /** IPv4 address. It is stored in network byte order. */ |
| 172 | typedef union |
| 173 | { |
| 174 | uint32_t u32; |
| 175 | uint8_t u8[4]; |
| 176 | } bcmos_ipv4_address; |
| 177 | |
| 178 | /** IPv6 address */ |
| 179 | typedef uint8_t bcmos_ipv6_address[16]; |
| 180 | |
| 181 | static inline void bcmos_mac_address_init(bcmos_mac_address *mac) |
| 182 | { |
| 183 | memset(mac, 0, sizeof(*mac)); |
| 184 | } |
| 185 | |
| 186 | static inline void bcmos_ipv4_address_init(bcmos_ipv4_address *ip) |
| 187 | { |
| 188 | memset(ip, 0, sizeof(*ip)); |
| 189 | } |
| 190 | |
| 191 | #ifndef CFE_BUILD |
| 192 | static inline char *bcmos_inet_ntoa(bcmos_ipv4_address *ip, char *ip_str) |
| 193 | { |
| 194 | snprintf(ip_str, 16, "%u.%u.%u.%u", ip->u8[0], ip->u8[1], ip->u8[2], ip->u8[3]); |
| 195 | |
| 196 | return ip_str; |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | #ifndef container_of |
| 201 | #define container_of(ptr, type, member) ({ \ |
| 202 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 203 | (type *)( (long)__mptr - offsetof(type,member) );}) |
| 204 | #endif |
| 205 | |
| 206 | /** @} */ |
| 207 | |
| 208 | #endif /* BCMOS_TYPES_H_ */ |