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>
37 #include <net/netevent.h>
46 static const char mlx5e_rep_driver_name
[] = "mlx5e_rep";
48 static void mlx5e_rep_get_drvinfo(struct net_device
*dev
,
49 struct ethtool_drvinfo
*drvinfo
)
51 strlcpy(drvinfo
->driver
, mlx5e_rep_driver_name
,
52 sizeof(drvinfo
->driver
));
53 strlcpy(drvinfo
->version
, UTS_RELEASE
, sizeof(drvinfo
->version
));
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
) },
63 #define NUM_VPORT_REP_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
65 static void mlx5e_rep_get_strings(struct net_device
*dev
,
66 u32 stringset
, uint8_t *data
)
72 for (i
= 0; i
< NUM_VPORT_REP_COUNTERS
; i
++)
73 strcpy(data
+ (i
* ETH_GSTRING_LEN
),
74 sw_rep_stats_desc
[i
].format
);
79 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv
*priv
)
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
;
88 err
= mlx5_eswitch_get_vport_stats(esw
, rep
->vport
, &vf_stats
);
90 pr_warn("vport %d error %d reading stats\n", rep
->vport
, err
);
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
;
102 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv
*priv
)
104 struct mlx5e_sw_stats
*s
= &priv
->stats
.sw
;
105 struct mlx5e_rq_stats
*rq_stats
;
106 struct mlx5e_sq_stats
*sq_stats
;
109 memset(s
, 0, sizeof(*s
));
110 for (i
= 0; i
< priv
->channels
.num
; i
++) {
111 struct mlx5e_channel
*c
= priv
->channels
.c
[i
];
113 rq_stats
= &c
->rq
.stats
;
115 s
->rx_packets
+= rq_stats
->packets
;
116 s
->rx_bytes
+= rq_stats
->bytes
;
118 for (j
= 0; j
< priv
->channels
.params
.num_tc
; j
++) {
119 sq_stats
= &c
->sq
[j
].stats
;
121 s
->tx_packets
+= sq_stats
->packets
;
122 s
->tx_bytes
+= sq_stats
->bytes
;
127 static void mlx5e_rep_update_stats(struct mlx5e_priv
*priv
)
129 mlx5e_rep_update_sw_counters(priv
);
130 mlx5e_rep_update_hw_counters(priv
);
133 static void mlx5e_rep_get_ethtool_stats(struct net_device
*dev
,
134 struct ethtool_stats
*stats
, u64
*data
)
136 struct mlx5e_priv
*priv
= netdev_priv(dev
);
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
);
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
);
152 static int mlx5e_rep_get_sset_count(struct net_device
*dev
, int sset
)
156 return NUM_VPORT_REP_COUNTERS
;
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
,
170 int mlx5e_attr_get(struct net_device
*dev
, struct switchdev_attr
*attr
)
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
;
177 if (esw
->mode
== SRIOV_NONE
)
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
);
192 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv
*priv
)
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;
202 sqs
= kcalloc(priv
->channels
.num
* priv
->channels
.params
.num_tc
, sizeof(u16
), GFP_KERNEL
);
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
;
212 err
= mlx5_eswitch_sqs2vport_start(esw
, rep
, sqs
, num_sqs
);
217 netdev_warn(priv
->netdev
, "Failed to add SQs FWD rules %d\n", err
);
221 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv
*priv
)
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
;
227 mlx5_eswitch_sqs2vport_stop(esw
, rep
);
230 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv
*rpriv
)
232 #if IS_ENABLED(CONFIG_IPV6)
233 unsigned long ipv6_interval
= NEIGH_VAR(&ipv6_stub
->nd_tbl
->parms
,
236 unsigned long ipv6_interval
= ~0UL;
238 unsigned long ipv4_interval
= NEIGH_VAR(&arp_tbl
.parms
,
240 struct net_device
*netdev
= rpriv
->rep
->netdev
;
241 struct mlx5e_priv
*priv
= netdev_priv(netdev
);
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
);
247 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv
*priv
)
249 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
250 struct mlx5e_neigh_update_table
*neigh_update
= &rpriv
->neigh_update
;
252 mlx5_fc_queue_stats_work(priv
->mdev
,
253 &neigh_update
->neigh_stats_work
,
254 neigh_update
->min_interval
);
257 static void mlx5e_rep_neigh_stats_work(struct work_struct
*work
)
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
;
266 if (!list_empty(&rpriv
->neigh_update
.neigh_list
))
267 mlx5e_rep_queue_neigh_stats_work(priv
);
269 list_for_each_entry(nhe
, &rpriv
->neigh_update
.neigh_list
, neigh_list
)
270 mlx5e_tc_update_neigh_used_value(nhe
);
275 static void mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry
*nhe
)
277 refcount_inc(&nhe
->refcnt
);
280 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry
*nhe
)
282 if (refcount_dec_and_test(&nhe
->refcnt
))
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
])
291 struct ethhdr
*eth
= (struct ethhdr
*)e
->encap_header
;
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
);
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
);
303 mlx5e_tc_encap_flows_add(priv
, e
);
307 static void mlx5e_rep_neigh_update(struct work_struct
*work
)
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
;
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.
326 read_lock_bh(&n
->lock
);
327 memcpy(ha
, n
->ha
, ETH_ALEN
);
328 nud_state
= n
->nud_state
;
330 read_unlock_bh(&n
->lock
);
332 neigh_connected
= (nud_state
& NUD_VALID
) && !dead
;
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
);
338 if (encap_connected
!= neigh_connected
||
339 !ether_addr_equal(e
->h_dest
, ha
))
340 mlx5e_rep_update_flows(priv
, e
, neigh_connected
, ha
);
342 mlx5e_rep_neigh_entry_release(nhe
);
347 static struct mlx5e_neigh_hash_entry
*
348 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv
*priv
,
349 struct mlx5e_neigh
*m_neigh
);
351 static int mlx5e_rep_netevent_event(struct notifier_block
*nb
,
352 unsigned long event
, void *ptr
)
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
;
366 case NETEVENT_NEIGH_UPDATE
:
368 #if IS_ENABLED(CONFIG_IPV6)
369 if (n
->tbl
!= ipv6_stub
->nd_tbl
&& n
->tbl
!= &arp_tbl
)
371 if (n
->tbl
!= &arp_tbl
)
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
);
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.
383 spin_lock_bh(&neigh_update
->encap_lock
);
384 nhe
= mlx5e_rep_neigh_entry_lookup(priv
, &m_neigh
);
386 spin_unlock_bh(&neigh_update
->encap_lock
);
390 /* This assignment is valid as long as the the neigh reference
395 /* Take a reference to ensure the neighbour and mlx5 encap
396 * entry won't be destructed until we drop the reference in
400 mlx5e_rep_neigh_entry_hold(nhe
);
402 if (!queue_work(priv
->wq
, &nhe
->neigh_update_work
)) {
403 mlx5e_rep_neigh_entry_release(nhe
);
406 spin_unlock_bh(&neigh_update
->encap_lock
);
409 case NETEVENT_DELAY_PROBE_TIME_UPDATE
:
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.
416 #if IS_ENABLED(CONFIG_IPV6)
417 if (!p
->dev
|| (p
->tbl
!= ipv6_stub
->nd_tbl
&& p
->tbl
!= &arp_tbl
))
419 if (!p
->dev
|| p
->tbl
!= &arp_tbl
)
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.
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
) {
435 spin_unlock_bh(&neigh_update
->encap_lock
);
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
);
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,
456 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv
*rpriv
)
458 struct mlx5e_neigh_update_table
*neigh_update
= &rpriv
->neigh_update
;
461 err
= rhashtable_init(&neigh_update
->neigh_ht
, &mlx5e_neigh_ht_params
);
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
);
471 rpriv
->neigh_update
.netevent_nb
.notifier_call
= mlx5e_rep_netevent_event
;
472 err
= register_netevent_notifier(&rpriv
->neigh_update
.netevent_nb
);
478 rhashtable_destroy(&neigh_update
->neigh_ht
);
482 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv
*rpriv
)
484 struct mlx5e_neigh_update_table
*neigh_update
= &rpriv
->neigh_update
;
485 struct mlx5e_priv
*priv
= netdev_priv(rpriv
->rep
->netdev
);
487 unregister_netevent_notifier(&neigh_update
->netevent_nb
);
489 flush_workqueue(priv
->wq
); /* flush neigh update works */
491 cancel_delayed_work_sync(&rpriv
->neigh_update
.neigh_stats_work
);
493 rhashtable_destroy(&neigh_update
->neigh_ht
);
496 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv
*priv
,
497 struct mlx5e_neigh_hash_entry
*nhe
)
499 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
502 err
= rhashtable_insert_fast(&rpriv
->neigh_update
.neigh_ht
,
504 mlx5e_neigh_ht_params
);
508 list_add(&nhe
->neigh_list
, &rpriv
->neigh_update
.neigh_list
);
513 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_priv
*priv
,
514 struct mlx5e_neigh_hash_entry
*nhe
)
516 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
518 spin_lock_bh(&rpriv
->neigh_update
.encap_lock
);
520 list_del(&nhe
->neigh_list
);
522 rhashtable_remove_fast(&rpriv
->neigh_update
.neigh_ht
,
524 mlx5e_neigh_ht_params
);
525 spin_unlock_bh(&rpriv
->neigh_update
.encap_lock
);
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.
531 static struct mlx5e_neigh_hash_entry
*
532 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv
*priv
,
533 struct mlx5e_neigh
*m_neigh
)
535 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
536 struct mlx5e_neigh_update_table
*neigh_update
= &rpriv
->neigh_update
;
538 return rhashtable_lookup_fast(&neigh_update
->neigh_ht
, m_neigh
,
539 mlx5e_neigh_ht_params
);
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
)
548 *nhe
= kzalloc(sizeof(**nhe
), GFP_KERNEL
);
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);
557 err
= mlx5e_rep_neigh_entry_insert(priv
, *nhe
);
567 static void mlx5e_rep_neigh_entry_destroy(struct mlx5e_priv
*priv
,
568 struct mlx5e_neigh_hash_entry
*nhe
)
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.
576 mlx5e_rep_neigh_entry_remove(priv
, nhe
);
577 mlx5e_rep_neigh_entry_release(nhe
);
580 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv
*priv
,
581 struct mlx5e_encap_entry
*e
)
583 struct mlx5e_neigh_hash_entry
*nhe
;
586 nhe
= mlx5e_rep_neigh_entry_lookup(priv
, &e
->m_neigh
);
588 err
= mlx5e_rep_neigh_entry_create(priv
, e
, &nhe
);
592 list_add(&e
->encap_list
, &nhe
->encap_list
);
596 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv
*priv
,
597 struct mlx5e_encap_entry
*e
)
599 struct mlx5e_neigh_hash_entry
*nhe
;
601 list_del(&e
->encap_list
);
602 nhe
= mlx5e_rep_neigh_entry_lookup(priv
, &e
->m_neigh
);
604 if (list_empty(&nhe
->encap_list
))
605 mlx5e_rep_neigh_entry_destroy(priv
, nhe
);
608 static int mlx5e_rep_open(struct net_device
*dev
)
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
;
616 err
= mlx5e_open(dev
);
620 err
= mlx5_eswitch_set_vport_state(esw
, rep
->vport
, MLX5_ESW_VPORT_ADMIN_STATE_UP
);
622 netif_carrier_on(dev
);
627 static int mlx5e_rep_close(struct net_device
*dev
)
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
;
634 (void)mlx5_eswitch_set_vport_state(esw
, rep
->vport
, MLX5_ESW_VPORT_ADMIN_STATE_DOWN
);
636 return mlx5e_close(dev
);
639 static int mlx5e_rep_get_phys_port_name(struct net_device
*dev
,
640 char *buf
, size_t len
)
642 struct mlx5e_priv
*priv
= netdev_priv(dev
);
643 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
644 struct mlx5_eswitch_rep
*rep
= rpriv
->rep
;
647 ret
= snprintf(buf
, len
, "%d", rep
->vport
- 1);
654 static int mlx5e_rep_ndo_setup_tc(struct net_device
*dev
, u32 handle
,
655 __be16 proto
, struct tc_to_netdev
*tc
)
657 struct mlx5e_priv
*priv
= netdev_priv(dev
);
659 if (TC_H_MAJ(handle
) != TC_H_MAJ(TC_H_INGRESS
))
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
);
666 return uplink_dev
->netdev_ops
->ndo_setup_tc(uplink_dev
, handle
,
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
);
685 bool mlx5e_is_uplink_rep(struct mlx5e_priv
*priv
)
687 struct mlx5_eswitch
*esw
= priv
->mdev
->priv
.eswitch
;
688 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
689 struct mlx5_eswitch_rep
*rep
;
691 if (!MLX5_CAP_GEN(priv
->mdev
, vport_group_manager
))
695 if (esw
->mode
== SRIOV_OFFLOADS
&&
696 rep
&& rep
->vport
== FDB_UPLINK_VPORT
)
702 static bool mlx5e_is_vf_vport_rep(struct mlx5e_priv
*priv
)
704 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
705 struct mlx5_eswitch_rep
*rep
= rpriv
->rep
;
707 if (rep
&& rep
->vport
!= FDB_UPLINK_VPORT
)
713 bool mlx5e_has_offload_stats(const struct net_device
*dev
, int attr_id
)
715 struct mlx5e_priv
*priv
= netdev_priv(dev
);
718 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
719 if (mlx5e_is_vf_vport_rep(priv
) || mlx5e_is_uplink_rep(priv
))
727 mlx5e_get_sw_stats64(const struct net_device
*dev
,
728 struct rtnl_link_stats64
*stats
)
730 struct mlx5e_priv
*priv
= netdev_priv(dev
);
731 struct mlx5e_sw_stats
*sstats
= &priv
->stats
.sw
;
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
;
738 stats
->tx_dropped
= sstats
->tx_queue_dropped
;
743 int mlx5e_get_offload_stats(int attr_id
, const struct net_device
*dev
,
747 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
748 return mlx5e_get_sw_stats64(dev
, sp
);
755 mlx5e_rep_get_stats(struct net_device
*dev
, struct rtnl_link_stats64
*stats
)
757 struct mlx5e_priv
*priv
= netdev_priv(dev
);
759 memcpy(stats
, &priv
->stats
.vf_vport
, sizeof(*stats
));
762 static const struct switchdev_ops mlx5e_rep_switchdev_ops
= {
763 .switchdev_port_attr_get
= mlx5e_attr_get
,
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
,
777 static void mlx5e_build_rep_params(struct mlx5_core_dev
*mdev
,
778 struct mlx5e_params
*params
)
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
;
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
;
788 params
->rx_am_enabled
= MLX5_CAP_GEN(mdev
, cq_moderation
);
789 mlx5e_set_rx_cq_mode_params(params
, cq_period_mode
);
791 params
->tx_max_inline
= mlx5e_get_max_inline_cap(mdev
);
793 params
->lro_wqe_sz
= MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ
;
795 mlx5_query_min_inline(mdev
, ¶ms
->tx_min_inline_mode
);
798 static void mlx5e_build_rep_netdev(struct net_device
*netdev
)
800 netdev
->netdev_ops
= &mlx5e_netdev_ops_rep
;
802 netdev
->watchdog_timeo
= 15 * HZ
;
804 netdev
->ethtool_ops
= &mlx5e_rep_ethtool_ops
;
806 #ifdef CONFIG_NET_SWITCHDEV
807 netdev
->switchdev_ops
= &mlx5e_rep_switchdev_ops
;
810 netdev
->features
|= NETIF_F_VLAN_CHALLENGED
| NETIF_F_HW_TC
| NETIF_F_NETNS_LOCAL
;
811 netdev
->hw_features
|= NETIF_F_HW_TC
;
813 eth_hw_addr_random(netdev
);
816 static void mlx5e_init_rep(struct mlx5_core_dev
*mdev
,
817 struct net_device
*netdev
,
818 const struct mlx5e_profile
*profile
,
821 struct mlx5e_priv
*priv
= netdev_priv(netdev
);
824 priv
->netdev
= netdev
;
825 priv
->profile
= profile
;
828 mutex_init(&priv
->state_lock
);
830 INIT_DELAYED_WORK(&priv
->update_stats_work
, mlx5e_update_stats_work
);
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
);
837 static int mlx5e_init_rep_rx(struct mlx5e_priv
*priv
)
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
;
845 mlx5e_init_l2_addr(priv
);
847 err
= mlx5e_create_direct_rqts(priv
);
851 err
= mlx5e_create_direct_tirs(priv
);
853 goto err_destroy_direct_rqts
;
855 flow_rule
= mlx5_eswitch_create_vport_rx_rule(esw
,
857 priv
->direct_tir
[0].tirn
);
858 if (IS_ERR(flow_rule
)) {
859 err
= PTR_ERR(flow_rule
);
860 goto err_destroy_direct_tirs
;
862 rep
->vport_rx_rule
= flow_rule
;
864 err
= mlx5e_tc_init(priv
);
866 goto 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
);
879 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv
*priv
)
881 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
882 struct mlx5_eswitch_rep
*rep
= rpriv
->rep
;
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
);
890 static int mlx5e_init_rep_tx(struct mlx5e_priv
*priv
)
894 err
= mlx5e_create_tises(priv
);
896 mlx5_core_warn(priv
->mdev
, "create tises failed, %d\n", err
);
902 static int mlx5e_get_rep_max_num_channels(struct mlx5_core_dev
*mdev
)
904 #define MLX5E_PORT_REPRESENTOR_NCH 1
905 return MLX5E_PORT_REPRESENTOR_NCH
;
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 */,
921 /* e-Switch vport representors */
924 mlx5e_nic_rep_load(struct mlx5_eswitch
*esw
, struct mlx5_eswitch_rep
*rep
)
926 struct mlx5e_priv
*priv
= netdev_priv(rep
->netdev
);
927 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
931 if (test_bit(MLX5E_STATE_OPENED
, &priv
->state
)) {
932 err
= mlx5e_add_sqs_fwd_rules(priv
);
937 err
= mlx5e_rep_neigh_init(rpriv
);
944 mlx5e_remove_sqs_fwd_rules(priv
);
949 mlx5e_nic_rep_unload(struct mlx5_eswitch
*esw
, struct mlx5_eswitch_rep
*rep
)
951 struct mlx5e_priv
*priv
= netdev_priv(rep
->netdev
);
952 struct mlx5e_rep_priv
*rpriv
= priv
->ppriv
;
954 if (test_bit(MLX5E_STATE_OPENED
, &priv
->state
))
955 mlx5e_remove_sqs_fwd_rules(priv
);
957 /* clean (and re-init) existing uplink offloaded TC rules */
958 mlx5e_tc_cleanup(priv
);
961 mlx5e_rep_neigh_cleanup(rpriv
);
965 mlx5e_vport_rep_load(struct mlx5_eswitch
*esw
, struct mlx5_eswitch_rep
*rep
)
967 struct mlx5e_rep_priv
*rpriv
;
968 struct net_device
*netdev
;
971 rpriv
= kzalloc(sizeof(*rpriv
), GFP_KERNEL
);
975 netdev
= mlx5e_create_netdev(esw
->dev
, &mlx5e_rep_profile
, rpriv
);
977 pr_warn("Failed to create representor netdev for vport %d\n",
983 rep
->netdev
= netdev
;
986 err
= mlx5e_attach_netdev(netdev_priv(netdev
));
988 pr_warn("Failed to attach representor netdev for vport %d\n",
990 goto err_destroy_netdev
;
993 err
= mlx5e_rep_neigh_init(rpriv
);
995 pr_warn("Failed to initialized neighbours handling for vport %d\n",
997 goto err_detach_netdev
;
1000 err
= register_netdev(netdev
);
1002 pr_warn("Failed to register representor netdev for vport %d\n",
1004 goto err_neigh_cleanup
;
1010 mlx5e_rep_neigh_cleanup(rpriv
);
1013 mlx5e_detach_netdev(netdev_priv(netdev
));
1016 mlx5e_destroy_netdev(netdev_priv(netdev
));
1023 mlx5e_vport_rep_unload(struct mlx5_eswitch
*esw
, struct mlx5_eswitch_rep
*rep
)
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
;
1030 unregister_netdev(rep
->netdev
);
1032 mlx5e_rep_neigh_cleanup(rpriv
);
1033 mlx5e_detach_netdev(priv
);
1034 mlx5e_destroy_netdev(priv
);
1035 kfree(ppriv
); /* mlx5e_rep_priv */
1038 static void mlx5e_rep_register_vf_vports(struct mlx5e_priv
*priv
)
1040 struct mlx5_core_dev
*mdev
= priv
->mdev
;
1041 struct mlx5_eswitch
*esw
= mdev
->priv
.eswitch
;
1042 int total_vfs
= MLX5_TOTAL_VPORTS(mdev
);
1046 mlx5_query_nic_vport_mac_address(mdev
, 0, mac
);
1048 for (vport
= 1; vport
< total_vfs
; vport
++) {
1049 struct mlx5_eswitch_rep rep
;
1051 rep
.load
= mlx5e_vport_rep_load
;
1052 rep
.unload
= mlx5e_vport_rep_unload
;
1054 ether_addr_copy(rep
.hw_id
, mac
);
1055 mlx5_eswitch_register_vport_rep(esw
, vport
, &rep
);
1059 static void mlx5e_rep_unregister_vf_vports(struct mlx5e_priv
*priv
)
1061 struct mlx5_core_dev
*mdev
= priv
->mdev
;
1062 struct mlx5_eswitch
*esw
= mdev
->priv
.eswitch
;
1063 int total_vfs
= MLX5_TOTAL_VPORTS(mdev
);
1066 for (vport
= 1; vport
< total_vfs
; vport
++)
1067 mlx5_eswitch_unregister_vport_rep(esw
, vport
);
1070 void mlx5e_register_vport_reps(struct mlx5e_priv
*priv
)
1072 struct mlx5_core_dev
*mdev
= priv
->mdev
;
1073 struct mlx5_eswitch
*esw
= mdev
->priv
.eswitch
;
1074 struct mlx5_eswitch_rep rep
;
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*/
1083 mlx5e_rep_register_vf_vports(priv
); /* VFs vports */
1086 void mlx5e_unregister_vport_reps(struct mlx5e_priv
*priv
)
1088 struct mlx5_core_dev
*mdev
= priv
->mdev
;
1089 struct mlx5_eswitch
*esw
= mdev
->priv
.eswitch
;
1091 mlx5e_rep_unregister_vf_vports(priv
); /* VFs vports */
1092 mlx5_eswitch_unregister_vport_rep(esw
, 0); /* UPLINK PF*/