OSDN Git Service

net: sched: use newly added classid identity helpers
[uclinux-h8/linux.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
655 mlx5e_rep_setup_tc_cls_flower(struct net_device *dev,
656                               struct tc_cls_flower_offload *cls_flower)
657 {
658         struct mlx5e_priv *priv = netdev_priv(dev);
659
660         if (is_classid_clsact_ingress(cls_flower->common.classid) ||
661             cls_flower->common.chain_index)
662                 return -EOPNOTSUPP;
663
664         if (cls_flower->egress_dev) {
665                 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
666
667                 dev = mlx5_eswitch_get_uplink_netdev(esw);
668                 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSFLOWER,
669                                                      cls_flower);
670         }
671
672         switch (cls_flower->command) {
673         case TC_CLSFLOWER_REPLACE:
674                 return mlx5e_configure_flower(priv, cls_flower);
675         case TC_CLSFLOWER_DESTROY:
676                 return mlx5e_delete_flower(priv, cls_flower);
677         case TC_CLSFLOWER_STATS:
678                 return mlx5e_stats_flower(priv, cls_flower);
679         default:
680                 return -EOPNOTSUPP;
681         }
682 }
683
684 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
685                               void *type_data)
686 {
687         switch (type) {
688         case TC_SETUP_CLSFLOWER:
689                 return mlx5e_rep_setup_tc_cls_flower(dev, type_data);
690         default:
691                 return -EOPNOTSUPP;
692         }
693 }
694
695 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
696 {
697         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
698         struct mlx5e_rep_priv *rpriv = priv->ppriv;
699         struct mlx5_eswitch_rep *rep;
700
701         if (!MLX5_CAP_GEN(priv->mdev, vport_group_manager))
702                 return false;
703
704         rep = rpriv->rep;
705         if (esw->mode == SRIOV_OFFLOADS &&
706             rep && rep->vport == FDB_UPLINK_VPORT)
707                 return true;
708
709         return false;
710 }
711
712 static bool mlx5e_is_vf_vport_rep(struct mlx5e_priv *priv)
713 {
714         struct mlx5e_rep_priv *rpriv = priv->ppriv;
715         struct mlx5_eswitch_rep *rep = rpriv->rep;
716
717         if (rep && rep->vport != FDB_UPLINK_VPORT)
718                 return true;
719
720         return false;
721 }
722
723 bool mlx5e_has_offload_stats(const struct net_device *dev, int attr_id)
724 {
725         struct mlx5e_priv *priv = netdev_priv(dev);
726
727         switch (attr_id) {
728         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
729                 if (mlx5e_is_vf_vport_rep(priv) || mlx5e_is_uplink_rep(priv))
730                         return true;
731         }
732
733         return false;
734 }
735
736 static int
737 mlx5e_get_sw_stats64(const struct net_device *dev,
738                      struct rtnl_link_stats64 *stats)
739 {
740         struct mlx5e_priv *priv = netdev_priv(dev);
741         struct mlx5e_sw_stats *sstats = &priv->stats.sw;
742
743         stats->rx_packets = sstats->rx_packets;
744         stats->rx_bytes   = sstats->rx_bytes;
745         stats->tx_packets = sstats->tx_packets;
746         stats->tx_bytes   = sstats->tx_bytes;
747
748         stats->tx_dropped = sstats->tx_queue_dropped;
749
750         return 0;
751 }
752
753 int mlx5e_get_offload_stats(int attr_id, const struct net_device *dev,
754                             void *sp)
755 {
756         switch (attr_id) {
757         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
758                 return mlx5e_get_sw_stats64(dev, sp);
759         }
760
761         return -EINVAL;
762 }
763
764 static void
765 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
766 {
767         struct mlx5e_priv *priv = netdev_priv(dev);
768
769         memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
770 }
771
772 static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
773         .switchdev_port_attr_get        = mlx5e_attr_get,
774 };
775
776 static const struct net_device_ops mlx5e_netdev_ops_rep = {
777         .ndo_open                = mlx5e_rep_open,
778         .ndo_stop                = mlx5e_rep_close,
779         .ndo_start_xmit          = mlx5e_xmit,
780         .ndo_get_phys_port_name  = mlx5e_rep_get_phys_port_name,
781         .ndo_setup_tc            = mlx5e_rep_setup_tc,
782         .ndo_get_stats64         = mlx5e_rep_get_stats,
783         .ndo_has_offload_stats   = mlx5e_has_offload_stats,
784         .ndo_get_offload_stats   = mlx5e_get_offload_stats,
785 };
786
787 static void mlx5e_build_rep_params(struct mlx5_core_dev *mdev,
788                                    struct mlx5e_params *params)
789 {
790         u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
791                                          MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
792                                          MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
793
794         params->log_sq_size = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
795         params->rq_wq_type  = MLX5_WQ_TYPE_LINKED_LIST;
796         params->log_rq_size = MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE;
797
798         params->rx_am_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
799         mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
800
801         params->tx_max_inline         = mlx5e_get_max_inline_cap(mdev);
802         params->num_tc                = 1;
803         params->lro_wqe_sz            = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
804
805         mlx5_query_min_inline(mdev, &params->tx_min_inline_mode);
806 }
807
808 static void mlx5e_build_rep_netdev(struct net_device *netdev)
809 {
810         netdev->netdev_ops = &mlx5e_netdev_ops_rep;
811
812         netdev->watchdog_timeo    = 15 * HZ;
813
814         netdev->ethtool_ops       = &mlx5e_rep_ethtool_ops;
815
816 #ifdef CONFIG_NET_SWITCHDEV
817         netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
818 #endif
819
820         netdev->features         |= NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
821         netdev->hw_features      |= NETIF_F_HW_TC;
822
823         eth_hw_addr_random(netdev);
824 }
825
826 static void mlx5e_init_rep(struct mlx5_core_dev *mdev,
827                            struct net_device *netdev,
828                            const struct mlx5e_profile *profile,
829                            void *ppriv)
830 {
831         struct mlx5e_priv *priv = netdev_priv(netdev);
832
833         priv->mdev                         = mdev;
834         priv->netdev                       = netdev;
835         priv->profile                      = profile;
836         priv->ppriv                        = ppriv;
837
838         mutex_init(&priv->state_lock);
839
840         INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
841
842         priv->channels.params.num_channels = profile->max_nch(mdev);
843
844         priv->hard_mtu = MLX5E_ETH_HARD_MTU;
845
846         mlx5e_build_rep_params(mdev, &priv->channels.params);
847         mlx5e_build_rep_netdev(netdev);
848 }
849
850 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
851 {
852         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
853         struct mlx5e_rep_priv *rpriv = priv->ppriv;
854         struct mlx5_eswitch_rep *rep = rpriv->rep;
855         struct mlx5_flow_handle *flow_rule;
856         int err;
857
858         mlx5e_init_l2_addr(priv);
859
860         err = mlx5e_create_direct_rqts(priv);
861         if (err)
862                 return err;
863
864         err = mlx5e_create_direct_tirs(priv);
865         if (err)
866                 goto err_destroy_direct_rqts;
867
868         flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
869                                                       rep->vport,
870                                                       priv->direct_tir[0].tirn);
871         if (IS_ERR(flow_rule)) {
872                 err = PTR_ERR(flow_rule);
873                 goto err_destroy_direct_tirs;
874         }
875         rep->vport_rx_rule = flow_rule;
876
877         err = mlx5e_tc_init(priv);
878         if (err)
879                 goto err_del_flow_rule;
880
881         return 0;
882
883 err_del_flow_rule:
884         mlx5_del_flow_rules(rep->vport_rx_rule);
885 err_destroy_direct_tirs:
886         mlx5e_destroy_direct_tirs(priv);
887 err_destroy_direct_rqts:
888         mlx5e_destroy_direct_rqts(priv);
889         return err;
890 }
891
892 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
893 {
894         struct mlx5e_rep_priv *rpriv = priv->ppriv;
895         struct mlx5_eswitch_rep *rep = rpriv->rep;
896
897         mlx5e_tc_cleanup(priv);
898         mlx5_del_flow_rules(rep->vport_rx_rule);
899         mlx5e_destroy_direct_tirs(priv);
900         mlx5e_destroy_direct_rqts(priv);
901 }
902
903 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
904 {
905         int err;
906
907         err = mlx5e_create_tises(priv);
908         if (err) {
909                 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
910                 return err;
911         }
912         return 0;
913 }
914
915 static int mlx5e_get_rep_max_num_channels(struct mlx5_core_dev *mdev)
916 {
917 #define MLX5E_PORT_REPRESENTOR_NCH 1
918         return MLX5E_PORT_REPRESENTOR_NCH;
919 }
920
921 static struct mlx5e_profile mlx5e_rep_profile = {
922         .init                   = mlx5e_init_rep,
923         .init_rx                = mlx5e_init_rep_rx,
924         .cleanup_rx             = mlx5e_cleanup_rep_rx,
925         .init_tx                = mlx5e_init_rep_tx,
926         .cleanup_tx             = mlx5e_cleanup_nic_tx,
927         .update_stats           = mlx5e_rep_update_stats,
928         .max_nch                = mlx5e_get_rep_max_num_channels,
929         .update_carrier         = NULL,
930         .rx_handlers.handle_rx_cqe       = mlx5e_handle_rx_cqe_rep,
931         .rx_handlers.handle_rx_cqe_mpwqe = NULL /* Not supported */,
932         .max_tc                 = 1,
933 };
934
935 /* e-Switch vport representors */
936
937 static int
938 mlx5e_nic_rep_load(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep)
939 {
940         struct mlx5e_priv *priv = netdev_priv(rep->netdev);
941         struct mlx5e_rep_priv *rpriv = priv->ppriv;
942
943         int err;
944
945         if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
946                 err = mlx5e_add_sqs_fwd_rules(priv);
947                 if (err)
948                         return err;
949         }
950
951         err = mlx5e_rep_neigh_init(rpriv);
952         if (err)
953                 goto err_remove_sqs;
954
955         return 0;
956
957 err_remove_sqs:
958         mlx5e_remove_sqs_fwd_rules(priv);
959         return err;
960 }
961
962 static void
963 mlx5e_nic_rep_unload(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep)
964 {
965         struct mlx5e_priv *priv = netdev_priv(rep->netdev);
966         struct mlx5e_rep_priv *rpriv = priv->ppriv;
967
968         if (test_bit(MLX5E_STATE_OPENED, &priv->state))
969                 mlx5e_remove_sqs_fwd_rules(priv);
970
971         /* clean (and re-init) existing uplink offloaded TC rules */
972         mlx5e_tc_cleanup(priv);
973         mlx5e_tc_init(priv);
974
975         mlx5e_rep_neigh_cleanup(rpriv);
976 }
977
978 static int
979 mlx5e_vport_rep_load(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep)
980 {
981         struct mlx5e_rep_priv *rpriv;
982         struct net_device *netdev;
983         int err;
984
985         rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
986         if (!rpriv)
987                 return -ENOMEM;
988
989         netdev = mlx5e_create_netdev(esw->dev, &mlx5e_rep_profile, rpriv);
990         if (!netdev) {
991                 pr_warn("Failed to create representor netdev for vport %d\n",
992                         rep->vport);
993                 kfree(rpriv);
994                 return -EINVAL;
995         }
996
997         rep->netdev = netdev;
998         rpriv->rep = rep;
999
1000         err = mlx5e_attach_netdev(netdev_priv(netdev));
1001         if (err) {
1002                 pr_warn("Failed to attach representor netdev for vport %d\n",
1003                         rep->vport);
1004                 goto err_destroy_netdev;
1005         }
1006
1007         err = mlx5e_rep_neigh_init(rpriv);
1008         if (err) {
1009                 pr_warn("Failed to initialized neighbours handling for vport %d\n",
1010                         rep->vport);
1011                 goto err_detach_netdev;
1012         }
1013
1014         err = register_netdev(netdev);
1015         if (err) {
1016                 pr_warn("Failed to register representor netdev for vport %d\n",
1017                         rep->vport);
1018                 goto err_neigh_cleanup;
1019         }
1020
1021         return 0;
1022
1023 err_neigh_cleanup:
1024         mlx5e_rep_neigh_cleanup(rpriv);
1025
1026 err_detach_netdev:
1027         mlx5e_detach_netdev(netdev_priv(netdev));
1028
1029 err_destroy_netdev:
1030         mlx5e_destroy_netdev(netdev_priv(netdev));
1031         kfree(rpriv);
1032         return err;
1033 }
1034
1035 static void
1036 mlx5e_vport_rep_unload(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep)
1037 {
1038         struct net_device *netdev = rep->netdev;
1039         struct mlx5e_priv *priv = netdev_priv(netdev);
1040         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1041         void *ppriv = priv->ppriv;
1042
1043         unregister_netdev(rep->netdev);
1044
1045         mlx5e_rep_neigh_cleanup(rpriv);
1046         mlx5e_detach_netdev(priv);
1047         mlx5e_destroy_netdev(priv);
1048         kfree(ppriv); /* mlx5e_rep_priv */
1049 }
1050
1051 static void mlx5e_rep_register_vf_vports(struct mlx5e_priv *priv)
1052 {
1053         struct mlx5_core_dev *mdev = priv->mdev;
1054         struct mlx5_eswitch *esw   = mdev->priv.eswitch;
1055         int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1056         int vport;
1057         u8 mac[ETH_ALEN];
1058
1059         mlx5_query_nic_vport_mac_address(mdev, 0, mac);
1060
1061         for (vport = 1; vport < total_vfs; vport++) {
1062                 struct mlx5_eswitch_rep rep;
1063
1064                 rep.load = mlx5e_vport_rep_load;
1065                 rep.unload = mlx5e_vport_rep_unload;
1066                 rep.vport = vport;
1067                 ether_addr_copy(rep.hw_id, mac);
1068                 mlx5_eswitch_register_vport_rep(esw, vport, &rep);
1069         }
1070 }
1071
1072 static void mlx5e_rep_unregister_vf_vports(struct mlx5e_priv *priv)
1073 {
1074         struct mlx5_core_dev *mdev = priv->mdev;
1075         struct mlx5_eswitch *esw = mdev->priv.eswitch;
1076         int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1077         int vport;
1078
1079         for (vport = 1; vport < total_vfs; vport++)
1080                 mlx5_eswitch_unregister_vport_rep(esw, vport);
1081 }
1082
1083 void mlx5e_register_vport_reps(struct mlx5e_priv *priv)
1084 {
1085         struct mlx5_core_dev *mdev = priv->mdev;
1086         struct mlx5_eswitch *esw   = mdev->priv.eswitch;
1087         struct mlx5_eswitch_rep rep;
1088
1089         mlx5_query_nic_vport_mac_address(mdev, 0, rep.hw_id);
1090         rep.load = mlx5e_nic_rep_load;
1091         rep.unload = mlx5e_nic_rep_unload;
1092         rep.vport = FDB_UPLINK_VPORT;
1093         rep.netdev = priv->netdev;
1094         mlx5_eswitch_register_vport_rep(esw, 0, &rep); /* UPLINK PF vport*/
1095
1096         mlx5e_rep_register_vf_vports(priv); /* VFs vports */
1097 }
1098
1099 void mlx5e_unregister_vport_reps(struct mlx5e_priv *priv)
1100 {
1101         struct mlx5_core_dev *mdev = priv->mdev;
1102         struct mlx5_eswitch *esw   = mdev->priv.eswitch;
1103
1104         mlx5e_rep_unregister_vf_vports(priv); /* VFs vports */
1105         mlx5_eswitch_unregister_vport_rep(esw, 0); /* UPLINK PF*/
1106 }
1107
1108 void *mlx5e_alloc_nic_rep_priv(struct mlx5_core_dev *mdev)
1109 {
1110         struct mlx5_eswitch *esw = mdev->priv.eswitch;
1111         struct mlx5e_rep_priv *rpriv;
1112
1113         rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1114         if (!rpriv)
1115                 return NULL;
1116
1117         rpriv->rep = &esw->offloads.vport_reps[0];
1118         return rpriv;
1119 }