xref: /freebsd/sys/compat/linuxkpi/common/include/linux/etherdevice.h (revision 833a452e9f082a7982a31c21f0da437dbbe0a39d)
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  * $FreeBSD$
26  */
27 #ifndef _LINUXKPI_LINUX_ETHERDEVICE_H_
28 #define	_LINUXKPI_LINUX_ETHERDEVICE_H_
29 
30 #include <linux/types.h>
31 
32 #include <sys/random.h>
33 #include <sys/libkern.h>
34 
35 #define	ETH_MODULE_SFF_8079		1
36 #define	ETH_MODULE_SFF_8079_LEN		256
37 #define	ETH_MODULE_SFF_8472		2
38 #define	ETH_MODULE_SFF_8472_LEN		512
39 #define	ETH_MODULE_SFF_8636		3
40 #define	ETH_MODULE_SFF_8636_LEN		256
41 #define	ETH_MODULE_SFF_8436		4
42 #define	ETH_MODULE_SFF_8436_LEN		256
43 
44 struct ethtool_eeprom {
45 	u32	offset;
46 	u32	len;
47 };
48 
49 struct ethtool_modinfo {
50 	u32	type;
51 	u32	eeprom_len;
52 };
53 
54 static inline bool
55 is_zero_ether_addr(const u8 * addr)
56 {
57 	return ((addr[0] + addr[1] + addr[2] + addr[3] + addr[4] + addr[5]) == 0x00);
58 }
59 
60 static inline bool
61 is_multicast_ether_addr(const u8 * addr)
62 {
63 	return (0x01 & addr[0]);
64 }
65 
66 static inline bool
67 is_broadcast_ether_addr(const u8 * addr)
68 {
69 	return ((addr[0] + addr[1] + addr[2] + addr[3] + addr[4] + addr[5]) == (6 * 0xff));
70 }
71 
72 static inline bool
73 is_valid_ether_addr(const u8 * addr)
74 {
75 	return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
76 }
77 
78 static inline void
79 ether_addr_copy(u8 * dst, const u8 * src)
80 {
81 	memcpy(dst, src, 6);
82 }
83 
84 static inline bool
85 ether_addr_equal_unaligned(const u8 *pa, const u8 *pb)
86 {
87 	return (memcmp(pa, pb, 6) == 0);
88 }
89 #define	ether_addr_equal(_pa, _pb)	ether_addr_equal_unaligned(_pa, _pb)
90 
91 static inline bool
92 ether_addr_equal_64bits(const u8 *pa, const u8 *pb)
93 {
94 	return (memcmp(pa, pb, 6) == 0);
95 }
96 
97 static inline void
98 eth_broadcast_addr(u8 *pa)
99 {
100 	memset(pa, 0xff, 6);
101 }
102 
103 static inline void
104 eth_zero_addr(u8 *pa)
105 {
106 	memset(pa, 0, 6);
107 }
108 
109 static inline void
110 random_ether_addr(u8 * dst)
111 {
112 	arc4random_buf(dst, 6);
113 
114 	dst[0] &= 0xfe;
115 	dst[0] |= 0x02;
116 }
117 
118 #endif					/* _LINUXKPI_LINUX_ETHERDEVICE_H_ */
119