2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/mlx5/fs.h>
34 #include <net/switchdev.h>
35 #include <net/pkt_cls.h>
36 #include <net/act_api.h>
37 #include <net/devlink.h>
38 #include <net/ipv6_stubs.h>
43 #include "en/params.h"
46 #include "en/rep/tc.h"
47 #include "en/rep/neigh.h"
48 #include "en/devlink.h"
51 #define CREATE_TRACE_POINTS
52 #include "diag/en_rep_tracepoint.h"
54 #define MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE \
55 max(0x7, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)
56 #define MLX5E_REP_PARAMS_DEF_NUM_CHANNELS 1
58 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
60 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
61 struct ethtool_drvinfo *drvinfo)
63 struct mlx5e_priv *priv = netdev_priv(dev);
64 struct mlx5_core_dev *mdev = priv->mdev;
66 strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
67 sizeof(drvinfo->driver));
68 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
70 fw_rev_maj(mdev), fw_rev_min(mdev),
71 fw_rev_sub(mdev), mdev->board_id);
74 static const struct counter_desc sw_rep_stats_desc[] = {
75 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
76 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
77 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
78 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
88 static const struct counter_desc vport_rep_stats_desc[] = {
89 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_packets) },
90 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_bytes) },
91 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_packets) },
92 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_bytes) },
95 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
96 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
98 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(sw_rep)
100 return NUM_VPORT_REP_SW_COUNTERS;
103 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(sw_rep)
107 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
108 strcpy(data + (idx++) * ETH_GSTRING_LEN,
109 sw_rep_stats_desc[i].format);
113 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(sw_rep)
117 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
118 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
119 sw_rep_stats_desc, i);
123 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(sw_rep)
125 struct mlx5e_sw_stats *s = &priv->stats.sw;
126 struct rtnl_link_stats64 stats64 = {};
128 memset(s, 0, sizeof(*s));
129 mlx5e_fold_sw_stats64(priv, &stats64);
131 s->rx_packets = stats64.rx_packets;
132 s->rx_bytes = stats64.rx_bytes;
133 s->tx_packets = stats64.tx_packets;
134 s->tx_bytes = stats64.tx_bytes;
135 s->tx_queue_dropped = stats64.tx_dropped;
138 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(vport_rep)
140 return NUM_VPORT_REP_HW_COUNTERS;
143 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(vport_rep)
147 for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
148 strcpy(data + (idx++) * ETH_GSTRING_LEN, vport_rep_stats_desc[i].format);
152 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(vport_rep)
156 for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
157 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
158 vport_rep_stats_desc, i);
162 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(vport_rep)
164 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
165 struct mlx5e_rep_priv *rpriv = priv->ppriv;
166 struct mlx5_eswitch_rep *rep = rpriv->rep;
167 struct rtnl_link_stats64 *vport_stats;
168 struct ifla_vf_stats vf_stats;
171 err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
173 netdev_warn(priv->netdev, "vport %d error %d reading stats\n",
178 vport_stats = &priv->stats.vf_vport;
179 /* flip tx/rx as we are reporting the counters for the switch vport */
180 vport_stats->rx_packets = vf_stats.tx_packets;
181 vport_stats->rx_bytes = vf_stats.tx_bytes;
182 vport_stats->tx_packets = vf_stats.rx_packets;
183 vport_stats->tx_bytes = vf_stats.rx_bytes;
186 static void mlx5e_rep_get_strings(struct net_device *dev,
187 u32 stringset, uint8_t *data)
189 struct mlx5e_priv *priv = netdev_priv(dev);
193 mlx5e_stats_fill_strings(priv, data);
198 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
199 struct ethtool_stats *stats, u64 *data)
201 struct mlx5e_priv *priv = netdev_priv(dev);
203 mlx5e_ethtool_get_ethtool_stats(priv, stats, data);
206 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
208 struct mlx5e_priv *priv = netdev_priv(dev);
212 return mlx5e_stats_total_num(priv);
218 static void mlx5e_rep_get_ringparam(struct net_device *dev,
219 struct ethtool_ringparam *param)
221 struct mlx5e_priv *priv = netdev_priv(dev);
223 mlx5e_ethtool_get_ringparam(priv, param);
226 static int mlx5e_rep_set_ringparam(struct net_device *dev,
227 struct ethtool_ringparam *param)
229 struct mlx5e_priv *priv = netdev_priv(dev);
231 return mlx5e_ethtool_set_ringparam(priv, param);
234 static void mlx5e_rep_get_channels(struct net_device *dev,
235 struct ethtool_channels *ch)
237 struct mlx5e_priv *priv = netdev_priv(dev);
239 mlx5e_ethtool_get_channels(priv, ch);
242 static int mlx5e_rep_set_channels(struct net_device *dev,
243 struct ethtool_channels *ch)
245 struct mlx5e_priv *priv = netdev_priv(dev);
247 return mlx5e_ethtool_set_channels(priv, ch);
250 static int mlx5e_rep_get_coalesce(struct net_device *netdev,
251 struct ethtool_coalesce *coal)
253 struct mlx5e_priv *priv = netdev_priv(netdev);
255 return mlx5e_ethtool_get_coalesce(priv, coal);
258 static int mlx5e_rep_set_coalesce(struct net_device *netdev,
259 struct ethtool_coalesce *coal)
261 struct mlx5e_priv *priv = netdev_priv(netdev);
263 return mlx5e_ethtool_set_coalesce(priv, coal);
266 static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
268 struct mlx5e_priv *priv = netdev_priv(netdev);
270 return mlx5e_ethtool_get_rxfh_key_size(priv);
273 static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
275 struct mlx5e_priv *priv = netdev_priv(netdev);
277 return mlx5e_ethtool_get_rxfh_indir_size(priv);
280 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
281 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
282 ETHTOOL_COALESCE_MAX_FRAMES |
283 ETHTOOL_COALESCE_USE_ADAPTIVE,
284 .get_drvinfo = mlx5e_rep_get_drvinfo,
285 .get_link = ethtool_op_get_link,
286 .get_strings = mlx5e_rep_get_strings,
287 .get_sset_count = mlx5e_rep_get_sset_count,
288 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
289 .get_ringparam = mlx5e_rep_get_ringparam,
290 .set_ringparam = mlx5e_rep_set_ringparam,
291 .get_channels = mlx5e_rep_get_channels,
292 .set_channels = mlx5e_rep_set_channels,
293 .get_coalesce = mlx5e_rep_get_coalesce,
294 .set_coalesce = mlx5e_rep_set_coalesce,
295 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
296 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
299 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
300 struct mlx5_eswitch_rep *rep)
302 struct mlx5e_rep_sq *rep_sq, *tmp;
303 struct mlx5e_rep_priv *rpriv;
305 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
308 rpriv = mlx5e_rep_to_rep_priv(rep);
309 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
310 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
311 list_del(&rep_sq->list);
316 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
317 struct mlx5_eswitch_rep *rep,
318 u32 *sqns_array, int sqns_num)
320 struct mlx5_flow_handle *flow_rule;
321 struct mlx5e_rep_priv *rpriv;
322 struct mlx5e_rep_sq *rep_sq;
326 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
329 rpriv = mlx5e_rep_to_rep_priv(rep);
330 for (i = 0; i < sqns_num; i++) {
331 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
337 /* Add re-inject rule to the PF/representor sqs */
338 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
341 if (IS_ERR(flow_rule)) {
342 err = PTR_ERR(flow_rule);
346 rep_sq->send_to_vport_rule = flow_rule;
347 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
352 mlx5e_sqs2vport_stop(esw, rep);
356 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
358 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
359 struct mlx5e_rep_priv *rpriv = priv->ppriv;
360 struct mlx5_eswitch_rep *rep = rpriv->rep;
361 struct mlx5e_channel *c;
362 int n, tc, num_sqs = 0;
366 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(*sqs), GFP_KERNEL);
370 for (n = 0; n < priv->channels.num; n++) {
371 c = priv->channels.c[n];
372 for (tc = 0; tc < c->num_tc; tc++)
373 sqs[num_sqs++] = c->sq[tc].sqn;
376 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
381 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
385 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
387 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
388 struct mlx5e_rep_priv *rpriv = priv->ppriv;
389 struct mlx5_eswitch_rep *rep = rpriv->rep;
391 mlx5e_sqs2vport_stop(esw, rep);
394 static int mlx5e_rep_open(struct net_device *dev)
396 struct mlx5e_priv *priv = netdev_priv(dev);
397 struct mlx5e_rep_priv *rpriv = priv->ppriv;
398 struct mlx5_eswitch_rep *rep = rpriv->rep;
401 mutex_lock(&priv->state_lock);
402 err = mlx5e_open_locked(dev);
406 if (!mlx5_modify_vport_admin_state(priv->mdev,
407 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
409 MLX5_VPORT_ADMIN_STATE_UP))
410 netif_carrier_on(dev);
413 mutex_unlock(&priv->state_lock);
417 static int mlx5e_rep_close(struct net_device *dev)
419 struct mlx5e_priv *priv = netdev_priv(dev);
420 struct mlx5e_rep_priv *rpriv = priv->ppriv;
421 struct mlx5_eswitch_rep *rep = rpriv->rep;
424 mutex_lock(&priv->state_lock);
425 mlx5_modify_vport_admin_state(priv->mdev,
426 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
428 MLX5_VPORT_ADMIN_STATE_DOWN);
429 ret = mlx5e_close_locked(dev);
430 mutex_unlock(&priv->state_lock);
434 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
436 struct mlx5e_rep_priv *rpriv = priv->ppriv;
437 struct mlx5_eswitch_rep *rep;
439 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
442 if (!rpriv) /* non vport rep mlx5e instances don't use this field */
446 return (rep->vport == MLX5_VPORT_UPLINK);
449 bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
452 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
460 mlx5e_get_sw_stats64(const struct net_device *dev,
461 struct rtnl_link_stats64 *stats)
463 struct mlx5e_priv *priv = netdev_priv(dev);
465 mlx5e_fold_sw_stats64(priv, stats);
469 int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
473 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
474 return mlx5e_get_sw_stats64(dev, sp);
481 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
483 struct mlx5e_priv *priv = netdev_priv(dev);
485 /* update HW stats in background for next time */
486 mlx5e_queue_update_stats(priv);
487 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
490 static int mlx5e_rep_change_mtu(struct net_device *netdev, int new_mtu)
492 return mlx5e_change_mtu(netdev, new_mtu, NULL);
495 static struct devlink_port *mlx5e_rep_get_devlink_port(struct net_device *netdev)
497 struct mlx5e_priv *priv = netdev_priv(netdev);
498 struct mlx5e_rep_priv *rpriv = priv->ppriv;
499 struct mlx5_core_dev *dev = priv->mdev;
501 return mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
504 static int mlx5e_rep_change_carrier(struct net_device *dev, bool new_carrier)
506 struct mlx5e_priv *priv = netdev_priv(dev);
507 struct mlx5e_rep_priv *rpriv = priv->ppriv;
508 struct mlx5_eswitch_rep *rep = rpriv->rep;
512 err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
513 rep->vport, 1, MLX5_VPORT_ADMIN_STATE_UP);
516 netif_carrier_on(dev);
518 err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
519 rep->vport, 1, MLX5_VPORT_ADMIN_STATE_DOWN);
522 netif_carrier_off(dev);
527 static const struct net_device_ops mlx5e_netdev_ops_rep = {
528 .ndo_open = mlx5e_rep_open,
529 .ndo_stop = mlx5e_rep_close,
530 .ndo_start_xmit = mlx5e_xmit,
531 .ndo_setup_tc = mlx5e_rep_setup_tc,
532 .ndo_get_devlink_port = mlx5e_rep_get_devlink_port,
533 .ndo_get_stats64 = mlx5e_rep_get_stats,
534 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
535 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
536 .ndo_change_mtu = mlx5e_rep_change_mtu,
537 .ndo_change_carrier = mlx5e_rep_change_carrier,
540 bool mlx5e_eswitch_uplink_rep(struct net_device *netdev)
542 return netdev->netdev_ops == &mlx5e_netdev_ops &&
543 mlx5e_is_uplink_rep(netdev_priv(netdev));
546 bool mlx5e_eswitch_vf_rep(struct net_device *netdev)
548 return netdev->netdev_ops == &mlx5e_netdev_ops_rep;
551 static void mlx5e_build_rep_params(struct net_device *netdev)
553 struct mlx5e_priv *priv = netdev_priv(netdev);
554 struct mlx5e_rep_priv *rpriv = priv->ppriv;
555 struct mlx5_eswitch_rep *rep = rpriv->rep;
556 struct mlx5_core_dev *mdev = priv->mdev;
557 struct mlx5e_params *params;
559 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
560 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
561 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
563 priv->max_nch = mlx5e_calc_max_nch(priv, priv->profile);
564 params = &priv->channels.params;
566 params->num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;
567 params->hard_mtu = MLX5E_ETH_HARD_MTU;
568 params->sw_mtu = netdev->mtu;
571 if (rep->vport == MLX5_VPORT_UPLINK)
572 params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
574 params->log_sq_size = MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE;
577 mlx5e_build_rq_params(mdev, params);
579 /* CQ moderation params */
580 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
581 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
584 params->tunneled_offload_en = false;
586 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
589 mlx5e_build_rss_params(&priv->rss_params, params->num_channels);
592 static void mlx5e_build_rep_netdev(struct net_device *netdev,
593 struct mlx5_core_dev *mdev)
595 SET_NETDEV_DEV(netdev, mdev->device);
596 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
597 eth_hw_addr_random(netdev);
598 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
600 netdev->watchdog_timeo = 15 * HZ;
602 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
603 netdev->hw_features |= NETIF_F_HW_TC;
605 netdev->hw_features |= NETIF_F_SG;
606 netdev->hw_features |= NETIF_F_IP_CSUM;
607 netdev->hw_features |= NETIF_F_IPV6_CSUM;
608 netdev->hw_features |= NETIF_F_GRO;
609 netdev->hw_features |= NETIF_F_TSO;
610 netdev->hw_features |= NETIF_F_TSO6;
611 netdev->hw_features |= NETIF_F_RXCSUM;
613 netdev->features |= netdev->hw_features;
614 netdev->features |= NETIF_F_VLAN_CHALLENGED;
615 netdev->features |= NETIF_F_NETNS_LOCAL;
618 static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
619 struct net_device *netdev)
621 struct mlx5e_priv *priv = netdev_priv(netdev);
623 mlx5e_build_rep_params(netdev);
624 mlx5e_timestamp_init(priv);
629 static int mlx5e_init_ul_rep(struct mlx5_core_dev *mdev,
630 struct net_device *netdev)
632 struct mlx5e_priv *priv = netdev_priv(netdev);
634 mlx5e_vxlan_set_netdev_info(priv);
635 return mlx5e_init_rep(mdev, netdev);
638 static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
642 static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
644 struct mlx5e_rep_priv *rpriv = priv->ppriv;
645 struct mlx5_eswitch_rep *rep = rpriv->rep;
646 struct ttc_params ttc_params = {};
649 priv->fs.ns = mlx5_get_flow_namespace(priv->mdev,
650 MLX5_FLOW_NAMESPACE_KERNEL);
652 /* The inner_ttc in the ttc params is intentionally not set */
653 ttc_params.any_tt_tirn = priv->direct_tir[0].tirn;
654 mlx5e_set_ttc_ft_params(&ttc_params);
656 if (rep->vport != MLX5_VPORT_UPLINK)
657 /* To give uplik rep TTC a lower level for chaining from root ft */
658 ttc_params.ft_attr.level = MLX5E_TTC_FT_LEVEL + 1;
660 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
661 ttc_params.indir_tirn[tt] = priv->indir_tir[tt].tirn;
663 err = mlx5e_create_ttc_table(priv, &ttc_params, &priv->fs.ttc);
665 netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n", err);
671 static int mlx5e_create_rep_root_ft(struct mlx5e_priv *priv)
673 struct mlx5e_rep_priv *rpriv = priv->ppriv;
674 struct mlx5_eswitch_rep *rep = rpriv->rep;
675 struct mlx5_flow_table_attr ft_attr = {};
676 struct mlx5_flow_namespace *ns;
679 if (rep->vport != MLX5_VPORT_UPLINK) {
680 /* non uplik reps will skip any bypass tables and go directly to
683 rpriv->root_ft = priv->fs.ttc.ft.t;
687 /* uplink root ft will be used to auto chain, to ethtool or ttc tables */
688 ns = mlx5_get_flow_namespace(priv->mdev, MLX5_FLOW_NAMESPACE_OFFLOADS);
690 netdev_err(priv->netdev, "Failed to get reps offloads namespace\n");
694 ft_attr.max_fte = 0; /* Empty table, miss rule will always point to next table */
698 rpriv->root_ft = mlx5_create_flow_table(ns, &ft_attr);
699 if (IS_ERR(rpriv->root_ft)) {
700 err = PTR_ERR(rpriv->root_ft);
701 rpriv->root_ft = NULL;
707 static void mlx5e_destroy_rep_root_ft(struct mlx5e_priv *priv)
709 struct mlx5e_rep_priv *rpriv = priv->ppriv;
710 struct mlx5_eswitch_rep *rep = rpriv->rep;
712 if (rep->vport != MLX5_VPORT_UPLINK)
714 mlx5_destroy_flow_table(rpriv->root_ft);
717 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
719 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
720 struct mlx5e_rep_priv *rpriv = priv->ppriv;
721 struct mlx5_eswitch_rep *rep = rpriv->rep;
722 struct mlx5_flow_handle *flow_rule;
723 struct mlx5_flow_destination dest;
725 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
726 dest.ft = rpriv->root_ft;
728 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw, rep->vport, &dest);
729 if (IS_ERR(flow_rule))
730 return PTR_ERR(flow_rule);
731 rpriv->vport_rx_rule = flow_rule;
735 static void rep_vport_rx_rule_destroy(struct mlx5e_priv *priv)
737 struct mlx5e_rep_priv *rpriv = priv->ppriv;
739 if (!rpriv->vport_rx_rule)
742 mlx5_del_flow_rules(rpriv->vport_rx_rule);
743 rpriv->vport_rx_rule = NULL;
746 int mlx5e_rep_bond_update(struct mlx5e_priv *priv, bool cleanup)
748 rep_vport_rx_rule_destroy(priv);
750 return cleanup ? 0 : mlx5e_create_rep_vport_rx_rule(priv);
753 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
755 struct mlx5_core_dev *mdev = priv->mdev;
758 mlx5e_init_l2_addr(priv);
760 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
762 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
766 err = mlx5e_create_indirect_rqt(priv);
768 goto err_close_drop_rq;
770 err = mlx5e_create_direct_rqts(priv, priv->direct_tir);
772 goto err_destroy_indirect_rqts;
774 err = mlx5e_create_indirect_tirs(priv, false);
776 goto err_destroy_direct_rqts;
778 err = mlx5e_create_direct_tirs(priv, priv->direct_tir);
780 goto err_destroy_indirect_tirs;
782 err = mlx5e_create_rep_ttc_table(priv);
784 goto err_destroy_direct_tirs;
786 err = mlx5e_create_rep_root_ft(priv);
788 goto err_destroy_ttc_table;
790 err = mlx5e_create_rep_vport_rx_rule(priv);
792 goto err_destroy_root_ft;
794 mlx5e_ethtool_init_steering(priv);
799 mlx5e_destroy_rep_root_ft(priv);
800 err_destroy_ttc_table:
801 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
802 err_destroy_direct_tirs:
803 mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
804 err_destroy_indirect_tirs:
805 mlx5e_destroy_indirect_tirs(priv);
806 err_destroy_direct_rqts:
807 mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
808 err_destroy_indirect_rqts:
809 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
811 mlx5e_close_drop_rq(&priv->drop_rq);
815 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
817 mlx5e_ethtool_cleanup_steering(priv);
818 rep_vport_rx_rule_destroy(priv);
819 mlx5e_destroy_rep_root_ft(priv);
820 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
821 mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
822 mlx5e_destroy_indirect_tirs(priv);
823 mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
824 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
825 mlx5e_close_drop_rq(&priv->drop_rq);
828 static int mlx5e_init_ul_rep_rx(struct mlx5e_priv *priv)
830 mlx5e_create_q_counters(priv);
831 return mlx5e_init_rep_rx(priv);
834 static void mlx5e_cleanup_ul_rep_rx(struct mlx5e_priv *priv)
836 mlx5e_cleanup_rep_rx(priv);
837 mlx5e_destroy_q_counters(priv);
840 static int mlx5e_init_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
842 struct mlx5_rep_uplink_priv *uplink_priv;
843 struct net_device *netdev;
844 struct mlx5e_priv *priv;
847 netdev = rpriv->netdev;
848 priv = netdev_priv(netdev);
849 uplink_priv = &rpriv->uplink_priv;
851 err = mlx5e_rep_tc_init(rpriv);
855 mlx5_init_port_tun_entropy(&uplink_priv->tun_entropy, priv->mdev);
857 mlx5e_rep_bond_init(rpriv);
858 err = mlx5e_rep_tc_netdevice_event_register(rpriv);
860 mlx5_core_err(priv->mdev, "Failed to register netdev notifier, err: %d\n",
868 mlx5e_rep_bond_cleanup(rpriv);
869 mlx5e_rep_tc_cleanup(rpriv);
873 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
875 struct mlx5e_rep_priv *rpriv = priv->ppriv;
878 err = mlx5e_create_tises(priv);
880 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
884 if (rpriv->rep->vport == MLX5_VPORT_UPLINK) {
885 err = mlx5e_init_uplink_rep_tx(rpriv);
893 mlx5e_destroy_tises(priv);
897 static void mlx5e_cleanup_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
899 mlx5e_rep_tc_netdevice_event_unregister(rpriv);
900 mlx5e_rep_bond_cleanup(rpriv);
901 mlx5e_rep_tc_cleanup(rpriv);
904 static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
906 struct mlx5e_rep_priv *rpriv = priv->ppriv;
908 mlx5e_destroy_tises(priv);
910 if (rpriv->rep->vport == MLX5_VPORT_UPLINK)
911 mlx5e_cleanup_uplink_rep_tx(rpriv);
914 static void mlx5e_rep_enable(struct mlx5e_priv *priv)
916 struct mlx5e_rep_priv *rpriv = priv->ppriv;
918 mlx5e_set_netdev_mtu_boundaries(priv);
919 mlx5e_rep_neigh_init(rpriv);
922 static void mlx5e_rep_disable(struct mlx5e_priv *priv)
924 struct mlx5e_rep_priv *rpriv = priv->ppriv;
926 mlx5e_rep_neigh_cleanup(rpriv);
929 static int mlx5e_update_rep_rx(struct mlx5e_priv *priv)
934 static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
936 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
938 if (event == MLX5_EVENT_TYPE_PORT_CHANGE) {
939 struct mlx5_eqe *eqe = data;
941 switch (eqe->sub_type) {
942 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
943 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
944 queue_work(priv->wq, &priv->update_carrier_work);
953 if (event == MLX5_DEV_EVENT_PORT_AFFINITY)
954 return mlx5e_rep_tc_event_port_affinity(priv);
959 static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
961 struct mlx5e_rep_priv *rpriv = priv->ppriv;
962 struct net_device *netdev = priv->netdev;
963 struct mlx5_core_dev *mdev = priv->mdev;
966 netdev->min_mtu = ETH_MIN_MTU;
967 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
968 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
969 mlx5e_set_dev_port_mtu(priv);
971 mlx5e_rep_tc_enable(priv);
973 mlx5_modify_vport_admin_state(mdev, MLX5_VPORT_STATE_OP_MOD_UPLINK,
974 0, 0, MLX5_VPORT_ADMIN_STATE_AUTO);
975 mlx5_lag_add(mdev, netdev);
976 priv->events_nb.notifier_call = uplink_rep_async_event;
977 mlx5_notifier_register(mdev, &priv->events_nb);
978 mlx5e_dcbnl_initialize(priv);
979 mlx5e_dcbnl_init_app(priv);
980 mlx5e_rep_neigh_init(rpriv);
982 netdev->wanted_features |= NETIF_F_HW_TC;
985 if (netif_running(netdev))
987 netif_device_attach(netdev);
991 static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
993 struct mlx5e_rep_priv *rpriv = priv->ppriv;
994 struct mlx5_core_dev *mdev = priv->mdev;
997 if (netif_running(priv->netdev))
998 mlx5e_close(priv->netdev);
999 netif_device_detach(priv->netdev);
1002 mlx5e_rep_neigh_cleanup(rpriv);
1003 mlx5e_dcbnl_delete_app(priv);
1004 mlx5_notifier_unregister(mdev, &priv->events_nb);
1005 mlx5e_rep_tc_disable(priv);
1006 mlx5_lag_remove(mdev);
1009 static MLX5E_DEFINE_STATS_GRP(sw_rep, 0);
1010 static MLX5E_DEFINE_STATS_GRP(vport_rep, MLX5E_NDO_UPDATE_STATS);
1012 /* The stats groups order is opposite to the update_stats() order calls */
1013 static mlx5e_stats_grp_t mlx5e_rep_stats_grps[] = {
1014 &MLX5E_STATS_GRP(sw_rep),
1015 &MLX5E_STATS_GRP(vport_rep),
1018 static unsigned int mlx5e_rep_stats_grps_num(struct mlx5e_priv *priv)
1020 return ARRAY_SIZE(mlx5e_rep_stats_grps);
1023 /* The stats groups order is opposite to the update_stats() order calls */
1024 static mlx5e_stats_grp_t mlx5e_ul_rep_stats_grps[] = {
1025 &MLX5E_STATS_GRP(sw),
1026 &MLX5E_STATS_GRP(qcnt),
1027 &MLX5E_STATS_GRP(vnic_env),
1028 &MLX5E_STATS_GRP(vport),
1029 &MLX5E_STATS_GRP(802_3),
1030 &MLX5E_STATS_GRP(2863),
1031 &MLX5E_STATS_GRP(2819),
1032 &MLX5E_STATS_GRP(phy),
1033 &MLX5E_STATS_GRP(eth_ext),
1034 &MLX5E_STATS_GRP(pcie),
1035 &MLX5E_STATS_GRP(per_prio),
1036 &MLX5E_STATS_GRP(pme),
1037 &MLX5E_STATS_GRP(channels),
1038 &MLX5E_STATS_GRP(per_port_buff_congest),
1041 static unsigned int mlx5e_ul_rep_stats_grps_num(struct mlx5e_priv *priv)
1043 return ARRAY_SIZE(mlx5e_ul_rep_stats_grps);
1046 static const struct mlx5e_profile mlx5e_rep_profile = {
1047 .init = mlx5e_init_rep,
1048 .cleanup = mlx5e_cleanup_rep,
1049 .init_rx = mlx5e_init_rep_rx,
1050 .cleanup_rx = mlx5e_cleanup_rep_rx,
1051 .init_tx = mlx5e_init_rep_tx,
1052 .cleanup_tx = mlx5e_cleanup_rep_tx,
1053 .enable = mlx5e_rep_enable,
1054 .disable = mlx5e_rep_disable,
1055 .update_rx = mlx5e_update_rep_rx,
1056 .update_stats = mlx5e_stats_update_ndo_stats,
1057 .rx_handlers = &mlx5e_rx_handlers_rep,
1059 .rq_groups = MLX5E_NUM_RQ_GROUPS(REGULAR),
1060 .stats_grps = mlx5e_rep_stats_grps,
1061 .stats_grps_num = mlx5e_rep_stats_grps_num,
1064 static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
1065 .init = mlx5e_init_ul_rep,
1066 .cleanup = mlx5e_cleanup_rep,
1067 .init_rx = mlx5e_init_ul_rep_rx,
1068 .cleanup_rx = mlx5e_cleanup_ul_rep_rx,
1069 .init_tx = mlx5e_init_rep_tx,
1070 .cleanup_tx = mlx5e_cleanup_rep_tx,
1071 .enable = mlx5e_uplink_rep_enable,
1072 .disable = mlx5e_uplink_rep_disable,
1073 .update_rx = mlx5e_update_rep_rx,
1074 .update_stats = mlx5e_stats_update_ndo_stats,
1075 .update_carrier = mlx5e_update_carrier,
1076 .rx_handlers = &mlx5e_rx_handlers_rep,
1077 .max_tc = MLX5E_MAX_NUM_TC,
1078 /* XSK is needed so we can replace profile with NIC netdev */
1079 .rq_groups = MLX5E_NUM_RQ_GROUPS(XSK),
1080 .stats_grps = mlx5e_ul_rep_stats_grps,
1081 .stats_grps_num = mlx5e_ul_rep_stats_grps_num,
1084 /* e-Switch vport representors */
1086 mlx5e_vport_uplink_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1088 struct mlx5e_priv *priv = netdev_priv(mlx5_uplink_netdev_get(dev));
1089 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1090 struct devlink_port *dl_port;
1093 rpriv->netdev = priv->netdev;
1095 err = mlx5e_netdev_change_profile(priv, &mlx5e_uplink_rep_profile,
1100 dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
1102 devlink_port_type_eth_set(dl_port, rpriv->netdev);
1108 mlx5e_vport_uplink_rep_unload(struct mlx5e_rep_priv *rpriv)
1110 struct net_device *netdev = rpriv->netdev;
1111 struct devlink_port *dl_port;
1112 struct mlx5_core_dev *dev;
1113 struct mlx5e_priv *priv;
1115 priv = netdev_priv(netdev);
1118 dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
1120 devlink_port_type_clear(dl_port);
1121 mlx5e_netdev_attach_nic_profile(priv);
1125 mlx5e_vport_vf_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1127 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1128 const struct mlx5e_profile *profile;
1129 struct devlink_port *dl_port;
1130 struct net_device *netdev;
1131 struct mlx5e_priv *priv;
1132 unsigned int txqs, rxqs;
1135 profile = &mlx5e_rep_profile;
1136 nch = mlx5e_get_max_num_channels(dev);
1137 txqs = nch * profile->max_tc;
1138 rxqs = nch * profile->rq_groups;
1139 netdev = mlx5e_create_netdev(dev, txqs, rxqs);
1142 "Failed to create representor netdev for vport %d\n",
1147 mlx5e_build_rep_netdev(netdev, dev);
1148 rpriv->netdev = netdev;
1150 priv = netdev_priv(netdev);
1151 priv->profile = profile;
1152 priv->ppriv = rpriv;
1153 err = profile->init(dev, netdev);
1155 netdev_warn(netdev, "rep profile init failed, %d\n", err);
1156 goto err_destroy_netdev;
1159 err = mlx5e_attach_netdev(netdev_priv(netdev));
1162 "Failed to attach representor netdev for vport %d\n",
1164 goto err_cleanup_profile;
1167 err = register_netdev(netdev);
1170 "Failed to register representor netdev for vport %d\n",
1172 goto err_detach_netdev;
1175 dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
1177 devlink_port_type_eth_set(dl_port, netdev);
1181 mlx5e_detach_netdev(netdev_priv(netdev));
1183 err_cleanup_profile:
1184 priv->profile->cleanup(priv);
1187 mlx5e_destroy_netdev(netdev_priv(netdev));
1192 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1194 struct mlx5e_rep_priv *rpriv;
1197 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1201 /* rpriv->rep to be looked up when profile->init() is called */
1203 rep->rep_data[REP_ETH].priv = rpriv;
1204 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1206 if (rep->vport == MLX5_VPORT_UPLINK)
1207 err = mlx5e_vport_uplink_rep_load(dev, rep);
1209 err = mlx5e_vport_vf_rep_load(dev, rep);
1218 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1220 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1221 struct net_device *netdev = rpriv->netdev;
1222 struct mlx5e_priv *priv = netdev_priv(netdev);
1223 struct mlx5_core_dev *dev = priv->mdev;
1224 struct devlink_port *dl_port;
1225 void *ppriv = priv->ppriv;
1227 if (rep->vport == MLX5_VPORT_UPLINK) {
1228 mlx5e_vport_uplink_rep_unload(rpriv);
1232 dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
1234 devlink_port_type_clear(dl_port);
1235 unregister_netdev(netdev);
1236 mlx5e_detach_netdev(priv);
1237 priv->profile->cleanup(priv);
1238 mlx5e_destroy_netdev(priv);
1240 kfree(ppriv); /* mlx5e_rep_priv */
1243 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1245 struct mlx5e_rep_priv *rpriv;
1247 rpriv = mlx5e_rep_to_rep_priv(rep);
1249 return rpriv->netdev;
1252 static const struct mlx5_eswitch_rep_ops rep_ops = {
1253 .load = mlx5e_vport_rep_load,
1254 .unload = mlx5e_vport_rep_unload,
1255 .get_proto_dev = mlx5e_vport_rep_get_proto_dev
1258 static int mlx5e_rep_probe(struct auxiliary_device *adev,
1259 const struct auxiliary_device_id *id)
1261 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
1262 struct mlx5_core_dev *mdev = edev->mdev;
1263 struct mlx5_eswitch *esw;
1265 esw = mdev->priv.eswitch;
1266 mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_ETH);
1270 static void mlx5e_rep_remove(struct auxiliary_device *adev)
1272 struct mlx5_adev *vdev = container_of(adev, struct mlx5_adev, adev);
1273 struct mlx5_core_dev *mdev = vdev->mdev;
1274 struct mlx5_eswitch *esw;
1276 esw = mdev->priv.eswitch;
1277 mlx5_eswitch_unregister_vport_reps(esw, REP_ETH);
1280 static const struct auxiliary_device_id mlx5e_rep_id_table[] = {
1281 { .name = MLX5_ADEV_NAME ".eth-rep", },
1285 MODULE_DEVICE_TABLE(auxiliary, mlx5e_rep_id_table);
1287 static struct auxiliary_driver mlx5e_rep_driver = {
1289 .probe = mlx5e_rep_probe,
1290 .remove = mlx5e_rep_remove,
1291 .id_table = mlx5e_rep_id_table,
1294 int mlx5e_rep_init(void)
1296 return auxiliary_driver_register(&mlx5e_rep_driver);
1299 void mlx5e_rep_cleanup(void)
1301 auxiliary_driver_unregister(&mlx5e_rep_driver);