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 <linux/bpf.h>
38 #include <net/page_pool.h>
43 #include "en_accel/ipsec.h"
44 #include "en_accel/ipsec_rxtx.h"
45 #include "en_accel/tls.h"
46 #include "accel/ipsec.h"
47 #include "accel/tls.h"
48 #include "lib/vxlan.h"
49 #include "lib/clock.h"
53 struct mlx5e_rq_param {
54 u32 rqc[MLX5_ST_SZ_DW(rqc)];
55 struct mlx5_wq_param wq;
56 struct mlx5e_rq_frags_info frags_info;
59 struct mlx5e_sq_param {
60 u32 sqc[MLX5_ST_SZ_DW(sqc)];
61 struct mlx5_wq_param wq;
64 struct mlx5e_cq_param {
65 u32 cqc[MLX5_ST_SZ_DW(cqc)];
66 struct mlx5_wq_param wq;
71 struct mlx5e_channel_param {
72 struct mlx5e_rq_param rq;
73 struct mlx5e_sq_param sq;
74 struct mlx5e_sq_param xdp_sq;
75 struct mlx5e_sq_param icosq;
76 struct mlx5e_cq_param rx_cq;
77 struct mlx5e_cq_param tx_cq;
78 struct mlx5e_cq_param icosq_cq;
81 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
83 bool striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) &&
84 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
85 MLX5_CAP_ETH(mdev, reg_umr_sq);
86 u16 max_wqe_sz_cap = MLX5_CAP_GEN(mdev, max_wqe_sz_sq);
87 bool inline_umr = MLX5E_UMR_WQE_INLINE_SZ <= max_wqe_sz_cap;
92 mlx5_core_warn(mdev, "Cannot support Striding RQ: UMR WQE size (%d) exceeds maximum supported (%d).\n",
93 (int)MLX5E_UMR_WQE_INLINE_SZ, max_wqe_sz_cap);
99 static u32 mlx5e_rx_get_linear_frag_sz(struct mlx5e_params *params)
101 u16 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
102 u16 linear_rq_headroom = params->xdp_prog ?
103 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
106 linear_rq_headroom += NET_IP_ALIGN;
108 frag_sz = MLX5_SKB_FRAG_SZ(linear_rq_headroom + hw_mtu);
110 if (params->xdp_prog && frag_sz < PAGE_SIZE)
116 static u8 mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5e_params *params)
118 u32 linear_frag_sz = mlx5e_rx_get_linear_frag_sz(params);
120 return MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(linear_frag_sz);
123 static bool mlx5e_rx_is_linear_skb(struct mlx5_core_dev *mdev,
124 struct mlx5e_params *params)
126 u32 frag_sz = mlx5e_rx_get_linear_frag_sz(params);
128 return !params->lro_en && frag_sz <= PAGE_SIZE;
131 static bool mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev *mdev,
132 struct mlx5e_params *params)
134 u32 frag_sz = mlx5e_rx_get_linear_frag_sz(params);
135 s8 signed_log_num_strides_param;
138 if (!mlx5e_rx_is_linear_skb(mdev, params))
141 if (MLX5_CAP_GEN(mdev, ext_stride_num_range))
144 log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(frag_sz);
145 signed_log_num_strides_param =
146 (s8)log_num_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE;
148 return signed_log_num_strides_param >= 0;
151 static u8 mlx5e_mpwqe_get_log_rq_size(struct mlx5e_params *params)
153 if (params->log_rq_mtu_frames <
154 mlx5e_mpwqe_log_pkts_per_wqe(params) + MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW)
155 return MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW;
157 return params->log_rq_mtu_frames - mlx5e_mpwqe_log_pkts_per_wqe(params);
160 static u8 mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev *mdev,
161 struct mlx5e_params *params)
163 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
164 return order_base_2(mlx5e_rx_get_linear_frag_sz(params));
166 return MLX5E_MPWQE_STRIDE_SZ(mdev,
167 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
170 static u8 mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev *mdev,
171 struct mlx5e_params *params)
173 return MLX5_MPWRQ_LOG_WQE_SZ -
174 mlx5e_mpwqe_get_log_stride_size(mdev, params);
177 static u16 mlx5e_get_rq_headroom(struct mlx5_core_dev *mdev,
178 struct mlx5e_params *params)
180 u16 linear_rq_headroom = params->xdp_prog ?
181 XDP_PACKET_HEADROOM : MLX5_RX_HEADROOM;
184 linear_rq_headroom += NET_IP_ALIGN;
186 is_linear_skb = (params->rq_wq_type == MLX5_WQ_TYPE_CYCLIC) ?
187 mlx5e_rx_is_linear_skb(mdev, params) :
188 mlx5e_rx_mpwqe_is_linear_skb(mdev, params);
190 return is_linear_skb ? linear_rq_headroom : 0;
193 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
194 struct mlx5e_params *params)
196 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
197 params->log_rq_mtu_frames = is_kdump_kernel() ?
198 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
199 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
201 mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
202 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
203 params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
204 BIT(mlx5e_mpwqe_get_log_rq_size(params)) :
205 BIT(params->log_rq_mtu_frames),
206 BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params)),
207 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
210 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
211 struct mlx5e_params *params)
213 return mlx5e_check_fragmented_striding_rq_cap(mdev) &&
214 !MLX5_IPSEC_DEV(mdev) &&
215 !(params->xdp_prog && !mlx5e_rx_mpwqe_is_linear_skb(mdev, params));
218 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
220 params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
221 MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
222 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
226 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
228 struct mlx5_core_dev *mdev = priv->mdev;
231 port_state = mlx5_query_vport_state(mdev,
232 MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT,
235 if (port_state == VPORT_STATE_UP) {
236 netdev_info(priv->netdev, "Link up\n");
237 netif_carrier_on(priv->netdev);
239 netdev_info(priv->netdev, "Link down\n");
240 netif_carrier_off(priv->netdev);
244 static void mlx5e_update_carrier_work(struct work_struct *work)
246 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
247 update_carrier_work);
249 mutex_lock(&priv->state_lock);
250 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
251 if (priv->profile->update_carrier)
252 priv->profile->update_carrier(priv);
253 mutex_unlock(&priv->state_lock);
256 void mlx5e_update_stats(struct mlx5e_priv *priv)
260 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
261 if (mlx5e_stats_grps[i].update_stats)
262 mlx5e_stats_grps[i].update_stats(priv);
265 static void mlx5e_update_ndo_stats(struct mlx5e_priv *priv)
269 for (i = mlx5e_num_stats_grps - 1; i >= 0; i--)
270 if (mlx5e_stats_grps[i].update_stats_mask &
271 MLX5E_NDO_UPDATE_STATS)
272 mlx5e_stats_grps[i].update_stats(priv);
275 static void mlx5e_update_stats_work(struct work_struct *work)
277 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
280 mutex_lock(&priv->state_lock);
281 priv->profile->update_stats(priv);
282 mutex_unlock(&priv->state_lock);
285 void mlx5e_queue_update_stats(struct mlx5e_priv *priv)
287 if (!priv->profile->update_stats)
290 if (unlikely(test_bit(MLX5E_STATE_DESTROYING, &priv->state)))
293 queue_work(priv->wq, &priv->update_stats_work);
296 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
297 enum mlx5_dev_event event, unsigned long param)
299 struct mlx5e_priv *priv = vpriv;
301 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
305 case MLX5_DEV_EVENT_PORT_UP:
306 case MLX5_DEV_EVENT_PORT_DOWN:
307 queue_work(priv->wq, &priv->update_carrier_work);
314 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
316 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
319 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
321 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
322 synchronize_irq(pci_irq_vector(priv->mdev->pdev, MLX5_EQ_VEC_ASYNC));
325 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
326 struct mlx5e_icosq *sq,
327 struct mlx5e_umr_wqe *wqe)
329 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
330 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
331 u8 ds_cnt = DIV_ROUND_UP(MLX5E_UMR_WQE_INLINE_SZ, MLX5_SEND_WQE_DS);
333 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
335 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
336 cseg->imm = rq->mkey_be;
338 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
339 ucseg->xlt_octowords =
340 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
341 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
344 static u32 mlx5e_rqwq_get_size(struct mlx5e_rq *rq)
346 switch (rq->wq_type) {
347 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
348 return mlx5_wq_ll_get_size(&rq->mpwqe.wq);
350 return mlx5_wq_cyc_get_size(&rq->wqe.wq);
354 static u32 mlx5e_rqwq_get_cur_sz(struct mlx5e_rq *rq)
356 switch (rq->wq_type) {
357 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
358 return rq->mpwqe.wq.cur_sz;
360 return rq->wqe.wq.cur_sz;
364 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
365 struct mlx5e_channel *c)
367 int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
369 rq->mpwqe.info = kvzalloc_node(array_size(wq_sz,
370 sizeof(*rq->mpwqe.info)),
371 GFP_KERNEL, cpu_to_node(c->cpu));
375 mlx5e_build_umr_wqe(rq, &c->icosq, &rq->mpwqe.umr_wqe);
380 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev,
381 u64 npages, u8 page_shift,
382 struct mlx5_core_mkey *umr_mkey)
384 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
389 in = kvzalloc(inlen, GFP_KERNEL);
393 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
395 MLX5_SET(mkc, mkc, free, 1);
396 MLX5_SET(mkc, mkc, umr_en, 1);
397 MLX5_SET(mkc, mkc, lw, 1);
398 MLX5_SET(mkc, mkc, lr, 1);
399 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
401 MLX5_SET(mkc, mkc, qpn, 0xffffff);
402 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
403 MLX5_SET64(mkc, mkc, len, npages << page_shift);
404 MLX5_SET(mkc, mkc, translations_octword_size,
405 MLX5_MTT_OCTW(npages));
406 MLX5_SET(mkc, mkc, log_page_size, page_shift);
408 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen);
414 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq)
416 u64 num_mtts = MLX5E_REQUIRED_MTTS(mlx5_wq_ll_get_size(&rq->mpwqe.wq));
418 return mlx5e_create_umr_mkey(mdev, num_mtts, PAGE_SHIFT, &rq->umr_mkey);
421 static inline u64 mlx5e_get_mpwqe_offset(struct mlx5e_rq *rq, u16 wqe_ix)
423 return (wqe_ix << MLX5E_LOG_ALIGNED_MPWQE_PPW) << PAGE_SHIFT;
426 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq)
428 struct mlx5e_wqe_frag_info next_frag, *prev;
431 next_frag.di = &rq->wqe.di[0];
432 next_frag.offset = 0;
435 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) {
436 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
437 struct mlx5e_wqe_frag_info *frag =
438 &rq->wqe.frags[i << rq->wqe.info.log_num_frags];
441 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) {
442 if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) {
444 next_frag.offset = 0;
446 prev->last_in_page = true;
451 next_frag.offset += frag_info[f].frag_stride;
457 prev->last_in_page = true;
460 static int mlx5e_init_di_list(struct mlx5e_rq *rq,
461 struct mlx5e_params *params,
464 int len = wq_sz << rq->wqe.info.log_num_frags;
466 rq->wqe.di = kvzalloc_node(array_size(len, sizeof(*rq->wqe.di)),
467 GFP_KERNEL, cpu_to_node(cpu));
471 mlx5e_init_frags_partition(rq);
476 static void mlx5e_free_di_list(struct mlx5e_rq *rq)
481 static int mlx5e_alloc_rq(struct mlx5e_channel *c,
482 struct mlx5e_params *params,
483 struct mlx5e_rq_param *rqp,
486 struct page_pool_params pp_params = { 0 };
487 struct mlx5_core_dev *mdev = c->mdev;
488 void *rqc = rqp->rqc;
489 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
495 rqp->wq.db_numa_node = cpu_to_node(c->cpu);
497 rq->wq_type = params->rq_wq_type;
499 rq->netdev = c->netdev;
500 rq->tstamp = c->tstamp;
501 rq->clock = &mdev->clock;
505 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
506 rq->stats = &c->priv->channel_stats[c->ix].rq;
508 rq->xdp_prog = params->xdp_prog ? bpf_prog_inc(params->xdp_prog) : NULL;
509 if (IS_ERR(rq->xdp_prog)) {
510 err = PTR_ERR(rq->xdp_prog);
512 goto err_rq_wq_destroy;
515 err = xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix);
517 goto err_rq_wq_destroy;
519 rq->buff.map_dir = rq->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
520 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params);
521 pool_size = 1 << params->log_rq_mtu_frames;
523 switch (rq->wq_type) {
524 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
525 err = mlx5_wq_ll_create(mdev, &rqp->wq, rqc_wq, &rq->mpwqe.wq,
530 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR];
532 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq);
534 pool_size = MLX5_MPWRQ_PAGES_PER_WQE << mlx5e_mpwqe_get_log_rq_size(params);
536 rq->post_wqes = mlx5e_post_rx_mpwqes;
537 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
539 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe_mpwqe;
540 #ifdef CONFIG_MLX5_EN_IPSEC
541 if (MLX5_IPSEC_DEV(mdev)) {
543 netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
544 goto err_rq_wq_destroy;
547 if (!rq->handle_rx_cqe) {
549 netdev_err(c->netdev, "RX handler of MPWQE RQ is not set, err %d\n", err);
550 goto err_rq_wq_destroy;
553 rq->mpwqe.skb_from_cqe_mpwrq =
554 mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ?
555 mlx5e_skb_from_cqe_mpwrq_linear :
556 mlx5e_skb_from_cqe_mpwrq_nonlinear;
557 rq->mpwqe.log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params);
558 rq->mpwqe.num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params));
560 err = mlx5e_create_rq_umr_mkey(mdev, rq);
562 goto err_rq_wq_destroy;
563 rq->mkey_be = cpu_to_be32(rq->umr_mkey.key);
565 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
569 default: /* MLX5_WQ_TYPE_CYCLIC */
570 err = mlx5_wq_cyc_create(mdev, &rqp->wq, rqc_wq, &rq->wqe.wq,
575 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR];
577 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq);
579 rq->wqe.info = rqp->frags_info;
581 kvzalloc_node(array_size(sizeof(*rq->wqe.frags),
582 (wq_sz << rq->wqe.info.log_num_frags)),
583 GFP_KERNEL, cpu_to_node(c->cpu));
584 if (!rq->wqe.frags) {
589 err = mlx5e_init_di_list(rq, params, wq_sz, c->cpu);
592 rq->post_wqes = mlx5e_post_rx_wqes;
593 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
595 #ifdef CONFIG_MLX5_EN_IPSEC
597 rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
600 rq->handle_rx_cqe = c->priv->profile->rx_handlers.handle_rx_cqe;
601 if (!rq->handle_rx_cqe) {
603 netdev_err(c->netdev, "RX handler of RQ is not set, err %d\n", err);
607 rq->wqe.skb_from_cqe = mlx5e_rx_is_linear_skb(mdev, params) ?
608 mlx5e_skb_from_cqe_linear :
609 mlx5e_skb_from_cqe_nonlinear;
610 rq->mkey_be = c->mkey_be;
613 /* Create a page_pool and register it with rxq */
615 pp_params.flags = 0; /* No-internal DMA mapping in page_pool */
616 pp_params.pool_size = pool_size;
617 pp_params.nid = cpu_to_node(c->cpu);
618 pp_params.dev = c->pdev;
619 pp_params.dma_dir = rq->buff.map_dir;
621 /* page_pool can be used even when there is no rq->xdp_prog,
622 * given page_pool does not handle DMA mapping there is no
623 * required state to clear. And page_pool gracefully handle
626 rq->page_pool = page_pool_create(&pp_params);
627 if (IS_ERR(rq->page_pool)) {
628 err = PTR_ERR(rq->page_pool);
629 rq->page_pool = NULL;
632 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
633 MEM_TYPE_PAGE_POOL, rq->page_pool);
637 for (i = 0; i < wq_sz; i++) {
638 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
639 struct mlx5e_rx_wqe_ll *wqe =
640 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i);
642 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz;
643 u64 dma_offset = mlx5e_get_mpwqe_offset(rq, i);
645 wqe->data[0].addr = cpu_to_be64(dma_offset + rq->buff.headroom);
646 wqe->data[0].byte_count = cpu_to_be32(byte_count);
647 wqe->data[0].lkey = rq->mkey_be;
649 struct mlx5e_rx_wqe_cyc *wqe =
650 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i);
653 for (f = 0; f < rq->wqe.info.num_frags; f++) {
654 u32 frag_size = rq->wqe.info.arr[f].frag_size |
655 MLX5_HW_START_PADDING;
657 wqe->data[f].byte_count = cpu_to_be32(frag_size);
658 wqe->data[f].lkey = rq->mkey_be;
660 /* check if num_frags is not a pow of two */
661 if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) {
662 wqe->data[f].byte_count = 0;
663 wqe->data[f].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
664 wqe->data[f].addr = 0;
669 INIT_WORK(&rq->dim.work, mlx5e_rx_dim_work);
671 switch (params->rx_cq_moderation.cq_period_mode) {
672 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
673 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
675 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
677 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
680 rq->page_cache.head = 0;
681 rq->page_cache.tail = 0;
686 switch (rq->wq_type) {
687 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
688 kvfree(rq->mpwqe.info);
689 mlx5_core_destroy_mkey(mdev, &rq->umr_mkey);
691 default: /* MLX5_WQ_TYPE_CYCLIC */
692 kvfree(rq->wqe.frags);
693 mlx5e_free_di_list(rq);
698 bpf_prog_put(rq->xdp_prog);
699 xdp_rxq_info_unreg(&rq->xdp_rxq);
701 page_pool_destroy(rq->page_pool);
702 mlx5_wq_destroy(&rq->wq_ctrl);
707 static void mlx5e_free_rq(struct mlx5e_rq *rq)
712 bpf_prog_put(rq->xdp_prog);
714 xdp_rxq_info_unreg(&rq->xdp_rxq);
716 page_pool_destroy(rq->page_pool);
718 switch (rq->wq_type) {
719 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
720 kvfree(rq->mpwqe.info);
721 mlx5_core_destroy_mkey(rq->mdev, &rq->umr_mkey);
723 default: /* MLX5_WQ_TYPE_CYCLIC */
724 kvfree(rq->wqe.frags);
725 mlx5e_free_di_list(rq);
728 for (i = rq->page_cache.head; i != rq->page_cache.tail;
729 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
730 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
732 mlx5e_page_release(rq, dma_info, false);
734 mlx5_wq_destroy(&rq->wq_ctrl);
737 static int mlx5e_create_rq(struct mlx5e_rq *rq,
738 struct mlx5e_rq_param *param)
740 struct mlx5_core_dev *mdev = rq->mdev;
748 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
749 sizeof(u64) * rq->wq_ctrl.buf.npages;
750 in = kvzalloc(inlen, GFP_KERNEL);
754 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
755 wq = MLX5_ADDR_OF(rqc, rqc, wq);
757 memcpy(rqc, param->rqc, sizeof(param->rqc));
759 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
760 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
761 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
762 MLX5_ADAPTER_PAGE_SHIFT);
763 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
765 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf,
766 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
768 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
775 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
778 struct mlx5_core_dev *mdev = rq->mdev;
785 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
786 in = kvzalloc(inlen, GFP_KERNEL);
790 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
792 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
793 MLX5_SET(rqc, rqc, state, next_state);
795 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
802 static int mlx5e_modify_rq_scatter_fcs(struct mlx5e_rq *rq, bool enable)
804 struct mlx5e_channel *c = rq->channel;
805 struct mlx5e_priv *priv = c->priv;
806 struct mlx5_core_dev *mdev = priv->mdev;
813 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
814 in = kvzalloc(inlen, GFP_KERNEL);
818 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
820 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
821 MLX5_SET64(modify_rq_in, in, modify_bitmask,
822 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS);
823 MLX5_SET(rqc, rqc, scatter_fcs, enable);
824 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
826 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
833 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
835 struct mlx5e_channel *c = rq->channel;
836 struct mlx5_core_dev *mdev = c->mdev;
842 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
843 in = kvzalloc(inlen, GFP_KERNEL);
847 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
849 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
850 MLX5_SET64(modify_rq_in, in, modify_bitmask,
851 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
852 MLX5_SET(rqc, rqc, vsd, vsd);
853 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
855 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
862 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
864 mlx5_core_destroy_rq(rq->mdev, rq->rqn);
867 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time)
869 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time);
870 struct mlx5e_channel *c = rq->channel;
872 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq));
875 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes)
879 } while (time_before(jiffies, exp_time));
881 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",
882 c->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes);
887 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
892 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
893 struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
895 /* UMR WQE (if in progress) is always at wq->head */
896 if (rq->mpwqe.umr_in_progress)
897 rq->dealloc_wqe(rq, wq->head);
899 while (!mlx5_wq_ll_is_empty(wq)) {
900 struct mlx5e_rx_wqe_ll *wqe;
902 wqe_ix_be = *wq->tail_next;
903 wqe_ix = be16_to_cpu(wqe_ix_be);
904 wqe = mlx5_wq_ll_get_wqe(wq, wqe_ix);
905 rq->dealloc_wqe(rq, wqe_ix);
906 mlx5_wq_ll_pop(wq, wqe_ix_be,
907 &wqe->next.next_wqe_index);
910 struct mlx5_wq_cyc *wq = &rq->wqe.wq;
912 while (!mlx5_wq_cyc_is_empty(wq)) {
913 wqe_ix = mlx5_wq_cyc_get_tail(wq);
914 rq->dealloc_wqe(rq, wqe_ix);
921 static int mlx5e_open_rq(struct mlx5e_channel *c,
922 struct mlx5e_params *params,
923 struct mlx5e_rq_param *param,
928 err = mlx5e_alloc_rq(c, params, param, rq);
932 err = mlx5e_create_rq(rq, param);
936 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
940 if (params->rx_dim_enabled)
941 __set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
943 if (params->pflags & MLX5E_PFLAG_RX_NO_CSUM_COMPLETE)
944 __set_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &c->rq.state);
949 mlx5e_destroy_rq(rq);
956 static void mlx5e_activate_rq(struct mlx5e_rq *rq)
958 struct mlx5e_icosq *sq = &rq->channel->icosq;
959 struct mlx5_wq_cyc *wq = &sq->wq;
960 struct mlx5e_tx_wqe *nopwqe;
962 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
964 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
965 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_NOP;
966 nopwqe = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
967 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nopwqe->ctrl);
970 static void mlx5e_deactivate_rq(struct mlx5e_rq *rq)
972 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state);
973 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
976 static void mlx5e_close_rq(struct mlx5e_rq *rq)
978 cancel_work_sync(&rq->dim.work);
979 mlx5e_destroy_rq(rq);
980 mlx5e_free_rx_descs(rq);
984 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq)
989 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa)
991 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
993 sq->db.xdpi = kvzalloc_node(array_size(wq_sz, sizeof(*sq->db.xdpi)),
996 mlx5e_free_xdpsq_db(sq);
1003 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c,
1004 struct mlx5e_params *params,
1005 struct mlx5e_sq_param *param,
1006 struct mlx5e_xdpsq *sq,
1009 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1010 struct mlx5_core_dev *mdev = c->mdev;
1011 struct mlx5_wq_cyc *wq = &sq->wq;
1015 sq->mkey_be = c->mkey_be;
1017 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1018 sq->min_inline_mode = params->tx_min_inline_mode;
1019 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
1020 sq->stats = is_redirect ?
1021 &c->priv->channel_stats[c->ix].xdpsq :
1022 &c->priv->channel_stats[c->ix].rq_xdpsq;
1024 param->wq.db_numa_node = cpu_to_node(c->cpu);
1025 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1028 wq->db = &wq->db[MLX5_SND_DBR];
1030 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu));
1032 goto err_sq_wq_destroy;
1037 mlx5_wq_destroy(&sq->wq_ctrl);
1042 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq)
1044 mlx5e_free_xdpsq_db(sq);
1045 mlx5_wq_destroy(&sq->wq_ctrl);
1048 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq)
1050 kvfree(sq->db.ico_wqe);
1053 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa)
1055 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1057 sq->db.ico_wqe = kvzalloc_node(array_size(wq_sz,
1058 sizeof(*sq->db.ico_wqe)),
1060 if (!sq->db.ico_wqe)
1066 static int mlx5e_alloc_icosq(struct mlx5e_channel *c,
1067 struct mlx5e_sq_param *param,
1068 struct mlx5e_icosq *sq)
1070 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1071 struct mlx5_core_dev *mdev = c->mdev;
1072 struct mlx5_wq_cyc *wq = &sq->wq;
1076 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1078 param->wq.db_numa_node = cpu_to_node(c->cpu);
1079 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1082 wq->db = &wq->db[MLX5_SND_DBR];
1084 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu));
1086 goto err_sq_wq_destroy;
1091 mlx5_wq_destroy(&sq->wq_ctrl);
1096 static void mlx5e_free_icosq(struct mlx5e_icosq *sq)
1098 mlx5e_free_icosq_db(sq);
1099 mlx5_wq_destroy(&sq->wq_ctrl);
1102 static void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq)
1104 kvfree(sq->db.wqe_info);
1105 kvfree(sq->db.dma_fifo);
1108 static int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa)
1110 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1111 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
1113 sq->db.dma_fifo = kvzalloc_node(array_size(df_sz,
1114 sizeof(*sq->db.dma_fifo)),
1116 sq->db.wqe_info = kvzalloc_node(array_size(wq_sz,
1117 sizeof(*sq->db.wqe_info)),
1119 if (!sq->db.dma_fifo || !sq->db.wqe_info) {
1120 mlx5e_free_txqsq_db(sq);
1124 sq->dma_fifo_mask = df_sz - 1;
1129 static void mlx5e_sq_recover(struct work_struct *work);
1130 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
1132 struct mlx5e_params *params,
1133 struct mlx5e_sq_param *param,
1134 struct mlx5e_txqsq *sq,
1137 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq);
1138 struct mlx5_core_dev *mdev = c->mdev;
1139 struct mlx5_wq_cyc *wq = &sq->wq;
1143 sq->tstamp = c->tstamp;
1144 sq->clock = &mdev->clock;
1145 sq->mkey_be = c->mkey_be;
1147 sq->txq_ix = txq_ix;
1148 sq->uar_map = mdev->mlx5e_res.bfreg.map;
1149 sq->min_inline_mode = params->tx_min_inline_mode;
1150 sq->stats = &c->priv->channel_stats[c->ix].sq[tc];
1151 INIT_WORK(&sq->recover.recover_work, mlx5e_sq_recover);
1152 if (MLX5_IPSEC_DEV(c->priv->mdev))
1153 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state);
1154 if (mlx5_accel_is_tls_device(c->priv->mdev))
1155 set_bit(MLX5E_SQ_STATE_TLS, &sq->state);
1157 param->wq.db_numa_node = cpu_to_node(c->cpu);
1158 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl);
1161 wq->db = &wq->db[MLX5_SND_DBR];
1163 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu));
1165 goto err_sq_wq_destroy;
1167 INIT_WORK(&sq->dim.work, mlx5e_tx_dim_work);
1168 sq->dim.mode = params->tx_cq_moderation.cq_period_mode;
1173 mlx5_wq_destroy(&sq->wq_ctrl);
1178 static void mlx5e_free_txqsq(struct mlx5e_txqsq *sq)
1180 mlx5e_free_txqsq_db(sq);
1181 mlx5_wq_destroy(&sq->wq_ctrl);
1184 struct mlx5e_create_sq_param {
1185 struct mlx5_wq_ctrl *wq_ctrl;
1192 static int mlx5e_create_sq(struct mlx5_core_dev *mdev,
1193 struct mlx5e_sq_param *param,
1194 struct mlx5e_create_sq_param *csp,
1203 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1204 sizeof(u64) * csp->wq_ctrl->buf.npages;
1205 in = kvzalloc(inlen, GFP_KERNEL);
1209 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1210 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1212 memcpy(sqc, param->sqc, sizeof(param->sqc));
1213 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz);
1214 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn);
1215 MLX5_SET(sqc, sqc, cqn, csp->cqn);
1217 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
1218 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode);
1220 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1221 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1223 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1224 MLX5_SET(wq, wq, uar_page, mdev->mlx5e_res.bfreg.index);
1225 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift -
1226 MLX5_ADAPTER_PAGE_SHIFT);
1227 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma);
1229 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf,
1230 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
1232 err = mlx5_core_create_sq(mdev, in, inlen, sqn);
1239 struct mlx5e_modify_sq_param {
1246 static int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn,
1247 struct mlx5e_modify_sq_param *p)
1254 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1255 in = kvzalloc(inlen, GFP_KERNEL);
1259 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1261 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state);
1262 MLX5_SET(sqc, sqc, state, p->next_state);
1263 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) {
1264 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
1265 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index);
1268 err = mlx5_core_modify_sq(mdev, sqn, in, inlen);
1275 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn)
1277 mlx5_core_destroy_sq(mdev, sqn);
1280 static int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev,
1281 struct mlx5e_sq_param *param,
1282 struct mlx5e_create_sq_param *csp,
1285 struct mlx5e_modify_sq_param msp = {0};
1288 err = mlx5e_create_sq(mdev, param, csp, sqn);
1292 msp.curr_state = MLX5_SQC_STATE_RST;
1293 msp.next_state = MLX5_SQC_STATE_RDY;
1294 err = mlx5e_modify_sq(mdev, *sqn, &msp);
1296 mlx5e_destroy_sq(mdev, *sqn);
1301 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1302 struct mlx5e_txqsq *sq, u32 rate);
1304 static int mlx5e_open_txqsq(struct mlx5e_channel *c,
1307 struct mlx5e_params *params,
1308 struct mlx5e_sq_param *param,
1309 struct mlx5e_txqsq *sq,
1312 struct mlx5e_create_sq_param csp = {};
1316 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc);
1322 csp.cqn = sq->cq.mcq.cqn;
1323 csp.wq_ctrl = &sq->wq_ctrl;
1324 csp.min_inline_mode = sq->min_inline_mode;
1325 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1327 goto err_free_txqsq;
1329 tx_rate = c->priv->tx_rates[sq->txq_ix];
1331 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate);
1333 if (params->tx_dim_enabled)
1334 sq->state |= BIT(MLX5E_SQ_STATE_AM);
1339 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1340 mlx5e_free_txqsq(sq);
1345 static void mlx5e_reset_txqsq_cc_pc(struct mlx5e_txqsq *sq)
1347 WARN_ONCE(sq->cc != sq->pc,
1348 "SQ 0x%x: cc (0x%x) != pc (0x%x)\n",
1349 sq->sqn, sq->cc, sq->pc);
1351 sq->dma_fifo_cc = 0;
1355 static void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq)
1357 sq->txq = netdev_get_tx_queue(sq->channel->netdev, sq->txq_ix);
1358 clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
1359 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1360 netdev_tx_reset_queue(sq->txq);
1361 netif_tx_start_queue(sq->txq);
1364 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
1366 __netif_tx_lock_bh(txq);
1367 netif_tx_stop_queue(txq);
1368 __netif_tx_unlock_bh(txq);
1371 static void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq)
1373 struct mlx5e_channel *c = sq->channel;
1374 struct mlx5_wq_cyc *wq = &sq->wq;
1376 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1377 /* prevent netif_tx_wake_queue */
1378 napi_synchronize(&c->napi);
1380 netif_tx_disable_queue(sq->txq);
1382 /* last doorbell out, godspeed .. */
1383 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) {
1384 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc);
1385 struct mlx5e_tx_wqe *nop;
1387 sq->db.wqe_info[pi].skb = NULL;
1388 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc);
1389 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl);
1393 static void mlx5e_close_txqsq(struct mlx5e_txqsq *sq)
1395 struct mlx5e_channel *c = sq->channel;
1396 struct mlx5_core_dev *mdev = c->mdev;
1397 struct mlx5_rate_limit rl = {0};
1399 mlx5e_destroy_sq(mdev, sq->sqn);
1400 if (sq->rate_limit) {
1401 rl.rate = sq->rate_limit;
1402 mlx5_rl_remove_rate(mdev, &rl);
1404 mlx5e_free_txqsq_descs(sq);
1405 mlx5e_free_txqsq(sq);
1408 static int mlx5e_wait_for_sq_flush(struct mlx5e_txqsq *sq)
1410 unsigned long exp_time = jiffies + msecs_to_jiffies(2000);
1412 while (time_before(jiffies, exp_time)) {
1413 if (sq->cc == sq->pc)
1419 netdev_err(sq->channel->netdev,
1420 "Wait for SQ 0x%x flush timeout (sq cc = 0x%x, sq pc = 0x%x)\n",
1421 sq->sqn, sq->cc, sq->pc);
1426 static int mlx5e_sq_to_ready(struct mlx5e_txqsq *sq, int curr_state)
1428 struct mlx5_core_dev *mdev = sq->channel->mdev;
1429 struct net_device *dev = sq->channel->netdev;
1430 struct mlx5e_modify_sq_param msp = {0};
1433 msp.curr_state = curr_state;
1434 msp.next_state = MLX5_SQC_STATE_RST;
1436 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1438 netdev_err(dev, "Failed to move sq 0x%x to reset\n", sq->sqn);
1442 memset(&msp, 0, sizeof(msp));
1443 msp.curr_state = MLX5_SQC_STATE_RST;
1444 msp.next_state = MLX5_SQC_STATE_RDY;
1446 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1448 netdev_err(dev, "Failed to move sq 0x%x to ready\n", sq->sqn);
1455 static void mlx5e_sq_recover(struct work_struct *work)
1457 struct mlx5e_txqsq_recover *recover =
1458 container_of(work, struct mlx5e_txqsq_recover,
1460 struct mlx5e_txqsq *sq = container_of(recover, struct mlx5e_txqsq,
1462 struct mlx5_core_dev *mdev = sq->channel->mdev;
1463 struct net_device *dev = sq->channel->netdev;
1467 err = mlx5_core_query_sq_state(mdev, sq->sqn, &state);
1469 netdev_err(dev, "Failed to query SQ 0x%x state. err = %d\n",
1474 if (state != MLX5_RQC_STATE_ERR) {
1475 netdev_err(dev, "SQ 0x%x not in ERROR state\n", sq->sqn);
1479 netif_tx_disable_queue(sq->txq);
1481 if (mlx5e_wait_for_sq_flush(sq))
1484 /* If the interval between two consecutive recovers per SQ is too
1485 * short, don't recover to avoid infinite loop of ERR_CQE -> recover.
1486 * If we reached this state, there is probably a bug that needs to be
1487 * fixed. let's keep the queue close and let tx timeout cleanup.
1489 if (jiffies_to_msecs(jiffies - recover->last_recover) <
1490 MLX5E_SQ_RECOVER_MIN_INTERVAL) {
1491 netdev_err(dev, "Recover SQ 0x%x canceled, too many error CQEs\n",
1496 /* At this point, no new packets will arrive from the stack as TXQ is
1497 * marked with QUEUE_STATE_DRV_XOFF. In addition, NAPI cleared all
1498 * pending WQEs. SQ can safely reset the SQ.
1500 if (mlx5e_sq_to_ready(sq, state))
1503 mlx5e_reset_txqsq_cc_pc(sq);
1504 sq->stats->recover++;
1505 recover->last_recover = jiffies;
1506 mlx5e_activate_txqsq(sq);
1509 static int mlx5e_open_icosq(struct mlx5e_channel *c,
1510 struct mlx5e_params *params,
1511 struct mlx5e_sq_param *param,
1512 struct mlx5e_icosq *sq)
1514 struct mlx5e_create_sq_param csp = {};
1517 err = mlx5e_alloc_icosq(c, param, sq);
1521 csp.cqn = sq->cq.mcq.cqn;
1522 csp.wq_ctrl = &sq->wq_ctrl;
1523 csp.min_inline_mode = params->tx_min_inline_mode;
1524 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1525 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1527 goto err_free_icosq;
1532 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1533 mlx5e_free_icosq(sq);
1538 static void mlx5e_close_icosq(struct mlx5e_icosq *sq)
1540 struct mlx5e_channel *c = sq->channel;
1542 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1543 napi_synchronize(&c->napi);
1545 mlx5e_destroy_sq(c->mdev, sq->sqn);
1546 mlx5e_free_icosq(sq);
1549 static int mlx5e_open_xdpsq(struct mlx5e_channel *c,
1550 struct mlx5e_params *params,
1551 struct mlx5e_sq_param *param,
1552 struct mlx5e_xdpsq *sq,
1555 unsigned int ds_cnt = MLX5E_XDP_TX_DS_COUNT;
1556 struct mlx5e_create_sq_param csp = {};
1557 unsigned int inline_hdr_sz = 0;
1561 err = mlx5e_alloc_xdpsq(c, params, param, sq, is_redirect);
1566 csp.tisn = c->priv->tisn[0]; /* tc = 0 */
1567 csp.cqn = sq->cq.mcq.cqn;
1568 csp.wq_ctrl = &sq->wq_ctrl;
1569 csp.min_inline_mode = sq->min_inline_mode;
1571 set_bit(MLX5E_SQ_STATE_REDIRECT, &sq->state);
1572 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1573 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, &sq->sqn);
1575 goto err_free_xdpsq;
1577 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
1578 inline_hdr_sz = MLX5E_XDP_MIN_INLINE;
1582 /* Pre initialize fixed WQE fields */
1583 for (i = 0; i < mlx5_wq_cyc_get_size(&sq->wq); i++) {
1584 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(&sq->wq, i);
1585 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
1586 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
1587 struct mlx5_wqe_data_seg *dseg;
1589 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | ds_cnt);
1590 eseg->inline_hdr.sz = cpu_to_be16(inline_hdr_sz);
1592 dseg = (struct mlx5_wqe_data_seg *)cseg + (ds_cnt - 1);
1593 dseg->lkey = sq->mkey_be;
1599 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1600 mlx5e_free_xdpsq(sq);
1605 static void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq)
1607 struct mlx5e_channel *c = sq->channel;
1609 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
1610 napi_synchronize(&c->napi);
1612 mlx5e_destroy_sq(c->mdev, sq->sqn);
1613 mlx5e_free_xdpsq_descs(sq);
1614 mlx5e_free_xdpsq(sq);
1617 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev,
1618 struct mlx5e_cq_param *param,
1619 struct mlx5e_cq *cq)
1621 struct mlx5_core_cq *mcq = &cq->mcq;
1627 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1631 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1637 mcq->set_ci_db = cq->wq_ctrl.db.db;
1638 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1639 *mcq->set_ci_db = 0;
1641 mcq->vector = param->eq_ix;
1642 mcq->comp = mlx5e_completion_event;
1643 mcq->event = mlx5e_cq_error_event;
1646 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1647 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1657 static int mlx5e_alloc_cq(struct mlx5e_channel *c,
1658 struct mlx5e_cq_param *param,
1659 struct mlx5e_cq *cq)
1661 struct mlx5_core_dev *mdev = c->priv->mdev;
1664 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1665 param->wq.db_numa_node = cpu_to_node(c->cpu);
1666 param->eq_ix = c->ix;
1668 err = mlx5e_alloc_cq_common(mdev, param, cq);
1670 cq->napi = &c->napi;
1676 static void mlx5e_free_cq(struct mlx5e_cq *cq)
1678 mlx5_wq_destroy(&cq->wq_ctrl);
1681 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1683 struct mlx5_core_dev *mdev = cq->mdev;
1684 struct mlx5_core_cq *mcq = &cq->mcq;
1689 unsigned int irqn_not_used;
1693 err = mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1697 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1698 sizeof(u64) * cq->wq_ctrl.buf.npages;
1699 in = kvzalloc(inlen, GFP_KERNEL);
1703 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1705 memcpy(cqc, param->cqc, sizeof(param->cqc));
1707 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf,
1708 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1710 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1711 MLX5_SET(cqc, cqc, c_eqn, eqn);
1712 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
1713 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1714 MLX5_ADAPTER_PAGE_SHIFT);
1715 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1717 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1729 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1731 mlx5_core_destroy_cq(cq->mdev, &cq->mcq);
1734 static int mlx5e_open_cq(struct mlx5e_channel *c,
1735 struct net_dim_cq_moder moder,
1736 struct mlx5e_cq_param *param,
1737 struct mlx5e_cq *cq)
1739 struct mlx5_core_dev *mdev = c->mdev;
1742 err = mlx5e_alloc_cq(c, param, cq);
1746 err = mlx5e_create_cq(cq, param);
1750 if (MLX5_CAP_GEN(mdev, cq_moderation))
1751 mlx5_core_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts);
1760 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1762 mlx5e_destroy_cq(cq);
1766 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1768 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1771 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1772 struct mlx5e_params *params,
1773 struct mlx5e_channel_param *cparam)
1778 for (tc = 0; tc < c->num_tc; tc++) {
1779 err = mlx5e_open_cq(c, params->tx_cq_moderation,
1780 &cparam->tx_cq, &c->sq[tc].cq);
1782 goto err_close_tx_cqs;
1788 for (tc--; tc >= 0; tc--)
1789 mlx5e_close_cq(&c->sq[tc].cq);
1794 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1798 for (tc = 0; tc < c->num_tc; tc++)
1799 mlx5e_close_cq(&c->sq[tc].cq);
1802 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1803 struct mlx5e_params *params,
1804 struct mlx5e_channel_param *cparam)
1806 struct mlx5e_priv *priv = c->priv;
1807 int err, tc, max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
1809 for (tc = 0; tc < params->num_tc; tc++) {
1810 int txq_ix = c->ix + tc * max_nch;
1812 err = mlx5e_open_txqsq(c, c->priv->tisn[tc], txq_ix,
1813 params, &cparam->sq, &c->sq[tc], tc);
1821 for (tc--; tc >= 0; tc--)
1822 mlx5e_close_txqsq(&c->sq[tc]);
1827 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1831 for (tc = 0; tc < c->num_tc; tc++)
1832 mlx5e_close_txqsq(&c->sq[tc]);
1835 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1836 struct mlx5e_txqsq *sq, u32 rate)
1838 struct mlx5e_priv *priv = netdev_priv(dev);
1839 struct mlx5_core_dev *mdev = priv->mdev;
1840 struct mlx5e_modify_sq_param msp = {0};
1841 struct mlx5_rate_limit rl = {0};
1845 if (rate == sq->rate_limit)
1849 if (sq->rate_limit) {
1850 rl.rate = sq->rate_limit;
1851 /* remove current rl index to free space to next ones */
1852 mlx5_rl_remove_rate(mdev, &rl);
1859 err = mlx5_rl_add_rate(mdev, &rl_index, &rl);
1861 netdev_err(dev, "Failed configuring rate %u: %d\n",
1867 msp.curr_state = MLX5_SQC_STATE_RDY;
1868 msp.next_state = MLX5_SQC_STATE_RDY;
1869 msp.rl_index = rl_index;
1870 msp.rl_update = true;
1871 err = mlx5e_modify_sq(mdev, sq->sqn, &msp);
1873 netdev_err(dev, "Failed configuring rate %u: %d\n",
1875 /* remove the rate from the table */
1877 mlx5_rl_remove_rate(mdev, &rl);
1881 sq->rate_limit = rate;
1885 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1887 struct mlx5e_priv *priv = netdev_priv(dev);
1888 struct mlx5_core_dev *mdev = priv->mdev;
1889 struct mlx5e_txqsq *sq = priv->txq2sq[index];
1892 if (!mlx5_rl_is_supported(mdev)) {
1893 netdev_err(dev, "Rate limiting is not supported on this device\n");
1897 /* rate is given in Mb/sec, HW config is in Kb/sec */
1900 /* Check whether rate in valid range, 0 is always valid */
1901 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1902 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1906 mutex_lock(&priv->state_lock);
1907 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1908 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1910 priv->tx_rates[index] = rate;
1911 mutex_unlock(&priv->state_lock);
1916 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1917 struct mlx5e_params *params,
1918 struct mlx5e_channel_param *cparam,
1919 struct mlx5e_channel **cp)
1921 struct net_dim_cq_moder icocq_moder = {0, 0};
1922 struct net_device *netdev = priv->netdev;
1923 int cpu = mlx5e_get_cpu(priv, ix);
1924 struct mlx5e_channel *c;
1929 err = mlx5_vector2eqn(priv->mdev, ix, &eqn, &irq);
1933 c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1938 c->mdev = priv->mdev;
1939 c->tstamp = &priv->tstamp;
1942 c->pdev = &priv->mdev->pdev->dev;
1943 c->netdev = priv->netdev;
1944 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1945 c->num_tc = params->num_tc;
1946 c->xdp = !!params->xdp_prog;
1947 c->stats = &priv->channel_stats[ix].ch;
1949 c->irq_desc = irq_to_desc(irq);
1951 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1953 err = mlx5e_open_cq(c, icocq_moder, &cparam->icosq_cq, &c->icosq.cq);
1957 err = mlx5e_open_tx_cqs(c, params, cparam);
1959 goto err_close_icosq_cq;
1961 err = mlx5e_open_cq(c, params->tx_cq_moderation, &cparam->tx_cq, &c->xdpsq.cq);
1963 goto err_close_tx_cqs;
1965 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rx_cq, &c->rq.cq);
1967 goto err_close_xdp_tx_cqs;
1969 /* XDP SQ CQ params are same as normal TXQ sq CQ params */
1970 err = c->xdp ? mlx5e_open_cq(c, params->tx_cq_moderation,
1971 &cparam->tx_cq, &c->rq.xdpsq.cq) : 0;
1973 goto err_close_rx_cq;
1975 napi_enable(&c->napi);
1977 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq);
1979 goto err_disable_napi;
1981 err = mlx5e_open_sqs(c, params, cparam);
1983 goto err_close_icosq;
1985 err = c->xdp ? mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->rq.xdpsq, false) : 0;
1989 err = mlx5e_open_rq(c, params, &cparam->rq, &c->rq);
1991 goto err_close_xdp_sq;
1993 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, &c->xdpsq, true);
2002 mlx5e_close_rq(&c->rq);
2006 mlx5e_close_xdpsq(&c->rq.xdpsq);
2012 mlx5e_close_icosq(&c->icosq);
2015 napi_disable(&c->napi);
2017 mlx5e_close_cq(&c->rq.xdpsq.cq);
2020 mlx5e_close_cq(&c->rq.cq);
2022 err_close_xdp_tx_cqs:
2023 mlx5e_close_cq(&c->xdpsq.cq);
2026 mlx5e_close_tx_cqs(c);
2029 mlx5e_close_cq(&c->icosq.cq);
2032 netif_napi_del(&c->napi);
2038 static void mlx5e_activate_channel(struct mlx5e_channel *c)
2042 for (tc = 0; tc < c->num_tc; tc++)
2043 mlx5e_activate_txqsq(&c->sq[tc]);
2044 mlx5e_activate_rq(&c->rq);
2045 netif_set_xps_queue(c->netdev, get_cpu_mask(c->cpu), c->ix);
2048 static void mlx5e_deactivate_channel(struct mlx5e_channel *c)
2052 mlx5e_deactivate_rq(&c->rq);
2053 for (tc = 0; tc < c->num_tc; tc++)
2054 mlx5e_deactivate_txqsq(&c->sq[tc]);
2057 static void mlx5e_close_channel(struct mlx5e_channel *c)
2059 mlx5e_close_xdpsq(&c->xdpsq);
2060 mlx5e_close_rq(&c->rq);
2062 mlx5e_close_xdpsq(&c->rq.xdpsq);
2064 mlx5e_close_icosq(&c->icosq);
2065 napi_disable(&c->napi);
2067 mlx5e_close_cq(&c->rq.xdpsq.cq);
2068 mlx5e_close_cq(&c->rq.cq);
2069 mlx5e_close_cq(&c->xdpsq.cq);
2070 mlx5e_close_tx_cqs(c);
2071 mlx5e_close_cq(&c->icosq.cq);
2072 netif_napi_del(&c->napi);
2077 #define DEFAULT_FRAG_SIZE (2048)
2079 static void mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
2080 struct mlx5e_params *params,
2081 struct mlx5e_rq_frags_info *info)
2083 u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
2084 int frag_size_max = DEFAULT_FRAG_SIZE;
2088 #ifdef CONFIG_MLX5_EN_IPSEC
2089 if (MLX5_IPSEC_DEV(mdev))
2090 byte_count += MLX5E_METADATA_ETHER_LEN;
2093 if (mlx5e_rx_is_linear_skb(mdev, params)) {
2096 frag_stride = mlx5e_rx_get_linear_frag_sz(params);
2097 frag_stride = roundup_pow_of_two(frag_stride);
2099 info->arr[0].frag_size = byte_count;
2100 info->arr[0].frag_stride = frag_stride;
2101 info->num_frags = 1;
2102 info->wqe_bulk = PAGE_SIZE / frag_stride;
2106 if (byte_count > PAGE_SIZE +
2107 (MLX5E_MAX_RX_FRAGS - 1) * frag_size_max)
2108 frag_size_max = PAGE_SIZE;
2111 while (buf_size < byte_count) {
2112 int frag_size = byte_count - buf_size;
2114 if (i < MLX5E_MAX_RX_FRAGS - 1)
2115 frag_size = min(frag_size, frag_size_max);
2117 info->arr[i].frag_size = frag_size;
2118 info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
2120 buf_size += frag_size;
2123 info->num_frags = i;
2124 /* number of different wqes sharing a page */
2125 info->wqe_bulk = 1 + (info->num_frags % 2);
2128 info->wqe_bulk = max_t(u8, info->wqe_bulk, 8);
2129 info->log_num_frags = order_base_2(info->num_frags);
2132 static inline u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
2134 int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
2137 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2138 sz += sizeof(struct mlx5e_rx_wqe_ll);
2140 default: /* MLX5_WQ_TYPE_CYCLIC */
2141 sz += sizeof(struct mlx5e_rx_wqe_cyc);
2144 return order_base_2(sz);
2147 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
2148 struct mlx5e_params *params,
2149 struct mlx5e_rq_param *param)
2151 struct mlx5_core_dev *mdev = priv->mdev;
2152 void *rqc = param->rqc;
2153 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2156 switch (params->rq_wq_type) {
2157 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2158 MLX5_SET(wq, wq, log_wqe_num_of_strides,
2159 mlx5e_mpwqe_get_log_num_strides(mdev, params) -
2160 MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
2161 MLX5_SET(wq, wq, log_wqe_stride_size,
2162 mlx5e_mpwqe_get_log_stride_size(mdev, params) -
2163 MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
2164 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params));
2166 default: /* MLX5_WQ_TYPE_CYCLIC */
2167 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
2168 mlx5e_build_rq_frags_info(mdev, params, ¶m->frags_info);
2169 ndsegs = param->frags_info.num_frags;
2172 MLX5_SET(wq, wq, wq_type, params->rq_wq_type);
2173 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
2174 MLX5_SET(wq, wq, log_wq_stride,
2175 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
2176 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.pdn);
2177 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
2178 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
2179 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
2181 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2184 static void mlx5e_build_drop_rq_param(struct mlx5e_priv *priv,
2185 struct mlx5e_rq_param *param)
2187 struct mlx5_core_dev *mdev = priv->mdev;
2188 void *rqc = param->rqc;
2189 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
2191 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
2192 MLX5_SET(wq, wq, log_wq_stride,
2193 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
2194 MLX5_SET(rqc, rqc, counter_set_id, priv->drop_rq_q_counter);
2196 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
2199 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
2200 struct mlx5e_sq_param *param)
2202 void *sqc = param->sqc;
2203 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2205 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
2206 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
2208 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
2211 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
2212 struct mlx5e_params *params,
2213 struct mlx5e_sq_param *param)
2215 void *sqc = param->sqc;
2216 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2218 mlx5e_build_sq_param_common(priv, param);
2219 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2220 MLX5_SET(sqc, sqc, allow_swp, !!MLX5_IPSEC_DEV(priv->mdev));
2223 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
2224 struct mlx5e_cq_param *param)
2226 void *cqc = param->cqc;
2228 MLX5_SET(cqc, cqc, uar_page, priv->mdev->priv.uar->index);
2231 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
2232 struct mlx5e_params *params,
2233 struct mlx5e_cq_param *param)
2235 struct mlx5_core_dev *mdev = priv->mdev;
2236 void *cqc = param->cqc;
2239 switch (params->rq_wq_type) {
2240 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2241 log_cq_size = mlx5e_mpwqe_get_log_rq_size(params) +
2242 mlx5e_mpwqe_get_log_num_strides(mdev, params);
2244 default: /* MLX5_WQ_TYPE_CYCLIC */
2245 log_cq_size = params->log_rq_mtu_frames;
2248 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
2249 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
2250 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
2251 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
2254 mlx5e_build_common_cq_param(priv, param);
2255 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
2258 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2259 struct mlx5e_params *params,
2260 struct mlx5e_cq_param *param)
2262 void *cqc = param->cqc;
2264 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
2266 mlx5e_build_common_cq_param(priv, param);
2267 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
2270 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
2272 struct mlx5e_cq_param *param)
2274 void *cqc = param->cqc;
2276 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
2278 mlx5e_build_common_cq_param(priv, param);
2280 param->cq_period_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2283 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
2285 struct mlx5e_sq_param *param)
2287 void *sqc = param->sqc;
2288 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2290 mlx5e_build_sq_param_common(priv, param);
2292 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
2293 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
2296 static void mlx5e_build_xdpsq_param(struct mlx5e_priv *priv,
2297 struct mlx5e_params *params,
2298 struct mlx5e_sq_param *param)
2300 void *sqc = param->sqc;
2301 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
2303 mlx5e_build_sq_param_common(priv, param);
2304 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
2307 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
2308 struct mlx5e_params *params,
2309 struct mlx5e_channel_param *cparam)
2311 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
2313 mlx5e_build_rq_param(priv, params, &cparam->rq);
2314 mlx5e_build_sq_param(priv, params, &cparam->sq);
2315 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
2316 mlx5e_build_icosq_param(priv, icosq_log_wq_sz, &cparam->icosq);
2317 mlx5e_build_rx_cq_param(priv, params, &cparam->rx_cq);
2318 mlx5e_build_tx_cq_param(priv, params, &cparam->tx_cq);
2319 mlx5e_build_ico_cq_param(priv, icosq_log_wq_sz, &cparam->icosq_cq);
2322 int mlx5e_open_channels(struct mlx5e_priv *priv,
2323 struct mlx5e_channels *chs)
2325 struct mlx5e_channel_param *cparam;
2329 chs->num = chs->params.num_channels;
2331 chs->c = kcalloc(chs->num, sizeof(struct mlx5e_channel *), GFP_KERNEL);
2332 cparam = kvzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
2333 if (!chs->c || !cparam)
2336 mlx5e_build_channel_param(priv, &chs->params, cparam);
2337 for (i = 0; i < chs->num; i++) {
2338 err = mlx5e_open_channel(priv, i, &chs->params, cparam, &chs->c[i]);
2340 goto err_close_channels;
2347 for (i--; i >= 0; i--)
2348 mlx5e_close_channel(chs->c[i]);
2357 static void mlx5e_activate_channels(struct mlx5e_channels *chs)
2361 for (i = 0; i < chs->num; i++)
2362 mlx5e_activate_channel(chs->c[i]);
2365 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
2370 for (i = 0; i < chs->num; i++)
2371 err |= mlx5e_wait_for_min_rx_wqes(&chs->c[i]->rq,
2374 return err ? -ETIMEDOUT : 0;
2377 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs)
2381 for (i = 0; i < chs->num; i++)
2382 mlx5e_deactivate_channel(chs->c[i]);
2385 void mlx5e_close_channels(struct mlx5e_channels *chs)
2389 for (i = 0; i < chs->num; i++)
2390 mlx5e_close_channel(chs->c[i]);
2397 mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, struct mlx5e_rqt *rqt)
2399 struct mlx5_core_dev *mdev = priv->mdev;
2406 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2407 in = kvzalloc(inlen, GFP_KERNEL);
2411 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2413 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2414 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2416 for (i = 0; i < sz; i++)
2417 MLX5_SET(rqtc, rqtc, rq_num[i], priv->drop_rq.rqn);
2419 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
2421 rqt->enabled = true;
2427 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
2429 rqt->enabled = false;
2430 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
2433 int mlx5e_create_indirect_rqt(struct mlx5e_priv *priv)
2435 struct mlx5e_rqt *rqt = &priv->indir_rqt;
2438 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, rqt);
2440 mlx5_core_warn(priv->mdev, "create indirect rqts failed, %d\n", err);
2444 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
2446 struct mlx5e_rqt *rqt;
2450 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2451 rqt = &priv->direct_tir[ix].rqt;
2452 err = mlx5e_create_rqt(priv, 1 /*size */, rqt);
2454 goto err_destroy_rqts;
2460 mlx5_core_warn(priv->mdev, "create direct rqts failed, %d\n", err);
2461 for (ix--; ix >= 0; ix--)
2462 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
2467 void mlx5e_destroy_direct_rqts(struct mlx5e_priv *priv)
2471 for (i = 0; i < mlx5e_get_netdev_max_channels(priv->netdev); i++)
2472 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
2475 static int mlx5e_rx_hash_fn(int hfunc)
2477 return (hfunc == ETH_RSS_HASH_TOP) ?
2478 MLX5_RX_HASH_FN_TOEPLITZ :
2479 MLX5_RX_HASH_FN_INVERTED_XOR8;
2482 int mlx5e_bits_invert(unsigned long a, int size)
2487 for (i = 0; i < size; i++)
2488 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
2493 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, int sz,
2494 struct mlx5e_redirect_rqt_param rrp, void *rqtc)
2498 for (i = 0; i < sz; i++) {
2504 if (rrp.rss.hfunc == ETH_RSS_HASH_XOR)
2505 ix = mlx5e_bits_invert(i, ilog2(sz));
2507 ix = priv->channels.params.indirection_rqt[ix];
2508 rqn = rrp.rss.channels->c[ix]->rq.rqn;
2512 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
2516 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz,
2517 struct mlx5e_redirect_rqt_param rrp)
2519 struct mlx5_core_dev *mdev = priv->mdev;
2525 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
2526 in = kvzalloc(inlen, GFP_KERNEL);
2530 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
2532 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2533 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
2534 mlx5e_fill_rqt_rqns(priv, sz, rrp, rqtc);
2535 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
2541 static u32 mlx5e_get_direct_rqn(struct mlx5e_priv *priv, int ix,
2542 struct mlx5e_redirect_rqt_param rrp)
2547 if (ix >= rrp.rss.channels->num)
2548 return priv->drop_rq.rqn;
2550 return rrp.rss.channels->c[ix]->rq.rqn;
2553 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv,
2554 struct mlx5e_redirect_rqt_param rrp)
2559 if (priv->indir_rqt.enabled) {
2561 rqtn = priv->indir_rqt.rqtn;
2562 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, rrp);
2565 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2566 struct mlx5e_redirect_rqt_param direct_rrp = {
2569 .rqn = mlx5e_get_direct_rqn(priv, ix, rrp)
2573 /* Direct RQ Tables */
2574 if (!priv->direct_tir[ix].rqt.enabled)
2577 rqtn = priv->direct_tir[ix].rqt.rqtn;
2578 mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
2582 static void mlx5e_redirect_rqts_to_channels(struct mlx5e_priv *priv,
2583 struct mlx5e_channels *chs)
2585 struct mlx5e_redirect_rqt_param rrp = {
2590 .hfunc = chs->params.rss_hfunc,
2595 mlx5e_redirect_rqts(priv, rrp);
2598 static void mlx5e_redirect_rqts_to_drop(struct mlx5e_priv *priv)
2600 struct mlx5e_redirect_rqt_param drop_rrp = {
2603 .rqn = priv->drop_rq.rqn,
2607 mlx5e_redirect_rqts(priv, drop_rrp);
2610 static void mlx5e_build_tir_ctx_lro(struct mlx5e_params *params, void *tirc)
2612 if (!params->lro_en)
2615 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2617 MLX5_SET(tirc, tirc, lro_enable_mask,
2618 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2619 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2620 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
2621 (params->lro_wqe_sz - ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2622 MLX5_SET(tirc, tirc, lro_timeout_period_usecs, params->lro_timeout);
2625 void mlx5e_build_indir_tir_ctx_hash(struct mlx5e_params *params,
2626 enum mlx5e_traffic_types tt,
2627 void *tirc, bool inner)
2629 void *hfso = inner ? MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner) :
2630 MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2632 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2633 MLX5_HASH_FIELD_SEL_DST_IP)
2635 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2636 MLX5_HASH_FIELD_SEL_DST_IP |\
2637 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2638 MLX5_HASH_FIELD_SEL_L4_DPORT)
2640 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2641 MLX5_HASH_FIELD_SEL_DST_IP |\
2642 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2644 MLX5_SET(tirc, tirc, rx_hash_fn, mlx5e_rx_hash_fn(params->rss_hfunc));
2645 if (params->rss_hfunc == ETH_RSS_HASH_TOP) {
2646 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
2647 rx_hash_toeplitz_key);
2648 size_t len = MLX5_FLD_SZ_BYTES(tirc,
2649 rx_hash_toeplitz_key);
2651 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2652 memcpy(rss_key, params->toeplitz_hash_key, len);
2656 case MLX5E_TT_IPV4_TCP:
2657 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2658 MLX5_L3_PROT_TYPE_IPV4);
2659 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2660 MLX5_L4_PROT_TYPE_TCP);
2661 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2662 MLX5_HASH_IP_L4PORTS);
2665 case MLX5E_TT_IPV6_TCP:
2666 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2667 MLX5_L3_PROT_TYPE_IPV6);
2668 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2669 MLX5_L4_PROT_TYPE_TCP);
2670 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2671 MLX5_HASH_IP_L4PORTS);
2674 case MLX5E_TT_IPV4_UDP:
2675 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2676 MLX5_L3_PROT_TYPE_IPV4);
2677 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2678 MLX5_L4_PROT_TYPE_UDP);
2679 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2680 MLX5_HASH_IP_L4PORTS);
2683 case MLX5E_TT_IPV6_UDP:
2684 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2685 MLX5_L3_PROT_TYPE_IPV6);
2686 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2687 MLX5_L4_PROT_TYPE_UDP);
2688 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2689 MLX5_HASH_IP_L4PORTS);
2692 case MLX5E_TT_IPV4_IPSEC_AH:
2693 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2694 MLX5_L3_PROT_TYPE_IPV4);
2695 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2696 MLX5_HASH_IP_IPSEC_SPI);
2699 case MLX5E_TT_IPV6_IPSEC_AH:
2700 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2701 MLX5_L3_PROT_TYPE_IPV6);
2702 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2703 MLX5_HASH_IP_IPSEC_SPI);
2706 case MLX5E_TT_IPV4_IPSEC_ESP:
2707 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2708 MLX5_L3_PROT_TYPE_IPV4);
2709 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2710 MLX5_HASH_IP_IPSEC_SPI);
2713 case MLX5E_TT_IPV6_IPSEC_ESP:
2714 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2715 MLX5_L3_PROT_TYPE_IPV6);
2716 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2717 MLX5_HASH_IP_IPSEC_SPI);
2721 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2722 MLX5_L3_PROT_TYPE_IPV4);
2723 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2728 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2729 MLX5_L3_PROT_TYPE_IPV6);
2730 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2734 WARN_ONCE(true, "%s: bad traffic type!\n", __func__);
2738 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
2740 struct mlx5_core_dev *mdev = priv->mdev;
2749 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
2750 in = kvzalloc(inlen, GFP_KERNEL);
2754 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
2755 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
2757 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2759 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2760 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
2766 for (ix = 0; ix < mlx5e_get_netdev_max_channels(priv->netdev); ix++) {
2767 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
2779 static void mlx5e_build_inner_indir_tir_ctx(struct mlx5e_priv *priv,
2780 enum mlx5e_traffic_types tt,
2783 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2785 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
2787 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2788 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2789 MLX5_SET(tirc, tirc, tunneled_offload_en, 0x1);
2791 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, true);
2794 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev,
2795 struct mlx5e_params *params, u16 mtu)
2797 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu);
2800 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
2804 /* Update vport context MTU */
2805 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
2809 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev,
2810 struct mlx5e_params *params, u16 *mtu)
2815 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
2816 if (err || !hw_mtu) /* fallback to port oper mtu */
2817 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
2819 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu);
2822 static int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv)
2824 struct mlx5e_params *params = &priv->channels.params;
2825 struct net_device *netdev = priv->netdev;
2826 struct mlx5_core_dev *mdev = priv->mdev;
2830 err = mlx5e_set_mtu(mdev, params, params->sw_mtu);
2834 mlx5e_query_mtu(mdev, params, &mtu);
2835 if (mtu != params->sw_mtu)
2836 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
2837 __func__, mtu, params->sw_mtu);
2839 params->sw_mtu = mtu;
2843 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
2845 struct mlx5e_priv *priv = netdev_priv(netdev);
2846 int nch = priv->channels.params.num_channels;
2847 int ntc = priv->channels.params.num_tc;
2850 netdev_reset_tc(netdev);
2855 netdev_set_num_tc(netdev, ntc);
2857 /* Map netdev TCs to offset 0
2858 * We have our own UP to TXQ mapping for QoS
2860 for (tc = 0; tc < ntc; tc++)
2861 netdev_set_tc_queue(netdev, tc, nch, 0);
2864 static void mlx5e_build_tc2txq_maps(struct mlx5e_priv *priv)
2866 int max_nch = mlx5e_get_netdev_max_channels(priv->netdev);
2869 for (i = 0; i < max_nch; i++)
2870 for (tc = 0; tc < priv->profile->max_tc; tc++)
2871 priv->channel_tc2txq[i][tc] = i + tc * max_nch;
2874 static void mlx5e_build_tx2sq_maps(struct mlx5e_priv *priv)
2876 struct mlx5e_channel *c;
2877 struct mlx5e_txqsq *sq;
2880 for (i = 0; i < priv->channels.num; i++) {
2881 c = priv->channels.c[i];
2882 for (tc = 0; tc < c->num_tc; tc++) {
2884 priv->txq2sq[sq->txq_ix] = sq;
2889 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv)
2891 int num_txqs = priv->channels.num * priv->channels.params.num_tc;
2892 struct net_device *netdev = priv->netdev;
2894 mlx5e_netdev_set_tcs(netdev);
2895 netif_set_real_num_tx_queues(netdev, num_txqs);
2896 netif_set_real_num_rx_queues(netdev, priv->channels.num);
2898 mlx5e_build_tx2sq_maps(priv);
2899 mlx5e_activate_channels(&priv->channels);
2900 netif_tx_start_all_queues(priv->netdev);
2902 if (MLX5_ESWITCH_MANAGER(priv->mdev))
2903 mlx5e_add_sqs_fwd_rules(priv);
2905 mlx5e_wait_channels_min_rx_wqes(&priv->channels);
2906 mlx5e_redirect_rqts_to_channels(priv, &priv->channels);
2909 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv)
2911 mlx5e_redirect_rqts_to_drop(priv);
2913 if (MLX5_ESWITCH_MANAGER(priv->mdev))
2914 mlx5e_remove_sqs_fwd_rules(priv);
2916 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
2917 * polling for inactive tx queues.
2919 netif_tx_stop_all_queues(priv->netdev);
2920 netif_tx_disable(priv->netdev);
2921 mlx5e_deactivate_channels(&priv->channels);
2924 void mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
2925 struct mlx5e_channels *new_chs,
2926 mlx5e_fp_hw_modify hw_modify)
2928 struct net_device *netdev = priv->netdev;
2931 new_num_txqs = new_chs->num * new_chs->params.num_tc;
2933 carrier_ok = netif_carrier_ok(netdev);
2934 netif_carrier_off(netdev);
2936 if (new_num_txqs < netdev->real_num_tx_queues)
2937 netif_set_real_num_tx_queues(netdev, new_num_txqs);
2939 mlx5e_deactivate_priv_channels(priv);
2940 mlx5e_close_channels(&priv->channels);
2942 priv->channels = *new_chs;
2944 /* New channels are ready to roll, modify HW settings if needed */
2948 mlx5e_refresh_tirs(priv, false);
2949 mlx5e_activate_priv_channels(priv);
2951 /* return carrier back if needed */
2953 netif_carrier_on(netdev);
2956 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
2958 priv->tstamp.tx_type = HWTSTAMP_TX_OFF;
2959 priv->tstamp.rx_filter = HWTSTAMP_FILTER_NONE;
2962 int mlx5e_open_locked(struct net_device *netdev)
2964 struct mlx5e_priv *priv = netdev_priv(netdev);
2967 set_bit(MLX5E_STATE_OPENED, &priv->state);
2969 err = mlx5e_open_channels(priv, &priv->channels);
2971 goto err_clear_state_opened_flag;
2973 mlx5e_refresh_tirs(priv, false);
2974 mlx5e_activate_priv_channels(priv);
2975 if (priv->profile->update_carrier)
2976 priv->profile->update_carrier(priv);
2978 mlx5e_queue_update_stats(priv);
2981 err_clear_state_opened_flag:
2982 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2986 int mlx5e_open(struct net_device *netdev)
2988 struct mlx5e_priv *priv = netdev_priv(netdev);
2991 mutex_lock(&priv->state_lock);
2992 err = mlx5e_open_locked(netdev);
2994 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_UP);
2995 mutex_unlock(&priv->state_lock);
2997 if (mlx5_vxlan_allowed(priv->mdev->vxlan))
2998 udp_tunnel_get_rx_info(netdev);
3003 int mlx5e_close_locked(struct net_device *netdev)
3005 struct mlx5e_priv *priv = netdev_priv(netdev);
3007 /* May already be CLOSED in case a previous configuration operation
3008 * (e.g RX/TX queue size change) that involves close&open failed.
3010 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3013 clear_bit(MLX5E_STATE_OPENED, &priv->state);
3015 netif_carrier_off(priv->netdev);
3016 mlx5e_deactivate_priv_channels(priv);
3017 mlx5e_close_channels(&priv->channels);
3022 int mlx5e_close(struct net_device *netdev)
3024 struct mlx5e_priv *priv = netdev_priv(netdev);
3027 if (!netif_device_present(netdev))
3030 mutex_lock(&priv->state_lock);
3031 mlx5_set_port_admin_status(priv->mdev, MLX5_PORT_DOWN);
3032 err = mlx5e_close_locked(netdev);
3033 mutex_unlock(&priv->state_lock);
3038 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev,
3039 struct mlx5e_rq *rq,
3040 struct mlx5e_rq_param *param)
3042 void *rqc = param->rqc;
3043 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
3046 param->wq.db_numa_node = param->wq.buf_numa_node;
3048 err = mlx5_wq_cyc_create(mdev, ¶m->wq, rqc_wq, &rq->wqe.wq,
3053 /* Mark as unused given "Drop-RQ" packets never reach XDP */
3054 xdp_rxq_info_unused(&rq->xdp_rxq);
3061 static int mlx5e_alloc_drop_cq(struct mlx5_core_dev *mdev,
3062 struct mlx5e_cq *cq,
3063 struct mlx5e_cq_param *param)
3065 param->wq.buf_numa_node = dev_to_node(&mdev->pdev->dev);
3066 param->wq.db_numa_node = dev_to_node(&mdev->pdev->dev);
3068 return mlx5e_alloc_cq_common(mdev, param, cq);
3071 int mlx5e_open_drop_rq(struct mlx5e_priv *priv,
3072 struct mlx5e_rq *drop_rq)
3074 struct mlx5_core_dev *mdev = priv->mdev;
3075 struct mlx5e_cq_param cq_param = {};
3076 struct mlx5e_rq_param rq_param = {};
3077 struct mlx5e_cq *cq = &drop_rq->cq;
3080 mlx5e_build_drop_rq_param(priv, &rq_param);
3082 err = mlx5e_alloc_drop_cq(mdev, cq, &cq_param);
3086 err = mlx5e_create_cq(cq, &cq_param);
3090 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param);
3092 goto err_destroy_cq;
3094 err = mlx5e_create_rq(drop_rq, &rq_param);
3098 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3100 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err);
3105 mlx5e_free_rq(drop_rq);
3108 mlx5e_destroy_cq(cq);
3116 void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq)
3118 mlx5e_destroy_rq(drop_rq);
3119 mlx5e_free_rq(drop_rq);
3120 mlx5e_destroy_cq(&drop_rq->cq);
3121 mlx5e_free_cq(&drop_rq->cq);
3124 int mlx5e_create_tis(struct mlx5_core_dev *mdev, int tc,
3125 u32 underlay_qpn, u32 *tisn)
3127 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
3128 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
3130 MLX5_SET(tisc, tisc, prio, tc << 1);
3131 MLX5_SET(tisc, tisc, underlay_qpn, underlay_qpn);
3132 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
3134 if (mlx5_lag_is_lacp_owner(mdev))
3135 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
3137 return mlx5_core_create_tis(mdev, in, sizeof(in), tisn);
3140 void mlx5e_destroy_tis(struct mlx5_core_dev *mdev, u32 tisn)
3142 mlx5_core_destroy_tis(mdev, tisn);
3145 int mlx5e_create_tises(struct mlx5e_priv *priv)
3150 for (tc = 0; tc < priv->profile->max_tc; tc++) {
3151 err = mlx5e_create_tis(priv->mdev, tc, 0, &priv->tisn[tc]);
3153 goto err_close_tises;
3159 for (tc--; tc >= 0; tc--)
3160 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3165 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
3169 for (tc = 0; tc < priv->profile->max_tc; tc++)
3170 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
3173 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv,
3174 enum mlx5e_traffic_types tt,
3177 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3179 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3181 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3182 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
3183 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
3186 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 rqtn, u32 *tirc)
3188 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
3190 mlx5e_build_tir_ctx_lro(&priv->channels.params, tirc);
3192 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
3193 MLX5_SET(tirc, tirc, indirect_table, rqtn);
3194 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
3197 int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3199 struct mlx5e_tir *tir;
3207 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3208 in = kvzalloc(inlen, GFP_KERNEL);
3212 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
3213 memset(in, 0, inlen);
3214 tir = &priv->indir_tir[tt];
3215 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3216 mlx5e_build_indir_tir_ctx(priv, tt, tirc);
3217 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3219 mlx5_core_warn(priv->mdev, "create indirect tirs failed, %d\n", err);
3220 goto err_destroy_inner_tirs;
3224 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3227 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
3228 memset(in, 0, inlen);
3229 tir = &priv->inner_indir_tir[i];
3230 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3231 mlx5e_build_inner_indir_tir_ctx(priv, i, tirc);
3232 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3234 mlx5_core_warn(priv->mdev, "create inner indirect tirs failed, %d\n", err);
3235 goto err_destroy_inner_tirs;
3244 err_destroy_inner_tirs:
3245 for (i--; i >= 0; i--)
3246 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3248 for (tt--; tt >= 0; tt--)
3249 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
3256 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
3258 int nch = mlx5e_get_netdev_max_channels(priv->netdev);
3259 struct mlx5e_tir *tir;
3266 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
3267 in = kvzalloc(inlen, GFP_KERNEL);
3271 for (ix = 0; ix < nch; ix++) {
3272 memset(in, 0, inlen);
3273 tir = &priv->direct_tir[ix];
3274 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
3275 mlx5e_build_direct_tir_ctx(priv, priv->direct_tir[ix].rqt.rqtn, tirc);
3276 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
3278 goto err_destroy_ch_tirs;
3285 err_destroy_ch_tirs:
3286 mlx5_core_warn(priv->mdev, "create direct tirs failed, %d\n", err);
3287 for (ix--; ix >= 0; ix--)
3288 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
3295 void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv, bool inner_ttc)
3299 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3300 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
3302 if (!inner_ttc || !mlx5e_tunnel_inner_ft_supported(priv->mdev))
3305 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
3306 mlx5e_destroy_tir(priv->mdev, &priv->inner_indir_tir[i]);
3309 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
3311 int nch = mlx5e_get_netdev_max_channels(priv->netdev);
3314 for (i = 0; i < nch; i++)
3315 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
3318 static int mlx5e_modify_channels_scatter_fcs(struct mlx5e_channels *chs, bool enable)
3323 for (i = 0; i < chs->num; i++) {
3324 err = mlx5e_modify_rq_scatter_fcs(&chs->c[i]->rq, enable);
3332 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd)
3337 for (i = 0; i < chs->num; i++) {
3338 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd);
3346 static int mlx5e_setup_tc_mqprio(struct net_device *netdev,
3347 struct tc_mqprio_qopt *mqprio)
3349 struct mlx5e_priv *priv = netdev_priv(netdev);
3350 struct mlx5e_channels new_channels = {};
3351 u8 tc = mqprio->num_tc;
3354 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
3356 if (tc && tc != MLX5E_MAX_NUM_TC)
3359 mutex_lock(&priv->state_lock);
3361 new_channels.params = priv->channels.params;
3362 new_channels.params.num_tc = tc ? tc : 1;
3364 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
3365 priv->channels.params = new_channels.params;
3369 err = mlx5e_open_channels(priv, &new_channels);
3373 priv->max_opened_tc = max_t(u8, priv->max_opened_tc,
3374 new_channels.params.num_tc);
3375 mlx5e_switch_priv_channels(priv, &new_channels, NULL);
3377 mutex_unlock(&priv->state_lock);
3381 #ifdef CONFIG_MLX5_ESWITCH
3382 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
3383 struct tc_cls_flower_offload *cls_flower,
3386 switch (cls_flower->command) {
3387 case TC_CLSFLOWER_REPLACE:
3388 return mlx5e_configure_flower(priv, cls_flower, flags);
3389 case TC_CLSFLOWER_DESTROY:
3390 return mlx5e_delete_flower(priv, cls_flower, flags);
3391 case TC_CLSFLOWER_STATS:
3392 return mlx5e_stats_flower(priv, cls_flower, flags);
3398 static int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3401 struct mlx5e_priv *priv = cb_priv;
3404 case TC_SETUP_CLSFLOWER:
3405 return mlx5e_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS);
3411 static int mlx5e_setup_tc_block(struct net_device *dev,
3412 struct tc_block_offload *f)
3414 struct mlx5e_priv *priv = netdev_priv(dev);
3416 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3419 switch (f->command) {
3421 return tcf_block_cb_register(f->block, mlx5e_setup_tc_block_cb,
3422 priv, priv, f->extack);
3423 case TC_BLOCK_UNBIND:
3424 tcf_block_cb_unregister(f->block, mlx5e_setup_tc_block_cb,
3433 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
3437 #ifdef CONFIG_MLX5_ESWITCH
3438 case TC_SETUP_BLOCK:
3439 return mlx5e_setup_tc_block(dev, type_data);
3441 case TC_SETUP_QDISC_MQPRIO:
3442 return mlx5e_setup_tc_mqprio(dev, type_data);
3449 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
3451 struct mlx5e_priv *priv = netdev_priv(dev);
3452 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
3453 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
3454 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
3456 /* update HW stats in background for next time */
3457 mlx5e_queue_update_stats(priv);
3459 if (mlx5e_is_uplink_rep(priv)) {
3460 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
3461 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
3462 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
3463 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
3465 mlx5e_grp_sw_update_stats(priv);
3466 stats->rx_packets = sstats->rx_packets;
3467 stats->rx_bytes = sstats->rx_bytes;
3468 stats->tx_packets = sstats->tx_packets;
3469 stats->tx_bytes = sstats->tx_bytes;
3470 stats->tx_dropped = sstats->tx_queue_dropped;
3473 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
3475 stats->rx_length_errors =
3476 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
3477 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
3478 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
3479 stats->rx_crc_errors =
3480 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
3481 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
3482 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
3483 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
3484 stats->rx_frame_errors;
3485 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
3487 /* vport multicast also counts packets that are dropped due to steering
3488 * or rx out of buffer
3491 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
3494 static void mlx5e_set_rx_mode(struct net_device *dev)
3496 struct mlx5e_priv *priv = netdev_priv(dev);
3498 queue_work(priv->wq, &priv->set_rx_mode_work);
3501 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
3503 struct mlx5e_priv *priv = netdev_priv(netdev);
3504 struct sockaddr *saddr = addr;
3506 if (!is_valid_ether_addr(saddr->sa_data))
3507 return -EADDRNOTAVAIL;
3509 netif_addr_lock_bh(netdev);
3510 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
3511 netif_addr_unlock_bh(netdev);
3513 queue_work(priv->wq, &priv->set_rx_mode_work);
3518 #define MLX5E_SET_FEATURE(features, feature, enable) \
3521 *features |= feature; \
3523 *features &= ~feature; \
3526 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
3528 static int set_feature_lro(struct net_device *netdev, bool enable)
3530 struct mlx5e_priv *priv = netdev_priv(netdev);
3531 struct mlx5_core_dev *mdev = priv->mdev;
3532 struct mlx5e_channels new_channels = {};
3533 struct mlx5e_params *old_params;
3537 mutex_lock(&priv->state_lock);
3539 old_params = &priv->channels.params;
3540 if (enable && !MLX5E_GET_PFLAG(old_params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3541 netdev_warn(netdev, "can't set LRO with legacy RQ\n");
3546 reset = test_bit(MLX5E_STATE_OPENED, &priv->state);
3548 new_channels.params = *old_params;
3549 new_channels.params.lro_en = enable;
3551 if (old_params->rq_wq_type != MLX5_WQ_TYPE_CYCLIC) {
3552 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, old_params) ==
3553 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_channels.params))
3558 *old_params = new_channels.params;
3559 err = mlx5e_modify_tirs_lro(priv);
3563 err = mlx5e_open_channels(priv, &new_channels);
3567 mlx5e_switch_priv_channels(priv, &new_channels, mlx5e_modify_tirs_lro);
3569 mutex_unlock(&priv->state_lock);
3573 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable)
3575 struct mlx5e_priv *priv = netdev_priv(netdev);
3578 mlx5e_enable_cvlan_filter(priv);
3580 mlx5e_disable_cvlan_filter(priv);
3585 #ifdef CONFIG_MLX5_ESWITCH
3586 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
3588 struct mlx5e_priv *priv = netdev_priv(netdev);
3590 if (!enable && mlx5e_tc_num_filters(priv)) {
3592 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
3600 static int set_feature_rx_all(struct net_device *netdev, bool enable)
3602 struct mlx5e_priv *priv = netdev_priv(netdev);
3603 struct mlx5_core_dev *mdev = priv->mdev;
3605 return mlx5_set_port_fcs(mdev, !enable);
3608 static int set_feature_rx_fcs(struct net_device *netdev, bool enable)
3610 struct mlx5e_priv *priv = netdev_priv(netdev);
3613 mutex_lock(&priv->state_lock);
3615 priv->channels.params.scatter_fcs_en = enable;
3616 err = mlx5e_modify_channels_scatter_fcs(&priv->channels, enable);
3618 priv->channels.params.scatter_fcs_en = !enable;
3620 mutex_unlock(&priv->state_lock);
3625 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
3627 struct mlx5e_priv *priv = netdev_priv(netdev);
3630 mutex_lock(&priv->state_lock);
3632 priv->channels.params.vlan_strip_disable = !enable;
3633 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
3636 err = mlx5e_modify_channels_vsd(&priv->channels, !enable);
3638 priv->channels.params.vlan_strip_disable = enable;
3641 mutex_unlock(&priv->state_lock);
3646 #ifdef CONFIG_MLX5_EN_ARFS
3647 static int set_feature_arfs(struct net_device *netdev, bool enable)
3649 struct mlx5e_priv *priv = netdev_priv(netdev);
3653 err = mlx5e_arfs_enable(priv);
3655 err = mlx5e_arfs_disable(priv);
3661 static int mlx5e_handle_feature(struct net_device *netdev,
3662 netdev_features_t *features,
3663 netdev_features_t wanted_features,
3664 netdev_features_t feature,
3665 mlx5e_feature_handler feature_handler)
3667 netdev_features_t changes = wanted_features ^ netdev->features;
3668 bool enable = !!(wanted_features & feature);
3671 if (!(changes & feature))
3674 err = feature_handler(netdev, enable);
3676 netdev_err(netdev, "%s feature %pNF failed, err %d\n",
3677 enable ? "Enable" : "Disable", &feature, err);
3681 MLX5E_SET_FEATURE(features, feature, enable);
3685 static int mlx5e_set_features(struct net_device *netdev,
3686 netdev_features_t features)
3688 netdev_features_t oper_features = netdev->features;
3691 #define MLX5E_HANDLE_FEATURE(feature, handler) \
3692 mlx5e_handle_feature(netdev, &oper_features, features, feature, handler)
3694 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro);
3695 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER,
3696 set_feature_cvlan_filter);
3697 #ifdef CONFIG_MLX5_ESWITCH
3698 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_tc_num_filters);
3700 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all);
3701 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs);
3702 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan);
3703 #ifdef CONFIG_MLX5_EN_ARFS
3704 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs);
3708 netdev->features = oper_features;
3715 static netdev_features_t mlx5e_fix_features(struct net_device *netdev,
3716 netdev_features_t features)
3718 struct mlx5e_priv *priv = netdev_priv(netdev);
3719 struct mlx5e_params *params;
3721 mutex_lock(&priv->state_lock);
3722 params = &priv->channels.params;
3723 if (!bitmap_empty(priv->fs.vlan.active_svlans, VLAN_N_VID)) {
3724 /* HW strips the outer C-tag header, this is a problem
3725 * for S-tag traffic.
3727 features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3728 if (!params->vlan_strip_disable)
3729 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n");
3731 if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) {
3732 features &= ~NETIF_F_LRO;
3734 netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n");
3737 mutex_unlock(&priv->state_lock);
3742 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu,
3743 change_hw_mtu_cb set_mtu_cb)
3745 struct mlx5e_priv *priv = netdev_priv(netdev);
3746 struct mlx5e_channels new_channels = {};
3747 struct mlx5e_params *params;
3751 mutex_lock(&priv->state_lock);
3753 params = &priv->channels.params;
3755 reset = !params->lro_en;
3756 reset = reset && test_bit(MLX5E_STATE_OPENED, &priv->state);
3758 new_channels.params = *params;
3759 new_channels.params.sw_mtu = new_mtu;
3761 if (params->xdp_prog &&
3762 !mlx5e_rx_is_linear_skb(priv->mdev, &new_channels.params)) {
3763 netdev_err(netdev, "MTU(%d) > %d is not allowed while XDP enabled\n",
3764 new_mtu, MLX5E_XDP_MAX_MTU);
3769 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) {
3770 bool is_linear = mlx5e_rx_mpwqe_is_linear_skb(priv->mdev, &new_channels.params);
3771 u8 ppw_old = mlx5e_mpwqe_log_pkts_per_wqe(params);
3772 u8 ppw_new = mlx5e_mpwqe_log_pkts_per_wqe(&new_channels.params);
3774 reset = reset && (is_linear || (ppw_old != ppw_new));
3778 params->sw_mtu = new_mtu;
3781 netdev->mtu = params->sw_mtu;
3785 err = mlx5e_open_channels(priv, &new_channels);
3789 mlx5e_switch_priv_channels(priv, &new_channels, set_mtu_cb);
3790 netdev->mtu = new_channels.params.sw_mtu;
3793 mutex_unlock(&priv->state_lock);
3797 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu)
3799 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
3802 int mlx5e_hwstamp_set(struct mlx5e_priv *priv, struct ifreq *ifr)
3804 struct hwtstamp_config config;
3807 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz) ||
3808 (mlx5_clock_get_ptp_index(priv->mdev) == -1))
3811 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
3814 /* TX HW timestamp */
3815 switch (config.tx_type) {
3816 case HWTSTAMP_TX_OFF:
3817 case HWTSTAMP_TX_ON:
3823 mutex_lock(&priv->state_lock);
3824 /* RX HW timestamp */
3825 switch (config.rx_filter) {
3826 case HWTSTAMP_FILTER_NONE:
3827 /* Reset CQE compression to Admin default */
3828 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
3830 case HWTSTAMP_FILTER_ALL:
3831 case HWTSTAMP_FILTER_SOME:
3832 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3833 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3834 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3835 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3836 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3837 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3838 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3839 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3840 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3841 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3842 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3843 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3844 case HWTSTAMP_FILTER_NTP_ALL:
3845 /* Disable CQE compression */
3846 netdev_warn(priv->netdev, "Disabling cqe compression");
3847 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
3849 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err);
3850 mutex_unlock(&priv->state_lock);
3853 config.rx_filter = HWTSTAMP_FILTER_ALL;
3856 mutex_unlock(&priv->state_lock);
3860 memcpy(&priv->tstamp, &config, sizeof(config));
3861 mutex_unlock(&priv->state_lock);
3863 return copy_to_user(ifr->ifr_data, &config,
3864 sizeof(config)) ? -EFAULT : 0;
3867 int mlx5e_hwstamp_get(struct mlx5e_priv *priv, struct ifreq *ifr)
3869 struct hwtstamp_config *cfg = &priv->tstamp;
3871 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
3874 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
3877 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3879 struct mlx5e_priv *priv = netdev_priv(dev);
3883 return mlx5e_hwstamp_set(priv, ifr);
3885 return mlx5e_hwstamp_get(priv, ifr);
3891 #ifdef CONFIG_MLX5_ESWITCH
3892 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
3894 struct mlx5e_priv *priv = netdev_priv(dev);
3895 struct mlx5_core_dev *mdev = priv->mdev;
3897 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
3900 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
3903 struct mlx5e_priv *priv = netdev_priv(dev);
3904 struct mlx5_core_dev *mdev = priv->mdev;
3906 if (vlan_proto != htons(ETH_P_8021Q))
3907 return -EPROTONOSUPPORT;
3909 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
3913 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
3915 struct mlx5e_priv *priv = netdev_priv(dev);
3916 struct mlx5_core_dev *mdev = priv->mdev;
3918 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
3921 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
3923 struct mlx5e_priv *priv = netdev_priv(dev);
3924 struct mlx5_core_dev *mdev = priv->mdev;
3926 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
3929 static int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
3932 struct mlx5e_priv *priv = netdev_priv(dev);
3933 struct mlx5_core_dev *mdev = priv->mdev;
3935 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1,
3936 max_tx_rate, min_tx_rate);
3939 static int mlx5_vport_link2ifla(u8 esw_link)
3942 case MLX5_VPORT_ADMIN_STATE_DOWN:
3943 return IFLA_VF_LINK_STATE_DISABLE;
3944 case MLX5_VPORT_ADMIN_STATE_UP:
3945 return IFLA_VF_LINK_STATE_ENABLE;
3947 return IFLA_VF_LINK_STATE_AUTO;
3950 static int mlx5_ifla_link2vport(u8 ifla_link)
3952 switch (ifla_link) {
3953 case IFLA_VF_LINK_STATE_DISABLE:
3954 return MLX5_VPORT_ADMIN_STATE_DOWN;
3955 case IFLA_VF_LINK_STATE_ENABLE:
3956 return MLX5_VPORT_ADMIN_STATE_UP;
3958 return MLX5_VPORT_ADMIN_STATE_AUTO;
3961 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
3964 struct mlx5e_priv *priv = netdev_priv(dev);
3965 struct mlx5_core_dev *mdev = priv->mdev;
3967 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
3968 mlx5_ifla_link2vport(link_state));
3971 static int mlx5e_get_vf_config(struct net_device *dev,
3972 int vf, struct ifla_vf_info *ivi)
3974 struct mlx5e_priv *priv = netdev_priv(dev);
3975 struct mlx5_core_dev *mdev = priv->mdev;
3978 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
3981 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
3985 static int mlx5e_get_vf_stats(struct net_device *dev,
3986 int vf, struct ifla_vf_stats *vf_stats)
3988 struct mlx5e_priv *priv = netdev_priv(dev);
3989 struct mlx5_core_dev *mdev = priv->mdev;
3991 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
3996 struct mlx5e_vxlan_work {
3997 struct work_struct work;
3998 struct mlx5e_priv *priv;
4002 static void mlx5e_vxlan_add_work(struct work_struct *work)
4004 struct mlx5e_vxlan_work *vxlan_work =
4005 container_of(work, struct mlx5e_vxlan_work, work);
4006 struct mlx5e_priv *priv = vxlan_work->priv;
4007 u16 port = vxlan_work->port;
4009 mutex_lock(&priv->state_lock);
4010 mlx5_vxlan_add_port(priv->mdev->vxlan, port);
4011 mutex_unlock(&priv->state_lock);
4016 static void mlx5e_vxlan_del_work(struct work_struct *work)
4018 struct mlx5e_vxlan_work *vxlan_work =
4019 container_of(work, struct mlx5e_vxlan_work, work);
4020 struct mlx5e_priv *priv = vxlan_work->priv;
4021 u16 port = vxlan_work->port;
4023 mutex_lock(&priv->state_lock);
4024 mlx5_vxlan_del_port(priv->mdev->vxlan, port);
4025 mutex_unlock(&priv->state_lock);
4029 static void mlx5e_vxlan_queue_work(struct mlx5e_priv *priv, u16 port, int add)
4031 struct mlx5e_vxlan_work *vxlan_work;
4033 vxlan_work = kmalloc(sizeof(*vxlan_work), GFP_ATOMIC);
4038 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_add_work);
4040 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_del_work);
4042 vxlan_work->priv = priv;
4043 vxlan_work->port = port;
4044 queue_work(priv->wq, &vxlan_work->work);
4047 static void mlx5e_add_vxlan_port(struct net_device *netdev,
4048 struct udp_tunnel_info *ti)
4050 struct mlx5e_priv *priv = netdev_priv(netdev);
4052 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4055 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4058 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 1);
4061 static void mlx5e_del_vxlan_port(struct net_device *netdev,
4062 struct udp_tunnel_info *ti)
4064 struct mlx5e_priv *priv = netdev_priv(netdev);
4066 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
4069 if (!mlx5_vxlan_allowed(priv->mdev->vxlan))
4072 mlx5e_vxlan_queue_work(priv, be16_to_cpu(ti->port), 0);
4075 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
4076 struct sk_buff *skb,
4077 netdev_features_t features)
4079 unsigned int offset = 0;
4080 struct udphdr *udph;
4084 switch (vlan_get_protocol(skb)) {
4085 case htons(ETH_P_IP):
4086 proto = ip_hdr(skb)->protocol;
4088 case htons(ETH_P_IPV6):
4089 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL);
4099 udph = udp_hdr(skb);
4100 port = be16_to_cpu(udph->dest);
4102 /* Verify if UDP port is being offloaded by HW */
4103 if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, port))
4108 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
4109 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4112 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
4113 struct net_device *netdev,
4114 netdev_features_t features)
4116 struct mlx5e_priv *priv = netdev_priv(netdev);
4118 features = vlan_features_check(skb, features);
4119 features = vxlan_features_check(skb, features);
4121 #ifdef CONFIG_MLX5_EN_IPSEC
4122 if (mlx5e_ipsec_feature_check(skb, netdev, features))
4126 /* Validate if the tunneled packet is being offloaded by HW */
4127 if (skb->encapsulation &&
4128 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
4129 return mlx5e_tunnel_features_check(priv, skb, features);
4134 static bool mlx5e_tx_timeout_eq_recover(struct net_device *dev,
4135 struct mlx5e_txqsq *sq)
4137 struct mlx5_eq *eq = sq->cq.mcq.eq;
4140 netdev_err(dev, "EQ 0x%x: Cons = 0x%x, irqn = 0x%x\n",
4141 eq->eqn, eq->cons_index, eq->irqn);
4143 eqe_count = mlx5_eq_poll_irq_disabled(eq);
4147 netdev_err(dev, "Recover %d eqes on EQ 0x%x\n", eqe_count, eq->eqn);
4148 sq->channel->stats->eq_rearm++;
4152 static void mlx5e_tx_timeout_work(struct work_struct *work)
4154 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
4156 struct net_device *dev = priv->netdev;
4157 bool reopen_channels = false;
4161 mutex_lock(&priv->state_lock);
4163 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
4166 for (i = 0; i < priv->channels.num * priv->channels.params.num_tc; i++) {
4167 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, i);
4168 struct mlx5e_txqsq *sq = priv->txq2sq[i];
4170 if (!netif_xmit_stopped(dev_queue))
4174 "TX timeout on queue: %d, SQ: 0x%x, CQ: 0x%x, SQ Cons: 0x%x SQ Prod: 0x%x, usecs since last trans: %u\n",
4175 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc,
4176 jiffies_to_usecs(jiffies - dev_queue->trans_start));
4178 /* If we recover a lost interrupt, most likely TX timeout will
4179 * be resolved, skip reopening channels
4181 if (!mlx5e_tx_timeout_eq_recover(dev, sq)) {
4182 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
4183 reopen_channels = true;
4187 if (!reopen_channels)
4190 mlx5e_close_locked(dev);
4191 err = mlx5e_open_locked(dev);
4193 netdev_err(priv->netdev,
4194 "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
4198 mutex_unlock(&priv->state_lock);
4202 static void mlx5e_tx_timeout(struct net_device *dev)
4204 struct mlx5e_priv *priv = netdev_priv(dev);
4206 netdev_err(dev, "TX timeout detected\n");
4207 queue_work(priv->wq, &priv->tx_timeout_work);
4210 static int mlx5e_xdp_allowed(struct mlx5e_priv *priv, struct bpf_prog *prog)
4212 struct net_device *netdev = priv->netdev;
4213 struct mlx5e_channels new_channels = {};
4215 if (priv->channels.params.lro_en) {
4216 netdev_warn(netdev, "can't set XDP while LRO is on, disable LRO first\n");
4220 if (MLX5_IPSEC_DEV(priv->mdev)) {
4221 netdev_warn(netdev, "can't set XDP with IPSec offload\n");
4225 new_channels.params = priv->channels.params;
4226 new_channels.params.xdp_prog = prog;
4228 if (!mlx5e_rx_is_linear_skb(priv->mdev, &new_channels.params)) {
4229 netdev_warn(netdev, "XDP is not allowed with MTU(%d) > %d\n",
4230 new_channels.params.sw_mtu, MLX5E_XDP_MAX_MTU);
4237 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
4239 struct mlx5e_priv *priv = netdev_priv(netdev);
4240 struct bpf_prog *old_prog;
4241 bool reset, was_opened;
4245 mutex_lock(&priv->state_lock);
4248 err = mlx5e_xdp_allowed(priv, prog);
4253 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
4254 /* no need for full reset when exchanging programs */
4255 reset = (!priv->channels.params.xdp_prog || !prog);
4257 if (was_opened && reset)
4258 mlx5e_close_locked(netdev);
4259 if (was_opened && !reset) {
4260 /* num_channels is invariant here, so we can take the
4261 * batched reference right upfront.
4263 prog = bpf_prog_add(prog, priv->channels.num);
4265 err = PTR_ERR(prog);
4270 /* exchange programs, extra prog reference we got from caller
4271 * as long as we don't fail from this point onwards.
4273 old_prog = xchg(&priv->channels.params.xdp_prog, prog);
4275 bpf_prog_put(old_prog);
4277 if (reset) /* change RQ type according to priv->xdp_prog */
4278 mlx5e_set_rq_type(priv->mdev, &priv->channels.params);
4280 if (was_opened && reset)
4281 mlx5e_open_locked(netdev);
4283 if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset)
4286 /* exchanging programs w/o reset, we update ref counts on behalf
4287 * of the channels RQs here.
4289 for (i = 0; i < priv->channels.num; i++) {
4290 struct mlx5e_channel *c = priv->channels.c[i];
4292 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4293 napi_synchronize(&c->napi);
4294 /* prevent mlx5e_poll_rx_cq from accessing rq->xdp_prog */
4296 old_prog = xchg(&c->rq.xdp_prog, prog);
4298 set_bit(MLX5E_RQ_STATE_ENABLED, &c->rq.state);
4299 /* napi_schedule in case we have missed anything */
4300 napi_schedule(&c->napi);
4303 bpf_prog_put(old_prog);
4307 mutex_unlock(&priv->state_lock);
4311 static u32 mlx5e_xdp_query(struct net_device *dev)
4313 struct mlx5e_priv *priv = netdev_priv(dev);
4314 const struct bpf_prog *xdp_prog;
4317 mutex_lock(&priv->state_lock);
4318 xdp_prog = priv->channels.params.xdp_prog;
4320 prog_id = xdp_prog->aux->id;
4321 mutex_unlock(&priv->state_lock);
4326 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4328 switch (xdp->command) {
4329 case XDP_SETUP_PROG:
4330 return mlx5e_xdp_set(dev, xdp->prog);
4331 case XDP_QUERY_PROG:
4332 xdp->prog_id = mlx5e_xdp_query(dev);
4339 const struct net_device_ops mlx5e_netdev_ops = {
4340 .ndo_open = mlx5e_open,
4341 .ndo_stop = mlx5e_close,
4342 .ndo_start_xmit = mlx5e_xmit,
4343 .ndo_setup_tc = mlx5e_setup_tc,
4344 .ndo_select_queue = mlx5e_select_queue,
4345 .ndo_get_stats64 = mlx5e_get_stats,
4346 .ndo_set_rx_mode = mlx5e_set_rx_mode,
4347 .ndo_set_mac_address = mlx5e_set_mac,
4348 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
4349 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
4350 .ndo_set_features = mlx5e_set_features,
4351 .ndo_fix_features = mlx5e_fix_features,
4352 .ndo_change_mtu = mlx5e_change_nic_mtu,
4353 .ndo_do_ioctl = mlx5e_ioctl,
4354 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
4355 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
4356 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
4357 .ndo_features_check = mlx5e_features_check,
4358 .ndo_tx_timeout = mlx5e_tx_timeout,
4359 .ndo_bpf = mlx5e_xdp,
4360 .ndo_xdp_xmit = mlx5e_xdp_xmit,
4361 #ifdef CONFIG_MLX5_EN_ARFS
4362 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
4364 #ifdef CONFIG_MLX5_ESWITCH
4365 /* SRIOV E-Switch NDOs */
4366 .ndo_set_vf_mac = mlx5e_set_vf_mac,
4367 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
4368 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
4369 .ndo_set_vf_trust = mlx5e_set_vf_trust,
4370 .ndo_set_vf_rate = mlx5e_set_vf_rate,
4371 .ndo_get_vf_config = mlx5e_get_vf_config,
4372 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
4373 .ndo_get_vf_stats = mlx5e_get_vf_stats,
4374 .ndo_has_offload_stats = mlx5e_has_offload_stats,
4375 .ndo_get_offload_stats = mlx5e_get_offload_stats,
4379 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
4381 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
4383 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
4384 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
4385 !MLX5_CAP_ETH(mdev, csum_cap) ||
4386 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
4387 !MLX5_CAP_ETH(mdev, vlan_cap) ||
4388 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
4389 MLX5_CAP_FLOWTABLE(mdev,
4390 flow_table_properties_nic_receive.max_ft_level)
4392 mlx5_core_warn(mdev,
4393 "Not creating net device, some required device capabilities are missing\n");
4396 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
4397 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
4398 if (!MLX5_CAP_GEN(mdev, cq_moderation))
4399 mlx5_core_warn(mdev, "CQ moderation is not supported\n");
4404 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
4409 for (i = 0; i < len; i++)
4410 indirection_rqt[i] = i % num_channels;
4413 static bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
4418 mlx5e_port_max_linkspeed(mdev, &link_speed);
4419 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
4420 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
4421 link_speed, pci_bw);
4423 #define MLX5E_SLOW_PCI_RATIO (2)
4425 return link_speed && pci_bw &&
4426 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
4429 static struct net_dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode)
4431 struct net_dim_cq_moder moder;
4433 moder.cq_period_mode = cq_period_mode;
4434 moder.pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
4435 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
4436 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4437 moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
4442 static struct net_dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
4444 struct net_dim_cq_moder moder;
4446 moder.cq_period_mode = cq_period_mode;
4447 moder.pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
4448 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
4449 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
4450 moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
4455 static u8 mlx5_to_net_dim_cq_period_mode(u8 cq_period_mode)
4457 return cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE ?
4458 NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE :
4459 NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4462 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4464 if (params->tx_dim_enabled) {
4465 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4467 params->tx_cq_moderation = net_dim_get_def_tx_moderation(dim_period_mode);
4469 params->tx_cq_moderation = mlx5e_get_def_tx_moderation(cq_period_mode);
4472 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
4473 params->tx_cq_moderation.cq_period_mode ==
4474 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4477 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
4479 if (params->rx_dim_enabled) {
4480 u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
4482 params->rx_cq_moderation = net_dim_get_def_rx_moderation(dim_period_mode);
4484 params->rx_cq_moderation = mlx5e_get_def_rx_moderation(cq_period_mode);
4487 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
4488 params->rx_cq_moderation.cq_period_mode ==
4489 MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
4492 static u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
4496 /* The supported periods are organized in ascending order */
4497 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
4498 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
4501 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
4504 void mlx5e_build_rq_params(struct mlx5_core_dev *mdev,
4505 struct mlx5e_params *params)
4507 /* Prefer Striding RQ, unless any of the following holds:
4508 * - Striding RQ configuration is not possible/supported.
4509 * - Slow PCI heuristic.
4510 * - Legacy RQ would use linear SKB while Striding RQ would use non-linear.
4512 if (!slow_pci_heuristic(mdev) &&
4513 mlx5e_striding_rq_possible(mdev, params) &&
4514 (mlx5e_rx_mpwqe_is_linear_skb(mdev, params) ||
4515 !mlx5e_rx_is_linear_skb(mdev, params)))
4516 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, true);
4517 mlx5e_set_rq_type(mdev, params);
4518 mlx5e_init_rq_type_params(mdev, params);
4521 void mlx5e_build_rss_params(struct mlx5e_params *params)
4523 params->rss_hfunc = ETH_RSS_HASH_XOR;
4524 netdev_rss_key_fill(params->toeplitz_hash_key, sizeof(params->toeplitz_hash_key));
4525 mlx5e_build_default_indir_rqt(params->indirection_rqt,
4526 MLX5E_INDIR_RQT_SIZE, params->num_channels);
4529 void mlx5e_build_nic_params(struct mlx5_core_dev *mdev,
4530 struct mlx5e_params *params,
4531 u16 max_channels, u16 mtu)
4533 u8 rx_cq_period_mode;
4535 params->sw_mtu = mtu;
4536 params->hard_mtu = MLX5E_ETH_HARD_MTU;
4537 params->num_channels = max_channels;
4541 params->log_sq_size = is_kdump_kernel() ?
4542 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE :
4543 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
4545 /* set CQE compression */
4546 params->rx_cqe_compress_def = false;
4547 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
4548 MLX5_CAP_GEN(mdev, vport_group_manager))
4549 params->rx_cqe_compress_def = slow_pci_heuristic(mdev);
4551 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def);
4552 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE, false);
4555 mlx5e_build_rq_params(mdev, params);
4559 /* TODO: && MLX5_CAP_ETH(mdev, lro_cap) */
4560 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
4561 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params))
4562 params->lro_en = !slow_pci_heuristic(mdev);
4563 params->lro_timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT);
4565 /* CQ moderation params */
4566 rx_cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
4567 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
4568 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
4569 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4570 params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
4571 mlx5e_set_rx_cq_mode_params(params, rx_cq_period_mode);
4572 mlx5e_set_tx_cq_mode_params(params, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
4575 params->tx_min_inline_mode = mlx5e_params_calculate_tx_min_inline(mdev);
4578 mlx5e_build_rss_params(params);
4581 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
4583 struct mlx5e_priv *priv = netdev_priv(netdev);
4585 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
4586 if (is_zero_ether_addr(netdev->dev_addr) &&
4587 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
4588 eth_hw_addr_random(netdev);
4589 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
4593 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4594 static const struct switchdev_ops mlx5e_switchdev_ops = {
4595 .switchdev_port_attr_get = mlx5e_attr_get,
4599 static void mlx5e_build_nic_netdev(struct net_device *netdev)
4601 struct mlx5e_priv *priv = netdev_priv(netdev);
4602 struct mlx5_core_dev *mdev = priv->mdev;
4606 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
4608 netdev->netdev_ops = &mlx5e_netdev_ops;
4610 #ifdef CONFIG_MLX5_CORE_EN_DCB
4611 if (MLX5_CAP_GEN(mdev, vport_group_manager) && MLX5_CAP_GEN(mdev, qos))
4612 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
4615 netdev->watchdog_timeo = 15 * HZ;
4617 netdev->ethtool_ops = &mlx5e_ethtool_ops;
4619 netdev->vlan_features |= NETIF_F_SG;
4620 netdev->vlan_features |= NETIF_F_IP_CSUM;
4621 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
4622 netdev->vlan_features |= NETIF_F_GRO;
4623 netdev->vlan_features |= NETIF_F_TSO;
4624 netdev->vlan_features |= NETIF_F_TSO6;
4625 netdev->vlan_features |= NETIF_F_RXCSUM;
4626 netdev->vlan_features |= NETIF_F_RXHASH;
4628 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX;
4629 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX;
4631 if (!!MLX5_CAP_ETH(mdev, lro_cap) &&
4632 mlx5e_check_fragmented_striding_rq_cap(mdev))
4633 netdev->vlan_features |= NETIF_F_LRO;
4635 netdev->hw_features = netdev->vlan_features;
4636 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4637 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4638 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4639 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4641 if (mlx5_vxlan_allowed(mdev->vxlan) || MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4642 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
4643 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
4644 netdev->hw_enc_features |= NETIF_F_TSO;
4645 netdev->hw_enc_features |= NETIF_F_TSO6;
4646 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL;
4649 if (mlx5_vxlan_allowed(mdev->vxlan)) {
4650 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
4651 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4652 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
4653 NETIF_F_GSO_UDP_TUNNEL_CSUM;
4654 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
4657 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) {
4658 netdev->hw_features |= NETIF_F_GSO_GRE |
4659 NETIF_F_GSO_GRE_CSUM;
4660 netdev->hw_enc_features |= NETIF_F_GSO_GRE |
4661 NETIF_F_GSO_GRE_CSUM;
4662 netdev->gso_partial_features |= NETIF_F_GSO_GRE |
4663 NETIF_F_GSO_GRE_CSUM;
4666 netdev->hw_features |= NETIF_F_GSO_PARTIAL;
4667 netdev->gso_partial_features |= NETIF_F_GSO_UDP_L4;
4668 netdev->hw_features |= NETIF_F_GSO_UDP_L4;
4669 netdev->features |= NETIF_F_GSO_UDP_L4;
4671 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
4674 netdev->hw_features |= NETIF_F_RXALL;
4676 if (MLX5_CAP_ETH(mdev, scatter_fcs))
4677 netdev->hw_features |= NETIF_F_RXFCS;
4679 netdev->features = netdev->hw_features;
4680 if (!priv->channels.params.lro_en)
4681 netdev->features &= ~NETIF_F_LRO;
4684 netdev->features &= ~NETIF_F_RXALL;
4686 if (!priv->channels.params.scatter_fcs_en)
4687 netdev->features &= ~NETIF_F_RXFCS;
4689 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
4690 if (FT_CAP(flow_modify_en) &&
4691 FT_CAP(modify_root) &&
4692 FT_CAP(identified_miss_table_mode) &&
4693 FT_CAP(flow_table_modify)) {
4694 #ifdef CONFIG_MLX5_ESWITCH
4695 netdev->hw_features |= NETIF_F_HW_TC;
4697 #ifdef CONFIG_MLX5_EN_ARFS
4698 netdev->hw_features |= NETIF_F_NTUPLE;
4702 netdev->features |= NETIF_F_HIGHDMA;
4703 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
4705 netdev->priv_flags |= IFF_UNICAST_FLT;
4707 mlx5e_set_netdev_dev_addr(netdev);
4709 #if IS_ENABLED(CONFIG_MLX5_ESWITCH)
4710 if (MLX5_ESWITCH_MANAGER(mdev))
4711 netdev->switchdev_ops = &mlx5e_switchdev_ops;
4714 mlx5e_ipsec_build_netdev(priv);
4715 mlx5e_tls_build_netdev(priv);
4718 void mlx5e_create_q_counters(struct mlx5e_priv *priv)
4720 struct mlx5_core_dev *mdev = priv->mdev;
4723 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
4725 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
4726 priv->q_counter = 0;
4729 err = mlx5_core_alloc_q_counter(mdev, &priv->drop_rq_q_counter);
4731 mlx5_core_warn(mdev, "alloc drop RQ counter failed, %d\n", err);
4732 priv->drop_rq_q_counter = 0;
4736 void mlx5e_destroy_q_counters(struct mlx5e_priv *priv)
4738 if (priv->q_counter)
4739 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
4741 if (priv->drop_rq_q_counter)
4742 mlx5_core_dealloc_q_counter(priv->mdev, priv->drop_rq_q_counter);
4745 static int mlx5e_nic_init(struct mlx5_core_dev *mdev,
4746 struct net_device *netdev,
4747 const struct mlx5e_profile *profile,
4750 struct mlx5e_priv *priv = netdev_priv(netdev);
4753 err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
4757 mlx5e_build_nic_params(mdev, &priv->channels.params,
4758 mlx5e_get_netdev_max_channels(netdev), netdev->mtu);
4760 mlx5e_timestamp_init(priv);
4762 err = mlx5e_ipsec_init(priv);
4764 mlx5_core_err(mdev, "IPSec initialization failed, %d\n", err);
4765 err = mlx5e_tls_init(priv);
4767 mlx5_core_err(mdev, "TLS initialization failed, %d\n", err);
4768 mlx5e_build_nic_netdev(netdev);
4769 mlx5e_build_tc2txq_maps(priv);
4774 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
4776 mlx5e_tls_cleanup(priv);
4777 mlx5e_ipsec_cleanup(priv);
4778 mlx5e_netdev_cleanup(priv->netdev, priv);
4781 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
4783 struct mlx5_core_dev *mdev = priv->mdev;
4786 mlx5e_create_q_counters(priv);
4788 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
4790 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
4791 goto err_destroy_q_counters;
4794 err = mlx5e_create_indirect_rqt(priv);
4796 goto err_close_drop_rq;
4798 err = mlx5e_create_direct_rqts(priv);
4800 goto err_destroy_indirect_rqts;
4802 err = mlx5e_create_indirect_tirs(priv, true);
4804 goto err_destroy_direct_rqts;
4806 err = mlx5e_create_direct_tirs(priv);
4808 goto err_destroy_indirect_tirs;
4810 err = mlx5e_create_flow_steering(priv);
4812 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
4813 goto err_destroy_direct_tirs;
4816 err = mlx5e_tc_nic_init(priv);
4818 goto err_destroy_flow_steering;
4822 err_destroy_flow_steering:
4823 mlx5e_destroy_flow_steering(priv);
4824 err_destroy_direct_tirs:
4825 mlx5e_destroy_direct_tirs(priv);
4826 err_destroy_indirect_tirs:
4827 mlx5e_destroy_indirect_tirs(priv, true);
4828 err_destroy_direct_rqts:
4829 mlx5e_destroy_direct_rqts(priv);
4830 err_destroy_indirect_rqts:
4831 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4833 mlx5e_close_drop_rq(&priv->drop_rq);
4834 err_destroy_q_counters:
4835 mlx5e_destroy_q_counters(priv);
4839 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
4841 mlx5e_tc_nic_cleanup(priv);
4842 mlx5e_destroy_flow_steering(priv);
4843 mlx5e_destroy_direct_tirs(priv);
4844 mlx5e_destroy_indirect_tirs(priv, true);
4845 mlx5e_destroy_direct_rqts(priv);
4846 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
4847 mlx5e_close_drop_rq(&priv->drop_rq);
4848 mlx5e_destroy_q_counters(priv);
4851 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
4855 err = mlx5e_create_tises(priv);
4857 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
4861 #ifdef CONFIG_MLX5_CORE_EN_DCB
4862 mlx5e_dcbnl_initialize(priv);
4867 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
4869 struct net_device *netdev = priv->netdev;
4870 struct mlx5_core_dev *mdev = priv->mdev;
4873 mlx5e_init_l2_addr(priv);
4875 /* Marking the link as currently not needed by the Driver */
4876 if (!netif_running(netdev))
4877 mlx5_set_port_admin_status(mdev, MLX5_PORT_DOWN);
4879 /* MTU range: 68 - hw-specific max */
4880 netdev->min_mtu = ETH_MIN_MTU;
4881 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
4882 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
4883 mlx5e_set_dev_port_mtu(priv);
4885 mlx5_lag_add(mdev, netdev);
4887 mlx5e_enable_async_events(priv);
4889 if (MLX5_ESWITCH_MANAGER(priv->mdev))
4890 mlx5e_register_vport_reps(priv);
4892 if (netdev->reg_state != NETREG_REGISTERED)
4894 #ifdef CONFIG_MLX5_CORE_EN_DCB
4895 mlx5e_dcbnl_init_app(priv);
4898 queue_work(priv->wq, &priv->set_rx_mode_work);
4901 if (netif_running(netdev))
4903 netif_device_attach(netdev);
4907 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
4909 struct mlx5_core_dev *mdev = priv->mdev;
4911 #ifdef CONFIG_MLX5_CORE_EN_DCB
4912 if (priv->netdev->reg_state == NETREG_REGISTERED)
4913 mlx5e_dcbnl_delete_app(priv);
4917 if (netif_running(priv->netdev))
4918 mlx5e_close(priv->netdev);
4919 netif_device_detach(priv->netdev);
4922 queue_work(priv->wq, &priv->set_rx_mode_work);
4924 if (MLX5_ESWITCH_MANAGER(priv->mdev))
4925 mlx5e_unregister_vport_reps(priv);
4927 mlx5e_disable_async_events(priv);
4928 mlx5_lag_remove(mdev);
4931 static const struct mlx5e_profile mlx5e_nic_profile = {
4932 .init = mlx5e_nic_init,
4933 .cleanup = mlx5e_nic_cleanup,
4934 .init_rx = mlx5e_init_nic_rx,
4935 .cleanup_rx = mlx5e_cleanup_nic_rx,
4936 .init_tx = mlx5e_init_nic_tx,
4937 .cleanup_tx = mlx5e_cleanup_nic_tx,
4938 .enable = mlx5e_nic_enable,
4939 .disable = mlx5e_nic_disable,
4940 .update_stats = mlx5e_update_ndo_stats,
4941 .update_carrier = mlx5e_update_carrier,
4942 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe,
4943 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
4944 .max_tc = MLX5E_MAX_NUM_TC,
4947 /* mlx5e generic netdev management API (move to en_common.c) */
4949 /* mlx5e_netdev_init/cleanup must be called from profile->init/cleanup callbacks */
4950 int mlx5e_netdev_init(struct net_device *netdev,
4951 struct mlx5e_priv *priv,
4952 struct mlx5_core_dev *mdev,
4953 const struct mlx5e_profile *profile,
4958 priv->netdev = netdev;
4959 priv->profile = profile;
4960 priv->ppriv = ppriv;
4961 priv->msglevel = MLX5E_MSG_LEVEL;
4962 priv->max_opened_tc = 1;
4964 mutex_init(&priv->state_lock);
4965 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
4966 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
4967 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
4968 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
4970 priv->wq = create_singlethread_workqueue("mlx5e");
4975 netif_carrier_off(netdev);
4977 #ifdef CONFIG_MLX5_EN_ARFS
4978 netdev->rx_cpu_rmap = mdev->rmap;
4984 void mlx5e_netdev_cleanup(struct net_device *netdev, struct mlx5e_priv *priv)
4986 destroy_workqueue(priv->wq);
4989 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
4990 const struct mlx5e_profile *profile,
4994 struct net_device *netdev;
4997 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
4998 nch * profile->max_tc,
5001 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
5005 err = profile->init(mdev, netdev, profile, ppriv);
5007 mlx5_core_err(mdev, "failed to init mlx5e profile %d\n", err);
5008 goto err_free_netdev;
5014 free_netdev(netdev);
5019 int mlx5e_attach_netdev(struct mlx5e_priv *priv)
5021 const struct mlx5e_profile *profile;
5025 profile = priv->profile;
5026 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
5028 /* max number of channels may have changed */
5029 max_nch = mlx5e_get_max_num_channels(priv->mdev);
5030 if (priv->channels.params.num_channels > max_nch) {
5031 mlx5_core_warn(priv->mdev, "MLX5E: Reducing number of channels to %d\n", max_nch);
5032 priv->channels.params.num_channels = max_nch;
5033 mlx5e_build_default_indir_rqt(priv->channels.params.indirection_rqt,
5034 MLX5E_INDIR_RQT_SIZE, max_nch);
5037 err = profile->init_tx(priv);
5041 err = profile->init_rx(priv);
5043 goto err_cleanup_tx;
5045 if (profile->enable)
5046 profile->enable(priv);
5051 profile->cleanup_tx(priv);
5057 void mlx5e_detach_netdev(struct mlx5e_priv *priv)
5059 const struct mlx5e_profile *profile = priv->profile;
5061 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
5063 if (profile->disable)
5064 profile->disable(priv);
5065 flush_workqueue(priv->wq);
5067 profile->cleanup_rx(priv);
5068 profile->cleanup_tx(priv);
5069 cancel_work_sync(&priv->update_stats_work);
5072 void mlx5e_destroy_netdev(struct mlx5e_priv *priv)
5074 const struct mlx5e_profile *profile = priv->profile;
5075 struct net_device *netdev = priv->netdev;
5077 if (profile->cleanup)
5078 profile->cleanup(priv);
5079 free_netdev(netdev);
5082 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
5083 * hardware contexts and to connect it to the current netdev.
5085 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
5087 struct mlx5e_priv *priv = vpriv;
5088 struct net_device *netdev = priv->netdev;
5091 if (netif_device_present(netdev))
5094 err = mlx5e_create_mdev_resources(mdev);
5098 err = mlx5e_attach_netdev(priv);
5100 mlx5e_destroy_mdev_resources(mdev);
5107 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
5109 struct mlx5e_priv *priv = vpriv;
5110 struct net_device *netdev = priv->netdev;
5112 if (!netif_device_present(netdev))
5115 mlx5e_detach_netdev(priv);
5116 mlx5e_destroy_mdev_resources(mdev);
5119 static void *mlx5e_add(struct mlx5_core_dev *mdev)
5121 struct net_device *netdev;
5127 err = mlx5e_check_required_hca_cap(mdev);
5131 #ifdef CONFIG_MLX5_ESWITCH
5132 if (MLX5_ESWITCH_MANAGER(mdev)) {
5133 rpriv = mlx5e_alloc_nic_rep_priv(mdev);
5135 mlx5_core_warn(mdev, "Failed to alloc NIC rep priv data\n");
5141 nch = mlx5e_get_max_num_channels(mdev);
5142 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, nch, rpriv);
5144 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
5145 goto err_free_rpriv;
5148 priv = netdev_priv(netdev);
5150 err = mlx5e_attach(mdev, priv);
5152 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
5153 goto err_destroy_netdev;
5156 err = register_netdev(netdev);
5158 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
5162 #ifdef CONFIG_MLX5_CORE_EN_DCB
5163 mlx5e_dcbnl_init_app(priv);
5168 mlx5e_detach(mdev, priv);
5170 mlx5e_destroy_netdev(priv);
5176 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
5178 struct mlx5e_priv *priv = vpriv;
5179 void *ppriv = priv->ppriv;
5181 #ifdef CONFIG_MLX5_CORE_EN_DCB
5182 mlx5e_dcbnl_delete_app(priv);
5184 unregister_netdev(priv->netdev);
5185 mlx5e_detach(mdev, vpriv);
5186 mlx5e_destroy_netdev(priv);
5190 static void *mlx5e_get_netdev(void *vpriv)
5192 struct mlx5e_priv *priv = vpriv;
5194 return priv->netdev;
5197 static struct mlx5_interface mlx5e_interface = {
5199 .remove = mlx5e_remove,
5200 .attach = mlx5e_attach,
5201 .detach = mlx5e_detach,
5202 .event = mlx5e_async_event,
5203 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
5204 .get_dev = mlx5e_get_netdev,
5207 void mlx5e_init(void)
5209 mlx5e_ipsec_build_inverse_table();
5210 mlx5e_build_ptys2ethtool_map();
5211 mlx5_register_interface(&mlx5e_interface);
5214 void mlx5e_cleanup(void)
5216 mlx5_unregister_interface(&mlx5e_interface);