2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <net/tc_act/tc_gact.h>
34 #include <net/pkt_cls.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
37 #include <net/geneve.h>
38 #include <linux/bpf.h>
39 #include <linux/if_bridge.h>
40 #include <net/page_pool.h>
41 #include <net/xdp_sock.h>
47 #include "en_accel/ipsec.h"
48 #include "en_accel/ipsec_rxtx.h"
49 #include "en_accel/en_accel.h"
50 #include "en_accel/tls.h"
51 #include "accel/ipsec.h"
52 #include "accel/tls.h"
53 #include "lib/vxlan.h"
54 #include "lib/clock.h"
58 #include "en/monitor_stats.h"
59 #include "en/reporter.h"
60 #include "en/params.h"
61 #include "en/xsk/umem.h"
62 #include "en/xsk/setup.h"
63 #include "en/xsk/rx.h"
64 #include "en/xsk/tx.h"
67 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
69 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
70 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
71 MLX5_CAP_ETH(mdev, reg_umr_sq);
72 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
73 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
78 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
79 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
85 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
86 struct mlx5e_params *params)
88 params->log_rq_mtu_frames = is_kdump_kernel() ?
89 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
90 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
92 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
93 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
94 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
95 BIT(mlx5e_mpwqe_get_log_rq_size(params, NULL)) :
96 BIT(params->log_rq_mtu_frames),
97 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params, NULL)),
98 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
101 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
102 struct mlx5e_params *params)
104 if (!mlx5e_check_fragmented_striding_rq_cap(mdev))
107 if (MLX5_IPSEC_DEV(mdev))
110 if (params->xdp_prog) {
111 /* XSK params are not considered here. If striding RQ is in use,
112 * and an XSK is being opened, mlx5e_rx_mpwqe_is_linear_skb will
113 * be called with the known XSK params.
115 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL))
122 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
124 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
125 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
126 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
130 void mlx5e_update_carrier(struct mlx5e_priv *priv)
132 struct mlx5_core_dev *mdev = priv->mdev;
135 port_state = mlx5_query_vport_state(mdev,
136 MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT,
139 if (port_state == VPORT_STATE_UP) {
140 netdev_info(priv->netdev, "Link up\n");
141 netif_carrier_on(priv->netdev);
143 netdev_info(priv->netdev, "Link down\n");
144 netif_carrier_off(priv->netdev);
148 static void mlx5e_update_carrier_work(struct work_struct *work)
150 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
151 update_carrier_work);
153 mutex_lock(&priv->state_lock);
154 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
155 if (priv->profile->update_carrier)
156 priv->profile->update_carrier(priv);
157 mutex_unlock(&priv->state_lock);
160 void mlx5e_update_stats(struct mlx5e_priv *priv)
164 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
165 if (mlx5e_stats_grps[i].update_stats)
166 mlx5e_stats_grps[i].update_stats(priv);
169 void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
173 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
174 if (mlx5e_stats_grps[i].update_stats_mask &
175 MLX5E_NDO_UPDATE_STATS)
176 mlx5e_stats_grps[i].update_stats(priv);
179 static void mlx5e_update_stats_work(struct work_struct *work)
181 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
184 mutex_lock(&priv->state_lock);
185 priv->profile->update_stats(priv);
186 mutex_unlock(&priv->state_lock);
189 void mlx5e_queue_update_stats(struct mlx5e_priv *priv)
191 if (!priv->profile->update_stats)
194 if (unlikely(test_bit(MLX5E_STATE_DESTROYING, &priv->state)))
197 queue_work(priv->wq, &priv->update_stats_work);
200 static int async_event(struct notifier_block *nb, unsigned long event, void *data)
202 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
203 struct mlx5_eqe *eqe = data;
205 if (event != MLX5_EVENT_TYPE_PORT_CHANGE)
208 switch (eqe->sub_type) {
209 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
210 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
211 queue_work(priv->wq, &priv->update_carrier_work);
220 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
222 priv->events_nb.notifier_call = async_event;
223 mlx5_notifier_register(priv->mdev, &priv->events_nb);
226 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
228 mlx5_notifier_unregister(priv->mdev, &priv->events_nb);
231 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
232 struct mlx5e_icosq *sq,
233 struct mlx5e_umr_wqe *wqe)
235 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
236 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
237 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
239 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
241 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
242 cseg->imm = rq->mkey_be;
244 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
245 ucseg->xlt_octowords =
246 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
247 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
250 static u32 mlx5e_rqwq_get_size(struct mlx5e_rq *rq)
252 switch (rq->wq_type) {
253 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
254 return mlx5_wq_ll_get_size(&rq->mpwqe.wq);
256 return mlx5_wq_cyc_get_size(&rq->wqe.wq);
260 static u32 mlx5e_rqwq_get_cur_sz(struct mlx5e_rq *rq)
262 switch (rq->wq_type) {
263 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
264 return rq->mpwqe.wq.cur_sz;
266 return rq->wqe.wq.cur_sz;
270 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
271 struct mlx5e_channel *c)
273 int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
275 rq->mpwqe.info = kvzalloc_node(array_size(wq_sz,
276 sizeof(*rq->mpwqe.info)),
277 GFP_KERNEL, cpu_to_node(c->cpu));
281 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
286 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
287 u64 npages, u8 page_shift,
288 struct mlx5_core_mkey *umr_mkey)
290 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
295 in = kvzalloc(inlen, GFP_KERNEL);
299 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
301 MLX5_SET(mkc, mkc, free, 1);
302 MLX5_SET(mkc, mkc, umr_en, 1);
303 MLX5_SET(mkc, mkc, lw, 1);
304 MLX5_SET(mkc, mkc, lr, 1);
305 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
307 MLX5_SET(mkc, mkc, qpn, 0xffffff);
308 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
309 MLX5_SET64(mkc, mkc, len, npages << page_shift);
310 MLX5_SET(mkc, mkc, translations_octword_size,
311 MLX5_MTT_OCTW(npages));
312 MLX5_SET(mkc, mkc, log_page_size, page_shift);
314 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
320 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
322 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->mpwqe.wq));
324 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
327 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
329 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
332 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq)
334 struct mlx5e_wqe_frag_info next_frag, *prev;
337 next_frag.di = &rq->wqe.di[0];
338 next_frag.offset = 0;
341 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
342 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
343 struct mlx5e_wqe_frag_info *frag =
344 &rq->wqe.frags[i << rq->wqe.info.log_num_frags];
347 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) {
348 if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) {
350 next_frag.offset = 0;
352 prev->last_in_page = true;
357 next_frag.offset += frag_info[f].frag_stride;
363 prev->last_in_page = true;
366 static int mlx5e_init_di_list(struct mlx5e_rq *rq,
369 int len = wq_sz << rq->wqe.info.log_num_frags;
371 rq->wqe.di = kvzalloc_node(array_size(len, sizeof(*rq->wqe.di)),
372 GFP_KERNEL, cpu_to_node(cpu));
376 mlx5e_init_frags_partition(rq);
381 static void mlx5e_free_di_list(struct mlx5e_rq *rq)
386 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
387 struct mlx5e_params *params,
388 struct mlx5e_xsk_param *xsk,
389 struct xdp_umem *umem,
390 struct mlx5e_rq_param *rqp,
393 struct page_pool_params pp_params = { 0 };
394 struct mlx5_core_dev *mdev = c->mdev;
395 void *rqc = rqp->rqc;
396 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
397 u32 num_xsk_frames = 0;
404 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
406 rq->wq_type = params->rq_wq_type;
408 rq->netdev = c->netdev;
409 rq->tstamp = c->tstamp;
410 rq->clock = &mdev->clock;
414 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
415 rq->xdpsq = &c->rq_xdpsq;
419 rq->stats = &c->priv->channel_stats[c->ix].xskrq;
421 rq->stats = &c->priv->channel_stats[c->ix].rq;
423 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
424 if (IS_ERR(rq->xdp_prog)) {
425 err = PTR_ERR(rq->xdp_prog);
427 goto err_rq_wq_destroy;
432 rq_xdp_ix += params->num_channels * MLX5E_RQ_GROUP_XSK;
433 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq_xdp_ix);
435 goto err_rq_wq_destroy;
437 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
438 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params, xsk);
439 rq->buff.umem_headroom = xsk ? xsk->headroom : 0;
440 pool_size = 1 << params->log_rq_mtu_frames;
442 switch (rq->wq_type) {
443 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
444 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->mpwqe.wq,
449 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR];
451 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
454 num_xsk_frames = wq_sz <<
455 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
457 pool_size = MLX5_MPWRQ_PAGES_PER_WQE <<
458 mlx5e_mpwqe_get_log_rq_size(params, xsk);
460 rq->post_wqes = mlx5e_post_rx_mpwqes;
461 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
463 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
464 #ifdef CONFIG_MLX5_EN_IPSEC
465 if (MLX5_IPSEC_DEV(mdev)) {
467 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
468 goto err_rq_wq_destroy;
471 if (!rq->handle_rx_cqe) {
473 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
474 goto err_rq_wq_destroy;
477 rq->mpwqe.skb_from_cqe_mpwrq = xsk ?
478 mlx5e_xsk_skb_from_cqe_mpwrq_linear :
479 mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ?
480 mlx5e_skb_from_cqe_mpwrq_linear :
481 mlx5e_skb_from_cqe_mpwrq_nonlinear;
483 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
484 rq->mpwqe.num_strides =
485 BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk));
487 err = mlx5e_create_rq_umr_mkey(mdev, rq);
489 goto err_rq_wq_destroy;
490 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
492 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
496 default: /* MLX5_WQ_TYPE_CYCLIC */
497 err = mlx5_wq_cyc_create(mdev, &rqp->wq, rqc_wq, &rq->wqe.wq,
502 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR];
504 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
507 num_xsk_frames = wq_sz << rq->wqe.info.log_num_frags;
509 rq->wqe.info = rqp->frags_info;
511 kvzalloc_node(array_size(sizeof(*rq->wqe.frags),
512 (wq_sz << rq->wqe.info.log_num_frags)),
513 GFP_KERNEL, cpu_to_node(c->cpu));
514 if (!rq->wqe.frags) {
519 err = mlx5e_init_di_list(rq, wq_sz, c->cpu);
523 rq->post_wqes = mlx5e_post_rx_wqes;
524 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
526 #ifdef CONFIG_MLX5_EN_IPSEC
528 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
531 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
532 if (!rq->handle_rx_cqe) {
534 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
538 rq->wqe.skb_from_cqe = xsk ?
539 mlx5e_xsk_skb_from_cqe_linear :
540 mlx5e_rx_is_linear_skb(params, NULL) ?
541 mlx5e_skb_from_cqe_linear :
542 mlx5e_skb_from_cqe_nonlinear;
543 rq->mkey_be = c->mkey_be;
547 err = mlx5e_xsk_resize_reuseq(umem, num_xsk_frames);
549 mlx5_core_err(mdev, "Unable to allocate the Reuse Ring for %u frames\n",
554 rq->zca.free = mlx5e_xsk_zca_free;
555 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
559 /* Create a page_pool and register it with rxq */
561 pp_params.flags = 0; /* No-internal DMA mapping in page_pool */
562 pp_params.pool_size = pool_size;
563 pp_params.nid = cpu_to_node(c->cpu);
564 pp_params.dev = c->pdev;
565 pp_params.dma_dir = rq->buff.map_dir;
567 /* page_pool can be used even when there is no rq->xdp_prog,
568 * given page_pool does not handle DMA mapping there is no
569 * required state to clear. And page_pool gracefully handle
572 rq->page_pool = page_pool_create(&pp_params);
573 if (IS_ERR(rq->page_pool)) {
574 err = PTR_ERR(rq->page_pool);
575 rq->page_pool = NULL;
578 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
579 MEM_TYPE_PAGE_POOL, rq->page_pool);
584 for (i = 0; i < wq_sz; i++) {
585 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
586 struct mlx5e_rx_wqe_ll *wqe =
587 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i);
589 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
590 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
592 wqe->data[0].addr = cpu_to_be64(dma_offset + rq->buff.headroom);
593 wqe->data[0].byte_count = cpu_to_be32(byte_count);
594 wqe->data[0].lkey = rq->mkey_be;
596 struct mlx5e_rx_wqe_cyc *wqe =
597 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i);
600 for (f = 0; f < rq->wqe.info.num_frags; f++) {
601 u32 frag_size = rq->wqe.info.arr[f].frag_size |
602 MLX5_HW_START_PADDING;
604 wqe->data[f].byte_count = cpu_to_be32(frag_size);
605 wqe->data[f].lkey = rq->mkey_be;
607 /* check if num_frags is not a pow of two */
608 if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) {
609 wqe->data[f].byte_count = 0;
610 wqe->data[f].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
611 wqe->data[f].addr = 0;
616 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
618 switch (params->rx_cq_moderation.cq_period_mode) {
619 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
620 rq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
622 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
624 rq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
627 rq->page_cache.head = 0;
628 rq->page_cache.tail = 0;
633 switch (rq->wq_type) {
634 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
635 kvfree(rq->mpwqe.info);
636 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
638 default: /* MLX5_WQ_TYPE_CYCLIC */
639 kvfree(rq->wqe.frags);
640 mlx5e_free_di_list(rq);
645 bpf_prog_put(rq->xdp_prog);
646 xdp_rxq_info_unreg(&rq->xdp_rxq);
647 page_pool_destroy(rq->page_pool);
648 mlx5_wq_destroy(&rq->wq_ctrl);
653 static void mlx5e_free_rq(struct mlx5e_rq *rq)
658 bpf_prog_put(rq->xdp_prog);
660 switch (rq->wq_type) {
661 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
662 kvfree(rq->mpwqe.info);
663 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
665 default: /* MLX5_WQ_TYPE_CYCLIC */
666 kvfree(rq->wqe.frags);
667 mlx5e_free_di_list(rq);
670 for (i = rq->page_cache.head; i != rq->page_cache.tail;
671 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
672 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
674 /* With AF_XDP, page_cache is not used, so this loop is not
675 * entered, and it's safe to call mlx5e_page_release_dynamic
678 mlx5e_page_release_dynamic(rq, dma_info, false);
681 xdp_rxq_info_unreg(&rq->xdp_rxq);
682 page_pool_destroy(rq->page_pool);
683 mlx5_wq_destroy(&rq->wq_ctrl);
686 static int mlx5e_create_rq(struct mlx5e_rq *rq,
687 struct mlx5e_rq_param *param)
689 struct mlx5_core_dev *mdev = rq->mdev;
697 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
698 sizeof(u64) * rq->wq_ctrl.buf.npages;
699 in = kvzalloc(inlen, GFP_KERNEL);
703 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
704 wq = MLX5_ADDR_OF(rqc, rqc, wq);
706 memcpy(rqc, param->rqc, sizeof(param->rqc));
708 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
709 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
710 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
711 MLX5_ADAPTER_PAGE_SHIFT);
712 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
714 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
715 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
717 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
724 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
727 struct mlx5_core_dev *mdev = rq->mdev;
734 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
735 in = kvzalloc(inlen, GFP_KERNEL);
739 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
741 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
742 MLX5_SET(rqc, rqc, state, next_state);
744 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
751 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
753 struct mlx5e_channel *c = rq->channel;
754 struct mlx5e_priv *priv = c->priv;
755 struct mlx5_core_dev *mdev = priv->mdev;
762 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
763 in = kvzalloc(inlen, GFP_KERNEL);
767 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
769 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
770 MLX5_SET64(modify_rq_in, in, modify_bitmask,
771 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
772 MLX5_SET(rqc, rqc, scatter_fcs, enable);
773 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
775 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
782 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
784 struct mlx5e_channel *c = rq->channel;
785 struct mlx5_core_dev *mdev = c->mdev;
791 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
792 in = kvzalloc(inlen, GFP_KERNEL);
796 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
798 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
799 MLX5_SET64(modify_rq_in, in, modify_bitmask,
800 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
801 MLX5_SET(rqc, rqc, vsd, vsd);
802 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
804 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
811 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
813 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
816 int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
818 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
819 struct mlx5e_channel *c = rq->channel;
821 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq));
824 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes)
828 } while (time_before(jiffies, exp_time));
830 netdev_warn(c->netdev, "Failed to get min RX wqes on Channel[%d] RQN[0x%x] wq cur_sz(%d) min_rx_wqes(%d)\n",
831 c->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes);
836 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
841 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
842 struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
846 /* Outstanding UMR WQEs (in progress) start at wq->head */
847 for (i = 0; i < rq->mpwqe.umr_in_progress; i++) {
848 rq->dealloc_wqe(rq, head);
849 head = mlx5_wq_ll_get_wqe_next_ix(wq, head);
852 while (!mlx5_wq_ll_is_empty(wq)) {
853 struct mlx5e_rx_wqe_ll *wqe;
855 wqe_ix_be = *wq->tail_next;
856 wqe_ix = be16_to_cpu(wqe_ix_be);
857 wqe = mlx5_wq_ll_get_wqe(wq, wqe_ix);
858 rq->dealloc_wqe(rq, wqe_ix);
859 mlx5_wq_ll_pop(wq, wqe_ix_be,
860 &wqe->next.next_wqe_index);
863 struct mlx5_wq_cyc *wq = &rq->wqe.wq;
865 while (!mlx5_wq_cyc_is_empty(wq)) {
866 wqe_ix = mlx5_wq_cyc_get_tail(wq);
867 rq->dealloc_wqe(rq, wqe_ix);
874 int mlx5e_open_rq(struct mlx5e_channel *c, struct mlx5e_params *params,
875 struct mlx5e_rq_param *param, struct mlx5e_xsk_param *xsk,
876 struct xdp_umem *umem, struct mlx5e_rq *rq)
880 err = mlx5e_alloc_rq(c, params, xsk, umem, param, rq);
884 err = mlx5e_create_rq(rq, param);
888 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
892 if (MLX5_CAP_ETH(c->mdev, cqe_checksum_full))
893 __set_bit(MLX5E_RQ_STATE_CSUM_FULL, &c->rq.state);
895 if (params->rx_dim_enabled)
896 __set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
898 /* We disable csum_complete when XDP is enabled since
899 * XDP programs might manipulate packets which will render
900 * skb->checksum incorrect.
902 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE) || c->xdp)
903 __set_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &c->rq.state);
908 mlx5e_destroy_rq(rq);
915 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
917 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
918 mlx5e_trigger_irq(&rq->channel->icosq);
921 void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
923 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
924 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
927 void mlx5e_close_rq(struct mlx5e_rq *rq)
929 cancel_work_sync(&rq->dim.work);
930 mlx5e_destroy_rq(rq);
931 mlx5e_free_rx_descs(rq);
935 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
937 kvfree(sq->db.xdpi_fifo.xi);
938 kvfree(sq->db.wqe_info);
941 static int mlx5e_alloc_xdpsq_fifo(struct mlx5e_xdpsq *sq, int numa)
943 struct mlx5e_xdp_info_fifo *xdpi_fifo = &sq->db.xdpi_fifo;
944 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
945 int dsegs_per_wq = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
947 xdpi_fifo->xi = kvzalloc_node(sizeof(*xdpi_fifo->xi) * dsegs_per_wq,
952 xdpi_fifo->pc = &sq->xdpi_fifo_pc;
953 xdpi_fifo->cc = &sq->xdpi_fifo_cc;
954 xdpi_fifo->mask = dsegs_per_wq - 1;
959 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
961 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
964 sq->db.wqe_info = kvzalloc_node(sizeof(*sq->db.wqe_info) * wq_sz,
966 if (!sq->db.wqe_info)
969 err = mlx5e_alloc_xdpsq_fifo(sq, numa);
971 mlx5e_free_xdpsq_db(sq);
978 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
979 struct mlx5e_params *params,
980 struct xdp_umem *umem,
981 struct mlx5e_sq_param *param,
982 struct mlx5e_xdpsq *sq,
985 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
986 struct mlx5_core_dev *mdev = c->mdev;
987 struct mlx5_wq_cyc *wq = &sq->wq;
991 sq->mkey_be = c->mkey_be;
993 sq->uar_map = mdev->mlx5e_res.bfreg.map;
994 sq->min_inline_mode = params->tx_min_inline_mode;
995 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
998 sq->stats = sq->umem ?
999 &c->priv->channel_stats[c->ix].xsksq :
1001 &c->priv->channel_stats[c->ix].xdpsq :
1002 &c->priv->channel_stats[c->ix].rq_xdpsq;
1004 param->wq.db_numa_node = cpu_to_node(c->cpu);
1005 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1008 wq->db = &wq->db[MLX5_SND_DBR];
1010 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
1012 goto err_sq_wq_destroy;
1017 mlx5_wq_destroy(&sq->wq_ctrl);
1022 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
1024 mlx5e_free_xdpsq_db(sq);
1025 mlx5_wq_destroy(&sq->wq_ctrl);
1028 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
1030 kvfree(sq->db.ico_wqe);
1033 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
1035 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1037 sq->db.ico_wqe = kvzalloc_node(array_size(wq_sz,
1038 sizeof(*sq->db.ico_wqe)),
1040 if (!sq->db.ico_wqe)
1046 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
1047 struct mlx5e_sq_param *param,
1048 struct mlx5e_icosq *sq)
1050 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1051 struct mlx5_core_dev *mdev = c->mdev;
1052 struct mlx5_wq_cyc *wq = &sq->wq;
1056 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1058 param->wq.db_numa_node = cpu_to_node(c->cpu);
1059 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1062 wq->db = &wq->db[MLX5_SND_DBR];
1064 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
1066 goto err_sq_wq_destroy;
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->stats = &c->priv->channel_stats[c->ix].sq[tc];
1132 sq->stop_room = MLX5E_SQ_STOP_ROOM;
1133 INIT_WORK(&sq->recover_work, mlx5e_tx_err_cqe_work);
1134 if (!MLX5_CAP_ETH(mdev, wqe_vlan_insert))
1135 set_bit(MLX5E_SQ_STATE_VLAN_NEED_L2_INLINE, &sq->state);
1136 if (MLX5_IPSEC_DEV(c->priv->mdev))
1137 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1138 if (mlx5_accel_is_tls_device(c->priv->mdev)) {
1139 set_bit(MLX5E_SQ_STATE_TLS, &sq->state);
1140 sq->stop_room += MLX5E_SQ_TLS_ROOM;
1143 param->wq.db_numa_node = cpu_to_node(c->cpu);
1144 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1147 wq->db = &wq->db[MLX5_SND_DBR];
1149 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1151 goto err_sq_wq_destroy;
1153 INIT_WORK(&sq->dim.work, mlx5e_tx_dim_work);
1154 sq->dim.mode = params->tx_cq_moderation.cq_period_mode;
1159 mlx5_wq_destroy(&sq->wq_ctrl);
1164 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1166 mlx5e_free_txqsq_db(sq);
1167 mlx5_wq_destroy(&sq->wq_ctrl);
1170 struct mlx5e_create_sq_param {
1171 struct mlx5_wq_ctrl *wq_ctrl;
1178 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1179 struct mlx5e_sq_param *param,
1180 struct mlx5e_create_sq_param *csp,
1189 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1190 sizeof(u64) * csp->wq_ctrl->buf.npages;
1191 in = kvzalloc(inlen, GFP_KERNEL);
1195 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1196 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1198 memcpy(sqc, param->sqc, sizeof(param->sqc));
1199 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1200 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1201 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1203 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1204 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1206 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1207 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1209 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1210 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1211 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1212 MLX5_ADAPTER_PAGE_SHIFT);
1213 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1215 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1216 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1218 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1225 int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1226 struct mlx5e_modify_sq_param *p)
1233 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1234 in = kvzalloc(inlen, GFP_KERNEL);
1238 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1240 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1241 MLX5_SET(sqc, sqc, state, p->next_state);
1242 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1243 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1244 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1247 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1254 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1256 mlx5_core_destroy_sq(mdev, sqn);
1259 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1260 struct mlx5e_sq_param *param,
1261 struct mlx5e_create_sq_param *csp,
1264 struct mlx5e_modify_sq_param msp = {0};
1267 err = mlx5e_create_sq(mdev, param, csp, sqn);
1271 msp.curr_state = MLX5_SQC_STATE_RST;
1272 msp.next_state = MLX5_SQC_STATE_RDY;
1273 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1275 mlx5e_destroy_sq(mdev, *sqn);
1280 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1281 struct mlx5e_txqsq *sq, u32 rate);
1283 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1286 struct mlx5e_params *params,
1287 struct mlx5e_sq_param *param,
1288 struct mlx5e_txqsq *sq,
1291 struct mlx5e_create_sq_param csp = {};
1295 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1301 csp.cqn = sq->cq.mcq.cqn;
1302 csp.wq_ctrl = &sq->wq_ctrl;
1303 csp.min_inline_mode = sq->min_inline_mode;
1304 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1306 goto err_free_txqsq;
1308 tx_rate = c->priv->tx_rates[sq->txq_ix];
1310 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1312 if (params->tx_dim_enabled)
1313 sq->state |= BIT(MLX5E_SQ_STATE_AM);
1318 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1319 mlx5e_free_txqsq(sq);
1324 void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1326 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1327 clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1328 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1329 netdev_tx_reset_queue(sq->txq);
1330 netif_tx_start_queue(sq->txq);
1333 void mlx5e_tx_disable_queue(struct netdev_queue *txq)
1335 __netif_tx_lock_bh(txq);
1336 netif_tx_stop_queue(txq);
1337 __netif_tx_unlock_bh(txq);
1340 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1342 struct mlx5e_channel *c = sq->channel;
1343 struct mlx5_wq_cyc *wq = &sq->wq;
1345 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1346 /* prevent netif_tx_wake_queue */
1347 napi_synchronize(&c->napi);
1349 mlx5e_tx_disable_queue(sq->txq);
1351 /* last doorbell out, godspeed .. */
1352 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1353 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1354 struct mlx5e_tx_wqe *nop;
1356 sq->db.wqe_info[pi].skb = NULL;
1357 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1358 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1362 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1364 struct mlx5e_channel *c = sq->channel;
1365 struct mlx5_core_dev *mdev = c->mdev;
1366 struct mlx5_rate_limit rl = {0};
1368 cancel_work_sync(&sq->dim.work);
1369 cancel_work_sync(&sq->recover_work);
1370 mlx5e_destroy_sq(mdev, sq->sqn);
1371 if (sq->rate_limit) {
1372 rl.rate = sq->rate_limit;
1373 mlx5_rl_remove_rate(mdev, &rl);
1375 mlx5e_free_txqsq_descs(sq);
1376 mlx5e_free_txqsq(sq);
1379 static void mlx5e_tx_err_cqe_work(struct work_struct *recover_work)
1381 struct mlx5e_txqsq *sq = container_of(recover_work, struct mlx5e_txqsq,
1384 mlx5e_tx_reporter_err_cqe(sq);
1387 int mlx5e_open_icosq(struct mlx5e_channel *c, struct mlx5e_params *params,
1388 struct mlx5e_sq_param *param, struct mlx5e_icosq *sq)
1390 struct mlx5e_create_sq_param csp = {};
1393 err = mlx5e_alloc_icosq(c, param, sq);
1397 csp.cqn = sq->cq.mcq.cqn;
1398 csp.wq_ctrl = &sq->wq_ctrl;
1399 csp.min_inline_mode = params->tx_min_inline_mode;
1400 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1401 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1403 goto err_free_icosq;
1408 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1409 mlx5e_free_icosq(sq);
1414 void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1416 struct mlx5e_channel *c = sq->channel;
1418 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1419 napi_synchronize(&c->napi);
1421 mlx5e_destroy_sq(c->mdev, sq->sqn);
1422 mlx5e_free_icosq(sq);
1425 int mlx5e_open_xdpsq(struct mlx5e_channel *c, struct mlx5e_params *params,
1426 struct mlx5e_sq_param *param, struct xdp_umem *umem,
1427 struct mlx5e_xdpsq *sq, bool is_redirect)
1429 struct mlx5e_create_sq_param csp = {};
1432 err = mlx5e_alloc_xdpsq(c, params, umem, param, sq, is_redirect);
1437 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1438 csp.cqn = sq->cq.mcq.cqn;
1439 csp.wq_ctrl = &sq->wq_ctrl;
1440 csp.min_inline_mode = sq->min_inline_mode;
1441 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1442 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1444 goto err_free_xdpsq;
1446 mlx5e_set_xmit_fp(sq, param->is_mpw);
1448 if (!param->is_mpw) {
1449 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1450 unsigned int inline_hdr_sz = 0;
1453 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1454 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1458 /* Pre initialize fixed WQE fields */
1459 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1460 struct mlx5e_xdp_wqe_info *wi = &sq->db.wqe_info[i];
1461 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1462 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1463 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1464 struct mlx5_wqe_data_seg *dseg;
1466 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1467 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1469 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1470 dseg->lkey = sq->mkey_be;
1480 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1481 mlx5e_free_xdpsq(sq);
1486 void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1488 struct mlx5e_channel *c = sq->channel;
1490 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1491 napi_synchronize(&c->napi);
1493 mlx5e_destroy_sq(c->mdev, sq->sqn);
1494 mlx5e_free_xdpsq_descs(sq);
1495 mlx5e_free_xdpsq(sq);
1498 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1499 struct mlx5e_cq_param *param,
1500 struct mlx5e_cq *cq)
1502 struct mlx5_core_cq *mcq = &cq->mcq;
1508 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1512 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1518 mcq->set_ci_db = cq->wq_ctrl.db.db;
1519 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1520 *mcq->set_ci_db = 0;
1522 mcq->vector = param->eq_ix;
1523 mcq->comp = mlx5e_completion_event;
1524 mcq->event = mlx5e_cq_error_event;
1527 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1528 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1538 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1539 struct mlx5e_cq_param *param,
1540 struct mlx5e_cq *cq)
1542 struct mlx5_core_dev *mdev = c->priv->mdev;
1545 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1546 param->wq.db_numa_node = cpu_to_node(c->cpu);
1547 param->eq_ix = c->ix;
1549 err = mlx5e_alloc_cq_common(mdev, param, cq);
1551 cq->napi = &c->napi;
1557 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1559 mlx5_wq_destroy(&cq->wq_ctrl);
1562 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1564 u32 out[MLX5_ST_SZ_DW(create_cq_out)];
1565 struct mlx5_core_dev *mdev = cq->mdev;
1566 struct mlx5_core_cq *mcq = &cq->mcq;
1571 unsigned int irqn_not_used;
1575 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1579 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1580 sizeof(u64) * cq->wq_ctrl.buf.npages;
1581 in = kvzalloc(inlen, GFP_KERNEL);
1585 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1587 memcpy(cqc, param->cqc, sizeof(param->cqc));
1589 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
1590 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1592 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1593 MLX5_SET(cqc, cqc, c_eqn, eqn);
1594 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1595 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1596 MLX5_ADAPTER_PAGE_SHIFT);
1597 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1599 err = mlx5_core_create_cq(mdev, mcq, in, inlen, out, sizeof(out));
1611 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1613 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1616 int mlx5e_open_cq(struct mlx5e_channel *c, struct dim_cq_moder moder,
1617 struct mlx5e_cq_param *param, struct mlx5e_cq *cq)
1619 struct mlx5_core_dev *mdev = c->mdev;
1622 err = mlx5e_alloc_cq(c, param, cq);
1626 err = mlx5e_create_cq(cq, param);
1630 if (MLX5_CAP_GEN(mdev, cq_moderation))
1631 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1640 void mlx5e_close_cq(struct mlx5e_cq *cq)
1642 mlx5e_destroy_cq(cq);
1646 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1647 struct mlx5e_params *params,
1648 struct mlx5e_channel_param *cparam)
1653 for (tc = 0; tc < c->num_tc; tc++) {
1654 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1655 &cparam->tx_cq, &c->sq[tc].cq);
1657 goto err_close_tx_cqs;
1663 for (tc--; tc >= 0; tc--)
1664 mlx5e_close_cq(&c->sq[tc].cq);
1669 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1673 for (tc = 0; tc < c->num_tc; tc++)
1674 mlx5e_close_cq(&c->sq[tc].cq);
1677 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1678 struct mlx5e_params *params,
1679 struct mlx5e_channel_param *cparam)
1681 struct mlx5e_priv *priv = c->priv;
1682 int err, tc, max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
1684 for (tc = 0; tc < params->num_tc; tc++) {
1685 int txq_ix = c->ix + tc * max_nch;
1687 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1688 params, &cparam->sq, &c->sq[tc], tc);
1696 for (tc--; tc >= 0; tc--)
1697 mlx5e_close_txqsq(&c->sq[tc]);
1702 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1706 for (tc = 0; tc < c->num_tc; tc++)
1707 mlx5e_close_txqsq(&c->sq[tc]);
1710 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1711 struct mlx5e_txqsq *sq, u32 rate)
1713 struct mlx5e_priv *priv = netdev_priv(dev);
1714 struct mlx5_core_dev *mdev = priv->mdev;
1715 struct mlx5e_modify_sq_param msp = {0};
1716 struct mlx5_rate_limit rl = {0};
1720 if (rate == sq->rate_limit)
1724 if (sq->rate_limit) {
1725 rl.rate = sq->rate_limit;
1726 /* remove current rl index to free space to next ones */
1727 mlx5_rl_remove_rate(mdev, &rl);
1734 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1736 netdev_err(dev, "Failed configuring rate %u: %d\n",
1742 msp.curr_state = MLX5_SQC_STATE_RDY;
1743 msp.next_state = MLX5_SQC_STATE_RDY;
1744 msp.rl_index = rl_index;
1745 msp.rl_update = true;
1746 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1748 netdev_err(dev, "Failed configuring rate %u: %d\n",
1750 /* remove the rate from the table */
1752 mlx5_rl_remove_rate(mdev, &rl);
1756 sq->rate_limit = rate;
1760 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1762 struct mlx5e_priv *priv = netdev_priv(dev);
1763 struct mlx5_core_dev *mdev = priv->mdev;
1764 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1767 if (!mlx5_rl_is_supported(mdev)) {
1768 netdev_err(dev, "Rate limiting is not supported on this device\n");
1772 /* rate is given in Mb/sec, HW config is in Kb/sec */
1775 /* Check whether rate in valid range, 0 is always valid */
1776 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1777 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1781 mutex_lock(&priv->state_lock);
1782 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1783 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1785 priv->tx_rates[index] = rate;
1786 mutex_unlock(&priv->state_lock);
1791 static int mlx5e_alloc_xps_cpumask(struct mlx5e_channel *c,
1792 struct mlx5e_params *params)
1794 int num_comp_vectors = mlx5_comp_vectors_count(c->mdev);
1797 if (!zalloc_cpumask_var(&c->xps_cpumask, GFP_KERNEL))
1800 for (irq = c->ix; irq < num_comp_vectors; irq += params->num_channels) {
1801 int cpu = cpumask_first(mlx5_comp_irq_get_affinity_mask(c->mdev, irq));
1803 cpumask_set_cpu(cpu, c->xps_cpumask);
1809 static void mlx5e_free_xps_cpumask(struct mlx5e_channel *c)
1811 free_cpumask_var(c->xps_cpumask);
1814 static int mlx5e_open_queues(struct mlx5e_channel *c,
1815 struct mlx5e_params *params,
1816 struct mlx5e_channel_param *cparam)
1818 struct dim_cq_moder icocq_moder = {0, 0};
1821 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1825 err = mlx5e_open_tx_cqs(c, params, cparam);
1827 goto err_close_icosq_cq;
1829 err = mlx5e_open_cq(c, params->tx_cq_moderation, &cparam->tx_cq, &c->xdpsq.cq);
1831 goto err_close_tx_cqs;
1833 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1835 goto err_close_xdp_tx_cqs;
1837 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1838 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1839 &cparam->tx_cq, &c->rq_xdpsq.cq) : 0;
1841 goto err_close_rx_cq;
1843 napi_enable(&c->napi);
1845 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1847 goto err_disable_napi;
1849 err = mlx5e_open_sqs(c, params, cparam);
1851 goto err_close_icosq;
1854 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL,
1855 &c->rq_xdpsq, false);
1860 err = mlx5e_open_rq(c, params, &cparam->rq, NULL, NULL, &c->rq);
1862 goto err_close_xdp_sq;
1864 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL, &c->xdpsq, true);
1871 mlx5e_close_rq(&c->rq);
1875 mlx5e_close_xdpsq(&c->rq_xdpsq);
1881 mlx5e_close_icosq(&c->icosq);
1884 napi_disable(&c->napi);
1887 mlx5e_close_cq(&c->rq_xdpsq.cq);
1890 mlx5e_close_cq(&c->rq.cq);
1892 err_close_xdp_tx_cqs:
1893 mlx5e_close_cq(&c->xdpsq.cq);
1896 mlx5e_close_tx_cqs(c);
1899 mlx5e_close_cq(&c->icosq.cq);
1904 static void mlx5e_close_queues(struct mlx5e_channel *c)
1906 mlx5e_close_xdpsq(&c->xdpsq);
1907 mlx5e_close_rq(&c->rq);
1909 mlx5e_close_xdpsq(&c->rq_xdpsq);
1911 mlx5e_close_icosq(&c->icosq);
1912 napi_disable(&c->napi);
1914 mlx5e_close_cq(&c->rq_xdpsq.cq);
1915 mlx5e_close_cq(&c->rq.cq);
1916 mlx5e_close_cq(&c->xdpsq.cq);
1917 mlx5e_close_tx_cqs(c);
1918 mlx5e_close_cq(&c->icosq.cq);
1921 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1922 struct mlx5e_params *params,
1923 struct mlx5e_channel_param *cparam,
1924 struct xdp_umem *umem,
1925 struct mlx5e_channel **cp)
1927 int cpu = cpumask_first(mlx5_comp_irq_get_affinity_mask(priv->mdev, ix));
1928 struct net_device *netdev = priv->netdev;
1929 struct mlx5e_xsk_param xsk;
1930 struct mlx5e_channel *c;
1935 err = mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1939 c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1944 c->mdev = priv->mdev;
1945 c->tstamp = &priv->tstamp;
1948 c->pdev = priv->mdev->device;
1949 c->netdev = priv->netdev;
1950 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1951 c->num_tc = params->num_tc;
1952 c->xdp = !!params->xdp_prog;
1953 c->stats = &priv->channel_stats[ix].ch;
1954 c->irq_desc = irq_to_desc(irq);
1956 err = mlx5e_alloc_xps_cpumask(c, params);
1958 goto err_free_channel;
1960 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1962 err = mlx5e_open_queues(c, params, cparam);
1967 mlx5e_build_xsk_param(umem, &xsk);
1968 err = mlx5e_open_xsk(priv, params, &xsk, umem, c);
1970 goto err_close_queues;
1978 mlx5e_close_queues(c);
1981 netif_napi_del(&c->napi);
1982 mlx5e_free_xps_cpumask(c);
1990 static void mlx5e_activate_channel(struct mlx5e_channel *c)
1994 for (tc = 0; tc < c->num_tc; tc++)
1995 mlx5e_activate_txqsq(&c->sq[tc]);
1996 mlx5e_activate_rq(&c->rq);
1997 netif_set_xps_queue(c->netdev, c->xps_cpumask, c->ix);
1999 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2000 mlx5e_activate_xsk(c);
2003 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
2007 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2008 mlx5e_deactivate_xsk(c);
2010 mlx5e_deactivate_rq(&c->rq);
2011 for (tc = 0; tc < c->num_tc; tc++)
2012 mlx5e_deactivate_txqsq(&c->sq[tc]);
2015 static void mlx5e_close_channel(struct mlx5e_channel *c)
2017 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
2019 mlx5e_close_queues(c);
2020 netif_napi_del(&c->napi);
2021 mlx5e_free_xps_cpumask(c);
2026 #define DEFAULT_FRAG_SIZE (2048)
2028 static void mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
2029 struct mlx5e_params *params,
2030 struct mlx5e_xsk_param *xsk,
2031 struct mlx5e_rq_frags_info *info)
2033 u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
2034 int frag_size_max = DEFAULT_FRAG_SIZE;
2038 #ifdef CONFIG_MLX5_EN_IPSEC
2039 if (MLX5_IPSEC_DEV(mdev))
2040 byte_count += MLX5E_METADATA_ETHER_LEN;
2043 if (mlx5e_rx_is_linear_skb(params, xsk)) {
2046 frag_stride = mlx5e_rx_get_linear_frag_sz(params, xsk);
2047 frag_stride = roundup_pow_of_two(frag_stride);
2049 info->arr[0].frag_size = byte_count;
2050 info->arr[0].frag_stride = frag_stride;
2051 info->num_frags = 1;
2052 info->wqe_bulk = PAGE_SIZE / frag_stride;
2056 if (byte_count > PAGE_SIZE +
2057 (MLX5E_MAX_RX_FRAGS - 1) * frag_size_max)
2058 frag_size_max = PAGE_SIZE;
2061 while (buf_size < byte_count) {
2062 int frag_size = byte_count - buf_size;
2064 if (i < MLX5E_MAX_RX_FRAGS - 1)
2065 frag_size = min(frag_size, frag_size_max);
2067 info->arr[i].frag_size = frag_size;
2068 info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
2070 buf_size += frag_size;
2073 info->num_frags = i;
2074 /* number of different wqes sharing a page */
2075 info->wqe_bulk = 1 + (info->num_frags % 2);
2078 info->wqe_bulk = max_t(u8, info->wqe_bulk, 8);
2079 info->log_num_frags = order_base_2(info->num_frags);
2082 static inline u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
2084 int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
2087 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2088 sz += sizeof(struct mlx5e_rx_wqe_ll);
2090 default: /* MLX5_WQ_TYPE_CYCLIC */
2091 sz += sizeof(struct mlx5e_rx_wqe_cyc);
2094 return order_base_2(sz);
2097 static u8 mlx5e_get_rq_log_wq_sz(void *rqc)
2099 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2101 return MLX5_GET(wq, wq, log_wq_sz);
2104 void mlx5e_build_rq_param(struct mlx5e_priv *priv,
2105 struct mlx5e_params *params,
2106 struct mlx5e_xsk_param *xsk,
2107 struct mlx5e_rq_param *param)
2109 struct mlx5_core_dev *mdev = priv->mdev;
2110 void *rqc = param->rqc;
2111 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2114 switch (params->rq_wq_type) {
2115 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2116 MLX5_SET(wq, wq, log_wqe_num_of_strides,
2117 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk) -
2118 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
2119 MLX5_SET(wq, wq, log_wqe_stride_size,
2120 mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk) -
2121 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
2122 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params, xsk));
2124 default: /* MLX5_WQ_TYPE_CYCLIC */
2125 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
2126 mlx5e_build_rq_frags_info(mdev, params, xsk, ¶m->frags_info);
2127 ndsegs = param->frags_info.num_frags;
2130 MLX5_SET(wq, wq, wq_type, params->rq_wq_type);
2131 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
2132 MLX5_SET(wq, wq, log_wq_stride,
2133 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
2134 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
2135 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
2136 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
2137 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
2139 param->wq.buf_numa_node = dev_to_node(mdev->device);
2142 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
2143 struct mlx5e_rq_param *param)
2145 struct mlx5_core_dev *mdev = priv->mdev;
2146 void *rqc = param->rqc;
2147 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2149 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
2150 MLX5_SET(wq, wq, log_wq_stride,
2151 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
2152 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
2154 param->wq.buf_numa_node = dev_to_node(mdev->device);
2157 void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
2158 struct mlx5e_sq_param *param)
2160 void *sqc = param->sqc;
2161 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2163 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
2164 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
2166 param->wq.buf_numa_node = dev_to_node(priv->mdev->device);
2169 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
2170 struct mlx5e_params *params,
2171 struct mlx5e_sq_param *param)
2173 void *sqc = param->sqc;
2174 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2177 allow_swp = mlx5_geneve_tx_allowed(priv->mdev) ||
2178 !!MLX5_IPSEC_DEV(priv->mdev);
2179 mlx5e_build_sq_param_common(priv, param);
2180 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2181 MLX5_SET(sqc, sqc, allow_swp, allow_swp);
2184 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
2185 struct mlx5e_cq_param *param)
2187 void *cqc = param->cqc;
2189 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2190 if (MLX5_CAP_GEN(priv->mdev, cqe_128_always) && cache_line_size() >= 128)
2191 MLX5_SET(cqc, cqc, cqe_sz, CQE_STRIDE_128_PAD);
2194 void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2195 struct mlx5e_params *params,
2196 struct mlx5e_xsk_param *xsk,
2197 struct mlx5e_cq_param *param)
2199 struct mlx5_core_dev *mdev = priv->mdev;
2200 void *cqc = param->cqc;
2203 switch (params->rq_wq_type) {
2204 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2205 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params, xsk) +
2206 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
2208 default: /* MLX5_WQ_TYPE_CYCLIC */
2209 log_cq_size = params->log_rq_mtu_frames;
2212 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2213 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2214 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2215 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2218 mlx5e_build_common_cq_param(priv, param);
2219 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2222 void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2223 struct mlx5e_params *params,
2224 struct mlx5e_cq_param *param)
2226 void *cqc = param->cqc;
2228 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2230 mlx5e_build_common_cq_param(priv, param);
2231 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2234 void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2236 struct mlx5e_cq_param *param)
2238 void *cqc = param->cqc;
2240 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2242 mlx5e_build_common_cq_param(priv, param);
2244 param->cq_period_mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2247 void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2249 struct mlx5e_sq_param *param)
2251 void *sqc = param->sqc;
2252 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2254 mlx5e_build_sq_param_common(priv, param);
2256 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2257 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2260 void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2261 struct mlx5e_params *params,
2262 struct mlx5e_sq_param *param)
2264 void *sqc = param->sqc;
2265 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2267 mlx5e_build_sq_param_common(priv, param);
2268 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2269 param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE);
2272 static u8 mlx5e_build_icosq_log_wq_sz(struct mlx5e_params *params,
2273 struct mlx5e_rq_param *rqp)
2275 switch (params->rq_wq_type) {
2276 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2277 return order_base_2(MLX5E_UMR_WQEBBS) +
2278 mlx5e_get_rq_log_wq_sz(rqp->rqc);
2279 default: /* MLX5_WQ_TYPE_CYCLIC */
2280 return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2284 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2285 struct mlx5e_params *params,
2286 struct mlx5e_channel_param *cparam)
2290 mlx5e_build_rq_param(priv, params, NULL, &cparam->rq);
2292 icosq_log_wq_sz = mlx5e_build_icosq_log_wq_sz(params, &cparam->rq);
2294 mlx5e_build_sq_param(priv, params, &cparam->sq);
2295 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2296 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2297 mlx5e_build_rx_cq_param(priv, params, NULL, &cparam->rx_cq);
2298 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2299 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2302 int mlx5e_open_channels(struct mlx5e_priv *priv,
2303 struct mlx5e_channels *chs)
2305 struct mlx5e_channel_param *cparam;
2309 chs->num = chs->params.num_channels;
2311 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2312 cparam = kvzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2313 if (!chs->c || !cparam)
2316 mlx5e_build_channel_param(priv, &chs->params, cparam);
2317 for (i = 0; i < chs->num; i++) {
2318 struct xdp_umem *umem = NULL;
2320 if (chs->params.xdp_prog)
2321 umem = mlx5e_xsk_get_umem(&chs->params, chs->params.xsk, i);
2323 err = mlx5e_open_channel(priv, i, &chs->params, cparam, umem, &chs->c[i]);
2325 goto err_close_channels;
2328 if (!IS_ERR_OR_NULL(priv->tx_reporter))
2329 devlink_health_reporter_state_update(priv->tx_reporter,
2330 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY);
2336 for (i--; i >= 0; i--)
2337 mlx5e_close_channel(chs->c[i]);
2346 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2350 for (i = 0; i < chs->num; i++)
2351 mlx5e_activate_channel(chs->c[i]);
2354 #define MLX5E_RQ_WQES_TIMEOUT 20000 /* msecs */
2356 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2361 for (i = 0; i < chs->num; i++) {
2362 int timeout = err ? 0 : MLX5E_RQ_WQES_TIMEOUT;
2364 err |= mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq, timeout);
2366 /* Don't wait on the XSK RQ, because the newer xdpsock sample
2367 * doesn't provide any Fill Ring entries at the setup stage.
2371 return err ? -ETIMEDOUT : 0;
2374 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2378 for (i = 0; i < chs->num; i++)
2379 mlx5e_deactivate_channel(chs->c[i]);
2382 void mlx5e_close_channels(struct mlx5e_channels *chs)
2386 for (i = 0; i < chs->num; i++)
2387 mlx5e_close_channel(chs->c[i]);
2394 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2396 struct mlx5_core_dev *mdev = priv->mdev;
2403 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2404 in = kvzalloc(inlen, GFP_KERNEL);
2408 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2410 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2411 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2413 for (i = 0; i < sz; i++)
2414 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2416 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2418 rqt->enabled = true;
2424 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2426 rqt->enabled = false;
2427 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2430 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2432 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2435 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2437 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2441 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
2443 const int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
2447 for (ix = 0; ix < max_nch; ix++) {
2448 err = mlx5e_create_rqt(priv, 1 /*size */, &tirs[ix].rqt);
2450 goto err_destroy_rqts;
2456 mlx5_core_warn(priv->mdev, "create rqts failed, %d\n", err);
2457 for (ix--; ix >= 0; ix--)
2458 mlx5e_destroy_rqt(priv, &tirs[ix].rqt);
2463 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
2465 const int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
2468 for (i = 0; i < max_nch; i++)
2469 mlx5e_destroy_rqt(priv, &tirs[i].rqt);
2472 static int mlx5e_rx_hash_fn(int hfunc)
2474 return (hfunc == ETH_RSS_HASH_TOP) ?
2475 MLX5_RX_HASH_FN_TOEPLITZ :
2476 MLX5_RX_HASH_FN_INVERTED_XOR8;
2479 int mlx5e_bits_invert(unsigned long a, int size)
2484 for (i = 0; i < size; i++)
2485 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2490 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2491 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2495 for (i = 0; i < sz; i++) {
2501 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2502 ix = mlx5e_bits_invert(i, ilog2(sz));
2504 ix = priv->rss_params.indirection_rqt[ix];
2505 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2509 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2513 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2514 struct mlx5e_redirect_rqt_param rrp)
2516 struct mlx5_core_dev *mdev = priv->mdev;
2522 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2523 in = kvzalloc(inlen, GFP_KERNEL);
2527 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2529 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2530 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2531 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2532 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2538 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2539 struct mlx5e_redirect_rqt_param rrp)
2544 if (ix >= rrp.rss.channels->num)
2545 return priv->drop_rq.rqn;
2547 return rrp.rss.channels->c[ix]->rq.rqn;
2550 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2551 struct mlx5e_redirect_rqt_param rrp)
2556 if (priv->indir_rqt.enabled) {
2558 rqtn = priv->indir_rqt.rqtn;
2559 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2562 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2563 struct mlx5e_redirect_rqt_param direct_rrp = {
2566 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2570 /* Direct RQ Tables */
2571 if (!priv->direct_tir[ix].rqt.enabled)
2574 rqtn = priv->direct_tir[ix].rqt.rqtn;
2575 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2579 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2580 struct mlx5e_channels *chs)
2582 struct mlx5e_redirect_rqt_param rrp = {
2587 .hfunc = priv->rss_params.hfunc,
2592 mlx5e_redirect_rqts(priv, rrp);
2595 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2597 struct mlx5e_redirect_rqt_param drop_rrp = {
2600 .rqn = priv->drop_rq.rqn,
2604 mlx5e_redirect_rqts(priv, drop_rrp);
2607 static const struct mlx5e_tirc_config tirc_default_config[MLX5E_NUM_INDIR_TIRS] = {
2608 [MLX5E_TT_IPV4_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2609 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2610 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2612 [MLX5E_TT_IPV6_TCP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2613 .l4_prot_type = MLX5_L4_PROT_TYPE_TCP,
2614 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2616 [MLX5E_TT_IPV4_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2617 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2618 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2620 [MLX5E_TT_IPV6_UDP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2621 .l4_prot_type = MLX5_L4_PROT_TYPE_UDP,
2622 .rx_hash_fields = MLX5_HASH_IP_L4PORTS,
2624 [MLX5E_TT_IPV4_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2626 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2628 [MLX5E_TT_IPV6_IPSEC_AH] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2630 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2632 [MLX5E_TT_IPV4_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2634 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2636 [MLX5E_TT_IPV6_IPSEC_ESP] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2638 .rx_hash_fields = MLX5_HASH_IP_IPSEC_SPI,
2640 [MLX5E_TT_IPV4] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV4,
2642 .rx_hash_fields = MLX5_HASH_IP,
2644 [MLX5E_TT_IPV6] = { .l3_prot_type = MLX5_L3_PROT_TYPE_IPV6,
2646 .rx_hash_fields = MLX5_HASH_IP,
2650 struct mlx5e_tirc_config mlx5e_tirc_get_default_config(enum mlx5e_traffic_types tt)
2652 return tirc_default_config[tt];
2655 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2657 if (!params->lro_en)
2660 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2662 MLX5_SET(tirc, tirc, lro_enable_mask,
2663 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2664 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2665 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2666 (MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2667 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2670 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_rss_params *rss_params,
2671 const struct mlx5e_tirc_config *ttconfig,
2672 void *tirc, bool inner)
2674 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2675 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2677 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(rss_params->hfunc));
2678 if (rss_params->hfunc == ETH_RSS_HASH_TOP) {
2679 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2680 rx_hash_toeplitz_key);
2681 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2682 rx_hash_toeplitz_key);
2684 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2685 memcpy(rss_key, rss_params->toeplitz_hash_key, len);
2687 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2688 ttconfig->l3_prot_type);
2689 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2690 ttconfig->l4_prot_type);
2691 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2692 ttconfig->rx_hash_fields);
2695 static void mlx5e_update_rx_hash_fields(struct mlx5e_tirc_config *ttconfig,
2696 enum mlx5e_traffic_types tt,
2699 *ttconfig = tirc_default_config[tt];
2700 ttconfig->rx_hash_fields = rx_hash_fields;
2703 void mlx5e_modify_tirs_hash(struct mlx5e_priv *priv, void *in, int inlen)
2705 void *tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2706 struct mlx5e_rss_params *rss = &priv->rss_params;
2707 struct mlx5_core_dev *mdev = priv->mdev;
2708 int ctxlen = MLX5_ST_SZ_BYTES(tirc);
2709 struct mlx5e_tirc_config ttconfig;
2712 MLX5_SET(modify_tir_in, in, bitmask.hash, 1);
2714 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2715 memset(tirc, 0, ctxlen);
2716 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2717 rss->rx_hash_fields[tt]);
2718 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, false);
2719 mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in, inlen);
2722 if (!mlx5e_tunnel_inner_ft_supported(priv->mdev))
2725 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2726 memset(tirc, 0, ctxlen);
2727 mlx5e_update_rx_hash_fields(&ttconfig, tt,
2728 rss->rx_hash_fields[tt]);
2729 mlx5e_build_indir_tir_ctx_hash(rss, &ttconfig, tirc, true);
2730 mlx5_core_modify_tir(mdev, priv->inner_indir_tir[tt].tirn, in,
2735 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2737 struct mlx5_core_dev *mdev = priv->mdev;
2746 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2747 in = kvzalloc(inlen, GFP_KERNEL);
2751 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2752 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2754 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2756 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2757 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2763 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2764 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2776 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2777 struct mlx5e_params *params, u16 mtu)
2779 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2782 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2786 /* Update vport context MTU */
2787 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2791 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2792 struct mlx5e_params *params, u16 *mtu)
2797 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2798 if (err || !hw_mtu) /* fallback to port oper mtu */
2799 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2801 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2804 int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2806 struct mlx5e_params *params = &priv->channels.params;
2807 struct net_device *netdev = priv->netdev;
2808 struct mlx5_core_dev *mdev = priv->mdev;
2812 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2816 mlx5e_query_mtu(mdev, params, &mtu);
2817 if (mtu != params->sw_mtu)
2818 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2819 __func__, mtu, params->sw_mtu);
2821 params->sw_mtu = mtu;
2825 void mlx5e_set_netdev_mtu_boundaries(struct mlx5e_priv *priv)
2827 struct mlx5e_params *params = &priv->channels.params;
2828 struct net_device *netdev = priv->netdev;
2829 struct mlx5_core_dev *mdev = priv->mdev;
2832 /* MTU range: 68 - hw-specific max */
2833 netdev->min_mtu = ETH_MIN_MTU;
2835 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2836 netdev->max_mtu = min_t(unsigned int, MLX5E_HW2SW_MTU(params, max_mtu),
2840 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2842 struct mlx5e_priv *priv = netdev_priv(netdev);
2843 int nch = priv->channels.params.num_channels;
2844 int ntc = priv->channels.params.num_tc;
2847 netdev_reset_tc(netdev);
2852 netdev_set_num_tc(netdev, ntc);
2854 /* Map netdev TCs to offset 0
2855 * We have our own UP to TXQ mapping for QoS
2857 for (tc = 0; tc < ntc; tc++)
2858 netdev_set_tc_queue(netdev, tc, nch, 0);
2861 static void mlx5e_build_tc2txq_maps(struct mlx5e_priv *priv)
2863 int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
2866 for (i = 0; i < max_nch; i++)
2867 for (tc = 0; tc < priv->profile->max_tc; tc++)
2868 priv->channel_tc2txq[i][tc] = i + tc * max_nch;
2871 static void mlx5e_build_tx2sq_maps(struct mlx5e_priv *priv)
2873 struct mlx5e_channel *c;
2874 struct mlx5e_txqsq *sq;
2877 for (i = 0; i < priv->channels.num; i++) {
2878 c = priv->channels.c[i];
2879 for (tc = 0; tc < c->num_tc; tc++) {
2881 priv->txq2sq[sq->txq_ix] = sq;
2886 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2888 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2889 int num_rxqs = priv->channels.num * MLX5E_NUM_RQ_GROUPS;
2890 struct net_device *netdev = priv->netdev;
2892 mlx5e_netdev_set_tcs(netdev);
2893 netif_set_real_num_tx_queues(netdev, num_txqs);
2894 netif_set_real_num_rx_queues(netdev, num_rxqs);
2896 mlx5e_build_tx2sq_maps(priv);
2897 mlx5e_activate_channels(&priv->channels);
2898 mlx5e_xdp_tx_enable(priv);
2899 netif_tx_start_all_queues(priv->netdev);
2901 if (mlx5e_is_vport_rep(priv))
2902 mlx5e_add_sqs_fwd_rules(priv);
2904 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2905 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2907 mlx5e_xsk_redirect_rqts_to_channels(priv, &priv->channels);
2910 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2912 mlx5e_xsk_redirect_rqts_to_drop(priv, &priv->channels);
2914 mlx5e_redirect_rqts_to_drop(priv);
2916 if (mlx5e_is_vport_rep(priv))
2917 mlx5e_remove_sqs_fwd_rules(priv);
2919 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2920 * polling for inactive tx queues.
2922 netif_tx_stop_all_queues(priv->netdev);
2923 netif_tx_disable(priv->netdev);
2924 mlx5e_xdp_tx_disable(priv);
2925 mlx5e_deactivate_channels(&priv->channels);
2928 static void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2929 struct mlx5e_channels *new_chs,
2930 mlx5e_fp_hw_modify hw_modify)
2932 struct net_device *netdev = priv->netdev;
2936 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2938 carrier_ok = netif_carrier_ok(netdev);
2939 netif_carrier_off(netdev);
2941 if (new_num_txqs < netdev->real_num_tx_queues)
2942 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2944 mlx5e_deactivate_priv_channels(priv);
2945 mlx5e_close_channels(&priv->channels);
2947 priv->channels = *new_chs;
2949 /* New channels are ready to roll, modify HW settings if needed */
2953 priv->profile->update_rx(priv);
2954 mlx5e_activate_priv_channels(priv);
2956 /* return carrier back if needed */
2958 netif_carrier_on(netdev);
2961 int mlx5e_safe_switch_channels(struct mlx5e_priv *priv,
2962 struct mlx5e_channels *new_chs,
2963 mlx5e_fp_hw_modify hw_modify)
2967 err = mlx5e_open_channels(priv, new_chs);
2971 mlx5e_switch_priv_channels(priv, new_chs, hw_modify);
2975 int mlx5e_safe_reopen_channels(struct mlx5e_priv *priv)
2977 struct mlx5e_channels new_channels = {};
2979 new_channels.params = priv->channels.params;
2980 return mlx5e_safe_switch_channels(priv, &new_channels, NULL);
2983 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2985 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
2986 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2989 int mlx5e_open_locked(struct net_device *netdev)
2991 struct mlx5e_priv *priv = netdev_priv(netdev);
2992 bool is_xdp = priv->channels.params.xdp_prog;
2995 set_bit(MLX5E_STATE_OPENED, &priv->state);
2997 mlx5e_xdp_set_open(priv);
2999 err = mlx5e_open_channels(priv, &priv->channels);
3001 goto err_clear_state_opened_flag;
3003 priv->profile->update_rx(priv);
3004 mlx5e_activate_priv_channels(priv);
3005 if (priv->profile->update_carrier)
3006 priv->profile->update_carrier(priv);
3008 mlx5e_queue_update_stats(priv);
3011 err_clear_state_opened_flag:
3013 mlx5e_xdp_set_closed(priv);
3014 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3018 int mlx5e_open(struct net_device *netdev)
3020 struct mlx5e_priv *priv = netdev_priv(netdev);
3023 mutex_lock(&priv->state_lock);
3024 err = mlx5e_open_locked(netdev);
3026 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
3027 mutex_unlock(&priv->state_lock);
3029 if (mlx5_vxlan_allowed(priv->mdev->vxlan))
3030 udp_tunnel_get_rx_info(netdev);
3035 int mlx5e_close_locked(struct net_device *netdev)
3037 struct mlx5e_priv *priv = netdev_priv(netdev);
3039 /* May already be CLOSED in case a previous configuration operation
3040 * (e.g RX/TX queue size change) that involves close&open failed.
3042 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3045 if (priv->channels.params.xdp_prog)
3046 mlx5e_xdp_set_closed(priv);
3047 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3049 netif_carrier_off(priv->netdev);
3050 mlx5e_deactivate_priv_channels(priv);
3051 mlx5e_close_channels(&priv->channels);
3056 int mlx5e_close(struct net_device *netdev)
3058 struct mlx5e_priv *priv = netdev_priv(netdev);
3061 if (!netif_device_present(netdev))
3064 mutex_lock(&priv->state_lock);
3065 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
3066 err = mlx5e_close_locked(netdev);
3067 mutex_unlock(&priv->state_lock);
3072 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
3073 struct mlx5e_rq *rq,
3074 struct mlx5e_rq_param *param)
3076 void *rqc = param->rqc;
3077 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
3080 param->wq.db_numa_node = param->wq.buf_numa_node;
3082 err = mlx5_wq_cyc_create(mdev, ¶m->wq, rqc_wq, &rq->wqe.wq,
3087 /* Mark as unused given "Drop-RQ" packets never reach XDP */
3088 xdp_rxq_info_unused(&rq->xdp_rxq);
3095 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
3096 struct mlx5e_cq *cq,
3097 struct mlx5e_cq_param *param)
3099 param->wq.buf_numa_node = dev_to_node(mdev->device);
3100 param->wq.db_numa_node = dev_to_node(mdev->device);
3102 return mlx5e_alloc_cq_common(mdev, param, cq);
3105 int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
3106 struct mlx5e_rq *drop_rq)
3108 struct mlx5_core_dev *mdev = priv->mdev;
3109 struct mlx5e_cq_param cq_param = {};
3110 struct mlx5e_rq_param rq_param = {};
3111 struct mlx5e_cq *cq = &drop_rq->cq;
3114 mlx5e_build_drop_rq_param(priv, &rq_param);
3116 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
3120 err = mlx5e_create_cq(cq, &cq_param);
3124 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3126 goto err_destroy_cq;
3128 err = mlx5e_create_rq(drop_rq, &rq_param);
3132 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3134 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3139 mlx5e_free_rq(drop_rq);
3142 mlx5e_destroy_cq(cq);
3150 void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3152 mlx5e_destroy_rq(drop_rq);
3153 mlx5e_free_rq(drop_rq);
3154 mlx5e_destroy_cq(&drop_rq->cq);
3155 mlx5e_free_cq(&drop_rq->cq);
3158 int mlx5e_create_tis(struct mlx5_core_dev *mdev, void *in, u32 *tisn)
3160 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3162 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
3164 if (MLX5_GET(tisc, tisc, tls_en))
3165 MLX5_SET(tisc, tisc, pd, mdev->mlx5e_res.pdn);
3167 if (mlx5_lag_is_lacp_owner(mdev))
3168 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
3170 return mlx5_core_create_tis(mdev, in, MLX5_ST_SZ_BYTES(create_tis_in), tisn);
3173 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
3175 mlx5_core_destroy_tis(mdev, tisn);
3178 int mlx5e_create_tises(struct mlx5e_priv *priv)
3183 for (tc = 0; tc < priv->profile->max_tc; tc++) {
3184 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
3187 tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3189 MLX5_SET(tisc, tisc, prio, tc << 1);
3191 err = mlx5e_create_tis(priv->mdev, in, &priv->tisn[tc]);
3193 goto err_close_tises;
3199 for (tc--; tc >= 0; tc--)
3200 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3205 static void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3209 mlx5e_tx_reporter_destroy(priv);
3210 for (tc = 0; tc < priv->profile->max_tc; tc++)
3211 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3214 static void mlx5e_build_indir_tir_ctx_common(struct mlx5e_priv *priv,
3215 u32 rqtn, u32 *tirc)
3217 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3218 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3219 MLX5_SET(tirc, tirc, indirect_table, rqtn);
3220 MLX5_SET(tirc, tirc, tunneled_offload_en,
3221 priv->channels.params.tunneled_offload_en);
3223 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3226 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
3227 enum mlx5e_traffic_types tt,
3230 mlx5e_build_indir_tir_ctx_common(priv, priv->indir_rqt.rqtn, tirc);
3231 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
3232 &tirc_default_config[tt], tirc, false);
3235 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
3237 mlx5e_build_indir_tir_ctx_common(priv, rqtn, tirc);
3238 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
3241 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
3242 enum mlx5e_traffic_types tt,
3245 mlx5e_build_indir_tir_ctx_common(priv, priv->indir_rqt.rqtn, tirc);
3246 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params,
3247 &tirc_default_config[tt], tirc, true);
3250 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3252 struct mlx5e_tir *tir;
3260 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3261 in = kvzalloc(inlen, GFP_KERNEL);
3265 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
3266 memset(in, 0, inlen);
3267 tir = &priv->indir_tir[tt];
3268 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3269 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
3270 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3272 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
3273 goto err_destroy_inner_tirs;
3277 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3280 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
3281 memset(in, 0, inlen);
3282 tir = &priv->inner_indir_tir[i];
3283 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3284 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
3285 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3287 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
3288 goto err_destroy_inner_tirs;
3297 err_destroy_inner_tirs:
3298 for (i--; i >= 0; i--)
3299 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3301 for (tt--; tt >= 0; tt--)
3302 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3309 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
3311 const int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
3312 struct mlx5e_tir *tir;
3319 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3320 in = kvzalloc(inlen, GFP_KERNEL);
3324 for (ix = 0; ix < max_nch; ix++) {
3325 memset(in, 0, inlen);
3327 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3328 mlx5e_build_direct_tir_ctx(priv, tir->rqt.rqtn, tirc);
3329 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3331 goto err_destroy_ch_tirs;
3336 err_destroy_ch_tirs:
3337 mlx5_core_warn(priv->mdev, "create tirs failed, %d\n", err);
3338 for (ix--; ix >= 0; ix--)
3339 mlx5e_destroy_tir(priv->mdev, &tirs[ix]);
3347 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3351 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3352 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3354 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3357 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3358 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3361 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv, struct mlx5e_tir *tirs)
3363 const int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
3366 for (i = 0; i < max_nch; i++)
3367 mlx5e_destroy_tir(priv->mdev, &tirs[i]);
3370 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3375 for (i = 0; i < chs->num; i++) {
3376 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3384 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3389 for (i = 0; i < chs->num; i++) {
3390 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3398 static int mlx5e_setup_tc_mqprio(struct mlx5e_priv *priv,
3399 struct tc_mqprio_qopt *mqprio)
3401 struct mlx5e_channels new_channels = {};
3402 u8 tc = mqprio->num_tc;
3405 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3407 if (tc && tc != MLX5E_MAX_NUM_TC)
3410 mutex_lock(&priv->state_lock);
3412 new_channels.params = priv->channels.params;
3413 new_channels.params.num_tc = tc ? tc : 1;
3415 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3416 priv->channels.params = new_channels.params;
3420 err = mlx5e_safe_switch_channels(priv, &new_channels, NULL);
3424 priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3425 new_channels.params.num_tc);
3427 mutex_unlock(&priv->state_lock);
3431 #ifdef CONFIG_MLX5_ESWITCH
3432 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3433 struct flow_cls_offload *cls_flower,
3434 unsigned long flags)
3436 switch (cls_flower->command) {
3437 case FLOW_CLS_REPLACE:
3438 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
3440 case FLOW_CLS_DESTROY:
3441 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
3443 case FLOW_CLS_STATS:
3444 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
3451 static int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3454 unsigned long flags = MLX5_TC_FLAG(INGRESS) | MLX5_TC_FLAG(NIC_OFFLOAD);
3455 struct mlx5e_priv *priv = cb_priv;
3458 case TC_SETUP_CLSFLOWER:
3459 return mlx5e_setup_tc_cls_flower(priv, type_data, flags);
3466 static LIST_HEAD(mlx5e_block_cb_list);
3468 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3471 struct mlx5e_priv *priv = netdev_priv(dev);
3474 #ifdef CONFIG_MLX5_ESWITCH
3475 case TC_SETUP_BLOCK:
3476 return flow_block_cb_setup_simple(type_data,
3477 &mlx5e_block_cb_list,
3478 mlx5e_setup_tc_block_cb,
3481 case TC_SETUP_QDISC_MQPRIO:
3482 return mlx5e_setup_tc_mqprio(priv, type_data);
3488 void mlx5e_fold_sw_stats64(struct mlx5e_priv *priv, struct rtnl_link_stats64 *s)
3492 for (i = 0; i < mlx5e_get_netdev_max_channels(priv->netdev); i++) {
3493 struct mlx5e_channel_stats *channel_stats = &priv->channel_stats[i];
3494 struct mlx5e_rq_stats *xskrq_stats = &channel_stats->xskrq;
3495 struct mlx5e_rq_stats *rq_stats = &channel_stats->rq;
3498 s->rx_packets += rq_stats->packets + xskrq_stats->packets;
3499 s->rx_bytes += rq_stats->bytes + xskrq_stats->bytes;
3501 for (j = 0; j < priv->max_opened_tc; j++) {
3502 struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[j];
3504 s->tx_packets += sq_stats->packets;
3505 s->tx_bytes += sq_stats->bytes;
3506 s->tx_dropped += sq_stats->dropped;
3512 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3514 struct mlx5e_priv *priv = netdev_priv(dev);
3515 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3516 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3518 if (!mlx5e_monitor_counter_supported(priv)) {
3519 /* update HW stats in background for next time */
3520 mlx5e_queue_update_stats(priv);
3523 if (mlx5e_is_uplink_rep(priv)) {
3524 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3525 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3526 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3527 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3529 mlx5e_fold_sw_stats64(priv, stats);
3532 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3534 stats->rx_length_errors =
3535 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3536 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3537 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3538 stats->rx_crc_errors =
3539 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3540 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3541 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3542 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3543 stats->rx_frame_errors;
3544 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3546 /* vport multicast also counts packets that are dropped due to steering
3547 * or rx out of buffer
3550 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3553 static void mlx5e_set_rx_mode(struct net_device *dev)
3555 struct mlx5e_priv *priv = netdev_priv(dev);
3557 queue_work(priv->wq, &priv->set_rx_mode_work);
3560 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3562 struct mlx5e_priv *priv = netdev_priv(netdev);
3563 struct sockaddr *saddr = addr;
3565 if (!is_valid_ether_addr(saddr->sa_data))
3566 return -EADDRNOTAVAIL;
3568 netif_addr_lock_bh(netdev);
3569 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3570 netif_addr_unlock_bh(netdev);
3572 queue_work(priv->wq, &priv->set_rx_mode_work);
3577 #define MLX5E_SET_FEATURE(features, feature, enable) \
3580 *features |= feature; \
3582 *features &= ~feature; \
3585 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3587 static int set_feature_lro(struct net_device *netdev, bool enable)
3589 struct mlx5e_priv *priv = netdev_priv(netdev);
3590 struct mlx5_core_dev *mdev = priv->mdev;
3591 struct mlx5e_channels new_channels = {};
3592 struct mlx5e_params *old_params;
3596 mutex_lock(&priv->state_lock);
3598 if (enable && priv->xsk.refcnt) {
3599 netdev_warn(netdev, "LRO is incompatible with AF_XDP (%hu XSKs are active)\n",
3605 old_params = &priv->channels.params;
3606 if (enable && !MLX5E_GET_PFLAG(old_params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3607 netdev_warn(netdev, "can't set LRO with legacy RQ\n");
3612 reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3614 new_channels.params = *old_params;
3615 new_channels.params.lro_en = enable;
3617 if (old_params->rq_wq_type != MLX5_WQ_TYPE_CYCLIC) {
3618 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params, NULL) ==
3619 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params, NULL))
3624 *old_params = new_channels.params;
3625 err = mlx5e_modify_tirs_lro(priv);
3629 err = mlx5e_safe_switch_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3631 mutex_unlock(&priv->state_lock);
3635 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3637 struct mlx5e_priv *priv = netdev_priv(netdev);
3640 mlx5e_enable_cvlan_filter(priv);
3642 mlx5e_disable_cvlan_filter(priv);
3647 #ifdef CONFIG_MLX5_ESWITCH
3648 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3650 struct mlx5e_priv *priv = netdev_priv(netdev);
3652 if (!enable && mlx5e_tc_num_filters(priv, MLX5_TC_FLAG(NIC_OFFLOAD))) {
3654 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3662 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3664 struct mlx5e_priv *priv = netdev_priv(netdev);
3665 struct mlx5_core_dev *mdev = priv->mdev;
3667 return mlx5_set_port_fcs(mdev, !enable);
3670 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3672 struct mlx5e_priv *priv = netdev_priv(netdev);
3675 mutex_lock(&priv->state_lock);
3677 priv->channels.params.scatter_fcs_en = enable;
3678 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3680 priv->channels.params.scatter_fcs_en = !enable;
3682 mutex_unlock(&priv->state_lock);
3687 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3689 struct mlx5e_priv *priv = netdev_priv(netdev);
3692 mutex_lock(&priv->state_lock);
3694 priv->channels.params.vlan_strip_disable = !enable;
3695 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3698 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3700 priv->channels.params.vlan_strip_disable = enable;
3703 mutex_unlock(&priv->state_lock);
3708 #ifdef CONFIG_MLX5_EN_ARFS
3709 static int set_feature_arfs(struct net_device *netdev, bool enable)
3711 struct mlx5e_priv *priv = netdev_priv(netdev);
3715 err = mlx5e_arfs_enable(priv);
3717 err = mlx5e_arfs_disable(priv);
3723 static int mlx5e_handle_feature(struct net_device *netdev,
3724 netdev_features_t *features,
3725 netdev_features_t wanted_features,
3726 netdev_features_t feature,
3727 mlx5e_feature_handler feature_handler)
3729 netdev_features_t changes = wanted_features ^ netdev->features;
3730 bool enable = !!(wanted_features & feature);
3733 if (!(changes & feature))
3736 err = feature_handler(netdev, enable);
3738 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3739 enable ? "Enable" : "Disable", &feature, err);
3743 MLX5E_SET_FEATURE(features, feature, enable);
3747 int mlx5e_set_features(struct net_device *netdev, netdev_features_t features)
3749 netdev_features_t oper_features = netdev->features;
3752 #define MLX5E_HANDLE_FEATURE(feature, handler) \
3753 mlx5e_handle_feature(netdev, &oper_features, features, feature, handler)
3755 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
3756 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
3757 set_feature_cvlan_filter);
3758 #ifdef CONFIG_MLX5_ESWITCH
3759 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_tc_num_filters);
3761 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
3762 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
3763 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3764 #ifdef CONFIG_MLX5_EN_ARFS
3765 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
3769 netdev->features = oper_features;
3776 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3777 netdev_features_t features)
3779 struct mlx5e_priv *priv = netdev_priv(netdev);
3780 struct mlx5e_params *params;
3782 mutex_lock(&priv->state_lock);
3783 params = &priv->channels.params;
3784 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3785 /* HW strips the outer C-tag header, this is a problem
3786 * for S-tag traffic.
3788 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3789 if (!params->vlan_strip_disable)
3790 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3792 if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3793 if (features & NETIF_F_LRO) {
3794 netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n");
3795 features &= ~NETIF_F_LRO;
3799 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
3800 features &= ~NETIF_F_RXHASH;
3801 if (netdev->features & NETIF_F_RXHASH)
3802 netdev_warn(netdev, "Disabling rxhash, not supported when CQE compress is active\n");
3805 mutex_unlock(&priv->state_lock);
3810 static bool mlx5e_xsk_validate_mtu(struct net_device *netdev,
3811 struct mlx5e_channels *chs,
3812 struct mlx5e_params *new_params,
3813 struct mlx5_core_dev *mdev)
3817 for (ix = 0; ix < chs->params.num_channels; ix++) {
3818 struct xdp_umem *umem = mlx5e_xsk_get_umem(&chs->params, chs->params.xsk, ix);
3819 struct mlx5e_xsk_param xsk;
3824 mlx5e_build_xsk_param(umem, &xsk);
3826 if (!mlx5e_validate_xsk_param(new_params, &xsk, mdev)) {
3827 u32 hr = mlx5e_get_linear_rq_headroom(new_params, &xsk);
3828 int max_mtu_frame, max_mtu_page, max_mtu;
3830 /* Two criteria must be met:
3831 * 1. HW MTU + all headrooms <= XSK frame size.
3832 * 2. Size of SKBs allocated on XDP_PASS <= PAGE_SIZE.
3834 max_mtu_frame = MLX5E_HW2SW_MTU(new_params, xsk.chunk_size - hr);
3835 max_mtu_page = mlx5e_xdp_max_mtu(new_params, &xsk);
3836 max_mtu = min(max_mtu_frame, max_mtu_page);
3838 netdev_err(netdev, "MTU %d is too big for an XSK running on channel %hu. Try MTU <= %d\n",
3839 new_params->sw_mtu, ix, max_mtu);
3847 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu,
3848 change_hw_mtu_cb set_mtu_cb)
3850 struct mlx5e_priv *priv = netdev_priv(netdev);
3851 struct mlx5e_channels new_channels = {};
3852 struct mlx5e_params *params;
3856 mutex_lock(&priv->state_lock);
3858 params = &priv->channels.params;
3860 reset = !params->lro_en;
3861 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3863 new_channels.params = *params;
3864 new_channels.params.sw_mtu = new_mtu;
3866 if (params->xdp_prog &&
3867 !mlx5e_rx_is_linear_skb(&new_channels.params, NULL)) {
3868 netdev_err(netdev, "MTU(%d) > %d is not allowed while XDP enabled\n",
3869 new_mtu, mlx5e_xdp_max_mtu(params, NULL));
3874 if (priv->xsk.refcnt &&
3875 !mlx5e_xsk_validate_mtu(netdev, &priv->channels,
3876 &new_channels.params, priv->mdev)) {
3881 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
3882 bool is_linear = mlx5e_rx_mpwqe_is_linear_skb(priv->mdev,
3883 &new_channels.params,
3885 u8 ppw_old = mlx5e_mpwqe_log_pkts_per_wqe(params, NULL);
3886 u8 ppw_new = mlx5e_mpwqe_log_pkts_per_wqe(&new_channels.params, NULL);
3888 /* If XSK is active, XSK RQs are linear. */
3889 is_linear |= priv->xsk.refcnt;
3891 /* Always reset in linear mode - hw_mtu is used in data path. */
3892 reset = reset && (is_linear || (ppw_old != ppw_new));
3896 params->sw_mtu = new_mtu;
3899 netdev->mtu = params->sw_mtu;
3903 err = mlx5e_safe_switch_channels(priv, &new_channels, set_mtu_cb);
3907 netdev->mtu = new_channels.params.sw_mtu;
3910 mutex_unlock(&priv->state_lock);
3914 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu)
3916 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
3919 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3921 struct hwtstamp_config config;
3924 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz) ||
3925 (mlx5_clock_get_ptp_index(priv->mdev) == -1))
3928 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3931 /* TX HW timestamp */
3932 switch (config.tx_type) {
3933 case HWTSTAMP_TX_OFF:
3934 case HWTSTAMP_TX_ON:
3940 mutex_lock(&priv->state_lock);
3941 /* RX HW timestamp */
3942 switch (config.rx_filter) {
3943 case HWTSTAMP_FILTER_NONE:
3944 /* Reset CQE compression to Admin default */
3945 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3947 case HWTSTAMP_FILTER_ALL:
3948 case HWTSTAMP_FILTER_SOME:
3949 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3950 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3951 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3952 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3953 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3954 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3955 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3956 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3957 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3958 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3959 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3960 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3961 case HWTSTAMP_FILTER_NTP_ALL:
3962 /* Disable CQE compression */
3963 if (MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS))
3964 netdev_warn(priv->netdev, "Disabling RX cqe compression\n");
3965 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
3967 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
3968 mutex_unlock(&priv->state_lock);
3971 config.rx_filter = HWTSTAMP_FILTER_ALL;
3974 mutex_unlock(&priv->state_lock);
3978 memcpy(&priv->tstamp, &config, sizeof(config));
3979 mutex_unlock(&priv->state_lock);
3981 /* might need to fix some features */
3982 netdev_update_features(priv->netdev);
3984 return copy_to_user(ifr->ifr_data, &config,
3985 sizeof(config)) ? -EFAULT : 0;
3988 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
3990 struct hwtstamp_config *cfg = &priv->tstamp;
3992 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3995 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
3998 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4000 struct mlx5e_priv *priv = netdev_priv(dev);
4004 return mlx5e_hwstamp_set(priv, ifr);
4006 return mlx5e_hwstamp_get(priv, ifr);
4012 #ifdef CONFIG_MLX5_ESWITCH
4013 int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
4015 struct mlx5e_priv *priv = netdev_priv(dev);
4016 struct mlx5_core_dev *mdev = priv->mdev;
4018 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
4021 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
4024 struct mlx5e_priv *priv = netdev_priv(dev);
4025 struct mlx5_core_dev *mdev = priv->mdev;
4027 if (vlan_proto != htons(ETH_P_8021Q))
4028 return -EPROTONOSUPPORT;
4030 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
4034 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
4036 struct mlx5e_priv *priv = netdev_priv(dev);
4037 struct mlx5_core_dev *mdev = priv->mdev;
4039 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
4042 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
4044 struct mlx5e_priv *priv = netdev_priv(dev);
4045 struct mlx5_core_dev *mdev = priv->mdev;
4047 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
4050 int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
4053 struct mlx5e_priv *priv = netdev_priv(dev);
4054 struct mlx5_core_dev *mdev = priv->mdev;
4056 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
4057 max_tx_rate, min_tx_rate);
4060 static int mlx5_vport_link2ifla(u8 esw_link)
4063 case MLX5_VPORT_ADMIN_STATE_DOWN:
4064 return IFLA_VF_LINK_STATE_DISABLE;
4065 case MLX5_VPORT_ADMIN_STATE_UP:
4066 return IFLA_VF_LINK_STATE_ENABLE;
4068 return IFLA_VF_LINK_STATE_AUTO;
4071 static int mlx5_ifla_link2vport(u8 ifla_link)
4073 switch (ifla_link) {
4074 case IFLA_VF_LINK_STATE_DISABLE:
4075 return MLX5_VPORT_ADMIN_STATE_DOWN;
4076 case IFLA_VF_LINK_STATE_ENABLE:
4077 return MLX5_VPORT_ADMIN_STATE_UP;
4079 return MLX5_VPORT_ADMIN_STATE_AUTO;
4082 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
4085 struct mlx5e_priv *priv = netdev_priv(dev);
4086 struct mlx5_core_dev *mdev = priv->mdev;
4088 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
4089 mlx5_ifla_link2vport(link_state));
4092 int mlx5e_get_vf_config(struct net_device *dev,
4093 int vf, struct ifla_vf_info *ivi)
4095 struct mlx5e_priv *priv = netdev_priv(dev);
4096 struct mlx5_core_dev *mdev = priv->mdev;
4099 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
4102 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
4106 int mlx5e_get_vf_stats(struct net_device *dev,
4107 int vf, struct ifla_vf_stats *vf_stats)
4109 struct mlx5e_priv *priv = netdev_priv(dev);
4110 struct mlx5_core_dev *mdev = priv->mdev;
4112 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
4117 struct mlx5e_vxlan_work {
4118 struct work_struct work;
4119 struct mlx5e_priv *priv;
4123 static void mlx5e_vxlan_add_work(struct work_struct *work)
4125 struct mlx5e_vxlan_work *vxlan_work =
4126 container_of(work, struct mlx5e_vxlan_work, work);
4127 struct mlx5e_priv *priv = vxlan_work->priv;
4128 u16 port = vxlan_work->port;
4130 mutex_lock(&priv->state_lock);
4131 mlx5_vxlan_add_port(priv->mdev->vxlan, port);
4132 mutex_unlock(&priv->state_lock);
4137 static void mlx5e_vxlan_del_work(struct work_struct *work)
4139 struct mlx5e_vxlan_work *vxlan_work =
4140 container_of(work, struct mlx5e_vxlan_work, work);
4141 struct mlx5e_priv *priv = vxlan_work->priv;
4142 u16 port = vxlan_work->port;
4144 mutex_lock(&priv->state_lock);
4145 mlx5_vxlan_del_port(priv->mdev->vxlan, port);
4146 mutex_unlock(&priv->state_lock);
4150 static void mlx5e_vxlan_queue_work(struct mlx5e_priv *priv, u16 port, int add)
4152 struct mlx5e_vxlan_work *vxlan_work;
4154 vxlan_work = kmalloc(sizeof(*vxlan_work), GFP_ATOMIC);
4159 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_add_work);
4161 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_del_work);
4163 vxlan_work->priv = priv;
4164 vxlan_work->port = port;
4165 queue_work(priv->wq, &vxlan_work->work);
4168 void mlx5e_add_vxlan_port(struct net_device *netdev, struct udp_tunnel_info *ti)
4170 struct mlx5e_priv *priv = netdev_priv(netdev);
4172 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4175 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4178 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 1);
4181 void mlx5e_del_vxlan_port(struct net_device *netdev, struct udp_tunnel_info *ti)
4183 struct mlx5e_priv *priv = netdev_priv(netdev);
4185 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4188 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4191 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 0);
4194 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
4195 struct sk_buff *skb,
4196 netdev_features_t features)
4198 unsigned int offset = 0;
4199 struct udphdr *udph;
4203 switch (vlan_get_protocol(skb)) {
4204 case htons(ETH_P_IP):
4205 proto = ip_hdr(skb)->protocol;
4207 case htons(ETH_P_IPV6):
4208 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
4218 udph = udp_hdr(skb);
4219 port = be16_to_cpu(udph->dest);
4221 /* Verify if UDP port is being offloaded by HW */
4222 if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, port))
4225 #if IS_ENABLED(CONFIG_GENEVE)
4226 /* Support Geneve offload for default UDP port */
4227 if (port == GENEVE_UDP_PORT && mlx5_geneve_tx_allowed(priv->mdev))
4233 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
4234 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4237 netdev_features_t mlx5e_features_check(struct sk_buff *skb,
4238 struct net_device *netdev,
4239 netdev_features_t features)
4241 struct mlx5e_priv *priv = netdev_priv(netdev);
4243 features = vlan_features_check(skb, features);
4244 features = vxlan_features_check(skb, features);
4246 #ifdef CONFIG_MLX5_EN_IPSEC
4247 if (mlx5e_ipsec_feature_check(skb, netdev, features))
4251 /* Validate if the tunneled packet is being offloaded by HW */
4252 if (skb->encapsulation &&
4253 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
4254 return mlx5e_tunnel_features_check(priv, skb, features);
4259 static void mlx5e_tx_timeout_work(struct work_struct *work)
4261 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
4263 bool report_failed = false;
4268 mutex_lock(&priv->state_lock);
4270 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
4273 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
4274 struct netdev_queue *dev_queue =
4275 netdev_get_tx_queue(priv->netdev, i);
4276 struct mlx5e_txqsq *sq = priv->txq2sq[i];
4278 if (!netif_xmit_stopped(dev_queue))
4281 if (mlx5e_tx_reporter_timeout(sq))
4282 report_failed = true;
4288 err = mlx5e_safe_reopen_channels(priv);
4290 netdev_err(priv->netdev,
4291 "mlx5e_safe_reopen_channels failed recovering from a tx_timeout, err(%d).\n",
4295 mutex_unlock(&priv->state_lock);
4299 static void mlx5e_tx_timeout(struct net_device *dev)
4301 struct mlx5e_priv *priv = netdev_priv(dev);
4303 netdev_err(dev, "TX timeout detected\n");
4304 queue_work(priv->wq, &priv->tx_timeout_work);
4307 static int mlx5e_xdp_allowed(struct mlx5e_priv *priv, struct bpf_prog *prog)
4309 struct net_device *netdev = priv->netdev;
4310 struct mlx5e_channels new_channels = {};
4312 if (priv->channels.params.lro_en) {
4313 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
4317 if (MLX5_IPSEC_DEV(priv->mdev)) {
4318 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
4322 new_channels.params = priv->channels.params;
4323 new_channels.params.xdp_prog = prog;
4325 /* No XSK params: AF_XDP can't be enabled yet at the point of setting
4328 if (!mlx5e_rx_is_linear_skb(&new_channels.params, NULL)) {
4329 netdev_warn(netdev, "XDP is not allowed with MTU(%d) > %d\n",
4330 new_channels.params.sw_mtu,
4331 mlx5e_xdp_max_mtu(&new_channels.params, NULL));
4338 static int mlx5e_xdp_update_state(struct mlx5e_priv *priv)
4340 if (priv->channels.params.xdp_prog)
4341 mlx5e_xdp_set_open(priv);
4343 mlx5e_xdp_set_closed(priv);
4348 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
4350 struct mlx5e_priv *priv = netdev_priv(netdev);
4351 struct bpf_prog *old_prog;
4352 bool reset, was_opened;
4356 mutex_lock(&priv->state_lock);
4359 err = mlx5e_xdp_allowed(priv, prog);
4364 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
4365 /* no need for full reset when exchanging programs */
4366 reset = (!priv->channels.params.xdp_prog || !prog);
4368 if (was_opened && !reset) {
4369 /* num_channels is invariant here, so we can take the
4370 * batched reference right upfront.
4372 prog = bpf_prog_add(prog, priv->channels.num);
4374 err = PTR_ERR(prog);
4379 if (was_opened && reset) {
4380 struct mlx5e_channels new_channels = {};
4382 new_channels.params = priv->channels.params;
4383 new_channels.params.xdp_prog = prog;
4384 mlx5e_set_rq_type(priv->mdev, &new_channels.params);
4385 old_prog = priv->channels.params.xdp_prog;
4387 err = mlx5e_safe_switch_channels(priv, &new_channels, mlx5e_xdp_update_state);
4391 /* exchange programs, extra prog reference we got from caller
4392 * as long as we don't fail from this point onwards.
4394 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
4398 bpf_prog_put(old_prog);
4400 if (!was_opened && reset) /* change RQ type according to priv->xdp_prog */
4401 mlx5e_set_rq_type(priv->mdev, &priv->channels.params);
4403 if (!was_opened || reset)
4406 /* exchanging programs w/o reset, we update ref counts on behalf
4407 * of the channels RQs here.
4409 for (i = 0; i < priv->channels.num; i++) {
4410 struct mlx5e_channel *c = priv->channels.c[i];
4411 bool xsk_open = test_bit(MLX5E_CHANNEL_STATE_XSK, c->state);
4413 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4415 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->xskrq.state);
4416 napi_synchronize(&c->napi);
4417 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
4419 old_prog = xchg(&c->rq.xdp_prog, prog);
4421 bpf_prog_put(old_prog);
4424 old_prog = xchg(&c->xskrq.xdp_prog, prog);
4426 bpf_prog_put(old_prog);
4429 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4431 set_bit(MLX5E_RQ_STATE_ENABLED, &c->xskrq.state);
4432 /* napi_schedule in case we have missed anything */
4433 napi_schedule(&c->napi);
4437 mutex_unlock(&priv->state_lock);
4441 static u32 mlx5e_xdp_query(struct net_device *dev)
4443 struct mlx5e_priv *priv = netdev_priv(dev);
4444 const struct bpf_prog *xdp_prog;
4447 mutex_lock(&priv->state_lock);
4448 xdp_prog = priv->channels.params.xdp_prog;
4450 prog_id = xdp_prog->aux->id;
4451 mutex_unlock(&priv->state_lock);
4456 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4458 switch (xdp->command) {
4459 case XDP_SETUP_PROG:
4460 return mlx5e_xdp_set(dev, xdp->prog);
4461 case XDP_QUERY_PROG:
4462 xdp->prog_id = mlx5e_xdp_query(dev);
4464 case XDP_SETUP_XSK_UMEM:
4465 return mlx5e_xsk_setup_umem(dev, xdp->xsk.umem,
4472 #ifdef CONFIG_MLX5_ESWITCH
4473 static int mlx5e_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4474 struct net_device *dev, u32 filter_mask,
4477 struct mlx5e_priv *priv = netdev_priv(dev);
4478 struct mlx5_core_dev *mdev = priv->mdev;
4482 err = mlx5_eswitch_get_vepa(mdev->priv.eswitch, &setting);
4485 mode = setting ? BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB;
4486 return ndo_dflt_bridge_getlink(skb, pid, seq, dev,
4488 0, 0, nlflags, filter_mask, NULL);
4491 static int mlx5e_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
4492 u16 flags, struct netlink_ext_ack *extack)
4494 struct mlx5e_priv *priv = netdev_priv(dev);
4495 struct mlx5_core_dev *mdev = priv->mdev;
4496 struct nlattr *attr, *br_spec;
4497 u16 mode = BRIDGE_MODE_UNDEF;
4501 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4505 nla_for_each_nested(attr, br_spec, rem) {
4506 if (nla_type(attr) != IFLA_BRIDGE_MODE)
4509 if (nla_len(attr) < sizeof(mode))
4512 mode = nla_get_u16(attr);
4513 if (mode > BRIDGE_MODE_VEPA)
4519 if (mode == BRIDGE_MODE_UNDEF)
4522 setting = (mode == BRIDGE_MODE_VEPA) ? 1 : 0;
4523 return mlx5_eswitch_set_vepa(mdev->priv.eswitch, setting);
4527 const struct net_device_ops mlx5e_netdev_ops = {
4528 .ndo_open = mlx5e_open,
4529 .ndo_stop = mlx5e_close,
4530 .ndo_start_xmit = mlx5e_xmit,
4531 .ndo_setup_tc = mlx5e_setup_tc,
4532 .ndo_select_queue = mlx5e_select_queue,
4533 .ndo_get_stats64 = mlx5e_get_stats,
4534 .ndo_set_rx_mode = mlx5e_set_rx_mode,
4535 .ndo_set_mac_address = mlx5e_set_mac,
4536 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
4537 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
4538 .ndo_set_features = mlx5e_set_features,
4539 .ndo_fix_features = mlx5e_fix_features,
4540 .ndo_change_mtu = mlx5e_change_nic_mtu,
4541 .ndo_do_ioctl = mlx5e_ioctl,
4542 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
4543 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
4544 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
4545 .ndo_features_check = mlx5e_features_check,
4546 .ndo_tx_timeout = mlx5e_tx_timeout,
4547 .ndo_bpf = mlx5e_xdp,
4548 .ndo_xdp_xmit = mlx5e_xdp_xmit,
4549 .ndo_xsk_async_xmit = mlx5e_xsk_async_xmit,
4550 #ifdef CONFIG_MLX5_EN_ARFS
4551 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
4553 #ifdef CONFIG_MLX5_ESWITCH
4554 .ndo_bridge_setlink = mlx5e_bridge_setlink,
4555 .ndo_bridge_getlink = mlx5e_bridge_getlink,
4557 /* SRIOV E-Switch NDOs */
4558 .ndo_set_vf_mac = mlx5e_set_vf_mac,
4559 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
4560 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
4561 .ndo_set_vf_trust = mlx5e_set_vf_trust,
4562 .ndo_set_vf_rate = mlx5e_set_vf_rate,
4563 .ndo_get_vf_config = mlx5e_get_vf_config,
4564 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
4565 .ndo_get_vf_stats = mlx5e_get_vf_stats,
4569 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
4571 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
4573 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
4574 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
4575 !MLX5_CAP_ETH(mdev, csum_cap) ||
4576 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
4577 !MLX5_CAP_ETH(mdev, vlan_cap) ||
4578 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
4579 MLX5_CAP_FLOWTABLE(mdev,
4580 flow_table_properties_nic_receive.max_ft_level)
4582 mlx5_core_warn(mdev,
4583 "Not creating net device, some required device capabilities are missing\n");
4586 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
4587 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
4588 if (!MLX5_CAP_GEN(mdev, cq_moderation))
4589 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
4594 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
4599 for (i = 0; i < len; i++)
4600 indirection_rqt[i] = i % num_channels;
4603 static bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
4608 mlx5e_port_max_linkspeed(mdev, &link_speed);
4609 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
4610 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
4611 link_speed, pci_bw);
4613 #define MLX5E_SLOW_PCI_RATIO (2)
4615 return link_speed && pci_bw &&
4616 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
4619 static struct dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode)
4621 struct dim_cq_moder moder;
4623 moder.cq_period_mode = cq_period_mode;
4624 moder.pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
4625 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
4626 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4627 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
4632 static struct dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
4634 struct dim_cq_moder moder;
4636 moder.cq_period_mode = cq_period_mode;
4637 moder.pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
4638 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
4639 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4640 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
4645 static u8 mlx5_to_net_dim_cq_period_mode(u8 cq_period_mode)
4647 return cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE ?
4648 DIM_CQ_PERIOD_MODE_START_FROM_CQE :
4649 DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4652 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4654 if (params->tx_dim_enabled) {
4655 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4657 params->tx_cq_moderation = net_dim_get_def_tx_moderation(dim_period_mode);
4659 params->tx_cq_moderation = mlx5e_get_def_tx_moderation(cq_period_mode);
4662 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
4663 params->tx_cq_moderation.cq_period_mode ==
4664 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4667 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4669 if (params->rx_dim_enabled) {
4670 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4672 params->rx_cq_moderation = net_dim_get_def_rx_moderation(dim_period_mode);
4674 params->rx_cq_moderation = mlx5e_get_def_rx_moderation(cq_period_mode);
4677 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
4678 params->rx_cq_moderation.cq_period_mode ==
4679 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4682 static u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
4686 /* The supported periods are organized in ascending order */
4687 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
4688 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
4691 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
4694 void mlx5e_build_rq_params(struct mlx5_core_dev *mdev,
4695 struct mlx5e_params *params)
4697 /* Prefer Striding RQ, unless any of the following holds:
4698 * - Striding RQ configuration is not possible/supported.
4699 * - Slow PCI heuristic.
4700 * - Legacy RQ would use linear SKB while Striding RQ would use non-linear.
4702 * No XSK params: checking the availability of striding RQ in general.
4704 if (!slow_pci_heuristic(mdev) &&
4705 mlx5e_striding_rq_possible(mdev, params) &&
4706 (mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ||
4707 !mlx5e_rx_is_linear_skb(params, NULL)))
4708 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, true);
4709 mlx5e_set_rq_type(mdev, params);
4710 mlx5e_init_rq_type_params(mdev, params);
4713 void mlx5e_build_rss_params(struct mlx5e_rss_params *rss_params,
4716 enum mlx5e_traffic_types tt;
4718 rss_params->hfunc = ETH_RSS_HASH_TOP;
4719 netdev_rss_key_fill(rss_params->toeplitz_hash_key,
4720 sizeof(rss_params->toeplitz_hash_key));
4721 mlx5e_build_default_indir_rqt(rss_params->indirection_rqt,
4722 MLX5E_INDIR_RQT_SIZE, num_channels);
4723 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
4724 rss_params->rx_hash_fields[tt] =
4725 tirc_default_config[tt].rx_hash_fields;
4728 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
4729 struct mlx5e_xsk *xsk,
4730 struct mlx5e_rss_params *rss_params,
4731 struct mlx5e_params *params,
4732 u16 max_channels, u16 mtu)
4734 u8 rx_cq_period_mode;
4736 params->sw_mtu = mtu;
4737 params->hard_mtu = MLX5E_ETH_HARD_MTU;
4738 params->num_channels = max_channels;
4742 params->log_sq_size = is_kdump_kernel() ?
4743 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
4744 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
4747 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE,
4748 MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe));
4750 /* set CQE compression */
4751 params->rx_cqe_compress_def = false;
4752 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
4753 MLX5_CAP_GEN(mdev, vport_group_manager))
4754 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
4756 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4757 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE, false);
4760 mlx5e_build_rq_params(mdev, params);
4764 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4765 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
4766 /* No XSK params: checking the availability of striding RQ in general. */
4767 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL))
4768 params->lro_en = !slow_pci_heuristic(mdev);
4770 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4772 /* CQ moderation params */
4773 rx_cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4774 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4775 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4776 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4777 params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4778 mlx5e_set_rx_cq_mode_params(params, rx_cq_period_mode);
4779 mlx5e_set_tx_cq_mode_params(params, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
4782 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
4785 mlx5e_build_rss_params(rss_params, params->num_channels);
4786 params->tunneled_offload_en =
4787 mlx5e_tunnel_inner_ft_supported(mdev);
4793 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4795 struct mlx5e_priv *priv = netdev_priv(netdev);
4797 mlx5_query_mac_address(priv->mdev, netdev->dev_addr);
4798 if (is_zero_ether_addr(netdev->dev_addr) &&
4799 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4800 eth_hw_addr_random(netdev);
4801 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4805 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4807 struct mlx5e_priv *priv = netdev_priv(netdev);
4808 struct mlx5_core_dev *mdev = priv->mdev;
4812 SET_NETDEV_DEV(netdev, mdev->device);
4814 netdev->netdev_ops = &mlx5e_netdev_ops;
4816 #ifdef CONFIG_MLX5_CORE_EN_DCB
4817 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4818 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4821 netdev->watchdog_timeo = 15 * HZ;
4823 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4825 netdev->vlan_features |= NETIF_F_SG;
4826 netdev->vlan_features |= NETIF_F_HW_CSUM;
4827 netdev->vlan_features |= NETIF_F_GRO;
4828 netdev->vlan_features |= NETIF_F_TSO;
4829 netdev->vlan_features |= NETIF_F_TSO6;
4830 netdev->vlan_features |= NETIF_F_RXCSUM;
4831 netdev->vlan_features |= NETIF_F_RXHASH;
4833 netdev->mpls_features |= NETIF_F_SG;
4834 netdev->mpls_features |= NETIF_F_HW_CSUM;
4835 netdev->mpls_features |= NETIF_F_TSO;
4836 netdev->mpls_features |= NETIF_F_TSO6;
4838 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
4839 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
4841 if (!!MLX5_CAP_ETH(mdev, lro_cap) &&
4842 mlx5e_check_fragmented_striding_rq_cap(mdev))
4843 netdev->vlan_features |= NETIF_F_LRO;
4845 netdev->hw_features = netdev->vlan_features;
4846 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4847 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4848 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4849 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4851 if (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev) ||
4852 MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4853 netdev->hw_enc_features |= NETIF_F_HW_CSUM;
4854 netdev->hw_enc_features |= NETIF_F_TSO;
4855 netdev->hw_enc_features |= NETIF_F_TSO6;
4856 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4859 if (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev)) {
4860 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4861 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4862 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4863 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4864 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4867 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4868 netdev->hw_features |= NETIF_F_GSO_GRE |
4869 NETIF_F_GSO_GRE_CSUM;
4870 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4871 NETIF_F_GSO_GRE_CSUM;
4872 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4873 NETIF_F_GSO_GRE_CSUM;
4876 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4877 netdev->gso_partial_features |= NETIF_F_GSO_UDP_L4;
4878 netdev->hw_features |= NETIF_F_GSO_UDP_L4;
4879 netdev->features |= NETIF_F_GSO_UDP_L4;
4881 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4884 netdev->hw_features |= NETIF_F_RXALL;
4886 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4887 netdev->hw_features |= NETIF_F_RXFCS;
4889 netdev->features = netdev->hw_features;
4890 if (!priv->channels.params.lro_en)
4891 netdev->features &= ~NETIF_F_LRO;
4894 netdev->features &= ~NETIF_F_RXALL;
4896 if (!priv->channels.params.scatter_fcs_en)
4897 netdev->features &= ~NETIF_F_RXFCS;
4899 /* prefere CQE compression over rxhash */
4900 if (MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS))
4901 netdev->features &= ~NETIF_F_RXHASH;
4903 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4904 if (FT_CAP(flow_modify_en) &&
4905 FT_CAP(modify_root) &&
4906 FT_CAP(identified_miss_table_mode) &&
4907 FT_CAP(flow_table_modify)) {
4908 #ifdef CONFIG_MLX5_ESWITCH
4909 netdev->hw_features |= NETIF_F_HW_TC;
4911 #ifdef CONFIG_MLX5_EN_ARFS
4912 netdev->hw_features |= NETIF_F_NTUPLE;
4916 netdev->features |= NETIF_F_HIGHDMA;
4917 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4919 netdev->priv_flags |= IFF_UNICAST_FLT;
4921 mlx5e_set_netdev_dev_addr(netdev);
4922 mlx5e_ipsec_build_netdev(priv);
4923 mlx5e_tls_build_netdev(priv);
4926 void mlx5e_create_q_counters(struct mlx5e_priv *priv)
4928 struct mlx5_core_dev *mdev = priv->mdev;
4931 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4933 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4934 priv->q_counter = 0;
4937 err = mlx5_core_alloc_q_counter(mdev, &priv->drop_rq_q_counter);
4939 mlx5_core_warn(mdev, "alloc drop RQ counter failed, %d\n", err);
4940 priv->drop_rq_q_counter = 0;
4944 void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
4946 if (priv->q_counter)
4947 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4949 if (priv->drop_rq_q_counter)
4950 mlx5_core_dealloc_q_counter(priv->mdev, priv->drop_rq_q_counter);
4953 static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
4954 struct net_device *netdev,
4955 const struct mlx5e_profile *profile,
4958 struct mlx5e_priv *priv = netdev_priv(netdev);
4959 struct mlx5e_rss_params *rss = &priv->rss_params;
4962 err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
4966 mlx5e_build_nic_params(mdev, &priv->xsk, rss, &priv->channels.params,
4967 mlx5e_get_netdev_max_channels(netdev),
4970 mlx5e_timestamp_init(priv);
4972 err = mlx5e_ipsec_init(priv);
4974 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
4975 err = mlx5e_tls_init(priv);
4977 mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
4978 mlx5e_build_nic_netdev(netdev);
4979 mlx5e_build_tc2txq_maps(priv);
4984 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
4986 mlx5e_tls_cleanup(priv);
4987 mlx5e_ipsec_cleanup(priv);
4988 mlx5e_netdev_cleanup(priv->netdev, priv);
4991 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
4993 struct mlx5_core_dev *mdev = priv->mdev;
4996 mlx5e_create_q_counters(priv);
4998 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
5000 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
5001 goto err_destroy_q_counters;
5004 err = mlx5e_create_indirect_rqt(priv);
5006 goto err_close_drop_rq;
5008 err = mlx5e_create_direct_rqts(priv, priv->direct_tir);
5010 goto err_destroy_indirect_rqts;
5012 err = mlx5e_create_indirect_tirs(priv, true);
5014 goto err_destroy_direct_rqts;
5016 err = mlx5e_create_direct_tirs(priv, priv->direct_tir);
5018 goto err_destroy_indirect_tirs;
5020 err = mlx5e_create_direct_rqts(priv, priv->xsk_tir);
5022 goto err_destroy_direct_tirs;
5024 err = mlx5e_create_direct_tirs(priv, priv->xsk_tir);
5026 goto err_destroy_xsk_rqts;
5028 err = mlx5e_create_flow_steering(priv);
5030 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
5031 goto err_destroy_xsk_tirs;
5034 err = mlx5e_tc_nic_init(priv);
5036 goto err_destroy_flow_steering;
5040 err_destroy_flow_steering:
5041 mlx5e_destroy_flow_steering(priv);
5042 err_destroy_xsk_tirs:
5043 mlx5e_destroy_direct_tirs(priv, priv->xsk_tir);
5044 err_destroy_xsk_rqts:
5045 mlx5e_destroy_direct_rqts(priv, priv->xsk_tir);
5046 err_destroy_direct_tirs:
5047 mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
5048 err_destroy_indirect_tirs:
5049 mlx5e_destroy_indirect_tirs(priv, true);
5050 err_destroy_direct_rqts:
5051 mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
5052 err_destroy_indirect_rqts:
5053 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
5055 mlx5e_close_drop_rq(&priv->drop_rq);
5056 err_destroy_q_counters:
5057 mlx5e_destroy_q_counters(priv);
5061 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
5063 mlx5e_tc_nic_cleanup(priv);
5064 mlx5e_destroy_flow_steering(priv);
5065 mlx5e_destroy_direct_tirs(priv, priv->xsk_tir);
5066 mlx5e_destroy_direct_rqts(priv, priv->xsk_tir);
5067 mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
5068 mlx5e_destroy_indirect_tirs(priv, true);
5069 mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
5070 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
5071 mlx5e_close_drop_rq(&priv->drop_rq);
5072 mlx5e_destroy_q_counters(priv);
5075 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
5079 err = mlx5e_create_tises(priv);
5081 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
5085 #ifdef CONFIG_MLX5_CORE_EN_DCB
5086 mlx5e_dcbnl_initialize(priv);
5088 mlx5e_tx_reporter_create(priv);
5092 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
5094 struct net_device *netdev = priv->netdev;
5095 struct mlx5_core_dev *mdev = priv->mdev;
5097 mlx5e_init_l2_addr(priv);
5099 /* Marking the link as currently not needed by the Driver */
5100 if (!netif_running(netdev))
5101 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
5103 mlx5e_set_netdev_mtu_boundaries(priv);
5104 mlx5e_set_dev_port_mtu(priv);
5106 mlx5_lag_add(mdev, netdev);
5108 mlx5e_enable_async_events(priv);
5109 if (mlx5e_monitor_counter_supported(priv))
5110 mlx5e_monitor_counter_init(priv);
5112 if (netdev->reg_state != NETREG_REGISTERED)
5114 #ifdef CONFIG_MLX5_CORE_EN_DCB
5115 mlx5e_dcbnl_init_app(priv);
5118 queue_work(priv->wq, &priv->set_rx_mode_work);
5121 if (netif_running(netdev))
5123 netif_device_attach(netdev);
5127 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
5129 struct mlx5_core_dev *mdev = priv->mdev;
5131 #ifdef CONFIG_MLX5_CORE_EN_DCB
5132 if (priv->netdev->reg_state == NETREG_REGISTERED)
5133 mlx5e_dcbnl_delete_app(priv);
5137 if (netif_running(priv->netdev))
5138 mlx5e_close(priv->netdev);
5139 netif_device_detach(priv->netdev);
5142 queue_work(priv->wq, &priv->set_rx_mode_work);
5144 if (mlx5e_monitor_counter_supported(priv))
5145 mlx5e_monitor_counter_cleanup(priv);
5147 mlx5e_disable_async_events(priv);
5148 mlx5_lag_remove(mdev);
5151 int mlx5e_update_nic_rx(struct mlx5e_priv *priv)
5153 return mlx5e_refresh_tirs(priv, false);
5156 static const struct mlx5e_profile mlx5e_nic_profile = {
5157 .init = mlx5e_nic_init,
5158 .cleanup = mlx5e_nic_cleanup,
5159 .init_rx = mlx5e_init_nic_rx,
5160 .cleanup_rx = mlx5e_cleanup_nic_rx,
5161 .init_tx = mlx5e_init_nic_tx,
5162 .cleanup_tx = mlx5e_cleanup_nic_tx,
5163 .enable = mlx5e_nic_enable,
5164 .disable = mlx5e_nic_disable,
5165 .update_rx = mlx5e_update_nic_rx,
5166 .update_stats = mlx5e_update_ndo_stats,
5167 .update_carrier = mlx5e_update_carrier,
5168 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
5169 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
5170 .max_tc = MLX5E_MAX_NUM_TC,
5173 /* mlx5e generic netdev management API (move to en_common.c) */
5175 /* mlx5e_netdev_init/cleanup must be called from profile->init/cleanup callbacks */
5176 int mlx5e_netdev_init(struct net_device *netdev,
5177 struct mlx5e_priv *priv,
5178 struct mlx5_core_dev *mdev,
5179 const struct mlx5e_profile *profile,
5184 priv->netdev = netdev;
5185 priv->profile = profile;
5186 priv->ppriv = ppriv;
5187 priv->msglevel = MLX5E_MSG_LEVEL;
5188 priv->max_opened_tc = 1;
5190 mutex_init(&priv->state_lock);
5191 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
5192 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
5193 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
5194 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
5196 priv->wq = create_singlethread_workqueue("mlx5e");
5201 netif_carrier_off(netdev);
5203 #ifdef CONFIG_MLX5_EN_ARFS
5204 netdev->rx_cpu_rmap = mlx5_eq_table_get_rmap(mdev);
5210 void mlx5e_netdev_cleanup(struct net_device *netdev, struct mlx5e_priv *priv)
5212 destroy_workqueue(priv->wq);
5215 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
5216 const struct mlx5e_profile *profile,
5220 struct net_device *netdev;
5223 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
5224 nch * profile->max_tc,
5225 nch * MLX5E_NUM_RQ_GROUPS);
5227 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
5231 err = profile->init(mdev, netdev, profile, ppriv);
5233 mlx5_core_err(mdev, "failed to init mlx5e profile %d\n", err);
5234 goto err_free_netdev;
5240 free_netdev(netdev);
5245 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
5247 const struct mlx5e_profile *profile;
5251 profile = priv->profile;
5252 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
5254 /* max number of channels may have changed */
5255 max_nch = mlx5e_get_max_num_channels(priv->mdev);
5256 if (priv->channels.params.num_channels > max_nch) {
5257 mlx5_core_warn(priv->mdev, "MLX5E: Reducing number of channels to %d\n", max_nch);
5258 priv->channels.params.num_channels = max_nch;
5259 mlx5e_build_default_indir_rqt(priv->rss_params.indirection_rqt,
5260 MLX5E_INDIR_RQT_SIZE, max_nch);
5263 err = profile->init_tx(priv);
5267 err = profile->init_rx(priv);
5269 goto err_cleanup_tx;
5271 if (profile->enable)
5272 profile->enable(priv);
5277 profile->cleanup_tx(priv);
5283 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
5285 const struct mlx5e_profile *profile = priv->profile;
5287 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
5289 if (profile->disable)
5290 profile->disable(priv);
5291 flush_workqueue(priv->wq);
5293 profile->cleanup_rx(priv);
5294 profile->cleanup_tx(priv);
5295 cancel_work_sync(&priv->update_stats_work);
5298 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
5300 const struct mlx5e_profile *profile = priv->profile;
5301 struct net_device *netdev = priv->netdev;
5303 if (profile->cleanup)
5304 profile->cleanup(priv);
5305 free_netdev(netdev);
5308 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
5309 * hardware contexts and to connect it to the current netdev.
5311 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
5313 struct mlx5e_priv *priv = vpriv;
5314 struct net_device *netdev = priv->netdev;
5317 if (netif_device_present(netdev))
5320 err = mlx5e_create_mdev_resources(mdev);
5324 err = mlx5e_attach_netdev(priv);
5326 mlx5e_destroy_mdev_resources(mdev);
5333 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
5335 struct mlx5e_priv *priv = vpriv;
5336 struct net_device *netdev = priv->netdev;
5338 #ifdef CONFIG_MLX5_ESWITCH
5339 if (MLX5_ESWITCH_MANAGER(mdev) && vpriv == mdev)
5343 if (!netif_device_present(netdev))
5346 mlx5e_detach_netdev(priv);
5347 mlx5e_destroy_mdev_resources(mdev);
5350 static void *mlx5e_add(struct mlx5_core_dev *mdev)
5352 struct net_device *netdev;
5357 err = mlx5e_check_required_hca_cap(mdev);
5361 #ifdef CONFIG_MLX5_ESWITCH
5362 if (MLX5_ESWITCH_MANAGER(mdev) &&
5363 mlx5_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
5364 mlx5e_rep_register_vport_reps(mdev);
5369 nch = mlx5e_get_max_num_channels(mdev);
5370 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, nch, NULL);
5372 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
5376 priv = netdev_priv(netdev);
5378 err = mlx5e_attach(mdev, priv);
5380 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
5381 goto err_destroy_netdev;
5384 err = register_netdev(netdev);
5386 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
5390 #ifdef CONFIG_MLX5_CORE_EN_DCB
5391 mlx5e_dcbnl_init_app(priv);
5396 mlx5e_detach(mdev, priv);
5398 mlx5e_destroy_netdev(priv);
5402 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
5404 struct mlx5e_priv *priv;
5406 #ifdef CONFIG_MLX5_ESWITCH
5407 if (MLX5_ESWITCH_MANAGER(mdev) && vpriv == mdev) {
5408 mlx5e_rep_unregister_vport_reps(mdev);
5413 #ifdef CONFIG_MLX5_CORE_EN_DCB
5414 mlx5e_dcbnl_delete_app(priv);
5416 unregister_netdev(priv->netdev);
5417 mlx5e_detach(mdev, vpriv);
5418 mlx5e_destroy_netdev(priv);
5421 static struct mlx5_interface mlx5e_interface = {
5423 .remove = mlx5e_remove,
5424 .attach = mlx5e_attach,
5425 .detach = mlx5e_detach,
5426 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
5429 void mlx5e_init(void)
5431 mlx5e_ipsec_build_inverse_table();
5432 mlx5e_build_ptys2ethtool_map();
5433 mlx5_register_interface(&mlx5e_interface);
5436 void mlx5e_cleanup(void)
5438 mlx5_unregister_interface(&mlx5e_interface);