New upstream version 17.11.3
[deb_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 <sys/ioctl.h>
42 #include <arpa/inet.h>
43
44 /* Verbs header. */
45 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic ignored "-Wpedantic"
48 #endif
49 #include <infiniband/verbs.h>
50 #ifdef PEDANTIC
51 #pragma GCC diagnostic error "-Wpedantic"
52 #endif
53
54 #include <rte_ether.h>
55 #include <rte_ethdev.h>
56 #include <rte_common.h>
57
58 #include "mlx5.h"
59 #include "mlx5_utils.h"
60 #include "mlx5_rxtx.h"
61 #include "mlx5_defs.h"
62
63 /**
64  * Get MAC address by querying netdevice.
65  *
66  * @param[in] dev
67  *   Pointer to Ethernet device.
68  * @param[out] mac
69  *   MAC address output buffer.
70  *
71  * @return
72  *   0 on success, a negative errno value otherwise and rte_errno is set.
73  */
74 int
75 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[ETHER_ADDR_LEN])
76 {
77         struct ifreq request;
78         int ret;
79
80         ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
81         if (ret)
82                 return ret;
83         memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
84         return 0;
85 }
86
87 /**
88  * DPDK callback to remove a MAC address.
89  *
90  * @param dev
91  *   Pointer to Ethernet device structure.
92  * @param index
93  *   MAC address index.
94  */
95 void
96 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
97 {
98         assert(index < MLX5_MAX_MAC_ADDRESSES);
99         memset(&dev->data->mac_addrs[index], 0, sizeof(struct ether_addr));
100         if (!dev->data->promiscuous) {
101                 int ret = mlx5_traffic_restart(dev);
102
103                 if (ret)
104                         DRV_LOG(ERR, "port %u cannot remove mac address: %s",
105                                 dev->data->port_id, strerror(rte_errno));
106         }
107 }
108
109 /**
110  * DPDK callback to add a MAC address.
111  *
112  * @param dev
113  *   Pointer to Ethernet device structure.
114  * @param mac_addr
115  *   MAC address to register.
116  * @param index
117  *   MAC address index.
118  * @param vmdq
119  *   VMDq pool index to associate address with (ignored).
120  *
121  * @return
122  *   0 on success, a negative errno value otherwise and rte_errno is set.
123  */
124 int
125 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac,
126                   uint32_t index, uint32_t vmdq __rte_unused)
127 {
128         unsigned int i;
129
130         assert(index < MLX5_MAX_MAC_ADDRESSES);
131         /* First, make sure this address isn't already configured. */
132         for (i = 0; (i != MLX5_MAX_MAC_ADDRESSES); ++i) {
133                 /* Skip this index, it's going to be reconfigured. */
134                 if (i == index)
135                         continue;
136                 if (memcmp(&dev->data->mac_addrs[i], mac, sizeof(*mac)))
137                         continue;
138                 /* Address already configured elsewhere, return with error. */
139                 rte_errno = EADDRINUSE;
140                 return -rte_errno;
141         }
142         dev->data->mac_addrs[index] = *mac;
143         if (!dev->data->promiscuous)
144                 return mlx5_traffic_restart(dev);
145         return 0;
146 }
147
148 /**
149  * DPDK callback to set primary MAC address.
150  *
151  * @param dev
152  *   Pointer to Ethernet device structure.
153  * @param mac_addr
154  *   MAC address to register.
155  */
156 void
157 mlx5_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
158 {
159         int ret;
160
161         DRV_LOG(DEBUG, "port %u setting primary MAC address",
162                 dev->data->port_id);
163
164         ret = mlx5_mac_addr_add(dev, mac_addr, 0, 0);
165         if (ret)
166                 DRV_LOG(ERR, "port %u cannot set mac address: %s",
167                         dev->data->port_id, strerror(rte_errno));
168 }