1 /*-
2 * Copyright (c) 2015-2016 Mellanox Technologies, Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice unmodified, this list of conditions, and the following
9 * disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 #ifndef _LINUXKPI_LINUX_ETHERDEVICE_H_
26 #define _LINUXKPI_LINUX_ETHERDEVICE_H_
27
28 #include <linux/types.h>
29 #include <linux/device.h>
30 #include <linux/skbuff.h>
31 #include <linux/netdevice.h>
32
33 #include <sys/random.h>
34 #include <sys/libkern.h>
35
36 #define ETH_MODULE_SFF_8079 1
37 #define ETH_MODULE_SFF_8079_LEN 256
38 #define ETH_MODULE_SFF_8472 2
39 #define ETH_MODULE_SFF_8472_LEN 512
40 #define ETH_MODULE_SFF_8636 3
41 #define ETH_MODULE_SFF_8636_LEN 256
42 #define ETH_MODULE_SFF_8436 4
43 #define ETH_MODULE_SFF_8436_LEN 256
44
45 struct ethtool_eeprom {
46 u32 offset;
47 u32 len;
48 };
49
50 struct ethtool_modinfo {
51 u32 type;
52 u32 eeprom_len;
53 };
54
55 static inline bool
is_zero_ether_addr(const u8 * addr)56 is_zero_ether_addr(const u8 * addr)
57 {
58 return ((addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]) ==
59 0x00);
60 }
61
62 static inline bool
is_unicast_ether_addr(const u8 * addr)63 is_unicast_ether_addr(const u8 * addr)
64 {
65 return ((addr[0] & 0x01) == 0x00);
66 }
67
68 static inline bool
is_multicast_ether_addr(const u8 * addr)69 is_multicast_ether_addr(const u8 * addr)
70 {
71 return ((addr[0] & 0x01) == 0x01);
72 }
73
74 static inline bool
is_broadcast_ether_addr(const u8 * addr)75 is_broadcast_ether_addr(const u8 * addr)
76 {
77 return ((addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) ==
78 0xff);
79 }
80
81 static inline bool
is_valid_ether_addr(const u8 * addr)82 is_valid_ether_addr(const u8 * addr)
83 {
84 return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
85 }
86
87 static inline void
ether_addr_copy(u8 * dst,const u8 * src)88 ether_addr_copy(u8 * dst, const u8 * src)
89 {
90 memcpy(dst, src, 6);
91 }
92
93 static inline bool
ether_addr_equal_unaligned(const u8 * pa,const u8 * pb)94 ether_addr_equal_unaligned(const u8 *pa, const u8 *pb)
95 {
96 return (memcmp(pa, pb, 6) == 0);
97 }
98 #define ether_addr_equal(_pa, _pb) ether_addr_equal_unaligned(_pa, _pb)
99
100 static inline bool
ether_addr_equal_64bits(const u8 * pa,const u8 * pb)101 ether_addr_equal_64bits(const u8 *pa, const u8 *pb)
102 {
103 return (memcmp(pa, pb, 6) == 0);
104 }
105
106 static inline void
eth_broadcast_addr(u8 * pa)107 eth_broadcast_addr(u8 *pa)
108 {
109 memset(pa, 0xff, 6);
110 }
111
112 static inline void
eth_zero_addr(u8 * pa)113 eth_zero_addr(u8 *pa)
114 {
115 memset(pa, 0, 6);
116 }
117
118 static inline void
random_ether_addr(u8 * dst)119 random_ether_addr(u8 *dst)
120 {
121 arc4random_buf(dst, 6);
122
123 dst[0] &= 0xfe;
124 dst[0] |= 0x02;
125 }
126
127 static inline void
eth_random_addr(u8 * dst)128 eth_random_addr(u8 *dst)
129 {
130
131 random_ether_addr(dst);
132 }
133
134 static inline int
device_get_mac_address(struct device * dev,char * dst)135 device_get_mac_address(struct device *dev, char *dst)
136 {
137
138 /* XXX get mac address from FDT? */
139 return (-ENOENT);
140 }
141
142 /* Returns network byte order. */
143 static inline uint16_t
eth_type_trans(struct sk_buff * skb,struct net_device * dev)144 eth_type_trans(struct sk_buff *skb, struct net_device *dev)
145 {
146 pr_debug("%s: TODO\n", __func__);
147 return (htons(ETHERTYPE_8023));
148 }
149
150 static inline void
eth_hw_addr_set(struct net_device * dev,const u8 * addr)151 eth_hw_addr_set(struct net_device *dev, const u8 *addr)
152 {
153 pr_debug("%s: TODO (if we want to)\n", __func__);
154 }
155
156 static inline int
eth_platform_get_mac_address(struct device * dev __unused,u8 * addr __unused)157 eth_platform_get_mac_address(struct device *dev __unused, u8 *addr __unused)
158 {
159 pr_debug("%s: TODO\n", __func__);
160 return (-ENODEV);
161 }
162
163 #endif /* _LINUXKPI_LINUX_ETHERDEVICE_H_ */
164