net/mlx5: use SPDX tags in 6WIND copyrighted files
[dpdk.git] / drivers / net / mlx5 / mlx5_mac.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox.
4  */
5
6 #include <stddef.h>
7 #include <assert.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <inttypes.h>
11 #include <errno.h>
12 #include <netinet/in.h>
13 #include <sys/ioctl.h>
14 #include <arpa/inet.h>
15
16 /* Verbs header. */
17 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic ignored "-Wpedantic"
20 #endif
21 #include <infiniband/verbs.h>
22 #ifdef PEDANTIC
23 #pragma GCC diagnostic error "-Wpedantic"
24 #endif
25
26 #include <rte_ether.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_common.h>
29
30 #include "mlx5.h"
31 #include "mlx5_utils.h"
32 #include "mlx5_rxtx.h"
33 #include "mlx5_defs.h"
34
35 /**
36  * Get MAC address by querying netdevice.
37  *
38  * @param[in] priv
39  *   struct priv for the requested device.
40  * @param[out] mac
41  *   MAC address output buffer.
42  *
43  * @return
44  *   0 on success, -1 on failure and errno is set.
45  */
46 int
47 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
48 {
49         struct ifreq request;
50
51         if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
52                 return -1;
53         memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
54         return 0;
55 }
56
57 /**
58  * DPDK callback to remove a MAC address.
59  *
60  * @param dev
61  *   Pointer to Ethernet device structure.
62  * @param index
63  *   MAC address index.
64  */
65 void
66 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
67 {
68         assert(index < MLX5_MAX_MAC_ADDRESSES);
69         memset(&dev->data->mac_addrs[index], 0, sizeof(struct ether_addr));
70         if (!dev->data->promiscuous)
71                 mlx5_traffic_restart(dev);
72 }
73
74 /**
75  * DPDK callback to add a MAC address.
76  *
77  * @param dev
78  *   Pointer to Ethernet device structure.
79  * @param mac_addr
80  *   MAC address to register.
81  * @param index
82  *   MAC address index.
83  * @param vmdq
84  *   VMDq pool index to associate address with (ignored).
85  *
86  * @return
87  *   0 on success.
88  */
89 int
90 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac,
91                   uint32_t index, uint32_t vmdq)
92 {
93         unsigned int i;
94         int ret = 0;
95
96         (void)vmdq;
97         assert(index < MLX5_MAX_MAC_ADDRESSES);
98         /* First, make sure this address isn't already configured. */
99         for (i = 0; (i != MLX5_MAX_MAC_ADDRESSES); ++i) {
100                 /* Skip this index, it's going to be reconfigured. */
101                 if (i == index)
102                         continue;
103                 if (memcmp(&dev->data->mac_addrs[i], mac, sizeof(*mac)))
104                         continue;
105                 /* Address already configured elsewhere, return with error. */
106                 return EADDRINUSE;
107         }
108         dev->data->mac_addrs[index] = *mac;
109         if (!dev->data->promiscuous)
110                 mlx5_traffic_restart(dev);
111         return ret;
112 }
113
114 /**
115  * DPDK callback to set primary MAC address.
116  *
117  * @param dev
118  *   Pointer to Ethernet device structure.
119  * @param mac_addr
120  *   MAC address to register.
121  */
122 void
123 mlx5_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
124 {
125         DEBUG("%p: setting primary MAC address", (void *)dev);
126         mlx5_mac_addr_add(dev, mac_addr, 0, 0);
127 }