mlx5: introduce new driver for Mellanox ConnectX-4 adapters
[dpdk.git] / drivers / net / mlx5 / mlx5_mac.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stddef.h>
35 #include <assert.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <inttypes.h>
39 #include <errno.h>
40 #include <netinet/in.h>
41 #include <linux/if.h>
42 #include <sys/ioctl.h>
43 #include <arpa/inet.h>
44
45 /* Verbs header. */
46 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
47 #ifdef PEDANTIC
48 #pragma GCC diagnostic ignored "-pedantic"
49 #endif
50 #include <infiniband/verbs.h>
51 #ifdef PEDANTIC
52 #pragma GCC diagnostic error "-pedantic"
53 #endif
54
55 /* DPDK headers don't like -pedantic. */
56 #ifdef PEDANTIC
57 #pragma GCC diagnostic ignored "-pedantic"
58 #endif
59 #include <rte_ether.h>
60 #include <rte_ethdev.h>
61 #include <rte_common.h>
62 #ifdef PEDANTIC
63 #pragma GCC diagnostic error "-pedantic"
64 #endif
65
66 #include "mlx5.h"
67 #include "mlx5_utils.h"
68
69 /**
70  * Get MAC address by querying netdevice.
71  *
72  * @param[in] priv
73  *   struct priv for the requested device.
74  * @param[out] mac
75  *   MAC address output buffer.
76  *
77  * @return
78  *   0 on success, -1 on failure and errno is set.
79  */
80 int
81 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
82 {
83         struct ifreq request;
84
85         if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
86                 return -1;
87         memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
88         return 0;
89 }
90
91 /**
92  * Unregister a MAC address.
93  *
94  * @param priv
95  *   Pointer to private structure.
96  * @param mac_index
97  *   MAC address index.
98  */
99 static void
100 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
101 {
102         assert(mac_index < RTE_DIM(priv->mac));
103         if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
104                 return;
105         BITFIELD_RESET(priv->mac_configured, mac_index);
106 }
107
108 /**
109  * Register a MAC address.
110  *
111  * @param priv
112  *   Pointer to private structure.
113  * @param mac_index
114  *   MAC address index to use.
115  * @param mac
116  *   MAC address to register.
117  *
118  * @return
119  *   0 on success, errno value on failure.
120  */
121 int
122 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
123                   const uint8_t (*mac)[ETHER_ADDR_LEN])
124 {
125         unsigned int i;
126
127         assert(mac_index < RTE_DIM(priv->mac));
128         /* First, make sure this address isn't already configured. */
129         for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
130                 /* Skip this index, it's going to be reconfigured. */
131                 if (i == mac_index)
132                         continue;
133                 if (!BITFIELD_ISSET(priv->mac_configured, i))
134                         continue;
135                 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
136                         continue;
137                 /* Address already configured elsewhere, return with error. */
138                 return EADDRINUSE;
139         }
140         if (BITFIELD_ISSET(priv->mac_configured, mac_index))
141                 priv_mac_addr_del(priv, mac_index);
142         priv->mac[mac_index] = (struct ether_addr){
143                 {
144                         (*mac)[0], (*mac)[1], (*mac)[2],
145                         (*mac)[3], (*mac)[4], (*mac)[5]
146                 }
147         };
148         BITFIELD_SET(priv->mac_configured, mac_index);
149         return 0;
150 }