1 /*
2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33 #include <linux/dim.h>
34 #include <linux/debugfs.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/switchdev.h>
37 #include <net/pkt_cls.h>
38 #include <net/act_api.h>
39 #include <net/devlink.h>
40 #include <net/ipv6_stubs.h>
41
42 #include "eswitch.h"
43 #include "en.h"
44 #include "en/dim.h"
45 #include "en_rep.h"
46 #include "en/params.h"
47 #include "en/txrx.h"
48 #include "en_tc.h"
49 #include "en/rep/tc.h"
50 #include "en/rep/neigh.h"
51 #include "en/rep/bridge.h"
52 #include "en/devlink.h"
53 #include "fs_core.h"
54 #include "lib/mlx5.h"
55 #include "lib/devcom.h"
56 #include "lib/vxlan.h"
57 #define CREATE_TRACE_POINTS
58 #include "diag/en_rep_tracepoint.h"
59 #include "diag/reporter_vnic.h"
60 #include "en_accel/ipsec.h"
61 #include "en/tc/int_port.h"
62 #include "en/ptp.h"
63 #include "en/fs_ethtool.h"
64
65 #define MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE \
66 max(0x7, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)
67 #define MLX5E_REP_PARAMS_DEF_NUM_CHANNELS 1
68
69 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
70
mlx5e_rep_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)71 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
72 struct ethtool_drvinfo *drvinfo)
73 {
74 struct mlx5e_priv *priv = netdev_priv(dev);
75 struct mlx5_core_dev *mdev = priv->mdev;
76 int count;
77
78 strscpy(drvinfo->driver, mlx5e_rep_driver_name,
79 sizeof(drvinfo->driver));
80 count = snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
81 "%d.%d.%04d (%.16s)", fw_rev_maj(mdev),
82 fw_rev_min(mdev), fw_rev_sub(mdev), mdev->board_id);
83 if (count >= sizeof(drvinfo->fw_version))
84 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
85 "%d.%d.%04d", fw_rev_maj(mdev),
86 fw_rev_min(mdev), fw_rev_sub(mdev));
87 }
88
89 static const struct counter_desc sw_rep_stats_desc[] = {
90 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
91 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
92 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
93 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
94 };
95
96 static const struct counter_desc vport_rep_stats_desc[] = {
97 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, vport_rx_packets) },
98 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, vport_rx_bytes) },
99 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, vport_tx_packets) },
100 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, vport_tx_bytes) },
101 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
102 rx_vport_rdma_unicast_packets) },
103 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, rx_vport_rdma_unicast_bytes) },
104 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
105 tx_vport_rdma_unicast_packets) },
106 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, tx_vport_rdma_unicast_bytes) },
107 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
108 rx_vport_rdma_multicast_packets) },
109 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
110 rx_vport_rdma_multicast_bytes) },
111 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
112 tx_vport_rdma_multicast_packets) },
113 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
114 tx_vport_rdma_multicast_bytes) },
115 };
116
117 static const struct counter_desc vport_rep_loopback_stats_desc[] = {
118 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
119 vport_loopback_packets) },
120 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
121 vport_loopback_bytes) },
122 };
123
124 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
125 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
126 #define NUM_VPORT_REP_LOOPBACK_COUNTERS(dev) \
127 (MLX5_CAP_GEN(dev, vport_counter_local_loopback) ? \
128 ARRAY_SIZE(vport_rep_loopback_stats_desc) : 0)
129
MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(sw_rep)130 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(sw_rep)
131 {
132 return NUM_VPORT_REP_SW_COUNTERS;
133 }
134
MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(sw_rep)135 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(sw_rep)
136 {
137 int i;
138
139 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
140 ethtool_puts(data, sw_rep_stats_desc[i].format);
141 }
142
MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(sw_rep)143 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(sw_rep)
144 {
145 int i;
146
147 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
148 mlx5e_ethtool_put_stat(
149 data, MLX5E_READ_CTR64_CPU(&priv->stats.sw,
150 sw_rep_stats_desc, i));
151 }
152
MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(sw_rep)153 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(sw_rep)
154 {
155 struct mlx5e_sw_stats *s = &priv->stats.sw;
156 struct rtnl_link_stats64 stats64 = {};
157
158 memset(s, 0, sizeof(*s));
159 mlx5e_fold_sw_stats64(priv, &stats64);
160
161 s->rx_packets = stats64.rx_packets;
162 s->rx_bytes = stats64.rx_bytes;
163 s->tx_packets = stats64.tx_packets;
164 s->tx_bytes = stats64.tx_bytes;
165 s->tx_queue_dropped = stats64.tx_dropped;
166 }
167
MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(vport_rep)168 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(vport_rep)
169 {
170 return NUM_VPORT_REP_HW_COUNTERS +
171 NUM_VPORT_REP_LOOPBACK_COUNTERS(priv->mdev);
172 }
173
MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(vport_rep)174 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(vport_rep)
175 {
176 int i;
177
178 for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
179 ethtool_puts(data, vport_rep_stats_desc[i].format);
180 for (i = 0; i < NUM_VPORT_REP_LOOPBACK_COUNTERS(priv->mdev); i++)
181 ethtool_puts(data, vport_rep_loopback_stats_desc[i].format);
182 }
183
MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(vport_rep)184 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(vport_rep)
185 {
186 int i;
187
188 for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
189 mlx5e_ethtool_put_stat(
190 data, MLX5E_READ_CTR64_CPU(&priv->stats.rep_stats,
191 vport_rep_stats_desc, i));
192 for (i = 0; i < NUM_VPORT_REP_LOOPBACK_COUNTERS(priv->mdev); i++)
193 mlx5e_ethtool_put_stat(
194 data,
195 MLX5E_READ_CTR64_CPU(&priv->stats.rep_stats,
196 vport_rep_loopback_stats_desc, i));
197 }
198
MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(vport_rep)199 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(vport_rep)
200 {
201 struct mlx5e_rep_stats *rep_stats = &priv->stats.rep_stats;
202 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
203 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
204 struct mlx5e_rep_priv *rpriv = priv->ppriv;
205 struct mlx5_eswitch_rep *rep = rpriv->rep;
206 u32 *out;
207 int err;
208
209 out = kvzalloc(outlen, GFP_KERNEL);
210 if (!out)
211 return;
212
213 err = mlx5_core_query_vport_counter(esw->dev, 1, rep->vport - 1, 0, out);
214 if (err) {
215 netdev_warn(priv->netdev, "vport %d error %d reading stats\n",
216 rep->vport, err);
217 goto out;
218 }
219
220 #define MLX5_GET_CTR(p, x) \
221 MLX5_GET64(query_vport_counter_out, p, x)
222 /* flip tx/rx as we are reporting the counters for the switch vport */
223 rep_stats->vport_rx_packets =
224 MLX5_GET_CTR(out, transmitted_ib_unicast.packets) +
225 MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
226 MLX5_GET_CTR(out, transmitted_ib_multicast.packets) +
227 MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
228 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
229
230 rep_stats->vport_tx_packets =
231 MLX5_GET_CTR(out, received_ib_unicast.packets) +
232 MLX5_GET_CTR(out, received_eth_unicast.packets) +
233 MLX5_GET_CTR(out, received_ib_multicast.packets) +
234 MLX5_GET_CTR(out, received_eth_multicast.packets) +
235 MLX5_GET_CTR(out, received_eth_broadcast.packets);
236
237 rep_stats->vport_rx_bytes =
238 MLX5_GET_CTR(out, transmitted_ib_unicast.octets) +
239 MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
240 MLX5_GET_CTR(out, transmitted_ib_multicast.octets) +
241 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
242
243 rep_stats->vport_tx_bytes =
244 MLX5_GET_CTR(out, received_ib_unicast.octets) +
245 MLX5_GET_CTR(out, received_eth_unicast.octets) +
246 MLX5_GET_CTR(out, received_ib_multicast.octets) +
247 MLX5_GET_CTR(out, received_eth_multicast.octets) +
248 MLX5_GET_CTR(out, received_eth_broadcast.octets);
249
250 rep_stats->rx_vport_rdma_unicast_packets =
251 MLX5_GET_CTR(out, transmitted_ib_unicast.packets);
252 rep_stats->tx_vport_rdma_unicast_packets =
253 MLX5_GET_CTR(out, received_ib_unicast.packets);
254 rep_stats->rx_vport_rdma_unicast_bytes =
255 MLX5_GET_CTR(out, transmitted_ib_unicast.octets);
256 rep_stats->tx_vport_rdma_unicast_bytes =
257 MLX5_GET_CTR(out, received_ib_unicast.octets);
258 rep_stats->rx_vport_rdma_multicast_packets =
259 MLX5_GET_CTR(out, transmitted_ib_multicast.packets);
260 rep_stats->tx_vport_rdma_multicast_packets =
261 MLX5_GET_CTR(out, received_ib_multicast.packets);
262 rep_stats->rx_vport_rdma_multicast_bytes =
263 MLX5_GET_CTR(out, transmitted_ib_multicast.octets);
264 rep_stats->tx_vport_rdma_multicast_bytes =
265 MLX5_GET_CTR(out, received_ib_multicast.octets);
266
267 if (MLX5_CAP_GEN(priv->mdev, vport_counter_local_loopback)) {
268 rep_stats->vport_loopback_packets =
269 MLX5_GET_CTR(out, local_loopback.packets);
270 rep_stats->vport_loopback_bytes =
271 MLX5_GET_CTR(out, local_loopback.octets);
272 }
273
274 out:
275 kvfree(out);
276 }
277
mlx5e_rep_query_aggr_q_counter(struct mlx5_core_dev * dev,int vport,void * out)278 static int mlx5e_rep_query_aggr_q_counter(struct mlx5_core_dev *dev, int vport, void *out)
279 {
280 u32 in[MLX5_ST_SZ_DW(query_q_counter_in)] = {};
281
282 MLX5_SET(query_q_counter_in, in, opcode, MLX5_CMD_OP_QUERY_Q_COUNTER);
283 MLX5_SET(query_q_counter_in, in, other_vport, 1);
284 MLX5_SET(query_q_counter_in, in, vport_number, vport);
285 MLX5_SET(query_q_counter_in, in, aggregate, 1);
286
287 return mlx5_cmd_exec_inout(dev, query_q_counter, in, out);
288 }
289
mlx5e_rep_update_vport_q_counter(struct mlx5e_priv * priv)290 static void mlx5e_rep_update_vport_q_counter(struct mlx5e_priv *priv)
291 {
292 struct mlx5e_rep_stats *rep_stats = &priv->stats.rep_stats;
293 u32 out[MLX5_ST_SZ_DW(query_q_counter_out)] = {};
294 struct mlx5e_rep_priv *rpriv = priv->ppriv;
295 struct mlx5_eswitch_rep *rep = rpriv->rep;
296 int err;
297
298 if (!MLX5_CAP_GEN(priv->mdev, q_counter_other_vport) ||
299 !MLX5_CAP_GEN(priv->mdev, q_counter_aggregation))
300 return;
301
302 err = mlx5e_rep_query_aggr_q_counter(priv->mdev, rep->vport, out);
303 if (err) {
304 netdev_warn(priv->netdev, "failed reading stats on vport %d, error %d\n",
305 rep->vport, err);
306 return;
307 }
308
309 rep_stats->rx_vport_out_of_buffer = MLX5_GET(query_q_counter_out, out, out_of_buffer);
310 }
311
mlx5e_rep_get_strings(struct net_device * dev,u32 stringset,u8 * data)312 static void mlx5e_rep_get_strings(struct net_device *dev,
313 u32 stringset, u8 *data)
314 {
315 struct mlx5e_priv *priv = netdev_priv(dev);
316
317 switch (stringset) {
318 case ETH_SS_STATS:
319 mlx5e_stats_fill_strings(priv, data);
320 break;
321 }
322 }
323
mlx5e_rep_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)324 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
325 struct ethtool_stats *stats, u64 *data)
326 {
327 struct mlx5e_priv *priv = netdev_priv(dev);
328
329 mlx5e_ethtool_get_ethtool_stats(priv, stats, data);
330 }
331
mlx5e_rep_get_sset_count(struct net_device * dev,int sset)332 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
333 {
334 struct mlx5e_priv *priv = netdev_priv(dev);
335
336 switch (sset) {
337 case ETH_SS_STATS:
338 return mlx5e_stats_total_num(priv);
339 default:
340 return -EOPNOTSUPP;
341 }
342 }
343
344 static void
mlx5e_rep_get_ringparam(struct net_device * dev,struct ethtool_ringparam * param,struct kernel_ethtool_ringparam * kernel_param,struct netlink_ext_ack * extack)345 mlx5e_rep_get_ringparam(struct net_device *dev,
346 struct ethtool_ringparam *param,
347 struct kernel_ethtool_ringparam *kernel_param,
348 struct netlink_ext_ack *extack)
349 {
350 struct mlx5e_priv *priv = netdev_priv(dev);
351
352 mlx5e_ethtool_get_ringparam(priv, param, kernel_param);
353 }
354
355 static int
mlx5e_rep_set_ringparam(struct net_device * dev,struct ethtool_ringparam * param,struct kernel_ethtool_ringparam * kernel_param,struct netlink_ext_ack * extack)356 mlx5e_rep_set_ringparam(struct net_device *dev,
357 struct ethtool_ringparam *param,
358 struct kernel_ethtool_ringparam *kernel_param,
359 struct netlink_ext_ack *extack)
360 {
361 struct mlx5e_priv *priv = netdev_priv(dev);
362
363 return mlx5e_ethtool_set_ringparam(priv, param, extack);
364 }
365
mlx5e_rep_get_channels(struct net_device * dev,struct ethtool_channels * ch)366 static void mlx5e_rep_get_channels(struct net_device *dev,
367 struct ethtool_channels *ch)
368 {
369 struct mlx5e_priv *priv = netdev_priv(dev);
370
371 mlx5e_ethtool_get_channels(priv, ch);
372 }
373
mlx5e_rep_set_channels(struct net_device * dev,struct ethtool_channels * ch)374 static int mlx5e_rep_set_channels(struct net_device *dev,
375 struct ethtool_channels *ch)
376 {
377 struct mlx5e_priv *priv = netdev_priv(dev);
378
379 return mlx5e_ethtool_set_channels(priv, ch);
380 }
381
mlx5e_rep_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * coal,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)382 static int mlx5e_rep_get_coalesce(struct net_device *netdev,
383 struct ethtool_coalesce *coal,
384 struct kernel_ethtool_coalesce *kernel_coal,
385 struct netlink_ext_ack *extack)
386 {
387 struct mlx5e_priv *priv = netdev_priv(netdev);
388
389 return mlx5e_ethtool_get_coalesce(priv, coal, kernel_coal, extack);
390 }
391
mlx5e_rep_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * coal,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)392 static int mlx5e_rep_set_coalesce(struct net_device *netdev,
393 struct ethtool_coalesce *coal,
394 struct kernel_ethtool_coalesce *kernel_coal,
395 struct netlink_ext_ack *extack)
396 {
397 struct mlx5e_priv *priv = netdev_priv(netdev);
398
399 return mlx5e_ethtool_set_coalesce(priv, coal, kernel_coal, extack);
400 }
401
mlx5e_rep_get_rxfh_key_size(struct net_device * netdev)402 static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
403 {
404 struct mlx5e_priv *priv = netdev_priv(netdev);
405
406 return mlx5e_ethtool_get_rxfh_key_size(priv);
407 }
408
mlx5e_rep_get_rxfh_indir_size(struct net_device * netdev)409 static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
410 {
411 struct mlx5e_priv *priv = netdev_priv(netdev);
412
413 return mlx5e_ethtool_get_rxfh_indir_size(priv);
414 }
415
416 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
417 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
418 ETHTOOL_COALESCE_MAX_FRAMES |
419 ETHTOOL_COALESCE_USE_ADAPTIVE,
420 .get_drvinfo = mlx5e_rep_get_drvinfo,
421 .get_link = ethtool_op_get_link,
422 .get_strings = mlx5e_rep_get_strings,
423 .get_sset_count = mlx5e_rep_get_sset_count,
424 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
425 .get_ringparam = mlx5e_rep_get_ringparam,
426 .set_ringparam = mlx5e_rep_set_ringparam,
427 .get_channels = mlx5e_rep_get_channels,
428 .set_channels = mlx5e_rep_set_channels,
429 .get_coalesce = mlx5e_rep_get_coalesce,
430 .set_coalesce = mlx5e_rep_set_coalesce,
431 .get_per_queue_coalesce = mlx5e_get_per_queue_coalesce,
432 .set_per_queue_coalesce = mlx5e_set_per_queue_coalesce,
433 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
434 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
435 };
436
mlx5e_sqs2vport_stop(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep)437 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
438 struct mlx5_eswitch_rep *rep)
439 {
440 struct mlx5e_rep_sq *rep_sq, *tmp;
441 struct mlx5e_rep_sq_peer *sq_peer;
442 struct mlx5e_rep_priv *rpriv;
443 unsigned long i;
444
445 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
446 return;
447
448 rpriv = mlx5e_rep_to_rep_priv(rep);
449 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
450 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
451 xa_for_each(&rep_sq->sq_peer, i, sq_peer) {
452 if (sq_peer->rule)
453 mlx5_eswitch_del_send_to_vport_rule(sq_peer->rule);
454
455 xa_erase(&rep_sq->sq_peer, i);
456 kfree(sq_peer);
457 }
458
459 xa_destroy(&rep_sq->sq_peer);
460 list_del(&rep_sq->list);
461 kfree(rep_sq);
462 }
463 }
464
mlx5e_sqs2vport_add_peers_rules(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep,struct mlx5e_rep_sq * rep_sq,int i)465 static int mlx5e_sqs2vport_add_peers_rules(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep,
466 struct mlx5e_rep_sq *rep_sq, int i)
467 {
468 struct mlx5_flow_handle *flow_rule;
469 struct mlx5_devcom_comp_dev *tmp;
470 struct mlx5_eswitch *peer_esw;
471
472 mlx5_devcom_for_each_peer_entry(esw->devcom, peer_esw, tmp) {
473 u16 peer_rule_idx = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
474 struct mlx5e_rep_sq_peer *sq_peer;
475 int err;
476
477 sq_peer = kzalloc(sizeof(*sq_peer), GFP_KERNEL);
478 if (!sq_peer)
479 return -ENOMEM;
480
481 flow_rule = mlx5_eswitch_add_send_to_vport_rule(peer_esw, esw,
482 rep, rep_sq->sqn);
483 if (IS_ERR(flow_rule)) {
484 kfree(sq_peer);
485 return PTR_ERR(flow_rule);
486 }
487
488 sq_peer->rule = flow_rule;
489 sq_peer->peer = peer_esw;
490 err = xa_insert(&rep_sq->sq_peer, peer_rule_idx, sq_peer, GFP_KERNEL);
491 if (err) {
492 kfree(sq_peer);
493 mlx5_eswitch_del_send_to_vport_rule(flow_rule);
494 return err;
495 }
496 }
497
498 return 0;
499 }
500
mlx5e_sqs2vport_start(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep,u32 * sqns_array,int sqns_num)501 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
502 struct mlx5_eswitch_rep *rep,
503 u32 *sqns_array, int sqns_num)
504 {
505 struct mlx5_flow_handle *flow_rule;
506 struct mlx5e_rep_priv *rpriv;
507 struct mlx5e_rep_sq *rep_sq;
508 bool devcom_locked = false;
509 int err;
510 int i;
511
512 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
513 return 0;
514
515 rpriv = mlx5e_rep_to_rep_priv(rep);
516
517 if (mlx5_devcom_comp_is_ready(esw->devcom) &&
518 mlx5_devcom_for_each_peer_begin(esw->devcom))
519 devcom_locked = true;
520
521 for (i = 0; i < sqns_num; i++) {
522 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
523 if (!rep_sq) {
524 err = -ENOMEM;
525 goto out_err;
526 }
527
528 /* Add re-inject rule to the PF/representor sqs */
529 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw, esw, rep,
530 sqns_array[i]);
531 if (IS_ERR(flow_rule)) {
532 err = PTR_ERR(flow_rule);
533 kfree(rep_sq);
534 goto out_err;
535 }
536 rep_sq->send_to_vport_rule = flow_rule;
537 rep_sq->sqn = sqns_array[i];
538
539 xa_init(&rep_sq->sq_peer);
540 if (devcom_locked) {
541 err = mlx5e_sqs2vport_add_peers_rules(esw, rep, rep_sq, i);
542 if (err) {
543 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
544 xa_destroy(&rep_sq->sq_peer);
545 kfree(rep_sq);
546 goto out_err;
547 }
548 }
549
550 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
551 }
552
553 if (devcom_locked)
554 mlx5_devcom_for_each_peer_end(esw->devcom);
555
556 return 0;
557
558 out_err:
559 mlx5e_sqs2vport_stop(esw, rep);
560
561 if (devcom_locked)
562 mlx5_devcom_for_each_peer_end(esw->devcom);
563
564 return err;
565 }
566
567 static int
mlx5e_add_sqs_fwd_rules(struct mlx5e_priv * priv)568 mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
569 {
570 int sqs_per_channel = mlx5e_get_dcb_num_tc(&priv->channels.params);
571 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
572 bool is_uplink_rep = mlx5e_is_uplink_rep(priv);
573 struct mlx5e_rep_priv *rpriv = priv->ppriv;
574 struct mlx5_eswitch_rep *rep = rpriv->rep;
575 int n, tc, nch, num_sqs = 0;
576 struct mlx5e_channel *c;
577 int err = -ENOMEM;
578 bool ptp_sq;
579 u32 *sqs;
580
581 ptp_sq = !!(priv->channels.ptp &&
582 MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_TX_PORT_TS));
583 nch = priv->channels.num + ptp_sq;
584 /* +2 for xdpsqs, they don't exist on the ptp channel but will not be
585 * counted for by num_sqs.
586 */
587 if (is_uplink_rep)
588 sqs_per_channel += 2;
589
590 sqs = kvcalloc(nch * sqs_per_channel, sizeof(*sqs), GFP_KERNEL);
591 if (!sqs)
592 goto out;
593
594 for (n = 0; n < priv->channels.num; n++) {
595 c = priv->channels.c[n];
596 for (tc = 0; tc < c->num_tc; tc++)
597 sqs[num_sqs++] = c->sq[tc].sqn;
598
599 if (is_uplink_rep) {
600 if (c->xdp)
601 sqs[num_sqs++] = c->rq_xdpsq.sqn;
602
603 sqs[num_sqs++] = c->xdpsq.sqn;
604 }
605 }
606 if (ptp_sq) {
607 struct mlx5e_ptp *ptp_ch = priv->channels.ptp;
608
609 for (tc = 0; tc < ptp_ch->num_tc; tc++)
610 sqs[num_sqs++] = ptp_ch->ptpsq[tc].txqsq.sqn;
611 }
612
613 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
614 kvfree(sqs);
615
616 out:
617 if (err)
618 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
619 return err;
620 }
621
622 static void
mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv * priv)623 mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
624 {
625 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
626 struct mlx5e_rep_priv *rpriv = priv->ppriv;
627 struct mlx5_eswitch_rep *rep = rpriv->rep;
628
629 mlx5e_sqs2vport_stop(esw, rep);
630 }
631
632 static int
mlx5e_rep_add_meta_tunnel_rule(struct mlx5e_priv * priv)633 mlx5e_rep_add_meta_tunnel_rule(struct mlx5e_priv *priv)
634 {
635 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
636 struct mlx5e_rep_priv *rpriv = priv->ppriv;
637 struct mlx5_eswitch_rep *rep = rpriv->rep;
638 struct mlx5_flow_handle *flow_rule;
639 struct mlx5_flow_group *g;
640
641 g = esw->fdb_table.offloads.send_to_vport_meta_grp;
642 if (!g)
643 return 0;
644
645 flow_rule = mlx5_eswitch_add_send_to_vport_meta_rule(esw, rep->vport);
646 if (IS_ERR(flow_rule))
647 return PTR_ERR(flow_rule);
648
649 rpriv->send_to_vport_meta_rule = flow_rule;
650
651 return 0;
652 }
653
654 static void
mlx5e_rep_del_meta_tunnel_rule(struct mlx5e_priv * priv)655 mlx5e_rep_del_meta_tunnel_rule(struct mlx5e_priv *priv)
656 {
657 struct mlx5e_rep_priv *rpriv = priv->ppriv;
658
659 if (rpriv->send_to_vport_meta_rule)
660 mlx5_eswitch_del_send_to_vport_meta_rule(rpriv->send_to_vport_meta_rule);
661 }
662
mlx5e_rep_activate_channels(struct mlx5e_priv * priv)663 void mlx5e_rep_activate_channels(struct mlx5e_priv *priv)
664 {
665 mlx5e_add_sqs_fwd_rules(priv);
666 mlx5e_rep_add_meta_tunnel_rule(priv);
667 }
668
mlx5e_rep_deactivate_channels(struct mlx5e_priv * priv)669 void mlx5e_rep_deactivate_channels(struct mlx5e_priv *priv)
670 {
671 mlx5e_rep_del_meta_tunnel_rule(priv);
672 mlx5e_remove_sqs_fwd_rules(priv);
673 }
674
mlx5e_rep_open(struct net_device * dev)675 static int mlx5e_rep_open(struct net_device *dev)
676 {
677 struct mlx5e_priv *priv = netdev_priv(dev);
678 struct mlx5e_rep_priv *rpriv = priv->ppriv;
679 struct mlx5_eswitch_rep *rep = rpriv->rep;
680 int err;
681
682 mutex_lock(&priv->state_lock);
683 err = mlx5e_open_locked(dev);
684 if (err)
685 goto unlock;
686
687 if (!mlx5_modify_vport_admin_state(priv->mdev,
688 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
689 rep->vport, 1,
690 MLX5_VPORT_ADMIN_STATE_UP))
691 netif_carrier_on(dev);
692
693 unlock:
694 mutex_unlock(&priv->state_lock);
695 return err;
696 }
697
mlx5e_rep_close(struct net_device * dev)698 static int mlx5e_rep_close(struct net_device *dev)
699 {
700 struct mlx5e_priv *priv = netdev_priv(dev);
701 struct mlx5e_rep_priv *rpriv = priv->ppriv;
702 struct mlx5_eswitch_rep *rep = rpriv->rep;
703 int ret;
704
705 mutex_lock(&priv->state_lock);
706 mlx5_modify_vport_admin_state(priv->mdev,
707 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
708 rep->vport, 1,
709 MLX5_VPORT_ADMIN_STATE_DOWN);
710 ret = mlx5e_close_locked(dev);
711 mutex_unlock(&priv->state_lock);
712 return ret;
713 }
714
mlx5e_is_uplink_rep(struct mlx5e_priv * priv)715 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
716 {
717 struct mlx5e_rep_priv *rpriv = priv->ppriv;
718 struct mlx5_eswitch_rep *rep;
719
720 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
721 return false;
722
723 if (!rpriv) /* non vport rep mlx5e instances don't use this field */
724 return false;
725
726 rep = rpriv->rep;
727 return (rep->vport == MLX5_VPORT_UPLINK);
728 }
729
mlx5e_rep_has_offload_stats(const struct net_device * dev,int attr_id)730 bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
731 {
732 switch (attr_id) {
733 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
734 return true;
735 }
736
737 return false;
738 }
739
740 static int
mlx5e_get_sw_stats64(const struct net_device * dev,struct rtnl_link_stats64 * stats)741 mlx5e_get_sw_stats64(const struct net_device *dev,
742 struct rtnl_link_stats64 *stats)
743 {
744 struct mlx5e_priv *priv = netdev_priv(dev);
745
746 mlx5e_fold_sw_stats64(priv, stats);
747 return 0;
748 }
749
mlx5e_rep_get_offload_stats(int attr_id,const struct net_device * dev,void * sp)750 int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
751 void *sp)
752 {
753 switch (attr_id) {
754 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
755 return mlx5e_get_sw_stats64(dev, sp);
756 }
757
758 return -EINVAL;
759 }
760
761 static void
mlx5e_rep_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)762 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
763 {
764 struct mlx5e_priv *priv = netdev_priv(dev);
765
766 /* update HW stats in background for next time */
767 mlx5e_queue_update_stats(priv);
768 mlx5e_stats_copy_rep_stats(stats, &priv->stats.rep_stats);
769 }
770
mlx5e_rep_change_mtu(struct net_device * netdev,int new_mtu)771 static int mlx5e_rep_change_mtu(struct net_device *netdev, int new_mtu)
772 {
773 return mlx5e_change_mtu(netdev, new_mtu, NULL);
774 }
775
mlx5e_rep_change_carrier(struct net_device * dev,bool new_carrier)776 static int mlx5e_rep_change_carrier(struct net_device *dev, bool new_carrier)
777 {
778 struct mlx5e_priv *priv = netdev_priv(dev);
779 struct mlx5e_rep_priv *rpriv = priv->ppriv;
780 struct mlx5_eswitch_rep *rep = rpriv->rep;
781 int err;
782
783 if (new_carrier) {
784 err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
785 rep->vport, 1, MLX5_VPORT_ADMIN_STATE_UP);
786 if (err)
787 return err;
788 netif_carrier_on(dev);
789 } else {
790 err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
791 rep->vport, 1, MLX5_VPORT_ADMIN_STATE_DOWN);
792 if (err)
793 return err;
794 netif_carrier_off(dev);
795 }
796 return 0;
797 }
798
799 static const struct net_device_ops mlx5e_netdev_ops_rep = {
800 .ndo_open = mlx5e_rep_open,
801 .ndo_stop = mlx5e_rep_close,
802 .ndo_start_xmit = mlx5e_xmit,
803 .ndo_setup_tc = mlx5e_rep_setup_tc,
804 .ndo_get_stats64 = mlx5e_rep_get_stats,
805 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
806 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
807 .ndo_change_mtu = mlx5e_rep_change_mtu,
808 .ndo_change_carrier = mlx5e_rep_change_carrier,
809 };
810
mlx5e_eswitch_uplink_rep(const struct net_device * netdev)811 bool mlx5e_eswitch_uplink_rep(const struct net_device *netdev)
812 {
813 return netdev->netdev_ops == &mlx5e_netdev_ops &&
814 mlx5e_is_uplink_rep(netdev_priv(netdev));
815 }
816
mlx5e_eswitch_vf_rep(const struct net_device * netdev)817 bool mlx5e_eswitch_vf_rep(const struct net_device *netdev)
818 {
819 return netdev->netdev_ops == &mlx5e_netdev_ops_rep;
820 }
821
822 /* One indirect TIR set for outer. Inner not supported in reps. */
823 #define REP_NUM_INDIR_TIRS MLX5E_NUM_INDIR_TIRS
824
mlx5e_rep_max_nch_limit(struct mlx5_core_dev * mdev)825 static int mlx5e_rep_max_nch_limit(struct mlx5_core_dev *mdev)
826 {
827 int max_tir_num = 1 << MLX5_CAP_GEN(mdev, log_max_tir);
828 int num_vports = mlx5_eswitch_get_total_vports(mdev);
829
830 return (max_tir_num - mlx5e_get_pf_num_tirs(mdev)
831 - (num_vports * REP_NUM_INDIR_TIRS)) / num_vports;
832 }
833
mlx5e_build_rep_params(struct net_device * netdev)834 static void mlx5e_build_rep_params(struct net_device *netdev)
835 {
836 const bool take_rtnl = netdev->reg_state == NETREG_REGISTERED;
837 struct mlx5e_priv *priv = netdev_priv(netdev);
838 struct mlx5e_rep_priv *rpriv = priv->ppriv;
839 struct mlx5_eswitch_rep *rep = rpriv->rep;
840 struct mlx5_core_dev *mdev = priv->mdev;
841 struct mlx5e_params *params;
842
843 params = &priv->channels.params;
844
845 params->num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;
846 params->hard_mtu = MLX5E_ETH_HARD_MTU;
847 params->sw_mtu = netdev->mtu;
848
849 /* SQ */
850 if (rep->vport == MLX5_VPORT_UPLINK)
851 params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
852 else
853 params->log_sq_size = MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE;
854
855 /* RQ */
856 mlx5e_build_rq_params(mdev, params);
857
858 /* If netdev is already registered (e.g. move from nic profile to uplink,
859 * RTNL lock must be held before triggering netdev notifiers.
860 */
861 if (take_rtnl)
862 rtnl_lock();
863 /* update XDP supported features */
864 mlx5e_set_xdp_feature(netdev);
865 if (take_rtnl)
866 rtnl_unlock();
867
868 /* CQ moderation params */
869 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
870 params->rx_moder_use_cqe_mode = !!MLX5_CAP_GEN(mdev, cq_period_start_from_cqe);
871
872 params->mqprio.num_tc = 1;
873 if (rep->vport != MLX5_VPORT_UPLINK)
874 params->vlan_strip_disable = true;
875
876 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
877 }
878
mlx5e_build_rep_netdev(struct net_device * netdev,struct mlx5_core_dev * mdev)879 static void mlx5e_build_rep_netdev(struct net_device *netdev,
880 struct mlx5_core_dev *mdev)
881 {
882 SET_NETDEV_DEV(netdev, mdev->device);
883 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
884 eth_hw_addr_random(netdev);
885 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
886
887 netdev->watchdog_timeo = 15 * HZ;
888
889 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
890 netdev->hw_features |= NETIF_F_HW_TC;
891 #endif
892 netdev->hw_features |= NETIF_F_SG;
893 netdev->hw_features |= NETIF_F_IP_CSUM;
894 netdev->hw_features |= NETIF_F_IPV6_CSUM;
895 netdev->hw_features |= NETIF_F_GRO;
896 netdev->hw_features |= NETIF_F_TSO;
897 netdev->hw_features |= NETIF_F_TSO6;
898 netdev->hw_features |= NETIF_F_RXCSUM;
899
900 netdev->features |= netdev->hw_features;
901
902 netdev->netns_local = true;
903 }
904
mlx5e_init_rep(struct mlx5_core_dev * mdev,struct net_device * netdev)905 static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
906 struct net_device *netdev)
907 {
908 struct mlx5e_priv *priv = netdev_priv(netdev);
909
910 priv->fs =
911 mlx5e_fs_init(priv->profile, mdev,
912 !test_bit(MLX5E_STATE_DESTROYING, &priv->state),
913 priv->dfs_root);
914 if (!priv->fs) {
915 netdev_err(priv->netdev, "FS allocation failed\n");
916 return -ENOMEM;
917 }
918
919 mlx5e_build_rep_params(netdev);
920 mlx5e_timestamp_init(priv);
921
922 return 0;
923 }
924
mlx5e_init_ul_rep(struct mlx5_core_dev * mdev,struct net_device * netdev)925 static int mlx5e_init_ul_rep(struct mlx5_core_dev *mdev,
926 struct net_device *netdev)
927 {
928 struct mlx5e_priv *priv = netdev_priv(netdev);
929
930 priv->dfs_root = debugfs_create_dir("nic",
931 mlx5_debugfs_get_dev_root(mdev));
932
933 priv->fs = mlx5e_fs_init(priv->profile, mdev,
934 !test_bit(MLX5E_STATE_DESTROYING, &priv->state),
935 priv->dfs_root);
936 if (!priv->fs) {
937 netdev_err(priv->netdev, "FS allocation failed\n");
938 debugfs_remove_recursive(priv->dfs_root);
939 return -ENOMEM;
940 }
941
942 mlx5e_vxlan_set_netdev_info(priv);
943 mlx5e_build_rep_params(netdev);
944 mlx5e_timestamp_init(priv);
945 return 0;
946 }
947
mlx5e_cleanup_rep(struct mlx5e_priv * priv)948 static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
949 {
950 mlx5e_fs_cleanup(priv->fs);
951 debugfs_remove_recursive(priv->dfs_root);
952 priv->fs = NULL;
953 }
954
mlx5e_create_rep_ttc_table(struct mlx5e_priv * priv)955 static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
956 {
957 struct mlx5e_rep_priv *rpriv = priv->ppriv;
958 struct mlx5_eswitch_rep *rep = rpriv->rep;
959 struct ttc_params ttc_params = {};
960 int err;
961
962 mlx5e_fs_set_ns(priv->fs,
963 mlx5_get_flow_namespace(priv->mdev,
964 MLX5_FLOW_NAMESPACE_KERNEL), false);
965
966 /* The inner_ttc in the ttc params is intentionally not set */
967 mlx5e_set_ttc_params(priv->fs, priv->rx_res, &ttc_params, false);
968
969 if (rep->vport != MLX5_VPORT_UPLINK)
970 /* To give uplik rep TTC a lower level for chaining from root ft */
971 ttc_params.ft_attr.level = MLX5E_TTC_FT_LEVEL + 1;
972
973 mlx5e_fs_set_ttc(priv->fs, mlx5_create_ttc_table(priv->mdev, &ttc_params), false);
974 if (IS_ERR(mlx5e_fs_get_ttc(priv->fs, false))) {
975 err = PTR_ERR(mlx5e_fs_get_ttc(priv->fs, false));
976 netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n",
977 err);
978 return err;
979 }
980 return 0;
981 }
982
mlx5e_create_rep_root_ft(struct mlx5e_priv * priv)983 static int mlx5e_create_rep_root_ft(struct mlx5e_priv *priv)
984 {
985 struct mlx5e_rep_priv *rpriv = priv->ppriv;
986 struct mlx5_eswitch_rep *rep = rpriv->rep;
987 struct mlx5_flow_table_attr ft_attr = {};
988 struct mlx5_flow_namespace *ns;
989 int err = 0;
990
991 if (rep->vport != MLX5_VPORT_UPLINK) {
992 /* non uplik reps will skip any bypass tables and go directly to
993 * their own ttc
994 */
995 rpriv->root_ft = mlx5_get_ttc_flow_table(mlx5e_fs_get_ttc(priv->fs, false));
996 return 0;
997 }
998
999 /* uplink root ft will be used to auto chain, to ethtool or ttc tables */
1000 ns = mlx5_get_flow_namespace(priv->mdev, MLX5_FLOW_NAMESPACE_OFFLOADS);
1001 if (!ns) {
1002 netdev_err(priv->netdev, "Failed to get reps offloads namespace\n");
1003 return -EOPNOTSUPP;
1004 }
1005
1006 ft_attr.max_fte = 0; /* Empty table, miss rule will always point to next table */
1007 ft_attr.prio = 1;
1008 ft_attr.level = 1;
1009
1010 rpriv->root_ft = mlx5_create_flow_table(ns, &ft_attr);
1011 if (IS_ERR(rpriv->root_ft)) {
1012 err = PTR_ERR(rpriv->root_ft);
1013 rpriv->root_ft = NULL;
1014 }
1015
1016 return err;
1017 }
1018
mlx5e_destroy_rep_root_ft(struct mlx5e_priv * priv)1019 static void mlx5e_destroy_rep_root_ft(struct mlx5e_priv *priv)
1020 {
1021 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1022 struct mlx5_eswitch_rep *rep = rpriv->rep;
1023
1024 if (rep->vport != MLX5_VPORT_UPLINK)
1025 return;
1026 mlx5_destroy_flow_table(rpriv->root_ft);
1027 }
1028
mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv * priv)1029 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
1030 {
1031 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1032 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1033 struct mlx5_eswitch_rep *rep = rpriv->rep;
1034 struct mlx5_flow_handle *flow_rule;
1035 struct mlx5_flow_destination dest;
1036
1037 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1038 dest.ft = rpriv->root_ft;
1039
1040 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw, rep->vport, &dest);
1041 if (IS_ERR(flow_rule))
1042 return PTR_ERR(flow_rule);
1043 rpriv->vport_rx_rule = flow_rule;
1044 return 0;
1045 }
1046
rep_vport_rx_rule_destroy(struct mlx5e_priv * priv)1047 static void rep_vport_rx_rule_destroy(struct mlx5e_priv *priv)
1048 {
1049 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1050
1051 if (!rpriv->vport_rx_rule)
1052 return;
1053
1054 mlx5_del_flow_rules(rpriv->vport_rx_rule);
1055 rpriv->vport_rx_rule = NULL;
1056 }
1057
mlx5e_rep_bond_update(struct mlx5e_priv * priv,bool cleanup)1058 int mlx5e_rep_bond_update(struct mlx5e_priv *priv, bool cleanup)
1059 {
1060 rep_vport_rx_rule_destroy(priv);
1061
1062 return cleanup ? 0 : mlx5e_create_rep_vport_rx_rule(priv);
1063 }
1064
mlx5e_init_rep_rx(struct mlx5e_priv * priv)1065 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
1066 {
1067 struct mlx5_core_dev *mdev = priv->mdev;
1068 int err;
1069
1070 mlx5e_fs_init_l2_addr(priv->fs, priv->netdev);
1071
1072 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
1073 if (err) {
1074 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
1075 goto err_free_fs;
1076 }
1077
1078 priv->rx_res = mlx5e_rx_res_create(priv->mdev, 0, priv->max_nch, priv->drop_rq.rqn,
1079 &priv->channels.params.packet_merge,
1080 priv->channels.params.num_channels);
1081 if (IS_ERR(priv->rx_res)) {
1082 err = PTR_ERR(priv->rx_res);
1083 mlx5_core_err(mdev, "Create rx resources failed, err=%d\n", err);
1084 goto err_close_drop_rq;
1085 }
1086
1087 err = mlx5e_create_rep_ttc_table(priv);
1088 if (err)
1089 goto err_destroy_rx_res;
1090
1091 err = mlx5e_create_rep_root_ft(priv);
1092 if (err)
1093 goto err_destroy_ttc_table;
1094
1095 err = mlx5e_create_rep_vport_rx_rule(priv);
1096 if (err)
1097 goto err_destroy_root_ft;
1098
1099 mlx5e_ethtool_init_steering(priv->fs);
1100
1101 return 0;
1102
1103 err_destroy_root_ft:
1104 mlx5e_destroy_rep_root_ft(priv);
1105 err_destroy_ttc_table:
1106 mlx5_destroy_ttc_table(mlx5e_fs_get_ttc(priv->fs, false));
1107 err_destroy_rx_res:
1108 mlx5e_rx_res_destroy(priv->rx_res);
1109 priv->rx_res = ERR_PTR(-EINVAL);
1110 err_close_drop_rq:
1111 mlx5e_close_drop_rq(&priv->drop_rq);
1112 err_free_fs:
1113 mlx5e_fs_cleanup(priv->fs);
1114 priv->fs = NULL;
1115 return err;
1116 }
1117
mlx5e_cleanup_rep_rx(struct mlx5e_priv * priv)1118 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
1119 {
1120 mlx5e_ethtool_cleanup_steering(priv->fs);
1121 rep_vport_rx_rule_destroy(priv);
1122 mlx5e_destroy_rep_root_ft(priv);
1123 mlx5_destroy_ttc_table(mlx5e_fs_get_ttc(priv->fs, false));
1124 mlx5e_rx_res_destroy(priv->rx_res);
1125 priv->rx_res = ERR_PTR(-EINVAL);
1126 mlx5e_close_drop_rq(&priv->drop_rq);
1127 }
1128
mlx5e_rep_mpesw_work(struct work_struct * work)1129 static void mlx5e_rep_mpesw_work(struct work_struct *work)
1130 {
1131 struct mlx5_rep_uplink_priv *uplink_priv =
1132 container_of(work, struct mlx5_rep_uplink_priv,
1133 mpesw_work);
1134 struct mlx5e_rep_priv *rpriv =
1135 container_of(uplink_priv, struct mlx5e_rep_priv,
1136 uplink_priv);
1137 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1138
1139 rep_vport_rx_rule_destroy(priv);
1140 mlx5e_create_rep_vport_rx_rule(priv);
1141 }
1142
mlx5e_init_ul_rep_rx(struct mlx5e_priv * priv)1143 static int mlx5e_init_ul_rep_rx(struct mlx5e_priv *priv)
1144 {
1145 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1146 int err;
1147
1148 mlx5e_create_q_counters(priv);
1149 err = mlx5e_init_rep_rx(priv);
1150 if (err)
1151 goto out;
1152
1153 mlx5e_tc_int_port_init_rep_rx(priv);
1154
1155 INIT_WORK(&rpriv->uplink_priv.mpesw_work, mlx5e_rep_mpesw_work);
1156
1157 out:
1158 return err;
1159 }
1160
mlx5e_cleanup_ul_rep_rx(struct mlx5e_priv * priv)1161 static void mlx5e_cleanup_ul_rep_rx(struct mlx5e_priv *priv)
1162 {
1163 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1164
1165 cancel_work_sync(&rpriv->uplink_priv.mpesw_work);
1166 mlx5e_tc_int_port_cleanup_rep_rx(priv);
1167 mlx5e_cleanup_rep_rx(priv);
1168 mlx5e_destroy_q_counters(priv);
1169 }
1170
mlx5e_init_uplink_rep_tx(struct mlx5e_rep_priv * rpriv)1171 static int mlx5e_init_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
1172 {
1173 struct mlx5_rep_uplink_priv *uplink_priv;
1174 struct net_device *netdev;
1175 struct mlx5e_priv *priv;
1176 int err;
1177
1178 netdev = rpriv->netdev;
1179 priv = netdev_priv(netdev);
1180 uplink_priv = &rpriv->uplink_priv;
1181
1182 err = mlx5e_rep_tc_init(rpriv);
1183 if (err)
1184 return err;
1185
1186 mlx5_init_port_tun_entropy(&uplink_priv->tun_entropy, priv->mdev);
1187
1188 mlx5e_rep_bond_init(rpriv);
1189 err = mlx5e_rep_tc_netdevice_event_register(rpriv);
1190 if (err) {
1191 mlx5_core_err(priv->mdev, "Failed to register netdev notifier, err: %d\n",
1192 err);
1193 goto err_event_reg;
1194 }
1195
1196 return 0;
1197
1198 err_event_reg:
1199 mlx5e_rep_bond_cleanup(rpriv);
1200 mlx5e_rep_tc_cleanup(rpriv);
1201 return err;
1202 }
1203
mlx5e_cleanup_uplink_rep_tx(struct mlx5e_rep_priv * rpriv)1204 static void mlx5e_cleanup_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
1205 {
1206 mlx5e_rep_tc_netdevice_event_unregister(rpriv);
1207 mlx5e_rep_bond_cleanup(rpriv);
1208 mlx5e_rep_tc_cleanup(rpriv);
1209 }
1210
mlx5e_init_rep_tx(struct mlx5e_priv * priv)1211 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1212 {
1213 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1214 int err;
1215
1216 err = mlx5e_rep_neigh_init(rpriv);
1217 if (err)
1218 goto err_neigh_init;
1219
1220 if (rpriv->rep->vport == MLX5_VPORT_UPLINK) {
1221 err = mlx5e_init_uplink_rep_tx(rpriv);
1222 if (err)
1223 goto err_init_tx;
1224 }
1225
1226 err = mlx5e_tc_ht_init(&rpriv->tc_ht);
1227 if (err)
1228 goto err_ht_init;
1229
1230 return 0;
1231
1232 err_ht_init:
1233 if (rpriv->rep->vport == MLX5_VPORT_UPLINK)
1234 mlx5e_cleanup_uplink_rep_tx(rpriv);
1235 err_init_tx:
1236 mlx5e_rep_neigh_cleanup(rpriv);
1237 err_neigh_init:
1238 return err;
1239 }
1240
mlx5e_cleanup_rep_tx(struct mlx5e_priv * priv)1241 static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
1242 {
1243 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1244
1245 mlx5e_tc_ht_cleanup(&rpriv->tc_ht);
1246
1247 if (rpriv->rep->vport == MLX5_VPORT_UPLINK)
1248 mlx5e_cleanup_uplink_rep_tx(rpriv);
1249
1250 mlx5e_rep_neigh_cleanup(rpriv);
1251 }
1252
mlx5e_rep_enable(struct mlx5e_priv * priv)1253 static void mlx5e_rep_enable(struct mlx5e_priv *priv)
1254 {
1255 mlx5e_set_netdev_mtu_boundaries(priv);
1256 }
1257
mlx5e_rep_disable(struct mlx5e_priv * priv)1258 static void mlx5e_rep_disable(struct mlx5e_priv *priv)
1259 {
1260 }
1261
mlx5e_update_rep_rx(struct mlx5e_priv * priv)1262 static int mlx5e_update_rep_rx(struct mlx5e_priv *priv)
1263 {
1264 return 0;
1265 }
1266
mlx5e_rep_stats_update_ndo_stats(struct mlx5e_priv * priv)1267 static void mlx5e_rep_stats_update_ndo_stats(struct mlx5e_priv *priv)
1268 {
1269 mlx5e_stats_update_ndo_stats(priv);
1270 mlx5e_rep_update_vport_q_counter(priv);
1271 }
1272
mlx5e_rep_event_mpesw(struct mlx5e_priv * priv)1273 static int mlx5e_rep_event_mpesw(struct mlx5e_priv *priv)
1274 {
1275 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1276 struct mlx5_eswitch_rep *rep = rpriv->rep;
1277
1278 if (rep->vport != MLX5_VPORT_UPLINK)
1279 return NOTIFY_DONE;
1280
1281 queue_work(priv->wq, &rpriv->uplink_priv.mpesw_work);
1282
1283 return NOTIFY_OK;
1284 }
1285
uplink_rep_async_event(struct notifier_block * nb,unsigned long event,void * data)1286 static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
1287 {
1288 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
1289
1290 if (event == MLX5_EVENT_TYPE_PORT_CHANGE) {
1291 struct mlx5_eqe *eqe = data;
1292
1293 switch (eqe->sub_type) {
1294 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
1295 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
1296 queue_work(priv->wq, &priv->update_carrier_work);
1297 break;
1298 default:
1299 return NOTIFY_DONE;
1300 }
1301
1302 return NOTIFY_OK;
1303 }
1304
1305 if (event == MLX5_DEV_EVENT_PORT_AFFINITY)
1306 return mlx5e_rep_tc_event_port_affinity(priv);
1307 else if (event == MLX5_DEV_EVENT_MULTIPORT_ESW)
1308 return mlx5e_rep_event_mpesw(priv);
1309
1310 return NOTIFY_DONE;
1311 }
1312
mlx5e_uplink_rep_enable(struct mlx5e_priv * priv)1313 static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
1314 {
1315 struct net_device *netdev = priv->netdev;
1316 struct mlx5_core_dev *mdev = priv->mdev;
1317 u16 max_mtu;
1318
1319 mlx5e_ipsec_init(priv);
1320
1321 netdev->min_mtu = ETH_MIN_MTU;
1322 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
1323 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1324 mlx5e_set_dev_port_mtu(priv);
1325
1326 mlx5e_rep_tc_enable(priv);
1327
1328 if (MLX5_CAP_GEN(mdev, uplink_follow))
1329 mlx5_modify_vport_admin_state(mdev, MLX5_VPORT_STATE_OP_MOD_UPLINK,
1330 0, 0, MLX5_VPORT_ADMIN_STATE_AUTO);
1331 mlx5_lag_add_netdev(mdev, netdev);
1332 priv->events_nb.notifier_call = uplink_rep_async_event;
1333 mlx5_notifier_register(mdev, &priv->events_nb);
1334 mlx5e_dcbnl_initialize(priv);
1335 mlx5e_dcbnl_init_app(priv);
1336 mlx5e_rep_bridge_init(priv);
1337
1338 netdev->wanted_features |= NETIF_F_HW_TC;
1339
1340 rtnl_lock();
1341 if (netif_running(netdev))
1342 mlx5e_open(netdev);
1343 udp_tunnel_nic_reset_ntf(priv->netdev);
1344 netif_device_attach(netdev);
1345 rtnl_unlock();
1346 }
1347
mlx5e_uplink_rep_disable(struct mlx5e_priv * priv)1348 static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
1349 {
1350 struct mlx5_core_dev *mdev = priv->mdev;
1351
1352 rtnl_lock();
1353 if (netif_running(priv->netdev))
1354 mlx5e_close(priv->netdev);
1355 netif_device_detach(priv->netdev);
1356 rtnl_unlock();
1357
1358 mlx5e_rep_bridge_cleanup(priv);
1359 mlx5e_dcbnl_delete_app(priv);
1360 mlx5_notifier_unregister(mdev, &priv->events_nb);
1361 mlx5e_rep_tc_disable(priv);
1362 mlx5_lag_remove_netdev(mdev, priv->netdev);
1363 mlx5_vxlan_reset_to_default(mdev->vxlan);
1364
1365 mlx5e_ipsec_cleanup(priv);
1366 }
1367
1368 static MLX5E_DEFINE_STATS_GRP(sw_rep, 0);
1369 static MLX5E_DEFINE_STATS_GRP(vport_rep, MLX5E_NDO_UPDATE_STATS);
1370
1371 /* The stats groups order is opposite to the update_stats() order calls */
1372 static mlx5e_stats_grp_t mlx5e_rep_stats_grps[] = {
1373 &MLX5E_STATS_GRP(sw_rep),
1374 &MLX5E_STATS_GRP(vport_rep),
1375 };
1376
mlx5e_rep_stats_grps_num(struct mlx5e_priv * priv)1377 static unsigned int mlx5e_rep_stats_grps_num(struct mlx5e_priv *priv)
1378 {
1379 return ARRAY_SIZE(mlx5e_rep_stats_grps);
1380 }
1381
1382 /* The stats groups order is opposite to the update_stats() order calls */
1383 static mlx5e_stats_grp_t mlx5e_ul_rep_stats_grps[] = {
1384 &MLX5E_STATS_GRP(sw),
1385 &MLX5E_STATS_GRP(qcnt),
1386 &MLX5E_STATS_GRP(vnic_env),
1387 &MLX5E_STATS_GRP(vport),
1388 &MLX5E_STATS_GRP(802_3),
1389 &MLX5E_STATS_GRP(2863),
1390 &MLX5E_STATS_GRP(2819),
1391 &MLX5E_STATS_GRP(phy),
1392 &MLX5E_STATS_GRP(eth_ext),
1393 &MLX5E_STATS_GRP(pcie),
1394 &MLX5E_STATS_GRP(per_prio),
1395 &MLX5E_STATS_GRP(pme),
1396 &MLX5E_STATS_GRP(channels),
1397 &MLX5E_STATS_GRP(per_port_buff_congest),
1398 #ifdef CONFIG_MLX5_EN_IPSEC
1399 &MLX5E_STATS_GRP(ipsec_hw),
1400 &MLX5E_STATS_GRP(ipsec_sw),
1401 #endif
1402 &MLX5E_STATS_GRP(ptp),
1403 };
1404
mlx5e_ul_rep_stats_grps_num(struct mlx5e_priv * priv)1405 static unsigned int mlx5e_ul_rep_stats_grps_num(struct mlx5e_priv *priv)
1406 {
1407 return ARRAY_SIZE(mlx5e_ul_rep_stats_grps);
1408 }
1409
1410 static int
mlx5e_rep_vnic_reporter_diagnose(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,struct netlink_ext_ack * extack)1411 mlx5e_rep_vnic_reporter_diagnose(struct devlink_health_reporter *reporter,
1412 struct devlink_fmsg *fmsg,
1413 struct netlink_ext_ack *extack)
1414 {
1415 struct mlx5e_rep_priv *rpriv = devlink_health_reporter_priv(reporter);
1416 struct mlx5_eswitch_rep *rep = rpriv->rep;
1417
1418 mlx5_reporter_vnic_diagnose_counters(rep->esw->dev, fmsg, rep->vport,
1419 true);
1420 return 0;
1421 }
1422
1423 static const struct devlink_health_reporter_ops mlx5_rep_vnic_reporter_ops = {
1424 .name = "vnic",
1425 .diagnose = mlx5e_rep_vnic_reporter_diagnose,
1426 };
1427
mlx5e_rep_vnic_reporter_create(struct mlx5e_priv * priv,struct devlink_port * dl_port)1428 static void mlx5e_rep_vnic_reporter_create(struct mlx5e_priv *priv,
1429 struct devlink_port *dl_port)
1430 {
1431 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1432 struct devlink_health_reporter *reporter;
1433
1434 reporter = devl_port_health_reporter_create(dl_port,
1435 &mlx5_rep_vnic_reporter_ops,
1436 0, rpriv);
1437 if (IS_ERR(reporter)) {
1438 mlx5_core_err(priv->mdev,
1439 "Failed to create representor vnic reporter, err = %ld\n",
1440 PTR_ERR(reporter));
1441 return;
1442 }
1443
1444 rpriv->rep_vnic_reporter = reporter;
1445 }
1446
mlx5e_rep_vnic_reporter_destroy(struct mlx5e_priv * priv)1447 static void mlx5e_rep_vnic_reporter_destroy(struct mlx5e_priv *priv)
1448 {
1449 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1450
1451 if (!IS_ERR_OR_NULL(rpriv->rep_vnic_reporter))
1452 devl_health_reporter_destroy(rpriv->rep_vnic_reporter);
1453 }
1454
1455 static const struct mlx5e_profile mlx5e_rep_profile = {
1456 .init = mlx5e_init_rep,
1457 .cleanup = mlx5e_cleanup_rep,
1458 .init_rx = mlx5e_init_rep_rx,
1459 .cleanup_rx = mlx5e_cleanup_rep_rx,
1460 .init_tx = mlx5e_init_rep_tx,
1461 .cleanup_tx = mlx5e_cleanup_rep_tx,
1462 .enable = mlx5e_rep_enable,
1463 .disable = mlx5e_rep_disable,
1464 .update_rx = mlx5e_update_rep_rx,
1465 .update_stats = mlx5e_rep_stats_update_ndo_stats,
1466 .rx_handlers = &mlx5e_rx_handlers_rep,
1467 .max_tc = 1,
1468 .stats_grps = mlx5e_rep_stats_grps,
1469 .stats_grps_num = mlx5e_rep_stats_grps_num,
1470 .max_nch_limit = mlx5e_rep_max_nch_limit,
1471 };
1472
1473 static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
1474 .init = mlx5e_init_ul_rep,
1475 .cleanup = mlx5e_cleanup_rep,
1476 .init_rx = mlx5e_init_ul_rep_rx,
1477 .cleanup_rx = mlx5e_cleanup_ul_rep_rx,
1478 .init_tx = mlx5e_init_rep_tx,
1479 .cleanup_tx = mlx5e_cleanup_rep_tx,
1480 .enable = mlx5e_uplink_rep_enable,
1481 .disable = mlx5e_uplink_rep_disable,
1482 .update_rx = mlx5e_update_rep_rx,
1483 .update_stats = mlx5e_stats_update_ndo_stats,
1484 .update_carrier = mlx5e_update_carrier,
1485 .rx_handlers = &mlx5e_rx_handlers_rep,
1486 .max_tc = MLX5_MAX_NUM_TC,
1487 .stats_grps = mlx5e_ul_rep_stats_grps,
1488 .stats_grps_num = mlx5e_ul_rep_stats_grps_num,
1489 };
1490
1491 /* e-Switch vport representors */
1492 static int
mlx5e_vport_uplink_rep_load(struct mlx5_core_dev * dev,struct mlx5_eswitch_rep * rep)1493 mlx5e_vport_uplink_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1494 {
1495 struct mlx5e_priv *priv = netdev_priv(mlx5_uplink_netdev_get(dev));
1496 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1497
1498 rpriv->netdev = priv->netdev;
1499 return mlx5e_netdev_change_profile(priv, &mlx5e_uplink_rep_profile,
1500 rpriv);
1501 }
1502
1503 static void
mlx5e_vport_uplink_rep_unload(struct mlx5e_rep_priv * rpriv)1504 mlx5e_vport_uplink_rep_unload(struct mlx5e_rep_priv *rpriv)
1505 {
1506 struct net_device *netdev = rpriv->netdev;
1507 struct mlx5e_priv *priv;
1508
1509 priv = netdev_priv(netdev);
1510
1511 mlx5e_netdev_attach_nic_profile(priv);
1512 }
1513
1514 static int
mlx5e_vport_vf_rep_load(struct mlx5_core_dev * dev,struct mlx5_eswitch_rep * rep)1515 mlx5e_vport_vf_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1516 {
1517 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1518 const struct mlx5e_profile *profile;
1519 struct devlink_port *dl_port;
1520 struct net_device *netdev;
1521 struct mlx5e_priv *priv;
1522 int err;
1523
1524 profile = &mlx5e_rep_profile;
1525 netdev = mlx5e_create_netdev(dev, profile);
1526 if (!netdev) {
1527 mlx5_core_warn(dev,
1528 "Failed to create representor netdev for vport %d\n",
1529 rep->vport);
1530 return -EINVAL;
1531 }
1532
1533 mlx5e_build_rep_netdev(netdev, dev);
1534 rpriv->netdev = netdev;
1535
1536 priv = netdev_priv(netdev);
1537 priv->profile = profile;
1538 priv->ppriv = rpriv;
1539 err = profile->init(dev, netdev);
1540 if (err) {
1541 netdev_warn(netdev, "rep profile init failed, %d\n", err);
1542 goto err_destroy_netdev;
1543 }
1544
1545 err = mlx5e_attach_netdev(netdev_priv(netdev));
1546 if (err) {
1547 netdev_warn(netdev,
1548 "Failed to attach representor netdev for vport %d\n",
1549 rep->vport);
1550 goto err_cleanup_profile;
1551 }
1552
1553 dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch,
1554 rpriv->rep->vport);
1555 if (!IS_ERR(dl_port)) {
1556 SET_NETDEV_DEVLINK_PORT(netdev, dl_port);
1557 mlx5e_rep_vnic_reporter_create(priv, dl_port);
1558 }
1559
1560 err = register_netdev(netdev);
1561 if (err) {
1562 netdev_warn(netdev,
1563 "Failed to register representor netdev for vport %d\n",
1564 rep->vport);
1565 goto err_detach_netdev;
1566 }
1567
1568 return 0;
1569
1570 err_detach_netdev:
1571 mlx5e_rep_vnic_reporter_destroy(priv);
1572 mlx5e_detach_netdev(netdev_priv(netdev));
1573 err_cleanup_profile:
1574 priv->profile->cleanup(priv);
1575
1576 err_destroy_netdev:
1577 mlx5e_destroy_netdev(netdev_priv(netdev));
1578 return err;
1579 }
1580
1581 static int
mlx5e_vport_rep_load(struct mlx5_core_dev * dev,struct mlx5_eswitch_rep * rep)1582 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1583 {
1584 struct mlx5e_rep_priv *rpriv;
1585 int err;
1586
1587 rpriv = kvzalloc(sizeof(*rpriv), GFP_KERNEL);
1588 if (!rpriv)
1589 return -ENOMEM;
1590
1591 /* rpriv->rep to be looked up when profile->init() is called */
1592 rpriv->rep = rep;
1593 rep->rep_data[REP_ETH].priv = rpriv;
1594 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1595
1596 if (rep->vport == MLX5_VPORT_UPLINK)
1597 err = mlx5e_vport_uplink_rep_load(dev, rep);
1598 else
1599 err = mlx5e_vport_vf_rep_load(dev, rep);
1600
1601 if (err)
1602 kvfree(rpriv);
1603
1604 return err;
1605 }
1606
1607 static void
mlx5e_vport_rep_unload(struct mlx5_eswitch_rep * rep)1608 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1609 {
1610 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1611 struct net_device *netdev = rpriv->netdev;
1612 struct mlx5e_priv *priv = netdev_priv(netdev);
1613 void *ppriv = priv->ppriv;
1614
1615 if (rep->vport == MLX5_VPORT_UPLINK) {
1616 mlx5e_vport_uplink_rep_unload(rpriv);
1617 goto free_ppriv;
1618 }
1619
1620 unregister_netdev(netdev);
1621 mlx5e_rep_vnic_reporter_destroy(priv);
1622 mlx5e_detach_netdev(priv);
1623 priv->profile->cleanup(priv);
1624 mlx5e_destroy_netdev(priv);
1625 free_ppriv:
1626 kvfree(ppriv); /* mlx5e_rep_priv */
1627 }
1628
mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep * rep)1629 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1630 {
1631 struct mlx5e_rep_priv *rpriv;
1632
1633 rpriv = mlx5e_rep_to_rep_priv(rep);
1634
1635 return rpriv->netdev;
1636 }
1637
mlx5e_vport_rep_event_unpair(struct mlx5_eswitch_rep * rep,struct mlx5_eswitch * peer_esw)1638 static void mlx5e_vport_rep_event_unpair(struct mlx5_eswitch_rep *rep,
1639 struct mlx5_eswitch *peer_esw)
1640 {
1641 u16 i = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
1642 struct mlx5e_rep_priv *rpriv;
1643 struct mlx5e_rep_sq *rep_sq;
1644
1645 WARN_ON_ONCE(!peer_esw);
1646 rpriv = mlx5e_rep_to_rep_priv(rep);
1647 list_for_each_entry(rep_sq, &rpriv->vport_sqs_list, list) {
1648 struct mlx5e_rep_sq_peer *sq_peer = xa_load(&rep_sq->sq_peer, i);
1649
1650 if (!sq_peer || sq_peer->peer != peer_esw)
1651 continue;
1652
1653 mlx5_eswitch_del_send_to_vport_rule(sq_peer->rule);
1654 xa_erase(&rep_sq->sq_peer, i);
1655 kfree(sq_peer);
1656 }
1657 }
1658
mlx5e_vport_rep_event_pair(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep,struct mlx5_eswitch * peer_esw)1659 static int mlx5e_vport_rep_event_pair(struct mlx5_eswitch *esw,
1660 struct mlx5_eswitch_rep *rep,
1661 struct mlx5_eswitch *peer_esw)
1662 {
1663 u16 i = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
1664 struct mlx5_flow_handle *flow_rule;
1665 struct mlx5e_rep_sq_peer *sq_peer;
1666 struct mlx5e_rep_priv *rpriv;
1667 struct mlx5e_rep_sq *rep_sq;
1668 int err;
1669
1670 rpriv = mlx5e_rep_to_rep_priv(rep);
1671 list_for_each_entry(rep_sq, &rpriv->vport_sqs_list, list) {
1672 sq_peer = xa_load(&rep_sq->sq_peer, i);
1673
1674 if (sq_peer && sq_peer->peer)
1675 continue;
1676
1677 flow_rule = mlx5_eswitch_add_send_to_vport_rule(peer_esw, esw, rep,
1678 rep_sq->sqn);
1679 if (IS_ERR(flow_rule)) {
1680 err = PTR_ERR(flow_rule);
1681 goto err_out;
1682 }
1683
1684 if (sq_peer) {
1685 sq_peer->rule = flow_rule;
1686 sq_peer->peer = peer_esw;
1687 continue;
1688 }
1689 sq_peer = kzalloc(sizeof(*sq_peer), GFP_KERNEL);
1690 if (!sq_peer) {
1691 err = -ENOMEM;
1692 goto err_sq_alloc;
1693 }
1694 err = xa_insert(&rep_sq->sq_peer, i, sq_peer, GFP_KERNEL);
1695 if (err)
1696 goto err_xa;
1697 sq_peer->rule = flow_rule;
1698 sq_peer->peer = peer_esw;
1699 }
1700
1701 return 0;
1702 err_xa:
1703 kfree(sq_peer);
1704 err_sq_alloc:
1705 mlx5_eswitch_del_send_to_vport_rule(flow_rule);
1706 err_out:
1707 mlx5e_vport_rep_event_unpair(rep, peer_esw);
1708 return err;
1709 }
1710
mlx5e_vport_rep_event(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep,enum mlx5_switchdev_event event,void * data)1711 static int mlx5e_vport_rep_event(struct mlx5_eswitch *esw,
1712 struct mlx5_eswitch_rep *rep,
1713 enum mlx5_switchdev_event event,
1714 void *data)
1715 {
1716 int err = 0;
1717
1718 if (event == MLX5_SWITCHDEV_EVENT_PAIR)
1719 err = mlx5e_vport_rep_event_pair(esw, rep, data);
1720 else if (event == MLX5_SWITCHDEV_EVENT_UNPAIR)
1721 mlx5e_vport_rep_event_unpair(rep, data);
1722
1723 return err;
1724 }
1725
1726 static const struct mlx5_eswitch_rep_ops rep_ops = {
1727 .load = mlx5e_vport_rep_load,
1728 .unload = mlx5e_vport_rep_unload,
1729 .get_proto_dev = mlx5e_vport_rep_get_proto_dev,
1730 .event = mlx5e_vport_rep_event,
1731 };
1732
mlx5e_rep_probe(struct auxiliary_device * adev,const struct auxiliary_device_id * id)1733 static int mlx5e_rep_probe(struct auxiliary_device *adev,
1734 const struct auxiliary_device_id *id)
1735 {
1736 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
1737 struct mlx5_core_dev *mdev = edev->mdev;
1738 struct mlx5_eswitch *esw;
1739
1740 esw = mdev->priv.eswitch;
1741 mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_ETH);
1742 return 0;
1743 }
1744
mlx5e_rep_remove(struct auxiliary_device * adev)1745 static void mlx5e_rep_remove(struct auxiliary_device *adev)
1746 {
1747 struct mlx5_adev *vdev = container_of(adev, struct mlx5_adev, adev);
1748 struct mlx5_core_dev *mdev = vdev->mdev;
1749 struct mlx5_eswitch *esw;
1750
1751 esw = mdev->priv.eswitch;
1752 mlx5_eswitch_unregister_vport_reps(esw, REP_ETH);
1753 }
1754
1755 static const struct auxiliary_device_id mlx5e_rep_id_table[] = {
1756 { .name = MLX5_ADEV_NAME ".eth-rep", },
1757 {},
1758 };
1759
1760 MODULE_DEVICE_TABLE(auxiliary, mlx5e_rep_id_table);
1761
1762 static struct auxiliary_driver mlx5e_rep_driver = {
1763 .name = "eth-rep",
1764 .probe = mlx5e_rep_probe,
1765 .remove = mlx5e_rep_remove,
1766 .id_table = mlx5e_rep_id_table,
1767 };
1768
mlx5e_rep_init(void)1769 int mlx5e_rep_init(void)
1770 {
1771 return auxiliary_driver_register(&mlx5e_rep_driver);
1772 }
1773
mlx5e_rep_cleanup(void)1774 void mlx5e_rep_cleanup(void)
1775 {
1776 auxiliary_driver_unregister(&mlx5e_rep_driver);
1777 }
1778