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 <generated/utsrelease.h>
34 #include <linux/mlx5/fs.h>
35 #include <net/switchdev.h>
36 #include <net/pkt_cls.h>
42 static const char mlx5e_rep_driver_name
[] = "mlx5e_rep";
44 static void mlx5e_rep_get_drvinfo(struct net_device
*dev
,
45 struct ethtool_drvinfo
*drvinfo
)
47 strlcpy(drvinfo
->driver
, mlx5e_rep_driver_name
,
48 sizeof(drvinfo
->driver
));
49 strlcpy(drvinfo
->version
, UTS_RELEASE
, sizeof(drvinfo
->version
));
52 static const struct counter_desc sw_rep_stats_desc
[] = {
53 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats
, rx_packets
) },
54 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats
, rx_bytes
) },
55 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats
, tx_packets
) },
56 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats
, tx_bytes
) },
59 #define NUM_VPORT_REP_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
61 static void mlx5e_rep_get_strings(struct net_device
*dev
,
62 u32 stringset
, uint8_t *data
)
68 for (i
= 0; i
< NUM_VPORT_REP_COUNTERS
; i
++)
69 strcpy(data
+ (i
* ETH_GSTRING_LEN
),
70 sw_rep_stats_desc
[i
].format
);
75 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv
*priv
)
77 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
78 struct mlx5_eswitch_rep
*rep
= priv
->ppriv
;
79 struct rtnl_link_stats64
*vport_stats
;
80 struct ifla_vf_stats vf_stats
;
83 err
= mlx5_eswitch_get_vport_stats(esw
, rep
->vport
, &vf_stats
);
85 pr_warn("vport %d error %d reading stats\n", rep
->vport
, err
);
89 vport_stats
= &priv
->stats
.vf_vport
;
90 /* flip tx/rx as we are reporting the counters for the switch vport */
91 vport_stats
->rx_packets
= vf_stats
.tx_packets
;
92 vport_stats
->rx_bytes
= vf_stats
.tx_bytes
;
93 vport_stats
->tx_packets
= vf_stats
.rx_packets
;
94 vport_stats
->tx_bytes
= vf_stats
.rx_bytes
;
97 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv
*priv
)
99 struct mlx5e_sw_stats
*s
= &priv
->stats
.sw
;
100 struct mlx5e_rq_stats
*rq_stats
;
101 struct mlx5e_sq_stats
*sq_stats
;
104 memset(s
, 0, sizeof(*s
));
105 for (i
= 0; i
< priv
->channels
.num
; i
++) {
106 struct mlx5e_channel
*c
= priv
->channels
.c
[i
];
108 rq_stats
= &c
->rq
.stats
;
110 s
->rx_packets
+= rq_stats
->packets
;
111 s
->rx_bytes
+= rq_stats
->bytes
;
113 for (j
= 0; j
< priv
->channels
.params
.num_tc
; j
++) {
114 sq_stats
= &c
->sq
[j
].stats
;
116 s
->tx_packets
+= sq_stats
->packets
;
117 s
->tx_bytes
+= sq_stats
->bytes
;
122 static void mlx5e_rep_update_stats(struct mlx5e_priv
*priv
)
124 mlx5e_rep_update_sw_counters(priv
);
125 mlx5e_rep_update_hw_counters(priv
);
128 static void mlx5e_rep_get_ethtool_stats(struct net_device
*dev
,
129 struct ethtool_stats
*stats
, u64
*data
)
131 struct mlx5e_priv
*priv
= netdev_priv(dev
);
137 mutex_lock(&priv
->state_lock
);
138 if (test_bit(MLX5E_STATE_OPENED
, &priv
->state
))
139 mlx5e_rep_update_sw_counters(priv
);
140 mutex_unlock(&priv
->state_lock
);
142 for (i
= 0; i
< NUM_VPORT_REP_COUNTERS
; i
++)
143 data
[i
] = MLX5E_READ_CTR64_CPU(&priv
->stats
.sw
,
144 sw_rep_stats_desc
, i
);
147 static int mlx5e_rep_get_sset_count(struct net_device
*dev
, int sset
)
151 return NUM_VPORT_REP_COUNTERS
;
157 static const struct ethtool_ops mlx5e_rep_ethtool_ops
= {
158 .get_drvinfo
= mlx5e_rep_get_drvinfo
,
159 .get_link
= ethtool_op_get_link
,
160 .get_strings
= mlx5e_rep_get_strings
,
161 .get_sset_count
= mlx5e_rep_get_sset_count
,
162 .get_ethtool_stats
= mlx5e_rep_get_ethtool_stats
,
165 int mlx5e_attr_get(struct net_device
*dev
, struct switchdev_attr
*attr
)
167 struct mlx5e_priv
*priv
= netdev_priv(dev
);
168 struct mlx5_eswitch_rep
*rep
= priv
->ppriv
;
169 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
171 if (esw
->mode
== SRIOV_NONE
)
175 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID
:
176 attr
->u
.ppid
.id_len
= ETH_ALEN
;
177 ether_addr_copy(attr
->u
.ppid
.id
, rep
->hw_id
);
186 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv
*priv
)
189 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
190 struct mlx5_eswitch_rep
*rep
= priv
->ppriv
;
191 struct mlx5e_channel
*c
;
192 int n
, tc
, err
, num_sqs
= 0;
195 sqs
= kcalloc(priv
->channels
.num
* priv
->channels
.params
.num_tc
, sizeof(u16
), GFP_KERNEL
);
199 for (n
= 0; n
< priv
->channels
.num
; n
++) {
200 c
= priv
->channels
.c
[n
];
201 for (tc
= 0; tc
< c
->num_tc
; tc
++)
202 sqs
[num_sqs
++] = c
->sq
[tc
].sqn
;
205 err
= mlx5_eswitch_sqs2vport_start(esw
, rep
, sqs
, num_sqs
);
211 int mlx5e_nic_rep_load(struct mlx5_eswitch
*esw
, struct mlx5_eswitch_rep
*rep
)
213 struct net_device
*netdev
= rep
->netdev
;
214 struct mlx5e_priv
*priv
= netdev_priv(netdev
);
216 if (test_bit(MLX5E_STATE_OPENED
, &priv
->state
))
217 return mlx5e_add_sqs_fwd_rules(priv
);
221 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv
*priv
)
223 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
224 struct mlx5_eswitch_rep
*rep
= priv
->ppriv
;
226 mlx5_eswitch_sqs2vport_stop(esw
, rep
);
229 void mlx5e_nic_rep_unload(struct mlx5_eswitch
*esw
,
230 struct mlx5_eswitch_rep
*rep
)
232 struct net_device
*netdev
= rep
->netdev
;
233 struct mlx5e_priv
*priv
= netdev_priv(netdev
);
235 if (test_bit(MLX5E_STATE_OPENED
, &priv
->state
))
236 mlx5e_remove_sqs_fwd_rules(priv
);
238 /* clean (and re-init) existing uplink offloaded TC rules */
239 mlx5e_tc_cleanup(priv
);
243 static int mlx5e_rep_open(struct net_device
*dev
)
245 struct mlx5e_priv
*priv
= netdev_priv(dev
);
246 struct mlx5_eswitch_rep
*rep
= priv
->ppriv
;
247 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
250 err
= mlx5e_open(dev
);
254 err
= mlx5_eswitch_set_vport_state(esw
, rep
->vport
, MLX5_ESW_VPORT_ADMIN_STATE_UP
);
256 netif_carrier_on(dev
);
261 static int mlx5e_rep_close(struct net_device
*dev
)
263 struct mlx5e_priv
*priv
= netdev_priv(dev
);
264 struct mlx5_eswitch_rep
*rep
= priv
->ppriv
;
265 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
267 (void)mlx5_eswitch_set_vport_state(esw
, rep
->vport
, MLX5_ESW_VPORT_ADMIN_STATE_DOWN
);
269 return mlx5e_close(dev
);
272 static int mlx5e_rep_get_phys_port_name(struct net_device
*dev
,
273 char *buf
, size_t len
)
275 struct mlx5e_priv
*priv
= netdev_priv(dev
);
276 struct mlx5_eswitch_rep
*rep
= priv
->ppriv
;
279 ret
= snprintf(buf
, len
, "%d", rep
->vport
- 1);
286 static int mlx5e_rep_ndo_setup_tc(struct net_device
*dev
, u32 handle
,
287 __be16 proto
, struct tc_to_netdev
*tc
)
289 struct mlx5e_priv
*priv
= netdev_priv(dev
);
291 if (TC_H_MAJ(handle
) != TC_H_MAJ(TC_H_INGRESS
))
294 if (tc
->egress_dev
) {
295 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
296 struct net_device
*uplink_dev
= mlx5_eswitch_get_uplink_netdev(esw
);
298 return uplink_dev
->netdev_ops
->ndo_setup_tc(uplink_dev
, handle
,
303 case TC_SETUP_CLSFLOWER
:
304 switch (tc
->cls_flower
->command
) {
305 case TC_CLSFLOWER_REPLACE
:
306 return mlx5e_configure_flower(priv
, proto
, tc
->cls_flower
);
307 case TC_CLSFLOWER_DESTROY
:
308 return mlx5e_delete_flower(priv
, tc
->cls_flower
);
309 case TC_CLSFLOWER_STATS
:
310 return mlx5e_stats_flower(priv
, tc
->cls_flower
);
317 bool mlx5e_is_uplink_rep(struct mlx5e_priv
*priv
)
319 struct mlx5_eswitch_rep
*rep
= (struct mlx5_eswitch_rep
*)priv
->ppriv
;
320 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
322 if (rep
&& rep
->vport
== FDB_UPLINK_VPORT
&& esw
->mode
== SRIOV_OFFLOADS
)
328 bool mlx5e_is_vf_vport_rep(struct mlx5e_priv
*priv
)
330 struct mlx5_eswitch_rep
*rep
= (struct mlx5_eswitch_rep
*)priv
->ppriv
;
332 if (rep
&& rep
->vport
!= FDB_UPLINK_VPORT
)
338 bool mlx5e_has_offload_stats(const struct net_device
*dev
, int attr_id
)
340 struct mlx5e_priv
*priv
= netdev_priv(dev
);
343 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
344 if (mlx5e_is_vf_vport_rep(priv
) || mlx5e_is_uplink_rep(priv
))
352 mlx5e_get_sw_stats64(const struct net_device
*dev
,
353 struct rtnl_link_stats64
*stats
)
355 struct mlx5e_priv
*priv
= netdev_priv(dev
);
356 struct mlx5e_sw_stats
*sstats
= &priv
->stats
.sw
;
358 stats
->rx_packets
= sstats
->rx_packets
;
359 stats
->rx_bytes
= sstats
->rx_bytes
;
360 stats
->tx_packets
= sstats
->tx_packets
;
361 stats
->tx_bytes
= sstats
->tx_bytes
;
363 stats
->tx_dropped
= sstats
->tx_queue_dropped
;
368 int mlx5e_get_offload_stats(int attr_id
, const struct net_device
*dev
,
372 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
373 return mlx5e_get_sw_stats64(dev
, sp
);
380 mlx5e_rep_get_stats(struct net_device
*dev
, struct rtnl_link_stats64
*stats
)
382 struct mlx5e_priv
*priv
= netdev_priv(dev
);
384 memcpy(stats
, &priv
->stats
.vf_vport
, sizeof(*stats
));
387 static const struct switchdev_ops mlx5e_rep_switchdev_ops
= {
388 .switchdev_port_attr_get
= mlx5e_attr_get
,
391 static const struct net_device_ops mlx5e_netdev_ops_rep
= {
392 .ndo_open
= mlx5e_rep_open
,
393 .ndo_stop
= mlx5e_rep_close
,
394 .ndo_start_xmit
= mlx5e_xmit
,
395 .ndo_get_phys_port_name
= mlx5e_rep_get_phys_port_name
,
396 .ndo_setup_tc
= mlx5e_rep_ndo_setup_tc
,
397 .ndo_get_stats64
= mlx5e_rep_get_stats
,
398 .ndo_has_offload_stats
= mlx5e_has_offload_stats
,
399 .ndo_get_offload_stats
= mlx5e_get_offload_stats
,
402 static void mlx5e_build_rep_params(struct mlx5_core_dev
*mdev
,
403 struct mlx5e_params
*params
)
405 u8 cq_period_mode
= MLX5_CAP_GEN(mdev
, cq_period_start_from_cqe
) ?
406 MLX5_CQ_PERIOD_MODE_START_FROM_CQE
:
407 MLX5_CQ_PERIOD_MODE_START_FROM_EQE
;
409 params
->log_sq_size
= MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE
;
410 params
->rq_wq_type
= MLX5_WQ_TYPE_LINKED_LIST
;
411 params
->log_rq_size
= MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE
;
413 params
->rx_am_enabled
= MLX5_CAP_GEN(mdev
, cq_moderation
);
414 mlx5e_set_rx_cq_mode_params(params
, cq_period_mode
);
416 params
->tx_max_inline
= mlx5e_get_max_inline_cap(mdev
);
418 params
->lro_wqe_sz
= MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ
;
421 static void mlx5e_build_rep_netdev(struct net_device
*netdev
)
423 netdev
->netdev_ops
= &mlx5e_netdev_ops_rep
;
425 netdev
->watchdog_timeo
= 15 * HZ
;
427 netdev
->ethtool_ops
= &mlx5e_rep_ethtool_ops
;
429 #ifdef CONFIG_NET_SWITCHDEV
430 netdev
->switchdev_ops
= &mlx5e_rep_switchdev_ops
;
433 netdev
->features
|= NETIF_F_VLAN_CHALLENGED
| NETIF_F_HW_TC
| NETIF_F_NETNS_LOCAL
;
434 netdev
->hw_features
|= NETIF_F_HW_TC
;
436 eth_hw_addr_random(netdev
);
439 static void mlx5e_init_rep(struct mlx5_core_dev
*mdev
,
440 struct net_device
*netdev
,
441 const struct mlx5e_profile
*profile
,
444 struct mlx5e_priv
*priv
= netdev_priv(netdev
);
447 priv
->netdev
= netdev
;
448 priv
->profile
= profile
;
451 mutex_init(&priv
->state_lock
);
453 INIT_DELAYED_WORK(&priv
->update_stats_work
, mlx5e_update_stats_work
);
455 priv
->channels
.params
.num_channels
= profile
->max_nch(mdev
);
456 mlx5e_build_rep_params(mdev
, &priv
->channels
.params
);
457 mlx5e_build_rep_netdev(netdev
);
460 static int mlx5e_init_rep_rx(struct mlx5e_priv
*priv
)
462 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
463 struct mlx5_eswitch_rep
*rep
= priv
->ppriv
;
464 struct mlx5_core_dev
*mdev
= priv
->mdev
;
465 struct mlx5_flow_handle
*flow_rule
;
469 err
= mlx5e_create_direct_rqts(priv
);
471 mlx5_core_warn(mdev
, "create direct rqts failed, %d\n", err
);
475 err
= mlx5e_create_direct_tirs(priv
);
477 mlx5_core_warn(mdev
, "create direct tirs failed, %d\n", err
);
478 goto err_destroy_direct_rqts
;
481 flow_rule
= mlx5_eswitch_create_vport_rx_rule(esw
,
483 priv
->direct_tir
[0].tirn
);
484 if (IS_ERR(flow_rule
)) {
485 err
= PTR_ERR(flow_rule
);
486 goto err_destroy_direct_tirs
;
488 rep
->vport_rx_rule
= flow_rule
;
490 err
= mlx5e_tc_init(priv
);
492 goto err_del_flow_rule
;
497 mlx5_del_flow_rules(rep
->vport_rx_rule
);
498 err_destroy_direct_tirs
:
499 mlx5e_destroy_direct_tirs(priv
);
500 err_destroy_direct_rqts
:
501 for (i
= 0; i
< priv
->channels
.params
.num_channels
; i
++)
502 mlx5e_destroy_rqt(priv
, &priv
->direct_tir
[i
].rqt
);
506 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv
*priv
)
508 struct mlx5_eswitch_rep
*rep
= priv
->ppriv
;
511 mlx5e_tc_cleanup(priv
);
512 mlx5_del_flow_rules(rep
->vport_rx_rule
);
513 mlx5e_destroy_direct_tirs(priv
);
514 for (i
= 0; i
< priv
->channels
.params
.num_channels
; i
++)
515 mlx5e_destroy_rqt(priv
, &priv
->direct_tir
[i
].rqt
);
518 static int mlx5e_init_rep_tx(struct mlx5e_priv
*priv
)
522 err
= mlx5e_create_tises(priv
);
524 mlx5_core_warn(priv
->mdev
, "create tises failed, %d\n", err
);
530 static int mlx5e_get_rep_max_num_channels(struct mlx5_core_dev
*mdev
)
532 #define MLX5E_PORT_REPRESENTOR_NCH 1
533 return MLX5E_PORT_REPRESENTOR_NCH
;
536 static struct mlx5e_profile mlx5e_rep_profile
= {
537 .init
= mlx5e_init_rep
,
538 .init_rx
= mlx5e_init_rep_rx
,
539 .cleanup_rx
= mlx5e_cleanup_rep_rx
,
540 .init_tx
= mlx5e_init_rep_tx
,
541 .cleanup_tx
= mlx5e_cleanup_nic_tx
,
542 .update_stats
= mlx5e_rep_update_stats
,
543 .max_nch
= mlx5e_get_rep_max_num_channels
,
547 int mlx5e_vport_rep_load(struct mlx5_eswitch
*esw
,
548 struct mlx5_eswitch_rep
*rep
)
550 struct net_device
*netdev
;
553 netdev
= mlx5e_create_netdev(esw
->dev
, &mlx5e_rep_profile
, rep
);
555 pr_warn("Failed to create representor netdev for vport %d\n",
560 rep
->netdev
= netdev
;
562 err
= mlx5e_attach_netdev(esw
->dev
, netdev
);
564 pr_warn("Failed to attach representor netdev for vport %d\n",
566 goto err_destroy_netdev
;
569 err
= register_netdev(netdev
);
571 pr_warn("Failed to register representor netdev for vport %d\n",
573 goto err_detach_netdev
;
579 mlx5e_detach_netdev(esw
->dev
, netdev
);
582 mlx5e_destroy_netdev(esw
->dev
, netdev_priv(netdev
));
588 void mlx5e_vport_rep_unload(struct mlx5_eswitch
*esw
,
589 struct mlx5_eswitch_rep
*rep
)
591 struct net_device
*netdev
= rep
->netdev
;
593 unregister_netdev(netdev
);
594 mlx5e_detach_netdev(esw
->dev
, netdev
);
595 mlx5e_destroy_netdev(esw
->dev
, netdev_priv(netdev
));