Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2015 6WIND S.A.
3 : : * Copyright 2015 Mellanox Technologies, Ltd
4 : : */
5 : :
6 : : #include <stddef.h>
7 : : #include <stdint.h>
8 : : #include <string.h>
9 : : #include <inttypes.h>
10 : : #include <errno.h>
11 : :
12 : : #include <rte_ether.h>
13 : : #include <ethdev_driver.h>
14 : : #include <rte_common.h>
15 : :
16 : : #include "mlx5_defs.h"
17 : : #include "mlx5.h"
18 : : #include "mlx5_utils.h"
19 : : #include "mlx5_rxtx.h"
20 : :
21 : : /**
22 : : * Remove a MAC address from the internal array.
23 : : *
24 : : * @param dev
25 : : * Pointer to Ethernet device structure.
26 : : * @param index
27 : : * MAC address index.
28 : : * @param addr
29 : : * If MAC address is actually removed, it will be stored here if pointer is not a NULL.
30 : : *
31 : : * @return
32 : : * True if there was a MAC address under given index.
33 : : */
34 : : static bool
35 : 0 : mlx5_internal_mac_addr_remove(struct rte_eth_dev *dev,
36 : : uint32_t index,
37 : : struct rte_ether_addr *addr)
38 : : {
39 : : MLX5_ASSERT(index < MLX5_SH(dev)->dev_cap.max_mac_addrs);
40 [ # # ]: 0 : if (rte_is_zero_ether_addr(&dev->data->mac_addrs[index]))
41 : : return false;
42 : 0 : mlx5_os_mac_addr_remove(dev, index);
43 [ # # ]: 0 : if (addr != NULL)
44 : 0 : *addr = dev->data->mac_addrs[index];
45 : 0 : memset(&dev->data->mac_addrs[index], 0, sizeof(struct rte_ether_addr));
46 : 0 : return true;
47 : : }
48 : :
49 : : /**
50 : : * Adds a MAC address to the internal array.
51 : : *
52 : : * @param dev
53 : : * Pointer to Ethernet device structure.
54 : : * @param mac_addr
55 : : * MAC address to register.
56 : : * @param index
57 : : * MAC address index.
58 : : *
59 : : * @return
60 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
61 : : */
62 : : static int
63 : 0 : mlx5_internal_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
64 : : uint32_t index)
65 : : {
66 [ # # ]: 0 : struct mlx5_priv *priv = dev->data->dev_private;
67 : : unsigned int i;
68 : : int ret;
69 : :
70 : : MLX5_ASSERT(index < priv->sh->dev_cap.max_mac_addrs);
71 [ # # ]: 0 : if (rte_is_zero_ether_addr(mac)) {
72 : 0 : rte_errno = EINVAL;
73 : 0 : return -rte_errno;
74 : : }
75 : : /* First, make sure this address isn't already configured. */
76 [ # # ]: 0 : for (i = 0; i != priv->sh->dev_cap.max_mac_addrs; ++i) {
77 : : /* Skip this index, it's going to be reconfigured. */
78 [ # # ]: 0 : if (i == index)
79 : 0 : continue;
80 [ # # ]: 0 : if (memcmp(&dev->data->mac_addrs[i], mac, sizeof(*mac)))
81 : 0 : continue;
82 : : /* Address already configured elsewhere, return with error. */
83 : 0 : rte_errno = EADDRINUSE;
84 : 0 : return -rte_errno;
85 : : }
86 : 0 : ret = mlx5_os_mac_addr_add(dev, mac, index);
87 [ # # ]: 0 : if (ret)
88 : : return ret;
89 : :
90 : 0 : dev->data->mac_addrs[index] = *mac;
91 : 0 : return 0;
92 : : }
93 : :
94 : : /**
95 : : * DPDK callback to remove a MAC address.
96 : : *
97 : : * @param dev
98 : : * Pointer to Ethernet device structure.
99 : : * @param index
100 : : * MAC address index.
101 : : */
102 : : void
103 : 0 : mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
104 : : {
105 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
106 : 0 : struct rte_ether_addr addr = { 0 };
107 : : int ret;
108 : :
109 [ # # ]: 0 : if (index >= priv->sh->dev_cap.max_uc_mac_addrs)
110 : 0 : return;
111 [ # # ]: 0 : if (mlx5_internal_mac_addr_remove(dev, index, &addr)) {
112 : 0 : ret = mlx5_traffic_mac_remove(dev, &addr);
113 [ # # ]: 0 : if (ret)
114 : 0 : DRV_LOG(ERR, "port %u cannot update control flow rules: %s",
115 : : dev->data->port_id, strerror(rte_errno));
116 : : }
117 : : }
118 : :
119 : : /**
120 : : * DPDK callback to add a MAC address.
121 : : *
122 : : * @param dev
123 : : * Pointer to Ethernet device structure.
124 : : * @param mac_addr
125 : : * MAC address to register.
126 : : * @param index
127 : : * MAC address index.
128 : : * @param vmdq
129 : : * VMDq pool index to associate address with (ignored).
130 : : *
131 : : * @return
132 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
133 : : */
134 : : int
135 : 0 : mlx5_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
136 : : uint32_t index, uint32_t vmdq __rte_unused)
137 : : {
138 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
139 : : int ret;
140 : :
141 [ # # ]: 0 : if (index >= priv->sh->dev_cap.max_uc_mac_addrs) {
142 : 0 : rte_errno = EINVAL;
143 : 0 : return -rte_errno;
144 : : }
145 : 0 : ret = mlx5_internal_mac_addr_add(dev, mac, index);
146 [ # # ]: 0 : if (ret < 0)
147 : : return ret;
148 : 0 : return mlx5_traffic_mac_add(dev, mac);
149 : : }
150 : :
151 : : /**
152 : : * DPDK callback to set primary MAC address.
153 : : *
154 : : * @param dev
155 : : * Pointer to Ethernet device structure.
156 : : * @param mac_addr
157 : : * MAC address to register.
158 : : *
159 : : * @return
160 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
161 : : */
162 : : int
163 : 0 : mlx5_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
164 : : {
165 : : uint16_t port_id;
166 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
167 : : struct mlx5_priv *pf_priv;
168 [ # # ]: 0 : struct rte_ether_addr old_mac_addr = dev->data->mac_addrs[0];
169 : : int ret;
170 : :
171 : : /* ethdev does not check if new default address is the same as the old one. */
172 [ # # ]: 0 : if (rte_is_same_ether_addr(mac_addr, &old_mac_addr))
173 : : return 0;
174 : :
175 : : /*
176 : : * Configuring the VF instead of its representor,
177 : : * need to skip the special cases:
178 : : * - HPF on BlueField,
179 : : * - SF representors,
180 : : * - uplink ports when running in MPESW mode.
181 : : */
182 [ # # # # : 0 : if (priv->representor && !mlx5_is_hpf(dev) && !mlx5_is_sf_repr(dev) &&
# # ]
183 [ # # ]: 0 : !priv->mpesw_uplink) {
184 : 0 : DRV_LOG(DEBUG, "VF represented by port %u setting primary MAC address",
185 : : dev->data->port_id);
186 [ # # ]: 0 : if (priv->pf_bond >= 0) {
187 : : /* Bonding, get owner PF ifindex from shared data. */
188 : 0 : return mlx5_os_vf_mac_addr_modify
189 : : (priv,
190 : 0 : priv->sh->bond.ports[priv->pf_bond].ifindex,
191 : : mac_addr,
192 : 0 : MLX5_REPRESENTOR_REPR(priv->representor_id));
193 : : }
194 [ # # ]: 0 : RTE_ETH_FOREACH_DEV_SIBLING(port_id, dev->data->port_id) {
195 : 0 : pf_priv = rte_eth_devices[port_id].data->dev_private;
196 [ # # ]: 0 : if (pf_priv->master == 1)
197 : 0 : return mlx5_os_vf_mac_addr_modify
198 : : (priv, pf_priv->if_index, mac_addr,
199 : 0 : MLX5_REPRESENTOR_REPR
200 : : (priv->representor_id));
201 : : }
202 : 0 : rte_errno = -ENOTSUP;
203 : 0 : return rte_errno;
204 : : }
205 : :
206 : 0 : DRV_LOG(DEBUG, "port %u setting primary MAC address",
207 : : dev->data->port_id);
208 : 0 : ret = mlx5_mac_addr_add(dev, mac_addr, 0, 0);
209 [ # # ]: 0 : if (ret)
210 : : return ret;
211 : 0 : return mlx5_traffic_mac_remove(dev, &old_mac_addr);
212 : : }
213 : :
214 : : /**
215 : : * DPDK callback to set multicast addresses list.
216 : : *
217 : : * @see rte_eth_dev_set_mc_addr_list()
218 : : */
219 : : int
220 : 0 : mlx5_set_mc_addr_list(struct rte_eth_dev *dev,
221 : : struct rte_ether_addr *mc_addr_set, uint32_t nb_mc_addr)
222 : : {
223 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
224 : : uint32_t i;
225 : : int ret;
226 : :
227 [ # # ]: 0 : if (nb_mc_addr >= priv->sh->dev_cap.max_mc_mac_addrs) {
228 : 0 : rte_errno = ENOSPC;
229 : 0 : return -rte_errno;
230 : : }
231 [ # # ]: 0 : for (i = priv->sh->dev_cap.max_uc_mac_addrs; i != priv->sh->dev_cap.max_mac_addrs; ++i)
232 : 0 : mlx5_internal_mac_addr_remove(dev, i, NULL);
233 : 0 : i = priv->sh->dev_cap.max_uc_mac_addrs;
234 [ # # ]: 0 : while (nb_mc_addr--) {
235 : 0 : ret = mlx5_internal_mac_addr_add(dev, mc_addr_set++, i++);
236 [ # # ]: 0 : if (ret)
237 : 0 : return ret;
238 : : }
239 [ # # ]: 0 : if (!dev->data->promiscuous)
240 : 0 : return mlx5_traffic_restart(dev);
241 : : return 0;
242 : : }
|