2 * Copyright (c) 2015-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 <net/tc_act/tc_gact.h>
34 #include <net/pkt_cls.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
37 #include <net/geneve.h>
38 #include <linux/bpf.h>
39 #include <linux/if_bridge.h>
40 #include <net/page_pool.h>
41 #include <net/xdp_sock.h>
47 #include "en_accel/ipsec.h"
48 #include "en_accel/ipsec_rxtx.h"
49 #include "en_accel/en_accel.h"
50 #include "en_accel/tls.h"
51 #include "accel/ipsec.h"
52 #include "accel/tls.h"
53 #include "lib/vxlan.h"
54 #include "lib/clock.h"
58 #include "en/monitor_stats.h"
59 #include "en/reporter.h"
60 #include "en/params.h"
61 #include "en/xsk/umem.h"
62 #include "en/xsk/setup.h"
63 #include "en/xsk/rx.h"
64 #include "en/xsk/tx.h"
67 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
69 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
70 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
71 MLX5_CAP_ETH(mdev, reg_umr_sq);
72 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
73 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
78 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
79 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
85 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
86 struct mlx5e_params *params)
88 params->log_rq_mtu_frames = is_kdump_kernel() ?
89 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
90 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
92 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
93 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
94 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
95 BIT(mlx5e_mpwqe_get_log_rq_size(params, NULL)) :
96 BIT(params->log_rq_mtu_frames),
97 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params, NULL)),
98 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
101 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
102 struct mlx5e_params *params)
104 if (!mlx5e_check_fragmented_striding_rq_cap(mdev))
107 if (MLX5_IPSEC_DEV(mdev))
110 if (params->xdp_prog) {
111 /* XSK params are not considered here. If striding RQ is in use,
112 * and an XSK is being opened, mlx5e_rx_mpwqe_is_linear_skb will
113 * be called with the known XSK params.
115 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL))
122 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
124 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
125 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
126 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
130 void mlx5e_update_carrier(struct mlx5e_priv *priv)
132 struct mlx5_core_dev *mdev = priv->mdev;
135 port_state = mlx5_query_vport_state(mdev,
136 MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT,
139 if (port_state == VPORT_STATE_UP) {
140 netdev_info(priv->netdev, "Link up\n");
141 netif_carrier_on(priv->netdev);
143 netdev_info(priv->netdev, "Link down\n");
144 netif_carrier_off(priv->netdev);
148 static void mlx5e_update_carrier_work(struct work_struct *work)
150 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
151 update_carrier_work);
153 mutex_lock(&priv->state_lock);
154 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
155 if (priv->profile->update_carrier)
156 priv->profile->update_carrier(priv);
157 mutex_unlock(&priv->state_lock);
160 void mlx5e_update_stats(struct mlx5e_priv *priv)
164 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
165 if (mlx5e_stats_grps[i].update_stats)
166 mlx5e_stats_grps[i].update_stats(priv);
169 void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
173 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
174 if (mlx5e_stats_grps[i].update_stats_mask &
175 MLX5E_NDO_UPDATE_STATS)
176 mlx5e_stats_grps[i].update_stats(priv);
179 static void mlx5e_update_stats_work(struct work_struct *work)
181 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
184 mutex_lock(&priv->state_lock);
185 priv->profile->update_stats(priv);
186 mutex_unlock(&priv->state_lock);
189 void mlx5e_queue_update_stats(struct mlx5e_priv *priv)
191 if (!priv->profile->update_stats)
194 if (unlikely(test_bit(MLX5E_STATE_DESTROYING, &priv->state)))
197 queue_work(priv->wq, &priv->update_stats_work);
200 static int async_event(struct notifier_block *nb, unsigned long event, void *data)
202 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
203 struct mlx5_eqe *eqe = data;
205 if (event != MLX5_EVENT_TYPE_PORT_CHANGE)
208 switch (eqe->sub_type) {
209 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
210 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
211 queue_work(priv->wq, &priv->update_carrier_work);
220 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
222 priv->events_nb.notifier_call = async_event;
223 mlx5_notifier_register(priv->mdev, &priv->events_nb);
226 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
228 mlx5_notifier_unregister(priv->mdev, &priv->events_nb);
231 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
232 struct mlx5e_icosq *sq,
233 struct mlx5e_umr_wqe *wqe)
235 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
236 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
237 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
239 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
241 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
242 cseg->imm = rq->mkey_be;
244 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
245 ucseg->xlt_octowords =
246 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
247 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
250 static u32 mlx5e_rqwq_get_size(struct mlx5e_rq *rq)
252 switch (rq->wq_type) {
253 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
254 return mlx5_wq_ll_get_size(&rq->mpwqe.wq);
256 return mlx5_wq_cyc_get_size(&rq->wqe.wq);
260 static u32 mlx5e_rqwq_get_cur_sz(struct mlx5e_rq *rq)
262 switch (rq->wq_type) {
263 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
264 return rq->mpwqe.wq.cur_sz;
266 return rq->wqe.wq.cur_sz;
270 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
271 struct mlx5e_channel *c)
273 int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
275 rq->mpwqe.info = kvzalloc_node(array_size(wq_sz,
276 sizeof(*rq->mpwqe.info)),
277 GFP_KERNEL, cpu_to_node(c->cpu));
281 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
286 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
287 u64 npages, u8 page_shift,
288 struct mlx5_core_mkey *umr_mkey)
290 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
295 in = kvzalloc(inlen, GFP_KERNEL);
299 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
301 MLX5_SET(mkc, mkc, free, 1);
302 MLX5_SET(mkc, mkc, umr_en, 1);
303 MLX5_SET(mkc, mkc, lw, 1);
304 MLX5_SET(mkc, mkc, lr, 1);
305 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
307 MLX5_SET(mkc, mkc, qpn, 0xffffff);
308 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
309 MLX5_SET64(mkc, mkc, len, npages << page_shift);
310 MLX5_SET(mkc, mkc, translations_octword_size,
311 MLX5_MTT_OCTW(npages));
312 MLX5_SET(mkc, mkc, log_page_size, page_shift);
314 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
320 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
322 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->mpwqe.wq));
324 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
327 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
329 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
332 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq)
334 struct mlx5e_wqe_frag_info next_frag, *prev;
337 next_frag.di = &rq->wqe.di[0];
338 next_frag.offset = 0;
341 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
342 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
343 struct mlx5e_wqe_frag_info *frag =
344 &rq->wqe.frags[i << rq->wqe.info.log_num_frags];
347 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) {
348 if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) {
350 next_frag.offset = 0;
352 prev->last_in_page = true;
357 next_frag.offset += frag_info[f].frag_stride;
363 prev->last_in_page = true;
366 static int mlx5e_init_di_list(struct mlx5e_rq *rq,
369 int len = wq_sz << rq->wqe.info.log_num_frags;
371 rq->wqe.di = kvzalloc_node(array_size(len, sizeof(*rq->wqe.di)),
372 GFP_KERNEL, cpu_to_node(cpu));
376 mlx5e_init_frags_partition(rq);
381 static void mlx5e_free_di_list(struct mlx5e_rq *rq)
386 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
387 struct mlx5e_params *params,
388 struct mlx5e_xsk_param *xsk,
389 struct xdp_umem *umem,
390 struct mlx5e_rq_param *rqp,
393 struct page_pool_params pp_params = { 0 };
394 struct mlx5_core_dev *mdev = c->mdev;
395 void *rqc = rqp->rqc;
396 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
397 u32 num_xsk_frames = 0;
404 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
406 rq->wq_type = params->rq_wq_type;
408 rq->netdev = c->netdev;
409 rq->tstamp = c->tstamp;
410 rq->clock = &mdev->clock;
414 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
415 rq->xdpsq = &c->rq_xdpsq;
419 rq->stats = &c->priv->channel_stats[c->ix].xskrq;
421 rq->stats = &c->priv->channel_stats[c->ix].rq;
423 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
424 if (IS_ERR(rq->xdp_prog)) {
425 err = PTR_ERR(rq->xdp_prog);
427 goto err_rq_wq_destroy;
432 rq_xdp_ix += params->num_channels * MLX5E_RQ_GROUP_XSK;
433 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq_xdp_ix);
435 goto err_rq_wq_destroy;
437 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
438 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params, xsk);
439 rq->buff.umem_headroom = xsk ? xsk->headroom : 0;
440 pool_size = 1 << params->log_rq_mtu_frames;
442 switch (rq->wq_type) {
443 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
444 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->mpwqe.wq,
449 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR];
451 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
454 num_xsk_frames = wq_sz <<
455 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
457 pool_size = MLX5_MPWRQ_PAGES_PER_WQE <<
458 mlx5e_mpwqe_get_log_rq_size(params, xsk);
460 rq->post_wqes = mlx5e_post_rx_mpwqes;
461 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
463 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
464 #ifdef CONFIG_MLX5_EN_IPSEC
465 if (MLX5_IPSEC_DEV(mdev)) {
467 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
468 goto err_rq_wq_destroy;
471 if (!rq->handle_rx_cqe) {
473 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
474 goto err_rq_wq_destroy;
477 rq->mpwqe.skb_from_cqe_mpwrq = xsk ?
478 mlx5e_xsk_skb_from_cqe_mpwrq_linear :
479 mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ?
480 mlx5e_skb_from_cqe_mpwrq_linear :
481 mlx5e_skb_from_cqe_mpwrq_nonlinear;
483 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
484 rq->mpwqe.num_strides =
485 BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk));
487 err = mlx5e_create_rq_umr_mkey(mdev, rq);
489 goto err_rq_wq_destroy;
490 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
492 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
496 default: /* MLX5_WQ_TYPE_CYCLIC */
497 err = mlx5_wq_cyc_create(mdev, &rqp->wq, rqc_wq, &rq->wqe.wq,
502 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR];
504 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
507 num_xsk_frames = wq_sz << rq->wqe.info.log_num_frags;
509 rq->wqe.info = rqp->frags_info;
511 kvzalloc_node(array_size(sizeof(*rq->wqe.frags),
512 (wq_sz << rq->wqe.info.log_num_frags)),
513 GFP_KERNEL, cpu_to_node(c->cpu));
514 if (!rq->wqe.frags) {
519 err = mlx5e_init_di_list(rq, wq_sz, c->cpu);
523 rq->post_wqes = mlx5e_post_rx_wqes;
524 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
526 #ifdef CONFIG_MLX5_EN_IPSEC
528 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
531 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
532 if (!rq->handle_rx_cqe) {
534 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
538 rq->wqe.skb_from_cqe = xsk ?
539 mlx5e_xsk_skb_from_cqe_linear :
540 mlx5e_rx_is_linear_skb(params, NULL) ?
541 mlx5e_skb_from_cqe_linear :
542 mlx5e_skb_from_cqe_nonlinear;
543 rq->mkey_be = c->mkey_be;
547 err = mlx5e_xsk_resize_reuseq(umem, num_xsk_frames);
549 mlx5_core_err(mdev, "Unable to allocate the Reuse Ring for %u frames\n",
554 rq->zca.free = mlx5e_xsk_zca_free;
555 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
559 /* Create a page_pool and register it with rxq */
561 pp_params.flags = 0; /* No-internal DMA mapping in page_pool */
562 pp_params.pool_size = pool_size;
563 pp_params.nid = cpu_to_node(c->cpu);
564 pp_params.dev = c->pdev;
565 pp_params.dma_dir = rq->buff.map_dir;
567 /* page_pool can be used even when there is no rq->xdp_prog,
568 * given page_pool does not handle DMA mapping there is no
569 * required state to clear. And page_pool gracefully handle
572 rq->page_pool = page_pool_create(&pp_params);
573 if (IS_ERR(rq->page_pool)) {
574 err = PTR_ERR(rq->page_pool);
575 rq->page_pool = NULL;
578 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
579 MEM_TYPE_PAGE_POOL, rq->page_pool);
581 page_pool_free(rq->page_pool);
586 for (i = 0; i < wq_sz; i++) {
587 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
588 struct mlx5e_rx_wqe_ll *wqe =
589 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i);
591 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
592 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
594 wqe->data[0].addr = cpu_to_be64(dma_offset + rq->buff.headroom);
595 wqe->data[0].byte_count = cpu_to_be32(byte_count);
596 wqe->data[0].lkey = rq->mkey_be;
598 struct mlx5e_rx_wqe_cyc *wqe =
599 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i);
602 for (f = 0; f < rq->wqe.info.num_frags; f++) {
603 u32 frag_size = rq->wqe.info.arr[f].frag_size |
604 MLX5_HW_START_PADDING;
606 wqe->data[f].byte_count = cpu_to_be32(frag_size);
607 wqe->data[f].lkey = rq->mkey_be;
609 /* check if num_frags is not a pow of two */
610 if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) {
611 wqe->data[f].byte_count = 0;
612 wqe->data[f].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
613 wqe->data[f].addr = 0;
618 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
620 switch (params->rx_cq_moderation.cq_period_mode) {
621 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
622 rq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
624 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
626 rq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
629 rq->page_cache.head = 0;
630 rq->page_cache.tail = 0;
635 switch (rq->wq_type) {
636 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
637 kvfree(rq->mpwqe.info);
638 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
640 default: /* MLX5_WQ_TYPE_CYCLIC */
641 kvfree(rq->wqe.frags);
642 mlx5e_free_di_list(rq);
647 bpf_prog_put(rq->xdp_prog);
648 xdp_rxq_info_unreg(&rq->xdp_rxq);
649 mlx5_wq_destroy(&rq->wq_ctrl);
654 static void mlx5e_free_rq(struct mlx5e_rq *rq)
659 bpf_prog_put(rq->xdp_prog);
661 switch (rq->wq_type) {
662 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
663 kvfree(rq->mpwqe.info);
664 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
666 default: /* MLX5_WQ_TYPE_CYCLIC */
667 kvfree(rq->wqe.frags);
668 mlx5e_free_di_list(rq);
671 for (i = rq->page_cache.head; i != rq->page_cache.tail;
672 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
673 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
675 /* With AF_XDP, page_cache is not used, so this loop is not
676 * entered, and it's safe to call mlx5e_page_release_dynamic
679 mlx5e_page_release_dynamic(rq, dma_info, false);
682 xdp_rxq_info_unreg(&rq->xdp_rxq);
683 mlx5_wq_destroy(&rq->wq_ctrl);
686 static int mlx5e_create_rq(struct mlx5e_rq *rq,
687 struct mlx5e_rq_param *param)
689 struct mlx5_core_dev *mdev = rq->mdev;
697 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
698 sizeof(u64) * rq->wq_ctrl.buf.npages;
699 in = kvzalloc(inlen, GFP_KERNEL);
703 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
704 wq = MLX5_ADDR_OF(rqc, rqc, wq);
706 memcpy(rqc, param->rqc, sizeof(param->rqc));
708 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
709 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
710 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
711 MLX5_ADAPTER_PAGE_SHIFT);
712 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
714 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
715 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
717 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
724 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
727 struct mlx5_core_dev *mdev = rq->mdev;
734 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
735 in = kvzalloc(inlen, GFP_KERNEL);
739 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
741 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
742 MLX5_SET(rqc, rqc, state, next_state);
744 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
751 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
753 struct mlx5e_channel *c = rq->channel;
754 struct mlx5e_priv *priv = c->priv;
755 struct mlx5_core_dev *mdev = priv->mdev;
762 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
763 in = kvzalloc(inlen, GFP_KERNEL);
767 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
769 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
770 MLX5_SET64(modify_rq_in, in, modify_bitmask,
771 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
772 MLX5_SET(rqc, rqc, scatter_fcs, enable);
773 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
775 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
782 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
784 struct mlx5e_channel *c = rq->channel;
785 struct mlx5_core_dev *mdev = c->mdev;
791 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
792 in = kvzalloc(inlen, GFP_KERNEL);
796 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
798 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
799 MLX5_SET64(modify_rq_in, in, modify_bitmask,
800 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
801 MLX5_SET(rqc, rqc, vsd, vsd);
802 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
804 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
811 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
813 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
816 int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
818 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
819 struct mlx5e_channel *c = rq->channel;
821 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq));
824 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes)
828 } while (time_before(jiffies, exp_time));
830 netdev_warn(c->netdev, "Failed to get min RX wqes on Channel[%d] RQN[0x%x] wq cur_sz(%d) min_rx_wqes(%d)\n",
831 c->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes);
836 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
841 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
842 struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
846 /* Outstanding UMR WQEs (in progress) start at wq->head */
847 for (i = 0; i < rq->mpwqe.umr_in_progress; i++) {
848 rq->dealloc_wqe(rq, head);
849 head = mlx5_wq_ll_get_wqe_next_ix(wq, head);
852 while (!mlx5_wq_ll_is_empty(wq)) {
853 struct mlx5e_rx_wqe_ll *wqe;
855 wqe_ix_be = *wq->tail_next;
856 wqe_ix = be16_to_cpu(wqe_ix_be);
857 wqe = mlx5_wq_ll_get_wqe(wq, wqe_ix);
858 rq->dealloc_wqe(rq, wqe_ix);
859 mlx5_wq_ll_pop(wq, wqe_ix_be,
860 &wqe->next.next_wqe_index);
863 struct mlx5_wq_cyc *wq = &rq->wqe.wq;
865 while (!mlx5_wq_cyc_is_empty(wq)) {
866 wqe_ix = mlx5_wq_cyc_get_tail(wq);
867 rq->dealloc_wqe(rq, wqe_ix);
874 int mlx5e_open_rq(struct mlx5e_channel *c, struct mlx5e_params *params,
875 struct mlx5e_rq_param *param, struct mlx5e_xsk_param *xsk,
876 struct xdp_umem *umem, struct mlx5e_rq *rq)
880 err = mlx5e_alloc_rq(c, params, xsk, umem, param, rq);
884 err = mlx5e_create_rq(rq, param);
888 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
892 if (params->rx_dim_enabled)
893 __set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
895 /* We disable csum_complete when XDP is enabled since
896 * XDP programs might manipulate packets which will render
897 * skb->checksum incorrect.
899 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE) || c->xdp)
900 __set_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &c->rq.state);
905 mlx5e_destroy_rq(rq);
912 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
914 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
915 mlx5e_trigger_irq(&rq->channel->icosq);
918 void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
920 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
921 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
924 void mlx5e_close_rq(struct mlx5e_rq *rq)
926 cancel_work_sync(&rq->dim.work);
927 mlx5e_destroy_rq(rq);
928 mlx5e_free_rx_descs(rq);
932 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
934 kvfree(sq->db.xdpi_fifo.xi);
935 kvfree(sq->db.wqe_info);
938 static int mlx5e_alloc_xdpsq_fifo(struct mlx5e_xdpsq *sq, int numa)
940 struct mlx5e_xdp_info_fifo *xdpi_fifo = &sq->db.xdpi_fifo;
941 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
942 int dsegs_per_wq = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
944 xdpi_fifo->xi = kvzalloc_node(sizeof(*xdpi_fifo->xi) * dsegs_per_wq,
949 xdpi_fifo->pc = &sq->xdpi_fifo_pc;
950 xdpi_fifo->cc = &sq->xdpi_fifo_cc;
951 xdpi_fifo->mask = dsegs_per_wq - 1;
956 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
958 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
961 sq->db.wqe_info = kvzalloc_node(sizeof(*sq->db.wqe_info) * wq_sz,
963 if (!sq->db.wqe_info)
966 err = mlx5e_alloc_xdpsq_fifo(sq, numa);
968 mlx5e_free_xdpsq_db(sq);
975 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
976 struct mlx5e_params *params,
977 struct xdp_umem *umem,
978 struct mlx5e_sq_param *param,
979 struct mlx5e_xdpsq *sq,
982 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
983 struct mlx5_core_dev *mdev = c->mdev;
984 struct mlx5_wq_cyc *wq = &sq->wq;
988 sq->mkey_be = c->mkey_be;
990 sq->uar_map = mdev->mlx5e_res.bfreg.map;
991 sq->min_inline_mode = params->tx_min_inline_mode;
992 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
995 sq->stats = sq->umem ?
996 &c->priv->channel_stats[c->ix].xsksq :
998 &c->priv->channel_stats[c->ix].xdpsq :
999 &c->priv->channel_stats[c->ix].rq_xdpsq;
1001 param->wq.db_numa_node = cpu_to_node(c->cpu);
1002 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1005 wq->db = &wq->db[MLX5_SND_DBR];
1007 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
1009 goto err_sq_wq_destroy;
1014 mlx5_wq_destroy(&sq->wq_ctrl);
1019 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
1021 mlx5e_free_xdpsq_db(sq);
1022 mlx5_wq_destroy(&sq->wq_ctrl);
1025 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
1027 kvfree(sq->db.ico_wqe);
1030 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
1032 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1034 sq->db.ico_wqe = kvzalloc_node(array_size(wq_sz,
1035 sizeof(*sq->db.ico_wqe)),
1037 if (!sq->db.ico_wqe)
1043 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
1044 struct mlx5e_sq_param *param,
1045 struct mlx5e_icosq *sq)
1047 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1048 struct mlx5_core_dev *mdev = c->mdev;
1049 struct mlx5_wq_cyc *wq = &sq->wq;
1053 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1055 param->wq.db_numa_node = cpu_to_node(c->cpu);
1056 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1059 wq->db = &wq->db[MLX5_SND_DBR];
1061 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
1063 goto err_sq_wq_destroy;
1068 mlx5_wq_destroy(&sq->wq_ctrl);
1073 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
1075 mlx5e_free_icosq_db(sq);
1076 mlx5_wq_destroy(&sq->wq_ctrl);
1079 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
1081 kvfree(sq->db.wqe_info);
1082 kvfree(sq->db.dma_fifo);
1085 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
1087 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1088 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
1090 sq->db.dma_fifo = kvzalloc_node(array_size(df_sz,
1091 sizeof(*sq->db.dma_fifo)),
1093 sq->db.wqe_info = kvzalloc_node(array_size(wq_sz,
1094 sizeof(*sq->db.wqe_info)),
1096 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
1097 mlx5e_free_txqsq_db(sq);
1101 sq->dma_fifo_mask = df_sz - 1;
1106 static void mlx5e_tx_err_cqe_work(struct work_struct *recover_work);
1107 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
1109 struct mlx5e_params *params,
1110 struct mlx5e_sq_param *param,
1111 struct mlx5e_txqsq *sq,
1114 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1115 struct mlx5_core_dev *mdev = c->mdev;
1116 struct mlx5_wq_cyc *wq = &sq->wq;
1120 sq->tstamp = c->tstamp;
1121 sq->clock = &mdev->clock;
1122 sq->mkey_be = c->mkey_be;
1125 sq->txq_ix = txq_ix;
1126 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1127 sq->min_inline_mode = params->tx_min_inline_mode;
1128 sq->stats = &c->priv->channel_stats[c->ix].sq[tc];
1129 sq->stop_room = MLX5E_SQ_STOP_ROOM;
1130 INIT_WORK(&sq->recover_work, mlx5e_tx_err_cqe_work);
1131 if (MLX5_IPSEC_DEV(c->priv->mdev))
1132 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1133 if (mlx5_accel_is_tls_device(c->priv->mdev)) {
1134 set_bit(MLX5E_SQ_STATE_TLS, &sq->state);
1135 sq->stop_room += MLX5E_SQ_TLS_ROOM;
1138 param->wq.db_numa_node = cpu_to_node(c->cpu);
1139 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1142 wq->db = &wq->db[MLX5_SND_DBR];
1144 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1146 goto err_sq_wq_destroy;
1148 INIT_WORK(&sq->dim.work, mlx5e_tx_dim_work);
1149 sq->dim.mode = params->tx_cq_moderation.cq_period_mode;
1154 mlx5_wq_destroy(&sq->wq_ctrl);
1159 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1161 mlx5e_free_txqsq_db(sq);
1162 mlx5_wq_destroy(&sq->wq_ctrl);
1165 struct mlx5e_create_sq_param {
1166 struct mlx5_wq_ctrl *wq_ctrl;
1173 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1174 struct mlx5e_sq_param *param,
1175 struct mlx5e_create_sq_param *csp,
1184 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1185 sizeof(u64) * csp->wq_ctrl->buf.npages;
1186 in = kvzalloc(inlen, GFP_KERNEL);
1190 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1191 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1193 memcpy(sqc, param->sqc, sizeof(param->sqc));
1194 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1195 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1196 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1198 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1199 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1201 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1202 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1204 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1205 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1206 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1207 MLX5_ADAPTER_PAGE_SHIFT);
1208 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1210 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1211 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1213 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1220 int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1221 struct mlx5e_modify_sq_param *p)
1228 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1229 in = kvzalloc(inlen, GFP_KERNEL);
1233 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1235 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1236 MLX5_SET(sqc, sqc, state, p->next_state);
1237 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1238 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1239 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1242 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1249 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1251 mlx5_core_destroy_sq(mdev, sqn);
1254 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1255 struct mlx5e_sq_param *param,
1256 struct mlx5e_create_sq_param *csp,
1259 struct mlx5e_modify_sq_param msp = {0};
1262 err = mlx5e_create_sq(mdev, param, csp, sqn);
1266 msp.curr_state = MLX5_SQC_STATE_RST;
1267 msp.next_state = MLX5_SQC_STATE_RDY;
1268 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1270 mlx5e_destroy_sq(mdev, *sqn);
1275 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1276 struct mlx5e_txqsq *sq, u32 rate);
1278 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1281 struct mlx5e_params *params,
1282 struct mlx5e_sq_param *param,
1283 struct mlx5e_txqsq *sq,
1286 struct mlx5e_create_sq_param csp = {};
1290 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1296 csp.cqn = sq->cq.mcq.cqn;
1297 csp.wq_ctrl = &sq->wq_ctrl;
1298 csp.min_inline_mode = sq->min_inline_mode;
1299 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1301 goto err_free_txqsq;
1303 tx_rate = c->priv->tx_rates[sq->txq_ix];
1305 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1307 if (params->tx_dim_enabled)
1308 sq->state |= BIT(MLX5E_SQ_STATE_AM);
1313 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1314 mlx5e_free_txqsq(sq);
1319 void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1321 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1322 clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1323 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1324 netdev_tx_reset_queue(sq->txq);
1325 netif_tx_start_queue(sq->txq);
1328 void mlx5e_tx_disable_queue(struct netdev_queue *txq)
1330 __netif_tx_lock_bh(txq);
1331 netif_tx_stop_queue(txq);
1332 __netif_tx_unlock_bh(txq);
1335 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1337 struct mlx5e_channel *c = sq->channel;
1338 struct mlx5_wq_cyc *wq = &sq->wq;
1340 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1341 /* prevent netif_tx_wake_queue */
1342 napi_synchronize(&c->napi);
1344 mlx5e_tx_disable_queue(sq->txq);
1346 /* last doorbell out, godspeed .. */
1347 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1348 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1349 struct mlx5e_tx_wqe *nop;
1351 sq->db.wqe_info[pi].skb = NULL;
1352 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1353 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1357 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1359 struct mlx5e_channel *c = sq->channel;
1360 struct mlx5_core_dev *mdev = c->mdev;
1361 struct mlx5_rate_limit rl = {0};
1363 cancel_work_sync(&sq->dim.work);
1364 cancel_work_sync(&sq->recover_work);
1365 mlx5e_destroy_sq(mdev, sq->sqn);
1366 if (sq->rate_limit) {
1367 rl.rate = sq->rate_limit;
1368 mlx5_rl_remove_rate(mdev, &rl);
1370 mlx5e_free_txqsq_descs(sq);
1371 mlx5e_free_txqsq(sq);
1374 static void mlx5e_tx_err_cqe_work(struct work_struct *recover_work)
1376 struct mlx5e_txqsq *sq = container_of(recover_work, struct mlx5e_txqsq,
1379 mlx5e_tx_reporter_err_cqe(sq);
1382 int mlx5e_open_icosq(struct mlx5e_channel *c, struct mlx5e_params *params,
1383 struct mlx5e_sq_param *param, struct mlx5e_icosq *sq)
1385 struct mlx5e_create_sq_param csp = {};
1388 err = mlx5e_alloc_icosq(c, param, sq);
1392 csp.cqn = sq->cq.mcq.cqn;
1393 csp.wq_ctrl = &sq->wq_ctrl;
1394 csp.min_inline_mode = params->tx_min_inline_mode;
1395 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1396 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1398 goto err_free_icosq;
1403 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1404 mlx5e_free_icosq(sq);
1409 void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1411 struct mlx5e_channel *c = sq->channel;
1413 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1414 napi_synchronize(&c->napi);
1416 mlx5e_destroy_sq(c->mdev, sq->sqn);
1417 mlx5e_free_icosq(sq);
1420 int mlx5e_open_xdpsq(struct mlx5e_channel *c, struct mlx5e_params *params,
1421 struct mlx5e_sq_param *param, struct xdp_umem *umem,
1422 struct mlx5e_xdpsq *sq, bool is_redirect)
1424 struct mlx5e_create_sq_param csp = {};
1427 err = mlx5e_alloc_xdpsq(c, params, umem, param, sq, is_redirect);
1432 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1433 csp.cqn = sq->cq.mcq.cqn;
1434 csp.wq_ctrl = &sq->wq_ctrl;
1435 csp.min_inline_mode = sq->min_inline_mode;
1436 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1437 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1439 goto err_free_xdpsq;
1441 mlx5e_set_xmit_fp(sq, param->is_mpw);
1443 if (!param->is_mpw) {
1444 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1445 unsigned int inline_hdr_sz = 0;
1448 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1449 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1453 /* Pre initialize fixed WQE fields */
1454 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1455 struct mlx5e_xdp_wqe_info *wi = &sq->db.wqe_info[i];
1456 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1457 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1458 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1459 struct mlx5_wqe_data_seg *dseg;
1461 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1462 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1464 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1465 dseg->lkey = sq->mkey_be;
1475 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1476 mlx5e_free_xdpsq(sq);
1481 void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1483 struct mlx5e_channel *c = sq->channel;
1485 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1486 napi_synchronize(&c->napi);
1488 mlx5e_destroy_sq(c->mdev, sq->sqn);
1489 mlx5e_free_xdpsq_descs(sq);
1490 mlx5e_free_xdpsq(sq);
1493 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1494 struct mlx5e_cq_param *param,
1495 struct mlx5e_cq *cq)
1497 struct mlx5_core_cq *mcq = &cq->mcq;
1503 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1507 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1513 mcq->set_ci_db = cq->wq_ctrl.db.db;
1514 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1515 *mcq->set_ci_db = 0;
1517 mcq->vector = param->eq_ix;
1518 mcq->comp = mlx5e_completion_event;
1519 mcq->event = mlx5e_cq_error_event;
1522 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1523 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1533 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1534 struct mlx5e_cq_param *param,
1535 struct mlx5e_cq *cq)
1537 struct mlx5_core_dev *mdev = c->priv->mdev;
1540 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1541 param->wq.db_numa_node = cpu_to_node(c->cpu);
1542 param->eq_ix = c->ix;
1544 err = mlx5e_alloc_cq_common(mdev, param, cq);
1546 cq->napi = &c->napi;
1552 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1554 mlx5_wq_destroy(&cq->wq_ctrl);
1557 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1559 u32 out[MLX5_ST_SZ_DW(create_cq_out)];
1560 struct mlx5_core_dev *mdev = cq->mdev;
1561 struct mlx5_core_cq *mcq = &cq->mcq;
1566 unsigned int irqn_not_used;
1570 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1574 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1575 sizeof(u64) * cq->wq_ctrl.buf.npages;
1576 in = kvzalloc(inlen, GFP_KERNEL);
1580 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1582 memcpy(cqc, param->cqc, sizeof(param->cqc));
1584 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
1585 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1587 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1588 MLX5_SET(cqc, cqc, c_eqn, eqn);
1589 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1590 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1591 MLX5_ADAPTER_PAGE_SHIFT);
1592 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1594 err = mlx5_core_create_cq(mdev, mcq, in, inlen, out, sizeof(out));
1606 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1608 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1611 int mlx5e_open_cq(struct mlx5e_channel *c, struct dim_cq_moder moder,
1612 struct mlx5e_cq_param *param, struct mlx5e_cq *cq)
1614 struct mlx5_core_dev *mdev = c->mdev;
1617 err = mlx5e_alloc_cq(c, param, cq);
1621 err = mlx5e_create_cq(cq, param);
1625 if (MLX5_CAP_GEN(mdev, cq_moderation))
1626 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1635 void mlx5e_close_cq(struct mlx5e_cq *cq)
1637 mlx5e_destroy_cq(cq);
1641 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1642 struct mlx5e_params *params,
1643 struct mlx5e_channel_param *cparam)
1648 for (tc = 0; tc < c->num_tc; tc++) {
1649 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1650 &cparam->tx_cq, &c->sq[tc].cq);
1652 goto err_close_tx_cqs;
1658 for (tc--; tc >= 0; tc--)
1659 mlx5e_close_cq(&c->sq[tc].cq);
1664 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1668 for (tc = 0; tc < c->num_tc; tc++)
1669 mlx5e_close_cq(&c->sq[tc].cq);
1672 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1673 struct mlx5e_params *params,
1674 struct mlx5e_channel_param *cparam)
1676 struct mlx5e_priv *priv = c->priv;
1677 int err, tc, max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
1679 for (tc = 0; tc < params->num_tc; tc++) {
1680 int txq_ix = c->ix + tc * max_nch;
1682 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1683 params, &cparam->sq, &c->sq[tc], tc);
1691 for (tc--; tc >= 0; tc--)
1692 mlx5e_close_txqsq(&c->sq[tc]);
1697 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1701 for (tc = 0; tc < c->num_tc; tc++)
1702 mlx5e_close_txqsq(&c->sq[tc]);
1705 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1706 struct mlx5e_txqsq *sq, u32 rate)
1708 struct mlx5e_priv *priv = netdev_priv(dev);
1709 struct mlx5_core_dev *mdev = priv->mdev;
1710 struct mlx5e_modify_sq_param msp = {0};
1711 struct mlx5_rate_limit rl = {0};
1715 if (rate == sq->rate_limit)
1719 if (sq->rate_limit) {
1720 rl.rate = sq->rate_limit;
1721 /* remove current rl index to free space to next ones */
1722 mlx5_rl_remove_rate(mdev, &rl);
1729 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1731 netdev_err(dev, "Failed configuring rate %u: %d\n",
1737 msp.curr_state = MLX5_SQC_STATE_RDY;
1738 msp.next_state = MLX5_SQC_STATE_RDY;
1739 msp.rl_index = rl_index;
1740 msp.rl_update = true;
1741 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1743 netdev_err(dev, "Failed configuring rate %u: %d\n",
1745 /* remove the rate from the table */
1747 mlx5_rl_remove_rate(mdev, &rl);
1751 sq->rate_limit = rate;
1755 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1757 struct mlx5e_priv *priv = netdev_priv(dev);
1758 struct mlx5_core_dev *mdev = priv->mdev;
1759 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1762 if (!mlx5_rl_is_supported(mdev)) {
1763 netdev_err(dev, "Rate limiting is not supported on this device\n");
1767 /* rate is given in Mb/sec, HW config is in Kb/sec */
1770 /* Check whether rate in valid range, 0 is always valid */
1771 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1772 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1776 mutex_lock(&priv->state_lock);
1777 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1778 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1780 priv->tx_rates[index] = rate;
1781 mutex_unlock(&priv->state_lock);
1786 static int mlx5e_alloc_xps_cpumask(struct mlx5e_channel *c,
1787 struct mlx5e_params *params)
1789 int num_comp_vectors = mlx5_comp_vectors_count(c->mdev);
1792 if (!zalloc_cpumask_var(&c->xps_cpumask, GFP_KERNEL))
1795 for (irq = c->ix; irq < num_comp_vectors; irq += params->num_channels) {
1796 int cpu = cpumask_first(mlx5_comp_irq_get_affinity_mask(c->mdev, irq));
1798 cpumask_set_cpu(cpu, c->xps_cpumask);
1804 static void mlx5e_free_xps_cpumask(struct mlx5e_channel *c)
1806 free_cpumask_var(c->xps_cpumask);
1809 static int mlx5e_open_queues(struct mlx5e_channel *c,
1810 struct mlx5e_params *params,
1811 struct mlx5e_channel_param *cparam)
1813 struct dim_cq_moder icocq_moder = {0, 0};
1816 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1820 err = mlx5e_open_tx_cqs(c, params, cparam);
1822 goto err_close_icosq_cq;
1824 err = mlx5e_open_cq(c, params->tx_cq_moderation, &cparam->tx_cq, &c->xdpsq.cq);
1826 goto err_close_tx_cqs;
1828 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1830 goto err_close_xdp_tx_cqs;
1832 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1833 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1834 &cparam->tx_cq, &c->rq_xdpsq.cq) : 0;
1836 goto err_close_rx_cq;
1838 napi_enable(&c->napi);
1840 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1842 goto err_disable_napi;
1844 err = mlx5e_open_sqs(c, params, cparam);
1846 goto err_close_icosq;
1849 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL,
1850 &c->rq_xdpsq, false);
1855 err = mlx5e_open_rq(c, params, &cparam->rq, NULL, NULL, &c->rq);
1857 goto err_close_xdp_sq;
1859 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL, &c->xdpsq, true);
1866 mlx5e_close_rq(&c->rq);
1870 mlx5e_close_xdpsq(&c->rq_xdpsq);
1876 mlx5e_close_icosq(&c->icosq);
1879 napi_disable(&c->napi);
1882 mlx5e_close_cq(&c->rq_xdpsq.cq);
1885 mlx5e_close_cq(&c->rq.cq);
1887 err_close_xdp_tx_cqs:
1888 mlx5e_close_cq(&c->xdpsq.cq);
1891 mlx5e_close_tx_cqs(c);
1894 mlx5e_close_cq(&c->icosq.cq);
1899 static void mlx5e_close_queues(struct mlx5e_channel *c)
1901 mlx5e_close_xdpsq(&c->xdpsq);
1902 mlx5e_close_rq(&c->rq);
1904 mlx5e_close_xdpsq(&c->rq_xdpsq);
1906 mlx5e_close_icosq(&c->icosq);
1907 napi_disable(&c->napi);
1909 mlx5e_close_cq(&c->rq_xdpsq.cq);
1910 mlx5e_close_cq(&c->rq.cq);
1911 mlx5e_close_cq(&c->xdpsq.cq);
1912 mlx5e_close_tx_cqs(c);
1913 mlx5e_close_cq(&c->icosq.cq);
1916 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1917 struct mlx5e_params *params,
1918 struct mlx5e_channel_param *cparam,
1919 struct xdp_umem *umem,
1920 struct mlx5e_channel **cp)
1922 int cpu = cpumask_first(mlx5_comp_irq_get_affinity_mask(priv->mdev, ix));
1923 struct net_device *netdev = priv->netdev;
1924 struct mlx5e_xsk_param xsk;
1925 struct mlx5e_channel *c;
1930 err = mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1934 c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1939 c->mdev = priv->mdev;
1940 c->tstamp = &priv->tstamp;
1943 c->pdev = priv->mdev->device;
1944 c->netdev = priv->netdev;
1945 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1946 c->num_tc = params->num_tc;
1947 c->xdp = !!params->xdp_prog;
1948 c->stats = &priv->channel_stats[ix].ch;
1949 c->irq_desc = irq_to_desc(irq);
1951 err = mlx5e_alloc_xps_cpumask(c, params);
1953 goto err_free_channel;
1955 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1957 err = mlx5e_open_queues(c, params, cparam);
1962 mlx5e_build_xsk_param(umem, &xsk);
1963 err = mlx5e_open_xsk(priv, params, &xsk, umem, c);
1965 goto err_close_queues;
1973 mlx5e_close_queues(c);
1976 netif_napi_del(&c->napi);
1977 mlx5e_free_xps_cpumask(c);
1985 static void mlx5e_activate_channel(struct mlx5e_channel *c)
1989 for (tc = 0; tc < c->num_tc; tc++)
1990 mlx5e_activate_txqsq(&c->sq[tc]);
1991 mlx5e_activate_rq(&c->rq);
1992 netif_set_xps_queue(c->netdev, c->xps_cpumask, c->ix);
1994 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
1995 mlx5e_activate_xsk(c);
1998 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
2002 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2003 mlx5e_deactivate_xsk(c);
2005 mlx5e_deactivate_rq(&c->rq);
2006 for (tc = 0; tc < c->num_tc; tc++)
2007 mlx5e_deactivate_txqsq(&c->sq[tc]);
2010 static void mlx5e_close_channel(struct mlx5e_channel *c)
2012 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2014 mlx5e_close_queues(c);
2015 netif_napi_del(&c->napi);
2016 mlx5e_free_xps_cpumask(c);
2021 #define DEFAULT_FRAG_SIZE (2048)
2023 static void mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
2024 struct mlx5e_params *params,
2025 struct mlx5e_xsk_param *xsk,
2026 struct mlx5e_rq_frags_info *info)
2028 u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
2029 int frag_size_max = DEFAULT_FRAG_SIZE;
2033 #ifdef CONFIG_MLX5_EN_IPSEC
2034 if (MLX5_IPSEC_DEV(mdev))
2035 byte_count += MLX5E_METADATA_ETHER_LEN;
2038 if (mlx5e_rx_is_linear_skb(params, xsk)) {
2041 frag_stride = mlx5e_rx_get_linear_frag_sz(params, xsk);
2042 frag_stride = roundup_pow_of_two(frag_stride);
2044 info->arr[0].frag_size = byte_count;
2045 info->arr[0].frag_stride = frag_stride;
2046 info->num_frags = 1;
2047 info->wqe_bulk = PAGE_SIZE / frag_stride;
2051 if (byte_count > PAGE_SIZE +
2052 (MLX5E_MAX_RX_FRAGS - 1) * frag_size_max)
2053 frag_size_max = PAGE_SIZE;
2056 while (buf_size < byte_count) {
2057 int frag_size = byte_count - buf_size;
2059 if (i < MLX5E_MAX_RX_FRAGS - 1)
2060 frag_size = min(frag_size, frag_size_max);
2062 info->arr[i].frag_size = frag_size;
2063 info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
2065 buf_size += frag_size;
2068 info->num_frags = i;
2069 /* number of different wqes sharing a page */
2070 info->wqe_bulk = 1 + (info->num_frags % 2);
2073 info->wqe_bulk = max_t(u8, info->wqe_bulk, 8);
2074 info->log_num_frags = order_base_2(info->num_frags);
2077 static inline u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
2079 int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
2082 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2083 sz += sizeof(struct mlx5e_rx_wqe_ll);
2085 default: /* MLX5_WQ_TYPE_CYCLIC */
2086 sz += sizeof(struct mlx5e_rx_wqe_cyc);
2089 return order_base_2(sz);
2092 static u8 mlx5e_get_rq_log_wq_sz(void *rqc)
2094 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2096 return MLX5_GET(wq, wq, log_wq_sz);
2099 void mlx5e_build_rq_param(struct mlx5e_priv *priv,
2100 struct mlx5e_params *params,
2101 struct mlx5e_xsk_param *xsk,
2102 struct mlx5e_rq_param *param)
2104 struct mlx5_core_dev *mdev = priv->mdev;
2105 void *rqc = param->rqc;
2106 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2109 switch (params->rq_wq_type) {
2110 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2111 MLX5_SET(wq, wq, log_wqe_num_of_strides,
2112 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk) -
2113 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
2114 MLX5_SET(wq, wq, log_wqe_stride_size,
2115 mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk) -
2116 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
2117 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params, xsk));
2119 default: /* MLX5_WQ_TYPE_CYCLIC */
2120 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
2121 mlx5e_build_rq_frags_info(mdev, params, xsk, ¶m->frags_info);
2122 ndsegs = param->frags_info.num_frags;
2125 MLX5_SET(wq, wq, wq_type, params->rq_wq_type);
2126 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
2127 MLX5_SET(wq, wq, log_wq_stride,
2128 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
2129 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
2130 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
2131 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
2132 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
2134 param->wq.buf_numa_node = dev_to_node(mdev->device);
2137 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
2138 struct mlx5e_rq_param *param)
2140 struct mlx5_core_dev *mdev = priv->mdev;
2141 void *rqc = param->rqc;
2142 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2144 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
2145 MLX5_SET(wq, wq, log_wq_stride,
2146 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
2147 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
2149 param->wq.buf_numa_node = dev_to_node(mdev->device);
2152 void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
2153 struct mlx5e_sq_param *param)
2155 void *sqc = param->sqc;
2156 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2158 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
2159 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
2161 param->wq.buf_numa_node = dev_to_node(priv->mdev->device);
2164 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
2165 struct mlx5e_params *params,
2166 struct mlx5e_sq_param *param)
2168 void *sqc = param->sqc;
2169 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2172 allow_swp = mlx5_geneve_tx_allowed(priv->mdev) ||
2173 !!MLX5_IPSEC_DEV(priv->mdev);
2174 mlx5e_build_sq_param_common(priv, param);
2175 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2176 MLX5_SET(sqc, sqc, allow_swp, allow_swp);
2179 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
2180 struct mlx5e_cq_param *param)
2182 void *cqc = param->cqc;
2184 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2185 if (MLX5_CAP_GEN(priv->mdev, cqe_128_always) && cache_line_size() >= 128)
2186 MLX5_SET(cqc, cqc, cqe_sz, CQE_STRIDE_128_PAD);
2189 void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2190 struct mlx5e_params *params,
2191 struct mlx5e_xsk_param *xsk,
2192 struct mlx5e_cq_param *param)
2194 struct mlx5_core_dev *mdev = priv->mdev;
2195 void *cqc = param->cqc;
2198 switch (params->rq_wq_type) {
2199 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2200 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params, xsk) +
2201 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
2203 default: /* MLX5_WQ_TYPE_CYCLIC */
2204 log_cq_size = params->log_rq_mtu_frames;
2207 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2208 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2209 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2210 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2213 mlx5e_build_common_cq_param(priv, param);
2214 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2217 void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2218 struct mlx5e_params *params,
2219 struct mlx5e_cq_param *param)
2221 void *cqc = param->cqc;
2223 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2225 mlx5e_build_common_cq_param(priv, param);
2226 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2229 void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2231 struct mlx5e_cq_param *param)
2233 void *cqc = param->cqc;
2235 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2237 mlx5e_build_common_cq_param(priv, param);
2239 param->cq_period_mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2242 void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2244 struct mlx5e_sq_param *param)
2246 void *sqc = param->sqc;
2247 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2249 mlx5e_build_sq_param_common(priv, param);
2251 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2252 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2255 void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2256 struct mlx5e_params *params,
2257 struct mlx5e_sq_param *param)
2259 void *sqc = param->sqc;
2260 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2262 mlx5e_build_sq_param_common(priv, param);
2263 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2264 param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE);
2267 static u8 mlx5e_build_icosq_log_wq_sz(struct mlx5e_params *params,
2268 struct mlx5e_rq_param *rqp)
2270 switch (params->rq_wq_type) {
2271 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2272 return order_base_2(MLX5E_UMR_WQEBBS) +
2273 mlx5e_get_rq_log_wq_sz(rqp->rqc);
2274 default: /* MLX5_WQ_TYPE_CYCLIC */
2275 return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2279 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2280 struct mlx5e_params *params,
2281 struct mlx5e_channel_param *cparam)
2285 mlx5e_build_rq_param(priv, params, NULL, &cparam->rq);
2287 icosq_log_wq_sz = mlx5e_build_icosq_log_wq_sz(params, &cparam->rq);
2289 mlx5e_build_sq_param(priv, params, &cparam->sq);
2290 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2291 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2292 mlx5e_build_rx_cq_param(priv, params, NULL, &cparam->rx_cq);
2293 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2294 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2297 int mlx5e_open_channels(struct mlx5e_priv *priv,
2298 struct mlx5e_channels *chs)
2300 struct mlx5e_channel_param *cparam;
2304 chs->num = chs->params.num_channels;
2306 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2307 cparam = kvzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2308 if (!chs->c || !cparam)
2311 mlx5e_build_channel_param(priv, &chs->params, cparam);
2312 for (i = 0; i < chs->num; i++) {
2313 struct xdp_umem *umem = NULL;
2315 if (chs->params.xdp_prog)
2316 umem = mlx5e_xsk_get_umem(&chs->params, chs->params.xsk, i);
2318 err = mlx5e_open_channel(priv, i, &chs->params, cparam, umem, &chs->c[i]);
2320 goto err_close_channels;
2323 if (!IS_ERR_OR_NULL(priv->tx_reporter))
2324 devlink_health_reporter_state_update(priv->tx_reporter,
2325 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY);
2331 for (i--; i >= 0; i--)
2332 mlx5e_close_channel(chs->c[i]);
2341 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2345 for (i = 0; i < chs->num; i++)
2346 mlx5e_activate_channel(chs->c[i]);
2349 #define MLX5E_RQ_WQES_TIMEOUT 20000 /* msecs */
2351 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2356 for (i = 0; i < chs->num; i++) {
2357 int timeout = err ? 0 : MLX5E_RQ_WQES_TIMEOUT;
2359 err |= mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq, timeout);
2361 /* Don't wait on the XSK RQ, because the newer xdpsock sample
2362 * doesn't provide any Fill Ring entries at the setup stage.
2366 return err ? -ETIMEDOUT : 0;
2369 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2373 for (i = 0; i < chs->num; i++)
2374 mlx5e_deactivate_channel(chs->c[i]);
2377 void mlx5e_close_channels(struct mlx5e_channels *chs)
2381 for (i = 0; i < chs->num; i++)
2382 mlx5e_close_channel(chs->c[i]);
2389 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2391 struct mlx5_core_dev *mdev = priv->mdev;
2398 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2399 in = kvzalloc(inlen, GFP_KERNEL);
2403 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2405 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2406 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2408 for (i = 0; i < sz; i++)
2409 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2411 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2413 rqt->enabled = true;
2419 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2421 rqt->enabled = false;
2422 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2425 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2427 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2430 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2432 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2436 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
2438 const int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
2442 for (ix = 0; ix < max_nch; ix++) {
2443 err = mlx5e_create_rqt(priv, 1 /*size */, &tirs[ix].rqt);
2445 goto err_destroy_rqts;
2451 mlx5_core_warn(priv->mdev, "create rqts failed, %d\n", err);
2452 for (ix--; ix >= 0; ix--)
2453 mlx5e_destroy_rqt(priv, &tirs[ix].rqt);
2458 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
2460 const int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
2463 for (i = 0; i < max_nch; i++)
2464 mlx5e_destroy_rqt(priv, &tirs[i].rqt);
2467 static int mlx5e_rx_hash_fn(int hfunc)
2469 return (hfunc == ETH_RSS_HASH_TOP) ?
2470 MLX5_RX_HASH_FN_TOEPLITZ :
2471 MLX5_RX_HASH_FN_INVERTED_XOR8;
2474 int mlx5e_bits_invert(unsigned long a, int size)
2479 for (i = 0; i < size; i++)
2480 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2485 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2486 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2490 for (i = 0; i < sz; i++) {
2496 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2497 ix = mlx5e_bits_invert(i, ilog2(sz));
2499 ix = priv->rss_params.indirection_rqt[ix];
2500 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2504 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2508 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2509 struct mlx5e_redirect_rqt_param rrp)
2511 struct mlx5_core_dev *mdev = priv->mdev;
2517 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2518 in = kvzalloc(inlen, GFP_KERNEL);
2522 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2524 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2525 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2526 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2527 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2533 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2534 struct mlx5e_redirect_rqt_param rrp)
2539 if (ix >= rrp.rss.channels->num)
2540 return priv->drop_rq.rqn;
2542 return rrp.rss.channels->c[ix]->rq.rqn;
2545 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2546 struct mlx5e_redirect_rqt_param rrp)
2551 if (priv->indir_rqt.enabled) {
2553 rqtn = priv->indir_rqt.rqtn;
2554 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2557 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2558 struct mlx5e_redirect_rqt_param direct_rrp = {
2561 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2565 /* Direct RQ Tables */
2566 if (!priv->direct_tir[ix].rqt.enabled)
2569 rqtn = priv->direct_tir[ix].rqt.rqtn;
2570 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2574 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2575 struct mlx5e_channels *chs)
2577 struct mlx5e_redirect_rqt_param rrp = {
2582 .hfunc = priv->rss_params.hfunc,
2587 mlx5e_redirect_rqts(priv, rrp);
2590 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2592 struct mlx5e_redirect_rqt_param drop_rrp = {
2595 .rqn = priv->drop_rq.rqn,
2599 mlx5e_redirect_rqts(priv, drop_rrp);
2602 static const struct mlx5e_tirc_config tirc_default_config[MLX5E_NUM_INDIR_TIRS] = {
2603 [MLX5E_TT_IPV4_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2604 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2605 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2607 [MLX5E_TT_IPV6_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2608 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2609 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2611 [MLX5E_TT_IPV4_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2612 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2613 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2615 [MLX5E_TT_IPV6_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2616 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2617 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2619 [MLX5E_TT_IPV4_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2621 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2623 [MLX5E_TT_IPV6_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2625 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2627 [MLX5E_TT_IPV4_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2629 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2631 [MLX5E_TT_IPV6_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2633 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2635 [MLX5E_TT_IPV4] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2637 .rx_hash_fields = MLX5_HASH_IP,
2639 [MLX5E_TT_IPV6] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2641 .rx_hash_fields = MLX5_HASH_IP,
2645 struct mlx5e_tirc_config mlx5e_tirc_get_default_config(enum mlx5e_traffic_types tt)
2647 return tirc_default_config[tt];
2650 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2652 if (!params->lro_en)
2655 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2657 MLX5_SET(tirc, tirc, lro_enable_mask,
2658 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2659 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2660 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2661 (MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2662 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2665 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_rss_params *rss_params,
2666 const struct mlx5e_tirc_config *ttconfig,
2667 void *tirc, bool inner)
2669 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2670 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2672 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(rss_params->hfunc));
2673 if (rss_params->hfunc == ETH_RSS_HASH_TOP) {
2674 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2675 rx_hash_toeplitz_key);
2676 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2677 rx_hash_toeplitz_key);
2679 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2680 memcpy(rss_key, rss_params->toeplitz_hash_key, len);
2682 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2683 ttconfig->l3_prot_type);
2684 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2685 ttconfig->l4_prot_type);
2686 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2687 ttconfig->rx_hash_fields);
2690 static void mlx5e_update_rx_hash_fields(struct mlx5e_tirc_config *ttconfig,
2691 enum mlx5e_traffic_types tt,
2694 *ttconfig = tirc_default_config[tt];
2695 ttconfig->rx_hash_fields = rx_hash_fields;
2698 void mlx5e_modify_tirs_hash(struct mlx5e_priv *priv, void *in, int inlen)
2700 void *tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2701 struct mlx5e_rss_params *rss = &priv->rss_params;
2702 struct mlx5_core_dev *mdev = priv->mdev;
2703 int ctxlen = MLX5_ST_SZ_BYTES(tirc);
2704 struct mlx5e_tirc_config ttconfig;
2707 MLX5_SET(modify_tir_in, in, bitmask.hash, 1);
2709 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2710 memset(tirc, 0, ctxlen);
2711 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2712 rss->rx_hash_fields[tt]);
2713 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, false);
2714 mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in, inlen);
2717 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
2720 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2721 memset(tirc, 0, ctxlen);
2722 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2723 rss->rx_hash_fields[tt]);
2724 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, true);
2725 mlx5_core_modify_tir(mdev, priv->inner_indir_tir[tt].tirn, in,
2730 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2732 struct mlx5_core_dev *mdev = priv->mdev;
2741 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2742 in = kvzalloc(inlen, GFP_KERNEL);
2746 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2747 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2749 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2751 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2752 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2758 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2759 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2771 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2772 struct mlx5e_params *params, u16 mtu)
2774 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2777 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2781 /* Update vport context MTU */
2782 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2786 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2787 struct mlx5e_params *params, u16 *mtu)
2792 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2793 if (err || !hw_mtu) /* fallback to port oper mtu */
2794 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2796 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2799 int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2801 struct mlx5e_params *params = &priv->channels.params;
2802 struct net_device *netdev = priv->netdev;
2803 struct mlx5_core_dev *mdev = priv->mdev;
2807 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2811 mlx5e_query_mtu(mdev, params, &mtu);
2812 if (mtu != params->sw_mtu)
2813 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2814 __func__, mtu, params->sw_mtu);
2816 params->sw_mtu = mtu;
2820 void mlx5e_set_netdev_mtu_boundaries(struct mlx5e_priv *priv)
2822 struct mlx5e_params *params = &priv->channels.params;
2823 struct net_device *netdev = priv->netdev;
2824 struct mlx5_core_dev *mdev = priv->mdev;
2827 /* MTU range: 68 - hw-specific max */
2828 netdev->min_mtu = ETH_MIN_MTU;
2830 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2831 netdev->max_mtu = min_t(unsigned int, MLX5E_HW2SW_MTU(params, max_mtu),
2835 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2837 struct mlx5e_priv *priv = netdev_priv(netdev);
2838 int nch = priv->channels.params.num_channels;
2839 int ntc = priv->channels.params.num_tc;
2842 netdev_reset_tc(netdev);
2847 netdev_set_num_tc(netdev, ntc);
2849 /* Map netdev TCs to offset 0
2850 * We have our own UP to TXQ mapping for QoS
2852 for (tc = 0; tc < ntc; tc++)
2853 netdev_set_tc_queue(netdev, tc, nch, 0);
2856 static void mlx5e_build_tc2txq_maps(struct mlx5e_priv *priv)
2858 int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
2861 for (i = 0; i < max_nch; i++)
2862 for (tc = 0; tc < priv->profile->max_tc; tc++)
2863 priv->channel_tc2txq[i][tc] = i + tc * max_nch;
2866 static void mlx5e_build_tx2sq_maps(struct mlx5e_priv *priv)
2868 struct mlx5e_channel *c;
2869 struct mlx5e_txqsq *sq;
2872 for (i = 0; i < priv->channels.num; i++) {
2873 c = priv->channels.c[i];
2874 for (tc = 0; tc < c->num_tc; tc++) {
2876 priv->txq2sq[sq->txq_ix] = sq;
2881 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2883 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2884 int num_rxqs = priv->channels.num * MLX5E_NUM_RQ_GROUPS;
2885 struct net_device *netdev = priv->netdev;
2887 mlx5e_netdev_set_tcs(netdev);
2888 netif_set_real_num_tx_queues(netdev, num_txqs);
2889 netif_set_real_num_rx_queues(netdev, num_rxqs);
2891 mlx5e_build_tx2sq_maps(priv);
2892 mlx5e_activate_channels(&priv->channels);
2893 mlx5e_xdp_tx_enable(priv);
2894 netif_tx_start_all_queues(priv->netdev);
2896 if (mlx5e_is_vport_rep(priv))
2897 mlx5e_add_sqs_fwd_rules(priv);
2899 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2900 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2902 mlx5e_xsk_redirect_rqts_to_channels(priv, &priv->channels);
2905 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2907 mlx5e_xsk_redirect_rqts_to_drop(priv, &priv->channels);
2909 mlx5e_redirect_rqts_to_drop(priv);
2911 if (mlx5e_is_vport_rep(priv))
2912 mlx5e_remove_sqs_fwd_rules(priv);
2914 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2915 * polling for inactive tx queues.
2917 netif_tx_stop_all_queues(priv->netdev);
2918 netif_tx_disable(priv->netdev);
2919 mlx5e_xdp_tx_disable(priv);
2920 mlx5e_deactivate_channels(&priv->channels);
2923 static void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2924 struct mlx5e_channels *new_chs,
2925 mlx5e_fp_hw_modify hw_modify)
2927 struct net_device *netdev = priv->netdev;
2931 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2933 carrier_ok = netif_carrier_ok(netdev);
2934 netif_carrier_off(netdev);
2936 if (new_num_txqs < netdev->real_num_tx_queues)
2937 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2939 mlx5e_deactivate_priv_channels(priv);
2940 mlx5e_close_channels(&priv->channels);
2942 priv->channels = *new_chs;
2944 /* New channels are ready to roll, modify HW settings if needed */
2948 priv->profile->update_rx(priv);
2949 mlx5e_activate_priv_channels(priv);
2951 /* return carrier back if needed */
2953 netif_carrier_on(netdev);
2956 int mlx5e_safe_switch_channels(struct mlx5e_priv *priv,
2957 struct mlx5e_channels *new_chs,
2958 mlx5e_fp_hw_modify hw_modify)
2962 err = mlx5e_open_channels(priv, new_chs);
2966 mlx5e_switch_priv_channels(priv, new_chs, hw_modify);
2970 int mlx5e_safe_reopen_channels(struct mlx5e_priv *priv)
2972 struct mlx5e_channels new_channels = {};
2974 new_channels.params = priv->channels.params;
2975 return mlx5e_safe_switch_channels(priv, &new_channels, NULL);
2978 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2980 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
2981 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2984 int mlx5e_open_locked(struct net_device *netdev)
2986 struct mlx5e_priv *priv = netdev_priv(netdev);
2987 bool is_xdp = priv->channels.params.xdp_prog;
2990 set_bit(MLX5E_STATE_OPENED, &priv->state);
2992 mlx5e_xdp_set_open(priv);
2994 err = mlx5e_open_channels(priv, &priv->channels);
2996 goto err_clear_state_opened_flag;
2998 priv->profile->update_rx(priv);
2999 mlx5e_activate_priv_channels(priv);
3000 if (priv->profile->update_carrier)
3001 priv->profile->update_carrier(priv);
3003 mlx5e_queue_update_stats(priv);
3006 err_clear_state_opened_flag:
3008 mlx5e_xdp_set_closed(priv);
3009 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3013 int mlx5e_open(struct net_device *netdev)
3015 struct mlx5e_priv *priv = netdev_priv(netdev);
3018 mutex_lock(&priv->state_lock);
3019 err = mlx5e_open_locked(netdev);
3021 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
3022 mutex_unlock(&priv->state_lock);
3024 if (mlx5_vxlan_allowed(priv->mdev->vxlan))
3025 udp_tunnel_get_rx_info(netdev);
3030 int mlx5e_close_locked(struct net_device *netdev)
3032 struct mlx5e_priv *priv = netdev_priv(netdev);
3034 /* May already be CLOSED in case a previous configuration operation
3035 * (e.g RX/TX queue size change) that involves close&open failed.
3037 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3040 if (priv->channels.params.xdp_prog)
3041 mlx5e_xdp_set_closed(priv);
3042 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3044 netif_carrier_off(priv->netdev);
3045 mlx5e_deactivate_priv_channels(priv);
3046 mlx5e_close_channels(&priv->channels);
3051 int mlx5e_close(struct net_device *netdev)
3053 struct mlx5e_priv *priv = netdev_priv(netdev);
3056 if (!netif_device_present(netdev))
3059 mutex_lock(&priv->state_lock);
3060 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
3061 err = mlx5e_close_locked(netdev);
3062 mutex_unlock(&priv->state_lock);
3067 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
3068 struct mlx5e_rq *rq,
3069 struct mlx5e_rq_param *param)
3071 void *rqc = param->rqc;
3072 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
3075 param->wq.db_numa_node = param->wq.buf_numa_node;
3077 err = mlx5_wq_cyc_create(mdev, ¶m->wq, rqc_wq, &rq->wqe.wq,
3082 /* Mark as unused given "Drop-RQ" packets never reach XDP */
3083 xdp_rxq_info_unused(&rq->xdp_rxq);
3090 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
3091 struct mlx5e_cq *cq,
3092 struct mlx5e_cq_param *param)
3094 param->wq.buf_numa_node = dev_to_node(mdev->device);
3095 param->wq.db_numa_node = dev_to_node(mdev->device);
3097 return mlx5e_alloc_cq_common(mdev, param, cq);
3100 int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
3101 struct mlx5e_rq *drop_rq)
3103 struct mlx5_core_dev *mdev = priv->mdev;
3104 struct mlx5e_cq_param cq_param = {};
3105 struct mlx5e_rq_param rq_param = {};
3106 struct mlx5e_cq *cq = &drop_rq->cq;
3109 mlx5e_build_drop_rq_param(priv, &rq_param);
3111 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
3115 err = mlx5e_create_cq(cq, &cq_param);
3119 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3121 goto err_destroy_cq;
3123 err = mlx5e_create_rq(drop_rq, &rq_param);
3127 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3129 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3134 mlx5e_free_rq(drop_rq);
3137 mlx5e_destroy_cq(cq);
3145 void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3147 mlx5e_destroy_rq(drop_rq);
3148 mlx5e_free_rq(drop_rq);
3149 mlx5e_destroy_cq(&drop_rq->cq);
3150 mlx5e_free_cq(&drop_rq->cq);
3153 int mlx5e_create_tis(struct mlx5_core_dev *mdev, void *in, u32 *tisn)
3155 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3157 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
3159 if (MLX5_GET(tisc, tisc, tls_en))
3160 MLX5_SET(tisc, tisc, pd, mdev->mlx5e_res.pdn);
3162 if (mlx5_lag_is_lacp_owner(mdev))
3163 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
3165 return mlx5_core_create_tis(mdev, in, MLX5_ST_SZ_BYTES(create_tis_in), tisn);
3168 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
3170 mlx5_core_destroy_tis(mdev, tisn);
3173 int mlx5e_create_tises(struct mlx5e_priv *priv)
3178 for (tc = 0; tc < priv->profile->max_tc; tc++) {
3179 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
3182 tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3184 MLX5_SET(tisc, tisc, prio, tc << 1);
3186 err = mlx5e_create_tis(priv->mdev, in, &priv->tisn[tc]);
3188 goto err_close_tises;
3194 for (tc--; tc >= 0; tc--)
3195 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3200 static void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3204 mlx5e_tx_reporter_destroy(priv);
3205 for (tc = 0; tc < priv->profile->max_tc; tc++)
3206 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3209 static void mlx5e_build_indir_tir_ctx_common(struct mlx5e_priv *priv,
3210 u32 rqtn, u32 *tirc)
3212 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3213 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3214 MLX5_SET(tirc, tirc, indirect_table, rqtn);
3215 MLX5_SET(tirc, tirc, tunneled_offload_en,
3216 priv->channels.params.tunneled_offload_en);
3218 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3221 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
3222 enum mlx5e_traffic_types tt,
3225 mlx5e_build_indir_tir_ctx_common(priv, priv->indir_rqt.rqtn, tirc);
3226 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
3227 &tirc_default_config[tt], tirc, false);
3230 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
3232 mlx5e_build_indir_tir_ctx_common(priv, rqtn, tirc);
3233 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
3236 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
3237 enum mlx5e_traffic_types tt,
3240 mlx5e_build_indir_tir_ctx_common(priv, priv->indir_rqt.rqtn, tirc);
3241 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
3242 &tirc_default_config[tt], tirc, true);
3245 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3247 struct mlx5e_tir *tir;
3255 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3256 in = kvzalloc(inlen, GFP_KERNEL);
3260 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
3261 memset(in, 0, inlen);
3262 tir = &priv->indir_tir[tt];
3263 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3264 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
3265 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3267 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
3268 goto err_destroy_inner_tirs;
3272 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3275 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
3276 memset(in, 0, inlen);
3277 tir = &priv->inner_indir_tir[i];
3278 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3279 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
3280 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3282 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
3283 goto err_destroy_inner_tirs;
3292 err_destroy_inner_tirs:
3293 for (i--; i >= 0; i--)
3294 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3296 for (tt--; tt >= 0; tt--)
3297 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3304 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
3306 const int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
3307 struct mlx5e_tir *tir;
3314 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3315 in = kvzalloc(inlen, GFP_KERNEL);
3319 for (ix = 0; ix < max_nch; ix++) {
3320 memset(in, 0, inlen);
3322 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3323 mlx5e_build_direct_tir_ctx(priv, tir->rqt.rqtn, tirc);
3324 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3326 goto err_destroy_ch_tirs;
3331 err_destroy_ch_tirs:
3332 mlx5_core_warn(priv->mdev, "create tirs failed, %d\n", err);
3333 for (ix--; ix >= 0; ix--)
3334 mlx5e_destroy_tir(priv->mdev, &tirs[ix]);
3342 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3346 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3347 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3349 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3352 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3353 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3356 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
3358 const int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
3361 for (i = 0; i < max_nch; i++)
3362 mlx5e_destroy_tir(priv->mdev, &tirs[i]);
3365 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3370 for (i = 0; i < chs->num; i++) {
3371 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3379 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3384 for (i = 0; i < chs->num; i++) {
3385 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3393 static int mlx5e_setup_tc_mqprio(struct net_device *netdev,
3394 struct tc_mqprio_qopt *mqprio)
3396 struct mlx5e_priv *priv = netdev_priv(netdev);
3397 struct mlx5e_channels new_channels = {};
3398 u8 tc = mqprio->num_tc;
3401 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3403 if (tc && tc != MLX5E_MAX_NUM_TC)
3406 mutex_lock(&priv->state_lock);
3408 new_channels.params = priv->channels.params;
3409 new_channels.params.num_tc = tc ? tc : 1;
3411 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3412 priv->channels.params = new_channels.params;
3416 err = mlx5e_safe_switch_channels(priv, &new_channels, NULL);
3420 priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3421 new_channels.params.num_tc);
3423 mutex_unlock(&priv->state_lock);
3427 #ifdef CONFIG_MLX5_ESWITCH
3428 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3429 struct tc_cls_flower_offload *cls_flower,
3432 switch (cls_flower->command) {
3433 case TC_CLSFLOWER_REPLACE:
3434 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
3436 case TC_CLSFLOWER_DESTROY:
3437 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
3439 case TC_CLSFLOWER_STATS:
3440 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
3447 static int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3450 struct mlx5e_priv *priv = cb_priv;
3453 case TC_SETUP_CLSFLOWER:
3454 return mlx5e_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS |
3455 MLX5E_TC_NIC_OFFLOAD);
3461 static int mlx5e_setup_tc_block(struct net_device *dev,
3462 struct tc_block_offload *f)
3464 struct mlx5e_priv *priv = netdev_priv(dev);
3466 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3469 switch (f->command) {
3471 return tcf_block_cb_register(f->block, mlx5e_setup_tc_block_cb,
3472 priv, priv, f->extack);
3473 case TC_BLOCK_UNBIND:
3474 tcf_block_cb_unregister(f->block, mlx5e_setup_tc_block_cb,
3483 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3487 #ifdef CONFIG_MLX5_ESWITCH
3488 case TC_SETUP_BLOCK:
3489 return mlx5e_setup_tc_block(dev, type_data);
3491 case TC_SETUP_QDISC_MQPRIO:
3492 return mlx5e_setup_tc_mqprio(dev, type_data);
3498 void mlx5e_fold_sw_stats64(struct mlx5e_priv *priv, struct rtnl_link_stats64 *s)
3502 for (i = 0; i < mlx5e_get_netdev_max_channels(priv->netdev); i++) {
3503 struct mlx5e_channel_stats *channel_stats = &priv->channel_stats[i];
3504 struct mlx5e_rq_stats *xskrq_stats = &channel_stats->xskrq;
3505 struct mlx5e_rq_stats *rq_stats = &channel_stats->rq;
3508 s->rx_packets += rq_stats->packets + xskrq_stats->packets;
3509 s->rx_bytes += rq_stats->bytes + xskrq_stats->bytes;
3511 for (j = 0; j < priv->max_opened_tc; j++) {
3512 struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[j];
3514 s->tx_packets += sq_stats->packets;
3515 s->tx_bytes += sq_stats->bytes;
3516 s->tx_dropped += sq_stats->dropped;
3522 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3524 struct mlx5e_priv *priv = netdev_priv(dev);
3525 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3526 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3528 if (!mlx5e_monitor_counter_supported(priv)) {
3529 /* update HW stats in background for next time */
3530 mlx5e_queue_update_stats(priv);
3533 if (mlx5e_is_uplink_rep(priv)) {
3534 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3535 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3536 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3537 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3539 mlx5e_fold_sw_stats64(priv, stats);
3542 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3544 stats->rx_length_errors =
3545 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3546 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3547 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3548 stats->rx_crc_errors =
3549 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3550 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3551 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3552 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3553 stats->rx_frame_errors;
3554 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3556 /* vport multicast also counts packets that are dropped due to steering
3557 * or rx out of buffer
3560 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3563 static void mlx5e_set_rx_mode(struct net_device *dev)
3565 struct mlx5e_priv *priv = netdev_priv(dev);
3567 queue_work(priv->wq, &priv->set_rx_mode_work);
3570 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3572 struct mlx5e_priv *priv = netdev_priv(netdev);
3573 struct sockaddr *saddr = addr;
3575 if (!is_valid_ether_addr(saddr->sa_data))
3576 return -EADDRNOTAVAIL;
3578 netif_addr_lock_bh(netdev);
3579 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3580 netif_addr_unlock_bh(netdev);
3582 queue_work(priv->wq, &priv->set_rx_mode_work);
3587 #define MLX5E_SET_FEATURE(features, feature, enable) \
3590 *features |= feature; \
3592 *features &= ~feature; \
3595 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3597 static int set_feature_lro(struct net_device *netdev, bool enable)
3599 struct mlx5e_priv *priv = netdev_priv(netdev);
3600 struct mlx5_core_dev *mdev = priv->mdev;
3601 struct mlx5e_channels new_channels = {};
3602 struct mlx5e_params *old_params;
3606 mutex_lock(&priv->state_lock);
3608 if (enable && priv->xsk.refcnt) {
3609 netdev_warn(netdev, "LRO is incompatible with AF_XDP (%hu XSKs are active)\n",
3615 old_params = &priv->channels.params;
3616 if (enable && !MLX5E_GET_PFLAG(old_params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3617 netdev_warn(netdev, "can't set LRO with legacy RQ\n");
3622 reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3624 new_channels.params = *old_params;
3625 new_channels.params.lro_en = enable;
3627 if (old_params->rq_wq_type != MLX5_WQ_TYPE_CYCLIC) {
3628 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params, NULL) ==
3629 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params, NULL))
3634 *old_params = new_channels.params;
3635 err = mlx5e_modify_tirs_lro(priv);
3639 err = mlx5e_safe_switch_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3641 mutex_unlock(&priv->state_lock);
3645 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3647 struct mlx5e_priv *priv = netdev_priv(netdev);
3650 mlx5e_enable_cvlan_filter(priv);
3652 mlx5e_disable_cvlan_filter(priv);
3657 #ifdef CONFIG_MLX5_ESWITCH
3658 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3660 struct mlx5e_priv *priv = netdev_priv(netdev);
3662 if (!enable && mlx5e_tc_num_filters(priv, MLX5E_TC_NIC_OFFLOAD)) {
3664 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3672 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3674 struct mlx5e_priv *priv = netdev_priv(netdev);
3675 struct mlx5_core_dev *mdev = priv->mdev;
3677 return mlx5_set_port_fcs(mdev, !enable);
3680 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3682 struct mlx5e_priv *priv = netdev_priv(netdev);
3685 mutex_lock(&priv->state_lock);
3687 priv->channels.params.scatter_fcs_en = enable;
3688 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3690 priv->channels.params.scatter_fcs_en = !enable;
3692 mutex_unlock(&priv->state_lock);
3697 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3699 struct mlx5e_priv *priv = netdev_priv(netdev);
3702 mutex_lock(&priv->state_lock);
3704 priv->channels.params.vlan_strip_disable = !enable;
3705 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3708 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3710 priv->channels.params.vlan_strip_disable = enable;
3713 mutex_unlock(&priv->state_lock);
3718 #ifdef CONFIG_MLX5_EN_ARFS
3719 static int set_feature_arfs(struct net_device *netdev, bool enable)
3721 struct mlx5e_priv *priv = netdev_priv(netdev);
3725 err = mlx5e_arfs_enable(priv);
3727 err = mlx5e_arfs_disable(priv);
3733 static int mlx5e_handle_feature(struct net_device *netdev,
3734 netdev_features_t *features,
3735 netdev_features_t wanted_features,
3736 netdev_features_t feature,
3737 mlx5e_feature_handler feature_handler)
3739 netdev_features_t changes = wanted_features ^ netdev->features;
3740 bool enable = !!(wanted_features & feature);
3743 if (!(changes & feature))
3746 err = feature_handler(netdev, enable);
3748 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3749 enable ? "Enable" : "Disable", &feature, err);
3753 MLX5E_SET_FEATURE(features, feature, enable);
3757 int mlx5e_set_features(struct net_device *netdev, netdev_features_t features)
3759 netdev_features_t oper_features = netdev->features;
3762 #define MLX5E_HANDLE_FEATURE(feature, handler) \
3763 mlx5e_handle_feature(netdev, &oper_features, features, feature, handler)
3765 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
3766 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
3767 set_feature_cvlan_filter);
3768 #ifdef CONFIG_MLX5_ESWITCH
3769 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_tc_num_filters);
3771 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
3772 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
3773 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3774 #ifdef CONFIG_MLX5_EN_ARFS
3775 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
3779 netdev->features = oper_features;
3786 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3787 netdev_features_t features)
3789 struct mlx5e_priv *priv = netdev_priv(netdev);
3790 struct mlx5e_params *params;
3792 mutex_lock(&priv->state_lock);
3793 params = &priv->channels.params;
3794 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3795 /* HW strips the outer C-tag header, this is a problem
3796 * for S-tag traffic.
3798 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3799 if (!params->vlan_strip_disable)
3800 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3802 if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3803 features &= ~NETIF_F_LRO;
3805 netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n");
3808 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
3809 features &= ~NETIF_F_RXHASH;
3810 if (netdev->features & NETIF_F_RXHASH)
3811 netdev_warn(netdev, "Disabling rxhash, not supported when CQE compress is active\n");
3814 mutex_unlock(&priv->state_lock);
3819 static bool mlx5e_xsk_validate_mtu(struct net_device *netdev,
3820 struct mlx5e_channels *chs,
3821 struct mlx5e_params *new_params,
3822 struct mlx5_core_dev *mdev)
3826 for (ix = 0; ix < chs->params.num_channels; ix++) {
3827 struct xdp_umem *umem = mlx5e_xsk_get_umem(&chs->params, chs->params.xsk, ix);
3828 struct mlx5e_xsk_param xsk;
3833 mlx5e_build_xsk_param(umem, &xsk);
3835 if (!mlx5e_validate_xsk_param(new_params, &xsk, mdev)) {
3836 u32 hr = mlx5e_get_linear_rq_headroom(new_params, &xsk);
3837 int max_mtu_frame, max_mtu_page, max_mtu;
3839 /* Two criteria must be met:
3840 * 1. HW MTU + all headrooms <= XSK frame size.
3841 * 2. Size of SKBs allocated on XDP_PASS <= PAGE_SIZE.
3843 max_mtu_frame = MLX5E_HW2SW_MTU(new_params, xsk.chunk_size - hr);
3844 max_mtu_page = mlx5e_xdp_max_mtu(new_params, &xsk);
3845 max_mtu = min(max_mtu_frame, max_mtu_page);
3847 netdev_err(netdev, "MTU %d is too big for an XSK running on channel %hu. Try MTU <= %d\n",
3848 new_params->sw_mtu, ix, max_mtu);
3856 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu,
3857 change_hw_mtu_cb set_mtu_cb)
3859 struct mlx5e_priv *priv = netdev_priv(netdev);
3860 struct mlx5e_channels new_channels = {};
3861 struct mlx5e_params *params;
3865 mutex_lock(&priv->state_lock);
3867 params = &priv->channels.params;
3869 reset = !params->lro_en;
3870 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3872 new_channels.params = *params;
3873 new_channels.params.sw_mtu = new_mtu;
3875 if (params->xdp_prog &&
3876 !mlx5e_rx_is_linear_skb(&new_channels.params, NULL)) {
3877 netdev_err(netdev, "MTU(%d) > %d is not allowed while XDP enabled\n",
3878 new_mtu, mlx5e_xdp_max_mtu(params, NULL));
3883 if (priv->xsk.refcnt &&
3884 !mlx5e_xsk_validate_mtu(netdev, &priv->channels,
3885 &new_channels.params, priv->mdev)) {
3890 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
3891 bool is_linear = mlx5e_rx_mpwqe_is_linear_skb(priv->mdev,
3892 &new_channels.params,
3894 u8 ppw_old = mlx5e_mpwqe_log_pkts_per_wqe(params, NULL);
3895 u8 ppw_new = mlx5e_mpwqe_log_pkts_per_wqe(&new_channels.params, NULL);
3897 /* If XSK is active, XSK RQs are linear. */
3898 is_linear |= priv->xsk.refcnt;
3900 /* Always reset in linear mode - hw_mtu is used in data path. */
3901 reset = reset && (is_linear || (ppw_old != ppw_new));
3905 params->sw_mtu = new_mtu;
3908 netdev->mtu = params->sw_mtu;
3912 err = mlx5e_safe_switch_channels(priv, &new_channels, set_mtu_cb);
3916 netdev->mtu = new_channels.params.sw_mtu;
3919 mutex_unlock(&priv->state_lock);
3923 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu)
3925 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
3928 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3930 struct hwtstamp_config config;
3933 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz) ||
3934 (mlx5_clock_get_ptp_index(priv->mdev) == -1))
3937 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3940 /* TX HW timestamp */
3941 switch (config.tx_type) {
3942 case HWTSTAMP_TX_OFF:
3943 case HWTSTAMP_TX_ON:
3949 mutex_lock(&priv->state_lock);
3950 /* RX HW timestamp */
3951 switch (config.rx_filter) {
3952 case HWTSTAMP_FILTER_NONE:
3953 /* Reset CQE compression to Admin default */
3954 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3956 case HWTSTAMP_FILTER_ALL:
3957 case HWTSTAMP_FILTER_SOME:
3958 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3959 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3960 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3961 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3962 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3963 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3964 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3965 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3966 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3967 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3968 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3969 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3970 case HWTSTAMP_FILTER_NTP_ALL:
3971 /* Disable CQE compression */
3972 netdev_warn(priv->netdev, "Disabling cqe compression");
3973 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
3975 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
3976 mutex_unlock(&priv->state_lock);
3979 config.rx_filter = HWTSTAMP_FILTER_ALL;
3982 mutex_unlock(&priv->state_lock);
3986 memcpy(&priv->tstamp, &config, sizeof(config));
3987 mutex_unlock(&priv->state_lock);
3989 /* might need to fix some features */
3990 netdev_update_features(priv->netdev);
3992 return copy_to_user(ifr->ifr_data, &config,
3993 sizeof(config)) ? -EFAULT : 0;
3996 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
3998 struct hwtstamp_config *cfg = &priv->tstamp;
4000 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
4003 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
4006 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4008 struct mlx5e_priv *priv = netdev_priv(dev);
4012 return mlx5e_hwstamp_set(priv, ifr);
4014 return mlx5e_hwstamp_get(priv, ifr);
4020 #ifdef CONFIG_MLX5_ESWITCH
4021 int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
4023 struct mlx5e_priv *priv = netdev_priv(dev);
4024 struct mlx5_core_dev *mdev = priv->mdev;
4026 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
4029 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
4032 struct mlx5e_priv *priv = netdev_priv(dev);
4033 struct mlx5_core_dev *mdev = priv->mdev;
4035 if (vlan_proto != htons(ETH_P_8021Q))
4036 return -EPROTONOSUPPORT;
4038 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
4042 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
4044 struct mlx5e_priv *priv = netdev_priv(dev);
4045 struct mlx5_core_dev *mdev = priv->mdev;
4047 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
4050 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
4052 struct mlx5e_priv *priv = netdev_priv(dev);
4053 struct mlx5_core_dev *mdev = priv->mdev;
4055 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
4058 int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
4061 struct mlx5e_priv *priv = netdev_priv(dev);
4062 struct mlx5_core_dev *mdev = priv->mdev;
4064 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
4065 max_tx_rate, min_tx_rate);
4068 static int mlx5_vport_link2ifla(u8 esw_link)
4071 case MLX5_VPORT_ADMIN_STATE_DOWN:
4072 return IFLA_VF_LINK_STATE_DISABLE;
4073 case MLX5_VPORT_ADMIN_STATE_UP:
4074 return IFLA_VF_LINK_STATE_ENABLE;
4076 return IFLA_VF_LINK_STATE_AUTO;
4079 static int mlx5_ifla_link2vport(u8 ifla_link)
4081 switch (ifla_link) {
4082 case IFLA_VF_LINK_STATE_DISABLE:
4083 return MLX5_VPORT_ADMIN_STATE_DOWN;
4084 case IFLA_VF_LINK_STATE_ENABLE:
4085 return MLX5_VPORT_ADMIN_STATE_UP;
4087 return MLX5_VPORT_ADMIN_STATE_AUTO;
4090 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
4093 struct mlx5e_priv *priv = netdev_priv(dev);
4094 struct mlx5_core_dev *mdev = priv->mdev;
4096 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
4097 mlx5_ifla_link2vport(link_state));
4100 int mlx5e_get_vf_config(struct net_device *dev,
4101 int vf, struct ifla_vf_info *ivi)
4103 struct mlx5e_priv *priv = netdev_priv(dev);
4104 struct mlx5_core_dev *mdev = priv->mdev;
4107 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
4110 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
4114 int mlx5e_get_vf_stats(struct net_device *dev,
4115 int vf, struct ifla_vf_stats *vf_stats)
4117 struct mlx5e_priv *priv = netdev_priv(dev);
4118 struct mlx5_core_dev *mdev = priv->mdev;
4120 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
4125 struct mlx5e_vxlan_work {
4126 struct work_struct work;
4127 struct mlx5e_priv *priv;
4131 static void mlx5e_vxlan_add_work(struct work_struct *work)
4133 struct mlx5e_vxlan_work *vxlan_work =
4134 container_of(work, struct mlx5e_vxlan_work, work);
4135 struct mlx5e_priv *priv = vxlan_work->priv;
4136 u16 port = vxlan_work->port;
4138 mutex_lock(&priv->state_lock);
4139 mlx5_vxlan_add_port(priv->mdev->vxlan, port);
4140 mutex_unlock(&priv->state_lock);
4145 static void mlx5e_vxlan_del_work(struct work_struct *work)
4147 struct mlx5e_vxlan_work *vxlan_work =
4148 container_of(work, struct mlx5e_vxlan_work, work);
4149 struct mlx5e_priv *priv = vxlan_work->priv;
4150 u16 port = vxlan_work->port;
4152 mutex_lock(&priv->state_lock);
4153 mlx5_vxlan_del_port(priv->mdev->vxlan, port);
4154 mutex_unlock(&priv->state_lock);
4158 static void mlx5e_vxlan_queue_work(struct mlx5e_priv *priv, u16 port, int add)
4160 struct mlx5e_vxlan_work *vxlan_work;
4162 vxlan_work = kmalloc(sizeof(*vxlan_work), GFP_ATOMIC);
4167 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_add_work);
4169 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_del_work);
4171 vxlan_work->priv = priv;
4172 vxlan_work->port = port;
4173 queue_work(priv->wq, &vxlan_work->work);
4176 void mlx5e_add_vxlan_port(struct net_device *netdev, struct udp_tunnel_info *ti)
4178 struct mlx5e_priv *priv = netdev_priv(netdev);
4180 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4183 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4186 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 1);
4189 void mlx5e_del_vxlan_port(struct net_device *netdev, struct udp_tunnel_info *ti)
4191 struct mlx5e_priv *priv = netdev_priv(netdev);
4193 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4196 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4199 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 0);
4202 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
4203 struct sk_buff *skb,
4204 netdev_features_t features)
4206 unsigned int offset = 0;
4207 struct udphdr *udph;
4211 switch (vlan_get_protocol(skb)) {
4212 case htons(ETH_P_IP):
4213 proto = ip_hdr(skb)->protocol;
4215 case htons(ETH_P_IPV6):
4216 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
4226 udph = udp_hdr(skb);
4227 port = be16_to_cpu(udph->dest);
4229 /* Verify if UDP port is being offloaded by HW */
4230 if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, port))
4233 #if IS_ENABLED(CONFIG_GENEVE)
4234 /* Support Geneve offload for default UDP port */
4235 if (port == GENEVE_UDP_PORT && mlx5_geneve_tx_allowed(priv->mdev))
4241 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
4242 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4245 netdev_features_t mlx5e_features_check(struct sk_buff *skb,
4246 struct net_device *netdev,
4247 netdev_features_t features)
4249 struct mlx5e_priv *priv = netdev_priv(netdev);
4251 features = vlan_features_check(skb, features);
4252 features = vxlan_features_check(skb, features);
4254 #ifdef CONFIG_MLX5_EN_IPSEC
4255 if (mlx5e_ipsec_feature_check(skb, netdev, features))
4259 /* Validate if the tunneled packet is being offloaded by HW */
4260 if (skb->encapsulation &&
4261 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
4262 return mlx5e_tunnel_features_check(priv, skb, features);
4267 static void mlx5e_tx_timeout_work(struct work_struct *work)
4269 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
4271 bool report_failed = false;
4276 mutex_lock(&priv->state_lock);
4278 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
4281 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
4282 struct netdev_queue *dev_queue =
4283 netdev_get_tx_queue(priv->netdev, i);
4284 struct mlx5e_txqsq *sq = priv->txq2sq[i];
4286 if (!netif_xmit_stopped(dev_queue))
4289 if (mlx5e_tx_reporter_timeout(sq))
4290 report_failed = true;
4296 err = mlx5e_safe_reopen_channels(priv);
4298 netdev_err(priv->netdev,
4299 "mlx5e_safe_reopen_channels failed recovering from a tx_timeout, err(%d).\n",
4303 mutex_unlock(&priv->state_lock);
4307 static void mlx5e_tx_timeout(struct net_device *dev)
4309 struct mlx5e_priv *priv = netdev_priv(dev);
4311 netdev_err(dev, "TX timeout detected\n");
4312 queue_work(priv->wq, &priv->tx_timeout_work);
4315 static int mlx5e_xdp_allowed(struct mlx5e_priv *priv, struct bpf_prog *prog)
4317 struct net_device *netdev = priv->netdev;
4318 struct mlx5e_channels new_channels = {};
4320 if (priv->channels.params.lro_en) {
4321 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
4325 if (MLX5_IPSEC_DEV(priv->mdev)) {
4326 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
4330 new_channels.params = priv->channels.params;
4331 new_channels.params.xdp_prog = prog;
4333 /* No XSK params: AF_XDP can't be enabled yet at the point of setting
4336 if (!mlx5e_rx_is_linear_skb(&new_channels.params, NULL)) {
4337 netdev_warn(netdev, "XDP is not allowed with MTU(%d) > %d\n",
4338 new_channels.params.sw_mtu,
4339 mlx5e_xdp_max_mtu(&new_channels.params, NULL));
4346 static int mlx5e_xdp_update_state(struct mlx5e_priv *priv)
4348 if (priv->channels.params.xdp_prog)
4349 mlx5e_xdp_set_open(priv);
4351 mlx5e_xdp_set_closed(priv);
4356 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
4358 struct mlx5e_priv *priv = netdev_priv(netdev);
4359 struct bpf_prog *old_prog;
4360 bool reset, was_opened;
4364 mutex_lock(&priv->state_lock);
4367 err = mlx5e_xdp_allowed(priv, prog);
4372 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
4373 /* no need for full reset when exchanging programs */
4374 reset = (!priv->channels.params.xdp_prog || !prog);
4376 if (was_opened && !reset) {
4377 /* num_channels is invariant here, so we can take the
4378 * batched reference right upfront.
4380 prog = bpf_prog_add(prog, priv->channels.num);
4382 err = PTR_ERR(prog);
4387 if (was_opened && reset) {
4388 struct mlx5e_channels new_channels = {};
4390 new_channels.params = priv->channels.params;
4391 new_channels.params.xdp_prog = prog;
4392 mlx5e_set_rq_type(priv->mdev, &new_channels.params);
4393 old_prog = priv->channels.params.xdp_prog;
4395 err = mlx5e_safe_switch_channels(priv, &new_channels, mlx5e_xdp_update_state);
4399 /* exchange programs, extra prog reference we got from caller
4400 * as long as we don't fail from this point onwards.
4402 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
4406 bpf_prog_put(old_prog);
4408 if (!was_opened && reset) /* change RQ type according to priv->xdp_prog */
4409 mlx5e_set_rq_type(priv->mdev, &priv->channels.params);
4411 if (!was_opened || reset)
4414 /* exchanging programs w/o reset, we update ref counts on behalf
4415 * of the channels RQs here.
4417 for (i = 0; i < priv->channels.num; i++) {
4418 struct mlx5e_channel *c = priv->channels.c[i];
4419 bool xsk_open = test_bit(MLX5E_CHANNEL_STATE_XSK, c->state);
4421 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4423 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->xskrq.state);
4424 napi_synchronize(&c->napi);
4425 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
4427 old_prog = xchg(&c->rq.xdp_prog, prog);
4429 bpf_prog_put(old_prog);
4432 old_prog = xchg(&c->xskrq.xdp_prog, prog);
4434 bpf_prog_put(old_prog);
4437 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4439 set_bit(MLX5E_RQ_STATE_ENABLED, &c->xskrq.state);
4440 /* napi_schedule in case we have missed anything */
4441 napi_schedule(&c->napi);
4445 mutex_unlock(&priv->state_lock);
4449 static u32 mlx5e_xdp_query(struct net_device *dev)
4451 struct mlx5e_priv *priv = netdev_priv(dev);
4452 const struct bpf_prog *xdp_prog;
4455 mutex_lock(&priv->state_lock);
4456 xdp_prog = priv->channels.params.xdp_prog;
4458 prog_id = xdp_prog->aux->id;
4459 mutex_unlock(&priv->state_lock);
4464 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4466 switch (xdp->command) {
4467 case XDP_SETUP_PROG:
4468 return mlx5e_xdp_set(dev, xdp->prog);
4469 case XDP_QUERY_PROG:
4470 xdp->prog_id = mlx5e_xdp_query(dev);
4472 case XDP_SETUP_XSK_UMEM:
4473 return mlx5e_xsk_setup_umem(dev, xdp->xsk.umem,
4480 #ifdef CONFIG_MLX5_ESWITCH
4481 static int mlx5e_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4482 struct net_device *dev, u32 filter_mask,
4485 struct mlx5e_priv *priv = netdev_priv(dev);
4486 struct mlx5_core_dev *mdev = priv->mdev;
4490 err = mlx5_eswitch_get_vepa(mdev->priv.eswitch, &setting);
4493 mode = setting ? BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB;
4494 return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
4496 0, 0, nlflags, filter_mask, NULL);
4499 static int mlx5e_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
4500 u16 flags, struct netlink_ext_ack *extack)
4502 struct mlx5e_priv *priv = netdev_priv(dev);
4503 struct mlx5_core_dev *mdev = priv->mdev;
4504 struct nlattr *attr, *br_spec;
4505 u16 mode = BRIDGE_MODE_UNDEF;
4509 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4513 nla_for_each_nested(attr, br_spec, rem) {
4514 if (nla_type(attr) != IFLA_BRIDGE_MODE)
4517 if (nla_len(attr) < sizeof(mode))
4520 mode = nla_get_u16(attr);
4521 if (mode > BRIDGE_MODE_VEPA)
4527 if (mode == BRIDGE_MODE_UNDEF)
4530 setting = (mode == BRIDGE_MODE_VEPA) ? 1 : 0;
4531 return mlx5_eswitch_set_vepa(mdev->priv.eswitch, setting);
4535 const struct net_device_ops mlx5e_netdev_ops = {
4536 .ndo_open = mlx5e_open,
4537 .ndo_stop = mlx5e_close,
4538 .ndo_start_xmit = mlx5e_xmit,
4539 .ndo_setup_tc = mlx5e_setup_tc,
4540 .ndo_select_queue = mlx5e_select_queue,
4541 .ndo_get_stats64 = mlx5e_get_stats,
4542 .ndo_set_rx_mode = mlx5e_set_rx_mode,
4543 .ndo_set_mac_address = mlx5e_set_mac,
4544 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
4545 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
4546 .ndo_set_features = mlx5e_set_features,
4547 .ndo_fix_features = mlx5e_fix_features,
4548 .ndo_change_mtu = mlx5e_change_nic_mtu,
4549 .ndo_do_ioctl = mlx5e_ioctl,
4550 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
4551 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
4552 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
4553 .ndo_features_check = mlx5e_features_check,
4554 .ndo_tx_timeout = mlx5e_tx_timeout,
4555 .ndo_bpf = mlx5e_xdp,
4556 .ndo_xdp_xmit = mlx5e_xdp_xmit,
4557 .ndo_xsk_async_xmit = mlx5e_xsk_async_xmit,
4558 #ifdef CONFIG_MLX5_EN_ARFS
4559 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
4561 #ifdef CONFIG_MLX5_ESWITCH
4562 .ndo_bridge_setlink = mlx5e_bridge_setlink,
4563 .ndo_bridge_getlink = mlx5e_bridge_getlink,
4565 /* SRIOV E-Switch NDOs */
4566 .ndo_set_vf_mac = mlx5e_set_vf_mac,
4567 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
4568 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
4569 .ndo_set_vf_trust = mlx5e_set_vf_trust,
4570 .ndo_set_vf_rate = mlx5e_set_vf_rate,
4571 .ndo_get_vf_config = mlx5e_get_vf_config,
4572 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
4573 .ndo_get_vf_stats = mlx5e_get_vf_stats,
4577 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
4579 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
4581 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
4582 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
4583 !MLX5_CAP_ETH(mdev, csum_cap) ||
4584 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
4585 !MLX5_CAP_ETH(mdev, vlan_cap) ||
4586 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
4587 MLX5_CAP_FLOWTABLE(mdev,
4588 flow_table_properties_nic_receive.max_ft_level)
4590 mlx5_core_warn(mdev,
4591 "Not creating net device, some required device capabilities are missing\n");
4594 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
4595 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
4596 if (!MLX5_CAP_GEN(mdev, cq_moderation))
4597 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
4602 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
4607 for (i = 0; i < len; i++)
4608 indirection_rqt[i] = i % num_channels;
4611 static bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
4616 mlx5e_port_max_linkspeed(mdev, &link_speed);
4617 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
4618 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
4619 link_speed, pci_bw);
4621 #define MLX5E_SLOW_PCI_RATIO (2)
4623 return link_speed && pci_bw &&
4624 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
4627 static struct dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode)
4629 struct dim_cq_moder moder;
4631 moder.cq_period_mode = cq_period_mode;
4632 moder.pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
4633 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
4634 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4635 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
4640 static struct dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
4642 struct dim_cq_moder moder;
4644 moder.cq_period_mode = cq_period_mode;
4645 moder.pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
4646 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
4647 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4648 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
4653 static u8 mlx5_to_net_dim_cq_period_mode(u8 cq_period_mode)
4655 return cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE ?
4656 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
4657 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4660 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4662 if (params->tx_dim_enabled) {
4663 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4665 params->tx_cq_moderation = net_dim_get_def_tx_moderation(dim_period_mode);
4667 params->tx_cq_moderation = mlx5e_get_def_tx_moderation(cq_period_mode);
4670 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
4671 params->tx_cq_moderation.cq_period_mode ==
4672 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4675 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4677 if (params->rx_dim_enabled) {
4678 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4680 params->rx_cq_moderation = net_dim_get_def_rx_moderation(dim_period_mode);
4682 params->rx_cq_moderation = mlx5e_get_def_rx_moderation(cq_period_mode);
4685 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
4686 params->rx_cq_moderation.cq_period_mode ==
4687 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4690 static u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
4694 /* The supported periods are organized in ascending order */
4695 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
4696 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
4699 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
4702 void mlx5e_build_rq_params(struct mlx5_core_dev *mdev,
4703 struct mlx5e_params *params)
4705 /* Prefer Striding RQ, unless any of the following holds:
4706 * - Striding RQ configuration is not possible/supported.
4707 * - Slow PCI heuristic.
4708 * - Legacy RQ would use linear SKB while Striding RQ would use non-linear.
4710 * No XSK params: checking the availability of striding RQ in general.
4712 if (!slow_pci_heuristic(mdev) &&
4713 mlx5e_striding_rq_possible(mdev, params) &&
4714 (mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ||
4715 !mlx5e_rx_is_linear_skb(params, NULL)))
4716 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, true);
4717 mlx5e_set_rq_type(mdev, params);
4718 mlx5e_init_rq_type_params(mdev, params);
4721 void mlx5e_build_rss_params(struct mlx5e_rss_params *rss_params,
4724 enum mlx5e_traffic_types tt;
4726 rss_params->hfunc = ETH_RSS_HASH_TOP;
4727 netdev_rss_key_fill(rss_params->toeplitz_hash_key,
4728 sizeof(rss_params->toeplitz_hash_key));
4729 mlx5e_build_default_indir_rqt(rss_params->indirection_rqt,
4730 MLX5E_INDIR_RQT_SIZE, num_channels);
4731 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
4732 rss_params->rx_hash_fields[tt] =
4733 tirc_default_config[tt].rx_hash_fields;
4736 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
4737 struct mlx5e_xsk *xsk,
4738 struct mlx5e_rss_params *rss_params,
4739 struct mlx5e_params *params,
4740 u16 max_channels, u16 mtu)
4742 u8 rx_cq_period_mode;
4744 params->sw_mtu = mtu;
4745 params->hard_mtu = MLX5E_ETH_HARD_MTU;
4746 params->num_channels = max_channels;
4750 params->log_sq_size = is_kdump_kernel() ?
4751 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
4752 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
4755 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE,
4756 MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe));
4758 /* set CQE compression */
4759 params->rx_cqe_compress_def = false;
4760 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
4761 MLX5_CAP_GEN(mdev, vport_group_manager))
4762 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
4764 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4765 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE, false);
4768 mlx5e_build_rq_params(mdev, params);
4772 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4773 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
4774 /* No XSK params: checking the availability of striding RQ in general. */
4775 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL))
4776 params->lro_en = !slow_pci_heuristic(mdev);
4778 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4780 /* CQ moderation params */
4781 rx_cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4782 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4783 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4784 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4785 params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4786 mlx5e_set_rx_cq_mode_params(params, rx_cq_period_mode);
4787 mlx5e_set_tx_cq_mode_params(params, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
4790 params->tx_min_inline_mode = mlx5e_params_calculate_tx_min_inline(mdev);
4793 mlx5e_build_rss_params(rss_params, params->num_channels);
4794 params->tunneled_offload_en =
4795 mlx5e_tunnel_inner_ft_supported(mdev);
4801 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4803 struct mlx5e_priv *priv = netdev_priv(netdev);
4805 mlx5_query_mac_address(priv->mdev, netdev->dev_addr);
4806 if (is_zero_ether_addr(netdev->dev_addr) &&
4807 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4808 eth_hw_addr_random(netdev);
4809 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4813 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4815 struct mlx5e_priv *priv = netdev_priv(netdev);
4816 struct mlx5_core_dev *mdev = priv->mdev;
4820 SET_NETDEV_DEV(netdev, mdev->device);
4822 netdev->netdev_ops = &mlx5e_netdev_ops;
4824 #ifdef CONFIG_MLX5_CORE_EN_DCB
4825 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4826 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4829 netdev->watchdog_timeo = 15 * HZ;
4831 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4833 netdev->vlan_features |= NETIF_F_SG;
4834 netdev->vlan_features |= NETIF_F_HW_CSUM;
4835 netdev->vlan_features |= NETIF_F_GRO;
4836 netdev->vlan_features |= NETIF_F_TSO;
4837 netdev->vlan_features |= NETIF_F_TSO6;
4838 netdev->vlan_features |= NETIF_F_RXCSUM;
4839 netdev->vlan_features |= NETIF_F_RXHASH;
4841 netdev->mpls_features |= NETIF_F_SG;
4842 netdev->mpls_features |= NETIF_F_HW_CSUM;
4843 netdev->mpls_features |= NETIF_F_TSO;
4844 netdev->mpls_features |= NETIF_F_TSO6;
4846 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
4847 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
4849 if (!!MLX5_CAP_ETH(mdev, lro_cap) &&
4850 mlx5e_check_fragmented_striding_rq_cap(mdev))
4851 netdev->vlan_features |= NETIF_F_LRO;
4853 netdev->hw_features = netdev->vlan_features;
4854 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4855 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4856 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4857 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4859 if (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev) ||
4860 MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4861 netdev->hw_enc_features |= NETIF_F_HW_CSUM;
4862 netdev->hw_enc_features |= NETIF_F_TSO;
4863 netdev->hw_enc_features |= NETIF_F_TSO6;
4864 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4867 if (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev)) {
4868 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4869 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4870 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4871 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4872 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4875 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4876 netdev->hw_features |= NETIF_F_GSO_GRE |
4877 NETIF_F_GSO_GRE_CSUM;
4878 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4879 NETIF_F_GSO_GRE_CSUM;
4880 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4881 NETIF_F_GSO_GRE_CSUM;
4884 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4885 netdev->gso_partial_features |= NETIF_F_GSO_UDP_L4;
4886 netdev->hw_features |= NETIF_F_GSO_UDP_L4;
4887 netdev->features |= NETIF_F_GSO_UDP_L4;
4889 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4892 netdev->hw_features |= NETIF_F_RXALL;
4894 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4895 netdev->hw_features |= NETIF_F_RXFCS;
4897 netdev->features = netdev->hw_features;
4898 if (!priv->channels.params.lro_en)
4899 netdev->features &= ~NETIF_F_LRO;
4902 netdev->features &= ~NETIF_F_RXALL;
4904 if (!priv->channels.params.scatter_fcs_en)
4905 netdev->features &= ~NETIF_F_RXFCS;
4907 /* prefere CQE compression over rxhash */
4908 if (MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS))
4909 netdev->features &= ~NETIF_F_RXHASH;
4911 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4912 if (FT_CAP(flow_modify_en) &&
4913 FT_CAP(modify_root) &&
4914 FT_CAP(identified_miss_table_mode) &&
4915 FT_CAP(flow_table_modify)) {
4916 #ifdef CONFIG_MLX5_ESWITCH
4917 netdev->hw_features |= NETIF_F_HW_TC;
4919 #ifdef CONFIG_MLX5_EN_ARFS
4920 netdev->hw_features |= NETIF_F_NTUPLE;
4924 netdev->features |= NETIF_F_HIGHDMA;
4925 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4927 netdev->priv_flags |= IFF_UNICAST_FLT;
4929 mlx5e_set_netdev_dev_addr(netdev);
4930 mlx5e_ipsec_build_netdev(priv);
4931 mlx5e_tls_build_netdev(priv);
4934 void mlx5e_create_q_counters(struct mlx5e_priv *priv)
4936 struct mlx5_core_dev *mdev = priv->mdev;
4939 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4941 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4942 priv->q_counter = 0;
4945 err = mlx5_core_alloc_q_counter(mdev, &priv->drop_rq_q_counter);
4947 mlx5_core_warn(mdev, "alloc drop RQ counter failed, %d\n", err);
4948 priv->drop_rq_q_counter = 0;
4952 void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
4954 if (priv->q_counter)
4955 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4957 if (priv->drop_rq_q_counter)
4958 mlx5_core_dealloc_q_counter(priv->mdev, priv->drop_rq_q_counter);
4961 static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
4962 struct net_device *netdev,
4963 const struct mlx5e_profile *profile,
4966 struct mlx5e_priv *priv = netdev_priv(netdev);
4967 struct mlx5e_rss_params *rss = &priv->rss_params;
4970 err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
4974 mlx5e_build_nic_params(mdev, &priv->xsk, rss, &priv->channels.params,
4975 mlx5e_get_netdev_max_channels(netdev),
4978 mlx5e_timestamp_init(priv);
4980 err = mlx5e_ipsec_init(priv);
4982 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
4983 err = mlx5e_tls_init(priv);
4985 mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
4986 mlx5e_build_nic_netdev(netdev);
4987 mlx5e_build_tc2txq_maps(priv);
4992 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
4994 mlx5e_tls_cleanup(priv);
4995 mlx5e_ipsec_cleanup(priv);
4996 mlx5e_netdev_cleanup(priv->netdev, priv);
4999 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
5001 struct mlx5_core_dev *mdev = priv->mdev;
5004 mlx5e_create_q_counters(priv);
5006 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
5008 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
5009 goto err_destroy_q_counters;
5012 err = mlx5e_create_indirect_rqt(priv);
5014 goto err_close_drop_rq;
5016 err = mlx5e_create_direct_rqts(priv, priv->direct_tir);
5018 goto err_destroy_indirect_rqts;
5020 err = mlx5e_create_indirect_tirs(priv, true);
5022 goto err_destroy_direct_rqts;
5024 err = mlx5e_create_direct_tirs(priv, priv->direct_tir);
5026 goto err_destroy_indirect_tirs;
5028 err = mlx5e_create_direct_rqts(priv, priv->xsk_tir);
5030 goto err_destroy_direct_tirs;
5032 err = mlx5e_create_direct_tirs(priv, priv->xsk_tir);
5034 goto err_destroy_xsk_rqts;
5036 err = mlx5e_create_flow_steering(priv);
5038 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
5039 goto err_destroy_xsk_tirs;
5042 err = mlx5e_tc_nic_init(priv);
5044 goto err_destroy_flow_steering;
5048 err_destroy_flow_steering:
5049 mlx5e_destroy_flow_steering(priv);
5050 err_destroy_xsk_tirs:
5051 mlx5e_destroy_direct_tirs(priv, priv->xsk_tir);
5052 err_destroy_xsk_rqts:
5053 mlx5e_destroy_direct_rqts(priv, priv->xsk_tir);
5054 err_destroy_direct_tirs:
5055 mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
5056 err_destroy_indirect_tirs:
5057 mlx5e_destroy_indirect_tirs(priv, true);
5058 err_destroy_direct_rqts:
5059 mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
5060 err_destroy_indirect_rqts:
5061 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
5063 mlx5e_close_drop_rq(&priv->drop_rq);
5064 err_destroy_q_counters:
5065 mlx5e_destroy_q_counters(priv);
5069 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
5071 mlx5e_tc_nic_cleanup(priv);
5072 mlx5e_destroy_flow_steering(priv);
5073 mlx5e_destroy_direct_tirs(priv, priv->xsk_tir);
5074 mlx5e_destroy_direct_rqts(priv, priv->xsk_tir);
5075 mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
5076 mlx5e_destroy_indirect_tirs(priv, true);
5077 mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
5078 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
5079 mlx5e_close_drop_rq(&priv->drop_rq);
5080 mlx5e_destroy_q_counters(priv);
5083 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
5087 err = mlx5e_create_tises(priv);
5089 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
5093 #ifdef CONFIG_MLX5_CORE_EN_DCB
5094 mlx5e_dcbnl_initialize(priv);
5096 mlx5e_tx_reporter_create(priv);
5100 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
5102 struct net_device *netdev = priv->netdev;
5103 struct mlx5_core_dev *mdev = priv->mdev;
5105 mlx5e_init_l2_addr(priv);
5107 /* Marking the link as currently not needed by the Driver */
5108 if (!netif_running(netdev))
5109 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
5111 mlx5e_set_netdev_mtu_boundaries(priv);
5112 mlx5e_set_dev_port_mtu(priv);
5114 mlx5_lag_add(mdev, netdev);
5116 mlx5e_enable_async_events(priv);
5117 if (mlx5e_monitor_counter_supported(priv))
5118 mlx5e_monitor_counter_init(priv);
5120 if (netdev->reg_state != NETREG_REGISTERED)
5122 #ifdef CONFIG_MLX5_CORE_EN_DCB
5123 mlx5e_dcbnl_init_app(priv);
5126 queue_work(priv->wq, &priv->set_rx_mode_work);
5129 if (netif_running(netdev))
5131 netif_device_attach(netdev);
5135 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
5137 struct mlx5_core_dev *mdev = priv->mdev;
5139 #ifdef CONFIG_MLX5_CORE_EN_DCB
5140 if (priv->netdev->reg_state == NETREG_REGISTERED)
5141 mlx5e_dcbnl_delete_app(priv);
5145 if (netif_running(priv->netdev))
5146 mlx5e_close(priv->netdev);
5147 netif_device_detach(priv->netdev);
5150 queue_work(priv->wq, &priv->set_rx_mode_work);
5152 if (mlx5e_monitor_counter_supported(priv))
5153 mlx5e_monitor_counter_cleanup(priv);
5155 mlx5e_disable_async_events(priv);
5156 mlx5_lag_remove(mdev);
5159 int mlx5e_update_nic_rx(struct mlx5e_priv *priv)
5161 return mlx5e_refresh_tirs(priv, false);
5164 static const struct mlx5e_profile mlx5e_nic_profile = {
5165 .init = mlx5e_nic_init,
5166 .cleanup = mlx5e_nic_cleanup,
5167 .init_rx = mlx5e_init_nic_rx,
5168 .cleanup_rx = mlx5e_cleanup_nic_rx,
5169 .init_tx = mlx5e_init_nic_tx,
5170 .cleanup_tx = mlx5e_cleanup_nic_tx,
5171 .enable = mlx5e_nic_enable,
5172 .disable = mlx5e_nic_disable,
5173 .update_rx = mlx5e_update_nic_rx,
5174 .update_stats = mlx5e_update_ndo_stats,
5175 .update_carrier = mlx5e_update_carrier,
5176 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
5177 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
5178 .max_tc = MLX5E_MAX_NUM_TC,
5181 /* mlx5e generic netdev management API (move to en_common.c) */
5183 /* mlx5e_netdev_init/cleanup must be called from profile->init/cleanup callbacks */
5184 int mlx5e_netdev_init(struct net_device *netdev,
5185 struct mlx5e_priv *priv,
5186 struct mlx5_core_dev *mdev,
5187 const struct mlx5e_profile *profile,
5192 priv->netdev = netdev;
5193 priv->profile = profile;
5194 priv->ppriv = ppriv;
5195 priv->msglevel = MLX5E_MSG_LEVEL;
5196 priv->max_opened_tc = 1;
5198 mutex_init(&priv->state_lock);
5199 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
5200 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
5201 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
5202 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
5204 priv->wq = create_singlethread_workqueue("mlx5e");
5209 netif_carrier_off(netdev);
5211 #ifdef CONFIG_MLX5_EN_ARFS
5212 netdev->rx_cpu_rmap = mlx5_eq_table_get_rmap(mdev);
5218 void mlx5e_netdev_cleanup(struct net_device *netdev, struct mlx5e_priv *priv)
5220 destroy_workqueue(priv->wq);
5223 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
5224 const struct mlx5e_profile *profile,
5228 struct net_device *netdev;
5231 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
5232 nch * profile->max_tc,
5233 nch * MLX5E_NUM_RQ_GROUPS);
5235 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
5239 err = profile->init(mdev, netdev, profile, ppriv);
5241 mlx5_core_err(mdev, "failed to init mlx5e profile %d\n", err);
5242 goto err_free_netdev;
5248 free_netdev(netdev);
5253 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
5255 const struct mlx5e_profile *profile;
5259 profile = priv->profile;
5260 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
5262 /* max number of channels may have changed */
5263 max_nch = mlx5e_get_max_num_channels(priv->mdev);
5264 if (priv->channels.params.num_channels > max_nch) {
5265 mlx5_core_warn(priv->mdev, "MLX5E: Reducing number of channels to %d\n", max_nch);
5266 priv->channels.params.num_channels = max_nch;
5267 mlx5e_build_default_indir_rqt(priv->rss_params.indirection_rqt,
5268 MLX5E_INDIR_RQT_SIZE, max_nch);
5271 err = profile->init_tx(priv);
5275 err = profile->init_rx(priv);
5277 goto err_cleanup_tx;
5279 if (profile->enable)
5280 profile->enable(priv);
5285 profile->cleanup_tx(priv);
5291 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
5293 const struct mlx5e_profile *profile = priv->profile;
5295 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
5297 if (profile->disable)
5298 profile->disable(priv);
5299 flush_workqueue(priv->wq);
5301 profile->cleanup_rx(priv);
5302 profile->cleanup_tx(priv);
5303 cancel_work_sync(&priv->update_stats_work);
5306 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
5308 const struct mlx5e_profile *profile = priv->profile;
5309 struct net_device *netdev = priv->netdev;
5311 if (profile->cleanup)
5312 profile->cleanup(priv);
5313 free_netdev(netdev);
5316 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
5317 * hardware contexts and to connect it to the current netdev.
5319 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
5321 struct mlx5e_priv *priv = vpriv;
5322 struct net_device *netdev = priv->netdev;
5325 if (netif_device_present(netdev))
5328 err = mlx5e_create_mdev_resources(mdev);
5332 err = mlx5e_attach_netdev(priv);
5334 mlx5e_destroy_mdev_resources(mdev);
5341 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
5343 struct mlx5e_priv *priv = vpriv;
5344 struct net_device *netdev = priv->netdev;
5346 #ifdef CONFIG_MLX5_ESWITCH
5347 if (MLX5_ESWITCH_MANAGER(mdev) && vpriv == mdev)
5351 if (!netif_device_present(netdev))
5354 mlx5e_detach_netdev(priv);
5355 mlx5e_destroy_mdev_resources(mdev);
5358 static void *mlx5e_add(struct mlx5_core_dev *mdev)
5360 struct net_device *netdev;
5365 err = mlx5e_check_required_hca_cap(mdev);
5369 #ifdef CONFIG_MLX5_ESWITCH
5370 if (MLX5_ESWITCH_MANAGER(mdev) &&
5371 mlx5_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
5372 mlx5e_rep_register_vport_reps(mdev);
5377 nch = mlx5e_get_max_num_channels(mdev);
5378 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, nch, NULL);
5380 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
5384 priv = netdev_priv(netdev);
5386 err = mlx5e_attach(mdev, priv);
5388 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
5389 goto err_destroy_netdev;
5392 err = register_netdev(netdev);
5394 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
5398 #ifdef CONFIG_MLX5_CORE_EN_DCB
5399 mlx5e_dcbnl_init_app(priv);
5404 mlx5e_detach(mdev, priv);
5406 mlx5e_destroy_netdev(priv);
5410 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
5412 struct mlx5e_priv *priv;
5414 #ifdef CONFIG_MLX5_ESWITCH
5415 if (MLX5_ESWITCH_MANAGER(mdev) && vpriv == mdev) {
5416 mlx5e_rep_unregister_vport_reps(mdev);
5421 #ifdef CONFIG_MLX5_CORE_EN_DCB
5422 mlx5e_dcbnl_delete_app(priv);
5424 unregister_netdev(priv->netdev);
5425 mlx5e_detach(mdev, vpriv);
5426 mlx5e_destroy_netdev(priv);
5429 static struct mlx5_interface mlx5e_interface = {
5431 .remove = mlx5e_remove,
5432 .attach = mlx5e_attach,
5433 .detach = mlx5e_detach,
5434 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
5437 void mlx5e_init(void)
5439 mlx5e_ipsec_build_inverse_table();
5440 mlx5e_build_ptys2ethtool_map();
5441 mlx5_register_interface(&mlx5e_interface);
5444 void mlx5e_cleanup(void)
5446 mlx5_unregister_interface(&mlx5e_interface);