]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/ethernet/mellanox/mlx5/core/en_rep.c
Merge remote-tracking branch 'regulator/fix/max77802' into regulator-linus
[mirror_ubuntu-artful-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 #include <net/netevent.h>
38 #include <net/arp.h>
39
40 #include "eswitch.h"
41 #include "en.h"
42 #include "en_rep.h"
43 #include "en_tc.h"
44 #include "fs_core.h"
45
46 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
47
48 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
49 struct ethtool_drvinfo *drvinfo)
50 {
51 strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
52 sizeof(drvinfo->driver));
53 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
54 }
55
56 static const struct counter_desc sw_rep_stats_desc[] = {
57 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
58 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
59 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
60 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
61 };
62
63 #define NUM_VPORT_REP_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
64
65 static void mlx5e_rep_get_strings(struct net_device *dev,
66 u32 stringset, uint8_t *data)
67 {
68 int i;
69
70 switch (stringset) {
71 case ETH_SS_STATS:
72 for (i = 0; i < NUM_VPORT_REP_COUNTERS; i++)
73 strcpy(data + (i * ETH_GSTRING_LEN),
74 sw_rep_stats_desc[i].format);
75 break;
76 }
77 }
78
79 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv *priv)
80 {
81 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
82 struct mlx5e_rep_priv *rpriv = priv->ppriv;
83 struct mlx5_eswitch_rep *rep = rpriv->rep;
84 struct rtnl_link_stats64 *vport_stats;
85 struct ifla_vf_stats vf_stats;
86 int err;
87
88 err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
89 if (err) {
90 pr_warn("vport %d error %d reading stats\n", rep->vport, err);
91 return;
92 }
93
94 vport_stats = &priv->stats.vf_vport;
95 /* flip tx/rx as we are reporting the counters for the switch vport */
96 vport_stats->rx_packets = vf_stats.tx_packets;
97 vport_stats->rx_bytes = vf_stats.tx_bytes;
98 vport_stats->tx_packets = vf_stats.rx_packets;
99 vport_stats->tx_bytes = vf_stats.rx_bytes;
100 }
101
102 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv *priv)
103 {
104 struct mlx5e_sw_stats *s = &priv->stats.sw;
105 struct mlx5e_rq_stats *rq_stats;
106 struct mlx5e_sq_stats *sq_stats;
107 int i, j;
108
109 memset(s, 0, sizeof(*s));
110 for (i = 0; i < priv->channels.num; i++) {
111 struct mlx5e_channel *c = priv->channels.c[i];
112
113 rq_stats = &c->rq.stats;
114
115 s->rx_packets += rq_stats->packets;
116 s->rx_bytes += rq_stats->bytes;
117
118 for (j = 0; j < priv->channels.params.num_tc; j++) {
119 sq_stats = &c->sq[j].stats;
120
121 s->tx_packets += sq_stats->packets;
122 s->tx_bytes += sq_stats->bytes;
123 }
124 }
125 }
126
127 static void mlx5e_rep_update_stats(struct mlx5e_priv *priv)
128 {
129 mlx5e_rep_update_sw_counters(priv);
130 mlx5e_rep_update_hw_counters(priv);
131 }
132
133 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
134 struct ethtool_stats *stats, u64 *data)
135 {
136 struct mlx5e_priv *priv = netdev_priv(dev);
137 int i;
138
139 if (!data)
140 return;
141
142 mutex_lock(&priv->state_lock);
143 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
144 mlx5e_rep_update_sw_counters(priv);
145 mutex_unlock(&priv->state_lock);
146
147 for (i = 0; i < NUM_VPORT_REP_COUNTERS; i++)
148 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
149 sw_rep_stats_desc, i);
150 }
151
152 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
153 {
154 switch (sset) {
155 case ETH_SS_STATS:
156 return NUM_VPORT_REP_COUNTERS;
157 default:
158 return -EOPNOTSUPP;
159 }
160 }
161
162 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
163 .get_drvinfo = mlx5e_rep_get_drvinfo,
164 .get_link = ethtool_op_get_link,
165 .get_strings = mlx5e_rep_get_strings,
166 .get_sset_count = mlx5e_rep_get_sset_count,
167 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
168 };
169
170 int mlx5e_attr_get(struct net_device *dev, struct switchdev_attr *attr)
171 {
172 struct mlx5e_priv *priv = netdev_priv(dev);
173 struct mlx5e_rep_priv *rpriv = priv->ppriv;
174 struct mlx5_eswitch_rep *rep = rpriv->rep;
175 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
176
177 if (esw->mode == SRIOV_NONE)
178 return -EOPNOTSUPP;
179
180 switch (attr->id) {
181 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
182 attr->u.ppid.id_len = ETH_ALEN;
183 ether_addr_copy(attr->u.ppid.id, rep->hw_id);
184 break;
185 default:
186 return -EOPNOTSUPP;
187 }
188
189 return 0;
190 }
191
192 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
193 {
194 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
195 struct mlx5e_rep_priv *rpriv = priv->ppriv;
196 struct mlx5_eswitch_rep *rep = rpriv->rep;
197 struct mlx5e_channel *c;
198 int n, tc, num_sqs = 0;
199 int err = -ENOMEM;
200 u16 *sqs;
201
202 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(u16), GFP_KERNEL);
203 if (!sqs)
204 goto out;
205
206 for (n = 0; n < priv->channels.num; n++) {
207 c = priv->channels.c[n];
208 for (tc = 0; tc < c->num_tc; tc++)
209 sqs[num_sqs++] = c->sq[tc].sqn;
210 }
211
212 err = mlx5_eswitch_sqs2vport_start(esw, rep, sqs, num_sqs);
213 kfree(sqs);
214
215 out:
216 if (err)
217 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
218 return err;
219 }
220
221 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
222 {
223 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
224 struct mlx5e_rep_priv *rpriv = priv->ppriv;
225 struct mlx5_eswitch_rep *rep = rpriv->rep;
226
227 mlx5_eswitch_sqs2vport_stop(esw, rep);
228 }
229
230 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv *rpriv)
231 {
232 #if IS_ENABLED(CONFIG_IPV6)
233 unsigned long ipv6_interval = NEIGH_VAR(&ipv6_stub->nd_tbl->parms,
234 DELAY_PROBE_TIME);
235 #else
236 unsigned long ipv6_interval = ~0UL;
237 #endif
238 unsigned long ipv4_interval = NEIGH_VAR(&arp_tbl.parms,
239 DELAY_PROBE_TIME);
240 struct net_device *netdev = rpriv->rep->netdev;
241 struct mlx5e_priv *priv = netdev_priv(netdev);
242
243 rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);
244 mlx5_fc_update_sampling_interval(priv->mdev, rpriv->neigh_update.min_interval);
245 }
246
247 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv)
248 {
249 struct mlx5e_rep_priv *rpriv = priv->ppriv;
250 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
251
252 mlx5_fc_queue_stats_work(priv->mdev,
253 &neigh_update->neigh_stats_work,
254 neigh_update->min_interval);
255 }
256
257 static void mlx5e_rep_neigh_stats_work(struct work_struct *work)
258 {
259 struct mlx5e_rep_priv *rpriv = container_of(work, struct mlx5e_rep_priv,
260 neigh_update.neigh_stats_work.work);
261 struct net_device *netdev = rpriv->rep->netdev;
262 struct mlx5e_priv *priv = netdev_priv(netdev);
263 struct mlx5e_neigh_hash_entry *nhe;
264
265 rtnl_lock();
266 if (!list_empty(&rpriv->neigh_update.neigh_list))
267 mlx5e_rep_queue_neigh_stats_work(priv);
268
269 list_for_each_entry(nhe, &rpriv->neigh_update.neigh_list, neigh_list)
270 mlx5e_tc_update_neigh_used_value(nhe);
271
272 rtnl_unlock();
273 }
274
275 static void mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry *nhe)
276 {
277 refcount_inc(&nhe->refcnt);
278 }
279
280 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry *nhe)
281 {
282 if (refcount_dec_and_test(&nhe->refcnt))
283 kfree(nhe);
284 }
285
286 static void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
287 struct mlx5e_encap_entry *e,
288 bool neigh_connected,
289 unsigned char ha[ETH_ALEN])
290 {
291 struct ethhdr *eth = (struct ethhdr *)e->encap_header;
292
293 ASSERT_RTNL();
294
295 if ((!neigh_connected && (e->flags & MLX5_ENCAP_ENTRY_VALID)) ||
296 !ether_addr_equal(e->h_dest, ha))
297 mlx5e_tc_encap_flows_del(priv, e);
298
299 if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
300 ether_addr_copy(e->h_dest, ha);
301 ether_addr_copy(eth->h_dest, ha);
302
303 mlx5e_tc_encap_flows_add(priv, e);
304 }
305 }
306
307 static void mlx5e_rep_neigh_update(struct work_struct *work)
308 {
309 struct mlx5e_neigh_hash_entry *nhe =
310 container_of(work, struct mlx5e_neigh_hash_entry, neigh_update_work);
311 struct neighbour *n = nhe->n;
312 struct mlx5e_encap_entry *e;
313 unsigned char ha[ETH_ALEN];
314 struct mlx5e_priv *priv;
315 bool neigh_connected;
316 bool encap_connected;
317 u8 nud_state, dead;
318
319 rtnl_lock();
320
321 /* If these parameters are changed after we release the lock,
322 * we'll receive another event letting us know about it.
323 * We use this lock to avoid inconsistency between the neigh validity
324 * and it's hw address.
325 */
326 read_lock_bh(&n->lock);
327 memcpy(ha, n->ha, ETH_ALEN);
328 nud_state = n->nud_state;
329 dead = n->dead;
330 read_unlock_bh(&n->lock);
331
332 neigh_connected = (nud_state & NUD_VALID) && !dead;
333
334 list_for_each_entry(e, &nhe->encap_list, encap_list) {
335 encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
336 priv = netdev_priv(e->out_dev);
337
338 if (encap_connected != neigh_connected ||
339 !ether_addr_equal(e->h_dest, ha))
340 mlx5e_rep_update_flows(priv, e, neigh_connected, ha);
341 }
342 mlx5e_rep_neigh_entry_release(nhe);
343 rtnl_unlock();
344 neigh_release(n);
345 }
346
347 static struct mlx5e_neigh_hash_entry *
348 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
349 struct mlx5e_neigh *m_neigh);
350
351 static int mlx5e_rep_netevent_event(struct notifier_block *nb,
352 unsigned long event, void *ptr)
353 {
354 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
355 neigh_update.netevent_nb);
356 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
357 struct net_device *netdev = rpriv->rep->netdev;
358 struct mlx5e_priv *priv = netdev_priv(netdev);
359 struct mlx5e_neigh_hash_entry *nhe = NULL;
360 struct mlx5e_neigh m_neigh = {};
361 struct neigh_parms *p;
362 struct neighbour *n;
363 bool found = false;
364
365 switch (event) {
366 case NETEVENT_NEIGH_UPDATE:
367 n = ptr;
368 #if IS_ENABLED(CONFIG_IPV6)
369 if (n->tbl != ipv6_stub->nd_tbl && n->tbl != &arp_tbl)
370 #else
371 if (n->tbl != &arp_tbl)
372 #endif
373 return NOTIFY_DONE;
374
375 m_neigh.dev = n->dev;
376 m_neigh.family = n->ops->family;
377 memcpy(&m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
378
379 /* We are in atomic context and can't take RTNL mutex, so use
380 * spin_lock_bh to lookup the neigh table. bh is used since
381 * netevent can be called from a softirq context.
382 */
383 spin_lock_bh(&neigh_update->encap_lock);
384 nhe = mlx5e_rep_neigh_entry_lookup(priv, &m_neigh);
385 if (!nhe) {
386 spin_unlock_bh(&neigh_update->encap_lock);
387 return NOTIFY_DONE;
388 }
389
390 /* This assignment is valid as long as the the neigh reference
391 * is taken
392 */
393 nhe->n = n;
394
395 /* Take a reference to ensure the neighbour and mlx5 encap
396 * entry won't be destructed until we drop the reference in
397 * delayed work.
398 */
399 neigh_hold(n);
400 mlx5e_rep_neigh_entry_hold(nhe);
401
402 if (!queue_work(priv->wq, &nhe->neigh_update_work)) {
403 mlx5e_rep_neigh_entry_release(nhe);
404 neigh_release(n);
405 }
406 spin_unlock_bh(&neigh_update->encap_lock);
407 break;
408
409 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
410 p = ptr;
411
412 /* We check the device is present since we don't care about
413 * changes in the default table, we only care about changes
414 * done per device delay prob time parameter.
415 */
416 #if IS_ENABLED(CONFIG_IPV6)
417 if (!p->dev || (p->tbl != ipv6_stub->nd_tbl && p->tbl != &arp_tbl))
418 #else
419 if (!p->dev || p->tbl != &arp_tbl)
420 #endif
421 return NOTIFY_DONE;
422
423 /* We are in atomic context and can't take RTNL mutex,
424 * so use spin_lock_bh to walk the neigh list and look for
425 * the relevant device. bh is used since netevent can be
426 * called from a softirq context.
427 */
428 spin_lock_bh(&neigh_update->encap_lock);
429 list_for_each_entry(nhe, &neigh_update->neigh_list, neigh_list) {
430 if (p->dev == nhe->m_neigh.dev) {
431 found = true;
432 break;
433 }
434 }
435 spin_unlock_bh(&neigh_update->encap_lock);
436 if (!found)
437 return NOTIFY_DONE;
438
439 neigh_update->min_interval = min_t(unsigned long,
440 NEIGH_VAR(p, DELAY_PROBE_TIME),
441 neigh_update->min_interval);
442 mlx5_fc_update_sampling_interval(priv->mdev,
443 neigh_update->min_interval);
444 break;
445 }
446 return NOTIFY_DONE;
447 }
448
449 static const struct rhashtable_params mlx5e_neigh_ht_params = {
450 .head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
451 .key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
452 .key_len = sizeof(struct mlx5e_neigh),
453 .automatic_shrinking = true,
454 };
455
456 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv *rpriv)
457 {
458 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
459 int err;
460
461 err = rhashtable_init(&neigh_update->neigh_ht, &mlx5e_neigh_ht_params);
462 if (err)
463 return err;
464
465 INIT_LIST_HEAD(&neigh_update->neigh_list);
466 spin_lock_init(&neigh_update->encap_lock);
467 INIT_DELAYED_WORK(&neigh_update->neigh_stats_work,
468 mlx5e_rep_neigh_stats_work);
469 mlx5e_rep_neigh_update_init_interval(rpriv);
470
471 rpriv->neigh_update.netevent_nb.notifier_call = mlx5e_rep_netevent_event;
472 err = register_netevent_notifier(&rpriv->neigh_update.netevent_nb);
473 if (err)
474 goto out_err;
475 return 0;
476
477 out_err:
478 rhashtable_destroy(&neigh_update->neigh_ht);
479 return err;
480 }
481
482 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv *rpriv)
483 {
484 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
485 struct mlx5e_priv *priv = netdev_priv(rpriv->rep->netdev);
486
487 unregister_netevent_notifier(&neigh_update->netevent_nb);
488
489 flush_workqueue(priv->wq); /* flush neigh update works */
490
491 cancel_delayed_work_sync(&rpriv->neigh_update.neigh_stats_work);
492
493 rhashtable_destroy(&neigh_update->neigh_ht);
494 }
495
496 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv *priv,
497 struct mlx5e_neigh_hash_entry *nhe)
498 {
499 struct mlx5e_rep_priv *rpriv = priv->ppriv;
500 int err;
501
502 err = rhashtable_insert_fast(&rpriv->neigh_update.neigh_ht,
503 &nhe->rhash_node,
504 mlx5e_neigh_ht_params);
505 if (err)
506 return err;
507
508 list_add(&nhe->neigh_list, &rpriv->neigh_update.neigh_list);
509
510 return err;
511 }
512
513 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_priv *priv,
514 struct mlx5e_neigh_hash_entry *nhe)
515 {
516 struct mlx5e_rep_priv *rpriv = priv->ppriv;
517
518 spin_lock_bh(&rpriv->neigh_update.encap_lock);
519
520 list_del(&nhe->neigh_list);
521
522 rhashtable_remove_fast(&rpriv->neigh_update.neigh_ht,
523 &nhe->rhash_node,
524 mlx5e_neigh_ht_params);
525 spin_unlock_bh(&rpriv->neigh_update.encap_lock);
526 }
527
528 /* This function must only be called under RTNL lock or under the
529 * representor's encap_lock in case RTNL mutex can't be held.
530 */
531 static struct mlx5e_neigh_hash_entry *
532 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
533 struct mlx5e_neigh *m_neigh)
534 {
535 struct mlx5e_rep_priv *rpriv = priv->ppriv;
536 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
537
538 return rhashtable_lookup_fast(&neigh_update->neigh_ht, m_neigh,
539 mlx5e_neigh_ht_params);
540 }
541
542 static int mlx5e_rep_neigh_entry_create(struct mlx5e_priv *priv,
543 struct mlx5e_encap_entry *e,
544 struct mlx5e_neigh_hash_entry **nhe)
545 {
546 int err;
547
548 *nhe = kzalloc(sizeof(**nhe), GFP_KERNEL);
549 if (!*nhe)
550 return -ENOMEM;
551
552 memcpy(&(*nhe)->m_neigh, &e->m_neigh, sizeof(e->m_neigh));
553 INIT_WORK(&(*nhe)->neigh_update_work, mlx5e_rep_neigh_update);
554 INIT_LIST_HEAD(&(*nhe)->encap_list);
555 refcount_set(&(*nhe)->refcnt, 1);
556
557 err = mlx5e_rep_neigh_entry_insert(priv, *nhe);
558 if (err)
559 goto out_free;
560 return 0;
561
562 out_free:
563 kfree(*nhe);
564 return err;
565 }
566
567 static void mlx5e_rep_neigh_entry_destroy(struct mlx5e_priv *priv,
568 struct mlx5e_neigh_hash_entry *nhe)
569 {
570 /* The neigh hash entry must be removed from the hash table regardless
571 * of the reference count value, so it won't be found by the next
572 * neigh notification call. The neigh hash entry reference count is
573 * incremented only during creation and neigh notification calls and
574 * protects from freeing the nhe struct.
575 */
576 mlx5e_rep_neigh_entry_remove(priv, nhe);
577 mlx5e_rep_neigh_entry_release(nhe);
578 }
579
580 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
581 struct mlx5e_encap_entry *e)
582 {
583 struct mlx5e_neigh_hash_entry *nhe;
584 int err;
585
586 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
587 if (!nhe) {
588 err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
589 if (err)
590 return err;
591 }
592 list_add(&e->encap_list, &nhe->encap_list);
593 return 0;
594 }
595
596 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
597 struct mlx5e_encap_entry *e)
598 {
599 struct mlx5e_neigh_hash_entry *nhe;
600
601 list_del(&e->encap_list);
602 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
603
604 if (list_empty(&nhe->encap_list))
605 mlx5e_rep_neigh_entry_destroy(priv, nhe);
606 }
607
608 static int mlx5e_rep_open(struct net_device *dev)
609 {
610 struct mlx5e_priv *priv = netdev_priv(dev);
611 struct mlx5e_rep_priv *rpriv = priv->ppriv;
612 struct mlx5_eswitch_rep *rep = rpriv->rep;
613 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
614 int err;
615
616 err = mlx5e_open(dev);
617 if (err)
618 return err;
619
620 err = mlx5_eswitch_set_vport_state(esw, rep->vport, MLX5_ESW_VPORT_ADMIN_STATE_UP);
621 if (!err)
622 netif_carrier_on(dev);
623
624 return 0;
625 }
626
627 static int mlx5e_rep_close(struct net_device *dev)
628 {
629 struct mlx5e_priv *priv = netdev_priv(dev);
630 struct mlx5e_rep_priv *rpriv = priv->ppriv;
631 struct mlx5_eswitch_rep *rep = rpriv->rep;
632 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
633
634 (void)mlx5_eswitch_set_vport_state(esw, rep->vport, MLX5_ESW_VPORT_ADMIN_STATE_DOWN);
635
636 return mlx5e_close(dev);
637 }
638
639 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
640 char *buf, size_t len)
641 {
642 struct mlx5e_priv *priv = netdev_priv(dev);
643 struct mlx5e_rep_priv *rpriv = priv->ppriv;
644 struct mlx5_eswitch_rep *rep = rpriv->rep;
645 int ret;
646
647 ret = snprintf(buf, len, "%d", rep->vport - 1);
648 if (ret >= len)
649 return -EOPNOTSUPP;
650
651 return 0;
652 }
653
654 static int mlx5e_rep_ndo_setup_tc(struct net_device *dev, u32 handle,
655 __be16 proto, struct tc_to_netdev *tc)
656 {
657 struct mlx5e_priv *priv = netdev_priv(dev);
658
659 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
660 return -EOPNOTSUPP;
661
662 if (tc->egress_dev) {
663 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
664 struct net_device *uplink_dev = mlx5_eswitch_get_uplink_netdev(esw);
665
666 return uplink_dev->netdev_ops->ndo_setup_tc(uplink_dev, handle,
667 proto, tc);
668 }
669
670 switch (tc->type) {
671 case TC_SETUP_CLSFLOWER:
672 switch (tc->cls_flower->command) {
673 case TC_CLSFLOWER_REPLACE:
674 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
675 case TC_CLSFLOWER_DESTROY:
676 return mlx5e_delete_flower(priv, tc->cls_flower);
677 case TC_CLSFLOWER_STATS:
678 return mlx5e_stats_flower(priv, tc->cls_flower);
679 }
680 default:
681 return -EOPNOTSUPP;
682 }
683 }
684
685 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
686 {
687 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
688 struct mlx5e_rep_priv *rpriv = priv->ppriv;
689 struct mlx5_eswitch_rep *rep;
690
691 if (!MLX5_CAP_GEN(priv->mdev, vport_group_manager))
692 return false;
693
694 rep = rpriv->rep;
695 if (esw->mode == SRIOV_OFFLOADS &&
696 rep && rep->vport == FDB_UPLINK_VPORT)
697 return true;
698
699 return false;
700 }
701
702 static bool mlx5e_is_vf_vport_rep(struct mlx5e_priv *priv)
703 {
704 struct mlx5e_rep_priv *rpriv = priv->ppriv;
705 struct mlx5_eswitch_rep *rep = rpriv->rep;
706
707 if (rep && rep->vport != FDB_UPLINK_VPORT)
708 return true;
709
710 return false;
711 }
712
713 bool mlx5e_has_offload_stats(const struct net_device *dev, int attr_id)
714 {
715 struct mlx5e_priv *priv = netdev_priv(dev);
716
717 switch (attr_id) {
718 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
719 if (mlx5e_is_vf_vport_rep(priv) || mlx5e_is_uplink_rep(priv))
720 return true;
721 }
722
723 return false;
724 }
725
726 static int
727 mlx5e_get_sw_stats64(const struct net_device *dev,
728 struct rtnl_link_stats64 *stats)
729 {
730 struct mlx5e_priv *priv = netdev_priv(dev);
731 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
732
733 stats->rx_packets = sstats->rx_packets;
734 stats->rx_bytes = sstats->rx_bytes;
735 stats->tx_packets = sstats->tx_packets;
736 stats->tx_bytes = sstats->tx_bytes;
737
738 stats->tx_dropped = sstats->tx_queue_dropped;
739
740 return 0;
741 }
742
743 int mlx5e_get_offload_stats(int attr_id, const struct net_device *dev,
744 void *sp)
745 {
746 switch (attr_id) {
747 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
748 return mlx5e_get_sw_stats64(dev, sp);
749 }
750
751 return -EINVAL;
752 }
753
754 static void
755 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
756 {
757 struct mlx5e_priv *priv = netdev_priv(dev);
758
759 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
760 }
761
762 static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
763 .switchdev_port_attr_get = mlx5e_attr_get,
764 };
765
766 static const struct net_device_ops mlx5e_netdev_ops_rep = {
767 .ndo_open = mlx5e_rep_open,
768 .ndo_stop = mlx5e_rep_close,
769 .ndo_start_xmit = mlx5e_xmit,
770 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
771 .ndo_setup_tc = mlx5e_rep_ndo_setup_tc,
772 .ndo_get_stats64 = mlx5e_rep_get_stats,
773 .ndo_has_offload_stats = mlx5e_has_offload_stats,
774 .ndo_get_offload_stats = mlx5e_get_offload_stats,
775 };
776
777 static void mlx5e_build_rep_params(struct mlx5_core_dev *mdev,
778 struct mlx5e_params *params)
779 {
780 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
781 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
782 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
783
784 params->log_sq_size = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
785 params->rq_wq_type = MLX5_WQ_TYPE_LINKED_LIST;
786 params->log_rq_size = MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE;
787
788 params->rx_am_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
789 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
790
791 params->tx_max_inline = mlx5e_get_max_inline_cap(mdev);
792 params->num_tc = 1;
793 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
794
795 mlx5_query_min_inline(mdev, &params->tx_min_inline_mode);
796 }
797
798 static void mlx5e_build_rep_netdev(struct net_device *netdev)
799 {
800 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
801
802 netdev->watchdog_timeo = 15 * HZ;
803
804 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
805
806 #ifdef CONFIG_NET_SWITCHDEV
807 netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
808 #endif
809
810 netdev->features |= NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
811 netdev->hw_features |= NETIF_F_HW_TC;
812
813 eth_hw_addr_random(netdev);
814 }
815
816 static void mlx5e_init_rep(struct mlx5_core_dev *mdev,
817 struct net_device *netdev,
818 const struct mlx5e_profile *profile,
819 void *ppriv)
820 {
821 struct mlx5e_priv *priv = netdev_priv(netdev);
822
823 priv->mdev = mdev;
824 priv->netdev = netdev;
825 priv->profile = profile;
826 priv->ppriv = ppriv;
827
828 mutex_init(&priv->state_lock);
829
830 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
831
832 priv->channels.params.num_channels = profile->max_nch(mdev);
833 mlx5e_build_rep_params(mdev, &priv->channels.params);
834 mlx5e_build_rep_netdev(netdev);
835 }
836
837 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
838 {
839 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
840 struct mlx5e_rep_priv *rpriv = priv->ppriv;
841 struct mlx5_eswitch_rep *rep = rpriv->rep;
842 struct mlx5_flow_handle *flow_rule;
843 int err;
844
845 mlx5e_init_l2_addr(priv);
846
847 err = mlx5e_create_direct_rqts(priv);
848 if (err)
849 return err;
850
851 err = mlx5e_create_direct_tirs(priv);
852 if (err)
853 goto err_destroy_direct_rqts;
854
855 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
856 rep->vport,
857 priv->direct_tir[0].tirn);
858 if (IS_ERR(flow_rule)) {
859 err = PTR_ERR(flow_rule);
860 goto err_destroy_direct_tirs;
861 }
862 rep->vport_rx_rule = flow_rule;
863
864 err = mlx5e_tc_init(priv);
865 if (err)
866 goto err_del_flow_rule;
867
868 return 0;
869
870 err_del_flow_rule:
871 mlx5_del_flow_rules(rep->vport_rx_rule);
872 err_destroy_direct_tirs:
873 mlx5e_destroy_direct_tirs(priv);
874 err_destroy_direct_rqts:
875 mlx5e_destroy_direct_rqts(priv);
876 return err;
877 }
878
879 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
880 {
881 struct mlx5e_rep_priv *rpriv = priv->ppriv;
882 struct mlx5_eswitch_rep *rep = rpriv->rep;
883
884 mlx5e_tc_cleanup(priv);
885 mlx5_del_flow_rules(rep->vport_rx_rule);
886 mlx5e_destroy_direct_tirs(priv);
887 mlx5e_destroy_direct_rqts(priv);
888 }
889
890 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
891 {
892 int err;
893
894 err = mlx5e_create_tises(priv);
895 if (err) {
896 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
897 return err;
898 }
899 return 0;
900 }
901
902 static int mlx5e_get_rep_max_num_channels(struct mlx5_core_dev *mdev)
903 {
904 #define MLX5E_PORT_REPRESENTOR_NCH 1
905 return MLX5E_PORT_REPRESENTOR_NCH;
906 }
907
908 static struct mlx5e_profile mlx5e_rep_profile = {
909 .init = mlx5e_init_rep,
910 .init_rx = mlx5e_init_rep_rx,
911 .cleanup_rx = mlx5e_cleanup_rep_rx,
912 .init_tx = mlx5e_init_rep_tx,
913 .cleanup_tx = mlx5e_cleanup_nic_tx,
914 .update_stats = mlx5e_rep_update_stats,
915 .max_nch = mlx5e_get_rep_max_num_channels,
916 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
917 .rx_handlers.handle_rx_cqe_mpwqe = NULL /* Not supported */,
918 .max_tc = 1,
919 };
920
921 /* e-Switch vport representors */
922
923 static int
924 mlx5e_nic_rep_load(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep)
925 {
926 struct mlx5e_priv *priv = netdev_priv(rep->netdev);
927 struct mlx5e_rep_priv *rpriv = priv->ppriv;
928
929 int err;
930
931 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
932 err = mlx5e_add_sqs_fwd_rules(priv);
933 if (err)
934 return err;
935 }
936
937 err = mlx5e_rep_neigh_init(rpriv);
938 if (err)
939 goto err_remove_sqs;
940
941 return 0;
942
943 err_remove_sqs:
944 mlx5e_remove_sqs_fwd_rules(priv);
945 return err;
946 }
947
948 static void
949 mlx5e_nic_rep_unload(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep)
950 {
951 struct mlx5e_priv *priv = netdev_priv(rep->netdev);
952 struct mlx5e_rep_priv *rpriv = priv->ppriv;
953
954 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
955 mlx5e_remove_sqs_fwd_rules(priv);
956
957 /* clean (and re-init) existing uplink offloaded TC rules */
958 mlx5e_tc_cleanup(priv);
959 mlx5e_tc_init(priv);
960
961 mlx5e_rep_neigh_cleanup(rpriv);
962 }
963
964 static int
965 mlx5e_vport_rep_load(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep)
966 {
967 struct mlx5e_rep_priv *rpriv;
968 struct net_device *netdev;
969 int err;
970
971 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
972 if (!rpriv)
973 return -ENOMEM;
974
975 netdev = mlx5e_create_netdev(esw->dev, &mlx5e_rep_profile, rpriv);
976 if (!netdev) {
977 pr_warn("Failed to create representor netdev for vport %d\n",
978 rep->vport);
979 kfree(rpriv);
980 return -EINVAL;
981 }
982
983 rep->netdev = netdev;
984 rpriv->rep = rep;
985
986 err = mlx5e_attach_netdev(netdev_priv(netdev));
987 if (err) {
988 pr_warn("Failed to attach representor netdev for vport %d\n",
989 rep->vport);
990 goto err_destroy_netdev;
991 }
992
993 err = mlx5e_rep_neigh_init(rpriv);
994 if (err) {
995 pr_warn("Failed to initialized neighbours handling for vport %d\n",
996 rep->vport);
997 goto err_detach_netdev;
998 }
999
1000 err = register_netdev(netdev);
1001 if (err) {
1002 pr_warn("Failed to register representor netdev for vport %d\n",
1003 rep->vport);
1004 goto err_neigh_cleanup;
1005 }
1006
1007 return 0;
1008
1009 err_neigh_cleanup:
1010 mlx5e_rep_neigh_cleanup(rpriv);
1011
1012 err_detach_netdev:
1013 mlx5e_detach_netdev(netdev_priv(netdev));
1014
1015 err_destroy_netdev:
1016 mlx5e_destroy_netdev(netdev_priv(netdev));
1017 kfree(rpriv);
1018 return err;
1019
1020 }
1021
1022 static void
1023 mlx5e_vport_rep_unload(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep)
1024 {
1025 struct net_device *netdev = rep->netdev;
1026 struct mlx5e_priv *priv = netdev_priv(netdev);
1027 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1028 void *ppriv = priv->ppriv;
1029
1030 unregister_netdev(rep->netdev);
1031
1032 mlx5e_rep_neigh_cleanup(rpriv);
1033 mlx5e_detach_netdev(priv);
1034 mlx5e_destroy_netdev(priv);
1035 kfree(ppriv); /* mlx5e_rep_priv */
1036 }
1037
1038 static void mlx5e_rep_register_vf_vports(struct mlx5e_priv *priv)
1039 {
1040 struct mlx5_core_dev *mdev = priv->mdev;
1041 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1042 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1043 int vport;
1044 u8 mac[ETH_ALEN];
1045
1046 mlx5_query_nic_vport_mac_address(mdev, 0, mac);
1047
1048 for (vport = 1; vport < total_vfs; vport++) {
1049 struct mlx5_eswitch_rep rep;
1050
1051 rep.load = mlx5e_vport_rep_load;
1052 rep.unload = mlx5e_vport_rep_unload;
1053 rep.vport = vport;
1054 ether_addr_copy(rep.hw_id, mac);
1055 mlx5_eswitch_register_vport_rep(esw, vport, &rep);
1056 }
1057 }
1058
1059 static void mlx5e_rep_unregister_vf_vports(struct mlx5e_priv *priv)
1060 {
1061 struct mlx5_core_dev *mdev = priv->mdev;
1062 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1063 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1064 int vport;
1065
1066 for (vport = 1; vport < total_vfs; vport++)
1067 mlx5_eswitch_unregister_vport_rep(esw, vport);
1068 }
1069
1070 void mlx5e_register_vport_reps(struct mlx5e_priv *priv)
1071 {
1072 struct mlx5_core_dev *mdev = priv->mdev;
1073 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1074 struct mlx5_eswitch_rep rep;
1075
1076 mlx5_query_nic_vport_mac_address(mdev, 0, rep.hw_id);
1077 rep.load = mlx5e_nic_rep_load;
1078 rep.unload = mlx5e_nic_rep_unload;
1079 rep.vport = FDB_UPLINK_VPORT;
1080 rep.netdev = priv->netdev;
1081 mlx5_eswitch_register_vport_rep(esw, 0, &rep); /* UPLINK PF vport*/
1082
1083 mlx5e_rep_register_vf_vports(priv); /* VFs vports */
1084 }
1085
1086 void mlx5e_unregister_vport_reps(struct mlx5e_priv *priv)
1087 {
1088 struct mlx5_core_dev *mdev = priv->mdev;
1089 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1090
1091 mlx5e_rep_unregister_vf_vports(priv); /* VFs vports */
1092 mlx5_eswitch_unregister_vport_rep(esw, 0); /* UPLINK PF*/
1093 }