blob: 45c97b59d654bbb46e3e8e363b5f048af80beb01 [file] [log] [blame]
developer3abe1ad2022-01-24 11:13:32 +08001/* Copyright (C) 2021-2022 Mediatek Inc. */
2#ifndef __ATENL_UTIL_H
3#define __ATENL_UTIL_H
4
5#include <linux/const.h>
6#include <linux/if_arp.h>
7#include <linux/if_ether.h>
8#include <linux/if_packet.h>
9#include <stdint.h>
10#include <string.h>
11
12typedef uint8_t u8;
13typedef uint16_t u16;
14typedef uint32_t u32;
15typedef uint64_t u64;
16typedef int8_t s8;
17typedef int16_t s16;
18typedef int32_t s32;
19typedef int64_t s64, ktime_t;
20
21#ifndef __WORDSIZE
22#define __WORDSIZE (__SIZEOF_LONG__ * 8)
23#endif
24
25#ifndef BITS_PER_LONG
26#define BITS_PER_LONG __WORDSIZE
27#endif
28
29#define UL(x) (_UL(x))
30#define ULL(x) (_ULL(x))
31
32#define BIT(nr) (1UL << (nr))
33
34#define GENMASK_INPUT_CHECK(h, l) 0
35#define __GENMASK(h, l) \
36 (((~UL(0)) - (UL(1) << (l)) + 1) & \
37 (~UL(0) >> (BITS_PER_LONG - 1 - (h))))
38#define GENMASK(h, l) \
39 (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
40
41#define __bf_shf(x) (__builtin_ffsll(x) - 1)
42#define FIELD_GET(_mask, _reg) \
43 ({ \
44 (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
45 })
developer5698c9c2022-05-30 16:40:23 +080046#define FIELD_PREP(_mask, _val) \
47 ({ \
48 ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
49 })
developer3abe1ad2022-01-24 11:13:32 +080050
51#ifndef ARRAY_SIZE
52#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
53#endif
54
55#ifndef DIV_ROUND_UP
56#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
57#endif
58
59#define PIPE_READ 0
60#define PIPE_WRITE 1
61
62#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
63#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
64
65static inline bool ether_addr_equal(const u8 *addr1, const u8 *addr2)
66{
67 const u16 *a = (const u16 *)addr1;
68 const u16 *b = (const u16 *)addr2;
69
70 return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0;
71}
72
73static inline bool is_broadcast_ether_addr(const u8 *addr)
74{
75 return (*(const u16 *)(addr + 0) &
76 *(const u16 *)(addr + 2) &
77 *(const u16 *)(addr + 4)) == 0xffff;
78}
79
80static inline bool is_multicast_ether_addr(const u8 *addr)
81{
82 return 0x01 & addr[0];
83}
84
developer5698c9c2022-05-30 16:40:23 +080085static inline bool is_unicast_ether_addr(const u8 *addr)
86{
87 return !is_multicast_ether_addr(addr);
88}
89
90static inline bool is_zero_ether_addr(const u8 *addr)
91{
92 return (*(const u16 *)(addr + 0) |
93 *(const u16 *)(addr + 2) |
94 *(const u16 *)(addr + 4)) == 0;
95}
96
97static inline bool use_default_addr(const u8 *addr)
98{
99 return !is_unicast_ether_addr(addr) ||
100 is_zero_ether_addr(addr);
101}
102
developer3abe1ad2022-01-24 11:13:32 +0800103static inline void eth_broadcast_addr(u8 *addr)
104{
105 memset(addr, 0xff, ETH_ALEN);
106}
107
108static inline void ether_addr_copy(u8 *dst, const u8 *src)
109{
110 u16 *a = (u16 *)dst;
111 const u16 *b = (const u16 *)src;
112
113 a[0] = b[0];
114 a[1] = b[1];
115 a[2] = b[2];
116}
117
118static inline int snprintf_error(size_t size, int res)
119{
120 return res < 0 || (unsigned int) res >= size;
121}
122
123#endif