]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/ethernet/mellanox/mlx5/core/en_rep.c
53db5ec2c1225a7960e78965403aced80eecc94d
[mirror_ubuntu-bionic-kernel.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_rep.c
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 <generated/utsrelease.h>
34 #include <linux/mlx5/fs.h>
35 #include <net/switchdev.h>
36 #include <net/pkt_cls.h>
37
38 #include "eswitch.h"
39 #include "en.h"
40 #include "en_tc.h"
41
42 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
43
44 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
45 struct ethtool_drvinfo *drvinfo)
46 {
47 strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
48 sizeof(drvinfo->driver));
49 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
50 }
51
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) },
57 };
58
59 #define NUM_VPORT_REP_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
60
61 static void mlx5e_rep_get_strings(struct net_device *dev,
62 u32 stringset, uint8_t *data)
63 {
64 int i;
65
66 switch (stringset) {
67 case ETH_SS_STATS:
68 for (i = 0; i < NUM_VPORT_REP_COUNTERS; i++)
69 strcpy(data + (i * ETH_GSTRING_LEN),
70 sw_rep_stats_desc[i].format);
71 break;
72 }
73 }
74
75 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv *priv)
76 {
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;
81 int err;
82
83 err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
84 if (err) {
85 pr_warn("vport %d error %d reading stats\n", rep->vport, err);
86 return;
87 }
88
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;
95 }
96
97 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv *priv)
98 {
99 struct mlx5e_sw_stats *s = &priv->stats.sw;
100 struct mlx5e_rq_stats *rq_stats;
101 struct mlx5e_sq_stats *sq_stats;
102 int i, j;
103
104 memset(s, 0, sizeof(*s));
105 for (i = 0; i < priv->channels.num; i++) {
106 struct mlx5e_channel *c = priv->channels.c[i];
107
108 rq_stats = &c->rq.stats;
109
110 s->rx_packets += rq_stats->packets;
111 s->rx_bytes += rq_stats->bytes;
112
113 for (j = 0; j < priv->channels.params.num_tc; j++) {
114 sq_stats = &c->sq[j].stats;
115
116 s->tx_packets += sq_stats->packets;
117 s->tx_bytes += sq_stats->bytes;
118 }
119 }
120 }
121
122 static void mlx5e_rep_update_stats(struct mlx5e_priv *priv)
123 {
124 mlx5e_rep_update_sw_counters(priv);
125 mlx5e_rep_update_hw_counters(priv);
126 }
127
128 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
129 struct ethtool_stats *stats, u64 *data)
130 {
131 struct mlx5e_priv *priv = netdev_priv(dev);
132 int i;
133
134 if (!data)
135 return;
136
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);
141
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);
145 }
146
147 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
148 {
149 switch (sset) {
150 case ETH_SS_STATS:
151 return NUM_VPORT_REP_COUNTERS;
152 default:
153 return -EOPNOTSUPP;
154 }
155 }
156
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,
163 };
164
165 int mlx5e_attr_get(struct net_device *dev, struct switchdev_attr *attr)
166 {
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;
170
171 if (esw->mode == SRIOV_NONE)
172 return -EOPNOTSUPP;
173
174 switch (attr->id) {
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);
178 break;
179 default:
180 return -EOPNOTSUPP;
181 }
182
183 return 0;
184 }
185
186 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
187
188 {
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, num_sqs = 0;
193 int err = -ENOMEM;
194 u16 *sqs;
195
196 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(u16), GFP_KERNEL);
197 if (!sqs)
198 goto out;
199
200 for (n = 0; n < priv->channels.num; n++) {
201 c = priv->channels.c[n];
202 for (tc = 0; tc < c->num_tc; tc++)
203 sqs[num_sqs++] = c->sq[tc].sqn;
204 }
205
206 err = mlx5_eswitch_sqs2vport_start(esw, rep, sqs, num_sqs);
207 kfree(sqs);
208
209 out:
210 if (err)
211 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
212 return err;
213 }
214
215 int mlx5e_nic_rep_load(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep)
216 {
217 struct net_device *netdev = rep->netdev;
218 struct mlx5e_priv *priv = netdev_priv(netdev);
219
220 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
221 return mlx5e_add_sqs_fwd_rules(priv);
222 return 0;
223 }
224
225 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
226 {
227 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
228 struct mlx5_eswitch_rep *rep = priv->ppriv;
229
230 mlx5_eswitch_sqs2vport_stop(esw, rep);
231 }
232
233 void mlx5e_nic_rep_unload(struct mlx5_eswitch *esw,
234 struct mlx5_eswitch_rep *rep)
235 {
236 struct net_device *netdev = rep->netdev;
237 struct mlx5e_priv *priv = netdev_priv(netdev);
238
239 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
240 mlx5e_remove_sqs_fwd_rules(priv);
241
242 /* clean (and re-init) existing uplink offloaded TC rules */
243 mlx5e_tc_cleanup(priv);
244 mlx5e_tc_init(priv);
245 }
246
247 static int mlx5e_rep_open(struct net_device *dev)
248 {
249 struct mlx5e_priv *priv = netdev_priv(dev);
250 struct mlx5_eswitch_rep *rep = priv->ppriv;
251 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
252 int err;
253
254 err = mlx5e_open(dev);
255 if (err)
256 return err;
257
258 err = mlx5_eswitch_set_vport_state(esw, rep->vport, MLX5_ESW_VPORT_ADMIN_STATE_UP);
259 if (!err)
260 netif_carrier_on(dev);
261
262 return 0;
263 }
264
265 static int mlx5e_rep_close(struct net_device *dev)
266 {
267 struct mlx5e_priv *priv = netdev_priv(dev);
268 struct mlx5_eswitch_rep *rep = priv->ppriv;
269 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
270
271 (void)mlx5_eswitch_set_vport_state(esw, rep->vport, MLX5_ESW_VPORT_ADMIN_STATE_DOWN);
272
273 return mlx5e_close(dev);
274 }
275
276 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
277 char *buf, size_t len)
278 {
279 struct mlx5e_priv *priv = netdev_priv(dev);
280 struct mlx5_eswitch_rep *rep = priv->ppriv;
281 int ret;
282
283 ret = snprintf(buf, len, "%d", rep->vport - 1);
284 if (ret >= len)
285 return -EOPNOTSUPP;
286
287 return 0;
288 }
289
290 static int mlx5e_rep_ndo_setup_tc(struct net_device *dev, u32 handle,
291 __be16 proto, struct tc_to_netdev *tc)
292 {
293 struct mlx5e_priv *priv = netdev_priv(dev);
294
295 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
296 return -EOPNOTSUPP;
297
298 if (tc->egress_dev) {
299 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
300 struct net_device *uplink_dev = mlx5_eswitch_get_uplink_netdev(esw);
301
302 return uplink_dev->netdev_ops->ndo_setup_tc(uplink_dev, handle,
303 proto, tc);
304 }
305
306 switch (tc->type) {
307 case TC_SETUP_CLSFLOWER:
308 switch (tc->cls_flower->command) {
309 case TC_CLSFLOWER_REPLACE:
310 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
311 case TC_CLSFLOWER_DESTROY:
312 return mlx5e_delete_flower(priv, tc->cls_flower);
313 case TC_CLSFLOWER_STATS:
314 return mlx5e_stats_flower(priv, tc->cls_flower);
315 }
316 default:
317 return -EOPNOTSUPP;
318 }
319 }
320
321 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
322 {
323 struct mlx5_eswitch_rep *rep = (struct mlx5_eswitch_rep *)priv->ppriv;
324 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
325
326 if (rep && rep->vport == FDB_UPLINK_VPORT && esw->mode == SRIOV_OFFLOADS)
327 return true;
328
329 return false;
330 }
331
332 bool mlx5e_is_vf_vport_rep(struct mlx5e_priv *priv)
333 {
334 struct mlx5_eswitch_rep *rep = (struct mlx5_eswitch_rep *)priv->ppriv;
335
336 if (rep && rep->vport != FDB_UPLINK_VPORT)
337 return true;
338
339 return false;
340 }
341
342 bool mlx5e_has_offload_stats(const struct net_device *dev, int attr_id)
343 {
344 struct mlx5e_priv *priv = netdev_priv(dev);
345
346 switch (attr_id) {
347 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
348 if (mlx5e_is_vf_vport_rep(priv) || mlx5e_is_uplink_rep(priv))
349 return true;
350 }
351
352 return false;
353 }
354
355 static int
356 mlx5e_get_sw_stats64(const struct net_device *dev,
357 struct rtnl_link_stats64 *stats)
358 {
359 struct mlx5e_priv *priv = netdev_priv(dev);
360 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
361
362 stats->rx_packets = sstats->rx_packets;
363 stats->rx_bytes = sstats->rx_bytes;
364 stats->tx_packets = sstats->tx_packets;
365 stats->tx_bytes = sstats->tx_bytes;
366
367 stats->tx_dropped = sstats->tx_queue_dropped;
368
369 return 0;
370 }
371
372 int mlx5e_get_offload_stats(int attr_id, const struct net_device *dev,
373 void *sp)
374 {
375 switch (attr_id) {
376 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
377 return mlx5e_get_sw_stats64(dev, sp);
378 }
379
380 return -EINVAL;
381 }
382
383 static void
384 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
385 {
386 struct mlx5e_priv *priv = netdev_priv(dev);
387
388 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
389 }
390
391 static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
392 .switchdev_port_attr_get = mlx5e_attr_get,
393 };
394
395 static const struct net_device_ops mlx5e_netdev_ops_rep = {
396 .ndo_open = mlx5e_rep_open,
397 .ndo_stop = mlx5e_rep_close,
398 .ndo_start_xmit = mlx5e_xmit,
399 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
400 .ndo_setup_tc = mlx5e_rep_ndo_setup_tc,
401 .ndo_get_stats64 = mlx5e_rep_get_stats,
402 .ndo_has_offload_stats = mlx5e_has_offload_stats,
403 .ndo_get_offload_stats = mlx5e_get_offload_stats,
404 };
405
406 static void mlx5e_build_rep_params(struct mlx5_core_dev *mdev,
407 struct mlx5e_params *params)
408 {
409 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
410 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
411 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
412
413 params->log_sq_size = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
414 params->rq_wq_type = MLX5_WQ_TYPE_LINKED_LIST;
415 params->log_rq_size = MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE;
416
417 params->rx_am_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
418 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
419
420 params->tx_max_inline = mlx5e_get_max_inline_cap(mdev);
421 params->num_tc = 1;
422 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
423 }
424
425 static void mlx5e_build_rep_netdev(struct net_device *netdev)
426 {
427 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
428
429 netdev->watchdog_timeo = 15 * HZ;
430
431 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
432
433 #ifdef CONFIG_NET_SWITCHDEV
434 netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
435 #endif
436
437 netdev->features |= NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
438 netdev->hw_features |= NETIF_F_HW_TC;
439
440 eth_hw_addr_random(netdev);
441 }
442
443 static void mlx5e_init_rep(struct mlx5_core_dev *mdev,
444 struct net_device *netdev,
445 const struct mlx5e_profile *profile,
446 void *ppriv)
447 {
448 struct mlx5e_priv *priv = netdev_priv(netdev);
449
450 priv->mdev = mdev;
451 priv->netdev = netdev;
452 priv->profile = profile;
453 priv->ppriv = ppriv;
454
455 mutex_init(&priv->state_lock);
456
457 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
458
459 priv->channels.params.num_channels = profile->max_nch(mdev);
460 mlx5e_build_rep_params(mdev, &priv->channels.params);
461 mlx5e_build_rep_netdev(netdev);
462 }
463
464 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
465 {
466 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
467 struct mlx5_eswitch_rep *rep = priv->ppriv;
468 struct mlx5_core_dev *mdev = priv->mdev;
469 struct mlx5_flow_handle *flow_rule;
470 int err;
471 int i;
472
473 err = mlx5e_create_direct_rqts(priv);
474 if (err) {
475 mlx5_core_warn(mdev, "create direct rqts failed, %d\n", err);
476 return err;
477 }
478
479 err = mlx5e_create_direct_tirs(priv);
480 if (err) {
481 mlx5_core_warn(mdev, "create direct tirs failed, %d\n", err);
482 goto err_destroy_direct_rqts;
483 }
484
485 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
486 rep->vport,
487 priv->direct_tir[0].tirn);
488 if (IS_ERR(flow_rule)) {
489 err = PTR_ERR(flow_rule);
490 goto err_destroy_direct_tirs;
491 }
492 rep->vport_rx_rule = flow_rule;
493
494 err = mlx5e_tc_init(priv);
495 if (err)
496 goto err_del_flow_rule;
497
498 return 0;
499
500 err_del_flow_rule:
501 mlx5_del_flow_rules(rep->vport_rx_rule);
502 err_destroy_direct_tirs:
503 mlx5e_destroy_direct_tirs(priv);
504 err_destroy_direct_rqts:
505 for (i = 0; i < priv->channels.params.num_channels; i++)
506 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
507 return err;
508 }
509
510 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
511 {
512 struct mlx5_eswitch_rep *rep = priv->ppriv;
513 int i;
514
515 mlx5e_tc_cleanup(priv);
516 mlx5_del_flow_rules(rep->vport_rx_rule);
517 mlx5e_destroy_direct_tirs(priv);
518 for (i = 0; i < priv->channels.params.num_channels; i++)
519 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
520 }
521
522 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
523 {
524 int err;
525
526 err = mlx5e_create_tises(priv);
527 if (err) {
528 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
529 return err;
530 }
531 return 0;
532 }
533
534 static int mlx5e_get_rep_max_num_channels(struct mlx5_core_dev *mdev)
535 {
536 #define MLX5E_PORT_REPRESENTOR_NCH 1
537 return MLX5E_PORT_REPRESENTOR_NCH;
538 }
539
540 static struct mlx5e_profile mlx5e_rep_profile = {
541 .init = mlx5e_init_rep,
542 .init_rx = mlx5e_init_rep_rx,
543 .cleanup_rx = mlx5e_cleanup_rep_rx,
544 .init_tx = mlx5e_init_rep_tx,
545 .cleanup_tx = mlx5e_cleanup_nic_tx,
546 .update_stats = mlx5e_rep_update_stats,
547 .max_nch = mlx5e_get_rep_max_num_channels,
548 .max_tc = 1,
549 };
550
551 int mlx5e_vport_rep_load(struct mlx5_eswitch *esw,
552 struct mlx5_eswitch_rep *rep)
553 {
554 struct net_device *netdev;
555 int err;
556
557 netdev = mlx5e_create_netdev(esw->dev, &mlx5e_rep_profile, rep);
558 if (!netdev) {
559 pr_warn("Failed to create representor netdev for vport %d\n",
560 rep->vport);
561 return -EINVAL;
562 }
563
564 rep->netdev = netdev;
565
566 err = mlx5e_attach_netdev(esw->dev, netdev);
567 if (err) {
568 pr_warn("Failed to attach representor netdev for vport %d\n",
569 rep->vport);
570 goto err_destroy_netdev;
571 }
572
573 err = register_netdev(netdev);
574 if (err) {
575 pr_warn("Failed to register representor netdev for vport %d\n",
576 rep->vport);
577 goto err_detach_netdev;
578 }
579
580 return 0;
581
582 err_detach_netdev:
583 mlx5e_detach_netdev(esw->dev, netdev);
584
585 err_destroy_netdev:
586 mlx5e_destroy_netdev(esw->dev, netdev_priv(netdev));
587
588 return err;
589
590 }
591
592 void mlx5e_vport_rep_unload(struct mlx5_eswitch *esw,
593 struct mlx5_eswitch_rep *rep)
594 {
595 struct net_device *netdev = rep->netdev;
596
597 unregister_netdev(netdev);
598 mlx5e_detach_netdev(esw->dev, netdev);
599 mlx5e_destroy_netdev(esw->dev, netdev_priv(netdev));
600 }