1273246Shselasky/*-
2270710Shselasky * Copyright (c) 2007 Cisco Systems, Inc.  All rights reserved.
3270710Shselasky * Copyright (c) 2014 Mellanox Technologies, Ltd. All rights reserved.
4270710Shselasky *
5270710Shselasky * This software is available to you under a choice of one of two
6270710Shselasky * licenses.  You may choose to be licensed under the terms of the GNU
7270710Shselasky * General Public License (GPL) Version 2, available from the file
8270710Shselasky * COPYING in the main directory of this source tree, or the
9270710Shselasky * OpenIB.org BSD license below:
10270710Shselasky *
11270710Shselasky *     Redistribution and use in source and binary forms, with or
12270710Shselasky *     without modification, are permitted provided that the following
13270710Shselasky *     conditions are met:
14270710Shselasky *
15270710Shselasky *	- Redistributions of source code must retain the above
16270710Shselasky *	  copyright notice, this list of conditions and the following
17270710Shselasky *	  disclaimer.
18270710Shselasky *
19270710Shselasky *	- Redistributions in binary form must reproduce the above
20270710Shselasky *	  copyright notice, this list of conditions and the following
21270710Shselasky *	  disclaimer in the documentation and/or other materials
22270710Shselasky *	  provided with the distribution.
23270710Shselasky *
24270710Shselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25270710Shselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26270710Shselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27270710Shselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28270710Shselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29270710Shselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30270710Shselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31270710Shselasky * SOFTWARE.
32270710Shselasky */
33270710Shselasky
34270710Shselasky
35270710Shselasky#ifndef _LINUX_ETHERDEVICE
36270710Shselasky#define _LINUX_ETHERDEVICE
37270710Shselasky
38270710Shselasky#include <linux/types.h>
39270710Shselasky
40301264Shselasky#include <sys/random.h>
41301264Shselasky#include <sys/libkern.h>
42301264Shselasky
43292105Shselasky#define	ETH_MODULE_SFF_8079		1
44292105Shselasky#define	ETH_MODULE_SFF_8079_LEN		256
45292105Shselasky#define	ETH_MODULE_SFF_8472		2
46292105Shselasky#define	ETH_MODULE_SFF_8472_LEN		512
47292105Shselasky#define	ETH_MODULE_SFF_8636		3
48292105Shselasky#define	ETH_MODULE_SFF_8636_LEN		256
49292105Shselasky#define	ETH_MODULE_SFF_8436		4
50292105Shselasky#define	ETH_MODULE_SFF_8436_LEN		256
51292105Shselasky
52292105Shselaskystruct ethtool_eeprom {
53292105Shselasky	u32	offset;
54292105Shselasky	u32	len;
55292105Shselasky};
56292105Shselasky
57292105Shselaskystruct ethtool_modinfo {
58292105Shselasky	u32	type;
59292105Shselasky	u32	eeprom_len;
60292105Shselasky};
61292105Shselasky
62270710Shselasky/**
63270710Shselasky * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
64270710Shselasky * @addr: Pointer to a six-byte array containing the Ethernet address
65270710Shselasky *
66270710Shselasky * Return true if the address is all zeroes.
67270710Shselasky */
68270710Shselaskystatic inline bool is_zero_ether_addr(const u8 *addr)
69270710Shselasky{
70270710Shselasky        return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
71270710Shselasky}
72270710Shselasky
73270710Shselasky
74270710Shselasky
75270710Shselasky/**
76270710Shselasky * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
77270710Shselasky * @addr: Pointer to a six-byte array containing the Ethernet address
78270710Shselasky *
79270710Shselasky * Return true if the address is a multicast address.
80270710Shselasky * By definition the broadcast address is also a multicast address.
81270710Shselasky */
82270710Shselaskystatic inline bool is_multicast_ether_addr(const u8 *addr)
83270710Shselasky{
84270710Shselasky        return (0x01 & addr[0]);
85270710Shselasky}
86270710Shselasky
87270710Shselasky/**
88270710Shselasky * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
89270710Shselasky * @addr: Pointer to a six-byte array containing the Ethernet address
90270710Shselasky *
91270710Shselasky * Return true if the address is the broadcast address.
92270710Shselasky */
93270710Shselaskystatic inline bool is_broadcast_ether_addr(const u8 *addr)
94270710Shselasky{
95270710Shselasky        return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
96270710Shselasky}
97270710Shselasky
98270710Shselasky/**
99270710Shselasky * is_valid_ether_addr - Determine if the given Ethernet address is valid
100270710Shselasky * @addr: Pointer to a six-byte array containing the Ethernet address
101270710Shselasky *
102270710Shselasky * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
103270710Shselasky * a multicast address, and is not FF:FF:FF:FF:FF:FF.
104270710Shselasky *
105270710Shselasky * Return true if the address is valid.
106270710Shselasky **/
107270710Shselaskystatic inline bool is_valid_ether_addr(const u8 *addr)
108270710Shselasky{
109270710Shselasky        /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
110270710Shselasky        ** explicitly check for it here. */
111270710Shselasky        return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
112270710Shselasky}
113270710Shselasky
114282513Shselaskystatic inline void ether_addr_copy(u8 *dst, const u8 *src)
115282513Shselasky{
116282513Shselasky	memcpy(dst, src, 6);
117282513Shselasky}
118270710Shselasky
119301264Shselaskystatic inline bool
120301264Shselaskyether_addr_equal(const u8 *pa, const u8 *pb)
121301264Shselasky{
122301264Shselasky	return (memcmp(pa, pb, 6) == 0);
123301264Shselasky}
124301264Shselasky
125301264Shselaskystatic inline bool
126301264Shselaskyether_addr_equal_64bits(const u8 *pa, const u8 *pb)
127301264Shselasky{
128301264Shselasky	return (memcmp(pa, pb, 6) == 0);
129301264Shselasky}
130301264Shselasky
131301264Shselaskystatic inline void
132301264Shselaskyeth_broadcast_addr(u8 *pa)
133301264Shselasky{
134301264Shselasky	memset(pa, 0xff, 6);
135301264Shselasky}
136301264Shselasky
137301264Shselaskystatic inline void
138301264Shselaskyrandom_ether_addr(u8 * dst)
139301264Shselasky{
140302271Shselasky	if (read_random(dst, 6) == 0)
141302271Shselasky		arc4rand(dst, 6, 0);
142301264Shselasky
143301264Shselasky	dst[0] &= 0xfe;
144301264Shselasky	dst[0] |= 0x02;
145301264Shselasky}
146301264Shselasky
147270710Shselasky#endif /* _LINUX_ETHERDEVICE */
148