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/health.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"
65 #include "en/hv_vhca_stats.h"
68 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
70 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
71 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
72 MLX5_CAP_ETH(mdev, reg_umr_sq);
73 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
74 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
79 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
80 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
86 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
87 struct mlx5e_params *params)
89 params->log_rq_mtu_frames = is_kdump_kernel() ?
90 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
91 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
93 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
94 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
95 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
96 BIT(mlx5e_mpwqe_get_log_rq_size(params, NULL)) :
97 BIT(params->log_rq_mtu_frames),
98 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params, NULL)),
99 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
102 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
103 struct mlx5e_params *params)
105 if (!mlx5e_check_fragmented_striding_rq_cap(mdev))
108 if (MLX5_IPSEC_DEV(mdev))
111 if (params->xdp_prog) {
112 /* XSK params are not considered here. If striding RQ is in use,
113 * and an XSK is being opened, mlx5e_rx_mpwqe_is_linear_skb will
114 * be called with the known XSK params.
116 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL))
123 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
125 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
126 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
127 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
131 void mlx5e_update_carrier(struct mlx5e_priv *priv)
133 struct mlx5_core_dev *mdev = priv->mdev;
136 port_state = mlx5_query_vport_state(mdev,
137 MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT,
140 if (port_state == VPORT_STATE_UP) {
141 netdev_info(priv->netdev, "Link up\n");
142 netif_carrier_on(priv->netdev);
144 netdev_info(priv->netdev, "Link down\n");
145 netif_carrier_off(priv->netdev);
149 static void mlx5e_update_carrier_work(struct work_struct *work)
151 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
152 update_carrier_work);
154 mutex_lock(&priv->state_lock);
155 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
156 if (priv->profile->update_carrier)
157 priv->profile->update_carrier(priv);
158 mutex_unlock(&priv->state_lock);
161 void mlx5e_update_stats(struct mlx5e_priv *priv)
165 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
166 if (mlx5e_stats_grps[i].update_stats)
167 mlx5e_stats_grps[i].update_stats(priv);
170 void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
174 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
175 if (mlx5e_stats_grps[i].update_stats_mask &
176 MLX5E_NDO_UPDATE_STATS)
177 mlx5e_stats_grps[i].update_stats(priv);
180 static void mlx5e_update_stats_work(struct work_struct *work)
182 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
185 mutex_lock(&priv->state_lock);
186 priv->profile->update_stats(priv);
187 mutex_unlock(&priv->state_lock);
190 void mlx5e_queue_update_stats(struct mlx5e_priv *priv)
192 if (!priv->profile->update_stats)
195 if (unlikely(test_bit(MLX5E_STATE_DESTROYING, &priv->state)))
198 queue_work(priv->wq, &priv->update_stats_work);
201 static int async_event(struct notifier_block *nb, unsigned long event, void *data)
203 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
204 struct mlx5_eqe *eqe = data;
206 if (event != MLX5_EVENT_TYPE_PORT_CHANGE)
209 switch (eqe->sub_type) {
210 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
211 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
212 queue_work(priv->wq, &priv->update_carrier_work);
221 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
223 priv->events_nb.notifier_call = async_event;
224 mlx5_notifier_register(priv->mdev, &priv->events_nb);
227 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
229 mlx5_notifier_unregister(priv->mdev, &priv->events_nb);
232 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
233 struct mlx5e_icosq *sq,
234 struct mlx5e_umr_wqe *wqe)
236 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
237 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
238 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
240 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
242 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
243 cseg->imm = rq->mkey_be;
245 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
246 ucseg->xlt_octowords =
247 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
248 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
251 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
252 struct mlx5e_channel *c)
254 int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
256 rq->mpwqe.info = kvzalloc_node(array_size(wq_sz,
257 sizeof(*rq->mpwqe.info)),
258 GFP_KERNEL, cpu_to_node(c->cpu));
262 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
267 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
268 u64 npages, u8 page_shift,
269 struct mlx5_core_mkey *umr_mkey)
271 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
276 in = kvzalloc(inlen, GFP_KERNEL);
280 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
282 MLX5_SET(mkc, mkc, free, 1);
283 MLX5_SET(mkc, mkc, umr_en, 1);
284 MLX5_SET(mkc, mkc, lw, 1);
285 MLX5_SET(mkc, mkc, lr, 1);
286 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
288 MLX5_SET(mkc, mkc, qpn, 0xffffff);
289 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
290 MLX5_SET64(mkc, mkc, len, npages << page_shift);
291 MLX5_SET(mkc, mkc, translations_octword_size,
292 MLX5_MTT_OCTW(npages));
293 MLX5_SET(mkc, mkc, log_page_size, page_shift);
295 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
301 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
303 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->mpwqe.wq));
305 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
308 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
310 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
313 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq)
315 struct mlx5e_wqe_frag_info next_frag = {};
316 struct mlx5e_wqe_frag_info *prev = NULL;
319 next_frag.di = &rq->wqe.di[0];
321 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
322 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
323 struct mlx5e_wqe_frag_info *frag =
324 &rq->wqe.frags[i << rq->wqe.info.log_num_frags];
327 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) {
328 if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) {
330 next_frag.offset = 0;
332 prev->last_in_page = true;
337 next_frag.offset += frag_info[f].frag_stride;
343 prev->last_in_page = true;
346 static int mlx5e_init_di_list(struct mlx5e_rq *rq,
349 int len = wq_sz << rq->wqe.info.log_num_frags;
351 rq->wqe.di = kvzalloc_node(array_size(len, sizeof(*rq->wqe.di)),
352 GFP_KERNEL, cpu_to_node(cpu));
356 mlx5e_init_frags_partition(rq);
361 static void mlx5e_free_di_list(struct mlx5e_rq *rq)
366 static void mlx5e_rq_err_cqe_work(struct work_struct *recover_work)
368 struct mlx5e_rq *rq = container_of(recover_work, struct mlx5e_rq, recover_work);
370 mlx5e_reporter_rq_cqe_err(rq);
373 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
374 struct mlx5e_params *params,
375 struct mlx5e_xsk_param *xsk,
376 struct xdp_umem *umem,
377 struct mlx5e_rq_param *rqp,
380 struct page_pool_params pp_params = { 0 };
381 struct mlx5_core_dev *mdev = c->mdev;
382 void *rqc = rqp->rqc;
383 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
384 u32 num_xsk_frames = 0;
391 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
393 rq->wq_type = params->rq_wq_type;
395 rq->netdev = c->netdev;
396 rq->tstamp = c->tstamp;
397 rq->clock = &mdev->clock;
401 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
402 rq->xdpsq = &c->rq_xdpsq;
406 rq->stats = &c->priv->channel_stats[c->ix].xskrq;
408 rq->stats = &c->priv->channel_stats[c->ix].rq;
409 INIT_WORK(&rq->recover_work, mlx5e_rq_err_cqe_work);
411 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
412 if (IS_ERR(rq->xdp_prog)) {
413 err = PTR_ERR(rq->xdp_prog);
415 goto err_rq_wq_destroy;
420 rq_xdp_ix += params->num_channels * MLX5E_RQ_GROUP_XSK;
421 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq_xdp_ix);
423 goto err_rq_wq_destroy;
425 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
426 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params, xsk);
427 rq->buff.umem_headroom = xsk ? xsk->headroom : 0;
428 pool_size = 1 << params->log_rq_mtu_frames;
430 switch (rq->wq_type) {
431 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
432 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->mpwqe.wq,
437 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR];
439 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
442 num_xsk_frames = wq_sz <<
443 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
445 pool_size = MLX5_MPWRQ_PAGES_PER_WQE <<
446 mlx5e_mpwqe_get_log_rq_size(params, xsk);
448 rq->post_wqes = mlx5e_post_rx_mpwqes;
449 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
451 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
452 #ifdef CONFIG_MLX5_EN_IPSEC
453 if (MLX5_IPSEC_DEV(mdev)) {
455 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
456 goto err_rq_wq_destroy;
459 if (!rq->handle_rx_cqe) {
461 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
462 goto err_rq_wq_destroy;
465 rq->mpwqe.skb_from_cqe_mpwrq = xsk ?
466 mlx5e_xsk_skb_from_cqe_mpwrq_linear :
467 mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ?
468 mlx5e_skb_from_cqe_mpwrq_linear :
469 mlx5e_skb_from_cqe_mpwrq_nonlinear;
471 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
472 rq->mpwqe.num_strides =
473 BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk));
475 err = mlx5e_create_rq_umr_mkey(mdev, rq);
477 goto err_rq_wq_destroy;
478 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
480 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
484 default: /* MLX5_WQ_TYPE_CYCLIC */
485 err = mlx5_wq_cyc_create(mdev, &rqp->wq, rqc_wq, &rq->wqe.wq,
490 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR];
492 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
495 num_xsk_frames = wq_sz << rq->wqe.info.log_num_frags;
497 rq->wqe.info = rqp->frags_info;
499 kvzalloc_node(array_size(sizeof(*rq->wqe.frags),
500 (wq_sz << rq->wqe.info.log_num_frags)),
501 GFP_KERNEL, cpu_to_node(c->cpu));
502 if (!rq->wqe.frags) {
507 err = mlx5e_init_di_list(rq, wq_sz, c->cpu);
511 rq->post_wqes = mlx5e_post_rx_wqes;
512 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
514 #ifdef CONFIG_MLX5_EN_IPSEC
516 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
519 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
520 if (!rq->handle_rx_cqe) {
522 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
526 rq->wqe.skb_from_cqe = xsk ?
527 mlx5e_xsk_skb_from_cqe_linear :
528 mlx5e_rx_is_linear_skb(params, NULL) ?
529 mlx5e_skb_from_cqe_linear :
530 mlx5e_skb_from_cqe_nonlinear;
531 rq->mkey_be = c->mkey_be;
535 err = mlx5e_xsk_resize_reuseq(umem, num_xsk_frames);
537 mlx5_core_err(mdev, "Unable to allocate the Reuse Ring for %u frames\n",
542 rq->zca.free = mlx5e_xsk_zca_free;
543 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
547 /* Create a page_pool and register it with rxq */
549 pp_params.flags = 0; /* No-internal DMA mapping in page_pool */
550 pp_params.pool_size = pool_size;
551 pp_params.nid = cpu_to_node(c->cpu);
552 pp_params.dev = c->pdev;
553 pp_params.dma_dir = rq->buff.map_dir;
555 /* page_pool can be used even when there is no rq->xdp_prog,
556 * given page_pool does not handle DMA mapping there is no
557 * required state to clear. And page_pool gracefully handle
560 rq->page_pool = page_pool_create(&pp_params);
561 if (IS_ERR(rq->page_pool)) {
562 err = PTR_ERR(rq->page_pool);
563 rq->page_pool = NULL;
566 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
567 MEM_TYPE_PAGE_POOL, rq->page_pool);
572 for (i = 0; i < wq_sz; i++) {
573 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
574 struct mlx5e_rx_wqe_ll *wqe =
575 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i);
577 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
578 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
580 wqe->data[0].addr = cpu_to_be64(dma_offset + rq->buff.headroom);
581 wqe->data[0].byte_count = cpu_to_be32(byte_count);
582 wqe->data[0].lkey = rq->mkey_be;
584 struct mlx5e_rx_wqe_cyc *wqe =
585 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i);
588 for (f = 0; f < rq->wqe.info.num_frags; f++) {
589 u32 frag_size = rq->wqe.info.arr[f].frag_size |
590 MLX5_HW_START_PADDING;
592 wqe->data[f].byte_count = cpu_to_be32(frag_size);
593 wqe->data[f].lkey = rq->mkey_be;
595 /* check if num_frags is not a pow of two */
596 if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) {
597 wqe->data[f].byte_count = 0;
598 wqe->data[f].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
599 wqe->data[f].addr = 0;
604 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
606 switch (params->rx_cq_moderation.cq_period_mode) {
607 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
608 rq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
610 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
612 rq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
615 rq->page_cache.head = 0;
616 rq->page_cache.tail = 0;
621 switch (rq->wq_type) {
622 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
623 kvfree(rq->mpwqe.info);
624 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
626 default: /* MLX5_WQ_TYPE_CYCLIC */
627 kvfree(rq->wqe.frags);
628 mlx5e_free_di_list(rq);
633 bpf_prog_put(rq->xdp_prog);
634 xdp_rxq_info_unreg(&rq->xdp_rxq);
635 page_pool_destroy(rq->page_pool);
636 mlx5_wq_destroy(&rq->wq_ctrl);
641 static void mlx5e_free_rq(struct mlx5e_rq *rq)
646 bpf_prog_put(rq->xdp_prog);
648 switch (rq->wq_type) {
649 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
650 kvfree(rq->mpwqe.info);
651 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
653 default: /* MLX5_WQ_TYPE_CYCLIC */
654 kvfree(rq->wqe.frags);
655 mlx5e_free_di_list(rq);
658 for (i = rq->page_cache.head; i != rq->page_cache.tail;
659 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
660 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
662 /* With AF_XDP, page_cache is not used, so this loop is not
663 * entered, and it's safe to call mlx5e_page_release_dynamic
666 mlx5e_page_release_dynamic(rq, dma_info, false);
669 xdp_rxq_info_unreg(&rq->xdp_rxq);
670 page_pool_destroy(rq->page_pool);
671 mlx5_wq_destroy(&rq->wq_ctrl);
674 static int mlx5e_create_rq(struct mlx5e_rq *rq,
675 struct mlx5e_rq_param *param)
677 struct mlx5_core_dev *mdev = rq->mdev;
685 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
686 sizeof(u64) * rq->wq_ctrl.buf.npages;
687 in = kvzalloc(inlen, GFP_KERNEL);
691 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
692 wq = MLX5_ADDR_OF(rqc, rqc, wq);
694 memcpy(rqc, param->rqc, sizeof(param->rqc));
696 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
697 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
698 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
699 MLX5_ADAPTER_PAGE_SHIFT);
700 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
702 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
703 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
705 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
712 int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state, int next_state)
714 struct mlx5_core_dev *mdev = rq->mdev;
721 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
722 in = kvzalloc(inlen, GFP_KERNEL);
726 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
728 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
729 MLX5_SET(rqc, rqc, state, next_state);
731 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
738 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
740 struct mlx5e_channel *c = rq->channel;
741 struct mlx5e_priv *priv = c->priv;
742 struct mlx5_core_dev *mdev = priv->mdev;
749 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
750 in = kvzalloc(inlen, GFP_KERNEL);
754 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
756 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
757 MLX5_SET64(modify_rq_in, in, modify_bitmask,
758 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
759 MLX5_SET(rqc, rqc, scatter_fcs, enable);
760 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
762 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
769 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
771 struct mlx5e_channel *c = rq->channel;
772 struct mlx5_core_dev *mdev = c->mdev;
778 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
779 in = kvzalloc(inlen, GFP_KERNEL);
783 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
785 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
786 MLX5_SET64(modify_rq_in, in, modify_bitmask,
787 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
788 MLX5_SET(rqc, rqc, vsd, vsd);
789 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
791 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
798 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
800 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
803 int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
805 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
806 struct mlx5e_channel *c = rq->channel;
808 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq));
811 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes)
815 } while (time_before(jiffies, exp_time));
817 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",
818 c->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes);
820 mlx5e_reporter_rx_timeout(rq);
824 void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
829 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
830 struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
834 /* Outstanding UMR WQEs (in progress) start at wq->head */
835 for (i = 0; i < rq->mpwqe.umr_in_progress; i++) {
836 rq->dealloc_wqe(rq, head);
837 head = mlx5_wq_ll_get_wqe_next_ix(wq, head);
840 while (!mlx5_wq_ll_is_empty(wq)) {
841 struct mlx5e_rx_wqe_ll *wqe;
843 wqe_ix_be = *wq->tail_next;
844 wqe_ix = be16_to_cpu(wqe_ix_be);
845 wqe = mlx5_wq_ll_get_wqe(wq, wqe_ix);
846 rq->dealloc_wqe(rq, wqe_ix);
847 mlx5_wq_ll_pop(wq, wqe_ix_be,
848 &wqe->next.next_wqe_index);
851 struct mlx5_wq_cyc *wq = &rq->wqe.wq;
853 while (!mlx5_wq_cyc_is_empty(wq)) {
854 wqe_ix = mlx5_wq_cyc_get_tail(wq);
855 rq->dealloc_wqe(rq, wqe_ix);
862 int mlx5e_open_rq(struct mlx5e_channel *c, struct mlx5e_params *params,
863 struct mlx5e_rq_param *param, struct mlx5e_xsk_param *xsk,
864 struct xdp_umem *umem, struct mlx5e_rq *rq)
868 err = mlx5e_alloc_rq(c, params, xsk, umem, param, rq);
872 err = mlx5e_create_rq(rq, param);
876 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
880 if (MLX5_CAP_ETH(c->mdev, cqe_checksum_full))
881 __set_bit(MLX5E_RQ_STATE_CSUM_FULL, &c->rq.state);
883 if (params->rx_dim_enabled)
884 __set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
886 /* We disable csum_complete when XDP is enabled since
887 * XDP programs might manipulate packets which will render
888 * skb->checksum incorrect.
890 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE) || c->xdp)
891 __set_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &c->rq.state);
896 mlx5e_destroy_rq(rq);
903 void mlx5e_activate_rq(struct mlx5e_rq *rq)
905 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
906 mlx5e_trigger_irq(&rq->channel->icosq);
909 void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
911 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
912 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
915 void mlx5e_close_rq(struct mlx5e_rq *rq)
917 cancel_work_sync(&rq->dim.work);
918 cancel_work_sync(&rq->channel->icosq.recover_work);
919 cancel_work_sync(&rq->recover_work);
920 mlx5e_destroy_rq(rq);
921 mlx5e_free_rx_descs(rq);
925 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
927 kvfree(sq->db.xdpi_fifo.xi);
928 kvfree(sq->db.wqe_info);
931 static int mlx5e_alloc_xdpsq_fifo(struct mlx5e_xdpsq *sq, int numa)
933 struct mlx5e_xdp_info_fifo *xdpi_fifo = &sq->db.xdpi_fifo;
934 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
935 int dsegs_per_wq = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
937 xdpi_fifo->xi = kvzalloc_node(sizeof(*xdpi_fifo->xi) * dsegs_per_wq,
942 xdpi_fifo->pc = &sq->xdpi_fifo_pc;
943 xdpi_fifo->cc = &sq->xdpi_fifo_cc;
944 xdpi_fifo->mask = dsegs_per_wq - 1;
949 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
951 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
954 sq->db.wqe_info = kvzalloc_node(sizeof(*sq->db.wqe_info) * wq_sz,
956 if (!sq->db.wqe_info)
959 err = mlx5e_alloc_xdpsq_fifo(sq, numa);
961 mlx5e_free_xdpsq_db(sq);
968 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
969 struct mlx5e_params *params,
970 struct xdp_umem *umem,
971 struct mlx5e_sq_param *param,
972 struct mlx5e_xdpsq *sq,
975 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
976 struct mlx5_core_dev *mdev = c->mdev;
977 struct mlx5_wq_cyc *wq = &sq->wq;
981 sq->mkey_be = c->mkey_be;
983 sq->uar_map = mdev->mlx5e_res.bfreg.map;
984 sq->min_inline_mode = params->tx_min_inline_mode;
985 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
988 sq->stats = sq->umem ?
989 &c->priv->channel_stats[c->ix].xsksq :
991 &c->priv->channel_stats[c->ix].xdpsq :
992 &c->priv->channel_stats[c->ix].rq_xdpsq;
994 param->wq.db_numa_node = cpu_to_node(c->cpu);
995 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
998 wq->db = &wq->db[MLX5_SND_DBR];
1000 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
1002 goto err_sq_wq_destroy;
1007 mlx5_wq_destroy(&sq->wq_ctrl);
1012 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
1014 mlx5e_free_xdpsq_db(sq);
1015 mlx5_wq_destroy(&sq->wq_ctrl);
1018 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
1020 kvfree(sq->db.ico_wqe);
1023 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
1025 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1027 sq->db.ico_wqe = kvzalloc_node(array_size(wq_sz,
1028 sizeof(*sq->db.ico_wqe)),
1030 if (!sq->db.ico_wqe)
1036 static void mlx5e_icosq_err_cqe_work(struct work_struct *recover_work)
1038 struct mlx5e_icosq *sq = container_of(recover_work, struct mlx5e_icosq,
1041 mlx5e_reporter_icosq_cqe_err(sq);
1044 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
1045 struct mlx5e_sq_param *param,
1046 struct mlx5e_icosq *sq)
1048 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1049 struct mlx5_core_dev *mdev = c->mdev;
1050 struct mlx5_wq_cyc *wq = &sq->wq;
1054 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1056 param->wq.db_numa_node = cpu_to_node(c->cpu);
1057 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1060 wq->db = &wq->db[MLX5_SND_DBR];
1062 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
1064 goto err_sq_wq_destroy;
1066 INIT_WORK(&sq->recover_work, mlx5e_icosq_err_cqe_work);
1071 mlx5_wq_destroy(&sq->wq_ctrl);
1076 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
1078 mlx5e_free_icosq_db(sq);
1079 mlx5_wq_destroy(&sq->wq_ctrl);
1082 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
1084 kvfree(sq->db.wqe_info);
1085 kvfree(sq->db.dma_fifo);
1088 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
1090 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1091 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
1093 sq->db.dma_fifo = kvzalloc_node(array_size(df_sz,
1094 sizeof(*sq->db.dma_fifo)),
1096 sq->db.wqe_info = kvzalloc_node(array_size(wq_sz,
1097 sizeof(*sq->db.wqe_info)),
1099 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
1100 mlx5e_free_txqsq_db(sq);
1104 sq->dma_fifo_mask = df_sz - 1;
1109 static void mlx5e_tx_err_cqe_work(struct work_struct *recover_work);
1110 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
1112 struct mlx5e_params *params,
1113 struct mlx5e_sq_param *param,
1114 struct mlx5e_txqsq *sq,
1117 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1118 struct mlx5_core_dev *mdev = c->mdev;
1119 struct mlx5_wq_cyc *wq = &sq->wq;
1123 sq->tstamp = c->tstamp;
1124 sq->clock = &mdev->clock;
1125 sq->mkey_be = c->mkey_be;
1128 sq->txq_ix = txq_ix;
1129 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1130 sq->min_inline_mode = params->tx_min_inline_mode;
1131 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
1132 sq->stats = &c->priv->channel_stats[c->ix].sq[tc];
1133 sq->stop_room = MLX5E_SQ_STOP_ROOM;
1134 INIT_WORK(&sq->recover_work, mlx5e_tx_err_cqe_work);
1135 if (!MLX5_CAP_ETH(mdev, wqe_vlan_insert))
1136 set_bit(MLX5E_SQ_STATE_VLAN_NEED_L2_INLINE, &sq->state);
1137 if (MLX5_IPSEC_DEV(c->priv->mdev))
1138 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1139 #ifdef CONFIG_MLX5_EN_TLS
1140 if (mlx5_accel_is_tls_device(c->priv->mdev)) {
1141 set_bit(MLX5E_SQ_STATE_TLS, &sq->state);
1142 sq->stop_room += MLX5E_SQ_TLS_ROOM +
1143 mlx5e_ktls_dumps_num_wqebbs(sq, MAX_SKB_FRAGS,
1144 TLS_MAX_PAYLOAD_SIZE);
1148 param->wq.db_numa_node = cpu_to_node(c->cpu);
1149 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1152 wq->db = &wq->db[MLX5_SND_DBR];
1154 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1156 goto err_sq_wq_destroy;
1158 INIT_WORK(&sq->dim.work, mlx5e_tx_dim_work);
1159 sq->dim.mode = params->tx_cq_moderation.cq_period_mode;
1164 mlx5_wq_destroy(&sq->wq_ctrl);
1169 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1171 mlx5e_free_txqsq_db(sq);
1172 mlx5_wq_destroy(&sq->wq_ctrl);
1175 struct mlx5e_create_sq_param {
1176 struct mlx5_wq_ctrl *wq_ctrl;
1183 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1184 struct mlx5e_sq_param *param,
1185 struct mlx5e_create_sq_param *csp,
1194 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1195 sizeof(u64) * csp->wq_ctrl->buf.npages;
1196 in = kvzalloc(inlen, GFP_KERNEL);
1200 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1201 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1203 memcpy(sqc, param->sqc, sizeof(param->sqc));
1204 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1205 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1206 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1208 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1209 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1211 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1212 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1214 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1215 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1216 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1217 MLX5_ADAPTER_PAGE_SHIFT);
1218 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1220 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1221 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1223 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1230 int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1231 struct mlx5e_modify_sq_param *p)
1238 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1239 in = kvzalloc(inlen, GFP_KERNEL);
1243 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1245 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1246 MLX5_SET(sqc, sqc, state, p->next_state);
1247 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1248 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1249 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1252 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1259 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1261 mlx5_core_destroy_sq(mdev, sqn);
1264 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1265 struct mlx5e_sq_param *param,
1266 struct mlx5e_create_sq_param *csp,
1269 struct mlx5e_modify_sq_param msp = {0};
1272 err = mlx5e_create_sq(mdev, param, csp, sqn);
1276 msp.curr_state = MLX5_SQC_STATE_RST;
1277 msp.next_state = MLX5_SQC_STATE_RDY;
1278 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1280 mlx5e_destroy_sq(mdev, *sqn);
1285 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1286 struct mlx5e_txqsq *sq, u32 rate);
1288 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1291 struct mlx5e_params *params,
1292 struct mlx5e_sq_param *param,
1293 struct mlx5e_txqsq *sq,
1296 struct mlx5e_create_sq_param csp = {};
1300 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1306 csp.cqn = sq->cq.mcq.cqn;
1307 csp.wq_ctrl = &sq->wq_ctrl;
1308 csp.min_inline_mode = sq->min_inline_mode;
1309 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1311 goto err_free_txqsq;
1313 tx_rate = c->priv->tx_rates[sq->txq_ix];
1315 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1317 if (params->tx_dim_enabled)
1318 sq->state |= BIT(MLX5E_SQ_STATE_AM);
1323 mlx5e_free_txqsq(sq);
1328 void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1330 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1331 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1332 netdev_tx_reset_queue(sq->txq);
1333 netif_tx_start_queue(sq->txq);
1336 void mlx5e_tx_disable_queue(struct netdev_queue *txq)
1338 __netif_tx_lock_bh(txq);
1339 netif_tx_stop_queue(txq);
1340 __netif_tx_unlock_bh(txq);
1343 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1345 struct mlx5e_channel *c = sq->channel;
1346 struct mlx5_wq_cyc *wq = &sq->wq;
1348 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1349 /* prevent netif_tx_wake_queue */
1350 napi_synchronize(&c->napi);
1352 mlx5e_tx_disable_queue(sq->txq);
1354 /* last doorbell out, godspeed .. */
1355 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1356 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1357 struct mlx5e_tx_wqe_info *wi;
1358 struct mlx5e_tx_wqe *nop;
1360 wi = &sq->db.wqe_info[pi];
1362 memset(wi, 0, sizeof(*wi));
1364 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1365 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1369 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1371 struct mlx5e_channel *c = sq->channel;
1372 struct mlx5_core_dev *mdev = c->mdev;
1373 struct mlx5_rate_limit rl = {0};
1375 cancel_work_sync(&sq->dim.work);
1376 cancel_work_sync(&sq->recover_work);
1377 mlx5e_destroy_sq(mdev, sq->sqn);
1378 if (sq->rate_limit) {
1379 rl.rate = sq->rate_limit;
1380 mlx5_rl_remove_rate(mdev, &rl);
1382 mlx5e_free_txqsq_descs(sq);
1383 mlx5e_free_txqsq(sq);
1386 static void mlx5e_tx_err_cqe_work(struct work_struct *recover_work)
1388 struct mlx5e_txqsq *sq = container_of(recover_work, struct mlx5e_txqsq,
1391 mlx5e_reporter_tx_err_cqe(sq);
1394 int mlx5e_open_icosq(struct mlx5e_channel *c, struct mlx5e_params *params,
1395 struct mlx5e_sq_param *param, struct mlx5e_icosq *sq)
1397 struct mlx5e_create_sq_param csp = {};
1400 err = mlx5e_alloc_icosq(c, param, sq);
1404 csp.cqn = sq->cq.mcq.cqn;
1405 csp.wq_ctrl = &sq->wq_ctrl;
1406 csp.min_inline_mode = params->tx_min_inline_mode;
1407 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1409 goto err_free_icosq;
1414 mlx5e_free_icosq(sq);
1419 void mlx5e_activate_icosq(struct mlx5e_icosq *icosq)
1421 set_bit(MLX5E_SQ_STATE_ENABLED, &icosq->state);
1424 void mlx5e_deactivate_icosq(struct mlx5e_icosq *icosq)
1426 struct mlx5e_channel *c = icosq->channel;
1428 clear_bit(MLX5E_SQ_STATE_ENABLED, &icosq->state);
1429 napi_synchronize(&c->napi);
1432 void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1434 struct mlx5e_channel *c = sq->channel;
1436 mlx5e_destroy_sq(c->mdev, sq->sqn);
1437 mlx5e_free_icosq(sq);
1440 int mlx5e_open_xdpsq(struct mlx5e_channel *c, struct mlx5e_params *params,
1441 struct mlx5e_sq_param *param, struct xdp_umem *umem,
1442 struct mlx5e_xdpsq *sq, bool is_redirect)
1444 struct mlx5e_create_sq_param csp = {};
1447 err = mlx5e_alloc_xdpsq(c, params, umem, param, sq, is_redirect);
1452 csp.tisn = c->priv->tisn[c->lag_port][0]; /* tc = 0 */
1453 csp.cqn = sq->cq.mcq.cqn;
1454 csp.wq_ctrl = &sq->wq_ctrl;
1455 csp.min_inline_mode = sq->min_inline_mode;
1456 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1457 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1459 goto err_free_xdpsq;
1461 mlx5e_set_xmit_fp(sq, param->is_mpw);
1463 if (!param->is_mpw) {
1464 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1465 unsigned int inline_hdr_sz = 0;
1468 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1469 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1473 /* Pre initialize fixed WQE fields */
1474 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1475 struct mlx5e_xdp_wqe_info *wi = &sq->db.wqe_info[i];
1476 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1477 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1478 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1479 struct mlx5_wqe_data_seg *dseg;
1481 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1482 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1484 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1485 dseg->lkey = sq->mkey_be;
1495 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1496 mlx5e_free_xdpsq(sq);
1501 void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1503 struct mlx5e_channel *c = sq->channel;
1505 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1506 napi_synchronize(&c->napi);
1508 mlx5e_destroy_sq(c->mdev, sq->sqn);
1509 mlx5e_free_xdpsq_descs(sq);
1510 mlx5e_free_xdpsq(sq);
1513 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1514 struct mlx5e_cq_param *param,
1515 struct mlx5e_cq *cq)
1517 struct mlx5_core_cq *mcq = &cq->mcq;
1523 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1527 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1533 mcq->set_ci_db = cq->wq_ctrl.db.db;
1534 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1535 *mcq->set_ci_db = 0;
1537 mcq->vector = param->eq_ix;
1538 mcq->comp = mlx5e_completion_event;
1539 mcq->event = mlx5e_cq_error_event;
1542 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1543 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1553 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1554 struct mlx5e_cq_param *param,
1555 struct mlx5e_cq *cq)
1557 struct mlx5_core_dev *mdev = c->priv->mdev;
1560 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1561 param->wq.db_numa_node = cpu_to_node(c->cpu);
1562 param->eq_ix = c->ix;
1564 err = mlx5e_alloc_cq_common(mdev, param, cq);
1566 cq->napi = &c->napi;
1572 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1574 mlx5_wq_destroy(&cq->wq_ctrl);
1577 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1579 u32 out[MLX5_ST_SZ_DW(create_cq_out)];
1580 struct mlx5_core_dev *mdev = cq->mdev;
1581 struct mlx5_core_cq *mcq = &cq->mcq;
1586 unsigned int irqn_not_used;
1590 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1594 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1595 sizeof(u64) * cq->wq_ctrl.buf.npages;
1596 in = kvzalloc(inlen, GFP_KERNEL);
1600 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1602 memcpy(cqc, param->cqc, sizeof(param->cqc));
1604 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
1605 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1607 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1608 MLX5_SET(cqc, cqc, c_eqn, eqn);
1609 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1610 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1611 MLX5_ADAPTER_PAGE_SHIFT);
1612 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1614 err = mlx5_core_create_cq(mdev, mcq, in, inlen, out, sizeof(out));
1626 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1628 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1631 int mlx5e_open_cq(struct mlx5e_channel *c, struct dim_cq_moder moder,
1632 struct mlx5e_cq_param *param, struct mlx5e_cq *cq)
1634 struct mlx5_core_dev *mdev = c->mdev;
1637 err = mlx5e_alloc_cq(c, param, cq);
1641 err = mlx5e_create_cq(cq, param);
1645 if (MLX5_CAP_GEN(mdev, cq_moderation))
1646 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1655 void mlx5e_close_cq(struct mlx5e_cq *cq)
1657 mlx5e_destroy_cq(cq);
1661 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1662 struct mlx5e_params *params,
1663 struct mlx5e_channel_param *cparam)
1668 for (tc = 0; tc < c->num_tc; tc++) {
1669 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1670 &cparam->tx_cq, &c->sq[tc].cq);
1672 goto err_close_tx_cqs;
1678 for (tc--; tc >= 0; tc--)
1679 mlx5e_close_cq(&c->sq[tc].cq);
1684 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1688 for (tc = 0; tc < c->num_tc; tc++)
1689 mlx5e_close_cq(&c->sq[tc].cq);
1692 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1693 struct mlx5e_params *params,
1694 struct mlx5e_channel_param *cparam)
1696 struct mlx5e_priv *priv = c->priv;
1699 for (tc = 0; tc < params->num_tc; tc++) {
1700 int txq_ix = c->ix + tc * priv->max_nch;
1702 err = mlx5e_open_txqsq(c, c->priv->tisn[c->lag_port][tc], txq_ix,
1703 params, &cparam->sq, &c->sq[tc], tc);
1711 for (tc--; tc >= 0; tc--)
1712 mlx5e_close_txqsq(&c->sq[tc]);
1717 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1721 for (tc = 0; tc < c->num_tc; tc++)
1722 mlx5e_close_txqsq(&c->sq[tc]);
1725 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1726 struct mlx5e_txqsq *sq, u32 rate)
1728 struct mlx5e_priv *priv = netdev_priv(dev);
1729 struct mlx5_core_dev *mdev = priv->mdev;
1730 struct mlx5e_modify_sq_param msp = {0};
1731 struct mlx5_rate_limit rl = {0};
1735 if (rate == sq->rate_limit)
1739 if (sq->rate_limit) {
1740 rl.rate = sq->rate_limit;
1741 /* remove current rl index to free space to next ones */
1742 mlx5_rl_remove_rate(mdev, &rl);
1749 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1751 netdev_err(dev, "Failed configuring rate %u: %d\n",
1757 msp.curr_state = MLX5_SQC_STATE_RDY;
1758 msp.next_state = MLX5_SQC_STATE_RDY;
1759 msp.rl_index = rl_index;
1760 msp.rl_update = true;
1761 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1763 netdev_err(dev, "Failed configuring rate %u: %d\n",
1765 /* remove the rate from the table */
1767 mlx5_rl_remove_rate(mdev, &rl);
1771 sq->rate_limit = rate;
1775 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1777 struct mlx5e_priv *priv = netdev_priv(dev);
1778 struct mlx5_core_dev *mdev = priv->mdev;
1779 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1782 if (!mlx5_rl_is_supported(mdev)) {
1783 netdev_err(dev, "Rate limiting is not supported on this device\n");
1787 /* rate is given in Mb/sec, HW config is in Kb/sec */
1790 /* Check whether rate in valid range, 0 is always valid */
1791 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1792 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1796 mutex_lock(&priv->state_lock);
1797 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1798 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1800 priv->tx_rates[index] = rate;
1801 mutex_unlock(&priv->state_lock);
1806 static int mlx5e_alloc_xps_cpumask(struct mlx5e_channel *c,
1807 struct mlx5e_params *params)
1809 int num_comp_vectors = mlx5_comp_vectors_count(c->mdev);
1812 if (!zalloc_cpumask_var(&c->xps_cpumask, GFP_KERNEL))
1815 for (irq = c->ix; irq < num_comp_vectors; irq += params->num_channels) {
1816 int cpu = cpumask_first(mlx5_comp_irq_get_affinity_mask(c->mdev, irq));
1818 cpumask_set_cpu(cpu, c->xps_cpumask);
1824 static void mlx5e_free_xps_cpumask(struct mlx5e_channel *c)
1826 free_cpumask_var(c->xps_cpumask);
1829 static int mlx5e_open_queues(struct mlx5e_channel *c,
1830 struct mlx5e_params *params,
1831 struct mlx5e_channel_param *cparam)
1833 struct dim_cq_moder icocq_moder = {0, 0};
1836 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1840 err = mlx5e_open_tx_cqs(c, params, cparam);
1842 goto err_close_icosq_cq;
1844 err = mlx5e_open_cq(c, params->tx_cq_moderation, &cparam->tx_cq, &c->xdpsq.cq);
1846 goto err_close_tx_cqs;
1848 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1850 goto err_close_xdp_tx_cqs;
1852 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1853 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1854 &cparam->tx_cq, &c->rq_xdpsq.cq) : 0;
1856 goto err_close_rx_cq;
1858 napi_enable(&c->napi);
1860 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1862 goto err_disable_napi;
1864 err = mlx5e_open_sqs(c, params, cparam);
1866 goto err_close_icosq;
1869 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL,
1870 &c->rq_xdpsq, false);
1875 err = mlx5e_open_rq(c, params, &cparam->rq, NULL, NULL, &c->rq);
1877 goto err_close_xdp_sq;
1879 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL, &c->xdpsq, true);
1886 mlx5e_close_rq(&c->rq);
1890 mlx5e_close_xdpsq(&c->rq_xdpsq);
1896 mlx5e_close_icosq(&c->icosq);
1899 napi_disable(&c->napi);
1902 mlx5e_close_cq(&c->rq_xdpsq.cq);
1905 mlx5e_close_cq(&c->rq.cq);
1907 err_close_xdp_tx_cqs:
1908 mlx5e_close_cq(&c->xdpsq.cq);
1911 mlx5e_close_tx_cqs(c);
1914 mlx5e_close_cq(&c->icosq.cq);
1919 static void mlx5e_close_queues(struct mlx5e_channel *c)
1921 mlx5e_close_xdpsq(&c->xdpsq);
1922 mlx5e_close_rq(&c->rq);
1924 mlx5e_close_xdpsq(&c->rq_xdpsq);
1926 mlx5e_close_icosq(&c->icosq);
1927 napi_disable(&c->napi);
1929 mlx5e_close_cq(&c->rq_xdpsq.cq);
1930 mlx5e_close_cq(&c->rq.cq);
1931 mlx5e_close_cq(&c->xdpsq.cq);
1932 mlx5e_close_tx_cqs(c);
1933 mlx5e_close_cq(&c->icosq.cq);
1936 static u8 mlx5e_enumerate_lag_port(struct mlx5_core_dev *mdev, int ix)
1938 u16 port_aff_bias = mlx5_core_is_pf(mdev) ? 0 : MLX5_CAP_GEN(mdev, vhca_id);
1940 return (ix + port_aff_bias) % mlx5e_get_num_lag_ports(mdev);
1943 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1944 struct mlx5e_params *params,
1945 struct mlx5e_channel_param *cparam,
1946 struct xdp_umem *umem,
1947 struct mlx5e_channel **cp)
1949 int cpu = cpumask_first(mlx5_comp_irq_get_affinity_mask(priv->mdev, ix));
1950 struct net_device *netdev = priv->netdev;
1951 struct mlx5e_xsk_param xsk;
1952 struct mlx5e_channel *c;
1957 err = mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1961 c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1966 c->mdev = priv->mdev;
1967 c->tstamp = &priv->tstamp;
1970 c->pdev = priv->mdev->device;
1971 c->netdev = priv->netdev;
1972 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1973 c->num_tc = params->num_tc;
1974 c->xdp = !!params->xdp_prog;
1975 c->stats = &priv->channel_stats[ix].ch;
1976 c->irq_desc = irq_to_desc(irq);
1977 c->lag_port = mlx5e_enumerate_lag_port(priv->mdev, ix);
1979 err = mlx5e_alloc_xps_cpumask(c, params);
1981 goto err_free_channel;
1983 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1985 err = mlx5e_open_queues(c, params, cparam);
1990 mlx5e_build_xsk_param(umem, &xsk);
1991 err = mlx5e_open_xsk(priv, params, &xsk, umem, c);
1993 goto err_close_queues;
2001 mlx5e_close_queues(c);
2004 netif_napi_del(&c->napi);
2005 mlx5e_free_xps_cpumask(c);
2013 static void mlx5e_activate_channel(struct mlx5e_channel *c)
2017 for (tc = 0; tc < c->num_tc; tc++)
2018 mlx5e_activate_txqsq(&c->sq[tc]);
2019 mlx5e_activate_icosq(&c->icosq);
2020 mlx5e_activate_rq(&c->rq);
2021 netif_set_xps_queue(c->netdev, c->xps_cpumask, c->ix);
2023 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2024 mlx5e_activate_xsk(c);
2027 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
2031 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2032 mlx5e_deactivate_xsk(c);
2034 mlx5e_deactivate_rq(&c->rq);
2035 mlx5e_deactivate_icosq(&c->icosq);
2036 for (tc = 0; tc < c->num_tc; tc++)
2037 mlx5e_deactivate_txqsq(&c->sq[tc]);
2040 static void mlx5e_close_channel(struct mlx5e_channel *c)
2042 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2044 mlx5e_close_queues(c);
2045 netif_napi_del(&c->napi);
2046 mlx5e_free_xps_cpumask(c);
2051 #define DEFAULT_FRAG_SIZE (2048)
2053 static void mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
2054 struct mlx5e_params *params,
2055 struct mlx5e_xsk_param *xsk,
2056 struct mlx5e_rq_frags_info *info)
2058 u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
2059 int frag_size_max = DEFAULT_FRAG_SIZE;
2063 #ifdef CONFIG_MLX5_EN_IPSEC
2064 if (MLX5_IPSEC_DEV(mdev))
2065 byte_count += MLX5E_METADATA_ETHER_LEN;
2068 if (mlx5e_rx_is_linear_skb(params, xsk)) {
2071 frag_stride = mlx5e_rx_get_linear_frag_sz(params, xsk);
2072 frag_stride = roundup_pow_of_two(frag_stride);
2074 info->arr[0].frag_size = byte_count;
2075 info->arr[0].frag_stride = frag_stride;
2076 info->num_frags = 1;
2077 info->wqe_bulk = PAGE_SIZE / frag_stride;
2081 if (byte_count > PAGE_SIZE +
2082 (MLX5E_MAX_RX_FRAGS - 1) * frag_size_max)
2083 frag_size_max = PAGE_SIZE;
2086 while (buf_size < byte_count) {
2087 int frag_size = byte_count - buf_size;
2089 if (i < MLX5E_MAX_RX_FRAGS - 1)
2090 frag_size = min(frag_size, frag_size_max);
2092 info->arr[i].frag_size = frag_size;
2093 info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
2095 buf_size += frag_size;
2098 info->num_frags = i;
2099 /* number of different wqes sharing a page */
2100 info->wqe_bulk = 1 + (info->num_frags % 2);
2103 info->wqe_bulk = max_t(u8, info->wqe_bulk, 8);
2104 info->log_num_frags = order_base_2(info->num_frags);
2107 static inline u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
2109 int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
2112 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2113 sz += sizeof(struct mlx5e_rx_wqe_ll);
2115 default: /* MLX5_WQ_TYPE_CYCLIC */
2116 sz += sizeof(struct mlx5e_rx_wqe_cyc);
2119 return order_base_2(sz);
2122 static u8 mlx5e_get_rq_log_wq_sz(void *rqc)
2124 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2126 return MLX5_GET(wq, wq, log_wq_sz);
2129 void mlx5e_build_rq_param(struct mlx5e_priv *priv,
2130 struct mlx5e_params *params,
2131 struct mlx5e_xsk_param *xsk,
2132 struct mlx5e_rq_param *param)
2134 struct mlx5_core_dev *mdev = priv->mdev;
2135 void *rqc = param->rqc;
2136 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2139 switch (params->rq_wq_type) {
2140 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2141 MLX5_SET(wq, wq, log_wqe_num_of_strides,
2142 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk) -
2143 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
2144 MLX5_SET(wq, wq, log_wqe_stride_size,
2145 mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk) -
2146 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
2147 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params, xsk));
2149 default: /* MLX5_WQ_TYPE_CYCLIC */
2150 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
2151 mlx5e_build_rq_frags_info(mdev, params, xsk, ¶m->frags_info);
2152 ndsegs = param->frags_info.num_frags;
2155 MLX5_SET(wq, wq, wq_type, params->rq_wq_type);
2156 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
2157 MLX5_SET(wq, wq, log_wq_stride,
2158 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
2159 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
2160 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
2161 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
2162 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
2164 param->wq.buf_numa_node = dev_to_node(mdev->device);
2167 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
2168 struct mlx5e_rq_param *param)
2170 struct mlx5_core_dev *mdev = priv->mdev;
2171 void *rqc = param->rqc;
2172 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2174 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
2175 MLX5_SET(wq, wq, log_wq_stride,
2176 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
2177 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
2179 param->wq.buf_numa_node = dev_to_node(mdev->device);
2182 void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
2183 struct mlx5e_sq_param *param)
2185 void *sqc = param->sqc;
2186 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2188 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
2189 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
2191 param->wq.buf_numa_node = dev_to_node(priv->mdev->device);
2194 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
2195 struct mlx5e_params *params,
2196 struct mlx5e_sq_param *param)
2198 void *sqc = param->sqc;
2199 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2202 allow_swp = mlx5_geneve_tx_allowed(priv->mdev) ||
2203 !!MLX5_IPSEC_DEV(priv->mdev);
2204 mlx5e_build_sq_param_common(priv, param);
2205 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2206 MLX5_SET(sqc, sqc, allow_swp, allow_swp);
2209 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
2210 struct mlx5e_cq_param *param)
2212 void *cqc = param->cqc;
2214 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2215 if (MLX5_CAP_GEN(priv->mdev, cqe_128_always) && cache_line_size() >= 128)
2216 MLX5_SET(cqc, cqc, cqe_sz, CQE_STRIDE_128_PAD);
2219 void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2220 struct mlx5e_params *params,
2221 struct mlx5e_xsk_param *xsk,
2222 struct mlx5e_cq_param *param)
2224 struct mlx5_core_dev *mdev = priv->mdev;
2225 void *cqc = param->cqc;
2228 switch (params->rq_wq_type) {
2229 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2230 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params, xsk) +
2231 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
2233 default: /* MLX5_WQ_TYPE_CYCLIC */
2234 log_cq_size = params->log_rq_mtu_frames;
2237 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2238 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2239 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2240 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2243 mlx5e_build_common_cq_param(priv, param);
2244 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2247 void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2248 struct mlx5e_params *params,
2249 struct mlx5e_cq_param *param)
2251 void *cqc = param->cqc;
2253 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2255 mlx5e_build_common_cq_param(priv, param);
2256 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2259 void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2261 struct mlx5e_cq_param *param)
2263 void *cqc = param->cqc;
2265 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2267 mlx5e_build_common_cq_param(priv, param);
2269 param->cq_period_mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2272 void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2274 struct mlx5e_sq_param *param)
2276 void *sqc = param->sqc;
2277 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2279 mlx5e_build_sq_param_common(priv, param);
2281 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2282 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2285 void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2286 struct mlx5e_params *params,
2287 struct mlx5e_sq_param *param)
2289 void *sqc = param->sqc;
2290 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2292 mlx5e_build_sq_param_common(priv, param);
2293 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2294 param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE);
2297 static u8 mlx5e_build_icosq_log_wq_sz(struct mlx5e_params *params,
2298 struct mlx5e_rq_param *rqp)
2300 switch (params->rq_wq_type) {
2301 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2302 return order_base_2(MLX5E_UMR_WQEBBS) +
2303 mlx5e_get_rq_log_wq_sz(rqp->rqc);
2304 default: /* MLX5_WQ_TYPE_CYCLIC */
2305 return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2309 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2310 struct mlx5e_params *params,
2311 struct mlx5e_channel_param *cparam)
2315 mlx5e_build_rq_param(priv, params, NULL, &cparam->rq);
2317 icosq_log_wq_sz = mlx5e_build_icosq_log_wq_sz(params, &cparam->rq);
2319 mlx5e_build_sq_param(priv, params, &cparam->sq);
2320 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2321 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2322 mlx5e_build_rx_cq_param(priv, params, NULL, &cparam->rx_cq);
2323 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2324 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2327 int mlx5e_open_channels(struct mlx5e_priv *priv,
2328 struct mlx5e_channels *chs)
2330 struct mlx5e_channel_param *cparam;
2334 chs->num = chs->params.num_channels;
2336 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2337 cparam = kvzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2338 if (!chs->c || !cparam)
2341 mlx5e_build_channel_param(priv, &chs->params, cparam);
2342 for (i = 0; i < chs->num; i++) {
2343 struct xdp_umem *umem = NULL;
2345 if (chs->params.xdp_prog)
2346 umem = mlx5e_xsk_get_umem(&chs->params, chs->params.xsk, i);
2348 err = mlx5e_open_channel(priv, i, &chs->params, cparam, umem, &chs->c[i]);
2350 goto err_close_channels;
2353 mlx5e_health_channels_update(priv);
2358 for (i--; i >= 0; i--)
2359 mlx5e_close_channel(chs->c[i]);
2368 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2372 for (i = 0; i < chs->num; i++)
2373 mlx5e_activate_channel(chs->c[i]);
2376 #define MLX5E_RQ_WQES_TIMEOUT 20000 /* msecs */
2378 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2383 for (i = 0; i < chs->num; i++) {
2384 int timeout = err ? 0 : MLX5E_RQ_WQES_TIMEOUT;
2386 err |= mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq, timeout);
2388 /* Don't wait on the XSK RQ, because the newer xdpsock sample
2389 * doesn't provide any Fill Ring entries at the setup stage.
2393 return err ? -ETIMEDOUT : 0;
2396 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2400 for (i = 0; i < chs->num; i++)
2401 mlx5e_deactivate_channel(chs->c[i]);
2404 void mlx5e_close_channels(struct mlx5e_channels *chs)
2408 for (i = 0; i < chs->num; i++)
2409 mlx5e_close_channel(chs->c[i]);
2416 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2418 struct mlx5_core_dev *mdev = priv->mdev;
2425 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2426 in = kvzalloc(inlen, GFP_KERNEL);
2430 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2432 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2433 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2435 for (i = 0; i < sz; i++)
2436 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2438 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2440 rqt->enabled = true;
2446 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2448 rqt->enabled = false;
2449 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2452 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2454 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2457 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2459 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2463 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
2468 for (ix = 0; ix < priv->max_nch; ix++) {
2469 err = mlx5e_create_rqt(priv, 1 /*size */, &tirs[ix].rqt);
2471 goto err_destroy_rqts;
2477 mlx5_core_warn(priv->mdev, "create rqts failed, %d\n", err);
2478 for (ix--; ix >= 0; ix--)
2479 mlx5e_destroy_rqt(priv, &tirs[ix].rqt);
2484 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
2488 for (i = 0; i < priv->max_nch; i++)
2489 mlx5e_destroy_rqt(priv, &tirs[i].rqt);
2492 static int mlx5e_rx_hash_fn(int hfunc)
2494 return (hfunc == ETH_RSS_HASH_TOP) ?
2495 MLX5_RX_HASH_FN_TOEPLITZ :
2496 MLX5_RX_HASH_FN_INVERTED_XOR8;
2499 int mlx5e_bits_invert(unsigned long a, int size)
2504 for (i = 0; i < size; i++)
2505 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2510 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2511 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2515 for (i = 0; i < sz; i++) {
2521 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2522 ix = mlx5e_bits_invert(i, ilog2(sz));
2524 ix = priv->rss_params.indirection_rqt[ix];
2525 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2529 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2533 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2534 struct mlx5e_redirect_rqt_param rrp)
2536 struct mlx5_core_dev *mdev = priv->mdev;
2542 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2543 in = kvzalloc(inlen, GFP_KERNEL);
2547 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2549 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2550 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2551 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2552 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2558 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2559 struct mlx5e_redirect_rqt_param rrp)
2564 if (ix >= rrp.rss.channels->num)
2565 return priv->drop_rq.rqn;
2567 return rrp.rss.channels->c[ix]->rq.rqn;
2570 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2571 struct mlx5e_redirect_rqt_param rrp)
2576 if (priv->indir_rqt.enabled) {
2578 rqtn = priv->indir_rqt.rqtn;
2579 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2582 for (ix = 0; ix < priv->max_nch; ix++) {
2583 struct mlx5e_redirect_rqt_param direct_rrp = {
2586 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2590 /* Direct RQ Tables */
2591 if (!priv->direct_tir[ix].rqt.enabled)
2594 rqtn = priv->direct_tir[ix].rqt.rqtn;
2595 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2599 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2600 struct mlx5e_channels *chs)
2602 struct mlx5e_redirect_rqt_param rrp = {
2607 .hfunc = priv->rss_params.hfunc,
2612 mlx5e_redirect_rqts(priv, rrp);
2615 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2617 struct mlx5e_redirect_rqt_param drop_rrp = {
2620 .rqn = priv->drop_rq.rqn,
2624 mlx5e_redirect_rqts(priv, drop_rrp);
2627 static const struct mlx5e_tirc_config tirc_default_config[MLX5E_NUM_INDIR_TIRS] = {
2628 [MLX5E_TT_IPV4_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2629 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2630 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2632 [MLX5E_TT_IPV6_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2633 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2634 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2636 [MLX5E_TT_IPV4_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2637 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2638 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2640 [MLX5E_TT_IPV6_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2641 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2642 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2644 [MLX5E_TT_IPV4_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2646 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2648 [MLX5E_TT_IPV6_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2650 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2652 [MLX5E_TT_IPV4_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2654 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2656 [MLX5E_TT_IPV6_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2658 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2660 [MLX5E_TT_IPV4] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2662 .rx_hash_fields = MLX5_HASH_IP,
2664 [MLX5E_TT_IPV6] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2666 .rx_hash_fields = MLX5_HASH_IP,
2670 struct mlx5e_tirc_config mlx5e_tirc_get_default_config(enum mlx5e_traffic_types tt)
2672 return tirc_default_config[tt];
2675 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2677 if (!params->lro_en)
2680 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2682 MLX5_SET(tirc, tirc, lro_enable_mask,
2683 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2684 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2685 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2686 (MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2687 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2690 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_rss_params *rss_params,
2691 const struct mlx5e_tirc_config *ttconfig,
2692 void *tirc, bool inner)
2694 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2695 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2697 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(rss_params->hfunc));
2698 if (rss_params->hfunc == ETH_RSS_HASH_TOP) {
2699 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2700 rx_hash_toeplitz_key);
2701 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2702 rx_hash_toeplitz_key);
2704 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2705 memcpy(rss_key, rss_params->toeplitz_hash_key, len);
2707 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2708 ttconfig->l3_prot_type);
2709 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2710 ttconfig->l4_prot_type);
2711 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2712 ttconfig->rx_hash_fields);
2715 static void mlx5e_update_rx_hash_fields(struct mlx5e_tirc_config *ttconfig,
2716 enum mlx5e_traffic_types tt,
2719 *ttconfig = tirc_default_config[tt];
2720 ttconfig->rx_hash_fields = rx_hash_fields;
2723 void mlx5e_modify_tirs_hash(struct mlx5e_priv *priv, void *in, int inlen)
2725 void *tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2726 struct mlx5e_rss_params *rss = &priv->rss_params;
2727 struct mlx5_core_dev *mdev = priv->mdev;
2728 int ctxlen = MLX5_ST_SZ_BYTES(tirc);
2729 struct mlx5e_tirc_config ttconfig;
2732 MLX5_SET(modify_tir_in, in, bitmask.hash, 1);
2734 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2735 memset(tirc, 0, ctxlen);
2736 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2737 rss->rx_hash_fields[tt]);
2738 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, false);
2739 mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in, inlen);
2742 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
2745 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2746 memset(tirc, 0, ctxlen);
2747 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2748 rss->rx_hash_fields[tt]);
2749 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, true);
2750 mlx5_core_modify_tir(mdev, priv->inner_indir_tir[tt].tirn, in,
2755 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2757 struct mlx5_core_dev *mdev = priv->mdev;
2766 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2767 in = kvzalloc(inlen, GFP_KERNEL);
2771 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2772 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2774 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2776 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2777 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2783 for (ix = 0; ix < priv->max_nch; ix++) {
2784 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2796 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2797 struct mlx5e_params *params, u16 mtu)
2799 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2802 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2806 /* Update vport context MTU */
2807 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2811 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2812 struct mlx5e_params *params, u16 *mtu)
2817 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2818 if (err || !hw_mtu) /* fallback to port oper mtu */
2819 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2821 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2824 int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2826 struct mlx5e_params *params = &priv->channels.params;
2827 struct net_device *netdev = priv->netdev;
2828 struct mlx5_core_dev *mdev = priv->mdev;
2832 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2836 mlx5e_query_mtu(mdev, params, &mtu);
2837 if (mtu != params->sw_mtu)
2838 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2839 __func__, mtu, params->sw_mtu);
2841 params->sw_mtu = mtu;
2845 void mlx5e_set_netdev_mtu_boundaries(struct mlx5e_priv *priv)
2847 struct mlx5e_params *params = &priv->channels.params;
2848 struct net_device *netdev = priv->netdev;
2849 struct mlx5_core_dev *mdev = priv->mdev;
2852 /* MTU range: 68 - hw-specific max */
2853 netdev->min_mtu = ETH_MIN_MTU;
2855 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2856 netdev->max_mtu = min_t(unsigned int, MLX5E_HW2SW_MTU(params, max_mtu),
2860 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2862 struct mlx5e_priv *priv = netdev_priv(netdev);
2863 int nch = priv->channels.params.num_channels;
2864 int ntc = priv->channels.params.num_tc;
2867 netdev_reset_tc(netdev);
2872 netdev_set_num_tc(netdev, ntc);
2874 /* Map netdev TCs to offset 0
2875 * We have our own UP to TXQ mapping for QoS
2877 for (tc = 0; tc < ntc; tc++)
2878 netdev_set_tc_queue(netdev, tc, nch, 0);
2881 static void mlx5e_build_tc2txq_maps(struct mlx5e_priv *priv)
2885 for (i = 0; i < priv->max_nch; i++)
2886 for (tc = 0; tc < priv->profile->max_tc; tc++)
2887 priv->channel_tc2txq[i][tc] = i + tc * priv->max_nch;
2890 static void mlx5e_build_tx2sq_maps(struct mlx5e_priv *priv)
2892 struct mlx5e_channel *c;
2893 struct mlx5e_txqsq *sq;
2896 for (i = 0; i < priv->channels.num; i++) {
2897 c = priv->channels.c[i];
2898 for (tc = 0; tc < c->num_tc; tc++) {
2900 priv->txq2sq[sq->txq_ix] = sq;
2905 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2907 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2908 int num_rxqs = priv->channels.num * priv->profile->rq_groups;
2909 struct net_device *netdev = priv->netdev;
2911 mlx5e_netdev_set_tcs(netdev);
2912 netif_set_real_num_tx_queues(netdev, num_txqs);
2913 netif_set_real_num_rx_queues(netdev, num_rxqs);
2915 mlx5e_build_tx2sq_maps(priv);
2916 mlx5e_activate_channels(&priv->channels);
2917 mlx5e_xdp_tx_enable(priv);
2918 netif_tx_start_all_queues(priv->netdev);
2920 if (mlx5e_is_vport_rep(priv))
2921 mlx5e_add_sqs_fwd_rules(priv);
2923 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2924 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2926 mlx5e_xsk_redirect_rqts_to_channels(priv, &priv->channels);
2929 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2931 mlx5e_xsk_redirect_rqts_to_drop(priv, &priv->channels);
2933 mlx5e_redirect_rqts_to_drop(priv);
2935 if (mlx5e_is_vport_rep(priv))
2936 mlx5e_remove_sqs_fwd_rules(priv);
2938 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2939 * polling for inactive tx queues.
2941 netif_tx_stop_all_queues(priv->netdev);
2942 netif_tx_disable(priv->netdev);
2943 mlx5e_xdp_tx_disable(priv);
2944 mlx5e_deactivate_channels(&priv->channels);
2947 static void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2948 struct mlx5e_channels *new_chs,
2949 mlx5e_fp_hw_modify hw_modify)
2951 struct net_device *netdev = priv->netdev;
2955 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2957 carrier_ok = netif_carrier_ok(netdev);
2958 netif_carrier_off(netdev);
2960 if (new_num_txqs < netdev->real_num_tx_queues)
2961 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2963 mlx5e_deactivate_priv_channels(priv);
2964 mlx5e_close_channels(&priv->channels);
2966 priv->channels = *new_chs;
2968 /* New channels are ready to roll, modify HW settings if needed */
2972 priv->profile->update_rx(priv);
2973 mlx5e_activate_priv_channels(priv);
2975 /* return carrier back if needed */
2977 netif_carrier_on(netdev);
2980 int mlx5e_safe_switch_channels(struct mlx5e_priv *priv,
2981 struct mlx5e_channels *new_chs,
2982 mlx5e_fp_hw_modify hw_modify)
2986 err = mlx5e_open_channels(priv, new_chs);
2990 mlx5e_switch_priv_channels(priv, new_chs, hw_modify);
2994 int mlx5e_safe_reopen_channels(struct mlx5e_priv *priv)
2996 struct mlx5e_channels new_channels = {};
2998 new_channels.params = priv->channels.params;
2999 return mlx5e_safe_switch_channels(priv, &new_channels, NULL);
3002 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
3004 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
3005 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
3008 int mlx5e_open_locked(struct net_device *netdev)
3010 struct mlx5e_priv *priv = netdev_priv(netdev);
3011 bool is_xdp = priv->channels.params.xdp_prog;
3014 set_bit(MLX5E_STATE_OPENED, &priv->state);
3016 mlx5e_xdp_set_open(priv);
3018 err = mlx5e_open_channels(priv, &priv->channels);
3020 goto err_clear_state_opened_flag;
3022 priv->profile->update_rx(priv);
3023 mlx5e_activate_priv_channels(priv);
3024 if (priv->profile->update_carrier)
3025 priv->profile->update_carrier(priv);
3027 mlx5e_queue_update_stats(priv);
3030 err_clear_state_opened_flag:
3032 mlx5e_xdp_set_closed(priv);
3033 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3037 int mlx5e_open(struct net_device *netdev)
3039 struct mlx5e_priv *priv = netdev_priv(netdev);
3042 mutex_lock(&priv->state_lock);
3043 err = mlx5e_open_locked(netdev);
3045 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
3046 mutex_unlock(&priv->state_lock);
3048 if (mlx5_vxlan_allowed(priv->mdev->vxlan))
3049 udp_tunnel_get_rx_info(netdev);
3054 int mlx5e_close_locked(struct net_device *netdev)
3056 struct mlx5e_priv *priv = netdev_priv(netdev);
3058 /* May already be CLOSED in case a previous configuration operation
3059 * (e.g RX/TX queue size change) that involves close&open failed.
3061 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3064 if (priv->channels.params.xdp_prog)
3065 mlx5e_xdp_set_closed(priv);
3066 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3068 netif_carrier_off(priv->netdev);
3069 mlx5e_deactivate_priv_channels(priv);
3070 mlx5e_close_channels(&priv->channels);
3075 int mlx5e_close(struct net_device *netdev)
3077 struct mlx5e_priv *priv = netdev_priv(netdev);
3080 if (!netif_device_present(netdev))
3083 mutex_lock(&priv->state_lock);
3084 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
3085 err = mlx5e_close_locked(netdev);
3086 mutex_unlock(&priv->state_lock);
3091 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
3092 struct mlx5e_rq *rq,
3093 struct mlx5e_rq_param *param)
3095 void *rqc = param->rqc;
3096 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
3099 param->wq.db_numa_node = param->wq.buf_numa_node;
3101 err = mlx5_wq_cyc_create(mdev, ¶m->wq, rqc_wq, &rq->wqe.wq,
3106 /* Mark as unused given "Drop-RQ" packets never reach XDP */
3107 xdp_rxq_info_unused(&rq->xdp_rxq);
3114 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
3115 struct mlx5e_cq *cq,
3116 struct mlx5e_cq_param *param)
3118 param->wq.buf_numa_node = dev_to_node(mdev->device);
3119 param->wq.db_numa_node = dev_to_node(mdev->device);
3121 return mlx5e_alloc_cq_common(mdev, param, cq);
3124 int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
3125 struct mlx5e_rq *drop_rq)
3127 struct mlx5_core_dev *mdev = priv->mdev;
3128 struct mlx5e_cq_param cq_param = {};
3129 struct mlx5e_rq_param rq_param = {};
3130 struct mlx5e_cq *cq = &drop_rq->cq;
3133 mlx5e_build_drop_rq_param(priv, &rq_param);
3135 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
3139 err = mlx5e_create_cq(cq, &cq_param);
3143 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3145 goto err_destroy_cq;
3147 err = mlx5e_create_rq(drop_rq, &rq_param);
3151 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3153 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3158 mlx5e_free_rq(drop_rq);
3161 mlx5e_destroy_cq(cq);
3169 void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3171 mlx5e_destroy_rq(drop_rq);
3172 mlx5e_free_rq(drop_rq);
3173 mlx5e_destroy_cq(&drop_rq->cq);
3174 mlx5e_free_cq(&drop_rq->cq);
3177 int mlx5e_create_tis(struct mlx5_core_dev *mdev, void *in, u32 *tisn)
3179 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3181 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
3183 if (MLX5_GET(tisc, tisc, tls_en))
3184 MLX5_SET(tisc, tisc, pd, mdev->mlx5e_res.pdn);
3186 if (mlx5_lag_is_lacp_owner(mdev))
3187 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
3189 return mlx5_core_create_tis(mdev, in, MLX5_ST_SZ_BYTES(create_tis_in), tisn);
3192 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
3194 mlx5_core_destroy_tis(mdev, tisn);
3197 void mlx5e_destroy_tises(struct mlx5e_priv *priv)
3201 for (i = 0; i < mlx5e_get_num_lag_ports(priv->mdev); i++)
3202 for (tc = 0; tc < priv->profile->max_tc; tc++)
3203 mlx5e_destroy_tis(priv->mdev, priv->tisn[i][tc]);
3206 static bool mlx5e_lag_should_assign_affinity(struct mlx5_core_dev *mdev)
3208 return MLX5_CAP_GEN(mdev, lag_tx_port_affinity) && mlx5e_get_num_lag_ports(mdev) > 1;
3211 int mlx5e_create_tises(struct mlx5e_priv *priv)
3216 for (i = 0; i < mlx5e_get_num_lag_ports(priv->mdev); i++) {
3217 for (tc = 0; tc < priv->profile->max_tc; tc++) {
3218 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
3221 tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3223 MLX5_SET(tisc, tisc, prio, tc << 1);
3225 if (mlx5e_lag_should_assign_affinity(priv->mdev))
3226 MLX5_SET(tisc, tisc, lag_tx_port_affinity, i + 1);
3228 err = mlx5e_create_tis(priv->mdev, in, &priv->tisn[i][tc]);
3230 goto err_close_tises;
3237 for (; i >= 0; i--) {
3238 for (tc--; tc >= 0; tc--)
3239 mlx5e_destroy_tis(priv->mdev, priv->tisn[i][tc]);
3240 tc = priv->profile->max_tc;
3246 static void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3248 mlx5e_destroy_tises(priv);
3251 static void mlx5e_build_indir_tir_ctx_common(struct mlx5e_priv *priv,
3252 u32 rqtn, u32 *tirc)
3254 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3255 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3256 MLX5_SET(tirc, tirc, indirect_table, rqtn);
3257 MLX5_SET(tirc, tirc, tunneled_offload_en,
3258 priv->channels.params.tunneled_offload_en);
3260 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3263 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
3264 enum mlx5e_traffic_types tt,
3267 mlx5e_build_indir_tir_ctx_common(priv, priv->indir_rqt.rqtn, tirc);
3268 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
3269 &tirc_default_config[tt], tirc, false);
3272 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
3274 mlx5e_build_indir_tir_ctx_common(priv, rqtn, tirc);
3275 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
3278 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
3279 enum mlx5e_traffic_types tt,
3282 mlx5e_build_indir_tir_ctx_common(priv, priv->indir_rqt.rqtn, tirc);
3283 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
3284 &tirc_default_config[tt], tirc, true);
3287 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3289 struct mlx5e_tir *tir;
3297 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3298 in = kvzalloc(inlen, GFP_KERNEL);
3302 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
3303 memset(in, 0, inlen);
3304 tir = &priv->indir_tir[tt];
3305 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3306 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
3307 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3309 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
3310 goto err_destroy_inner_tirs;
3314 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3317 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
3318 memset(in, 0, inlen);
3319 tir = &priv->inner_indir_tir[i];
3320 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3321 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
3322 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3324 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
3325 goto err_destroy_inner_tirs;
3334 err_destroy_inner_tirs:
3335 for (i--; i >= 0; i--)
3336 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3338 for (tt--; tt >= 0; tt--)
3339 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3346 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
3348 struct mlx5e_tir *tir;
3355 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3356 in = kvzalloc(inlen, GFP_KERNEL);
3360 for (ix = 0; ix < priv->max_nch; ix++) {
3361 memset(in, 0, inlen);
3363 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3364 mlx5e_build_direct_tir_ctx(priv, tir->rqt.rqtn, tirc);
3365 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3367 goto err_destroy_ch_tirs;
3372 err_destroy_ch_tirs:
3373 mlx5_core_warn(priv->mdev, "create tirs failed, %d\n", err);
3374 for (ix--; ix >= 0; ix--)
3375 mlx5e_destroy_tir(priv->mdev, &tirs[ix]);
3383 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3387 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3388 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3390 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3393 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3394 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3397 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
3401 for (i = 0; i < priv->max_nch; i++)
3402 mlx5e_destroy_tir(priv->mdev, &tirs[i]);
3405 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3410 for (i = 0; i < chs->num; i++) {
3411 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3419 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3424 for (i = 0; i < chs->num; i++) {
3425 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3433 static int mlx5e_setup_tc_mqprio(struct mlx5e_priv *priv,
3434 struct tc_mqprio_qopt *mqprio)
3436 struct mlx5e_channels new_channels = {};
3437 u8 tc = mqprio->num_tc;
3440 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3442 if (tc && tc != MLX5E_MAX_NUM_TC)
3445 mutex_lock(&priv->state_lock);
3447 new_channels.params = priv->channels.params;
3448 new_channels.params.num_tc = tc ? tc : 1;
3450 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3451 priv->channels.params = new_channels.params;
3455 err = mlx5e_safe_switch_channels(priv, &new_channels, NULL);
3459 priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3460 new_channels.params.num_tc);
3462 mutex_unlock(&priv->state_lock);
3466 #ifdef CONFIG_MLX5_ESWITCH
3467 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3468 struct flow_cls_offload *cls_flower,
3469 unsigned long flags)
3471 switch (cls_flower->command) {
3472 case FLOW_CLS_REPLACE:
3473 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
3475 case FLOW_CLS_DESTROY:
3476 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
3478 case FLOW_CLS_STATS:
3479 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
3486 static int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3489 unsigned long flags = MLX5_TC_FLAG(INGRESS) | MLX5_TC_FLAG(NIC_OFFLOAD);
3490 struct mlx5e_priv *priv = cb_priv;
3493 case TC_SETUP_CLSFLOWER:
3494 return mlx5e_setup_tc_cls_flower(priv, type_data, flags);
3501 static LIST_HEAD(mlx5e_block_cb_list);
3503 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3506 struct mlx5e_priv *priv = netdev_priv(dev);
3509 #ifdef CONFIG_MLX5_ESWITCH
3510 case TC_SETUP_BLOCK: {
3511 struct flow_block_offload *f = type_data;
3513 f->unlocked_driver_cb = true;
3514 return flow_block_cb_setup_simple(type_data,
3515 &mlx5e_block_cb_list,
3516 mlx5e_setup_tc_block_cb,
3520 case TC_SETUP_QDISC_MQPRIO:
3521 return mlx5e_setup_tc_mqprio(priv, type_data);
3527 void mlx5e_fold_sw_stats64(struct mlx5e_priv *priv, struct rtnl_link_stats64 *s)
3531 for (i = 0; i < priv->max_nch; i++) {
3532 struct mlx5e_channel_stats *channel_stats = &priv->channel_stats[i];
3533 struct mlx5e_rq_stats *xskrq_stats = &channel_stats->xskrq;
3534 struct mlx5e_rq_stats *rq_stats = &channel_stats->rq;
3537 s->rx_packets += rq_stats->packets + xskrq_stats->packets;
3538 s->rx_bytes += rq_stats->bytes + xskrq_stats->bytes;
3540 for (j = 0; j < priv->max_opened_tc; j++) {
3541 struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[j];
3543 s->tx_packets += sq_stats->packets;
3544 s->tx_bytes += sq_stats->bytes;
3545 s->tx_dropped += sq_stats->dropped;
3551 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3553 struct mlx5e_priv *priv = netdev_priv(dev);
3554 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3555 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3557 if (!mlx5e_monitor_counter_supported(priv)) {
3558 /* update HW stats in background for next time */
3559 mlx5e_queue_update_stats(priv);
3562 if (mlx5e_is_uplink_rep(priv)) {
3563 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3564 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3565 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3566 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3568 mlx5e_fold_sw_stats64(priv, stats);
3571 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3573 stats->rx_length_errors =
3574 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3575 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3576 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3577 stats->rx_crc_errors =
3578 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3579 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3580 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3581 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3582 stats->rx_frame_errors;
3583 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3585 /* vport multicast also counts packets that are dropped due to steering
3586 * or rx out of buffer
3589 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3592 static void mlx5e_set_rx_mode(struct net_device *dev)
3594 struct mlx5e_priv *priv = netdev_priv(dev);
3596 queue_work(priv->wq, &priv->set_rx_mode_work);
3599 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3601 struct mlx5e_priv *priv = netdev_priv(netdev);
3602 struct sockaddr *saddr = addr;
3604 if (!is_valid_ether_addr(saddr->sa_data))
3605 return -EADDRNOTAVAIL;
3607 netif_addr_lock_bh(netdev);
3608 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3609 netif_addr_unlock_bh(netdev);
3611 queue_work(priv->wq, &priv->set_rx_mode_work);
3616 #define MLX5E_SET_FEATURE(features, feature, enable) \
3619 *features |= feature; \
3621 *features &= ~feature; \
3624 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3626 static int set_feature_lro(struct net_device *netdev, bool enable)
3628 struct mlx5e_priv *priv = netdev_priv(netdev);
3629 struct mlx5_core_dev *mdev = priv->mdev;
3630 struct mlx5e_channels new_channels = {};
3631 struct mlx5e_params *old_params;
3635 mutex_lock(&priv->state_lock);
3637 if (enable && priv->xsk.refcnt) {
3638 netdev_warn(netdev, "LRO is incompatible with AF_XDP (%hu XSKs are active)\n",
3644 old_params = &priv->channels.params;
3645 if (enable && !MLX5E_GET_PFLAG(old_params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3646 netdev_warn(netdev, "can't set LRO with legacy RQ\n");
3651 reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3653 new_channels.params = *old_params;
3654 new_channels.params.lro_en = enable;
3656 if (old_params->rq_wq_type != MLX5_WQ_TYPE_CYCLIC) {
3657 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params, NULL) ==
3658 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params, NULL))
3663 *old_params = new_channels.params;
3664 err = mlx5e_modify_tirs_lro(priv);
3668 err = mlx5e_safe_switch_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3670 mutex_unlock(&priv->state_lock);
3674 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3676 struct mlx5e_priv *priv = netdev_priv(netdev);
3679 mlx5e_enable_cvlan_filter(priv);
3681 mlx5e_disable_cvlan_filter(priv);
3686 #ifdef CONFIG_MLX5_ESWITCH
3687 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3689 struct mlx5e_priv *priv = netdev_priv(netdev);
3691 if (!enable && mlx5e_tc_num_filters(priv, MLX5_TC_FLAG(NIC_OFFLOAD))) {
3693 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3701 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3703 struct mlx5e_priv *priv = netdev_priv(netdev);
3704 struct mlx5_core_dev *mdev = priv->mdev;
3706 return mlx5_set_port_fcs(mdev, !enable);
3709 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3711 struct mlx5e_priv *priv = netdev_priv(netdev);
3714 mutex_lock(&priv->state_lock);
3716 priv->channels.params.scatter_fcs_en = enable;
3717 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3719 priv->channels.params.scatter_fcs_en = !enable;
3721 mutex_unlock(&priv->state_lock);
3726 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3728 struct mlx5e_priv *priv = netdev_priv(netdev);
3731 mutex_lock(&priv->state_lock);
3733 priv->channels.params.vlan_strip_disable = !enable;
3734 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3737 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3739 priv->channels.params.vlan_strip_disable = enable;
3742 mutex_unlock(&priv->state_lock);
3747 #ifdef CONFIG_MLX5_EN_ARFS
3748 static int set_feature_arfs(struct net_device *netdev, bool enable)
3750 struct mlx5e_priv *priv = netdev_priv(netdev);
3754 err = mlx5e_arfs_enable(priv);
3756 err = mlx5e_arfs_disable(priv);
3762 static int mlx5e_handle_feature(struct net_device *netdev,
3763 netdev_features_t *features,
3764 netdev_features_t wanted_features,
3765 netdev_features_t feature,
3766 mlx5e_feature_handler feature_handler)
3768 netdev_features_t changes = wanted_features ^ netdev->features;
3769 bool enable = !!(wanted_features & feature);
3772 if (!(changes & feature))
3775 err = feature_handler(netdev, enable);
3777 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3778 enable ? "Enable" : "Disable", &feature, err);
3782 MLX5E_SET_FEATURE(features, feature, enable);
3786 int mlx5e_set_features(struct net_device *netdev, netdev_features_t features)
3788 netdev_features_t oper_features = netdev->features;
3791 #define MLX5E_HANDLE_FEATURE(feature, handler) \
3792 mlx5e_handle_feature(netdev, &oper_features, features, feature, handler)
3794 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
3795 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
3796 set_feature_cvlan_filter);
3797 #ifdef CONFIG_MLX5_ESWITCH
3798 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_tc_num_filters);
3800 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
3801 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
3802 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3803 #ifdef CONFIG_MLX5_EN_ARFS
3804 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
3808 netdev->features = oper_features;
3815 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3816 netdev_features_t features)
3818 struct mlx5e_priv *priv = netdev_priv(netdev);
3819 struct mlx5e_params *params;
3821 mutex_lock(&priv->state_lock);
3822 params = &priv->channels.params;
3823 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3824 /* HW strips the outer C-tag header, this is a problem
3825 * for S-tag traffic.
3827 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3828 if (!params->vlan_strip_disable)
3829 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3831 if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3832 if (features & NETIF_F_LRO) {
3833 netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n");
3834 features &= ~NETIF_F_LRO;
3838 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
3839 features &= ~NETIF_F_RXHASH;
3840 if (netdev->features & NETIF_F_RXHASH)
3841 netdev_warn(netdev, "Disabling rxhash, not supported when CQE compress is active\n");
3844 mutex_unlock(&priv->state_lock);
3849 static bool mlx5e_xsk_validate_mtu(struct net_device *netdev,
3850 struct mlx5e_channels *chs,
3851 struct mlx5e_params *new_params,
3852 struct mlx5_core_dev *mdev)
3856 for (ix = 0; ix < chs->params.num_channels; ix++) {
3857 struct xdp_umem *umem = mlx5e_xsk_get_umem(&chs->params, chs->params.xsk, ix);
3858 struct mlx5e_xsk_param xsk;
3863 mlx5e_build_xsk_param(umem, &xsk);
3865 if (!mlx5e_validate_xsk_param(new_params, &xsk, mdev)) {
3866 u32 hr = mlx5e_get_linear_rq_headroom(new_params, &xsk);
3867 int max_mtu_frame, max_mtu_page, max_mtu;
3869 /* Two criteria must be met:
3870 * 1. HW MTU + all headrooms <= XSK frame size.
3871 * 2. Size of SKBs allocated on XDP_PASS <= PAGE_SIZE.
3873 max_mtu_frame = MLX5E_HW2SW_MTU(new_params, xsk.chunk_size - hr);
3874 max_mtu_page = mlx5e_xdp_max_mtu(new_params, &xsk);
3875 max_mtu = min(max_mtu_frame, max_mtu_page);
3877 netdev_err(netdev, "MTU %d is too big for an XSK running on channel %hu. Try MTU <= %d\n",
3878 new_params->sw_mtu, ix, max_mtu);
3886 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu,
3887 change_hw_mtu_cb set_mtu_cb)
3889 struct mlx5e_priv *priv = netdev_priv(netdev);
3890 struct mlx5e_channels new_channels = {};
3891 struct mlx5e_params *params;
3895 mutex_lock(&priv->state_lock);
3897 params = &priv->channels.params;
3899 reset = !params->lro_en;
3900 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3902 new_channels.params = *params;
3903 new_channels.params.sw_mtu = new_mtu;
3905 if (params->xdp_prog &&
3906 !mlx5e_rx_is_linear_skb(&new_channels.params, NULL)) {
3907 netdev_err(netdev, "MTU(%d) > %d is not allowed while XDP enabled\n",
3908 new_mtu, mlx5e_xdp_max_mtu(params, NULL));
3913 if (priv->xsk.refcnt &&
3914 !mlx5e_xsk_validate_mtu(netdev, &priv->channels,
3915 &new_channels.params, priv->mdev)) {
3920 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
3921 bool is_linear = mlx5e_rx_mpwqe_is_linear_skb(priv->mdev,
3922 &new_channels.params,
3924 u8 ppw_old = mlx5e_mpwqe_log_pkts_per_wqe(params, NULL);
3925 u8 ppw_new = mlx5e_mpwqe_log_pkts_per_wqe(&new_channels.params, NULL);
3927 /* If XSK is active, XSK RQs are linear. */
3928 is_linear |= priv->xsk.refcnt;
3930 /* Always reset in linear mode - hw_mtu is used in data path. */
3931 reset = reset && (is_linear || (ppw_old != ppw_new));
3935 params->sw_mtu = new_mtu;
3938 netdev->mtu = params->sw_mtu;
3942 err = mlx5e_safe_switch_channels(priv, &new_channels, set_mtu_cb);
3946 netdev->mtu = new_channels.params.sw_mtu;
3949 mutex_unlock(&priv->state_lock);
3953 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu)
3955 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
3958 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3960 struct hwtstamp_config config;
3963 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz) ||
3964 (mlx5_clock_get_ptp_index(priv->mdev) == -1))
3967 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3970 /* TX HW timestamp */
3971 switch (config.tx_type) {
3972 case HWTSTAMP_TX_OFF:
3973 case HWTSTAMP_TX_ON:
3979 mutex_lock(&priv->state_lock);
3980 /* RX HW timestamp */
3981 switch (config.rx_filter) {
3982 case HWTSTAMP_FILTER_NONE:
3983 /* Reset CQE compression to Admin default */
3984 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3986 case HWTSTAMP_FILTER_ALL:
3987 case HWTSTAMP_FILTER_SOME:
3988 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3989 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3990 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3991 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3992 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3993 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3994 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3995 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3996 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3997 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3998 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3999 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
4000 case HWTSTAMP_FILTER_NTP_ALL:
4001 /* Disable CQE compression */
4002 if (MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS))
4003 netdev_warn(priv->netdev, "Disabling RX cqe compression\n");
4004 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
4006 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
4007 mutex_unlock(&priv->state_lock);
4010 config.rx_filter = HWTSTAMP_FILTER_ALL;
4013 mutex_unlock(&priv->state_lock);
4017 memcpy(&priv->tstamp, &config, sizeof(config));
4018 mutex_unlock(&priv->state_lock);
4020 /* might need to fix some features */
4021 netdev_update_features(priv->netdev);
4023 return copy_to_user(ifr->ifr_data, &config,
4024 sizeof(config)) ? -EFAULT : 0;
4027 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
4029 struct hwtstamp_config *cfg = &priv->tstamp;
4031 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
4034 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
4037 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4039 struct mlx5e_priv *priv = netdev_priv(dev);
4043 return mlx5e_hwstamp_set(priv, ifr);
4045 return mlx5e_hwstamp_get(priv, ifr);
4051 #ifdef CONFIG_MLX5_ESWITCH
4052 int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
4054 struct mlx5e_priv *priv = netdev_priv(dev);
4055 struct mlx5_core_dev *mdev = priv->mdev;
4057 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
4060 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
4063 struct mlx5e_priv *priv = netdev_priv(dev);
4064 struct mlx5_core_dev *mdev = priv->mdev;
4066 if (vlan_proto != htons(ETH_P_8021Q))
4067 return -EPROTONOSUPPORT;
4069 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
4073 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
4075 struct mlx5e_priv *priv = netdev_priv(dev);
4076 struct mlx5_core_dev *mdev = priv->mdev;
4078 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
4081 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
4083 struct mlx5e_priv *priv = netdev_priv(dev);
4084 struct mlx5_core_dev *mdev = priv->mdev;
4086 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
4089 int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
4092 struct mlx5e_priv *priv = netdev_priv(dev);
4093 struct mlx5_core_dev *mdev = priv->mdev;
4095 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
4096 max_tx_rate, min_tx_rate);
4099 static int mlx5_vport_link2ifla(u8 esw_link)
4102 case MLX5_VPORT_ADMIN_STATE_DOWN:
4103 return IFLA_VF_LINK_STATE_DISABLE;
4104 case MLX5_VPORT_ADMIN_STATE_UP:
4105 return IFLA_VF_LINK_STATE_ENABLE;
4107 return IFLA_VF_LINK_STATE_AUTO;
4110 static int mlx5_ifla_link2vport(u8 ifla_link)
4112 switch (ifla_link) {
4113 case IFLA_VF_LINK_STATE_DISABLE:
4114 return MLX5_VPORT_ADMIN_STATE_DOWN;
4115 case IFLA_VF_LINK_STATE_ENABLE:
4116 return MLX5_VPORT_ADMIN_STATE_UP;
4118 return MLX5_VPORT_ADMIN_STATE_AUTO;
4121 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
4124 struct mlx5e_priv *priv = netdev_priv(dev);
4125 struct mlx5_core_dev *mdev = priv->mdev;
4127 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
4128 mlx5_ifla_link2vport(link_state));
4131 int mlx5e_get_vf_config(struct net_device *dev,
4132 int vf, struct ifla_vf_info *ivi)
4134 struct mlx5e_priv *priv = netdev_priv(dev);
4135 struct mlx5_core_dev *mdev = priv->mdev;
4138 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
4141 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
4145 int mlx5e_get_vf_stats(struct net_device *dev,
4146 int vf, struct ifla_vf_stats *vf_stats)
4148 struct mlx5e_priv *priv = netdev_priv(dev);
4149 struct mlx5_core_dev *mdev = priv->mdev;
4151 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
4156 struct mlx5e_vxlan_work {
4157 struct work_struct work;
4158 struct mlx5e_priv *priv;
4162 static void mlx5e_vxlan_add_work(struct work_struct *work)
4164 struct mlx5e_vxlan_work *vxlan_work =
4165 container_of(work, struct mlx5e_vxlan_work, work);
4166 struct mlx5e_priv *priv = vxlan_work->priv;
4167 u16 port = vxlan_work->port;
4169 mutex_lock(&priv->state_lock);
4170 mlx5_vxlan_add_port(priv->mdev->vxlan, port);
4171 mutex_unlock(&priv->state_lock);
4176 static void mlx5e_vxlan_del_work(struct work_struct *work)
4178 struct mlx5e_vxlan_work *vxlan_work =
4179 container_of(work, struct mlx5e_vxlan_work, work);
4180 struct mlx5e_priv *priv = vxlan_work->priv;
4181 u16 port = vxlan_work->port;
4183 mutex_lock(&priv->state_lock);
4184 mlx5_vxlan_del_port(priv->mdev->vxlan, port);
4185 mutex_unlock(&priv->state_lock);
4189 static void mlx5e_vxlan_queue_work(struct mlx5e_priv *priv, u16 port, int add)
4191 struct mlx5e_vxlan_work *vxlan_work;
4193 vxlan_work = kmalloc(sizeof(*vxlan_work), GFP_ATOMIC);
4198 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_add_work);
4200 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_del_work);
4202 vxlan_work->priv = priv;
4203 vxlan_work->port = port;
4204 queue_work(priv->wq, &vxlan_work->work);
4207 void mlx5e_add_vxlan_port(struct net_device *netdev, struct udp_tunnel_info *ti)
4209 struct mlx5e_priv *priv = netdev_priv(netdev);
4211 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4214 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4217 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 1);
4220 void mlx5e_del_vxlan_port(struct net_device *netdev, struct udp_tunnel_info *ti)
4222 struct mlx5e_priv *priv = netdev_priv(netdev);
4224 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4227 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4230 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 0);
4233 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
4234 struct sk_buff *skb,
4235 netdev_features_t features)
4237 unsigned int offset = 0;
4238 struct udphdr *udph;
4242 switch (vlan_get_protocol(skb)) {
4243 case htons(ETH_P_IP):
4244 proto = ip_hdr(skb)->protocol;
4246 case htons(ETH_P_IPV6):
4247 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
4259 udph = udp_hdr(skb);
4260 port = be16_to_cpu(udph->dest);
4262 /* Verify if UDP port is being offloaded by HW */
4263 if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, port))
4266 #if IS_ENABLED(CONFIG_GENEVE)
4267 /* Support Geneve offload for default UDP port */
4268 if (port == GENEVE_UDP_PORT && mlx5_geneve_tx_allowed(priv->mdev))
4274 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
4275 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4278 netdev_features_t mlx5e_features_check(struct sk_buff *skb,
4279 struct net_device *netdev,
4280 netdev_features_t features)
4282 struct mlx5e_priv *priv = netdev_priv(netdev);
4284 features = vlan_features_check(skb, features);
4285 features = vxlan_features_check(skb, features);
4287 #ifdef CONFIG_MLX5_EN_IPSEC
4288 if (mlx5e_ipsec_feature_check(skb, netdev, features))
4292 /* Validate if the tunneled packet is being offloaded by HW */
4293 if (skb->encapsulation &&
4294 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
4295 return mlx5e_tunnel_features_check(priv, skb, features);
4300 static void mlx5e_tx_timeout_work(struct work_struct *work)
4302 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
4304 bool report_failed = false;
4309 mutex_lock(&priv->state_lock);
4311 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
4314 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
4315 struct netdev_queue *dev_queue =
4316 netdev_get_tx_queue(priv->netdev, i);
4317 struct mlx5e_txqsq *sq = priv->txq2sq[i];
4319 if (!netif_xmit_stopped(dev_queue))
4322 if (mlx5e_reporter_tx_timeout(sq))
4323 report_failed = true;
4329 err = mlx5e_safe_reopen_channels(priv);
4331 netdev_err(priv->netdev,
4332 "mlx5e_safe_reopen_channels failed recovering from a tx_timeout, err(%d).\n",
4336 mutex_unlock(&priv->state_lock);
4340 static void mlx5e_tx_timeout(struct net_device *dev)
4342 struct mlx5e_priv *priv = netdev_priv(dev);
4344 netdev_err(dev, "TX timeout detected\n");
4345 queue_work(priv->wq, &priv->tx_timeout_work);
4348 static int mlx5e_xdp_allowed(struct mlx5e_priv *priv, struct bpf_prog *prog)
4350 struct net_device *netdev = priv->netdev;
4351 struct mlx5e_channels new_channels = {};
4353 if (priv->channels.params.lro_en) {
4354 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
4358 if (MLX5_IPSEC_DEV(priv->mdev)) {
4359 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
4363 new_channels.params = priv->channels.params;
4364 new_channels.params.xdp_prog = prog;
4366 /* No XSK params: AF_XDP can't be enabled yet at the point of setting
4369 if (!mlx5e_rx_is_linear_skb(&new_channels.params, NULL)) {
4370 netdev_warn(netdev, "XDP is not allowed with MTU(%d) > %d\n",
4371 new_channels.params.sw_mtu,
4372 mlx5e_xdp_max_mtu(&new_channels.params, NULL));
4379 static int mlx5e_xdp_update_state(struct mlx5e_priv *priv)
4381 if (priv->channels.params.xdp_prog)
4382 mlx5e_xdp_set_open(priv);
4384 mlx5e_xdp_set_closed(priv);
4389 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
4391 struct mlx5e_priv *priv = netdev_priv(netdev);
4392 struct bpf_prog *old_prog;
4393 bool reset, was_opened;
4397 mutex_lock(&priv->state_lock);
4400 err = mlx5e_xdp_allowed(priv, prog);
4405 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
4406 /* no need for full reset when exchanging programs */
4407 reset = (!priv->channels.params.xdp_prog || !prog);
4409 if (was_opened && !reset) {
4410 /* num_channels is invariant here, so we can take the
4411 * batched reference right upfront.
4413 prog = bpf_prog_add(prog, priv->channels.num);
4415 err = PTR_ERR(prog);
4420 if (was_opened && reset) {
4421 struct mlx5e_channels new_channels = {};
4423 new_channels.params = priv->channels.params;
4424 new_channels.params.xdp_prog = prog;
4425 mlx5e_set_rq_type(priv->mdev, &new_channels.params);
4426 old_prog = priv->channels.params.xdp_prog;
4428 err = mlx5e_safe_switch_channels(priv, &new_channels, mlx5e_xdp_update_state);
4432 /* exchange programs, extra prog reference we got from caller
4433 * as long as we don't fail from this point onwards.
4435 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
4439 bpf_prog_put(old_prog);
4441 if (!was_opened && reset) /* change RQ type according to priv->xdp_prog */
4442 mlx5e_set_rq_type(priv->mdev, &priv->channels.params);
4444 if (!was_opened || reset)
4447 /* exchanging programs w/o reset, we update ref counts on behalf
4448 * of the channels RQs here.
4450 for (i = 0; i < priv->channels.num; i++) {
4451 struct mlx5e_channel *c = priv->channels.c[i];
4452 bool xsk_open = test_bit(MLX5E_CHANNEL_STATE_XSK, c->state);
4454 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4456 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->xskrq.state);
4457 napi_synchronize(&c->napi);
4458 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
4460 old_prog = xchg(&c->rq.xdp_prog, prog);
4462 bpf_prog_put(old_prog);
4465 old_prog = xchg(&c->xskrq.xdp_prog, prog);
4467 bpf_prog_put(old_prog);
4470 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4472 set_bit(MLX5E_RQ_STATE_ENABLED, &c->xskrq.state);
4473 /* napi_schedule in case we have missed anything */
4474 napi_schedule(&c->napi);
4478 mutex_unlock(&priv->state_lock);
4482 static u32 mlx5e_xdp_query(struct net_device *dev)
4484 struct mlx5e_priv *priv = netdev_priv(dev);
4485 const struct bpf_prog *xdp_prog;
4488 mutex_lock(&priv->state_lock);
4489 xdp_prog = priv->channels.params.xdp_prog;
4491 prog_id = xdp_prog->aux->id;
4492 mutex_unlock(&priv->state_lock);
4497 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4499 switch (xdp->command) {
4500 case XDP_SETUP_PROG:
4501 return mlx5e_xdp_set(dev, xdp->prog);
4502 case XDP_QUERY_PROG:
4503 xdp->prog_id = mlx5e_xdp_query(dev);
4505 case XDP_SETUP_XSK_UMEM:
4506 return mlx5e_xsk_setup_umem(dev, xdp->xsk.umem,
4513 #ifdef CONFIG_MLX5_ESWITCH
4514 static int mlx5e_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4515 struct net_device *dev, u32 filter_mask,
4518 struct mlx5e_priv *priv = netdev_priv(dev);
4519 struct mlx5_core_dev *mdev = priv->mdev;
4523 err = mlx5_eswitch_get_vepa(mdev->priv.eswitch, &setting);
4526 mode = setting ? BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB;
4527 return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
4529 0, 0, nlflags, filter_mask, NULL);
4532 static int mlx5e_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
4533 u16 flags, struct netlink_ext_ack *extack)
4535 struct mlx5e_priv *priv = netdev_priv(dev);
4536 struct mlx5_core_dev *mdev = priv->mdev;
4537 struct nlattr *attr, *br_spec;
4538 u16 mode = BRIDGE_MODE_UNDEF;
4542 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4546 nla_for_each_nested(attr, br_spec, rem) {
4547 if (nla_type(attr) != IFLA_BRIDGE_MODE)
4550 if (nla_len(attr) < sizeof(mode))
4553 mode = nla_get_u16(attr);
4554 if (mode > BRIDGE_MODE_VEPA)
4560 if (mode == BRIDGE_MODE_UNDEF)
4563 setting = (mode == BRIDGE_MODE_VEPA) ? 1 : 0;
4564 return mlx5_eswitch_set_vepa(mdev->priv.eswitch, setting);
4568 const struct net_device_ops mlx5e_netdev_ops = {
4569 .ndo_open = mlx5e_open,
4570 .ndo_stop = mlx5e_close,
4571 .ndo_start_xmit = mlx5e_xmit,
4572 .ndo_setup_tc = mlx5e_setup_tc,
4573 .ndo_select_queue = mlx5e_select_queue,
4574 .ndo_get_stats64 = mlx5e_get_stats,
4575 .ndo_set_rx_mode = mlx5e_set_rx_mode,
4576 .ndo_set_mac_address = mlx5e_set_mac,
4577 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
4578 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
4579 .ndo_set_features = mlx5e_set_features,
4580 .ndo_fix_features = mlx5e_fix_features,
4581 .ndo_change_mtu = mlx5e_change_nic_mtu,
4582 .ndo_do_ioctl = mlx5e_ioctl,
4583 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
4584 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
4585 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
4586 .ndo_features_check = mlx5e_features_check,
4587 .ndo_tx_timeout = mlx5e_tx_timeout,
4588 .ndo_bpf = mlx5e_xdp,
4589 .ndo_xdp_xmit = mlx5e_xdp_xmit,
4590 .ndo_xsk_wakeup = mlx5e_xsk_wakeup,
4591 #ifdef CONFIG_MLX5_EN_ARFS
4592 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
4594 #ifdef CONFIG_MLX5_ESWITCH
4595 .ndo_bridge_setlink = mlx5e_bridge_setlink,
4596 .ndo_bridge_getlink = mlx5e_bridge_getlink,
4598 /* SRIOV E-Switch NDOs */
4599 .ndo_set_vf_mac = mlx5e_set_vf_mac,
4600 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
4601 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
4602 .ndo_set_vf_trust = mlx5e_set_vf_trust,
4603 .ndo_set_vf_rate = mlx5e_set_vf_rate,
4604 .ndo_get_vf_config = mlx5e_get_vf_config,
4605 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
4606 .ndo_get_vf_stats = mlx5e_get_vf_stats,
4610 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
4612 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
4614 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
4615 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
4616 !MLX5_CAP_ETH(mdev, csum_cap) ||
4617 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
4618 !MLX5_CAP_ETH(mdev, vlan_cap) ||
4619 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
4620 MLX5_CAP_FLOWTABLE(mdev,
4621 flow_table_properties_nic_receive.max_ft_level)
4623 mlx5_core_warn(mdev,
4624 "Not creating net device, some required device capabilities are missing\n");
4627 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
4628 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
4629 if (!MLX5_CAP_GEN(mdev, cq_moderation))
4630 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
4635 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
4640 for (i = 0; i < len; i++)
4641 indirection_rqt[i] = i % num_channels;
4644 static bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
4649 mlx5e_port_max_linkspeed(mdev, &link_speed);
4650 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
4651 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
4652 link_speed, pci_bw);
4654 #define MLX5E_SLOW_PCI_RATIO (2)
4656 return link_speed && pci_bw &&
4657 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
4660 static struct dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode)
4662 struct dim_cq_moder moder;
4664 moder.cq_period_mode = cq_period_mode;
4665 moder.pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
4666 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
4667 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4668 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
4673 static struct dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
4675 struct dim_cq_moder moder;
4677 moder.cq_period_mode = cq_period_mode;
4678 moder.pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
4679 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
4680 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4681 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
4686 static u8 mlx5_to_net_dim_cq_period_mode(u8 cq_period_mode)
4688 return cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE ?
4689 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
4690 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4693 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4695 if (params->tx_dim_enabled) {
4696 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4698 params->tx_cq_moderation = net_dim_get_def_tx_moderation(dim_period_mode);
4700 params->tx_cq_moderation = mlx5e_get_def_tx_moderation(cq_period_mode);
4703 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
4704 params->tx_cq_moderation.cq_period_mode ==
4705 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4708 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4710 if (params->rx_dim_enabled) {
4711 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4713 params->rx_cq_moderation = net_dim_get_def_rx_moderation(dim_period_mode);
4715 params->rx_cq_moderation = mlx5e_get_def_rx_moderation(cq_period_mode);
4718 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
4719 params->rx_cq_moderation.cq_period_mode ==
4720 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4723 static u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
4727 /* The supported periods are organized in ascending order */
4728 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
4729 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
4732 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
4735 void mlx5e_build_rq_params(struct mlx5_core_dev *mdev,
4736 struct mlx5e_params *params)
4738 /* Prefer Striding RQ, unless any of the following holds:
4739 * - Striding RQ configuration is not possible/supported.
4740 * - Slow PCI heuristic.
4741 * - Legacy RQ would use linear SKB while Striding RQ would use non-linear.
4743 * No XSK params: checking the availability of striding RQ in general.
4745 if (!slow_pci_heuristic(mdev) &&
4746 mlx5e_striding_rq_possible(mdev, params) &&
4747 (mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ||
4748 !mlx5e_rx_is_linear_skb(params, NULL)))
4749 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, true);
4750 mlx5e_set_rq_type(mdev, params);
4751 mlx5e_init_rq_type_params(mdev, params);
4754 void mlx5e_build_rss_params(struct mlx5e_rss_params *rss_params,
4757 enum mlx5e_traffic_types tt;
4759 rss_params->hfunc = ETH_RSS_HASH_TOP;
4760 netdev_rss_key_fill(rss_params->toeplitz_hash_key,
4761 sizeof(rss_params->toeplitz_hash_key));
4762 mlx5e_build_default_indir_rqt(rss_params->indirection_rqt,
4763 MLX5E_INDIR_RQT_SIZE, num_channels);
4764 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
4765 rss_params->rx_hash_fields[tt] =
4766 tirc_default_config[tt].rx_hash_fields;
4769 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
4770 struct mlx5e_xsk *xsk,
4771 struct mlx5e_rss_params *rss_params,
4772 struct mlx5e_params *params,
4773 u16 max_channels, u16 mtu)
4775 u8 rx_cq_period_mode;
4777 params->sw_mtu = mtu;
4778 params->hard_mtu = MLX5E_ETH_HARD_MTU;
4779 params->num_channels = max_channels;
4783 params->log_sq_size = is_kdump_kernel() ?
4784 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
4785 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
4788 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE,
4789 MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe));
4791 /* set CQE compression */
4792 params->rx_cqe_compress_def = false;
4793 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
4794 MLX5_CAP_GEN(mdev, vport_group_manager))
4795 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
4797 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4798 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE, false);
4801 mlx5e_build_rq_params(mdev, params);
4805 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4806 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
4807 /* No XSK params: checking the availability of striding RQ in general. */
4808 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL))
4809 params->lro_en = !slow_pci_heuristic(mdev);
4811 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4813 /* CQ moderation params */
4814 rx_cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4815 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4816 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4817 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4818 params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4819 mlx5e_set_rx_cq_mode_params(params, rx_cq_period_mode);
4820 mlx5e_set_tx_cq_mode_params(params, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
4823 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
4826 mlx5e_build_rss_params(rss_params, params->num_channels);
4827 params->tunneled_offload_en =
4828 mlx5e_tunnel_inner_ft_supported(mdev);
4834 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4836 struct mlx5e_priv *priv = netdev_priv(netdev);
4838 mlx5_query_mac_address(priv->mdev, netdev->dev_addr);
4839 if (is_zero_ether_addr(netdev->dev_addr) &&
4840 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4841 eth_hw_addr_random(netdev);
4842 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4846 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4848 struct mlx5e_priv *priv = netdev_priv(netdev);
4849 struct mlx5_core_dev *mdev = priv->mdev;
4853 SET_NETDEV_DEV(netdev, mdev->device);
4855 netdev->netdev_ops = &mlx5e_netdev_ops;
4857 #ifdef CONFIG_MLX5_CORE_EN_DCB
4858 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4859 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4862 netdev->watchdog_timeo = 15 * HZ;
4864 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4866 netdev->vlan_features |= NETIF_F_SG;
4867 netdev->vlan_features |= NETIF_F_HW_CSUM;
4868 netdev->vlan_features |= NETIF_F_GRO;
4869 netdev->vlan_features |= NETIF_F_TSO;
4870 netdev->vlan_features |= NETIF_F_TSO6;
4871 netdev->vlan_features |= NETIF_F_RXCSUM;
4872 netdev->vlan_features |= NETIF_F_RXHASH;
4874 netdev->mpls_features |= NETIF_F_SG;
4875 netdev->mpls_features |= NETIF_F_HW_CSUM;
4876 netdev->mpls_features |= NETIF_F_TSO;
4877 netdev->mpls_features |= NETIF_F_TSO6;
4879 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
4880 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
4882 if (!!MLX5_CAP_ETH(mdev, lro_cap) &&
4883 mlx5e_check_fragmented_striding_rq_cap(mdev))
4884 netdev->vlan_features |= NETIF_F_LRO;
4886 netdev->hw_features = netdev->vlan_features;
4887 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4888 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4889 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4890 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4892 if (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev) ||
4893 mlx5e_any_tunnel_proto_supported(mdev)) {
4894 netdev->hw_enc_features |= NETIF_F_HW_CSUM;
4895 netdev->hw_enc_features |= NETIF_F_TSO;
4896 netdev->hw_enc_features |= NETIF_F_TSO6;
4897 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4900 if (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev)) {
4901 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4902 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4903 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4904 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4905 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4908 if (mlx5e_tunnel_proto_supported(mdev, IPPROTO_GRE)) {
4909 netdev->hw_features |= NETIF_F_GSO_GRE |
4910 NETIF_F_GSO_GRE_CSUM;
4911 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4912 NETIF_F_GSO_GRE_CSUM;
4913 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4914 NETIF_F_GSO_GRE_CSUM;
4917 if (mlx5e_tunnel_proto_supported(mdev, IPPROTO_IPIP)) {
4918 netdev->hw_features |= NETIF_F_GSO_IPXIP4 |
4920 netdev->hw_enc_features |= NETIF_F_GSO_IPXIP4 |
4922 netdev->gso_partial_features |= NETIF_F_GSO_IPXIP4 |
4926 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4927 netdev->gso_partial_features |= NETIF_F_GSO_UDP_L4;
4928 netdev->hw_features |= NETIF_F_GSO_UDP_L4;
4929 netdev->features |= NETIF_F_GSO_UDP_L4;
4931 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4934 netdev->hw_features |= NETIF_F_RXALL;
4936 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4937 netdev->hw_features |= NETIF_F_RXFCS;
4939 netdev->features = netdev->hw_features;
4940 if (!priv->channels.params.lro_en)
4941 netdev->features &= ~NETIF_F_LRO;
4944 netdev->features &= ~NETIF_F_RXALL;
4946 if (!priv->channels.params.scatter_fcs_en)
4947 netdev->features &= ~NETIF_F_RXFCS;
4949 /* prefere CQE compression over rxhash */
4950 if (MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS))
4951 netdev->features &= ~NETIF_F_RXHASH;
4953 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4954 if (FT_CAP(flow_modify_en) &&
4955 FT_CAP(modify_root) &&
4956 FT_CAP(identified_miss_table_mode) &&
4957 FT_CAP(flow_table_modify)) {
4958 #ifdef CONFIG_MLX5_ESWITCH
4959 netdev->hw_features |= NETIF_F_HW_TC;
4961 #ifdef CONFIG_MLX5_EN_ARFS
4962 netdev->hw_features |= NETIF_F_NTUPLE;
4966 netdev->features |= NETIF_F_HIGHDMA;
4967 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4969 netdev->priv_flags |= IFF_UNICAST_FLT;
4971 mlx5e_set_netdev_dev_addr(netdev);
4972 mlx5e_ipsec_build_netdev(priv);
4973 mlx5e_tls_build_netdev(priv);
4976 void mlx5e_create_q_counters(struct mlx5e_priv *priv)
4978 struct mlx5_core_dev *mdev = priv->mdev;
4981 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4983 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4984 priv->q_counter = 0;
4987 err = mlx5_core_alloc_q_counter(mdev, &priv->drop_rq_q_counter);
4989 mlx5_core_warn(mdev, "alloc drop RQ counter failed, %d\n", err);
4990 priv->drop_rq_q_counter = 0;
4994 void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
4996 if (priv->q_counter)
4997 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4999 if (priv->drop_rq_q_counter)
5000 mlx5_core_dealloc_q_counter(priv->mdev, priv->drop_rq_q_counter);
5003 static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
5004 struct net_device *netdev,
5005 const struct mlx5e_profile *profile,
5008 struct mlx5e_priv *priv = netdev_priv(netdev);
5009 struct mlx5e_rss_params *rss = &priv->rss_params;
5012 err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
5016 mlx5e_build_nic_params(mdev, &priv->xsk, rss, &priv->channels.params,
5017 priv->max_nch, netdev->mtu);
5019 mlx5e_timestamp_init(priv);
5021 err = mlx5e_ipsec_init(priv);
5023 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
5024 err = mlx5e_tls_init(priv);
5026 mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
5027 mlx5e_build_nic_netdev(netdev);
5028 mlx5e_build_tc2txq_maps(priv);
5029 mlx5e_health_create_reporters(priv);
5034 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
5036 mlx5e_health_destroy_reporters(priv);
5037 mlx5e_tls_cleanup(priv);
5038 mlx5e_ipsec_cleanup(priv);
5039 mlx5e_netdev_cleanup(priv->netdev, priv);
5042 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
5044 struct mlx5_core_dev *mdev = priv->mdev;
5047 mlx5e_create_q_counters(priv);
5049 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
5051 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
5052 goto err_destroy_q_counters;
5055 err = mlx5e_create_indirect_rqt(priv);
5057 goto err_close_drop_rq;
5059 err = mlx5e_create_direct_rqts(priv, priv->direct_tir);
5061 goto err_destroy_indirect_rqts;
5063 err = mlx5e_create_indirect_tirs(priv, true);
5065 goto err_destroy_direct_rqts;
5067 err = mlx5e_create_direct_tirs(priv, priv->direct_tir);
5069 goto err_destroy_indirect_tirs;
5071 err = mlx5e_create_direct_rqts(priv, priv->xsk_tir);
5073 goto err_destroy_direct_tirs;
5075 err = mlx5e_create_direct_tirs(priv, priv->xsk_tir);
5077 goto err_destroy_xsk_rqts;
5079 err = mlx5e_create_flow_steering(priv);
5081 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
5082 goto err_destroy_xsk_tirs;
5085 err = mlx5e_tc_nic_init(priv);
5087 goto err_destroy_flow_steering;
5091 err_destroy_flow_steering:
5092 mlx5e_destroy_flow_steering(priv);
5093 err_destroy_xsk_tirs:
5094 mlx5e_destroy_direct_tirs(priv, priv->xsk_tir);
5095 err_destroy_xsk_rqts:
5096 mlx5e_destroy_direct_rqts(priv, priv->xsk_tir);
5097 err_destroy_direct_tirs:
5098 mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
5099 err_destroy_indirect_tirs:
5100 mlx5e_destroy_indirect_tirs(priv, true);
5101 err_destroy_direct_rqts:
5102 mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
5103 err_destroy_indirect_rqts:
5104 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
5106 mlx5e_close_drop_rq(&priv->drop_rq);
5107 err_destroy_q_counters:
5108 mlx5e_destroy_q_counters(priv);
5112 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
5114 mlx5e_tc_nic_cleanup(priv);
5115 mlx5e_destroy_flow_steering(priv);
5116 mlx5e_destroy_direct_tirs(priv, priv->xsk_tir);
5117 mlx5e_destroy_direct_rqts(priv, priv->xsk_tir);
5118 mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
5119 mlx5e_destroy_indirect_tirs(priv, true);
5120 mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
5121 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
5122 mlx5e_close_drop_rq(&priv->drop_rq);
5123 mlx5e_destroy_q_counters(priv);
5126 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
5130 err = mlx5e_create_tises(priv);
5132 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
5136 #ifdef CONFIG_MLX5_CORE_EN_DCB
5137 mlx5e_dcbnl_initialize(priv);
5142 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
5144 struct net_device *netdev = priv->netdev;
5145 struct mlx5_core_dev *mdev = priv->mdev;
5147 mlx5e_init_l2_addr(priv);
5149 /* Marking the link as currently not needed by the Driver */
5150 if (!netif_running(netdev))
5151 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
5153 mlx5e_set_netdev_mtu_boundaries(priv);
5154 mlx5e_set_dev_port_mtu(priv);
5156 mlx5_lag_add(mdev, netdev);
5158 mlx5e_enable_async_events(priv);
5159 if (mlx5e_monitor_counter_supported(priv))
5160 mlx5e_monitor_counter_init(priv);
5162 mlx5e_hv_vhca_stats_create(priv);
5163 if (netdev->reg_state != NETREG_REGISTERED)
5165 #ifdef CONFIG_MLX5_CORE_EN_DCB
5166 mlx5e_dcbnl_init_app(priv);
5169 queue_work(priv->wq, &priv->set_rx_mode_work);
5172 if (netif_running(netdev))
5174 netif_device_attach(netdev);
5178 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
5180 struct mlx5_core_dev *mdev = priv->mdev;
5182 #ifdef CONFIG_MLX5_CORE_EN_DCB
5183 if (priv->netdev->reg_state == NETREG_REGISTERED)
5184 mlx5e_dcbnl_delete_app(priv);
5188 if (netif_running(priv->netdev))
5189 mlx5e_close(priv->netdev);
5190 netif_device_detach(priv->netdev);
5193 queue_work(priv->wq, &priv->set_rx_mode_work);
5195 mlx5e_hv_vhca_stats_destroy(priv);
5196 if (mlx5e_monitor_counter_supported(priv))
5197 mlx5e_monitor_counter_cleanup(priv);
5199 mlx5e_disable_async_events(priv);
5200 mlx5_lag_remove(mdev);
5203 int mlx5e_update_nic_rx(struct mlx5e_priv *priv)
5205 return mlx5e_refresh_tirs(priv, false);
5208 static const struct mlx5e_profile mlx5e_nic_profile = {
5209 .init = mlx5e_nic_init,
5210 .cleanup = mlx5e_nic_cleanup,
5211 .init_rx = mlx5e_init_nic_rx,
5212 .cleanup_rx = mlx5e_cleanup_nic_rx,
5213 .init_tx = mlx5e_init_nic_tx,
5214 .cleanup_tx = mlx5e_cleanup_nic_tx,
5215 .enable = mlx5e_nic_enable,
5216 .disable = mlx5e_nic_disable,
5217 .update_rx = mlx5e_update_nic_rx,
5218 .update_stats = mlx5e_update_ndo_stats,
5219 .update_carrier = mlx5e_update_carrier,
5220 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
5221 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
5222 .max_tc = MLX5E_MAX_NUM_TC,
5223 .rq_groups = MLX5E_NUM_RQ_GROUPS(XSK),
5226 /* mlx5e generic netdev management API (move to en_common.c) */
5228 /* mlx5e_netdev_init/cleanup must be called from profile->init/cleanup callbacks */
5229 int mlx5e_netdev_init(struct net_device *netdev,
5230 struct mlx5e_priv *priv,
5231 struct mlx5_core_dev *mdev,
5232 const struct mlx5e_profile *profile,
5237 priv->netdev = netdev;
5238 priv->profile = profile;
5239 priv->ppriv = ppriv;
5240 priv->msglevel = MLX5E_MSG_LEVEL;
5241 priv->max_nch = netdev->num_rx_queues / max_t(u8, profile->rq_groups, 1);
5242 priv->max_opened_tc = 1;
5244 mutex_init(&priv->state_lock);
5245 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
5246 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
5247 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
5248 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
5250 priv->wq = create_singlethread_workqueue("mlx5e");
5255 netif_carrier_off(netdev);
5257 #ifdef CONFIG_MLX5_EN_ARFS
5258 netdev->rx_cpu_rmap = mlx5_eq_table_get_rmap(mdev);
5264 void mlx5e_netdev_cleanup(struct net_device *netdev, struct mlx5e_priv *priv)
5266 destroy_workqueue(priv->wq);
5269 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
5270 const struct mlx5e_profile *profile,
5274 struct net_device *netdev;
5277 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
5278 nch * profile->max_tc,
5279 nch * profile->rq_groups);
5281 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
5285 err = profile->init(mdev, netdev, profile, ppriv);
5287 mlx5_core_err(mdev, "failed to init mlx5e profile %d\n", err);
5288 goto err_free_netdev;
5294 free_netdev(netdev);
5299 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
5301 const struct mlx5e_profile *profile;
5305 profile = priv->profile;
5306 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
5308 /* max number of channels may have changed */
5309 max_nch = mlx5e_get_max_num_channels(priv->mdev);
5310 if (priv->channels.params.num_channels > max_nch) {
5311 mlx5_core_warn(priv->mdev, "MLX5E: Reducing number of channels to %d\n", max_nch);
5312 priv->channels.params.num_channels = max_nch;
5313 mlx5e_build_default_indir_rqt(priv->rss_params.indirection_rqt,
5314 MLX5E_INDIR_RQT_SIZE, max_nch);
5317 err = profile->init_tx(priv);
5321 err = profile->init_rx(priv);
5323 goto err_cleanup_tx;
5325 if (profile->enable)
5326 profile->enable(priv);
5331 profile->cleanup_tx(priv);
5337 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
5339 const struct mlx5e_profile *profile = priv->profile;
5341 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
5343 if (profile->disable)
5344 profile->disable(priv);
5345 flush_workqueue(priv->wq);
5347 profile->cleanup_rx(priv);
5348 profile->cleanup_tx(priv);
5349 cancel_work_sync(&priv->update_stats_work);
5352 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
5354 const struct mlx5e_profile *profile = priv->profile;
5355 struct net_device *netdev = priv->netdev;
5357 if (profile->cleanup)
5358 profile->cleanup(priv);
5359 free_netdev(netdev);
5362 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
5363 * hardware contexts and to connect it to the current netdev.
5365 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
5367 struct mlx5e_priv *priv = vpriv;
5368 struct net_device *netdev = priv->netdev;
5371 if (netif_device_present(netdev))
5374 err = mlx5e_create_mdev_resources(mdev);
5378 err = mlx5e_attach_netdev(priv);
5380 mlx5e_destroy_mdev_resources(mdev);
5387 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
5389 struct mlx5e_priv *priv = vpriv;
5390 struct net_device *netdev = priv->netdev;
5392 #ifdef CONFIG_MLX5_ESWITCH
5393 if (MLX5_ESWITCH_MANAGER(mdev) && vpriv == mdev)
5397 if (!netif_device_present(netdev))
5400 mlx5e_detach_netdev(priv);
5401 mlx5e_destroy_mdev_resources(mdev);
5404 static void *mlx5e_add(struct mlx5_core_dev *mdev)
5406 struct net_device *netdev;
5411 err = mlx5e_check_required_hca_cap(mdev);
5415 #ifdef CONFIG_MLX5_ESWITCH
5416 if (MLX5_ESWITCH_MANAGER(mdev) &&
5417 mlx5_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
5418 mlx5e_rep_register_vport_reps(mdev);
5423 nch = mlx5e_get_max_num_channels(mdev);
5424 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, nch, NULL);
5426 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
5430 priv = netdev_priv(netdev);
5432 err = mlx5e_attach(mdev, priv);
5434 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
5435 goto err_destroy_netdev;
5438 err = register_netdev(netdev);
5440 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
5444 #ifdef CONFIG_MLX5_CORE_EN_DCB
5445 mlx5e_dcbnl_init_app(priv);
5450 mlx5e_detach(mdev, priv);
5452 mlx5e_destroy_netdev(priv);
5456 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
5458 struct mlx5e_priv *priv;
5460 #ifdef CONFIG_MLX5_ESWITCH
5461 if (MLX5_ESWITCH_MANAGER(mdev) && vpriv == mdev) {
5462 mlx5e_rep_unregister_vport_reps(mdev);
5467 #ifdef CONFIG_MLX5_CORE_EN_DCB
5468 mlx5e_dcbnl_delete_app(priv);
5470 unregister_netdev(priv->netdev);
5471 mlx5e_detach(mdev, vpriv);
5472 mlx5e_destroy_netdev(priv);
5475 static struct mlx5_interface mlx5e_interface = {
5477 .remove = mlx5e_remove,
5478 .attach = mlx5e_attach,
5479 .detach = mlx5e_detach,
5480 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
5483 void mlx5e_init(void)
5485 mlx5e_ipsec_build_inverse_table();
5486 mlx5e_build_ptys2ethtool_map();
5487 mlx5_register_interface(&mlx5e_interface);
5490 void mlx5e_cleanup(void)
5492 mlx5_unregister_interface(&mlx5e_interface);